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

Return type of mean method

Tags: None
(comma "," separated)
di4jc8732
Registered Member
Posts
11
Karma
0

Return type of mean method

Tue Nov 08, 2011 6:01 pm
The documentation's example (Tutorial page 7) implies that the mean() method returns real numbers from integer matrix, but when I apply it to partial reduction of integer matrix, the return type appears to be integer vectors.

The following code prints integers throughout,
Code: Select all
MatrixXi tmp{ MatrixXi::Random(25,4) };
auto  ave= tmp.rowwise().mean() ;
cout << "Random matrix:\n" << tmp << endl;
cout << "The row-wise average is:\n" << ave << endl;

while this
Code: Select all
MatrixXi tmp{ MatrixXi::Random(25,4) };
VectorXd  ave= tmp.rowwise().mean() ;

won't compile.

Could someone enlighten me as to what is going on and update the documentation accordingly? Spending 2 hours discovering this was very unpleasant.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Return type of mean method

Tue Nov 08, 2011 10:08 pm
hm, the example of the tutorial uses a matrix of double.

In general in Eigen we never automatically promote integer to floating point numbers. Use .cast<double>() to switch to doubles.
di4jc8732
Registered Member
Posts
11
Karma
0

Re: Return type of mean method

Wed Nov 09, 2011 12:26 am
Well, I stand corrected.

But perhaps it would be better to populate the example with real numbers and not integers to accentuate the type of the matrix.

I would also argue that it doesn't make sense to force mean() to return the same type since that is contrary to mathematical custom. Not a big deal, just a nasty surprise.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Return type of mean method

Wed Nov 09, 2011 5:29 pm
ok, but to what type? double? float? rational numbers?

That said we could certainly add some warnings in the documentation about this fact.
jitseniesen
Registered Member
Posts
204
Karma
2

Re: Return type of mean method

Wed Nov 09, 2011 8:07 pm
Perhaps mean() on integers should yield a compile-time error, just like the entry-wise sqrt().
mattd
Registered Member
Posts
28
Karma
0

Re: Return type of mean method

Fri Nov 11, 2011 3:49 am
Mathematically, it makes sense to me for a mean's codomain to agree with its domain, mean : T -> T.
So, if mean takes ints, then mean returns ints.
This is consistent* with the behavior of operator+ and operator/ in terms of which (mathematically) mean is defined (by function composition, map-reduce style) -- just as integer addition and integer division produce integer results, so does their composition. Seems perfectly correct and natural, just as no one acquainted with the types of operations would expect the result of 5.0/2.0==2.5 to agree with 5/2==2.

* - http://en.wikipedia.org/wiki/Principle_of_compositionality
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Return type of mean method

Fri Nov 11, 2011 4:16 am
Just to add that I agree with mattd on this.


Bookmarks



Who is online

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