Unspecified type

next - skip - up - start

Skip this section on your first reading.

If you want to work with a matrix of unknown type, say in a function. You can construct a matrix of type GenericMatrix. Eg

   Matrix A;
   .....                                  // put some values in A
   GenericMatrix GM = A;
A GenericMatrix matrix can be used anywhere where a matrix expression can be used and also on the left hand side of an =. You can pass any type of matrix (excluding the Crout and BandLUMatrix types) to a const GenericMatrix& argument in a function. However most scalar functions including Nrows(), Ncols(), Type() and element access do not work with it. Nor does the ReturnMatrix construct. See also the paragraph on LinearEquationSolver.

An alternative and less flexible approach is to use Basematrix or GeneralMatrix.

Suppose you wish to write a function which accesses a matrix of unknown type including expressions (eg A*B). Then use a layout similar to the following:

   void YourFunction(BaseMatrix& X)
   {
      GeneralMatrix* gm = X.Evaluate();   // evaluate an expression
                                          // if necessary
      ........                            // operations on *gm
      gm->tDelete();                      // delete *gm if a temporary
   }
See, as an example, the definitions of operator<< in newmat9.cxx.

Under certain circumstances; particularly where X is to be used just once in an expression you can leave out the Evaluate() statement and the corresponding tDelete(). Just use X in the expression.

If you know YourFunction will never have to handle a formula as its argument you could also use

   void YourFunction(const GeneralMatrix& X)
   {
      ........                            // operations on X
   }
Do not try to construct a GeneralMatrix or BaseMatrix.