12

I have learnt how to find inverse of a matrix using Eigen. But when I'm finding inverse of an array that is a output of function I got an error

request for member ‘inverse’ in ‘x’, which is of non-class type ‘double**’

Please help me out, in using c++ library to find inverse of a matrix.

The code I have written is:

#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;
}

Here user defined function f return an array x. I'm trying to find inverse of x using eigen library.

Mykola
  • 3,343
  • 6
  • 23
  • 39
AGN
  • 223
  • 1
  • 2
  • 7
  • 1
    That would be because it's a double**, and not an Eigen matrix? – user253751 Feb 26 '16 at 09:29
  • @immibis Sir I want to find one matrix inverse using eigen library without using "eigen" matrix declaration syntax eg "Matrix3f" etc. – AGN Feb 26 '16 at 09:44
  • @ArunGovindNeelanA I'm not sure it's directly possible, Eigen uses its own types. – Zermingore Feb 26 '16 at 10:02
  • @J.P.Quenord-Zermingore, Sir, Is there is any other library that can directly inverse a matrix that is declared using standard C++ syntax other than using its own matrix declaration syntax ? – AGN Feb 26 '16 at 10:09
  • @ArunGovindNeelanA sorry I don't know – Zermingore Feb 26 '16 at 10:13
  • 2
    `double **` is an awful way to declare a matrix for anything other than the most trivial of toy programs. (It splatters your matrix all over memory in a cache unfriendly way for one thing, it is really hard to avoid leaking memory for another, you don't encapsulate the dimensions for a third.) You are much better off using a purpose designed "Matrix" class - is there any reason you can't use the Eigen one? – Martin Bonner supports Monica Feb 26 '16 at 10:16
  • @MartinBonner , Sorry for my bad code practise (Its a piece ). Even if I define a matrix like a[2][2] and try to find inverse using library. I'm getting the some error. – AGN Feb 26 '16 at 10:23
  • You haven't answered my question: "Is there any reason you can't use the Matrix class provided by Eigen?" – Martin Bonner supports Monica Feb 26 '16 at 10:30
  • @MartinBonner, I'm new to these libraries and I started using c++ after a long gap. Please give me a link, if there is any documentation regarding "matrix class". Because for my case I think I can't use syntax like "Matrix3f", "Matrix4f" etc. – AGN Feb 26 '16 at 10:35
  • Why can't you use that syntax? I think you need to explain more what your case is. – Martin Bonner supports Monica Feb 26 '16 at 14:17
  • Thanks, @MartinBonner, if I shouldn't use pointer, then could you tell me how can I use dynamic memory allocation for array in c++? – AGN Feb 29 '16 at 01:09
  • `std::vector` or `std::unique_ptr` are the general replacements for pointers in C++. In your case, `std::vector` is the right answer. (There are other smart pointers, including `std::shared_ptr`, and `std::array` is worth remembering.) – Martin Bonner supports Monica Feb 29 '16 at 06:50
  • Thanks @MartinBonner – AGN Feb 29 '16 at 06:57

1 Answers1

13

First, as mentioned by Martin Bonner, don't use double** to store a matrix, but make sure the coefficients are sequentially stored.

Then, you can use the Eigen::Map class to see a raw buffer as an Eigen's object, as documented there. For instance:

double data[2][2];
Eigen::Map<Matrix<double,2,2,RowMajor> > mat(data[0]);
mat = mat.inverse();
ggael
  • 28,425
  • 2
  • 65
  • 71