Introduction to the Julia programming language
1 Introduction¶
Why Julia? (1)¶
Julia is a relatively young (2012) programming language designed to be particularly effective for scientific workflows - the developers specifically call out Fortran and MATLAB as predecessors in this area.
Compared to these, Julia has much of the dynamic and interactive expressiveness of languages such as Python (including functionality that is only provided there by the third-party NumPy library), whilst leveraging just-in-time compilation and specialization to allow performance approaching (and sometimes better than) high-performance compiled languages such as C, C++, Modern Fortran and Rust.
Why Julia? (2)¶
Solves the dual-language problem. From ThinkJulia.jl:
Julia is a unique programming language because it solves the so-called two languages problem. No other programming language is needed to write high-performance code. This does not mean it happens automatically. It is the responsibility of the programmer to optimize the code that forms a bottleneck but this can done in Julia itself.
- Open source and free: The whole language and all packages!
- Easy to get started
- Many third party libraries
Some of Julia's properties¶
- High-performance (usually within a factor of 0.5 of C; often at parity)
- Full Unicode support - including LaTeX markup support in REPL, and natural mathematical symbols for common operations ($\sin^2\theta + \cos^2\theta = 1$)
- First-class support in Jupyter notebooks (it's the Ju bit of the name)
- Supports generic programming (with multiple dispatch), functional-style method chaining, and other modern paradigms
- Dynamic typing, with efficient function specialization (via JIT) and optional typing supported deeply in language (unlike Python)
- Has an excellent, robust built-in package manager making installation and reproducible environments a breeze
Julia is also just a really fun language to program in!
A small example code showing the Julia performance¶
We will estimate π using the equation: $π = 4 \times \left(1 - \dfrac{1}{3} + \dfrac{1}{5} - \dfrac{1}{7} + \dfrac{1}{9}-\dfrac{1}{11}+\dots\right)$
There are much better ways to estimate π, but this one is easy to implement.
function estimate_pi(n)
s = 1.0
for i in 1:n
s += (isodd(i) ? -1 : 1) / (2i + 1)
end
4s
end
p = estimate_pi(100_000_000)
println("π ≈ $p")
println("Error is $(p - π)")
# takes 0.2 s on first execution, then 0.1 s
π ≈ 3.141592663589326 Error is 9.999532757376528e-9
The corresponding Python code is about a factor 100 slower:
import math
def estimate_pi(n):
s = 1.0
for i in range(1, n + 1):
s += (-1 if i % 2 else 1) / (2 * i + 1)
return 4 * s
p = estimate_pi(100_000_000)
print(f"π ≈ {p}")
print(f"Error is {p - math.pi}")
Takes 6.8 s in Python:
π ≈ 3.141592663589326
Error is 9.999532757376528e-09
My programming background¶
- BASIC on a Commodore C64
- Assembler on a MOS 6510 CPU (C64)
- Pascal (in school, in particular UCSD Pascal and Turbo Pascal)
- FORTRAN 77 (for doctoral thesis)
- C/C++
- Mathematica/Wolfram language
- Perl
- Python
- Julia
Julia vs Python pros and cons¶
- Julia combines elegance, productivity, and performance. As easy to learn as Python.
- However, at the moment, Python seems to be the most popular general programming language. Often used as a glue language to integrate several components.
- The Python ecosystem is more mature, especially when it comes to machine learning.
- In Python it feels really strange though to me that all performance-critical code needs to done with external libraries.
- Will Python become faster in the future? Maybe. And there are tools like numba.
- The Python performance problem is also addressed by Mojo which mentions a speedup vs Python 3.10 of a factor 68000.
- Hard to tell how this plays out in the future ...
Speeding up Python code¶
import numba
@numba.jit
def estimate_pi(n):
s = 1.0
for i in range(1, n + 1):
s += (-1 if i % 2 else 1) / (2 * i + 1)
return 4 * s
p = estimate_pi(100_000_000)
print(f"π ≈ {p}")
print(f"Error is {p - math.pi}")
After Just-in-time compilation this takes 0.1 s
π ≈ 3.141592663589326
Error is 9.999532757376528e-09
Should I learn any programming language at all in view of ChatGPT & Co?¶
Q: Can you provide a julia function that calculates pi based on an a sum of inverse integers?
ChatGPT 3.5: Certainly! You can use the famous Gregory-Leibniz series to approximate π. This series states that:
π/4 = 1 - 1/3 + 1/5 - 1/7 + 1/9 - ...
Here's a Julia function that calculates π using this series up to a specified number of terms:
function calculate_pi(num_terms::Int)
pi_estimate = 0.0
sign = 1.0
for i in 1:num_terms
pi_estimate += sign / (2*i - 1)
sign *= -1
end
return 4 * pi_estimate
end
# Example usage:
num_terms = 1000000
pi_approximation = calculate_pi(num_terms)
println("Approximation of Pi using $num_terms terms: ", pi_approximation)
Approximation of Pi using 1000000 terms: 3.1415916535897743
That is certainly impressive. One has to focus less on details like the syntax of a language. LLM's a helpful to achieve a programming goal faster. However, a general understanding of concepts remains crucial.
Prospects for Julia in High Energy Physics¶
Widely used programming languages in physics (from Jim Pivarski, IRIS-HEP):
See https://github.com/JuliaHEP for HEP tool, e.g., for reading root files.
Further reading: Tamas Gal, Is Julia ready to be adopted by HEP?
More useful links¶
General
- Oliver Schulz, Why Julia? Motivation and comparison to other languages
- Jonas Eschle et al., Potential of the Julia Programming Language for High Energy Physics Computing, Comput.Softw.Big Sci. 7 (2023) 1, 10 (arXiv 2306.03675)
- Jeffrey M. Perkel, Julia: come for the syntax, stay for the speed, Nature 572, 141-142 (2019)
- Matthijs Cox, How to solve the two language problem?
Practical tips