在scipy中有一个低密度脂蛋白分解和一个解决方法,但是我在python中不能得到与我使用eigen3编写的非常简单的代码相同的结果。下面是C++代码:
#include <iostream>
#include <Eigen/Dense>
using namespace std;
using namespace Eigen;
int main()
{
Matrix2f A, b;
A << 2, -1, -1, 3;
b << 1, 2, 3, 1;
cout << "Here is the matrix A:\n" << A << endl;
cout << "Here is the right hand side b:\n" << b << endl;
Matrix2f x = A.ldlt().solve(b);
cout << "The solution is:\n" << x << endl;
}该程序的输出为:
Here is the matrix A:
2 -1
-1 3
Here is the right hand side b:
1 2
3 1
The solution is:
1.2 1.4
1.4 0.8如何使用numpy/scipy来编写该代码才能获得相同的结果?
我尝试了以下一些方法:
import scipy.linalg
A = np.array([[2,-1],[-1,3]])
b = np.array([[1,2],[3,1]])
a = scipy.linalg.ldl(A)
print(scipy.linalg.solve(a[0],b))我尝试了转置和做a和b的成员的各种组合,我得到了一个结果,但它与C++程序中的结果不同,那么我在这里做错了什么呢?
谢谢
发布于 2019-08-27 00:11:16
啊哈,我被所有的措辞搞糊涂了。这就是解决Ax=b的问题,所以scipy.linalg.solve(A,b)做到了。
https://stackoverflow.com/questions/57661089
复制相似问题