Types
Matrix Types
- 2x2 matrices
typedef union {
nml_t elems[4];
Vec2 cols[2];
} Mat2;
- 3x3 matrices
typedef union {
nml_t elems[9];
Vec3 cols[3];
} Mat3;
- 4x4 matrices
typedef union {
nml_t elems[16];
Vec4 cols[4];
} Mat4;
Initialization
Initialization Functions
Initialize from a 2d Array (Column-Major)
- Reference
int mat2Init(const nml_t arr[4], Mat2 *mOut);
int mat3Init(const nml_t arr[9], Mat3 *mOut);
int mat4Init(const nml_t arr[16], Mat4 *mOut);
-
Parameters
arr: 2d array of (float/double)s- For
Mat2: arr[4] - For
Mat3: arr[9] - For
Mat4: arr[16]
- For
mOut: A matrix populated with the values of the initial array
-
Return Value
int: Error code
-
Example
nml_t array[4] = { 1.0, 3,0
2.0, 4.0 };
Mat2 A;
mat2Init(array, &A);
- Representation \[ \text{A} = \begin{bmatrix} 1.0 & 2.0 \ 3.0 & 4.0 \end{bmatrix} \]
Initialize a Zero matrix
- Reference
int mat2InitZero(Mat2 *mOut);
int mat3InitZero(Mat3 *mOut);
int mat4InitZero(Mat4 *mOut);
-
Parameters
mOut: A zero initialized matrix
-
Return Value
int: Error code
-
Example
Mat3 A;
mat2InitZero(Mat3 *mOut);
- Representation \[ \text{A} = \begin{bmatrix} 0 & 0 & 0 \ 0 & 0 & 0 \ 0 & 0 & 0 \end{bmatrix} \]
Initialize a Diagonal Matrix
- Reference
int mat2Diagonal(nml_t val, Mat2 *mOut);
int mat3Diagonal(nml_t val, Mat3 *mOut);
int mat4Diagonal(nml_t val, Mat4 *mOut);
-
Parameters
val: Value to assign to the diagonal elementsmOut: A diagonal matrix with diagonal elements set toval
-
Return Value
int: Error code
-
Example
Mat4 A;
mat4Diagonal(5.0, &A);
- Representation \[ \text{A} = \begin{bmatrix} 5.0 & 0 & 0 & 0 \ 0 & 5.0 & 0 & 0 \ 0 & 0 & 5.0 & 0 \ 0 & 0 & 0 & 5.0 \end{bmatrix} \]
Initialize a Identity Matrix
- Reference
int mat2Identity(Mat2 *mOut);
int mat3Identity(Mat3 *mOut);
int mat4Identity(Mat4 *mOut);
-
Parameters
mOut: A identity matrix
-
Return Value
int: Error code
-
Example
Mat2 A;
mat2Identity(&A);
- Representation \[ \text{A} = \begin{bmatrix} 1 & 0 \ 0 & 1 \end{bmatrix} \]
Basic Operations
Basic Matrix Operations
Matrix Addition
- Reference
int mat2Add(Mat2 *mat1, Mat2 *mat2, Mat2 *mOut);
int mat3Add(Mat3 *mat1, Mat3 *mat2, Mat3 *mOut);
int mat4Add(Mat4 *mat1, Mat4 *mat2, Mat4 *mOut);
-
Parameters
mat1: First matrix operandmat2: Second matrix operandmOut: A new matrix that is the result of element-wise addition of mat1 and mat2
-
Return Value
int: Error code
-
Example
Mat2 mat1, mat2, result;
mat2Identity(&mat1);
mat2Diagonal(2.0, &mat2);
mat2Add(&mat1, &mat2, &result);
- Representation \[ \text{mat1}+\text{mat2}=\text{result} \]
\[ \begin{bmatrix} 1 & 0 \ 0 & 1 \end{bmatrix} \quad \text{+} \quad \begin{bmatrix} 2 & 0 \ 0 & 2 \end{bmatrix} \quad \text{=} \quad \begin{bmatrix} 3 & 0 \ 0 & 3 \end{bmatrix} \]
Matrix Subtraction
- Reference
int mat2Sub(Mat2 *mat1, Mat2 *mat2, Mat2 *mOut);
int mat3Sub(Mat3 *mat1, Mat3 *mat2, Mat3 *mOut);
int mat4Sub(Mat4 *mat1, Mat4 *mat2, Mat4 *mOut);
-
Parameters
mat1: First matrix operand (minuend)mat2: Second matrix operand (subtrahend)mOut: A new matrix that is the result of element-wise subtraction of mat2 from mat1
-
Return Value
int: Error code
-
Example
Mat3 mat1, mat2, result;
mat3Diagonal(5.0, &mat1);
mat3Identity(&mat2);
mat3Sub(&mat1, &mat2, &result);
- Representation \[ \text{mat1}-\text{mat2}=\text{result} \]
\[ \begin{bmatrix} 5 & 0 & 0 \ 0 & 5 & 0 \ 0 & 0 & 5 \end{bmatrix} \quad \text{-} \quad \begin{bmatrix} 1 & 0 & 0 \ 0 & 1 & 0 \ 0 & 0 & 1 \end{bmatrix} \quad \text{=} \quad \begin{bmatrix} 4 & 0 & 0 \ 0 & 4 & 0 \ 0 & 0 & 4 \end{bmatrix} \]
Matrix Scaling
- Reference
int mat2Scale(Mat2 *mat, nml_t s, Mat2 *mOut);
int mat3Scale(Mat3 *mat, nml_t s, Mat3 *mOut);
int mat4Scale(Mat4 *mat, nml_t s, Mat4 *mOut);
-
Parameters
mat: Matrix to be scaleds: Scalar valuemOut: A new matrix with all elements of mat multiplied by scalar s
-
Return Value
int: Error code
-
Example
Mat4 mat, scaled;
mat4Identity(&mat);
mat4Scale(mat, 3.0, &scaled);
- Representation \[ \text{s}\cdot\text{mat}=\text{result} \]
\[ \text{3.0} \quad \cdot \quad \begin{bmatrix} 1 & 0 & 0 & 0 \ 0 & 1 & 0 & 0 \ 0 & 0 & 1 & 0 \ 0 & 0 & 0 & 1 \end{bmatrix} \quad \text{=} \quad \begin{bmatrix} 3 & 0 & 0 & 0 \ 0 & 3 & 0 & 0 \ 0 & 0 & 3 & 0 \ 0 & 0 & 0 & 3 \end{bmatrix} \]
Matrix negation
- Reference
int mat2Negate(Mat2 *mat, Mat2 *mOut);
int mat3Negate(Mat3 *mat, Mat3 *mOut);
int mat4Negate(Mat4 *mat, Mat4 *mOut);
-
Parameters
mat: Matrix to negatemOut: A new matrix with the elements negated
-
Return Value
int: Error code
-
Example
nml_t arr[4] = { 1, 3, 2, 4 };
Mat2 mat, result;
mat2Init(arr, &mat);
mat2Negate(&mat, &result);
- Representation \[ \text{-} \text{mat} \quad = \quad \text{result} \]
\[ \text{-} \begin{bmatrix} 1 & 2 \ 3 & 4 \end{bmatrix} \quad \text{=} \quad \begin{bmatrix} -1 & -2 \ -3 & -4 \end{bmatrix} \quad \]
Matrix Multiplication
Matrix Multiplication
Hadamard Matrix
- Reference
int mat2Hadamard(Mat2 *mat1, Mat2 *mat2, Mat2 *mOut);
int mat3Hadamard(Mat3 *mat1, Mat3 *mat2, Mat3 *mOut);
int mat4Hadamard(Mat4 *mat1, Mat4 *mat2, Mat4 *mOut);
-
Parameters
mat1: First Matrix Operandmat2: Second Matrix OperandmOut: A new matrix that is the result of Hadamard Product
-
Return Value
int: Error code
-
Example
nml_t arr1[9] = {1.0, 4.0, 7.0,
2.0, 5.0, 8.0,
3.0, 6.0, 9.0};
nml_t arr2[9] = {9.0, 6.0, 3.0,
8.0, 5.0, 2.0,
7.0, 4.0, 1.0};
nml_t expected[9] = {9.0, 24.0, 21.0,
16.0, 25.0, 16.0,
21.0, 24.0, 9.0};
Mat mat2, mat2, result;
mat2Init(arr, &A);
mat2Init(arr, &B);
mat2Hadamard(&A, &B, &result);
- Representation \[ \text{mat1}\odot\text{mat2}=\text{result} \]
\[ \begin{bmatrix} 1.0 & 4.0 & 7.0 \ 2.0 & 5.0 & 8.0 \ 3.0 & 6.0 & 9.0 \end{bmatrix} \quad \odot \quad \begin{bmatrix} 9.0 & 6.0 & 3.0 \ 8.0 & 5.0 & 2.0 \ 7.0 & 4.0 & 1.0 \end{bmatrix} \quad \text{=} \quad \begin{bmatrix} 9.0 & 24.0 & 21.0 \ 16.0 & 25.0 & 16.0 \ 21.0 & 24.0 & 9.0 \end{bmatrix} \quad \]
Matrix-Vector Multiplication
- Reference
int mat2MulMat2(Mat2 *mat, Vec2 *vec, Vec2 *vOut);
int mat3MulMat3(Mat3 *mat, Vec3 *vec, Vec3 *vOut);
int mat4MulMat4(Mat4 *mat, Vec4 *vec, Vec4 *vOut);
-
Parameters
mat: Matrix Operandvec: Vector OperandvOut: A new vector that is the result of Matrix-Vector Multiplication
-
Return Value
int: Error code
-
Example
nml_t mat_arr[9] = {1.0, 4.0, 7.0,
2.0, 5.0, 8.0,
3.0, 6.0, 9.0};
Mat3 mat;
Vec3 vec, result;
mat3Init(mat_arr, &mat);
vec3Init(2.0, 3.0, 4.0, &vec);
mat3MulVec3(&mat, &vec, &result);
- Representation \[ \text{mat}\cdot\text{vec}=\text{result} \]
\[ \begin{bmatrix} 1.0 & 4.0 & 7.0 \ 2.0 & 5.0 & 8.0 \ 3.0 & 6.0 & 9.0 \end{bmatrix} \quad \text{x} \quad \begin{pmatrix} 2.0 \ 3 0 \ 4.0 \ \end{pmatrix} \quad \text{=} \quad \begin{pmatrix} 20.0 \ 47.0 \ 74.0 \end{pmatrix} \quad \]
Matrix-Matrix Multiplication
- Reference
int mat2MulMat2(Mat2 *mat1, Mat2 *mat2, Mat2 *mOut);
int mat3MulMat3(Mat3 *mat1, Mat3 *mat2, Mat3 *mOut);
int mat4MulMat4(Mat4 *mat1, Mat4 *mat2, Mat4 *mOut);
-
Parameters
mat1: First Matrix Operandmat2: Second Matrix OperandmOut: A new matrix that is the result of Matrix-Matrix Multiplication
-
Return Value
int: Error code
-
Example
nml_t arr1[4] = {1.0, 3.0,
2.0, 4.0};
nml_t arr2[4] = {4.0, 2.0,
3.0, 1.0};
Mat mat1, mat2, result;
mat2Init(arr, &mat1);
mat2Init(arr, &mat2);
mat2Hadamard(&mat1, &mat2, &result);
- Representation \[ \text{mat}\cdot\text{vec}=\text{result} \]
\[ \begin{bmatrix} 1.0 & 3.0 \ 2.0 & 4.0 \end{bmatrix} \quad \text{+} \quad \begin{bmatrix} 4.0 & 2.0 \ 3.0 & 1.0 \end{bmatrix} \quad \text{=} \quad \begin{bmatrix} 8.0 & 20.0 \ 5.0 & 13.0 \end{bmatrix} \quad \]