Matrix Multiplication

The Numpu matmul() function is used to return the matrix product of 2 arrays. Here is how it works

1) 2-D arrays, it returns normal product

2) Dimensions > 2, the product is treated as a stack of matrix

3) 1-D array is first promoted to a matrix, and then the product is calculated

numpy.matmul(x, y, out=None)

Here,

x,y: Input arrays. scalars not allowed

out: This is optional parameter. Usually output is stored in ndarray

Example:

In the same way, you can compute matrices multiplication with np.matmul

### Matmul: matruc product of two arrays
h = [[1,2],[3,4]] 
i = [[5,6],[7,8]] 
### 1*5+2*7 = 19
np.matmul(h, i)

Output:

array([[19, 22],       
            [43, 50]])			

Determinant

Last but not least, if you need to compute the determinant, you can use np.linalg.det(). Note that numpy takes care of the dimension.

## Determinant 2*2 matrix
### 5*8-7*6np.linalg.det(i)			

Output:

-2.000000000000005			

 

YOU MIGHT LIKE: