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

Return scalar array

Tags: None
(comma "," separated)
kp0987
Registered Member
Posts
21
Karma
0

Return scalar array

Fri Oct 14, 2011 5:04 pm
What would be the best way to convert the following to a generic ArrayBase type?

Code: Select all
inline Eigen::ArrayXd db10(Eigen::ArrayXd x) { return 10.0/log(10.0) * x.abs().log(); };
inline Eigen::ArrayXd db10(Eigen::ArrayXcd x) { return 10.0/log(10.0) * x.abs().log(); };


I think I can figure out the first case (both argument and return are double), but the second case (argument is complex, return is double) is not clear to me.

Thanks.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Return scalar array

Fri Oct 14, 2011 7:37 pm
you can use typename Derived::RealScalar for the scalar type of the returned Array object.
kp0987
Registered Member
Posts
21
Karma
0

Re: Return scalar array

Tue Oct 18, 2011 9:15 pm
Associated with this (maybe if I fix this one, I can fix my original issue), I have working code for an inplace function:
Code: Select all
  template <typename Derived>
  void my_swap_inplace(const Eigen::DenseBase<Derived>& x_)
  {
    Eigen::DenseBase<Derived>& x = const_cast< Eigen::DenseBase<Derived>& >(x_);
    x.block(0,0,x.rows()/2,x.cols()).swap(x.block(x.rows()/2,0,x.rows()/2,x.cols()));
  }


And I want a non-inplace version (one returning a value). I've tried various code fragments like:
Code: Select all
  template <typename Derived, typename OtherDerived>
  typename Eigen::DenseBase<Derived>::PlainObject my_swap(const Eigen::DenseBase<OtherDerived>& x)
  {
    typename Eigen::DenseBase<Derived>::PlainObject y = x.derived();   
    y.block(0,0,y.rows()/2,y.cols()).swap(y.block(y.rows()/2,0,y.rows()/2,y.cols()));
    return y;
  }

But have not been successful in getting to compile. Any ideas? Thanks.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Return scalar array

Wed Oct 19, 2011 7:10 am
remove the OtherDerived template parameter, and use Derived everywhere.
kp0987
Registered Member
Posts
21
Karma
0

Re: Return scalar array

Wed Oct 19, 2011 5:13 pm
Thanks. That's what I tried first:
Code: Select all
  template <typename Derived>
  typename Eigen::DenseBase<Derived>::PlainObject my_swap(const Eigen::DenseBase<Derived>& x)
  {
    typename Eigen::DenseBase<Derived>::PlainObject y = x.derived();   
    y.block(0,0,y.rows()/2,y.cols()).swap(y.block(y.rows()/2,0,y.rows()/2,y.cols()));
    return y;
  }


But get a "No matching function" error on compile.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Return scalar array

Mon Oct 24, 2011 1:55 pm
How do you call it? Perhaps you could also directly use the input type:

template <typename T>
typename T::PlainObject my_swap(const T& x)
{
typename T::PlainObject y = x.derived();
...
kp0987
Registered Member
Posts
21
Karma
0

Re: Return scalar array

Fri Oct 28, 2011 7:09 pm
Thanks. Directly using the input type worked fine.

For the initial problem of returning a real scalar I ended up with:
Code: Select all
template <typename Derived>
typename Eigen::Array<typename Derived::RealScalar, Derived::RowsAtCompileTime, Derived::ColsAtCompileTime>::PlainObject
db10(const Eigen::ArrayBase<Derived>& x)

  return 10.0/log(10.0) * x.derived().abs().log();
}


which seems to work fine for all array types. Please let me know if there was a better way (other than the ReturnByValue class I've seen described at http://listengine.tuxfamily.org/lists.t ... 00216.html which is probably overkill for this case). Thanks.


Bookmarks



Who is online

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