Prev | Up | Next

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;

  1. Returns the number of elements in a matrix object.


  2. Returns the type size of the matrix elements.


  3. Returns the row number of a matrix.


  4. Returns the column number of a matrix.


  5. Returns the number of elements in a column of a matrix, i.e., its row number.


  6. Returns the number of elements in a row of a matrix, i.e., its column number.


  7. Returns the sum of all elements of a matrix.


  8. Returns the minimum value of all matrix elements.


  9. Returns the maximum value of all matrix elements.


  10. Returns the sum of diagonal elements of a matrix. The default diagonal is the main diagonal of the matrix.


  11. Resizes a matrix to a nRow by nCol matrix. This is an expensive operation and should not be used frequently unless it is necessary.


  12. 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.


  13. Sets all elements of a matrix equal to zero.


  14. Sets the matrix as a unit matrix, i.e., its main diagonal elements will be equal to 1.0 and the rest zero.


  15. 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.


  16. Returns the adjoin of a matrix. This is an expensive operation and only of academic interest.


  17. Returns the cofactor of ith row and jth column of a matrix.


  18. Returns the determinant of a matrix.


  19. Returns the condition number of a matrix.


  20. Returns the rank of a matrix.


  21. Returns the one norm of a matrix.


  22. Returns the two norm of a matrix.


  23. Returns the infinity norm of a matrix.


  24. Returns the Frobenius norm of a matrix.


  25. Applies the function fn to all elements of a matrix and returns the new matrix object.


  26. Applies the function fn to all elements of a matrix and returns the new matrix object.


  27. Applies the function fn to all elements of a matrix and returns the new matrix object.


  28. 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