.. _program_listing_file__tmp_ws_src_ecl_core_ecl_linear_algebra_include_ecl_linear_algebra_eigen_formatters.hpp: Program Listing for File formatters.hpp ======================================= |exhale_lsh| :ref:`Return to documentation for file ` (``/tmp/ws/src/ecl_core/ecl_linear_algebra/include/ecl/linear_algebra/eigen/formatters.hpp``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp /***************************************************************************** ** Ifdefs *****************************************************************************/ #ifndef ECL_LINEAR_ALGEBRA_FORMATTERS_HPP_ #define ECL_LINEAR_ALGEBRA_FORMATTERS_HPP_ /***************************************************************************** ** Includes *****************************************************************************/ #include #include #include #include /***************************************************************************** ** Namespaces *****************************************************************************/ namespace Eigen { /***************************************************************************** ** Interface [MatrixFormatter] *****************************************************************************/ template class MatrixFormatter { private: MatrixFormatter() {} }; /***************************************************************************** ** Interface [FloatMatrixFormatter] *****************************************************************************/ template class FloatMatrixFormatter { public: /****************************************** ** C&D's *******************************************/ FloatMatrixFormatter(const int &w = -1, const unsigned int &p = 2) : tmp_formatting(false), ready_to_format(false), _matrix(NULL) { format.width(w); format.precision(p); } virtual ~FloatMatrixFormatter() {} /****************************************** ** Configuration *******************************************/ FloatMatrixFormatter& precision( const unsigned int &p ) { format.precision(p); return *this; } FloatMatrixFormatter& width( const int &w ) { format.width(w); return *this; } unsigned int precision() { return format.precision(); } int width() { return format.width(); } /****************************************** ** Format a value *******************************************/ FloatMatrixFormatter< Derived >& operator() (const Derived & matrix ) { _matrix = &matrix; ready_to_format = true; return (*this); } FloatMatrixFormatter< Derived >& operator() (const Derived & matrix, const int &w, const unsigned int &p) { _matrix = &matrix; tmp_precision = p; tmp_width = w; tmp_formatting = true; ready_to_format = true; return (*this); } template friend OutputStream& operator << ( OutputStream& ostream, FloatMatrixFormatter & formatter ); private: ecl::Format format; int tmp_width; unsigned int tmp_precision; bool tmp_formatting; bool ready_to_format; const Derived *_matrix; }; template OutputStream& operator << (OutputStream& ostream, FloatMatrixFormatter< Derived_ > & formatter ) { ecl_assert_throw( formatter._matrix, ecl::StandardException(LOC,ecl::UsageError,"The formatter cannot print any data - " "_matrix was not initialised " "please pass the your matrix through () operator") ); ecl_assert_throw(formatter.ready_to_format, ecl::StandardException(LOC,ecl::UsageError,"The formatter cannot print any data - " "either there is no data available, or you have tried to use the " "formatter more than once in a single streaming operation. " "C++ produces unspecified results when functors are used multiply " "in the same stream sequence, so this is not permitted here.") ); if ( formatter.ready_to_format ) { unsigned int prm_precision = formatter.format.precision();; int prm_width = formatter.format.width(); if ( formatter.tmp_formatting ) { formatter.format.precision(formatter.tmp_precision); formatter.format.width(formatter.tmp_width); } /********************* ** Stream Matrix **********************/ // write matrix data // @note norma matrix allows matrix(i,j) however sparse matrix does not. // So i put coeff(i,j) function which can be used for accessing each element for both of sparse and dense. int rows = formatter._matrix->rows(); int cols = formatter._matrix->cols(); for( int i=0; icoeff( i, j )) << " "; } if ( rows != 1 ) { // special condition for row vectors so we can do inline the formatting. ostream << "\n"; } } if ( formatter.tmp_formatting ) { formatter.format.precision(prm_precision); formatter.format.width(prm_width); formatter.tmp_formatting = false; } formatter.ready_to_format = false; } return ostream; } /***************************************************************************** ** Interface [MatrixFormatter][Float Types] *****************************************************************************/ template class MatrixFormatter >::type> : public FloatMatrixFormatter { public: MatrixFormatter(const int &w = -1, const unsigned int &p = 2) : FloatMatrixFormatter(w, p) {}; }; } // namespace Eigen #endif /* ECL_LINEAR_ALGEBRA_FORMATTERS_HPP_ */