numpy.arccos(numpy.inf)????

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

numpy.arccos(numpy.inf)????

Stuart Brorson
Hi --

Sorry to be a pest with corner cases, but I found another one.

In this case, if you try to take the arccos of numpy.inf in the
context of a complex array, you get a bogus return (IMO).  Like this:

In [147]: R = numpy.array([1, numpy.inf])

In [148]: numpy.arccos(R)
Warning: invalid value encountered in arccos
Out[148]: array([  0.,  NaN])

In [149]: C = numpy.array([1+1j, numpy.inf])

In [150]: numpy.arccos(C)
Warning: invalid value encountered in arccos
Out[150]: array([ 0.90455689-1.06127506j,         NaN       -Infj])

The arccos(numpy.inf) in the context of a real array is OK, but taking
arcocs(numpy.inf) in the context of a complex array should return
NaN + NaNj, IMO.

Thoughts?

Stuart Brorson
Interactive Supercomputing, inc.
135 Beaver Street | Waltham | MA | 02452 | USA
http://www.interactivesupercomputing.com/

_______________________________________________
Numpy-discussion mailing list
[hidden email]
http://projects.scipy.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

Re: numpy.arccos(numpy.inf)????

Robert Kern-2
On Fri, May 16, 2008 at 11:47 AM, Stuart Brorson <[hidden email]> wrote:

> Hi --
>
> Sorry to be a pest with corner cases, but I found another one.
>
> In this case, if you try to take the arccos of numpy.inf in the
> context of a complex array, you get a bogus return (IMO).  Like this:
>
> In [147]: R = numpy.array([1, numpy.inf])
>
> In [148]: numpy.arccos(R)
> Warning: invalid value encountered in arccos
> Out[148]: array([  0.,  NaN])
>
> In [149]: C = numpy.array([1+1j, numpy.inf])
>
> In [150]: numpy.arccos(C)
> Warning: invalid value encountered in arccos
> Out[150]: array([ 0.90455689-1.06127506j,         NaN       -Infj])
>
> The arccos(numpy.inf) in the context of a real array is OK, but taking
> arcocs(numpy.inf) in the context of a complex array should return
> NaN + NaNj, IMO.
>
> Thoughts?

Hmm, this works fine on OS X. This may be a problem with one of the
lower-level math functions which we defer to the platform.


In [1]: from numpy import *

In [2]: arccos(nan+nan*1j)
Out[2]: (nan+nanj)

In [3]: arccos(nan+0j)
Out[3]: (nan+nanj)

In [4]: arccos(nan)
Out[4]: nan

In [5]: arccos([1.0+0j, nan])
Out[5]: array([  0. -0.j,  NaN NaNj])


The implementations of the complex versions are in umathmodule.c.src
(for the expanded versions, see umathmodule.c after building); they
are all prefixed with "nc_". E.g. the following are calling nc_acos()
for doubles. Here is the source:

static void
nc_acos(cdouble *x, cdouble *r)
{
    nc_prod(x,x,r);
    nc_diff(&nc_1, r, r);
    nc_sqrt(r, r);
    nc_prodi(r, r);
    nc_sum(x, r, r);
    nc_log(r, r);
    nc_prodi(r, r);
    nc_neg(r, r);
    return;
    /* return nc_neg(nc_prodi(nc_log(nc_sum(x,nc_prod(nc_i,
       nc_sqrt(nc_diff(nc_1,nc_prod(x,x))))))));
    */
}

I suspect the problem comes from the nc_log() which calls your
platform's atan2() for the imaginary part of the result.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
 -- Umberto Eco
_______________________________________________
Numpy-discussion mailing list
[hidden email]
http://projects.scipy.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

Re: numpy.arccos(numpy.inf)????

Robert Kern-2
On Fri, May 16, 2008 at 1:37 PM, Robert Kern <[hidden email]> wrote:

> On Fri, May 16, 2008 at 11:47 AM, Stuart Brorson <[hidden email]> wrote:
>> Hi --
>>
>> Sorry to be a pest with corner cases, but I found another one.
>>
>> In this case, if you try to take the arccos of numpy.inf in the
>> context of a complex array, you get a bogus return (IMO).  Like this:
>>
>> In [147]: R = numpy.array([1, numpy.inf])
>>
>> In [148]: numpy.arccos(R)
>> Warning: invalid value encountered in arccos
>> Out[148]: array([  0.,  NaN])
>>
>> In [149]: C = numpy.array([1+1j, numpy.inf])
>>
>> In [150]: numpy.arccos(C)
>> Warning: invalid value encountered in arccos
>> Out[150]: array([ 0.90455689-1.06127506j,         NaN       -Infj])
>>
>> The arccos(numpy.inf) in the context of a real array is OK, but taking
>> arcocs(numpy.inf) in the context of a complex array should return
>> NaN + NaNj, IMO.
>>
>> Thoughts?
>
> Hmm, this works fine on OS X.

Sorry, I'm an idiot. I get the same results as you when I read the
message correctly.

In [2]: arccos(inf+0j)
Out[2]: (nan+-infj)

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
 -- Umberto Eco
_______________________________________________
Numpy-discussion mailing list
[hidden email]
http://projects.scipy.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

Re: numpy.arccos(numpy.inf)????

Anne Archibald
In reply to this post by Stuart Brorson
2008/5/16 Stuart Brorson <[hidden email]>:

> Hi --
>
> Sorry to be a pest with corner cases, but I found another one.
>
> In this case, if you try to take the arccos of numpy.inf in the
> context of a complex array, you get a bogus return (IMO).  Like this:
>
> In [147]: R = numpy.array([1, numpy.inf])
>
> In [148]: numpy.arccos(R)
> Warning: invalid value encountered in arccos
> Out[148]: array([  0.,  NaN])
>
> In [149]: C = numpy.array([1+1j, numpy.inf])
>
> In [150]: numpy.arccos(C)
> Warning: invalid value encountered in arccos
> Out[150]: array([ 0.90455689-1.06127506j,         NaN       -Infj])
>
> The arccos(numpy.inf) in the context of a real array is OK, but taking
> arcocs(numpy.inf) in the context of a complex array should return
> NaN + NaNj, IMO.
>
> Thoughts?

Is this so bad?

lim x->inf arccos(x) = -j inf

Granted this is only when x approaches infinity along the positive real axis...

Anne
_______________________________________________
Numpy-discussion mailing list
[hidden email]
http://projects.scipy.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

Re: numpy.arccos(numpy.inf)????

Christian Heimes-2
In reply to this post by Stuart Brorson
Stuart Brorson schrieb:
> Hi --
>
> Sorry to be a pest with corner cases, but I found another one.

[...]

Mark and I spent a *lot* of time in fixing those edge cases in Python
2.6 and 3.0. We used the C99 standard as template. I recommend that you
look at our code.

Christian

_______________________________________________
Numpy-discussion mailing list
[hidden email]
http://projects.scipy.org/mailman/listinfo/numpy-discussion