Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_MatrixMarket_Raw_Writer.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Tpetra: Templated Linear Algebra Services Package
4 //
5 // Copyright 2008 NTESS and the Tpetra contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef __Teuchos_MatrixMarket_Raw_Writer_hpp
11 #define __Teuchos_MatrixMarket_Raw_Writer_hpp
12 
14 #include "Teuchos_ArrayView.hpp"
16 #include <fstream>
17 #include <iostream>
18 
19 namespace Teuchos {
20  namespace MatrixMarket {
21  namespace Raw {
34  template<class ScalarType, class OrdinalType>
35  class Writer {
36  public:
58  void
59  writeFile (const std::string& filename,
60  const ArrayView<const OrdinalType>& rowptr,
61  const ArrayView<const OrdinalType>& colind,
62  const ArrayView<const ScalarType>& values,
63  const OrdinalType numRows,
64  const OrdinalType numCols)
65  {
66  std::ofstream out (filename.c_str ());
67  TEUCHOS_TEST_FOR_EXCEPTION(! out, std::runtime_error,
68  "Failed to open file \"" << filename << "\" for writing.");
69  write (out, rowptr, colind, values, numRows, numCols);
70  }
71 
97  void
98  write (std::ostream& out,
99  const ArrayView<const OrdinalType>& rowptr,
100  const ArrayView<const OrdinalType>& colind,
101  const ArrayView<const ScalarType>& values,
102  const OrdinalType numRows,
103  const OrdinalType numCols)
104  {
105  using std::endl;
106  typedef ScalarTraits<ScalarType> STS;
107  typedef typename ArrayView<const OrdinalType>::size_type size_type;
108 
109  // Make the output stream write floating-point numbers in
110  // scientific notation. It will politely put the output
111  // stream back to its state on input, when this scope
112  // terminates.
114 
115  // Data type string for ScalarType.
116  std::string dataType;
117  if (STS::isComplex) {
118  dataType = "complex";
119  } else if (STS::isOrdinal) {
120  dataType = "integer";
121  } else {
122  dataType = "real";
123  }
124 
125  // Print the Matrix Market banner line. We assume
126  // nonsymmetric storage ("general").
127  out << "%%MatrixMarket matrix coordinate " << dataType << " general"
128  << endl;
129 
130  // // Print comments (the matrix name and / or description).
131  // if (matrixName != "") {
132  // printAsComment (out, matrixName);
133  // }
134  // if (matrixDescription != "") {
135  // printAsComment (out, matrixDescription);
136  // }
137 
138  // Write the dimensions of the sparse matrix: (# rows, #
139  // columns, # matrix entries (counting duplicates as
140  // separate entries)).
141  out << numRows << " " << numCols << " " << rowptr[numRows] << endl;
142 
143  for (size_type i = 0; i < numRows; ++i) {
144  for (OrdinalType k = rowptr[i]; k < rowptr[i+1]; ++k) {
145  const OrdinalType j = colind[k];
146  const ScalarType& A_ij = values[k];
147 
148  // Matrix Market files use 1-based row and column indices.
149  out << (i+1) << " " << (j+1) << " ";
150  if (STS::isComplex) {
151  out << STS::real (A_ij) << " " << STS::imag (A_ij);
152  } else {
153  out << A_ij;
154  }
155  out << endl;
156  }
157  }
158  }
159  }; // end of class Writer
160  } // namespace Raw
161  } // namespace MatrixMarket
162 } // namespace Teuchos
163 
164 #endif // __Teuchos_MatrixMarket_Raw_Writer_hpp
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
Ordinal size_type
Type representing the number of elements in an ArrayRCP or view thereof.
Temporarily make an output stream use scientific notation with sufficient precision.
Templated Parameter List class.
void write(std::ostream &out, const ArrayView< const OrdinalType > &rowptr, const ArrayView< const OrdinalType > &colind, const ArrayView< const ScalarType > &values, const OrdinalType numRows, const OrdinalType numCols)
Write the sparse matrix to the given output stream.
void writeFile(const std::string &filename, const ArrayView< const OrdinalType > &rowptr, const ArrayView< const OrdinalType > &colind, const ArrayView< const ScalarType > &values, const OrdinalType numRows, const OrdinalType numCols)
Write the sparse matrix to the given file.
Nonowning array view.
Write a sparse matrix from raw CSR (compressed sparse row) storage to a Matrix Market file...