ulqr  0.1.0
LinearAlgebra

Data Structures

struct  Matrix
 Represents a matrix of double-precision data. More...
 

Functions

Matrix slap_NewMatrix (int rows, int cols)
 Allocate a new matrix on the heap. More...
 
Matrix slap_NewMatrixZeros (int rows, int cols)
 Allocate a new matrix on the heap, initialized with zeros. More...
 
int slap_MatrixSetConst (Matrix *mat, double val)
 Sets all of the elements in a matrix to a single value. More...
 
int slap_FreeMatrix (Matrix *mat)
 Free the data for a matrix. More...
 
int slap_MatrixNumElements (const Matrix *mat)
 Get the number of elements in a matrix, i.e. m * n. More...
 
int slap_MatrixGetLinearIndex (const Matrix *mat, int row, int col)
 Get the linear index for a given row and column in the matrix. More...
 
double * slap_MatrixGetElementTranspose (const Matrix *mat, int row, int col, bool istranposed)
 Get the element of a matrix or its transpose. More...
 
int slap_MatrixSetElement (Matrix *mat, int row, int col, double val)
 The a matrix element to a given value. More...
 
double * slap_MatrixGetElement (const Matrix *mat, int row, int col)
 Get the element of a matrix given row, column indices. More...
 
int slap_MatrixCopyTranspose (Matrix *dest, Matrix *src)
 Copy a matrix to another matrix, transposed. More...
 
int slap_MatrixCopy (Matrix *dest, Matrix *src)
 Copy a matrix to another matrix. More...
 
int slap_MatrixCopyFromArray (Matrix *mat, const double *data)
 Copy the data from an array into the matrix, columnwise. More...
 
int slap_MatrixScaleByConst (Matrix *mat, double alpha)
 Scale a matrix by a constant factor. More...
 
double slap_MatrixNormedDifference (Matrix *A, Matrix *B)
 Return the normed difference between 2 matrices of the same size. More...
 
int slap_MatrixFlatten (Matrix *mat)
 Flatten a 2D matrix to a column vector. More...
 
int slap_MatrixFlattenToRow (Matrix *mat)
 Flatten a 2D matrix to a row vector. More...
 
int slap_PrintMatrix (const Matrix *mat)
 Print the elements of a matrix to stdout. More...
 
int slap_PrintRowVector (const Matrix *mat)
 Print the entire matrix as a row vector. More...
 
int slap_SetMatrixSize (Matrix *mat, int rows, int cols)
 Set the dimensions of the matrix. More...
 

Detailed Description

Function Documentation

◆ slap_FreeMatrix()

int slap_FreeMatrix ( Matrix mat)

Free the data for a matrix.

Note this does NOT attempt to free the matrix object itself, only the data it wraps.

Parameters
mat
Postcondition
mat.data will be NULL.
Returns
0 if successful

◆ slap_MatrixCopy()

int slap_MatrixCopy ( Matrix dest,
Matrix src 
)

Copy a matrix to another matrix.

Parameters
desta matrix of size (m,n)
srca matrix of size (n,m)
Returns
0 if successful

◆ slap_MatrixCopyFromArray()

int slap_MatrixCopyFromArray ( Matrix mat,
const double *  data 
)

Copy the data from an array into the matrix, columnwise.

Parameters
matMatrix with nonzero size
dataData to be copied into the array. Must have length of at least mat.rows * mat.cols.
Returns
0 if successful

◆ slap_MatrixCopyTranspose()

int slap_MatrixCopyTranspose ( Matrix dest,
Matrix src 
)

Copy a matrix to another matrix, transposed.

Parameters
desta matrix of size (m,n)
srca matrix of size (n,m)
Returns
0 if successful

◆ slap_MatrixFlatten()

int slap_MatrixFlatten ( Matrix mat)

Flatten a 2D matrix to a column vector.

Changes the row and column data so that the matrix is now a column vector. The underlying data is unchanged.

Parameters
matMatrix to be flattened.
Returns
0 if successful

◆ slap_MatrixFlattenToRow()

int slap_MatrixFlattenToRow ( Matrix mat)

Flatten a 2D matrix to a row vector.

Changes the row and column data so that the matrix is now a row vector. The underlying data is unchanged.

Parameters
matMatrix to be flattened
Returns
0 if successful

◆ slap_MatrixGetElement()

double* slap_MatrixGetElement ( const Matrix mat,
int  row,
int  col 
)

Get the element of a matrix given row, column indices.

Parameters
matMatrix of nonzero size
rowRow index
colColumn index
Returns
A pointer to the element of the matrix. NULL for invalid input.

◆ slap_MatrixGetElementTranspose()

double* slap_MatrixGetElementTranspose ( const Matrix mat,
int  row,
int  col,
bool  istranposed 
)

Get the element of a matrix or its transpose.

If istransposed is false, then this method acts just like MatrixGetElement(). Otherwise, it is equalivalent to flipping the row and col arguments to MatrixGetElement().

Parameters
matMatrix with nonzero size and initialized data
rowRow index
colColumn index
istranposedAre the indicies for the transpose of A?
Returns
Pointer to the data at the given element.

◆ slap_MatrixGetLinearIndex()

int slap_MatrixGetLinearIndex ( const Matrix mat,
int  row,
int  col 
)

Get the linear index for a given row and column in the matrix.

Converts a cartesian index of row and column into a linear index for accessing an element of the underlying data.

Parameters
matMatrix with nonzero size and initialized data
rowRow index
colColumn index
Returns
Linear index corresponding to row and col. Returns -1 for a bad input.

◆ slap_MatrixNormedDifference()

double slap_MatrixNormedDifference ( Matrix A,
Matrix B 
)

Return the normed difference between 2 matrices of the same size.

Returns \( \sqrt{\sum_{i=0}^{m-1} \sum_{j=0}^{n-1} (A_{ij} - B_{ij})^2 } \)

Parameters
AA matrix of dimension (m,n)
BA matrix of dimension (m,n)
Returns

◆ slap_MatrixNumElements()

int slap_MatrixNumElements ( const Matrix mat)

Get the number of elements in a matrix, i.e. m * n.

Parameters
matAny matrix
Returns
Number of elements in the matrix

◆ slap_MatrixScaleByConst()

int slap_MatrixScaleByConst ( Matrix mat,
double  alpha 
)

Scale a matrix by a constant factor.

Parameters
matFully initialized matrix of non-zero size. Values will be modified.
alphascalar by which to multiply the matrix
Returns
0 if successsful

◆ slap_MatrixSetConst()

int slap_MatrixSetConst ( Matrix mat,
double  val 
)

Sets all of the elements in a matrix to a single value.

Parameters
matMatrix to be modified
valValue to which each element will be set
Returns
0 if successful

◆ slap_MatrixSetElement()

int slap_MatrixSetElement ( Matrix mat,
int  row,
int  col,
double  val 
)

The a matrix element to a given value.

Parameters
matMatrix with nonzero size and initialized data
rowRow index
colColumn index
valValue to which the element should be set
Returns
0 if successful

◆ slap_NewMatrix()

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 to FreeMatrix.

Parameters
rowsnumber of rows in the matrix
colsnumber of columns in the matrix
Returns
A new matrix

◆ slap_NewMatrixZeros()

Matrix slap_NewMatrixZeros ( int  rows,
int  cols 
)

Allocate a new matrix on the heap, initialized with zeros.

Data will not be initialized. Wrapper around a call to malloc. Must be followed by a call to FreeMatrix.

Parameters
rowsnumber of rows in the matrix
colsnumber of columns in the matrix
Returns
A new matrix

◆ slap_PrintMatrix()

int slap_PrintMatrix ( const Matrix mat)

Print the elements of a matrix to stdout.

Precision of the printing can be controlled by the global variable PRECISION.

Parameters
matMatrix to be printed
Returns
0 if successful

◆ slap_PrintRowVector()

int slap_PrintRowVector ( const Matrix mat)

Print the entire matrix as a row vector.

Same result as calling PrintMatrix() after a call to MatrixFlattenToRow().

Parameters
matMatrix to be printed
Returns
0 if successful

◆ slap_SetMatrixSize()

int slap_SetMatrixSize ( Matrix mat,
int  rows,
int  cols 
)

Set the dimensions of the matrix.

Note that this does not change the underlying data, only it's interpretation.

Parameters
matMatrix
rowsNew number of rows
colsNew number of columns
Returns
0 if successful