Go to the documentation of this file.00001
00008
00009
00010
00011
00012 #ifndef ECL_LINEAR_ALGEBRA_FORMATTERS_HPP_
00013 #define ECL_LINEAR_ALGEBRA_FORMATTERS_HPP_
00014
00015
00016
00017
00018
00019 #include <ecl/exceptions/standard_exception.hpp>
00020 #include <ecl/formatters.hpp>
00021 #include <ecl/mpl/enable_if.hpp>
00022 #include <ecl/type_traits/fundamental_types.hpp>
00023
00024
00025
00026
00027
00028 namespace Eigen {
00029
00030
00031
00032
00033
00040 template <typename Derived, typename Scalar, typename Enable = void>
00041 class MatrixFormatter {
00042 private:
00043 MatrixFormatter() {}
00044 };
00045
00046
00047
00048
00049
00060 template<typename Derived>
00061 class FloatMatrixFormatter {
00062 public:
00063
00064
00065
00073 FloatMatrixFormatter(const unsigned int &p = 2, const int &w = -1) :
00074 tmp_formatting(false),
00075 ready_to_format(false),
00076 _matrix(NULL)
00077 {
00078 format.width(w);
00079 format.precision(p);
00080 }
00081 virtual ~FloatMatrixFormatter() {}
00082
00083
00084
00085
00094 FloatMatrixFormatter<Derived>& precision( const unsigned int &p ) {
00095 format.precision(p);
00096 return *this;
00097 }
00098
00107 FloatMatrixFormatter<Derived>& width( const int &w ) {
00108 format.width(w);
00109 return *this;
00110 }
00111
00116 unsigned int precision() { return format.precision(); }
00117
00122 int width() { return format.width(); }
00123
00124
00125
00126
00140 FloatMatrixFormatter< Derived >& operator() (const Derived & matrix ) {
00141 _matrix = &matrix;
00142 ready_to_format = true;
00143 return (*this);
00144 }
00162 FloatMatrixFormatter< Derived >& operator() (const Derived & matrix, const unsigned int &p, const int &w ) {
00163 _matrix = &matrix;
00164 tmp_precision = p;
00165 tmp_width = w;
00166 tmp_formatting = true;
00167 ready_to_format = true;
00168 return (*this);
00169 }
00170
00184 template <typename OutputStream, typename Derived_ >
00185 friend OutputStream& operator << ( OutputStream& ostream, FloatMatrixFormatter<Derived_> & formatter ) ecl_assert_throw_decl(ecl::StandardException);
00186
00187 private:
00188 ecl::Format<typename Derived::Scalar> format;
00189 int tmp_width;
00190 unsigned int tmp_precision;
00191 bool tmp_formatting;
00192 bool ready_to_format;
00193 const Derived *_matrix;
00194 };
00195
00196 template <typename OutputStream, typename Derived_ >
00197 OutputStream& operator << (OutputStream& ostream, FloatMatrixFormatter< Derived_ > & formatter ) ecl_assert_throw_decl(ecl::StandardException)
00198 {
00199 ecl_assert_throw( formatter._matrix, ecl::StandardException(LOC,ecl::UsageError,"The formatter cannot print any data - "
00200 "_matrix was not initialised "
00201 "please pass the your matrix through () operator") );
00202 ecl_assert_throw(formatter.ready_to_format, ecl::StandardException(LOC,ecl::UsageError,"The formatter cannot print any data - "
00203 "either there is no data available, or you have tried to use the "
00204 "formatter more than once in a single streaming operation. "
00205 "C++ produces unspecified results when functors are used multiply "
00206 "in the same stream sequence, so this is not permitted here.") );
00207
00208
00209 if ( formatter.ready_to_format ) {
00210 unsigned int prm_precision = formatter.format.precision();;
00211 int prm_width = formatter.format.width();
00212 if ( formatter.tmp_formatting ) {
00213 formatter.format.precision(formatter.tmp_precision);
00214 formatter.format.width(formatter.tmp_width);
00215 }
00216
00217
00218
00219
00220
00221
00222
00223 int rows = formatter._matrix->rows();
00224 int cols = formatter._matrix->cols();
00225 for( int i=0; i<rows; i++ )
00226 {
00227 for( int j=0; j<cols; j++ )
00228 {
00229 ostream << formatter.format(formatter._matrix->coeff( i, j )) << " ";
00230 }
00231 ostream << "\n";
00232 }
00233
00234 if ( formatter.tmp_formatting ) {
00235 formatter.format.precision(prm_precision);
00236 formatter.format.width(prm_width);
00237 formatter.tmp_formatting = false;
00238 }
00239 formatter.ready_to_format = false;
00240 }
00241 return ostream;
00242 }
00243
00244
00245
00246
00247
00248 template <typename Derived, typename Scalar>
00249 class MatrixFormatter<Derived, Scalar, typename ecl::enable_if< ecl::is_float<Scalar> >::type> : public FloatMatrixFormatter<Derived> {
00250 public:
00251 MatrixFormatter(const unsigned int &p = 2, const int &w = -1) : FloatMatrixFormatter<Derived>(p,w) {};
00252 };
00253
00254 }
00255
00256 #endif