You are here

How to Create and Fill a Matrix?

Several decisions have to be made:

  • Float or double precision matrix. In the first case you will use the TMatrixF class family, in the latter case the TMatrixD one.
  • The matrix type: general, symmetric or sparse.
  • The matrix size and the index range.

For example a double-precision general matrix whose row index runs from -2 to 5 and column index from -5 to 10 is created through:

   TMatrixD a(-2,5,-5,-10);

If you want the more conventional indexing, starting at 0 and a matrix size of 7x15:

   TMatrixD a(7,15);

Filling the matrix can also be done in several ways. In case you fill the matrix by assigning an array, it is important to know that the elements are row-wise stored . All of the following methods can be applied to the different matrix types: + Referencing row/column combination (irow,icol) for matrix a as a[irow][icol] or a(irow,icol). + Copying an array. This can be either through the constructor or through SetMatrixArray. + Using an array or matrix through the Use function. Here, no data is copied.

An important issue in the linear algebra package is that matrices should have the same shape before they can be copied through an assignment. For instance:

   TMatrixD b(2,3);
   TMatrixD a(4,4);
   b.ResizeTo(a);
   b = a;