Re: [eigen] Matrix assignment

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


On Monday 20 October 2008 19:18:24 Cristóvão Sousa wrote:
> Hi,
>
> I'm wondering what operator is better for matrix (coefficient wise like)
> assignment:
>
>    = or << ?

Use operator= unless you need the extra features of operator<<.

operator<< allows to build a matrix block-by-block or 
coefficient-by-coefficient. Like:

Matrix3d m;
m << 1,2,3,
     4,5,6,
     7,8,9;

The terms on both sides may be expressions, so you can build a matrix block by 
block, for example, cf. the docs.

By contrast, operator= is only for assignment between two matrix expressions.

Operator<< should in theory be just as fast as operator=, but it is more 
stressing to your compiler so you will get longer compile times and sometimes 
certain compilers fail to optimize operator<< correctly.

>
> I have a matrixXd "m" which will store the result of some matrices(Xd)
> operations.
> Matrix "m" should have a fixed size, only known at runtime.

Here's a problem: "fixed size" in Eigen means known at compile-time.
If it's only known at runtime you have to use matrixXd indeed, but then you 
don't get assert failures if it has to be resized (as you ask for in 
paragraph below).

> If for some 
> reason, related to a bug in my code, the operations result in a different
> matrix size, the assignment
>    m = "operations with m2,m3,m4,..."
> will change "m" size.
> I rather need, for debug purposes, an assertion failure being raised.

I don't see how Eigen can help with that, for reasons said above. The simplest 
thing you could do is, before writing "m = expression;" write a 
check "assert(m.rows() == expression.rows() && m.cols() == 
expression.cols());". You could make a macro or a function for that, 

template<typename Dst, typename Src>
Eigen::MatrixBase<Dst>&
my_nonresizing_assignment(Eigen::MatrixBase<Dst>& dst,
                          const Eigen::MatrixBase<Src>& src)
{
	assert(dst.rows() == src.rows() && dst.cols() == src.cols();
	dst = src;
}


>
> I discover that the "<<" operator does just what I need (it fails even if
> the operations resulting matrix is smaller than "m").
> But now I'm wondering if "<<" operator is equal, in terms of speed and
> laziness optimizations, to the "=" operator.

Aah, now i see why you looked at operator<<. Well, why not... but as I said 
using it systematically instead of operator= makes the life harder for your 
compiler, Gael saw poor compiled code with ICC; recent GCC seems to be doing 
well but even with it I expect longer compilation times.

Cheers,
Benoit

>
> Thanks in advance. And thanks very very very much for Eigen! It was the
> matrix killer library I was looking for! I had tried  Blitz,boost::ublas,
> GSL, Tvmet, Newmat...
> Honestly, Eigen (version 2) is just the best single piece of C++ code I
> ever used!

Thanks (on behalf of Gael and me)
Benoit

---


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