Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inverse of a matrix using eigen

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.

like image 277
Arun Govind Neelan Avatar asked Feb 26 '16 09:02

Arun Govind Neelan


People also ask

Do a and an inverse have the same eigenvalues?

The answer is yes. First note that the eigenvalue λ is not zero since A is invertible. v=λA−1v. A−1v=1λv.

How do you the inverse of a matrix?

We can find the matrix inverse only for square matrices, whose number of rows and columns are equal such as 2 × 2, 3 × 3, etc. In simple words, inverse matrix is obtained by dividing the adjugate of the given matrix by the determinant of the given matrix.

Can you find the inverse of a 3x3 matrix?

What is the Inverse of 3x3 Matrix? The inverse of a 3x3 matrix, say A, is a matrix of the same order denoted by A-1 where AA-1 = A-1A = I, where I is the identity matrix of order 3x3. i.e., I = ⎡⎢⎣100010010⎤⎥⎦ [ 1 0 0 0 1 0 0 1 0 ] .


1 Answers

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();
like image 66
ggael Avatar answered Oct 12 '22 03:10

ggael