Engee documentation
Notebook

Basic matrix operations

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

Basic operations with matrices

First, 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 save the result in a new vector. In the Julia language, this is done using 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, row b. For visualization, you need to connect the Plots.jl library. And the input of the function plot() submit a column vector. To do this, we 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 a different type of chart. For example, let's display the values of vector c as a diagram. To do this, you must first call the function plotlyjs().

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

To create a rectangular matrix, you need to specify the lines through ;.

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

We transpose the matrix and write the result to 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 multiplication of matrices will not occur according to the "row per column" rule, but piecemeal.

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

Let's solve a simple equation of the following form

setting the in the form of 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

Library LinearAlgebra allows you to work with various matrix characteristics and matrix calculations. For example, let's find the eigenvalues of the matrix A. To do this, use the function eigvals().

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

We will find the determinant of a 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

You can also perform a singular decomposition of 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 operations with matrices from linear algebra can be found in the section Linear Algebra.