Matrix Core — Metadata, Construction & Basic Operations¶
MATRIX METADATA¶
Matrix Structure
The Mat class uses a row-major storage layout with support for padding and step. In this class, stride is a backward-compatible alias of step. This design enables efficient memory access patterns and compatibility with DSP libraries.
Core Dimensions¶
-
int row: Number of rows in the matrix. -
int col: Number of columns in the matrix. -
int element: Total number of elements = rows × cols.
Memory Layout¶
-
int step/int stride: Total elements per row (logical columns + padding). In this class they are equivalent, andstrideis an alias ofstep. -
int pad: Number of padding elements between two rows. Padding is used for memory alignment and DSP optimization. -
int memory: Size of the data buffer = rows × step (in number of float elements).
Data Pointers¶
-
float *data: Pointer to the data buffer containing matrix elements. Elements are stored in row-major order: element at (i, j) is atdata[i * step + j]. -
float *temp: Pointer to the temporary data buffer (if allocated). Used internally for certain operations.
Memory Management Flags¶
-
bool ext_buff: Flag indicating that the matrix uses an external buffer. Whentrue, the destructor will not free the memory (caller is responsible). -
bool sub_matrix: Flag indicating that the matrix is a subset/view of another matrix. Whentrue, the matrix shares data with the parent matrix.
Memory Layout Example
For a 3×4 matrix with stride=4 (no padding):
[a b c d] row 0: data[0*4+0] to data[0*4+3]
[e f g h] row 1: data[1*4+0] to data[1*4+3]
[i j k l] row 2: data[2*4+0] to data[2*4+3]
For a 3×4 matrix with stride=6 (padding=2):
ROI STRUCTURE¶
Region of Interest
The ROI (Region of Interest) structure represents a rectangular subregion of a matrix. It's used with view_roi() and copy_roi() functions to extract or reference submatrices efficiently.
ROI Metadata¶
-
int pos_x: Starting column index (x-coordinate of the top-left corner). -
int pos_y: Starting row index (y-coordinate of the top-left corner). -
int width: Width of the ROI in columns. -
int height: Height of the ROI in rows.
ROI Constructor¶
Description:
ROI constructor initializes the ROI with the specified position and size.
Parameters:
-
int pos_x: Starting column index. -
int pos_y: Starting row index. -
int width: Width of the ROI (columns). -
int height: Height of the ROI (rows).
ROI RESIZE¶
Description:
Resizes the ROI to the specified position and size.
Parameters:
-
int pos_x: Starting column index. -
int pos_y: Starting row index. -
int width: Width of the ROI (columns). -
int height: Height of the ROI (rows).
Returns:
void
AREA ROI¶
Description:
Calculates the area of the ROI.
Parameters:
void
Returns:
int - Area of the ROI.
PRINTING FUNCTIONS¶
Debugging Tools
These functions are essential for debugging and understanding matrix state. Use them to verify matrix dimensions, memory layout, and data values during development.
Print Matrix Information¶
Description:
Prints comprehensive matrix information including:
-
Dimensions: rows, columns, elements
-
Memory layout: paddings, stride, memory size
-
Pointers: data buffer address, temporary buffer address
-
Flags: external buffer usage, sub-matrix status
-
Warnings: dimension mismatches, invalid states
Parameters:
void
Returns:
void
Usage Insights:
-
Debugging: Essential for verifying matrix state and detecting memory issues.
-
Memory Analysis: Shows actual memory usage vs. logical size, helping identify memory inefficiencies.
-
Sub-Matrix Detection: Clearly indicates if a matrix is a view, which affects memory management.
Print Matrix Elements¶
Description:
Prints the matrix elements in a formatted table. Optionally displays padding elements separated by a visual separator.
Parameters:
bool show_padding: Iftrue, displays padding values with a separator|. Iffalse, only shows actual matrix elements.
Returns:
void
Usage Insights:
-
Formatting: Elements are formatted with fixed width (12 characters) for alignment.
-
Padding Visualization: The
show_paddingoption helps understand memory layout and verify padding values. -
Large Matrices: For very large matrices, consider using
view_roi()to print specific regions.
CONSTRUCTORS & DESTRUCTOR¶
Memory Management
Constructors handle memory allocation automatically. The destructor safely frees memory only if it was internally allocated (not external buffers or views). Always check the data pointer after construction to ensure successful allocation.
RAII: The Biggest Advantage Over C
The C tiny_mat functions require you to pre-allocate all buffers. The C++ Mat class allocates and frees automatically — this eliminates an entire class of memory bugs (use-after-free, double-free, forgetting to free). For complex SHM workflows with multiple intermediate matrices, RAII saves 50-100 lines of manual cleanup code.
However, on ESP32-S3 with only 512 KB SRAM, uncontrolled RAII can cause heap fragmentation. For real-time loops, pre-allocate your matrices once and reuse them rather than constructing/destroying each iteration.
Memory Allocation¶
Description:
Internal function that allocates memory for the matrix according to the computed memory requirements. Sets ext_buff = false and allocates row * step float elements.
Parameters:
void
Returns:
void
Usage Insights:
-
Automatic Call: Called automatically by constructors. Rarely needs manual invocation.
-
Memory Calculation: Allocates
row * stepelements, which may include padding. -
Error Handling: If allocation fails,
dataremainsnullptr. Always checkdataafter construction.
Default Constructor¶
Description:
Default constructor creates a 1×1 zero matrix. This is useful for initialization and as a return value for error cases.
Mathematical Principle:
Creates the identity element for matrix operations in some contexts, though typically you'll want to specify dimensions.
Parameters:
void
Returns:
Mat - A 1×1 matrix with element = 0.
Constructor - Mat(int rows, int cols)¶
Description:
Constructor creates a matrix with specified dimensions. All elements are initialized to zero. This is the most commonly used constructor.
Parameters:
-
int rows: Number of rows (must be > 0). -
int cols: Number of columns (must be > 0).
Returns:
Mat - A rows×cols matrix with all elements initialized to 0.
Usage Insights:
-
Zero Initialization: All elements are set to zero using
memset, ensuring clean state. -
Memory Layout: Creates a contiguous memory layout with no padding (stride = cols).
-
Error Handling: If memory allocation fails,
datawill benullptr. Always verify allocation success.
Constructor - Mat(int rows, int cols, int step)¶
Description:
Constructor creates a matrix with specified dimensions and step. Useful when you need padding for memory alignment or DSP optimization.
Parameters:
-
int rows: Number of rows. -
int cols: Number of columns. -
int step: Total elements per row (must be ≥ cols). Padding = step - cols.
Returns:
Mat - A rows×cols matrix with step, all elements initialized to 0.
Usage Insights:
-
DSP Optimization: Some DSP libraries require aligned memory. Use stride to ensure proper alignment.
-
Memory Efficiency: Padding allows efficient vectorized operations on aligned boundaries.
-
Compatibility: Enables compatibility with external libraries that use strided memory layouts.
Constructor - Mat(float *data, int rows, int cols)¶
Description:
Constructor creates a matrix view over an external data buffer. The matrix does not own the memory; the caller is responsible for managing it. Useful for interfacing with existing data arrays.
Parameters:
-
float *data: Pointer to external data buffer (must remain valid for matrix lifetime). -
int rows: Number of rows. -
int cols: Number of columns.
Returns:
Mat - A matrix view with ext_buff = true.
Usage Insights:
-
Zero-Copy: No memory copy occurs; the matrix directly references external data.
-
Lifetime Management: The external buffer must remain valid while the matrix exists. The destructor will not free this memory.
-
Data Layout: Assumes row-major layout with no padding (stride = cols).
-
Use Cases:
-
Wrapping C arrays
-
Interfacing with other libraries
-
Avoiding unnecessary copies
Constructor - Mat(float *data, int rows, int cols, int step)¶
Description:
Constructor creates a matrix view over an external data buffer with specified step. Supports padded row-major layouts for DSP compatibility.
Parameters:
-
float *data: Pointer to external data buffer (must remain valid for matrix lifetime). -
int rows: Number of rows. -
int cols: Number of columns. -
int step: Total elements per row (must be ≥ cols).
Returns:
Mat - A matrix view with ext_buff = true and specified step.
Usage Insights:
-
Strided Layouts: Essential for working with DSP libraries that use strided memory layouts.
-
Memory Safety: Same lifetime requirements as the previous constructor - external buffer must remain valid.
-
Padding Support: Can handle buffers with padding between rows.
Copy Constructor - Mat(const Mat &src)¶
Description:
Copy constructor creates a new matrix from a source matrix. Uses intelligent copying: deep copy for regular matrices, shallow copy for sub-matrix views.
Copy Strategy:
-
Regular matrices: Deep copy - allocates new memory and copies all data
-
Sub-matrix views: Shallow copy - shares data with source (creates another view)
Parameters:
const Mat &src: Source matrix.
Returns:
Mat - A new matrix with copied or shared data depending on source type.
Usage Insights:
-
Automatic Selection: Automatically chooses deep or shallow copy based on source matrix type.
-
Memory Efficiency: Sub-matrix views are copied shallowly to avoid unnecessary memory allocation.
-
Independence: Deep copies are independent; modifications don't affect the source.
Destructor¶
Description:
Destructor safely releases allocated memory. Only frees memory if it was internally allocated (ext_buff = false). External buffers and views are not freed.
Memory Management:
-
Frees
databuffer ifext_buff = false -
Frees
tempbuffer if allocated -
Does nothing for external buffers or views
Parameters:
void
Returns:
void
Constructor and Destructor Rules
- Constructor functions must have the same name as the class and no return type
- C++ allows function overloading by changing parameter number/order
- The destructor is automatically called when the object goes out of scope
- Always check
data != nullptrafter construction to verify successful allocation
ELEMENT ACCESS¶
Matrix Indexing
The Mat class uses operator overloading to provide intuitive matrix element access. The operator() allows natural syntax like A(i, j) instead of A.data[i * step + j]. The implementation automatically handles step and padding.
Access Matrix Elements (Non-Const)¶
Description:
Accesses matrix elements with read-write capability. Returns a reference to the element, allowing both reading and modification.
Mathematical Principle:
Element at position (row, col) is accessed as data[row * step + col], where step accounts for padding.
Parameters:
-
int row: Row index (0-based, must be in range [0, row-1]). -
int col: Column index (0-based, must be in range [0, col-1]).
Returns:
float& - Reference to the matrix element, enabling modification.
Usage Insights:
-
Bounds Checking: No automatic bounds checking for performance. Ensure indices are valid.
-
Stride Handling: Automatically accounts for stride, so it works correctly with padded matrices.
-
Performance: Inline function with minimal overhead, suitable for tight loops.
-
Example:
A(2, 3) = 5.0f;sets element at row 2, column 3 to 5.0.
Access Matrix Elements (Const)¶
Description:
Accesses matrix elements in read-only mode. Returns a const reference, preventing modification. Used when the matrix is const.
Parameters:
-
int row: Row index (0-based). -
int col: Column index (0-based).
Returns:
const float& - Const reference to the matrix element (read-only).
Usage Insights:
-
Const Correctness: Enables proper const-correct code. Use this version in const member functions.
-
Safety: Prevents accidental modification of const matrices.
Operator Overloading
These functions overload the () operator, enabling natural matrix indexing syntax:
DATA MANIPULATION¶
Copy other matrix into this matrix as a sub-matrix¶
Description:
Copies the specified source matrix into this matrix as a sub-matrix starting from the specified row and column positions, not sharing the data buffer.
Parameters:
-
const Mat &src: Source matrix. -
int row_pos: Starting row position. -
int col_pos: Starting column position.
Returns:
tinyerrort - Error code (TINY_OK on success).
Copy header of other matrix to this matrix¶
Description:
Copies the header of the specified source matrix to this matrix, sharing the data buffer. All items copy the source matrix.
Parameters:
const Mat &src: Source matrix.
Returns:
tinyerrort - Error code.
Get a view (shallow copy) of sub-matrix (ROI) from this matrix¶
Description:
Gets a view (shallow copy) of the sub-matrix (ROI) from this matrix starting from the specified row and column positions.
Parameters:
-
int start_row: Starting row position. -
int start_col: Starting column position. -
int roi_rows: Number of rows in the ROI. -
int roi_cols: Number of columns in the ROI.
Warning
Unlike ESP-DSP, view_roi does not allow to setup stride as it will automatically calculate the stride based on the number of columns and paddings. The function will also refuse illegal requests, i.e., out of bound requests.
Get a view (shallow copy) of sub-matrix (ROI) from this matrix using ROI structure¶
Description:
Gets a view (shallow copy) of the sub-matrix (ROI) from this matrix using the specified ROI structure. This function will call the previous function in low level by passing the ROI structure to the parameters.
Parameters:
const Mat::ROI &roi: ROI structure.
Get a replica (deep copy) of sub-matrix (ROI)¶
Description:
Gets a replica (deep copy) of the sub-matrix (ROI) from this matrix starting from the specified row and column positions. This function will return a new matrix object that does not share the data buffer with the original matrix.
Parameters:
-
int start_row: Starting row position. -
int start_col: Starting column position. -
int height: Number of rows in the ROI. -
int width: Number of columns in the ROI.
Get a replica (deep copy) of sub-matrix (ROI) using ROI structure¶
Description:
Gets a replica (deep copy) of the sub-matrix (ROI) from this matrix using the specified ROI structure. This function will call the previous function in low level by passing the ROI structure to the parameters.
Parameters:
const Mat::ROI &roi: ROI structure.
Get a block of matrix¶
Description:
Gets a block of the matrix starting from the specified row and column positions.
Parameters:
-
int start_row: Starting row position. -
int start_col: Starting column position. -
int block_rows: Number of rows in the block. -
int block_cols: Number of columns in the block.
Differences between viewroi | copyroi | block
view_roi: Shallow copy of the sub-matrix (ROI) from this matrix.copy_roi: Deep copy of the sub-matrix (ROI) from this matrix. Rigid and faster.- Performance:
view_roiis \(O(1)\) (just copies a pointer + offset).copy_roiandblockare \(O(roi \\times cols)\). Useview_roiin hot paths.
Sub-Matrix Ownership
view_roiis ideal for temporary block access inside kernel functions — pass by value without cleanup concerns.copy_roiis needed when the parent matrix will be reused or destroyed before the sub-matrix computation finishes.blockis likecopy_roibut uses raw coordinates instead of anROIstruct.-
Memory safety: A
view_roi's destructor does NOT free memory. If you reassign the view, the original matrix data persists. -
block: Deep copy of the block from this matrix. Flexible and slower.
Swap rows¶
Description:
Swaps the specified rows in the matrix.
Parameters:
-
int row1: First row index. -
int row2: Second row index.
Returns:
void
Swap columns¶
Description:
Swaps the specified columns in the matrix.
Parameters:
-
int col1: First column index. -
int col2: Second column index.
Returns:
void
Clear matrix¶
Description:
Clears the matrix by setting all elements to zero.
Parameters:
void
Returns:
void