Loading...
Searching...
No Matches
IO for matrices and vectors.
Iterative Solvers Template Library (ISTL) » Sparse Matrix and Vector classes

Provides methods for reading and writing matrices and vectors in various formats. More...

Collaboration diagram for IO for matrices and vectors.:

## Files

file | io.hh
| Some generic functions for pretty printing vectors and matrices.
file | matrixmarket.hh
| Provides classes for reading and writing MatrixMarket Files with an extension for parallel matrices.

## Namespaces

namespace | Dune::MatrixMarketImpl

## Classes

struct | Dune::DefaultSVGMatrixOptions
| Default options class to write SVG matrices. More...
class | Dune::MatrixMarketFormatError

## Functions

template<class V>
void | Dune::recursive_printvector (std::ostream &s, const V &v, std::string rowtext, int &counter, int columns, int width)
| Recursively print a vector.
template<class V>
void | Dune::printvector (std::ostream &s, const V &v, std::string title, std::string rowtext, int columns=1, int width=10, int precision=2)
| Print an ISTL vector.
void | Dune::fill_row (std::ostream &s, int m, int width, int precision)
| Print a row of zeros for a non-existing block.
template<class K, std::enable_if_t< Dune::IsNumber< K >::value, int > = 0>
void | Dune::print_row (std::ostream &s, const K &value, typename FieldMatrix< K, 1, 1 >::size_type I, typename FieldMatrix< K, 1, 1 >::size_type J, typename FieldMatrix< K, 1, 1 >::size_type therow, int width, int precision)
| Print one row of a matrix, specialization for number types.
template<class M, std::enable_if_t< not Dune::IsNumber< M >::value, int > = 0>
void | Dune::print_row (std::ostream &s, const M &A, typename M::size_type I, typename M::size_type J, typename M::size_type therow, int width, int precision)
| Print one row of a matrix.
template<class M>
void | Dune::printmatrix (std::ostream &s, const M &A, std::string title, std::string rowtext, int width=10, int precision=2)
| Print a generic block matrix.
template<class A, class InnerMatrixType>
void | Dune::printSparseMatrix (std::ostream &s, const BCRSMatrix< InnerMatrixType, A > &mat, std::string title, std::string rowtext, int width=3, int precision=2)
| Prints a BCRSMatrix with fixed sized blocks.
template<class FieldType, std::enable_if_t< Dune::IsNumber< FieldType >::value, int > = 0>
void | Dune::writeMatrixToMatlabHelper (const FieldType &value, int rowOffset, int colOffset, std::ostream &s)
| Helper method for the writeMatrixToMatlab routine.
template<class MatrixType, std::enable_if_t< not Dune::IsNumber< MatrixType >::value, int > = 0>
void | Dune::writeMatrixToMatlabHelper (const MatrixType &matrix, int externalRowOffset, int externalColOffset, std::ostream &s)
| Helper method for the writeMatrixToMatlab routine.
template<class MatrixType>
void | Dune::writeMatrixToMatlab (const MatrixType &matrix, const std::string &filename, int outputPrecision=18)
| Writes sparse matrix in a Matlab-readable format.
template<class V>
void | Dune::writeVectorToMatlabHelper (const V &v, std::ostream &stream)
template<class VectorType>
void | Dune::writeVectorToMatlab (const VectorType &vector, const std::string &filename, int outputPrecision=18)
| Writes vectors in a Matlab-readable format.
template<class Mat, class SVGOptions = DefaultSVGMatrixOptions>
void | Dune::writeSVGMatrix (std::ostream &out, const Mat &mat, SVGOptions opts={})
| Writes the visualization of matrix in the SVG format.
void | Dune::mm_read_header (std::size_t &rows, std::size_t &cols, MatrixMarketImpl::MMHeader &header, std::istream &istr, bool isVector)
template<typename T, typename A>
void | Dune::mm_read_vector_entries (Dune::BlockVector< T, A > &vector, std::size_t size, std::istream &istr, size_t lane)
template<typename T, typename A, int entries>
void | Dune::mm_read_vector_entries (Dune::BlockVector< Dune::FieldVector< T, entries >, A > &vector, std::size_t size, std::istream &istr, size_t lane)
template<typename T, typename A>
void | Dune::readMatrixMarket (Dune::BlockVector< T, A > &vector, std::istream &istr)
| Reads a BlockVector from a matrix market file.
template<typename T, typename A>
void | Dune::readMatrixMarket (Dune::BCRSMatrix< T, A > &matrix, std::istream &istr)
| Reads a sparse matrix from a matrix market file.
template<typename B>
void | Dune::mm_print_entry (const B &entry, std::size_t rowidx, std::size_t colidx, std::ostream &ostr)
template<typename V>
void | Dune::mm_print_vector_entry (const V &entry, std::ostream &ostr, const std::integral_constant< int, 1 > &, size_t lane)
template<typename V>
void | Dune::mm_print_vector_entry (const V &vector, std::ostream &ostr, const std::integral_constant< int, 0 > &, size_t lane)
template<typename T, typename A>
std::size_t | Dune::countEntries (const BlockVector< T, A > &vector)
template<typename T, typename A, int i>
std::size_t | Dune::countEntries (const BlockVector< FieldVector< T, i >, A > &vector)
template<typename V>
void | Dune::writeMatrixMarket (const V &vector, std::ostream &ostr, const std::integral_constant< int, 0 > &)
template<typename M>
void | Dune::writeMatrixMarket (const M &matrix, std::ostream &ostr, const std::integral_constant< int, 1 > &)
template<typename M>
void | Dune::writeMatrixMarket (const M &matrix, std::ostream &ostr)
| writes a ISTL matrix or vector to a stream in matrix market format.
template<typename M>
void | Dune::storeMatrixMarket (const M &matrix, std::string filename, int prec=default_precision)
| Stores a parallel matrix/vector in matrix market format in a file.
template<typename M, typename G, typename L>
void | Dune::storeMatrixMarket (const M &matrix, std::string filename, const OwnerOverlapCopyCommunication< G, L > &comm, bool storeIndices=true, int prec=default_precision)
| Stores a parallel matrix/vector in matrix market format in a file.
template<typename M, typename G, typename L>
void | Dune::loadMatrixMarket (M &matrix, const std::string &filename, OwnerOverlapCopyCommunication< G, L > &comm, bool readIndices=true)
| Load a parallel matrix/vector stored in matrix market format.
template<typename M>
void | Dune::loadMatrixMarket (M &matrix, const std::string &filename)
| Load a matrix/vector stored in matrix market format.

## Variables

static const int | Dune::default_precision = -1

## Detailed Description

Provides methods for reading and writing matrices and vectors in various formats.

Routine printmatrix prints a (sparse matrix with all entries (even zeroes). Function printvector prints a vector to a stream. PrintSparseMatrix prints a sparse matrix omitting all nonzeroes. With writeMatrixToMatlab one can write a matrix in a Matlab readable format. Using storeMartrixMarket and loadMatrixMarket one can store and load a parallel ISTL matrix in MatrixMarket format. The latter can even read a matrix written with writeMatrixToMatlab.

## Function Documentation

## countEntries() [1/2]

template<typename T, typename A, int i>

std::size_t Dune::countEntries | ( | const BlockVector< FieldVector< T, i >, A > & | vector | ) |

## countEntries() [2/2]

template<typename T, typename A>

std::size_t Dune::countEntries | ( | const BlockVector< T, A > & | vector | ) |

## fill_row()

void Dune::fill_row | ( | std::ostream & | s,
|  | int | m,
|  | int | width,
|  | int | precision )

inline

Print a row of zeros for a non-existing block.

#include <dune/istl/io.hh>
io.hh
Some generic functions for pretty printing vectors and matrices.

## loadMatrixMarket() [1/2]

template<typename M>

void Dune::loadMatrixMarket | ( | M & | matrix,
|  | const std::string & | filename )

Load a matrix/vector stored in matrix market format.

More about the matrix market exchange format can be found here.

Parameters

  matrix | Where to store the matrix/vector.
  filename | the name of the filename (without suffix and rank!) rank i will read the file filename_i.mm

## loadMatrixMarket() [2/2]

template<typename M, typename G, typename L>

void Dune::loadMatrixMarket | ( | M & | matrix,
|  | const std::string & | filename,
|  | OwnerOverlapCopyCommunication< G, L > & | comm,
|  | bool | readIndices = true )

Load a parallel matrix/vector stored in matrix market format.

More about the matrix market exchange format can be found here.

Parameters

  matrix | Where to store the matrix/vector.
  filename | the name of the filename (without suffix and rank!) rank i will read the file filename_i.mm
  comm | The information about the data distribution.
  readIndices | Whether to read the parallel index information. If true rank i reads the index information form file filename_i.idx And builds the remote indices information.

## mm_print_entry()

template<typename B>

void Dune::mm_print_entry | ( | const B & | entry,
|  | std::size_t | rowidx,
|  | std::size_t | colidx,
|  | std::ostream & | ostr )

## mm_print_vector_entry() [1/2]

template<typename V>

void Dune::mm_print_vector_entry | ( | const V & | entry,
|  | std::ostream & | ostr,
|  | const std::integral_constant< int, 1 > & | ,
|  | size_t | lane )

## mm_print_vector_entry() [2/2]

template<typename V>

void Dune::mm_print_vector_entry | ( | const V & | vector,
|  | std::ostream & | ostr,
|  | const std::integral_constant< int, 0 > & | ,
|  | size_t | lane )

## mm_read_header()

void Dune::mm_read_header | ( | std::size_t & | rows,
|  | std::size_t & | cols,
|  | MatrixMarketImpl::MMHeader & | header,
|  | std::istream & | istr,
|  | bool | isVector )

inline

## mm_read_vector_entries() [1/2]

template<typename T, typename A, int entries>

void Dune::mm_read_vector_entries | ( | Dune::BlockVector< Dune::FieldVector< T, entries >, A > & | vector,
|  | std::size_t | size,
|  | std::istream & | istr,
|  | size_t | lane )

## mm_read_vector_entries() [2/2]

template<typename T, typename A>

void Dune::mm_read_vector_entries | ( | Dune::BlockVector< T, A > & | vector,
|  | std::size_t | size,
|  | std::istream & | istr,
|  | size_t | lane )

## print_row() [1/2]

template<class K, std::enable_if_t< Dune::IsNumber< K >::value, int > = 0>

void Dune::print_row | ( | std::ostream & | s,
|  | const K & | value,
|  | typename FieldMatrix< K, 1, 1 >::size_type | I,
|  | typename FieldMatrix< K, 1, 1 >::size_type | J,
|  | typename FieldMatrix< K, 1, 1 >::size_type | therow,
|  | int | width,
|  | int | precision )

Print one row of a matrix, specialization for number types.

#include <dune/istl/io.hh>

## print_row() [2/2]

template<class M, std::enable_if_t< not Dune::IsNumber< M >::value, int > = 0>

void Dune::print_row | ( | std::ostream & | s,
|  | const M & | A,
|  | typename M::size_type | I,
|  | typename M::size_type | J,
|  | typename M::size_type | therow,
|  | int | width,
|  | int | precision )

Print one row of a matrix.

#include <dune/istl/io.hh>

## printmatrix()

template<class M>

void Dune::printmatrix | ( | std::ostream & | s,
|  | const M & | A,
|  | std::string | title,
|  | std::string | rowtext,
|  | int | width = 10,
|  | int | precision = 2 )

Print a generic block matrix.

#include <dune/istl/io.hh>

Bug
  Empty rows and columns are omitted by this method. (FlySpray #7)

## printSparseMatrix()

template<class A, class InnerMatrixType>

void Dune::printSparseMatrix | ( | std::ostream & | s,
|  | const BCRSMatrix< InnerMatrixType, A > & | mat,
|  | std::string | title,
|  | std::string | rowtext,
|  | int | width = 3,
|  | int | precision = 2 )

Prints a BCRSMatrix with fixed sized blocks.

#include <dune/istl/io.hh>

Only the nonzero entries will be printed as matrix blocks together with their corresponding column index and all others will be omitted.

This might be preferable over printmatrix in the case of big sparse matrices with nonscalar blocks.

Parameters

  s | The ostream to print to.
  mat | The matrix to print.
  title | The title for the matrix.
  rowtext | The text to prepend to each print out of a matrix row.
  width | The number of nonzero blocks to print in one line.
  precision | The precision to use when printing the numbers.

## printvector()

template<class V>

void Dune::printvector | ( | std::ostream & | s,
|  | const V & | v,
|  | std::string | title,
|  | std::string | rowtext,
|  | int | columns = 1,
|  | int | width = 10,
|  | int | precision = 2 )

Print an ISTL vector.

#include <dune/istl/io.hh>

## readMatrixMarket() [1/2]

template<typename T, typename A>

void Dune::readMatrixMarket | ( | Dune::BCRSMatrix< T, A > & | matrix,
|  | std::istream & | istr )

Reads a sparse matrix from a matrix market file.

Parameters

  matrix | The matrix to store the data in.
  istr | The input stream to read the data from.

Warning
  Not all formats are supported!

## readMatrixMarket() [2/2]

template<typename T, typename A>

void Dune::readMatrixMarket | ( | Dune::BlockVector< T, A > & | vector,
|  | std::istream & | istr )

Reads a BlockVector from a matrix market file.

Parameters

  vector | The vector to store the data in.
  istr | The input stream to read the data from.

Warning
  Not all formats are supported!

## recursive_printvector()

template<class V>

void Dune::recursive_printvector | ( | std::ostream & | s,
|  | const V & | v,
|  | std::string | rowtext,
|  | int & | counter,
|  | int | columns,
|  | int | width )

Recursively print a vector.

#include <dune/istl/io.hh>

## storeMatrixMarket() [1/2]

template<typename M, typename G, typename L>

void Dune::storeMatrixMarket | ( | const M & | matrix,
|  | std::string | filename,
|  | const OwnerOverlapCopyCommunication< G, L > & | comm,
|  | bool | storeIndices = true,
|  | int | prec = default_precision )

Stores a parallel matrix/vector in matrix market format in a file.

More about the matrix market exchange format can be found here.

Parameters

  matrix | The matrix/vector to store.
  filename | the name of the filename (without suffix and rank!) rank i will write the file filename_i.mm
  comm | The information about the data distribution.
  storeIndices | Whether to store the parallel index information. If true rank i writes the index information to file filename_i.idx.
  prec | Set the decimal precision to be used

## storeMatrixMarket() [2/2]

template<typename M>

void Dune::storeMatrixMarket | ( | const M & | matrix,
|  | std::string | filename,
|  | int | prec = default_precision )

Stores a parallel matrix/vector in matrix market format in a file.

More about the matrix market exchange format can be found here.

Parameters

  matrix | The matrix/vector to store.
  filename | the name of the filename (without suffix and rank!) rank i will write the file filename_i.mm
  prec | Set the decimal precision to be used

## writeMatrixMarket() [1/3]

template<typename M>

void Dune::writeMatrixMarket | ( | const M & | matrix,
|  | std::ostream & | ostr )

writes a ISTL matrix or vector to a stream in matrix market format.

## writeMatrixMarket() [2/3]

template<typename M>

void Dune::writeMatrixMarket | ( | const M & | matrix,
|  | std::ostream & | ostr,
|  | const std::integral_constant< int, 1 > & | )

## writeMatrixMarket() [3/3]

template<typename V>

void Dune::writeMatrixMarket | ( | const V & | vector,
|  | std::ostream & | ostr,
|  | const std::integral_constant< int, 0 > & | )

## writeMatrixToMatlab()

template<class MatrixType>

void Dune::writeMatrixToMatlab | ( | const MatrixType & | matrix,
|  | const std::string & | filename,
|  | int | outputPrecision = 18 )

Writes sparse matrix in a Matlab-readable format.

#include <dune/istl/io.hh>

This routine writes the argument BCRSMatrix to a file with the name given by the filename argument. The file format is ASCII, with no header, and three data columns. Each row describes a scalar matrix entry and consists of the matrix row and column numbers (both counted starting from 1), and the matrix entry. Such a file can be read from Matlab using the command

new_mat = spconvert(load('filename'));

Parameters

  matrix | reference to matrix
  filename |
  outputPrecision | (number of digits) which is used to write the output file

## writeMatrixToMatlabHelper() [1/2]

template<class FieldType, std::enable_if_t< Dune::IsNumber< FieldType >::value, int > = 0>

void Dune::writeMatrixToMatlabHelper | ( | const FieldType & | value,
|  | int | rowOffset,
|  | int | colOffset,
|  | std::ostream & | s )

Helper method for the writeMatrixToMatlab routine.

#include <dune/istl/io.hh>

This specialization for numbers ends the recursion

## writeMatrixToMatlabHelper() [2/2]

template<class MatrixType, std::enable_if_t< not Dune::IsNumber< MatrixType >::value, int > = 0>

void Dune::writeMatrixToMatlabHelper | ( | const MatrixType & | matrix,
|  | int | externalRowOffset,
|  | int | externalColOffset,
|  | std::ostream & | s )

Helper method for the writeMatrixToMatlab routine.

#include <dune/istl/io.hh>

## writeSVGMatrix()

template<class Mat, class SVGOptions = DefaultSVGMatrixOptions>

void Dune::writeSVGMatrix | ( | std::ostream & | out,
|  | const Mat & | mat,
|  | SVGOptions | opts = {} )

Writes the visualization of matrix in the SVG format.

The default visualization writes a rectangle for every block bounding box. This is enough to visualize patterns. If you need a more advance SVG object in each matrix block (e.g. color scale, or write the value in text), just provide a custom SVGOptions that fulfills the DefaultSVGMatrixOptions interface.

Template Parameters

  Mat | Matrix type to write
  SVGOptions | Options object type (see DefaultSVGMatrixOptions)

Parameters

  mat | The matrix to write
  out | A output stream to write SVG to
  opts | SVG Options object

## writeVectorToMatlab()

template<class VectorType>

void Dune::writeVectorToMatlab | ( | const VectorType & | vector,
|  | const std::string & | filename,
|  | int | outputPrecision = 18 )

Writes vectors in a Matlab-readable format.

#include <dune/istl/io.hh>

This routine writes the argument block vector to a file with the name given by the filename argument. The file format is ASCII, with no header, and a single data column. Such a file can be read from Matlab using the command

new_vec = load('filename');

Parameters

  vector | reference to vector to be printed to output file
  filename | filename of output file
  outputPrecision | (number of digits) which is used to write the output file

## writeVectorToMatlabHelper()

template<class V>

void Dune::writeVectorToMatlabHelper | ( | const V & | v,
|  | std::ostream & | stream )

## Variable Documentation

## default_precision

const int Dune::default_precision = -1

static