首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >c++ eigen3示例的scipy/numpy等效项

c++ eigen3示例的scipy/numpy等效项
EN

Stack Overflow用户
提问于 2019-08-26 23:46:03
回答 1查看 448关注 0票数 0

在scipy中有一个低密度脂蛋白分解和一个解决方法,但是我在python中不能得到与我使用eigen3编写的非常简单的代码相同的结果。下面是C++代码:

代码语言:javascript
复制
#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;
}

该程序的输出为:

代码语言:javascript
复制
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来编写该代码才能获得相同的结果?

我尝试了以下一些方法:

代码语言:javascript
复制
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++程序中的结果不同,那么我在这里做错了什么呢?

谢谢

EN

回答 1

Stack Overflow用户

发布于 2019-08-27 00:11:16

啊哈,我被所有的措辞搞糊涂了。这就是解决Ax=b的问题,所以scipy.linalg.solve(A,b)做到了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57661089

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档