outputMatlab.hh

Go to the documentation of this file.
1 
6 #ifndef OutputMatlab_hh
7 #define OutputMatlab_hh
8 
9 #include <iostream>
10 #include <complex>
11 #include <set>
12 #include <map>
13 #include <vector>
14 #include <queue>
15 #include "outputOperator.hh"
16 #include "vectorsMatrices.hh"
17 #include <iomanip>
18 
19 namespace concepts {
20 
21  // ********************************************************** outputMatlab **
22 
28  // general matlab output
29  template<typename T>
30  std::ostream& outputMatlab(std::ostream& os, const T& val) {
31  return os << std::setw(13) << std::scientific << val;
32  }
33 
34  // special matlab output for complex numbers
35  template<typename T>
36  std::ostream& outputMatlab(std::ostream& os, const std::complex<T>& val) {
37  const T r = std::real(val),
38  i = std::imag(val);
39  /*
40  if (r != 0 || i == 0)
41  os << std::setw(12) << std::scientific << r;
42  if (i != 0) {
43  if (i > 0) {
44  if (r != 0)
45  os << '+';
46  } else
47  os << '-';
48 #if _GLIBCPP_USE_C99
49  if (std::isnan(i))
50  os << "NaN*i";
51  else
52 #endif
53  {
54  if (std::abs(i) != 1)
55  os << std::setw(12) << std::scientific << std::abs(i);
56  os << 'i';
57  }
58  }*/
59  os << std::setw(13) << std::scientific << r;
60  if (i > 0) {
61  os << '+';
62  } else
63  os << '-';
64 
65 #if _GLIBCPP_USE_C99
66  if (std::isnan(i))
67  os << "NaN*i";
68  else
69 #endif
70  {
71  if (std::abs(i) != 1)
72  os << std::setw(13) << std::scientific << std::abs(i);
73  os << 'i';
74  }
75 
76 
77  return os;
78  }
79 
80  // ********************************************************** OutputMatlab **
81 
87  template<typename F>
88  class OutputMatlab : public virtual OutputOperator {
89  public:
90  OutputMatlab(const F& val) : val_(val) {}
91  protected:
93  std::ostream& info(std::ostream& os) const {
94  return outputMatlab(os, val_);
95  }
96  private:
97  const F& val_;
98  };
99 
101  template<typename F, int dim>
102  class OutputMatlab<Point<F,dim> > : public virtual OutputOperator {
103  public:
104  OutputMatlab(const Point<F,dim>& val) : val_(val) {}
105  protected:
107  std::ostream& info(std::ostream& os) const {
108  os << '[' << OutputMatlab<F>(val_[0]) << ';' << OutputMatlab<F>(val_[1]);
109  if (dim == 3) os << ';' << OutputMatlab<F>(val_[2]);
110  return os << ']';
111  }
112  private:
114  };
115 
117  template<typename F, int dim>
118  class OutputMatlab<Mapping<F,dim> > : public virtual OutputOperator {
119  public:
120  OutputMatlab(const Mapping<F,dim>& val) : val_(val) {}
121  protected:
124  std::ostream& info(std::ostream& os) const {
125  os << '[';
126  for(uint i = 0; i < dim; ++i)
127  for(uint j = 0; j < dim; ++j)
128  os << OutputMatlab<F>(val_(i,j)) << ';';
129  return os << ']';
130  }
131  private:
133  };
134 
136  template<typename F>
137  class OutputMatlab<F*> : public virtual OutputOperator {
138  public:
139  OutputMatlab(const F* val) : val_(val) {}
140  protected:
142  std::ostream& info(std::ostream& os) const {
143  if (val_) return os << OutputMatlab<F>(*val_);
144  return os << 0;
145  }
146  private:
147  const F* val_;
148  };
149 
151  template<>
152  class OutputMatlab<std::string> : public virtual OutputOperator {
153  public:
154  OutputMatlab(const std::string val) : val_(val) {}
155  protected:
157  std::ostream& info(std::ostream& os) const {
158  return os << "'" << val_ << "'";
159  }
160  private:
161  const std::string val_;
162  };
163 
165  template<>
166  class OutputMatlab<char*> : public OutputMatlab<std::string> {
167  public:
168  OutputMatlab(const char* val) : OutputMatlab<std::string>(val) {}
169  };
170 
171  template<>
172  class OutputMatlab<bool> : public OutputOperator {
173  public:
174  OutputMatlab(const bool val) : val_(val) {}
175  protected:
177  std::ostream& info(std::ostream& os) const {
178  bool nobool = !(os.flags() & std::ios_base::boolalpha);
179  os << std::boolalpha << val_;
180  if (nobool) os << std::noboolalpha;
181  return os;
182  }
183  private:
184  const bool val_;
185  };
186 
187  template<class F>
188  class OutputMatlab<std::set<F> > : public OutputOperator {
189  public:
190  OutputMatlab(const std::set<F>& val) : val_(val) {}
191  protected:
193  std::ostream& info(std::ostream& os) const {
194  os << '[';
195  typename std::set<F>::const_iterator i = val_.begin();
196  for(; i != val_.end(); ) {
197  os << OutputMatlab<F>(*i);
198  if (++i != val_.end()) os << ", ";
199  }
200  return os << ']';
201  }
202  private:
203  const std::set<F> val_;
204  };
205 
206  template<class F, class G>
207  class OutputMatlab<std::map<F,G> > : public OutputOperator {
208  public:
209  OutputMatlab(const std::map<F,G>& val) : val_(val) {}
210  protected:
212  std::ostream& info(std::ostream& os) const {
213  os << '[';
214  typename std::map<F,G>::const_iterator i = val_.begin();
215  for(; i != val_.end(); ++i)
216  os << "("
217  << OutputMatlab<F>(i->first ) << ", "
218  << OutputMatlab<G>(i->second) << ") ";
219  return os << ']';
220  }
221  private:
222  const std::map<F,G> val_;
223  };
224 
225  template<class F>
226  class OutputMatlab<std::vector<F> > : public OutputOperator {
227  public:
228  OutputMatlab(const std::vector<F>& val) : val_(val) {}
229  protected:
231  std::ostream& info(std::ostream& os) const {
232  os << '[';
233  typename std::vector<F>::const_iterator i = val_.begin();
234  for(; i != val_.end();) {
235  os << OutputMatlab<F>(*i);
236  if (++i != val_.end()) os << ", ";
237  }
238  return os << ']';
239  }
240  private:
241  const std::vector<F> val_;
242  };
243 
244  template<class F>
245  class OutputMatlab<std::queue<F> > : public OutputOperator {
246  public:
248  OutputMatlab(const std::queue<F>& val) : val_(val) {}
249  protected:
251  std::ostream& info(std::ostream& os) const {
252  std::queue<F> val = val_;
253  os << '[';
254  const uint size = val.size();
255  for(uint i = 0; i < size; ++i) {
256  os << OutputMatlab<F>(val.front());
257  if (i < size - 1) os << ", ";
258  val.pop(); // erase first element
259  }
260  return os << ']';
261  }
262  private:
263  const std::queue<F> val_;
264  };
265 
266 } // namespace concepts
267 
268 #endif // OutputMatlab_hh
OutputMatlab(const std::vector< F > &val)
Basic class for a 2D or 3D map.
OutputMatlab(const Point< F, dim > &val)
std::ostream & info(std::ostream &os) const
Returns information in an output stream In matlab, use e.g.
OutputMatlab(const std::string val)
Basic class for a Point or a vector.
OutputMatlab(const Mapping< F, dim > &val)
std::ostream & info(std::ostream &os) const
Returns information in an output stream.
OutputMatlab(const std::map< F, G > &val)
std::ostream & outputMatlab(std::ostream &os, const T &val)
Function for output of basic types to matlab.
Definition: outputMatlab.hh:30
Class for output of objects to matlab.
Definition: outputMatlab.hh:88
std::ostream & info(std::ostream &os) const
Returns information in an output stream.
OutputMatlab(const std::queue< F > &val)
Constructor.
OutputMatlab(const std::set< F > &val)
std::ostream & info(std::ostream &os) const
Returns information in an output stream.
Definition: outputMatlab.hh:93
std::ostream & info(std::ostream &os) const
Returns information in an output stream.
std::ostream & info(std::ostream &os) const
Returns information in an output stream.
uint abs(const uint &v)
Definition: operations.hh:95
std::ostream & info(std::ostream &os) const
Returns information in an output stream.
std::ostream & info(std::ostream &os) const
Returns information in an output stream.
std::ostream & info(std::ostream &os) const
Returns information in an output stream.
Class providing an output operator.
Class for output of C++ strings.
std::ostream & info(std::ostream &os) const
Returns information in an output stream.
Basic namespace for Concepts-2.
Definition: pml_formula.h:16
OutputMatlab(const F &val)
Definition: outputMatlab.hh:90
Page URL: http://wiki.math.ethz.ch/bin/view/Concepts/WebHome
21 August 2020
© 2020 Eidgenössische Technische Hochschule Zürich