{ "metadata": { "language": "Julia", "name": "", "signature": "sha256:103e429db35f1dea82a8c4a06fa2a6c5f5da4822a14c5681668a7b0da9962af1" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Julia Basics" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Julia is a dynamic language. You don't need type declarations, and can change variable types dynamically and interactively.\n", "\n", "For working with simple numbers, arrays, and strings, its syntax is *superficially* similar to Matlab, Python, and other popular languages. " ] }, { "cell_type": "code", "collapsed": false, "input": [ "A = rand(10,300)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 1, "text": [ "10x300 Array{Float64,2}:\n", " 0.375636 0.482139 0.361992 0.257166 \u2026 0.754869 0.120599 0.893813\n", " 0.0686832 0.671072 0.999855 0.286798 0.425177 0.382689 0.920532\n", " 0.447454 0.0964471 0.311746 0.606187 0.941434 0.125957 0.625727\n", " 0.730125 0.227747 0.577157 0.91215 0.194293 0.209349 0.196251\n", " 0.323599 0.625352 0.834394 0.662423 0.48925 0.955718 0.510419\n", " 0.0350443 0.926088 0.385119 0.398782 \u2026 0.715289 0.549668 0.486071\n", " 0.980887 0.427284 0.445409 0.352723 0.482097 0.747813 0.143269\n", " 0.0323402 0.766844 0.637901 0.136801 0.778503 0.5898 0.557505\n", " 0.962389 0.788106 0.258881 0.18815 0.693301 0.754942 0.42183 \n", " 0.830188 0.917995 0.822675 0.96834 0.253111 0.0893574 0.638984" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "It has all of the usual built-in Matlab/Numpy-like linear-algebra and vector functions:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "b = rand(10)\n", "x = A \\ b\n", "B = A' * A\n", "erf(eigvals(B)) - 2x.^2 + 4x - 6" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 2, "text": [ "300-element Array{Float64,1}:\n", " -5.99331\n", " -5.96398\n", " -5.96022\n", " -5.96506\n", " -5.98213\n", " -5.99655\n", " -5.98238\n", " -5.98213\n", " -5.93154\n", " -5.97005\n", " -5.97269\n", " -5.97308\n", " -5.9354 \n", " \u22ee \n", " -5.96845\n", " -5.99352\n", " -4.99281\n", " -4.95339\n", " -4.94527\n", " -4.9767 \n", " -5.00616\n", " -5.01051\n", " -5.01131\n", " -5.04615\n", " -5.03316\n", " -5.00371" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Complex numbers and arbitrary-precision arithmetic (via MPFR), of course." ] }, { "cell_type": "code", "collapsed": false, "input": [ "cos(big(3 + 4im))" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 4, "text": [ "-2.703494560307422464769480266827091348467753695567661661019265514673434246483996e+01 with 256 bits of precision - 3.851153334811777536563337123053124569704160846091637003157728595256494186490506e+00 with 256 bits of precisionim" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Unicode" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "All strings are UTF-8 encoded Unicode by default (UTF-16 and UTF-32 also supported):" ] }, { "cell_type": "code", "collapsed": false, "input": [ "matchall(r\"\\s[a-z]+\", \"\u03b1 is a Gr\u00eb\u00e9k letter\") # regex search of a Unicode string" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 5, "text": [ "3-element Array{SubString{UTF8String},1}:\n", " \" is\" \n", " \" a\" \n", " \" letter\"" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Like Python 3, variable names can be Unicode, but Julia allows a somewhat wider range of codepoints in identifiers, which can be typed by LaTeX-like tab-completion *\\alpha[TAB]\\hat[TAB]\\_2[TAB]\\prime*." ] }, { "cell_type": "code", "collapsed": false, "input": [ "\u03b1\u0302\u2082\u2032 = 7\n", "\u0127 = 6.62606957e-34 / 2\u03c0\n", "x\u0307 = \u0127 * \u03b1\u0302\u2082\u2032" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 6, "text": [ "7.3820020773540256e-34" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Unlike Python 3, Unicode math operators are parsed as infix operators, which are available for user-defined meanings:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\u226a(x,y) = x < 0.1*y\n", "50 \u226a 100, 5 \u226a 100, 5 \u2264 50" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 8, "text": [ "(false,true,true)" ] } ], "prompt_number": 8 }, { "cell_type": "code", "collapsed": false, "input": [ "const \u2297 = kron\n", "eye(2,2) \u2297 rand(2,2)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 9, "text": [ "4x4 Array{Float64,2}:\n", " 0.556894 0.861108 0.0 0.0 \n", " 0.344389 0.960697 0.0 0.0 \n", " 0.0 0.0 0.556894 0.861108\n", " 0.0 0.0 0.344389 0.960697" ] } ], "prompt_number": 9 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Functions and JIT-compilation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Functions can be defined in several ways, and *don't require type-declarations*." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# verbose form:\n", "function foo(x)\n", " return x + 1\n", "end\n", "\n", "# one-line form:\n", "foo(x) = x + 1\n", "\n", "# anonymous function\n", "x -> x + 1" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 10, "text": [ "(anonymous function)" ] } ], "prompt_number": 10 }, { "cell_type": "markdown", "metadata": {}, "source": [ "The *first time you call* a function with arguments of a particular type, Julia JIT-compiles that function *specialized for that type* with LLVM, which is then cached for subsequent calls for the *same types*." ] }, { "cell_type": "code", "collapsed": false, "input": [ "foo(3) # compiles foo for Int arguments" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 11, "text": [ "4" ] } ], "prompt_number": 11 }, { "cell_type": "code", "collapsed": false, "input": [ "foo(7) # re-uses compiled foo(Int)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 12, "text": [ "8" ] } ], "prompt_number": 12 }, { "cell_type": "code", "collapsed": false, "input": [ "foo(7.3) # compiles a different version for Float64 arguments" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 13, "text": [ "8.3" ] } ], "prompt_number": 13 }, { "cell_type": "code", "collapsed": false, "input": [ "foo([1,2,7,9]) # compiles a different version for Array{Int,1} arguments" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 14, "text": [ "4-element Array{Int64,1}:\n", " 2\n", " 3\n", " 8\n", " 10" ] } ], "prompt_number": 14 }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can *inspect* the generated code easily at various compilation stages, to see that Julia **uses the type of the arguments to infer the types of expressions** and **can produce C-like fast code**." ] }, { "cell_type": "code", "collapsed": false, "input": [ "code_typed(foo, (Int,)) # julia expressions with type annotations" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 15, "text": [ "1-element Array{Any,1}:\n", " :($(Expr(:lambda, {:x}, {{},{{:x,Int64,0}},{}}, :(begin # In[10], line 7:\n", " return (top(box))(Int64,(top(add_int))(x::Int64,1))::Int64\n", " end::Int64))))" ] } ], "prompt_number": 15 }, { "cell_type": "code", "collapsed": false, "input": [ "code_native(foo, (Int,)) # assembly code" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\t.section\t__TEXT,__text,regular,pure_instructions\n", "Filename: In[10]\n", "Source line: 7\n", "\tpush\tRBP\n", "\tmov\tRBP, RSP\n", "Source line: 7\n", "\tlea\tRAX, QWORD PTR [RDI + 1]\n", "\tpop\tRBP\n", "\tret\n" ] } ], "prompt_number": 16 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }