我学会了如何使用本征找到矩阵的逆矩阵。但是当我找到作为函数输出的数组的逆函数时,我得到了一个错误
请求成员"x"中的"反向",这是非类类型 "双**"
请帮助我,使用 c++ 库查找矩阵的逆矩阵。
我编写的代码是:
#include <iostream>
#include <armadillo>
#include <cmath>
#include <Eigen/Dense>
using namespace std;
using namespace arma;
using namespace Eigen;
int main()
{
vec a;
double ** x;
double ** inv_x;
a <<0 << 1 << 2 << 3 << 4; //input vector to function
double ** f (vec a); //function declaration
x= f(a); // function call
//inv_x=inv(x);
cout << "The inverse of x is:n" << x.inverse() << endl; // eigen command to find inverse
return 0;
}
// function definition
double ** f(vec a)
{
double ** b = 0;
int h=5;
for(int i1=0;i1<h;i1++)
{
b[i1] = new double[h];
{
b[i1][0]=1;
b[i1][1]=a[i1];
b[i1][2]=a[i1]*a[i1]+1/12;
b[i1][3]=pow(a[i1],3)+a[i1]/4;
b[i1][4]=1/80+pow(a[i1],2)/2+pow(a[i1],4);
}
}
return b;
}
这里用户定义的函数f
返回一个数组x
。我正在尝试使用特征库找到x
的反转。
首先,正如Martin Bonner所提到的,不要使用double**来存储矩阵,但要确保系数是按顺序存储的。
然后,可以使用 Eigen::Map
类将原始缓冲区视为 Eigen 的对象,如此处所述。例如:
double data[2][2];
Eigen::Map<Matrix<double,2,2,RowMajor> > mat(data[0]);
mat = mat.inverse();