help-octave
[Top][All Lists]

## Re: Applying function to vector by index

 From: Andy Adler Subject: Re: Applying function to vector by index Date: Thu, 12 Dec 2002 11:39:55 -0500 (EST)

```On Thu, 12 Dec 2002, Paul Kienzle wrote:
> On Tue, Dec 10, 2002 at 10:38:48AM +0100, Schloegl Alois wrote:
> > > I've got most of a patch in place, but there's all
> > > sorts of weird side cases.
> > >
> > > For example, what should sparse(eye(2)).^(1+1i) give?
> > >
> > > Matlab gives:
> > > >> full(sparse(eye(2)).^(1+1i))
> > >      1     0
> > >      0     1
> > >
> > > >> full(full(eye(2)).^(1+1i))
> > >    1.0000             NaN +    NaNi
> > >       NaN +    NaNi   1.0000
> > >
> > > That's because a^(b+ci) = e^(ln(a))*(b+ci)
> > > and ln(a) is NaN
> >
> > It should be
> >    a^(b+ci) = e^(ln(a)*(b+ci))
> >             = e^(ln(a)*b + ln(a)*ci)
> >             = e^(ln(a)*b)*e^(ln(a)*ci)
> >
> > ln(a) = [0,-inf;-inf,0]
> >
> > Because e^(ln(a)*i) is limited (abs(e^(ln(a)*i)<=1) and b>0, the result
> > converges. Hence, the result [1,0;0,1] is correct.
>
> However, for b <=0 the result should be NaN.
>
> Matlab is inconsistent in this case:
>
> >> eye(2).^(-2+i)
>    1.0000                NaN +    NaNi
>       NaN +    NaNi   1.0000
>
> >> sparse(eye(2)).^(-2+i)
>    (1,1)        1
>    (2,2)        1
>

Thanks for noticing this Paul.
It's quite a pain, to implement, though.
If the exponent is negative, the operation should fall
through to octave's default (which is to apply the convert
to matrix operator), but the sparse optimization should
be used if the exponent is non-negative.

Is it worth being right in this case, or is it
appropriate to be Matlab compatible?

Andy

-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------

```