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/linear_algebra/macros.hpp>
00020 
00021 #include <ecl/exceptions/standard_exception.hpp>
00022 #include <ecl/formatters.hpp>
00023 #include <ecl/mpl/enable_if.hpp>
00024 #include <ecl/type_traits/fundamental_types.hpp>
00025 
00026 /*****************************************************************************
00027 ** Namespaces
00028 *****************************************************************************/
00029 
00030 namespace Eigen {
00031 
00032 /*****************************************************************************
00033 ** Interface [MatrixFormatter]
00034 *****************************************************************************/
00035 
00042 template <typename Derived, typename Scalar, typename Enable = void>
00043 class MatrixFormatter {
00044 private:
00045         MatrixFormatter() {}
00046 };
00047 
00048 /*****************************************************************************
00049 ** Interface [FloatMatrixFormatter]
00050 *****************************************************************************/
00051 
00062 template<typename Derived>
00063 class FloatMatrixFormatter {
00064 public:
00065         /******************************************
00066         ** C&D's
00067         *******************************************/
00075         FloatMatrixFormatter(const unsigned int &p = 2, const int &w = -1) :
00076                 tmp_formatting(false),
00077                 ready_to_format(false),
00078                 _matrix(NULL)
00079         {
00080                 format.width(w);
00081                 format.precision(p);
00082         }
00083         virtual ~FloatMatrixFormatter() {}
00084 
00085         /******************************************
00086         ** Configuration
00087         *******************************************/
00096         FloatMatrixFormatter<Derived>& precision( const unsigned int &p ) {
00097                 format.precision(p);
00098                 return *this;
00099         }
00100 
00109         FloatMatrixFormatter<Derived>& width( const int &w ) {
00110                 format.width(w);
00111                 return *this;
00112         }
00113 
00118         unsigned int precision() { return format.precision(); }
00119 
00124         int width() { return format.width(); }
00125 
00126         /******************************************
00127         ** Format a value
00128         *******************************************/
00142         FloatMatrixFormatter< Derived >& operator() (const Derived & matrix ) {
00143                 _matrix = &matrix;
00144                 ready_to_format = true;
00145                 return (*this);
00146         }
00164         FloatMatrixFormatter< Derived >& operator() (const Derived & matrix, const unsigned int &p, const int &w ) {
00165                 _matrix = &matrix;
00166                 tmp_precision = p;
00167                 tmp_width = w;
00168                 tmp_formatting = true;
00169                 ready_to_format = true;
00170                 return (*this);
00171         }
00172 
00186         template <typename OutputStream, typename Derived_ >
00187         friend OutputStream& operator << ( OutputStream& ostream, FloatMatrixFormatter<Derived_> & formatter ) ecl_assert_throw_decl(ecl::StandardException);
00188 
00189     private:
00190                 ecl::Format<typename Derived::Scalar> format;
00191                 int tmp_width;
00192                 unsigned int tmp_precision;
00193                 bool tmp_formatting;
00194                 bool ready_to_format;
00195                 const Derived *_matrix;
00196 };
00197 
00198 template <typename OutputStream, typename Derived_ >
00199 OutputStream& operator << (OutputStream& ostream, FloatMatrixFormatter< Derived_ > & formatter ) ecl_assert_throw_decl(ecl::StandardException)
00200 {
00201     ecl_assert_throw( formatter._matrix, ecl::StandardException(LOC,ecl::UsageError,"The formatter cannot print any data - "
00202             "_matrix was not initialised "
00203             "please pass the your matrix through () operator") );
00204     ecl_assert_throw(formatter.ready_to_format, ecl::StandardException(LOC,ecl::UsageError,"The formatter cannot print any data - "
00205             "either there is no data available, or you have tried to use the "
00206             "formatter more than once in a single streaming operation. "
00207             "C++ produces unspecified results when functors are used multiply "
00208             "in the same stream sequence, so this is not permitted here.") );
00209 
00210 
00211     if ( formatter.ready_to_format ) {
00212         unsigned int prm_precision = formatter.format.precision();;
00213         int prm_width = formatter.format.width();
00214                 if ( formatter.tmp_formatting ) {
00215                         formatter.format.precision(formatter.tmp_precision);
00216                         formatter.format.width(formatter.tmp_width);
00217                 }
00218 
00219                 /*********************
00220                 ** Stream Matrix
00221                 **********************/
00222                 // write matrix data
00223                 // @note norma matrix allows matrix(i,j) however sparse matrix does not.
00224                 // So i put coeff(i,j) function which can be used for accessing each element for both of sparse and dense.
00225                 int rows = formatter._matrix->rows();
00226                 int cols = formatter._matrix->cols();
00227                 for( int i=0; i<rows; i++ )
00228                 {
00229                         for( int j=0; j<cols; j++ )
00230                         {
00231                                 ostream <<  formatter.format(formatter._matrix->coeff( i, j )) << "  ";
00232                         }
00233                         ostream << "\n";
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 unsigned int &p = 2, const int &w = -1) : FloatMatrixFormatter<Derived>(p,w) {};
00254 };
00255 
00256 } // namespace Eigen
00257 
00258 #endif /* ECL_LINEAR_ALGEBRA_FORMATTERS_HPP_ */


ecl_linear_algebra
Author(s): Daniel Stonier
autogenerated on Sun Oct 5 2014 23:35:29