/*****************************************************************************
* File: readme.txt
* Author: Nathan Toner
* Modified: January 26, 2012
*****************************************************************************/
/*****************************************************************************
* NTmatrix.h
*****************************************************************************/
Header file for NTmatrix class. For a program in which you want to use this
class, move this file and NTmatrix.cpp to the present working directory and:
#include "NTmatrix.h"
Then compile the main program along with NTmatrix.cpp. In the code, you can
define objects of class NTmatrix, and perform operations between them
(discussed below). You can also move the header file in to your ~/usr/include
directory and add this directory to your path. The header file then does not
need to be present in your working directory, and the above #include
directive should be changed to:
#include <NTmatrix.h>
For compiling a code that uses the NTmatrix class, execute the following (or
similar):
g++ -Wall -o [exec_name] [source_name].cpp NTmatrix.cpp
Where [exec_name] is the desired name of the executable, and [source_name] is
the name of your C++ source code.
/*****************************************************************************
* NTmatrix.cpp
*****************************************************************************/
Includes the code defining all member functions of class NTmatrix, as well as
operations between members of this class. Operations currently include
addition (+), subtraction (-), multiplication (*), assignment (=) and
transpose assignment (!=). These operators will be discussed in more detail in
the following.
Objects of class NTmatrix contain the following:
int n - number of rows in the matrix
int m - number of columns in the matrix
double** mat - values of the matrix stored in matrix form ( mat[n][m] )
double* vect - values of the matrix 'mat' stored in a vector in column-
major order
/*****************************************************************************
* Objects of class NTmatrix may be created with the following syntax:
*****************************************************************************/
NTmatrix object;
- null constructor for class NTmatrix
- sets n = m = 1, and mat = vect = 0
NTmatrix object(n, m);
- specify the rows and columns of the matrix as integer values n and m
- allocates memory for mat and vect of appropriate size
- sets all elements of the object equal to zero (0).
NTmatrix object(n, m, flag);
- specify the rows and columns of the matrix as integer values n and m
- allocates memory for mat and vect of appropriate size
- if char flag is 'I' or 'D' and the matrix is square, the diagonal
elements of the matrix are set to unity (identity matrix)
- otherwise sets all elements of the object equal to zero (0).
NTmatrix object(n, m, values);
- specify the rows and columns of the matrix as integer values n and m
- specify the values of the matrix as vector (double*) 'values' in
column-major order
- An example of column-major order:
vector[2 * 2] = {1, 2, 3, 4} is equivalent to
matrix[2][2] = {{1, 2}, {3, 4}}.
The first is the vector in column-major order containing all the
elements of the matrix.
NTmatrix object(n, m, flag, values);
- specify the rows and columns of the matrix as integer values n and m
- if char flag is 'D', then 'values' should only hold the diagonal
elements of the matrix, and the matrix will be initialized with these
values along the main diagonal and zeros everywhere else
(diagonal matrix)
- otherwise specify the values of the matrix as vector (double*) 'values'
in column-major order
- An example of column-major order:
NTmatrix object(n, m, matrix);
- specify the rows and columns of the matrix as integer values n and m
- specify the values of the matrix as 'matrix' (double**)
- as of January 26, 2012 (v1.1) this option does not work.
/*****************************************************************************
* The following operators are defined between objects of class NTmatrix:
*****************************************************************************/
Addition (+)
- between two matrices of the same size (rows and columns). The matrices
are then added element by element.
- between a matrix and a vector with the same number of rows. The vector
is then added to each column of the matrix.
- gives an error and exits program if dimensions are incompatable.
Subtraction (-)
- between two matrices of the same size (rows and columns). The matrices
are then subtracted element by element.
- between a matrix and a vector with the same number of rows. The vector
is then subtracted from each column of the matrix.
- gives an error and exits program if dimensions are incompatable.
Multiplication (*)
- here the number of rows of the right-hand argument must equal the number
of columns of the left-hand argument OR the RIGHT-HAND argument must be
a double precision scalar.
- for multiplication between two matrices, result will have the same number
of rows as the left-hand argument, and same number of columns as the
right-hand argument.
- for multiplication by a scalar, result will be the left-hand argument
with each element multiplied by the right-hand argument.
- gives an error and exits program if dimensions are incompatable.
Assignment (=)
- sets the left-hand argument equal to the right-hand argument
- does so by copying the values of n and m, allocating memory for the new
object, and assigning values from the 'vect' member of the right-hand
argument to the left-hand argument.
- gives an error and exits program if self-assignment is detected (a = a).
- requires that the right-hand argument be an expression, not just an
object, e.g.:
'object1 = object2*object3' works, but
'object1 = object2' does not.
For the latter case, use 'object1.assign(object2.vect)' instead.
Transpose Assignment (!=)
- Sets the left-hand argument equal to the transpose of the right-hand
argument
- Does so by copying the values of n and m from the right-hand argument in
to the values of m and n respectively of the left-hand argument, and
then copying the transpose of the right-hand argument in to the
left-hand argument's 'vect' member. It then assigns values from this
'vect' to the left-hand argument (left.assign(left.vect);).
- Allows for self-assignment.
/*****************************************************************************
* The following member functions exist for objects of class NTmatrix:
*****************************************************************************/
zero();
- Sets all elements of the members 'mat' and 'vect' to zero. Retains the
size of the matrix (n and m are unchanged).
trace();
- Returns the trace of a matrix as a double where trace = sum of diagonal
elements of matrix
- Note: matrix must be square for trace to work
printMat();
- prints the values stored in the matrix to the screen with a tag identifying
it as "mat".
- syntax is 'objectName.printMat();'
printTrans();
- similar to above but prints the transpose of the matrix with tag "trans =".
- syntax is 'objectName.printTrans();'
printVect();
- similar to above but prints the values in the matrix in a single vector in
column-major order with the tag "vect = (column-major order)".
- syntax is 'objectName.printVect();'
/*****************************************************************************
* testMain.cpp
*****************************************************************************/
Contains a main function for testing the NTmatrix class (NTmatrix.cpp and
NTmatrix.h files). Must be compiled along with NTmatrix.cpp to use the
NTmatrix class. Also must have #include "NTmatrix.h" at the beginning of the
file. Compile with:
g++ -Wall -o testMat testMain.cpp NTmatrix.cpp