formatters.hpp
Go to the documentation of this file.
00001 
00008 /*****************************************************************************
00009 ** Ifdefs
00010 *****************************************************************************/
00011 
00012 #ifndef ECL_LINEAR_ALGEBRA_FORMATTERS_HPP_
00013 #define ECL_LINEAR_ALGEBRA_FORMATTERS_HPP_
00014 
00015 /*****************************************************************************
00016 ** Includes
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 ** Namespaces
00026 *****************************************************************************/
00027 
00028 namespace Eigen {
00029 
00030 /*****************************************************************************
00031 ** Interface [MatrixFormatter]
00032 *****************************************************************************/
00033 
00040 template <typename Derived, typename Scalar, typename Enable = void>
00041 class MatrixFormatter {
00042 private:
00043         MatrixFormatter() {}
00044 };
00045 
00046 /*****************************************************************************
00047 ** Interface [FloatMatrixFormatter]
00048 *****************************************************************************/
00049 
00060 template<typename Derived>
00061 class FloatMatrixFormatter {
00062 public:
00063         /******************************************
00064         ** C&D's
00065         *******************************************/
00073         FloatMatrixFormatter(const int &w = -1, const unsigned int &p = 2) :
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         ** Configuration
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         ** Format a value
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 int &w, const unsigned int &p) {
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                 ** Stream Matrix
00219                 **********************/
00220                 // write matrix data
00221                 // @note norma matrix allows matrix(i,j) however sparse matrix does not.
00222                 // So i put coeff(i,j) function which can be used for accessing each element for both of sparse and dense.
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                         if ( rows != 1 ) {  // special condition for row vectors so we can do inline the formatting.
00232                           ostream << "\n";
00233                         }
00234                 }
00235 
00236                 if ( formatter.tmp_formatting ) {
00237                         formatter.format.precision(prm_precision);
00238                         formatter.format.width(prm_width);
00239                         formatter.tmp_formatting = false;
00240                 }
00241         formatter.ready_to_format = false;
00242     }
00243     return ostream;
00244 }
00245 
00246 /*****************************************************************************
00247 ** Interface [MatrixFormatter][Float Types]
00248 *****************************************************************************/
00249 
00250 template <typename Derived, typename Scalar>
00251 class MatrixFormatter<Derived, Scalar, typename ecl::enable_if< ecl::is_float<Scalar> >::type> : public FloatMatrixFormatter<Derived> {
00252 public:
00253         MatrixFormatter(const int &w = -1, const unsigned int &p = 2) : FloatMatrixFormatter<Derived>(w, p) {};
00254 };
00255 
00256 } // namespace Eigen
00257 
00258 #endif /* ECL_LINEAR_ALGEBRA_FORMATTERS_HPP_ */


ecl_linear_algebra
Author(s): Daniel Stonier
autogenerated on Thu Jun 6 2019 21:17:37