Re: [eigen] calculating on ranges |
[ Thread Index |
Date Index
| More lists.tuxfamily.org/eigen Archives
]
- To: eigen@xxxxxxxxxxxxxxxxxxx
- Subject: Re: [eigen] calculating on ranges
- From: Benoit Jacob <jacob.benoit.1@xxxxxxxxx>
- Date: Tue, 27 Jan 2009 14:04:08 +0100
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:content-type :content-transfer-encoding; bh=RpYrqU990sIosRykHZK5dhpdjzf2StlK2WTnvbN77Qk=; b=esyk/V2X6gSSkhzOXuvsjJoX1jXac2SqQOo02j8fE+SRHo2RZVlmvh4GtnopifE8vE PBtPUR9tFIvFnyyoqXZfsUmNLhmPmQvFUNmwe3TktRQbkVtKuU24Z9dOspxNE48YMi1T ZSsPzXTwgMI1DIkLYMMAnmbWOUzjx1syidP68=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; b=YMc5/gyYf8ObZ9RMB46bkcyJrI6aFa6MRes3OdBWoRdZUVpZuiwoYoBc4xrUwcWCYc d8wjMMMyeEF+jcTAlNj5IBW3stHWLCFJkLty6DLfbnh1M1h9/HUHRCHZ+tKNDbFbHutQ hegSMKQouQdJePyxEwwpbCiXUmhTjTrWQDDNg=
Hi,
Yes, Eigen's lazy evaluation means that if you implement this with
Eigen it will also evaluate the function only on the range that's
needed.
Compared to your C function, I don't know if you would prefer the
Eigen way or not. Normally, Eigen would bring the advantage of
vectorization, except that here you use exp and that is not yet
vectorized (it could be if that were important. we would use Pade
approximants if there are no SSE instructions for that.). The other
advantage of using Eigen is that it will automatically unroll the loop
if the size (here 10) is known at compile-time and the total cost
isn't too big (you can tune EIGEN_UNROLLING_LIMIT).
Here is a basic Eigen implementation:
#include <Eigen/Array>
template<typename VectorType>
void addGaussianToCurve(
const MatrixBase<VectorType>& x,
const MatrixBase<VectorType>& y,
typename VectorType::Scalar a,
typename VectorType::Scalar b,
typename VectorType::Scalar c)
{
y += a * ( -(x.cwise() - b).cwise().square() / c ).cwise().exp();
}
Now if you have vectors VectorXd x,y; you can call your function like this:
addGaussianToCurve(
x.segment(start,length),
y.segment(start,length),
a,b,c);
Now if you know length at compile time you can do:
addGaussianToCurve(
x.segment<length>(start),
y.segment<length>(start),
a,b,c);
And it will automatically unroll the loop if appropriate (see above).
Again if vectorization matters it's no big deal, we just need a packet
version of exp(). You'd then benefit from it automatically.
Cheers,
Benoit