Julia is described as a “high-level, high-performance dynamic programming language for numerical computing”. The Julia language was started in 2009 by a team of computer scientists who wanted to combine the speed of C with the best parts of Ruby, Mathematica, R, Python, MatLab, among other languages (here is a blog post on why Julia was created). Julia is thus a solution to Ousterhout’s dichotomy or the “two language” problem - fast (compiled) languages are hard to use while easy to use (interpreted) languages are slow. In essence, Julia is a compiled language which behaves like an interpreted language because it is compiled at runtime. The beta version of Julia was released in 2012, followed by the stable release in 2017. Julia is under active development, and is quickly being adopted by programmers in a variety of fields.
The astrophysical community adopted Python because it is open-source and easy to learn. Julia is also open-source, and I hope to demonstrate here that it is easy to learn. You might be wondering: If Python and Julia share these qualities, why switch?
Perhaps you are thinking about switching from IDL to Python, but then heard about Julia? Here is an AstroBetter blog post about switching from IDL to Python, dated May 4, 2009. While presently there is no comparable post for Julia, I would argue that you should switch from IDL for the same reasons why you would switch from Python.
If you’ve decided to try Julia, you may be wondering: How do I install Julia? The answer to this question depends on how you want to run Julia.
Note for August - November 2018: There have been several updates to Julia in this short period of time. Julia version 1.0.0, 1.0.1, 1.0.1.1, and 1.0.2 were all released. Here is documentation for 1.0+, including reasons for changes since v0.6. Julia v0.7 is comparable to v1.0+. JuliaPro includes v1.0.1.1 - If you want to run v1.0.2 (or any new version not part of the JuliaPro package, then follow these instructions.
Installation of both the command line version and JuliaPro are straightforward; so if you find yourself looking at an error, I suggest going to the Discourse forum for help. The most common error for Mac users might be not having Xcode. You can install Xcode in Terminal: $ xcode-select --install
You also need to authorize Xcode: $ sudo xcodebuild -license
So, you’ve installed Julia. What’s next?
If you installed the command line version of Julia, then you may want to add the following to your bash profile: alias julia=/Applications/Julia-(version).app/Contents/Resources/julia/bin/julia
. This way, you can launch the Julia binary as $ julia
and exit as ^D
. The current version of Julia is 1.0.2.
If you installed JuliaPro, then you can add the following to your bash profile: alias julia=/Applications/JuliaPro-(version).app/Contents/Resources/julia/Contents/Resources/julia/bin/julia
. When you open JuliaPro and go to Settings (open with Cmd + , ) -> Packages -> julia-client, the same path should be noted. If you are installing Julia with Atom instead of getting the JuliaPro package, then this is also where you would define the directory that Julia is located.
To show the version of Julia on either the command line or JuliaPro console, enter versioninfo()
.
The first thing you want to do in Julia, regardless of whether you have the command line version or JuliaPro, is update the packages. In previous versions of Julia (0.6 or earlier), this would be accomplished as: Pkg.update()
. In v0.7+, packages are managed with the Pkg REPL (Read-Eval-Print Loop). To enter Pkg REPL-mode, enter the ]
key; and, to exit, enter delete
/backspace
. To update in the Pkg REPL-mode, simply enter update
. To see the current list of packages and their version information, enter status
.
Here is more information about Julia packages and package development. A list of packages registered with Julia is available here.
To add a package, enter add (package name)
. To remove a package, enter rm (package)
. After adding a package, you need to build it: build (package)
. If you enter status
in Pkg REPL-mode, then the package that you just installed should be listed. If there are issues installing packages, particularly unregistered ones, it would be wise to post on the Discourse forum for help.
If you do not want to use the Pkg REPL and prefer the old package manager, then you need to use the Pkg package: using Pkg
. Then you can use: Pkg.update()
, Pkg.status()
, Pkg.add("package name")
, Pkg.rm("package")
, and Pkg.build("package")
.
Here are some essential Julia packages:
To use a package, either type using (package)
in the command line, JuliaPro console, or in a Julia script (extension .jl). If you are in the editor tab of JuliaPro, you can execute this line with Shift+Enter, like in Mathematica. Below is an example of using Unitful, UnitfulAstro
in JuliaPro’s console (left) and editor (right).
Perhaps you decided to try Julia because you were drawn by the promise that you don’t need to give up Python. By default, the PyCall package in Julia points to /usr/local/bin/python
.
Suppose you have 2 versions of Python on your computer, and want Julia to use a particular version. How would you point Julia to that version?
/Applications/Julia-(version).app/Contents/Resources/julia/etc/julia/juliarc.jl
if you have the command line version or /Applications/JuliaPro-(version).app/Contents/Resources/julia/Contents/Resources/julia/etc/julia/juliarc.jl
if you have JuliaPro. Change the system environment variable ENV["PYTHON"]=normpath("/", "usr", "bin", "python")
to the path that contains the version of Python that you want.Pkg.build("PyCall")
The goal of this section is to get you started with creating variables, performing arithmetic, defining functions, using arrays and matrices. All of the following is detailed in the Julia Manual. You will also find this 1-page printable cheatsheet and more extensive cheatsheet helpful.
Variables: You can name assign values to variables as (variable) = (value)
in Julia. You can also reassign values to variables and perform operations on variables as demonstrated below. Information about integer and floating-point bits can be found here. If you want to determine the type of integer/ floating-point a number or variable is, then you can use the typeof()
function. In JuliaPro, all of your variables, arrays, and most recent calculations would appear in the Workspace. In previous versions of Julia (0.6 and earlier), you could clear all values with workspace()
. However for v0.7+, you need to restart Julia with ^D
and enter
in on the command line or JuliaPro console.
Arithmetic in Julia is the same as other languages.
a+b
or a-b
a*b
or a/b
a^c
, sqrt(a)
, exp(a)
log(a)
or log10(b)
sin(a)
, cos(b)
,….Functions: There are two ways of writing functions in Julia. If you have a short function, you might simply write f(x) = (some operation)
. If you want something more complicated - perhaps including conditional statements - then you would write a multi-line function like on the right.
Arrays and Matrices: Arrays are ordered lists of elements, which can be numbers or strings or variables. Arrays can be 1D vectors, 2D matrices, or multi-dimensional matrices. 1D column vectors (n x 1) in Julia are written as A = [1,2,3,....]
, while row vectors (1 x n) are A = [1 2 3 ....]
. For 2D matrices, rows are separated by semicolons (;) like A = [1 2 3; 4 5 6; 7 8 9]
. To create a vector or matrix with randomn numbers, us the rand()
function. If you want to reassign the value of an element of a vector or matrix, you would note the element number as A[element] = (new value)
.
You may want to perform an operation on equally spaced points from a to b. There are many ways to generate the points from a to b:
range()
function or using a colon (:) like a:b
. You can define the increment in between a and b as a:increment:b
.linspace(a,b,n)
function to create a range with n elements - the increment is calculated within the function. The logarithmic version is logspace(a,b,n)
.The syntax for arithmetic operations on arrays differs slightly from arithmetic operations on numbers:
A+k
, A-k
, A*k
, A/k
A+B
or A-B
A.*B
or A./B
A.^c
, sqrt.(A)
, exp.(A)
sin.(A)
, cos.(A)
,….A*B
A'
As an example, let’s make an array of values from $-\pi/2$ to $\pi/2$ and take the sine of that.
if-elseif-else
statements
&&
and ||
evaluations
while
and for
loops
I am an Astrophysics Ph.D. student at Arizona State University, where I work on modeling the accretion of tidally disrupted rocky bodies onto white dwarf stars and the single degenerate scenario for Type Ia supernovae. I started learning and using Julia in early 2016 because I did not want to write code in Fortran 77 - like my advisors - but needed a computationally fast language for my research. I am presently the only graduate student in my department who uses Julia; however, I hope that by writing this guide, I will convince many to become Julians ;)
Updated November 2018