A C++ class for mathematical matrix usage
The class `cmatrix' is designed to provide a
type-safe and bounds-aware class used for conceptualizing mathematical matrix
operations as well as acting as a numerical-based "container" class. This
cmatrix class was originally based on an example class published by Bruce Eckel
(see here) and significantly expanded on by the author. By using
a simple reference-counting mechanism, it is both memory and speed efficient.
Moreover, it is bounds-safe - removing a commonplace source of errors and bugs
when using arrays in C / C++.
Internally, the data component of the class is quite simple. It merely
comprises a doubly indirected pointer to the data of the matrix, as well as
information on the size and dimensionality of the matrix (rows and columns).
Furthermore, each instance of a matrix maintains a reference counter to minimize
the repitition of multiple instances of the same core data.
Conventional C / C++ provides no native "matrix" type, and hence a programmer
needs to either use a third-party matrix class library, or develop his/her own.
Most third party matrix classes I examined seemed to either be too complex for
simple "quick and dirty" use, or, alternatively, far too primitive to really be
of any value. This class was designed to strike a midpoint between richness of
API and ease of use. No specific compilers or setup is required, merely the
header file and the compiled library itself.
The cmatrix API is divided into several
- type-definition enumerations. These define several enumerations for programmer
readability, including type of matrix (vector - row or column, square matrix),
sort preferences, normalization preferences, etc.
- constructor / destructor / initializations. Several different constructors and
initializers are defined. Matrices can be constructed from saved files, from
blocks of unstructured memory, with default (or specified) uniform values, with
random values, with chosen values randomly distributed over the matrix data,
with values quantized between specified limits, etc.
- access. Matrix data can be accessed in "individual" row column addressing, or
in whole row or whole column forms.
- output. Matrix contents can be output in a variety of forms (variable
precision or integer), offset with leading tabs, and to a variety of "streams"
- console based or file based.
- informational routines. Several methods exist to provide information about
matrix size and type. These include row or column vector types, size of
matrices, compatability between matrices, etc.
- structural manipulation routines. Various manipulation methods can be used to
remove / insert / replace / sort / etc. the contents of matrices. These can be
applied on a row-specific, column-specific, or sub-matrix-specific manner.
- find. Several search and "find and replace" methods are available.
- mathematical operations. Indeed, this constitutes one of the main reasons for
this class. Some simple statistical methods are provided (max., min, mean,
average, sum, standard deviation), as well as more traditional matrix-based
operations, including determinants, and operator overloads (+ - / * etc.).