forked from CalculusWithJulia/CalculusWithJulia.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
julia_overview.html
1963 lines (1767 loc) · 66.9 KB
/
julia_overview.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css"
rel="stylesheet">
<style>
.julia {display: block; font-family: "Source Code Pro";
color:#0033CC;
}
.hljl {font-family: "Source Code Pro";
color:#0033CC;
}
body { padding-top: 60px; }
h5:before {content:"\2746\ ";}
h6:before {content:"\2742\ ";}
pre {display: block;}
th, td {
padding: 15px;
text-align: left;
border-bottom: 1px solid #ddd;
}
tr:hover {background-color: #f5f5f5;}
.admonition-title:before {content:"\2746\ ";}
.admonition-title { color:#0033CC}
</style>
<!-- .julia:before {content: "julia> "} -->
<style></style>
<script src="https://code.jquery.com/jquery.js"></script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
tex2jax: {
inlineMath: [ ["\$","\$"], ["\\(","\\)"]]
},
displayAlign: "left",
displayIndent: "5%"
});
</script>
<!-- not TeX-AMS-MML_HTMLorMML-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML" async></script>
</script>
<script>
window.PlotlyConfig = {MathJaxConfig: 'local'}
</script>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<script type="text/javascript">
$( document ).ready(function() {
$("h1").each(function(index) {
var title = $( this ).text()
$("#page_title").html("<strong>" + title + "</strong>");
document.title = title
});
$( "h2" ).each(function( index ) {
var nm = $( this ).text();
var id = $.trim(nm).replace(/ /g,'');
this.id = id
$("#page_dropdown").append("<li><a href='#" + id + "'>" + nm + "</a></li>");
});
$('[data-toggle="popover"]').popover();
});
</script>
</head>
<body data-spy="scroll" >
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li><a href="#" id="page_title"></a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
Jump to... <span class="caret"></span></a>
<ul class="dropdown-menu" role="menu" id="page_dropdown"></ul>
</li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
<header>
</header>
<div class="title">
</div>
<div class="container-fluid">
<div class="span10 offset1">
<h1>Overview of Julia commands</h1>
<p><a href="http://www.julialang.org">Julia</a> is a programming language that is freely available.</p>
<p><a href="https://mybinder.org/v2/gh/CalculusWithJulia/CwJScratchPad.git/master"><img src="https://mybinder.org/badge_logo.svg" alt="Binder" /></a></p>
<p>We can use <code>Julia</code> without installation by clicking the "launch Binder" badge. The notebook <code>CalculusWithJulia.ipynb</code> is a blank notebook except for the command to load the <code>CalculusWithJulia</code> package.</p>
<p>Alternatively, the <a href="https://www.juliabox.com">juliabox.com</a> service provides access to <code>Julia</code> through the internet, though the free access is slated to be discontinued. Both use the <a href="https://jupyter.org/">Jupyter</a> notebook interface, which we will assume, though many other means of interacting with <code>Julia</code> are available.</p>
<h2>Commands</h2>
<p>Commands are typed into a notebook cell in <code>Jupyter</code>. (Or at the command line.)</p>
<pre class='hljl'>
<span class='hljl-ni'>2</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-ni'>2</span><span class='hljl-t'> </span><span class='hljl-cs'># use shift-enter to evaluate</span>
</pre>
<pre class="output">
4
</pre>
<p>Commands are executed by using <code>shift-enter</code> or the play button in <code>Jupyter</code>.</p>
<p>Commands may be separated by new lines or semicolons, allowing multiple commands per cell.</p>
<p>On a line, anything after a <code>#</code> is a <em>comment</em>.</p>
<p>The results of the last line executed will be displayed in an output area. Separating values by commas allows more than one value to be displayed. Explicit printing can be done to output intermediate values.</p>
<h2>Numbers, variable types</h2>
<p><code>Julia</code> has many different number types beyond the floating point type employed by most calculators. These include</p>
<ul>
<li><p>Floating point numbers: <code>0.5</code></p>
</li>
<li><p>Integers: <code>2</code></p>
</li>
<li><p>Rational numbers: <code>1//2</code></p>
</li>
<li><p>Complex numbers <code>2 + 0im</code></p>
</li>
</ul>
<p>As much as possible, operations involving certain types of numbers will produce output of a given type. For example, both of these divisions produce a floating point answer, even though mathematically, they need not:</p>
<pre class='hljl'>
<span class='hljl-ni'>2</span><span class='hljl-oB'>/</span><span class='hljl-ni'>1</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-ni'>1</span><span class='hljl-oB'>/</span><span class='hljl-ni'>2</span>
</pre>
<pre class="output">
(2.0, 0.5)
</pre>
<p>Some operations won't work with integer types, but will with floating point types, as the type of output can't be assured.</p>
<p>Powers with negative bases, like <code>(-3.0)^(1/3)</code>, are not defined. However, <code>Julia</code> provides the special-case function <code>cbrt</code> (and <code>sqrt</code>) for handling these.</p>
<p>Integer operations may silently overflow, producing odd answers, at first glance:</p>
<pre class='hljl'>
<span class='hljl-ni'>2</span><span class='hljl-oB'>^</span><span class='hljl-ni'>64</span>
</pre>
<pre class="output">
0
</pre>
<p>(Though the output is predictable, if overflow is taken into consideration appropriateley.)</p>
<p>When different types of numbers are mixed, <code>Julia</code> will usually promote the values to a common type before the operation:</p>
<pre class='hljl'>
<span class='hljl-p'>(</span><span class='hljl-ni'>2</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-ni'>1</span><span class='hljl-oB'>//</span><span class='hljl-ni'>2</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-nfB'>0.5</span>
</pre>
<pre class="output">
3.0
</pre>
<p><code>Julia</code> will first add <code>2</code> and <code>1//2</code> converting <code>2</code> to rational before doing so. Then add the result, <code>5//2</code> to <code>0.5</code> by promoting <code>5//2</code> to the floating point number <code>2.5</code> before proceeding.</p>
<p><code>Julia</code> uses a special type to store a handful of constants, of which both <code>pi</code> and <code>e</code> are used here. Theh special type allows these to be treated without round off, until they mix with other floating point numbers. There are some functions that require these be explicitly promoted to floating point. This can be done by calling <code>float</code>.</p>
<p>The standard mathematical operations are implemented by <code>+</code>, <code>-</code>, <code>*</code>, <code>/</code>, <code>^</code>. Parentheses are used for grouping.</p>
<h3>Vectors</h3>
<p>A vector is an indexed collection of similarly typed values. Vectors can be constructed with square brackets (syntax for concatenation):</p>
<pre class='hljl'>
<span class='hljl-p'>[</span><span class='hljl-ni'>1</span><span class='hljl-p'>,</span><span class='hljl-ni'>1</span><span class='hljl-p'>,</span><span class='hljl-ni'>2</span><span class='hljl-p'>,</span><span class='hljl-ni'>3</span><span class='hljl-p'>,</span><span class='hljl-ni'>5</span><span class='hljl-p'>,</span><span class='hljl-ni'>8</span><span class='hljl-p'>]</span>
</pre>
<pre class="output">
6-element Array{Int64,1}:
1
1
2
3
5
8
</pre>
<p>(Vectors are used as a return type so some familiarity is needed.)</p>
<p>Regular arithmetic sequences can be defined by either:</p>
<ul>
<li><p>Range operations: <code>a:h:b</code> or <code>a:b</code> which produces a generator of values starting at <code>a</code> separated by <code>h</code> (<code>h</code> is <code>1</code> in the last form) until they reach <code>b</code>.</p>
</li>
<li><p><code>range(a, b, length=n)</code> which produces a generator of <code>n</code> values between <code>a</code> and <code>b</code>; (<code>range(a, stop=b, length=n)</code> is needed for version <code>v1.0.0</code> of `Julia.)</p>
</li>
</ul>
<p>These constructs return range objects. A range object <em>compactly</em> stores the values it references. To see all the values, they can be collected with the <code>collect</code> function, though this is hardly needed in practice.</p>
<h2>Variables</h2>
<p>Values can be assigned variable names, with <code>=</code>. There are some variants</p>
<pre class='hljl'>
<span class='hljl-n'>x</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-ni'>2</span><span class='hljl-t'>
</span><span class='hljl-n'>a_really_long_name</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-ni'>3</span><span class='hljl-t'>
</span><span class='hljl-n'>a</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-n'>b</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-ni'>1</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-ni'>2</span><span class='hljl-t'> </span><span class='hljl-cs'># multiple assignment</span><span class='hljl-t'>
</span><span class='hljl-n'>a1</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-n'>a2</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-ni'>0</span><span class='hljl-t'> </span><span class='hljl-cs'># chained assignment, sets a2 and a1 to 0</span>
</pre>
<pre class="output">
0
</pre>
<p>The names can be short, as above, or more verbose. They can't start with a number, but can include numbers. It can also be a fancy <a href="../unicode.html">unicode</a> or even an emoji.</p>
<p>We can then use the variables to reference the values:</p>
<pre class='hljl'>
<span class='hljl-n'>x</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-n'>a_really_long_name</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-n'>a</span><span class='hljl-t'> </span><span class='hljl-oB'>-</span><span class='hljl-t'> </span><span class='hljl-n'>b</span>
</pre>
<pre class="output">
4
</pre>
<p>Names may be repurposed, even with values of different types (a dynamic language), save for function names, which have some special rules and can only be redefined as an another function. (Generic functions are central to <code>Julia</code>'s design. Generic functions use a method table to dispatch on, so once a name is assigned to a generic function, it can not be used as a variable name; the reverse is also true.</p>
<h2>Functions</h2>
<p>Functions in <code>Julia</code> are just regular objects. In these notes, we often pass them as arguments to other functions. There are many built-in functions and it is easy to define new functions.</p>
<p>We "call" a function by passing argument(s) to it, grouped by parentheses:</p>
<pre class='hljl'>
<span class='hljl-nf'>sqrt</span><span class='hljl-p'>(</span><span class='hljl-ni'>10</span><span class='hljl-p'>)</span><span class='hljl-t'>
</span><span class='hljl-nf'>sin</span><span class='hljl-p'>(</span><span class='hljl-n'>pi</span><span class='hljl-oB'>/</span><span class='hljl-ni'>3</span><span class='hljl-p'>)</span><span class='hljl-t'>
</span><span class='hljl-nf'>log</span><span class='hljl-p'>(</span><span class='hljl-ni'>5</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-ni'>100</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-cs'># log base 5 of 100</span>
</pre>
<pre class="output">
2.8613531161467867
</pre>
<p>With out parentheses, the name (usually) refers to a generic name and the output lists the number of available implementations.</p>
<pre class='hljl'>
<span class='hljl-n'>log</span>
</pre>
<pre class="output">
log (generic function with 36 methods)
</pre>
<h3>Built-in functions</h3>
<p><code>Julia</code> has numerous built-in <a href="http://julia.readthedocs.io/">mathematical</a> functions, we review a few here:</p>
<h4>Powers logs and roots</h4>
<p>Besides <code>^</code>, there are <code>sqrt</code> and <code>cbrt</code> for powers. In addition basic functions for exponential and logarithmic functions:</p>
<pre><code>sqrt(x), cbrt(x)
exp(x)
log(x) # base e
log10(x), log2(x), log(b, x)</code></pre>
<h4>Trigonometric functions</h4>
<p>The <span class="math">$6$</span> standard trig functions are implemented; their implementation for degree arguments; their inverse functions; and the hyperbolic analogs.</p>
<pre><code>sin, cos, tan, csc, sec, cot
asin, acos, atan, acsc, asec, acot
sinh, cosh, tanh, csch, sech, coth
asinh, acosh, atanh, acsch, asech, acoth</code></pre>
<p>If degrees are preferred, the following are defined to work with degrees:</p>
<pre><code>sind, cosd, tand, cscd, secd, cotd</code></pre>
<h4>Useful functions</h4>
<p>Other useful and familiar functions are defined:</p>
<ul>
<li><p><code>abs(x)</code>: absolute value</p>
</li>
<li><p><code>sign(x)</code>: is <span class="math">$\lvert x \rvert/x$</span> except at <span class="math">$x=0$</span>, where it is <span class="math">$0$</span>.</p>
</li>
<li><p><code>floor(x)</code>, <code>ceil(x)</code>: greatest integer less or least integer greater</p>
</li>
<li><p><code>max(a,b)</code>, <code>min(a,b)</code>: larger (or smaller) of <code>a</code> or <code>b</code></p>
</li>
<li><p><code>maximum(xs)</code>, <code>minimum(xs)</code>: largest or smallest of the collection referred to by <code>xs</code></p>
</li>
</ul>
<h3>User-defined functions</h3>
<p>Simple mathematical functions can be defined using standard mathematical notation:</p>
<pre class='hljl'>
<span class='hljl-nf'>f</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-oB'>-</span><span class='hljl-ni'>16</span><span class='hljl-n'>x</span><span class='hljl-oB'>^</span><span class='hljl-ni'>2</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-ni'>100</span><span class='hljl-n'>x</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-ni'>2</span>
</pre>
<pre class="output">
f (generic function with 1 method)
</pre>
<p>The argument <code>x</code> is passed into the body of function.</p>
<p>Other values are found from the environment where defined:</p>
<pre class='hljl'>
<span class='hljl-n'>a</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-ni'>1</span><span class='hljl-t'>
</span><span class='hljl-nf'>f</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-ni'>2</span><span class='hljl-oB'>*</span><span class='hljl-n'>a</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-n'>x</span><span class='hljl-t'>
</span><span class='hljl-nf'>f</span><span class='hljl-p'>(</span><span class='hljl-ni'>3</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-cs'># 2 * 1 + 3</span><span class='hljl-t'>
</span><span class='hljl-n'>a</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-ni'>4</span><span class='hljl-t'>
</span><span class='hljl-nf'>f</span><span class='hljl-p'>(</span><span class='hljl-ni'>3</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-cs'># now 2 * 4 + 3</span>
</pre>
<pre class="output">
11
</pre>
<p>User defined functions can have 0, 1 or more arguments:</p>
<pre class='hljl'>
<span class='hljl-nf'>area</span><span class='hljl-p'>(</span><span class='hljl-n'>w</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-n'>h</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-n'>w</span><span class='hljl-oB'>*</span><span class='hljl-n'>h</span>
</pre>
<pre class="output">
area (generic function with 1 method)
</pre>
<p>Julia makes different <em>methods</em> for <em>generic</em> function names, so functions whose argument specification is different are different functions, even if the name is the same. This is <em>polymorphism</em>. The practical use is that it means users need only remember a much smaller set of function names.</p>
<p>Functions can be defined with <em>keyword</em> arguments that may have defaults specified:</p>
<pre class='hljl'>
<span class='hljl-nf'>f</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-p'>;</span><span class='hljl-t'> </span><span class='hljl-n'>m</span><span class='hljl-oB'>=</span><span class='hljl-ni'>1</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-n'>b</span><span class='hljl-oB'>=</span><span class='hljl-ni'>0</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-n'>m</span><span class='hljl-oB'>*</span><span class='hljl-n'>x</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-n'>b</span><span class='hljl-t'> </span><span class='hljl-cs'># note ";"</span><span class='hljl-t'>
</span><span class='hljl-nf'>f</span><span class='hljl-p'>(</span><span class='hljl-ni'>1</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-cs'># uses m=1, b=0 -> 1 * 1 + 0</span><span class='hljl-t'>
</span><span class='hljl-nf'>f</span><span class='hljl-p'>(</span><span class='hljl-ni'>1</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-n'>m</span><span class='hljl-oB'>=</span><span class='hljl-ni'>10</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-cs'># uses m=10, b=0 -> 10 * 1 + 0</span><span class='hljl-t'>
</span><span class='hljl-nf'>f</span><span class='hljl-p'>(</span><span class='hljl-ni'>1</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-n'>m</span><span class='hljl-oB'>=</span><span class='hljl-ni'>10</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-n'>b</span><span class='hljl-oB'>=</span><span class='hljl-ni'>5</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-cs'># uses m=10, b=5 -> 10 * 1 + 5</span>
</pre>
<pre class="output">
15
</pre>
<p>Longer functions can be defined using the <code>function</code> keyword, the last command executed is returned:</p>
<pre class='hljl'>
<span class='hljl-k'>function</span><span class='hljl-t'> </span><span class='hljl-nf'>f</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-p'>)</span><span class='hljl-t'>
</span><span class='hljl-n'>y</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-n'>x</span><span class='hljl-oB'>^</span><span class='hljl-ni'>2</span><span class='hljl-t'>
</span><span class='hljl-n'>z</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-n'>y</span><span class='hljl-t'> </span><span class='hljl-oB'>-</span><span class='hljl-t'> </span><span class='hljl-ni'>3</span><span class='hljl-t'>
</span><span class='hljl-n'>z</span><span class='hljl-t'>
</span><span class='hljl-k'>end</span>
</pre>
<pre class="output">
f (generic function with 1 method)
</pre>
<p>Functions without names, <em>anonymous functions</em>, are made with the <code>-></code> syntax as in:</p>
<pre class='hljl'>
<span class='hljl-n'>x</span><span class='hljl-t'> </span><span class='hljl-oB'>-></span><span class='hljl-t'> </span><span class='hljl-nf'>cos</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-p'>)</span><span class='hljl-oB'>^</span><span class='hljl-ni'>2</span><span class='hljl-t'> </span><span class='hljl-oB'>-</span><span class='hljl-t'> </span><span class='hljl-nf'>cos</span><span class='hljl-p'>(</span><span class='hljl-ni'>2</span><span class='hljl-n'>x</span><span class='hljl-p'>)</span>
</pre>
<pre class="output">
#2 (generic function with 1 method)
</pre>
<p>These are useful when passing a function to another function or when writing a function that <em>returns</em> a function.</p>
<h2>Conditional statements</h2>
<p><code>Julia</code> provides the traditional <code>if-else-end</code> statements, but more conveniently has a <code>ternary</code> operator for the simplest case:</p>
<pre class='hljl'>
<span class='hljl-nf'>our_abs</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-t'> </span><span class='hljl-oB'><</span><span class='hljl-t'> </span><span class='hljl-ni'>0</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>?</span><span class='hljl-t'> </span><span class='hljl-oB'>-</span><span class='hljl-n'>x</span><span class='hljl-t'> </span><span class='hljl-oB'>:</span><span class='hljl-t'> </span><span class='hljl-n'>x</span>
</pre>
<pre class="output">
our_abs (generic function with 1 method)
</pre>
<h2>Looping</h2>
<p>Iterating over a collection can be done with the traditional <code>for</code> loop. However, there are list comprehensions to mimic the definition of a set:</p>
<pre class='hljl'>
<span class='hljl-p'>[</span><span class='hljl-n'>x</span><span class='hljl-oB'>^</span><span class='hljl-ni'>2</span><span class='hljl-t'> </span><span class='hljl-k'>for</span><span class='hljl-t'> </span><span class='hljl-n'>x</span><span class='hljl-t'> </span><span class='hljl-kp'>in</span><span class='hljl-t'> </span><span class='hljl-ni'>1</span><span class='hljl-oB'>:</span><span class='hljl-ni'>10</span><span class='hljl-p'>]</span>
</pre>
<pre class="output">
10-element Array{Int64,1}:
1
4
9
16
25
36
49
64
81
100
</pre>
<h2>Broadcasting, mapping</h2>
<p>A function can be applied to each element of a vector through mapping or broadcasting. The latter is implemented in a succinct notation. Calling a function with a "." before its opening "(` will apply the function to each individual value in the argument:</p>
<pre class='hljl'>
<span class='hljl-n'>xs</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-p'>[</span><span class='hljl-ni'>1</span><span class='hljl-p'>,</span><span class='hljl-ni'>2</span><span class='hljl-p'>,</span><span class='hljl-ni'>3</span><span class='hljl-p'>,</span><span class='hljl-ni'>4</span><span class='hljl-p'>,</span><span class='hljl-ni'>5</span><span class='hljl-p'>]</span><span class='hljl-t'>
</span><span class='hljl-n'>sin</span><span class='hljl-oB'>.</span><span class='hljl-p'>(</span><span class='hljl-n'>xs</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-cs'># gives back [sin(1), sin(2), sin(3), sin(4), sin(5)]</span>
</pre>
<pre class="output">
5-element Array{Float64,1}:
0.8414709848078965
0.9092974268256817
0.1411200080598672
-0.7568024953079282
-0.9589242746631385
</pre>
<h2>Plotting</h2>
<p>Plotting is <em>not</em> built-in to <code>Julia</code>, rather added through add-on packages. <code>Julia</code>'s <code>Plots</code> package is an interface to several plotting packages. We mention <code>plotly</code> (built-in) for web based graphics, and <code>gr</code> for other graphics.</p>
<p>To use an add-on package, it must have been installed and it must be loaded each session. In these notes, the necessary packages are all loaded when an accompanying package, <code>CalculusWithJulia</code> is loaded. Assuming it has been installed, this command will do so:</p>
<pre class='hljl'>
<span class='hljl-k'>using</span><span class='hljl-t'> </span><span class='hljl-n'>CalculusWithJulia</span>
</pre>
<p>With <code>Plots</code> loaded, we can plot a function by passing the function object by name to <code>plot</code>, specifying the range of <span class="math">$x$</span> values to show, as follows:</p>
<pre class='hljl'>
<span class='hljl-nf'>plot</span><span class='hljl-p'>(</span><span class='hljl-n'>sin</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-ni'>0</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-ni'>2</span><span class='hljl-n'>pi</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-cs'># plot a function---by name---over an interval [a,b]</span>
</pre>
<div id="53b3b71b-b7aa-490a-b3d8-41983957053f" style="width:576px;height:384px;"></div>
<script>
PLOT = document.getElementById('53b3b71b-b7aa-490a-b3d8-41983957053f');
Plotly.plot(PLOT, [
{
"xaxis": "x1",
"colorbar": {
"title": ""
},
"yaxis": "y1",
"text": [
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null
],
"x": [
0.030680225288853866,
0.06136045057770773,
0.34504343394708514,
0.6287264173164625,
0.7992080658377971,
0.9696897143591319,
1.0417597464278976,
1.113829778496663,
1.1858998105654286,
1.2579698426341943,
1.4145463533885865,
1.5711228641429789,
1.7154725576207572,
1.8598222510985358,
2.0145803783513556,
2.1693385056041756,
2.3486409728203155,
2.5279434400364558,
2.6872863130479567,
2.846629186059458,
2.986358526427849,
3.1260878667962397,
3.27843035510683,
3.43077284341742,
3.5840207449586465,
3.7372686464998734,
3.906703798257779,
4.076138950015684,
4.2373522215721,
4.398565493128515,
4.482377691684766,
4.566189890241017,
4.650002088797269,
4.733814287353519,
4.874918324975402,
5.016022362597284,
5.188294399061325,
5.360566435525367,
5.522507230894133,
5.684448026262899,
5.943889738480623,
6.203331450698347,
6.243258378938966
],
"showlegend": true,
"mode": "lines",
"name": "y1",
"zmin": -0.1,
"legendgroup": "y1",
"zmax": 0.1,
"line": {
"color": "rgba(0, 154, 250, 1.000)",
"shape": "linear",
"dash": "solid",
"width": 1
},
"y": [
0.03067541242093728,
0.061321953070307515,
0.33823755155018476,
0.5881151905779408,
0.7168041201646171,
0.8247102693051217,
0.8632937107664949,
0.8973950679428847,
0.9268372919861522,
0.9514675235298343,
0.9878177879548484,
0.9999999466866806,
0.989552636203005,
0.9585219599793448,
0.9031334125880434,
0.8261578853619512,
0.7124277016382363,
0.5758547097527327,
0.43883912087112936,
0.29070489558439705,
0.15461141539348522,
0.015504165579995984,
-0.13641106359642388,
-0.28516655102344535,
-0.42813502702217854,
-0.5610684489029837,
-0.6926171720883418,
-0.8043295777666695,
-0.8892759098040912,
-0.9511602238189567,
-0.9736638213734828,
-0.9893319351014062,
-0.9980545689901091,
-0.999770486890561,
-0.9868211552281166,
-0.9542564477114475,
-0.8888782743946314,
-0.7971854551477641,
-0.6894127808039027,
-0.5635998564911867,
-0.3328229035580867,
-0.07976901700741997,
-0.03991632077017164
],
"type": "scatter",
"hoverinfo": "text"
}
]
, {
"showlegend": true,
"xaxis": {
"showticklabels": true,
"gridwidth": 0.5,
"tickvals": [
0.0,
1.0,
2.0,
3.0,
4.0,
5.0,
6.0
],
"visible": true,
"ticks": "inside",
"range": [
-0.1556971193206495,
6.429635723548469
],
"domain": [
0.06856347817633907,
0.9931649168853893
],
"tickmode": "array",
"linecolor": "rgba(0, 0, 0, 1.000)",
"showgrid": true,
"title": "",
"mirror": false,
"tickangle": 0,
"showline": true,
"gridcolor": "rgba(0, 0, 0, 0.100)",
"titlefont": {
"color": "rgba(0, 0, 0, 1.000)",
"family": "sans-serif",
"size": 15
},
"tickcolor": "rgb(0, 0, 0)",
"ticktext": [
"0",
"1",
"2",
"3",
"4",
"5",
"6"
],
"zeroline": false,
"type": "-",
"tickfont": {
"color": "rgba(0, 0, 0, 1.000)",
"family": "sans-serif",
"size": 11
},
"zerolinecolor": "rgba(0, 0, 0, 1.000)",
"anchor": "y1"
},
"paper_bgcolor": "rgba(255, 255, 255, 1.000)",
"annotations": [],
"height": 384,
"margin": {
"l": 0,
"b": 20,
"r": 0,
"t": 20
},
"plot_bgcolor": "rgba(255, 255, 255, 1.000)",
"yaxis": {
"showticklabels": true,
"gridwidth": 0.5,
"tickvals": [
-1.0,
-0.5,
0.0,
0.5,
1.0
],
"visible": true,
"ticks": "inside",
"range": [
-1.0597635998978783,
1.0599930596939977
],
"domain": [
0.0391878098571012,
0.989747375328084
],
"tickmode": "array",
"linecolor": "rgba(0, 0, 0, 1.000)",
"showgrid": true,
"title": "",
"mirror": false,
"tickangle": 0,
"showline": true,
"gridcolor": "rgba(0, 0, 0, 0.100)",
"titlefont": {
"color": "rgba(0, 0, 0, 1.000)",
"family": "sans-serif",
"size": 15
},
"tickcolor": "rgb(0, 0, 0)",
"ticktext": [
"-1.0",
"-0.5",
"0.0",
"0.5",
"1.0"
],
"zeroline": false,
"type": "-",
"tickfont": {
"color": "rgba(0, 0, 0, 1.000)",
"family": "sans-serif",
"size": 11
},
"zerolinecolor": "rgba(0, 0, 0, 1.000)",
"anchor": "x1"
},
"legend": {
"tracegroupgap": 0,
"bordercolor": "rgba(0, 0, 0, 1.000)",
"bgcolor": "rgba(255, 255, 255, 1.000)",
"font": {
"color": "rgba(0, 0, 0, 1.000)",
"family": "sans-serif",
"size": 11
},
"y": 1.0,
"x": 1.0
},
"width": 576
}
);
</script>
<div class="alert alert-info" role="alert">
<div class="markdown"><p>This is in the form of <strong>the</strong> basic pattern employed: <code>verb(function_object, arguments...)</code>. The verb in this example is <code>plot</code>, the object <code>sin</code>, the arguments <code>0, 2pi</code> to specify <code>[a,b]</code> domain to plot over.</p>
</div>
</div>
<p>Plotting more than one function over <span class="math">$[a,b]$</span> is achieved through the <code>plot!</code> function, which modifies the existing plot (<code>plot</code> creates a new one):</p>
<pre class='hljl'>
<span class='hljl-nf'>plot</span><span class='hljl-p'>(</span><span class='hljl-n'>sin</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-ni'>0</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-ni'>2</span><span class='hljl-n'>pi</span><span class='hljl-p'>)</span><span class='hljl-t'>
</span><span class='hljl-nf'>plot!</span><span class='hljl-p'>(</span><span class='hljl-n'>cos</span><span class='hljl-p'>)</span><span class='hljl-t'>
</span><span class='hljl-nf'>plot!</span><span class='hljl-p'>(</span><span class='hljl-n'>zero</span><span class='hljl-p'>)</span>
</pre>
<div id="264a8b23-3f80-429e-982f-a483adaa6e6d" style="width:576px;height:384px;"></div>
<script>
PLOT = document.getElementById('264a8b23-3f80-429e-982f-a483adaa6e6d');
Plotly.plot(PLOT, [
{
"xaxis": "x1",
"colorbar": {
"title": ""
},
"yaxis": "y1",
"text": [
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null
],
"x": [
0.030680225288853866,
0.06136045057770773,
0.34504343394708514,
0.6287264173164625,
0.7992080658377971,
0.9696897143591319,
1.0417597464278976,
1.113829778496663,
1.1858998105654286,
1.2579698426341943,
1.4145463533885865,
1.5711228641429789,
1.7154725576207572,
1.8598222510985358,
2.0145803783513556,
2.1693385056041756,
2.3486409728203155,
2.5279434400364558,
2.6872863130479567,
2.846629186059458,
2.986358526427849,
3.1260878667962397,
3.27843035510683,
3.43077284341742,
3.5840207449586465,
3.7372686464998734,
3.906703798257779,
4.076138950015684,
4.2373522215721,
4.398565493128515,
4.482377691684766,
4.566189890241017,
4.650002088797269,
4.733814287353519,
4.874918324975402,
5.016022362597284,
5.188294399061325,
5.360566435525367,
5.522507230894133,
5.684448026262899,
5.943889738480623,
6.203331450698347,
6.243258378938966
],
"showlegend": true,
"mode": "lines",
"name": "y1",
"zmin": -0.1,
"legendgroup": "y1",
"zmax": 0.1,
"line": {
"color": "rgba(0, 154, 250, 1.000)",
"shape": "linear",
"dash": "solid",
"width": 1
},
"y": [
0.03067541242093728,
0.061321953070307515,
0.33823755155018476,
0.5881151905779408,
0.7168041201646171,
0.8247102693051217,
0.8632937107664949,
0.8973950679428847,
0.9268372919861522,
0.9514675235298343,
0.9878177879548484,
0.9999999466866806,
0.989552636203005,
0.9585219599793448,
0.9031334125880434,
0.8261578853619512,
0.7124277016382363,
0.5758547097527327,
0.43883912087112936,
0.29070489558439705,
0.15461141539348522,
0.015504165579995984,
-0.13641106359642388,
-0.28516655102344535,
-0.42813502702217854,
-0.5610684489029837,
-0.6926171720883418,
-0.8043295777666695,
-0.8892759098040912,
-0.9511602238189567,
-0.9736638213734828,
-0.9893319351014062,
-0.9980545689901091,
-0.999770486890561,
-0.9868211552281166,
-0.9542564477114475,
-0.8888782743946314,
-0.7971854551477641,
-0.6894127808039027,
-0.5635998564911867,
-0.3328229035580867,
-0.07976901700741997,
-0.03991632077017164
],
"type": "scatter",
"hoverinfo": "text"
},
{
"xaxis": "x1",
"colorbar": {
"title": ""
},
"yaxis": "y1",
"text": [
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,