title: Apply a fonction to multiple vectors stored in a numpy array without loops
body: <p>I have a function taking two vectors with same size, making some calculations with them, and returning a third vector. Let's now consider that I have a multiple dimension array containing many vectors that I want to pass to my function as the first argument, and a fixed vector that I want to pass as the second argument. Below is an example. Is there a way to simplify the code by removing the loops?</p>
  <pre><code>def foo(x, y):
      result = np.zeros(x.shape)
      for i in range(y.size-1):
          result[i+1] = result[i] + (x[i+1] + x[i]) / (y[i+1] + y[i])
      return result
  
  a = np.arange(2*3*4).reshape(2,3,4)
  b = np.arange(4)*10
  c = np.ones(a.shape)*-9999.
  for i in range(a.shape[0]):
      for j in range(a.shape[1]):
          c[i, j, :] = foo(a[i, j, :], b)
  </code></pre>
  <p>Thanks for your help!</p>
  <p>EDIT: Below is the real function I'm trying to implement.</p>
  <pre><code>def press2alt(press, Tv):
      &quot;&quot;&quot;
      Convert pressure level to altitude level with hydrostatic atmosphere calculation (hypsometric
      equation). The altitude reference (z = 0 km) is taken for the largest pressure level.
      :param press: pressure level profile [hPa]
      :param Tv: virtual temperature profile [K]
      :return: altitude level profile [km]
      &quot;&quot;&quot;
      press_c = np.copy(press)
      if press[0] &lt; press[1]:
          press_c = press_c[::-1] # high press first
          Tv = Tv[::-1]
  
      alt = np.zeros(press_c.size)
      for i in range(alt.size-1):
          alt[i+1] = alt[i] + DRY_AIR_GAS_CST/STD_GRAV_ACC*(Tv[i+1]-Tv[i])* \
                     (np.log(press_c[i])-np.log(press_c[i+1]))/(np.log(Tv[i+1])-np.log(Tv[i]))
      
      if press[0] &lt; press[1]:
          alt = alt[::-1]
          
      return alt
  
  
  # Get altitude at each pressure level
  z = np.ones(tv.shape)*FILL_VALUE_FLOAT
      for i_month in range(tv.shape[0]):
          for i_lat in range(tv.shape[2]):
              for i_lon in range(tv.shape[3]):
                  z[i_month, :, i_lat, i_lon] = \
                      press2alt(pressure_level, tv[i_month, :, i_lat, i_lon])
  </code></pre>