[英]Finding inverse of a matrix using Gauss - Jordan Method
I am trying to write a program that finds the inverse of an nxn matrix in Mathematica.我正在尝试编写一个程序来查找 Mathematica 中 nxn 矩阵的逆矩阵。 I've been able to find matrices that don't find the inverse of the matrix but whose product has a determinant of 1, lol.
我已经能够找到没有找到矩阵的逆矩阵但其乘积的行列式为 1 的矩阵,大声笑。 Can you tell me where I went wrong?
你能告诉我哪里出错了吗?
m = Input["row"];
n = Input["column"];
mat = Table[Subscript[b, i, j], {i, m}, {j, n}];
mat2 = Table[Subscript[c, i, j], {i, m}, {j, n}];
For[i = 1, i <= m, i++,
For[j = 1, j <= n,
j++, {Subscript[c, i, i] = 1, Subscript[c, i, j] = 0,
Subscript[c, m, n] = 1}]];
For[i = 1, i <= m, i++,
For[j = 1, j <= n, j++,
Subscript[b, i, j] = RandomInteger[{1, 3}]]];
Print[mat // MatrixForm]
For[k = 1, k <= n - 1, k++,
For[l = k + 1, l <= n, l++,
If[Subscript[b, l, k] == 0, continue,
coe = Subscript[b, l, k]/Subscript[b, k, k];
For[t = k, t <= n, t++,
Subscript[b, l, t] = Subscript[b, l, t] - coe*Subscript[b, k, t];
Subscript[c, l, t] =
Subscript[c, l, t] - coe*Subscript[c, k, t]]]]];
For[i = 1, i <= m, i++,
coe2 = 1/Subscript[b, i, i];
For[j = 1, j <= n, j++,
Subscript[b, i, j] = Subscript[b, i, j]*coe2;
Subscript[c, i, j] = Subscript[c, i, j]*coe2]];
For[k = n - 1, k >= 1, k--,
For[l = n, l >= k + 1, l--,
If[Subscript[b, k, l] == 0, continue,
coe3 = Subscript[b, k, l]/Subscript[b, k, k];
For[t = k, t <= n, t++,
Subscript[b, t, l] = Subscript[b, t, l] - coe3*Subscript[b, t, k];
Subscript[c, t, l] =
Subscript[c, t, l] - coe3*Subscript[c, t, k]]]]];
Print[mat // MatrixForm];
Print[mat2 // MatrixForm];
Quit[];
This is not a complete answer.这不是一个完整的答案。 Just a work in progress.
只是一个正在进行的工作。
For an nxn matrix you won't need m and n.对于 nxn 矩阵,您不需要 m和n。 Also, using
Subscript
makes the code difficult to read so I have replaced Subscript[b, i, j]
with b[i, j]
etc.此外,使用
Subscript
会使代码难以阅读,因此我将Subscript[b, i, j]
替换为b[i, j]
等。
Clear[c, b, m, n, i, j, coe, coe2, coe3, k, l, t]
(* m = n = Input["n"]; *)
m = n = 5;
a = mat = Table[b[i, j], {i, m}, {j, n}];
mat2 = Table[c[i, j], {i, m}, {j, n}];
For[i = 1, i <= m, i++,
For[j = 1, j <= n, j++,
{c[i, i] = 1, c[i, j] = 0, c[m, n] = 1}]];
For[i = 1, i <= m, i++,
For[j = 1, j <= n, j++,
b[i, j] = RandomInteger[{1, 3}]]];
Print[mat // MatrixForm]
This is what you are aiming for: ie using RowReduce
for the elimination, as per this post .这就是您的目标:即根据这篇文章,使用
RowReduce
进行消除。
MatrixForm[inverse = RowReduce[Join[mat, mat2, 2]][[All, n + 1 ;;]]]
a.inverse == IdentityMatrix[n]
These matrices are not always invertible, eg这些矩阵并不总是可逆的,例如
a = mat = {{1, 2, 3, 3, 3}, {1, 3, 1, 2, 1},
{2, 3, 3, 2, 3}, {3, 2, 1, 2, 1}, {3, 1, 3, 1, 3}};
So far unable to correct your code.到目前为止无法更正您的代码。
For[k = 1, k <= n - 1, k++,
For[l = k + 1, l <= n, l++,
If[b[l, k] != 0,
coe = b[l, k]/b[k, k];
For[t = k, t <= n, t++,
b[l, t] = b[l, t] - coe*b[k, t];
c[l, t] = c[l, t] - coe*c[k, t]]]]];
For[i = 1, i <= m, i++,
coe2 = 1/b[i, i];
For[j = 1, j <= n, j++,
b[i, j] = b[i, j]*coe2;
c[i, j] = c[i, j]*coe2]];
For[k = n - 1, k >= 1, k--,
For[l = n, l >= k + 1, l--,
If[b[k, l] != 0,
coe3 = b[k, l]/b[k, k];
For[t = k, t <= n, t++,
b[t, l] = b[t, l] - coe3*b[t, k];
c[t, l] = c[t, l] - coe3*c[t, k]]]]];
IdentityMatrix[n] == mat
MatrixForm[inverse = mat2]
a.inverse == IdentityMatrix[n]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.