This Tutorials shows the general use of matfiles in Concepts. A tool that allows yourself to store concepts::DenseMatrix<T> and concepts::SparseMatrix<T> in a binary format, which can be read with Matlab/Octave or MatfileInput to regain matrices in Concepts.
Include files for input/output handling and matrices.
#include "matfile.hh" #include "operator.hh"
In the first lines some matrices for later handling are initialisized.
uint ddimX = 3;
uint ddimY = 3;
uint sdimX = 4;
uint sdimY = 5;
concepts::DenseMatrix<Cmplx> dCmplx(ddimX,ddimY);
concepts::SparseMatrix<Real> sReal(sdimX,sdimY);
for(uint i=0; i<ddimX; ++i){
for(uint j=0; j<ddimY; ++j){
dCmplx(i,j) = std::complex<double>(1,1);
}
}
for(uint i=0;i<sdimX;++i)
sReal(i,i)=i;
First one has to create a concepts::MatfileOutput with a string as input, that will be the name of the matfile. The methods addDense and addSparse expect a reference of a matrix and a string, which will be the name of the matrix in the matfile.
concepts::MatfileOutput mo("File"); mo.addDense(&dCmplx,"dense_cmplx"); mo.addSparse(&sReal, "sparse_real"); mo.closeMatfile();
In order to regain matrices one has to create a concepts::MatfileInput with a string as input, that is the name of the matfile. The methods getDense<T> and getSparse<T> expect a type T, that is either real or complex. As input the name of the matrix in the matfile is needed. Methods return pointers to the matrices.
concepts::MatfileInput mi("File"); concepts::DenseMatrix<Cmplx>* dCmplx_correct_ptr = mi.getDense<Cmplx>("dense_cmplx"); concepts::SparseMatrix<Real>* sReal_ptr = mi.getSparse<Real>("sparse_real");
Be sure to know the field over which the matrices are defined. Else some data will be lost (compare to Output ).
concepts::DenseMatrix<Real>* dcmplx_wrong_ptr = mi.getDense<Real>("dense_cmplx");
Output of the main. Notice the lost data in the second matrix.
$ ./matfileTutorial DenseMatrix (correct) Output: DenseMatrix(3x3, [ 1.000000e+00+i 1.000000e+00+i 1.000000e+00+i 1.000000e+00+i 1.000000e+00+i 1.000000e+00+i 1.000000e+00+i 1.000000e+00+i 1.000000e+00+i]) DenseMatrix (wrong) Output: DenseMatrix(3x3, [ 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00 1.000000e+00]) SparseMatrix Output: 1.000000e+00, 2.000000e+00, 3.000000e+00,
#include "basics/exceptions.hh" #include "matfile.hh" #include "operator.hh" using concepts::Real; using concepts::Cmplx; int main(int argc, char** argv) { concepts::Stacktrace::doit() = false; try { // ** preparations ** uint ddimX = 3; uint ddimY = 3; uint sdimX = 4; uint sdimY = 5; concepts::DenseMatrix<Cmplx> dCmplx(ddimX,ddimY); concepts::SparseMatrix<Real> sReal(sdimX,sdimY); for(uint i=0; i<ddimX; ++i){ for(uint j=0; j<ddimY; ++j){ dCmplx(i,j) = std::complex<double>(1,1); } } for(uint i=0;i<sdimX;++i) sReal(i,i)=i; // ** write matrices to matfile ** concepts::MatfileOutput mo("File"); mo.addDense(&dCmplx,"dense_cmplx"); mo.addSparse(&sReal, "sparse_real"); mo.closeMatfile(); // ** read matrices from matfile ** concepts::MatfileInput mi("File"); concepts::DenseMatrix<Cmplx>* dCmplx_correct_ptr = mi.getDense<Cmplx>("dense_cmplx"); concepts::SparseMatrix<Real>* sReal_ptr = mi.getSparse<Real>("sparse_real"); // ** read matrices from matfile incorrectly ** concepts::DenseMatrix<Real>* dcmplx_wrong_ptr = mi.getDense<Real>("dense_cmplx"); // ** output of program ** std::cout << "DenseMatrix (correct) Output:" << std::endl; std::cout << *dCmplx_correct_ptr << std::endl; std::cout << "DenseMatrix (wrong) Output:" << std::endl; std::cout << *dcmplx_wrong_ptr << std::endl; std::cout << "SparseMatrix Output:" << std::endl; for(concepts::SparseMatrix<Real>::const_iterator iter=sReal_ptr->begin();iter!=sReal_ptr->end();++iter) std::cout << *iter << ", "; std::cout << std::endl; } catch(concepts::ExceptionBase& e) { std::cout << e << std::endl; return 1; } }