Class documentation of Concepts

Loading...
Searching...
No Matches
outputMatlab.hh
Go to the documentation of this file.
1
6#ifndef OperatorOutputMatlab_hh
7#define OperatorOutputMatlab_hh
8
10#include "sparseMatrix.hh"
11
12namespace concepts {
13
14 // ********************************************** storeDenseMatrixToMatlab **
15
17 static uint storeDenseMatrixMatlabCounter_ = 0;
18 static uint storeSparseMatrixMatlabCounter_ = 0;
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) {
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
const uint nofCols() const
Number of columns.
Definition matrix.hh:58
const uint nofRows() const
Number of rows.
Definition matrix.hh:56
const HashedSparseMatrix< F > * m() const
Returns the sparse matrix itself.
void storeSparseMatrixToOctave(SparseMatrix< F > &matrix, std::ostream &ofs, std::string name="")
Set< F > makeSet(uint n, const F &first,...)
Definition set.hh:320
bool storeDenseMatrixToMatlab(F &matrix, const uint nofRows, const uint nofCols, const std::string filename, std::string name="", bool append=false)