# Python – Matrix Arithmetics using Numpy

Numpy is excellent data structure in Python which contains lot of modules and methods to do mathematical operations. Matrix arithmetics widely used in data analysis and other mathematical operations. In this blog, we will go through matrix arithmetics with simple example using Numpy.

Below are the operations we will be discussing in this post.

- Matrix addition
- Matrix subtraction
- Matrix multiplication
- Scalar product
- Cross product

#### 1) Matrix addition

In matrix addition, one can add a scalar or constant value to the matrix or can add two matrix with each other. Below example demonstrate the matrix addition.

import numpy as np def addition(): A = np.array(((1,2,3), (4,5,6), (7,8,9))) #Adding scalar number to matrix result = A + 2 print("Matrix + Scalar value") print(result) #Adding another matrix B = np.array(((1,0,1), (1,2,3), (1,1,1))) result = A + B print("Addition of two matrix") print(result) if __name__ == '__main__': addition()

##### Output

Matrix + Scalar value [[ 3 4 5] [ 6 7 8] [ 9 10 11]] Addition of two matrix [[ 2 2 4] [ 5 7 9] [ 8 9 10]]

#### 2) Matrix subtraction

Similar to addition, one can subtract a scalar or constant value from the matrix or can subtract one matrix from another one. Below example demonstrate the matrix subtraction.

import numpy as np def subtract(): A = np.array(((1,2,3), (4,5,6), (7,8,9))) #Subtracting scalar number from matrix result = A - 2 print("Matrix - Scalar value") print(result) #Subtracting another matrix B = np.array(((1,0,1), (1,2,3), (1,1,1))) result = A - B print("Subtraction of two matrix") print(result) if __name__ == '__main__': subtract()

##### Output

Matrix - Scalar value [[-1 0 1] [ 2 3 4] [ 5 6 7]] Subtraction of two matrix [[0 2 2] [3 3 3] [6 7 8]]

#### 3) Matrix multiplication

The matrix product of two matrices can be calculated if the number of columns of the left matrix is equal to the number of rows of the second or right matrix.If we want to perform matrix multiplication with two numpy arrays (ndarray), we have to use the dot product(Scalar product) which we will see in below section. Here, we will see simple example to multiply a constant value to each element of the matrix, multiply element of one matrix to other and an alternate ways to do matrix multiplication.

import numpy as np def multiply(): A = np.array(((1,2,3), (4,5,6), (7,8,9))) #Multiplying scalar number to matrix result = A * 2 print("Matrix * Scalar value") print(result) #component wise multiplication of two matrix B = np.array(((1,0,1), (1,2,3), (1,1,1))) result = A * B print("Component wise multiplication of two matrix") print(result) #matrix multiplication using matrix objects result = np.mat(A) * np.mat(B) print("Matrix multiplication") print(result) if __name__ == '__main__': multiply()

##### Output

Matrix * Scalar value [[ 2 4 6] [ 8 10 12] [14 16 18]] Component wise multiplication of two matrix [[ 1 0 3] [ 4 10 18] [ 7 8 9]] Matrix multiplication [[ 6 7 10] [15 16 25] [24 25 40]]

#### 4) Scalar product

In mathematics, the dot product is an algebraic operation that takes two coordinate vectors of equal size and returns a single number. The result is calculated by multiplying corresponding entries and adding up those products.Mathematically it is defined as the cosine of the angle between two vectors. Below example demonstrate scalar product of two matrix.

import numpy as np def scalarProduct(): A = np.array(((1,2,3), (4,5,6), (7,8,9))) B = np.array(((1,0,1), (1,2,3), (1,1,1))) #Scalar product of two matrix result = np.dot(A, B) print("Scalar product of two matrix") print(result) if __name__ == '__main__': scalarProduct()

##### Output

Scalar product of two matrix [[ 6 7 10] [15 16 25] [24 25 40]]

#### 5) Cross product

Mathematically it is defined as the sine of the angle between two vectors. Below example demonstrate scalar product of two matrix.

import numpy as np def crossProduct(): A = np.array(((1,2,3), (4,5,6), (7,8,9))) B = np.array(((1,0,1), (1,2,3), (1,1,1))) #cross product of two matrix result = np.cross(A, B) print("Cross product of two matrix") print(result) if __name__ == '__main__': crossProduct()

##### Output

Cross product of two matrix [[ 2 2 -2] [ 3 -6 3] [-1 2 -1]]

Download source code

Stay tuned for more updates and tutorials !!!