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.
Pkg.add(["LinearAlgebra"])
a = [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 .+
.
b = a .+ 2
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 '
.
c = b'
using Plots
plot(c)
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()
.
plotlyjs()
bar(c, xlabel="месяц",ylabel="количество")
To create a rectangular matrix, you must specify the rows via ;
.
A = [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.
B = A'
C = A * B
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.
D = A .* B
Let us solve a simple equation of the following form
set as a column vector.
b = [1,3,5]
#Решение уравнения
x = A\b
#Покажем, что Ax=b
r = A*x - b
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()
.
using LinearAlgebra
eigvals(A)
Let us find the determinant of the given matrix by passing it as an input value to the function det()
.
M = [1 0; 2 2]
det(M)
It is also possible to perform singular decomposition of the matrix A, using the function svd()
.
svd(A)
More matrix operations from linear algebra can be found in Linear Algebra.