Prev | Up | Next

Utility methods

The following utility methods are available in the matrix class.

1. size_t size () const;
2. size_t typesize () const;

3. size_t rowno () const;
4. size_t colno () const;

5. T sum () const;
6. T min () const;
7. T max () const;
8. T trace (int i=0) const;

9. void resize (size_t nRow, size_t nCol);
10. void free ();

11. void null ();
12. void unit ();
13. void rand (int rmin=-1, int rmax=1, int rseed=0);

14. matrix<T> adj () const;
15. T cofact (size_t row, size_t col) const;
16. T det () const;
17. T cond () const;
18. size_t rank () const;

19. T norm1 () const;
20. T norm2 () const;
21. T normI () const;
22. T normF () const;

23. matrix<T> apply (T (*fn)(T v)) const;
24. matrix<T> apply (T (*fn)(const T& v)) const;
25. matrix<T> apply (T (*fn)(size_t i, size_t j, T v)) const;
26. 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 sum of all elements of a matrix.


  6. Returns the minimum value of all matrix elements.


  7. Returns the maximum value of all matrix elements.


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


  9. Resizes a matrix to a nRow by nCol matrix. It is provided only for completeness, and you do not normally need to use this function.


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


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


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


  13. Generates a random number matrix. By default the random numbers will be in the range of -1.0 to 1.0, but you can provide a different minimum and maximum range, as well as the initial seed value for the random number generator.


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


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


  16. Returns the determinant of a matrix.


  17. Returns the condition number of a matrix.


  18. Returns the rank of a matrix.


  19. Returns the one norm of a matrix.


  20. Returns the two norm of a matrix.


  21. Returns the infinity norm of a matrix.


  22. Returns the Frobenius norm of a matrix.


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


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


  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.


Examples
typedef techsoft::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