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
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 rotated about the Z axis by the 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 dimensionality of a matrix through factorisation
A feature matrix of three objects is given:
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:
- Decompose the matrix into components
U, s, VT
using the functionsvd()
. - Create a null matrix
Sigma
of the same size as the matrixA
, and fill its main diagonal with the elements of the vectorS
. - Separate 2 columns of the matrix
Sigma
and 2 rows of the matrixV
to find the projection of the matrix into the reduced dimension 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]: