Matrix_Operation_using_Python
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.
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]]
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 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.
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).
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}")
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 .
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.
Transpose of A : [[1 4 7]
[2 5 8]
[3 6 9]]
Determinant of a Matrix
Determinant of A : 0.0
Trace of a Matrix
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}")
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}")
Diagonal Matrix
A square matrix in which the non-diagonal elements are zero is called a Diagonal Matrix.
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.
One's Matrix
In [32]: one = np.ones((3,3))
print(f"One Matrix : {one}")
Null Matrix