[eigen] Some trouble with triangularView

[ Thread Index | Date Index | More lists.tuxfamily.org/eigen Archives ]


To speed up covariance computations, I tried to use triangularView to avoid computing unnecessary coefficients.  However, that didn't work as expected.

To start with, there seems to be a g++ bug that prevents compiling the following template:

#include <Eigen/Core>
using namespace Eigen;
template <typename TPoints>
    inline static Matrix<typename TPoints::Scalar,TPoints::RowsAtCompileTime,TPoints::RowsAtCompileTime>
    CovarianceX(Eigen::MatrixBase<TPoints>const & points) {
        typedef typename TPoints::Scalar Scalar;
        typedef Eigen::Matrix<Scalar,TPoints::RowsAtCompileTime,1> TPoint;
        typedef Matrix<typename TPoints::Scalar,TPoints::RowsAtCompileTime,TPoints::RowsAtCompileTime> TMatrix;
        TPoint mean = points.rowwise().sum() * (1.0/points.cols());
        TPoint diff = TPoint::Zero(points.rows());
        TMatrix cov = TMatrix::Zero(points.rows(),points.rows());

        for(int i=0;i<points.cols();++i) {
            diff.noalias() = points.col(i) - mean;
            cov.triangularView<Eigen::Upper>() += diff * diff.transpose(); //line A
        }
       
        cov.triangularView<Eigen::StrictlyLower>() = cov.transpose(); //line B
        return cov * (1.0/(points.cols()-1.0));
    }
  
int main(){return 0;}

In particular, on lines A&B the compiler complains of "expected primary-_expression_ before ')' token".  If the function isn't templated it compiles (and works).  In MSC, it works.  It looks like specifying a template argument here breaks something - anyone know of a workaround?  I tried using a templated class with a static function, but that fails equally.

Now, even when this works, it's a lot slower than without triangularView, which is odd.  At first I thought that's because I can't mix .noalias and triangularView, but as far as I can tell, line A is using a lazy product despite possible aliasing (isn't that a bug?) so the perf degredation may simply be due to the greater code complexity.

--eamon@xxxxxxxxxxxx - Tel#:+31-6-15142163


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/