Some notes when learning the Julia programming language
Continue here: https://techytok.com/lesson-variable-scope/
This one looks good for afterwards: https://ucidatascienceinitiative.github.io/IntroToJulia/
For most of this, I'm just writing down main differences with Python.
Some general things to remember:
- Julia starts indexing at 1, like Matlab
- Loops and function definitions require and explicit 'end' statement
- This allows Julia to be less strict about indentation than Python
- Print formatting for variables a, b, c:
print("$a $b $c") - If returns are absent, Julia will return the last computed value!
- That's tricky! Don't know why they did that.
function test()
2+3
end
a = test()
Arrays and vectors
Unlike Python, we do not need an explicit nested list syntax for 2D arrays. We can do:
m = [1 2 3; 4 5 6]
Apparently Julia is more efficient when you first iterate over columns.
So in Python you would first iterate over each row, and then add a list as an element to get a 2D list. Or better, use numpy.
In Julia, we instead do:
table = zeros(2,3,4)
for k in 1:4
for j in 1:3
for i in 1:2
table[i,j,k] = i*j*k
end
end
end
If we need copies of arrays, use copy() or deepcopy().
In the following, b will be a reference to the memory locations of a, and adjusting b will therefore also adjust a.
a=[1,2,3]
b=copy(a)
b[2] = 42
Then a = [1, 42, 3]
Column vs row vector
a = [1, 2, 3] # column vector
a = [1 2 3] # row vector
No need for vectorised code
In Python, one reason why using numpy is so essential for doing fast computations is that normally most of the computation time is spent calling underlying compiled routines rather than doing the actual computation. If we write vectorized code, numpy does this more efficiently by processing in batches and optimizing the amount of calls etc.
In Julia nothing like this happens. Julia itself is very close to the speed of C apparently, and array-like operations are writtin in Julia itself (meta-programming).
Array broadcasting
Operations/functions that only take a single value are not defined on matrices/arrays/etc. We can of course loop over matrix elements to apply the function to each element, but there is also a handy broadcasting syntax for this.
We can use a dot either before an operation or before the brackets of a function call to apply the function to individual elements.
a = [1, 2, 3]
sin.(a)
This returns an array with [sin(1), sin(2), sin(3)]
Some operations work both on whole matrices as well on elements; in this case you can use the broadcasting syntax to distinguish these two scenarios.
Not having to write vectorized code makes the code more readable.
The broadcasting syntax largely replaces the map function.
(Named) Tuples
Tuples are just as intuitive in Python, allowing multiple returns and unpacking.
They are also immutable and may contain different datatypes.
Just like in Python, you can also initialize a tuple as such a = 1, 2, 3.
In Python you unpack a tuple with a star expression, e.g. add(*a).
In Julia, you "splat" tuples as such add(a...)
Unlike in Python, you can have named tuples with a special syntax for accessing elements.
namedTup1 = (a=1, b="hello")
namedTup2 = NamedTuple{(:a, :b)}(2,"hello2")
# Accessing named tuple elements
namedTup1[:a]
namedTup2[:b]
Dictionaries
Unordered by default.
Indexing of dictionaries is the same as in Python, but for creation we have a new syntax:
person3 = Dict("Name" => "Vittorio", "Phone" => 123456789, "Shoe-size" => 42)
addressBook["Vittorio"] = person3
TODO set highlight color to something I can read...