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

mean() in PartialRedux

Tags: None
(comma "," separated)
User avatar
marton
Registered Member
Posts
10
Karma
0
OS

mean() in PartialRedux

Wed Apr 29, 2009 5:38 pm
Hi,

I'm trying to implement mean(). In Redux.h, it works:
Code: Select all
template
EIGEN_STRONG_INLINE typename ei_traits::Scalar
MatrixBase::mean() const
{
    NumTraits::Scalar>::Real n = size();
    return sum() / n;
}


In PartialRedux.h, however, this code gives a segfault...
Code: Select all
const CwiseUnaryOp::Real>, typename ReturnType::Type>
    mean() const
{
    NumTraits::Real n = (Direction==Vertical ? m_matrix.rows() : m_matrix.cols());
    return sum() / n;
}


Can anybody point me in the right direction? I've got no idea what I'm doing wrong.

Thanks!

Last edited by marton on Wed Apr 29, 2009 5:39 pm, edited 1 time in total.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

RE: mean() in PartialRedux

Wed Apr 29, 2009 7:42 pm
marton wrote:In PartialRedux.h, however, this code gives a segfault...
Code: Select all
const CwiseUnaryOp::Real>, typename ReturnType::Type>
    mean() const
{
    NumTraits::Real n = (Direction==Vertical ? m_matrix.rows() : m_matrix.cols());
    return sum() / n;
}


Can anybody point me in the right direction? I've got no idea what I'm doing wrong.

Thanks!


this is because you return a temporary by reference. here is the fix:
Code: Select all
const CwiseUnaryOp::Real>, NestByValue::Type> >
    mean() const
{
    NumTraits::Real n = (Direction==Vertical ? m_matrix.rows() : m_matrix.cols());
    return sum().nestByValue() / n;
}


btw, one of the TODO item is a Statistic module where the goal would be to have such {matrix,column,row}-wise functions, just in case you are interested in starting it ;)


Bookmarks



Who is online

Registered users: Bing [Bot], Evergrowing, Google [Bot], rblackwell