Hamza.S
Hamza.S

Reputation: 1377

Is numpy matrix multiplication same as Linear Algebra matrix multiplication?

As we know that In Linear Algebra it is mandatory to multiply a vector by matrix or multiply two matrices, the number of rows of one matrix or vector must be equal to the number of columns in other vector or matrix.

while i was working in numpy python and it is giving me a different result.

Here is my code and it works.

np.array([1,2]) * np.array([[1],[2],[3]])

so is there any difference between numpy vector to matrix matlication vs linear algebra vector to matrix multiplication.

Upvotes: 1

Views: 327

Answers (1)

Hamza.S
Hamza.S

Reputation: 1377

use numpy np.dot(a,b)

Use the following code and you will get error you want.

np.dot(np.array([1,2]) ,  np.array([[1],[2],[3]]))

Becuase *,+,-,/ works element-wise on arrays.

If either a or b is 0-D (scalar), it is equivalent to multiply and using numpy.multiply(a, b) or a * b is preferred.

Upvotes: 2

Related Questions