Package: mathmatrix
FeaturesAddition, Multiplication, Division, Subraction operations supported
between matrices and between a matrix and a int / floatCalculates:DeterminantInverseCofactor of a given element in the MatrixAdjointGetting startedCreating a MatrixTo create a matrix, specify the order of the Matrix (mxn) where the
first argument (m) is the number of rows in the matrix and the second
argument (n) is the number of columnsWe can use a nested list to represent a Matrix during initialization of
an object In a nested list, the length of the outer list would be ‘m’
and the number of elements the inner lists have would be ‘n’frommatriximportMatrixmatrix_list=[[1,2,3],[4,5,6],[7,8,9]]matrix1=Matrix(3,3,matrix_list)print(matrix1)#Prints:# [ 1, 2, 3#   4, 5, 6#   7, 8, 9 ]Operations on MatricesAddition and SubractionWe can add and subract matrices extremely easily:matrix_list2=[[0,1,3],[5,2,7],[7,1,9]]matrix2=Matrix(3,3,matrix_list2)matrix3=matrix1+matrix2print(matrix3)#Prints:# [ 1, 3, 6#   9, 7, 13#   14, 9, 18 ]Adding an int / float to a matrix will perform the operation on all
elements of the matrix and return a new matrixmatrix4=matrix1+5print(matrix4)#Prints:# [ 6, 7, 8#   9, 10, 11#   12, 13, 14 ]# Same way,print(matrix4-matrix1)# [ 5, 5, 5#   5, 5, 5#   5, 5, 5 ]print(matrix1-3)#Prints:# [ -2, -1, 0#   1, 2, 3#   4, 5, 6 ]Multiplication and DivisionMatrix multiplication can only be implemented if the number of columns
in the first matrix is equal to the number of rows in the other matrix.
Basically: Am x nMatrix can only be multiplied with an x lMatrix .The order of the resultant Matrix will bem x lExample:# m x n * n x l : Gives m x l# 2 x 3 * 3 x 2 : Gives 2 x 2# 2 x 3 * 4 x 2 : Cannot mutliplyInternally, division is calculated by multiplying a matrix and the
inverse of the other matrix therefore the same condition applies for
divisionprint(matrix1*5)# [ 5, 10, 15#   20, 25, 30#   35, 40, 45 ]print(matrix1*matrix2)# [ 31, 8, 44#   67, 20, 101#   103, 32, 44 ]Comparing matricesMatrix == Matrix | 0->boolMatrices can be compared for equality to another matrix Zero is used as
an alias for a zero matrixFunctionalitiesmathmatrixprovides many functionalities for matrices out of the
box:Let’s create a sample matrixmatrixto perform the operations onfrommathmatriximportMatrixmatrix=Matrix(3,3,[[1,2,3],[4,5,6],[7,8,9]])Transposing a matrixMatrix.transpose()->MatrixAfter creating a matrix, you can transpose a Matrix using thetranspose()method of Matrixprint(matrix.transpose())# [ 1, 4, 7#   2, 5, 8#   3, 6, 7 ]Adjoint of a MatrixMatrix.adjoint()->MatrixAdjoint of a matrix is calculated as the transpose of cofactor matrix of
a Matrix It can be calculated using theadjoint()methodprint(matrix.adjoint())# [ -3, 6, -3#   6, -12, 6#  -3, 6, -3 ]Determinant of a MatrixMatrix.determinant()->int | floatprint(matrix.determinant())# 0Inverse of a MatrixMatrix.inverse()->MatrixInverse of a matrix only exists for non-singular matrices ( Determinant
of the Matrix should not be zero )print(matrix.determinant())# 0# Since determinant is zero, if we try to calculate Inverse it will throw the error:# ZeroDivisionError: Determinant of Matrix is zero, inverse of the matrix does not existCofactor of an elementMatrix.cofactor(m:int, n:int)->int | floatSpecify the position of the desired element in row number (m) and column
number (n) to calculate it’s corresponding cofactorChaining functionsSince functions return a new Matrix, you can chain many functions to get
the desired output For example:matrix.transpose().adjoint().determinant()(matrix.determinant()*matrix.adjoint()).transpose()are all completely validAdditional FunctionsGenerating a zero matrixgen_zero_matrix(m:int, n:int)->MatrixYou can use thegen_zero_matrixfunction to create a zero matrix of
a given order For example,frommathmatriximportgen_zero_matrix,Matrixzero3=gen_zero_matrix(3,3)print(zero3)# [ 0, 0, 0#   0, 0, 0#   0, 0, 0 ]print(zero3==0)# TrueGenerating an identity matrixgen_zero_matrix(m:int, n:int)->MatrixYou can use thegen_zero_matrixfunction to create a zero matrix of
a given order For example,frommathmatriximportgen_zero_matrix,Matrixzero3=gen_zero_matrix(3,3)print(zero3)# [ 0, 0, 0#   0, 0, 0#   0, 0, 0 ]print(zero3==0)# TrueNote: For any Matrixmatrix,print(matrix*matrix.inverse()==gen_identity_matrix(matrix.m,matrix.n))# Always true (Inverse cannot be calculated for singular matrices so error is thrown in that case)print((matrix-matrix)==gen_zero_matrix(matrix.m,matrix.n))# Always true