Engee documentation
Notebook

Basic matrix operations

This example shows the basic methods and functions for working with matrices in Engee.

Basic matrix operations

Firstly, let's define a vector of 9 elements.

In [ ]:
Pkg.add(["LinearAlgebra"])
In [ ]:
a = [1 2 3 4 6 4 3 9 10]
Out[0]:
1×9 Matrix{Int64}:
 1  2  3  4  6  4  3  9  10

Increase the value of each element by 2 and store the result in a new vector. In Julia, this is done with the operator .+.

In [ ]:
b = a .+ 2
Out[0]:
1×9 Matrix{Int64}:
 3  4  5  6  8  6  5  11  12

Let's plot the resulting vector-string b. For visualisation it is necessary to connect the library Plots.jl. And feed the column vector to the input of the function plot(). To do this, transpose b using the operator '.

In [ ]:
c = b'
Out[0]:
9×1 adjoint(::Matrix{Int64}) with eltype Int64:
  3
  4
  5
  6
  8
  6
  5
 11
 12
In [ ]:
using Plots

plot(c)
Out[0]:

We can also make another type of graph. For example, let's display the values of vector c in the form of a diagram. To do this, first call the function plotlyjs().

In [ ]:
plotlyjs()
bar(c, xlabel="месяц",ylabel="количество")
Out[0]:

To create a rectangular matrix, you must specify the rows via ;.

In [ ]:
A = [1 2 0; 2 5 -1; 4 10 -1]
Out[0]:
3×3 Matrix{Int64}:
 1   2   0
 2   5  -1
 4  10  -1

Transpose the matrix and write the result into another variable, for example B. Multiply matrix A with the resulting matrix B.

In [ ]:
B = A'
Out[0]:
3×3 adjoint(::Matrix{Int64}) with eltype Int64:
 1   2   4
 2   5  10
 0  -1  -1
In [ ]:
C = A * B
Out[0]:
3×3 Matrix{Int64}:
  5  12   24
 12  30   59
 24  59  117

But we can also multiply each element of matrix A by each element of matrix B, as we did with vectors earlier. That is, the matrices will be multiplied not by the rule "row to column", but element by element.

In [ ]:
D = A .* B
Out[0]:
3×3 Matrix{Int64}:
 1    4    0
 4   25  -10
 0  -10    1

Let us solve a simple equation of the following form $$ A*x=b $$ set $b$ as a column vector.

In [ ]:
b = [1,3,5]
Out[0]:
3-element Vector{Int64}:
 1
 3
 5
In [ ]:
#Решение уравнения
x = A\b
Out[0]:
3-element Vector{Float64}:
  1.0
 -0.0
 -1.0
In [ ]:
#Покажем, что Ax=b
r = A*x - b
Out[0]:
3-element Vector{Float64}:
 0.0
 0.0
 0.0

Matrix operations from the linear algebra section

The library LinearAlgebra allows you to work with various matrix characterisations and matrix calculations. For example, let's find the eigenvalues of matrix A. To do this, we apply the function eigvals().

In [ ]:
using LinearAlgebra
eigvals(A)
Out[0]:
3-element Vector{Float64}:
 0.267949192431123
 0.9999999999999999
 3.732050807568875

Let us find the determinant of the given matrix by passing it as an input value to the function det().

In [ ]:
M = [1 0; 2 2]
det(M)
Out[0]:
2.0

It is also possible to perform singular decomposition of the matrix A, using the function svd().

In [ ]:
svd(A)
Out[0]:
SVD{Float64, Float64, Matrix{Float64}, Vector{Float64}}
U factor:
3×3 Matrix{Float64}:
 -0.179911  -0.521682   0.833954
 -0.443397   0.799785   0.404652
 -0.878084  -0.296971  -0.375203
singular values:
3-element Vector{Float64}:
 12.317062005786141
  0.5148992632302947
  0.15767781817449114
Vt factor:
3×3 Matrix{Float64}:
 -0.371764  -0.922107    0.107289
 -0.213631  -0.0274913  -0.976527
  0.903412  -0.385958   -0.18677

More matrix operations from linear algebra can be found in Linear Algebra.