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]:
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]:
It also supports convenient vectorisation of functions using the . operator:
In [3]:
erf.(eigvals(B)) - 2x.^2 + 4x - 6
Out[3]:
Complex numbers and arbitrary-precision arithmetic (via MPFR) are available, of course.
In [4]:
cos(big(3 + 4im))
Out[4]:
In [5]:
matchall(r"\s[a-z]+", "α is a Grëék letter") # regex search of a Unicode string
Out[5]:
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]:
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]:
In [8]:
const ⊗ = kron
eye(2,2) ⊗ rand(2,2)
Out[8]:
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]:
In [10]:
foo(3) # compiles foo for Int arguments
Out[10]:
In [11]:
foo(7) # re-uses compiled foo(Int)
Out[11]:
In [12]:
foo(7.3) # compiles a different version for Float64 arguments
Out[12]:
In [13]:
foo([1,2,7,9]) # compiles a different version for Array{Int,1} arguments
Out[13]: