01 Julia Introduction for EECS 551

2018-08-11 Jeff Fessler (based on 2017 version by David Hong)
Julia 0.7.0
2019-01-20 Julia 1.0.3 and add note about line breaks
2020-08-05 Julia 1.5.0 2021-08-23 Julia 1.6.2

Numbers and arithmetic (and thinking about types!)

Make a real number!

Variables in Julia have a type.

We can add, subtract, multiply and divide like usual.

Dividing Int values with \ produces a Float:

This is different from Python 2, but similar to Python 3.

To divide integers with rounding, use ÷ instead. Type \div then hit tab.

More info about numbers here:

Vector and matrices (i.e., arrays)

Make a vector of real numbers:

$x = \begin{bmatrix} 1.0 \\ 3.5 \\ 2 \end{bmatrix}$

Note the type: Array{Float64,1}.
Having just one real number in the array sufficed for the array have all Float64 elements.

This is a true one-dimensional array of Float64's.
(Matlab does not have 1D arrays; it fakes it using 2D arrays of size N × 1.)

This is a one-dimensional array of Int64's. We'll use these less often in 551.

Make a matrix using a semicolon to separate rows:

$ A = \begin{bmatrix} 1.1 & 1.2 & 1.3 \\ 2.1 & 2.2 & 2.3 \end{bmatrix}$

This is a two-dimensional array (aka a matrix) of Float64 values.

Different from Matlab! length always returns the total number of elements.

Make vectors and matrices of all zeros.

Different from Matlab! Do not write zeros(3,1) because Julia has proper 1D arrays.
zeros(3,1) and zeros(3) are different!

And ones:

The "identity matrix" I in Julia's LinearAlgebra package is sophisticated.
Look at the following examples:

If that I seems too fancy, then you could make your own "eye" command akin to Matlab as follows (but it should not be needed and it uses unnecessary memory):

Make diagonal matrices using the Diagonal function in LinearAlgebra

This is far more memory efficient than Matlab's diag command or Julia's LinearAlgebra.diagm method. Avoid using those!

Make random vectors and matrices.

$ x = \begin{bmatrix} \mathcal{N}(0,1) \\ \mathcal{N}(0,1) \\ \mathcal{N}(0,1) \end{bmatrix} \qquad \text{i.e.,} \quad x_i \overset{\text{iid}}{\sim} \mathcal{N}(0,1)$

$ A = \begin{bmatrix} \mathcal{N}(0,1) & \mathcal{N}(0,1) & \mathcal{N}(0,1) \\ \mathcal{N}(0,1) & \mathcal{N}(0,1) & \mathcal{N}(0,1) \end{bmatrix} \qquad \text{i.e., } \quad A_{ij} \overset{\text{iid}}{\sim} \mathcal{N}(0,1)$

Matrix operations

Indexing is done with square brackets and begins at 1 (like in Matlab and counting) not 0 (like in C or Python).

This row-slice is a one-dimensional slice! Not a 1×2 matrix.

Vector dot product

Different from Matlab! The output is a scalar, not a 1×1 "matrix."

Matrix times vector

Matrix times matrix

Matrix transpose (conjugate and non-conjugate)

Matrix determinant

Matrix trace

More info here: https://docs.julialang.org/en/v1/manual/arrays

Important Aside: Getting help!

Julia analogue of Matlab's help is ?

Full documentation online: https://docs.julialang.org/en/stable/
Searching their Github repo can sometimes also uncover folks with similar issues: https://github.com/JuliaLang/julia

Also lots of neat talks on their Youtube Channel: https://www.youtube.com/user/JuliaLanguage
Especially encourage this very interesting one about vector transposes! https://www.youtube.com/watch?v=C2RO34b_oPM

Aside: Creating ranges

It's different from (and much more efficient than) Matlab!

Not an Array! But can be indexed.

Used often in for loops.

Form an array by using collect if needed (use rarely):

Other ways to make ranges.

Aside: Comprehensions

A convenient way to create arrays!

Defining functions

Way 1

Way 2

Way 3: Anonymous function

Can return multiple outputs

Output is a Tuple of the outputs.

Convenient way to split out the outputs.

Any function can be "vectorized" using "broadcast" capability.

This particular function was not designed to be applied to vector input arguments!
But it can be used with vectors by adding a "." to tell Julia to apply it element-wise. This is called a "broadcast"

More info here: https://docs.julialang.org/en/v1/manual/functions

Conditionals: If, Else, End, For

Generally similar to Matlab. Optional use of in instead of = in the for loop.

Julia has the convenient ternary operator.

Plotting

Suggested package: Plots.jl with its default gr backend.

Note: Usually slower the first time you plot due to precompiling.
You must add the "Plots" package first. In a regular Julia REPL you do this by using the ] key to enter the package manager REPL, and then type add Plots then wait.
In a Jupyter notebook, type using Pkg then add Plots and wait.

Plot values from a vector. (The labels are optional arguments.)

Revisiting the heatmap

cf example in n-01-matrix notes

Using the jim function the MIRTjim.jl package simplifies the display of 2D images, among other features. See: https://jefffessler.github.io/MIRTjim.jl/stable/examples/1-examples/

A convenient way to plot functions

Plots.jl allows you to pass in the domain and a function. It does the rest. :)
This is one many examples of how Julia exploits "multiple dispatch."

More info about plotting here: https://juliaplots.github.io

Caution about line breaks (newlines)

If you want an expression to span multiple lines, then be sure to enclose it in parentheses.

Submitting homework

A quick example to try submitting problems.

Task: Implement a function that takes two inputs and outputs them in reverse order.

Copy the above function code into a file named template1.jl and email to eecs551@autograder.eecs.umich.edu.

Make sure that:

An undocumented function is bad programming practice.
Julia supports docstrings for comments like this:

You can see the docstring by using the ? key: