Aru*_*n A 6 c++ armadillo eigen
我已经学会了如何使用Eigen找到矩阵的逆.但是当我发现数组的反函数是函数的输出时,我得到了一个错误
请求'x'中的成员'inverse',这是非类型'double**'
请帮帮我,使用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;
}
Run Code Online (Sandbox Code Playgroud)
这里用户定义的函数f返回一个数组x.我试图找到x使用特征库的逆.
首先,正如马丁·邦纳(Martin Bonner)所述,不要使用double **来存储矩阵,但要确保系数是顺序存储的。
然后,你可以使用Eigen::Map类来看看原始缓冲区作为本征的对象,如记录存在。例如:
double data[2][2];
Eigen::Map<Matrix<double,2,2,RowMajor> > mat(data[0]);
mat = mat.inverse();
Run Code Online (Sandbox Code Playgroud)