DEV Community

Ângelo Galvão
Ângelo Galvão

Posted on

Julia: Working with vectors and matrices — Part 1

Working with vector and matrices in Julia

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:

4-vector

To declare it in Julia type the following:

x = [ 3, 4, 0, -1]
Enter fullscreen mode Exit fullscreen mode

The result will be something like this:

4-element Vector{Int64}:
  3
  4
  0
 -1
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

The result is a zero 5-vector:

5-element Vector{Float64}:
 0.0
 0.0
 0.0
 0.0
 0.0
Enter fullscreen mode Exit fullscreen mode

Ones Vector

Just like the zero vector, to create a vector only containing ones, use the function ones():

o = ones(5)
Enter fullscreen mode Exit fullscreen mode

The result, as you might expect, is:

5-element Vector{Float64}:
 1.0
 1.0
 1.0
 1.0
 1.0
Enter fullscreen mode Exit fullscreen mode

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:
e₃ unit vector

# 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;
Enter fullscreen mode Exit fullscreen mode

The result:

5-element Vector{Float64}:
 0.0
 0.0
 1.0
 0.0
 0.0
Enter fullscreen mode Exit fullscreen mode

Stacked vectors

You can stack vectors one on top of another, like this:

# Notice that the separetor is ;  not ,
stacked = [ x; z; o ]
Enter fullscreen mode Exit fullscreen mode

The result is a new vector containing all the elements of the 3 vectors:

14-element Vector{Float64}:
  3.0
  ...
  0.0
  ...
  1.0
Enter fullscreen mode Exit fullscreen mode

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.

aTb=a1b1+a2b2++anbn a^Tb=a_1b_1 + a_2b_2 + \ldots + a_nb_n



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
Enter fullscreen mode Exit fullscreen mode

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?

x=x12+x22++xn2 \lVert x \rVert = \sqrt{x^2_1 + x^2_2 + \ldots + x^2_n}



The euclidian norm is also called 2-norm. Its formula can be represented like this also:

x2=x12+x22++xn2 \lVert x \rVert_2 = \sqrt{x^2_1 + x^2_2 + \ldots + x^2_n}



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)
Enter fullscreen mode Exit fullscreen mode

Infinity norm

There is also other types of norms. For example, the infinity norm return the absolute maximum element on the n-vector.

x=maxn=1,2,,nxi \lVert x \rVert_\infty = \max_{n=1, 2, \ldots, n}|x_i|
# The result will be 3
inf_norm = norm(a, Inf)
Enter fullscreen mode Exit fullscreen mode

1-norm

The 1-norm return the sum of the absolute values of its elements.

x1=n=1nxi \lVert x \rVert_1 = \sum_{n=1}^{n}|x_i|
# The result will be 6
one_norm = norm(a, 1)
Enter fullscreen mode Exit fullscreen mode

Distance

How about calculating the distance between two vectors? The mathematical formula point us to the solution:

dist(a,b)=ab dist(a, b) = \lVert a - b \rVert



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
Enter fullscreen mode Exit fullscreen mode

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:

μ=(1/n)Ta \mu = (\textbf{1}/n)^{T}a



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)
Enter fullscreen mode Exit fullscreen mode

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:

σ=xavg(x)1 \sigma = x - avg(x)\textbf{1}
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)
Enter fullscreen mode Exit fullscreen mode

Next matrices

This post became long, the next post in this series I will discuss about matrices in Julia. Stay on!

Top comments (0)