Utility methods
The following utility methods are available for the matrix class.
1. size_t size () const;
2. size_t typesize () const;
3. size_t rowno () const;
4. size_t colno () const;
5. size_t colsize () const;
6. size_t rowsize () const;
7. T sum () const;
8. T min () const;
9. T max () const;
10. T trace (int i=0) const;
11. void resize (size_t nRow, size_t nCol);
12. void free ();
13. void null ();
14. void unit ();
15. void rand (unsigned RandMax = 1, unsigned RandSeed = 0);
16. matrix<T> adj () const;
17. T cofact (size_t row, size_t col) const;
18. T det () const;
19. T cond () const;
20. size_t rank () const;
21. T norm1 () const;
22. T norm2 () const;
23. T normI () const;
24. T normF () const;
25. matrix<T> apply (T (*fn)(T v)) const;
26. matrix<T> apply (T (*fn)(const T& v)) const;
27. matrix<T> apply (T (*fn)(size_t i, size_t j, T v)) const;
28. matrix<T> apply (T (*fn)(size_t i, size_t j, const T& v)) const;
- Returns the number of elements in a matrix object.
- Returns the type size of the matrix elements.
- Returns the row number of a matrix.
- Returns the column number of a matrix.
- Returns the number of elements in a column of a matrix, i.e., its row number.
- Returns the number of elements in a row of a matrix, i.e., its column number.
- Returns the sum of all elements of a matrix.
- Returns the minimum value of all matrix elements.
- Returns the maximum value of all matrix elements.
- Returns the sum of diagonal elements of a matrix. The default diagonal is the main diagonal of the matrix.
- Resizes a matrix to a nRow by nCol matrix. This is an expensive operation and should not be used frequently unless it is necessary.
- Frees the memory associated with a matrix object and sets its size as zero by zero matrix. Note that you can't use a zero size matrix in any operation.
- Sets all elements of a matrix equal to zero.
- Sets the matrix as a unit matrix, i.e., its main diagonal elements will be equal to 1.0 and the rest zero.
- Generates a random number matrix. By default the random number will be from 0 to 1, but you can provide the maximum value and initial seed value for the random number generator.
- Returns the adjoin of a matrix. This is an expensive operation and only of academic interest.
- Returns the cofactor of ith row and jth column of a matrix.
- Returns the determinant of a matrix.
- Returns the condition number of a matrix.
- Returns the rank of a matrix.
- Returns the one norm of a matrix.
- Returns the two norm of a matrix.
- Returns the infinity norm of a matrix.
- Returns the Frobenius norm of a matrix.
- Applies the function fn to all elements of a matrix and returns the new matrix object.
- Applies the function fn to all elements of a matrix and returns the new matrix object.
- Applies the function fn to all elements of a matrix and returns the new matrix object.
- Applies the function fn to all elements of a matrix and returns the new matrix object.
Examples
typedef matrix<double> Matrix;
Matrix A(6,4);
double x;
size_t i,j;
A.rand( 100); // Generates a random matrix with elements 0 to 100
i = A.size(); // i = 6 x 4 = 24
j = A.rowno(); // Row number equal to 6
j = A.colno(); // Column number equal to 4
A.resize( 4, 4); // Resize the matrix to a 4 x 4 one
x = A.sum(); // Sum of all elements
x = A.trace(-1); // Sum of the diagonal elements just below the main diagonal
x = A.det(); // Determinant of A
x = A.cond(); // Condition number of A
x = A.normF(); // Frobenius norm of A
Matrix B = A.apply(sin);// Apply sin function to A
A.unit(); // A is now a unit matrix