Python – Matrix Arithmetics using Numpy

Posted on Updated on

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.

  1. Matrix addition
  2. Matrix subtraction
  3. Matrix multiplication
  4. Scalar product
  5. 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.

Output

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.

Output

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.

Output

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.

Output

5) Cross product

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

Output

Download source code

Matrix Arithmetics

Stay tuned for more updates and tutorials !!!

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.