Defines | |
#define | mat_IDX(m, i, j) array_INDEX2( m, double, i, j ) |
index matrix at i,j. | |
#define | matrix_CHECK(flag, m) |
is m a matrix? | |
#define | matrix_CHECKSQR(flag, m) |
is m a square matrix? | |
#define | vec_IDX(v, i) array_INDEX1( v, double, i ) |
index vector at i. | |
#define | vector_CHECK(flag, v) |
is v a vector? | |
Functions | |
Array * | matrix_get_col (Array *m, int col) |
get a column of a matrix as vector. | |
Array * | matrix_get_row (Array *m, int row, bool alloc) |
get a row of a matrix as vector. | |
Array * | matrix_mean (Array *a, int dim) |
calculate the mean of matrix a along dimension dim. | |
Array * | matrix_mult (const Array *m1, const Array *m2) |
Matrix Multiplication. | |
Array * | matrix_pca (Array *X, Array **var, bool alloc) |
Principal Components analysis. | |
gsl_matrix * | matrix_to_gsl (Array *in, bool alloc) |
convert 2D- Array struct to gsl_matrix. | |
Array * | matrix_transpose (Array *m, bool alloc) |
Matrix transpose. |
STATUS: work in progress Linear Algebra.
#define mat_IDX | ( | m, | ||
i, | ||||
j | ||||
) | array_INDEX2( m, double, i, j ) |
#define matrix_CHECK | ( | flag, | ||
m | ||||
) |
if( (m)==NULL ){ \ errprintf("got NULL as matrix\n"); \ flag=FALSE; \ } else if(!( (m)->ndim==2 && (m)->dtype==DOUBLE )){ \ char *dts=""; \ array_DTYPESTRING( dts, m->dtype ); \ errprintf("not a matrix, ndim=%i, dtype=%s\n", m->ndim, dts ); \ flag=FALSE; \ } else { flag=TRUE; }
is m a matrix?
Usage:
bool ismatrix; matrix_CHECK( ismatrix, X ); if( !ismatrix ) return NULL;
flag | (output) (bool) set by macro | |
m | (input) Array* to check |
#define matrix_CHECKSQR | ( | flag, | ||
m | ||||
) |
if(!( (m)->ndim==2 && (m)->dtype==DOUBLE )){ \ char *dts=""; \ array_DTYPESTRING( dts, (m)->dtype ); \ errprintf("not a matrix, ndim=%i, dtype=%s\n", (m)->ndim, dts ); \ flag=FALSE; \ } else if( (m)->size[0]!=(m)->size[1] ) { \ errprintf("Matrix is not square, got (%i,%i)\n",(m)->size[0],(m)->size[1]); \ flag=FALSE; \ } else { flag=TRUE; }
is m a square matrix?
Usage:
bool ismatrix; matrix_CHECKSQR( ismatrix, X ); if( !ismatrix ) return NULL;
flag | (output) (bool) set by macro | |
m | (input) Array* to check |
#define vec_IDX | ( | v, | ||
i | ||||
) | array_INDEX1( v, double, i ) |
#define vector_CHECK | ( | flag, | ||
v | ||||
) |
if( (v)==NULL ){ \ errprintf("got NULL as vector\n"); \ flag=FALSE; \ } else if(!( (v)->ndim==1 && (v)->dtype==DOUBLE ) ){ \ char *dts=""; \ array_DTYPESTRING( dts, v->dtype ); \ errprintf("not a vector, ndim=%i, dtype=%s\n", v->ndim, dts ); \ flag=FALSE; \ } else { flag=TRUE; }
is v a vector?
Usage:
bool isvector; vector_CHECK( isvector, X ); if( !isvector ) return NULL;
flag | (output) (bool) set by macro | |
v | (input) Array* to check |
get a column of a matrix as vector.
m | the matrix | |
col | index to the column |
get a row of a matrix as vector.
m | the matrix | |
row | index to the row | |
alloc | allocate memory for the row-data, or only pass the pointer to the new Array |
Principal Components analysis.
This implementation uses SVD to calculate the PCA. Example:
Array *X = get_data_from_somewhere(); Array *var, *X_pca; matrix_pca( X, &var, FALSE ); // overwrite X X_pca = matrix_pca( X, &var, TRUE ); // do not touch X
X | a 2D array observations x variables containing the data | |
var | output: vector of eigenvalues of XX^T in decreasing order; if you pass NULL, it is ignored; | |
alloc | if true, new memory is allocated and returned. Else X is overwritten. |
gsl_matrix* matrix_to_gsl | ( | Array * | in, | |
bool | alloc | |||
) |
convert 2D- Array struct to gsl_matrix.
This function works also for non-double Array's, but the input array MUST be of DType DOUBLE if alloc is FALSE. If alloc is TRUE, the type is casted to double during the copy-process.
Example:
gsl_matrix *g; Array *a = array_new_dummy( DOUBLE, 2, 10, 11 ); g = matrix_to_gsl( a, FALSES ); gsl_matrix_free( g ); //does not free a->data (!) array_free( a ); // does free a->data
in | the input 2D-array | |
alloc | if true, copy the memory pointed to by in; if false, simply put the pointer into the gsl_matrix struct (bad stuff happens if it is not DType DOUBLE); |