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.
Pkg.add(["LinearAlgebra"])
a = [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 .+.
b = a .+ 2
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 '.
c = b'
using Plots
plot(c)
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().
plotlyjs()
bar(c, xlabel="месяц",ylabel="количество")
To create a rectangular matrix, you need to specify the lines through ;.
A = [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.
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 multiplication of matrices will not occur according to the "row per column" rule, but piecemeal.
D = A .* B
Let's solve a simple equation of the following form
setting the in the form of a column vector.
b = [1,3,5]
#Решение уравнения
x = A\b
#Покажем, что Ax=b
r = A*x - b
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().
using LinearAlgebra
eigvals(A)
We will find the determinant of a given matrix by passing it as an input value to the function det().
M = [1 0; 2 2]
det(M)
You can also perform a singular decomposition of matrix A using the function svd().
svd(A)
More operations with matrices from linear algebra can be found in the section Linear Algebra.