# Python NumPy Matrix Multiplication

In this Python tutorial, we will learn how do we do matrix multiplication in NumPy array Python. With the Python NumPy add function, we will cover these topics.

• Python numpy matrix multiplication element-wise
• Python numpy matrix multiplication operator
• Python numpy matrix multiplication function
• Python numpy matrix multiply vector
• Python numpy multiply matrix by scaler
• Python numpy matrix dot product
• Python numpy matrix cross product
• Python matrix multiplication without numpy

## Python NumPy matrix multiplication

• In this Program, we will discuss how to multiply two NumPy matrices in Python.
• To solve this problem we are going to use the numpy.matmul() function and return the matrix product of two input arrays. In Python the numpy.matmul() function is used to find out the matrix multiplication of two arrays.
• In this function, we cannot use scaler values for our input array. When we are using a 2-dimensional array it will return a simple product and if the matrices are greater than 2-d then it is considered a stack of matrices.

Syntax:

Let’s have a look at the Syntax and understand the working of numpy.matmul() function

``````numpy.matmul
(
x1,
x2
/,
out=None,
*,
casting='same_kind',
order='K',
dtype=None,
subok=True
[,
signature,
extobj,
axes,
axis
]
)``````

Note: This function takes only three parameters and others are optional parameters. We will take only the first and second input array as an argument.

Example:

Let’s take an example and check how to multiply two numpy arrays in Python by using the numpy.matmul() function

Source Code:

``````import numpy as np

new_array = np.array([[14, 26],
[6, 8]])
new_array2 = np.array([[15, 23],
[73, 118]])
result= np.matmul(new_array,new_array2)
print('Multiplication of two matrices:',result)``````

In the above Program, we imported the numpy library and then we have taken two input arrays named ‘new_array’ and ‘new_array2‘. Now we have to display the product of two matrices. To do this we are going to use the numpy.matmul() function and the result will show the new array.

Here is the Screenshot of the following given code

Also, check: Python NumPy Add Tutorial

## Python NumPy matrix multiplication element-wise

• In this section, we will discuss how to multiply the matrix element-wise in NumPy Python.
• In Python, the multiplication of matrix is an operation where we take two numpy matrices as input and if you want item-wise multiplication then you can easily use the multiply() function.
• In Python the numpy.multiply() function is used to calculate the multiplication between two numpy arrays and it is a universal function available in the numpy package module.
• This method takes several parameters and the two input arrays must have the same shape that they have the same number of columns and rows.

Syntax:

Let’s have a look at the Syntax and understand the working of Python numpy.multiply() function

``````numpy.multiply
(
x1,
x2,
/,
out=None,
*,
where=True,
casting='same_kind',
order='K',
dtype=None,
subok=True
)``````
• It consists of a few parameters
• x1,x2: This parameter indicates the first input array you want to multiply.
• out: By default it takes none value and where the result is to be stored.
• where: This parameter will check the condition if it is true the out array will be set to the ufunc result.
• dtype: By default it takes none value and it is an optionbal parameter.

Example:

Let’s take an example and check how to multiply the matrix in NumPy Python

Source Code:

``````import numpy as np

new_arr=np.array([[14,24,27],[17,19,21]],ndmin=2)
new_arr2=np.array([[34,78,92],[25,41,19]],ndmin=2)
new_output=np.multiply(new_arr,new_arr2)
print("Matrix multiplication:",new_output)``````

In the above code, we imported the numpy library and then initialize an array by using the np.array() function. After that, we have applied the np.multiply() function for calculating the product between ‘new_arr’ and ‘new_arr2’. Once you will print ‘new_output then the result will display a new array.

Here is the implementation of the following given code

## Python numpy matrix multiplication operator

• In this section, we will discuss how to use the @ operator for the multiplication of two numpy arrays in Python.
• In Python, the @ operator is used in the Python3.5 version and it is the same as working in numpy.matmul() function but in this example, we will change the operator as infix @ operator. This method is basically used for conventional matrices and returns the matrix product of two input arrays.

Example:

Let’s take an example and check how to multiply two input matrices by using the infix@ operator

Source Code:

``````import numpy as np

new_val1 = np.array([[32, 26],
[45, 87]])
new_val2 = np.array([[13, 26],
[56, 98]])
result= new_val1 @ new_val2
print("Matrix multiplication by operator:",result)``````

Here is the execution of the following given code

## Python numpy matrix multiplication function

• In this section, we will learn how to get the matrix multiplication of two numpy arrays by using the numpy.multiply() function.
• To perform this particular task we are going to use the numpy.multiply() function. In Python, this function is used to calculate the multiplication between two numpy arrays and it must be having the same shape that specifies the same number of columns and rows.
• The matrix of multiplication is possible when both arrays are compatible. For example, suppose we have two numpy arrays ‘array1’ and ‘array2’ and if we want to get the resultant matrix for array1 and array2. we just simply use the mathematics concept mn matrix ‘array1’ with an np matrix ‘array2’. After that when we execute the program the output can be given as ‘array3’ of the order ‘m*p‘.
• Let’s understand the concept in detail by taking an example using numpy.multiply() function.

Syntax:

Here is the Syntax of numpy.multiply() function

``````numpy.multiply
(
x1,
x2,
/,
out=None,
*,
where=True,
casting='same_kind',
order='K',
dtype=None,
subok=True
)``````

Example:

Let’s take an example and understand the working of Python numpy.multiply() function

``````import numpy as np

values=np.array([[45,17,89],[23,56,98]])
values2=np.array([[73,91,26],[98,17,18]])
new_output=np.multiply(values,values2)
print("Matrix multiplication:",new_output)``````

You can refer to the below Screenshot

## Python numpy matrix multiply vector

• In this Program, we will discuss how to multiply vectors in NumPy Python.
• To perform this particular task we are going to use the * operator for the multiplication of two vectors. In Python, this operand is used to produce the product of the input array.
• This operator is mostly used in the multiplication of given inputs and it is available in the Python package module.

Example:

Let’s take an example and check how to multiply the vectors in Python by using the * operator

Source Code:

``````import numpy as np

new_val = np.array([[20,45],[18,27],[45,87]])
new_val2= np.array([[55,46],[13,22],[98,79]])
new_result= new_val *new_val2
print("Vector multiplication:",new_result)``````

In the above code, we imported the numpy library and then initialize an array by using the np.array() function. After that, we declared the variable ‘new_result’ in which we have multiply both the arrays by using the * operator.

Here is the Output of the following given code

## Python numpy multiply matrix by scaler

• In this section, we will discuss how to multiply the matrix by scaler in Python.
• To do this task we are going to use the np.array() function and then initialize a scaler value ‘2’. Now we want to get the product of scaler an array by using the numpy.multiply() function.

Example:

``````import numpy as np

va11 = np.array([20,45])
val2= 2
new_result= np.multiply(va11,val2)
print(new_result)
``````

Here is the implementation of the following given code

## Python numpy matrix dot product

• In this Program, we will learn how to multiply matrices by using numpy dot product method in Python.
• In Python, this function is used to perform the dot product of two matrices. This method takes two equal numpy matrices and returns a single matrix.
• In Python, this method takes two numpy matrices as an argument and returns the multiplication of two given matrices.

Syntax:

Let’s have a look at the Syntax and understand the working of numpy.dot() function

``````numpy.dot
(
a,
b,
out=None
)``````
• It consists of a few parameters
• a,b: This parameter indicates the first and second input array which we want to compute.
• out: By default it takes none value and it must be C contiguous. This method returns a scaler if arr1 and arr2 are scalers or 1-d, otherwise it will return a numpy array.

Example:

Let’s take an example and check how to multiply matrices by using the numpy.dot() function

Source Code:

``````import numpy as np

new_ele1 = np.array([[20,45],
[45,24]])
new_ele2=  np.array([[14,28],
[13,16]])
final_output= np.dot(new_ele1,new_ele2)
print(final_output)
``````

Here is the execution of the following given code

You can also refer to our detailed article on the Python dot product.

## Python numpy matrix cross product

• Here we will discuss how to find the multiplication of two matrices in Python by using the cross product method.
• In Python, the cross product is also known as vector product and it is denoted by symbol X. This method is available in the NumPy package module and it is also used for linear algebra and matrices.
• This method will always return the cross product of two given matrices and it is defined as the axis of C containing the cross product.

Syntax:

Here is the Syntax of Python numpy.cross() method

``````numpy.cross
(
a,
b,
axisa=-1,
axisb=-1,
axisc=-1,
axis=None
)``````
• It consists of a few parameters
• a,b: This parameter indicates the first and second vector.
• axis: This is an optional parameter and by default it takes last axis.

Example:

Let’s take an example and understand the working of Python numpy.cross() method

Source Code:

``````import numpy as np

new_arr = np.array([16, 26,78])
new_arr2 = np.array([18, 28,56])
new_result = np.cross(new_arr, new_arr2)

print(new_result)``````

Here is the implementation of the following given code

## Python matrix multiplication without numpy

• In this section, we will discuss how to multiply the matrix in Python without numpy.
• To perform this particular task we are going to use the for loop() method and get the matrix multiplication in Python.

Source Code:

``````new_values= [[78, 15, 9],
[14, 26, 89],
[13, 19, 21]]
new_values2= [[14, 17, 19],
[89, 87, 29],
[36, 89, 21]]
new_output = [[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]
for m in range(len(new_values)):

for n in range(len(new_values2[0])):

for o in range(len(new_values2)):
new_output[m][n] += new_values[m][o] * new_values2[o][n]
for d in new_output:
print(d)``````

Here is the implementation of the following given code

You may also like to read the following articles on Python NumPy.

In this Python tutorial, we will learn how do we do matrix multiplication in NumPy array Python. With the Python NumPy add function, we will cover these topics.

• Python numpy matrix multiplication element-wise
• Python numpy matrix multiplication operator
• Python numpy matrix multiplication function
• Python numpy matrix multiply vector
• Python numpy multiply matrix by scaler
• Python numpy matrix dot product
• Python numpy matrix cross product
• Python matrix multiplication without numpy