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]:
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)
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:
- Decompose the matrix into components
U, s, VTusing the functionsvd(). - Create a null matrix
Sigmathe same size as the matrixA, and fill its main diagonal with vector elementsS. - Separate the 2 columns of the matrix
Sigmaand 2 rows of the matrixVto find the projection of a matrix into a reduced-dimensional space using the commandsT = 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]:
In [ ]:
# Приблизительное восстановление матрицы по сжатой информации
B = U * (Sigma * VT)
Out[0]: