Multiplying matrices in python

Multiplying matrices in python

matrix multiplication is only possible if the number of columns in the first matrix (A) is equal to the number of rows in the second matrix (B). In this example, both matrices have two rows and two columns, so the multiplication is valid. the two functions we will be using are from the Numpy library in Python. NumPy is a library for scientific computing in Python that provides functions for working with arrays and matrices.

Using the dot function

To multiply two matrices in Python, you can use the "dot" function from the NumPy library. Here's an example of how to use the dot function to multiply two matrices in Python:

import numpy as np
# Let us define matrices A and B
A = np.array([[5, 3], [7, 10]])
B = np.array([[5, 8], [8, 8]])
# multiply matrices A and B
C = np.dot(A, B)
print(C)

This will output the following matrix:

array([[ 49,  64],
       [115, 136]])
​

Using the matmul function

You can also use the "matmul" function from NumPy to perform matrix multiplication. The syntax for using the matmul function is similar to the dot function:

import numpy as np

# define matrices A and B
A = np.array([[5, 3], [7, 10]])
B = np.array([[5, 8], [8, 8]])

# multiply matrices A and B
C = np.matmul(A, B)

print(C)

This will also output the same result as the example above.

To know more click here