Reply to topic

Compiling error: *** was not declared in this scope

matteog
Registered Member
Posts
5
Karma
0
i'm a neophyte of C++ programmation. i have to implement a program witch calculates the pseuinverse of a matrix. as the Eigen tutorial suggests, i have written a code like this:
Code: Select all
    #include <stdio.h>
    #include <stdlib.h>
    #include <Core>
    #include <iostream>
    #include <Eigen/Dense>
    #include <Eigen/SVD>
    #include <Eigen/Eigen>
    using namespace Eigen;
    using namespace std;



    void pinv(MatrixXf& pinvmat)
     {
    ei_assert(m_isInitialized && "SVD is not initialized.");
   double  pinvtoler=1.e-6;                       // choose tolerance
    SingularValuesType m_sigma_inv=m_sigma;
    for ( long i=0; i<m_workMatrix.cols(); ++i) {
    if ( m_sigma(i) > pinvtoler )
     m_sigma_inv(i)=1.0/m_sigma(i);
   else m_sigma_inv(i)=0;
   }
    pinvmat = (m_matV*m_sigma_inv.asDiagonal()*m_matU.transpose());


   }

    int main()
   {

    MatrixXf A(3,2);
    A<<1,2,3,4,5,6;
    pinv(A);
    cout << "pinv =" << endl << A << endl;
    return 0;
   }


if i try to compile it i'll get the errors:

tut_eigen/pinv.cpp: In function ‘void pinv(Eigen::MatrixXf&)’:

tut_eigen/pinv.cpp:18:14: error: ‘m_isInitialized’ was not declared in this scope

tut_eigen/pinv.cpp:18:58: error: ‘ei_assert’ was not declared in this scope

tut_eigen/pinv.cpp:20:4: error: ‘SingularValuesType’ was not declared in this scope

tut_eigen/pinv.cpp:20:23: error: expected ‘;’ before ‘m_sigma_inv’

tut_eigen/pinv.cpp:21:22: error: ‘m_workMatrix’ was not declared in this scope

tut_eigen/pinv.cpp:22:19: error: ‘m_sigma’ was not declared in this scope

tut_eigen/pinv.cpp:23:19: error: ‘m_sigma_inv’ was not declared in this scope

tut_eigen/pinv.cpp:24:22: error: ‘m_sigma_inv’ was not declared in this scope

tut_eigen/pinv.cpp:26:15: error: ‘m_matV’ was not declared in this scope

tut_eigen/pinv.cpp:26:22: error: ‘m_sigma_inv’ was not declared in this scope

tut_eigen/pinv.cpp:26:47: error: ‘m_matU’ was not declared in this scope

why?? they are not declared in SVD file? thank for your helping
Dee33
Registered Member
Posts
54
Karma
0
OS
You should not use private variables of a class ( all of m_*) outside of a class definition. No tutorial page suggest that.
You should use high levels functions to access the values you want.
To get the singular values, you can use the JacobiSVD class http://eigen.tuxfamily.org/dox-devel/cl ... biSVD.html
matteog
Registered Member
Posts
5
Karma
0
thank you Dee33.
so...nobody can use this code!? why is it present in the FAQ?
Should I modify my code using the types defined in JacobiSVD library instead of SVD library?
can you suggest me how?
thank you again
Dee33
Registered Member
Posts
54
Karma
0
OS
ok I see. You mean the old fragment of code in this page http://eigen.tuxfamily.org/index.php?title=FAQ
The code was intended to be put in the SVD class, probably in Eigen2.
For your own purposes, just use the matrices U, V and the singularvalues from the JacobiSVD class to compute the pseudo-inverse.

This modified version of that code should do the job
Code: Select all
template <typename MatrixType>
void pinv( MatrixType& pinvmat)
 {
   JacobiSVD<MatrixType> svd(pinvmat,ComputeFullU|ComputeFullV); // Compute the SVD of pinvmat
  double  pinvtoler=1.e-6; // choose your tolerance widely!
  SingularValuesType sigma,sigma_inv;
  sigma  = svd.singularValues();
  sigma_inv.setZeros();
  for ( int i=0; i<pinvmat.cols(); ++i) {
      if ( sigma(i) > pinvtoler )
        sigma_inv(i)=1.0/sigma(i);
   }
  pinvmat =  svd.matrixV()*sigma_inv.asDiagonal()*svd.matrixU().transpose() ;
}

Last edited by Dee33 on Tue Nov 06, 2012 2:39 pm, edited 1 time in total.
matteog
Registered Member
Posts
5
Karma
0
mmm
i included JacobiSVD and i tried the modified code but doesn't work:

acer@ubuntu:~$ g++ -I / tut_eigen/pinv.cpp -o pinv
tut_eigen/pinv.cpp:33:15: error: variable or field ‘pinv’ declared void
tut_eigen/pinv.cpp:33:15: error: ‘MatrixType’ was not declared in this scope
tut_eigen/pinv.cpp:33:27: error: ‘pinvmat’ was not declared in this scope

i don't understand ...MatrixType is a template defined in JacobiSVD :'(
Dee33
Registered Member
Posts
54
Karma
0
OS
MatrixType is just a template type for that function.
I forgot to put template<typename MatrixType> before the definition of the function.
matteog
Registered Member
Posts
5
Karma
0
yes, i had already tried it...
but...

tut_eigen/pinv.cpp: In function ‘void pinv(MatrixType&)’:
tut_eigen/pinv.cpp:37:7: error: ‘SingularValuesType’ was not declared in this scope
tut_eigen/pinv.cpp:37:26: error: expected ‘;’ before ‘sigma’
tut_eigen/pinv.cpp:38:7: error: ‘sigma’ was not declared in this scope
tut_eigen/pinv.cpp:39:7: error: ‘sigma_inv’ was not declared in this scope


.........
Dee33
Registered Member
Posts
54
Karma
0
OS
Code: Select all
typename JacobiSVD<MatrixType>::SingularValuesType sigma;

Please, note that the goal here is to show you how to access outputs from Eigen classes.
It's up to you to read the manual pages and to write your code for your own purposes.
Dee33
Registered Member
Posts
54
Karma
0
OS
This version will works for you triangular test matrix.

Code: Select all
template <typename MatrixType>
void pinv( MatrixType& pinvmat)
{
    JacobiSVD<MatrixType> svd(pinvmat,ComputeFullU|ComputeFullV); // Compute the SVD of pinvmat
    double  pinvtoler=1.e-6; // choose your tolerance widely!
    typename JacobiSVD<MatrixType>::SingularValuesType sigma,sigma_inv;
    sigma  = svd.singularValues();
    sigma_inv.resizeLike(sigma);
    for ( int i=0; i<sigma.size();i++) {
      if ( sigma(i) > pinvtoler )
        sigma_inv(i)=1.0/sigma(i);
      else
        sigma_inv(i) = 0;
    }
    MatrixType diag(pinvmat.cols(), pinvmat.rows());
    diag.setZero();
    diag.diagonal() = sigma_inv;
    pinvmat =  svd.matrixV()*diag*svd.matrixU().transpose() ;
}


 
Reply to topic

Bookmarks



Who is online

Registered users: AElfwine, anditosan, Baidu [Spider], bcooksley, Bing [Bot], doublerainbow64, Exabot [Bot], Google [Bot], google01103, hefeweiz3n, inksi, jensreuterberg, kainz.a, koriun, l3u, moisachedaniela, Paulms, pbCyanide, raphaelpoli, scummos, SecretCode, Sentynel, toad, tparrott, Yahoo [Bot]