Matrix API Reference#
Table of Contents
Constructor#
- class pyevspace.Matrix([initializer0, initializer1, initializer2])#
Create a
Matrix
object to initialized values.Matrix can be initialized with optional initializer list, with each initializer representing a row of the matrix values. If using initializer list, all three arguments must be present. If no arguments are used, the initial values default to zero.
>>> row0 = (1, 2, 3) >>> row1 = (4, 5, 6) >>> row2 = (7, 8, 9) >>> matrix = Matrix(row0, row1, row2)
- Parameters:
initializer0 (iterable) – an iterable object of length three whose values are numeric types (initializer1 and initializer2 must also be set)
initializer1 (iterable) – an iterable object of length three whose values are numeric types (initializer0 and initializer2 must also be set)
initializer2 (iterable) – an iterable object of length three whose values are numeric types (initializer0 and initializer1 must also be set)
- Raises:
TypeError – if any initializer is not an iterable
TypeError – if any value of an initializer is not a numeric type
ValueError – if initializer or any of the sub-iterables of initializer are not exactly length three
Attributes#
Arithmetic Operators#
- Matrix.__add__(other)#
Standard matrix addition of two matrices.
- Matrix.__sub__(other)#
Standard matrix subtraction of two matrices.
- Matrix.__mul__(other)#
Standard multiplication depending on the type of other:
- Matrix.__truediv__(scalar)#
Standard scalar division.
- Matrix.__iadd__(other)#
Inplace standard vector addition.
- Matrix.__isub__(other)#
Inplace standard vector subtraction.
- Matrix.__imul__(scalar)#
Inplace standard scalar multiplication.
- Matrix.__itruediv__(scalar)#
Inplace standard scalar division.
- Matrix..__repr__()#
Returns a string representation of self, representative of a constructor call with the component values of the matrix. The format fo the components follows the same as
Matrix.__str__()
.- Returns:
a string representation of self
- Return type:
str
- Matrix.__str__()#
Returns a string representation of self, formatted similarly to a list of lists.
- Returns:
a string representation of self
- Return type:
str
- Matrix.__reduce__()#
Allows support for pickling.
- Returns:
a tuple used for reconstructing self’s state
- Return type:
tuple
Module Methods#
- pyevspace.det(matrix)#
Computes the determinate of a matrix. Useful for determining if a matrix is invertible i.e. the determinate is non-zero.
- pyevspace.transpose(matrix)#
Returns the transpose of matrix. The transpose of a matrix is simply a flipped matrix by switching its rows and columns. Therefore if T is the transpose of M,
M[i, j] == T[j, i]
.
Logical Operators#
- Matrix.__eq__(other)#
Compares each component of two matrices for equality.
- Matrix.__ne__(other)#
Compares each component of two matrices for an inequality.
- Returns:
True if any component of other and self are not equivalent
- Return type:
bool
- Raises:
TypeError – if other is not a
Matrix
type
All other logic operators are not implemented and will raise a
TypeError
.
Mapping Protocol#
The mapping protocol allows for accessing and mutating components of the
matrix. This allows for the square bracket notation, similar to the
sequence protocol of a Vector
. The indices are the row and
column of the component you wish to access. A TypeError
is
raised if either index is not an integer, and an IndexError
is
raised if either index is not in the interval [0, 2].
>>> matrix = Matrix((1, 2, 3), (4, 5, 6), (7, 8, 9))
>>> matrix[1, 1]
5.0
>>> matrix[0, 2] = 11
>>> print(matrix)
[[1, 2, 11]
[4, 5, 6]
[7, 8, 9]]
>>> matrix[2, 3]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: col index (3) must be in [0-2]
>>> matrix[1, 1.0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'float' object cannot be interpreted as an integer
A single argument using square bracket notation allows access to a row
of a Matrix
. Because the matrix type is basically a data
array, there is no Python type that represents a row of the matrix type.
The closest thing would be a memoryview
object, which gives
access to the memory buffer of the desired row of the matrix.
>>> matrix = Matrix((1, 2, 3), (4, 5, 6), (7, 8, 9))
>>> row1 = matrix[1]
>>> row1List = row1.tolist()
>>> row1List
[4.0, 5.0, 6.0]
>>> row1[1] = 12.3
>>> print(matrix)
[[1, 2, 3]
[4, 12.3, 6]
[7, 8, 9]]
While this provides a way of quickly accessing an entire row of data, it’s not the most efficient way to change a value of a matrix since a buffer object has to be created first. The ordinary mapping protocol should be used in this case:
>>> matrix = Matrix((1, 2, 3), (4, 5, 6), (7, 8, 9))
>>> # faster
>>> matrix[1, 1] = 10
>>> # slower
>>> matrix[1][1] = 10
Buffer Protocol#
The Matrix
class supports the buffer protocol and can be used
by other objects which also support the buffer interface. For example
it can be used to instantiate a memoryview
object
>>> matrix = Matrix((1, 2, 3), (4, 5, 6), (7, 8, 9))
>>> view = memoryview(matrix)
>>> view[1, 1] = 1.69
>>> print(matrix)
[[1, 2, 3]
[4, 1.69, 6]
[7, 8, 9]]
as well as other third-party numeric libraries that support buffers like NumPy.
>>> matrix = Matrix((1, 2, 3), (4, 5, 6), (7, 8, 9))
>>> array = np.ndarray((3, 3), buffer=matrix)
>>> array[0, 1] = 3.14
>>> print(matrix)
[[1, 3.14, 3]
[4, 5, 6]
[7, 8, 9]]