The matrix class defined in C++ forms the basis for all ModelSpace mathematics. The class is a templated, safe library designed for use in embedded and simulation operations. It is also swig wrapped for use in Python.
This guide provides the basics on how to interact with the Matrix class. Note that all classes derived from Matrix, including vectors and attitude representations, may be interacted with using Matrix functions, except where explicitly noted.
For a full guide on how to interact with Matrix, check out the Doxygen page. This guide is abbreviated and provided for simplicity. The official/formal documentation is provided by Doxygen. The Doxygen for Matrix is found here:
The Matrix Class
Description and Naming
The Matrix class is a templated class with three template arguments - T, R, and C.
Matrix<T, R, C>
produces an RxC matrix of type T.
The three arguments are, respectively:
-
T - The type of the Matrix. This should be a floating point type.
-
R - The number of rows of the matrix. This should be an integer input
-
C - The number of columns in the matrix. This should be an integer input.
Because the class is templated, macros are provided to simplify interaction with the Matrix class. These are described in the table below. The Matrix class may be freely defined to any size with at least one row and one column in C. The macros must be used for Matrix declaration in C.
Macros are defined for sizes up to 6x6. Examples provided below (not an exhaustive list). The full type definitions are found in modelspace/clockwerk/src/core/macros.h.
Matrix Template Arguments | Macro |
---|---|
|
|
|
|
Creating a Matrix
Description | C++ Example | Python Example |
---|---|---|
Default Constructor - Creates a matrix filled with zeros. |
|
|
Constructor with Initial Value - Creates a matrix where all elements are the same value. |
|
|
Constructor with initializer list - Creates a matrix from an initializer list |
|
|
Constructor with 2D Array - Creates a matrix from a 2D array. |
|
|
Copy Constructor - Creates a new matrix as a copy of another matrix. |
|
|
Setting and Getting Values
Description | C++ Example | Python Example |
---|---|---|
Setting a Value - Set the value at a specific row and column. |
|
|
Getting a Value - Get the value at a specific row and column. |
|
|
Bracket access - Directly access the value at a specific row and column (unsafe for embedded operations). |
|
|
Direct Access - Directly access the value at a specific row and column (unsafe for embedded operations). |
|
|
Matrix Operations
Description | C++ Example | Python Example |
---|---|---|
Dumping Matrix Content - Prints the matrix to the console. |
|
|
Getting Matrix as a String - Returns the matrix as a string. |
|
|
Transposing a Matrix - Transposes the matrix. |
|
|
Finding Maximum Value - Finds the maximum value and its position. |
|
|
Finding Minimum Value - Finds the minimum value and its position. |
|
|
Determinant of a Matrix - Calculates the determinant of the matrix. |
|
|
Inverse of a Matrix - Calculates the inverse of the matrix. |
|
|
Trace of a Matrix - Calculates the trace of the matrix (sum of diagonal elements). |
|
|
Additional Operations
Description | C++ Example | Python Example |
---|---|---|
Setting All Elements to Zero - Sets all elements of the matrix to zero. |
|
|
Matrix Math Functions
Description and Naming
The matrix math functions operate on the Matrix
class. They have two flavors: functions which return a pass by reference and
an error code if the operation is invalid, which are typically used for embedded/safe operations, and functions
which are overloaded and more intuitive operators which return directly without an error code. The matrix functions
follow a consistent naming convention:
-
Capital letters represent matrices.
-
Lowercase letters represent scalars.
-
Vectors are treated as 1-dimensional matrices.
-
Pass-by-reference return values are all named
result
. -
The letter used in the function indicates the order of operations (e.g., A/a comes first, then B/b).
For a complete guide on interacting with the matrix math functions, refer to the Doxygen documentation: Matrix Math Functions Doxygen
Adding Matrices
Description | C++ Example | Python Example |
---|---|---|
Adding a Scalar to a Matrix - Adds a scalar to a matrix and returns the result. |
|
|
Element-wise Addition - Adds two matrices element-wise and returns the result. |
|
|
Matrix Addition - Overloaded operator for adding two matrices. |
|
|
Scalar Addition - Overloaded operator for adding a scalar to a matrix. |
|
|
Subtracting Matrices
Description | C++ Example | Python Example |
---|---|---|
Element-wise Subtraction - Subtracts one matrix from another element-wise and returns the result. |
|
|
Matrix Subtraction - Overloaded operator for subtracting one matrix from another. |
|
|
Multiplying Matrices
Description | C++ Example | Python Example |
---|---|---|
Multiplying Two Matrices - Multiplies two matrices and returns the result. |
|
|
Multiplying a Scalar by a Matrix - Multiplies a scalar by a matrix and returns the result. |
|
|
Element-wise Multiplication - Multiplies two matrices element-wise and returns the result. |
|
|
Matrix Multiplication - Overloaded operator for multiplying two matrices. |
|
|
Scalar Multiplication - Overloaded operator for multiplying a matrix by a scalar. |
|
|