matrix - Maple,编写一个 Proc 来计算矩阵的逆
问题描述
所以我正在研究一个有两个部分的问题。我确实在以下帮助下完成了第一部分:这个有用的论坛。一些机构已经尝试解决问题的第一部分,我拿走了他们的代码。
问题:
- 在 Maple 中编写一个 proc(这里我将其命名为“reduced”),用于计算矩阵的简化梯形形式。
- 编写一个使用“reduced”计算矩阵逆的过程。
第一部分的代码(代码经过测试,我声称它运行正确)
multiplikation:= proc(
m::posint,
a::depends(And(posint, satisfies(a-> a <= m))),
b::And({float, rational}, Not(identical(0,0.)))
)
Matrix((m,m), (i,j)-> `if`(i=j, `if`(i=a, b, 1), 0))
end proc:
addition:= proc(
m::posint,
a::depends(And(posint, satisfies(a-> a <= m))),
b::depends(And(posint, satisfies(b-> b <= m))),
c::And({float, rational}, Not(identical(0,0.)))
)
Matrix((m,m), (i,j)-> `if`(i=a and j=b, c, `if`(i=j, 1, 0)))
end proc:
perm:= proc(
m::posint,
a::depends(And(posint, satisfies(a-> a <= m))),
b::depends(And(posint, satisfies(b-> b <= m and a<>b)))
)
Matrix((m,m), (i,j)-> `if`({i,j}={a,b} or i=j and not i in {a,b}, 1, 0))
end proc:
和主要过程:
reduced:= proc(B::Matrix)
uses LA= LinearAlgebra;
local
M:= B, l:= 1, #l is current column.
m:= LA:-RowDimension(M), n:= LA:-ColumnDimension(M), i, j
;
for i to m do #going through every row item
#l needs to be less than column number n.
if n < l then return M end if;
j:= i; #Initialize current row number.
while M[j,l]=0 do #Search for 1st row item <> 0.
j:= j+1;
if m < j then #End of row: Go to next column.
j:= i;
l:= l+1;
if n < l then return M fi #end of column and row
end if
end do;
if j<>i then M:= perm(m,j,i).M end if; #Permute rows j and i
#Multiply row i with 1/M[i,l], if it's not 0.
if M[i,l] <> 0 then M:= multiplikation(m,i,1/M[i,l]).M fi;
#Subtract each row j with row i for M[j,l]-times.
for j to m do if j<>i then M:= addition(m,j,i,-M[j,l]).M fi od;
l:= l+1 #Increase l by 1; next iteration i increase either.
end do;
return M
end proc:
如果您需要有关上述代码的任何其他信息,我会解释更多。
对于第二部分,我正在考虑使用高斯乔丹算法,但我有一个问题:我不能使用单位矩阵作为“减少”中的参数。因为它在行和列中有 0。
您知道如何在我的 proc 的帮助下实现 Gauss Jordan 算法:减少吗?
解决方案
既定目标是利用该reduced
程序。
一种方法是通过单位矩阵增加输入矩阵,减少它,然后返回增加矩阵的右半部分。
将输入矩阵转换为单位矩阵的步骤也将单位矩阵转换为(输入矩阵的)逆矩阵。
例如,使用您的程序,
inv := proc(B::Matrix(square))
local augmented,m;
uses LinearAlgebra;
m := RowDimension(B);
augmented := <<B|IdentityMatrix(m)>>;
return reduced(augmented)[..,m+1..-1];
end proc:
MM := LinearAlgebra:-RandomMatrix(3,generator=1..5);
[1 4 2]
[ ]
MM := [1 5 3]
[ ]
[2 3 5]
ans := inv(MM);
[ 8 -7 1]
[ - -- -]
[ 3 3 3]
[ ]
[ 1 1 -1]
ans := [ - - --]
[ 6 6 6 ]
[ ]
[-7 5 1]
[-- - -]
[6 6 6]
ans.MM, MM.ans;
[1 0 0] [1 0 0]
[ ] [ ]
[0 1 0], [0 1 0]
[ ] [ ]
[0 0 1] [0 0 1]
附言。您可能还想考虑reduce
当矩阵不可逆时您的程序会做什么。
推荐阅读
- javascript - 从子组件调用父方法(Vue.js)
- angular - 使用 ionic 或 angular9 在 Base64 中编码/调整海量图像的大小(不使用 Javascript)
- nginx - uWSGI 和 Flask:在请求之间将对象保存在内存中
- python - 使用 Python 从共享邮箱 (O365) 发送电子邮件
- selenium - 选择器 - 查找该部分的第二个链接,然后是带有文本的部分包含
- javascript - 两个 JsFiddles,完全相同的代码,一个有效,一个无效。有谁知道这个问题?
- javascript - 如何防止选择添加到选择元素的新选项?
- kotlin - null 的类型是什么?
- postgresql - Postgresql 竞争条件
- django - 通过按钮 HTML 更新模型值 django(布尔字段)