Package: matops
What ismatops?matopsis a lightweight pure Python package to do basic matrix operations like addition, multiplication etc. as well as to check some properties of matrices e.g. whether a matrix is scalar or not.What is a matrix?In mathematics, a matrix is a set of numbers arranged in rows and columns so as to form a rectangular array. The numbers are called the elements, or entries, of the matrix.Installation:You can usepipto installmatopswith the following command:pip install matopsExamples:Addition:frommatopsimportMatrixm1=Matrix([[1,2],[3,4],])m2=Matrix([[5,6],[7,8],])m3=m1+m2print(m3)[[6, 8], [10, 12]]You can useprettify()method to print it in a more readable format.m3.prettify()[6, 8]
[10, 12]Transpose:frommatopsimportMatrixm=Matrix([[1,2],[3,4],])m.transpose().prettify()[1, 3]
[2, 4]TheMatrixclassTheMatrixclass is at the heart ofmatops. You can import it as follows to use it in your code:frommatopsimportMatrixInstantiationm=Matrix([[1,2],[3,4],])Supported operations and methods:AdditionfrommatopsimportMatrixm1=Matrix([[1,2],[3,4],])m2=Matrix([[5,6],[7,8],])m3=m1+m2m3.prettify()[6, 8]
[10, 12]SubtractionfrommatopsimportMatrixm1=Matrix([[7,9],[1,5],])m2=Matrix([[3,1],[0,2],])m3=m1-m2m3.prettify()[4, 8]
[1, 3]MultiplicationMultiplication can be performed either with anint,floator anotherMatrix.With anintor afloatWhen multiplying aMatrixwith anintor afloat, make sure thatMatrixis on the left side of the*operator.frommatopsimportMatrixm1=Matrix([[4,0],[1,-9],])m2=m1*2m2.prettify()[8, 0]
[2, -18]With anotherMatrixWhen multiplying aMatrixwith anotherMatrix, make sure that theMatrixon the left side of the*operator has the same number of columns as the rows of theMatrixon the right side. If that's not the case, you might get an error.frommatopsimportMatrixm1=Matrix([[3,4,2],])m2=Matrix([[13,9,7,15],[8,7,4,6],[6,4,0,3],])m3=m1*m2m3.prettify()[83, 63, 37, 75]NegativeNegative of aMatrixbasically flips the sign of every element from+to-and vice versa. It is essentially the same as multiplying theMatrixby-1.frommatopsimportMatrixm1=Matrix([[1,-2],[-3,4],])m2=-m1m2.prettify()[-1, 2]
[3, -4]TransposeThe transpose of a matrix is an operator which flips a matrix over its diagonal. We essentially convert rows into columns (or columns into rows). The.transpose()method can be used to find the transpose of aMatrix.frommatopsimportMatrixm=Matrix([[1,2],[3,4],])m.transpose().prettify()[1, 3]
[2, 4]EqualityWe can check whether twoMatrixare equal or not using==and!=operators.frommatopsimportMatrixm1=Matrix([[1,2],[3,4],])m2=Matrix([[1,2],[3,4],])m3=Matrix([[1,0],[0,1],])print(m1==m2)print(m1==m3)print(m1!=m3)True
False
TrueRow MatrixIf a matrix has only one row, it's called a row matrix. We can use.is_row_matrix()method to check whether aMatrixis a row matrix or not.frommatopsimportMatrixm1=Matrix([[1,2],])m2=Matrix([[1],[2],])print(m1.is_row_matrix())print(m2.is_row_matrix())True
FalseColumn MatrixIf a matrix has only one column, it's called a column matrix. We can use.is_column_matrix()method to check whether aMatrixis a column matrix or not.frommatopsimportMatrixm1=Matrix([[1],[2],])m2=Matrix([[1,2],])print(m1.is_column_matrix())print(m2.is_column_matrix())True
FalseIn this case,m1is a column matrix whilem2is not.Rectangular MatrixIf the number of rows in a matrix are not equal to the number of columns, that matrix is callled a rectangular matrix. We can use.is_rectangular_matrix()method to check whether aMatrixis a rectangular matrix or not.frommatopsimportMatrixm=Matrix([[1],[2],])print(m.is_rectangular_matrix())TrueIn this casemis a rectangular matrix because the number of rows (2) are not equal to the number of columns (1).Square MatrixAs opposed to a rectangular matrix, if the number of rows in a matrix are equal to the number of columns, that matrix is callled a square matrix. We can use.is_square_matrix()method to check whether aMatrixis a square matrix or not.frommatopsimportMatrixm=Matrix([[1,2],[3,4],])print(m.is_square_matrix())TrueIn this case,mis a square matrix because the number of rows (2) are equal to the number of columns (2).Zero MatrixIf all the elements of a matrix are zero, it is callled zero matrix. We can use.is_zero_matrix()method to check whether aMatrixis a zero matrix or not.frommatopsimportMatrixm=Matrix([[0,0],[0,0],])print(m.is_zero_matrix())TrueIn this case,mis a zero matrix because all of its elements are equal to zero.Symmetric MatrixIf the transpose of a matrix is equal to the original matrix itself, then that matrix is called a symmetric matrix. We can use.is_symmetric_matrix()method to check whether aMatrixis a symmetric matrix or not. Consider the following example:frommatopsimportMatrixm=Matrix([[3,2],[2,4],])m.transpose().prettify()[3, 2]
[2, 4]As you can see we are printing the transpose of the matrixmbut it is again equal tomitself. Somis a symmetric matrix and we can verify that as follows:frommatopsimportMatrixm=Matrix([[3,2],[2,4],])print(m.is_symmetric_matrix())TrueSkew-symmetric MatrixIf the transpose of a matrix is equal to the negative of the original matrix, then that matrix is called a skew-symmetric matrix. We can use.is_skew_symmetric_matrix()method to check whether aMatrixis a skew-symmetric matrix or not. Consider the following example:frommatopsimportMatrixm=Matrix([[0,2,3],[-2,0,1],[-3,-1,0],])m.transpose().prettify()[0, -2, -3]
[2, 0, -1]
[3, 1, 0]If you look closely, you'll see that the transpose of matrixmis actually negative of the matrixm. Somis a skew-symmetric matrix and we can verify that as follows:frommatopsimportMatrixm=Matrix([[0,2,3],[-2,0,1],[-3,-1,0],])print(m.is_skew_symmetric_matrix())TrueDiagonal MatrixA diagonal matrix is a square matrix whose:Off-diagonal entries are all equal to zero.At least one of the diagonal entries is non-zeroWe can useis_diagonal_matrix()method to check whether aMatrixis a diagonal matrix or not.frommatopsimportMatrixm=Matrix([[1,0,0],[0,2,0],[0,0,3],])print(m.is_diagonal_matrix())TrueIf any off-diagonal element is non-zero, it won't be a diagonal matrix.frommatopsimportMatrixm=Matrix([[1,1,0],[0,2,0],[0,0,3],])print(m.is_diagonal_matrix())FalseIf all the diagonal entries are zero too then it won't be a diagonal matrix either. Instead, it will become a zero matrix.frommatopsimportMatrixm=Matrix([[0,0,0],[0,0,0],[0,0,0],])print(m.is_diagonal_matrix())FalseScalar MatrixA diagonal matrix is called a scalar matrix if all the diagonal entries are equal and non-zero. We can useis_scalar_matrix()method to check whether aMatrixis a scalar matrix or not.frommatopsimportMatrixm=Matrix([[7,0,0],[0,7,0],[0,0,7],])print(m.is_scalar_matrix())TrueIf any off-diagonal element is non-zero, it won't be a diagonal matrix and hence it won't be a scalar matrix either.frommatopsimportMatrixm=Matrix([[7,1,0],[0,7,0],[0,0,7],])print(m.is_scalar_matrix())FalseIf all the diagonal entries are not equal and non-zero then it won't be a scalar matrix.frommatopsimportMatrixm=Matrix([[1,0,0],[0,7,0],[0,0,7],])print(m.is_scalar_matrix())FalseIdentity MatrixA scalar matrix is called an identity matrix if all the diagonal entries are equal to1. We can useis_identity_matrix()method to check whether aMatrixis an identity matrix or not.frommatopsimportMatrixm=Matrix([[1,0,0],[0,1,0],[0,0,1],])print(m.is_identity_matrix())True