Julia cheatsheet¶
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. A is a scalar, B is a flat array with 3 elements, C is a 1 by 3 vector, D is a 3 by
1 vector, and E is a 2 by 2 matrix. 
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)

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

which will create a matrix of all ones with the same dimensions as matrix or vector 
A = eye(n)


A = j:k:n

This will create a sequence starting at j , ending at n , with difference
k between points. For example, A = 2:4:10 will create the sequence 2, 6, 10
To convert the output to an array, use collect(A) . 
A = linspace(j, n, m)

This will create a sequence of m points starting at j , ending at n . For example,
A = linspace(2, 10, 3) will create the sequence 2.0, 6.0, 10.0 . To convert the output to an
array, use collect(A) . 
A = diagm(x)


A = rand(m, n)

Creates an m by n matrix of random numbers drawn from a uniform distribution on
\([0, 1]\). Alternatively, rand can be used to draw random elements from a set X . For
example, if X = [1, 2, 3] , rand(X) will return either 1 , 2 , or 3 . 
A = randn(m, n)

Creates an m by n matrix of random numbers drawn from a standard normal distribution. 
A[m, n]

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

nrow, ncol = size(A)


diag(A)

This function returns a vector of the diagonal elements of A
(i.e., A[1, 1], A[2, 2] , etc…). 
A = hcat([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])

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
For this to work, the number of elements in 
A[:]

Converts matrix A to a vector. For example, if

flipdim(A, d)


repmat(A, m, n)

Repeats matrix

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. 
A * B

When A and B are matrices, * will perform matrix multiplication, as long as the number
of columns in A is the same as the number of columns in B . 
dot(A, B)

This function returns the dot product/inner product of the two vectors A and B . The two
vectors need to be dimensionless or column vectors. 
A.'

This syntax returns the transpose of the matrix

A'

This syntax returns the complex conjugate transpose of the matrix

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

These functions compute the sum, maximum, and minimum elements, respectively, in matrix or vector
A . We can also add an additional argument for the dimension to compute the sum/maximum/minumum
across. For example sum(A, 2) will compute the row sums of A and maximum(A, 1) will compute
the maxima of eachcolumn of A . 
inv(A)


det(A)

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

Returns the eigenvalues (val ) and eigenvectors (vec ) of matrix A . In the output,
val[i] is the eigenvalue corresponding to eigenvector val[:, i] . 
norm(A)

Returns the Euclidean norm of matrix or vector
which will compute the 
A \ b

If A is square, this syntax solves the linear system \(Ax = b\). Therefore, it returns
x such that A * x = b . If A is rectangular, it solves for the leastsquares solution
to the problem. 
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 #= and =# . 
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

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

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
We can also just have an if statement on its own, in which case it would square

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, 
println("Hello world")

How to print to screen. We can also print the values of variables to screen:
