Template Numerical Library: TNL::Matrices::MatrixWriter< Matrix, Device > Class Template Reference

Template Numerical Library version\ main:be7e16d88

Loading...
Searching...
No Matches
Public Types | Static Public Member Functions | Protected Types | List of all members
TNL::Matrices::MatrixWriter< Matrix, Device > Class Template Reference

Helper class for exporting of matrices to different output formats. More...

`#include <TNL/Matrices/MatrixWriter.h>`

## Public Types
using | DeviceType = typename Matrix::RealType
| Device where the matrix is allocated.
using | IndexType = typename Matrix::IndexType
| Type used for indexing of matrix elements.
using | RealType = typename Matrix::RealType
| Type of matrix elements values.

## Static Public Member Functions
static void | writeEps (const std::string &fileName, const Matrix &matrix, bool verbose=false)
| Method for exporting matrix to file with given filename using EPS format.
static void | writeEps (std::ostream &str, const Matrix &matrix, bool verbose=false)
| Method for exporting matrix to STL output stream using EPS format.
static void | writeGnuplot (const std::string &fileName, const Matrix &matrix, bool verbose=false)
| Method for exporting matrix to file with given filename using Gnuplot format.
static void | writeGnuplot (std::ostream &str, const Matrix &matrix, bool verbose=false)
| Method for exporting matrix to STL output stream using Gnuplot format.
static void | writeMtx (const std::string &fileName, const Matrix &matrix, bool verbose=false)
| Method for exporting matrix to file with given filename using MTX format.
static void | writeMtx (std::ostream &str, const Matrix &matrix, bool verbose=false)
| Method for exporting matrix to STL output stream using MTX format.

## Protected Types
using | HostMatrix = typename Matrix::template Self< RealType, TNL::Devices::Host >

## Detailed Description

template<typename Matrix, typename Device = typename Matrix::DeviceType>
class TNL::Matrices::MatrixWriter< Matrix, Device >

Helper class for exporting of matrices to different output formats.

Currently it supports:

1. Coordinate MTX Format is supported.
2. Gnuplot format for matrix visualization in Gnuplot.
3. EPS format for matrix pattern visualization in Encapsulated PostScript

Template Parameters

  Matrix | is a type of matrix into which we want to import the MTX file.
  Device | is used only for the purpose of template specialization.

Example
  #include <iostream>
  #include <TNL/Matrices/SparseMatrix.h>
  #include <TNL/Matrices/DenseMatrix.h>
  #include <TNL/Matrices/MatrixReader.h>
  #include <TNL/Matrices/MatrixWriter.h>
  #include <TNL/Devices/Host.h>
  #include <TNL/Devices/Cuda.h>
  template< typename Device >
  void
  matrixWriterExample()
  {
  using Matrix = TNL::Matrices::SparseMatrix< double, Device >;
  Matrix matrix( 5, // number of matrix rows
  5, // number of matrix columns
  {
  // matrix elements definition
  // clang-format off
  { 0, 0, 2.0 },
  { 1, 0, -1.0 }, { 1, 1, 2.0 }, { 1, 2, -1.0 },
  { 2, 1, -1.0 }, { 2, 2, 2.0 }, { 2, 3, -1.0 },
  { 3, 2, -1.0 }, { 3, 3, 2.0 }, { 3, 4, -1.0 },
  { 4, 4, 2.0 },
  // clang-format on
  } );
  std::cout << "Matrix:\n" << matrix << '\n';
  std::cout << "Writing matrix in Gnuplot format into the file matrix-writer-example.gplt ...";
  TNL::Matrices::MatrixWriter< Matrix >::writeGnuplot( "matrix-writer-example.gplt", matrix );
  std::cout << " OK\n";
  std::cout << "Writing matrix pattern in EPS format into the file matrix-writer-example.eps ...";
  TNL::Matrices::MatrixWriter< Matrix >::writeEps( "matrix-writer-example.eps", matrix );
  std::cout << " OK\n";
  std::cout << "Writing matrix in MTX format into the file matrix-writer-example.mtx ...";
  TNL::Matrices::MatrixWriter< Matrix >::writeMtx( "matrix-writer-example.mtx", matrix );
  std::cout << " OK\n";
  }
  template< typename Device >
  void
  matrixReaderExample()
  {
  using SparseMatrix = TNL::Matrices::SparseMatrix< double, Device >;
  SparseMatrix sparseMatrix;
  std::cout << "Reading sparse matrix from MTX file matrix-writer-example.mtx ... ";
  TNL::Matrices::MatrixReader< SparseMatrix >::readMtx( "matrix-writer-example.mtx", sparseMatrix );
  std::cout << " OK\n";
  std::cout << "Imported matrix is:\n" << sparseMatrix << '\n';
  using DenseMatrix = TNL::Matrices::DenseMatrix< double, Device >;
  DenseMatrix denseMatrix;
  std::cout << "Reading dense matrix from MTX file matrix-writer-example.mtx ... ";
  TNL::Matrices::MatrixReader< DenseMatrix >::readMtx( "matrix-writer-example.mtx", denseMatrix );
  std::cout << " OK\n";
  std::cout << "Imported matrix is:\n" << denseMatrix << '\n';
  }
  int
  main( int argc, char* argv[] )
  {
  std::cout << "Creating matrices on CPU ...\n";
  matrixWriterExample< TNL::Devices::Host >();
  matrixReaderExample< TNL::Devices::Host >();
  #ifdef __CUDACC__
  std::cout << "\n\n";
  std::cout << "Creating matrices on CUDA GPU ...\n";
  matrixWriterExample< TNL::Devices::Cuda >();
  matrixReaderExample< TNL::Devices::Cuda >();
  #endif
  }
  std::cout
  TNL::Matrices::DenseMatrix
  Implementation of dense matrix, i.e. matrix storing explicitly all of its elements including zeros.
  Definition DenseMatrix.h:30
  TNL::Matrices::MatrixReader::readMtx
  static void readMtx(const std::string &fileName, Matrix &matrix, bool verbose=false)
  Method for importing matrix from file with given filename.
  TNL::Matrices::MatrixWriter::writeGnuplot
  static void writeGnuplot(const std::string &fileName, const Matrix &matrix, bool verbose=false)
  Method for exporting matrix to file with given filename using Gnuplot format.
  TNL::Matrices::MatrixWriter::writeMtx
  static void writeMtx(const std::string &fileName, const Matrix &matrix, bool verbose=false)
  Method for exporting matrix to file with given filename using MTX format.
  TNL::Matrices::MatrixWriter::writeEps
  static void writeEps(const std::string &fileName, const Matrix &matrix, bool verbose=false)
  Method for exporting matrix to file with given filename using EPS format.
  TNL::Matrices::SparseMatrix
  Implementation of sparse matrix, i.e. matrix storing only non-zero elements.
  Definition SparseMatrix.h:55

Output
  Creating matrices on CPU ...
  Matrix:
  Row: 0 -> 0:2
  Row: 1 -> 0:-1 1:2 2:-1
  Row: 2 -> 1:-1 2:2 3:-1
  Row: 3 -> 2:-1 3:2 4:-1
  Row: 4 -> 4:2
  Writing matrix in Gnuplot format into the file matrix-writer-example.gplt ... OK
  Writing matrix pattern in EPS format into the file matrix-writer-example.eps ... OK
  Writing matrix in MTX format into the file matrix-writer-example.mtx ... OK
  Reading sparse matrix from MTX file matrix-writer-example.mtx ... OK
  Imported matrix is:
  Row: 0 -> 0:2
  Row: 1 -> 0:-1 1:2 2:-1
  Row: 2 -> 1:-1 2:2 3:-1
  Row: 3 -> 2:-1 3:2 4:-1
  Row: 4 -> 4:2
  Reading dense matrix from MTX file matrix-writer-example.mtx ... OK
  Imported matrix is:
  Row: 0 -> 0:2 1:0 2:0 3:0 4:0
  Row: 1 -> 0:-1 1:2 2:-1 3:0 4:0
  Row: 2 -> 0:0 1:-1 2:2 3:-1 4:0
  Row: 3 -> 0:0 1:0 2:-1 3:2 4:-1
  Row: 4 -> 0:0 1:0 2:0 3:0 4:2

## Member Function Documentation

## ◆ writeEps() [1/2]

template<typename Matrix, typename Device = typename Matrix::DeviceType>

void TNL::Matrices::MatrixWriter< Matrix, Device >::writeEps | ( | const std::string & | fileName,
|  | const Matrix & | matrix,
|  | bool | verbose = false )
| static

Method for exporting matrix to file with given filename using EPS format.

Parameters

  fileName | is the name of the target file.
  matrix | is the source matrix.
  verbose | controls verbosity of the matrix export.

## ◆ writeEps() [2/2]

template<typename Matrix, typename Device = typename Matrix::DeviceType>

void TNL::Matrices::MatrixWriter< Matrix, Device >::writeEps | ( | std::ostream & | str,
|  | const Matrix & | matrix,
|  | bool | verbose = false )
| static

Method for exporting matrix to STL output stream using EPS format.

Parameters

  str | is the output stream.
  matrix | is the source matrix.
  verbose | controls verbosity of the matrix export.

## ◆ writeGnuplot() [1/2]

template<typename Matrix, typename Device = typename Matrix::DeviceType>

void TNL::Matrices::MatrixWriter< Matrix, Device >::writeGnuplot | ( | const std::string & | fileName,
|  | const Matrix & | matrix,
|  | bool | verbose = false )
| static

Method for exporting matrix to file with given filename using Gnuplot format.

Parameters

  fileName | is the name of the target file.
  matrix | is the source matrix.
  verbose | controls verbosity of the matrix export.

## ◆ writeGnuplot() [2/2]

template<typename Matrix, typename Device = typename Matrix::DeviceType>

void TNL::Matrices::MatrixWriter< Matrix, Device >::writeGnuplot | ( | std::ostream & | str,
|  | const Matrix & | matrix,
|  | bool | verbose = false )
| static

Method for exporting matrix to STL output stream using Gnuplot format.

Parameters

  str | is the output stream.
  matrix | is the source matrix.
  verbose | controls verbosity of the matrix export.

## ◆ writeMtx() [1/2]

template<typename Matrix, typename Device = typename Matrix::DeviceType>

void TNL::Matrices::MatrixWriter< Matrix, Device >::writeMtx | ( | const std::string & | fileName,
|  | const Matrix & | matrix,
|  | bool | verbose = false )
| static

Method for exporting matrix to file with given filename using MTX format.

Parameters

  fileName | is the name of the target file.
  matrix | is the source matrix.
  verbose | controls verbosity of the matrix export.

## ◆ writeMtx() [2/2]

template<typename Matrix, typename Device = typename Matrix::DeviceType>

void TNL::Matrices::MatrixWriter< Matrix, Device >::writeMtx | ( | std::ostream & | str,
|  | const Matrix & | matrix,
|  | bool | verbose = false )
| static

Method for exporting matrix to STL output stream using MTX format.

Parameters

  str | is the output stream.
  matrix | is the source matrix.
  verbose | controls verbosity of the matrix export.

The documentation for this class was generated from the following file:

- src/TNL/Matrices/MatrixWriter.h