title: Numpy: Subtract array element by element
body: <p>The title might be ambiguous, didn't know how else to word it.</p>
  
  <p>I have gotten a bit far with my particle simulator in python using numpy and matplotlib, I have managed to implement coloumb, gravity and wind, now I just want to add temperature and pressure but I have a pre-optimization question (root of all evil). I want to see when particles crash:</p>
  
  <p><strong>Q:</strong> Is it in numpy possible to take the difference of an array with each of its own element based on a bool condition? I want to avoid looping.</p>
  
  <p>Eg: <code>(x - any element in x) &lt; a</code>
  Should return something like</p>
  
  <pre><code>[True, True, False, True]
  </code></pre>
  
  <p>If element 0,1 and 3 in x meets the condition. </p>
  
  <p><strong>Edit:</strong></p>
  
  <p>The loop quivalent would be:</p>
  
  <pre><code>for i in len(x):
    for j in in len(x):
      #!= not so important
      ##earlier question I asked lets me figure that one out
      if i!=j: 
        if x[j] - x[i] &lt; a:
         True
  </code></pre>
  
  <p>I notice numpy operations are far faster than if tests and this has helped me speed up things ALOT.</p>
  
  <p>Here is a sample code if anyone wants to play with it.</p>
  
  <pre><code>#Simple circular box simulator, part of part_sim
  #Restructure to import into gravity() or coloumb () or wind() or pressure()
  #Or to use all forces: sim_full()
  #Note: Implement crashing as backbone to all forces
  
  import numpy as np
  import matplotlib.pyplot as plt
  
  N = 1000        #Number of particles
  R = 8000        #Radius of box          
  r = np.random.randint(0,R/2,2*N).reshape(N,2)
  v = np.random.randint(-200,200,r.shape)
  v_limit = 10000 #Speedlimit
  
  
  plt.ion()
  line, = plt.plot([],'o')
  plt.axis([-10000,10000,-10000,10000])
  
  while True:
      r_hit = np.sqrt(np.sum(r**2,axis=1))&gt;R   #Who let the dogs out, who, who?
      r_nhit = ~r_hit                     
      N_rhit = r_hit[r_hit].shape[0]
      r[r_hit] = r[r_hit] - 0.1*v[r_hit]       #Get the dogs back inside
      r[r_nhit] = r[r_nhit] +0.1*v[r_nhit]
      #Dogs should turn tail before they crash!
      #---
      #---crash code here....
      #---crash end
      #---
      vmin, vmax = np.min(v), np.max(v)        
      #Give the particles a random kick when they hit the wall
      v[r_hit]  = -v[r_hit] + np.random.randint(vmin, vmax, (N_rhit,2))
      #Slow down honey
      v_abs = np.abs(v) &gt; v_limit
      #Hit the wall at too high v honey? You are getting a speed reduction
      v[v_abs] *=0.5
      line.set_ydata(r[:,1])
      line.set_xdata(r[:,0])
      plt.draw()
  </code></pre>
  
  <p>I plan to add colors to the datapoints above once I figure out how...such that high velocity particles can easily be distinguished in larger boxes.</p>