This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Translating Matlab code to Eigen

Tags: None
(comma "," separated)
flaviotruzzi
Registered Member
Posts
7
Karma
0

Translating Matlab code to Eigen

Mon Aug 06, 2012 3:50 pm
Hi, I'm totally newbie in eigen but I'm trying to convert the following Matlab code to C++ using Eigen.

Code: Select all
V = zeros(S,1);
pi = zeros(S,tau,'int8');

Q = zeros(S,A);

for t=1:tau
    disp(t)
    for a=1:A
        Q(:,a) = MDP{a}.R + MDP{a}.T*V;
    end
    [V pi(:,t)] = max(Q,[],2);
end


Note that T is a sparse Matrix. I manage to make this:
Code: Select all
void MDP::plan() {
   V = MatrixXf(S,1);
   V.setConstant(0.0);
   policy = MatrixXf(S,tau);
   Q = MatrixXf(S,A);
   Q.setConstant(0.0);

   for (int t = 0; t < tau; t++) {
      for (int a = 0; a < A; a++) {
         Q.col(a) = R[a] + T[a]*(V);
      }
   }
}

T still a SparseMatrix. The code above is not the "complete" code, and the compiler pukes tons of lines of errors, I guess its the Sparse*MatrixXf is the main problem... any ideas?

Thanks in Advance!
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
could you show how the variables are declared, and the first errors.
flaviotruzzi
Registered Member
Posts
7
Karma
0

Re: Translating Matlab code to Eigen

Mon Aug 06, 2012 10:28 pm
Code: Select all

S = largeNumber (~640k);

T = new SparseMatrix<double>[A];
R = new SparseVector<double>[A];

...

T[a] = SparseMatrix<double>(S,S);
R[a] = SparseVector<double>(S);


T and R are already filled when the plan method is called. The full code is available in: https://github.com/flaviotruzzi/MDP-in-C--

There are 342 lines of debug errors...

The first error is:

g++ -I/usr/include/eigen3/ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"MDP.d" -MT"MDP.d" -o "MDP.o" "../MDP.cpp" -std=c++0x -lm
In file included from /usr/include/eigen3/Eigen/Core:313:0,
from /usr/include/eigen3/Eigen/Dense:1,
from ../MDP.h:11,
from ../MDP.cpp:8:
/usr/include/eigen3/Eigen/src/Core/ProductBase.h: In instantiation of ‘struct Eigen::internal::traits<Eigen::ProductBase<Eigen::SparseTimeDenseProduct<Eigen::SparseMatrix<double>, Eigen::Matrix<float, -1, -1> >, Eigen::SparseMatrix<double>, Eigen::Matrix<float, -1, -1> > >’:
/usr/include/eigen3/Eigen/src/SparseCore/SparseDenseProduct.h:133:8: required from ‘struct Eigen::internal::traits<Eigen::SparseTimeDenseProduct<Eigen::SparseMatrix<double>, Eigen::Matrix<float, -1, -1> > >’
/usr/include/eigen3/Eigen/src/Core/DenseBase.h:31:34: required from ‘class Eigen::DenseBase<Eigen::SparseTimeDenseProduct<Eigen::SparseMatrix<double>, Eigen::Matrix<float, -1, -1> > >’
/usr/include/eigen3/Eigen/src/Core/MatrixBase.h:48:34: required from ‘class Eigen::MatrixBase<Eigen::SparseTimeDenseProduct<Eigen::SparseMatrix<double>, Eigen::Matrix<float, -1, -1> > >’
/usr/include/eigen3/Eigen/src/Core/ProductBase.h:63:7: required from ‘class Eigen::ProductBase<Eigen::SparseTimeDenseProduct<Eigen::SparseMatrix<double>, Eigen::Matrix<float, -1, -1> >, Eigen::SparseMatrix<double>, Eigen::Matrix<float, -1, -1> >’
/usr/include/eigen3/Eigen/src/SparseCore/SparseDenseProduct.h:238:7: required from ‘class Eigen::SparseTimeDenseProduct<Eigen::SparseMatrix<double>, Eigen::Matrix<float, -1, -1> >’
../MDP.cpp:162:35: required from here
/usr/include/eigen3/Eigen/src/Core/ProductBase.h:27:98: error: invalid use of incomplete type ‘struct Eigen::internal::scalar_product_traits<double, float>’
In file included from /usr/include/eigen3/Eigen/Core:245:0,
from /usr/include/eigen3/Eigen/Dense:1,
from ../MDP.h:11,
from ../MDP.cpp:8:
/usr/include/eigen3/Eigen/src/Core/util/Meta.h:189:41: error: declaration of ‘struct Eigen::internal::scalar_product_traits<double, float>’
In file included from /usr/include/eigen3/Eigen/Core:270:0,
from /usr/include/eigen3/Eigen/Dense:1,
from ../MDP.h:11,
from ../MDP.cpp:8:
flaviotruzzi
Registered Member
Posts
7
Karma
0
I read in the forums that I should not mix floats and doubles (viewtopic.php?f=74&t=107124), I changed all the MaxtrixXf to MatrixXd, and tryed to use SparseMatrix<double>(S,1) instead of SparseVector<double>(S).

I could make it work! Thanks anyway, and thanks the other people on the forum!


Bookmarks



Who is online

Registered users: Baidu [Spider], Bing [Bot], Google [Bot], Yahoo [Bot]