This notebook is an introduction to the language Julia, and the use of the IJulia/Jupyter notebook in a statistics/optimization workflow. It is meant to be presented in 1 hour. It was created to be runned with Julia 0.5.
The Julia language will be introduced quickly, the aim is to motivate the readers to learn the language by themselves using more thorough teaching material.
Quoting the Julia website:
Julia is a high-level, high-performance dynamic programming language for technical computing, with syntax that is familiar to users of other technical computing environments. It provides a sophisticated compiler, distributed parallel execution, numerical accuracy, and an extensive mathematical function library.
High-level language
For example, Solving $Ax = b$ with $A = \begin{pmatrix} 1 & 2 & 3\\ 2 & 1 & 2\\ 3 & 2 & 1 \end{pmatrix}$ and $b = \begin{pmatrix} 1 \\ 1 \\ 1 \end{pmatrix}$ is as simple as:
In [1]:
A = [1 2 3
2 1 2
3 2 1]
b = [1,1,1]
A\b
Out[1]:
A dynamic language:
Julia is, like Python, Matlab or R, a dynamic language: you can interact with the language without the need to compile your code. Static or compiled languages, like C or Fortran, are more complicated to use but generally faster, and thus used when there is a need for time-efficient computations.
This is the two-languages problem: one generally use a high level language for research and scripting, and then translate the final result in a static language for performance.
A High Performance language:
In [2]:
function countTo(n)
count = 0
for i = 1:n
count += 1
end
return count
end
println("First use: slow like a dynamic language")
@time countTo(10_000_000)
println("Second use: compiled and optimized automatically")
@time countTo(10_000_000);
A language for technical computing
Once Julia is installed, start julia and just run the following command to install the IJulia
package
Pkg.install("IJulia")
This should work on its own. If there is any issue, check out the IJulia website.
Once IJulia is installed, go to the notebook file (.ipynb) directory, start julia and run:
using IJulia
notebook()
A webpage should open automatically, just click on the notebook to load it.
When you are not editing a cell, you are in Command mode and can edit the structure of the notebook (cells, name, options...)
Insert -> Insert Cell
a
or b
in Command ModeAlt+Enter
in Edit ModeEdit -> Delete Cell
dd
Cell -> Run
Ctrl+Enter
Other functions:
Ctrl+z
in Edit Modez
in Command ModeCtrl+s
in Edit Modes
in Command ModeThough notebooks rely on your browser to work, they do not require an internet connection (except for math rendering).
[Exercise]: Close/open
- Save the notebook
- Copy the address
- Close the tab
- Paste the address into a new tab (or re-open the last closed tab with
Ctrl+Shift+T
on Chrome)The document is still there, and the Julia kernel is still alive! Nothing is lost.
[Exercise]: Zoom
Try changing the magnification of the web page (
Ctrl+, Ctrl-
on Chrome).Text and math scale well (so do graphics if you use an SVG or PDF backend).
[Exercise]: MathJax
- Create a new cell, and select the type
Markdown
(or pressm
)- Type an opening \$, your favorite mathematical expression, and a closing \$.
- Run the cell to render the $\LaTeX$ expression.
- Right-click the rendered expression.
This section is a brief introduction to Julia. It is not a comprehensive tutorial but more a taste of the language for those who do not know it, and a showcase of cool features for those who already know Julia.
Very good tutorials are available online and in books if you are interested in learning the language.
In [3]:
1+1
Out[3]:
In [4]:
sin(exp(2*pi)+sqrt(3))
Out[4]:
The building blocs of Julia code are variables:
In [5]:
a = 1
b = 2
# This is a comment
c = a^2 + b^3
Out[5]:
Julia supports the common if
, while
and for
structures:
In [6]:
if c >= 10
print("Joey")
else
print("Huchette")
end
In [7]:
i = 1
while i <= 5
println("Joey!") # Print with a new line
i += 1
end
In [8]:
for i = 1:3
print("$i Joey") # '$' can be used to insert variables into text
if i>1
print("s")
end
println() # Just a new line
end
Do not worry about writing loops: in Julia, they are as fast as writing vectorized code, and sometimes faster!
Arrays (list of numbers) are at the core of research computing and Julia's arrays are extremely optimized.
In [9]:
myList = [1, 2, 3]
Out[9]:
Array indexing starts with 1 in Julia:
In [10]:
myList[1]
Out[10]:
In [11]:
myList[3] = 4
myList
Out[11]:
A 2-dimensional array is a Matrix
In [12]:
A = [1 2 3
2 1 2
3 2 1]
A = [1 2 3; 2 1 2; 3 2 1] #same thing
Out[12]:
Matrix can be multiplied, inversed...
In [13]:
A^-1 #inverse
A^2 * A^-1
Out[13]:
In [14]:
A*[1,2,3]
Out[14]:
In [15]:
eigenValues, eigenVectors = eig(A)
eigenValues
Out[15]:
Types: Everything has a type in Julia
In [16]:
typeof(1)
Out[16]:
In [17]:
typeof(1.5)
Out[17]:
In [18]:
typeof("abc")
Out[18]:
Types are at the core of Julia's performance. multiple dispatch is used dynamically when a function is called, selecting the right version of the function depending on the type of its argument.
In [19]:
1//2 # fraction in Julia
Out[19]:
In [20]:
typeof(1//2)
Out[20]:
In [21]:
(1//2)^2
Out[21]:
In [22]:
(0.5)^2 # The same function gives different results depending on the type
Out[22]:
In [23]:
(im)^2 # This also works with complex numbers
Out[23]:
In [24]:
function myFunction(x)
println("Julia!")
end
function myFunction(x::Int) # only called when x is an integer
println("IJulia!")
end
myFunction(1.0)
myFunction(1)
myFunction("Joey")
A lot more functionalities are available and for you to discover!
In [25]:
l = [i^2 for i in 1:10 if i%2 == 0] # list comprehensions (similar to Python)
Out[25]:
In [ ]:
# Add Packages Plots, and Pyplot (can take some time)
Pkg.add("Plots")
Pkg.add("PyPlot")
# Update
Pkg.update()
#Remove:
# Pkg.rm("PyPlot")
Use ?
to get the documentation of a function
In [26]:
?eig
Out[26]:
Use tab-completion to auto-complete functions and variables names: try myF<TAB>
:
In [ ]:
myF
fac
The methods
function lists all of the different implementations of a function depending on the input types.
Click on the link to see the Julia source code.
In [27]:
methods(sin)
Out[27]:
There are several Julia plotting packages.
Jupyter/IJulia will render the plots directly on the notebook!
In [32]:
using Plots
pyplot() #plot using Matplotlib
x = linspace(0,5,1000)
plot(x, sin(x.^2))
Out[32]:
Note that the first plot while always take a few seconds to be drawn, a consequence of Julia's just in time compilation. Lots of other plot types are available
Github and sharing
If you save your .ipynb notebook file in a .git project, hosted on Github, you can easily visualize and share it online (in non-interactive mode).
For example, this notebook is available at https://github.com/sebmart/intro-julia-jupyter/blob/master/intro-julia-jupyter.ipynb
You can also use Gist and nbviewer to quickly share a notebook (for example to your advisor) without creating a git repo.
Converting your notebook
Jupyter notebooks are a popular format that can be converted to a variety of types of documents, depending on your needs:
These conversions use the nbconvert
command.
Interactivity
Julia code can be made interactive in the notebook using the Interact.jl
Julia Package. Users can for example change the parameters of a plot dynamically:
Remote computing
The Notebook system is a web interface. Notebooks can be run on another computer. This is useful if you want your code to run on a more powerful remote machine.
Port-forwarding through SSH is a good start for this.
Advanced Markdown
Jupyter text cells use Markdown for formatting. Markdown is an easy to use formatting language (a little like HTML or LaTeX in more simple). You can use the text of this notebook as an example, or learn more here.
Jupyter uses Github flavored Markdown, and is particularly good at displaying math and colored code. You can even include a video!
Using the command line from Julia
You can run bash command from Julia, just start the command with a semi-colon: ;
In [40]:
;ls
Unicode character
You can use unicode characters as part of variables and function names in Julia. You can use $\LaTeX$-style autocompletes in the Julia terminal or a Jupyter/IJulia code-cell to write them. Some of them are already defined Julia constants and functions
Try to type
\pi<TAB>
in a cell.
In [41]:
π
Out[41]:
In [42]:
"carrot" ∈ ["potato", "tomato", "carrot"]
Out[42]:
Juno
Julia has a very nice and powerful text editor, Juno, that is built on Atom. It is very similar to the Matlab interface or RStudio. Functionalities include:
It is better suited for serious projects with several files, when an IJulia notebook is not enough.
Advanced Julia functionalities Julia is a state-of-the-art programming language, with lots of useful functionalities, including:
Some content in this notebook was adapted from materials by Miles Lubin