使用特征反演矩阵

时间:2016-02-26 09:28:39

标签: c++ eigen armadillo

我已经学会了如何使用Eigen找到矩阵的逆矩阵。但是当我发现数组的反函数是函数的输出时我得到了一个错误

  

请求'x'中的成员'inverse',这是非类型的   “双**”

请帮助我,使用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的逆。

1 个答案:

答案 0 :(得分:5)

首先,如Martin Bonner所述,不要使用双**来存储矩阵,但要确保系数是按顺序存储的。

然后,您可以使用Eigen::Map类将原始缓冲区视为Eigen的对象,如文档there所示。例如:

double data[2][2];
Eigen::Map<Matrix<double,2,2,RowMajor> > mat(data[0]);
mat = mat.inverse();
相关问题