Neural Network
Generic Neural Network for Classification
Classes | Macros | Typedefs | Functions
linalg.h File Reference

File containing common linear algebra functions. More...

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <assert.h>
#include <iostream>
Include dependency graph for linalg.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  _vector
 represents a vector padding is to make sure that matrix and vector both have the same byte size and allignment between matrix and vector they are able to be casted into one another More...
 
struct  _matrix
 represents a matrix between matrix and vector they are able to be casted into one another More...
 

Macros

#define MAT(m, x, y)   (m->data[(x * m->col) + y])
 macro for element access in a matrix struct
 
#define VEC(v, x)   (v->data[x])
 macro for element access in a vector struct
 

Typedefs

typedef struct _vector vector
 represents a vector padding is to make sure that matrix and vector both have the same byte size and allignment between matrix and vector they are able to be casted into one another
 
typedef struct _matrix matrix
 represents a matrix between matrix and vector they are able to be casted into one another
 

Functions

vectorvector_create (size_t size)
 Creates a vector this function will malloc the exact space for the required dimensions. More...
 
matrixmatrix_create (size_t row, size_t col)
 Creates a matrix this function will malloc the exact space for the required dimensions. More...
 
matrixvec_to_mat (vector *vec, int orientation)
 Converts vector into matrix this function will "cast" the vector into a matrix by using the fact that they both have the same size. when calling this function, calling free() on the matrix will free the vector and vice versa. More...
 
vectormat_to_vec (matrix *mat)
 Converts matrix into vector this function will "cast" the matrix into a vector by using the fact that they both have the same size. when calling this function, calling free() on the vector will free the matrix and vice versa. the input matrix can only be a matrix with dimensions 1 X N (a row matrix) More...
 
void matrix_reshape (matrix *mat, size_t row, size_t col)
 Reshapes the matrix this function will reshape the matrix in constant time. More...
 
double dot_product (const double *left, const double *right, int length)
 Performs dot product on two double* this function will malloc for the user a double arrays must be of same size. More...
 
vectorvecscalar_multiply (const vector *vec, const double scalar)
 Performs scalar multiplication of a vector this function will malloc for the user a vector*. More...
 
vectorvecscalar_divide (const vector *vec, const double scalar)
 Performs scalar division of a vector this function will malloc for the user a vector*. More...
 
vectorvecmat_multiply (const vector *vec, const matrix *mat)
 Performs vector matrix multiplication this function will malloc for the user a vector* if the vector is of size M and the matrix size M by N, the resulting column vector will be of size N. More...
 
vectormatvec_multiply (const matrix *mat, const vector *vec)
 Performs matrix vector multiplication this function will malloc for the user a vector* if the matrix is of size M by N and the vector N, the resulting row vector will be of size M. More...
 
void mat_print (const matrix *mat)
 prints the matrix this function will not modify the matrix and print to stdout More...
 
void vec_print (const vector *vec)
 prints the vector this function will not modify the vector and print to stdout More...
 
matrixmatmat_multiply (const matrix *left, const matrix *right)
 Performs standard matrix multiplication this function will malloc for the user a matrix*. More...
 
matrixmatmat_addition (const matrix *left, const matrix *right)
 Performs standard matrix addition this function will malloc for the user a matrix*. More...
 
matrixmatmat_subtraction (const matrix *left, const matrix *right)
 Performs standard matrix subtration this function will malloc for the user a matrix*. More...
 
matrixmatscalar_multiply (const matrix *mat, const double scalar)
 Performs scalar multiplication of a matrix this function will malloc for the user a matrix*. More...
 
matrixmatscalar_divide (const matrix *mat, const double scalar)
 Performs scalar division of a matrix this function will malloc for the user a matrix*. More...
 
matrixmat_transpose (const matrix *mat)
 Performs matrix transpose this function will malloc for the user a matrix*. More...
 
void vec_append (vector **vec_a, vector *vec_b)
 Appends vector b to vector a this function will realloc for the user vector a and free vector b. More...
 
void eigen (int N, double a[], int it_max, double v[], double d[], int *it_num, int *rot_num)
 Performs Jacobi eigenvalue iteration this function will required the user to pass in non-null it_num and rot_num it does not malloc but rather requires the caller to malloc space for it. More...
 
void mat_identity (int n, double a[])
 modifies a matrix to be the identity matrix of size n More...
 
void diag_vector (int n, double a[], double v[])
 gets the diagonal entries More...
 
double frobenius_norm (int n, int k, double a[], double x[], double lambda[])
 computes the Frobenius norm in a right eigensystem More...
 
matrixcovmat (matrix *mat)
 computes the variance covariance matrix More...
 

Detailed Description

File containing common linear algebra functions.

Minhyuk Park

Date
7 Nov 2017

Function Documentation

matrix* covmat ( matrix mat)

computes the variance covariance matrix

Returns
matrix* the output variance-covariance matrix this function will malloc a new matrix
Parameters
mata matrix* representing the input matrix of deviation scores of size n by k
void diag_vector ( int  n,
double  a[],
double  v[] 
)

gets the diagonal entries

Parameters
nint the dimension
a[]double[] input the matrix, N by N
v[]double[] output the diagonal entries, N
double dot_product ( const double *  left,
const double *  right,
int  length 
)

Performs dot product on two double* this function will malloc for the user a double arrays must be of same size.

Returns
a double representing the inner product
Parameters
leftconst double* the first vector
rightconst double* the second vector
lengthint the size of the vectors
void eigen ( int  N,
double  a[],
int  it_max,
double  v[],
double  d[],
int *  it_num,
int *  rot_num 
)

Performs Jacobi eigenvalue iteration this function will required the user to pass in non-null it_num and rot_num it does not malloc but rather requires the caller to malloc space for it.

Parameters
Nint the dimiension of the input matrix a, which is a N by N matrix
a[]double[] the input matrix which has to be square, real, and symmetric
it_maxint maximum number of iterations to stop at
v[]double[]output matrix of eigenvectors, which is a N by N matrix
d[]double[] output matrix of eigenvalues, in descending order
it_numint* output total number of iterations
rot_numint* output total number of rotations
double frobenius_norm ( int  n,
int  k,
double  a[],
double  x[],
double  lambda[] 
)

computes the Frobenius norm in a right eigensystem

Returns
double the frobenius norm of A * X - X * lambda
Parameters
nint the dimension of the matrix
kint the number of eigen vectors
a[]double[] input matrix of size n by n
x[]double[] input vector of eigenvectors of size k
lamdba[]double[] input vector of eigen values
void mat_identity ( int  n,
double  a[] 
)

modifies a matrix to be the identity matrix of size n

Parameters
nint the dimension
a[]double output identity matrix
void mat_print ( const matrix mat)

prints the matrix this function will not modify the matrix and print to stdout

Parameters
matconst matrix* the matrix to be printed
vector* mat_to_vec ( matrix mat)

Converts matrix into vector this function will "cast" the matrix into a vector by using the fact that they both have the same size. when calling this function, calling free() on the vector will free the matrix and vice versa. the input matrix can only be a matrix with dimensions 1 X N (a row matrix)

Returns
vector* the new pointer for input matrix*
Parameters
matmatrix* to be converted in terms of the newly formed vector
matrix* mat_transpose ( const matrix mat)

Performs matrix transpose this function will malloc for the user a matrix*.

Returns
matrix* the newly transposed and malloced matrix
Parameters
matconst matrix* the matrix to be transposed

Referenced by Network::Network().

matrix* matmat_addition ( const matrix left,
const matrix right 
)

Performs standard matrix addition this function will malloc for the user a matrix*.

Returns
matrix* newly malloced result of left plus right
Parameters
leftconst matrix* the matrix to be added to
rightconst matrix* the matrix to be added

Referenced by Network::Network().

matrix* matmat_multiply ( const matrix left,
const matrix right 
)

Performs standard matrix multiplication this function will malloc for the user a matrix*.

Returns
matrix* newly malloced result of left times right
Parameters
leftconst matrix* the matrix to be multiplied to
rightconst matrix* the matrix to be multiplied

Referenced by Network::Network().

matrix* matmat_subtraction ( const matrix left,
const matrix right 
)

Performs standard matrix subtration this function will malloc for the user a matrix*.

Returns
matrix* newly malloced result of left minus right
Parameters
leftconst matrix* the matrix to be subtracted from
rightconst matrix* the matrix to be subtrated
matrix* matrix_create ( size_t  row,
size_t  col 
)

Creates a matrix this function will malloc the exact space for the required dimensions.

Returns
matrix* the newly malloced matrix
Parameters
rowsize_t the desired number of rows in the matrix
colsize_t the desired number of columns in the matrix

Referenced by Network::Network().

void matrix_reshape ( matrix mat,
size_t  row,
size_t  col 
)

Reshapes the matrix this function will reshape the matrix in constant time.

Parameters
matmatrix* to be reshaped
rowsize_t the new row
colsize_t the new column
matrix* matscalar_divide ( const matrix mat,
const double  scalar 
)

Performs scalar division of a matrix this function will malloc for the user a matrix*.

Returns
matrix* that is the result of element wise division of the matrix by the scalar
Parameters
matconst matrix* the input matrix
scalarconst double the input scalar
matrix* matscalar_multiply ( const matrix mat,
const double  scalar 
)

Performs scalar multiplication of a matrix this function will malloc for the user a matrix*.

Returns
matrix* that is the result of element wise multiplication of the matrix by the scalar
Parameters
matconst matrix* the input matrix
scalarconst double the input scalar
vector* matvec_multiply ( const matrix mat,
const vector vec 
)

Performs matrix vector multiplication this function will malloc for the user a vector* if the matrix is of size M by N and the vector N, the resulting row vector will be of size M.

Returns
vector* newly malloced result of matrix vector multiplication
Parameters
matconst matrix* the input matrix
vecconst vector* the input vector
void vec_append ( vector **  vec_a,
vector vec_b 
)

Appends vector b to vector a this function will realloc for the user vector a and free vector b.

Parameters
vec_avector** the vector to be realloced
vec_bvector* the vector to be appended and freed thereafter
void vec_print ( const vector vec)

prints the vector this function will not modify the vector and print to stdout

Parameters
vecconst vector* the vector to be printed
matrix* vec_to_mat ( vector vec,
int  orientation 
)

Converts vector into matrix this function will "cast" the vector into a matrix by using the fact that they both have the same size. when calling this function, calling free() on the matrix will free the vector and vice versa.

Returns
matrix* the new pointer for input vector*
Parameters
vecvector* to be converted
orientationint 1 is Row-wise and 0 is Column-wise in terms of the newly formed matrix
vector* vecmat_multiply ( const vector vec,
const matrix mat 
)

Performs vector matrix multiplication this function will malloc for the user a vector* if the vector is of size M and the matrix size M by N, the resulting column vector will be of size N.

Returns
vector* newly malloced result of vector matrix multiplication
Parameters
vecconst vector* the input vector
matconst matrix* the input matrix
vector* vecscalar_divide ( const vector vec,
const double  scalar 
)

Performs scalar division of a vector this function will malloc for the user a vector*.

Returns
vector* that is the result of element wise division of the vector by the scalar
Parameters
vecconst vector* the input vector
scalarconst double the input scalar
vector* vecscalar_multiply ( const vector vec,
const double  scalar 
)

Performs scalar multiplication of a vector this function will malloc for the user a vector*.

Returns
vector* that is the result of element wise multiplication of the vector by the scalar
Parameters
vecconst vector* the input vector
scalarconst double the input scalar
vector* vector_create ( size_t  size)

Creates a vector this function will malloc the exact space for the required dimensions.

Returns
vector* the newly malloced vector
Parameters
sizesize_t the desired number of elements in the vecrtor

Referenced by Network::Network().