Re: [eigen] How to raise double to array powers in Eigen
• To: eigen@xxxxxxxxxxxxxxxxxxx
• Subject: Re: [eigen] How to raise double to array powers in Eigen
• From: Ian Bell <ian.h.bell@xxxxxxxxx>
• Date: Mon, 18 Aug 2014 19:30:54 +0200
• 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=0bxtkrSXE4c4HVTR5cQyp7WzdGnVLPgjxnePuzDTuL0=; b=lz0d2skAgX86IxYSHfZrC/6zifKuP0aY4JF7mkGC7ebJQvRpcsd8vJE4BVbXdbMlT6 VimoVJgG6TCYx6vQmIDwgKxPGfjTgMSiUQLLexYVdxbKUbCVMjQGtk4ERaqG9ynebub6 9Hduq41+Kjy/vIDT4hIOzy8kU5CA5GOjjdFJghZDq5mIGCZb96jW6HZ9TRaqE07uLjC7 i/rtZU4R3S37hTNbtdA9lt0JZ97sxMb8IwHBbCss/iOXRpE9WEZkV4wEsDPHtsHWOeJX thRIrcBPUY/AkDhu37C5zLZwN6h30JEaf+e0TpPin8L2JXRllb2uTvjOTCBehh7sEEvb 8YBg==

Chen-Pang,

Can you provide a bit more detail on the exponentiation by squaring?  How would I re-write my simple c++ loop above using your method?

Thanks,
Ian

On Mon, Aug 18, 2014 at 2:30 PM, Chen-Pang He wrote:
If you need no optimization and want functional programming,
ArrayBase::unaryExpr with a homemade functor is enough.

If you need optimization, because all exponents are integers,
binary powering (aka exponentiation by squaring) is *the*
algorithm.   We can reuse `pow(delta, (1 << k))`.

Cheers,
Chen-Pang

> On Mon, Aug 18, 2014 at 1:28 PM, Christoph Hertzberg <
> chtz@xxxxxxxxxxxxxxxxxxxxxxxx> wrote:
>
> > On 18.08.2014 00:23, Ian Bell wrote:
> >
> >> cross-posted to stack overflow...
> >>
> >
> > If you cross-post, could you post a link as well?
>
> http://stackoverflow.com/questions/25354205/how-to-raise-double-to-array-powers-in-eigen
>
> >
> >
> >  I need to raise a double value to an array of integer powers, in c++ this
> >> would look something like
> >>
> >
> > Could you be a bit more specific about the "array of integer powers" (I
> > guess 'exponents')?
> > If they are a sequence 0,1,2,3,... your proposed solution is definitely
> > very inefficient. If they are of great range, with no apparent pattern, I
> > would say the exp(log(delta)*exponent) approach is basically the most
> > efficient solution (maybe using log2 and exp2 would be more efficient, if
> > they were available).
> >
>
> The exponents are typically in the range [0, 10), and can be sorted if that
> is of use.  They are not necessarily linearly increasing, we might have
> exponents 0,0,0,1,1,1,1,2,2 for instance.  Obviously it would be better to
> short-circuit the 0 powers as well since they yield 1 again (x^0=1).  This
> method with the exp(log(delta)*exponent) also doesn't allow you to skip
> those powers.  I think that the >0 comparison should be cheaper than doing
> pow(x, 0) when 0 is integer.  Especially this will be true when using exp().
--

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