Julia cheatsheet¶
Version and Dependencies¶
This assumes
Julia v1.0 or above
using LinearAlgebra, Statistics, Compat
has been runThe
while
andfor
are assumed to be within a Jupyter Notebook or within a function. Otherwise, Julia 1.0 has different scoping rules for global variables, which will be made more consistent in a future release
Variables¶
Here are a few examples of basic kinds of variables we might be interested in creating.
Command 
Description 

A = 4.1
B = [1, 2, 3]
C = [1.1 2.2 3.3]
D = [1 2 3]'
E = [1 2; 3 4]

How to create a scalar, a vector, or a matrix. Here, each example will result in a slightly different form
of output. 
s = "This is a string"

A string variable 
x = true

A Boolean variable 
Vectors and Matrices¶
These are a few kinds of special vectors/matrices we can create and some things we can do with them.
Command 
Description 

A = zeros(m, n)

Creates a matrix of all zeros of size A = zeros(B)
which will create a matrix of all zeros with the same dimensions as matrix or vector 
A = ones(m, n)

Creates a matrix of all ones of size A = ones(B)
which will create a matrix of all ones with the same dimensions as matrix or vector 
A = I

Creates a

A = j:k:n

This will create a sequence starting at 
A = range(start, stop,
length = l)
A = range(start, stop,
step = s)

Creates a 
A = Diagonal(x)

\[\begin{split}\begin{pmatrix}
1 & \cdot & \cdot\\
\cdot & 2 & \cdot \\
\cdot & \cdot & 3
\end{pmatrix}\end{split}\]

A = rand(m, n)

Creates an 
A = randn(m, n)

Creates an 
A[m, n]

This is the general syntax for accessing elements of an array or matrix, where

nrow, ncol = size(A)

Returns the number of rows and columns in a matrix. Alternatively, we can do nrow = size(A, 1)
and ncol = size(A, 2)

diag(A)

This function returns a vector of the diagonal elements of 
A = hcat([1 2], [3 4])

Horizontally concatenates two matrices or vectors. The example here would return
\[\begin{pmatrix}
1 & 2 & 3 & 4
\end{pmatrix}\]
An alternative syntax is: A = [[1 2] [3 4]]
For either of these commands to work, both matrices or vectors must have the same number of rows. 
A = vcat([1 2], [3 4])

Vertically concatenates two matrices or vectors. The example here would return
\[\begin{split}\begin{pmatrix}
1 & 2 \\
3 & 4
\end{pmatrix}\end{split}\]
An alternative syntax is: A = [[1 2]; [3 4]]
For either of these commands to work, both matrices or vectors must have the same number of columns. 
A = reshape(a, m, n)

Reshapes matrix or vector
\[\begin{split}\begin{pmatrix}
1 & 6 \\
2 & 7 \\
3 & 8 \\
4 & 9 \\
5 & 10
\end{pmatrix}\end{split}\]
For this to work, the number of elements in 
A[:]

Converts matrix A to a vector. For example, if
\[\begin{split}\begin{pmatrix}
1 \\
2 \\
3 \\
4
\end{pmatrix}\end{split}\]

reverse(A, dims = d)

\[\begin{split}\begin{pmatrix}
4 & 5 & 6 \\
1 & 2 & 3
\end{pmatrix}\end{split}\]
\[\begin{split}\begin{pmatrix}
3 & 2 & 1 \\
6 & 5 & 4
\end{pmatrix}\end{split}\]

repeat(A, m, n)

Repeats matrix
\[\begin{split}\begin{pmatrix}
1 & 2 & 1 & 2 & 1 & 2 \\
3 & 4 & 3 & 4 & 3 & 4 \\
1 & 2 & 1 & 2 & 1 & 2 \\
3 & 4 & 3 & 4 & 3 & 4
\end{pmatrix}\end{split}\]

Mathematical Functions¶
Here, we cover some useful functions for doing math.
Command 
Description 

5 + 2
5  2
5 * 2
5 / 2
5 ^ 2
5 % 2

Scalar arithmetic operations: addition, subtraction, multiplication, division, power, remainder. 
A .+ B
A . B
A .* B
A ./ B
A .^ B
A .% B

Elementbyelement operations on matrices. This syntax applies the operation elementwise to corresponding elements of the matrices. More generally, the 
A * B

When 
dot(A, B)
A ⋅ B

This function returns the dot product/inner product of the two vectors Can also be called with the unicode ⋅ ( 
transpose(A)

This syntax returns the transpose of the matrix For example if
\[\begin{split}A = \begin{pmatrix}
1 & 2 \\
3 & 4
\end{pmatrix}\end{split}\]
then
\[\begin{split}\begin{pmatrix}
1 & 3 \\
2 & 4
\end{pmatrix}\end{split}\]
If
\[\begin{split}A = \begin{pmatrix}
11i & 2+1i \\
32i & 4+2i
\end{pmatrix}\end{split}\]
then
\[\begin{split}\begin{pmatrix}
11i & 32i \\
2+1i & 4+2i
\end{pmatrix}\end{split}\]
The function is recursive, so it will also transpose all elements if possible. 
A'

This syntax returns the adjoint of the matrix For example if
\[\begin{split}A = \begin{pmatrix}
1 & 2 \\
3 & 4
\end{pmatrix}\end{split}\]
then
\[\begin{split}\begin{pmatrix}
1 & 3 \\
2 & 4
\end{pmatrix}\end{split}\]
which is exactly the transpose. If
\[\begin{split}A = \begin{pmatrix}
11i & 2+1i \\
32i & 4+2i
\end{pmatrix}\end{split}\]
then
\[\begin{split}\begin{pmatrix}
1+1i & 3+2i \\
21i & 42i
\end{pmatrix}\end{split}\]

sum(A)
maximum(A)
minimum(A)

These functions compute the sum, maximum, and minimum elements, respectively, in matrix or vector

inv(A)

This function returns the inverse of the matrix A ^ (1)

det(A)

This function returns the determinant of the matrix 
val, vec = eigen(A)

Returns the eigenvalues ( 
norm(A)

Returns the Euclidean norm of matrix or vector norm(A, p)
which will compute the 
A \ b

If 
Programming¶
The following are useful basics for Julia programming.
Command 
Description 

# One line comment
#=
Comment block
=#

Two ways to make comments. Comments are useful for annotating code and explaining what it does.
The first example limits your comment to one line and the second example allows the comments to span
multiple lines between the 
for i in iterable
# do something
end

A for loop is used to perform a sequence of commands for each element in an iterable object,
such as an array. For example, the following for loop fills the vector N = 3
l = zeros(N, 1)
for i = 1:N
l[i] = i ^ 2
end

while i <= N
# do something
end

A while loop performs a sequence of commands as long as some condition is true. For example, the following while loop achieves the same result as the for loop above l = [0]
while norm(l) < 5
push!(l, 2)
end

if i <= N
# do something
else
# do something else
end

An if/else statement performs commands if a condition is met. For example, the following squares
if x == 5
x = x ^ 2
else
x = x ^ 3
end
We can also just have an if statement on its own, in which case it would square if x == 5
x = x ^ 2
end

fun(x, y) = 5 * x + y
function fun(x, y)
ret = 5 * x
return ret + y
end

These are two ways to define functions. Both examples here define equivalent functions. The first method is for defining a function on one line. The name of the function is The second method is used to create functions of more than one line. The name of the function, 
foo = x > x + 3

Defines an anonymous function and binds it to the name 
println("Hello world")

Print to screen. We can also print the values of variables to screen: println("The value of x is $(x).")
