Matrix Initialization
Here we cover the handful of ways slap provides for initializing matrices.
From an Array
As a lightweight wrapper around a raw double*, Matrix is best
initialized from a pointer, along with the size information:
-
Matrix slap_MatrixFromArray(int rows, int cols, sfloat *data)
Wraps existing data in a Matrix class.
This is the most common way to “create” a matrix. Typical usage will look something like this:
// Stack-allocated memory sfloat data_stack[24]; Matrix A = slap_MatrixFromArray(6, 4, data_stack); // Heap-allocated memory sfloat *data_heap = (sfloat*)malloc(24 * sizeof(sfloat)); Matrix B = slap_MatrixFromArray(6, 4, data_heap); free(data_heap);
- Parameters:
rows – Number of rows in the matrix
cols – Number of columns in the matrix
data – Data for the matrix. Must not be NULL, and should have at least rows * cols elements.
- Returns:
A new matrix
Default Initialization
A “default initialized” matrix is considered a “Null” matrix, a internally-defined state (that has a NULL pointer, 0 size, etc.). Use the following methods for creating and checking for Null matrices:
-
static inline Matrix slap_NullMatrix(void)
Create a “Null” matrix.
Useful for default initialization of the matrix where the data it wraps hasn’t been specified or allocated yet. Can check if a matrix is in this state using slap_IsNull().
Example
Matrix A = slap_NullMatrix();
See also: slap_IsNull(), slap_SetNull()
- Returns:
A default “Null” instance of a matrix.
-
static inline void slap_SetNull(Matrix *mat)
Set a matrix to a “Null” instance.
See also: slap_NullMatrix(), slap_IsNull()
- Parameters:
mat – [in] Pointer to the matrix to set to null
-
static inline bool slap_IsNull(Matrix mat)
Check if matrix is a “Null” instance of a matrix (i.e. uninitialized)
The “Null” state of a matrix is an internally-defined state created by the slap_NullMatrix() method.
See also: slap_NullMatrix(), slap_SetNull()
- Parameters:
mat – [in]
- Returns:
true if matrix is a “Null” matrix
Allocating Initialization
While all methods in slap assumes the user owns the underlying data, these methods
provide some convenient methods for initializing matrices on the heap.
-
Matrix slap_NewMatrix(int rows, int cols)
Allocate a new matrix on the heap.
Data will not be initialized. Wrapper around a call to
malloc. Must be followed by a call toFreeMatrix.Example
Matrix A = slap_NewMatrix(3,4); slap_FreeMatrix(A);
Header File:
"slap/new_matrix.h"- Parameters:
rows – number of rows in the matrix
cols – number of columns in the matrix
- Returns:
A new matrix
-
Matrix slap_NewMatrixZeros(int rows, int cols)
Allocate a new matrix on the heap, initialized with zeros.
Data will be initialized to zeros. Wrapper around a call to
calloc. Must be followed by a call toFreeMatrix.Example
Matrix A = slap_NewMatrixZeros(3,4); slap_FreeMatrix(A);
Header File:
"slap/new_matrix.h"- Parameters:
rows – number of rows in the matrix
cols – number of columns in the matrix
- Returns:
A new matrix
-
enum slap_ErrorCode slap_FreeMatrix(Matrix *mat)
Free the data for a matrix.
Note that this methods takes the pointer to the matrix as a argument, so that it can set the internal data pointer to NULL after the free.
Note this does NOT attempt to free the matrix object itself, only the data it wraps.
Should only be used in conjunction with slap_NewMatrix() or slap_NewMatrixZeros().
Header File:
"slap/new_matrix.h"
Note
These functions are NOT included by default when including slap/slap.h,
and must be explicitly brought in by including slap/new_matrix.h