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

setting and gettin of 1 number in a matrix by overloading []

Tags: None
(comma "," separated)
maarten
Registered Member
Posts
6
Karma
0
Hello,
I'm a trying to port a home written matrix "library" to make use of eigen instead of the home written code. I would like to make this port in such a way that it does not need any change in the classes that make use of the home grown 'library". This would be the fist step in optimizing the code in terms of speed and maintainability. I completed this task for 99% but got stuck on the final 1%.

The home grown library makes use of a plain array ("data") to store the numbers, and 2 integers ("nrow","ncol")for the matrix dimensions. I replaced data with from the old version with:
Code: Select all
     data = MatrixBase<DT>(nr, nc);

The problem is the following (type of) line of of code which occurs multiple times in multiple files :
Code: Select all
matrix_a[3] = sqrt(matrix_b[4]);

where
Code: Select all
template<class DT>
DT &myownmatrix<DT>::operator[](int i)
{
    return data[i];
}

The overloaded [] function is used as setter and getter (I have a Java background). I tried to make it work but I started to generate random code: I was able to make it as a getter but unable to write it as getter and setter. Is it possible to call a eigen matrix in this way? The last working getter function looks like this:


Code: Select all
template<class DT>
DT myownmatrix<DT>::operator[]( int i)
{
    int column = i % ncol;
    int row = (int) floor((double) i / ncol);

    DT temp = data(row, column);

    return temp;
}


I hope someone can help.

Last edited by maarten on Wed May 02, 2012 7:21 pm, edited 1 time in total.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
your operator[] should returns a 'DT&', and directly:

return data(row, column);

Note that Eigen support 1D indexing by default but only when it can be used with no overhead, e.g. for Matrix object. Also note that Eigen's 1D indexing is dependent on the storage order.
maarten
Registered Member
Posts
6
Karma
0
Thanks! This helped a lot with float matrixes. However if I transform the type of the matrix from float to double the compiler starts to complain:

Code: Select all
In file included from test_matrix.cpp:12:0:
eigen_myownmatrix1.h: In member function 'DT& myownmatrix1<DT>::operator[](int) [with DT = double]':
test_matrix.cpp:60:28:   instantiated from here
eigen_myownmatrix1.h:75:28: error: invalid initialization of reference of type 'double&' from expression of type 'Eigen::DenseCoeffsBase<Eigen::Matrix<float, -0x00000000000000001, -0x00000000000000001>, 1>::Scalar {aka float}'
eigen_myownmatrix1.h:76:1: warning: control reaches end of non-void function [-Wreturn-type]


where test_matrix.cpp:60
Code: Select all
    std::cout <<"0 " << m[0] << std::endl;

(the value of this position is set)

and the second last line of the next snippet corresponds myownmatrix1.h:75

Code: Select all
template<class DT>
DT & myownmatrix<DT>::operator[](const int i)
{
    int column = i % ncol;
    int row = (int) floor((double) i / ncol);

    return data(row, column);
}


I have looked together with a colleague at this problem and we are quite clueless .
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
check your data(,) function, it seems it is returning a float while DT is double.


Bookmarks



Who is online

Registered users: Bing [Bot], claydoh, Google [Bot], rblackwell, Yahoo [Bot]