Robotat Linalg
Loading...
Searching...
No Matches
Matf32

Matrix Linear Algebra Operations. More...

Data Structures

struct  matf32_t
 Floating point matrix data structure. More...

Enumerations

enum  err_status_t {
  MATH_SUCCESS , MATH_ARGUMENT_ERROR , MATH_LENGTH_ERROR , MATH_SIZE_MISMATCH ,
  MATH_NANINF , MATH_SINGULAR , MATH_TEST_FAILURE , MATH_DECOMPOSITION_FAILURE
}
 Error status from matrix operations. More...
enum  pseudoinverse_methods_t { BASIC_PINV , SVD_PINV }
 Available methods to calculate the pseudoinverse of a matrix. More...

Functions

float dot (float *p_srca, float *p_srcb, uint16_t length)
 Find the dot product of two vectors, pointed by p_srca and p_srcb, of the same size. Assign to a float variable to store the result. Both vectores need to have the same length.
void eye (float *p_dst, uint16_t row, uint16_t column)
 Create an identity matrix array with size row x column.
void diag (float *p_src, float *p_dst, int row_d, int column_d)
 Creates a diagonal matrix array from the vector pointed by p_src and storing the result into a vector pointed by p_dst, with size row x column. Notice that the row of vector x needs to have the same length as the column of the matrix.
void zeros (float *p_dst, int row, int column)
 Turn into 0 all the elements of the matrix array pointed by p_dst (size row x column).
void ones (float *p_dst, int row, int column)
 Turn into 1 all the elements of the matrix array pointed by p_dst (size row x column).
void randn (float *p_dst, uint16_t length, float mu, float sigma)
 Creates a random array with values sampled from a normal (Gaussian) distribution.
float norm (float *p_src, int row, int column)
 Calculates the norm of a given matrix (euclidean for vectors, frobenius for matrices). Assign to a float variable to store the result of the norm.
void scale (float *p_src, uint16_t length, float scalar, float *p_dst)
 Multiplies a vector by a scalar, stores the result in a vector (can be the same as the input vector or a different vector).
void copy (float *p_src, float *p_dst, int row, int column)
 Size-aware matrix copy.
void print (float *p_src, uint16_t row, uint16_t column)
 Prints an array to the console (formatted).
float saturation (float input, float lower_limit, float upper_limit)
 Saturates the input, given upper and lower limits.
float sign (float number)
 Gets the input's sign by checking if the number is greater, equal to, or less than 0.
float mean (float *p_src, uint16_t length)
 Gets the mean of a given array.
float std_dev (float *p_src, uint16_t length)
 Gets the standard deviation of a given array. Named this way instead of "std" to avoid conflict with std namesapce in c++.
bool is_equal (float *p_a, float *p_b, uint16_t length)
 Checks if two arrays are equal, taking into account a margin or tolerance. For this, it uses the function is_equal_margin.
void zero_patch (float *p_a, uint16_t length)
 Changes NaN and Inf values to zero. Needed to avoid errors in cases where irrelevant elements end set as NaN or Inf.
void matf32_init (matf32_t *const instance, uint16_t num_rows, uint16_t num_cols, float *p_data)
 Constructor for the floating point matrix data structure.
void matf32_print (const matf32_t *p_src)
 Prints matrix data to console (formatted to print as a matrix).
void err_status_print (err_status_t err)
 Prints error status err_status_t to console. For example: prints MATH_SUCCESS for MATH_SUCCESS, MATH_SINGULAR for MATH_SINGULAR, etc.
void matf32_cond (const matf32_t *const p_src, float *p_cond)
 Calculates condition number of a matrix. This routine is useful for monitoring the behavior of matrices, as a higher condition number may lead to approximation issues or unexpected behavior in some operations.
err_status_t matf32_submatrix_copy (const matf32_t *const p_src, matf32_t *const p_dst, const uint16_t src_row, const uint16_t src_col, const uint16_t dst_row, const uint16_t dst_col, const uint16_t rows, const uint16_t cols)
 Copies a specified number of rows and columns from a matrix into another matrix. That means, it can either copy an entire matrix to another one, or just a submatrix of the former.
void matf32_set_row (matf32_t *const p_dst, uint16_t row, float val)
 Sets all elements in a matrix row to a specified value.
void matf32_set_col (matf32_t *const p_dst, uint16_t col, float val)
 Sets all elements in a matrix column to a specified value.
void matf32_eye (matf32_t *const p_dst)
 Sets a matrix structure to the identity matrix.
void matf32_diag (float *p_src, matf32_t *const p_dst)
 Sets a matrix structure to a diagonal matrix, created from a given vector.
void matf32_zeros (matf32_t *const p_dst)
 Sets a matrix structure to the zero matrix.
void matf32_ones (matf32_t *const p_dst)
 Sets a matrix structure to a ones matrix.
void matf32_randn (matf32_t *const p_dst, float mu, float sigma)
 Sets a matrix structure to a matrix with random entries, sampled from a normal distribution.
bool matf32_check_triangular_upper (const matf32_t *const p_mat)
 Checks if matrix is upper triangular.
bool matf32_check_triangular_lower (const matf32_t *const p_mat)
 Checks if matrix is lower triangular.
bool matf32_is_equal (const matf32_t *const p_mat_a, const matf32_t *const p_mat_b)
 Checks if two matrices are equal.
bool matf32_is_equal_scalar (const matf32_t *const p_mat, float scalar)
 Checks if a matrix values are equal to a scalar.
bool matf32_is_equal_less_scalar (const matf32_t *const p_mat, float scalar)
 Checks if a matrix values are equal or lesser than a scalar.
bool matf32_check_symmetric (const matf32_t *const p_mat)
 Checks if a matrix is symmetric.
bool matf32_check_hessenberg_upper (const matf32_t *const p_mat)
 Checks if a matrix is upper hessenberg.
bool matf32_check_hessenberg_lower (const matf32_t *const p_mat)
 Checks if a matrix is lower hessenberg.
bool matf32_check_symposdef (const matf32_t *const p_mat)
 Checks if a matrix is symmetric positive definite.
err_status_t matf32_add (const matf32_t *p_srca, const matf32_t *p_srcb, matf32_t *p_dst)
 Adds two matrices and saves the result in another matrix. The result can be saved either in a new matf32_t matrix or in one of the input matrices (overwriting its previous contents). Both must have the same dimensions.
err_status_t matf32_sub (const matf32_t *p_srca, const matf32_t *p_srcb, matf32_t *p_dst)
 Substracts two matrices and saves the result in another matrix. The result can be saved either in a new matf32_t matrix or in one of the input matrices (overwriting its previous contents). Both need to be of the same dimension.
err_status_t matf32_scale (const matf32_t *p_src, float scalar, matf32_t *p_dst)
 Multiplies a matrix by a scalar, element-wise, and saves the result in another matrix. The result can be saved either in a new matf32_t matrix or in one of the input matrices (overwriting its previous contents).
err_status_t matf32_trans (const matf32_t *p_src, matf32_t *p_dst)
 Transposes a matrix and saves the result in another matrix.
err_status_t matf32_mul (const matf32_t *p_srca, const matf32_t *p_srcb, matf32_t *p_dst)
 Multiplies two matrices. The number of columns of the first matrix must be the same as the number of rows of the second matrix.
err_status_t matf32_lup (const matf32_t *p_src, matf32_t *p_lu, uint16_t *pivot)
 Computes the LU decomposition (with partial pivoting) of a square matrix A, pointed by p_src, such that PA = LU.
err_status_t matf32_inv (const matf32_t *p_src, matf32_t *p_dst)
 Computes the inverse of a square, non-singular matrix.
err_status_t matf32_pinv (const matf32_t *const p_a, matf32_t *const p_dst, pseudoinverse_methods_t method)
 Calculates the pseudoinverse of a given matrix using the singular values of the SVD decomposition of A.
err_status_t matf32_dot (const matf32_t *const p_srca, const matf32_t *const p_srcb, float *const p_dst)
 Dot product between two vectors (wether row or column).
void matf32_vecposmul (const matf32_t *p_srcm, float *p_srcv, float *p_dst)
 Matrix-vector post multiplication, i.e. Ax. Assumes a column vector.
void matf32_vecpremul (const matf32_t *p_srcm, float *p_srcv, float *p_dst)
 vector-Matrix pre multiplication, i.e. xA. Assumes a row vector.
void matf32_vecmul_col_row (const float *const col_vec, const float *const row_vec, matf32_t *const p_dst)
 Vector-Vector Multiplication (the result is a matrix).
err_status_t matf32_exp (const matf32_t *const p_src, matf32_t *const p_dst, uint16_t exp)
 Calculates the power of a matrix to a given exponent.
err_status_t matf32_arr_add (const matf32_t **const p_matarray, uint16_t length, matf32_t *p_dst)
 Adds an array of matrices sequentially. The size of all matrices must be the same.
err_status_t matf32_arr_sub (const matf32_t **const p_matarray, uint16_t length, matf32_t *p_dst)
 Subtracts an array of matrices sequentially. The size of all matrices must be the same.
err_status_t matf32_arr_mul (const matf32_t **const p_matarray, uint16_t length, matf32_t *p_dst)
 Multiplies an array of matrices sequentially. The number of columns of any matrix must be the same as the number of rows of the next. Output matrix cannot be the same as one of the inputs.
err_status_t matf32_qr (const matf32_t *const p_a, matf32_t *const p_q, matf32_t *const p_r)
 Computes the Householder QR factorization of a matrix A, saving the Q and R factors in different matrices.
err_status_t matf32_lu (const matf32_t *p_a, matf32_t *const p_l, matf32_t *const p_u, uint16_t *p_index)
 Computes the LU decomposition of a square matrix A, pointed by p_a, such that PA = LU, and saves the L and U factors in different matrices, and the list of permutations corresponding to the matrix P are saved in an array to save memory.
err_status_t matf32_cholesky (const matf32_t *const p_a, matf32_t *const p_c)
 Calculates the Cholesky decomposition of a matrix A = LL', where L is a lowe triangular matrix.
err_status_t matf32_one_sided_jacobi (const matf32_t *const p_a, matf32_t *const p_v, uint16_t j, uint16_t k)
 Generates and applies a one-sided jacobi rotation to matrix A and saves the result as A = AV and V (an orthonormal matrix to accumulate the rotations applied).
err_status_t matf32_jacobi_svd (const matf32_t *const p_a, matf32_t *const p_u, matf32_t *const p_s, matf32_t *const p_v)
 Calculates the SVD of a matrix using using the one-sided Jacobi algorithm.

Detailed Description

Matrix Linear Algebra Operations.

Date
File created on: 2 Aug. 2025 By: Andrea Pineda to unify into a single file the previous linear algebra libraries: matf32_math.h, matf32_check.h, matf32_def.h and math_util.c previously developed by Daniel Pineda and Miguel Zea, and to add new functions.

Last modified: 18 Nov. 2026 By: Andrea Pineda


Data Structure Documentation

◆ matf32_t

struct matf32_t

Floating point matrix data structure.

Used for readability and compatibility with ARM's CMSIS-DSP library.

Data Fields
uint16_t num_cols

Number of columns of the matrix.

uint16_t num_rows

Number of rows of the matrix.

float * p_data

Points to the data of the matrix.

Enumeration Type Documentation

◆ err_status_t

Error status from matrix operations.

Defined this way for compatibility with ARM's CMSIS-DSP library.

Enumerator
MATH_SUCCESS 

Successfull mathematical operation.

MATH_ARGUMENT_ERROR 

Error in an input matf32_t instance.

MATH_LENGTH_ERROR 

Incorrect size for one of the dimensions of a matf32_t instance.

MATH_SIZE_MISMATCH 

matf32_t matrix dimensions don't match and thus cannot be operated as needed.

MATH_NANINF 

Result has NaN or Inf values.

MATH_SINGULAR 

Matrix is singular or close to singular and may impact negatively the precision of the results.

MATH_TEST_FAILURE 

Matrix operation failure.

MATH_DECOMPOSITION_FAILURE 

Matrix factorization or decomposition failed.

◆ pseudoinverse_methods_t

Available methods to calculate the pseudoinverse of a matrix.

Enumerator
BASIC_PINV 

Pseudoinverse calculated as pinv(A) = (A'A)^-1 * A'

SVD_PINV 

Pseudoinverse calculated with the SVD matrices: A = USV', so that pinv(A) = VSU'

Function Documentation

◆ copy()

void copy ( float * p_src,
float * p_dst,
int row,
int column )

Size-aware matrix copy.

Parameters
[in]p_srcPoints to matrix to copy from.
[in,out]p_dstPoints to matrix to copy to.
[in]rowNumber of rows of the matrix.
[in]columnNumber of columns of the matrix.
Returns
None.

◆ diag()

void diag ( float * p_src,
float * p_dst,
int row_d,
int column_d )

Creates a diagonal matrix array from the vector pointed by p_src and storing the result into a vector pointed by p_dst, with size row x column. Notice that the row of vector x needs to have the same length as the column of the matrix.

Parameters
[in]p_srcPoints to vector with diagonal entries.
[in,out]p_dstPoints to array to allocate the diagonal matrix.
[in]row_dNumber of required rows.
[in]column_dNumber of required columns.
Returns
None.

◆ dot()

float dot ( float * p_srca,
float * p_srcb,
uint16_t length )

Find the dot product of two vectors, pointed by p_srca and p_srcb, of the same size. Assign to a float variable to store the result. Both vectores need to have the same length.

Parameters
[in]p_srcaPoints to vector 1.
[in]p_srcbPoints to vector 2.
[in]lengthLength of vectors.
Returns
Float with computed value of the dot product between the input vectors.

◆ err_status_print()

void err_status_print ( err_status_t err)

Prints error status err_status_t to console. For example: prints MATH_SUCCESS for MATH_SUCCESS, MATH_SINGULAR for MATH_SINGULAR, etc.

Parameters
[in]errError stats value to print.
Returns
None.

◆ eye()

void eye ( float * p_dst,
uint16_t row,
uint16_t column )

Create an identity matrix array with size row x column.

Parameters
[in,out]p_dstPoints to array to allocate the identity matrix.
[in]rowNumber of required rows.
[in]columnNumber of required columns.
Returns
None.

◆ is_equal()

bool is_equal ( float * p_a,
float * p_b,
uint16_t length )

Checks if two arrays are equal, taking into account a margin or tolerance. For this, it uses the function is_equal_margin.

Parameters
[in]p_aPoints to first array.
[in]p_bPoints to second array.
[in]lengthArrays length.
Returns
Comparison result True : If arrays are equal within the tolerance, False : If arrays are not equal (the tolerance is surpassed at least once).

◆ matf32_add()

err_status_t matf32_add ( const matf32_t * p_srca,
const matf32_t * p_srcb,
matf32_t * p_dst )

Adds two matrices and saves the result in another matrix. The result can be saved either in a new matf32_t matrix or in one of the input matrices (overwriting its previous contents). Both must have the same dimensions.

Parameters
[in]p_srcaPoints to first input matrix structure.
[in]p_srcbPoints to second input matrix structure.
[in,out]p_dstPoints to output matrix structure.
Returns
Execution status MATH_SUCCESS : Operation successful, MATH_SIZE_MISMATCH : Matrix size check failed.

◆ matf32_arr_add()

err_status_t matf32_arr_add ( const matf32_t **const p_matarray,
uint16_t length,
matf32_t * p_dst )

Adds an array of matrices sequentially. The size of all matrices must be the same.

Parameters
[in]p_matarrayPoints to the matrix array.
[in]lengthNumber of matrices in the array.
[in,out]p_dstPoints to output matrix structure.
Returns
Execution status MATH_SUCCESS : Operation successful, MATH_SIZE_MISMATCH : Matrix size check failed.
Here is the call graph for this function:

◆ matf32_arr_mul()

err_status_t matf32_arr_mul ( const matf32_t **const p_matarray,
uint16_t length,
matf32_t * p_dst )

Multiplies an array of matrices sequentially. The number of columns of any matrix must be the same as the number of rows of the next. Output matrix cannot be the same as one of the inputs.

Parameters
[in]p_matarrayPoints to the matrix array.
[in]lengthNumber of matrices in the array.
[in,out]p_dstPoints to output matrix structure.
Returns
Execution status MATH_SUCCESS : Operation successful, MATH_SIZE_MISMATCH : Matrix size check failed.
Here is the call graph for this function:

◆ matf32_arr_sub()

err_status_t matf32_arr_sub ( const matf32_t **const p_matarray,
uint16_t length,
matf32_t * p_dst )

Subtracts an array of matrices sequentially. The size of all matrices must be the same.

Parameters
[in]p_matarrayPoints to the matrix array.
[in]lengthNumber of matrices in the array.
[in,out]p_dstPoints to output matrix structure.
Returns
Execution status MATH_SUCCESS : Operation successful, MATH_SIZE_MISMATCH : Matrix size check failed.
Here is the call graph for this function:

◆ matf32_check_hessenberg_lower()

bool matf32_check_hessenberg_lower ( const matf32_t *const p_mat)

Checks if a matrix is lower hessenberg.

Parameters
[in]p_matPoints to matrix to check.
Returns
Execution status true : Matrices is lower hessenberg, false : Matrices is not lower hessenberg.

◆ matf32_check_hessenberg_upper()

bool matf32_check_hessenberg_upper ( const matf32_t *const p_mat)

Checks if a matrix is upper hessenberg.

Parameters
[in]p_matPoints to matrix to check.
Returns
Execution status true : Matrices is upper hessenberg, false : Matrices is not upper hessenberg.

◆ matf32_check_symmetric()

bool matf32_check_symmetric ( const matf32_t *const p_mat)

Checks if a matrix is symmetric.

Parameters
[in]p_matPoints to matrix to check.
Returns
Execution status true : Matrix is symmetrical, false : Matrix is not symmetrical.

◆ matf32_check_symposdef()

bool matf32_check_symposdef ( const matf32_t *const p_mat)

Checks if a matrix is symmetric positive definite.

Parameters
[in]p_matPoints to matrix to check.
Returns
Execution status true : Matrix is symmetric positive definite, false : Matrix is not symmetric positive definite (cannot perform cholesky).
Here is the call graph for this function:

◆ matf32_check_triangular_lower()

bool matf32_check_triangular_lower ( const matf32_t *const p_mat)

Checks if matrix is lower triangular.

Parameters
[in]p_matPoints to the matrix to test.
Returns
Execution status true : Matrix is lower triangular, false : Matrix is not lower triangular.

◆ matf32_check_triangular_upper()

bool matf32_check_triangular_upper ( const matf32_t *const p_mat)

Checks if matrix is upper triangular.

Parameters
[in]p_matPoints to the matrix to test.
Returns
Execution status true : Matrix is upper triangular, false : Matrix is not upper triangular.

◆ matf32_cholesky()

err_status_t matf32_cholesky ( const matf32_t *const p_a,
matf32_t *const p_c )

Calculates the Cholesky decomposition of a matrix A = LL', where L is a lowe triangular matrix.

Below is an example of how this factorization looks like:

\[\begin{aligned} \textbf{A}= \textbf{LL}^\top = \begin{bmatrix} a_{11} & a_{12} & a_{13}\\ a_{21} & a_{22} & a_{23}\\ a_{31} & a_{32} & a_{33} \end{bmatrix} = \begin{bmatrix} m_{11} & 0 & 0\\ m_{21} & m_{22} & 0\\ m_{31} & m_{32} & m_{33} \end{bmatrix} \begin{bmatrix} m_{11} & m_{21} & m_{31}\\ 0 & m_{22} & m_{32}\\ 0 & 0 & m_{33} \end{bmatrix} \end{aligned} \]

Warning
This routine saves the Cholesky Factor matrix L in upper triangular form, that is L', due to the specific algorithm implemented. Depending on the application, it may be required as lower triangular, for which it can be transposed and used without issue. In case of needing both the upper and lower triangular forms, either transpose as needed for each step of the operation or use a temporal matrix to save the other form.
Parameters
[in]p_aPoints to matrix to factorize.
[in,out]p_cPoints to lower triangular factorized matrix.
Returns
Execution status MATH_SUCCESS : Operation successful, MATH_SIZE_MISMATCH : The size of matrix p_a or p_c are not equal, MATH_ARGUMENT_ERROR : The input matrix is either not square or not symmetric.
Here is the call graph for this function:

◆ matf32_cond()

void matf32_cond ( const matf32_t *const p_src,
float * p_cond )

Calculates condition number of a matrix. This routine is useful for monitoring the behavior of matrices, as a higher condition number may lead to approximation issues or unexpected behavior in some operations.

Parameters
[in]p_srcPoints to matrix from which to calculate the condition number.
[in]p_condPoints to float to store condition number.
Returns
None.
Here is the call graph for this function:

◆ matf32_diag()

void matf32_diag ( float * p_src,
matf32_t *const p_dst )

Sets a matrix structure to a diagonal matrix, created from a given vector.

Parameters
[in]p_srcPoints to vector with the diagonal entries to be used.
[in,out]p_dstPoints to matrix to allocate the diagonal matrix.
Returns
None.
Here is the call graph for this function:

◆ matf32_dot()

err_status_t matf32_dot ( const matf32_t *const p_srca,
const matf32_t *const p_srcb,
float *const p_dst )

Dot product between two vectors (wether row or column).

Parameters
[in]p_srcaPoints to first input vector.
[in]p_srcbPoints to second input vector.
[in]p_dstPoints to scalar result.
Returns
Execution status. MATH_SUCCESS : Completed operation without issue, MATH_SIZE_MISMATCH : Input vectors have different amount of elements.
Here is the call graph for this function:

◆ matf32_exp()

err_status_t matf32_exp ( const matf32_t *const p_src,
matf32_t *const p_dst,
uint16_t exp )

Calculates the power of a matrix to a given exponent.

Warning
This routine currently works exclusively with square matrices. So error is returned if a rectangular matrix is introduced as argument.
Parameters
[in]p_srcPoints to the source matrix
[in,out]p_dstPoints to the output matrix
[in]expIndicates the exponent to be used
Returns
Execution status. MATH_SUCCESS : Completed operation without issue, MATH_ARGUMENT_ERROR : Input matrix is not square.
Here is the call graph for this function:

◆ matf32_eye()

void matf32_eye ( matf32_t *const p_dst)

Sets a matrix structure to the identity matrix.

Parameters
[in,out]p_dstPoints to matrix to allocate the identity matrix.
Returns
None.
Here is the call graph for this function:

◆ matf32_init()

void matf32_init ( matf32_t *const instance,
uint16_t num_rows,
uint16_t num_cols,
float * p_data )

Constructor for the floating point matrix data structure.

Parameters
[in,out]instancePoints to an instance of the floating-point matrix structure.
[in]num_rowsNumber of rows in the matrix.
[in]num_colsNumber of columns in the matrix.
[in]p_dataPoints to the matrix data array.
Returns
None.

◆ matf32_inv()

err_status_t matf32_inv ( const matf32_t * p_src,
matf32_t * p_dst )

Computes the inverse of a square, non-singular matrix.

NOTE: use only as a last resort, solving the linear system Ax = b should always be the first choice. This routine is also very numerically sensitive, as the matrix are defined with 32-bit floating point data.

Parameters
[in]p_srcPoints to input matrix.
[in,out]p_dstPoints to output matrix.
Returns
Execution status MATH_SUCCESS : Operation successful, MATH_SIZE_MISMATCH : Matrix size check failed, MATH_SINGULAR : Matrix is singular.
Here is the call graph for this function:

◆ matf32_is_equal()

bool matf32_is_equal ( const matf32_t *const p_mat_a,
const matf32_t *const p_mat_b )

Checks if two matrices are equal.

Parameters
[in]p_mat_aPoints to first matrix to compare.
[in]p_mat_bPoints to second matrix to compare.
Returns
Execution status true : Matrices are equal, false : Matrices are not equal.
Here is the call graph for this function:

◆ matf32_is_equal_less_scalar()

bool matf32_is_equal_less_scalar ( const matf32_t *const p_mat,
float scalar )

Checks if a matrix values are equal or lesser than a scalar.

Parameters
[in]p_matPoints to the matrix to compare.
[in]scalarValue to compare against.
Returns
Execution status true : Matrix values are equal or less than scalar, false : Matrix values are not equal or less than scalar.

◆ matf32_is_equal_scalar()

bool matf32_is_equal_scalar ( const matf32_t *const p_mat,
float scalar )

Checks if a matrix values are equal to a scalar.

Parameters
[in]p_matPoints to the matrix to compare.
[in]scalarValue to compare against.
Returns
Execution status true : Matrix values are equal to scalar, false : Matrix values are not equal to scalar.

◆ matf32_jacobi_svd()

err_status_t matf32_jacobi_svd ( const matf32_t *const p_a,
matf32_t *const p_u,
matf32_t *const p_s,
matf32_t *const p_v )

Calculates the SVD of a matrix using using the one-sided Jacobi algorithm.

Below is an example of how this factorization looks like:

\[\begin{aligned} \textbf{A} = \textbf{U}\boldsymbol{\Sigma}\textbf{V}^\top = \begin{bmatrix} u_{11} & u_{12} & u_{13}\\ u_{21} & u_{22} & u_{23}\\ u_{31} & u_{32} & u_{33} \end{bmatrix} \begin{bmatrix} \sigma_{11} & 0 & 0 \\ 0 & \sigma_{22} & 0 \\ 0 & 0 & \sigma_{33} \end{bmatrix} \begin{bmatrix} v_{11} & v_{12} & v_{13}\\ v_{21} & v_{22} & v_{23}\\ v_{31} & v_{32} & v_{33} \end{bmatrix} \end{aligned} \]

Warning
This routine OVERWRITES matrix A and replaces it with AV. If the original A is needed later, it's recommended to pass as argument to this function a copy of A.
Parameters
[in]p_aPoints to input matrix (square matrix)
[in,out]p_uPoints to matrix to save the orthogonal matrix U
[in,out]p_sPoints to matrix to save the singular values matrix S
[in,out]p_vPoints to matrix to save the orthogonal matrix V
Returns
Execution status
Here is the call graph for this function:

◆ matf32_lu()

err_status_t matf32_lu ( const matf32_t * p_a,
matf32_t *const p_l,
matf32_t *const p_u,
uint16_t * p_index )

Computes the LU decomposition of a square matrix A, pointed by p_a, such that PA = LU, and saves the L and U factors in different matrices, and the list of permutations corresponding to the matrix P are saved in an array to save memory.

Below is an example of how this factorization looks like:

\[\begin{aligned} \textbf{A}= \textbf{LU} = \begin{bmatrix} a_{11} & a_{12} & a_{13}\\ a_{21} & a_{22} & a_{23}\\ a_{31} & a_{32} & a_{33} \end{bmatrix} = \begin{bmatrix} 1 & 0 & 0\\ m_{21} & 1 & 0\\ m_{31} & m_{32} & 1 \end{bmatrix} \begin{bmatrix} u_{11} & u_{12} & u_{13}\\ 0 & u_{22} & u_{23}\\ 0 & 0 & u_{33} \end{bmatrix} \end{aligned} \]

Warning
In this algorithm, A will not be equal to LU (that is, A != LU), instead: PA = LU. This is because of the pivoting or permutations implemented. If you want A = LU, the same permutations should be applied to LU (such as it's done in the linsolve library to solve Ax = b through LU). The permutations are saved in an array (p_index) so that: p_index[0] means: Row 0 must be switched with the row number saved in p_index[0]. p_index[1] means: Row 1 must be switched with the row number saved in p_index[1]. and so on...
Parameters
[in]p_aPoints to square matrix to decompose.
[in,out]p_lPoints to the lower result of the decomposition.
[in,out]p_uPoints to the upper of the decomposition.
[in,out]p_indexSaved indexes for permutation
Returns
Execution status MATH_SUCCESS : Operation successful, MATH_SIZE_MISMATCH : Matrix size check failed.
Here is the call graph for this function:

◆ matf32_lup()

err_status_t matf32_lup ( const matf32_t * p_src,
matf32_t * p_lu,
uint16_t * pivot )

Computes the LU decomposition (with partial pivoting) of a square matrix A, pointed by p_src, such that PA = LU.

Parameters
[in]p_srcPoints to square matrix to decompose.
[in,out]p_luPoints to the result of the decomposition.
[in,out]pivotPoints to the pivot vector.
Returns
Execution status MATH_SUCCESS : Operation successful, MATH_SIZE_MISMATCH : Matrix size check failed, MATH_SINGULAR : Matrix is singular.

◆ matf32_mul()

err_status_t matf32_mul ( const matf32_t * p_srca,
const matf32_t * p_srcb,
matf32_t * p_dst )

Multiplies two matrices. The number of columns of the first matrix must be the same as the number of rows of the second matrix.

Warning
The result CANNOT be stored in any of the input matrices. Instead, a matrix different to both input matrices must be used to save the result. This is to avoid losing data during the multiplication.
Parameters
[in]p_srcaPoints to first input matrix structure.
[in]p_srcbPoints to second input matrix structure.
[in,out]p_dstPoints to output matrix structure.
Returns
Execution status MATH_SUCCESS : Operation successful, MATH_SIZE_MISMATCH : Input matrices cannot be multiplied due to rows and cols number mismatch, MATH_ARGUMENT_ERROR : One of the input matrices is set as the output, which is not allowed.

◆ matf32_one_sided_jacobi()

err_status_t matf32_one_sided_jacobi ( const matf32_t *const p_a,
matf32_t *const p_v,
uint16_t j,
uint16_t k )

Generates and applies a one-sided jacobi rotation to matrix A and saves the result as A = AV and V (an orthonormal matrix to accumulate the rotations applied).

Warning
This routine OVERWRITES matrix A and replaces it with AV. If the original A is needed later, it's recommended to pass as argument to this function a copy of A.
Parameters
[in]p_aPoints to input matrix (square matrix)
[in,out]p_vPoints to matrix to save the orthonormal matrix V
[in]jIndex j for the rotation
[in]kIndex k for the rotation
Returns
Execution status
Here is the call graph for this function:

◆ matf32_ones()

void matf32_ones ( matf32_t *const p_dst)

Sets a matrix structure to a ones matrix.

Parameters
[in,out]p_dstPoints to matrix to allocate the ones matrix.
Returns
None.
Here is the call graph for this function:

◆ matf32_pinv()

err_status_t matf32_pinv ( const matf32_t *const p_a,
matf32_t *const p_dst,
pseudoinverse_methods_t method )

Calculates the pseudoinverse of a given matrix using the singular values of the SVD decomposition of A.

Parameters
[in]p_aPoints to input matrix to work with
[in,out]p_dstPoints to output matrix in which to save the pseudoinverse
[in]methodEnum constant to choose method to calculate the pseudoinverse
Returns
Execution status. MATH_SUCCESS : Completed operation without issue.
Here is the call graph for this function:

◆ matf32_print()

void matf32_print ( const matf32_t * p_src)

Prints matrix data to console (formatted to print as a matrix).

Parameters
[in]p_srcPoints to matrix to be printed.
Returns
None.

◆ matf32_qr()

err_status_t matf32_qr ( const matf32_t *const p_a,
matf32_t *const p_q,
matf32_t *const p_r )

Computes the Householder QR factorization of a matrix A, saving the Q and R factors in different matrices.

Below is an example of how this factorization looks like:

\[\begin{aligned} \textbf{A}= \textbf{QR} = \begin{bmatrix} a_{11} & a_{12} & a_{13}\\ a_{21} & a_{22} & a_{23}\\ a_{31} & a_{32} & a_{33} \end{bmatrix} = \begin{bmatrix} q_{11} & q_{12} & q_{13}\\ q_{21} & q_{22} & q_{23}\\ q_{31} & q_{32} & q_{33} \end{bmatrix} \begin{bmatrix} r_{11} & r_{21} & r_{31}\\ 0 & r_{22} & r_{32}\\ 0 & 0 & r_{33} \end{bmatrix} \end{aligned} \]

Parameters
[in]p_aPoints to the matrix to be factorized/decomposed into a QR factorization.
[in,out]p_qPoints to a matrix where the Q factor will be saved.
[in,out]p_rPoints to a matrix where the R factor will be saved.
Returns
Execution status. MATH_SUCCESS : Operation completed without issue.
Here is the call graph for this function:

◆ matf32_randn()

void matf32_randn ( matf32_t *const p_dst,
float mu,
float sigma )

Sets a matrix structure to a matrix with random entries, sampled from a normal distribution.

Parameters
[in,out]p_dstPoints to random matrix.
[in]muMean.
[in]sigmaStandard deviation.
Returns
None.
Here is the call graph for this function:

◆ matf32_scale()

err_status_t matf32_scale ( const matf32_t * p_src,
float scalar,
matf32_t * p_dst )

Multiplies a matrix by a scalar, element-wise, and saves the result in another matrix. The result can be saved either in a new matf32_t matrix or in one of the input matrices (overwriting its previous contents).

Parameters
[in]p_srcPoints to input matrix.
[in]scalarScaling factor.
[in,out]p_dstPoints to output matrix.
Returns
Execution status MATH_SUCCESS : Operation successful, MATH_SIZE_MISMATCH : Matrix size check failed.
Here is the call graph for this function:

◆ matf32_set_col()

void matf32_set_col ( matf32_t *const p_dst,
uint16_t col,
float val )

Sets all elements in a matrix column to a specified value.

Parameters
[in,out]p_dstPoints to matrix to edit.
[in]colNumber of column to set as a value.
[in]valValue to set the row to.
Returns
None.

◆ matf32_set_row()

void matf32_set_row ( matf32_t *const p_dst,
uint16_t row,
float val )

Sets all elements in a matrix row to a specified value.

Parameters
[in,out]p_dstPoints to matrix to edit.
[in]rowNumber of row to set as a value.
[in]valValue to set the row to.
Returns
None.

◆ matf32_sub()

err_status_t matf32_sub ( const matf32_t * p_srca,
const matf32_t * p_srcb,
matf32_t * p_dst )

Substracts two matrices and saves the result in another matrix. The result can be saved either in a new matf32_t matrix or in one of the input matrices (overwriting its previous contents). Both need to be of the same dimension.

Parameters
[in]p_srcaPoints to first input matrix structure.
[in]p_srcbPoints to second input matrix structure.
[in,out]p_dstPoints to output matrix structure.
Returns
Execution status MATH_SUCCESS : Operation successful, MATH_SIZE_MISMATCH : Matrix size check failed.

◆ matf32_submatrix_copy()

err_status_t matf32_submatrix_copy ( const matf32_t *const p_src,
matf32_t *const p_dst,
const uint16_t src_row,
const uint16_t src_col,
const uint16_t dst_row,
const uint16_t dst_col,
const uint16_t rows,
const uint16_t cols )

Copies a specified number of rows and columns from a matrix into another matrix. That means, it can either copy an entire matrix to another one, or just a submatrix of the former.

Warning
This routine uses zero-based indexing, that means, the origin and destination rows and columns are introduced with values starting in zero (instead of 1 as in mathematical indexing). Do not confuse with the number of rows and columns to copy in total, as that is the actual amount of rows and columns to be copied.
Parameters
[in]p_srcPoints to the origin matrix from which to copy data.
[in,out]p_dstPoints to the destination matrix on which to store the copied data.
[in]src_rowOrigin row from which to start copying data from the origin matrix (zero-indexed).
[in]src_colOrigin column from which to start copying data from the origin matrix (zero-indexed).
[in]dst_rowRow in which to start saving the copied data in the destination matrix (zero-indexed).
[in]dst_colColumn in which to start saving the copied data in the destination matrix (zero-indexed).
[in]rowsNumber of rows to copy in total (starting to count from the origin row and until reaching this rows value).
[in]colsNumber of columns to copy in total (starting to count from the origin column and until reaching this cols value).
Returns
Execution status. MATH_SIZE_MISMATCH : If the number of rows or columns to be copied surpasses the available rows or columns in any of the matrices, MATH_SUCCESS : If the operation is completed without issue.

◆ matf32_trans()

err_status_t matf32_trans ( const matf32_t * p_src,
matf32_t * p_dst )

Transposes a matrix and saves the result in another matrix.

Warning
If the matrix is square, the transpose can be saved in the input matrix (replacing its previous content) without issue. Otherwise (if the matrix is rectangular), it either has to be reshaped first, or a new matrix be used, to match the transposed dimensions.
Parameters
[in]p_srcPoints to input matrix.
[in,out]p_dstPoints to output matrix.
Returns
Execution status MATH_SUCCESS : Operation successful, MATH_SIZE_MISMATCH : Matrix size check failed.
Here is the call graph for this function:

◆ matf32_vecmul_col_row()

void matf32_vecmul_col_row ( const float *const col_vec,
const float *const row_vec,
matf32_t *const p_dst )

Vector-Vector Multiplication (the result is a matrix).

Parameters
[in]col_vecPoints to a float array representing the column vector
[in]row_vecPoints to a float array representing the row vector
[in,out]p_dstPoints to matrix in which to save the result
Returns
None.

◆ matf32_vecposmul()

void matf32_vecposmul ( const matf32_t * p_srcm,
float * p_srcv,
float * p_dst )

Matrix-vector post multiplication, i.e. Ax. Assumes a column vector.

Parameters
[in]p_srcmPoints to input matrix.
[in]p_srcvPoints to input vector.
[in,out]p_dstPoints to result vector.
Returns
None.
Here is the call graph for this function:

◆ matf32_vecpremul()

void matf32_vecpremul ( const matf32_t * p_srcm,
float * p_srcv,
float * p_dst )

vector-Matrix pre multiplication, i.e. xA. Assumes a row vector.

Parameters
[in]p_srcmPoints to input matrix.
[in]p_srcvPoints to input vector.
[in,out]p_dstPoints to result vector.
Returns
None.
Here is the call graph for this function:

◆ matf32_zeros()

void matf32_zeros ( matf32_t *const p_dst)

Sets a matrix structure to the zero matrix.

Parameters
[in,out]p_dstPoints to matrix to allocate the zero matrix.
Returns
None.
Here is the call graph for this function:

◆ mean()

float mean ( float * p_src,
uint16_t length )

Gets the mean of a given array.

Parameters
[in]p_srcPoints to input array.
[in]lengthArray length.
Returns
Float containing the computed mean of the array.

◆ norm()

float norm ( float * p_src,
int row,
int column )

Calculates the norm of a given matrix (euclidean for vectors, frobenius for matrices). Assign to a float variable to store the result of the norm.

Parameters
[in,out]p_srcPoints to matrix array.
[in]rowNumber or rows.
[in]columnNumber of columns.
Returns
Float with computed value of the norm of the matrix/vector.

◆ ones()

void ones ( float * p_dst,
int row,
int column )

Turn into 1 all the elements of the matrix array pointed by p_dst (size row x column).

Parameters
[in,out]p_dstPoints to ones matrix array.
[in]rowNumber or rows.
[in]columnNumber of columns.
Returns
None.

◆ print()

void print ( float * p_src,
uint16_t row,
uint16_t column )

Prints an array to the console (formatted).

Parameters
[in]p_srcPoints to array to be printed.
[in]rowNumber of rows.
[in]columnNumber of columns.
Returns
None.

◆ randn()

void randn ( float * p_dst,
uint16_t length,
float mu,
float sigma )

Creates a random array with values sampled from a normal (Gaussian) distribution.

Parameters
[in,out]p_dstPoints to random vector to create.
[in]lengthVector length.
[in]muMean.
[in]sigmaStandard deviation.
Returns
None.

◆ saturation()

float saturation ( float input,
float lower_limit,
float upper_limit )

Saturates the input, given upper and lower limits.

Parameters
[in]inputInput value.
[in]lower_limitLower saturation threshold.
[in]upper_limitUpper saturation threshold.
Returns
Float with the saturated output upper_limit : if input > upper_limit, lower_limit : if input < lower_limit, input : if none of the former cases apply.

◆ scale()

void scale ( float * p_src,
uint16_t length,
float scalar,
float * p_dst )

Multiplies a vector by a scalar, stores the result in a vector (can be the same as the input vector or a different vector).

Parameters
[in]p_srcVector to scale.
[in]lengthVector size.
[in]scalarFactor to scale the vector.
[out]p_dstScaled vector (output vector).
Returns
None.

◆ sign()

float sign ( float number)

Gets the input's sign by checking if the number is greater, equal to, or less than 0.

Parameters
[in]numberInput value.
Returns
Sign of input, indicated by: 1 : if the number is greater than 0 (positive sign), -1 : if the number is below 0 (negative sign), 0 : if the number is equal to 0 (no sign as it's 0).

◆ std_dev()

float std_dev ( float * p_src,
uint16_t length )

Gets the standard deviation of a given array. Named this way instead of "std" to avoid conflict with std namesapce in c++.

Parameters
[in]p_srcPoints to input array.
[in]lengthArray length.
Returns
Float with the standard deviation of the array.
Here is the call graph for this function:

◆ zero_patch()

void zero_patch ( float * p_a,
uint16_t length )

Changes NaN and Inf values to zero. Needed to avoid errors in cases where irrelevant elements end set as NaN or Inf.

Parameters
[in]p_aPoints to array.
[in]lengthArray length.
Returns
None.

◆ zeros()

void zeros ( float * p_dst,
int row,
int column )

Turn into 0 all the elements of the matrix array pointed by p_dst (size row x column).

Parameters
[in,out]p_dstPoints to zero matrix array.
[in]rowNumber or rows.
[in]columnNumber of columns.
Returns
None.