基本矩阵运算¶
本例展示了在 Engee 中处理矩阵的基本方法和函数。
基本矩阵操作¶
首先,让我们定义一个包含 9 个元素的向量。
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
将每个元素的值增加 2,并将结果存储到一个新的向量中。在 Julia 中,可以通过运算符.+
来实现。
In [ ]:
b = a .+ 2
Out[0]:
1×9 Matrix{Int64}: 3 4 5 6 8 6 5 11 12
让我们绘制得到的向量字符串 b。为了实现可视化,有必要连接 Plots.jl 库。并将列向量输入函数plot()
的输入端。为此,使用运算符'
对 b 进行转置。
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]:
我们还可以制作另一种图表。例如,让我们以图表的形式显示向量 c 的值。为此,首先调用函数plotlyjs()
。
In [ ]:
plotlyjs()
bar(c, xlabel="месяц",ylabel="количество")
Out[0]:
要创建矩形矩阵,必须通过;
指定行。
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
将矩阵平移,并将结果写入另一个变量,例如 B。将矩阵 A 与结果矩阵 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
但我们也可以将矩阵 A 的每个元素与矩阵 B 的每个元素相乘,就像前面处理向量那样。也就是说,矩阵的乘法规则不是 "行对列",而是元素对元素。
In [ ]:
D = A .* B
Out[0]:
3×3 Matrix{Int64}: 1 4 0 4 25 -10 0 -10 1
让我们求解一个如下形式的简单方程 $$ A*x=b $$ 设$b$ 为列向量。
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
线性代数部分的矩阵运算¶
通过LinearAlgebra
库,您可以处理各种矩阵特征和矩阵计算。例如,让我们找出矩阵 A 的特征值。为此,我们应用函数eigvals()
。
In [ ]:
using LinearAlgebra
eigvals(A)
Out[0]:
3-element Vector{Float64}: 0.267949192431123 0.9999999999999999 3.732050807568875
将给定矩阵作为输入值传递给函数det()
,即可求出矩阵的行列式。
In [ ]:
M = [1 0; 2 2]
det(M)
Out[0]:
2.0
还可以使用函数svd()
对矩阵 A 进行奇异分解。
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
更多线性代数中的矩阵运算可参阅 Linear Algebra。
{"id": "48c15860-736b-469d-8415-1bc38b8405b4", "data": [{"xaxis": "x", "mode": "lines", "x": [1, 2, 3, 4, 5, 6, 7, 8, 9], "showlegend": true, "name": "y1", "colorbar": {"title": {"text": ""}}, "zmin": null, "z": null, "legendgroup": "y1", "y": [3, 4, 5, 6, 8, 6, 5, 11, 12], "zmax": null, "line": {"shape": "linear", "color": "rgba(0, 154, 250, 1.000)", "dash": "solid", "width": 1}, "zaxis": null, "yaxis": "y", "metadata": {"smartZoomParams": {"maxCount": 9, "minCount": 25000, "currentCount": 9}, "shouldEnableSmartZoom": false}, "type": "scatter"}], "config": {"showlegend": true, "height": 400, "xaxis": {"showline": true, "showticklabels": true, "gridcolor": "rgba(0, 0, 0, 0.1)", "tickfont": {"family": "sans-serif", "color": "rgba(0, 0, 0, 1)", "size": 11}, "tickmode": "array", "anchor": "y", "range": [0.7599999999999998, 9.24], "tickcolor": "rgb(0, 0, 0)", "gridwidth": 0.5, "visible": true, "showgrid": true, "linecolor": "rgba(0, 0, 0, 1)", "zerolinecolor": "rgba(0, 0, 0, 1)", "tickangle": 0, "zeroline": false, "mirror": false, "domain": [0.03619130941965587, 0.9934383202099738], "title": {"font": {"family": "sans-serif", "color": "rgba(0, 0, 0, 1)", "size": 15}, "text": ""}, "ticks": "inside", "type": "linear"}, "annotations": [], "plot_bgcolor": "rgba(255, 255, 255, 1.000)", "paper_bgcolor": "rgba(255, 255, 255, 1.000)", "margin": {"t": 20, "b": 20, "r": 0, "l": 0}, "yaxis": {"showline": true, "showticklabels": true, "gridcolor": "rgba(0, 0, 0, 0.1)", "tickfont": {"family": "sans-serif", "color": "rgba(0, 0, 0, 1)", "size": 11}, "tickmode": "array", "anchor": "x", "range": [2.7299999999999995, 12.27], "tickcolor": "rgb(0, 0, 0)", "gridwidth": 0.5, "visible": true, "showgrid": true, "linecolor": "rgba(0, 0, 0, 1)", "zerolinecolor": "rgba(0, 0, 0, 1)", "tickangle": 0, "zeroline": false, "mirror": false, "domain": [0.03762029746281716, 0.9901574803149606], "title": {"font": {"family": "sans-serif", "color": "rgba(0, 0, 0, 1)", "size": 15}, "text": ""}, "ticks": "inside", "type": "linear"}, "width": 1515.828125, "legend": {"x": 1, "traceorder": "normal", "yanchor": "auto", "font": {"family": "sans-serif", "color": "rgba(0, 0, 0, 1)", "size": 11}, "borderwidth": 1, "bordercolor": "rgba(0, 0, 0, 1)", "y": 1, "bgcolor": "rgba(255, 255, 255, 1.000)", "tracegroupgap": 0, "title": {"font": {"family": "sans-serif", "color": "rgba(0, 0, 0, 1)", "size": 15}, "text": ""}, "xanchor": "auto"}}}
{"id": "5606a0b1-0fec-45bc-985a-c3a03f8bacd5", "data": [{"xaxis": "x", "mode": "lines", "x": [0.6, 0.6, 1.4, 1.4, 0.6, 0.6], "showlegend": true, "name": "y1", "fill": "tozeroy", "z": null, "legendgroup": "y1", "y": [3, 0, 0, 3, 3, 3], "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "zaxis": null, "fillcolor": "rgba(0, 154, 250, 1.000)", "yaxis": "y", "metadata": {"smartZoomParams": {"maxCount": 6, "minCount": 25000, "currentCount": 6}, "shouldEnableSmartZoom": false}, "type": "scatter"}, {"xaxis": "x", "mode": "lines", "x": [1.6, 1.6, 2.4, 2.4, 1.6, 1.6], "showlegend": false, "name": "y1", "fill": "tozeroy", "z": null, "legendgroup": "y1", "y": [4, 0, 0, 4, 4, 4], "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "zaxis": null, "fillcolor": "rgba(0, 154, 250, 1.000)", "yaxis": "y", "metadata": {"smartZoomParams": {"maxCount": 6, "minCount": 25000, "currentCount": 6}, "shouldEnableSmartZoom": false}, "type": "scatter"}, {"xaxis": "x", "mode": "lines", "x": [2.6, 2.6, 3.4, 3.4, 2.6, 2.6], "showlegend": false, "name": "y1", "fill": "tozeroy", "z": null, "legendgroup": "y1", "y": [5, 0, 0, 5, 5, 5], "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "zaxis": null, "fillcolor": "rgba(0, 154, 250, 1.000)", "yaxis": "y", "metadata": {"smartZoomParams": {"maxCount": 6, "minCount": 25000, "currentCount": 6}, "shouldEnableSmartZoom": false}, "type": "scatter"}, {"xaxis": "x", "mode": "lines", "x": [3.6, 3.6, 4.4, 4.4, 3.6, 3.6], "showlegend": false, "name": "y1", "fill": "tozeroy", "z": null, "legendgroup": "y1", "y": [6, 0, 0, 6, 6, 6], "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "zaxis": null, "fillcolor": "rgba(0, 154, 250, 1.000)", "yaxis": "y", "metadata": {"smartZoomParams": {"maxCount": 6, "minCount": 25000, "currentCount": 6}, "shouldEnableSmartZoom": false}, "type": "scatter"}, {"xaxis": "x", "mode": "lines", "x": [4.6, 4.6, 5.4, 5.4, 4.6, 4.6], "showlegend": false, "name": "y1", "fill": "tozeroy", "z": null, "legendgroup": "y1", "y": [8, 0, 0, 8, 8, 8], "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "zaxis": null, "fillcolor": "rgba(0, 154, 250, 1.000)", "yaxis": "y", "metadata": {"smartZoomParams": {"maxCount": 6, "minCount": 25000, "currentCount": 6}, "shouldEnableSmartZoom": false}, "type": "scatter"}, {"xaxis": "x", "mode": "lines", "x": [5.6, 5.6, 6.4, 6.4, 5.6, 5.6], "showlegend": false, "name": "y1", "fill": "tozeroy", "z": null, "legendgroup": "y1", "y": [6, 0, 0, 6, 6, 6], "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "zaxis": null, "fillcolor": "rgba(0, 154, 250, 1.000)", "yaxis": "y", "metadata": {"smartZoomParams": {"maxCount": 6, "minCount": 25000, "currentCount": 6}, "shouldEnableSmartZoom": false}, "type": "scatter"}, {"xaxis": "x", "mode": "lines", "x": [6.6, 6.6, 7.4, 7.4, 6.6, 6.6], "showlegend": false, "name": "y1", "fill": "tozeroy", "z": null, "legendgroup": "y1", "y": [5, 0, 0, 5, 5, 5], "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "zaxis": null, "fillcolor": "rgba(0, 154, 250, 1.000)", "yaxis": "y", "metadata": {"smartZoomParams": {"maxCount": 6, "minCount": 25000, "currentCount": 6}, "shouldEnableSmartZoom": false}, "type": "scatter"}, {"xaxis": "x", "mode": "lines", "x": [7.6, 7.6, 8.4, 8.4, 7.6, 7.6], "showlegend": false, "name": "y1", "fill": "tozeroy", "z": null, "legendgroup": "y1", "y": [11, 0, 0, 11, 11, 11], "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "zaxis": null, "fillcolor": "rgba(0, 154, 250, 1.000)", "yaxis": "y", "metadata": {"smartZoomParams": {"maxCount": 6, "minCount": 25000, "currentCount": 6}, "shouldEnableSmartZoom": false}, "type": "scatter"}, {"xaxis": "x", "mode": "lines", "x": [8.6, 8.6, 9.4, 9.4, 8.6, 8.6], "showlegend": false, "name": "y1", "fill": "tozeroy", "z": null, "legendgroup": "y1", "y": [12, 0, 0, 12, 12, 12], "line": {"color": "rgba(0, 0, 0, 1)", "dash": "solid", "width": 1}, "zaxis": null, "fillcolor": "rgba(0, 154, 250, 1.000)", "yaxis": "y", "metadata": {"smartZoomParams": {"maxCount": 6, "minCount": 25000, "currentCount": 6}, "shouldEnableSmartZoom": false}, "type": "scatter"}, {"xaxis": "x", "mode": "markers", "x": [1, 2, 3, 4, 5, 6, 7, 8, 9], "showlegend": false, "name": "y1", "colorbar": {"title": {"text": ""}}, "zmin": null, "z": null, "legendgroup": "y1", "y": [3, 4, 5, 6, 8, 6, 5, 11, 12], "zmax": null, "zaxis": null, "yaxis": "y", "marker": {"line": {"color": "rgba(0, 0, 0, 0)", "width": 1}, "symbol": "circle", "color": "rgba(0, 154, 250, 0.000)", "size": 0}, "metadata": {"smartZoomParams": {"maxCount": 9, "minCount": 25000, "currentCount": 9}, "shouldEnableSmartZoom": false}, "type": "scatter"}], "config": {"showlegend": true, "height": 400, "xaxis": {"showline": true, "showticklabels": true, "gridcolor": "rgba(0, 0, 0, 0.1)", "tickfont": {"family": "sans-serif", "color": "rgba(0, 0, 0, 1)", "size": 11}, "tickmode": "array", "anchor": "y", "range": [0.05615999999999843, 9.943840000000002], "tickcolor": "rgb(0, 0, 0)", "gridwidth": 0.5, "visible": true, "showgrid": true, "linecolor": "rgba(0, 0, 0, 1)", "zerolinecolor": "rgba(0, 0, 0, 1)", "tickangle": 0, "zeroline": false, "mirror": false, "domain": [0.09128390201224845, 0.9934383202099738], "title": {"font": {"family": "sans-serif", "color": "rgba(0, 0, 0, 1)", "size": 15}, "text": "месяц"}, "ticks": "inside", "type": "linear"}, "annotations": [], "plot_bgcolor": "rgba(255, 255, 255, 1.000)", "paper_bgcolor": "rgba(255, 255, 255, 1.000)", "margin": {"t": 20, "b": 20, "r": 0, "l": 0}, "yaxis": {"showline": true, "showticklabels": true, "gridcolor": "rgba(0, 0, 0, 0.1)", "tickfont": {"family": "sans-serif", "color": "rgba(0, 0, 0, 1)", "size": 11}, "tickmode": "array", "anchor": "x", "range": [-0.3600000000000003, 12.36], "tickcolor": "rgb(0, 0, 0)", "gridwidth": 0.5, "visible": true, "showgrid": true, "linecolor": "rgba(0, 0, 0, 1)", "zerolinecolor": "rgba(0, 0, 0, 1)", "tickangle": 0, "zeroline": false, "mirror": false, "domain": [0.07581474190726165, 0.9901574803149606], "title": {"font": {"family": "sans-serif", "color": "rgba(0, 0, 0, 1)", "size": 15}, "text": "количество"}, "ticks": "inside", "type": "linear"}, "width": 1515.828125, "legend": {"x": 1, "traceorder": "normal", "yanchor": "auto", "font": {"family": "sans-serif", "color": "rgba(0, 0, 0, 1)", "size": 11}, "borderwidth": 1, "bordercolor": "rgba(0, 0, 0, 1)", "y": 1, "bgcolor": "rgba(255, 255, 255, 1.000)", "tracegroupgap": 0, "title": {"font": {"family": "sans-serif", "color": "rgba(0, 0, 0, 1)", "size": 15}, "text": ""}, "xanchor": "auto"}}}