[Numpy-discussion] Unexpected RuntimeWarning

Bob Dowling rjd4+numpy at cam.ac.uk
Fri Nov 23 07:07:51 EST 2012


> You may want to use this:
> http://docs.scipy.org/doc/numpy/reference/generated/numpy.piecewise.html

Thank you.  That's just what I needed.

Works a treat:
  --- start ---

import numpy

def chebyshev(x, m):
     '''Calculates Chebyshev functions of the first kind using the 
trigonometric identities.'''

     zone_a = x < -1.0
     zone_b = numpy.logical_and(-1.0 <= x, x <= 1.0)
     zone_c = 1.0 < x

     theta = numpy.piecewise(
         x,
         [zone_a, zone_b, zone_c],
         [lambda z: numpy.arccosh(numpy.abs(z)), numpy.arccos, 
numpy.arccosh]
         )

     y = numpy.piecewise(
         theta,
         [zone_a, zone_b, zone_c],
         [lambda z: (-1.0)**m*numpy.cosh(m*z), lambda z: numpy.cos(m*z), 
lambda z: numpy.cosh(m*z)]
         )

     return y


if __name__ == '__main__':
     x = numpy.linspace(-2.0, 2.0, 21)
     y = chebyshev(x,3)
     print(y)

  --- end---



More information about the NumPy-Discussion mailing list