Re: [eigen] Sign function
• To: eigen <eigen@xxxxxxxxxxxxxxxxxxx>
• Subject: Re: [eigen] Sign function
• From: Gael Guennebaud <gael.guennebaud@xxxxxxxxx>
• Date: Sat, 8 Dec 2012 14:08:25 +0100
• Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=/kAMAMx/rs4P8HXOrKw51NLmS9OWhaJu9afeTltVczo=; b=oXhkri4Z7n+2UCEs2d+5nDtsqyw1ijsGP/3hyRc++W5XkZVwEDu9tEPu1XpmpSMpvl FLuYliiHWsZ8z8xSpFdWzhk1S2mhjN23pvjNKQxMHTMdqvIoAMbRgOW2YyOLtby1Ne4x SVZGbvZ4s5iDWU6sW79ZjHUK5IHSnZ5Xw7fQNvhDAuT6C1wdhmjsRvFIoGECLq2niWcn EcPebCVTJ52LEnR8TjBGpWSV+0/vl0ZyUIAQybzjWqu4G3N02l8sLPKFPufOq8vEExxF 6m4/GGy0513iDoKcSG+Xo9zmYCOdiYNOVz7eBrlEp2rE8QQHF6PGyc6oKVX+Xy4wlOKF RYpw==

You can do something like:

A += (B.array()>0)..select(MatType::Constant(k),-k);

Unfortunately the following cannot work because we have to know the matrix type returned by select from at least one of its arguments:

A += (B.array()>0).select(k,-k);

You can also use B.unaryExpr(std::ptr_fun(sign)) to reproduce "sign(B)".

gael

On Sat, Dec 8, 2012 at 12:26 PM, Carlos Becker wrote:
Hello everyone,

I am wondering which would be the fast way to compute the sign of every element in a vector or matrix, since I have to do the following element-wise:

A = A + k * sign(B)

where A and B are double matrices and k is a scalar.

I was trying to find some information in the docs but so far I didn't succeed.
I know I can do a boolean operation and then cast to double, multiply by 2 and subtract 1, but that doesn't seem very efficient.

Maybe some kind of custom element-wise processing, since it would be enough to compute the sign and then add k or -k to each element of A ?

Thanks!

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