Sunday, March 4, 2018

Try Julia: from install to simple mathematical operation

I started to use Julia. Although more or less one year ago, I touched Julia, it didn't become my daily tool. But these days I frequently hear about Julia in data science field and also the news about version 1.0 hit on me. So, I decided earnestly to tackle with it.
On this article, I'll arrange some information of Julia and show some mathematical and matrix operations.




Julia


Julia is one of the programming languages. Especially in data science and machine learning areas, it’s commanding considerable attentions. About the detail, please check the official page below.
There are many remarkable features. For me, especially the performance and the way of handling data are awesome.
One of the motivations for me to tackle with Julia is from the following article.
From this,
We want a language that's open source, with a liberal license. We want the speed of C with the dynamism of Ruby. We want a language that’s homoiconic, with true macros like Lisp, but with obvious, familiar mathematical notation like Matlab. We want something as usable for general programming as Python, as easy for statistics as R, as natural for string processing as Perl, as powerful for linear algebra as Matlab, as good at gluing programs together as the shell. Something that is dirt simple to learn, yet keeps the most serious hackers happy. We want it interactive and we want it compiled.
This is really cool.
There are some books about Julia.

Install

If you are Mac user, you can install Julia by Homebrew. Actually, when I tried to install by brew, it failed on the phase of openblas-julia. This time, I wanted to re-enter Julia as soon as possible. So I just used brew cask to avoid this. Later, I’ll try to fix.

brew install julia

or

brew cask install julia

Environment

To write the code about Julia, you can choose some tools. The followings are some examples.

IDE
Vim
Jupyter
Julia Studio

Check how it works

Here, I'll touch simple mathematical and matrix operations.
As a premise, my Julia’s version is 0.6.2.
At first, declare the variables and check the type.

x = 2
println(typeof(x))

y = 2.0
println(typeof(y))
Int64
Float64

The division returns the real result.

x = 8
y = 6
x/y
1.3333333333333333

By using div() function, you can get integer divisor.

x = 8
y = 6
println(div(x, y))
1

There are some ways of making Array.

A = [1,2,3]
println(A)
[1, 2, 3]

Of course, you can make empty Array and append to it.

A = Int64[]
push!(A, 1)
push!(A, 2)
println(A)
[1, 2]

Julia has zeros() and ones() functions. Those return Array.

A = zeros(15)
B = ones(15)
println(A)
println(B)
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]

rand() function also returns Array which is filled with random numbers.

A = rand(1:100, 15)
println(A)
[84, 61, 99, 87, 60, 82, 92, 63, 41, 60, 59, 91, 96, 79, 46]

The mathematical operations to Array are very intuitive.

A = rand(1:100, 15) / 10
println(A)
[7.2, 2.2, 8.5, 7.9, 3.8, 7.9, 6.4, 4.1, 6.4, 6.8, 7.0, 0.9, 9.0, 0.9, 3.6]

If you separate the values by space, it becomes matrix. By semicolons, you can define more rows.

A = [1 2 3; 4 5 6]
println(A)
[1 2 3; 4 5 6]

It is very easy and intuitive to get access to the values of a matrix. It has same manner as Python's pandas and R.

A = [1 2 3; 4 5 6]
println(A[1,1])
println(A[2,3])
println(A[1,:])
println(A[:,2])
1
6
[1, 2, 3]
[2, 5]

Array can be reshaped.

A = [1, 2, 3, 4, 5, 6]
B = reshape(A,2,3)
println(A)
println(B)
[1, 2, 3, 4, 5, 6]
[1 3 5; 2 4 6]

Julia as default has transpose() function.

A = [1 2 3; 4 5 6]
B = transpose(A)
println(B)
[1 4; 2 5; 3 6]

The matrix calculation follows the normal matrix rules.

A = [1 2 3; 4 5 6]
B = [1;2;3]
println(A * B)
[14, 32]

Element-wise operation is also easy.

# element operation
A = [1 2 3; 4 5 6]
B = [4 5 6; 7 8 9]
println(A .* B)
[4 10 18; 28 40 54]