Skip to main content

Section 5.3 Linear Algebra in Julia

As we saw in the previous section, creating and manipulating matrices is quite easy and natural in Julia. This section will show how to perform a number of basic Linear Algebra tasks.

Subsection 5.3.1 Basic Matrix Operations in Julia

Many of the basic operations from Linear Algebra are natural in julia. Let’s say that we have
A = [ 1 2 3 4; 5 6 7 8; 9 10 11 12]
B=[i+j for i=1:3, j=1:4]
C=[(-1)^(i+j) for i=1:3, j=1:3]
Then A+B returns
3Γ—4 Matrix{Int64}:
 3   5   7   9
 8  10  12  14
13  15  17  19
and A-B returns
3Γ—4 Matrix{Int64}:
-1  -1  -1  -1
 2   2   2   2
 5   5   5   5
which perform matrix addition and subtraction. 4A returns
3Γ—4 Matrix{Int64}:
 4   8  12  16
20  24  28  32
36  40  44  48
is scalar multiplication and note again that if a variable is left-multiplied by a constant then the multiplication operator * is optional.
An example of matrix multiplication is C*A resulting in
3Γ—4 Matrix{Int64}:
 5   6   7   8
-5  -6  -7  -8
 5   6   7   8
and if you multiply incompatible matrices like A*B you will get the error:
DimensionMismatch: incompatible dimensions for matrix multiplication: tried to multiply a matrix of size (3, 4) with a matrix of size (3, 4). The second dimension of the first matrix: 4, does not match the first dimension of the second matrix: 3. 
which is perhaps the most detailed matrix incompatibility description in most software.

Subsection 5.3.2 Other Matrix Operations in Julia

Some other important matrix operations include the determinant, the transpose, ...
Let’s say that we have defined
A = [2i+3j for i=1:3, j=1:3]
then the determinant of this can be found with det(A) returning -1.0658141036401493e-14.
 1 
Note that even though the matrix A is an integer matrix, then the determinant is a floating-point number although the result will be an integer. This has to do with the way that the algorithm uses floating points in the calculation.
The transpose of the matrix can be found with transpose as in transpose(A) which returns.
3Γ—3 transpose(::Matrix{Int64}) with eltype Int64:
  5   7   9
  8  10  12
 11  13  15
This actually performs what is called a lazy transpose, which just flags the matrix as a transpose without flipping the elements due to speed.
The related operation of the adjoint of the matrix combines the transpose and the complex conjugate. If the matrix is real, the adjoint and transpose are identical and is often used because the operation ' is used. For example, A' returns
3Γ—3 adjoint(::Matrix{Int64}) with eltype Int64:
 5   7   9
 8  10  12
11  13  15

Subsection 5.3.3 Matrix Inverse in Julia

The inverse of a matrix can be found with the inv function. If we have a matrix A defined as
A = [3 2 1; 1 1 1; 3 1 1]
then the inverse can be found with inv(A) which returns
3Γ—3 Matrix{Float64}:
 -6.4763e-17  -0.5   0.5
 1.0           0.0  -1.0
-1.0           1.5   0.5
where the upper left element is very close to zero due to numerical errors. Note that the inverse is a floating-point matrix even though the original matrix is an integer matrix. This is because the inverse is calculated using floating-point arithmetic.
It is often nice to have the inverse in terms of rational numbers. To accomplish this, we need to rationalize the matrix A. This can be one with rationalize.(A) and if the inverse of that is found with inv(rationalize.(A)) resulting in
3Γ—3 Matrix{Rational{Int64}}:
  0  -1//2  1//2
  1    0    -1
 -1   3//2  1//2
Which is the rational version of the one above, but notice that the numerical error in the upper left element is now gone.