Back | Table of content | Next |
To use those functions, include the file CheapMatrix.h in your program and link with -lCheapMatrix -lm. All those functions are located in the CheapMatrix namespace, except the operators.
Matrix map(MatrixType (*func)(MatrixType), const Matrix& a);
Matrix map(MatrixType (*func)(MatrixType,MatrixType), const Matrix& a, const Matrix& b);
Matrix map(MatrixType (*func)(MatrixType,MatrixType), const Matrix& a, const MatrixType& b);
Matrix map(MatrixType (*func)(MatrixType,MatrixType), const MatrixType& a, const Matrix& b);
Matrix map(MatrixType (*func)(MatrixType,MatrixType,MatrixType), const Matrix& a, const Matrix& b, const Matrix& c);
Matrix map(MatrixType (*func)(MatrixType,MatrixType,MatrixType), const Matrix& a, const Matrix& b, const MatrixType& c);
Matrix map(MatrixType (*func)(MatrixType,MatrixType,MatrixType), const Matrix& a, const MatrixType& b, const Matrix& c);
Matrix map(MatrixType (*func)(MatrixType,MatrixType,MatrixType), const Matrix& a, const MatrixType& b, const MatrixType& c);
Matrix map(MatrixType (*func)(MatrixType,MatrixType,MatrixType), const MatrixType& a, const Matrix& b, const Matrix& c);
Matrix map(MatrixType (*func)(MatrixType,MatrixType,MatrixType), const MatrixType& a, const Matrix& b, const MatrixType& c);
Matrix map(MatrixType (*func)(MatrixType,MatrixType,MatrixType), const MatrixType& a, const MatrixType& b, const Matrix& c);
Matrix map(MatrixType (*func)(MatrixType,void*), const Matrix& a, void* arg);
Matrix map(MatrixType (*func)(MatrixType,MatrixType,void*), const Matrix& a, const Matrix& b, void* arg);
Matrix map(MatrixType (*func)(MatrixType,MatrixType,void*), const Matrix& a, const MatrixType& b, void* arg);
Matrix map(MatrixType (*func)(MatrixType,MatrixType,void*), const MatrixType& a, const Matrix& b, void* arg);
Matrix map(MatrixType (*func)(MatrixType,MatrixType,MatrixType,void*), const Matrix& a, const Matrix& b, const Matrix& c, void* arg);
Matrix map(MatrixType (*func)(MatrixType,MatrixType,MatrixType,void*), const Matrix& a, const Matrix& b, const MatrixType& c, void* arg);
Matrix map(MatrixType (*func)(MatrixType,MatrixType,MatrixType,void*), const Matrix& a, const MatrixType& b, const Matrix& c, void* arg);
Matrix map(MatrixType (*func)(MatrixType,MatrixType,MatrixType,void*), const Matrix& a, const MatrixType& b, const MatrixType& c, void* arg);
Matrix map(MatrixType (*func)(MatrixType,MatrixType,MatrixType,void*), const MatrixType& a, const Matrix& b, const Matrix& c, void* arg);
Matrix map(MatrixType (*func)(MatrixType,MatrixType,MatrixType,void*), const MatrixType& a, const Matrix& b, const MatrixType& c, void* arg);
Matrix map(MatrixType (*func)(MatrixType,MatrixType,MatrixType,void*), const MatrixType& a, const MatrixType& b, const Matrix& c, void* arg);
Maps the function given as the first argument to each matrix element.
For example, map(cos, a) returns a matrix whose elements are the cosines
of the elements of a. If an argument is a scalar value, it is used for
all the matrix. For example map(pow, a, 4.2) will return a matrix whose
elements are the elements of a to the power 4.2, and for
map(pow, 2.0, a) 2.0 to the power the corresponding element of
a.
The void* extra argument will be passed to your function each time it is
called. Be very wary when using it, and make sure you can really cast it back.
An abstract base class pointer stored as void* may go wrong when you try to
dynamic_cast it back to an inherited class, for example. This could possibly
be solved by creating a plain structure to store it, but then it becomes quite
awkward (!). You can also create a wrapper. You can map MatrixType
foo(MatrixType x, a_value_of_other_type) to a matrix by creating a wrapper
MatrixType doo(MatrixType x) {return foo(x, a_value_of_other_type);}
(note that I don't make it inline). All this is unfortunately a poor replacement
for the implicit function mechanisms you can find in other languages, but better
than nothing.
See also the example mapper.cpp
Matrix generate(MatrixType (*func)(int,int), int rows, int cols, size_t bsize=0, const string& filename="", bool auto_del=false);
Create a matrix whose elements are generated by a user function. The matrix can be created in
memory (default) or using a swap file if specified. See the constructor for a description of
the corresponding arguments.
The function will be called successively on each cell of the new matrix, to generate a value
for the (row,column) it receives in argument.
See also the example mapper.cpp
tan, sin, cos, exp, log, log10, sqrt, acos, asin, atan, cosh, sinh,
tanh, acosh, asinh, atanh, ceil, fabs, floor, atan2, pow, fmod
Those functions of <math.h> have conveniently been mapped using
inline overloads. This means you can write directly sqrt(a)
and it is equivalent to map(sqrt,a).
Matrix operator+(const Matrix& a, const Matrix& b);
Matrix operator+=(const Matrix& a, const Matrix& b);
Matrix operator-(const Matrix& a, const Matrix& b);
Matrix operator-=(const Matrix& a, const Matrix& b);
Matrix operator+(const Matrix& a, const MatrixType& b);
Matrix operator+(const MatrixType& a, const Matrix& b);
Matrix operator+=(const Matrix& a, const MatrixType& b);
Matrix operator-(const Matrix& a, const MatrixType& b);
Matrix operator-(const MatrixType& a, const Matrix& b);
Matrix operator-=(const Matrix& a, const MatrixType& b);
Matrix operator*(const Matrix& a, const MatrixType& b);
Matrix operator*(const MatrixType& a, const Matrix& b);
Matrix operator*=(const Matrix& a, const MatrixType& b);
Matrix operator/(const Matrix& a, const MatrixType& b);
Matrix operator/(const MatrixType& a, const Matrix& b);
Matrix operator/=(const Matrix& a, const MatrixType& b);
Binary operators. The corresponding operation is applied on each matrix
elements, using a second matrix or a single value for all elements as
the other argument of the operation. Assertions are used to ensure the
dimensions match when using a two matrices.
Matrix operator*(const Matrix& mat1, const Matrix& mat2);
Matrix multiplication. An assertion is used to ensure the dimensions match.
The *= operator is member function and work only for square matrices.
Matrix multiply(const Matrix& a, const Matrix& b);
Matrix divide(const Matrix& a, const Matrix& b);
Element to element multiplication and division. Each element of the first Matrix
is multiplied or divided by the corresponding element of the second, and the result
returned.
MatrixType dot(const Matrix& a, const Matrix& b);
Dot product. Each element of the first Matrix is multiplied by the
corresponding element of the second, and all results are added together.
This operation is usually defined only on vectors, but is extended here
to matrices as well.
Matrix pow(const Matrix& a, int exponent);
Apply the 'fast exponentiation' algorithm to all matrix elements, on the
assumption that for integer exponents, this is faster. You can still call
the <math.h> original function if you wish (see above) by
writing pow(a, 5.0) instead of pow(a, 5).
Note: I didn't write a ^ operator for square matrices (possible confusion with the bit xor?, or with the element pow?), nor did I write a matrix exponential function. Mainly because I have no use for them at present. Feel free to contribute...
Note 2: There is no Matrix inverse operation, because of 2 good reasons:
- LAPACK does it very well using a LU decomposition already. See the inverse function.
- Inverting a Matrix is often used to solve a set of linear equations, which is
quite a bad thing to do, both in computational time and numerical stability. Once
again, LAPACK does it nicely with least squares estimation (see the LSE function).
Since I personally use LAPACK, and have no other use for Matrix inversion, I didn't
bother writing even a simple Gauss elimination algorithm.
Note: The following operators loop through the matrix line by line, which is natural when reading a text but can be quite slow when using a mapping to a file with a small memory buffer, since the FORTRAN memory convention (column-major) is used internally. For this reason, you might consider using the load and save utilities to read and write matrices in a transposed form.
ostream& operator<<(ostream& o, const Matrix& mat);
Output operator. The output format consists in printing the elements line by
line, separated by a space. This format is compatible with the Matlab(TM) text
format. Tip: You can use the setf and precision function, as in the following code:
ofstream output; output.precision(10); output.setf(ios::scientific);
output << a;.
istream& operator>>(istream& s, const Matrix& mat);
Input operator. This will read in a matrix from a stream, using the >>
operator consecutively for each element, line by line, which is also compatible
with the format generated by the << operator. This function will return
when the matrix has been read entirely (and not before). Extraneous elements
are not read. For this reason, if you don't know the dimensions a matrix will
have in advance, you may want to use the load utility.
Back | Table of content | Next |
Nicolas Brodu, 2000-06-18