title: Adding different sized/shaped displaced NumPy matrices
body: <p>In short: I have two matrices (or arrays):</p>
  
  <pre><code>import numpy
  
  block_1 = numpy.matrix([[ 0, 0, 0, 0, 0],
                          [ 0, 0, 0, 0, 0],
                          [ 0, 0, 0, 0, 0],
                          [ 0, 0, 0, 0, 0]])
  
  block_2 = numpy.matrix([[ 1, 1, 1],
                          [ 1, 1, 1],
                          [ 1, 1, 1],
                          [ 1, 1, 1]])
  </code></pre>
  
  <p>I have the displacement of <code>block_2</code> in the <code>block_1</code> element coordinate system.</p>
  
  <pre><code>pos = (1,1)
  </code></pre>
  
  <p>I want to be able to add them (quickly), to get:</p>
  
  <pre><code>[[0 0 0 0 0]
   [0 1 1 1 0]
   [0 1 1 1 0]
   [0 1 1 1 0]]
  </code></pre>
  
  <p>In long: I would like a fast way to add two different shape matrices together, where one of the matrices can be displaced. The resulting matrix must have the shape of the first matrix, and the overlapping elements between the two matrices are summed. If there is no overlap, just the first matrix is returned unmutated.</p>
  
  <p>I have a function that works fine, but it's kind of ugly, and elementwise:</p>
  
  <pre><code>def add_blocks(block_1, block_2, pos):
      for i in xrange(0, block_2.shape[0]):
          for j in xrange(0, block_2.shape[1]):
              if (i + pos[1] &gt;= 0) and (i + pos[1] &lt; block_1.shape[0])
                 and (j + pos[0] &gt;= 0) and (j + pos[0] &lt; block_1.shape[1]):
                  block_1[pos[1] + i, pos[0] + j] += block_2[i,j]
      return block_1
  </code></pre>
  
  <p>Can broadcasting or slicing perhaps do this?</p>
  
  <p>I feel like maybe I'm missing something obvious.</p>