eigen/formatters.hpp
Go to the documentation of this file.
1 
8 /*****************************************************************************
9 ** Ifdefs
10 *****************************************************************************/
11 
12 #ifndef ECL_LINEAR_ALGEBRA_FORMATTERS_HPP_
13 #define ECL_LINEAR_ALGEBRA_FORMATTERS_HPP_
14 
15 /*****************************************************************************
16 ** Includes
17 *****************************************************************************/
18 
20 #include <ecl/formatters.hpp>
21 #include <ecl/mpl/enable_if.hpp>
23 
24 /*****************************************************************************
25 ** Namespaces
26 *****************************************************************************/
27 
28 namespace Eigen {
29 
30 /*****************************************************************************
31 ** Interface [MatrixFormatter]
32 *****************************************************************************/
33 
40 template <typename Derived, typename Scalar, typename Enable = void>
42 private:
44 };
45 
46 /*****************************************************************************
47 ** Interface [FloatMatrixFormatter]
48 *****************************************************************************/
49 
60 template<typename Derived>
62 public:
63  /******************************************
64  ** C&D's
65  *******************************************/
73  FloatMatrixFormatter(const int &w = -1, const unsigned int &p = 2) :
74  tmp_formatting(false),
75  ready_to_format(false),
76  _matrix(NULL)
77  {
78  format.width(w);
79  format.precision(p);
80  }
81  virtual ~FloatMatrixFormatter() {}
82 
83  /******************************************
84  ** Configuration
85  *******************************************/
94  FloatMatrixFormatter<Derived>& precision( const unsigned int &p ) {
95  format.precision(p);
96  return *this;
97  }
98 
108  format.width(w);
109  return *this;
110  }
111 
116  unsigned int precision() { return format.precision(); }
117 
122  int width() { return format.width(); }
123 
124  /******************************************
125  ** Format a value
126  *******************************************/
140  FloatMatrixFormatter< Derived >& operator() (const Derived & matrix ) {
141  _matrix = &matrix;
142  ready_to_format = true;
143  return (*this);
144  }
162  FloatMatrixFormatter< Derived >& operator() (const Derived & matrix, const int &w, const unsigned int &p) {
163  _matrix = &matrix;
164  tmp_precision = p;
165  tmp_width = w;
166  tmp_formatting = true;
167  ready_to_format = true;
168  return (*this);
169  }
170 
184  template <typename OutputStream, typename Derived_ >
185  friend OutputStream& operator << ( OutputStream& ostream, FloatMatrixFormatter<Derived_> & formatter );
186 
187  private:
190  unsigned int tmp_precision;
193  const Derived *_matrix;
194 };
195 
196 template <typename OutputStream, typename Derived_ >
197 OutputStream& operator << (OutputStream& ostream, FloatMatrixFormatter< Derived_ > & formatter )
198 {
199  ecl_assert_throw( formatter._matrix, ecl::StandardException(LOC,ecl::UsageError,"The formatter cannot print any data - "
200  "_matrix was not initialised "
201  "please pass the your matrix through () operator") );
202  ecl_assert_throw(formatter.ready_to_format, ecl::StandardException(LOC,ecl::UsageError,"The formatter cannot print any data - "
203  "either there is no data available, or you have tried to use the "
204  "formatter more than once in a single streaming operation. "
205  "C++ produces unspecified results when functors are used multiply "
206  "in the same stream sequence, so this is not permitted here.") );
207 
208 
209  if ( formatter.ready_to_format ) {
210  unsigned int prm_precision = formatter.format.precision();;
211  int prm_width = formatter.format.width();
212  if ( formatter.tmp_formatting ) {
213  formatter.format.precision(formatter.tmp_precision);
214  formatter.format.width(formatter.tmp_width);
215  }
216 
217  /*********************
218  ** Stream Matrix
219  **********************/
220  // write matrix data
221  // @note norma matrix allows matrix(i,j) however sparse matrix does not.
222  // So i put coeff(i,j) function which can be used for accessing each element for both of sparse and dense.
223  int rows = formatter._matrix->rows();
224  int cols = formatter._matrix->cols();
225  for( int i=0; i<rows; i++ )
226  {
227  for( int j=0; j<cols; j++ )
228  {
229  ostream << formatter.format(formatter._matrix->coeff( i, j )) << " ";
230  }
231  if ( rows != 1 ) { // special condition for row vectors so we can do inline the formatting.
232  ostream << "\n";
233  }
234  }
235 
236  if ( formatter.tmp_formatting ) {
237  formatter.format.precision(prm_precision);
238  formatter.format.width(prm_width);
239  formatter.tmp_formatting = false;
240  }
241  formatter.ready_to_format = false;
242  }
243  return ostream;
244 }
245 
246 /*****************************************************************************
247 ** Interface [MatrixFormatter][Float Types]
248 *****************************************************************************/
249 
250 template <typename Derived, typename Scalar>
251 class MatrixFormatter<Derived, Scalar, typename ecl::enable_if< ecl::is_float<Scalar> >::type> : public FloatMatrixFormatter<Derived> {
252 public:
253  MatrixFormatter(const int &w = -1, const unsigned int &p = 2) : FloatMatrixFormatter<Derived>(w, p) {};
254 };
255 
256 } // namespace Eigen
257 
258 #endif /* ECL_LINEAR_ALGEBRA_FORMATTERS_HPP_ */
Parent for matrix formatters - used to negate unimplemented formatters.
ecl::Format< typename Derived::Scalar > format
#define LOC
#define ecl_assert_throw(expression, exception)
FloatMatrixFormatter(const int &w=-1, const unsigned int &p=2)
Default constructor.
UsageError
Formatter for Eigen matrix type.
FloatMatrixFormatter< Derived > & precision(const unsigned int &p)
Sets the precision format parameter.
int width()
Returns the current width setting.
unsigned int precision()
Returns the current precision setting.
FloatMatrixFormatter< Derived > & width(const int &w)
Sets the width format parameter.


ecl_linear_algebra
Author(s): Daniel Stonier
autogenerated on Mon Feb 28 2022 22:18:38