Prev | Up | Next

Subscript and sub-matrix operations

Use the following metods and operators to access/modify elements within a matrix object. Most of the subscript and sub-matrix operators are implemented using temporary class object for efficiency and usability reasons. These temporary classes are not shown in this document for simplicity as you do not need to create them. To know exactly how they work, please refer to the Matrix TCL Pro source code. For invalid row and column values, these methods/operators will throw out_of_range exception only if the RANGE_CHECK_ constant is defined; otherwise the behavior is undefined.

1. valarray<T>& row (size_t i);
2. valarray<T> row (size_t i) const;
3. valarray<T>& column (size_t i);
4. valarray<T> column (size_t i) const;
5. valarray<T>& diag (int i=0);
6. valarray<T> diag (int i=0) const;

7. valarray<T>& operator[] (size_t i);
8. valarray<T> operator[] (size_t i) const;

9. valarray<T>& operator() (size_t i);
10. valarray<T> operator() (size_t i) const;

11. T& operator[][] (size_t i, size_t j);
12. T operator[][] (size_t i, size_t j) const;

13. T& operator() (size_t i, size_t j);
14. T operator() (size_t i, size_t j) const;

15. matrix<T>& operator[] (mslice ms);
16. matrix<T> operator[] (mslice ms) const;

17. matrix<T>& operator[] (gmslice gms);
18. matrix<T> operator[] (gmslice gms) const;

enum MATIRX_TYPE { DIAGONAL, TRIDIAGONAL, UTRIANG, LTRIANG };

class mslice;
class gmslice;

mslice (size_t StartRow, size_t StartCol, size_t NoOfRows, size_t NoOfCols);
gmslice (MATIRX_TYPE mtype);
gmslice (size_t nCol, const valarray<size_t>& sRow, const valarray<size_t>& nRow);

Parameter

i
The i th row or column of the matrix. The index starts from zero.
j
The j th row or column of the matrix. The index starts from zero.
ms
An object of type mslice
gms
An object of type gmslice

  1. Returns the i th row of a matrix object. You can use it as both R-value and L-value in further matrix operations.


  2. Returns the i th row of a constant matrix object. You can use it as R-value in further matrix operations.


  3. Returns the i th column of a matrix object. You can use it as both R-value and L-value in further matrix operations.


  4. Returns the i th column of a constant matrix object. You can use it as R-value in further matrix operations.


  5. Returns the i th diagonal of a matrix object. The default diagonal is the main diagonal of the matrix. Use negative index to get the diagonals below the main diagonal, and positive index for diagonals above the main diagonal of the matrix. You can use the return as both R-value and L-value in further matrix operations.


  6. Returns the i th diagonal of a constant matrix object. The default diagonal is the main diagonal of the matrix. Use negative index to get the diagonals below the main diagonal, and positive index for diagonals above the main diagonal of the matrix. The return value can be used as R-value in further matrix operations.


  7. Returns the i th row of a matrix object. The return value can be used as both R-value and L-value in further matrix operations.


  8. Returns the i th row of a constant matrix object. The return value can be used as R-value in further matrix operations.


  9. Returns the i th column of a matrix object. The return value can be used as both R-value and L-value in further matrix operations.


  10. Returns the i th column of a constant matrix object. The return value can be used as R-value in further matrix operations.


  11. Returns the i,j th element of a matrix object. The return value can be used as both R-value and L-value in further mathematical operations.


  12. Returns the i,j th element of a constant matrix object. The return value can be used as R-value in further mathematical operations.


  13. Returns the i,j th element of a matrix object. The return value can be used as both R-value and L-value in further mathematical operations. This might be slightly more efficient than the double operator[][].


  14. Returns the i,j th element of a constant matrix object. The return value can be used as R-value in further mathematical operations. This might be slightly more efficient than the double operator[][].


  15. Returns a sub-matrix from a matrix object as defined by the mslice parameter. You need to provide starting row, starting column, row number, and column number to construct the mslice object. The return matrix can be used as both R-value and L-value to construct/assign to a new matrix, or to change the existing matrix when used as L-value.


  16. Returns a sub-matrix from a matrix object as defined by the mslice parameter. You need to provide starting row, starting column, row number, and column number to construct the mslice object. The return matrix can be used as R-value to construct a new matrix or assign to different matrix.


  17. Returns a sub-matrix from a matrix object as defined by the gmslice parameter. You can either use MATIRX_TYPE constants, e.g., DIAGONAL, TRIDIAGONAL, UTRIANG, or LTRIANG, or provide starting column and number of elements in each row of the matrix to construct a gmslice object. The return matrix can be used as both R-value and L-value to construct a new matrix or assign to different matrix.


  18. Returns a sub-matrix from a matrix object as defined by the gmslice parameter. You can either use MATIRX_TYPE constants, e.g., DIAGONAL, TRIDIAGONAL, UTRIANG, or LTRIANG, or provide starting column and number of elements in each row of the matrix to construct a gmslice object. The return matrix can be used as R-value to construct a new matrix or assign to different matrix.


Examples
typedef techsoft::matrix<double> Matrix;
typedef std::valarray<double> Vector;

using techsoft::mslice;
using techsoft::gmslice;
using techsoft::DIAGONAL;
using techsoft::UTRIANG;
using techsoft::LTRIANG;
using techsoft::TRIDIAGONAL;

Matrix A(5,5);
double x,y;
size_t i=0,j=0;

A.rand();
const Matrix B = A;

x = B[i][j];            // Gets the element B(i,j)
A[i+1][j+1] = x;        

x = A(i,j)--;
y = ++A(i,j);

x = A[i][j]  * B[i+1][j+1];
y = x * A[i][j];

A[i][j] += B[i+1][j+1];
A[i][j] /= -B[i+1][j+1];

double *pA = &A[0][0];  // Gets the address of the first element of A

Vector V = B[4];        // Gets 4th row of matrix B
A(1) = V;               // Assigns the vector V to 1st column of matrix A

A[i] += V;
A[j] *= V;
A[i] = 1.0;             // Sets the i th row to 1.0

V = B.diag();           // Gets the diagonal elements of B
A.diag() = V;

Vector V2 = B.diag(-1); // Gets the diagonal just below the main diagonal
A.diag(1) = V2;         // Assigns the vector just above the main diagonal
A.diag() = 1.0;         // Sets the elements of main diagonal to 1.0
A.diag(-2)[1] = 0.0;

Matrix SA = B[mslice(1,1,3,3)];
A[mslice(1,1,3,3)] = SA;
A[mslice(2,2,1,2)] = B[mslice(2,2,1,2)];

A[mslice(1,0,2,3)] *= 10.0;
A[mslice(1,0,2,3)] /= 10.0;
A[mslice(1,0,2,3)] = 1.0;

A[mslice(1,0,2,3)] += B[mslice(1,0,2,3)];
A[mslice(1,0,2,3)] -= B[mslice(1,0,2,3)];

Matrix C = A[gmslice(DIAGONAL)];  
C = A[gmslice(TRIDIAGONAL)];
C = A[gmslice(UTRIANG)];
C = A[gmslice(LTRIANG)];

C[gmslice(DIAGONAL)] += B;   
C[gmslice(TRIDIAGONAL)] -= B;      
C[gmslice(UTRIANG)] *= 12.0;
C[gmslice(LTRIANG)] /= 12.0;

A = A[gmslice(UTRIANG)];
A[gmslice(LTRIANG)] = ~A;       // Gets a symmetric matrix