title: Broadcasting a python function on to numpy arrays
body: <p>Let's say we have a particularly simple function like</p>
  
  <pre><code>import scipy as sp
  def func(x, y):
     return x + y
  </code></pre>
  
  <p>This function evidently works for several builtin python datatypes of <code>x</code> and <code>y</code> like string, list, int, float, array, etc. Since we are particularly interested in arrays, we consider two arrays:</p>
  
  <pre><code>x = sp.array([-2, -1, 0, 1, 2])
  y = sp.array([-2, -1, 0, 1, 2])
  
  xx = x[:, sp.newaxis]
  yy = y[sp.newaxis, :]
  
  &gt;&gt;&gt; func(xx, yy)
  </code></pre>
  
  <p>this returns</p>
  
  <pre><code>array([[-4, -3, -2, -1,  0],
    [-3, -2, -1,  0,  1],
    [-2, -1,  0,  1,  2],
    [-1,  0,  1,  2,  3],
    [ 0,  1,  2,  3,  4]])
  </code></pre>
  
  <p>just as we would expect.</p>
  
  <p>Now what if one wants to throw in arrays as the inputs for the following function?</p>
  
  <pre><code>def func2(x, y):
    if x &gt; y:
       return x + y
    else:
       return x - y
  </code></pre>
  
  <p>doing <code>&gt;&gt;&gt;func(xx, yy)</code> would raise an error.</p>
  
  <p>The first obvious method that one would come up with is the <code>sp.vectorize</code> function in scipy/numpy. This method, nevertheless has been proved to be not very efficient. Can anyone think of a more robust way of broadcasting any function in general on to numpy arrays?</p>
  
  <p>If re-writing the code in an array-friendly fashion is the only way, it would help if you could mention it here too.</p>