├── LICENSE.md ├── README.md ├── during_sessions ├── Fractale de Newton.ipynb ├── Modules.ipynb ├── MyModule.jl ├── Session 1 discussions.ipynb └── Session II discussions.ipynb ├── installing.md ├── notebooks ├── 0. Why Julia.ipynb ├── Session I - Basic Julia │ ├── 1. Numbers, variables and basic functions.ipynb │ ├── 2. Iteration - ranges, vectors and conditionals.ipynb │ ├── 3. Packages and visualization with PyPlot.ipynb │ ├── 4. Vectors.ipynb │ ├── 5. Matrices.ipynb │ ├── 6. Interactivity using Interact.jl.ipynb │ ├── 7. Functions.ipynb │ └── 8. Funny but useful syntax.ipynb └── Session II - Intermediate Julia │ ├── 1. Structuring Julia code.ipynb │ ├── 2. Defining new types.ipynb │ ├── 3. Parametrised types.ipynb │ └── 4. Testing.ipynb └── outline.md /LICENSE.md: -------------------------------------------------------------------------------- 1 | The "Hands-on Julia" materials are licensed under the following licenses: 2 | 3 | - the **code** portions under the MIT "Expat" License; 4 | 5 | - **everything else** under the CC-by-SA 4.0 License: 6 | 7 | 8 | ## MIT "Expat" License for the code portions: 9 | 10 | > Copyright (c) 2016: David Sanders. 11 | > 12 | > Permission is hereby granted, free of charge, to any person obtaining 13 | > a copy of this software and associated documentation files (the 14 | > "Software"), to deal in the Software without restriction, including 15 | > without limitation the rights to use, copy, modify, merge, publish, 16 | > distribute, sublicense, and/or sell copies of the Software, and to 17 | > permit persons to whom the Software is furnished to do so, subject to 18 | > the following conditions: 19 | > 20 | > The above copyright notice and this permission notice shall be 21 | > included in all copies or substantial portions of the Software. 22 | > 23 | > THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 24 | > EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 25 | > MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 26 | > IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 27 | > CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 28 | > TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 29 | > SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 30 | 31 | ## CC by SA 4.0 for the non-code portions: 32 | Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. 33 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Hands on Julia 2 | 3 | Material for a 2-day workshop on Julia, first given at the 4 | Université de Paris-Sud on June 16th and 17th, 2015. 5 | 6 | The material consists of directed exercises, aimed to 7 | provide a "hands-on" way of learning Julia by getting the 8 | user to try out ideas semi-autonomously, as opposed to 9 | standard tutorials, which tend to give all the answers 10 | immediately. 11 | 12 | David Sanders was the on-site instructor for both days, 13 | helping sort out installation problems, asking questions, 14 | and discussing various points at the front of the class. 15 | 16 | ## Acknowledgements 17 | 18 | Financial support is acknowledged from DGAPA-UNAM PAPIME grant PE-107114, and DGAPA-UNAM PAPIIT grant IN-117214. 19 | -------------------------------------------------------------------------------- /during_sessions/Modules.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": false 8 | }, 9 | "outputs": [ 10 | { 11 | "name": "stdout", 12 | "output_type": "stream", 13 | "text": [ 14 | "Fractale de Newton.ipynb\n", 15 | "Modules.ipynb\n", 16 | "MyModule.jl\n", 17 | "Session 1 discussions.ipynb\n", 18 | "Session II discussions.ipynb\n" 19 | ] 20 | } 21 | ], 22 | "source": [ 23 | ";ls" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 1, 29 | "metadata": { 30 | "collapsed": true 31 | }, 32 | "outputs": [], 33 | "source": [ 34 | "using MyModule" 35 | ] 36 | }, 37 | { 38 | "cell_type": "code", 39 | "execution_count": 3, 40 | "metadata": { 41 | "collapsed": false 42 | }, 43 | "outputs": [ 44 | { 45 | "data": { 46 | "text/plain": [ 47 | "MyModule" 48 | ] 49 | }, 50 | "execution_count": 3, 51 | "metadata": {}, 52 | "output_type": "execute_result" 53 | } 54 | ], 55 | "source": [ 56 | "MyModule" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 4, 62 | "metadata": { 63 | "collapsed": false 64 | }, 65 | "outputs": [ 66 | { 67 | "data": { 68 | "text/plain": [ 69 | "Module" 70 | ] 71 | }, 72 | "execution_count": 4, 73 | "metadata": {}, 74 | "output_type": "execute_result" 75 | } 76 | ], 77 | "source": [ 78 | "typeof(ans)" 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": 5, 84 | "metadata": { 85 | "collapsed": false 86 | }, 87 | "outputs": [ 88 | { 89 | "data": { 90 | "text/plain": [ 91 | "MyType (constructor with 2 methods)" 92 | ] 93 | }, 94 | "execution_count": 5, 95 | "metadata": {}, 96 | "output_type": "execute_result" 97 | } 98 | ], 99 | "source": [ 100 | "MyModule.MyType" 101 | ] 102 | }, 103 | { 104 | "cell_type": "code", 105 | "execution_count": 6, 106 | "metadata": { 107 | "collapsed": false 108 | }, 109 | "outputs": [ 110 | { 111 | "ename": "LoadError", 112 | "evalue": "MyType not defined\nwhile loading In[6], in expression starting on line 1", 113 | "output_type": "error", 114 | "traceback": [ 115 | "MyType not defined\nwhile loading In[6], in expression starting on line 1", 116 | "" 117 | ] 118 | } 119 | ], 120 | "source": [ 121 | "MyType" 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "execution_count": 7, 127 | "metadata": { 128 | "collapsed": false 129 | }, 130 | "outputs": [ 131 | { 132 | "name": "stdout", 133 | "output_type": "stream", 134 | "text": [ 135 | "Base Module\n", 136 | "Compat Module\n", 137 | "Core Module\n", 138 | "DataStructures Module\n", 139 | "Docile Module\n", 140 | "IJulia Module\n", 141 | "IPythonDisplay Module\n", 142 | "JSON Module\n", 143 | "Main Module\n", 144 | "MyModule Module\n", 145 | "Nettle Module\n", 146 | "ZMQ Module\n" 147 | ] 148 | } 149 | ], 150 | "source": [ 151 | "whos()" 152 | ] 153 | }, 154 | { 155 | "cell_type": "code", 156 | "execution_count": 8, 157 | "metadata": { 158 | "collapsed": false 159 | }, 160 | "outputs": [ 161 | { 162 | "data": { 163 | "text/plain": [ 164 | "3" 165 | ] 166 | }, 167 | "execution_count": 8, 168 | "metadata": {}, 169 | "output_type": "execute_result" 170 | } 171 | ], 172 | "source": [ 173 | "a= 3" 174 | ] 175 | }, 176 | { 177 | "cell_type": "code", 178 | "execution_count": 9, 179 | "metadata": { 180 | "collapsed": false 181 | }, 182 | "outputs": [ 183 | { 184 | "name": "stdout", 185 | "output_type": "stream", 186 | "text": [ 187 | "Base Module\n", 188 | "Compat Module\n", 189 | "Core Module\n", 190 | "DataStructures Module\n", 191 | "Docile Module\n", 192 | "IJulia Module\n", 193 | "IPythonDisplay Module\n", 194 | "JSON Module\n", 195 | "Main Module\n", 196 | "MyModule Module\n", 197 | "Nettle Module\n", 198 | "ZMQ Module\n", 199 | "a Int64\n" 200 | ] 201 | } 202 | ], 203 | "source": [ 204 | "whos()" 205 | ] 206 | }, 207 | { 208 | "cell_type": "code", 209 | "execution_count": 2, 210 | "metadata": { 211 | "collapsed": false 212 | }, 213 | "outputs": [ 214 | { 215 | "name": "stdout", 216 | "output_type": "stream", 217 | "text": [ 218 | "Base Module\n", 219 | "Compat Module\n", 220 | "Core Module\n", 221 | "DataStructures Module\n", 222 | "Docile Module\n", 223 | "IJulia Module\n", 224 | "IPythonDisplay Module\n", 225 | "JSON Module\n", 226 | "Main Module\n", 227 | "MyModule Module\n", 228 | "Nettle Module\n", 229 | "ZMQ Module\n" 230 | ] 231 | } 232 | ], 233 | "source": [ 234 | "whos()" 235 | ] 236 | }, 237 | { 238 | "cell_type": "code", 239 | "execution_count": 3, 240 | "metadata": { 241 | "collapsed": false 242 | }, 243 | "outputs": [ 244 | { 245 | "data": { 246 | "text/plain": [ 247 | "MyType (constructor with 2 methods)" 248 | ] 249 | }, 250 | "execution_count": 3, 251 | "metadata": {}, 252 | "output_type": "execute_result" 253 | } 254 | ], 255 | "source": [ 256 | "MyType" 257 | ] 258 | }, 259 | { 260 | "cell_type": "code", 261 | "execution_count": 6, 262 | "metadata": { 263 | "collapsed": false 264 | }, 265 | "outputs": [ 266 | { 267 | "data": { 268 | "text/plain": [ 269 | "MyType(100.0)" 270 | ] 271 | }, 272 | "execution_count": 6, 273 | "metadata": {}, 274 | "output_type": "execute_result" 275 | } 276 | ], 277 | "source": [ 278 | "t = MyType(100)" 279 | ] 280 | }, 281 | { 282 | "cell_type": "code", 283 | "execution_count": 7, 284 | "metadata": { 285 | "collapsed": false 286 | }, 287 | "outputs": [ 288 | { 289 | "data": { 290 | "text/plain": [ 291 | "MyType(100.0)" 292 | ] 293 | }, 294 | "execution_count": 7, 295 | "metadata": {}, 296 | "output_type": "execute_result" 297 | } 298 | ], 299 | "source": [ 300 | "t" 301 | ] 302 | }, 303 | { 304 | "cell_type": "code", 305 | "execution_count": 8, 306 | "metadata": { 307 | "collapsed": false 308 | }, 309 | "outputs": [ 310 | { 311 | "data": { 312 | "text/plain": [ 313 | "10.0" 314 | ] 315 | }, 316 | "execution_count": 8, 317 | "metadata": {}, 318 | "output_type": "execute_result" 319 | } 320 | ], 321 | "source": [ 322 | "f!(t)" 323 | ] 324 | }, 325 | { 326 | "cell_type": "code", 327 | "execution_count": 9, 328 | "metadata": { 329 | "collapsed": false 330 | }, 331 | "outputs": [ 332 | { 333 | "data": { 334 | "text/plain": [ 335 | "MyType(10.0)" 336 | ] 337 | }, 338 | "execution_count": 9, 339 | "metadata": {}, 340 | "output_type": "execute_result" 341 | } 342 | ], 343 | "source": [ 344 | "t" 345 | ] 346 | }, 347 | { 348 | "cell_type": "code", 349 | "execution_count": 1, 350 | "metadata": { 351 | "collapsed": false 352 | }, 353 | "outputs": [], 354 | "source": [ 355 | "using MyModule" 356 | ] 357 | }, 358 | { 359 | "cell_type": "code", 360 | "execution_count": 2, 361 | "metadata": { 362 | "collapsed": false 363 | }, 364 | "outputs": [ 365 | { 366 | "data": { 367 | "text/plain": [ 368 | "MyType(10.0)" 369 | ] 370 | }, 371 | "execution_count": 2, 372 | "metadata": {}, 373 | "output_type": "execute_result" 374 | } 375 | ], 376 | "source": [ 377 | "t = MyType(10)" 378 | ] 379 | }, 380 | { 381 | "cell_type": "code", 382 | "execution_count": 3, 383 | "metadata": { 384 | "collapsed": false 385 | }, 386 | "outputs": [ 387 | { 388 | "data": { 389 | "text/plain": [ 390 | "-0.5440211108893698" 391 | ] 392 | }, 393 | "execution_count": 3, 394 | "metadata": {}, 395 | "output_type": "execute_result" 396 | } 397 | ], 398 | "source": [ 399 | "sin(t)" 400 | ] 401 | }, 402 | { 403 | "cell_type": "code", 404 | "execution_count": 19, 405 | "metadata": { 406 | "collapsed": true 407 | }, 408 | "outputs": [], 409 | "source": [ 410 | "workspace()\n", 411 | "reload(\"MyModule\")\n", 412 | "using MyModule" 413 | ] 414 | }, 415 | { 416 | "cell_type": "code", 417 | "execution_count": 1, 418 | "metadata": { 419 | "collapsed": false 420 | }, 421 | "outputs": [], 422 | "source": [ 423 | "using MyModule" 424 | ] 425 | }, 426 | { 427 | "cell_type": "code", 428 | "execution_count": 16, 429 | "metadata": { 430 | "collapsed": true 431 | }, 432 | "outputs": [], 433 | "source": [] 434 | }, 435 | { 436 | "cell_type": "code", 437 | "execution_count": 2, 438 | "metadata": { 439 | "collapsed": false 440 | }, 441 | "outputs": [ 442 | { 443 | "data": { 444 | "text/plain": [ 445 | "MyType(10.0)" 446 | ] 447 | }, 448 | "execution_count": 2, 449 | "metadata": {}, 450 | "output_type": "execute_result" 451 | } 452 | ], 453 | "source": [ 454 | "t = MyType(10)" 455 | ] 456 | }, 457 | { 458 | "cell_type": "code", 459 | "execution_count": 3, 460 | "metadata": { 461 | "collapsed": false 462 | }, 463 | "outputs": [ 464 | { 465 | "data": { 466 | "text/plain": [ 467 | "-0.8390715290764524" 468 | ] 469 | }, 470 | "execution_count": 3, 471 | "metadata": {}, 472 | "output_type": "execute_result" 473 | } 474 | ], 475 | "source": [ 476 | "cos(t)" 477 | ] 478 | }, 479 | { 480 | "cell_type": "code", 481 | "execution_count": 4, 482 | "metadata": { 483 | "collapsed": false 484 | }, 485 | "outputs": [ 486 | { 487 | "data": { 488 | "text/plain": [ 489 | "86" 490 | ] 491 | }, 492 | "execution_count": 4, 493 | "metadata": {}, 494 | "output_type": "execute_result" 495 | } 496 | ], 497 | "source": [ 498 | "v = [3, 4, 5]\n", 499 | "w = [6, 7, 8]\n", 500 | "\n", 501 | "v ⋅ w" 502 | ] 503 | }, 504 | { 505 | "cell_type": "code", 506 | "execution_count": 5, 507 | "metadata": { 508 | "collapsed": false 509 | }, 510 | "outputs": [ 511 | { 512 | "data": { 513 | "text/plain": [ 514 | "dot (generic function with 7 methods)" 515 | ] 516 | }, 517 | "execution_count": 5, 518 | "metadata": {}, 519 | "output_type": "execute_result" 520 | } 521 | ], 522 | "source": [ 523 | "⋅" 524 | ] 525 | }, 526 | { 527 | "cell_type": "code", 528 | "execution_count": 6, 529 | "metadata": { 530 | "collapsed": false 531 | }, 532 | "outputs": [ 533 | { 534 | "data": { 535 | "text/plain": [ 536 | "86" 537 | ] 538 | }, 539 | "execution_count": 6, 540 | "metadata": {}, 541 | "output_type": "execute_result" 542 | } 543 | ], 544 | "source": [ 545 | "dot(v, w)" 546 | ] 547 | }, 548 | { 549 | "cell_type": "code", 550 | "execution_count": 7, 551 | "metadata": { 552 | "collapsed": false 553 | }, 554 | "outputs": [ 555 | { 556 | "data": { 557 | "text/plain": [ 558 | "86" 559 | ] 560 | }, 561 | "execution_count": 7, 562 | "metadata": {}, 563 | "output_type": "execute_result" 564 | } 565 | ], 566 | "source": [ 567 | "⋅(v, w)" 568 | ] 569 | }, 570 | { 571 | "cell_type": "code", 572 | "execution_count": 8, 573 | "metadata": { 574 | "collapsed": false 575 | }, 576 | "outputs": [ 577 | { 578 | "ename": "LoadError", 579 | "evalue": "syntax: extra token \"dot\" after end of expression\nwhile loading In[8], in expression starting on line 1", 580 | "output_type": "error", 581 | "traceback": [ 582 | "syntax: extra token \"dot\" after end of expression\nwhile loading In[8], in expression starting on line 1", 583 | "" 584 | ] 585 | } 586 | ], 587 | "source": [ 588 | "v dot w" 589 | ] 590 | }, 591 | { 592 | "cell_type": "code", 593 | "execution_count": 9, 594 | "metadata": { 595 | "collapsed": false 596 | }, 597 | "outputs": [ 598 | { 599 | "ename": "LoadError", 600 | "evalue": "AutoDiff not defined\nwhile loading In[9], in expression starting on line 1", 601 | "output_type": "error", 602 | "traceback": [ 603 | "AutoDiff not defined\nwhile loading In[9], in expression starting on line 1", 604 | "" 605 | ] 606 | } 607 | ], 608 | "source": [ 609 | "-(x::AutoDiff) = AutoDiff(-x.u, -x.u′)" 610 | ] 611 | }, 612 | { 613 | "cell_type": "markdown", 614 | "metadata": {}, 615 | "source": [ 616 | "# Macro " 617 | ] 618 | }, 619 | { 620 | "cell_type": "markdown", 621 | "metadata": {}, 622 | "source": [ 623 | "Un **macro** s'écrit avec un `@`:\n", 624 | "\n", 625 | "une fonction qui prend comme argument un morceau de code, le transforme, et renvoie un autre morceau de code." 626 | ] 627 | }, 628 | { 629 | "cell_type": "code", 630 | "execution_count": 32, 631 | "metadata": { 632 | "collapsed": false 633 | }, 634 | "outputs": [], 635 | "source": [ 636 | "macro replace(expr)\n", 637 | " @show expr\n", 638 | " expr.args[2] = :z\n", 639 | " \n", 640 | " expr\n", 641 | "end" 642 | ] 643 | }, 644 | { 645 | "cell_type": "code", 646 | "execution_count": 31, 647 | "metadata": { 648 | "collapsed": false 649 | }, 650 | "outputs": [ 651 | { 652 | "data": { 653 | "text/plain": [ 654 | "4" 655 | ] 656 | }, 657 | "execution_count": 31, 658 | "metadata": {}, 659 | "output_type": "execute_result" 660 | } 661 | ], 662 | "source": [ 663 | "z" 664 | ] 665 | }, 666 | { 667 | "cell_type": "code", 668 | "execution_count": 33, 669 | "metadata": { 670 | "collapsed": false 671 | }, 672 | "outputs": [ 673 | { 674 | "name": "stdout", 675 | "output_type": "stream", 676 | "text": [ 677 | "expr => :(x + y)\n" 678 | ] 679 | }, 680 | { 681 | "data": { 682 | "text/plain": [ 683 | "7" 684 | ] 685 | }, 686 | "execution_count": 33, 687 | "metadata": {}, 688 | "output_type": "execute_result" 689 | } 690 | ], 691 | "source": [ 692 | "@replace x + y" 693 | ] 694 | }, 695 | { 696 | "cell_type": "code", 697 | "execution_count": 22, 698 | "metadata": { 699 | "collapsed": false 700 | }, 701 | "outputs": [ 702 | { 703 | "ename": "LoadError", 704 | "evalue": "x not defined\nwhile loading In[22], in expression starting on line 1", 705 | "output_type": "error", 706 | "traceback": [ 707 | "x not defined\nwhile loading In[22], in expression starting on line 1", 708 | "" 709 | ] 710 | } 711 | ], 712 | "source": [ 713 | "eval(:(x+y))" 714 | ] 715 | }, 716 | { 717 | "cell_type": "code", 718 | "execution_count": 23, 719 | "metadata": { 720 | "collapsed": false 721 | }, 722 | "outputs": [ 723 | { 724 | "data": { 725 | "text/plain": [ 726 | "4" 727 | ] 728 | }, 729 | "execution_count": 23, 730 | "metadata": {}, 731 | "output_type": "execute_result" 732 | } 733 | ], 734 | "source": [ 735 | "y = 3; z = 4" 736 | ] 737 | }, 738 | { 739 | "cell_type": "code", 740 | "execution_count": 26, 741 | "metadata": { 742 | "collapsed": false 743 | }, 744 | "outputs": [ 745 | { 746 | "name": "stdout", 747 | "output_type": "stream", 748 | "text": [ 749 | "expr => :(x + @replace x + y)\n", 750 | "expr => :(z + @replace x + y)\n", 751 | "expr => :(x + y)\n", 752 | "expr => :(z + y)\n" 753 | ] 754 | }, 755 | { 756 | "data": { 757 | "text/plain": [ 758 | "11" 759 | ] 760 | }, 761 | "execution_count": 26, 762 | "metadata": {}, 763 | "output_type": "execute_result" 764 | } 765 | ], 766 | "source": [ 767 | "@replace x + @replace x + y" 768 | ] 769 | }, 770 | { 771 | "cell_type": "code", 772 | "execution_count": 13, 773 | "metadata": { 774 | "collapsed": false 775 | }, 776 | "outputs": [ 777 | { 778 | "data": { 779 | "text/plain": [ 780 | ":(x + y)" 781 | ] 782 | }, 783 | "execution_count": 13, 784 | "metadata": {}, 785 | "output_type": "execute_result" 786 | } 787 | ], 788 | "source": [ 789 | "macroexpand(:(@replace x+y))" 790 | ] 791 | }, 792 | { 793 | "cell_type": "code", 794 | "execution_count": 14, 795 | "metadata": { 796 | "collapsed": false 797 | }, 798 | "outputs": [ 799 | { 800 | "data": { 801 | "text/plain": [ 802 | ":(@replace x + y)" 803 | ] 804 | }, 805 | "execution_count": 14, 806 | "metadata": {}, 807 | "output_type": "execute_result" 808 | } 809 | ], 810 | "source": [ 811 | ":(@replace x+y)" 812 | ] 813 | }, 814 | { 815 | "cell_type": "code", 816 | "execution_count": 28, 817 | "metadata": { 818 | "collapsed": false 819 | }, 820 | "outputs": [ 821 | { 822 | "name": "stdout", 823 | "output_type": "stream", 824 | "text": [ 825 | "y => 3\n" 826 | ] 827 | }, 828 | { 829 | "data": { 830 | "text/plain": [ 831 | "3" 832 | ] 833 | }, 834 | "execution_count": 28, 835 | "metadata": {}, 836 | "output_type": "execute_result" 837 | } 838 | ], 839 | "source": [ 840 | "@show y" 841 | ] 842 | }, 843 | { 844 | "cell_type": "code", 845 | "execution_count": 36, 846 | "metadata": { 847 | "collapsed": false 848 | }, 849 | "outputs": [ 850 | { 851 | "data": { 852 | "text/plain": [ 853 | "quote # util.jl, line 53:\n", 854 | " local #434#b0 = Base.gc_bytes() # line 54:\n", 855 | " local #435#t0 = Base.time_ns() # line 55:\n", 856 | " local #436#g0 = Base.gc_time_ns() # line 56:\n", 857 | " local #437#val = sin(10) # line 57:\n", 858 | " local #438#g1 = Base.gc_time_ns() # line 58:\n", 859 | " local #439#t1 = Base.time_ns() # line 59:\n", 860 | " local #440#b1 = Base.gc_bytes() # line 60:\n", 861 | " Base.time_print(Base.-(#439#t1,#435#t0),Base.-(#440#b1,#434#b0),Base.-(#438#g1,#436#g0)) # line 61:\n", 862 | " #437#val\n", 863 | "end" 864 | ] 865 | }, 866 | "execution_count": 36, 867 | "metadata": {}, 868 | "output_type": "execute_result" 869 | } 870 | ], 871 | "source": [ 872 | "macroexpand( :(@time sin(10)) )" 873 | ] 874 | }, 875 | { 876 | "cell_type": "code", 877 | "execution_count": 37, 878 | "metadata": { 879 | "collapsed": false 880 | }, 881 | "outputs": [ 882 | { 883 | "data": { 884 | "text/plain": [ 885 | "0x000050999ddbfb8c" 886 | ] 887 | }, 888 | "execution_count": 37, 889 | "metadata": {}, 890 | "output_type": "execute_result" 891 | } 892 | ], 893 | "source": [ 894 | "t1 = Base.time_ns()" 895 | ] 896 | }, 897 | { 898 | "cell_type": "code", 899 | "execution_count": 38, 900 | "metadata": { 901 | "collapsed": false 902 | }, 903 | "outputs": [ 904 | { 905 | "data": { 906 | "text/plain": [ 907 | "0x0000509aaca5417f" 908 | ] 909 | }, 910 | "execution_count": 38, 911 | "metadata": {}, 912 | "output_type": "execute_result" 913 | } 914 | ], 915 | "source": [ 916 | "t2 = Base.time_ns()" 917 | ] 918 | }, 919 | { 920 | "cell_type": "code", 921 | "execution_count": 39, 922 | "metadata": { 923 | "collapsed": false 924 | }, 925 | "outputs": [ 926 | { 927 | "data": { 928 | "text/plain": [ 929 | "0x000000010ec945f3" 930 | ] 931 | }, 932 | "execution_count": 39, 933 | "metadata": {}, 934 | "output_type": "execute_result" 935 | } 936 | ], 937 | "source": [ 938 | "t2 - t1" 939 | ] 940 | }, 941 | { 942 | "cell_type": "code", 943 | "execution_count": 40, 944 | "metadata": { 945 | "collapsed": false 946 | }, 947 | "outputs": [ 948 | { 949 | "data": { 950 | "text/plain": [ 951 | "4543038963" 952 | ] 953 | }, 954 | "execution_count": 40, 955 | "metadata": {}, 956 | "output_type": "execute_result" 957 | } 958 | ], 959 | "source": [ 960 | "int(ans)" 961 | ] 962 | }, 963 | { 964 | "cell_type": "code", 965 | "execution_count": 41, 966 | "metadata": { 967 | "collapsed": false 968 | }, 969 | "outputs": [ 970 | { 971 | "data": { 972 | "text/plain": [ 973 | "0x0000509e66537c44" 974 | ] 975 | }, 976 | "execution_count": 41, 977 | "metadata": {}, 978 | "output_type": "execute_result" 979 | } 980 | ], 981 | "source": [ 982 | "tic()" 983 | ] 984 | }, 985 | { 986 | "cell_type": "code", 987 | "execution_count": 42, 988 | "metadata": { 989 | "collapsed": false 990 | }, 991 | "outputs": [ 992 | { 993 | "name": "stdout", 994 | "output_type": "stream", 995 | "text": [ 996 | "elapsed time: 0.97959562 seconds\n" 997 | ] 998 | }, 999 | { 1000 | "data": { 1001 | "text/plain": [ 1002 | "0.97959562" 1003 | ] 1004 | }, 1005 | "execution_count": 42, 1006 | "metadata": {}, 1007 | "output_type": "execute_result" 1008 | } 1009 | ], 1010 | "source": [ 1011 | "toc()" 1012 | ] 1013 | }, 1014 | { 1015 | "cell_type": "code", 1016 | "execution_count": 43, 1017 | "metadata": { 1018 | "collapsed": true 1019 | }, 1020 | "outputs": [], 1021 | "source": [ 1022 | "macro bonjour(a, b)\n", 1023 | " @show a\n", 1024 | " @show b\n", 1025 | "end" 1026 | ] 1027 | }, 1028 | { 1029 | "cell_type": "code", 1030 | "execution_count": 46, 1031 | "metadata": { 1032 | "collapsed": false 1033 | }, 1034 | "outputs": [ 1035 | { 1036 | "name": "stdout", 1037 | "output_type": "stream", 1038 | "text": [ 1039 | "a => quote # In[46], line 1:\n", 1040 | " a = 3 # line 1:\n", 1041 | " 2a\n", 1042 | "end\n", 1043 | "b => :b\n" 1044 | ] 1045 | }, 1046 | { 1047 | "ename": "LoadError", 1048 | "evalue": "b not defined\nwhile loading In[46], in expression starting on line 1", 1049 | "output_type": "error", 1050 | "traceback": [ 1051 | "b not defined\nwhile loading In[46], in expression starting on line 1", 1052 | "" 1053 | ] 1054 | } 1055 | ], 1056 | "source": [ 1057 | "@bonjour begin a = 3; 2a end b" 1058 | ] 1059 | }, 1060 | { 1061 | "cell_type": "markdown", 1062 | "metadata": {}, 1063 | "source": [ 1064 | "# Exemple de macros " 1065 | ] 1066 | }, 1067 | { 1068 | "cell_type": "code", 1069 | "execution_count": 48, 1070 | "metadata": { 1071 | "collapsed": false 1072 | }, 1073 | "outputs": [ 1074 | { 1075 | "name": "stderr", 1076 | "output_type": "stream", 1077 | "text": [ 1078 | "INFO: Nothing to be done\n" 1079 | ] 1080 | } 1081 | ], 1082 | "source": [ 1083 | "Pkg.add(\"ValidatedNumerics\")" 1084 | ] 1085 | }, 1086 | { 1087 | "cell_type": "code", 1088 | "execution_count": 6, 1089 | "metadata": { 1090 | "collapsed": true 1091 | }, 1092 | "outputs": [], 1093 | "source": [ 1094 | "using ValidatedNumerics" 1095 | ] 1096 | }, 1097 | { 1098 | "cell_type": "code", 1099 | "execution_count": 7, 1100 | "metadata": { 1101 | "collapsed": false 1102 | }, 1103 | "outputs": [ 1104 | { 1105 | "data": { 1106 | "text/plain": [ 1107 | "[1, 2]" 1108 | ] 1109 | }, 1110 | "execution_count": 7, 1111 | "metadata": {}, 1112 | "output_type": "execute_result" 1113 | } 1114 | ], 1115 | "source": [ 1116 | "Interval(1, 2)" 1117 | ] 1118 | }, 1119 | { 1120 | "cell_type": "code", 1121 | "execution_count": 3, 1122 | "metadata": { 1123 | "collapsed": false 1124 | }, 1125 | "outputs": [], 1126 | "source": [ 1127 | "type MyType\n", 1128 | " x\n", 1129 | "end" 1130 | ] 1131 | }, 1132 | { 1133 | "cell_type": "code", 1134 | "execution_count": 4, 1135 | "metadata": { 1136 | "collapsed": false 1137 | }, 1138 | "outputs": [ 1139 | { 1140 | "data": { 1141 | "text/plain": [ 1142 | "show (generic function with 92 methods)" 1143 | ] 1144 | }, 1145 | "execution_count": 4, 1146 | "metadata": {}, 1147 | "output_type": "execute_result" 1148 | } 1149 | ], 1150 | "source": [ 1151 | "Base.show(io::IO, t::MyType) = print(io, \n", 1152 | " \"Objet de type MyType avec valeur $(t.x)\")" 1153 | ] 1154 | }, 1155 | { 1156 | "cell_type": "code", 1157 | "execution_count": 5, 1158 | "metadata": { 1159 | "collapsed": false 1160 | }, 1161 | "outputs": [ 1162 | { 1163 | "data": { 1164 | "text/plain": [ 1165 | "Objet de type MyType avec valeur 10" 1166 | ] 1167 | }, 1168 | "execution_count": 5, 1169 | "metadata": {}, 1170 | "output_type": "execute_result" 1171 | } 1172 | ], 1173 | "source": [ 1174 | "t = MyType(10)" 1175 | ] 1176 | }, 1177 | { 1178 | "cell_type": "code", 1179 | "execution_count": 11, 1180 | "metadata": { 1181 | "collapsed": false 1182 | }, 1183 | "outputs": [ 1184 | { 1185 | "data": { 1186 | "text/plain": [ 1187 | "[0.1, 0.1]" 1188 | ] 1189 | }, 1190 | "execution_count": 11, 1191 | "metadata": {}, 1192 | "output_type": "execute_result" 1193 | } 1194 | ], 1195 | "source": [ 1196 | "I = Interval(0.1)" 1197 | ] 1198 | }, 1199 | { 1200 | "cell_type": "code", 1201 | "execution_count": 9, 1202 | "metadata": { 1203 | "collapsed": false 1204 | }, 1205 | "outputs": [ 1206 | { 1207 | "data": { 1208 | "text/plain": [ 1209 | "1.000000000000000055511151231257827021181583404541015625e-01 with 256 bits of precision" 1210 | ] 1211 | }, 1212 | "execution_count": 9, 1213 | "metadata": {}, 1214 | "output_type": "execute_result" 1215 | } 1216 | ], 1217 | "source": [ 1218 | "big(0.1)" 1219 | ] 1220 | }, 1221 | { 1222 | "cell_type": "code", 1223 | "execution_count": 10, 1224 | "metadata": { 1225 | "collapsed": false 1226 | }, 1227 | "outputs": [ 1228 | { 1229 | "data": { 1230 | "text/plain": [ 1231 | "f (generic function with 1 method)" 1232 | ] 1233 | }, 1234 | "execution_count": 10, 1235 | "metadata": {}, 1236 | "output_type": "execute_result" 1237 | } 1238 | ], 1239 | "source": [ 1240 | "f(x) = x^2 + 2x" 1241 | ] 1242 | }, 1243 | { 1244 | "cell_type": "code", 1245 | "execution_count": 12, 1246 | "metadata": { 1247 | "collapsed": false 1248 | }, 1249 | "outputs": [ 1250 | { 1251 | "data": { 1252 | "text/plain": [ 1253 | "[0.21, 0.21000000000000002]" 1254 | ] 1255 | }, 1256 | "execution_count": 12, 1257 | "metadata": {}, 1258 | "output_type": "execute_result" 1259 | } 1260 | ], 1261 | "source": [ 1262 | "f(I)" 1263 | ] 1264 | }, 1265 | { 1266 | "cell_type": "code", 1267 | "execution_count": 13, 1268 | "metadata": { 1269 | "collapsed": false 1270 | }, 1271 | "outputs": [ 1272 | { 1273 | "data": { 1274 | "text/plain": [ 1275 | "2.099999999999999922284388276239042170345783233642578125e-01 with 256 bits of precision" 1276 | ] 1277 | }, 1278 | "execution_count": 13, 1279 | "metadata": {}, 1280 | "output_type": "execute_result" 1281 | } 1282 | ], 1283 | "source": [ 1284 | "big(0.21)" 1285 | ] 1286 | }, 1287 | { 1288 | "cell_type": "code", 1289 | "execution_count": 14, 1290 | "metadata": { 1291 | "collapsed": false 1292 | }, 1293 | "outputs": [ 1294 | { 1295 | "data": { 1296 | "text/plain": [ 1297 | "[0.09999999999999999, 0.1]" 1298 | ] 1299 | }, 1300 | "execution_count": 14, 1301 | "metadata": {}, 1302 | "output_type": "execute_result" 1303 | } 1304 | ], 1305 | "source": [ 1306 | "@interval 0.1" 1307 | ] 1308 | }, 1309 | { 1310 | "cell_type": "code", 1311 | "execution_count": 15, 1312 | "metadata": { 1313 | "collapsed": false 1314 | }, 1315 | "outputs": [ 1316 | { 1317 | "data": { 1318 | "text/plain": [ 1319 | ":(ValidatedNumerics.make_interval(interval_parameters.precision_type,0.1))" 1320 | ] 1321 | }, 1322 | "execution_count": 15, 1323 | "metadata": {}, 1324 | "output_type": "execute_result" 1325 | } 1326 | ], 1327 | "source": [ 1328 | "macroexpand(:(@interval 0.1))" 1329 | ] 1330 | }, 1331 | { 1332 | "cell_type": "code", 1333 | "execution_count": 17, 1334 | "metadata": { 1335 | "collapsed": false 1336 | }, 1337 | "outputs": [ 1338 | { 1339 | "data": { 1340 | "text/plain": [ 1341 | ":(sin(ValidatedNumerics.make_interval(interval_parameters.precision_type,0.1)))" 1342 | ] 1343 | }, 1344 | "execution_count": 17, 1345 | "metadata": {}, 1346 | "output_type": "execute_result" 1347 | } 1348 | ], 1349 | "source": [ 1350 | "macroexpand(:(@interval sin(0.1)))" 1351 | ] 1352 | }, 1353 | { 1354 | "cell_type": "code", 1355 | "execution_count": 18, 1356 | "metadata": { 1357 | "collapsed": false 1358 | }, 1359 | "outputs": [ 1360 | { 1361 | "data": { 1362 | "text/plain": [ 1363 | "[0.09983341664682814, 0.09983341664682817]" 1364 | ] 1365 | }, 1366 | "execution_count": 18, 1367 | "metadata": {}, 1368 | "output_type": "execute_result" 1369 | } 1370 | ], 1371 | "source": [ 1372 | "@interval sin(0.1)" 1373 | ] 1374 | }, 1375 | { 1376 | "cell_type": "markdown", 1377 | "metadata": {}, 1378 | "source": [ 1379 | "# ccall " 1380 | ] 1381 | }, 1382 | { 1383 | "cell_type": "markdown", 1384 | "metadata": {}, 1385 | "source": [ 1386 | "# Packages " 1387 | ] 1388 | }, 1389 | { 1390 | "cell_type": "markdown", 1391 | "metadata": {}, 1392 | "source": [ 1393 | "Les paquets sont des depots de git:" 1394 | ] 1395 | }, 1396 | { 1397 | "cell_type": "code", 1398 | "execution_count": 1, 1399 | "metadata": { 1400 | "collapsed": false 1401 | }, 1402 | "outputs": [ 1403 | { 1404 | "name": "stderr", 1405 | "output_type": "stream", 1406 | "text": [ 1407 | "INFO: Nothing to be done\n" 1408 | ] 1409 | } 1410 | ], 1411 | "source": [ 1412 | "Pkg.add(\"RCall\")" 1413 | ] 1414 | }, 1415 | { 1416 | "cell_type": "code", 1417 | "execution_count": 2, 1418 | "metadata": { 1419 | "collapsed": false 1420 | }, 1421 | "outputs": [ 1422 | { 1423 | "name": "stderr", 1424 | "output_type": "stream", 1425 | "text": [ 1426 | "INFO: Building RCall\n" 1427 | ] 1428 | } 1429 | ], 1430 | "source": [ 1431 | "Pkg.build(\"RCall\")" 1432 | ] 1433 | }, 1434 | { 1435 | "cell_type": "code", 1436 | "execution_count": 5, 1437 | "metadata": { 1438 | "collapsed": false 1439 | }, 1440 | "outputs": [ 1441 | { 1442 | "name": "stdout", 1443 | "output_type": "stream", 1444 | "text": [ 1445 | "url\n", 1446 | "versions\n" 1447 | ] 1448 | } 1449 | ], 1450 | "source": [ 1451 | ";ls ~/.julia/v0.3/METADATA/RCall" 1452 | ] 1453 | }, 1454 | { 1455 | "cell_type": "code", 1456 | "execution_count": 6, 1457 | "metadata": { 1458 | "collapsed": false 1459 | }, 1460 | "outputs": [ 1461 | { 1462 | "name": "stdout", 1463 | "output_type": "stream", 1464 | "text": [ 1465 | "git://github.com/JuliaStats/RCall.jl.git\n" 1466 | ] 1467 | } 1468 | ], 1469 | "source": [ 1470 | ";less ~/.julia/v0.3/METADATA/RCall/url" 1471 | ] 1472 | }, 1473 | { 1474 | "cell_type": "code", 1475 | "execution_count": null, 1476 | "metadata": { 1477 | "collapsed": true 1478 | }, 1479 | "outputs": [], 1480 | "source": [ 1481 | "Pkg.clone(\"https://github.com/dpsanders/BilliardModels.jl\")" 1482 | ] 1483 | }, 1484 | { 1485 | "cell_type": "code", 1486 | "execution_count": 7, 1487 | "metadata": { 1488 | "collapsed": false 1489 | }, 1490 | "outputs": [ 1491 | { 1492 | "name": "stderr", 1493 | "output_type": "stream", 1494 | "text": [ 1495 | "INFO: Updating METADATA...\n", 1496 | "INFO: Updating TaylorSeries...\n", 1497 | "INFO: Updating ValidatedNumerics...\n", 1498 | "INFO: Updating Compose...\n", 1499 | "INFO: Updating ODE...\n", 1500 | "INFO: Computing changes...\n", 1501 | "INFO: No packages to install, update or remove\n" 1502 | ] 1503 | } 1504 | ], 1505 | "source": [ 1506 | "Pkg.update()" 1507 | ] 1508 | }, 1509 | { 1510 | "cell_type": "markdown", 1511 | "metadata": {}, 1512 | "source": [ 1513 | "Pour developper un paquet:" 1514 | ] 1515 | }, 1516 | { 1517 | "cell_type": "code", 1518 | "execution_count": 10, 1519 | "metadata": { 1520 | "collapsed": false 1521 | }, 1522 | "outputs": [ 1523 | { 1524 | "name": "stderr", 1525 | "output_type": "stream", 1526 | "text": [ 1527 | "INFO: Initializing Orsay repo: /Users/dsanders/.julia/v0.3/Orsay\n", 1528 | "INFO: Origin: git://github.com/dpsanders/Orsay.jl.git\n", 1529 | "INFO: Generating LICENSE.md\n", 1530 | "INFO: Generating README.md\n", 1531 | "INFO: Generating src/Orsay.jl\n", 1532 | "INFO: Generating test/runtests.jl\n", 1533 | "INFO: Generating .travis.yml\n", 1534 | "INFO: Generating .gitignore\n", 1535 | "INFO: Committing Orsay generated files\n" 1536 | ] 1537 | } 1538 | ], 1539 | "source": [ 1540 | "Pkg.generate(\"Orsay\", \"MIT\")" 1541 | ] 1542 | }, 1543 | { 1544 | "cell_type": "code", 1545 | "execution_count": 9, 1546 | "metadata": { 1547 | "collapsed": false 1548 | }, 1549 | "outputs": [ 1550 | { 1551 | "name": "stderr", 1552 | "output_type": "stream", 1553 | "text": [ 1554 | "INFO: Loading help data...\n" 1555 | ] 1556 | }, 1557 | { 1558 | "name": "stdout", 1559 | "output_type": "stream", 1560 | "text": [ 1561 | "Base.Pkg.generate(pkg, license)\n", 1562 | "\n", 1563 | " Generate a new package named \"pkg\" with one of these license\n", 1564 | " keys: \"\"MIT\"\" or \"\"BSD\"\". If you want to make a package\n", 1565 | " with a different license, you can edit it afterwards. Generate\n", 1566 | " creates a git repo at \"Pkg.dir(pkg)\" for the package and inside\n", 1567 | " it \"LICENSE.md\", \"README.md\", the julia entrypoint\n", 1568 | " \"$pkg/src/$pkg.jl\", and a travis test file, \".travis.yml\".\n" 1569 | ] 1570 | } 1571 | ], 1572 | "source": [ 1573 | "?Pkg.generate" 1574 | ] 1575 | }, 1576 | { 1577 | "cell_type": "code", 1578 | "execution_count": 11, 1579 | "metadata": { 1580 | "collapsed": false 1581 | }, 1582 | "outputs": [ 1583 | { 1584 | "name": "stdout", 1585 | "output_type": "stream", 1586 | "text": [ 1587 | "LICENSE.md\n", 1588 | "README.md\n", 1589 | "src\n", 1590 | "test\n" 1591 | ] 1592 | } 1593 | ], 1594 | "source": [ 1595 | "; ls /Users/dsanders/.julia/v0.3/Orsay" 1596 | ] 1597 | }, 1598 | { 1599 | "cell_type": "code", 1600 | "execution_count": 13, 1601 | "metadata": { 1602 | "collapsed": false 1603 | }, 1604 | "outputs": [ 1605 | { 1606 | "name": "stdout", 1607 | "output_type": "stream", 1608 | "text": [ 1609 | "The Orsay.jl package is licensed under the MIT \"Expat\" License:\n", 1610 | "\n", 1611 | "> Copyright (c) 2015: David P. Sanders.\n", 1612 | ">\n", 1613 | "> Permission is hereby granted, free of charge, to any person obtaining\n", 1614 | "> a copy of this software and associated documentation files (the\n", 1615 | "> \"Software\"), to deal in the Software without restriction, including\n", 1616 | "> without limitation the rights to use, copy, modify, merge, publish,\n", 1617 | "> distribute, sublicense, and/or sell copies of the Software, and to\n", 1618 | "> permit persons to whom the Software is furnished to do so, subject to\n", 1619 | "> the following conditions:\n", 1620 | ">\n", 1621 | "> The above copyright notice and this permission notice shall be\n", 1622 | "> included in all copies or substantial portions of the Software.\n", 1623 | ">\n", 1624 | "> THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\n", 1625 | "> EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\n", 1626 | "> MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\n", 1627 | "> IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY\n", 1628 | "> CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,\n", 1629 | "> TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE\n", 1630 | "> SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n" 1631 | ] 1632 | } 1633 | ], 1634 | "source": [ 1635 | "; cat /Users/dsanders/.julia/v0.3/Orsay/LICENSE.md" 1636 | ] 1637 | }, 1638 | { 1639 | "cell_type": "code", 1640 | "execution_count": 16, 1641 | "metadata": { 1642 | "collapsed": false 1643 | }, 1644 | "outputs": [ 1645 | { 1646 | "name": "stdout", 1647 | "output_type": "stream", 1648 | "text": [ 1649 | "LICENSE.md\n", 1650 | "README.md\n", 1651 | "src\n", 1652 | "test\n" 1653 | ] 1654 | } 1655 | ], 1656 | "source": [ 1657 | "; ls /Users/dsanders/.julia/v0.3/Orsay/" 1658 | ] 1659 | }, 1660 | { 1661 | "cell_type": "code", 1662 | "execution_count": 18, 1663 | "metadata": { 1664 | "collapsed": false 1665 | }, 1666 | "outputs": [ 1667 | { 1668 | "name": "stdout", 1669 | "output_type": "stream", 1670 | "text": [ 1671 | "module Orsay\n", 1672 | "\n", 1673 | "# package code goes here\n", 1674 | "\n", 1675 | "end # module\n" 1676 | ] 1677 | } 1678 | ], 1679 | "source": [ 1680 | "; less /Users/dsanders/.julia/v0.3/Orsay/src/Orsay.jl" 1681 | ] 1682 | }, 1683 | { 1684 | "cell_type": "code", 1685 | "execution_count": 19, 1686 | "metadata": { 1687 | "collapsed": false 1688 | }, 1689 | "outputs": [ 1690 | { 1691 | "name": "stdout", 1692 | "output_type": "stream", 1693 | "text": [ 1694 | "using Orsay\n", 1695 | "using Base.Test\n", 1696 | "\n", 1697 | "# write your own tests here\n", 1698 | "@test 1 == 1\n" 1699 | ] 1700 | } 1701 | ], 1702 | "source": [ 1703 | "; less /Users/dsanders/.julia/v0.3/Orsay/test/runtests.jl" 1704 | ] 1705 | }, 1706 | { 1707 | "cell_type": "markdown", 1708 | "metadata": {}, 1709 | "source": [ 1710 | "Quand le paquet est pret:\n" 1711 | ] 1712 | }, 1713 | { 1714 | "cell_type": "code", 1715 | "execution_count": 21, 1716 | "metadata": { 1717 | "collapsed": false 1718 | }, 1719 | "outputs": [ 1720 | { 1721 | "name": "stderr", 1722 | "output_type": "stream", 1723 | "text": [ 1724 | "INFO: Tagging Orsay v0.0.1\n" 1725 | ] 1726 | } 1727 | ], 1728 | "source": [ 1729 | "Pkg.tag(\"Orsay\")" 1730 | ] 1731 | }, 1732 | { 1733 | "cell_type": "code", 1734 | "execution_count": 22, 1735 | "metadata": { 1736 | "collapsed": true 1737 | }, 1738 | "outputs": [], 1739 | "source": [ 1740 | "# QUANT TU EST PRET!: Pkg.publish()" 1741 | ] 1742 | }, 1743 | { 1744 | "cell_type": "markdown", 1745 | "metadata": {}, 1746 | "source": [ 1747 | "# Compat " 1748 | ] 1749 | }, 1750 | { 1751 | "cell_type": "code", 1752 | "execution_count": 5, 1753 | "metadata": { 1754 | "collapsed": true 1755 | }, 1756 | "outputs": [], 1757 | "source": [ 1758 | "using Compat" 1759 | ] 1760 | }, 1761 | { 1762 | "cell_type": "code", 1763 | "execution_count": 24, 1764 | "metadata": { 1765 | "collapsed": true 1766 | }, 1767 | "outputs": [], 1768 | "source": [ 1769 | "# En 0.3:" 1770 | ] 1771 | }, 1772 | { 1773 | "cell_type": "code", 1774 | "execution_count": 2, 1775 | "metadata": { 1776 | "collapsed": false 1777 | }, 1778 | "outputs": [ 1779 | { 1780 | "data": { 1781 | "text/plain": [ 1782 | "3" 1783 | ] 1784 | }, 1785 | "execution_count": 2, 1786 | "metadata": {}, 1787 | "output_type": "execute_result" 1788 | } 1789 | ], 1790 | "source": [ 1791 | "i = int(\"3\")" 1792 | ] 1793 | }, 1794 | { 1795 | "cell_type": "code", 1796 | "execution_count": 3, 1797 | "metadata": { 1798 | "collapsed": false 1799 | }, 1800 | "outputs": [ 1801 | { 1802 | "data": { 1803 | "text/plain": [ 1804 | "3" 1805 | ] 1806 | }, 1807 | "execution_count": 3, 1808 | "metadata": {}, 1809 | "output_type": "execute_result" 1810 | } 1811 | ], 1812 | "source": [ 1813 | "parse(Int, \"3\")" 1814 | ] 1815 | }, 1816 | { 1817 | "cell_type": "code", 1818 | "execution_count": 29, 1819 | "metadata": { 1820 | "collapsed": false 1821 | }, 1822 | "outputs": [ 1823 | { 1824 | "data": { 1825 | "text/plain": [ 1826 | "0.3" 1827 | ] 1828 | }, 1829 | "execution_count": 29, 1830 | "metadata": {}, 1831 | "output_type": "execute_result" 1832 | } 1833 | ], 1834 | "source": [ 1835 | "@compat parse(Float64, \"0.3\")" 1836 | ] 1837 | }, 1838 | { 1839 | "cell_type": "code", 1840 | "execution_count": 4, 1841 | "metadata": { 1842 | "collapsed": false 1843 | }, 1844 | "outputs": [ 1845 | { 1846 | "data": { 1847 | "text/plain": [ 1848 | "0-element Array{None,1}" 1849 | ] 1850 | }, 1851 | "execution_count": 4, 1852 | "metadata": {}, 1853 | "output_type": "execute_result" 1854 | } 1855 | ], 1856 | "source": [ 1857 | "a = []" 1858 | ] 1859 | }, 1860 | { 1861 | "cell_type": "code", 1862 | "execution_count": 7, 1863 | "metadata": { 1864 | "collapsed": false 1865 | }, 1866 | "outputs": [ 1867 | { 1868 | "data": { 1869 | "text/plain": [ 1870 | "0-element Array{Any,1}" 1871 | ] 1872 | }, 1873 | "execution_count": 7, 1874 | "metadata": {}, 1875 | "output_type": "execute_result" 1876 | } 1877 | ], 1878 | "source": [] 1879 | }, 1880 | { 1881 | "cell_type": "code", 1882 | "execution_count": 10, 1883 | "metadata": { 1884 | "collapsed": false 1885 | }, 1886 | "outputs": [ 1887 | { 1888 | "ename": "LoadError", 1889 | "evalue": "unsupported or misplaced expression =>\nwhile loading In[10], in expression starting on line 1", 1890 | "output_type": "error", 1891 | "traceback": [ 1892 | "unsupported or misplaced expression =>\nwhile loading In[10], in expression starting on line 1", 1893 | "" 1894 | ] 1895 | } 1896 | ], 1897 | "source": [ 1898 | "Dict(1 => 3, 2 => 4) # ne marche pas en 0.3" 1899 | ] 1900 | }, 1901 | { 1902 | "cell_type": "code", 1903 | "execution_count": 11, 1904 | "metadata": { 1905 | "collapsed": false 1906 | }, 1907 | "outputs": [ 1908 | { 1909 | "data": { 1910 | "text/plain": [ 1911 | "Dict{Int64,Int64} with 2 entries:\n", 1912 | " 2 => 4\n", 1913 | " 1 => 3" 1914 | ] 1915 | }, 1916 | "execution_count": 11, 1917 | "metadata": {}, 1918 | "output_type": "execute_result" 1919 | } 1920 | ], 1921 | "source": [ 1922 | "@compat Dict(1 => 3, 2 => 4) # maintenant ça marche" 1923 | ] 1924 | }, 1925 | { 1926 | "cell_type": "markdown", 1927 | "metadata": {}, 1928 | "source": [ 1929 | "# Noeuds d'un arbre" 1930 | ] 1931 | }, 1932 | { 1933 | "cell_type": "code", 1934 | "execution_count": 1, 1935 | "metadata": { 1936 | "collapsed": false 1937 | }, 1938 | "outputs": [], 1939 | "source": [ 1940 | "type Node\n", 1941 | " value::Int\n", 1942 | " left_child::Node\n", 1943 | " right_child::Node\n", 1944 | " \n", 1945 | " Node(value::Int) = new(value)\n", 1946 | " Node(value, child1, child2) = new(value, child1, child2)\n", 1947 | "end\n", 1948 | "\n" 1949 | ] 1950 | }, 1951 | { 1952 | "cell_type": "code", 1953 | "execution_count": 2, 1954 | "metadata": { 1955 | "collapsed": false 1956 | }, 1957 | "outputs": [ 1958 | { 1959 | "data": { 1960 | "text/plain": [ 1961 | "create_node (generic function with 1 method)" 1962 | ] 1963 | }, 1964 | "execution_count": 2, 1965 | "metadata": {}, 1966 | "output_type": "execute_result" 1967 | } 1968 | ], 1969 | "source": [ 1970 | "function create_node(value)\n", 1971 | " n = Node(value)\n", 1972 | " \n", 1973 | " n.left_child = n\n", 1974 | " n.right_child = n\n", 1975 | " n\n", 1976 | "end" 1977 | ] 1978 | }, 1979 | { 1980 | "cell_type": "code", 1981 | "execution_count": 3, 1982 | "metadata": { 1983 | "collapsed": false 1984 | }, 1985 | "outputs": [ 1986 | { 1987 | "data": { 1988 | "text/plain": [ 1989 | "Node(3,Node(#= circular reference =#),Node(#= circular reference =#))" 1990 | ] 1991 | }, 1992 | "execution_count": 3, 1993 | "metadata": {}, 1994 | "output_type": "execute_result" 1995 | } 1996 | ], 1997 | "source": [ 1998 | "n = create_node(3)" 1999 | ] 2000 | }, 2001 | { 2002 | "cell_type": "code", 2003 | "execution_count": 5, 2004 | "metadata": { 2005 | "collapsed": false 2006 | }, 2007 | "outputs": [ 2008 | { 2009 | "data": { 2010 | "text/plain": [ 2011 | "true" 2012 | ] 2013 | }, 2014 | "execution_count": 5, 2015 | "metadata": {}, 2016 | "output_type": "execute_result" 2017 | } 2018 | ], 2019 | "source": [ 2020 | "n.left_child == n" 2021 | ] 2022 | }, 2023 | { 2024 | "cell_type": "code", 2025 | "execution_count": 6, 2026 | "metadata": { 2027 | "collapsed": false 2028 | }, 2029 | "outputs": [ 2030 | { 2031 | "data": { 2032 | "text/plain": [ 2033 | "Node(4,Node(#= circular reference =#),Node(#= circular reference =#))" 2034 | ] 2035 | }, 2036 | "execution_count": 6, 2037 | "metadata": {}, 2038 | "output_type": "execute_result" 2039 | } 2040 | ], 2041 | "source": [ 2042 | "m = create_node(4)" 2043 | ] 2044 | }, 2045 | { 2046 | "cell_type": "code", 2047 | "execution_count": 7, 2048 | "metadata": { 2049 | "collapsed": false 2050 | }, 2051 | "outputs": [ 2052 | { 2053 | "data": { 2054 | "text/plain": [ 2055 | "Node(4,Node(#= circular reference =#),Node(#= circular reference =#))" 2056 | ] 2057 | }, 2058 | "execution_count": 7, 2059 | "metadata": {}, 2060 | "output_type": "execute_result" 2061 | } 2062 | ], 2063 | "source": [ 2064 | "n.left_child = m" 2065 | ] 2066 | }, 2067 | { 2068 | "cell_type": "code", 2069 | "execution_count": 8, 2070 | "metadata": { 2071 | "collapsed": false 2072 | }, 2073 | "outputs": [ 2074 | { 2075 | "data": { 2076 | "text/plain": [ 2077 | "Node(3,Node(4,Node(#= circular reference =#),Node(#= circular reference =#)),Node(#= circular reference =#))" 2078 | ] 2079 | }, 2080 | "execution_count": 8, 2081 | "metadata": {}, 2082 | "output_type": "execute_result" 2083 | } 2084 | ], 2085 | "source": [ 2086 | "n" 2087 | ] 2088 | }, 2089 | { 2090 | "cell_type": "code", 2091 | "execution_count": 9, 2092 | "metadata": { 2093 | "collapsed": false 2094 | }, 2095 | "outputs": [ 2096 | { 2097 | "data": { 2098 | "text/plain": [ 2099 | "Node(10,#undef,#undef)" 2100 | ] 2101 | }, 2102 | "execution_count": 9, 2103 | "metadata": {}, 2104 | "output_type": "execute_result" 2105 | } 2106 | ], 2107 | "source": [ 2108 | "v = Node(10)" 2109 | ] 2110 | }, 2111 | { 2112 | "cell_type": "code", 2113 | "execution_count": 10, 2114 | "metadata": { 2115 | "collapsed": false 2116 | }, 2117 | "outputs": [ 2118 | { 2119 | "ename": "LoadError", 2120 | "evalue": "access to undefined reference\nwhile loading In[10], in expression starting on line 1", 2121 | "output_type": "error", 2122 | "traceback": [ 2123 | "access to undefined reference\nwhile loading In[10], in expression starting on line 1", 2124 | "" 2125 | ] 2126 | } 2127 | ], 2128 | "source": [ 2129 | "v.left_child" 2130 | ] 2131 | }, 2132 | { 2133 | "cell_type": "code", 2134 | "execution_count": 11, 2135 | "metadata": { 2136 | "collapsed": false 2137 | }, 2138 | "outputs": [ 2139 | { 2140 | "ename": "LoadError", 2141 | "evalue": "`Node` has no method matching Node(::Int64, ::Node, ::Node)\nwhile loading In[11], in expression starting on line 1", 2142 | "output_type": "error", 2143 | "traceback": [ 2144 | "`Node` has no method matching Node(::Int64, ::Node, ::Node)\nwhile loading In[11], in expression starting on line 1", 2145 | "" 2146 | ] 2147 | } 2148 | ], 2149 | "source": [ 2150 | "Node(20, m, m)" 2151 | ] 2152 | }, 2153 | { 2154 | "cell_type": "code", 2155 | "execution_count": 4, 2156 | "metadata": { 2157 | "collapsed": false 2158 | }, 2159 | "outputs": [ 2160 | { 2161 | "data": { 2162 | "text/plain": [ 2163 | "Node(20,Node(#= circular reference =#),Node(#= circular reference =#))" 2164 | ] 2165 | }, 2166 | "execution_count": 4, 2167 | "metadata": {}, 2168 | "output_type": "execute_result" 2169 | } 2170 | ], 2171 | "source": [ 2172 | "m = create_node(10)\n", 2173 | "p = create_node(20)" 2174 | ] 2175 | }, 2176 | { 2177 | "cell_type": "code", 2178 | "execution_count": 5, 2179 | "metadata": { 2180 | "collapsed": false 2181 | }, 2182 | "outputs": [ 2183 | { 2184 | "data": { 2185 | "text/plain": [ 2186 | "Node(30,Node(10,Node(#= circular reference =#),Node(#= circular reference =#)),Node(20,Node(#= circular reference =#),Node(#= circular reference =#)))" 2187 | ] 2188 | }, 2189 | "execution_count": 5, 2190 | "metadata": {}, 2191 | "output_type": "execute_result" 2192 | } 2193 | ], 2194 | "source": [ 2195 | "z = Node(30, m, p)" 2196 | ] 2197 | }, 2198 | { 2199 | "cell_type": "code", 2200 | "execution_count": null, 2201 | "metadata": { 2202 | "collapsed": true 2203 | }, 2204 | "outputs": [], 2205 | "source": [] 2206 | } 2207 | ], 2208 | "metadata": { 2209 | "kernelspec": { 2210 | "display_name": "Julia 0.3.9-pre", 2211 | "language": "julia", 2212 | "name": "julia-0.3" 2213 | }, 2214 | "language_info": { 2215 | "name": "julia", 2216 | "version": "0.3.10" 2217 | } 2218 | }, 2219 | "nbformat": 4, 2220 | "nbformat_minor": 0 2221 | } 2222 | -------------------------------------------------------------------------------- /during_sessions/MyModule.jl: -------------------------------------------------------------------------------- 1 | module MyModule 2 | 3 | export MyType, f! 4 | 5 | import Base: 6 | sin, cos 7 | 8 | type MyType 9 | x :: Float64 10 | end 11 | 12 | f!(a::MyType) = a.x = 10 13 | 14 | Base.sin(a::MyType) = sin(a.x) 15 | cos(a::MyType) = cos(a.x) 16 | 17 | end 18 | -------------------------------------------------------------------------------- /installing.md: -------------------------------------------------------------------------------- 1 | # Hands-on Julia workshop 2 | 3 | In order to minimise the time we spend during the workshop on installation issues, please try to install the following software on your laptop before the course starts. 4 | 5 | However, if you have problems with these instructions, we will (try to) solve them during the workshop! 6 | 7 | # Installing Julia 8 | 9 | ## Binary install 10 | 11 | The simplest way to install Julia is with a binary install: download an installer for your operating system from [here](http://julialang.org/downloads/). 12 | 13 | If you use Windows, you probably want to also install [`git` for Windows](https://msysgit.github.io/), 14 | which provides a proper Unix-like shell. 15 | 16 | ## Source install 17 | 18 | The install from source (which requires compiling Julia itself) takes around 1 hour. 19 | At the prompt, do 20 | 21 | ``` 22 | $ git clone git@github.com:JuliaLang/julia.git 23 | $ cd julia 24 | $ make -j 4 25 | ``` 26 | The 4 in the last line is the number of processors that will be used for the build. 27 | It requires various other tools installed, in particular `cmake` and `m4`; see full details in the Julia README [here](https://github.com/JuliaLang/julia). 28 | 29 | # Juno 30 | 31 | Juno is a neat IDE (Integrated Development Environment) for Julia. 32 | It can also be downloaded from [here](http://julialang.org/downloads/); see also . 33 | 34 | 35 | # IJulia 36 | 37 | [IJulia](https://github.com/JuliaLang/IJulia.jl) is a Julia interface for the [Jupyter (formerly IPython) notebook](http://ipython.org/) 38 | that we will use extensively. 39 | 40 | ## 41 | 42 | First you must install IPython itself. [Currently, the parts of IPython which are not directly related to Python are being separated out into a package called Jupyter.] The simplest way to do so is to install the free [Anaconda Python distribution](http://continuum.io/downloads), which includes IPython, the `matplotlib` plotting library, and many other useful Python packages. 43 | 44 | If you prefer something more lightweight, you can use `pip` (an installer for Python packages): 45 | 46 | ``` 47 | pip install --upgrade ipython[all] 48 | ``` 49 | On Ubuntu, you will first need to do 50 | ``` 51 | sudo apt-get install python-dev 52 | sudo apt-get install python-pip 53 | ``` 54 | 55 | On Mac OSX you will need to add the two lines 56 | ``` 57 | export LC_ALL=en_US.UTF-8 58 | export LANG=en_US.UTF-8 59 | ``` 60 | to the `.bash_profile` file in your home directory (or create that file with that content if it does not already exist). 61 | 62 | 63 | ## Installing the IJulia package 64 | 65 | Once the IPython notebook has been installed, run Julia, and from within Julia add the IJulia package: 66 | 67 | ```julia 68 | Pkg.add("IJulia") 69 | ``` 70 | After several things have been installed, you should see a message saying that IJulia successfully found your IPython installation and 71 | has created the necessary files. 72 | -------------------------------------------------------------------------------- /notebooks/0. Why Julia.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "slideshow": { 7 | "slide_type": "slide" 8 | } 9 | }, 10 | "source": [ 11 | "# Why Julia?" 12 | ] 13 | }, 14 | { 15 | "cell_type": "markdown", 16 | "metadata": { 17 | "slideshow": { 18 | "slide_type": "subslide" 19 | } 20 | }, 21 | "source": [ 22 | "# Or: WSIWMTLAPL " 23 | ] 24 | }, 25 | { 26 | "cell_type": "markdown", 27 | "metadata": { 28 | "slideshow": { 29 | "slide_type": "fragment" 30 | } 31 | }, 32 | "source": [ 33 | "# \"Why should I waste my precious time learning yet another faddish programming language??\"" 34 | ] 35 | }, 36 | { 37 | "cell_type": "markdown", 38 | "metadata": { 39 | "slideshow": { 40 | "slide_type": "fragment" 41 | } 42 | }, 43 | "source": [ 44 | "##[David P. Sanders](http://sistemas.fciencias.unam.mx/~dsanders/)\n", 45 | "\n", 46 | "\n", 47 | "\n", 48 | "###Department of Physics, Faculty of Sciences\n", 49 | "###National University of Mexico (UNAM)\n", 50 | "\n", 51 | "\n", 52 | "###Twitter: @DavidPSanders\n", 53 | "###GitHub: dpsanders" 54 | ] 55 | }, 56 | { 57 | "cell_type": "markdown", 58 | "metadata": { 59 | "slideshow": { 60 | "slide_type": "slide" 61 | } 62 | }, 63 | "source": [ 64 | "# Advantages of Julia" 65 | ] 66 | }, 67 | { 68 | "cell_type": "markdown", 69 | "metadata": { 70 | "slideshow": { 71 | "slide_type": "fragment" 72 | } 73 | }, 74 | "source": [ 75 | "Julia is:\n", 76 | "\n", 77 | "- high-level and easy to learn\n", 78 | "- interactive\n", 79 | "- but Just-In-Time (JIT) compiled" 80 | ] 81 | }, 82 | { 83 | "cell_type": "markdown", 84 | "metadata": { 85 | "slideshow": { 86 | "slide_type": "fragment" 87 | } 88 | }, 89 | "source": [ 90 | "So, Julia\n", 91 | "\n", 92 | "- is fast (~2-3x C speed)\n", 93 | "\n", 94 | "- solves the infamous **two-language problem**\n", 95 | "\n" 96 | ] 97 | }, 98 | { 99 | "cell_type": "markdown", 100 | "metadata": { 101 | "slideshow": { 102 | "slide_type": "fragment" 103 | } 104 | }, 105 | "source": [ 106 | "\n", 107 | "- Most of the standard library is implemented *in Julia itself* \n", 108 | "\n", 109 | "- \"Users are developers\"\n" 110 | ] 111 | }, 112 | { 113 | "cell_type": "markdown", 114 | "metadata": { 115 | "slideshow": { 116 | "slide_type": "subslide" 117 | } 118 | }, 119 | "source": [ 120 | "Julia:\n", 121 | "\n", 122 | "- has a sophisticated type system\n", 123 | "- but it is not *necessary* to talk about types\n", 124 | "\n", 125 | "- has *multiple dispatch*: functions specialised on the types of their arguments\n", 126 | "\n" 127 | ] 128 | }, 129 | { 130 | "cell_type": "markdown", 131 | "metadata": { 132 | "slideshow": { 133 | "slide_type": "fragment" 134 | } 135 | }, 136 | "source": [ 137 | "\n", 138 | "- has sophisticated *metaprogramming* (macros) for generating code programatically\n", 139 | "- allows the creation of domain-specific languages\n" 140 | ] 141 | }, 142 | { 143 | "cell_type": "markdown", 144 | "metadata": { 145 | "slideshow": { 146 | "slide_type": "subslide" 147 | } 148 | }, 149 | "source": [ 150 | "Julia is:\n", 151 | "\n", 152 | "- open source\n", 153 | "\n", 154 | "- free\n", 155 | "\n", 156 | "- MIT license (allows commercial use)\n", 157 | "\n", 158 | "- developed by a worldwide community" 159 | ] 160 | }, 161 | { 162 | "cell_type": "markdown", 163 | "metadata": { 164 | "slideshow": { 165 | "slide_type": "subslide" 166 | } 167 | }, 168 | "source": [ 169 | "Julia is:\n", 170 | "\n", 171 | "- a **new direction** in scientific computing" 172 | ] 173 | }, 174 | { 175 | "cell_type": "markdown", 176 | "metadata": { 177 | "slideshow": { 178 | "slide_type": "subslide" 179 | } 180 | }, 181 | "source": [ 182 | "Julia is:\n", 183 | "\n", 184 | "- the **future** of scientific computing" 185 | ] 186 | }, 187 | { 188 | "cell_type": "markdown", 189 | "metadata": { 190 | "slideshow": { 191 | "slide_type": "slide" 192 | } 193 | }, 194 | "source": [ 195 | "# Using Julia" 196 | ] 197 | }, 198 | { 199 | "cell_type": "markdown", 200 | "metadata": { 201 | "slideshow": { 202 | "slide_type": "subslide" 203 | } 204 | }, 205 | "source": [ 206 | "- From the REPL (Read--Eval--Print Loop):\n", 207 | "\n", 208 | " julia\n", 209 | " \n", 210 | " \n", 211 | " \n", 212 | "- Inside IJulia notebook (Jupyter notebook with Julia kernel):\n", 213 | "\n", 214 | " ipython notebook\n", 215 | "\n", 216 | "\n", 217 | "\n", 218 | "- Inside an IDE, e.g. Juno\n", 219 | "\n", 220 | "\n", 221 | "\n", 222 | "- In the cloud: [JuliaBox](https://juliabox.org/)" 223 | ] 224 | }, 225 | { 226 | "cell_type": "markdown", 227 | "metadata": { 228 | "slideshow": { 229 | "slide_type": "slide" 230 | } 231 | }, 232 | "source": [ 233 | "# Getting help with Julia" 234 | ] 235 | }, 236 | { 237 | "cell_type": "markdown", 238 | "metadata": { 239 | "slideshow": { 240 | "slide_type": "fragment" 241 | } 242 | }, 243 | "source": [ 244 | "- [Julia manual](http://julia.readthedocs.org/en/latest/manual)\n", 245 | "\n", 246 | "\n", 247 | "\n", 248 | "- [`julia-users` mailing list](https://groups.google.com/forum/#!forum/julia-users)\n", 249 | "\n", 250 | "\n", 251 | "\n", 252 | "- [List of resources for learning Julia](http://julialang.org/learning/)\n", 253 | "\n", 254 | "\n" 255 | ] 256 | }, 257 | { 258 | "cell_type": "markdown", 259 | "metadata": { 260 | "slideshow": { 261 | "slide_type": "slide" 262 | } 263 | }, 264 | "source": [ 265 | "# Goal of the workshop " 266 | ] 267 | }, 268 | { 269 | "cell_type": "markdown", 270 | "metadata": { 271 | "slideshow": { 272 | "slide_type": "subslide" 273 | } 274 | }, 275 | "source": [ 276 | "- Get you to feel comfortable working in Julia\n", 277 | "\n", 278 | "\n", 279 | "- Convince you that you are *not* wasting your time\n", 280 | "\n", 281 | "\n", 282 | "- Cover some best practices for scientific computing with Julia\n" 283 | ] 284 | }, 285 | { 286 | "cell_type": "markdown", 287 | "metadata": { 288 | "slideshow": { 289 | "slide_type": "slide" 290 | } 291 | }, 292 | "source": [ 293 | "# Structure of the workshop\n", 294 | "\n", 295 | "- \"Flipped classroom\" (cf. [Lorena Barba](http://lorenabarba.com/))\n", 296 | "\n", 297 | "\n", 298 | "- Small questions and directed exercises (\"Exercise\")\n", 299 | "\n", 300 | "\n", 301 | "- You work in pairs \n", 302 | "\n", 303 | "\n", 304 | "- **ASK QUESTIONS!**\n", 305 | "\n" 306 | ] 307 | }, 308 | { 309 | "cell_type": "markdown", 310 | "metadata": { 311 | "slideshow": { 312 | "slide_type": "slide" 313 | } 314 | }, 315 | "source": [ 316 | "# Guide to the Jupyter notebook and Juno " 317 | ] 318 | } 319 | ], 320 | "metadata": { 321 | "celltoolbar": "Slideshow", 322 | "kernelspec": { 323 | "display_name": "Julia 0.3.8-pre", 324 | "language": "julia", 325 | "name": "julia 0.3" 326 | }, 327 | "language_info": { 328 | "name": "julia", 329 | "version": "0.3.10" 330 | } 331 | }, 332 | "nbformat": 4, 333 | "nbformat_minor": 0 334 | } 335 | -------------------------------------------------------------------------------- /notebooks/Session I - Basic Julia/1. Numbers, variables and basic functions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Numbers and arithmetic" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "For scientific computing, we must begin with representing numbers in Julia. We can exploit the *interactivity* of Julia to quickly explore how Julia \"sees the world\"." 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "## Integers, $\\mathbb{Z}$" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "[1] Type different types of integers and try different operations with them. What does `3/4` do?\n", 29 | "What about `3**4`? [Use `shift-return` to execute the current cell in IJulia.]\n", 30 | "\n", 31 | "[2] Calculate powers of 10. What happens?" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": {}, 37 | "source": [ 38 | "Types are crucial in Julia (although we often do not need to mention them explicitly).\n", 39 | "The function `typeof` tells us what type an object is:" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": null, 45 | "metadata": { 46 | "collapsed": false 47 | }, 48 | "outputs": [], 49 | "source": [ 50 | "typeof(10)" 51 | ] 52 | }, 53 | { 54 | "cell_type": "markdown", 55 | "metadata": {}, 56 | "source": [ 57 | "Note that functions in Julia take argument lists inside parentheses, '`(`' y '`)`'." 58 | ] 59 | }, 60 | { 61 | "cell_type": "markdown", 62 | "metadata": {}, 63 | "source": [ 64 | "We can use arbitrary-precision arithmetic using the `big` function." 65 | ] 66 | }, 67 | { 68 | "cell_type": "markdown", 69 | "metadata": {}, 70 | "source": [ 71 | "[3] Calculate powers of 10 with arbitrary precision. What happens if you do `big(10^20)`? Why?\n", 72 | "\n", 73 | "[4] Calculate $2^{2^{2^{2^2}}}$ with normal arithmetic and in arbitrary precision.\n", 74 | "\n", 75 | "[5] Use the `string` function to convert the previous result (called `ans`) into a string. How could we calculate the number of digits in the number? [Hint: try typing the first few letters of a function and use `` to find possible completions.]" 76 | ] 77 | }, 78 | { 79 | "cell_type": "markdown", 80 | "metadata": {}, 81 | "source": [ 82 | "## Rationals, $\\mathbb{Q}$" 83 | ] 84 | }, 85 | { 86 | "cell_type": "markdown", 87 | "metadata": {}, 88 | "source": [ 89 | "There is a built-in rational type in the Julia standard library. Rationals are constructed using the `//` operator." 90 | ] 91 | }, 92 | { 93 | "cell_type": "markdown", 94 | "metadata": {}, 95 | "source": [ 96 | "[1] Calculate $\\frac{3}{4} + \\frac{5}{6}$.\n", 97 | "\n", 98 | "[2] Calculate powers of $\\frac{3}{4}$. What happens? What is the solution? What is the type of the resulting objects?" 99 | ] 100 | }, 101 | { 102 | "cell_type": "markdown", 103 | "metadata": {}, 104 | "source": [ 105 | "## Reals, $\\mathbb{R}$" 106 | ] 107 | }, 108 | { 109 | "cell_type": "markdown", 110 | "metadata": {}, 111 | "source": [ 112 | "Real numbers are approximated by floating-point numbers. Julia has several different floating-point types available, whose names start with `Float`. " 113 | ] 114 | }, 115 | { 116 | "cell_type": "markdown", 117 | "metadata": {}, 118 | "source": [ 119 | "[1] Use tab completion to find the available floating-point types. " 120 | ] 121 | }, 122 | { 123 | "cell_type": "markdown", 124 | "metadata": {}, 125 | "source": [ 126 | "[1] ¿Cómo se escriben números flotantes (con decimal) en Julia? ¿De qué tipo son?\n", 127 | "\n", 128 | "[2] What happens when we calculate $2.3 \\times 4.6$?\n", 129 | "\n", 130 | "[3] What type is the answer? (This tells us the default floating-point type.)\n", 131 | "\n", 132 | "[4] Use the `bits` function to examine the internal representation of `2.3`. " 133 | ] 134 | }, 135 | { 136 | "cell_type": "markdown", 137 | "metadata": {}, 138 | "source": [ 139 | "\"Scientific notation\" may be used for large and small numbers, e.g. `1.3e100`." 140 | ] 141 | }, 142 | { 143 | "cell_type": "markdown", 144 | "metadata": {}, 145 | "source": [ 146 | "Julia has built-in support for arbitrary-precision floating-point numbers:" 147 | ] 148 | }, 149 | { 150 | "cell_type": "markdown", 151 | "metadata": {}, 152 | "source": [ 153 | "[5] What happens if we do `big(0.1)`? What is the resulting type? Why is the answer strange?" 154 | ] 155 | }, 156 | { 157 | "cell_type": "markdown", 158 | "metadata": {}, 159 | "source": [ 160 | "[6] Change to a larger precision using `set_bigfloat_precision`. [Note that help is available on functions using `?NAME`, where `NAME` is the name of the function.] Make a more precise $0.1$ using `BigFloat(\"0.1\")`. [In Julia 0.4, this may be written `big\"0.1\"`.] What happens? Why? Does it help to further increase the precision?" 161 | ] 162 | }, 163 | { 164 | "cell_type": "markdown", 165 | "metadata": {}, 166 | "source": [ 167 | "## Complex numbers, $\\mathbb{C}$" 168 | ] 169 | }, 170 | { 171 | "cell_type": "markdown", 172 | "metadata": {}, 173 | "source": [ 174 | "Julia also has built-in support for complex numbers. The imaginary unit, $i = \\sqrt{-1}$, is denoted by `im` in Julia. " 175 | ] 176 | }, 177 | { 178 | "cell_type": "markdown", 179 | "metadata": {}, 180 | "source": [ 181 | "[1] Check that Julia knows that $i^2 = -1$.\n", 182 | "\n", 183 | "[2] How are complex numbers written? What is the type of a complex number formed with integers? With reals? With rationals? This starts to tell us how Julia's types work.\n", 184 | "\n", 185 | "[3] Guess the name of the function to calculate the complex conjugate. Use it to calculate the absolute value of $3 + 4i$. Check it against the absolute value function." 186 | ] 187 | }, 188 | { 189 | "cell_type": "markdown", 190 | "metadata": {}, 191 | "source": [ 192 | "# Variables " 193 | ] 194 | }, 195 | { 196 | "cell_type": "markdown", 197 | "metadata": {}, 198 | "source": [ 199 | "Variables in Julia are defined directly using the assignment operator. No type declaration is necessary." 200 | ] 201 | }, 202 | { 203 | "cell_type": "code", 204 | "execution_count": null, 205 | "metadata": { 206 | "collapsed": false 207 | }, 208 | "outputs": [], 209 | "source": [ 210 | "x = 3" 211 | ] 212 | }, 213 | { 214 | "cell_type": "markdown", 215 | "metadata": {}, 216 | "source": [ 217 | "[1] `x` has a type, that is assigned automatically. What is its type? Can it change type?" 218 | ] 219 | }, 220 | { 221 | "cell_type": "markdown", 222 | "metadata": {}, 223 | "source": [ 224 | "[2] Declare a constant `xx` using `const`? What happens if you try to change its value?" 225 | ] 226 | }, 227 | { 228 | "cell_type": "markdown", 229 | "metadata": {}, 230 | "source": [ 231 | "Variable names can contain any Unicode character (although the first letter of the variable name is restricted to be letter-like). Julia allows many useful Unicode characters to be typed using LaTeX notation: type `\\alpha`. You can type `\\alp` and then type `` to see possible completions." 232 | ] 233 | }, 234 | { 235 | "cell_type": "code", 236 | "execution_count": null, 237 | "metadata": { 238 | "collapsed": false 239 | }, 240 | "outputs": [], 241 | "source": [ 242 | "α = 3; ℵ = 10" 243 | ] 244 | }, 245 | { 246 | "cell_type": "markdown", 247 | "metadata": {}, 248 | "source": [ 249 | "Note that statements on the same line may be separated using semicolons." 250 | ] 251 | }, 252 | { 253 | "cell_type": "markdown", 254 | "metadata": {}, 255 | "source": [ 256 | "[3] Define two string variables, one that consists of only ASCII (i.e., \"normal\") characters, and the other that contains Unicode (e.g. accented characters, Greek letters, etc.). What types are these variables?" 257 | ] 258 | }, 259 | { 260 | "cell_type": "markdown", 261 | "metadata": {}, 262 | "source": [ 263 | "The `print` function displays its arguments; `println` also adds a new line." 264 | ] 265 | }, 266 | { 267 | "cell_type": "markdown", 268 | "metadata": {}, 269 | "source": [ 270 | "[4] Print a user-friendly phrase giving the value of α." 271 | ] 272 | }, 273 | { 274 | "cell_type": "markdown", 275 | "metadata": {}, 276 | "source": [ 277 | "### String interpolation" 278 | ] 279 | }, 280 | { 281 | "cell_type": "markdown", 282 | "metadata": {}, 283 | "source": [ 284 | "Suppose we wish to greet the user. We would like to do" 285 | ] 286 | }, 287 | { 288 | "cell_type": "code", 289 | "execution_count": null, 290 | "metadata": { 291 | "collapsed": false 292 | }, 293 | "outputs": [], 294 | "source": [ 295 | "greeting = \"Hello, name, how are you?\"" 296 | ] 297 | }, 298 | { 299 | "cell_type": "markdown", 300 | "metadata": {}, 301 | "source": [ 302 | "We would like to substitute the *value* of the variable `name` in the string where the word `name` currently is.\n", 303 | "This may be done using a dollar sign (`$`):" 304 | ] 305 | }, 306 | { 307 | "cell_type": "code", 308 | "execution_count": null, 309 | "metadata": { 310 | "collapsed": false 311 | }, 312 | "outputs": [], 313 | "source": [ 314 | "greeting = \"Hello, $name, how are you?\"" 315 | ] 316 | }, 317 | { 318 | "cell_type": "markdown", 319 | "metadata": {}, 320 | "source": [ 321 | "[5] Use this to redo exercise [4]." 322 | ] 323 | }, 324 | { 325 | "cell_type": "markdown", 326 | "metadata": {}, 327 | "source": [ 328 | "# Basic functions" 329 | ] 330 | }, 331 | { 332 | "cell_type": "markdown", 333 | "metadata": {}, 334 | "source": [ 335 | "Functions are fundamental en mathematics, physics and computer science.\n", 336 | "In Julia, functions take zero or more arguments, and return zero or more results.\n", 337 | "\n", 338 | "Julia provides two types of syntax for defining functions: a concise mathematical-type notation for one-line definitions, and an extended notation for longer functions:" 339 | ] 340 | }, 341 | { 342 | "cell_type": "code", 343 | "execution_count": 6, 344 | "metadata": { 345 | "collapsed": false 346 | }, 347 | "outputs": [ 348 | { 349 | "data": { 350 | "text/plain": [ 351 | "f (generic function with 1 method)" 352 | ] 353 | }, 354 | "execution_count": 6, 355 | "metadata": {}, 356 | "output_type": "execute_result" 357 | } 358 | ], 359 | "source": [ 360 | "f(x) = 2x^2 # note that (currently) no multiplication sign is required" 361 | ] 362 | }, 363 | { 364 | "cell_type": "markdown", 365 | "metadata": {}, 366 | "source": [ 367 | "[Line comments are begun with `#`; multi-line comments are written `#= ... =#`.]" 368 | ] 369 | }, 370 | { 371 | "cell_type": "markdown", 372 | "metadata": {}, 373 | "source": [ 374 | "[1] The name `f` now denotes an object. What type does it have?\n", 375 | "\n", 376 | "[2] Define a function $g(x) = (x+1)^2$ and a function $h$ which is the expansion of that.\n", 377 | "Define a third function that checks if those two functions do indeed return the same for a given value of $x$." 378 | ] 379 | }, 380 | { 381 | "cell_type": "markdown", 382 | "metadata": {}, 383 | "source": [ 384 | "The extended syntax for defining functions is" 385 | ] 386 | }, 387 | { 388 | "cell_type": "code", 389 | "execution_count": 7, 390 | "metadata": { 391 | "collapsed": false 392 | }, 393 | "outputs": [ 394 | { 395 | "data": { 396 | "text/plain": [ 397 | "F (generic function with 1 method)" 398 | ] 399 | }, 400 | "execution_count": 7, 401 | "metadata": {}, 402 | "output_type": "execute_result" 403 | } 404 | ], 405 | "source": [ 406 | "function F(x)\n", 407 | " x += 1 # equivalent to x = x + 1\n", 408 | " x^2 # no return statement required\n", 409 | "end" 410 | ] 411 | }, 412 | { 413 | "cell_type": "markdown", 414 | "metadata": {}, 415 | "source": [ 416 | "Note that the value returned by the function is the value of the last expression calculated in the function.\n", 417 | "The keyword `return` is also avalable." 418 | ] 419 | }, 420 | { 421 | "cell_type": "markdown", 422 | "metadata": {}, 423 | "source": [ 424 | "[3] Define functions to calculate the volume and surface area of a sphere. Check that the numerical derivative of one is close to the other.\n", 425 | "\n", 426 | "Note that you can provide default values to function arguments.\n", 427 | "\n", 428 | "There is much more to functions in Julia, as we will see later." 429 | ] 430 | } 431 | ], 432 | "metadata": { 433 | "kernelspec": { 434 | "display_name": "Julia 0.3.8-pre", 435 | "language": "julia", 436 | "name": "julia 0.3" 437 | }, 438 | "language_info": { 439 | "name": "julia", 440 | "version": "0.3.10" 441 | } 442 | }, 443 | "nbformat": 4, 444 | "nbformat_minor": 0 445 | } 446 | -------------------------------------------------------------------------------- /notebooks/Session I - Basic Julia/2. Iteration - ranges, vectors and conditionals.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Iteration " 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "Iteration is key to many scientific computing tasks; Julia has `for` and `while` loops.\n", 15 | "\n", 16 | "A block of code (e.g. the body of a `for` loop or of a function) must end with the keyword `end`.\n", 17 | "It is usually written with 4 spaces of indentation (although this is not actually necessary; indentation is not significant in Julia). " 18 | ] 19 | }, 20 | { 21 | "cell_type": "markdown", 22 | "metadata": {}, 23 | "source": [ 24 | "## Ranges " 25 | ] 26 | }, 27 | { 28 | "cell_type": "markdown", 29 | "metadata": {}, 30 | "source": [ 31 | "A common style of `for` loop runs over a *range* of numbers. Julia has a special syntax and types to represent ranges:" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": 18, 37 | "metadata": { 38 | "collapsed": false 39 | }, 40 | "outputs": [ 41 | { 42 | "name": "stdout", 43 | "output_type": "stream", 44 | "text": [ 45 | "1\n", 46 | "2\n", 47 | "3\n", 48 | "4\n", 49 | "5\n" 50 | ] 51 | } 52 | ], 53 | "source": [ 54 | "R = 1:5\n", 55 | "for i in R\n", 56 | " println(i)\n", 57 | "end" 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": null, 63 | "metadata": { 64 | "collapsed": false 65 | }, 66 | "outputs": [], 67 | "source": [ 68 | "R = 1:5" 69 | ] 70 | }, 71 | { 72 | "cell_type": "markdown", 73 | "metadata": {}, 74 | "source": [ 75 | "[1] What is the value of the expression that assigns the range to the variable? What type is the resulting variable? What about if we put floating-point numbers in a range? Guess the syntax to include a different step size." 76 | ] 77 | }, 78 | { 79 | "cell_type": "markdown", 80 | "metadata": {}, 81 | "source": [ 82 | "These types do not store the numbers that they contain, i.e. they are memory-efficient. In general, Julia tries to be as efficient as possible in this way. They are *iterable*, which means that an [iteration protocol]() is defined for ranges; this is what allows the `for` to work." 83 | ] 84 | }, 85 | { 86 | "cell_type": "markdown", 87 | "metadata": {}, 88 | "source": [ 89 | "## Vectors " 90 | ] 91 | }, 92 | { 93 | "cell_type": "markdown", 94 | "metadata": {}, 95 | "source": [ 96 | "If we do in fact require the list of numbers in a range, we can obtain it via" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": null, 102 | "metadata": { 103 | "collapsed": false 104 | }, 105 | "outputs": [], 106 | "source": [ 107 | "v = collect(1:5)" 108 | ] 109 | }, 110 | { 111 | "cell_type": "markdown", 112 | "metadata": {}, 113 | "source": [ 114 | "[1] The resulting object is a vector. What is its exact type? An alternative way of writing this is `Vector{Int64}`." 115 | ] 116 | }, 117 | { 118 | "cell_type": "markdown", 119 | "metadata": {}, 120 | "source": [ 121 | "Vectors may be created using square brackets, `[` and `]`:\n", 122 | "\n", 123 | " v = [3, 4, 7]. \n", 124 | " \n", 125 | "They may be iterated over with a `for`; elements are also extracted and set using square brackets:\n", 126 | "\n", 127 | " v[3] = 10" 128 | ] 129 | }, 130 | { 131 | "cell_type": "markdown", 132 | "metadata": {}, 133 | "source": [ 134 | "[2] Make a function `my_mean` that takes a vector and calculates its mean. [To do this, guess the name of the function that finds the number of elements in a vector and use tab completion to check your guess]. Test it with different inputs. What type does it return? What about for inputs that are complex numbers? Does the function also work for ranges?" 135 | ] 136 | }, 137 | { 138 | "cell_type": "markdown", 139 | "metadata": {}, 140 | "source": [ 141 | "## Exercise: Babylonian algorithm " 142 | ] 143 | }, 144 | { 145 | "cell_type": "markdown", 146 | "metadata": {}, 147 | "source": [ 148 | "The Babylonian algorithm is an efficient way to calculate the square root $\\sqrt{y}$ of a number $y$, given by\n", 149 | "\n", 150 | "$$x_{n+1} := \\textstyle \\frac{1}{2} (x_n + \\textstyle \\frac{y}{x_n}).$$\n" 151 | ] 152 | }, 153 | { 154 | "cell_type": "markdown", 155 | "metadata": {}, 156 | "source": [ 157 | "[1] Implement this in a function that accepts an argument `y`. We can specify that `y` must be real by annotating the argument as\n", 158 | "\n", 159 | " y :: Real\n", 160 | "\n", 161 | "[2] What happens if we call the function with an argument that is not real?" 162 | ] 163 | }, 164 | { 165 | "cell_type": "markdown", 166 | "metadata": {}, 167 | "source": [ 168 | "## Storing results by growing vectors " 169 | ] 170 | }, 171 | { 172 | "cell_type": "markdown", 173 | "metadata": {}, 174 | "source": [ 175 | "Suppose we now wish to store a list of the values $(x_n)$ generated during the algorithm. For this, we use the *same* vector type. Entries can be added to a one-dimensional vector using the functions `push!` and `append!`." 176 | ] 177 | }, 178 | { 179 | "cell_type": "markdown", 180 | "metadata": {}, 181 | "source": [ 182 | "[3] Make a vector and work out how `push!` and `append!` work, and how they differ. [You can also use e.g. `?push!` to get the available help/documentation on a given command.]" 183 | ] 184 | }, 185 | { 186 | "cell_type": "markdown", 187 | "metadata": {}, 188 | "source": [ 189 | "[4] Return the list of intermediate results from the Babylonian algorithm, in addition to the final answer. [Julia allows us to return several values from a function, and assign them one by one:\n", 190 | "\n", 191 | " function f(x)\n", 192 | " x, x^2 # return the pair x, x^2\n", 193 | " end\n", 194 | " \n", 195 | " y, z = f(3)\n", 196 | "]\n", 197 | "\n", 198 | "[5] What is the type of `f(3)`? [NB: this has changed in v0.4.]" 199 | ] 200 | }, 201 | { 202 | "cell_type": "markdown", 203 | "metadata": {}, 204 | "source": [ 205 | "You may worry that growing vectors dynamically using `push!` is expensive. \n", 206 | "A size for the vector can be suggested using `sizehint`; an alternative is to create a vector of the correct size using `zeros` and fill it using element access. However, `push!` is usually sufficient. \n", 207 | "\n", 208 | "[Remember that any concerns about efficiency should be *checked by profiling* rather than guessing: it is often difficult to predict the behaviour of modern compilers and processors.]" 209 | ] 210 | }, 211 | { 212 | "cell_type": "markdown", 213 | "metadata": {}, 214 | "source": [ 215 | "# Conditionals " 216 | ] 217 | }, 218 | { 219 | "cell_type": "markdown", 220 | "metadata": {}, 221 | "source": [ 222 | "Conditionals are written using standard C-type boolean operators: `<`, `>`, `<=`, `>=`, `==` (equality), `!=` (not equal).\n", 223 | "\n", 224 | "[1] Define a variable `x` to be `3`. What are the value and type of the boolean expressions `x < 3` and `x <= 3`?\n", 225 | "\n", 226 | "They are joined by boolean operators `&&` (and) and `||` (or).\n", 227 | "\n", 228 | "The structure of a conditional is\n", 229 | "\n", 230 | " if \n", 231 | " \n", 232 | " \n", 233 | " \n", 234 | " elseif \n", 235 | " \n", 236 | " \n", 237 | " elseif \n", 238 | " \n", 239 | " \n", 240 | " else\n", 241 | " \n", 242 | " \n", 243 | " end\n", 244 | " \n", 245 | "Both the `elseif` and `else` are optional, but don't forget the final `end`!" 246 | ] 247 | }, 248 | { 249 | "cell_type": "markdown", 250 | "metadata": {}, 251 | "source": [ 252 | "[2] Define a function to sample a random number from the discrete distribution taking values $1$, $2$, $3$ and $4$ with probabilities $0.1$, $0.2$, $0.3$ and $0.4$, respectively.\n", 253 | "\n", 254 | "Take a large number of samples and make a histogram (bar chart) to check that the probabilities are sampled correctly.\n", 255 | "\n", 256 | "[Note that arbitrary discrete distributions can be sampled efficiently using the `Categorical` type in the `Distributions`package.]" 257 | ] 258 | } 259 | ], 260 | "metadata": { 261 | "kernelspec": { 262 | "display_name": "Julia 0.3.8-pre", 263 | "language": "julia", 264 | "name": "julia 0.3" 265 | }, 266 | "language_info": { 267 | "name": "julia", 268 | "version": "0.3.10" 269 | } 270 | }, 271 | "nbformat": 4, 272 | "nbformat_minor": 0 273 | } 274 | -------------------------------------------------------------------------------- /notebooks/Session I - Basic Julia/3. Packages and visualization with PyPlot.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## The Julia package manager, `Pkg` " 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "Julia has a built-in package manager module, called `Pkg`;\n", 15 | "Julia packages are [`git`](https://git-scm.com/) repositories.\n", 16 | "\n", 17 | "The package called [`Distributions.jl`](), for example, is added with \n", 18 | "\n", 19 | " Pkg.add(\"Distributions\") # no .jl \n", 20 | " \n", 21 | "and \"removed\" (although not completely deleted) with \n", 22 | "\n", 23 | " Pkg.rm(\"Distributions\")\n", 24 | " \n", 25 | "[The package manager actually provides a dependency solver that determines which packages are actually required to be installed.]\n", 26 | "\n", 27 | "\n", 28 | "The package ecosystem is rapidly maturing; a complete list of *registered* packages (which are required to have a certain level of testing and documentation) is available at . Non-registered packages are added by cloning the relevant git repository; `Pkg` again provides an interface for this operation:" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": {}, 34 | "source": [ 35 | "A package need only be `add`ed once, at which point it is downloaded into your local `.julia` directory in your home directory, in a subdirectory `v0.3` or `v0.4`, depending on your Julia version. [If you start having problems with packages that seem to be unsolvable, you can try just deleting your `.julia` directory and reinstalling all your packages.]\n", 36 | "\n", 37 | "Periodically, you should run\n", 38 | "\n", 39 | " Pkg.update()\n", 40 | " \n", 41 | "which (currently) checks for, downloads and installs updated versions of *all* the packages you currently have installed." 42 | ] 43 | }, 44 | { 45 | "cell_type": "markdown", 46 | "metadata": {}, 47 | "source": [ 48 | "## `using` " 49 | ] 50 | }, 51 | { 52 | "cell_type": "markdown", 53 | "metadata": {}, 54 | "source": [ 55 | "Packages provide Julia *modules*, which are not loaded by default. To load a package, do e.g.\n", 56 | "\n", 57 | " using Distributions\n", 58 | " \n", 59 | "This pulls all of the *exported* functions in the module into your local namespace, as you can check using the `whos()` command.\n", 60 | "\n", 61 | "An alternative is\n", 62 | "\n", 63 | " import Distributions\n", 64 | " \n", 65 | "Now, the functions from the `Distributions` package are available only using `Distributions.`. (All functions, not only exported functions, are always available like this.)" 66 | ] 67 | }, 68 | { 69 | "cell_type": "markdown", 70 | "metadata": {}, 71 | "source": [ 72 | "# PyPlot " 73 | ] 74 | }, 75 | { 76 | "cell_type": "markdown", 77 | "metadata": {}, 78 | "source": [ 79 | "`PyPlot` is a Julia *package* (library) for publication-quality plots, mainly in 2D (although with limited support for simple 3D plots).\n", 80 | "\n", 81 | "It is a Julian interface to the `pyplot` module of the Python `matplotlib` library, which is a well-known and mature plotting library for Python. It provides a direct plotting style where features of plots are turned on and off through independent commands; complicated plots [may be created](http://matplotlib.org/gallery.html). All of the functions from the `pyplot` module are made available directly, and the interface between Julian objects and the corresponding Python types is transparent (using the `PyCall.jl` module)." 82 | ] 83 | }, 84 | { 85 | "cell_type": "markdown", 86 | "metadata": {}, 87 | "source": [ 88 | "[1] Add the PyPlot package and load it. Examine the list of available functions using tab completion." 89 | ] 90 | }, 91 | { 92 | "cell_type": "markdown", 93 | "metadata": {}, 94 | "source": [ 95 | "[2] Make some random data for $x$ and $y$ coordinates of the same length and store them in variables `xx` and `yy`. Plot them using\n", 96 | "\n", 97 | " plot(xx, yy, \"o\")\n", 98 | " \n", 99 | "The \"o\" string is used to change the plotting style to points.\n", 100 | "\n", 101 | "[3] What happens if you remove the \"o\" string? What happens if you just do `plot(xx)`?" 102 | ] 103 | }, 104 | { 105 | "cell_type": "markdown", 106 | "metadata": {}, 107 | "source": [ 108 | "PyPlot is designed for plotting data (as opposed to functions). In order to plot functions, we must sample them appropriately." 109 | ] 110 | }, 111 | { 112 | "cell_type": "markdown", 113 | "metadata": {}, 114 | "source": [ 115 | "[4] Create an array (or other object) `xx` of numbers equally spaced from -3 to 3 and a small step.\n", 116 | "Create an array `yy` given by $3x^2 - 2$. Draw the function.\n", 117 | "\n", 118 | "[5] Use the help [`?plot` o `help(plot)`] to change the style of the plot to use red lines and green points.\n", 119 | "Look up (or ask your neighbour) para cambiar el estilo de la gráfica para utilizar líneas rojas y puntos verdes." 120 | ] 121 | }, 122 | { 123 | "cell_type": "markdown", 124 | "metadata": { 125 | "collapsed": true 126 | }, 127 | "source": [ 128 | "## Exercise: The Newton fractal " 129 | ] 130 | }, 131 | { 132 | "cell_type": "markdown", 133 | "metadata": { 134 | "collapsed": true 135 | }, 136 | "source": [ 137 | "The (1D) Newton (or Newton-Raphson) method finds roots (zeros) of a nonlinear function $f$ of one variable. It is an iterative method defined by\n", 138 | "\n", 139 | "$$ x_{n+1} = x_n - \\frac{f(x_n)}{f'(x_n)} $$\n", 140 | "\n", 141 | "[1] Implement the Newton method to find roots of a function $f$. Use it to find square roots of $2$. [You can use Unicode to define a variable with the name $f'$ by writing `f\\prime`.] \n", 142 | "\n", 143 | "Note that functions are **first-class objects** in Julia, i.e. you can use functions anywhere you would use other types of variables.\n", 144 | "\n", 145 | "[2] Use the Newton method to find complex cube roots of $1$. Starting from a grid of initial conditions $x_0$, determine which of the roots each reaches. (Put a bound on the maximum time allowed.) Store the results in a matrix.\n", 146 | "\n", 147 | "[3] Plot the resulting matrix using the `imshow`, `pcolor` and/or `pcolormesh` from `PyPlot`.\n", 148 | "\n", 149 | "[4] Experiment with different complex functions, e.g. other polynomials and `sin`." 150 | ] 151 | } 152 | ], 153 | "metadata": { 154 | "kernelspec": { 155 | "display_name": "Julia 0.3.9-pre", 156 | "language": "julia", 157 | "name": "julia-0.3" 158 | }, 159 | "language_info": { 160 | "name": "julia", 161 | "version": "0.3.10" 162 | } 163 | }, 164 | "nbformat": 4, 165 | "nbformat_minor": 0 166 | } 167 | -------------------------------------------------------------------------------- /notebooks/Session I - Basic Julia/4. Vectors.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Vectors" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "Arrays (vectors, matrices, etc.) in Julia have a dual function: \n", 15 | "\n", 16 | "1. They act as *containers* that store information;\n", 17 | "2. They behave like mathematical vectors and matrices." 18 | ] 19 | }, 20 | { 21 | "cell_type": "markdown", 22 | "metadata": {}, 23 | "source": [ 24 | "[1] Define two vectors `v` y `w` with three components each.\n", 25 | "\n", 26 | "[2] Try doing vector space operations on the vectors (adding two vectors, multiplying a vector by a scalar).\n", 27 | "\n", 28 | "[3] Try multiplying two vectors. Does this work? What could/should this mean? If you want *element-by-element* operations, add a `.` before the operator name, e.g. \"`.*`\" (MATLAB style). What about division?\n", 29 | "\n", 30 | "[4] Guess the names for dot and cross product. Since Julia tries, when possible, to allow Unicode for mathematical notation, these can also be written\n", 31 | "as `\\cdot` and `\\times`. Try it.\n", 32 | "\n", 33 | "[5] Many mathematical functions are defined to act component-wise on vectors. Try your favourite ones. " 34 | ] 35 | }, 36 | { 37 | "cell_type": "markdown", 38 | "metadata": {}, 39 | "source": [ 40 | "# Creating vectors" 41 | ] 42 | }, 43 | { 44 | "cell_type": "markdown", 45 | "metadata": {}, 46 | "source": [ 47 | "There are various utility functions for creating vectors:\n", 48 | "\n", 49 | "[1] Experiment with `zeros`, `ones`\n", 50 | "\n", 51 | "Concatenation is possible using `;` inside the `[...]`.\n", 52 | "\n", 53 | "[2] Create a vector containing 1 to 10, 20 to 30, followed by 100, using concatenation." 54 | ] 55 | }, 56 | { 57 | "cell_type": "markdown", 58 | "metadata": {}, 59 | "source": [ 60 | "# Array comprehensions" 61 | ] 62 | }, 63 | { 64 | "cell_type": "markdown", 65 | "metadata": {}, 66 | "source": [ 67 | "Julia provides a powerful *array comprehension* syntax for constructing vectors (or, in general, arrays) from another sequence.\n", 68 | "This provides a syntax similar to the mathematical definition of a set; for example, the set $S$ defined by\n", 69 | "\n", 70 | "$$S := \\{ x^2 : x \\in \\{1, \\ldots, 10 \\} \\}$$\n", 71 | "\n", 72 | "is the set of the squares of the numbers from 1 to 10. In Julia we can accomplish this as\n", 73 | "\n", 74 | " S = [x^2 for x in 1:10]" 75 | ] 76 | }, 77 | { 78 | "cell_type": "markdown", 79 | "metadata": {}, 80 | "source": [ 81 | "[1] Use an array comprehension to define a function `my_exp` that gives an approximation to the exponential function.\n", 82 | "\n", 83 | "[2] How could we use this to calculate `my_sin` and `my_cos`?" 84 | ] 85 | }, 86 | { 87 | "cell_type": "markdown", 88 | "metadata": {}, 89 | "source": [ 90 | "# Constructing vectors: `map`, `filter`" 91 | ] 92 | }, 93 | { 94 | "cell_type": "markdown", 95 | "metadata": {}, 96 | "source": [ 97 | "Given a vector (or, in general, an iterable), another vector may be created by applying a given function to each element\n", 98 | "Two useful higher-order functions in Julia are:\n", 99 | "1. `map`: apply a given function to each element of a given iterable\n", 100 | "2. `filter`: return only those elements for which a given condition is satisfied.\n", 101 | "\n", 102 | "[6] Experiment with `map`. \n", 103 | "\n", 104 | "[7] Check the relative performance of `map` and an array comprehension for the same function and vector.\n", 105 | "\n", 106 | "`filter` is often used with an *anonymous function* -- a function created with no name, for the sole purpose of using it in the `filter`. The Julia syntax for a function is similar to the mathematical syntax $x \\mapsto 3x^2$:\n", 107 | "\n", 108 | " filter(x -> x < 10, v)\n", 109 | " \n", 110 | "[8] What kind of object is `x -> x < 10`? (You can assign this to a variable.) Check that anonymous functions may also be \n", 111 | " \n", 112 | "[8] Check the relative performance of `filter` and the following syntax that also selects those elements of the array satisfying the given condition:\n", 113 | "\n", 114 | " v[v .< 10]\n", 115 | " \n", 116 | "[9] What does `v .< 10` by itself do?\n", 117 | "\n", 118 | "[Note that higher-order functions and anonymous functions are commonly thought to be \"slow\" currently in Julia, and so should not be used in performance-critical parts of code.]" 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "execution_count": null, 124 | "metadata": { 125 | "collapsed": true 126 | }, 127 | "outputs": [], 128 | "source": [] 129 | } 130 | ], 131 | "metadata": { 132 | "kernelspec": { 133 | "display_name": "Julia 0.3.8-pre", 134 | "language": "julia", 135 | "name": "julia 0.3" 136 | }, 137 | "language_info": { 138 | "name": "julia", 139 | "version": "0.3.10" 140 | } 141 | }, 142 | "nbformat": 4, 143 | "nbformat_minor": 0 144 | } 145 | -------------------------------------------------------------------------------- /notebooks/Session I - Basic Julia/5. Matrices.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Matrices " 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "Many scientific calculations make heavy use of matrices. Julia has excellent support for matrices, including special classes, and interfaces to standard linear algebra libraries." 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "Matrices can be defined by hand, or using one of many functions for constructing matrices. To define matrices by hand, the following notation is used:" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": null, 27 | "metadata": { 28 | "collapsed": false 29 | }, 30 | "outputs": [], 31 | "source": [ 32 | "A = [1 2; 3 4]" 33 | ] 34 | }, 35 | { 36 | "cell_type": "markdown", 37 | "metadata": {}, 38 | "source": [ 39 | "The spaces separate consecutive elements in the same row; the semicolons separate different rows." 40 | ] 41 | }, 42 | { 43 | "cell_type": "markdown", 44 | "metadata": {}, 45 | "source": [ 46 | "[1] What is the type of `A`? What information is thus stored in the type of an array (comparing the types of a matrix and a vector)?\n", 47 | "\n", 48 | "[2] What does `R = [1 2]` do?\n", 49 | "\n", 50 | "[3] Define another $2 \\times 2$ matrix `B` and experiment with matrix operations.\n", 51 | "\n", 52 | "[4] How can we construct a matrix by dividing element by element the elements of `A` by those of `B`? \n", 53 | "\n", 54 | "[5] Experiment with different ways of constructing matrices: `zeros`, `ones`, `eye`, `diag`. Use the help function if necessary.\n", 55 | "\n", 56 | "Matrix-vector and matrix-matrix multiplication uses the `*` operator. \n", 57 | "\n", 58 | "[6] Check that `*` is matrix-matrix multiplication. How is element-by-element multiplication done?" 59 | ] 60 | }, 61 | { 62 | "cell_type": "markdown", 63 | "metadata": {}, 64 | "source": [ 65 | "Matrices may also be constructed using array comprehensions, by using a double `for` loop of the type\n", 66 | "\n", 67 | " for x in 1:10, y in 1:10\n", 68 | " ...\n", 69 | " end\n", 70 | " \n", 71 | "[7] Construct a matrix $M$ whose $(i,j)$th element is $i+j$." 72 | ] 73 | }, 74 | { 75 | "cell_type": "markdown", 76 | "metadata": {}, 77 | "source": [ 78 | "## Exercise: Calculating the largest eigenvalue of a matrix" 79 | ] 80 | }, 81 | { 82 | "cell_type": "markdown", 83 | "metadata": {}, 84 | "source": [ 85 | "In this exercise, we shall use a simple method, the *power method*, to calculate the largest eigenvalue $\\lambda_1$ of a matrix.\n", 86 | "\n", 87 | "[\n", 88 | "Recall that $\\lambda$ is an *eigenvalue* of the matrix $\\mathsf{M}$ with corresponding *eigenvector* $\\mathbf{v}$ if\n", 89 | "\n", 90 | "$$ \\mathsf{M} \\cdot \\mathbf{v} = \\lambda \\mathbf{v}, $$\n", 91 | "\n", 92 | "i.e. if the direction of $\\mathbf{v}$ remains unchanged when acted on by $\\mathsf{M}$.\n", 93 | "]\n", 94 | "\n", 95 | "The power method consists of simply multiplying an arbitrary (non-zero) initial vector $\\mathbf{v}_0$ by $\\mathsf{M}$ many times:\n", 96 | "\n", 97 | "$$\\mathbf{v}_{n+1} := \\mathsf{M} \\cdot \\mathbf{v}_n$$.\n", 98 | "\n", 99 | "[1] Implement the power method to calculate the largest eigenvalue $\\lambda_1$ of a given matrix. What happens? How can we fix this?\n", 100 | "\n", 101 | "[2] How can we find the corresponding eigenvector?\n", 102 | "\n", 103 | "`**` [3] Can this be extended to find several eigenvectors? (Suppose that the matrix is *symmetric* and that the eigenvectors are thus *orthogonal* with respect to the standard scalar product.)" 104 | ] 105 | }, 106 | { 107 | "cell_type": "markdown", 108 | "metadata": {}, 109 | "source": [ 110 | "## Linear algebra" 111 | ] 112 | }, 113 | { 114 | "cell_type": "markdown", 115 | "metadata": {}, 116 | "source": [ 117 | "Julia provides a simple interface to standard LAPACK routines.\n", 118 | "\n", 119 | "[4] Guess the names for calculating the determinant, inverse, and eigenvalues/vectors. (Use tab completion.)\n", 120 | "What format are the eigenvectors returned in?" 121 | ] 122 | }, 123 | { 124 | "cell_type": "markdown", 125 | "metadata": {}, 126 | "source": [ 127 | "The notation for solving the system of linear equations $\\mathsf{M} \\cdot \\mathbf{x} = \\mathbf{c}$ for the unknown variables $\\mathbf{x}$ is \n", 128 | "\n", 129 | " M \\ b" 130 | ] 131 | }, 132 | { 133 | "cell_type": "markdown", 134 | "metadata": {}, 135 | "source": [ 136 | "## Exercise: Distribution of eigenvalues of a random matrix" 137 | ] 138 | }, 139 | { 140 | "cell_type": "markdown", 141 | "metadata": {}, 142 | "source": [ 143 | "Random matrices are important in several branches of mathematics and physics. Let's calculate a few properties of their eigenvalues." 144 | ] 145 | }, 146 | { 147 | "cell_type": "markdown", 148 | "metadata": {}, 149 | "source": [ 150 | "[1] Create a random matrix `M` of samples from a Gaussian / normal distribution using `randn`.\n", 151 | "\n", 152 | "[2] Make a symmetric version of the matrix using `Symmetric`. What is the type of the resulting object? Check using a small matrix exactly what it does.\n", 153 | "\n", 154 | "[3] Calculate all eigenvalues of `M`, normalize them by and store them in a variable `lamb`. \n", 155 | "\n", 156 | "[4] Plot `lamb`. Now make a histogram.\n", 157 | "\n", 158 | "[5] Repeat the eigenvalue calculation with different realizations of the disorder, and include all of the results in the histogram to improve the statistics.\n", 159 | "\n", 160 | "[6] Calculate differences between successive eigenvalues, and normalise the result by its mean. To this several times to improve the statistics, and compare the histogram of the result with the known density function,\n", 161 | "\n", 162 | "$$f(s) = \\frac{\\pi}{2} \\, s \\, e^{-\\pi s^2/4}.$$\n", 163 | "\n", 164 | "[7] Install the [`KernelDensity`](https://github.com/JuliaStats/KernelDensity.jl) package and use kernel density estimation instead." 165 | ] 166 | } 167 | ], 168 | "metadata": { 169 | "kernelspec": { 170 | "display_name": "Julia 0.3.9-pre", 171 | "language": "julia", 172 | "name": "julia-0.3" 173 | }, 174 | "language_info": { 175 | "name": "julia", 176 | "version": "0.3.10" 177 | } 178 | }, 179 | "nbformat": 4, 180 | "nbformat_minor": 0 181 | } 182 | -------------------------------------------------------------------------------- /notebooks/Session I - Basic Julia/6. Interactivity using Interact.jl.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Interactivity in IJulia using `Interact.jl`" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "The `Interact.jl` package makes Jupyter's interactive widget functionality available from Julia." 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "[1] Add the `Interact.jl` package and load it. If you have IPython 3, you will need to checkout the `git` master branch of the package with" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 5, 27 | "metadata": { 28 | "collapsed": false 29 | }, 30 | "outputs": [ 31 | { 32 | "name": "stderr", 33 | "output_type": "stream", 34 | "text": [ 35 | "INFO: Checking out Interact master...\n", 36 | "INFO: Pulling Interact latest master...\n", 37 | "INFO: No packages to install, update or remove\n" 38 | ] 39 | } 40 | ], 41 | "source": [ 42 | "Pkg.checkout(\"Interact\")" 43 | ] 44 | }, 45 | { 46 | "cell_type": "markdown", 47 | "metadata": {}, 48 | "source": [ 49 | "The syntax to use widgets is to start with a `for` loop (that loops over the values of the variables that you would like to allow in the widgets) and wrap it in a `@manipulate` macro:" 50 | ] 51 | }, 52 | { 53 | "cell_type": "markdown", 54 | "metadata": {}, 55 | "source": [ 56 | "[2] Write a simple `for` loop that evaluates the square of the loop counter, and wrap it in `@manipulate`.\n", 57 | "A widget should appear that enables you to interactively move a slider to change the displayed value.\n", 58 | "\n", 59 | "[3] Use the `html` function (part of `Interact.jl`) to display repeated text, where the number of repetitions depends on the loop argument.\n", 60 | "\n", 61 | "[4] Use a double `for` loop to get two sliders." 62 | ] 63 | }, 64 | { 65 | "cell_type": "markdown", 66 | "metadata": {}, 67 | "source": [ 68 | "`Interact.jl` should work with `Gadfly` without further changes.\n", 69 | "\n", 70 | "To use `PyPlot`, it is necessary to first define a `figure` object with\n", 71 | "\n", 72 | " f = figure()\n", 73 | "\n", 74 | "and then use the `withfig` function as\n", 75 | "\n", 76 | " withfig(f) do\n", 77 | " ...\n", 78 | " end\n", 79 | " \n", 80 | "inside the `@manipulate`d `for` loop.\n" 81 | ] 82 | }, 83 | { 84 | "cell_type": "markdown", 85 | "metadata": {}, 86 | "source": [ 87 | "[5] Make an interactive visualisation of a quadratic fucntion, in which you can manipulate all of the coefficients.\n", 88 | "\n", 89 | "[6] Make an interactive visualisation of a ball moving " 90 | ] 91 | } 92 | ], 93 | "metadata": { 94 | "kernelspec": { 95 | "display_name": "Julia 0.3.8-pre", 96 | "language": "julia", 97 | "name": "julia 0.3" 98 | }, 99 | "language_info": { 100 | "name": "julia", 101 | "version": "0.3.10" 102 | } 103 | }, 104 | "nbformat": 4, 105 | "nbformat_minor": 0 106 | } 107 | -------------------------------------------------------------------------------- /notebooks/Session I - Basic Julia/7. Functions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Generic functions and multiple dispatch" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "We have already seen how to define functions in a basic way, and we have used some predefined functions in Julia.\n", 15 | "But the story about functions in Julia is deeper and more interesting.\n", 16 | "\n", 17 | "Let's consider the operator `+`. In Julia, `+` and all other similar operators are, in fact, functions:" 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": 2, 23 | "metadata": { 24 | "collapsed": false 25 | }, 26 | "outputs": [ 27 | { 28 | "data": { 29 | "text/plain": [ 30 | "+ (generic function with 117 methods)" 31 | ] 32 | }, 33 | "execution_count": 2, 34 | "metadata": {}, 35 | "output_type": "execute_result" 36 | } 37 | ], 38 | "source": [ 39 | "+" 40 | ] 41 | }, 42 | { 43 | "cell_type": "markdown", 44 | "metadata": {}, 45 | "source": [ 46 | "We can call them just as if they were functions:" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": 3, 52 | "metadata": { 53 | "collapsed": false 54 | }, 55 | "outputs": [ 56 | { 57 | "data": { 58 | "text/plain": [ 59 | "7" 60 | ] 61 | }, 62 | "execution_count": 3, 63 | "metadata": {}, 64 | "output_type": "execute_result" 65 | } 66 | ], 67 | "source": [ 68 | "+(3, 4)" 69 | ] 70 | }, 71 | { 72 | "cell_type": "markdown", 73 | "metadata": {}, 74 | "source": [ 75 | "The infix notation `3 + 4` is just \"syntactic sugar\" (i.e. a more convenient-for-humans way of writing the same thing).\n", 76 | "\n", 77 | "We see from Julia's response that `+` is what is called a *generic function* with a certain (large) number of *methods*. The available methods are obtained using the function called `methods`:" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": 4, 83 | "metadata": { 84 | "collapsed": false 85 | }, 86 | "outputs": [ 87 | { 88 | "data": { 89 | "text/html": [ 90 | "117 methods for generic function +:" 91 | ], 92 | "text/plain": [ 93 | "# 117 methods for generic function \"+\":\n", 94 | "+(x::Bool) at bool.jl:36\n", 95 | "+(x::Bool,y::Bool) at bool.jl:39\n", 96 | "+(y::FloatingPoint,x::Bool) at bool.jl:49\n", 97 | "+(A::BitArray{N},B::BitArray{N}) at bitarray.jl:848\n", 98 | "+(A::Union(SubArray{Bool,N,A<:DenseArray{T,N},I<:(Union(Int64,Range{Int64})...,)},DenseArray{Bool,N}),B::Union(SubArray{Bool,N,A<:DenseArray{T,N},I<:(Union(Int64,Range{Int64})...,)},DenseArray{Bool,N})) at array.jl:801\n", 99 | "+{S,T}(A::Union(DenseArray{S,N},SubArray{S,N,A<:DenseArray{T,N},I<:(Union(Int64,Range{Int64})...,)}),B::Union(DenseArray{T,N},SubArray{T,N,A<:DenseArray{T,N},I<:(Union(Int64,Range{Int64})...,)})) at array.jl:723\n", 100 | "+{T<:Union(Int16,Int32,Int8)}(x::T<:Union(Int16,Int32,Int8),y::T<:Union(Int16,Int32,Int8)) at int.jl:16\n", 101 | "+{T<:Union(Uint32,Uint16,Uint8)}(x::T<:Union(Uint32,Uint16,Uint8),y::T<:Union(Uint32,Uint16,Uint8)) at int.jl:20\n", 102 | "+(x::Int64,y::Int64) at int.jl:33\n", 103 | "+(x::Uint64,y::Uint64) at int.jl:34\n", 104 | "+(x::Int128,y::Int128) at int.jl:35\n", 105 | "+(x::Uint128,y::Uint128) at int.jl:36\n", 106 | "+(x::Float32,y::Float32) at float.jl:119\n", 107 | "+(x::Float64,y::Float64) at float.jl:120\n", 108 | "+(z::Complex{T<:Real},w::Complex{T<:Real}) at complex.jl:110\n", 109 | "+(x::Real,z::Complex{T<:Real}) at complex.jl:120\n", 110 | "+(z::Complex{T<:Real},x::Real) at complex.jl:121\n", 111 | "+(x::Rational{T<:Integer},y::Rational{T<:Integer}) at rational.jl:113\n", 112 | "+(x::Char,y::Char) at char.jl:23\n", 113 | "+(x::Char,y::Integer) at char.jl:26\n", 114 | "+(x::Integer,y::Char) at char.jl:27\n", 115 | "+(a::Float16,b::Float16) at float16.jl:132\n", 116 | "+(x::BigInt,y::BigInt) at gmp.jl:195\n", 117 | "+(a::BigInt,b::BigInt,c::BigInt) at gmp.jl:218\n", 118 | "+(a::BigInt,b::BigInt,c::BigInt,d::BigInt) at gmp.jl:224\n", 119 | "+(a::BigInt,b::BigInt,c::BigInt,d::BigInt,e::BigInt) at gmp.jl:231\n", 120 | "+(x::BigInt,c::Union(Uint32,Uint64,Uint16,Uint8)) at gmp.jl:243\n", 121 | "+(c::Union(Uint32,Uint64,Uint16,Uint8),x::BigInt) at gmp.jl:247\n", 122 | "+(x::BigInt,c::Union(Int16,Int64,Int32,Int8)) at gmp.jl:259\n", 123 | "+(c::Union(Int16,Int64,Int32,Int8),x::BigInt) at gmp.jl:260\n", 124 | "+(x::BigFloat,y::BigFloat) at mpfr.jl:149\n", 125 | "+(x::BigFloat,c::Union(Uint32,Uint64,Uint16,Uint8)) at mpfr.jl:156\n", 126 | "+(c::Union(Uint32,Uint64,Uint16,Uint8),x::BigFloat) at mpfr.jl:160\n", 127 | "+(x::BigFloat,c::Union(Int16,Int64,Int32,Int8)) at mpfr.jl:164\n", 128 | "+(c::Union(Int16,Int64,Int32,Int8),x::BigFloat) at mpfr.jl:168\n", 129 | "+(x::BigFloat,c::Union(Float32,Float16,Float64)) at mpfr.jl:172\n", 130 | "+(c::Union(Float32,Float16,Float64),x::BigFloat) at mpfr.jl:176\n", 131 | "+(x::BigFloat,c::BigInt) at mpfr.jl:180\n", 132 | "+(c::BigInt,x::BigFloat) at mpfr.jl:184\n", 133 | "+(a::BigFloat,b::BigFloat,c::BigFloat) at mpfr.jl:255\n", 134 | "+(a::BigFloat,b::BigFloat,c::BigFloat,d::BigFloat) at mpfr.jl:261\n", 135 | "+(a::BigFloat,b::BigFloat,c::BigFloat,d::BigFloat,e::BigFloat) at mpfr.jl:268\n", 136 | "+(x::MathConst{sym},y::MathConst{sym}) at constants.jl:23\n", 137 | "+{T<:Number}(x::T<:Number,y::T<:Number) at promotion.jl:188\n", 138 | "+{T<:FloatingPoint}(x::Bool,y::T<:FloatingPoint) at bool.jl:46\n", 139 | "+(x::Number,y::Number) at promotion.jl:158\n", 140 | "+(x::Integer,y::Ptr{T}) at pointer.jl:68\n", 141 | "+(x::Bool,A::AbstractArray{Bool,N}) at array.jl:771\n", 142 | "+(x::Number) at operators.jl:71\n", 143 | "+(r1::OrdinalRange{T,S},r2::OrdinalRange{T,S}) at operators.jl:325\n", 144 | "+{T<:FloatingPoint}(r1::FloatRange{T<:FloatingPoint},r2::FloatRange{T<:FloatingPoint}) at operators.jl:331\n", 145 | "+(r1::FloatRange{T<:FloatingPoint},r2::FloatRange{T<:FloatingPoint}) at operators.jl:348\n", 146 | "+(r1::FloatRange{T<:FloatingPoint},r2::OrdinalRange{T,S}) at operators.jl:349\n", 147 | "+(r1::OrdinalRange{T,S},r2::FloatRange{T<:FloatingPoint}) at operators.jl:350\n", 148 | "+(x::Ptr{T},y::Integer) at pointer.jl:66\n", 149 | "+{S,T<:Real}(A::Union(DenseArray{S,N},SubArray{S,N,A<:DenseArray{T,N},I<:(Union(Int64,Range{Int64})...,)}),B::Range{T<:Real}) at array.jl:731\n", 150 | "+{S<:Real,T}(A::Range{S<:Real},B::Union(DenseArray{T,N},SubArray{T,N,A<:DenseArray{T,N},I<:(Union(Int64,Range{Int64})...,)})) at array.jl:740\n", 151 | "+(A::AbstractArray{Bool,N},x::Bool) at array.jl:770\n", 152 | "+{Tv,Ti}(A::SparseMatrixCSC{Tv,Ti},B::SparseMatrixCSC{Tv,Ti}) at sparse/sparsematrix.jl:552\n", 153 | "+{TvA,TiA,TvB,TiB}(A::SparseMatrixCSC{TvA,TiA},B::SparseMatrixCSC{TvB,TiB}) at sparse/sparsematrix.jl:544\n", 154 | "+(A::SparseMatrixCSC{Tv,Ti<:Integer},B::Array{T,N}) at sparse/sparsematrix.jl:643\n", 155 | "+(A::Array{T,N},B::SparseMatrixCSC{Tv,Ti<:Integer}) at sparse/sparsematrix.jl:645\n", 156 | "+(A::SymTridiagonal{T},B::SymTridiagonal{T}) at linalg/tridiag.jl:57\n", 157 | "+(A::Tridiagonal{T},B::Tridiagonal{T}) at linalg/tridiag.jl:247\n", 158 | "+(A::Tridiagonal{T},B::SymTridiagonal{T}) at linalg/special.jl:99\n", 159 | "+(A::SymTridiagonal{T},B::Tridiagonal{T}) at linalg/special.jl:98\n", 160 | "+{T,MT,uplo}(A::Triangular{T,MT,uplo,IsUnit},B::Triangular{T,MT,uplo,IsUnit}) at linalg/triangular.jl:11\n", 161 | "+{T,MT,uplo1,uplo2}(A::Triangular{T,MT,uplo1,IsUnit},B::Triangular{T,MT,uplo2,IsUnit}) at linalg/triangular.jl:12\n", 162 | "+(Da::Diagonal{T},Db::Diagonal{T}) at linalg/diagonal.jl:47\n", 163 | "+(A::Bidiagonal{T},B::Bidiagonal{T}) at linalg/bidiag.jl:92\n", 164 | "+{T}(B::BitArray{2},J::UniformScaling{T}) at linalg/uniformscaling.jl:26\n", 165 | "+(A::Diagonal{T},B::Bidiagonal{T}) at linalg/special.jl:89\n", 166 | "+(A::Bidiagonal{T},B::Diagonal{T}) at linalg/special.jl:90\n", 167 | "+(A::Diagonal{T},B::Tridiagonal{T}) at linalg/special.jl:89\n", 168 | "+(A::Tridiagonal{T},B::Diagonal{T}) at linalg/special.jl:90\n", 169 | "+(A::Diagonal{T},B::Triangular{T,S<:AbstractArray{T,2},UpLo,IsUnit}) at linalg/special.jl:89\n", 170 | "+(A::Triangular{T,S<:AbstractArray{T,2},UpLo,IsUnit},B::Diagonal{T}) at linalg/special.jl:90\n", 171 | "+(A::Diagonal{T},B::Array{T,2}) at linalg/special.jl:89\n", 172 | "+(A::Array{T,2},B::Diagonal{T}) at linalg/special.jl:90\n", 173 | "+(A::Bidiagonal{T},B::Tridiagonal{T}) at linalg/special.jl:89\n", 174 | "+(A::Tridiagonal{T},B::Bidiagonal{T}) at linalg/special.jl:90\n", 175 | "+(A::Bidiagonal{T},B::Triangular{T,S<:AbstractArray{T,2},UpLo,IsUnit}) at linalg/special.jl:89\n", 176 | "+(A::Triangular{T,S<:AbstractArray{T,2},UpLo,IsUnit},B::Bidiagonal{T}) at linalg/special.jl:90\n", 177 | "+(A::Bidiagonal{T},B::Array{T,2}) at linalg/special.jl:89\n", 178 | "+(A::Array{T,2},B::Bidiagonal{T}) at linalg/special.jl:90\n", 179 | "+(A::Tridiagonal{T},B::Triangular{T,S<:AbstractArray{T,2},UpLo,IsUnit}) at linalg/special.jl:89\n", 180 | "+(A::Triangular{T,S<:AbstractArray{T,2},UpLo,IsUnit},B::Tridiagonal{T}) at linalg/special.jl:90\n", 181 | "+(A::Tridiagonal{T},B::Array{T,2}) at linalg/special.jl:89\n", 182 | "+(A::Array{T,2},B::Tridiagonal{T}) at linalg/special.jl:90\n", 183 | "+(A::Triangular{T,S<:AbstractArray{T,2},UpLo,IsUnit},B::Array{T,2}) at linalg/special.jl:89\n", 184 | "+(A::Array{T,2},B::Triangular{T,S<:AbstractArray{T,2},UpLo,IsUnit}) at linalg/special.jl:90\n", 185 | "+(A::SymTridiagonal{T},B::Triangular{T,S<:AbstractArray{T,2},UpLo,IsUnit}) at linalg/special.jl:98\n", 186 | "+(A::Triangular{T,S<:AbstractArray{T,2},UpLo,IsUnit},B::SymTridiagonal{T}) at linalg/special.jl:99\n", 187 | "+(A::SymTridiagonal{T},B::Array{T,2}) at linalg/special.jl:98\n", 188 | "+(A::Array{T,2},B::SymTridiagonal{T}) at linalg/special.jl:99\n", 189 | "+(A::Diagonal{T},B::SymTridiagonal{T}) at linalg/special.jl:107\n", 190 | "+(A::SymTridiagonal{T},B::Diagonal{T}) at linalg/special.jl:108\n", 191 | "+(A::Bidiagonal{T},B::SymTridiagonal{T}) at linalg/special.jl:107\n", 192 | "+(A::SymTridiagonal{T},B::Bidiagonal{T}) at linalg/special.jl:108\n", 193 | "+{T<:Number}(x::AbstractArray{T<:Number,N}) at abstractarray.jl:362\n", 194 | "+(A::AbstractArray{T,N},x::Number) at array.jl:774\n", 195 | "+(x::Number,A::AbstractArray{T,N}) at array.jl:775\n", 196 | "+(J1::UniformScaling{T<:Number},J2::UniformScaling{T<:Number}) at linalg/uniformscaling.jl:25\n", 197 | "+(J::UniformScaling{T<:Number},B::BitArray{2}) at linalg/uniformscaling.jl:27\n", 198 | "+(J::UniformScaling{T<:Number},A::AbstractArray{T,2}) at linalg/uniformscaling.jl:28\n", 199 | "+(J::UniformScaling{T<:Number},x::Number) at linalg/uniformscaling.jl:29\n", 200 | "+(x::Number,J::UniformScaling{T<:Number}) at linalg/uniformscaling.jl:30\n", 201 | "+{TA,TJ}(A::AbstractArray{TA,2},J::UniformScaling{TJ}) at linalg/uniformscaling.jl:33\n", 202 | "+{T}(a::HierarchicalValue{T},b::HierarchicalValue{T}) at pkg/resolve/versionweight.jl:19\n", 203 | "+(a::VWPreBuildItem,b::VWPreBuildItem) at pkg/resolve/versionweight.jl:81\n", 204 | "+(a::VWPreBuild,b::VWPreBuild) at pkg/resolve/versionweight.jl:127\n", 205 | "+(a::VersionWeight,b::VersionWeight) at pkg/resolve/versionweight.jl:181\n", 206 | "+(a::FieldValue,b::FieldValue) at pkg/resolve/fieldvalue.jl:41\n", 207 | "+(a::Vec2,b::Vec2) at graphics.jl:60\n", 208 | "+(bb1::BoundingBox,bb2::BoundingBox) at graphics.jl:123\n", 209 | "+(a,b,c) at operators.jl:82\n", 210 | "+(a,b,c,xs...) at operators.jl:83" 211 | ] 212 | }, 213 | "execution_count": 4, 214 | "metadata": {}, 215 | "output_type": "execute_result" 216 | } 217 | ], 218 | "source": [ 219 | "methods(+)" 220 | ] 221 | }, 222 | { 223 | "cell_type": "markdown", 224 | "metadata": {}, 225 | "source": [ 226 | "These methods are the different \"versions\" of `+`, distinguished by the *types of their arguments*, as denoted by the *type annotations* with `::`. Julia thus allows us to define a different method for any unique combination (tuple) of argument types, and picks the correct one to choose based on this. The \"meaning\" in all the cases should be the same -- in this case, addition. This meaning emerges out of a patchwork of different definitions that are suitable for different use cases. For example, the definition of `+` for the sum of two matrices may use the definition of `+` for two `Float64`s, which is in turn defined in terms of \"LLVM intrinsics\".\n", 227 | "\n", 228 | "[Note that when using IJulia or Juno, a link is provided that takes us directly to the line in the Julia source code at which the given method is defined.]\n", 229 | "\n", 230 | "Of course, this (whether a particular method \"belongs\" in a particular generic function) is sometimes ambiguous. For example, many languages use the `+` operator for string concatenation (i.e. making a single string by placing one string after another)." 231 | ] 232 | }, 233 | { 234 | "cell_type": "markdown", 235 | "metadata": {}, 236 | "source": [ 237 | "[1] Try to add two strings. What happens?" 238 | ] 239 | }, 240 | { 241 | "cell_type": "markdown", 242 | "metadata": {}, 243 | "source": [ 244 | "The designers of Julia decided that strings would be concatenated with `*` instead of `+`. [Basically this was due to the commutativity of `+` and the non-commutativity of `*`.\n", 245 | "In fact, a better solution is to use the `string` function.]\n", 246 | "\n", 247 | "[2] Try using `*` and using the `string` function. In what sense is the latter more useful?" 248 | ] 249 | }, 250 | { 251 | "cell_type": "markdown", 252 | "metadata": {}, 253 | "source": [ 254 | "However, on the Julia users mailing list and in Julia issues there are (very) long discussions about the wisdom or correctness of this decision. In many languages, this could represent a fundamental stumbling block, since methods are usually defined inside classes. In Julia, however, methods are defined *outside* the objects that they act on.\n", 255 | "\n", 256 | "We can, in fact, ourselves very easily *define* `+` to act on two strings:" 257 | ] 258 | }, 259 | { 260 | "cell_type": "markdown", 261 | "metadata": {}, 262 | "source": [ 263 | "[3] Define `+` acting on two strings, following the pattern of the methods that are already defined. Check using `methods` that it is defined, and check that it works." 264 | ] 265 | }, 266 | { 267 | "cell_type": "markdown", 268 | "metadata": {}, 269 | "source": [ 270 | "[4] Define a method of the function `my_abs` for real numbers, and a method for complex numbers.\n", 271 | "\n", 272 | "[5] Define a function `f` that gives `a + b` for numbers and strings, and `a - b` for floating point numbers. Note that this is really *not* a good idea in general!" 273 | ] 274 | }, 275 | { 276 | "cell_type": "markdown", 277 | "metadata": {}, 278 | "source": [ 279 | "This approach (selecting a method based on the types of its arguments) turns out to be very natural and very powerful. It receives the name **multiple dispatch**, \"dispatch\" being the action of selecting which function to call. Julia is one of very few languages to allow this in such generality." 280 | ] 281 | } 282 | ], 283 | "metadata": { 284 | "kernelspec": { 285 | "display_name": "Julia 0.3.9-pre", 286 | "language": "julia", 287 | "name": "julia-0.3" 288 | }, 289 | "language_info": { 290 | "name": "julia", 291 | "version": "0.3.10" 292 | } 293 | }, 294 | "nbformat": 4, 295 | "nbformat_minor": 0 296 | } 297 | -------------------------------------------------------------------------------- /notebooks/Session I - Basic Julia/8. Funny but useful syntax.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Slurping and splatting: `...`\n", 8 | "\n", 9 | "The `...` operator is used for two different (almost opposite) purposes:\n", 10 | "\n", 11 | "1. to \"unpack\" iterables (vectors, tuples, etc.) in order to pass them as separate arguments to a function\n", 12 | "2. to collect multiple arguments to a function into a tuple (\"varargs\")" 13 | ] 14 | }, 15 | { 16 | "cell_type": "markdown", 17 | "metadata": {}, 18 | "source": [ 19 | "[1] Define a function `f` that takes two arguments. What happens if you pass `f` a vector or tuple of two components? Work out how to use the `...` operator to unpack a vector of two arguments to send it to the function.\n", 20 | "\n", 21 | "[2] Define a function `g` that takes one argument that is a function, and a variable other number of arguments called `a` (use `...`). What type does `a` have [use, for example, `@show`]? Define the function so that it applies the given function to the given arguments.\n", 22 | "\n", 23 | "[3] The function `vcat` does a vertical concatenation of arrays (try it). Use `vcat` together with the (conjugate) transpose operator `'` to convert a vector of vectors into the corresponding 2D matrix." 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": {}, 29 | "source": [ 30 | "# Short-circuit evaluation " 31 | ] 32 | }, 33 | { 34 | "cell_type": "markdown", 35 | "metadata": {}, 36 | "source": [ 37 | "It is idiomatic (i.e., often used, and considered good style) in Julia to use special syntax for simple `if` and `if...else` constructions." 38 | ] 39 | }, 40 | { 41 | "cell_type": "markdown", 42 | "metadata": {}, 43 | "source": [ 44 | "For simple `if`s, we use the boolean operators `&&` and `||`, since they have \"short-circuit\" behaviour. This means that they evaluate their first argument and based on its value they may already know what the result is.\n", 45 | "\n", 46 | "For example, if the first argument of `&&` (\"and\") is false, then the result of the `&&` is false, so we can use it as an `if...then`:\n", 47 | "\n" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 12, 53 | "metadata": { 54 | "collapsed": false 55 | }, 56 | "outputs": [ 57 | { 58 | "name": "stdout", 59 | "output_type": "stream", 60 | "text": [ 61 | "Small\n", 62 | "Few digits\n" 63 | ] 64 | }, 65 | { 66 | "data": { 67 | "text/plain": [ 68 | "false" 69 | ] 70 | }, 71 | "execution_count": 12, 72 | "metadata": {}, 73 | "output_type": "execute_result" 74 | } 75 | ], 76 | "source": [ 77 | "x = 2\n", 78 | "(x < 3) && println(\"Small\") # equivalent to: if (x<3) println(\"Small\"); end\n", 79 | "x < 100 && println(\"Few digits\")\n", 80 | "(x < 0) && println(\"Negative\")" 81 | ] 82 | }, 83 | { 84 | "cell_type": "markdown", 85 | "metadata": {}, 86 | "source": [ 87 | "[1] What does `||` do?" 88 | ] 89 | }, 90 | { 91 | "cell_type": "markdown", 92 | "metadata": {}, 93 | "source": [ 94 | "Julia also has a C-style ternary operator for `if-else`, denoted `CONDITION ? X : Y`, which does `X` if the condition is true and `Y` if the condition is false.\n", 95 | "\n", 96 | "[2] Use the ternary operator to print out \"small\" or \"large\" if x is less than 100 or greater than 100." 97 | ] 98 | } 99 | ], 100 | "metadata": { 101 | "kernelspec": { 102 | "display_name": "Julia 0.3.9-pre", 103 | "language": "julia", 104 | "name": "julia-0.3" 105 | }, 106 | "language_info": { 107 | "name": "julia", 108 | "version": "0.3.10" 109 | } 110 | }, 111 | "nbformat": 4, 112 | "nbformat_minor": 0 113 | } 114 | -------------------------------------------------------------------------------- /notebooks/Session II - Intermediate Julia/1. Structuring Julia code.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# `include` " 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "While the Jupyter notebook is a great tool for interactive development, it can become unwieldy once code becomes too long and you start jumping around reexecuting different cells.\n", 15 | "\n", 16 | "In this case, it is time to separate out code implementing the functions you will call into a file, probably leaving the calling of those functions and the manipulation of the resulting data in the notebook.\n", 17 | "\n", 18 | "The simplest way to do this is to literally extract the code for the functions from the notebook and insert it into a Julia script, i.e. a file ending in `.jl`. This code can then be *included* in the notebook (or in another file, etc.) using\n", 19 | "\n", 20 | " include(\"code.jl\")\n", 21 | " \n", 22 | "The result is the same as if you literally copied and pasted the code from the file straight at the point where the `include` function was called." 23 | ] 24 | }, 25 | { 26 | "cell_type": "markdown", 27 | "metadata": {}, 28 | "source": [ 29 | "# Calling Julia scripts from the command line" 30 | ] 31 | }, 32 | { 33 | "cell_type": "markdown", 34 | "metadata": {}, 35 | "source": [ 36 | "Julia scripts can be called from the command line simply as\n", 37 | "\n", 38 | " > julia code.jl\n", 39 | " \n", 40 | "Any arguments used (e.g. \n", 41 | "\n", 42 | " > julia code.jl 1 2 3 hello\n", 43 | "\n", 44 | "are placed in a variable `ARGS`.\n", 45 | "\n", 46 | "[1] Write a script that prints out the arguments passed in. What type does `ARGS` have?\n", 47 | "\n", 48 | "[2] Modify the script to take two arguments, which will be converted into an integer and a float.\n", 49 | "To do so, check how many arguments are passed in and print out a message giving the syntax for calling the script.\n", 50 | "\n", 51 | "Note that if you want to call the script interactively from within a Julia session (instead of from the command line), you can just set `ARGS` by hand to specify the command line arguments." 52 | ] 53 | }, 54 | { 55 | "cell_type": "markdown", 56 | "metadata": { 57 | "collapsed": true 58 | }, 59 | "source": [ 60 | "# Modules" 61 | ] 62 | }, 63 | { 64 | "cell_type": "markdown", 65 | "metadata": {}, 66 | "source": [ 67 | "When we have developed functionality that we with to reuse in the future in other projects, we can make a Julia *module*, i.e. a library. Modules have the ability that they can (partially) hide implementation details from the user, by *export*ing only those functions that the author desires.\n", 68 | "\n", 69 | "The structure of a module is\n", 70 | "\n", 71 | " module MyModule\n", 72 | " \n", 73 | " import Base: show\n", 74 | " \n", 75 | " export f\n", 76 | " \n", 77 | " \n", 78 | " type MyType\n", 79 | " end\n", 80 | " \n", 81 | " g(x::MyType) = x\n", 82 | " f(x::MyType) = g(x)\n", 83 | " \n", 84 | " end\n", 85 | " \n", 86 | "Functions from base Julia that you wish to extend to work with your own types are imported. Functions to export are also listed; here, only the function `f`, and not `g`, will appear in the namespace once the module is imported. Nonetheless, `g` is available as `MyModule.g`." 87 | ] 88 | }, 89 | { 90 | "cell_type": "markdown", 91 | "metadata": {}, 92 | "source": [ 93 | "[1] Define a module for `Vector2D` that exports relevant functions. Put it in a file in the current directory. How can you load it with `using`?\n", 94 | "\n", 95 | "[2] If it is not in the current directory, Julia will still be able to find it if the directory is in the special variable called `LOAD_PATH`. Work out how to add a directory to `LOAD_PATH`. [If you wish to do this in every session, you can add this command to your `.juliarc.jl`.]" 96 | ] 97 | }, 98 | { 99 | "cell_type": "markdown", 100 | "metadata": {}, 101 | "source": [ 102 | "NB: It is important to note that you cannot redefine types directly; it is necessary to use the `workspace()` function to \"clean\" the current namespace before trying to do `using MyModule` again." 103 | ] 104 | }, 105 | { 106 | "cell_type": "markdown", 107 | "metadata": {}, 108 | "source": [ 109 | "It is common to split up the *implementation* of a module into separate files using `include` inside the module definition. Several modules may also be defined in the same file." 110 | ] 111 | }, 112 | { 113 | "cell_type": "markdown", 114 | "metadata": {}, 115 | "source": [ 116 | "## Documentation " 117 | ] 118 | }, 119 | { 120 | "cell_type": "markdown", 121 | "metadata": {}, 122 | "source": [ 123 | "As we all know, it is necessary to document your code for future readers (for example, yourself in a week's time).\n", 124 | "Julia includes documentation tools from v0.4 on; there is also a package, `Docile`, for 0.3, that has the same effect.\n", 125 | "\n", 126 | "The syntax to document an object, for example a function, is" 127 | ] 128 | }, 129 | { 130 | "cell_type": "code", 131 | "execution_count": 1, 132 | "metadata": { 133 | "collapsed": false 134 | }, 135 | "outputs": [ 136 | { 137 | "data": { 138 | "text/plain": [ 139 | "f (generic function with 1 method)" 140 | ] 141 | }, 142 | "execution_count": 1, 143 | "metadata": {}, 144 | "output_type": "execute_result" 145 | } 146 | ], 147 | "source": [ 148 | "VERSION < v\"0.4-\" && using Docile\n", 149 | "@doc doc\"\"\"\n", 150 | "`square` calculates the square of its argument.\n", 151 | "\"\"\" ->\n", 152 | "f(x) = x^2" 153 | ] 154 | }, 155 | { 156 | "cell_type": "markdown", 157 | "metadata": {}, 158 | "source": [ 159 | "[The exact syntax is liable to be simplified in the future.] We can use Markdown syntax." 160 | ] 161 | }, 162 | { 163 | "cell_type": "markdown", 164 | "metadata": {}, 165 | "source": [ 166 | "[In v0.3 you must also add the `Lexicon` package to be able to view the documentation using `?`; this, furthermore, only works in the REPL in v0.3.]" 167 | ] 168 | }, 169 | { 170 | "cell_type": "code", 171 | "execution_count": 4, 172 | "metadata": { 173 | "collapsed": false 174 | }, 175 | "outputs": [ 176 | { 177 | "name": "stdout", 178 | "output_type": "stream", 179 | "text": [ 180 | "search: fft fft! FFTW fftshift rfft ifft bfft ifft! bfft! ifftshift irfft brfft\n", 181 | "\n" 182 | ] 183 | }, 184 | { 185 | "ename": "MethodError", 186 | "evalue": "MethodError: `plain` has no method matching plain(::IOBuffer, ::Base.Markdown.LaTeX)\nClosest candidates are:\n plain(::IO, !Matched::Base.Markdown.Table)\n plain(::IO, !Matched::Array{T,1})\n plain(::IO, !Matched::Base.Markdown.MD)\n ...", 187 | "output_type": "error", 188 | "traceback": [ 189 | "MethodError: `plain` has no method matching plain(::IOBuffer, ::Base.Markdown.LaTeX)\nClosest candidates are:\n plain(::IO, !Matched::Base.Markdown.Table)\n plain(::IO, !Matched::Array{T,1})\n plain(::IO, !Matched::Base.Markdown.MD)\n ...", 190 | "", 191 | " in plain at markdown/render/plain.jl:8", 192 | " in writemime at markdown/render/plain.jl:73", 193 | " in writemime at multimedia.jl:43", 194 | " in sprint at iostream.jl:205", 195 | " in display_dict at /Users/dsanders/.julia/v0.4/IJulia/src/execute_request.jl:25" 196 | ] 197 | } 198 | ], 199 | "source": [ 200 | "?fft" 201 | ] 202 | }, 203 | { 204 | "cell_type": "markdown", 205 | "metadata": {}, 206 | "source": [ 207 | "[1] Document your automatic differentation code." 208 | ] 209 | } 210 | ], 211 | "metadata": { 212 | "kernelspec": { 213 | "display_name": "Julia 0.3.9-pre", 214 | "language": "julia", 215 | "name": "julia-0.3" 216 | }, 217 | "language_info": { 218 | "name": "julia", 219 | "version": "0.3.10" 220 | } 221 | }, 222 | "nbformat": 4, 223 | "nbformat_minor": 0 224 | } 225 | -------------------------------------------------------------------------------- /notebooks/Session II - Intermediate Julia/2. Defining new types.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Composite types " 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "Julia allows us to define new types; in fact, many Julia types, such as `Rational` and `Complex`, are defined directly in Julia code, and are on the same footing as the types we can define ourselves -- just try `methods(Rational)`, for example, to look at the code." 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "Suppose we wish to define a type to represent a rational number. We would need to store the numerator and denominator, and define functions such as `+` acting on two rationals, or on a rational and an integer, etc.\n", 22 | "\n", 23 | "Since we now already know how to extend arithmetic operators and other functions to act on rationals, we only need to know the syntax for declaring a new so-called **composite type** (often called a structure, record, or class in other languages). It looks like the following:" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 4, 29 | "metadata": { 30 | "collapsed": true 31 | }, 32 | "outputs": [], 33 | "source": [ 34 | "type MyRational\n", 35 | " numerator :: Int\n", 36 | " denominator :: Int\n", 37 | "end" 38 | ] 39 | }, 40 | { 41 | "cell_type": "markdown", 42 | "metadata": {}, 43 | "source": [ 44 | "This declares what an object (basically, a black box with contents) of type `MyRational` looks like, i.e. which variables it contains, and what types those variables have. \n", 45 | "\n", 46 | "In fact, it also (by default) declares a *constructor*, a function with the same name as the type that is used to create objects of the type:" 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "metadata": {}, 52 | "source": [ 53 | "[1] Use `methods` to find out which versions of the constructor have been created automatically for our new type. Create some objects. \n", 54 | "\n", 55 | "An object `r` has two fields which are accessed by `r.numerator` and `r.denominator`. You can see the list of available properties of an object using tab completion or the `names` function [changed to `fieldnames` in v0.4]. \n", 56 | "\n", 57 | "[2] Define `*` and `+` for two objects of the `MyRational` type. [Hint: What should these functions return?]" 58 | ] 59 | }, 60 | { 61 | "cell_type": "markdown", 62 | "metadata": {}, 63 | "source": [ 64 | "The definition of the built-in `Rational` type takes a very similar form." 65 | ] 66 | }, 67 | { 68 | "cell_type": "markdown", 69 | "metadata": {}, 70 | "source": [ 71 | "As with any generic function, we can ourselves add methods:\n", 72 | "\n", 73 | "[3] Add a method to the constructor that takes a single integer and constructs the corresponding rational number.\n", 74 | "Such methods are called *outer constructors*, since they are placed outside the definition of the type." 75 | ] 76 | }, 77 | { 78 | "cell_type": "markdown", 79 | "metadata": {}, 80 | "source": [ 81 | "## Exercise: `Vector2D`" 82 | ] 83 | }, 84 | { 85 | "cell_type": "markdown", 86 | "metadata": {}, 87 | "source": [ 88 | "As we have seen, Julia's arrays act both as storage and as vectors. Currently, there are no *fixed-size* vectors in base Julia (although this is expected to change in the future). Nonetheless, fixed size arrays are important in many aplications. [There are the [`ImmutableArrays`](https://github.com/JuliaGeometry/ImmutableArrays.jl) and, more recently, [`FixedSizeArrays.jl`](https://github.com/SimonDanisch/FixedSizeArrays.jl) packages.] \n", 89 | "\n", 90 | "For example, suppose we wish to model a particle moving in 2 dimensions, with a 2-dimensional position vector and a 2-dimensional velocity vector. We could use a standard Julia array for the position and velocity, but this will, in principle, be less efficient than if we could directly use an array of fixed size 2. We can easily define such an object by hand:" 91 | ] 92 | }, 93 | { 94 | "cell_type": "markdown", 95 | "metadata": {}, 96 | "source": [ 97 | "[1] Define a `Vector2D` object that has `x` and `y` components that are `Float64`s.\n", 98 | "\n", 99 | "[2] Define `+`, multiplication by a scalar, and dot product. [For non-arithmetic operations, you must explicitly tell Julia that you are not creating a new function but are extending the previous pre-existing function. This may be done by defining explicitly `Base.dot`, or by first importing the function using `import Base: dot`.]" 100 | ] 101 | }, 102 | { 103 | "cell_type": "markdown", 104 | "metadata": {}, 105 | "source": [ 106 | "[3] Test the relative efficiency of standard Julia vectors and these `Vector2D` objects. To do so, use the `@time` macro, wrapped around `begin...end` blocks or function calls. Don't forget to run the code once before using `@time` in order to compile the function." 107 | ] 108 | }, 109 | { 110 | "cell_type": "markdown", 111 | "metadata": {}, 112 | "source": [ 113 | "## Exercise: Particles" 114 | ] 115 | }, 116 | { 117 | "cell_type": "markdown", 118 | "metadata": {}, 119 | "source": [ 120 | "[3] Define a `Particle` type that contains a position and a velocity. Define a function `move!` that takes an argument $\\delta t$ and moves the particle over a time step with that time.\n", 121 | "\n", 122 | "[4] Define a `Gas` type that consists of `N` particles. Write a constructor that accepts a number `N` and creates `N` particles with random positions and velocities. Define a function `move!` that moves the whole gas." 123 | ] 124 | }, 125 | { 126 | "cell_type": "markdown", 127 | "metadata": {}, 128 | "source": [ 129 | "## Exercise: Automatic differentiation " 130 | ] 131 | }, 132 | { 133 | "cell_type": "markdown", 134 | "metadata": {}, 135 | "source": [ 136 | "A rather interesting, but surprisingly little-known algorithmic technique is that of *automatic* (or *algorithmic*) differentiation, which allows us to differentiate a function exactly by defining a suitable type and using operator overloading.\n", 137 | "\n", 138 | "The idea is to calculate the derivative of a complicated function by splitting it up into simple pieces. In the simplest version, this is done automatically just by defining suitable methods. Pieces may look, for example, like $u+v$, whose derivative at a point $a$ is $u'(a) + v'(a)$, or $u \\cdot v$, whose derivative at $a$ is $u'(a) v(a) + u(a) v'(a)$. Thus the information required for a function $u$ is just $u(a)$ and $u'(a)$, which we will denote by $u$ and $u'$.\n", 139 | "\n", 140 | "[1] Define a type `AutoDiff` that contains `Float64`s `u` and `u'`. [The latter may be written `u\\prime`.]\n", 141 | "\n", 142 | "[2] Using the standard rules of differentiation, define `+` and `*` such that the result gives the value and derivative of the corresponding function.\n", 143 | "\n", 144 | "[3] Write a function `sin` acting on an `AutoDiff` object. Define a function $f(x) = \\sin(x^2)$. You should be able to *automatically differentiate* the function if you pass it the correct type of object. What object should you pass it to calculate $f'(a)$ at a point $a$?" 145 | ] 146 | } 147 | ], 148 | "metadata": { 149 | "kernelspec": { 150 | "display_name": "Julia 0.3.9-pre", 151 | "language": "julia", 152 | "name": "julia-0.3" 153 | }, 154 | "language_info": { 155 | "name": "julia", 156 | "version": "0.3.10" 157 | } 158 | }, 159 | "nbformat": 4, 160 | "nbformat_minor": 0 161 | } 162 | -------------------------------------------------------------------------------- /notebooks/Session II - Intermediate Julia/3. Parametrised types.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Parametrised types " 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "In the previous notebook, we defined a `Vector2D` type that contained `Float64`s.\n", 15 | "But we can think of 2D vectors that contain any kind of objects, not just `Float64`s. One solution would be to leave off the type annotations when defining the type, but this will allow any types for either of the two variables.\n", 16 | "\n", 17 | "Often we rather want to guarantee that they both have the *same* type, but allow the possibility of putting *any* type there. Julia allows this with *parametrised types*: we introduce a *type parameter*:\n" 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": 3, 23 | "metadata": { 24 | "collapsed": true 25 | }, 26 | "outputs": [], 27 | "source": [ 28 | "type Vector2D{T}\n", 29 | " x::T\n", 30 | " y::T\n", 31 | "end" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": {}, 37 | "source": [ 38 | "This allows an arbitrary number of *different* `Vector2D` types with different contents:" 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "metadata": {}, 44 | "source": [ 45 | "[1] Try creating a `Vector2D` containing two integers. What is the type of the resulting object?\n", 46 | "\n", 47 | "[2] Do the same for two `Float64`.\n", 48 | "\n", 49 | "[3] What happens if you try one integer and one float?\n", 50 | "\n", 51 | "[4] What do you think `Vector2D{Float64}(3, 4)` should do? Does it work?" 52 | ] 53 | }, 54 | { 55 | "cell_type": "markdown", 56 | "metadata": {}, 57 | "source": [ 58 | "Functions such as `+` can also be parametrised using `{T}` after the function name. \n", 59 | "\n", 60 | "[5] Define a parametrised `+` for `Vector2D`. Can you define a specialised `+` for one particular type of `Vector2D` (i.e. for one particular value of `T`)?" 61 | ] 62 | }, 63 | { 64 | "cell_type": "markdown", 65 | "metadata": {}, 66 | "source": [ 67 | "## Inner constructors" 68 | ] 69 | }, 70 | { 71 | "cell_type": "markdown", 72 | "metadata": {}, 73 | "source": [ 74 | "Often, it is necessary to create a constructor that does some validation of the arguments before the object is created. These are placed *inside* the definition of the type, and so are called *inner constructors*. They use a special function `new` which is passed the values of the variables; some may be left uninitialized.\n", 75 | "\n", 76 | "If an inner constructor is provided, then the default constructors are no longer provided. In the case of parametrised types, we often need to provide reasonable \"default\" constructors in this case." 77 | ] 78 | }, 79 | { 80 | "cell_type": "markdown", 81 | "metadata": {}, 82 | "source": [ 83 | "[1] Define an `Interval` type to represent an interval of real numbers with a `min` and a `max`. Use an inner constructor to ensure that the `min` is less than the `max`. [What could you do if not?]" 84 | ] 85 | } 86 | ], 87 | "metadata": { 88 | "kernelspec": { 89 | "display_name": "Julia 0.3.9-pre", 90 | "language": "julia", 91 | "name": "julia-0.3" 92 | }, 93 | "language_info": { 94 | "name": "julia", 95 | "version": "0.3.10" 96 | } 97 | }, 98 | "nbformat": 4, 99 | "nbformat_minor": 0 100 | } 101 | -------------------------------------------------------------------------------- /notebooks/Session II - Intermediate Julia/4. Testing.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Testing and exception handling" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "The importance of testing has been recognised in software engineering for the last 20 years, but in many scientific computing contexts it has only recently been discovered.\n", 15 | "\n", 16 | "Julia has (at least) two commonly-used testing frameworks: `Base.Test` and the [`FactCheck.jl`](https://github.com/JuliaLang/FactCheck.jl) package. `Base.Test` is used for writing tests for base Julia, while `FactCheck.jl` is commonly recommended for new packages (and we second this recommendation)." 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": {}, 22 | "source": [ 23 | "## `Base.Test`" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": {}, 29 | "source": [ 30 | "`Base.Test` is relatively basic: it provides a macro `@test`, as well as several variants (use tab completion), that take an expression and test its value. For example:" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": null, 36 | "metadata": { 37 | "collapsed": true 38 | }, 39 | "outputs": [], 40 | "source": [ 41 | "using Base.Test" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": null, 47 | "metadata": { 48 | "collapsed": false 49 | }, 50 | "outputs": [], 51 | "source": [ 52 | "f(x) = x^2\n", 53 | "@test f(3) == 9" 54 | ] 55 | }, 56 | { 57 | "cell_type": "markdown", 58 | "metadata": {}, 59 | "source": [ 60 | "[1] What happens if you write a test in which `f(3)` is compared with 10?\n", 61 | "\n", 62 | "It is common to put all the tests in a file called \"runtests.jl\" (or split them up into separate files and `include` those in `runtest.jl`).\n", 63 | "\n", 64 | "[2] Design tests for the `Vector2D` and `AutoDiff` types and associated functions." 65 | ] 66 | }, 67 | { 68 | "cell_type": "markdown", 69 | "metadata": {}, 70 | "source": [ 71 | "## `FactCheck.jl`" 72 | ] 73 | }, 74 | { 75 | "cell_type": "markdown", 76 | "metadata": {}, 77 | "source": [ 78 | "One of the problems with `Base.Test` is that it stops as soon as the first failing test is found. It is often more useful to run the tests and have a record of which passed, or at least which failed. The `FactCheck.jl` package provides this:" 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": null, 84 | "metadata": { 85 | "collapsed": true 86 | }, 87 | "outputs": [], 88 | "source": [ 89 | "using FactCheck" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": null, 95 | "metadata": { 96 | "collapsed": false 97 | }, 98 | "outputs": [], 99 | "source": [ 100 | "facts(\"Tests of f\") do\n", 101 | " @fact f(3) => 9\n", 102 | "end" 103 | ] 104 | }, 105 | { 106 | "cell_type": "markdown", 107 | "metadata": {}, 108 | "source": [ 109 | "[This uses the `do...end` syntax, which passes the code between `do` and `end` as an anonymous function as the first argument of the function `facts` (in this case).]\n", 110 | "\n", 111 | "[1] Add some more passing and failing tests both to the `Base.Test` and `FactCheck.jl` versions. What happens?\n", 112 | "\n", 113 | "[2] Write tests using `FactCheck` for `Vector2D` and `AutoDiff`. You can also use `context(\"...\") do...end` for finer control over the output." 114 | ] 115 | }, 116 | { 117 | "cell_type": "markdown", 118 | "metadata": {}, 119 | "source": [ 120 | "The tests of an installed package may be run using `Pkg.test(\"Distributions\")`." 121 | ] 122 | }, 123 | { 124 | "cell_type": "markdown", 125 | "metadata": {}, 126 | "source": [ 127 | "# Exception handling " 128 | ] 129 | }, 130 | { 131 | "cell_type": "markdown", 132 | "metadata": {}, 133 | "source": [ 134 | "Exceptions occur when an \"unusual\" condition occurs in your code, for example:" 135 | ] 136 | }, 137 | { 138 | "cell_type": "code", 139 | "execution_count": 1, 140 | "metadata": { 141 | "collapsed": false 142 | }, 143 | "outputs": [ 144 | { 145 | "ename": "LoadError", 146 | "evalue": "DomainError\nwhile loading In[1], in expression starting on line 1", 147 | "output_type": "error", 148 | "traceback": [ 149 | "DomainError\nwhile loading In[1], in expression starting on line 1", 150 | "", 151 | " in sqrt at math.jl:133" 152 | ] 153 | } 154 | ], 155 | "source": [ 156 | "sqrt(-10)" 157 | ] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "execution_count": 2, 162 | "metadata": { 163 | "collapsed": false 164 | }, 165 | "outputs": [ 166 | { 167 | "ename": "LoadError", 168 | "evalue": "InexactError()\nwhile loading In[2], in expression starting on line 2", 169 | "output_type": "error", 170 | "traceback": [ 171 | "InexactError()\nwhile loading In[2], in expression starting on line 2", 172 | "", 173 | " in setindex! at array.jl:307" 174 | ] 175 | } 176 | ], 177 | "source": [ 178 | "x = [3, 4]\n", 179 | "x[1] = 3.5" 180 | ] 181 | }, 182 | { 183 | "cell_type": "markdown", 184 | "metadata": {}, 185 | "source": [ 186 | "[1] Find the \"parent\" type of these two types using the `super` function." 187 | ] 188 | }, 189 | { 190 | "cell_type": "markdown", 191 | "metadata": {}, 192 | "source": [ 193 | "We can tell when exceptions have occured using `try...catch`:\n", 194 | "\n", 195 | " try \n", 196 | " \n", 197 | " \n", 198 | " \n", 199 | " catch \n", 200 | " \n", 201 | " \n", 202 | " \n", 203 | " end\n", 204 | " \n", 205 | " \n", 206 | "We can also use `throw()` to generate an exception." 207 | ] 208 | }, 209 | { 210 | "cell_type": "markdown", 211 | "metadata": {}, 212 | "source": [ 213 | "[1] Make a version of `sqrt` that works for positive and negative real arguments. [NB: The result will *not* be type stable.]\n", 214 | "\n", 215 | "[2] Define an interval type that represents an interval of real numbers. Define a version of square root that accepts an interval and returns a new interval that represents the possible square roots of all the values in the interval, if all the numbers in the interval are positive; it throws one type of exception if the argument intersects the positive numbers, and it throws a different exception if the interval is completely negative. [You may define new types of exception using `type MyException <: Exception end`.]" 216 | ] 217 | } 218 | ], 219 | "metadata": { 220 | "kernelspec": { 221 | "display_name": "Julia 0.3.9-pre", 222 | "language": "julia", 223 | "name": "julia-0.3" 224 | }, 225 | "language_info": { 226 | "name": "julia", 227 | "version": "0.3.10" 228 | } 229 | }, 230 | "nbformat": 4, 231 | "nbformat_minor": 0 232 | } 233 | -------------------------------------------------------------------------------- /outline.md: -------------------------------------------------------------------------------- 1 | 2 | # Hands-on Julia for scientific computing 3 | ## A 2-day workshop 4 | 5 | ## [Dr. David P. Sanders](http://sistemas.fciencias.unam.mx/~dsanders) 6 | 7 | ### Departament of Physics, Faculty of Sciences, Universidad Nacional Autónoma de México ([UNAM](www.unam.mx)) 8 | 9 | This will be a tutorial workshop on the [Julia](www.julialang.org) language, consisting mainly of hands-on exercises, designed to take participants who are experienced scientific programmers, but who have no knowledge of Julia, to a stage where they are beginning to feel comfortable with Julia and are able to write fairly idiomatic Julia code. 10 | 11 | The emphasis will be on those features that *differ* from other well-known languages used for scientific computing (e.g. Fortran, C, C++, Python, R, and MATLAB). 12 | 13 | The current stable version of Julia is the 0.3 line (0.3.9 is the current latest release). 14 | There are several important changes in the current development branch 0.4 (master), which will be in the process of being finalised when the workshop takes place; we will try to highlight the differences. 15 | 16 | ## Day 1 (16 June 2015) 17 | 18 | ###Introduction 19 | 20 | - Why should I learn Julia? 21 | - How can I use Julia? 22 | - REPL (terminal), IJulia (notebook), Juno (IDE), editors 23 | - JuliaBox (and SageMathCloud?) 24 | 25 | ### Basic Julia 26 | - Numbers and types 27 | - Vectors, matrices and arrays 28 | - Conditionals and loops 29 | - Some curious syntax features 30 | 31 | ### Plotting 32 | - Basic plotting (PyPlot and Gadfly) 33 | - Interactive widgets in IJulia 34 | 35 | ### Functions 36 | - To type or not to type 37 | - How functions work in Julia: methods and multiple dispatch 38 | - Why this is at the core of the Julia experience 39 | - `@code_typed`, etc. 40 | 41 | 42 | ## Day 2: Intermediate Julia 43 | 44 | ### Composite types 45 | - Julia's take on object-oriented programming 46 | - Conversion and promotion 47 | - Multiple dispatch again 48 | - Parametrized types 49 | 50 | ### Structuring Julia code 51 | - Scripts and command-line arguments 52 | - Modules 53 | - `include`, `import` and `using` 54 | - Documentation 55 | 56 | ### Testing and exception handling 57 | - Testing 58 | - Exception handling 59 | 60 | ### Towards good (=C) performance 61 | - No globals, please 62 | - Type stability 63 | - Profiling 64 | - How to check allocation 65 | 66 | ### Metaprogramming 67 | - Code that writes code 68 | - Macros 69 | 70 | ### Developing a Julia package and contributing to Julia 71 | --------------------------------------------------------------------------------