Julia (v1.0.3) Cheatsheet

typeset by Cheng Tan | Kyoto University | i@c-tan.com | https://c-tan.com | 2019-02-10
Linear Algebra

Basic Operations

Fundamental Mathematics

typeof(x)typeassert(x, type)varinfo()?
;pi
MathConstants.e
MathConstants.eulergamma
MathConstants.golden
Inf-InfNaN# 3.1415926...
# 2.7182818...
# 0.5772156...
# 1.6180339...
# ∞# −∞# not a number

2//3
2//3 + 4//5

# rational number# = 22/15

2.0 + 3.0im# complex number# detailed memory state# goes to 'help' mode# goes to 'shell' mode

Package Installation
using Pkg
Pkg.init()
Pkg.update()
Pkg.add("SomePackage")
Pkg.rm("SomePackage")
Pkg.status()]# install a new package# uninstall a package# summary of installed packages# goes to 'package' moded = Dict("alpha"=>1, "beta"=>2)
d["beta"]get(d, "gamma", 10)haskey(d, "gamma")keys(d)values(d)delete!(d, "alpha")

# a+b
# a−b
# a×b
# a÷bt = (1, 2, 3)
length(t)t[1:2]e, d = d, e

s = Set([1; 2; 3; 2; 1])
push!(s, 69)in(2, s)intersect(s, s1)
union(s, s1)
setdiff(s, s1)

Stringa == ba != ba > ba >= ba < ba <= ba < b > c-aa ^ b
a \ b
a % b

# −a
# ab
# 1/a × b
# a − ⌊a/b⌋ × b# a=b# a ̸= b
# a>b
# a≥b
# a<b
# a≤b
# a < b and b > cisequal(a, b)
isfinite(a)isinf(a)isnan(a)iszero(a)isone(a)isinteger(a)# a=b# |a| < ∞# a = ±∞
# a is not a number# a=0# a=1# a = [a]Complex numbers
complex(a, b) # a + ibreal(z)imag(z)angle(z)rad2deg(angle(z))

using Randomrandstring("ATCG", 200)

div(x, y)fld(x, y)cld(x, y)rem(x, y)mod(x, y)abs(z)
abs2(z)conj(z)cis(z)
sign(z)# ∥z∥# ∥z∥2# z̄# eiz# z/∥z∥# ternary expression
mod(5, 2) == 0 ? "AT" : "CG"# if-else
if rand(1:10) < 5
println("Good.")elseif rand(1:100) > 45
println("Well.")else
println("What?")end# while loopb = 100; i = 1;
while b > 10
b -= ii += 1end# for loops = 0
a = [1:2:20...]for (i, v) in enumerate(a)s += v
if s > 80
break
end
println("$i, $s")end
# try-catch
tryerror("whatever")catch eprintln("ERROR! $e $e")endsin(x)
csc(x)
cos(x)
sec(x)
tan(x)
cot(x)

zeros([T=Float64], dims...)ones([T=Float64], dims...)trues(dims...)falses(dims...)fill(x, dims...)rand([T=Float64], dims...)randn([T=Float64], dims...)reshape(A, dims...)repeat(A,inner=(1,3),outer=(3,1))
reverse(A, dims=1)

sort(a)p = sortperm(a); a[p]# x/y rounded → 0# ⌊x/y⌋# ⌈x/y⌉# x%y sing same as x
# x%y sing same as y

√
# √x
3
# √x
#x2 + y 2
# exp(x)# ex − 1asin(x)acsc(x)acos(x)asec(x)atan(x)acot(x)sind(x)cscd(x)cosd(x)secd(x)tand(x)cotd(x)divrem(x, y)
fldmod(x, y)
gcd(x, y)lcm(x, y)
mod2pi(x)# (div(x, y), rem(x, y))# (fld(x, y), mod(x, y))
# GCD
# LCM
# x%2πlog(x)
log1p(x)log(b, x)log2(x)log10(x)# ln(x)# ln(1 + x)
# logb (x)# log2 (x)# log10 (x)asind(x)acscd(x)acosd(x)asecd(x)atand(x)acotd(x)Functionssinh(x)csch(x)cosh(x)sech(x)tanh(x)coth(x)pop!(a, n)
popfirst!(a, n)
splice!(a, i)Sort
sort(a, by=abs, rev=true)sort(A, dims=1)

Searchn in ain(n, a)findmax(A)findmin(A)findmax(A, dims=1)
findmin(A, dims=2)# return indices !!!
findall(f::Function, A)
findfirst(f::Function, A)
findlast(f::Function, A)
findnext(f::Function, A, i)findprev(f::Function, A, i)

Matrix properties

Trigonometric and hyperbolic functions
Control Flow

# [1; 2; 3; 4]# [1; 3; 5; 7]# [0.1; 1.2; 2.3; 3.4]
# [0.1; 0.3; 0.5; 0.7]
# [1; -1; -3; -5]# [1; 2; 3; 4]# [1; 3; 5; 7]# [1; 4; 9; 16]# [2 4 8; 3 9 27]

push!(a, n)
pushfirst!(a, n)append!(a, b)Power and logarithmsqrt(x)cbrt(x)hypot(x, y)exp(x)
expm1(x)collect(1:4)
collect(1:2:8)
collect(0.1:3.5)
collect(0.1:0.2:0.8)
collect(1:-2:-5)[1:4...][1:2:8...][x^2 for x in a][x^y for x=2:3, y=1:3]

Modification

Number theory
cmp(s1, s2)
findfirst(r, s3)
occursin(s1, s3)
replace(s3, "hello" => "nihao")split(s3, "o_ ")strip(s3)startswith(s3, 'h')endswith(s3, "J!")# 1# 4# [1; 2; 3] !!!
# [1; 2; 3; 4] !!!
# [2; 3]# [2 3; 5 6; 8 9]
# [1:9...]Construction

Arithmetic operators
a + ba - b
a * b
a / b

# indexinga[1]b[end]
a[1:3]a[:]b[2:end-1]
A[:,2:3]A[:]

a = [1 2 3 4]
b = [1; 2; 3; 4]
A = [1 2 3; 4 5 6; 7 8 9]size(a)# (1, 4)size(b)# (4, )
ndims(a)# 2ndims(b)# 1length(a)# 4Comparison operators

Data Structures

s1 = "hello"; s2 = " world!";
s1 * s2# "hello world!"
s1 ^ 3# "hellohellohello"s3 = "$(s1)$(s2)"reverse(s3)rpad(s3, 100)lpad(s2, 100, "fuck ")first(s3, 5)
last(s3, 1)Some functions require using LinearAlgebra .
Basic information

asinh(x)acsch(x)acosh(x)asech(x)atanh(x)acoth(x)det(A)
rank(A)
tr(A)inv(A)
pinv(A)diag(M, k::Integer=0)
diagm(1 => [1,2,3], -1 => [4,5])
tril(A)triu(A)norm(A)

Matrix, vector operations
a' * bdot(x, y)cross(x, y)a .* b
A * a
A \ b

# dot product# dot product# cross productA'dot(A, B)
A / B
A \ BA^nexp(A)Special matrix# define a function
function fnname(a, b = 2; c = 10, d = -3)
result = (a + b) * (c + d)return resultend# define a function in one line
fnname1(x, y) = x * y# Anonymous Functions(x -> mod(x, 12)).([1:24...])

ishermitian(A)issymmetric(A)isposdef(A)Decomposition
F = qr(A)
F.Q
F.R# QR factorization

E = eigen(A)E.values
E.vectors# transpose