Julia Beginner’s Guide Part 2

Welcome to the part 2 of the guide I hope you have already gone over the part 1 of the tutorial and have a working version 0.5 of Julia installed. In this guide we will be dealing with dynamic typing, arrays, indexing, installing and working with a package called PyCall.


Dynamic Typing

Julia is a dynamically typed language what you mean by this is that there is no need to explicitly specify type definitions. Julia itself infers the type from the code.

julia> num = 10

Here Julia understands that num is a integer variable. More specifically Int64.

You can even specify specific types in Julia while passing variables in functions.

julia> function mult(x::Float64, y::Float64)
x * y
end

In this particular case x is defined as a Float64 and y also as a Float64 variable. This case Julia behaves like a statically typed language. But it is still considered as a dynamically typed language predominantly.


Arrays

Now lets code!

# creating a vector
julia> vec1 = [11, 21, 31, 41]
4-element Array{Int64,1}:
11
21
31
41

append! pop! deleteat!

julia> append!(vec1, 5)
5-element Array{Int64,1}:
11
21
31
41
5
julia> pop!(vec1)
5
julia> vec1
4-element Array{Int64,1}:
11
21
31
41
julia> deleteat!(vec1, 3)
3-element Array{Int64,1}:
11
21
41

For insight into more functions you can always look into the documentation.

Multidimensional Arrays

Looking into how multidimensional arrays are stored in a computer. Column major and row major are two methods of storing multidimensional arrays in linear storage like random access memory.

It basically means if we have a multidimensional array like

1 2 3
4 5 6
7 8 9

In row major it will be stored like 1 2 3 4 5 6 7 8 9 while in column major it will be stored like 1 4 7 2 5 8 3 6 9.

Julia uses column major form.

Try it out yourself,

julia> array_2d_row_vector = [121 225 324 400]
1×4 Array{Int64,2}:
121 225 324 400
julia> array_1d_column_vector = [121, 225, 324, 400]
4-element Array{Int64,1}:
121
225
324
400

All the coders from python (numpy), C / C++, background take your time let this code snippet sink in. The first array array_2d_row_vector is a row vector first of all but also as Julia uses Column based indexing it is a 2d vector. Makes sense? (I mostly code in C++ and Python so when I actually looked at this I went into coma!)

lets look at another example:

julia> x = [1 2; 3 4]
2x2 Array{Int64,2}:
1 2
3 4
# `:` means access all elements from start to end
julia> x[:]
4-element Array{Int64,1}:
1
3
2
4

The output of this code snippet directly points to the column major form of multidimensional arrays in Julia. Instead of a showing the wrong output shown below

julia> x[:]
4-element Array{Int64, 1}
1 2 3 4

Julia Shows,

julia> x[:]
4-element Array{Int64,1}:
1
3
2
4

If you have come from the row major background you must be wonder does it really make that much of a difference?

julia> x = [1 2 3; 4 5 6; 7 8 9]
3×3 Array{Int64,2}:
1 2 3
4 5 6
7 8 9
julia> x[1, 2:3]
2-element Array{Int64,1}:
2
3
julia> x[1:end, 2:3]
3×2 Array{Int64,2}:
2 3
5 6
8 9

No, not really. As you can see accessing indexes in Julia is super easy. As long as you remember everything is column major everything should be fine!


PyCall

PyCall is a package that lets you directly run Python modules, functions inside Julia. It also gives you the ability to create Python classes in Julia and directly share large data structure without copying. All you just need to do is make one import and you are ready to go.

For installing PyCall in Julia just type in Julia REPL

julia> Pkg.add("PyCall")

This will install PyCall with systems default Python version. if you want to change the Python version PyCall is configured with, you can run:

ENV["PYTHON"] = "... path of the python version you want ..."
Pkg.build("PyCall")

Usage

julia> using PyCall
julia> @pyimport math
julia> math.sqrt(2123) # Python's sqrt function
46.07602413403309
julia> sqrt(2123) # Julia's sqrt function
46.07602413403309

Now write any code in python and directly port it to Julia. For more tips on using PyCall you can my GSoC project here.