ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Because in Eigen 2 there is no Pseudo inverse function I helped myself by writing my own code. It is just working for 6x6 square matrizes for speed and because of lazyness :). You can alter it easily for any square matrix my code looks like this:

#include <Eigen/Eigen>
#include <Eigen/SVD>
#include <stdio.h>

void pinv_six_six( Eigen::Matrix< double, 6, 6> /*change for other dimensions*/ &input_matrix)
{
    Eigen::Matrix< double, 6, 6> S ; //change for other dimensions
    Eigen::Matrix< double, 6, 6> U ; //change for other dimensions
    Eigen::Matrix< double, 6, 6> V ; //change for other dimensions
    Eigen::JacobiSVD<Eigen::MatrixXd> svd(input_matrix, Eigen::ComputeThinU | Eigen::ComputeThinV);
    U = -svd.matrixU();
    U.transposeInPlace();
    S = svd.singularValues().asDiagonal();
    S=S.inverse(); 
    V = -svd.matrixV();
    input_matrix=V*S*U;
}

A function call looks like this:

    int main(int argc, char **argv)
    {
    Eigen::Matrix <double,6,6> example matrix;
    hessian <<        1,2,3,4,5,6,
                      1,2,3,4,5,6,
                      1,2,3,4,5,6,
                      1,2,3,4,5,6,
                      1,2,3,4,5,6,
                      1,2,3,4,5,6;//
    pinv_six_six(example_matrix);  //calculates the pseudo inverse
    std::cout << "pinv example matrix: " << example_matrix << std::cout; //output of the matrix

    return 0;
    }

Because in Eigen 2 there is no Pseudo inverse function I helped myself by writing my own code. It is just working for 6x6 square matrizes (independent if they have full rank or not) for speed and because of lazyness :). You can alter it easily for any square matrix my code looks like this:

#include <Eigen/Eigen>
#include <Eigen/SVD>
#include <stdio.h>

void pinv_six_six( Eigen::Matrix< double, 6, 6> /*change for other dimensions*/ &input_matrix)
{
    Eigen::Matrix< double, 6, 6> S ; //change for other dimensions
    Eigen::Matrix< double, 6, 6> U ; //change for other dimensions
    Eigen::Matrix< double, 6, 6> V ; //change for other dimensions
    Eigen::JacobiSVD<Eigen::MatrixXd> svd(input_matrix, Eigen::ComputeThinU | Eigen::ComputeThinV);
    U = -svd.matrixU();
    U.transposeInPlace();
    S = svd.singularValues().asDiagonal();
    S=S.inverse(); 
    V = -svd.matrixV();
    input_matrix=V*S*U;
}

A function call looks like this:

    int main(int argc, char **argv)
    {
    Eigen::Matrix <double,6,6> example matrix;
    hessian <<        1,2,3,4,5,6,
                      1,2,3,4,5,6,
                      1,2,3,4,5,6,
                      1,2,3,4,5,6,
                      1,2,3,4,5,6,
                      1,2,3,4,5,6;//
    pinv_six_six(example_matrix);  //calculates the pseudo inverse
    std::cout << "pinv example matrix: " << example_matrix << std::cout; //output of the matrix

    return 0;
    }

Because in Eigen 2 there is no Pseudo inverse function I helped myself by writing my own code. code.following advice from post number 18 from this blog entry. I am using the JacobiSVD objetct's solve method called with an identity matrix. It is just working for 6x6 square matrizes (independent if they have full rank or not) for speed and because of lazyness :). You can alter it easily for any square matrix returns the pinv my code looks like this:matlab also gives back.

 #include <Eigen/Eigen>
 #include <Eigen/SVD>
 #include <stdio.h>

 void pinv_six_six( Eigen::Matrix< double, 6, 6> /*change for other dimensions*/ &input_matrix)
pinv( Eigen::MatrixXd &input_matrix,Eigen::MatrixXd &output_matrix) {
    Eigen::Matrix< double, 6, 6> S ; //change for other dimensions
    Eigen::Matrix< double, 6, 6> U ; //change for other dimensions
    Eigen::Matrix< double, 6, 6> V ; //change for other dimensions

    output_matrix=Eigen::MatrixXd::Identity(input_matrix.rows(),input_matrix.cols());
    Eigen::JacobiSVD<Eigen::MatrixXd> svd(input_matrix, Eigen::ComputeThinU | Eigen::ComputeThinV);
    U = -svd.matrixU();
    U.transposeInPlace();
    S = svd.singularValues().asDiagonal();
    S=S.inverse(); 
    V = -svd.matrixV();
    input_matrix=V*S*U;
output_matrix=svd.solve(output_matrix); }

A function call looks like this:

  int main(int argc, char **argv)
     {
    Eigen::Matrix <double,6,6> example matrix;
    hessian     Eigen::MatrixXd example_matrix(6,6);
        Eigen::MatrixXd output_matrix(6,6);
        example_matrix <<        1,2,3,4,5,6,
                       1,2,3,4,5,6,
                       1,2,3,4,5,6,
                       1,2,3,4,5,6,
                       1,2,3,4,5,6,
                      1,2,3,4,5,6;//
    pinv_six_six(example_matrix);            1,2,3,4,5,6;
        pinv(example_matrix,output_matrix);  //calculates the pseudo inverse
     std::cout << "pinv example matrix: " << example_matrix output_matrix << std::cout; //output of the matrix

     return 0;
     }

Because in Eigen 2 there is no Pseudo inverse function I helped myself by writing my own code.following advice from post number 18 from this blog entry. I am using the JacobiSVD objetct's solve method called with an identity matrix. It returns the pinv my matlab also gives back.

    #include <Eigen/Eigen>
    #include <Eigen/SVD>
    #include <stdio.h>

  void pinv( Eigen::MatrixXd &input_matrix,Eigen::MatrixXd &output_matrix) {
 // fills the output matrix with a identity matrix for later use in the solvemethod
    output_matrix=Eigen::MatrixXd::Identity(input_matrix.rows(),input_matrix.cols());
    Eigen::JacobiSVD<Eigen::MatrixXd> svd(input_matrix, Eigen::ComputeThinU | Eigen::ComputeThinV);
    output_matrix=svd.solve(output_matrix); }

A function call looks like this:

        int main(int argc, char **argv)
        {
        Eigen::MatrixXd example_matrix(6,6);
        Eigen::MatrixXd output_matrix(6,6);
        example_matrix <<        1,2,3,4,5,6,
                                 1,2,3,4,5,6,
                                 1,2,3,4,5,6,
                                 1,2,3,4,5,6,
                                 1,2,3,4,5,6,
                                 1,2,3,4,5,6;
        pinv(example_matrix,output_matrix);  //calculates the pseudo inverse
        std::cout << "pinv example matrix: " << output_matrix << std::cout; //output of the matrix

        return 0;
        }