Defining a complex-valued, piecewise function
<p>I'm trying to define a function f(x) which yields 1.0 for x = 0 and 1.0/(2j pi x) otherwise. Here is a 'test' script I'm using:</p>

<pre><code>import numpy as np

def f(x):
    return np.piecewise(x,[x==0],[1.0, lambda x: 1.0/(2j*np.pi*x)])

x = np.linspace(-1,1,21)

print(x)
print(f(x))
</code></pre>

<p>If the lambda function is not complex-valued, this works as I would expect. However, with the "2j" term I get the following warning and output:</p>

<pre><code>/usr/local/lib/python2.7/dist-packages/numpy/lib/function_base.py:1144: ComplexWarning: Casting complex values to real discards the imaginary part
  y[condlist[k]] = item(vals, *args, **kw)
[-1.  -0.9 -0.8 -0.7 -0.6 -0.5 -0.4 -0.3 -0.2 -0.1  0.   0.1  0.2  0.3  0.4
  0.5  0.6  0.7  0.8  0.9  1. ]
[-0. -0. -0. -0. -0. -0. -0. -0. -0. -0.  1.  0.  0.  0.  0.  0.  0.  0.
  0.  0.  0.]
</code></pre>

<p>Apparently numpy's <code>piecewise</code> automatically takes the real part. Is this not quite a limitation of <code>piecewise</code>? Is there another way to define piecewise complex-valued functions (without having to define the real and imaginary parts separately)?</p>