Matrix Class#

class rocketpy.mathutils.vector_matrix.Matrix[source]#

Pure Python 3x3 Matrix class for simple matrix-matrix and matrix-vector operations.

Notes

Instances of the Matrix class are immutable. Real and complex components are supported.

Examples

Creating a Matrix instance requires passing its components as a nested iterable:

>>> M = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> M
Matrix([1, 2, 3],
       [4, 5, 6],
       [7, 8, 9])

Matrix instances can be indexed and sliced like lists:

>>> M[0]
[1, 2, 3]
>>> M[0][0]
1
>>> M[0, 0]
1
>>> M[0, 0:2]
[1, 2]

Matrix instances components can be accessed as attributes:

>>> M.xx, M.xy, M.xz
(1, 2, 3)

Matrix instances can be called as functions, if their elements are callable:

>>> M = Matrix([[lambda x: x**1, lambda x: x**2, lambda x: x**3],
...             [lambda x: x**4, lambda x: x**5, lambda x: x**6],
...             [lambda x: x**7, lambda x: x**8, lambda x: x**9]])
>>> M(2)
Matrix([2, 4, 8],
       [16, 32, 64],
       [128, 256, 512])

Matrix instances can be added, subtracted, multiplied and divided by scalars:

>>> M = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> M + M
Matrix([2, 4, 6],
       [8, 10, 12],
       [14, 16, 18])
>>> M - M
Matrix([0, 0, 0],
       [0, 0, 0],
       [0, 0, 0])
>>> M * 2
Matrix([2, 4, 6],
       [8, 10, 12],
       [14, 16, 18])
>>> M / 2
Matrix([0.5, 1.0, 1.5],
       [2.0, 2.5, 3.0],
       [3.5, 4.0, 4.5])

Matrix instances can be multiplied (inner product) by other matrices:

>>> M = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> N = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> M @ N
Matrix([30, 36, 42],
       [66, 81, 96],
       [102, 126, 150])

Matrix instances can be used to transform vectors by the inner product:

>>> M = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> v = Vector([1, 2, 3])
>>> M @ v
Vector(14, 32, 50)

Matrix instances can be transposed and inverted:

>>> M = Matrix([[1, 2, 3], [4, 0, 6], [7, 8, 9]])
>>> M.transpose
Matrix([1, 4, 7],
       [2, 0, 8],
       [3, 6, 9])
>>> M.inverse
Matrix([-0.8, 0.1, 0.2],
       [0.1, -0.2, 0.1],
       [0.5333333333333333, 0.1, -0.13333333333333333])

Matrix instances can be element-wise operated on by callables:

>>> M = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> M.element_wise(lambda x: x**2)
Matrix([1, 4, 9],
       [16, 25, 36],
       [49, 64, 81])

Determinants can be calculated:

>>> M = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> M.det
0
>>> abs(M)
0

Matrices can be compared for equality:

>>> M = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> N = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> M == N
True
>>> M != N
False
__init__(components)[source]#

Matrix class constructor.

Parameters:

components (3x3 array-like) – 3x3 array-like with matrix components. Indexing must be [row, column].

Examples

>>> M = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> M
Matrix([1, 2, 3],
       [4, 5, 6],
       [7, 8, 9])
__getitem__(args)[source]#

Adds support for indexing and slicing.

__iter__()[source]#

Adds support for iteration.

__call__(*args)[source]#

Adds support for calling a matrix as a function, if its elements are callable.

Parameters:

args (tuple) – Arguments to be passed to the matrix elements.

Returns:

Matrix with the same shape as the original, but with its elements replaced by the result of calling them with the given arguments.

Return type:

Matrix

Examples

>>> M = Matrix([[lambda x: x**1, lambda x: x**2, lambda x: x**3],
...             [lambda x: x**4, lambda x: x**5, lambda x: x**6],
...             [lambda x: x**7, lambda x: x**8, lambda x: x**9]])
>>> M(2)
Matrix([2, 4, 8],
       [16, 32, 64],
       [128, 256, 512])
__len__()[source]#

Adds support for the len() function.

shape#

Shape of the matrix.

Type:

tuple

trace#

Matrix trace, sum of its diagonal components.

transpose#

Matrix transpose.

det#

Matrix determinant.

is_diagonal#

Boolean indicating if matrix is diagonal.

Parameters:

tol (float, optional) – Tolerance used to determine if non-diagonal elements are negligible. Defaults to 1e-6.

Returns:

True if matrix is diagonal, False otherwise.

Return type:

bool

Examples

>>> M = Matrix([[1, 0, 0], [0, 2, 0], [0, 0, 3]])
>>> M.is_diagonal
True
>>> M = Matrix([[1, 0, 0], [0, 2, 0], [0, 1e-7, 3]])
>>> M.is_diagonal
True
>>> M = Matrix([[1, 0, 0], [0, 2, 0], [0, 1e-5, 3]])
>>> M.is_diagonal
False
inverse#

Matrix inverse.

Returns:

Inverse of the matrix.

Return type:

Matrix

Notes

If the matrix is diagonal, the inverse is computed by inverting its diagonal elements. If not, the inverse is computed using the adjugate matrix.

Raises:

ZeroDivisionError – If the matrix is singular.

__abs__()[source]#

Matrix determinant.

__neg__()[source]#

-1 times 3x3 matrix self.

__add__(other)[source]#

Sum two 3x3 matrices.

__sub__(other)[source]#

Subtract two 3x3 matrices.

__mul__(other)[source]#

Element wise multiplication of 3x3 matrix self by scalar other.

__rmul__(other)[source]#

Element wise multiplication of 3x3 matrix self by scalar other.

__truediv__(other)[source]#

Element wise division is carried out.

__matmul__(other)[source]#

Dot (inner) product between two 3x3 matrices or between 3x3 matrix and R3 vector.

Parameters:

other (Matrix or Vector) – The other matrix or vector.

Returns:

The result of the dot product. A Matrix if other if Matrix, and a Vector if other is Vector.

Return type:

Matrix or Vector

Examples

>>> M = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> v = Vector([1, 2, 3])
>>> M @ v
Vector(14, 32, 50)
>>> M = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> N = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> M @ N
Matrix([30, 36, 42],
       [66, 81, 96],
       [102, 126, 150])
__pow__(other)[source]#

Exponentiation of 3x3 matrix by integer other.

Parameters:

other (int) – The exponent.

Returns:

The result of exponentiation.

Return type:

Matrix

Examples

>>> M = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> M ** 2
Matrix([30, 36, 42],
       [66, 81, 96],
       [102, 126, 150])
__eq__(other)[source]#

Equality of two 3x3 matrices.

Parameters:

other (Matrix) – The other matrix.

Returns:

True if the two matrices are equal, False otherwise.

Return type:

bool

Examples

>>> M = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> N = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> M == N
True
>>> M = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> N = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 10]])
>>> M == N
False

Notes

Equality is determined by comparing each element of the two matrices with an absolute tolerance of 1e-9 using Python’s cmath.isclose.

element_wise(operation)[source]#

Element wise operation.

Parameters:

operation (callable) – Callable with a single argument, which should take an element and return the result of the desired operation.

Returns:

The result of the element wise operation.

Return type:

Matrix

Examples

>>> M = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> M.element_wise(lambda x: x ** 2)
Matrix([1, 4, 9],
       [16, 25, 36],
       [49, 64, 81])
dot(other)[source]#

Dot product between two 3x3 matrices or between 3x3 matrix and R3 vector.

static identity()[source]#

Returns the 3x3 identity matrix.

static zeros()[source]#

Returns the 3x3 zero matrix.

static transformation(quaternion)[source]#

Returns the transformation Matrix from frame B to frame A, where B is rotated by the quaternion q with respect to A.

Parameters:

q (tuple of 4 floats) – The quaternion representing the rotation from frame A to frame B. Example: (cos(phi/2), 0, 0, sin(phi/2)) represents a rotation of phi around the z-axis. Note: the quaternion must be normalized.

Returns:

The transformation matrix from frame B to frame A.

Return type:

Matrix