Direct Access to Data
Advanced users may need to access the data stored in a matrix directly. This is especially important if you plan to interface a matrix type with another language, such as C or Fortran. The datavec() member function is provided for this purpose in all matrices except general matrices. To access the data in a general matrix, see the Essential Math Module documentation. The rest of this section describes matrix types other than general matrices.
The data in a matrix is stored using the vector classes of the Essential Math Module. The storage pattern for each matrix shape is described in detail in the SourcePro API Reference Guide. The member function datavec() returns the vector holding the data. The following simple program illustrates how the data is arranged in a tridiagonal matrix:
 
#include <iostream>
#include <rw/lapack/trdgmat.h>
 
int main()
{
RWTriDiagMat<double> A(4,4); // 1
RWMathVec<double> Adata = A.dataVec(); // 2
Adata = RWMathVec<double>(Adata.length(),1,1); // 3
std::cout << A << std::endl; // 4
 
return 0;
}
Here is a description of what this program does:
//1 This line declares A to be a 4 x 4 tridiagonal matrix.
//2 This line accesses the data vector used to store the entries of A. This gives you a vector that references the same data as A.
//3 This line sets the data vector equal to a vector of the same length containing the elements 1,2,3,...
//4 This line prints the matrix. The entries are numbered according to their ordering in the data vector.
This program can be used to show you how the data is stored for any matrix type.
Sometimes it is useful to operate only on elements of a matrix that you know are explicitly stored. For example, you may want to set all elements of a banded matrix within the nonzero band equal to 1. The following shows how you could do this:
 
#include <rw/lapack/bandmat.h>
 
int main()
{
RWBandMat<float> A(8,8,2,1); // 1
// A = 1 illegal // 2
A.dataVec() = 1; // 3
 
return 0;
}
Line //1 defines A to be an 8 x 8 banded matrix with an upper bandwidth of 2 and a lower bandwidth of 1. Line //2 in the program would not be legal; there is no operator=(float) function defined for a RWBandMat<float>. This is because setting the matrix equal to 1 would imply that every element in the matrix be set equal to 1. This is impossible because, for the matrix to remain banded, some of the entries are defined to be 0. Line //3 shows how to set all entries in the nonzero band to 1. The dataVec() function returns a reference to the vector used to store the nonzero elements. By setting this vector to 1, you can set all nonzero elements to 1.