outputMatlab.hh

Go to the documentation of this file.
1 
6 #ifndef OperatorOutputMatlab_hh
7 #define OperatorOutputMatlab_hh
8 
9 #include "basics/outputMatlab.hh"
10 #include "sparseMatrix.hh"
11 
12 namespace concepts {
13 
14  // ********************************************** storeDenseMatrixToMatlab **
15 
19 
31  template<class F>
32  bool storeDenseMatrixToMatlab(F& matrix, const uint nofRows,
33  const uint nofCols, const std::string filename,
34  std::string name = "", bool append = false) {
35 
36  // Mode, new or append
37  std::ios_base::openmode mode = std::ofstream::out;
38  if (append) mode = mode | std::ofstream::app;
39 
40  std::ofstream ofs(filename.c_str(), mode);
41  if (!ofs.is_open()) {
42  std::cerr << "Could not open file '" << filename << "'" << std::endl;
43  return false;
44  }
45 
46  storeDenseMatrixToMatlab(matrix, nofRows, nofCols, ofs, name);
47  return true;
48  }
49 
50 
61  template<class F>
62  void storeDenseMatrixToMatlab(F& matrix, const uint nofRows,
63  const uint nofCols, std::ostream& ofs,
64  std::string name = "") {
65  if (name.empty()) {
66  std::stringstream s;
67  s << "MatDense_" << storeDenseMatrixMatlabCounter_++;
68  name = s.str();
69  }
70  std::string offset(name.size() + 4, ' ');
71  ofs << name << " = [" << std::setprecision(16);
72 
73  for (uint i = 0; i < nofRows; ++i) {
74  for (uint j = 0; j < nofCols; ++j) {
75  ofs << OutputMatlab<typename F::value_type>(matrix(i,j));
76  if (j == nofCols-1) {
77  if (i < nofRows-1)
78  ofs << std::endl << offset;
79  } else ofs << ' ';
80  }
81  }
82  ofs << "];" << std::endl;
83  }
84 
85 
97  template<class F>
98  void storeSparseMatrixToOctave(SparseMatrix<F>& matrix, std::ostream& ofs,
99  std::string name = "")
100  {
101  if (name.empty()) {
102  std::stringstream s;
103  s << "MatSparse_" << storeSparseMatrixMatlabCounter_++;
104  name = s.str();
105  }
106  std::string offset(name.size() + 4, ' ');
107  ofs << name << " = zeros(" << matrix.nofRows() << ", " << matrix.nofCols()
108  << ");" << std::endl;
109  ofs << "zzz = [" << std::endl << std::setprecision(16);
110  matrix.m()->outputMatlab(ofs);
111  ofs << "];" << std::endl;
112  ofs << "for i__=1:size(zzz,1)" << std::endl
113  << " " << name << "(zzz(i__,1), zzz(i__,2)) = zzz(i__,3);"<< std::endl
114  << "end" << std::endl
115  << "clear zzz" << std::endl;
116  }
117 
118 } // namespace concepts
119 
120 #endif // OperatorOutputMatlab_hh
bool storeDenseMatrixToMatlab(F &matrix, const uint nofRows, const uint nofCols, const std::string filename, std::string name="", bool append=false)
Stores a matrix to the matlab file filename as dense matrix name.
Definition: outputMatlab.hh:32
const HashedSparseMatrix< F > * m() const
Returns the sparse matrix itself.
static uint storeSparseMatrixMatlabCounter_
Definition: outputMatlab.hh:18
static uint storeDenseMatrixMatlabCounter_
Counts number of Matlab outputs (used to uniquely name the matrices)
Definition: outputMatlab.hh:17
void storeSparseMatrixToOctave(SparseMatrix< F > &matrix, std::ostream &ofs, std::string name="")
Writes a matrix to the stream ofs as dense matrix name in Octave format (older version don't have spa...
Definition: outputMatlab.hh:98
Basic namespace for Concepts-2.
Definition: pml_formula.h:16
Page URL: http://wiki.math.ethz.ch/bin/view/Concepts/WebHome
21 August 2020
© 2020 Eidgenössische Technische Hochschule Zürich