gpt4 book ai didi

c++ - Armadillo C++ 找不到矩阵求逆

转载 作者:搜寻专家 更新时间:2023-10-31 01:31:57 25 4
gpt4 key购买 nike

我正在使用 Armadillo 和 C++,我试图找到矩阵的逆矩阵,但是,逆矩阵只返回矩阵本身。

在我看来,没有任何计算。此外,不会引发任何错误。

我正在使用以下 header :

#include <armadillo>
using namespace std;
using namespace arma;

我已经使用 Armadillo 几天了,并运行了几个正常工作的矩阵操作。

输入:

mat A = randu<mat>(5,5);
A.print("A: ");
mat B = inv(A);
B.print("inv(A): ");

输出:

A: 
0.0013 0.1741 0.9885 0.1662 0.8760
0.1933 0.7105 0.1191 0.4508 0.9559
0.5850 0.3040 0.0089 0.0571 0.5393
0.3503 0.0914 0.5317 0.7833 0.4621
0.8228 0.1473 0.6018 0.5199 0.8622
inv(A):
0.0013 0.1741 0.9885 0.1662 0.8760
0.1933 0.7105 0.1191 0.4508 0.9559
0.5850 0.3040 0.0089 0.0571 0.5393
0.3503 0.0914 0.5317 0.7833 0.4621
0.8228 0.1473 0.6018 0.5199 0.8622
Process finished with exit code 0

问题:

为什么 inv(ofAMatrix) 不工作,有什么提示或想法吗?谢谢!

最佳答案

这与带有 Intel (R) MKL 的 Armadillo 7.900.1 一起工作得很好后端和 Clang 5.0。

除非绝对必要,否则永远不要对矩阵求逆。此外,您还必须确保逆函数确实存在,否则算法将愉快地输出垃圾。如果您想计算 A 的倒数以找到 x,如

x = A-1 b

求解线性系统比较好

A x = b

相反。这些求解器速度更快,收敛性更好。

#include <armadillo>

int main()
{
arma::mat A = { { 0.0013 , 0.1741 , 0.9885 , 0.1662 , 0.8760 } ,
{ 0.1933 , 0.7105 , 0.1191 , 0.4508 , 0.9559 } ,
{ 0.5850 , 0.3040 , 0.0089 , 0.0571 , 0.5393 } ,
{ 0.3503 , 0.0914 , 0.5317 , 0.7833 , 0.4621 } ,
{ 0.8228 , 0.1473 , 0.6018 , 0.5199 , 0.8622 } };
A.print("A: ");
arma::mat B = arma::inv(A);
B.print("inv(A): ");
arma::mat I = A*B;
I.print("I: ");
}

输出:

A: 
0.0013 0.1741 0.9885 0.1662 0.8760
0.1933 0.7105 0.1191 0.4508 0.9559
0.5850 0.3040 0.0089 0.0571 0.5393
0.3503 0.0914 0.5317 0.7833 0.4621
0.8228 0.1473 0.6018 0.5199 0.8622
inv(A):
0.4736 -1.7906 4.4377 2.2515 -2.4784
2.9108 -3.1697 12.1159 7.7356 -11.1675
2.5212 -2.8557 6.8074 4.7142 -6.1801
-1.0317 0.9400 -2.3230 0.2413 1.3297
-2.0869 3.6766 -9.6555 -6.9062 8.9447
I:
1.0000e+00 1.1340e-16 -1.8134e-15 -6.4918e-16 -4.8899e-17
7.6334e-17 1.0000e+00 -9.1810e-16 -9.4668e-16 8.7907e-16
2.5424e-16 -4.3981e-16 1.0000e+00 9.2981e-16 -2.0864e-15
9.3036e-17 -2.6745e-17 7.5137e-16 1.0000e+00 -8.1372e-16
4.3422e-16 -4.2293e-16 1.1321e-15 1.0687e-15 1.0000e+00

关于c++ - Armadillo C++ 找不到矩阵求逆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44510612/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com