[Numpy-discussion] Unexpected RuntimeWarning

Charles R Harris charlesr.harris at gmail.com
Fri Nov 23 13:01:13 EST 2012


On Fri, Nov 23, 2012 at 4:38 AM, Bob Dowling <rjd4+numpy at cam.ac.uk> wrote:

> I have a simple function defined in the following snippet:
>
>   --- start ---
> import numpy
>
> def chebyshev(x, m):
>      '''Calculates Chebyshev functions of the first kind using the
> trigonometric identities.'''
>
>      theta = numpy.where(
>          numpy.abs(x)<=1.0,
>          numpy.arccos(x),
>          numpy.arccosh(numpy.abs(x))
>          )
>
>      y = numpy.where(
>          numpy.abs(x)<=1.0,
>          numpy.cos(m*theta),
>          numpy.cosh(m*theta) * numpy.where(
>              x > 0.0,
>              1.0,
>              -1.0
>              )**m
>          )
>
>      return y
>
>
> if __name__ == '__main__':
>      x = numpy.linspace(-2.0, 2.0, 21)
>      y = chebyshev(x,3)
>      print(y)
>
>   --- end ---
>
>
> I'm using the numpy.where() call to extract only legal values for the
> circular and hyperbolic trigonometric functions.  But I still get
> warnings that I'm passing invalid anrguments:
>
> --- start---
>
> $ python3 demo.py
> demo.py:8: RuntimeWarning: invalid value encountered in arccos
>    numpy.arccos(x),
> demo.py:9: RuntimeWarning: invalid value encountered in arccosh
>    numpy.arccosh(numpy.abs(x))
> [ -2.60000000e+01  -1.79280000e+01  -1.15840000e+01  -6.77600000e+00
>    -3.31200000e+00  -1.00000000e+00   3.52000000e-01   9.36000000e-01
>     9.44000000e-01   5.68000000e-01  -1.83697020e-16  -5.68000000e-01
>    -9.44000000e-01  -9.36000000e-01  -3.52000000e-01   1.00000000e+00
>     3.31200000e+00   6.77600000e+00   1.15840000e+01   1.79280000e+01
>     2.60000000e+01]
>
>   --- end ---
>
> (I get the same with Python 2 so don't get excited about that.)
>
>
> I'm guessing that numpy.where() is evaluating the complete arccos and
> arccosh arrays and therefore getting invalid arguments.
>
> Now, I can turn off the warnings with numpy.seterr(invalid='ignore') but
> that's not what I would regard as good practice.
>
> Is there a "numpythonic" way to address the issue?
>

Are you aware of the Chebyshev series in numpy ?

In [1]: from numpy.polynomial import Chebyshev as T

In [2]: p = T([1,2,3])

In [3]: p(linspace(-2,2, 10))
Out[3]:
array([ 18.        ,   9.40740741,   3.18518519,  -0.66666667,
        -2.14814815,  -1.25925926,   2.        ,   7.62962963,
        15.62962963,  26.        ])




> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20121123/7af101e8/attachment.html>


More information about the NumPy-Discussion mailing list