Pymatrix module in python Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report Pymatrix is a lightweight matrix library which supports basic linear algebra operations. The elements in matrix should be numeric type to support basic algebra operations - int, float, rational, complex. Instantiating Matrix Using Matrix constructor Matrix can be initialised using constructor of Matrix class in pymatrix library. Syntax: Matrix(rows, cols, fill=val(optional))Parameters: rows - specify number of rows cols - specify number of columns fill - initialise all the elements with this value. It is an optional argument, the default fill is 0. Example: Python3 import pymatrix m = pymatrix.Matrix(2, 3, fill = '2') print(m) Output: 2 2 2 2 2 2 Using list of lists We can convert a list of lists into a matrix using the from_list() method where each list is treated as a row. Example- Python3 import pymatrix list = [[1, 2, 3], [6, 4, 7], [3, 9, 1]] m = pymatrix.Matrix.from_list(list) print(m) Output: 1 2 3 6 4 7 3 9 1 Using string We can convert a string into matrix object using from_string() method. The string is in triple quotes and each line is treated as a row. Example Python3 import pymatrix string = '''1 2 3 6 4 7 3 9 1''' m = pymatrix.Matrix.from_string(string) print(m) Output: 1 2 3 6 4 7 3 9 1 Matrix methods Following are some of the methods provided in pymatrix library : identity(n) - Creating an identity matrix of given size. This returns an object of Matrix class.is_square() - Checks whether given matrix is a square matrix or not. This returns a boolean value.is_invertible() - Checks whether given matrix is invertible or not. This returns a boolean value.inv() - Returns the inverse matrix if it exists, otherwise raises MatrixError exception.det() - Returns the determinant matrix if square matrix, otherwise raises MatrixError('non-square matrix does not have determinant') exception.rank() - Returns the rank of matrix, rank is of integer type.trans() - Returns the transpose of the matrix.adjoint() - Returns the adjoint matrix. Example: Python3 # Python program for Matrix methods from pymatrix import Matrix # identity matrix of size 2 identity_matrix = Matrix.identity(2) print('\nIdentity matrix :') print(identity_matrix) m = Matrix.from_list([[1,2,1],[2,1,1],[1,1,1]]) print('\nIs a square matrix :') print(m.is_square()) print('\nIs an invertible matrix :') print(m.is_invertible()) print('\nInverse :') print(m.inv()) print('\nDeterminant :') print(m.det()) print('\nRank :') print(m.rank()) print('\nTranspose :') print(m.trans()) print('\nAdjoint :') print(m.adjoint()) Output: Identity matrix : 1 0 0 1 Is a square matrix : True Is an invertible matrix : True Inverse : 0.0 1.0 -1.0 1.0 0.0 -1.0 -1.0 -1.0 3.0 Determinant : -1.0 Rank : 3 Transpose : 1 2 1 2 1 1 1 1 1 Adjoint : 0.0 -1.0 1.0 -1.0 0.0 1.0 1.0 1.0 -3.0 Comment More infoAdvertise with us Next Article Pymatrix module in python N naina024 Follow Improve Article Tags : Python python-modules Practice Tags : python Similar Reads Matrix manipulation in Python In python matrix can be implemented as 2D list or 2D Array. Forming matrix from latter, gives the additional functionalities for performing various operations in matrix. These operations and array are defines in module "numpy". Operation on Matrix : 1. add() :- This function is used to perform eleme 4 min read Python Module Index Python has a vast ecosystem of modules and packages. These modules enable developers to perform a wide range of tasks without taking the headache of creating a custom module for them to perform a particular task. Whether we have to perform data analysis, set up a web server, or automate tasks, there 4 min read Python - Matrix A matrix is a way to organize numbers in a rectangular grid made up of rows and columns. We can assume it like a table, where:Rows go across (left to right)Columns go down (top to bottom)The size of a matrix is defined by the number of rows (m) and columns (n). If a matrix has 3 rows and 4 columns, 10 min read numpy.matrix() in Python This class returns a matrix from a string of data or array-like object. Matrix obtained is a specialised 2D array. Syntax : numpy.matrix(data, dtype = None) : Parameters : data : data needs to be array-like or string dtype : Data type of returned array. Returns : data interpreted as a matrix Python 1 min read Python Modules Python Module is a file that contains built-in functions, classes,its and variables. There are many Python modules, each with its specific work.In this article, we will cover all about Python modules, such as How to create our own simple module, Import Python modules, From statements in Python, we c 7 min read Basics Of Python Modules A library refers to a collection of modules that together cater to a specific type of needs or application. Module is a file(.py file) containing variables, class definitions statements, and functions related to a particular task. Python modules that come preloaded with Python are called standard li 3 min read Polytopes in Python Python provides us with a third-party module to operate on polytopes. For one who doesn't know what polytopes are, Polytopes are a geometrical form in an n-dimensional geometry corresponding to a polygon or polyhedron. Like other geometrical figures such as a square or a cube, a polytope isn't limit 4 min read Built-in Modules in Python Python is one of the most popular programming languages because of its vast collection of modules which make the work of developers easy and save time from writing the code for a particular task for their program. Python provides various types of modules which include Python built-in modules and ext 9 min read External Modules in Python Python is one of the most popular programming languages because of its vast collection of modules which make the work of developers easy and save time from writing the code for a particular task for their program. Python provides various types of modules which include built-in modules and external m 5 min read How to locate a particular module in Python? In this article, we will see how to locate a particular module in Python. Locating a module means finding the directory from which the module is imported. When we import a module the Python interpreter searches for the module in the following manner: First, it searches for the module in the current 3 min read Like