Matrix API Reference#

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#

Matrix.id#

Identity matrix. This value should not be edited.

Value:

Matrix((1, 0, 0), (0, 1, 0), (0, 0, 1))

Type:

Matrix

Arithmetic Operators#

Matrix.__add__(other)#

Standard matrix addition of two matrices.

Parameters:

other (Matrix) – the matrix to be added to self

Returns:

the matrix sum of self and other

Return type:

Matrix

Raises:

TypeError – if other is not a Matrix type

Matrix.__sub__(other)#

Standard matrix subtraction of two matrices.

Parameters:

other (Matrix) – the matrix to be subtracted from self

Returns:

the matrix difference of self and other

Return type:

Matrix

Raises:

TypeError – if other is not a Matrix type

Matrix.__mul__(other)#

Standard multiplication depending on the type of other:

  • numeric - standard scalar multiplication

  • Vector - left-hand matrix multiplication of a vector

  • Matrix - matrix multiplication of a matrix

Parameters:

other (numeric | EVector | Matrix) – the object to multiply self by

Returns:

the product of self and other

Return type:

EVector or Matrix

Raises:

TypeError – if other is not a numeric, EVector or Matrix type

Matrix.__truediv__(scalar)#

Standard scalar division.

Parameters:

scalar (numeric) – the scalar to divide each component of self by

Returns:

the scalar quotient

Return type:

Matrix

Raises:

TypeError – if scalar is not a numeric type

Matrix.__neg__()#

Negates each component of self.

Returns:

the negative of self

Return type:

Matrix

Matrix.__iadd__(other)#

Inplace standard vector addition.

Parameters:

other (Matrix) – the matrix to add to self

Raises:

TypeError – if other is not a Matrix type

Matrix.__isub__(other)#

Inplace standard vector subtraction.

Parameters:

other (Matrix) – the matrix to subtract from self

Raises:

TypeError – if other is not a Matrix type

Matrix.__imul__(scalar)#

Inplace standard scalar multiplication.

Parameters:

scalar (numeric) – the scalar to multiply each element of self by

Raises:

TypeError – if scalar is not a numeric type

Matrix.__itruediv__(scalar)#

Inplace standard scalar division.

Parameters:

scalar (numeric) – the scalar to divide each component of self by

Raises:

TypeError – if scalar is not a numeric type

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.

Parameters:

matrix (Matrix) – matrix to compute the determinate of

Returns:

determinate of matrix

Return type:

Matrix

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

Parameters:

matrix (Matrix) – the matrix to transpose

Returns:

the transpose of matrix

Return type:

Matrix

Logical Operators#

Matrix.__eq__(other)#

Compares each component of two matrices for equality.

Parameters:

other (Matrix) – The matrix to compare to self

Returns:

True if each component of other and self are equivalent

Return type:

bool

Raises:

TypeError – if other is not a Matrix type

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]]