Julia cheatsheet#

Ways to work in Julia#

REPL (Read-Eval-Print Loop)#

This is the default way to start and run Julia.

  • Press ] to enter package management mode; press backspace to leave it.

  • Press Ctrl-C to interrupt execution.

  • Press Ctrl-D to exit.

  • Press ? to enter help mode.

  • Press Up/Down arrows to walk through the command history.

  • There are other interactive utilities described [in the manual]](https://docs.julialang.org/en/v1/stdlib/InteractiveUtils/).

Jupyter notebook#

Install the IJulia package, then import it and run IJulia.notebook() at the REPL to launch a session in a web browser.

  • You can put multiple lines in a code cell. Press Shift-Enter to execute the cell.

  • Click the Stop button (square icon) to interrupt execution.

  • Use include Pkg and then Pkg.add, etc. for package management.

Pluto notebook#

Unlike a Jupyter notebook, a Pluto notebook is reactive in the sense that a change anywhere affects all of the code cells immediately. To use it, install and import the \texttt{Pluto} package, then use \texttt{Pluto.run()} to launch it in a web browser.

Visual Studio Code#

Install the Julia extension and run the REPL in a pane. This option is most like an IDE such as PyCharm or Spyder and has many features to assist with coding.

Basics#

Item/Reference

Example

Help on a function

?func

Search help

apropos("topic")

Assign to variable

=

Comment

starts with #

Function definition

function foo(x,y) or foo = (x,y) ->

Other defined symbols

% ÷

Symbols using LaTeX nomenclature

\beta+Tab, \le+Tab, x\_0+Tab, z\hat+Tab

Input and output#

Example

Package

Integers

176, -1, 0

Floats

3.14, 1.0, 0., NaN, Inf

Scientific notation

1.234e-5

Complex numbers

3+4im, complex(3,4)

Constants

pi, π, exp(1)

Strings

"string"

Show result

@show

Print to screen

print, println

Formatted print

@sprintf, @printf

Printf

Interpolate into string

"x is $x"

Table

pretty_table

PrettyTables

Operators#

Example

Arithmetic

+ - * / ^

Broadcast over array

prefix with dot, or @. in front of all

Equality test

==

Comparison

< <= > >=

Logical AND / OR / NOT

& `

Short-circuit logic

&& `

Errors#

Message/Symptom

Possible cause

BoundsError

Accessed a nonexistent array element

MethodError

Probably used the wrong number/type of function arguments

InexactError

Illegal type conversion, maybe by assigning to an array

“Cannot juxtapose string literal”

Invalid string construction

Unexpected array changes

Use copy instead of array reference

Wrong norm

Used norm when opnorm is needed

Iteration#

Example

Predetermined number

for i in 1:10

Based on condition

while abs(x) > 1

Comprehension

[i+j for i in 1:3, j in 1:3]

Generator

sum(k for k in 1:10)

Vectors, matrices, arrays#

Example

Package

All ones, all zeros

ones(4), zeros(2,5)

Random elements

rand(100), randn(3,3)

Concatenate

[1;2;3] vertically, [1 2 3] horizontally

Get dimensions

length, size

Extract diagonal

diag(A), diag(A,-1)

LinearAlgebra

Build by diagonal

diagm([1,2,3]), diagm(1=>[-1,-1])

LinearAlgebra

Access element

x[2], A[1,4]

Access block

x[1:end-1], A[1:4,2:3]

Access row/column

A[4,:], A[:,2:2:end]

Range by step size

0:4, 1:2:9, 5:-1:1

Range by length

range(a,b,length=10)

Make a copy

copy

Linear algebra#

Example

Package

Adjoint/transpose

A'

Inner (scalar) product

dot(x,y)

LinearAlgebra

Outer product

x*v'

Solve linear system

A\b

Solve overdetermined system

A\b

Identity

I, I(5)

LinearAlgebra

Factorizations

lu, qr, Cholesky

LinearAlgebra

Norm

norm, opnorm

LinearAlgebra

Condition number

cond

LinearAlgebra

Eigenvalues

eigen, eigvals, eigs

LinearAlgebra

Singular values

svd, svdvals

LinearAlgebra

Major problem types#

Example

Package

Linear system / least squares

A\b

Sparse matrix

A\b, gmres, minres, cg, eigs

SparseArrays, IterativeSolvers, Arpack

Polynomial interpolation/approximation

fit

Polynomials

Polynomial roots

roots(Polynomial(-1,1,1))

Polynomials

Rootfinding

nlsolve

NLsolve

Integration

quadgk(sin,0,pi)

QuadGK

Initial-value problem

ODEProblem, solve

DifferentialEquations

Boundary-value problem

BVProblem, solve

DifferentialEquations

Graphics#

Example

Package

One variable

plot, scatter, hline, vline

Plots

Two variables

surface, contour, contourf

Plots

Network

graphplot

GraphRecipes

Matrix values

spy

Plots

Modify existing plot

plot! etc., xlabel!, xlims!, title!

Plots

Layouts

layout=(2,1), subplot=2

Plots

Log scales

yscale=:log10

Plots

Colors and images

RGB(1,0,1), Gray.(img)

Images