Re: [eigen] inconsistent cwise() support |

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

*To*: eigen@xxxxxxxxxxxxxxxxxxx*Subject*: Re: [eigen] inconsistent cwise() support*From*: "Schleimer, Ben" <bensch128@xxxxxxxxx>*Date*: Wed, 18 Nov 2009 12:04:59 -0800 (PST)*Dkim-signature*: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1258574699; bh=Vm/N6N4WPN+hVsyS54o53+kxWMjb6x8MjCehbZKFJiY=; h=Message-ID:X-YMail-OSG:Received:X-Mailer:References:Date:From:Reply-To:Subject:To:In-Reply-To:MIME-Version:Content-Type; b=ifHCti9rcoCSOQfS+G7ao9ingEpFMEzXiQftjY30voWasAmAE1Wzl1fd8yNfKq9W3usNa0Kh1k7fSsTcikBX8jrzwh/0r22T/HuMFewC3vEiiU59sdmBgxKfEzjeyp5JUsnNZ41PfF+iD6vFxFqOcM1UWAusKZZ1EMJ1uh1wwQ4=*Domainkey-signature*: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=Message-ID:X-YMail-OSG:Received:X-Mailer:References:Date:From:Reply-To:Subject:To:In-Reply-To:MIME-Version:Content-Type; b=UvG4AYqOBJJX1yqou+00E15mQpfALQvpS2halohQC5ND4visvRAzJB4bWk6D2+kzXX4G9j3+C8C2gQplea4Kir9iOaYqWZxk7Yf/aaCNeq18L8cL6l/LOAfgVmRwKNbaX/NiYI+a5yMiErB7xpdWk+cCrJB3sLZmuizf/Cot5X8=;

Hi,

I wanted to stop lurking and jump in with my two cents.

I think from a user's perspective it would be clearest if operations that are component wise are always marked with cwise() or array(). Example:

MatrixdX matProd = A * B; // legal, indicates the matrix product of A and B

MatrixdX compProd = A.cwise() * B.cwise(); // legal, indicates the component product of A and B

MatrixdX invalidProd = A.cwise() * B; // illegal, this isn't a matrix product or pre-component product

MatrixdX invalidProd = A * B.cwise(); // illegal, same as above

likewise, exp() would be the same:

A.exp() -> matrix exponential

A.cwise().exp() -> per-component exponential

I'd advocate for the sake of consistency that:

MatrixdX sum = A.cwise() + B.cwise(); // legal

MatrixdX sum = A + B; // illegal

Adding .cwise() (or .array()) to the end of the matrix name is not a huge burden and makes the subjects of the operation explicit.

I wanted to stop lurking and jump in with my two cents.

I think from a user's perspective it would be clearest if operations that are component wise are always marked with cwise() or array(). Example:

MatrixdX matProd = A * B; // legal, indicates the matrix product of A and B

MatrixdX compProd = A.cwise() * B.cwise(); // legal, indicates the component product of A and B

MatrixdX invalidProd = A.cwise() * B; // illegal, this isn't a matrix product or pre-component product

MatrixdX invalidProd = A * B.cwise(); // illegal, same as above

likewise, exp() would be the same:

A.exp() -> matrix exponential

A.cwise().exp() -> per-component exponential

I'd advocate for the sake of consistency that:

MatrixdX sum = A.cwise() + B.cwise(); // legal

MatrixdX sum = A + B; // illegal

Adding .cwise() (or .array()) to the end of the matrix name is not a huge burden and makes the subjects of the operation explicit.

What do you think?

Ben

On Wed, Nov 18, 2009 at 1:03 PM, Jitse Niesen <jitse@xxxxxxxxxxxxxxxxx> wrote:

Actually I think that's one of the main goals of the class Array.

Well, to be precise, the .matrix() here would be useless because Matrix would have a ctor from Array and vice versa. So:

MatrixXd prod = A.array() * B.array();

will work. Note that because Array::operator* would expect a template ArrayBase object, in spite of this Matrix from Array ctor, the following still won't work:

MatrixXd prod = A.array() * B;

That let me think such a ctor is safe.

he he, this is exactly what we had at the very beginning of Eigen2. If I remember well the major reason, not to say the unique one, that motivated us to move to the cwise() prefix was to be able to overload operators. Indeed, if we list all the cwise operators we currently have together with their possible respective method names, one can see that some are very hard to translate (especially the +=, -=, and *= operators):

+ scalar <=> cwiseAdd(scalar)

- scalar <=> cwiseSub(scalar)

* mat <=> cwiseMul(mat)

< mat <=> cwiseLessThan(mat) or cwiseLT

<= mat <=> cwiseLessEqual(mat) or cwiseLE

< scalar <=> cwiseLessThan(scalar) or cwiseLT

<= scalar <=> cwiseLessEqual(scalar) or cwiseLE

// same for >, >=, ==, !=

+= scalar <=> cwiseAddInPlace(scalar)

-= scalar <=> cwiseSubInPlace(scalar)

*= mat <=> cwiseMulInPlace(scalar)

And we could also imagine to support all kind of bitwise operations: |, &, ~, ^, ! that we could name bitwiseXxx.

Now, I also acknowledge this drawback is mitigated by the introduction of an Array class, because then we can expect that the need to apply such operators on matrices will be significantly reduced.

Nevertheless, I think it is still important to be able to view matrix as array, and vice versa. Two options:

1 - We introduce easy to use .array() and .matrix() methods, and then we'll have both my proposal and your proposal, so again an infinite ways to do the same thing that is not very nice IMO.

2 - We assume such a conversion is worth it only if you use the view-as-array object multiple times, and so we enforce to use named wrapper objects, e.g.:

MatrixXf m = ...;

ArrayWrapper<MatrixXf> a(m);

// take care that "a" and "m" reference the same data.

a += 2;

Finally, there is another issue that I forgot to mention in my previous email and that go in favor of your proposal. All those cwise operators/functions are not only useful for dense objects but also for sparse matrices and certainly all other special storage objects (especially abs(), abs2(), etc.). Since we want to preserve a consistent API as well as to reduce code duplication, this means that with my proposal the Array class should be able to potentially wrap any kind of matrices.... No need to say that significantly complicate the implementation of the Array class (e.g., some will support only unary ops, some require to extend Array with other overloads, etc.). The implementation of your proposal is far much simpler because 1) Array would be reserved to dense storage, and 2) the cwise* functions won't be hidden into wrapper classes. (sorry if these last sentences does not make much sense to you, but believe me it's much simpler ;) )

So after all, I'm going to like your proposal, and will wait for other opinions.

As a side note, if we go for your proposal, we can also think about implementing in MatrixBase only a subset of the cwise ops available in Array.

Gael.

On Tue, 17 Nov 2009, Gael Guennebaud wrote:I agree that we need an easy way to apply functions like sin coefficient-wise. However, the Array class has the disadvantage that A*B has different meaning depending on whether A and B are Matrix or Array.

I'm thinking that once we'll have a true array support, all this array/cwise stuff is going to be messy. Here I'll assume that a true Array class supporting the standard global math function (e.g., std::sin) is really needed.

To me, that seems a serious issue, but perhaps I'm alone in that opinion.

Actually I think that's one of the main goals of the class Array.

Nevertheless, let me discuss how to mitigate this. When the Array class is proposed, I thought that it was to support the different languages used in different domains (e.g., and rather crudely, theoretical linear algebra for Matrix, data manipulation for Array). A program in the first domain would use exclusively Matrix, and a program in the second domain would use exclusively Array. In that case, confusion is minimal, so I think that would be good programming style.

However, now it's envisaged that Matrix and Array are freely mixed, and if cwise() is abolished it's even necessary to do so. So, the next best thing is to make sure that all your variables are Matrix (supposing you're in the theoretical linear algebra domain). As Gael said, this will lead to code like the following for componentwise multiplication = Hadamard product:

MatrixXd A = ...

MatrixXd B = ...

MatrixXd prod = (A.array() * B.array()).matrix();

Well, to be precise, the .matrix() here would be useless because Matrix would have a ctor from Array and vice versa. So:

MatrixXd prod = A.array() * B.array();

will work. Note that because Array::operator* would expect a template ArrayBase object, in spite of this Matrix from Array ctor, the following still won't work:

MatrixXd prod = A.array() * B;

That let me think such a ctor is safe.

Not only is that cumbersome, but I think that it's more difficult to understand than A.cwise() * B.I agree with this, cwise() is difficult, because of what you call the prefix concept: in A.cwise().max(B), cwise() modifies max, while C++ programmers will think it modifies A. Perhaps a resolution would be to add a cwisemax() function to MatrixBase, so that we would replace A.cwise().max(B) to A.cwisemax(B). Now it's clear that the cwise prefix modifies max.

I think it is also important to acknowledge that the current cwise() API is odd enough to make it hard to learn for new comers. They often have some difficulties to determine where .cwise() is needed (e.g., m1.cwise() * m2.cwise()), and how it propagates (though it does not, of course). For instance, if you look at "m1.cwise() * m2" and you know, or did not understand the prefix concept of .cwise(), then it seems that .cwise propagate to m2.

With componentwise multiplication, that would mean that we replace A.cwise() * B by A.cwisemul(B). It's a pity that we lose the multiplication sign, but I think that we win on clarity: it's hard to misinterpret what A.cwisemul(B) would mean.

he he, this is exactly what we had at the very beginning of Eigen2. If I remember well the major reason, not to say the unique one, that motivated us to move to the cwise() prefix was to be able to overload operators. Indeed, if we list all the cwise operators we currently have together with their possible respective method names, one can see that some are very hard to translate (especially the +=, -=, and *= operators):

+ scalar <=> cwiseAdd(scalar)

- scalar <=> cwiseSub(scalar)

* mat <=> cwiseMul(mat)

< mat <=> cwiseLessThan(mat) or cwiseLT

<= mat <=> cwiseLessEqual(mat) or cwiseLE

< scalar <=> cwiseLessThan(scalar) or cwiseLT

<= scalar <=> cwiseLessEqual(scalar) or cwiseLE

// same for >, >=, ==, !=

+= scalar <=> cwiseAddInPlace(scalar)

-= scalar <=> cwiseSubInPlace(scalar)

*= mat <=> cwiseMulInPlace(scalar)

And we could also imagine to support all kind of bitwise operations: |, &, ~, ^, ! that we could name bitwiseXxx.

Now, I also acknowledge this drawback is mitigated by the introduction of an Array class, because then we can expect that the need to apply such operators on matrices will be significantly reduced.

Nevertheless, I think it is still important to be able to view matrix as array, and vice versa. Two options:

1 - We introduce easy to use .array() and .matrix() methods, and then we'll have both my proposal and your proposal, so again an infinite ways to do the same thing that is not very nice IMO.

2 - We assume such a conversion is worth it only if you use the view-as-array object multiple times, and so we enforce to use named wrapper objects, e.g.:

MatrixXf m = ...;

ArrayWrapper<MatrixXf> a(m);

// take care that "a" and "m" reference the same data.

a += 2;

Finally, there is another issue that I forgot to mention in my previous email and that go in favor of your proposal. All those cwise operators/functions are not only useful for dense objects but also for sparse matrices and certainly all other special storage objects (especially abs(), abs2(), etc.). Since we want to preserve a consistent API as well as to reduce code duplication, this means that with my proposal the Array class should be able to potentially wrap any kind of matrices.... No need to say that significantly complicate the implementation of the Array class (e.g., some will support only unary ops, some require to extend Array with other overloads, etc.). The implementation of your proposal is far much simpler because 1) Array would be reserved to dense storage, and 2) the cwise* functions won't be hidden into wrapper classes. (sorry if these last sentences does not make much sense to you, but believe me it's much simpler ;) )

So after all, I'm going to like your proposal, and will wait for other opinions.

As a side note, if we go for your proposal, we can also think about implementing in MatrixBase only a subset of the cwise ops available in Array.

Gael.

With the addition of Array, that leads to the following API. The idea is that a module would use either the left or the right column, but not mix expressions.

Matrix A,B Array A,B

matrix multiply: A * B A.matrixmul(B)

c-wise multiply: A.cwisemul(B) A * B

matrix exponential: A.exp() A.matrixexp()

c-wise exponential: A.cwiseexp() A.exp()

The discussion on whether to introduce global functions instead of (or in parallel of) member functions seems to be orthogonal to this, but just for illustration, this is how it would work with global functions.

Matrix A,B Array A,B

matrix multiply: A * B matrixmul(A,B)

c-wise multiply: cwisemul(A,B) A * B

matrix exponential: exp(A) matrixexp(A)

c-wise exponential: cwiseexp(A) exp(A)

Cheers,

Jitse

**Follow-Ups**:**Re: [eigen] inconsistent cwise() support***From:*Benoit Jacob

**References**:**[eigen] inconsistent cwise() support***From:*Hauke Heibel

**Re: [eigen] inconsistent cwise() support***From:*Benoit Jacob

**Re: [eigen] inconsistent cwise() support***From:*Hauke Heibel

**Re: [eigen] inconsistent cwise() support***From:*Benoit Jacob

**Re: [eigen] inconsistent cwise() support***From:*Gael Guennebaud

**Re: [eigen] inconsistent cwise() support***From:*Jitse Niesen

**Re: [eigen] inconsistent cwise() support***From:*Gael Guennebaud

**Messages sorted by:**[ date | thread ]- Prev by Date:
**Re: [eigen] Nesting by reference of by value ?** - Next by Date:
**Re: [eigen] Nesting by reference of by value ?** - Previous by thread:
**Re: [eigen] inconsistent cwise() support** - Next by thread:
**Re: [eigen] inconsistent cwise() support**

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