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

unaryExpr() API sugar

Tags: None
(comma "," separated)
manuels
Registered Member
Posts
47
Karma
0

unaryExpr() API sugar

Thu Nov 18, 2010 11:03 am
Hi,

the use of a custom function for .unaryExpr() is a bit clumsy.
Using the CwiseUnaryOp struct is probably the most versatile approach, but in most cases the user just wants to specify a specific function Scalar func(Scalar).

Thus I wrote this piece of code:
Code: Select all
  template<typename Scalar, Scalar Func(Scalar)>
  struct CwiseOp {
    const Scalar operator()(const Scalar& x) const { return Func(x); }
  };


Though, it would be great, if one could use .unaryExpr<func_name>() additionally to .unaryExpr(CwiseUnaryOp).
So .unaryExpr<func_name>() would just call .unaryExpr( CwiseOp<Scalar, func_name> ).

The only problem with my struct is, that you have to declare it for any return and argument type:
Code: Select all
Scalar Func(Scalar)
Scalar Func(const Scalar)
Scalar Func(const Scalar&)
Scalar Func(Scalar &)
const Scalar Func(Scalar)
const Scalar& Func(Scalar&)
const Scalar& Func(const Scalar&)
const Scalar Func(const Scalar)
const Scalar& Func(const Scalar&)
...


What do think about that?
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: unaryExpr() API sugar  Topic is solved

Thu Nov 18, 2010 12:20 pm
Hi,

you can already do that using STL's ptr_fun construct, e.g.:

B = A.unaryExpr(std::ptr_fun(fabs));
manuels
Registered Member
Posts
47
Karma
0

Re: unaryExpr() API sugar

Thu Nov 18, 2010 1:06 pm
oh, ok. Thanks!

Would be great if it would be added to the documentation of unaryExpr(), because I guess this is a common problem.


Cheers,

Manuel
manuels
Registered Member
Posts
47
Karma
0

Re: unaryExpr() API sugar

Tue Feb 15, 2011 11:47 am
Hi, it's me again!

Is there any possibility to execute the unaryExpr() only on non-zero elements of a sparse matrix?

Cheers,

Manuel
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: unaryExpr() API sugar

Tue Feb 15, 2011 3:21 pm
this is already the case.
manuels
Registered Member
Posts
47
Karma
0

Re: unaryExpr() API sugar

Tue Feb 15, 2011 3:29 pm
Oh, ok. That was an easy one! ;D


Bookmarks



Who is online

Registered users: bartoloni, Bing [Bot], Evergrowing, Google [Bot], ourcraft