Pymatrix exports a lightweight, easy-to-use matrix object that supports all of Python's native numeric types, including integers, floats, and fractions (rational numbers).
Be warned that this library has been built for comfort, not for speed. Users with heavy-duty computational needs should turn to an optimised alternative like NumPy instead.
You can instantiate a matrix object directly, optionally specifying a fill value:
m = Matrix(rows, cols, fill=0)
You can instantiate a matrix object from a list of lists using the
from_list() static method:
m = Matrix.from_list([ [1, 2, 3], [4, 5, 6] ])
You can instantiate a matrix object from a string using the
from_string() static method:
s = """ 1 2 3/7 4/7 5 6 """ m = Matrix.from_string(s, rowsep=None, colsep=None, parser=fractions.Fraction)
Row separators default to newlines, column separators default to spaces. Leading and trailing whitespace is stripped from the string. Elements are parsed as fractions (rational numbers) by default.
You can instantiate an n x n identity matrix using the
identity() static method:
m = Matrix.identity(n)
matrix() function supports the syntax of all three static methods:
m = matrix([[1, 2, 3]]) m = matrix('1 2 3') m = matrix(3)
Matrix objects are iterable. Iteration proceeds left-to-right by column, then top-to-bottom by row; i.e. the top-left element will be returned first, the bottom-right element will be returned last:
for element in matrix: ...
elements() method returns an iterator over a tuple including row and column indices:
for row, col, element in matrix.elements(): ...
Matrices are indexed as two-dimensional arrays:
matrix[row][col] = element element = matrix[row][col]
Note that indices are zero-based in accordance with programming convention rather than one-based in typical math style, i.e. the matrix's top-left element is
matrix rather than
Matrix objects support the following methods:
Returns the adjoint matrix as a new object.
Returns the specified cofactor.
Returns the matrix of cofactors as a new object.
Returns an iterator over the specified column.
Returns a column iterator for each column in the matrix.
Returns the specified column as a new column vector.
Returns a copy of the matrix.
Returns the vector product of the matrix with
otheras a new matrix.
Returns a new matrix with the specified column deleted.
Returns a new matrix with the specified row deleted.
Returns the determinant of the matrix.
Vectors only. Returns the unit vector in the direction of the vector.
Returns the scalar product of the matrix with
Iterator returning the tuple
(row, col, element).
True if the matrices are the same size and their corresponding elements agree to within
Returns the inverse matrix if it exists, otherwise raises MatrixError.
True if the matrix is invertible.
True if the matrix is square.
Vectors only. Returns the length of the vector.
Returns a new matrix formed by mapping
functo each element.
Returns the specified minor.
Returns the rank of the matrix.
Returns the row echelon form of the matrix.
Returns an iterator over the specified row.
rowop_add(r1, m, r2)
In-place row operation. Adds
In-place row operation. Multiplies the specified row by the scalar
In-place row operation. Interchanges the two specified rows.
Returns a row iterator for each row in the matrix.
Returns the specified row as a new row vector.
Returns the reduced row echelon form of the matrix.
Returns the transpose of the matrix as a new object.
pymatrix module exports the following functions:
u . v- the inner/scalar/dot product of the vectors
u x v- the vector/cross product of the 3D column vectors
Shortcut function for instantiating
Matrixobjects; supports the syntax of the various static instantiation methods.
An invalid operation on a matrix object will raise a