matrix

A Python package for various 2D matrix operations and manipulations.

View on GitHub

Matrix Object Interactions

The Matrix class implements the following python object interactions.

Intelligent string representation

Calling str(matrix) returns a string representation of the matrix.

Subscription

Note:

A subscription can be performed on a matrix to get:

  1. The element at the given coordinate, if the subscript is a tuple of integers (row, col).
    • e.g matrix[1, 2]
    • Indices less than 1 are not allowed.
  2. a new Matrix instance, the intersection of the selected rows and columns (called a “block-slice”), if the subscript is a tuple of slices.
    • e.g matrix[::2, 3:4]
    • the first slice selects rows.
    • the second slice selects columns.
    • any ‘stop’ greater than the number of rows/columns is “forgiven”.
    • Indices or steps less than 1 are not allowed.

These forms of subscription can also be used in an assignment to replace the element or block-slice.

They also support augmented assignments, as supported by the object returned by the form of subscription.

Truth-value Testing

A matrix has a truth values of False is it’s a null matrix, and True otherwise.

Membership test for elements

A matrix can be tested if it contains a certain element.

Iteration over elements

Iterating over a matrix instance yields it’s elements in order starting from the first row.

Per-element rounding

This can be done in two ways: