title: Vectorize a for loop in numpy to calculate duct-tape overlaping
body: <p>I'm creating an application with python to calculate duct-tape overlapping (modeling a dispenser applies a product on a rotating drum).</p>
  
  <p>I have a program that works correctly, but is really slow. I'm looking for a solution to optimize a <code>for</code> loop used to fill a numpy array. Could someone help me vectorize the code below?</p>
  
  <pre><code>import numpy as np
  import matplotlib.pyplot as plt
  
  # Some parameters
  width = 264
  bbddiam = 940
  accuracy = 4 #2 points per pixel
  
  drum = np.zeros(accuracy**2 * width * bbddiam).reshape((bbddiam * accuracy , width * accuracy))
  
  # The "slow" function
  def line_mask(drum, coef, intercept, upper=True, accuracy=accuracy):
      """Masks a half of the array"""
      to_return = np.zeros(drum.shape)
      for index, v in np.ndenumerate(to_return):
          if upper == True:
              if index[0] * coef + intercept &gt; index[1]:
                  to_return[index] = 1
          else:
              if index[0] * coef + intercept &lt;= index[1]:
                  to_return[index] = 1
      return to_return
  
  
  def get_band(drum, coef, intercept, bandwidth):
      """Calculate a ribbon path on the drum"""
      to_return = np.zeros(drum.shape)
      t1 = line_mask(drum, coef, intercept + bandwidth / 2, upper=True)
      t2 = line_mask(drum, coef, intercept - bandwidth / 2, upper=False)
      to_return = t1 + t2
      return np.where(to_return == 2, 1, 0)
  
  single_band = get_band(drum, 1 / 10, 130, bandwidth=15)
  
  # Visualize the result !
  plt.imshow(single_band)
  plt.show()
  </code></pre>
  
  <p>Numba does wonders for my code, reducing runtime from 5.8s to 86ms  (special thanks to @Maarten-vd-Sande):</p>
  
  <pre><code>from numba import jit
  @jit(nopython=True, parallel=True)
  def line_mask(drum, coef, intercept, upper=True, accuracy=accuracy):
      ...
  </code></pre>
  
  <p>A better solution with numpy is still welcome ;-)</p>