Julia Basics

Julia is a dynamic language. You don't need type declarations, and can change variable types dynamically and interactively.

For working with simple numbers, arrays, and strings, its syntax is superficially similar to Matlab, Python, and other popular languages.

In order to execute the "In" cells, select the cell and press Shift-Enter, or press the Play button above. To run the entire notebook, navigate to Cell and then click Run All.


In [1]:
A = rand(10,300)


Out[1]:
10×300 Array{Float64,2}:
 0.601102   0.545478  0.906272  0.398077  …  0.659722  0.0193259  0.493484 
 0.0716165  0.500614  0.406478  0.851484     0.919554  0.206508   0.239634 
 0.670625   0.469405  0.871157  0.439927     0.36548   0.361499   0.296581 
 0.843612   0.744969  0.20099   0.363494     0.609047  0.373218   0.0133326
 0.677346   0.714646  0.592945  0.572062     0.201304  0.0983049  0.333854 
 0.529378   0.98971   0.740716  0.445254  …  0.562897  0.528341   0.390515 
 0.429954   0.197207  0.555682  0.36379      0.369002  0.230761   0.147777 
 0.305452   0.148601  0.721603  0.212736     0.457648  0.020073   0.783245 
 0.854558   0.131547  0.433203  0.588435     0.723114  0.901864   0.226535 
 0.06325    0.803508  0.889544  0.413003     0.705337  0.886079   0.178953 

It has all of the usual built-in Matlab/Numpy-like linear-algebra and vector functions:


In [2]:
b = rand(10) # a random rank-10 vector
x = A \ b    # solve for x satisfying A*x = b; has rank 300
B = A' * A   # A-transpose multiplied by A; is a 300x300 symmetrix matrix with real eigenvalues
eigvals(B)


Out[2]:
300-element Array{Float64,1}:
  -4.16451e-13
  -3.70282e-13
  -2.85379e-13
  -2.25732e-13
  -1.92911e-13
  -1.60442e-13
  -1.38125e-13
  -1.02445e-13
  -9.05251e-14
  -7.68042e-14
  -5.05736e-14
  -4.42838e-14
  -4.23436e-14
   ⋮          
   3.32291e-13
   3.89741e-13
  17.1969     
  19.7795     
  20.6154     
  22.868      
  24.1414     
  26.2514     
  29.3138     
  30.9734     
  32.2518     
 793.245      

It also supports convenient vectorisation of functions using the . operator:


In [3]:
erf.(eigvals(B)) - 2x.^2 + 4x - 6


Out[3]:
300-element Array{Float64,1}:
 -6.02299
 -5.93161
 -5.96133
 -6.0167 
 -5.96599
 -6.05746
 -6.12523
 -5.97322
 -5.99125
 -5.96492
 -6.00245
 -5.95491
 -5.99214
  ⋮      
 -6.03991
 -5.9894 
 -5.04053
 -4.998  
 -5.02277
 -5.06534
 -4.96746
 -4.96039
 -4.98431
 -4.97944
 -5.02127
 -4.95307

Complex numbers and arbitrary-precision arithmetic (via MPFR) are available, of course.


In [4]:
cos(big(3 + 4im))


Out[4]:
-2.703494560307422464769480266827091348467753695567661661019265514673434246483996e+01 - 3.851153334811777536563337123053124569704160846091637003157728595256494186490506im

Unicode

All strings are UTF-8 encoded Unicode by default (UTF-16 and UTF-32 also supported):


In [5]:
matchall(r"\s[a-z]+", "α is a Grëék letter") # regex search of a Unicode string


Out[5]:
3-element Array{SubString{String},1}:
 " is"    
 " a"     
 " letter"

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.


In [6]:
α̂₂′ = 7
ħ = 6.62606957e-34 / 2π
 = ħ * α̂₂′


Out[6]:
7.3820020773540256e-34

Unlike Python 3, Unicode math operators are parsed as infix operators, which are available for user-defined meanings:


In [7]:
(x,y) = x < 0.1*y
50  100, 5  100, 5  50


Out[7]:
(false,true,true)

In [8]:
const  = kron
eye(2,2)  rand(2,2)


Out[8]:
4×4 Array{Float64,2}:
 0.952167   0.253814  0.0        0.0     
 0.0724192  0.902237  0.0        0.0     
 0.0        0.0       0.952167   0.253814
 0.0        0.0       0.0724192  0.902237

Functions and JIT-compilation

Functions can be defined in several ways, and don't require type-declarations.


In [9]:
# verbose form:
function foo(x)
    return x + 1
end

# one-line form:
bar(x) = x + 2

# anonymous function
x -> x + 3


Out[9]:
(::#1) (generic function with 1 method)

In [10]:
foo(3) # compiles foo for Int arguments


Out[10]:
4

In [11]:
foo(7) # re-uses compiled foo(Int)


Out[11]:
8

In [12]:
foo(7.3) # compiles a different version for Float64 arguments


Out[12]:
8.3

In [13]:
foo([1,2,7,9]) # compiles a different version for Array{Int,1} arguments


Out[13]:
4-element Array{Int64,1}:
  2
  3
  8
 10