In this notebook, we go through basic constructs and commands.
The user should know to start Julia in various modes (command line prompt, IJulia), how to exit, learn some features and be able to write simple programs.
This notebook is based on the slides accompanying the Lightning Round video by Alan Edelman, all part of the Julia Tutorial.
Julia resources are accessible through the Julia home page.
Please check packages, docs and juliacon (here you will also find links to videos from previous conferences).
Possibility to write comments / code / formulas in Markdown cells, makes Jupyter notebooks ideal for teaching and research. Text is written using Julia Markdown, which is GitHub Markdown with additional understanding of basic LaTeX.
Mastering (GitHub) Markdown is a 3-minute read, another short and very good manual is at http://daringfireball.net/projects/markdown/.
Some particulars of Julia Markdown are described in Documentation section of Julia Manual, yet another 3-minute read.
nbconvertIt is extremely easy to convert notebooks to slides, LaTeX, or PDF. For details see the documentation.
Clicking View -> Cell Toolbar -> Slideshow opens the Slide Type menu for each cell.
The slideshow is made with the command
jupyter nbconvert --to slides notebook.ipynb
The slideshow is displayed in browser with the command
jupyter nbconvert --to slides --post serve notebook.ipynb
jupyter nbconvert --to latex notebook.ipynb
jupyter nbconvert --to PDF notebook.ipynb
N.B. For the above conversions Pandoc needs to be installed.
In [1]:
versioninfo()
In [2]:
# exit()
Documentation is well written and the starting point is http://docs.julialang.org/en/latest/
But, also remeber that Julia is open source and all routines are available on GitHub. You will learn how to make full use of this later in the course.
In [3]:
# Matrix with random entries between 0 and 1
A=rand(5,5)
Out[3]:
In [4]:
A[1,1]
Out[4]:
In [5]:
# You can index into output directly
rand(5,5)[1:2,3:4]
Out[5]:
In [6]:
(A^10)[4:5,4:5] # Parenthesses around A^10 are necessary
Out[6]:
In [7]:
[i for i=1:5]
Out[7]:
In [8]:
[trace(rand(n,n)) for n=1:5]
Out[8]:
In [9]:
x=1:10
Out[9]:
In [10]:
[ x[i]+x[i+1] for i=1:9 ]
Out[10]:
In [11]:
z = [eye(n) for n=1:5] # z is Array of Arrays
Out[11]:
In [12]:
# First element is a 1x1 Array
z[1]
Out[12]:
In [13]:
# What is the fourth element?
z[4]
Out[13]:
In [14]:
# Another example of a comprehension
A=[ i+j for i=1:5, j=1:5 ]
Out[14]:
In [15]:
# Notice the promotion
B=[ i+j for i=1:5, j=1.0:5 ]
Out[15]:
In [16]:
ndims(ans)
Out[16]:
In [17]:
# z is a one-dimensional array
ndims(z)
Out[17]:
In [18]:
# Array of Arrays
typeof(z)
Out[18]:
In [19]:
# z[5] is a two-dimensional array
typeof(z[5])
Out[19]:
In [20]:
typeof(A)
Out[20]:
See Multi-dimensional arrays for more.
In [21]:
v=rand(5,1) # This is 2-dimensional array
Out[21]:
In [22]:
vv=vec(v) # This is an 1-dimensional array or vector
Out[22]:
In [23]:
v==vv # Notice that they are different
Out[23]:
In [24]:
v-vv # Again a promotion
Out[24]:
In [25]:
w=rand(5) # This is again a vector
Out[25]:
In [26]:
Mv=[v w] # First column is a 5 x 1 matrix, second column is a vector of length 5
Out[26]:
In [27]:
x=Mv[:,1] # Matrix columns are extracted as vectors
Out[27]:
In [28]:
y=Mv[:,2]
Out[28]:
In [29]:
x==v # The types differ
Out[29]:
In [30]:
y==w
Out[30]:
In [31]:
# Transpose of a matrix is a matrix
v'
Out[31]:
In [32]:
# Transpose of a vector is a RowVector
w'
Out[32]:
In [33]:
w=1.0:5
Out[33]:
In [34]:
A*w
Out[34]:
In [35]:
w=collect(1.0:5)
Out[35]:
In [36]:
A*w # This returns a 1-dimensional array
Out[36]:
In [37]:
A*v # This returns a 2-dimensional array - v is a 5 x 1 array
Out[37]:
In [38]:
B=[A[i,:]*A[:,j] for i=1:5, j=1:5]
In [39]:
# Rows and columsn are both 1D vectors - must use dot product
B=[A[i,:]⋅A[:,j] for i=1:5, j=1:5]
Out[39]:
In [40]:
inv(B)
Out[40]:
In [41]:
# The output type depends on the argument. Float64 is the default.
ones(3,5), ones(5), ones(rand(1:3,4,6))
Out[41]:
In [42]:
rand(1:3,4,6)
Out[42]:
In [43]:
zeros(3,5)
Out[43]:
In [44]:
zeros(5)
Out[44]:
In [45]:
zeros(rand(1:3,4,6))
Out[45]:
In [46]:
eye(4)
Out[46]:
In [47]:
eye(Int,4)
Out[47]:
In [48]:
eye(Int32,4)
Out[48]:
In [49]:
complex(eye(4))
Out[49]:
In [50]:
im
Out[50]:
In [51]:
2im
Out[51]:
In [52]:
typeof(ans)
Out[52]:
In [53]:
typeof(2.0im)
Out[53]:
In [54]:
# Another way of defining complex numbers
complex(3,4)
Out[54]:
In [55]:
# If one of the arguments if Float64, so is the
# entire number - promotion!
complex(3,4.0)
Out[55]:
In [56]:
# This produces an error (like in any other language)
sqrt(-1)
In [57]:
# and this is fine.
sqrt(complex(-1))
Out[57]:
In [58]:
si(x) = (x>0) ? 1 : -1
Out[58]:
In [59]:
si(-13)
Out[59]:
This is equivalent to:
In [60]:
function si(x)
if x>0
return 1
else
return -1
end
end
Out[60]:
In [61]:
si(pi-8), si(0), si(0.0)
Out[61]:
The expressions can be nested:
In [62]:
# now si(0) is 0
si(x) = (x>0) ? 1 : ((x<0) ? -1: 0)
Out[62]:
In [63]:
# '\pi Tab' produces π and means π
si(π-8), si(0)
Out[63]:
In [64]:
Ξ = 8; Ψ = 6; Γ = Ξ ⋅ Ψ
Out[64]:
In [65]:
typeof(Γ)
Out[65]:
In [67]:
ω₁=7; xᵏ=23
ω₁*xᵏ
Out[67]:
Special feature of Julia is that the results of commands are not displayed, unless explicitely required.
To display results you can use commands @show or println() (or many others, see the Text I/O in the manual.)
Consider the file deploy.jl with the following code
n=int(ARGS[1]) # take one integer argument
println(rand(1:n,n,n)) # generate and print n x n matrix of random integers between 1 and n
@show b=3 # set b to 3 and show the result
c=4 # set c to 4
Running the program in the shell gives
$ julia deploy.jl 5
[1 3 2 4 1
5 3 1 1 4
5 4 2 2 5
3 1 2 3 4
4 4 5 4 4]
b = 3 => 3
Notice that the result of the last command (c) is not displayed.
You can, of course, also run the above command in the
Consoletab ofJuliaBox. To do this, you first have to change the directorycd Julia-Course/src
Similarly, the program can be converted to executable and run directly,
without referencing julia in the command line.
The refernece to julia must be added in the first line, as in the file deploy1.jl:
#!/usr/bin/julia
n=int(ARGS[1])
println(rand(1:n,n,n))
@show b=3
c=4
In the shell do:
$ chmod +x deploy1.jl
$ ./deploy1.jl 5
[4 5 3 2 5
4 2 1 5 1
3 2 4 5 1
2 4 4 3 1
3 4 5 3 3]
b = 3 => 3
Finally, to run the same program in julia shell or IJulia, the input has to be changed, as in the file deploy2.jl:
n=int(readline(STDIN))
println(rand(1:n,n,n))
@show b=3
c=4
Notice that now the result of the last line is displayed by default - in this case it is 4, the values of c. The output of the random matrix and of b is forced.
In [68]:
include("deploy2.jl")
Out[68]:
In [69]:
?run
Out[69]:
Notice, that this is not a gret help, Julia has much better commands for this.
In [70]:
# This calls the unix Calendar program
run(`cal`)
In [71]:
# The pipe is '|>' instead of usual '|'
run(pipeline(`cal`,`grep Sa`))
In [72]:
?ccall
Out[72]:
In [73]:
# Simple version
ccall(:clock,Int,())
Out[73]:
In [74]:
path = ccall((:getenv, "libc"), Cstring, (Cstring,), "SHELL")
Out[74]:
In [75]:
unsafe_string(path) # Human readable version
Out[75]:
Task() and ChannelJulia has a control flow feature that allows computations to be suspended
and resumed in a flexible manner (see
Tasks in the manual).
In [76]:
function stepbystep(c::Channel)
put!(c, "start")
for n=1:3
put!(c,n^2)
end
put!(c,"stop")
end
Out[76]:
In [77]:
c1=Channel(stepbystep)
Out[77]:
In [78]:
take!(c1)
Out[78]:
In [79]:
take!(c1)
Out[79]:
In [80]:
take!(c1)
Out[80]:
In [81]:
take!(c1)
Out[81]:
In [82]:
take!(c1)
Out[82]:
In [83]:
# Guess what comes next?
take!(c1)
In [ ]: