src/features/dense.c file

Functions

static auto vm_mat_at(const vm_mat* m, const int r, const int c) -> vm_float_t*
Returns a pointer to the element at the specified row and column in the matrix.
auto vm_mat_alloc(const int rows, const int cols) -> vm_mat
Allocates a new matrix with the specified dimensions.
void vm_mat_free(vm_mat* m)
Frees the memory allocated for a matrix and resets its state.
void vm_mat_zero(vm_mat* m)
Sets all elements of the matrix to zero.
auto vm_mat_get(const vm_mat* m, const int r, const int c) -> vm_float_t
Returns the value of the matrix element at the specified row and column.
void vm_mat_set(vm_mat* m, const int r, const int c, const vm_float_t v)
Sets the element at the specified row and column in the matrix to the given value.
auto vm_mat_copy(vm_mat* dst, const vm_mat* src) -> bool
Copies the contents of one matrix to another.

Function documentation

static vm_float_t* vm_mat_at(const vm_mat* m, const int r, const int c)

Returns a pointer to the element at the specified row and column in the matrix.

Parameters
m pointer to the matrix
r row index
c column index
Returns pointer to the matrix element at (r, c)

The matrix is stored in column-major order.

vm_mat vm_mat_alloc(const int rows, const int cols)

Allocates a new matrix with the specified dimensions.

Parameters
rows number of rows in the matrix
cols number of columns in the matrix
Returns allocated matrix or zero matrix on failure

Memory is allocated using calloc to initialize all elements to zero. If allocation fails or if dimensions are invalid, a matrix with zero dimensions and NULL data pointer is returned.

void vm_mat_free(vm_mat* m)

Frees the memory allocated for a matrix and resets its state.

Parameters
m pointer to the matrix to free

If the matrix pointer is NULL, the function returns immediately. The data pointer is freed (if allocated), then set to NULL and both dimensions are reset to zero.

void vm_mat_zero(vm_mat* m)

Sets all elements of the matrix to zero.

Parameters
m pointer to the matrix to be zeroed

If the matrix pointer or its data pointer is NULL, the function returns immediately without performing any operation.

vm_float_t vm_mat_get(const vm_mat* m, const int r, const int c)

Returns the value of the matrix element at the specified row and column.

Parameters
m pointer to the matrix
r zero-based row index
c zero-based column index
Returns value at (r, c) or 0.0f on invalid input

If the matrix pointer is NULL, the data pointer is NULL, or the row or column indices are out of bounds, the function returns 0.0f without accessing memory.

void vm_mat_set(vm_mat* m, const int r, const int c, const vm_float_t v)

Sets the element at the specified row and column in the matrix to the given value.

Parameters
m pointer to the matrix to modify
r row index
c column index
v value to set at the specified position

The function performs bounds checking and validates the matrix pointer and its data buffer before performing the assignment. If any validation fails, the function returns without modifying any data.

bool vm_mat_copy(vm_mat* dst, const vm_mat* src)

Copies the contents of one matrix to another.

Parameters
dst pointer to the destination matrix
src pointer to the source matrix
Returns true on success, false if either pointer is NULL, source data is NULL or memory allocation fails

If the destination matrix has incompatible dimensions or unallocated data it is freed and reallocated to match the source dimensions. The source matrix must be valid and have allocated data.