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]:
10x300 Array{Float64,2}:
 0.208508   0.728715   0.672505   …  0.112352   0.47847    0.364063
 0.542239   0.593282   0.208913      0.39994    0.893613   0.959817
 0.483305   0.50298    0.0480054     0.736535   0.0887915  0.658661
 0.866776   0.0415607  0.775155      0.518471   0.208801   0.696874
 0.251744   0.779817   0.488604      0.644497   0.349663   0.303017
 0.524304   0.568782   0.558393   …  0.701242   0.737875   0.329395
 0.0168825  0.297376   0.421494      0.937676   0.396563   0.985999
 0.740437   0.964427   0.0272315     0.561772   0.307406   0.303533
 0.200871   0.0427033  0.898042      0.417478   0.401182   0.604956
 0.050995   0.0773741  0.452127      0.0221649  0.526271   0.1555  

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


In [2]:
b = rand(10)
x = A \ b
B = A' * A
erf(eigvals(B)) - 2x.^2 + 4x - 6


Out[2]:
300-element Array{Float64,1}:
 -6.01286
 -5.97168
 -6.0151 
 -5.91049
 -5.90693
 -6.0145 
 -6.04953
 -6.07324
 -6.01977
 -6.00471
 -5.93151
 -6.04943
 -5.9462 
  ⋮      
 -5.97585
 -6.03065
 -5.04475
 -4.9739 
 -5.00375
 -5.04576
 -5.00844
 -5.01893
 -4.99459
 -4.90959
 -5.03554
 -4.95436

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


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


Out[3]:
-2.703494560307422464769480266827091348467753695567661661019265514673434246483996e+01 with 256 bits of precision - 3.851153334811777536563337123053124569704160846091637003157728595256494186490506e+00 with 256 bits of precisionim

Unicode

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


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


Out[4]:
3-element Array{SubString{UTF8String},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 [5]:
α̂₂′ = 7
ħ = 6.62606957e-34 / 2π
 = ħ * α̂₂′


Out[5]:
7.3820020773540256e-34

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


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


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

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


Out[7]:
4x4 Array{Float64,2}:
 0.734652  0.128968   0.0       0.0      
 0.405652  0.0699451  0.0       0.0      
 0.0       0.0        0.734652  0.128968 
 0.0       0.0        0.405652  0.0699451

Functions and JIT-compilation

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


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

# one-line form:
foo(x) = x + 1

# anonymous function
x -> x + 1


Out[8]:
(anonymous function)

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.


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


Out[9]:
4

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


Out[10]:
8

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


Out[11]:
8.3

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


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