title: numpy vectorize multidimensional function
body: <p>I am having problems to vectorize a multidimensional function.<br>
  Consider the following example:</p>
  
  <pre><code>def _cost(u):
      return u[0] - u[1]
  
  cost = np.vectorize(_cost)
  
  &gt;&gt;&gt; x = np.random.normal(0, 1,(10, 2))
  &gt;&gt;&gt; cost(x)
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
    File "/Users/lucapuggini/MyApps/scientific_python_3_5/lib/python3.5/site-packages/numpy/lib/function_base.py", line 2218, in __call__
      return self._vectorize_call(func=func, args=vargs)
    File "/Users/lucapuggini/MyApps/scientific_python_3_5/lib/python3.5/site-packages/numpy/lib/function_base.py", line 2281, in _vectorize_call
      ufunc, otypes = self._get_ufunc_and_otypes(func=func, args=args)
    File "/Users/lucapuggini/MyApps/scientific_python_3_5/lib/python3.5/site-packages/numpy/lib/function_base.py", line 2243, in _get_ufunc_and_otypes
      outputs = func(*inputs)
  TypeError: _cost() missing 1 required positional argument: 'v'
  </code></pre>
  
  <p>Background information:
  I encountered the problem while trying to generalize the following code (Particle Swarm Optimization Algorithm) to multivariate data:</p>
  
  <pre><code>import numpy as np
  import matplotlib.pyplot as plt
  
  
  def pso(cost, sim, space_dimension, n_particles, left_lim, right_lim, f1=1, f2=1, verbose=False):
  
      best_scores = np.array([np.inf]*n_particles)
      best_positions = np.zeros(shape=(n_particles, space_dimension))
      particles = np.random.uniform(left_lim, right_lim, (n_particles, space_dimension))
      velocities = np.zeros(shape=(n_particles, space_dimension))
  
      for i in range(sim):
          particles = particles + velocities
          print(particles)
          scores = cost(particles).ravel()
          better_positions = np.argwhere(scores &lt; best_scores).ravel()
          best_scores[better_positions] = scores[better_positions]
          best_positions[better_positions, :] = particles[better_positions, :]
          g = best_positions[np.argmin(best_scores), :]
  
          u1 = np.random.uniform(0, f1, (n_particles, 1))
          u2 = np.random.uniform(0, f2, (n_particles, 1))
          velocities = velocities + u1 * (best_positions - particles) + u2 * (g - particles)
  
          if verbose and i % 50 == 0:
              print('it=', i, ' score=', cost(g))
  
  
              x = np.linspace(-5, 20, 1000)
              y = cost(x)
  
              plt.plot(x, y)
              plt.plot(particles, cost(particles), 'o')
              plt.vlines(g, y.min()-2, y.max())
              plt.show()
  
  
      return g, cost(g)
  
  
  
  
  def test_pso_1_dim():
  
      def _cost(x):
          if 0 &lt; x &lt; 15:
              return np.sin(x)*x 
          else:
              return 15 + np.min([np.abs(x-0), np.abs(x-15)])
  
      cost = np.vectorize(_cost)
  
      sim = 100
      space_dimension = 1
      n_particles = 5
      left_lim, right_lim = 0, 15
      f1, f2  = 1, 1
  
      x, cost_x = pso(cost, sim, space_dimension, n_particles,
                      left_lim, right_lim, f1, f2, verbose=False)
  
      x0 = 11.0841839
      assert np.abs(x - x0) &lt; 0.01
  
      return 
  </code></pre>
  
  <p>Please advise  me if vectorization is not a good idea in this case. </p>