├── .gitignore
├── 1. Introducing Julia.ipynb
├── 2. Using Julia.ipynb
├── 3. Advanced Julia.ipynb
├── README.md
└── fib.jl
/.gitignore:
--------------------------------------------------------------------------------
1 | .ipynb_checkpoints
2 |
--------------------------------------------------------------------------------
/1. Introducing Julia.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "metadata": {
3 | "language": "Julia",
4 | "name": "",
5 | "signature": "sha256:db4b7bd1fd2fe4df34acfd422dbf560d0ccf78447ef4b3cbcea5d2939cf91d9d"
6 | },
7 | "nbformat": 3,
8 | "nbformat_minor": 0,
9 | "worksheets": [
10 | {
11 | "cells": [
12 | {
13 | "cell_type": "markdown",
14 | "metadata": {},
15 | "source": [
16 | "#
\n",
17 | "## Julia : Programming in a number driven world "
18 | ]
19 | },
20 | {
21 | "cell_type": "markdown",
22 | "metadata": {},
23 | "source": [
24 | " \n",
25 | "Image courtesy Stefan Karpinski "
26 | ]
27 | },
28 | {
29 | "cell_type": "markdown",
30 | "metadata": {},
31 | "source": [
32 | "### What is a numerical language?\n",
33 | "\n",
34 | "- Obviously, it's one that specializes in numerical work.\n",
35 | "\n",
36 | "But specialisation means different things\n",
37 | "\n",
38 | "- **Matlab**\n",
39 | " - originally, everything was a matrix of complex doubles\n",
40 | " - everything else has been extended from that\n",
41 | "- **R**\n",
42 | " - data frames as basic type\n",
43 | " - support for \"NA\" values everywhere\n",
44 | "- **Mathematica**\n",
45 | " - symbolic evaluation semantics\n",
46 | "\n",
47 | "Every system picks its own angle/niche to specialize."
48 | ]
49 | },
50 | {
51 | "cell_type": "markdown",
52 | "metadata": {},
53 | "source": [
54 | "### Julia does NOT have a numerical speciality\n",
55 | "\n",
56 | "- instead, it is designed so you can *define numeric types and their behaviors yourself*"
57 | ]
58 | },
59 | {
60 | "cell_type": "markdown",
61 | "metadata": {},
62 | "source": [
63 | "## Design principles\n",
64 | "\n",
65 | "- Turtles all the way down \n",
66 | " - Write all code in Julia\n",
67 | " - No need to escape to C/Fortran for inner kernels\n",
68 | " - Escape the two-language paradigm\n",
69 | "- Natural code is fast \n",
70 | " - Loops are fast\n",
71 | "- Code is written both for human and computers"
72 | ]
73 | },
74 | {
75 | "cell_type": "markdown",
76 | "metadata": {},
77 | "source": [
78 | "### Performance is enabled by a number of features that work together\n",
79 | "\n",
80 | "- An expressive parametric type system, allowing optional type annotations\n",
81 | "- Multiple dispatch using those types to select implementations \n",
82 | "- A dynamic data ow type inference algorithm allowing types of most expressions to be inferred\n",
83 | "- Careful design of the language and standard library to be amenable to type analysis\n",
84 | "- Aggressive code specialization against run-time types \n",
85 | "- Metaprogramming for sophisticated code generation \n",
86 | "- Just-In-Time (JIT) compilation using the Low-level Virtual Machine (LLVM) compiler framework"
87 | ]
88 | },
89 | {
90 | "cell_type": "markdown",
91 | "metadata": {},
92 | "source": [
93 | "## History\n",
94 | " \n",
95 | " Julia creators with Prof. Kahan Jan 2013. Source: http://julialang.org/images/kahan.jpg \n",
96 | "\n",
97 | "### Based around MIT\n",
98 | " - Jeff Bezanson\n",
99 | " - Stefan Karpinski\n",
100 | " - Viral Shah\n",
101 | " - Prof. Alan Edelman\n",
102 | " \n",
103 | "### Around 5 years in the works\n",
104 | " - Development started late 2009\n",
105 | " - First public announcement Feb 2010\n",
106 | " - Currently over 250 contributors\n",
107 | " - Over 20 developers with more than 100 commits\n",
108 | " - Developed on Github: https://github.com/JuliaLang/julia"
109 | ]
110 | },
111 | {
112 | "cell_type": "markdown",
113 | "metadata": {},
114 | "source": [
115 | "##Syntax Basics\n",
116 | "_Fibonacci Sequence, the Hello World of numerical computing_\n",
117 | "$$F_n = F_{n-1} + F_{n-2},\\!\\,$$\n",
118 | "$$F_0 = 0,\\; F_1 = 1$$"
119 | ]
120 | },
121 | {
122 | "cell_type": "code",
123 | "collapsed": false,
124 | "input": [
125 | "function fib(n) \n",
126 | " if n < 2 \n",
127 | " return n\n",
128 | " else\n",
129 | " return fib(n-1) + fib(n-2)\n",
130 | " end\n",
131 | "end"
132 | ],
133 | "language": "python",
134 | "metadata": {},
135 | "outputs": [
136 | {
137 | "metadata": {},
138 | "output_type": "pyout",
139 | "prompt_number": 79,
140 | "text": [
141 | "fib (generic function with 1 method)"
142 | ]
143 | }
144 | ],
145 | "prompt_number": 79
146 | },
147 | {
148 | "cell_type": "code",
149 | "collapsed": false,
150 | "input": [
151 | "fib(20)"
152 | ],
153 | "language": "python",
154 | "metadata": {},
155 | "outputs": [
156 | {
157 | "metadata": {},
158 | "output_type": "pyout",
159 | "prompt_number": 81,
160 | "text": [
161 | "6765"
162 | ]
163 | }
164 | ],
165 | "prompt_number": 81
166 | },
167 | {
168 | "cell_type": "code",
169 | "collapsed": false,
170 | "input": [
171 | "@time fib(40)"
172 | ],
173 | "language": "python",
174 | "metadata": {},
175 | "outputs": [
176 | {
177 | "output_type": "stream",
178 | "stream": "stdout",
179 | "text": [
180 | "elapsed time: 1"
181 | ]
182 | },
183 | {
184 | "output_type": "stream",
185 | "stream": "stdout",
186 | "text": [
187 | ".259992227 seconds (96 bytes allocated)\n"
188 | ]
189 | },
190 | {
191 | "metadata": {},
192 | "output_type": "pyout",
193 | "prompt_number": 82,
194 | "text": [
195 | "102334155"
196 | ]
197 | }
198 | ],
199 | "prompt_number": 82
200 | },
201 | {
202 | "cell_type": "code",
203 | "collapsed": false,
204 | "input": [
205 | "#This is a comment. \n",
206 | "#The type is declared for the function paramter n\n",
207 | "function fib2(n::Int64) \n",
208 | " if n < 2 \n",
209 | " return n\n",
210 | " else\n",
211 | " return fib(n-1) + fib(n-2)\n",
212 | " end\n",
213 | "end"
214 | ],
215 | "language": "python",
216 | "metadata": {},
217 | "outputs": [
218 | {
219 | "metadata": {},
220 | "output_type": "pyout",
221 | "prompt_number": 83,
222 | "text": [
223 | "fib2 (generic function with 1 method)"
224 | ]
225 | }
226 | ],
227 | "prompt_number": 83
228 | },
229 | {
230 | "cell_type": "code",
231 | "collapsed": false,
232 | "input": [
233 | "fib2(10)"
234 | ],
235 | "language": "python",
236 | "metadata": {},
237 | "outputs": [
238 | {
239 | "metadata": {},
240 | "output_type": "pyout",
241 | "prompt_number": 84,
242 | "text": [
243 | "55"
244 | ]
245 | }
246 | ],
247 | "prompt_number": 84
248 | },
249 | {
250 | "cell_type": "code",
251 | "collapsed": false,
252 | "input": [
253 | "@time fib2(40)"
254 | ],
255 | "language": "python",
256 | "metadata": {},
257 | "outputs": [
258 | {
259 | "output_type": "stream",
260 | "stream": "stdout",
261 | "text": [
262 | "elapsed time: 1"
263 | ]
264 | },
265 | {
266 | "output_type": "stream",
267 | "stream": "stdout",
268 | "text": [
269 | ".044553691 seconds (96 bytes allocated)\n"
270 | ]
271 | },
272 | {
273 | "metadata": {},
274 | "output_type": "pyout",
275 | "prompt_number": 85,
276 | "text": [
277 | "102334155"
278 | ]
279 | }
280 | ],
281 | "prompt_number": 85
282 | },
283 | {
284 | "cell_type": "markdown",
285 | "metadata": {},
286 | "source": [
287 | "###Newton's Method \n",
288 | "$$x_{n+1} = x_n - \\frac{f(x_n)}{f'(x_n)} \\,$$"
289 | ]
290 | },
291 | {
292 | "cell_type": "markdown",
293 | "metadata": {},
294 | "source": [
295 | "_Abbreviated syntax for single line functions._\n",
296 | "\n",
297 | "_Multiplication symbol can sometimes be omitted_"
298 | ]
299 | },
300 | {
301 | "cell_type": "code",
302 | "collapsed": false,
303 | "input": [
304 | "poly(x) = x ^ 2 + x \n",
305 | "dpoly(x) = 2x + 1"
306 | ],
307 | "language": "python",
308 | "metadata": {},
309 | "outputs": [
310 | {
311 | "metadata": {},
312 | "output_type": "pyout",
313 | "prompt_number": 86,
314 | "text": [
315 | "dpoly (generic function with 1 method)"
316 | ]
317 | }
318 | ],
319 | "prompt_number": 86
320 | },
321 | {
322 | "cell_type": "code",
323 | "collapsed": false,
324 | "input": [
325 | "function poly_root() \n",
326 | " x0=1\n",
327 | " x1=x0 - (poly(x0)/dpoly(x0))\n",
328 | " while abs(x1 - x0) > 1e-9\n",
329 | " x0=x1\n",
330 | " x1=x0 - (poly(x0)/dpoly(x0))\n",
331 | " end\n",
332 | " return x0\n",
333 | "end\n",
334 | "\n",
335 | "poly_root()"
336 | ],
337 | "language": "python",
338 | "metadata": {},
339 | "outputs": [
340 | {
341 | "metadata": {},
342 | "output_type": "pyout",
343 | "prompt_number": 87,
344 | "text": [
345 | "2.3283064370807974e-10"
346 | ]
347 | }
348 | ],
349 | "prompt_number": 87
350 | },
351 | {
352 | "cell_type": "markdown",
353 | "metadata": {},
354 | "source": [
355 | "####First Class Functions"
356 | ]
357 | },
358 | {
359 | "cell_type": "code",
360 | "collapsed": false,
361 | "input": [
362 | "function root(f, g, init) \n",
363 | " x0=init\n",
364 | " x1=x0 - (f(x0)/g(x0))\n",
365 | " while abs(x1 - x0) > 1e-9\n",
366 | " x0=x1\n",
367 | " x1=x0 - (f(x0)/g(x0))\n",
368 | " end\n",
369 | " return x0\n",
370 | "end\n"
371 | ],
372 | "language": "python",
373 | "metadata": {},
374 | "outputs": [
375 | {
376 | "metadata": {},
377 | "output_type": "pyout",
378 | "prompt_number": 88,
379 | "text": [
380 | "root (generic function with 1 method)"
381 | ]
382 | }
383 | ],
384 | "prompt_number": 88
385 | },
386 | {
387 | "cell_type": "code",
388 | "collapsed": false,
389 | "input": [
390 | "root(poly, dpoly, 1)"
391 | ],
392 | "language": "python",
393 | "metadata": {},
394 | "outputs": [
395 | {
396 | "metadata": {},
397 | "output_type": "pyout",
398 | "prompt_number": 89,
399 | "text": [
400 | "2.3283064370807974e-10"
401 | ]
402 | }
403 | ],
404 | "prompt_number": 89
405 | },
406 | {
407 | "cell_type": "code",
408 | "collapsed": false,
409 | "input": [
410 | "root(sin, cos, 1)"
411 | ],
412 | "language": "python",
413 | "metadata": {},
414 | "outputs": [
415 | {
416 | "metadata": {},
417 | "output_type": "pyout",
418 | "prompt_number": 91,
419 | "text": [
420 | "2.923566201412306e-13"
421 | ]
422 | }
423 | ],
424 | "prompt_number": 91
425 | },
426 | {
427 | "cell_type": "code",
428 | "collapsed": false,
429 | "input": [
430 | "sin(2.923566201412306e-13)"
431 | ],
432 | "language": "python",
433 | "metadata": {},
434 | "outputs": [
435 | {
436 | "metadata": {},
437 | "output_type": "pyout",
438 | "prompt_number": 92,
439 | "text": [
440 | "2.923566201412306e-13"
441 | ]
442 | }
443 | ],
444 | "prompt_number": 92
445 | },
446 | {
447 | "cell_type": "markdown",
448 | "metadata": {},
449 | "source": [
450 | "_Anonymous functions _"
451 | ]
452 | },
453 | {
454 | "cell_type": "code",
455 | "collapsed": false,
456 | "input": [
457 | "root(x->x^2, x->2x, 1)"
458 | ],
459 | "language": "python",
460 | "metadata": {},
461 | "outputs": [
462 | {
463 | "metadata": {},
464 | "output_type": "pyout",
465 | "prompt_number": 93,
466 | "text": [
467 | "1.862645149230957e-9"
468 | ]
469 | }
470 | ],
471 | "prompt_number": 93
472 | },
473 | {
474 | "cell_type": "markdown",
475 | "metadata": {},
476 | "source": [
477 | "####For Loops"
478 | ]
479 | },
480 | {
481 | "cell_type": "code",
482 | "collapsed": false,
483 | "input": [
484 | "function prefix_sum(w)\n",
485 | " for i=2:length(w)\n",
486 | " w[i] += w[i-1]\n",
487 | " end\n",
488 | " return w\n",
489 | "end\n"
490 | ],
491 | "language": "python",
492 | "metadata": {},
493 | "outputs": [
494 | {
495 | "metadata": {},
496 | "output_type": "pyout",
497 | "prompt_number": 21,
498 | "text": [
499 | "prefix_sum (generic function with 1 method)"
500 | ]
501 | }
502 | ],
503 | "prompt_number": 21
504 | },
505 | {
506 | "cell_type": "code",
507 | "collapsed": false,
508 | "input": [
509 | "prefix_sum([1:10])"
510 | ],
511 | "language": "python",
512 | "metadata": {},
513 | "outputs": [
514 | {
515 | "metadata": {},
516 | "output_type": "pyout",
517 | "prompt_number": 94,
518 | "text": [
519 | "10-element Array{Int64,1}:\n",
520 | " 1\n",
521 | " 3\n",
522 | " 6\n",
523 | " 10\n",
524 | " 15\n",
525 | " 21\n",
526 | " 28\n",
527 | " 36\n",
528 | " 45\n",
529 | " 55"
530 | ]
531 | }
532 | ],
533 | "prompt_number": 94
534 | },
535 | {
536 | "cell_type": "markdown",
537 | "metadata": {},
538 | "source": [
539 | "###Ranges"
540 | ]
541 | },
542 | {
543 | "cell_type": "code",
544 | "collapsed": false,
545 | "input": [
546 | "1:10"
547 | ],
548 | "language": "python",
549 | "metadata": {},
550 | "outputs": [
551 | {
552 | "metadata": {},
553 | "output_type": "pyout",
554 | "prompt_number": 23,
555 | "text": [
556 | "1:10"
557 | ]
558 | }
559 | ],
560 | "prompt_number": 23
561 | },
562 | {
563 | "cell_type": "code",
564 | "collapsed": false,
565 | "input": [
566 | "typeof(1:10)"
567 | ],
568 | "language": "python",
569 | "metadata": {},
570 | "outputs": [
571 | {
572 | "metadata": {},
573 | "output_type": "pyout",
574 | "prompt_number": 24,
575 | "text": [
576 | "UnitRange{Int64} (constructor with 1 method)"
577 | ]
578 | }
579 | ],
580 | "prompt_number": 24
581 | },
582 | {
583 | "cell_type": "code",
584 | "collapsed": false,
585 | "input": [
586 | "typeof(1.0:0.5:10.0)"
587 | ],
588 | "language": "python",
589 | "metadata": {},
590 | "outputs": [
591 | {
592 | "metadata": {},
593 | "output_type": "pyout",
594 | "prompt_number": 25,
595 | "text": [
596 | "FloatRange{Float64} (constructor with 1 method)"
597 | ]
598 | }
599 | ],
600 | "prompt_number": 25
601 | },
602 | {
603 | "cell_type": "code",
604 | "collapsed": false,
605 | "input": [
606 | "[1.0:0.5:10.0]"
607 | ],
608 | "language": "python",
609 | "metadata": {},
610 | "outputs": [
611 | {
612 | "metadata": {},
613 | "output_type": "pyout",
614 | "prompt_number": 26,
615 | "text": [
616 | "19-element Array{Float64,1}:\n",
617 | " 1.0\n",
618 | " 1.5\n",
619 | " 2.0\n",
620 | " 2.5\n",
621 | " 3.0\n",
622 | " 3.5\n",
623 | " 4.0\n",
624 | " 4.5\n",
625 | " 5.0\n",
626 | " 5.5\n",
627 | " 6.0\n",
628 | " 6.5\n",
629 | " 7.0\n",
630 | " 7.5\n",
631 | " 8.0\n",
632 | " 8.5\n",
633 | " 9.0\n",
634 | " 9.5\n",
635 | " 10.0"
636 | ]
637 | }
638 | ],
639 | "prompt_number": 26
640 | },
641 | {
642 | "cell_type": "markdown",
643 | "metadata": {},
644 | "source": [
645 | "##Numbers\n",
646 | "\n",
647 | "- Default 64 bit singed integers on 64 bit systems (Int64)\n",
648 | " - 32 bit integers on 32 bit systems (Int32)\n",
649 | " - Alias `Int` for system independent default integer type\n",
650 | "- 64 bit IEEE Floats (Float64)\n",
651 | "- Uses machine numbers \n",
652 | " - Same binary layout as C\n",
653 | " - No overflow/underflow checks"
654 | ]
655 | },
656 | {
657 | "cell_type": "code",
658 | "collapsed": false,
659 | "input": [
660 | "typeof(1)"
661 | ],
662 | "language": "python",
663 | "metadata": {},
664 | "outputs": [
665 | {
666 | "metadata": {},
667 | "output_type": "pyout",
668 | "prompt_number": 27,
669 | "text": [
670 | "Int64"
671 | ]
672 | }
673 | ],
674 | "prompt_number": 27
675 | },
676 | {
677 | "cell_type": "code",
678 | "collapsed": false,
679 | "input": [
680 | "typeof(1.0)"
681 | ],
682 | "language": "python",
683 | "metadata": {},
684 | "outputs": [
685 | {
686 | "metadata": {},
687 | "output_type": "pyout",
688 | "prompt_number": 28,
689 | "text": [
690 | "Float64"
691 | ]
692 | }
693 | ],
694 | "prompt_number": 28
695 | },
696 | {
697 | "cell_type": "code",
698 | "collapsed": false,
699 | "input": [
700 | "1/10"
701 | ],
702 | "language": "python",
703 | "metadata": {},
704 | "outputs": [
705 | {
706 | "metadata": {},
707 | "output_type": "pyout",
708 | "prompt_number": 29,
709 | "text": [
710 | "0.1"
711 | ]
712 | }
713 | ],
714 | "prompt_number": 29
715 | },
716 | {
717 | "cell_type": "code",
718 | "collapsed": false,
719 | "input": [
720 | "eps(1.0)"
721 | ],
722 | "language": "python",
723 | "metadata": {},
724 | "outputs": [
725 | {
726 | "metadata": {},
727 | "output_type": "pyout",
728 | "prompt_number": 30,
729 | "text": [
730 | "2.220446049250313e-16"
731 | ]
732 | }
733 | ],
734 | "prompt_number": 30
735 | },
736 | {
737 | "cell_type": "code",
738 | "collapsed": false,
739 | "input": [
740 | "2^63 + 1"
741 | ],
742 | "language": "python",
743 | "metadata": {},
744 | "outputs": [
745 | {
746 | "metadata": {},
747 | "output_type": "pyout",
748 | "prompt_number": 95,
749 | "text": [
750 | "-9223372036854775807"
751 | ]
752 | }
753 | ],
754 | "prompt_number": 95
755 | },
756 | {
757 | "cell_type": "markdown",
758 | "metadata": {},
759 | "source": [
760 | "####BigNum"
761 | ]
762 | },
763 | {
764 | "cell_type": "code",
765 | "collapsed": false,
766 | "input": [
767 | "big(2)"
768 | ],
769 | "language": "python",
770 | "metadata": {},
771 | "outputs": [
772 | {
773 | "metadata": {},
774 | "output_type": "pyout",
775 | "prompt_number": 32,
776 | "text": [
777 | "2"
778 | ]
779 | }
780 | ],
781 | "prompt_number": 32
782 | },
783 | {
784 | "cell_type": "code",
785 | "collapsed": false,
786 | "input": [
787 | "typeof(big(2))"
788 | ],
789 | "language": "python",
790 | "metadata": {},
791 | "outputs": [
792 | {
793 | "metadata": {},
794 | "output_type": "pyout",
795 | "prompt_number": 33,
796 | "text": [
797 | "BigInt (constructor with 10 methods)"
798 | ]
799 | }
800 | ],
801 | "prompt_number": 33
802 | },
803 | {
804 | "cell_type": "code",
805 | "collapsed": false,
806 | "input": [
807 | "big(2)^63 + 1"
808 | ],
809 | "language": "python",
810 | "metadata": {},
811 | "outputs": [
812 | {
813 | "metadata": {},
814 | "output_type": "pyout",
815 | "prompt_number": 96,
816 | "text": [
817 | "9223372036854775809"
818 | ]
819 | }
820 | ],
821 | "prompt_number": 96
822 | },
823 | {
824 | "cell_type": "code",
825 | "collapsed": false,
826 | "input": [
827 | "big(2.0)"
828 | ],
829 | "language": "python",
830 | "metadata": {},
831 | "outputs": [
832 | {
833 | "metadata": {},
834 | "output_type": "pyout",
835 | "prompt_number": 35,
836 | "text": [
837 | "2e+00 with 256 bits of precision"
838 | ]
839 | }
840 | ],
841 | "prompt_number": 35
842 | },
843 | {
844 | "cell_type": "markdown",
845 | "metadata": {},
846 | "source": [
847 | "####Rational Numbers"
848 | ]
849 | },
850 | {
851 | "cell_type": "code",
852 | "collapsed": false,
853 | "input": [
854 | "1//3"
855 | ],
856 | "language": "python",
857 | "metadata": {},
858 | "outputs": [
859 | {
860 | "metadata": {},
861 | "output_type": "pyout",
862 | "prompt_number": 36,
863 | "text": [
864 | "1//3"
865 | ]
866 | }
867 | ],
868 | "prompt_number": 36
869 | },
870 | {
871 | "cell_type": "code",
872 | "collapsed": false,
873 | "input": [
874 | "typeof(1//3)"
875 | ],
876 | "language": "python",
877 | "metadata": {},
878 | "outputs": [
879 | {
880 | "metadata": {},
881 | "output_type": "pyout",
882 | "prompt_number": 37,
883 | "text": [
884 | "Rational{Int64} (constructor with 1 method)"
885 | ]
886 | }
887 | ],
888 | "prompt_number": 37
889 | },
890 | {
891 | "cell_type": "code",
892 | "collapsed": false,
893 | "input": [
894 | "1//3 + 1//5"
895 | ],
896 | "language": "python",
897 | "metadata": {},
898 | "outputs": [
899 | {
900 | "metadata": {},
901 | "output_type": "pyout",
902 | "prompt_number": 38,
903 | "text": [
904 | "8//15"
905 | ]
906 | }
907 | ],
908 | "prompt_number": 38
909 | },
910 | {
911 | "cell_type": "markdown",
912 | "metadata": {},
913 | "source": [
914 | "####Complex Numbers"
915 | ]
916 | },
917 | {
918 | "cell_type": "code",
919 | "collapsed": false,
920 | "input": [
921 | "1 + 2im"
922 | ],
923 | "language": "python",
924 | "metadata": {},
925 | "outputs": [
926 | {
927 | "metadata": {},
928 | "output_type": "pyout",
929 | "prompt_number": 39,
930 | "text": [
931 | "1 + 2im"
932 | ]
933 | }
934 | ],
935 | "prompt_number": 39
936 | },
937 | {
938 | "cell_type": "code",
939 | "collapsed": false,
940 | "input": [
941 | "typeof(1+2im)"
942 | ],
943 | "language": "python",
944 | "metadata": {},
945 | "outputs": [
946 | {
947 | "metadata": {},
948 | "output_type": "pyout",
949 | "prompt_number": 40,
950 | "text": [
951 | "Complex{Int64} (constructor with 1 method)"
952 | ]
953 | }
954 | ],
955 | "prompt_number": 40
956 | },
957 | {
958 | "cell_type": "code",
959 | "collapsed": false,
960 | "input": [
961 | "exp(im*pi) + 1"
962 | ],
963 | "language": "python",
964 | "metadata": {},
965 | "outputs": [
966 | {
967 | "metadata": {},
968 | "output_type": "pyout",
969 | "prompt_number": 97,
970 | "text": [
971 | "0.0 + 1.2246467991473532e-16im"
972 | ]
973 | }
974 | ],
975 | "prompt_number": 97
976 | },
977 | {
978 | "cell_type": "markdown",
979 | "metadata": {},
980 | "source": [
981 | "##Arrays"
982 | ]
983 | },
984 | {
985 | "cell_type": "code",
986 | "collapsed": false,
987 | "input": [
988 | "a=rand(2,2)"
989 | ],
990 | "language": "python",
991 | "metadata": {},
992 | "outputs": [
993 | {
994 | "metadata": {},
995 | "output_type": "pyout",
996 | "prompt_number": 98,
997 | "text": [
998 | "2x2 Array{Float64,2}:\n",
999 | " 0.457634 0.807653\n",
1000 | " 0.296989 0.299374"
1001 | ]
1002 | }
1003 | ],
1004 | "prompt_number": 98
1005 | },
1006 | {
1007 | "cell_type": "code",
1008 | "collapsed": false,
1009 | "input": [
1010 | "b=[1,2,3]"
1011 | ],
1012 | "language": "python",
1013 | "metadata": {},
1014 | "outputs": [
1015 | {
1016 | "metadata": {},
1017 | "output_type": "pyout",
1018 | "prompt_number": 99,
1019 | "text": [
1020 | "3-element Array{Int64,1}:\n",
1021 | " 1\n",
1022 | " 2\n",
1023 | " 3"
1024 | ]
1025 | }
1026 | ],
1027 | "prompt_number": 99
1028 | },
1029 | {
1030 | "cell_type": "code",
1031 | "collapsed": false,
1032 | "input": [
1033 | "a[1,2]"
1034 | ],
1035 | "language": "python",
1036 | "metadata": {},
1037 | "outputs": [
1038 | {
1039 | "metadata": {},
1040 | "output_type": "pyout",
1041 | "prompt_number": 104,
1042 | "text": [
1043 | "0.8076534171651035"
1044 | ]
1045 | }
1046 | ],
1047 | "prompt_number": 104
1048 | },
1049 | {
1050 | "cell_type": "code",
1051 | "collapsed": false,
1052 | "input": [
1053 | "c=[1 2 3; 4 5 6]"
1054 | ],
1055 | "language": "python",
1056 | "metadata": {},
1057 | "outputs": [
1058 | {
1059 | "metadata": {},
1060 | "output_type": "pyout",
1061 | "prompt_number": 107,
1062 | "text": [
1063 | "2x3 Array{Int64,2}:\n",
1064 | " 1 2 3\n",
1065 | " 4 5 6"
1066 | ]
1067 | }
1068 | ],
1069 | "prompt_number": 107
1070 | },
1071 | {
1072 | "cell_type": "code",
1073 | "collapsed": false,
1074 | "input": [
1075 | "a[1,:]"
1076 | ],
1077 | "language": "python",
1078 | "metadata": {},
1079 | "outputs": [
1080 | {
1081 | "metadata": {},
1082 | "output_type": "pyout",
1083 | "prompt_number": 109,
1084 | "text": [
1085 | "1x2 Array{Float64,2}:\n",
1086 | " 0.457634 0.807653"
1087 | ]
1088 | }
1089 | ],
1090 | "prompt_number": 109
1091 | },
1092 | {
1093 | "cell_type": "code",
1094 | "collapsed": false,
1095 | "input": [
1096 | "a[:,1]"
1097 | ],
1098 | "language": "python",
1099 | "metadata": {},
1100 | "outputs": [
1101 | {
1102 | "metadata": {},
1103 | "output_type": "pyout",
1104 | "prompt_number": 110,
1105 | "text": [
1106 | "2-element Array{Float64,1}:\n",
1107 | " 0.457634\n",
1108 | " 0.296989"
1109 | ]
1110 | }
1111 | ],
1112 | "prompt_number": 110
1113 | },
1114 | {
1115 | "cell_type": "code",
1116 | "collapsed": false,
1117 | "input": [
1118 | "b[2:end]"
1119 | ],
1120 | "language": "python",
1121 | "metadata": {},
1122 | "outputs": [
1123 | {
1124 | "metadata": {},
1125 | "output_type": "pyout",
1126 | "prompt_number": 48,
1127 | "text": [
1128 | "2-element Array{Int64,1}:\n",
1129 | " 2\n",
1130 | " 3"
1131 | ]
1132 | }
1133 | ],
1134 | "prompt_number": 48
1135 | },
1136 | {
1137 | "cell_type": "code",
1138 | "collapsed": false,
1139 | "input": [
1140 | "c=eye(ones(Complex64, (3,3)))"
1141 | ],
1142 | "language": "python",
1143 | "metadata": {},
1144 | "outputs": [
1145 | {
1146 | "metadata": {},
1147 | "output_type": "pyout",
1148 | "prompt_number": 123,
1149 | "text": [
1150 | "3x3 Array{Complex{Float32},2}:\n",
1151 | " 1.0+0.0im 0.0+0.0im 0.0+0.0im\n",
1152 | " 0.0+0.0im 1.0+0.0im 0.0+0.0im\n",
1153 | " 0.0+0.0im 0.0+0.0im 1.0+0.0im"
1154 | ]
1155 | }
1156 | ],
1157 | "prompt_number": 123
1158 | },
1159 | {
1160 | "cell_type": "markdown",
1161 | "metadata": {},
1162 | "source": [
1163 | "###Comprehensions"
1164 | ]
1165 | },
1166 | {
1167 | "cell_type": "code",
1168 | "collapsed": false,
1169 | "input": [
1170 | "[x^2 for x=1:10]"
1171 | ],
1172 | "language": "python",
1173 | "metadata": {},
1174 | "outputs": [
1175 | {
1176 | "metadata": {},
1177 | "output_type": "pyout",
1178 | "prompt_number": 50,
1179 | "text": [
1180 | "10-element Array{Int64,1}:\n",
1181 | " 1\n",
1182 | " 4\n",
1183 | " 9\n",
1184 | " 16\n",
1185 | " 25\n",
1186 | " 36\n",
1187 | " 49\n",
1188 | " 64\n",
1189 | " 81\n",
1190 | " 100"
1191 | ]
1192 | }
1193 | ],
1194 | "prompt_number": 50
1195 | },
1196 | {
1197 | "cell_type": "code",
1198 | "collapsed": false,
1199 | "input": [
1200 | "[x+y for x=1:10, y=1:10]"
1201 | ],
1202 | "language": "python",
1203 | "metadata": {},
1204 | "outputs": [
1205 | {
1206 | "metadata": {},
1207 | "output_type": "pyout",
1208 | "prompt_number": 51,
1209 | "text": [
1210 | "10x10 Array{Int64,2}:\n",
1211 | " 2 3 4 5 6 7 8 9 10 11\n",
1212 | " 3 4 5 6 7 8 9 10 11 12\n",
1213 | " 4 5 6 7 8 9 10 11 12 13\n",
1214 | " 5 6 7 8 9 10 11 12 13 14\n",
1215 | " 6 7 8 9 10 11 12 13 14 15\n",
1216 | " 7 8 9 10 11 12 13 14 15 16\n",
1217 | " 8 9 10 11 12 13 14 15 16 17\n",
1218 | " 9 10 11 12 13 14 15 16 17 18\n",
1219 | " 10 11 12 13 14 15 16 17 18 19\n",
1220 | " 11 12 13 14 15 16 17 18 19 20"
1221 | ]
1222 | }
1223 | ],
1224 | "prompt_number": 51
1225 | },
1226 | {
1227 | "cell_type": "markdown",
1228 | "metadata": {},
1229 | "source": [
1230 | "##Excercise \n",
1231 | "\n",
1232 | "Write an iterative implementation for the Fibonacci Sequence, and use it to find fib(100). One simple algorithm is to store the intermediate values in an array. "
1233 | ]
1234 | },
1235 | {
1236 | "cell_type": "code",
1237 | "collapsed": false,
1238 | "input": [],
1239 | "language": "python",
1240 | "metadata": {},
1241 | "outputs": [],
1242 | "prompt_number": 52
1243 | },
1244 | {
1245 | "cell_type": "markdown",
1246 | "metadata": {},
1247 | "source": [
1248 | "##Strings"
1249 | ]
1250 | },
1251 | {
1252 | "cell_type": "code",
1253 | "collapsed": false,
1254 | "input": [
1255 | "typeof(\"abc\")"
1256 | ],
1257 | "language": "python",
1258 | "metadata": {},
1259 | "outputs": [
1260 | {
1261 | "metadata": {},
1262 | "output_type": "pyout",
1263 | "prompt_number": 53,
1264 | "text": [
1265 | "ASCIIString (constructor with 2 methods)"
1266 | ]
1267 | }
1268 | ],
1269 | "prompt_number": 53
1270 | },
1271 | {
1272 | "cell_type": "code",
1273 | "collapsed": false,
1274 | "input": [
1275 | "typeof(\"\u03b1\u03b2\u03b3\")"
1276 | ],
1277 | "language": "python",
1278 | "metadata": {},
1279 | "outputs": [
1280 | {
1281 | "metadata": {},
1282 | "output_type": "pyout",
1283 | "prompt_number": 54,
1284 | "text": [
1285 | "UTF8String (constructor with 2 methods)"
1286 | ]
1287 | }
1288 | ],
1289 | "prompt_number": 54
1290 | },
1291 | {
1292 | "cell_type": "code",
1293 | "collapsed": false,
1294 | "input": [
1295 | "x=2; \n",
1296 | "s=\"The square root of $x is $(sqrt(x))\""
1297 | ],
1298 | "language": "python",
1299 | "metadata": {},
1300 | "outputs": [
1301 | {
1302 | "metadata": {},
1303 | "output_type": "pyout",
1304 | "prompt_number": 55,
1305 | "text": [
1306 | "\"The square root of 2 is 1.4142135623730951\""
1307 | ]
1308 | }
1309 | ],
1310 | "prompt_number": 55
1311 | },
1312 | {
1313 | "cell_type": "markdown",
1314 | "metadata": {},
1315 | "source": [
1316 | "##Tuples\n",
1317 | "Tuples are just a small collection of values. Usually function parameters are represented as tuples. In Julia, tuples are exposed to the programmer. Among other things, they can be used to return multple values from a function\n"
1318 | ]
1319 | },
1320 | {
1321 | "cell_type": "code",
1322 | "collapsed": false,
1323 | "input": [
1324 | "(1,2)"
1325 | ],
1326 | "language": "python",
1327 | "metadata": {},
1328 | "outputs": [
1329 | {
1330 | "metadata": {},
1331 | "output_type": "pyout",
1332 | "prompt_number": 56,
1333 | "text": [
1334 | "(1,2)"
1335 | ]
1336 | }
1337 | ],
1338 | "prompt_number": 56
1339 | },
1340 | {
1341 | "cell_type": "code",
1342 | "collapsed": false,
1343 | "input": [
1344 | "typeof((1,2))"
1345 | ],
1346 | "language": "python",
1347 | "metadata": {},
1348 | "outputs": [
1349 | {
1350 | "metadata": {},
1351 | "output_type": "pyout",
1352 | "prompt_number": 57,
1353 | "text": [
1354 | "(Int64,Int64)"
1355 | ]
1356 | }
1357 | ],
1358 | "prompt_number": 57
1359 | },
1360 | {
1361 | "cell_type": "code",
1362 | "collapsed": false,
1363 | "input": [
1364 | "typeof(1,2)"
1365 | ],
1366 | "language": "python",
1367 | "metadata": {},
1368 | "outputs": [
1369 | {
1370 | "ename": "LoadError",
1371 | "evalue": "typeof: too many arguments (expected 1)\nwhile loading In[58], in expression starting on line 1",
1372 | "output_type": "pyerr",
1373 | "traceback": [
1374 | "typeof: too many arguments (expected 1)\nwhile loading In[58], in expression starting on line 1",
1375 | ""
1376 | ]
1377 | }
1378 | ],
1379 | "prompt_number": 58
1380 | },
1381 | {
1382 | "cell_type": "code",
1383 | "collapsed": false,
1384 | "input": [
1385 | "function sq_and_cb(x) \n",
1386 | " return (x^2, x^3)\n",
1387 | "end"
1388 | ],
1389 | "language": "python",
1390 | "metadata": {},
1391 | "outputs": [
1392 | {
1393 | "metadata": {},
1394 | "output_type": "pyout",
1395 | "prompt_number": 59,
1396 | "text": [
1397 | "sq_and_cb (generic function with 1 method)"
1398 | ]
1399 | }
1400 | ],
1401 | "prompt_number": 59
1402 | },
1403 | {
1404 | "cell_type": "code",
1405 | "collapsed": false,
1406 | "input": [
1407 | "sq_and_cb(9)"
1408 | ],
1409 | "language": "python",
1410 | "metadata": {},
1411 | "outputs": [
1412 | {
1413 | "metadata": {},
1414 | "output_type": "pyout",
1415 | "prompt_number": 60,
1416 | "text": [
1417 | "(81,729)"
1418 | ]
1419 | }
1420 | ],
1421 | "prompt_number": 60
1422 | },
1423 | {
1424 | "cell_type": "markdown",
1425 | "metadata": {},
1426 | "source": [
1427 | "##Types and Method Dispatch"
1428 | ]
1429 | },
1430 | {
1431 | "cell_type": "markdown",
1432 | "metadata": {},
1433 | "source": [
1434 | "###Types\n",
1435 | " - Describes the domain of values\n",
1436 | " - In Julia, it is tag on all values\n",
1437 | " - There is a heirarchy of types\n",
1438 | " - eg: Number -> Integer -> Int64\n",
1439 | " "
1440 | ]
1441 | },
1442 | {
1443 | "cell_type": "markdown",
1444 | "metadata": {},
1445 | "source": [
1446 | "###Abstract and concrete\n",
1447 | " - Abstract types cannot hold data\n",
1448 | " - Abstract types can be subclassed\n",
1449 | " - Values are of a concrete type\n",
1450 | " - Concrete types cannot subtyped (ie, they are final)"
1451 | ]
1452 | },
1453 | {
1454 | "cell_type": "markdown",
1455 | "metadata": {},
1456 | "source": [
1457 | "#### _Any_ is the top type in Julia. All types are subtypes of Any\n",
1458 | "#### _None_ is the bottom type. There are no instance of None, and it is the subtype of all types. "
1459 | ]
1460 | },
1461 | {
1462 | "cell_type": "markdown",
1463 | "metadata": {},
1464 | "source": [
1465 | "###Multiple Dispatch"
1466 | ]
1467 | },
1468 | {
1469 | "cell_type": "code",
1470 | "collapsed": false,
1471 | "input": [
1472 | "f(a, b) = \"fallback\"\n",
1473 | "f(a::Number, b::Number) = \"a and b are both numbers\"\n",
1474 | "f(a::Number, b) = \"a is a number\"\n",
1475 | "f(a, b::Number) = \"b is a number\"\n",
1476 | "f(a::Integer, b::Integer) = \"a and b are both integers\""
1477 | ],
1478 | "language": "python",
1479 | "metadata": {},
1480 | "outputs": [
1481 | {
1482 | "metadata": {},
1483 | "output_type": "pyout",
1484 | "prompt_number": 124,
1485 | "text": [
1486 | "f (generic function with 8 methods)"
1487 | ]
1488 | }
1489 | ],
1490 | "prompt_number": 124
1491 | },
1492 | {
1493 | "cell_type": "code",
1494 | "collapsed": false,
1495 | "input": [
1496 | "f(1.5,2)"
1497 | ],
1498 | "language": "python",
1499 | "metadata": {},
1500 | "outputs": [
1501 | {
1502 | "metadata": {},
1503 | "output_type": "pyout",
1504 | "prompt_number": 125,
1505 | "text": [
1506 | "\"a and b are both numbers\""
1507 | ]
1508 | }
1509 | ],
1510 | "prompt_number": 125
1511 | },
1512 | {
1513 | "cell_type": "code",
1514 | "collapsed": false,
1515 | "input": [
1516 | "f(1, \"bar\")"
1517 | ],
1518 | "language": "python",
1519 | "metadata": {},
1520 | "outputs": [
1521 | {
1522 | "metadata": {},
1523 | "output_type": "pyout",
1524 | "prompt_number": 126,
1525 | "text": [
1526 | "\"a is a number\""
1527 | ]
1528 | }
1529 | ],
1530 | "prompt_number": 126
1531 | },
1532 | {
1533 | "cell_type": "code",
1534 | "collapsed": false,
1535 | "input": [
1536 | "f(1,2)"
1537 | ],
1538 | "language": "python",
1539 | "metadata": {},
1540 | "outputs": [
1541 | {
1542 | "metadata": {},
1543 | "output_type": "pyout",
1544 | "prompt_number": 127,
1545 | "text": [
1546 | "\"a and b are both integers\""
1547 | ]
1548 | }
1549 | ],
1550 | "prompt_number": 127
1551 | },
1552 | {
1553 | "cell_type": "code",
1554 | "collapsed": false,
1555 | "input": [
1556 | "f(\"foo\", [1,2])"
1557 | ],
1558 | "language": "python",
1559 | "metadata": {},
1560 | "outputs": [
1561 | {
1562 | "metadata": {},
1563 | "output_type": "pyout",
1564 | "prompt_number": 128,
1565 | "text": [
1566 | "\"fallback\""
1567 | ]
1568 | }
1569 | ],
1570 | "prompt_number": 128
1571 | },
1572 | {
1573 | "cell_type": "code",
1574 | "collapsed": false,
1575 | "input": [
1576 | "methods(f)"
1577 | ],
1578 | "language": "python",
1579 | "metadata": {},
1580 | "outputs": [
1581 | {
1582 | "html": [
1583 | "5 methods for generic function f : f(a::Integer ,b::Integer ) at In[61]:5 f(a::Number ,b::Number ) at In[61]:2 f(a::Number ,b) at In[61]:3 f(a,b::Number ) at In[61]:4 f(a,b) at In[61]:1 "
1584 | ],
1585 | "metadata": {},
1586 | "output_type": "pyout",
1587 | "prompt_number": 66,
1588 | "text": [
1589 | "# 5 methods for generic function \"f\":\n",
1590 | "f(a::Integer,b::Integer) at In[61]:5\n",
1591 | "f(a::Number,b::Number) at In[61]:2\n",
1592 | "f(a::Number,b) at In[61]:3\n",
1593 | "f(a,b::Number) at In[61]:4\n",
1594 | "f(a,b) at In[61]:1"
1595 | ]
1596 | }
1597 | ],
1598 | "prompt_number": 66
1599 | },
1600 | {
1601 | "cell_type": "markdown",
1602 | "metadata": {},
1603 | "source": [
1604 | "####Type Parameters\n",
1605 | "\n",
1606 | "Method definitions can optionally have type parameters immediately after the method name and before the parameters tuple. The type declarations on the parameters itself can then be variable. "
1607 | ]
1608 | },
1609 | {
1610 | "cell_type": "markdown",
1611 | "metadata": {},
1612 | "source": [
1613 | "####Diagonal Dispatch"
1614 | ]
1615 | },
1616 | {
1617 | "cell_type": "code",
1618 | "collapsed": false,
1619 | "input": [
1620 | "f{T<:Number}(a::T, b::T) = \"a and b are both $(T)s\""
1621 | ],
1622 | "language": "python",
1623 | "metadata": {},
1624 | "outputs": [
1625 | {
1626 | "metadata": {},
1627 | "output_type": "pyout",
1628 | "prompt_number": 129,
1629 | "text": [
1630 | "f (generic function with 8 methods)"
1631 | ]
1632 | }
1633 | ],
1634 | "prompt_number": 129
1635 | },
1636 | {
1637 | "cell_type": "code",
1638 | "collapsed": false,
1639 | "input": [
1640 | "f(big(1.5),big(2.5))"
1641 | ],
1642 | "language": "python",
1643 | "metadata": {},
1644 | "outputs": [
1645 | {
1646 | "metadata": {},
1647 | "output_type": "pyout",
1648 | "prompt_number": 130,
1649 | "text": [
1650 | "\"a and b are both BigFloats\""
1651 | ]
1652 | }
1653 | ],
1654 | "prompt_number": 130
1655 | },
1656 | {
1657 | "cell_type": "code",
1658 | "collapsed": false,
1659 | "input": [
1660 | "f(big(1),big(2)) #<== integer rule is more specific"
1661 | ],
1662 | "language": "python",
1663 | "metadata": {},
1664 | "outputs": [
1665 | {
1666 | "metadata": {},
1667 | "output_type": "pyout",
1668 | "prompt_number": 131,
1669 | "text": [
1670 | "\"a and b are both integers\""
1671 | ]
1672 | }
1673 | ],
1674 | "prompt_number": 131
1675 | },
1676 | {
1677 | "cell_type": "code",
1678 | "collapsed": false,
1679 | "input": [
1680 | "f(\"foo\",\"bar\") #<== still doesn't apply to non-numbers"
1681 | ],
1682 | "language": "python",
1683 | "metadata": {},
1684 | "outputs": [
1685 | {
1686 | "metadata": {},
1687 | "output_type": "pyout",
1688 | "prompt_number": 132,
1689 | "text": [
1690 | "\"fallback\""
1691 | ]
1692 | }
1693 | ],
1694 | "prompt_number": 132
1695 | },
1696 | {
1697 | "cell_type": "markdown",
1698 | "metadata": {},
1699 | "source": [
1700 | "####Varargs methods"
1701 | ]
1702 | },
1703 | {
1704 | "cell_type": "code",
1705 | "collapsed": false,
1706 | "input": [
1707 | "f(args::Number...) = \"$(length(args))-parameters heterogeneous call\"\n",
1708 | "f{T<:Number}(args::T...) = \"$(length(args))-parameters homogeneous call\""
1709 | ],
1710 | "language": "python",
1711 | "metadata": {},
1712 | "outputs": [
1713 | {
1714 | "metadata": {},
1715 | "output_type": "pyout",
1716 | "prompt_number": 71,
1717 | "text": [
1718 | "f (generic function with 8 methods)"
1719 | ]
1720 | }
1721 | ],
1722 | "prompt_number": 71
1723 | },
1724 | {
1725 | "cell_type": "code",
1726 | "collapsed": false,
1727 | "input": [
1728 | "f(1)"
1729 | ],
1730 | "language": "python",
1731 | "metadata": {},
1732 | "outputs": [
1733 | {
1734 | "metadata": {},
1735 | "output_type": "pyout",
1736 | "prompt_number": 133,
1737 | "text": [
1738 | "\"1-parameters homogeneous call\""
1739 | ]
1740 | }
1741 | ],
1742 | "prompt_number": 133
1743 | },
1744 | {
1745 | "cell_type": "code",
1746 | "collapsed": false,
1747 | "input": [
1748 | "f(1,2,3)"
1749 | ],
1750 | "language": "python",
1751 | "metadata": {},
1752 | "outputs": [
1753 | {
1754 | "metadata": {},
1755 | "output_type": "pyout",
1756 | "prompt_number": 134,
1757 | "text": [
1758 | "\"3-parameters homogeneous call\""
1759 | ]
1760 | }
1761 | ],
1762 | "prompt_number": 134
1763 | },
1764 | {
1765 | "cell_type": "code",
1766 | "collapsed": false,
1767 | "input": [
1768 | "f(1,1.5,2)"
1769 | ],
1770 | "language": "python",
1771 | "metadata": {},
1772 | "outputs": [
1773 | {
1774 | "metadata": {},
1775 | "output_type": "pyout",
1776 | "prompt_number": 135,
1777 | "text": [
1778 | "\"3-parameters heterogeneous call\""
1779 | ]
1780 | }
1781 | ],
1782 | "prompt_number": 135
1783 | },
1784 | {
1785 | "cell_type": "code",
1786 | "collapsed": false,
1787 | "input": [
1788 | "f() # heterogeneous because we can't bind T"
1789 | ],
1790 | "language": "python",
1791 | "metadata": {},
1792 | "outputs": [
1793 | {
1794 | "metadata": {},
1795 | "output_type": "pyout",
1796 | "prompt_number": 136,
1797 | "text": [
1798 | "\"0-parameters heterogeneous call\""
1799 | ]
1800 | }
1801 | ],
1802 | "prompt_number": 136
1803 | },
1804 | {
1805 | "cell_type": "code",
1806 | "collapsed": false,
1807 | "input": [
1808 | "f(1,2) # previous 2-arg method is more specific"
1809 | ],
1810 | "language": "python",
1811 | "metadata": {},
1812 | "outputs": [
1813 | {
1814 | "metadata": {},
1815 | "output_type": "pyout",
1816 | "prompt_number": 137,
1817 | "text": [
1818 | "\"a and b are both integers\""
1819 | ]
1820 | }
1821 | ],
1822 | "prompt_number": 137
1823 | },
1824 | {
1825 | "cell_type": "code",
1826 | "collapsed": false,
1827 | "input": [
1828 | "f(\"foo\") # doesn't apply to non-numbers"
1829 | ],
1830 | "language": "python",
1831 | "metadata": {},
1832 | "outputs": [
1833 | {
1834 | "ename": "LoadError",
1835 | "evalue": "`f` has no method matching f(::ASCIIString)\nwhile loading In[138], in expression starting on line 1",
1836 | "output_type": "pyerr",
1837 | "traceback": [
1838 | "`f` has no method matching f(::ASCIIString)\nwhile loading In[138], in expression starting on line 1",
1839 | ""
1840 | ]
1841 | }
1842 | ],
1843 | "prompt_number": 138
1844 | },
1845 | {
1846 | "cell_type": "code",
1847 | "collapsed": false,
1848 | "input": [],
1849 | "language": "python",
1850 | "metadata": {},
1851 | "outputs": [],
1852 | "prompt_number": 78
1853 | }
1854 | ],
1855 | "metadata": {}
1856 | }
1857 | ]
1858 | }
--------------------------------------------------------------------------------
/3. Advanced Julia.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "metadata": {
3 | "language": "Julia",
4 | "name": "",
5 | "signature": "sha256:afb46195f954c8c0ff83ce124a5672cd5627cd20615ff4e8f1d73db960916f28"
6 | },
7 | "nbformat": 3,
8 | "nbformat_minor": 0,
9 | "worksheets": [
10 | {
11 | "cells": [
12 | {
13 | "cell_type": "markdown",
14 | "metadata": {},
15 | "source": [
16 | "##Performance of Julia code\n",
17 | "\n",
18 | "http://julia.readthedocs.org/en/latest/manual/performance-tips/"
19 | ]
20 | },
21 | {
22 | "cell_type": "markdown",
23 | "metadata": {},
24 | "source": [
25 | "One of the reasons Julia is fast is that the runtime compiles and caches specialised versions of all methods."
26 | ]
27 | },
28 | {
29 | "cell_type": "code",
30 | "collapsed": false,
31 | "input": [
32 | "function prefix_sum(w)\n",
33 | " for i=2:length(w)\n",
34 | " w[i] += w[i-1]\n",
35 | " end\n",
36 | " return w\n",
37 | "end"
38 | ],
39 | "language": "python",
40 | "metadata": {},
41 | "outputs": [
42 | {
43 | "metadata": {},
44 | "output_type": "pyout",
45 | "prompt_number": 53,
46 | "text": [
47 | "prefix_sum (generic function with 1 method)"
48 | ]
49 | }
50 | ],
51 | "prompt_number": 53
52 | },
53 | {
54 | "cell_type": "code",
55 | "collapsed": false,
56 | "input": [
57 | "x = ones(1000000)\n",
58 | "@time prefix_sum(x)\n",
59 | "y = ones(1000000) + im*ones(1000000)\n",
60 | "@time prefix_sum(y);"
61 | ],
62 | "language": "python",
63 | "metadata": {},
64 | "outputs": [
65 | {
66 | "output_type": "stream",
67 | "stream": "stdout",
68 | "text": [
69 | "elapsed time: 0."
70 | ]
71 | },
72 | {
73 | "output_type": "stream",
74 | "stream": "stdout",
75 | "text": [
76 | "003911471 seconds (80 bytes allocated)\n",
77 | "elapsed time: 0.00496724 seconds (80 bytes allocated)\n"
78 | ]
79 | }
80 | ],
81 | "prompt_number": 55
82 | },
83 | {
84 | "cell_type": "markdown",
85 | "metadata": {},
86 | "source": [
87 | "Remarkably, we get very similar times for an array of primitive floats, versus an array of complex(sic) values."
88 | ]
89 | },
90 | {
91 | "cell_type": "code",
92 | "collapsed": false,
93 | "input": [
94 | "code_native(+, (Complex{Int64}, Complex{Int64}))"
95 | ],
96 | "language": "python",
97 | "metadata": {},
98 | "outputs": [
99 | {
100 | "output_type": "stream",
101 | "stream": "stdout",
102 | "text": [
103 | "\t.section\t__TEXT,__text,regular,pure_instructions\n",
104 | "Filename: complex.jl\n",
105 | "Source line: 110\n",
106 | "\tpush\tRBP\n",
107 | "\tmov\tRBP, RSP\n",
108 | "Source line: 110\n",
109 | "\tadd\tRDI, RDX\n",
110 | "\tadd\tRSI, RCX\n",
111 | "\tmov\tRAX, RDI\n",
112 | "\tmov\tRDX, RSI\n",
113 | "\tpop\tRBP\n",
114 | "\tret\n"
115 | ]
116 | }
117 | ],
118 | "prompt_number": 30
119 | },
120 | {
121 | "cell_type": "markdown",
122 | "metadata": {},
123 | "source": [
124 | "###Type Stable\n",
125 | " For this to work, Julia code must be type stable. \n"
126 | ]
127 | },
128 | {
129 | "cell_type": "markdown",
130 | "metadata": {},
131 | "source": [
132 | "#### Return type of a method must only depend on the types of its parameters, not their values"
133 | ]
134 | },
135 | {
136 | "cell_type": "code",
137 | "collapsed": false,
138 | "input": [
139 | "sqrt(-1)"
140 | ],
141 | "language": "python",
142 | "metadata": {},
143 | "outputs": [
144 | {
145 | "ename": "LoadError",
146 | "evalue": "DomainError\nwhile loading In[31], in expression starting on line 1",
147 | "output_type": "pyerr",
148 | "traceback": [
149 | "DomainError\nwhile loading In[31], in expression starting on line 1",
150 | "",
151 | " in sqrt at math.jl:131"
152 | ]
153 | }
154 | ],
155 | "prompt_number": 31
156 | },
157 | {
158 | "cell_type": "code",
159 | "collapsed": false,
160 | "input": [
161 | "sqrt(-1 + 0im)"
162 | ],
163 | "language": "python",
164 | "metadata": {},
165 | "outputs": [
166 | {
167 | "metadata": {},
168 | "output_type": "pyout",
169 | "prompt_number": 32,
170 | "text": [
171 | "0.0 + 1.0im"
172 | ]
173 | }
174 | ],
175 | "prompt_number": 32
176 | },
177 | {
178 | "cell_type": "markdown",
179 | "metadata": {},
180 | "source": [
181 | "This is also partly why Julia does not automatically promote to BigNum on overflow"
182 | ]
183 | },
184 | {
185 | "cell_type": "markdown",
186 | "metadata": {},
187 | "source": [
188 | "####Variables in inner loops must not change types"
189 | ]
190 | },
191 | {
192 | "cell_type": "code",
193 | "collapsed": false,
194 | "input": [
195 | "function sum1(N)\n",
196 | " total = 0\n",
197 | " \n",
198 | " for i in 1:N\n",
199 | " total += i/2\n",
200 | " end\n",
201 | " \n",
202 | " total\n",
203 | "end\n",
204 | "\n",
205 | "function sum2(N)\n",
206 | " total = 0.0\n",
207 | " \n",
208 | " for i in 1:N\n",
209 | " total += i/2\n",
210 | " end\n",
211 | " \n",
212 | " total\n",
213 | "end"
214 | ],
215 | "language": "python",
216 | "metadata": {},
217 | "outputs": [
218 | {
219 | "metadata": {},
220 | "output_type": "pyout",
221 | "prompt_number": 33,
222 | "text": [
223 | "sum2 (generic function with 1 method)"
224 | ]
225 | }
226 | ],
227 | "prompt_number": 33
228 | },
229 | {
230 | "cell_type": "code",
231 | "collapsed": false,
232 | "input": [
233 | "sum1(100), sum2(100)"
234 | ],
235 | "language": "python",
236 | "metadata": {},
237 | "outputs": [
238 | {
239 | "metadata": {},
240 | "output_type": "pyout",
241 | "prompt_number": 34,
242 | "text": [
243 | "(2525.0,2525.0)"
244 | ]
245 | }
246 | ],
247 | "prompt_number": 34
248 | },
249 | {
250 | "cell_type": "code",
251 | "collapsed": false,
252 | "input": [
253 | "@time sum1(10000000)\n",
254 | "@time sum2(10000000)"
255 | ],
256 | "language": "python",
257 | "metadata": {},
258 | "outputs": [
259 | {
260 | "output_type": "stream",
261 | "stream": "stdout",
262 | "text": [
263 | "elapsed time: "
264 | ]
265 | },
266 | {
267 | "output_type": "stream",
268 | "stream": "stdout",
269 | "text": [
270 | "0.955197429 seconds (320000080 bytes allocated, 44.74% gc time)\n",
271 | "elapsed time: 0.04012344 seconds (96 bytes allocated)\n"
272 | ]
273 | },
274 | {
275 | "metadata": {},
276 | "output_type": "pyout",
277 | "prompt_number": 35,
278 | "text": [
279 | "2.50000025e13"
280 | ]
281 | }
282 | ],
283 | "prompt_number": 35
284 | },
285 | {
286 | "cell_type": "code",
287 | "collapsed": false,
288 | "input": [
289 | "code_llvm(sum1, (Int,))"
290 | ],
291 | "language": "python",
292 | "metadata": {},
293 | "outputs": [
294 | {
295 | "output_type": "stream",
296 | "stream": "stdout",
297 | "text": [
298 | "\n",
299 | "define %jl_value_t* @julia_sum1_22395(i64) {\n",
300 | "top:\n",
301 | " %1 = alloca [5 x %jl_value_t*], align 8\n",
302 | " %.sub = getelementptr inbounds [5 x %jl_value_t*]* %1, i64 0, i64 0\n",
303 | " %2 = getelementptr [5 x %jl_value_t*]* %1, i64 0, i64 2, !dbg !7780\n",
304 | " store %jl_value_t* inttoptr (i64 6 to %jl_value_t*), %jl_value_t** %.sub, align 8\n",
305 | " %3 = load %jl_value_t*** @jl_pgcstack, align 8, !dbg !7780\n",
306 | " %4 = getelementptr [5 x %jl_value_t*]* %1, i64 0, i64 1, !dbg !7780\n",
307 | " %.c = bitcast %jl_value_t** %3 to %jl_value_t*, !dbg !7780\n",
308 | " store %jl_value_t* %.c, %jl_value_t** %4, align 8, !dbg !7780\n",
309 | " store %jl_value_t** %.sub, %jl_value_t*** @jl_pgcstack, align 8, !dbg !7780\n",
310 | " %5 = getelementptr [5 x %jl_value_t*]* %1, i64 0, i64 3\n",
311 | " store %jl_value_t* null, %jl_value_t** %5, align 8\n",
312 | " %6 = getelementptr [5 x %jl_value_t*]* %1, i64 0, i64 4\n",
313 | " store %jl_value_t* null, %jl_value_t** %6, align 8\n",
314 | " store %jl_value_t* inttoptr (i64 140671537323584 to %jl_value_t*), %jl_value_t** %2, align 8, !dbg !7781\n",
315 | " %7 = icmp sgt i64 %0, 0, !dbg !7782\n",
316 | " br i1 %7, label %L, label %L3, !dbg !7782\n",
317 | "\n",
318 | "L: ; preds = %top, %L\n",
319 | " %8 = phi %jl_value_t* [ %16, %L ], [ inttoptr (i64 140671537323584 to %jl_value_t*), %top ], !dbg !7782\n",
320 | " %\"#s237.0\" = phi i64 [ %9, %L ], [ 1, %top ]\n",
321 | " %9 = add i64 %\"#s237.0\", 1, !dbg !7782\n",
322 | " store %jl_value_t* %8, %jl_value_t** %5, align 8, !dbg !7783\n",
323 | " %10 = sitofp i64 %\"#s237.0\" to double, !dbg !7783\n",
324 | " %11 = fmul double %10, 5.000000e-01, !dbg !7783\n",
325 | " %12 = call %jl_value_t* @alloc_2w(), !dbg !7783\n",
326 | " %13 = getelementptr inbounds %jl_value_t* %12, i64 0, i32 0, !dbg !7783\n",
327 | " store %jl_value_t* inttoptr (i64 140671537250576 to %jl_value_t*), %jl_value_t** %13, align 8, !dbg !7783\n",
328 | " %14 = getelementptr inbounds %jl_value_t* %12, i64 1, i32 0, !dbg !7783\n",
329 | " %15 = bitcast %jl_value_t** %14 to double*, !dbg !7783\n",
330 | " store double %11, double* %15, align 8, !dbg !7783\n",
331 | " store %jl_value_t* %12, %jl_value_t** %6, align 8, !dbg !7783\n",
332 | " %16 = call %jl_value_t* @jl_apply_generic(%jl_value_t* inttoptr (i64 140671559565120 to %jl_value_t*), %jl_value_t** %5, i32 2), !dbg !7783\n",
333 | " store %jl_value_t* %16, %jl_value_t** %2, align 8, !dbg !7783\n",
334 | " %17 = icmp eq i64 %\"#s237.0\", %0, !dbg !7783\n",
335 | " br i1 %17, label %L3, label %L, !dbg !7783\n",
336 | "\n",
337 | "L3: ; preds = %L, %top\n",
338 | " %18 = phi %jl_value_t* [ inttoptr (i64 140671537323584 to %jl_value_t*), %top ], [ %16, %L ]\n",
339 | " %19 = load %jl_value_t** %4, align 8, !dbg !7784\n",
340 | " %20 = getelementptr inbounds %jl_value_t* %19, i64 0, i32 0, !dbg !7784\n",
341 | " store %jl_value_t** %20, %jl_value_t*** @jl_pgcstack, align 8, !dbg !7784\n",
342 | " ret %jl_value_t* %18, !dbg !7784\n",
343 | "}\n"
344 | ]
345 | }
346 | ],
347 | "prompt_number": 36
348 | },
349 | {
350 | "cell_type": "code",
351 | "collapsed": false,
352 | "input": [
353 | "code_llvm(sum2, (Int,))"
354 | ],
355 | "language": "python",
356 | "metadata": {},
357 | "outputs": [
358 | {
359 | "output_type": "stream",
360 | "stream": "stdout",
361 | "text": [
362 | "\n",
363 | "define double @julia_sum2_22396(i64) {\n",
364 | "top:\n",
365 | " %1 = icmp sgt i64 %0, 0, !dbg !7783\n",
366 | " br i1 %1, label %L, label %L3, !dbg !7783\n",
367 | "\n",
368 | "L: ; preds = %top, %L\n",
369 | " %total.0 = phi double [ %5, %L ], [ 0.000000e+00, %top ]\n",
370 | " %\"#s237.0\" = phi i64 [ %2, %L ], [ 1, %top ]\n",
371 | " %2 = add i64 %\"#s237.0\", 1, !dbg !7783\n",
372 | " %3 = sitofp i64 %\"#s237.0\" to double, !dbg !7784\n",
373 | " %4 = fmul double %3, 5.000000e-01, !dbg !7784\n",
374 | " %5 = fadd double %total.0, %4, !dbg !7784\n",
375 | " %6 = icmp eq i64 %\"#s237.0\", %0, !dbg !7784\n",
376 | " br i1 %6, label %L3, label %L, !dbg !7784\n",
377 | "\n",
378 | "L3: ; preds = %L, %top\n",
379 | " %total.1 = phi double [ 0.000000e+00, %top ], [ %5, %L ]\n",
380 | " ret double %total.1, !dbg !7785\n",
381 | "}\n"
382 | ]
383 | }
384 | ],
385 | "prompt_number": 37
386 | },
387 | {
388 | "cell_type": "markdown",
389 | "metadata": {},
390 | "source": [
391 | "####No Global variables\n",
392 | "Global variables are very slow in Julia, since the compiler cannot guarantee at any point that the value of such a variable does not change. Try to move all variables to function scope. Anything that absolutely must be global should be declared _const_"
393 | ]
394 | },
395 | {
396 | "cell_type": "markdown",
397 | "metadata": {},
398 | "source": [
399 | "## Calling C libraries from Julia\n",
400 | "Julia has a zero overhead ability to call any C library, using the _ccall_ function. "
401 | ]
402 | },
403 | {
404 | "cell_type": "code",
405 | "collapsed": false,
406 | "input": [
407 | "t=ccall( (:clock, \"libc\"), Cint, ())"
408 | ],
409 | "language": "python",
410 | "metadata": {},
411 | "outputs": [
412 | {
413 | "metadata": {},
414 | "output_type": "pyout",
415 | "prompt_number": 38,
416 | "text": [
417 | "35521476"
418 | ]
419 | }
420 | ],
421 | "prompt_number": 38
422 | },
423 | {
424 | "cell_type": "markdown",
425 | "metadata": {},
426 | "source": [
427 | "- Tuple of function name, library name\n",
428 | "- Type of return value\n",
429 | "- Tuple of types of function parameters\n",
430 | "- Function parameter values"
431 | ]
432 | },
433 | {
434 | "cell_type": "code",
435 | "collapsed": false,
436 | "input": [
437 | "ccall(:printf, Cint, (Ptr{Uint8},), \"Hello, world!\")"
438 | ],
439 | "language": "python",
440 | "metadata": {},
441 | "outputs": [
442 | {
443 | "output_type": "stream",
444 | "stream": "stdout",
445 | "text": [
446 | "Hello, world!"
447 | ]
448 | },
449 | {
450 | "metadata": {},
451 | "output_type": "pyout",
452 | "prompt_number": 39,
453 | "text": [
454 | "13"
455 | ]
456 | }
457 | ],
458 | "prompt_number": 39
459 | },
460 | {
461 | "cell_type": "code",
462 | "collapsed": false,
463 | "input": [
464 | "csin(x) = ccall((:sin,\"libm\"), Cdouble, (Cdouble,), x)"
465 | ],
466 | "language": "python",
467 | "metadata": {},
468 | "outputs": [
469 | {
470 | "metadata": {},
471 | "output_type": "pyout",
472 | "prompt_number": 40,
473 | "text": [
474 | "csin (generic function with 1 method)"
475 | ]
476 | }
477 | ],
478 | "prompt_number": 40
479 | },
480 | {
481 | "cell_type": "code",
482 | "collapsed": false,
483 | "input": [
484 | "csin(3)"
485 | ],
486 | "language": "python",
487 | "metadata": {},
488 | "outputs": [
489 | {
490 | "metadata": {},
491 | "output_type": "pyout",
492 | "prompt_number": 41,
493 | "text": [
494 | "0.1411200080598672"
495 | ]
496 | }
497 | ],
498 | "prompt_number": 41
499 | },
500 | {
501 | "cell_type": "code",
502 | "collapsed": false,
503 | "input": [
504 | "sin(3) - csin(3)"
505 | ],
506 | "language": "python",
507 | "metadata": {},
508 | "outputs": [
509 | {
510 | "metadata": {},
511 | "output_type": "pyout",
512 | "prompt_number": 42,
513 | "text": [
514 | "0.0"
515 | ]
516 | }
517 | ],
518 | "prompt_number": 42
519 | },
520 | {
521 | "cell_type": "markdown",
522 | "metadata": {},
523 | "source": [
524 | "##Calling Python Programs from Julia\n",
525 | "\n",
526 | "Thanks to the PyCall package, arbitrary python functions can be called using CPython's C API\n",
527 | "\n",
528 | "https://github.com/stevengj/PyCall.jl"
529 | ]
530 | },
531 | {
532 | "cell_type": "code",
533 | "collapsed": false,
534 | "input": [
535 | "using PyCall\n",
536 | "@pyimport math as pymath"
537 | ],
538 | "language": "python",
539 | "metadata": {},
540 | "outputs": [],
541 | "prompt_number": 43
542 | },
543 | {
544 | "cell_type": "code",
545 | "collapsed": false,
546 | "input": [
547 | "pymath.sin(3)"
548 | ],
549 | "language": "python",
550 | "metadata": {},
551 | "outputs": [
552 | {
553 | "metadata": {},
554 | "output_type": "pyout",
555 | "prompt_number": 44,
556 | "text": [
557 | "0.1411200080598672"
558 | ]
559 | }
560 | ],
561 | "prompt_number": 44
562 | },
563 | {
564 | "cell_type": "code",
565 | "collapsed": false,
566 | "input": [
567 | "sin(3) - pymath.sin(3)"
568 | ],
569 | "language": "python",
570 | "metadata": {},
571 | "outputs": [
572 | {
573 | "metadata": {},
574 | "output_type": "pyout",
575 | "prompt_number": 45,
576 | "text": [
577 | "0.0"
578 | ]
579 | }
580 | ],
581 | "prompt_number": 45
582 | },
583 | {
584 | "cell_type": "markdown",
585 | "metadata": {},
586 | "source": [
587 | "Arrays sent to python are copy free. Return arrays are copied. "
588 | ]
589 | },
590 | {
591 | "cell_type": "code",
592 | "collapsed": false,
593 | "input": [
594 | "@pyimport numpy as np\n",
595 | "x = [-100, 39, 59, 55, 20]\n",
596 | "np.irr(x)"
597 | ],
598 | "language": "python",
599 | "metadata": {},
600 | "outputs": [
601 | {
602 | "metadata": {},
603 | "output_type": "pyout",
604 | "prompt_number": 46,
605 | "text": [
606 | "0.2809484211599609"
607 | ]
608 | }
609 | ],
610 | "prompt_number": 46
611 | },
612 | {
613 | "cell_type": "markdown",
614 | "metadata": {},
615 | "source": [
616 | "####Passing Julia functions to python\n",
617 | "Useful for optimisation, root finding etc. "
618 | ]
619 | },
620 | {
621 | "cell_type": "code",
622 | "collapsed": false,
623 | "input": [
624 | "@pyimport scipy.optimize as so\n",
625 | "function f(x)\n",
626 | " println(\"Julia function called with $x\")\n",
627 | " cos(x) - x\n",
628 | "end\n",
629 | "so.newton(f, 1.2)"
630 | ],
631 | "language": "python",
632 | "metadata": {},
633 | "outputs": [
634 | {
635 | "output_type": "stream",
636 | "stream": "stdout",
637 | "text": [
638 | "Julia function called with 1.2\n"
639 | ]
640 | },
641 | {
642 | "output_type": "stream",
643 | "stream": "stdout",
644 | "text": [
645 | "Julia function called with 1.2002199999999998\n",
646 | "Julia function called with 0.7664554749111869\n",
647 | "Julia function called with 0.7412167885608414\n",
648 | "Julia function called with 0.7390978176492645\n",
649 | "Julia function called with 0.7390851391787693\n"
650 | ]
651 | },
652 | {
653 | "metadata": {},
654 | "output_type": "pyout",
655 | "prompt_number": 47,
656 | "text": [
657 | "0.7390851332151773"
658 | ]
659 | }
660 | ],
661 | "prompt_number": 47
662 | },
663 | {
664 | "cell_type": "markdown",
665 | "metadata": {},
666 | "source": [
667 | "Similary, there is JavaCall.jl, that allows calling Java programs from Julia, and a forthcoming RubyCall.jl"
668 | ]
669 | },
670 | {
671 | "cell_type": "markdown",
672 | "metadata": {},
673 | "source": [
674 | "##Interactive widgets\n",
675 | "\n",
676 | "Using a functional reactive framework, Interact.jl allows creation of interactive widgets in IJulia"
677 | ]
678 | },
679 | {
680 | "cell_type": "code",
681 | "collapsed": false,
682 | "input": [
683 | "using Interact"
684 | ],
685 | "language": "python",
686 | "metadata": {},
687 | "outputs": [],
688 | "prompt_number": 48
689 | },
690 | {
691 | "cell_type": "code",
692 | "collapsed": false,
693 | "input": [
694 | "@manipulate for n in 1:10\n",
695 | " rand(n,n)\n",
696 | "end"
697 | ],
698 | "language": "python",
699 | "metadata": {},
700 | "outputs": [
701 | {
702 | "html": [],
703 | "metadata": {},
704 | "output_type": "display_data",
705 | "text": [
706 | "Slider{Int64}([Input{Int64}] 5,\"n\",5,1:10)"
707 | ]
708 | },
709 | {
710 | "metadata": {
711 | "comm_id": "8f656e72-174b-4155-acb9-35c491714ca1",
712 | "reactive": true
713 | },
714 | "output_type": "pyout",
715 | "prompt_number": 49,
716 | "text": [
717 | "5x5 Array{Float64,2}:\n",
718 | " 0.312192 0.661945 0.794238 0.0409841 0.339624\n",
719 | " 0.184042 0.562061 0.65663 0.735989 0.138815\n",
720 | " 0.169089 0.259701 0.537516 0.692569 0.229201\n",
721 | " 0.203451 0.820103 0.977622 0.24926 0.385863\n",
722 | " 0.303323 0.75052 0.693575 0.120508 0.751001"
723 | ]
724 | }
725 | ],
726 | "prompt_number": 49
727 | },
728 | {
729 | "cell_type": "code",
730 | "collapsed": false,
731 | "input": [
732 | "using Color\n",
733 | "@manipulate for r in 0:0.1:1, g in 0:0.1:1, b in 0:0.1:1, n in 1:50\n",
734 | " linspace(RGB(0,0,0), RGB(r,g,b), n)\n",
735 | "end"
736 | ],
737 | "language": "python",
738 | "metadata": {},
739 | "outputs": [
740 | {
741 | "html": [],
742 | "metadata": {},
743 | "output_type": "display_data",
744 | "text": [
745 | "Slider{Float64}([Input{Float64}] 0.5,\"r\",0.5,0.0:0.1:1.0)"
746 | ]
747 | },
748 | {
749 | "html": [],
750 | "metadata": {},
751 | "output_type": "display_data",
752 | "text": [
753 | "Slider{Float64}([Input{Float64}] 0.5,\"g\",0.5,0.0:0.1:1.0)"
754 | ]
755 | },
756 | {
757 | "html": [],
758 | "metadata": {},
759 | "output_type": "display_data",
760 | "text": [
761 | "Slider{Float64}([Input{Float64}] 0.5,\"b\",0.5,0.0:0.1:1.0)"
762 | ]
763 | },
764 | {
765 | "html": [],
766 | "metadata": {},
767 | "output_type": "display_data",
768 | "text": [
769 | "Slider{Int64}([Input{Int64}] 25,\"n\",25,1:50)"
770 | ]
771 | },
772 | {
773 | "metadata": {
774 | "comm_id": "6e0eca7b-1f3a-4b4e-a751-35cec713fbde",
775 | "reactive": true
776 | },
777 | "output_type": "pyout",
778 | "prompt_number": 50,
779 | "svg": [
780 | "\n",
781 | "\n",
783 | "\n",
786 | " \n",
789 | " \n",
792 | " \n",
795 | " \n",
798 | " \n",
801 | " \n",
804 | " \n",
807 | " \n",
810 | " \n",
813 | " \n",
816 | " \n",
819 | " \n",
822 | " \n",
825 | " \n",
828 | " \n",
831 | " \n",
834 | " \n",
837 | " \n",
840 | " \n",
843 | " \n",
846 | " \n",
849 | " \n",
852 | " \n",
855 | " \n",
858 | " \n",
861 | " "
862 | ],
863 | "text": [
864 | "25-element Array{RGB{Float64},1}:\n",
865 | " RGB{Float64}(0.0,0.0,0.0) \n",
866 | " RGB{Float64}(0.020833333333333315,0.020833333333333315,0.020833333333333315)\n",
867 | " RGB{Float64}(0.041666666666666685,0.041666666666666685,0.041666666666666685)\n",
868 | " RGB{Float64}(0.0625,0.0625,0.0625) \n",
869 | " RGB{Float64}(0.08333333333333331,0.08333333333333331,0.08333333333333331) \n",
870 | " RGB{Float64}(0.10416666666666669,0.10416666666666669,0.10416666666666669) \n",
871 | " RGB{Float64}(0.125,0.125,0.125) \n",
872 | " RGB{Float64}(0.14583333333333331,0.14583333333333331,0.14583333333333331) \n",
873 | " RGB{Float64}(0.16666666666666669,0.16666666666666669,0.16666666666666669) \n",
874 | " RGB{Float64}(0.1875,0.1875,0.1875) \n",
875 | " RGB{Float64}(0.20833333333333331,0.20833333333333331,0.20833333333333331) \n",
876 | " RGB{Float64}(0.22916666666666669,0.22916666666666669,0.22916666666666669) \n",
877 | " RGB{Float64}(0.25,0.25,0.25) \n",
878 | " RGB{Float64}(0.27083333333333337,0.27083333333333337,0.27083333333333337) \n",
879 | " RGB{Float64}(0.29166666666666663,0.29166666666666663,0.29166666666666663) \n",
880 | " RGB{Float64}(0.3125,0.3125,0.3125) \n",
881 | " RGB{Float64}(0.33333333333333337,0.33333333333333337,0.33333333333333337) \n",
882 | " RGB{Float64}(0.35416666666666663,0.35416666666666663,0.35416666666666663) \n",
883 | " RGB{Float64}(0.375,0.375,0.375) \n",
884 | " RGB{Float64}(0.3958333333333333,0.3958333333333333,0.3958333333333333) \n",
885 | " RGB{Float64}(0.4166666666666667,0.4166666666666667,0.4166666666666667) \n",
886 | " RGB{Float64}(0.4375,0.4375,0.4375) \n",
887 | " RGB{Float64}(0.4583333333333333,0.4583333333333333,0.4583333333333333) \n",
888 | " RGB{Float64}(0.4791666666666667,0.4791666666666667,0.4791666666666667) \n",
889 | " RGB{Float64}(0.5,0.5,0.5) "
890 | ]
891 | }
892 | ],
893 | "prompt_number": 50
894 | },
895 | {
896 | "cell_type": "code",
897 | "collapsed": false,
898 | "input": [
899 | "using SymPy\n",
900 | "x = sym\"x\"\n",
901 | "@manipulate for n=0:10\n",
902 | " latex(SymPy.diff(sin(x^2), x, n))\n",
903 | "end"
904 | ],
905 | "language": "python",
906 | "metadata": {},
907 | "outputs": [
908 | {
909 | "html": [],
910 | "metadata": {},
911 | "output_type": "display_data",
912 | "text": [
913 | "Slider{Int64}([Input{Int64}] 5,\"n\",5,0:10)"
914 | ]
915 | },
916 | {
917 | "html": [],
918 | "metadata": {},
919 | "output_type": "pyout",
920 | "prompt_number": 56,
921 | "text": [
922 | "Latex(\"\",\"\\$\\$8 x \\\\left(4 x^{4} \\\\cos{\\\\left (x^{2} \\\\right )} + 20 x^{2} \\\\sin{\\\\left (x^{2} \\\\right )} - 15 \\\\cos{\\\\left (x^{2} \\\\right )}\\\\right)\\$\\$\")"
923 | ]
924 | }
925 | ],
926 | "prompt_number": 56
927 | },
928 | {
929 | "cell_type": "markdown",
930 | "metadata": {},
931 | "source": [
932 | "##Acknowledgements\n",
933 | "\n",
934 | "- https://github.com/stevengj/Julia-EuroSciPy14\n",
935 | "- http://github.com/dpsanders/scipy_2014_julia\n",
936 | "- http://arxiv.org/abs/1411.1607"
937 | ]
938 | },
939 | {
940 | "cell_type": "code",
941 | "collapsed": false,
942 | "input": [],
943 | "language": "python",
944 | "metadata": {},
945 | "outputs": [],
946 | "prompt_number": 52
947 | }
948 | ],
949 | "metadata": {}
950 | }
951 | ]
952 | }
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## Programming for a Number Driven World
2 | ####A Julia Tutorial
3 |
4 | The content in this repository are [IJulia](https://github.com/JuliaLang/IJulia.jl) files.
5 |
6 | To run the code without installing Julia, sign in to [JuliaBox](https://juliabox.org) and sync this repository.
7 |
8 | To simply view the notebook, without interactivity, go to [NBViewer](http://nbviewer.ipython.org/github/aviks/learn-julia/tree/master/)
9 |
--------------------------------------------------------------------------------
/fib.jl:
--------------------------------------------------------------------------------
1 | function fib(n)
2 | if n < 2
3 | return n
4 | else
5 | return fib(n-1) + fib(n-2)
6 | end
7 | end
8 |
--------------------------------------------------------------------------------