Engee documentation
Notebook

Linear algebra

In this example we will show how to solve simple linear algebra problems such as finding the determinant, matrix multiplication and SVG factorisation.

First of all, please run the preparatory code cell.

In [ ]:
Pkg.add(["LinearAlgebra", "Rotations"])
In [ ]:
using LinearAlgebra;
using Rotations;
using Plots;
plotlyjs();

Finding the determinant of a matrix

Find the determinant of the matrix $\left [ \begin{array}{cc}1 & 2 & 3 \\4 & 1 & 6 \\ 7 & 8 & 1 \end{array} \right ]$

In [ ]:
A = [1 2 3; 4 1 6; 7 8 1]
det(A)
Out[0]:
104.0

Matrix multiplication

Find the coordinates of the point (1,2,0) when rotated about the Z axis by the angle $90^{\circ}$.

In [ ]:
X = [1, 2, 0]
scatter([X[1]], [X[2]], [X[3]], framestyle = :zerolines, legend=false, aspect_ratio = 1)
Out[0]:
In [ ]:
R_euler = RotXYZ(0,0,90*pi/180);
Y = R_euler * X
scatter!([Y[1]], [Y[2]], [Y[3]], framestyle = :zerolines, legend=false, aspect_ratio = 1)
Out[0]:
In [ ]:
print(Y)
[-2.0, 1.0000000000000002, 0.0]

Reducing the dimensionality of a matrix through factorisation

A feature matrix of three objects is given:

$A = \left [ \begin{array}{cc}1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10\\ 11 & 12 & 13 & 14 & 15 & 16 & 17 & 18 & 19 & 20\\ 21 & 22 & 23 & 24 & 25 & 26 & 27 & 28 & 29 & 30 \end{array} \right ]$

The columns contain the features of the objects, but they contain redundant information. Perform dimensionality reduction to two variables using SVD and additional operations. Specific steps of the algorithm:

  1. Decompose the matrix into components U, s, VT using the function svd().
  2. Create a null matrix Sigma of the same size as the matrix A, and fill its main diagonal with the elements of the vector S.
  3. Separate 2 columns of the matrix Sigma and 2 rows of the matrix V to find the projection of the matrix into the reduced dimension space using the commands T = U * Sigma.
In [ ]:
A = [ 1 2 3 4 5 6 7 8 9 10;
      11 12 13 14 15 16 17 18 19 20;
      21 22 23 24 25 26 27 28 29 30 ];

U, s, VT = svd(A);
In [ ]:
# Создадим матрицу Sigma
Sigma = zeros(size(A,1), size(A,2));
Sigma[1:size(A,1), 1:size(A,1)] = diagm(s);

# Выберем только 2 признака для описания
n_elements = 2;
Sigma = Sigma[:, 1:n_elements];
VT = VT[1:n_elements, :];

# Находим проекцию матрицы в пространство уменьшенной размерности
T = U * Sigma
Out[0]:
3×2 Matrix{Float64}:
 -18.5216   6.47697
 -49.8131   1.91182
 -81.1046  -2.65333
In [ ]:
# Приблизительное восстановление матрицы по сжатой информации
B = U * (Sigma * VT)
Out[0]:
3×3 Matrix{Float64}:
  2.80454   7.16027  12.4563
 11.5326   25.8784   22.6968
 20.2608   44.5964   32.9373