Python Numpy : operands could not be broadcast together with shapes when producting matrix
<pre><code>import numpy as np
def grams(A):
    (m,n) = A.shape
    Q = A
    R = np.zeros((n,n))
    for i in range(0,n-1):
        R[i,i] = np.linalg.norm(Q[:,i])
        Q[:,i] =Q[:,i]/R[i,i]
        R[i,i+1:n] = np.transpose(Q[:,i])*Q[:,i+1:n]
        Q[:,i+1:n] = Q[:,i+1:n+1]-Q[:,i]*R[i,i+1:n]
    R[n,n] = np.linalg.norm(Q[:,n])
    Q[:,n] = Q[:,n]/R[n,n] 
    return Q, R
A = np.array( [[1,1,2],[4,3,1],[1,6,6]] )
print grams(A)
</code></pre>

<p>The error is on the line </p>

<pre><code>R[i,i+1:n] = np.transpose(Q[:,i])*Q[:,i+1:n]
</code></pre>

<p>The error is </p>

<pre><code>ValueError:
    operands could not be broadcast together with shapes (3,) (3,2) 
</code></pre>

<p>Lets recreate this in a small case</p>

<pre><code>In [105]: n=4
In [106]: m,n=2,4
In [107]: Q=np.arange(m*n).reshape(m,n)
In [108]: R=np.zeros((n,n))
</code></pre>

<p>So for one step, the target is a (3,) slot in <code>R</code></p>

<pre><code>In [109]: i=0
In [110]: R[i,i+1:n]
Out[110]: array([ 0.,  0.,  0.])
In [111]: Q[:,i]
Out[111]: array([0, 4])
In [112]: Q[:,i+1:n]
Out[112]: 
array([[1, 2, 3],
       [5, 6, 7]])
</code></pre>

<p>One array is (m,), the other (m,3).  The only way to combine them and produce (3,) is with a <code>dot</code> (using last/only dim of one, and 2nd last dim of the other):</p>

<pre><code>In [113]: np.dot(Q[:,i],Q[:,i+1:n])
Out[113]: array([20, 24, 28])
</code></pre>

python numpy