Julia 101
If this is the first time you've come across Julia, this page will provide a basic tutorial into Julia. Much of the syntax is similar to MATLAB and Python.
Basics
The basics of operation are very similar.
julia> x = 10
10
julia> y = x + 3
13
julia> y = x * 3
30
julia> y = x / 3
3.3333333333333335
julia> y = x ÷ 3
3
julia> y = x % 3
1
julia> for i in 1:5
println("2 x i = ", 2i)
end
2 x i = 2
2 x i = 4
2 x i = 6
2 x i = 8
2 x i = 10Arrays
Like MATLAB, array indices start with 1
julia> x = [10, 20, 30, 40, 50]
5-element Vector{Int64}:
10
20
30
40
50
julia> a = x[1]
10
julia> b = x[2]
20Like MATLAB, to perform element-wise operations on arrays, use dot in front of the operator. This performs broadcasting and will be useful for arrays of different sizes
julia> x = [10, 20, 30, 40, 50]
5-element Vector{Int64}:
10
20
30
40
50
julia> y = x .+ 5
5-element Vector{Int64}:
15
25
35
45
55
julia> y = x ./ 10
5-element Vector{Float64}:
1.0
2.0
3.0
4.0
5.0
julia> y = x .+ x'
5×5 Matrix{Int64}:
20 30 40 50 60
30 40 50 60 70
40 50 60 70 80
50 60 70 80 90
60 70 80 90 100When functions and complex expressions are needed to be performed, @. can be used to get a cleaner code.
julia> x = [10, 20, 30, 40, 50]
5-element Vector{Int64}:
10
20
30
40
50
julia> y = exp.(sin.(x ./ 10))
5-element Vector{Float64}:
2.319776824715853
2.4825777280150008
1.151562836514535
0.46916418587400077
0.3833049951722714
julia> y = @. exp(sin(x/10))
5-element Vector{Float64}:
2.319776824715853
2.4825777280150008
1.151562836514535
0.46916418587400077
0.3833049951722714Functions
Usage of function is similar to MATLAB. In the above example, we already call functions. Julia also has a bunch of mutating functions. The name of such functions mostly end with a ! and mutate the value stored in (usually) the first variables
julia> x = [30, 40, 10, 50, 20]
5-element Vector{Int64}:
30
40
10
50
20
julia> sort!(x)
5-element Vector{Int64}:
10
20
30
40
50
julia> x
5-element Vector{Int64}:
10
20
30
40
50Such functions are helpful when you want to reduce allocations to speed up the code.
Creating new functions
Creating new functions is also similar to MATLAB, and is done via the following syntax :
julia> function f(x, y)
a = x/(1 + x^2)
b = y/(1 + y^2)
z = a + b
return z
end
f (generic function with 1 method)
julia> f(2.0, 3.0)
0.7You can also do a single line definition :
julia> f(x, y) = 2x + y
f (generic function with 1 method)
julia> f(2.0, 3.0)
7.0Optional and keyword arguments
A lot of times, we want default arguments or named arguments. Consider the following function definition:
function f(x, y, a=2; operation_type=2)
if operation_type == 1
return x+y/a
elseif operation_type == 2
return x/(x^2 + a^2)
elseif operation_type == 3
return x/(x^2 + a^2)
else
return y/a
end
endf (generic function with 2 methods)In the above definition, a is called an optional argument. It assumes a value if it is not explicitly passed
Note
Note that optional arguments are positional. If a function is defined as
function fn(x, y, a=1.0, b=2.0, c=3.0)
# some operation
endThen the function call fn(2., 3., 2., 4., 5.) assumes a = 2., b = 4. and c = 5.. Similarly, a function call fn(2., 3., 4.) assumes a = 4. and default values for b and c.
f(2.0, 3.0)0.25A different value for a can be passed without referring to it explicitly:
f(2.0, 3.0, 1.0)0.4operation_type is a keyword argument. In the above case, it also assumed a value, but this is not necessary. If you want to pass a value to it, you have to explicitly refer to it in the function call:
f(2.0, 3.0; operation_type=2)0.25and with a different optional argument as :
f(2.0, 3.0, 1.0; operation_type=2)0.4Named Tuple
Named Tuples are data structures that hold values assigned to them by a name. These are the most important data structures in the context of the package and are used a lot.
julia> x = (; a=30, b=2.0, c="Hello")
(a = 30, b = 2.0, c = "Hello")
julia> x.a
30
julia> x.b
2.0
julia> x.c
"Hello"Named-tuples are immutable, that is, once created, their values do not change. However, we can create another named-tuple of the same name that can feel like mutating.
julia> x = (; a=30, b=2.0, c="Hello")
(a = 30, b = 2.0, c = "Hello")
julia> x.a
30
julia> x = (; x..., a=50)
(a = 50, b = 2.0, c = "Hello")
julia> x.a
50While Julia has lot more features than we can cover here, this should help you get started with this package.