Julia
Table of Contents
High-Level, Speed of C, Dynamism of Ruby, General Purpose Skills of Python, Meta Programming of Lisp, String Processing Power of Pearl, Linear Algebra Support of Matlab, Statistics Support of R; in a massively parallel situations.
1. Syntax
- Rank Polymorphism is available.
;Equivalent to the newline.
1.1. Definition
1.1.1. Type Hierarchy
NumberRealFloatIntegerIntInt8Int16Int32Int64
BigInt
1.1.2. Variables
x::Integer = 1x::Char = 'a'x::String = "hello!"- String interpolation with
"...$var..."or"...$(myfunc())..." - Regular Expression Literal
r"..."- Postfix with flags
i,m,sandxto modify the behavior.
- Postfix with flags
- Byte Array Literal
b"..." - Version Number Literal
v"..."- It allows comparison between version numbers.
- Raw String Literal
raw"..."- Quotation marks still must be escaped.
- Python Literal
py"..."
- String interpolation with
x::Vector = [1, 2, 3]x::Matrix = [1 2; 3 4]- Array Interpolation is possible with for loop within the brackets.
x::Rational = 1//2x::Complex = 1 + 2im
Subtype can be defined with
<:: Subtype Of
1.1.3. Function
function my_function(x::Number, y::Number)
x+y
end
# or
function my_func(x::Number, y::Number)::Number; x+y; end
# or
f(x::Number, y::Number)::Number = x + y
Anonymous Function with
(x, y) -> x+y
Composition ∘ and Pipe |> is available.
1.1.3.1. Multiple Dispatch
Function can have multiple methods via multiple dispatch based on the argument types.
1.1.3.2. Parametric Method
Generic type can be used:
myappend(v::Vector{T}, x::T) where {T<:Number} ...
1.1.3.3. Mutating Function
Function ending with ! means that it might mutate the
arguments.
1.1.3.4. Broadcasting
. after the function name (or before for binary functions) vectorizes the
function—broadcast it.
For example, f.(vector) is equivalent to broadcast(f, vector).
broadcast is a generalization of map.
It expands the unary dimensions—dimension of length 1—to
match the size.
.= is special. X .= sin.(Y) is equivalent to
broadcast!(f, X, Y). Note that X needs to be pre-allocated,
which can be done by X = similar(Y).
@. in the beginning of a statement vectorizes every functions
within it.
1.2. Operation
a + b + cis the shorthand for+(a, b, c).^: Exponentiation- Juxtaposition of a number and a variable is multiplication.
÷: Integer Division%: Remainder
1.3. Control Flow
1.3.1. Ternary Operation
... ? ... : ...
1.3.2. If Statement
if x > n; ... ;elseif x == n; ...; else; ...; end
1.3.3. Short-Circuiting
... && ... and ... || ...
Note that with a single & or |, which is also the binary
operation, the second expression will be evaluated regardless of
the value of the first expression.
1.3.4. While Loop
while x < n; ...; end
1.3.5. For Loop
for i = 1:n; ...; endfor i in 1:n; ...; end,for i ∈ 1:n; ...; endfor i = 1:n, j=1:n; ...; end
1.4. Struct
struct MyStruct{T<:Real} x::T y::T end
1.4.1. Constructor
- Inner constructor method:
struct MyStruct ... MyStruct(x, y) = ... end - Outer constructor method:
MyStruct(x, y, z) = ... MyStruct(x, y) ...
1.5. Symbol
- Starts with
:.
1.6. Macro
- Starts with
@. @benchmark: Benchmark the function after it.@show: Show the value of the expression after it.@which: Similar to the$whichin the shell.
1.7. Command
- Enclosed by
`which can be piped, or executed withrun,read,write. - String interpolation also works.
2. Unicode Input
- Use the TeX notations.
- The comprehensive list of available symbols
3. Packages
3.1. LinearAlgebra
lu::Matrix -> LU: The LU Decompositionqr::Matrix -> QRCompact: The QR decompositioneigen::Matrix -> Eigen: The Spectral Decomposition
3.1.1. Special Matrix Structures
- Julia defines special
strutfor the special matrices to save memory and computation power. Diagonal,Triangular,Symmetric,Hermitian, …
3.2. Plots
- For plotting
plot,scatter,histogram,surface,plot3d
3.3. Symbolics
@variables xto define symbols.
4. REPL
]: Enter Package Environment?: Enter Help Environment;: Enter Shell Environment