Engee documentation
Notebook

Linear algebra

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

First, please run the preparation cell with the code.

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

Finding the determinant of the matrix

Find the determinant of the matrix

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 rotating around the Z axis by an angle .

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 dimension of the matrix through factorization

A matrix of features of three objects is given:

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

  1. Decompose the matrix into components U, s, VT using the function svd().
  2. Create a null matrix Sigma the same size as the matrix A, and fill its main diagonal with vector elements S.
  3. Separate the 2 columns of the matrix Sigma and 2 rows of the matrix V to find the projection of a matrix into a reduced-dimensional 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