What is Julia?

What is Julia?

I wrote several posts that use Julia for orbital mechanics, so I want to step back and say what the language is. Julia is a programming language for numerical and scientific computing. Its home page is julialang.org.

What it is for

Julia targets mathematics, statistics and other heavy numeric work: data science, machine learning, parallel computing and scientific simulation. The pitch is that you write code that reads like the equations, and it still runs fast.

How it works

Julia is compiled just in time. You type code in a session or a file, and Julia compiles each function to machine code the first time it runs with a given set of argument types. Later calls skip that step. The compiler is built on LLVM.

The central idea is multiple dispatch. A function can have several methods, and Julia picks the method from the types of all its arguments. Here is a small example I ran on Julia 1.13.3:

f(x) = x^2 + 1

f(3)        # 10
f(2.5)      # 7.25
f(2//3)     # 13//9

One definition works for integers, floating point numbers and exact fractions. Each call returns the type you would expect: an integer, a float and a rational. Julia compiles a separate version of f for each type.

You can also write methods for specific types:

area(r::Float64) = pi * r^2
area(s::String) = "not a number"

area(2.0)   # 12.566370614359172
area("x")   # "not a number"

Libraries use this a lot. The same + works on numbers and on matrices.

Linear algebra is built in. This finds the eigenvalues of a small matrix:

using LinearAlgebra
eigvals([2.0 1.0; 1.0 3.0])   # [1.381966011250105, 3.618033988749895]

Versions and installing

Julia 1.0 shipped in August 2018. Since then the project has followed semantic versioning: code written for 1.0 is meant to keep working on every 1.x release. Releases are listed at julialang.org/downloads. Install and update with juliaup, the installer the project recommends, and check that page for the current version before you pick one.

Packages come from the built-in package manager:

using Pkg
Pkg.add("DifferentialEquations")

Why I use it

I ported orbital mechanics code from Python to Julia, and I plan to port some quantum algorithms. Python has libraries for quantum computing already, so the ports should not be hard. The Julia versions of the orbital code are short, and the syntax stays close to the maths. You can see the results in the Kepler’s equation and orbital angles posts.

What to expect

Julia is a programming language, and what you know from another language carries over. Loops, functions, arrays and packages work the way you expect. The new parts are the type system and multiple dispatch, and the Julia manual covers both.

Subscribe to Just another Geek

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe