0% found this document useful (0 votes)
9 views

Matrix_Operation_using_Python

The document provides a comprehensive guide on matrix operations using Python, specifically with the NumPy library. It covers concepts such as matrix creation, addition, subtraction, multiplication, transposition, determinants, traces, inverses, and various types of matrices including diagonal, identity, and null matrices. Additionally, it includes practical examples of each operation using code snippets.

Uploaded by

Jangam Swathi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Matrix_Operation_using_Python

The document provides a comprehensive guide on matrix operations using Python, specifically with the NumPy library. It covers concepts such as matrix creation, addition, subtraction, multiplication, transposition, determinants, traces, inverses, and various types of matrices including diagonal, identity, and null matrices. Additionally, it includes practical examples of each operation using code snippets.

Uploaded by

Jangam Swathi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Matrix Operation using Python

Importing Libraries
In [1]: import numpy as np

What is Matrix?
• A rectangular array of numbers, symbols, or characters is called Matrix.

• Matrices are identified by their order. The order of the matrices is given in the form of number of rows
⨯ number of columns.

• A Matrix is represented as [A]m⨯n where A is the matrix, m is the number of rows and n is the number
of columns.

Creating Matrix by using List of List


In [45]: A = [[1,2,3],[4,5,6],[7,8,9]] # Using List of List or Nested List
print(f"Matrix A : {A}")

Matrix A : [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

In [46]: B = [[11,12,13],[14,15,16],[17,18,19]]
print(f"Matrix B : {B}")

Matrix B : [[11, 12, 13], [14, 15, 16], [17, 18, 19]]

Creating Matrix by using NumPy Array Method


In [47]: np.array([[1,2,3],[4,5,6],[7,8,9]]) # Using np.array Method

Out[47]: array([[1, 2, 3],


[4, 5, 6],
[7, 8, 9]])
Accessing Elements of Matrix
In [5]: print(f"Element at 1st Row and 1st Column of Matrix A : {A[0][0]}")

Element at 1st Row and 1st Column of Matrix A : 1

In [6]: print(f"Element at 3rd Row and 3rd Column of Matrix B : {B[2][2]}")

Element at 3rd Row and 3rd Column of Matrix B : 19

Order of Matrix

Order of Matrix tells about the number of rows and columns present in a matrix.
Order of a matrix is represented as the number of rows times the number of columns.
Let’s say if a matrix has 4 rows and 5 columns then the order of the matrix will be 4⨯5.
First number in the order signifies the number of rows present in the matrix and the second
number signifies the number of columns in the matrix.

In [36]: order = np.shape(A)


print(f"Order of Matrix A : {order}")

Order of Matrix A : (3, 3)

In [37]: order = np.shape(B)


print(f"Order of Matrix B : {order}")

Order of Matrix B : (3, 3)

Adding Two Matrices

In addition of matrices, the elements of two matrices are added to yield a matrix that contains
elements obtained as the sum of two matrices.
The addition of matrices is performed between two matrices of the same order.

In [7]: sum = np.array(A) + np.array(B)


print(f"Sum : {sum}")

Sum : [[12 14 16]


[18 20 22]
[24 26 28]]

Sum of all Elements of Matrix


In [8]: sum_elements = np.sum(A)
print(f"Sum of all Elements of A is : {sum_elements}")

Sum of all Elements of A is : 45

Sum of all Elements of Matrix by Axis (0=Column & 1=Row)


In [9]: sum_elements_col = np.sum(A,axis=0) # Column Wise
print(f"Sum of Elements of A by Column : {sum_elements_col}")

Sum of Elements of A by Column : [12 15 18]

In [10]: sum_elements_row = np.sum(A,axis=1) # Row Wise


print(f"Sum of Elements of A by Row : {sum_elements_row}")

Sum of Elements of A by Row : [ 6 15 24]

Subtracting Two Matrices

Subtraction of Matrices is the difference between the elements of two matrices of the same
order.
The subtraction of two matrices can be represented in terms of the addition of two matrices.
Let’s say we have to subtract matrix B from matrix A then we can write A – B. We can also rewrite
it as A + (-B).

In [49]: sub = np.array(A) + (-np.array(B))


print(f"Difference : {sub}")

Difference : [[-10 -10 -10]


[-10 -10 -10]
[-10 -10 -10]]

In [50]: sub = np.array(A) - np.array(B)


print(f"Difference : {sub}")

Difference : [[-10 -10 -10]


[-10 -10 -10]
[-10 -10 -10]]

Scalar Product of a Matrix

Scalar Multiplication of matrices refers to the multiplication of each term of a matrix with a
scalar term.
If a scalar let’s ‘k’ is multiplied by a matrix then the equivalent matrix will contain elements equal
to the product of the scalar and the element of the original matrix.

In [12]: a = 5
s_prod = a*np.array(A)
print(f"Scalar Product : {s_prod}")

Scalar Product : [[ 5 10 15]


[20 25 30]
[35 40 45]]

Multiplication of Two Matrices

In the multiplication of matrices, two matrices are multiplied to yield a single equivalent matrix.
The multiplication is performed in the manner that the elements of the row of the first matrix
multiply with the elements of the columns of the second matrix.
If a matrix [A]i⨯j is multiplied with matrix [B]j⨯k then the product is given as [AB]i⨯k .

In [14]: mul = np.dot(A,B)


print(f"Multiplication : {mul}")

Multiplication : [[ 90 96 102]
[216 231 246]
[342 366 390]]

Transpose of a Matrix

Transpose of Matrix is basically the rearrangement of row elements in column and column
elements in a row to yield an equivalent matrix.
A matrix in which the elements of the row of the original matrix are arranged in columns or vice
versa is called Transpose Matrix.
The transpose matrix is represented as AT. if A = [aij]mxn , then AT = [bij]nxm where bij
= aji.

In [15]: trans = np.transpose(A)


print(f"Transpose of A : {trans}")

Transpose of A : [[1 4 7]
[2 5 8]
[3 6 9]]

Determinant of a Matrix

The determinant of a matrix is a number associated with that square matrix.


The determinant of a matrix can only be calculated for a square matrix.
It is represented by |A|.
The determinant of a matrix is calculated by adding the product of the elements of a matrix with
their cofactors.

In [16]: determinant = np.linalg.det(A)


print(f"Determinant of A : {determinant}")

Determinant of A : 0.0

Trace of a Matrix

Trace of a Matrix is the sum of the diagonal elements of a square matrix.


Trace of a matrix is only found in the case of a square matrix because diagonal elements exist
only in square matrices.

In [17]: trace = np.trace(A)


print(f"Trace of A : {trace}")
Trace of A : 15

Inverse of a Matrix

A matrix is said to be an inverse of matrix ‘A’ if the matrix is raised to power -1 i.e. A-1.
The inverse is only calculated for a square matrix whose determinant is non-zero.
The formula for the inverse of a matrix is given as : A-1 = adj(A)/det(A) = (1/|A|)(Adj A)
where |A| should not be equal to zero, which means matrix A should be non-singular..

In [21]: X = np.array([[7,11,2],[4,2,18],[13,10,19]])
inverse = np.linalg.inv(X)
print(f"Inverse of X : {inverse}")

Inverse of X : [[-0.18393782 -0.24481865 0.25129534]


[ 0.20466321 0.13860104 -0.15284974]
[ 0.01813472 0.09455959 -0.0388601 ]]

Flatten a Matrix
In [43]: X = np.array([[7,11,2],[4,2,18],[13,10,19]]) # By using Flatten Method
flat = X.flatten()
print(f"Flatten Array of Matrix X : {flat}")

Flatten Array of Matrix X : [ 7 11 2 4 2 18 13 10 19]

In [44]: X = np.array([[7,11,2],[4,2,18],[13,10,19]]) # By using Ravel Method


ravel = X.ravel()
print(f"Flatten Array of Matrix X : {ravel}")

Flatten Array of Matrix X : [ 7 11 2 4 2 18 13 10 19]

Diagonal Matrix

A square matrix in which the non-diagonal elements are zero is called a Diagonal Matrix.

In [30]: dig = np.diag((5,6,7))


print(f"Diagonal Matrix : {dig}")

Diagonal Matrix : [[5 0 0]


[0 6 0]
[0 0 7]]

Identity Matrix

A diagonal matrix whose all diagonal elements are 1 is called a Unit Matrix.
A unit matrix is also called an Identity matrix.
An identity matrix is represented by I.

In [31]: idnty = np.identity(3)


print(f"Identity Matrix : {idnty}")
Identity Matrix : [[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]

One's Matrix
In [32]: one = np.ones((3,3))
print(f"One Matrix : {one}")

One Matrix : [[1. 1. 1.]


[1. 1. 1.]
[1. 1. 1.]]

Null Matrix

A matrix whose all elements are zero is called a Zero Matrix.


A zero matrix is also called as Null Matrix.

In [33]: zero = np.zeros((3,3))


print(f"Null Matrix : {zero}")

Null Matrix : [[0. 0. 0.]


[0. 0. 0.]
[0. 0. 0.]]

You might also like