MATRIX.H

Description : Class library for matrix and vector operations

Header files called upon : VECTOR.H

Files required in project : MATRIX.CPP

Class details are as follows:

Class declaration : Matrix

Member variables (private)
float *mat; float pointer to matrix element
int nr, nc; numbers of rows and columns
Constructors (public)
Matrix(int p, int q) : nr(p), nc(q)
Constructor to allocate space for a matrix of specified size
eg Matrix mat1(3, 2);
NB Object mat1 will be allocated 3 rows and 2 columns

Matrix() : mat(new float[9]), nr(3), nc(3) {}
Default constructor allocating space for 3x3 matrix
eg Matrix mat1;

Matrix(float e1, float e2, float e3, float e4, float e5, float e6, float e7, float e8, float e9) : nr(3), nc(3)
Constructor for an initialized 3x3 matrix
eg Matrix m1(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0);

Matrix(const Matrix &m)
Constructor to copy an already existing matrix into a previously undeclared matrix
eg Matrix mat2(mat1);
NB1 mat2 is previously undeclared, mat1 already initialized
NB2 function defined in MATRIX.CPP

~Matrix()
Destructor
Member functions (public)
float& operator() (int i, int j) const
Overloads parentheses operator to access a particular element in a matrix
eg float scl1 = mat1(i,j);
NB (i,j)th matrix element corresponds to ([i-1],[j-1])th array element) therefore mat1(1,1) accesses first matrix element but zeroth array element

Vector operator* (Vector& v) const
Overloads '*' operator for pre-multiplication of a vector by a matrix
eg vec2 = mat1 * vec1;
NB function defined in MATRIX.CPP

Matrix operator- (Vector &v) const
Subtraction of a vector from each row of a matrix
eg mat2 = mat1 - vec1;
NB function defined in MATRIX.CPP

Vector colMean()
Returns the column mean of a matrix as a vector, in other words, the nth element of the vector is the mean of the values of the nth row in the matrix
eg vec1 = mat1.colMean(); if mat1 = (1.1 3.3) then vec1 = (2.2)
NB function defined in MATRIX.CPP

void eigens(int n, Vector &eval, Matrix &evec)
Derives 'n' eigenvalues and puts them in decreasing magnitude into specified vector, also derives corresponding eigenvectors and puts them in columns into specified (n x n) matrix
eg mat1.eigens(n,vec_eigenval,mat_eigenvect);
NB1 maximum value of n allowed for is 10
NB2 vec_eigenval is vector of returned eigenvalues
NB3 mat_eigenvect is matrix of returned eigenvectors
NB4 function defined in MATRIX.CPP

void eigen(int mv, int n, float* a, float* r)
Calculates 'n' eigenvalues (mv = 0) or 'n' eigenvalues+eigenvectors (mv = 1)
NB1 'a' is a pointer to a 1D float array of size n*(n+1)/2 used for workspace and also returns eigenvalues, 'r' is a pointer to a 1D float array of size n*n and returns eigenvectors
NB2 'eigen' is called by the above member function 'eigens'
NB3 function defined in MATRIX.CPP

void esort(int mv, int n, float* a, float* r)
Sorts eigenvalues (mv = 0) or eigenvalues+eigenvectors (mv = 1) in order of descending eigenvalue
NB1 arguments must be identical to those for the above member function 'eigen' which must be called first before calling 'esort'
NB2 'esort' is called by member function 'eigens'
NB3 function defined in MATRIX.CPP

void cholinv()
Inverts a matrix employing Cholesky decomposition
eg mat1.cholinv();
NB1 mat1 must be square matrix with positive determinant
NB2 function defined in MATRIX.CPP

friend Matrix operator * (const Matrix& m1, const Matrix& m2)
Overloads '*' operator for matrix multiplication
eg mat1 = mat2 * mat3;
NB defined in MATRIX.CPP

friend Matrix operator & (const Matrix& m1, const Matrix& m2)
Overloads '&' operator to multiply a transpose of a matrix by another matrix
eg mat1 = mat2 & mat3;
NB1 the transpose of mat2 is used but mat2 itself is not affected
NB2 mat2 and mat3 must have same number of rows (errorchecked by function)
NB3 user will need to ensure that the (rows x columns) dimensions of mat1 are (mat2_columns x mat3_columns)
NB4 function defined in MATRIX.CPP

friend ostream& operator << (ostream &os, Matrix &m)
Overloads '<<' operator to write out a matrix
eg cout << mat1;
NB function defined in MATRIX.CPP

Class declaration : LtMatrix

As yet incomplete