Основные матричные операции¶
Этот пример показывает основные методы и функции для работы с матрицами в Engee.
Базовые операции с матрицами¶
Во-первых зададим вектор из 9 элементов.
Out[0]:
1×9 Matrix{Int64}:
1 2 3 4 6 4 3 9 10
Увеличим значение каждого элемента на 2 и сохраним результат в новом векторе. На языке Julia это делается с помощью оператора .+
.
Out[0]:
1×9 Matrix{Int64}:
3 4 5 6 8 6 5 11 12
Построим график результирующего вектора-строки b. Для визуализации необходимо подключить библиотеку Plots.jl. И на вход функции plot()
подать вектор-столбец. Для этого транспонируем b с помощью оператора '
.
Out[0]:
9×1 adjoint(::Matrix{Int64}) with eltype Int64:
3
4
5
6
8
6
5
11
12
Также можем сделать другой тип графика. Например, отобразим значения вектора с в виде диаграммы. Для этого нужно сначала вызвать функцию plotlyjs()
.
Чтобы создать прямоугольную матрицу, необходимо указать строки через ;
.
Out[0]:
3×3 Matrix{Int64}:
1 2 0
2 5 -1
4 10 -1
Транспонируем матрицу и результат запишем в другую переменную, например B. Перемножим матрицу A с получившейся матрицей B.
Out[0]:
3×3 adjoint(::Matrix{Int64}) with eltype Int64:
1 2 4
2 5 10
0 -1 -1
Out[0]:
3×3 Matrix{Int64}:
5 12 24
12 30 59
24 59 117
Но также мы можем умножить каждый элемент матрицы А на каждый элемент матрицы В, как мы это делали с векторами ранее. То есть перемножение матриц будет происходить не по правилу "строка на столбец", а поэлементно.
Out[0]:
3×3 Matrix{Int64}:
1 4 0
4 25 -10
0 -10 1
Давайте решим простое уравнение следующего вида
$$
A*x=b
$$
зададим $b$ в виде вектора-столбца.
Out[0]:
3-element Vector{Int64}:
1
3
5
Out[0]:
3-element Vector{Float64}:
1.0
-0.0
-1.0
Out[0]:
3-element Vector{Float64}:
0.0
0.0
0.0
Матричные операции из раздела линейной алгебры¶
Библиотека LinearAlgebra
позволяет работать различными характеристиками матриц и матричными вычислениями. Например, найдем собственные значения матрицы А. Для этого применим функцию eigvals()
.
Out[0]:
3-element Vector{Float64}:
0.267949192431123
0.9999999999999999
3.732050807568875
Найдем определитель заданной матрцы, передав ее в качестве входного значения в функцию det()
.
Также можно выполнить сингулярную декомпозицию матрицы А, с помощью функции svd()
.
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
Больше операций с матрицами из линейной алгебры можно найти в разделе Линейная алгебра.
{"id": "48c15860-736b-469d-8415-1bc38b8405b4", "data": [{"showlegend": true, "mode": "lines", "xaxis": "x", "colorbar": {"title": {"text": ""}}, "name": "y1", "zmin": null, "yaxis": "y", "legendgroup": "y1", "zmax": null, "line": {"color": "rgba(0, 154, 250, 1.000)", "shape": "linear", "dash": "solid", "width": 1}, "y": [3, 4, 5, 6, 8, 6, 5, 11, 12], "type": "scatter", "x": [1, 2, 3, 4, 5, 6, 7, 8, 9], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 9, "currentCount": 9}}}], "config": {"showlegend": true, "xaxis": {"showticklabels": true, "gridwidth": 0.5, "range": [0.7599999999999998, 9.24], "domain": [0.03619130941965587, 0.9934383202099738], "mirror": false, "tickangle": 0, "showline": true, "zeroline": false, "tickfont": {"color": "rgba(0, 0, 0, 1)", "family": "sans-serif", "size": 11}, "zerolinecolor": "rgba(0, 0, 0, 1)", "anchor": "y", "visible": true, "ticks": "inside", "tickmode": "array", "linecolor": "rgba(0, 0, 0, 1)", "showgrid": true, "title": {"text": "", "font": {"color": "rgba(0, 0, 0, 1)", "family": "sans-serif", "size": 15}}, "gridcolor": "rgba(0, 0, 0, 0.1)", "tickcolor": "rgb(0, 0, 0)", "type": "linear"}, "paper_bgcolor": "rgba(255, 255, 255, 1.000)", "annotations": [], "height": 400, "margin": {"l": 0, "b": 20, "r": 0, "t": 20}, "plot_bgcolor": "rgba(255, 255, 255, 1.000)", "yaxis": {"showticklabels": true, "gridwidth": 0.5, "range": [2.7299999999999995, 12.27], "domain": [0.03762029746281716, 0.9901574803149606], "mirror": false, "tickangle": 0, "showline": true, "zeroline": false, "tickfont": {"color": "rgba(0, 0, 0, 1)", "family": "sans-serif", "size": 11}, "zerolinecolor": "rgba(0, 0, 0, 1)", "anchor": "x", "visible": true, "ticks": "inside", "tickmode": "array", "linecolor": "rgba(0, 0, 0, 1)", "showgrid": true, "title": {"text": "", "font": {"color": "rgba(0, 0, 0, 1)", "family": "sans-serif", "size": 15}}, "gridcolor": "rgba(0, 0, 0, 0.1)", "tickcolor": "rgb(0, 0, 0)", "type": "linear"}, "legend": {"yanchor": "auto", "xanchor": "auto", "bordercolor": "rgba(0, 0, 0, 1)", "bgcolor": "rgba(255, 255, 255, 1.000)", "borderwidth": 1, "tracegroupgap": 0, "y": 1, "font": {"color": "rgba(0, 0, 0, 1)", "family": "sans-serif", "size": 11}, "title": {"font": {"color": "rgba(0, 0, 0, 1)", "family": "sans-serif", "size": 15}, "text": ""}, "traceorder": "normal", "x": 1}, "width": 1515.828125}}
{"id": "5606a0b1-0fec-45bc-985a-c3a03f8bacd5", "data": [{"showlegend": true, "mode": "lines", "xaxis": "x", "fillcolor": "rgba(0, 154, 250, 1.000)", "name": "y1", "fill": "tozeroy", "yaxis": "y", "legendgroup": "y1", "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "y": [3, 0, 0, 3, 3, 3], "type": "scatter", "x": [0.6, 0.6, 1.4, 1.4, 0.6, 0.6], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 6, "currentCount": 6}}}, {"showlegend": false, "mode": "lines", "xaxis": "x", "fillcolor": "rgba(0, 154, 250, 1.000)", "name": "y1", "fill": "tozeroy", "yaxis": "y", "legendgroup": "y1", "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "y": [4, 0, 0, 4, 4, 4], "type": "scatter", "x": [1.6, 1.6, 2.4, 2.4, 1.6, 1.6], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 6, "currentCount": 6}}}, {"showlegend": false, "mode": "lines", "xaxis": "x", "fillcolor": "rgba(0, 154, 250, 1.000)", "name": "y1", "fill": "tozeroy", "yaxis": "y", "legendgroup": "y1", "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "y": [5, 0, 0, 5, 5, 5], "type": "scatter", "x": [2.6, 2.6, 3.4, 3.4, 2.6, 2.6], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 6, "currentCount": 6}}}, {"showlegend": false, "mode": "lines", "xaxis": "x", "fillcolor": "rgba(0, 154, 250, 1.000)", "name": "y1", "fill": "tozeroy", "yaxis": "y", "legendgroup": "y1", "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "y": [6, 0, 0, 6, 6, 6], "type": "scatter", "x": [3.6, 3.6, 4.4, 4.4, 3.6, 3.6], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 6, "currentCount": 6}}}, {"showlegend": false, "mode": "lines", "xaxis": "x", "fillcolor": "rgba(0, 154, 250, 1.000)", "name": "y1", "fill": "tozeroy", "yaxis": "y", "legendgroup": "y1", "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "y": [8, 0, 0, 8, 8, 8], "type": "scatter", "x": [4.6, 4.6, 5.4, 5.4, 4.6, 4.6], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 6, "currentCount": 6}}}, {"showlegend": false, "mode": "lines", "xaxis": "x", "fillcolor": "rgba(0, 154, 250, 1.000)", "name": "y1", "fill": "tozeroy", "yaxis": "y", "legendgroup": "y1", "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "y": [6, 0, 0, 6, 6, 6], "type": "scatter", "x": [5.6, 5.6, 6.4, 6.4, 5.6, 5.6], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 6, "currentCount": 6}}}, {"showlegend": false, "mode": "lines", "xaxis": "x", "fillcolor": "rgba(0, 154, 250, 1.000)", "name": "y1", "fill": "tozeroy", "yaxis": "y", "legendgroup": "y1", "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "y": [5, 0, 0, 5, 5, 5], "type": "scatter", "x": [6.6, 6.6, 7.4, 7.4, 6.6, 6.6], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 6, "currentCount": 6}}}, {"showlegend": false, "mode": "lines", "xaxis": "x", "fillcolor": "rgba(0, 154, 250, 1.000)", "name": "y1", "fill": "tozeroy", "yaxis": "y", "legendgroup": "y1", "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "y": [11, 0, 0, 11, 11, 11], "type": "scatter", "x": [7.6, 7.6, 8.4, 8.4, 7.6, 7.6], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 6, "currentCount": 6}}}, {"showlegend": false, "mode": "lines", "xaxis": "x", "fillcolor": "rgba(0, 154, 250, 1.000)", "name": "y1", "fill": "tozeroy", "yaxis": "y", "legendgroup": "y1", "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "y": [12, 0, 0, 12, 12, 12], "type": "scatter", "x": [8.6, 8.6, 9.4, 9.4, 8.6, 8.6], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 6, "currentCount": 6}}}, {"showlegend": false, "mode": "markers", "xaxis": "x", "colorbar": {"title": {"text": ""}}, "name": "y1", "zmin": null, "yaxis": "y", "legendgroup": "y1", "marker": {"symbol": "circle", "color": "rgba(0, 154, 250, 0.000)", "line": {"color": "rgba(0, 0, 0, 0)", "width": 1}, "size": 0}, "zmax": null, "y": [3, 4, 5, 6, 8, 6, 5, 11, 12], "type": "scatter", "x": [1, 2, 3, 4, 5, 6, 7, 8, 9], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 9, "currentCount": 9}}}], "config": {"showlegend": true, "xaxis": {"showticklabels": true, "gridwidth": 0.5, "range": [0.05615999999999843, 9.943840000000002], "domain": [0.09128390201224845, 0.9934383202099738], "mirror": false, "tickangle": 0, "showline": true, "zeroline": false, "tickfont": {"color": "rgba(0, 0, 0, 1)", "family": "sans-serif", "size": 11}, "zerolinecolor": "rgba(0, 0, 0, 1)", "anchor": "y", "visible": true, "ticks": "inside", "tickmode": "array", "linecolor": "rgba(0, 0, 0, 1)", "showgrid": true, "title": {"text": "месяц", "font": {"color": "rgba(0, 0, 0, 1)", "family": "sans-serif", "size": 15}}, "gridcolor": "rgba(0, 0, 0, 0.1)", "tickcolor": "rgb(0, 0, 0)", "type": "linear"}, "paper_bgcolor": "rgba(255, 255, 255, 1.000)", "annotations": [], "height": 400, "margin": {"l": 0, "b": 20, "r": 0, "t": 20}, "plot_bgcolor": "rgba(255, 255, 255, 1.000)", "yaxis": {"showticklabels": true, "gridwidth": 0.5, "range": [-0.3600000000000003, 12.36], "domain": [0.07581474190726165, 0.9901574803149606], "mirror": false, "tickangle": 0, "showline": true, "zeroline": false, "tickfont": {"color": "rgba(0, 0, 0, 1)", "family": "sans-serif", "size": 11}, "zerolinecolor": "rgba(0, 0, 0, 1)", "anchor": "x", "visible": true, "ticks": "inside", "tickmode": "array", "linecolor": "rgba(0, 0, 0, 1)", "showgrid": true, "title": {"text": "количество", "font": {"color": "rgba(0, 0, 0, 1)", "family": "sans-serif", "size": 15}}, "gridcolor": "rgba(0, 0, 0, 0.1)", "tickcolor": "rgb(0, 0, 0)", "type": "linear"}, "legend": {"yanchor": "auto", "xanchor": "auto", "bordercolor": "rgba(0, 0, 0, 1)", "bgcolor": "rgba(255, 255, 255, 1.000)", "borderwidth": 1, "tracegroupgap": 0, "y": 1, "font": {"color": "rgba(0, 0, 0, 1)", "family": "sans-serif", "size": 11}, "title": {"font": {"color": "rgba(0, 0, 0, 1)", "family": "sans-serif", "size": 15}, "text": ""}, "traceorder": "normal", "x": 1}, "width": 1515.828125}}