user3407267
user3407267

Reputation:

Differences between matrix multiplication and array dot

I am new to Python. I have a trouble doing matrix multiplication. I have two lists:

      A =[3.0,3.0]    
      # 1 by 2 matrix

      B =[[ 50.33112583, -49.66887417],
           [-49.66887417,  50.33112583]]
      # 2 by 2 matrix 

      Result should be :
      # 1 by 2 matrix
      c = [1.9867549668874176, 1.986754966887446] 


      Right now I am doing:
     >> A = numpy.matrix(A)
     >> B = numpy.matrix(B)

     >> C =A * B  
     >> C
        matrix([[ 1.98675497,  1.98675497]])

     >>C.tolist()
       [[1.9867549668874176, 1.986754966887446]]

If I do dot product then,

    >>> B =numpy.array(B)
    >>> B
    array([[ 50.33112583, -49.66887417],
   [-49.66887417,  50.33112583]])
    >>> A = [ 3.,  3.]
    >>> A =numpy.array(A)
    >>> A
      array([ 3.,  3.])
    >>> C = numpy.dot(A,B)
    >>> C
    array([ 1.98675497,  1.98675497])
    >>> C.tolist()
    [1.9867549668874176, 1.986754966887446]

Why I am getting two brackets when I use matrix multiplication?? Whether dot product and matrix multiplication are same here? Can some one explain me this??

Upvotes: 5

Views: 3837

Answers (1)

Saullo G. P. Castro
Saullo G. P. Castro

Reputation: 58895

When you use np.matrix() it is by definition a 2-D container and the operations must be performed between 2-D entities and will return 2-D entities:

np.matrix([[1,2,3], [4,5,6]])*[[1], [2], [3]]
#matrix([[14],
#        [32]])

np.matrix([[1,2,3], [4,5,6]])*[1, 2, 3]
#ValueError

When you use a np.array() in tha case of dot() between two 2-D arrays the result is a 2-D array; while between a 2-D array and a 1-D array the result is a 1-D array:

np.array([[1,2,3], [4,5,6]]).dot([[1], [2], [3]])
#array([[14],
#       [32]])

np.array([[1,2,3], [4,5,6]]).dot([1, 2, 3])
#array([14, 32])

More complex and flexible broadcasting rules for arrays are available when element-wise operations are desired. Here is how each row can be multiplied by a different scalar:

np.array([[1,2,3], [4,5,6]])*[[1], [2]]
#array([[ 1,  2,  3],
#       [ 8, 10, 12]])

and how each column can be multiplied by a different scalar:

np.array([[1,2,3], [4,5,6]])*[1, 2, 3]
#array([[ 1,  4,  9],
#       [ 4, 10, 18]])

Upvotes: 5

Related Questions