Back Table of content Next

Utilities

To use those functions, include the file CM_util.h in your program and link with -lCheapMatrix -lm. All those functions are located in the CheapMatrix namespace.

Generators

Matrix zeros(int rows, int cols=1, size_t bsize=0, const string& filename="", bool auto_del=false);
Matrix ones(int rows, int cols=1, size_t bsize=0, const string& filename="", bool auto_del=false);
Matrix eye(int rows, int cols=1, size_t bsize=0, const string& filename="", bool auto_del=false);

Convenience functions that use a generator. They produce respectively a Matrix full of 0, full of 1, or with 1 on the diagonal only. Please refer to the generate function, and the Matrix constructor, for a description of the arguments. Also, cols defaults to 1, which is more natural when creating Vectors.

Vector range(MatrixType bound1, MatrixType bound2);
Vector range(MatrixType bound1, MatrixType increment, MatrixType bound2);

The range function works like the Octave a:b:c utility. A series of values is created, starting from bound1, incremented as specified (default is 1), and up to bound2 included (but not beyond). The results is put in a Vector, which is particularly useful for sub-matrix extractions.
See also the example range.cpp for a more detailed description.

Common math functions

MatrixType sum(const Matrix& mat);
Matrix sum(const Matrix& mat, int dim);

Sum all elements, or only along the dimension given.

inline MatrixType min(const MatrixType& a, const MatrixType& b)
inline MatrixType max(const MatrixType& a, const MatrixType& b)

min and max for elements, in our own namespace to avoid collisions.

MatrixType min(const Matrix& mat);
MatrixType max(const Matrix& mat);
Matrix min(const Matrix& mat, int dim);
Matrix max(const Matrix& mat, int dim);

min or max of all elements, or only along the dimension given.

Basic statistics

MatrixType mean(const Matrix& mat);
Matrix mean(const Matrix& mat, int dim);

mean is sum / number of elements used in the sum.

MatrixType var(const Matrix& mat);
Matrix var(const Matrix& mat, int dim);

var is the variance, of all elements, or only along the dimension given.

MatrixType stdev(const Matrix& mat);
Matrix stdev(const Matrix& mat, int dim);

Standard deviation, of all elements, or only along the dimension given.

Random numbers

Warning: All the random generator functions use the system random(), so might not be enough if that function is poorly implemented. You are welcome to contribute proper random number generators if you need them!

void randinit();
void randinit(long seed);

Initialize the seed with a value taken from the internal clock, or the argument given (which enables to reproduce results). It will be called automatically by the other functions if needed.

MatrixType randu();
Returns a MatrixType from a uniform distribution on [0,1).

MatrixType randn();
Returns a MatrixType from a normal (gaussian) distribution with mean 0, variance 1.

Matrix randu(int rows, int cols=1, size_t bsize=0, const string& filename="", bool auto_del=false);
Matrix randn(int rows, int cols=1, size_t bsize=0, const string& filename="", bool auto_del=false);

Generator facilities to create random matrices and vectors. Please refer to the generate function, and the Matrix constructor, for a description of the arguments..

Input / Output

bool load(Matrix &a, const string& filename);
bool save(const Matrix &a, const string& filename, int precision=10);

Load and save in transposed form in a text file. This is much faster than the operators for swapped matrices, since the looping is done column-major.
Also, load will fill the matrix given if the dimension match, or use a temporary otherwise. Thus, you don't need to know the dimension before loading.
The precision optional argument of save will be used for the iostream modifier, and the format chosen is 'scientific' (exponential form, like -3.8305971903e+00).


Back Table of content Next

Nicolas Brodu, 2000-06-18