help-octave
[Top][All Lists]

## Re: Applying function to vector by index

 From: Schloegl Alois Subject: Re: Applying function to vector by index Date: Thu, 12 Dec 2002 17:58:32 +0100 (MET) User-agent: IMP/PHP IMAP webmail program 2.2.8

```Zitiere Paul Kienzle <address@hidden>:

> 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))
> > >
> > > ans =
> > >
> > >      1     0
> > >      0     1
> > >
> > > >> full(full(eye(2)).^(1+1i))
> > >
> > > ans =
> > >
> > >    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)
>
> ans =
>
>    1.0000                NaN +    NaNi
>       NaN +    NaNi   1.0000
>
> >> sparse(eye(2)).^(-2+i)
>
> ans =
>
>    (1,1)        1
>    (2,2)        1
>

I'd call it flexibility:
sparse(a).^(b+i*c) evaluates only the non-zero elements of a.
The alternative is always possible with full(a).^(b+i*c)

Lets take another example from Octave:

octave-2.1.35:16> [0,0]*[nan nan]'
ans = NaN
octave-2.1.35:18> sparse([0,0])*[nan nan]'
ans = 0

For me, both examples are ok. Because it enables to skip [0*NaN] (using
SPARSE), and it enables to propagate [0*NaN] using FULL.
This flexibility is needed in some cases, and I don't want to miss it.

-------------------------------------------------------------
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
-------------------------------------------------------------

```