Guide

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.

Instantiation

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:

string = '''
1 2 3/7
4/7 5 6
'''

m = Matrix.from_string(
    string,
    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)

The shortcut matrix() function supports the syntax of all three static methods:

m = matrix([[1, 2, 3]])
m = matrix('1 2 3')
m = matrix(3)

Iteration

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:
    ...

Alternatively, the elements() method returns an iterator over a tuple including row and column indices:

for row, col, element in matrix.elements():
    ...

Indexing

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[0][0] rather than matrix[1][1].

Matrix Methods

Matrix objects support the following methods:

.adjoint()

Returns the adjoint matrix as a new object.

.cofactor(row, col)

Returns the specified cofactor.

.cofactors()

Returns the matrix of cofactors as a new object.

.col(n)

Returns an iterator over the specified column.

.cols()

Returns a column iterator for each column in the matrix.

.colvec(n)

Returns the specified column as a new column vector.

.copy()

Returns a copy of the matrix.

.cross(other)

Returns the vector product of the matrix with other as a new matrix.

.del_col(col)

Returns a new matrix with the specified column deleted.

.del_row(row)

Returns a new matrix with the specified row deleted.

.det()

Returns the determinant of the matrix.

.dir()

Vectors only. Returns the unit vector in the direction of the vector.

.dot(other)

Returns the scalar product of the matrix with other.

.elements()

Iterator returning the tuple (row, col, element).

.equals(other, delta)

True if the matrices are the same size and their corresponding elements agree to within delta.

.inv()

Returns the inverse matrix if it exists, otherwise raises MatrixError.

.is_invertible()

True if the matrix is invertible.

.is_square()

True if the matrix is square.

.len()

Vectors only. Returns the length of the vector.

.map(func)

Returns a new matrix formed by mapping func to each element.

.minor(row, col)

Returns the specified minor.

.rank()

Returns the rank of the matrix.

.ref()

Returns the row echelon form of the matrix.

.row(n)

Returns an iterator over the specified row.

.rowop_add(r1, m, r2)

In-place row operation. Adds m times row r2 to row r1.

.rowop_multiply(row, m)

In-place row operation. Multiplies the specified row by the scalar m.

.rowop_swap(r1, r2)

In-place row operation. Interchanges the two specified rows.

.rows()

Returns a row iterator for each row in the matrix.

.rowvec(n)

Returns the specified row as a new row vector.

.rref()

Returns the reduced row echelon form of the matrix.

.trans()

Returns the transpose of the matrix as a new object.

Module Functions

The pymatrix module exports the following functions:

dot(u, v)

Returns u . v - the inner/scalar/dot product of the vectors u and v.

cross(u, v)

Returns u x v - the vector/cross product of the 3D column vectors u and v.

matrix()

Shortcut function for instantiating Matrix objects; supports the syntax of the various static instantiation methods.

Exceptions

An invalid operation on a matrix object will raise a MatrixError exception.