This is the first post of a series in which I’ll talk about the Julia programming language and how to work with vectors and matrices.
Julia is a high-level, high-performance, dynamic programming language primarily designed for numerical, scientific, and technical computing. It is also open-sourced.
The language is simple and easy to learn, so let’s follow together and learn more about it. In this post, I will discuss the basics of working with vectors and matrices in Julia, but I will not discuss the very basic features, like addition, slicing, or scalar multiplication, because this is common in most programming languages, just use your common sense that it will be useful also work in Julia. I am interested in what makes Julia cool.
Vectors
A vector is an ordered finite list of numbers. It's numbers are member of a domain, like the ℝ (real numbers), or ℂ (complex numbers), or any other number domain.
So, given the following Integer vector:
To declare it in Julia type the following:
x = [ 3, 4, 0, -1]
The result will be something like this:
4-element Vector{Int64}:
3
4
0
-1
Special Vectors
Zero Vector
You can create special vectors in Julia, for example, if you want to create a vector only containing zeros, use the function zeros():
z = zeros(5)
The result is a zero 5-vector:
5-element Vector{Float64}:
0.0
0.0
0.0
0.0
0.0
Ones Vector
Just like the zero vector, to create a vector only containing ones, use the function ones():
o = ones(5)
The result, as you might expect, is:
5-element Vector{Float64}:
1.0
1.0
1.0
1.0
1.0
Unit Vector
Unit vector is a vector which one element is 1 and the others are 0. Unit vectors are used in multiple operations in linear algebra and usually are described by eᵢₜₕ, where ith means the index of the number 1 in the n-vector. For example, a 5-vector where the 1 index is 3, conventionally called e₃, is represented like:

# Create a zero matrix, the change the index position to 1
# To declare e₃, just type e\_3 and hit tab.
e₃ = zeros(5); e₃[3] = 1;
The result:
5-element Vector{Float64}:
0.0
0.0
1.0
0.0
0.0
Stacked vectors
You can stack vectors one on top of another, like this:
# Notice that the separetor is ; not ,
stacked = [ x; z; o ]
The result is a new vector containing all the elements of the 3 vectors:
14-element Vector{Float64}:
3.0
...
0.0
...
1.0
Vector operations
Inner product (dot product)
If you remember linear algebra class, the inner product of two vector an and b is a transpose times b.
In Julia, it's implemented like this:
a = [ 1, 2, 3]
b = [4, -5, -6]
# The result of the inner product is -24
# The result variable is aᵀb. In Julia, you can define a variable in various ways.
aᵀb = a'b
You may wondering how I define the variable like that. It is simple! Just type a^T and hit tab. Julia support LaTeX expressions as variable names :-)
Vector norm
The n-vector norm, or Euclidian norm, is the square root of the sum of the squares of its elements — remember Pythagoras theorem?
The euclidian norm is also called 2-norm. Its formula can be represented like this also:
Euclidian norm is the same as 2-norm.
Here is how to implement it in Julia:
# The result will be 3.7416573867739413
two_norm = norm(a) # or norm(a,2)
Infinity norm
There is also other types of norms. For example, the infinity norm return the absolute maximum element on the n-vector.
# The result will be 3
inf_norm = norm(a, Inf)
1-norm
The 1-norm return the sum of the absolute values of its elements.
# The result will be 6
one_norm = norm(a, 1)
Distance
How about calculating the distance between two vectors? The mathematical formula point us to the solution:
So, in Julia, to calculate the distances of two vectors, run the following:
# the result will be 11.789826122551595
distance = norm(a - b) # or norm(b - a), it doesn't matter
Average (or mean)
You can calculate the mean of the vector elements by using the Statistics module. Here is the average formula (where n is size/length and 1 is a ones vector of the same size of n) of the vector:
Here is an example on how to implement in Julia:
using Statistics
# the result is 2.0
# to declare μ, just type \mu and hit the tab key
μ = mean(a)
Standard deviation
Last but not least, the standard deviation is obtained by subtracting from each entry of the vector the mean value of the entries. Confusing? Here is the math formula:
using Statistics # don't need to repeat if already declared
# the result is 1.0
# to declare σ, just type \sigma and hit the tab key
σ = std(a)
Next matrices
This post became long, the next post in this series I will discuss about matrices in Julia. Stay on!


Top comments (0)