A small C library for matrix manipulation.
To install, run following commands from terminal :
make
make install
Use LIB_PATH
and INCLUDE_PATH
to install the library in specific directory. By default, library is installed at /usr/local/lib
and header file is stored at /usr/local/include
.
At compilation, use -lcmat
flag. i.e.
gcc sample.c -lcmat
After installation, include cmat
and use any of the following functions.
-
MATRIX* cmat_malloc(int r, int c);
Returns a pointer to MATRIX structure with
r
rows andc
columns. -
void cmat_free(MATRIX* m);
Frees the memory allocated to the matrix pointed my
m
. -
int cmat_set(MATRIX* m, int row, int col, double data);
Sets the value of (
row
,col
) todata
in the matrix pointed bym
. Returns 0 on success, -1 ifrow
orcol
is out of bounds. -
double cmat_get(MATRIX* m, int row, int col);
Returns the value stored at (
row
,col
) in matrixm
; 0 if eitherrow
orcol
is out of bounds.
-
MATRIX* cmat_add_const(MATRIX* m, double a);
Adds constant
a
to the each element of matrixm
and returns the new matrix. -
MATRIX* cmat_multiply_const(MATRIX* m, double a);
Multiplies each element of matrix
m
witha
and returns the new matrix. -
MATRIX* cmat_add(MATRIX* m, MATRIX* n);
Adds two matrices
m
andn
and returns the new matrix. NULL if both matrices do not have same number of rows and columns. -
MATRIX* cmat_subtract(MATRIX* m, MATRIX* n);
Subtracts two matrices
m
andn
and returns the new matrix. NULL if both matrices do not have same number of rows and columns. -
MATRIX* cmat_divide_each(MATRIX* m, MATRIX* n);
Divides each element of
m
with respective element ofn
and returns the new matrix. NULL if both matrices do not have same number of rows and columns. -
MATRIX* cmat_multiply_each(MATRIX* m, MATRIX* n);
Multiplies each element of
m
with respective element ofn
and returns the new matrix. NULL if both matrices do not have same number of rows and columns. -
MATRIX* cmat_multiply(MATRIX* m, MATRIX* n);
Performs matrix multiplication on matrices
m
andn
and returns the answer matrix.
-
MATRIX* cmat_zeros(int r, int c);
Returns a matrix of dimension
r
xc
, filled with zeros. -
MATRIX* cmat_ones(int r, int c);
Returns a matrix of dimension
r
xc
, filled with ones. -
MATRIX* cmat_identity(int dim);
Returns an identity matrix of dimension
dim
xdim
. -
MATRIX* cmat_upper_trig(int dim);
Returns an upper triangular matrix of dimension
dim
xdim
. -
MATRIX* cmat_lower_trig(int dim);
Returns an lower triangular matrix of dimension
dim
xdim
.
-
double cmat_min(MATRIX* m);
Returns the smallest element in the matrix
m
. -
double cmat_max(MATRIX* m);
Returns the largest element in the matrix
m
. -
MATRIX* cmat_transpose(MATRIX* m);
Returns the transpose matrix of the matrix
m
. -
MATRIX* cmat_normalize(MATRIX* m);
Returns the normalized version of the matrix
m
.
- Transpose
- Normalization
- Determinant
- Rank of the matrix
- Inverse of a matrix
- Gaussian Reduction
- Reshaping and merging
Copyright (C) 2016 Nijraj Gelani (MIT)