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])
- __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:
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])
- property shape#
Shape of the matrix.
- Type:
tuple
- property trace#
Matrix trace, sum of its diagonal components.
- property transpose#
Matrix transpose.
- property det#
Matrix determinant.
- property is_diagonal#
Boolean indicating if matrix is diagonal.
- 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
- property inverse#
Matrix inverse.
- Returns:
Inverse of the matrix.
- Return type:
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.
- __matmul__(other)[source]#
Dot (inner) product between two 3x3 matrices or between 3x3 matrix and R3 vector.
- Parameters:
- Returns:
The result of the dot product. A Matrix if other if Matrix, and a Vector if other is Vector.
- Return type:
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:
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:
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.
See also
- round(decimals=0)[source]#
Round all the values matrix to a given number of decimals.
- Parameters:
decimals (
int
, optional) – Number of decimal places to round to. Defaults to 0.- Returns:
The rounded matrix.
- Return type:
Examples
>>> M = Matrix([[1.1234, 2.3456, 3.4567], [4.5678, 5.6789, 6.7890], [7.8901, 8.9012, 9.0123]]) >>> M.round(2) Matrix([1.12, 2.35, 3.46], [4.57, 5.68, 6.79], [7.89, 8.9, 9.01])
- 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
of4 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.- Returns:
Matrix
– The transformation matrix from frame B to frame A.Reference
---------
https (
//en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles
)
- static transformation_euler_angles(roll, pitch, roll2)[source]#
Returns the transformation Matrix from frame B to frame A, where B is rotated by the Euler angles roll, pitch and roll2 in an instrinsic 3-1-3 sequence with respect to A.
- Parameters:
roll (
float
) – The roll angle in radians.pitch (
float
) – The pitch angle in radians.roll2 (
float
) – The roll2 angle in radians.
- Returns:
The transformation matrix from frame B to frame A.
- Return type: