Julia cheat sheet

less than 1 minute read

Published:

A blog post to hold useful snippets of julia code.

Displaying large arrays

Define a convenience function showall() to print the entire contents of an array to the REPL. Based on https://discourse.julialang.org/t/show-entire-matrix/1437/3.

showall(x)=show(IOContext(stdout, :limit=>false), MIME"text/plain"(), x)

Optimisation

Least-squares fitting (LsqFit)

Levenberg-Marquardt fitting with error estimation.

using LsqFit

residuals(p) = @. sim(p...) - yobs
sol = LsqFit.lmfit(residuals, p0, Float64[])
pfit = sol.param
pfiterr = stderror(sol)

Non-linear minimisation (Optim)

General minimisation, not using Levenberg-Marquardt.

using Optim

residuals(p) = @. sim(p...) - yobs
costfunc(p) = sum(residuals(p).^2)
sol = optimize(costfunc, p0)
pfit = sol.minimizer

Plotting

Colour schemes

using Plots, ColorSchemes

plot(lotsofdataseries, palette=:Paired_12)
plot(lotsofdataseries, palette=palette(:lightrainbow, 11))

Useful discrete palettes:

  • :Paired_12 (light-dark-light-dark etc)
  • :Set1

Useful continuous palettes:

  • :lightrainbow
  • :magma