Sunday, 27 December 2020

This week 7/2020 - Julia

This post describing my first feel when I completed a Julia basic course.

I am experienced Java developer but I have also osculation with C/C++, Python and Octave languages. For me Julia has something from all those languages.

Linear Algebra support:

Octave:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
A = eye(1,2)
%Diagonal Matrix
%
%   1   0

B = eye(3,2)
%Diagonal Matrix
%
%   1   0
%   0   1
%   0   0
C = [A
B]
%C =
%
%   1   0
%   1   0
%   0   1
%   0   0

Julia:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
using LinearAlgebra

A = 1*  Matrix(I,1,2)
# 1×2 Array{Int64,2}:
#  1  0

B = 1*  Matrix(I,3,2)
# 3×2 Array{Int64,2}:
#  1  0
#  0  1
#  0  0

C = [A
       B]
# 4×2 Array{Int64,2}:
#  1  0
#  1  0
#  0  1
#  0  0

There is also similarity when multiply matrixes. Julia support ex. operations f(A) = A*A and f.(A) whene every A[i,j] * A[i,j].

Syntax similarity to Python:

Julia:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# for loop
for i in 1:10, j in 1:20
    println("Hi $i , $j")
end
for item in items
    println("Hi $item")
end

# function definition
function power(x)
# last element is returnes - the same as in Python
    x^2
end

# other options to define function 
power(x) = x^2

power = x -> x^2

# immutable sorting
sort(x)

#mutable sorting
sort!(x)

Overload operators:

Python:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class String:
    def __init__(self, x=""):
        self.x = x

    def __add__(self, other):
        return self.x == other.x


p1 = String("test1")
p2 = String("test1")

print(p1 + p2)

Julia:

1
2
3
4
5
6
import Base: +

+(x::String, y:: String) = x == y

# returns boolean value
x+y 

Performance:

Benchmarks which I saw in course [3] shows that Julia has similar or a little better performance than C code and this about 2 orders of magnitude than Python.


Resources:

[1] https://julialang.org/

[2] Introduction to Julia (for programmers)

[3] Parallel Computing


No comments:

Post a Comment