Matrices and Matrix Arrays

bewegung offers matrices and matrix arrays. Both of them work for 2x2 and 3x3 shapes. They are intended for simple tasks like rotations of vectors and vector arrays.

The Matrix Class

class bewegung.Matrix(matrix=typing.Iterable[typing.Iterable[~ N]], dtype=None, meta=None)

A simple matrix implementation for transforming vectors and vector arrays

Mutable.

Parameters
  • matrix – 2D or 3D arrangement in a list of lists containing Python numbers

  • dtype (Optional[Type[Number]]) – Data type. Derived from entries in matrix if not explicitly provided.

  • meta (Optional[Dict[str, Union[str, bytes, Number]]]) – A dict holding arbitrary metadata

__eq__(other)

Equality check between matrices

Parameters

other (Any) – Another matrix

Return type

Union[bool, NotImplementedType]

__getitem__(index)

Item access, returns value at position

Parameters

index (Tuple[int, int]) – Row and column index

Return type

Number

__matmul__(other)

Multiplies the matrix with a vector or array of vectors and returns the resulting new vector or array of vectors. Raises an exception if matrix and vector or array of vectors have different numbers of dimensions.

Parameters

other (Any) – A 2D or 3D vector or array of vectors

Return type

Union[Vector, VectorArray, NotImplementedType]

__mod__(other)

Is-close check between matrices

Parameters

other (Any) – Another matrix

Return type

Union[bool, NotImplementedType]

__repr__()

String representation for interactive use

Return type

str

__setitem__(index, value)

Item access, sets new value at position

Parameters
  • index (Tuple[int, int]) – Row and column index

  • value (Number) – New value

as_ndarray(dtype='f4')

Exports matrix as a numpy.ndarry object, shape (2, 2) or (3, 3).

Parameters

dtype (Union[str, Type[Number], dtype]) – Desired numpy data type of new vector

Return type

ndarray

as_tuple()

Exports matrix as a tuple of tuples

Return type

Tuple[Tuple[TypeVar(N, bound= Number), ...], ...]

copy()

Copies matrix & meta data

Return type

MatrixABC

property dtype: Type[numbers.Number]

(Python) data type of matrix components

Return type

Type[Number]

classmethod from_2d_rotation(cls, a, meta=None)

Generates new 2D matrix object from an angle

Parameters
  • a (Number) – An angle in radians

  • meta (Optional[Dict[str, Union[str, bytes, Number]]]) – A dict holding arbitrary metadata

Return type

MatrixABC

classmethod from_3d_rotation(cls, v, a, meta=None)

Generates new 3D matrix object from a vector and an angle. Rotates by angle around vector.

Parameters
  • v (Vector3D) – A 3D vector

  • a (Number) – An angle in radians

  • meta (Optional[Dict[str, Union[str, bytes, Number]]]) – A dict holding arbitrary metadata

Return type

MatrixABC

classmethod from_ndarray(cls, matrix, dtype=<class 'float'>, meta=None)

Generates new matrix object from numpy.ndarray object of shape (2, 2) or (3, 3)

Parameters
  • matrix (ndarray) – Input data

  • dtype (Type[Number]) – Desired (Python) data type of matrix

  • meta (Optional[Dict[str, Union[str, bytes, Number]]]) – A dict holding arbitrary metadata

Return type

MatrixABC

property meta: Dict[str, Union[str, bytes, numbers.Number]]

meta data dict

Return type

Dict[str, Union[str, bytes, Number]]

property ndim: int

Number of dimensions, either 2 or 3.

Return type

int

The MatrixArray Class

class bewegung.MatrixArray(matrix=typing.Iterable[typing.Iterable[numpy.ndarray]], dtype=None, meta=None)

An array implementation of simple matrices for transforming vectors and vector arrays

Mutable.

Parameters
  • matrix – 2D or 3D arrangement in a list of lists containing numpy nd arrays

  • dtype (Union[str, Type[Number], dtype, None]) –

  • meta (Optional[Dict[str, ndarray]]) –

__eq__(other)

Equality check between matrix arrays

Parameters

other (Any) – Another matrix array of equal length

Return type

Union[bool, NotImplementedType]

__getitem__(idx)

Item access, returns value at position

Parameters
  • index – Row, column and position index

  • idx (Union[int, slice]) –

Return type

Union[Matrix, MatrixArrayABC]

__iter__()

Iterator interface (1/2)

Return type

MatrixArrayABC

__len__()

Length of array

Return type

int

__matmul__(other)

Multiplies the matrix array with a vector or array of vectors and returns the resulting new vector or array of vectors. Raises an exception if matrix and vector or array of vectors have different numbers of dimensions.

Parameters
  • vector – A 2D or 3D vector or array of vectors

  • other (Any) –

Return type

Union[VectorArray, NotImplementedType]

__mod__(other)

Is-close check between matrix arrays

Parameters

other (Any) – Another matrix array of equal length

Return type

Union[bool, NotImplementedType]

__next__()

Iterator interface (2/2)

Return type

Matrix

__repr__()

String representation for interactive use

Return type

str

as_list()

Exports a list of bewegung.Matrix objects

Return type

List[Matrix]

as_ndarray(dtype='f4')

Exports matrix array as a numpy.ndarry object, shape (len(self), self.ndim, self.ndim).

Parameters

dtype (Union[str, Type[Number], dtype]) – Desired numpy data type of new vector

Return type

ndarray

as_tuple(copy=True)

Exports matrix array as a tuple of tuples of numpy.ndarray objects

Parameters

copy (bool) – Provide a copy of underlying numpy.ndarry

Return type

Tuple[Tuple[ndarray, ...], ...]

as_type(dtype)

Exports matrix array as another matrix array with new dtype

Parameters

dtype (Union[str, Type[Number], dtype]) – Desired numpy data type of new vector array

Return type

MatrixArrayABC

copy()

Copies matrix array & meta data

Return type

MatrixArrayABC

property dtype: numpy.dtype

(Python) data type of matrix components

Return type

dtype

classmethod from_2d_rotation(cls, a, meta=None)

Generates new 2D matrix array object from an array of angles

Parameters
  • a (ndarray) – An array of angles in radians

  • meta (Optional[Dict[str, ndarray]]) –

Return type

MatrixArrayABC

classmethod from_3d_rotation(cls, v, a, meta=None)

Generates new 3D matrix array object from a vector or vector array and an angle or one-dimensional numpy.ndarray of angles. Rotates by angle around vector.

Parameters
  • v (Union[Vector3D, VectorArray3D]) – A 3D vector or vector array

  • a (Union[Number, ndarray]) – An angle or array of angles in radians

  • meta (Optional[Dict[str, ndarray]]) –

Return type

MatrixArrayABC

classmethod from_iterable(cls, obj, dtype='f4')

Generates matrix array object from an iterable of bewegung.Matrix objects

Parameters
  • obj (Iterable[Matrix]) – iterable

  • dtype (Union[str, Type[Number], dtype]) – Desired numpy data type of new vector array

Return type

MatrixArrayABC

classmethod from_ndarray(cls, matrix_array, meta=None)

Generates new matrix array object from single numpy.ndarray object of shape (length, ndim, ndim)

Parameters
  • matrix_array (ndarray) – Input data

  • meta (Optional[Dict[str, ndarray]]) –

Return type

MatrixArrayABC

property meta: Dict[str, numpy.ndarray]

meta data dict

Return type

Dict[str, ndarray]

property ndim: int

Number of dimensions, either 2 or 3.

Return type

int