[Home] [News] [API] [Example usage] [FAQ] [Roadmap] [Download] [Contact]


A C++ class for mathematical matrix usage

Rudolph Pienaar


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 sections: