title: Piecewise functions on Numpy Arrays
body: <p>What is efficient (speed) way to apply Piecewise functions on Numpy Array?</p>
  
  <p>Say, for example, Piecewise functions are like</p>
  
  <pre><code>For (1) :  x&lt;=2 f(x) = 2*x + x^2
      (2) :  x&gt;2  f(x) = -(x^2 + 2)
  </code></pre>
  
  <p>Here's what I did.</p>
  
  <pre><code>data = np.random.random_integers(5, size=(5,6))
  print data
  np.piecewise(data, [data &lt;= 2, data &gt; 2],
               [lambda x: 2*x + pow(2, x),
                lambda x: -(pow(x, 2) + 2)])
  
  data = 
  [[4 2 1 1 5 3]
   [4 3 3 5 4 5]
   [3 2 4 2 5 3]
   [2 5 4 3 1 4]
   [5 3 3 5 5 5]]
  output = 
  array([[-18,   8,   4,   4, -27, -11],
         [-18, -11, -11, -27, -18, -27],
         [-11,   8, -18,   8, -27, -11],
         [  8, -27, -18, -11,   4, -18],
         [-27, -11, -11, -27, -27, -27]])
  </code></pre>
  
  <p>Is there an efficient method to go by for smaller arrays, large arrays, many functions etc? My concern is with lambda functions being used. Not sure if these are Numpy optimized.</p>