Search code examples
pythonarrayspython-3.xnumpymatrix-multiplication

Matrix multiplication of numpy ndarrays


I have a two numpy arrays a, B like this.

>> a 
[1 2 3]
>> type(a)
<class 'numpy.ndarray'>
>> B
[[1 2 3]
 [2 2 7]
 [3 4 6]]
>> type(B)
<class 'numpy.ndarray'>

I want to do the matrix multiplication like a * B * a_transpose which is (1*3)*(3*3)*(3*1) type matrix multiplication which should result in (1*1).

How do I do this in numpy?


Solution

  • a.T is the transpose of matrix a

    temp = np.dot(a, B)        # a * B
    
    final= np.dot(temp, a.T)   #(a * B) * a_transpose
    

    Answer for your example is 155