Guide
Pymatrix exports a lightweight, easytouse 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 heavyduty 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:
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)
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 lefttoright by column, then toptobottom by row; i.e. the topleft element will be returned first, the bottomright 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 twodimensional arrays:
matrix[row][col] = element element = matrix[row][col]
Note that indices are zerobased in accordance with programming convention rather than onebased in typical math style, i.e. the matrix's topleft 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)
Inplace row operation. Adds
m
times rowr2
to rowr1
. 
rowop_multiply(row, m)
Inplace row operation. Multiplies the specified row by the scalar
m
. 
rowop_swap(r1, r2)
Inplace 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 vectorsu
andv
. 
cross(u, v)
Returns
u x v
 the vector/cross product of the 3D column vectorsu
andv
. 
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.