Inverse of a matrix using eigen
Asked Answered
U

1

13

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.

Underbid answered 26/2, 2016 at 9:28 Comment(13)
That would be because it's a double**, and not an Eigen matrix?Alvinaalvine
@immibis Sir I want to find one matrix inverse using eigen library without using "eigen" matrix declaration syntax eg "Matrix3f" etc.Underbid
@ArunGovindNeelanA I'm not sure it's directly possible, Eigen uses its own types.Algology
@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 ?Underbid
@ArunGovindNeelanA sorry I don't knowAlgology
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?Adumbrate
@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.Underbid
You haven't answered my question: "Is there any reason you can't use the Matrix class provided by Eigen?"Adumbrate
@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.Underbid
Why can't you use that syntax? I think you need to explain more what your case is.Adumbrate
Thanks, @MartinBonner, if I shouldn't use pointer, then could you tell me how can I use dynamic memory allocation for array in c++?Underbid
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.)Adumbrate
Thanks @MartinBonnerUnderbid
S
15

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();
Stirling answered 26/2, 2016 at 21:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.