



libscuff provides some simple classes
called
This page documents a representative subset of the
matrix and vector functions provided; for the full API,
consult the file
1. Creating from scratchCreate realvalued matrices and vectors of known dimensions:
HVector *V = new HVector( 13); // create a 13component realvalued vector HMatrix *M = new HMatrix( 5, 6); // create a 5x6 realvalued matrix Create complexvalued matrices and vectors:
HVector *V = new HVector( 13, LHM_COMPLEX); HMatrix *M = new HMatrix( 5, 6, LHM_COMPLEX); For symmetric or Hermitian matrices, you have the option of using packed storage; this roughly halves the RAM needed to store your matrix, but has the drawback of slowing down linear algebra operations.
// real symmetric matrix, M_{ij} = M_{ji} HMatrix *M = new HMatrix( 5, 5, LHM_REAL, LHM_SYMMETRIC ); // complex hermitian matrix, M_{ij} = M^*_{ji} HMatrix *M = new HMatrix( 5, 5, LHM_COMPLEX, LHM_HERMITIAN ); // complex symmetric matrix, M_{ij} = M_{ji} HMatrix *M = new HMatrix( 5, 5, LHM_COMPLEX, LHM_SYMMETRIC );
2. Importing from text or binary files
Text files:Create a new vector or matrix by importing a list of numbers in an ASCII text file:
HVector *V = new HVector("MyVector.dat", LHM_TEXT ); if (V>ErrMsg) ErrExit(V>ErrMsg); HVector *M = new HMatrix("MyMatrix.dat", LHM_TEXT ); if (M>ErrMsg) ErrExit(M>ErrMsg);
Here
Note that, if the file import operation fails,
the constructor returns an
If the operation is successful, then
HDF5 files:Create a new vector or matrix by importing from an HDF5 binary data file:
HMatrix *M1 = new HMatrix("MyFile.hdf5", LHM_HDF5, "M1"); HMatrix *M2 = new HMatrix("MyFile.hdf5", LHM_HDF5, "M2"); HVector *V = new HVector("MyFile.hdf5", LHM_HDF5, "V"); if (M1>ErrMsg) ErrExit(M1>ErrMsg); if (M2>ErrMsg) ErrExit(M2>ErrMsg); if (V>ErrMsg) ErrExit(V>ErrMsg); Note that the third parameter to the constructor here is the label of the dataset to within the HDF5 file.
3. Exporting to text or binary files
Text files:Write the contents of a vector or matrix to an ASCII text file:
M>ExportToText("MyMatrix.dat"); V>ExportToText("MyVector.dat");
The argument to int N = 3; M>ExportToText("Matrix_%i.dat", N);
HDF5 files:There are two calling conventions for exporting matrices and vectors to HDF5 files. If you want each matrix and vector to be exported to a separate HDF5 file, you can say simply
M>ExportToHDF5("MyMatrix.hdf5","M"); V>ExportToHDF5("MyVector.hdf5","V"); Alternatively, you can write multiple matrices and vectors to a single HDF5 file.
void *HC = HMatrix::OpenHDF5Context("MyFile.hdf5"); M>ExportToHDF5(HC, "M"); V>ExportToHDF5(HC, "V"); HMatrix::CloseHDF5Context(HC);
4. Simple manipulationsNote: All indices are zerobased. Get or set individual entries:
HMatrix *M = new HMatrix(M, N, LHM_COMPLEX); HVector *V = new HVector(N); M>SetEntry( 3, 4, 5.6 ); M>SetEntry( 3, 4, cdouble(5.6+7.8) ); V>SetEntry( 7, 8.009 ); double D; cdouble Z; Z = M>GetEntry( 3, 4 ); D = M>GetEntryD( 3, 4 ); // discard any imaginary part Z = V>GetEntry(7); D = V>GetEntryD(8); Augment individual entries:
M>AddEntry( 3, 4, 5.6 ); // the (3,4) entry gets increased by 5.6 V>AddEntry( 0, cdouble(0.0, 2.3) ); // the 0th entry gets increased by 2.3i Replace a matrix with its conjugate or nonconjugate tranpose (no distinction for realvalued matrices):
M>Adjoint(); // conjugate transpose M>Transpose(); // nonconjugate transpose
5. Numerical linear algebra: LAPACK/BLAS wrappersMultiply two matrices:
int P, Q, R; A=new HMatrix(P, Q); B=new HMatrix(Q, R); C=new HMatrix(P, R); ... A>Multiply(B, C); // set C = A*B Replace a matrix with its LU factorization:
M>LUFactorize(); Solve a single linear system using an LUfactorized matrix:
M=new HMatrix(N, N); // insert code to fill in M M>LUFactorize(); V=new HVector(N); // insert code to fill in V M>LUSolve(V); // replaces V with M^{1} * V
Of course, we only need to call
We can also solve multiple simultaneous systems by passing
an
M=new HMatrix(N, N); // insert code to fill in M M>LUFactorize(); R=new HMatrix(N, M); // insert code to fill in the M columns of R M>LUSolve(R); // replaces R with M^{1} * R

libscuff documentation: Matrices and Vectors, by Homer Reid 

Last Modified: 11/16/16 