In scipy there is an ldl decomposition and a solve method, But I am not able to get the same result in python as I get using eigen3 for a very simple piece of code. Here is the C++ code:
#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;
}
The output of the program is:
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
How can that be written using numpy/scipy to get the same result?
I tried some of the following things:
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))
I tried transposing and doing various combinations of members of a and b. I get a result but its just not the same as in the C++ program, so what am I doing wrong here?
Thanks
Ahhh I got confused by all wording. Its just solving Ax=b. So scipy.linalg.solve(A,b)
does the trick.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.