This assumes
- Julia v1.0 or above
using LinearAlgebra, Statistics, Compat
has been run- The
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
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 |
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 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 = range(start, stop,
length = l)
A = range(start, stop,
step = s) |
Creates a StepRangeLen iterable starting at start and ending at stop . Can be specified
using either the length or step size (will not overshoot). |
A = Diagonal(x) |
\begin{pmatrix} 1 & \cdot & \cdot\\ \cdot & 2 & \cdot \\ \cdot & \cdot & 3 \end{pmatrix} |
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) |
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
(i.e., A[1, 1], A[2, 2] , etc...). |
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{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 columns. |
A = reshape(a, m, n) |
Reshapes matrix or vector \begin{pmatrix} 1 & 6 \\ 2 & 7 \\ 3 & 8 \\ 4 & 9 \\ 5 & 10 \end{pmatrix} For this to work, the number of elements in |
A[:] |
Converts matrix A to a vector. For example, if \begin{pmatrix} 1 \\ 2 \\ 3 \\ 4 \end{pmatrix} |
reverse(A, dims = d) |
\begin{pmatrix} 4 & 5 & 6 \\ 1 & 2 & 3 \end{pmatrix}
\begin{pmatrix} 3 & 2 & 1 \\ 6 & 5 & 4 \end{pmatrix} |
repeat(A, m, n) |
Repeats matrix \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} |
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 |
Element-by-element operations on matrices. This syntax applies the operation element-wise to corresponding elements of the matrices. More generally, the |
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)
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 A = \begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix} then \begin{pmatrix} 1 & 3 \\ 2 & 4 \end{pmatrix} If A = \begin{pmatrix} 1-1i & 2+1i \\ 3-2i & 4+2i \end{pmatrix} then \begin{pmatrix} 1-1i & 3-2i \\ 2+1i & 4+2i \end{pmatrix} 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 A = \begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix} then \begin{pmatrix} 1 & 3 \\ 2 & 4 \end{pmatrix} which is exactly the transpose. If A = \begin{pmatrix} 1-1i & 2+1i \\ 3-2i & 4+2i \end{pmatrix} then \begin{pmatrix} 1+1i & 3+2i \\ 2-1i & 4-2i \end{pmatrix} |
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, dims = 2) will compute the row sums of A and
maximum(A, dims =1) will compute the maxima of eachcolumn of A . |
inv(A) |
This function returns the inverse of the matrix A ^ (-1) |
det(A) |
This function returns the determinant of the matrix A . |
val, vec = eigen(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 norm(A, p) 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 least-squares solution
to the problem. |
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 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 foo . |
println("Hello world") |
Print to screen. We can also print the values of variables to screen: println("The value of x is $(x).") |