title: Numpy array broadcasting rules
body: <p>I'm having some trouble understanding the rules for array broadcasting in Numpy.</p>
  
  <p>Obviously, if you perform element-wise multiplication on two arrays of the same dimensions and shape, everything is fine.  Also, if you multiply a multi-dimensional array by a scalar it works.  This I understand.</p>
  
  <p>But if you have two N-dimensional arrays of <em>different</em> shapes, it's unclear to me exactly what the broadcasting rules are.  This <a href="http://www.scipy.org/EricsBroadcastingDoc">documentation/tutorial</a> explains that: <em>In order to broadcast, the size of the <strong>trailing axes</strong> for both arrays in an operation must either be the same size or one of them must be one.</em></p>
  
  <p>Okay, so I assume by <em>trailing axis</em> they are referring to the <code>N</code> in a <code>M x N</code> array.  So, that means if I attempt to multiply two 2D arrays (matrices) with equal number of columns, it should work?  Except it doesn't...</p>
  
  <pre><code>&gt;&gt;&gt; from numpy import *
  &gt;&gt;&gt; A = array([[1,2],[3,4]])
  &gt;&gt;&gt; B = array([[2,3],[4,6],[6,9],[8,12]])
  &gt;&gt;&gt; print(A)
  [[1 2]
   [3 4]]
  &gt;&gt;&gt; print(B)
  [[ 2  3]
   [ 4  6]
   [ 6  9]
   [ 8 12]]
  &gt;&gt;&gt; 
  &gt;&gt;&gt; A * B
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  ValueError: shape mismatch: objects cannot be broadcast to a single shape
  </code></pre>
  
  <p>Since both <code>A</code> and <code>B</code> have two columns, I would have thought this would work.  So, I'm probably misunderstanding something here about the term "trailing axis", and how it applies to N-dimensional arrays.</p>
  
  <p>Can someone explain why my example doesn't work, and what is meant by "trailing axis"?</p>