Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:56:14

0001 // This file is part of Eigen, a lightweight C++ template library
0002 // for linear algebra.
0003 //
0004 // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
0005 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
0006 //
0007 // This Source Code Form is subject to the terms of the Mozilla
0008 // Public License v. 2.0. If a copy of the MPL was not distributed
0009 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
0010 
0011 #ifndef EIGEN_IO_H
0012 #define EIGEN_IO_H
0013 
0014 namespace Eigen { 
0015 
0016 enum { DontAlignCols = 1 };
0017 enum { StreamPrecision = -1,
0018        FullPrecision = -2 };
0019 
0020 namespace internal {
0021 template<typename Derived>
0022 std::ostream & print_matrix(std::ostream & s, const Derived& _m, const IOFormat& fmt);
0023 }
0024 
0025 /** \class IOFormat
0026   * \ingroup Core_Module
0027   *
0028   * \brief Stores a set of parameters controlling the way matrices are printed
0029   *
0030   * List of available parameters:
0031   *  - \b precision number of digits for floating point values, or one of the special constants \c StreamPrecision and \c FullPrecision.
0032   *                 The default is the special value \c StreamPrecision which means to use the
0033   *                 stream's own precision setting, as set for instance using \c cout.precision(3). The other special value
0034   *                 \c FullPrecision means that the number of digits will be computed to match the full precision of each floating-point
0035   *                 type.
0036   *  - \b flags an OR-ed combination of flags, the default value is 0, the only currently available flag is \c DontAlignCols which
0037   *             allows to disable the alignment of columns, resulting in faster code.
0038   *  - \b coeffSeparator string printed between two coefficients of the same row
0039   *  - \b rowSeparator string printed between two rows
0040   *  - \b rowPrefix string printed at the beginning of each row
0041   *  - \b rowSuffix string printed at the end of each row
0042   *  - \b matPrefix string printed at the beginning of the matrix
0043   *  - \b matSuffix string printed at the end of the matrix
0044   *  - \b fill character printed to fill the empty space in aligned columns
0045   *
0046   * Example: \include IOFormat.cpp
0047   * Output: \verbinclude IOFormat.out
0048   *
0049   * \sa DenseBase::format(), class WithFormat
0050   */
0051 struct IOFormat
0052 {
0053   /** Default constructor, see class IOFormat for the meaning of the parameters */
0054   IOFormat(int _precision = StreamPrecision, int _flags = 0,
0055     const std::string& _coeffSeparator = " ",
0056     const std::string& _rowSeparator = "\n", const std::string& _rowPrefix="", const std::string& _rowSuffix="",
0057     const std::string& _matPrefix="", const std::string& _matSuffix="", const char _fill=' ')
0058   : matPrefix(_matPrefix), matSuffix(_matSuffix), rowPrefix(_rowPrefix), rowSuffix(_rowSuffix), rowSeparator(_rowSeparator),
0059     rowSpacer(""), coeffSeparator(_coeffSeparator), fill(_fill), precision(_precision), flags(_flags)
0060   {
0061     // TODO check if rowPrefix, rowSuffix or rowSeparator contains a newline
0062     // don't add rowSpacer if columns are not to be aligned
0063     if((flags & DontAlignCols))
0064       return;
0065     int i = int(matSuffix.length())-1;
0066     while (i>=0 && matSuffix[i]!='\n')
0067     {
0068       rowSpacer += ' ';
0069       i--;
0070     }
0071   }
0072   std::string matPrefix, matSuffix;
0073   std::string rowPrefix, rowSuffix, rowSeparator, rowSpacer;
0074   std::string coeffSeparator;
0075   char fill;
0076   int precision;
0077   int flags;
0078 };
0079 
0080 /** \class WithFormat
0081   * \ingroup Core_Module
0082   *
0083   * \brief Pseudo expression providing matrix output with given format
0084   *
0085   * \tparam ExpressionType the type of the object on which IO stream operations are performed
0086   *
0087   * This class represents an expression with stream operators controlled by a given IOFormat.
0088   * It is the return type of DenseBase::format()
0089   * and most of the time this is the only way it is used.
0090   *
0091   * See class IOFormat for some examples.
0092   *
0093   * \sa DenseBase::format(), class IOFormat
0094   */
0095 template<typename ExpressionType>
0096 class WithFormat
0097 {
0098   public:
0099 
0100     WithFormat(const ExpressionType& matrix, const IOFormat& format)
0101       : m_matrix(matrix), m_format(format)
0102     {}
0103 
0104     friend std::ostream & operator << (std::ostream & s, const WithFormat& wf)
0105     {
0106       return internal::print_matrix(s, wf.m_matrix.eval(), wf.m_format);
0107     }
0108 
0109   protected:
0110     typename ExpressionType::Nested m_matrix;
0111     IOFormat m_format;
0112 };
0113 
0114 namespace internal {
0115 
0116 // NOTE: This helper is kept for backward compatibility with previous code specializing
0117 //       this internal::significant_decimals_impl structure. In the future we should directly
0118 //       call digits10() which has been introduced in July 2016 in 3.3.
0119 template<typename Scalar>
0120 struct significant_decimals_impl
0121 {
0122   static inline int run()
0123   {
0124     return NumTraits<Scalar>::digits10();
0125   }
0126 };
0127 
0128 /** \internal
0129   * print the matrix \a _m to the output stream \a s using the output format \a fmt */
0130 template<typename Derived>
0131 std::ostream & print_matrix(std::ostream & s, const Derived& _m, const IOFormat& fmt)
0132 {
0133   using internal::is_same;
0134   using internal::conditional;
0135 
0136   if(_m.size() == 0)
0137   {
0138     s << fmt.matPrefix << fmt.matSuffix;
0139     return s;
0140   }
0141   
0142   typename Derived::Nested m = _m;
0143   typedef typename Derived::Scalar Scalar;
0144   typedef typename
0145       conditional<
0146           is_same<Scalar, char>::value ||
0147             is_same<Scalar, unsigned char>::value ||
0148             is_same<Scalar, numext::int8_t>::value ||
0149             is_same<Scalar, numext::uint8_t>::value,
0150           int,
0151           typename conditional<
0152               is_same<Scalar, std::complex<char> >::value ||
0153                 is_same<Scalar, std::complex<unsigned char> >::value ||
0154                 is_same<Scalar, std::complex<numext::int8_t> >::value ||
0155                 is_same<Scalar, std::complex<numext::uint8_t> >::value,
0156               std::complex<int>,
0157               const Scalar&
0158             >::type
0159         >::type PrintType;
0160 
0161   Index width = 0;
0162 
0163   std::streamsize explicit_precision;
0164   if(fmt.precision == StreamPrecision)
0165   {
0166     explicit_precision = 0;
0167   }
0168   else if(fmt.precision == FullPrecision)
0169   {
0170     if (NumTraits<Scalar>::IsInteger)
0171     {
0172       explicit_precision = 0;
0173     }
0174     else
0175     {
0176       explicit_precision = significant_decimals_impl<Scalar>::run();
0177     }
0178   }
0179   else
0180   {
0181     explicit_precision = fmt.precision;
0182   }
0183 
0184   std::streamsize old_precision = 0;
0185   if(explicit_precision) old_precision = s.precision(explicit_precision);
0186 
0187   bool align_cols = !(fmt.flags & DontAlignCols);
0188   if(align_cols)
0189   {
0190     // compute the largest width
0191     for(Index j = 0; j < m.cols(); ++j)
0192       for(Index i = 0; i < m.rows(); ++i)
0193       {
0194         std::stringstream sstr;
0195         sstr.copyfmt(s);
0196         sstr << static_cast<PrintType>(m.coeff(i,j));
0197         width = std::max<Index>(width, Index(sstr.str().length()));
0198       }
0199   }
0200   std::streamsize old_width = s.width();
0201   char old_fill_character = s.fill();
0202   s << fmt.matPrefix;
0203   for(Index i = 0; i < m.rows(); ++i)
0204   {
0205     if (i)
0206       s << fmt.rowSpacer;
0207     s << fmt.rowPrefix;
0208     if(width) {
0209       s.fill(fmt.fill);
0210       s.width(width);
0211     }
0212     s << static_cast<PrintType>(m.coeff(i, 0));
0213     for(Index j = 1; j < m.cols(); ++j)
0214     {
0215       s << fmt.coeffSeparator;
0216       if(width) {
0217         s.fill(fmt.fill);
0218         s.width(width);
0219       }
0220       s << static_cast<PrintType>(m.coeff(i, j));
0221     }
0222     s << fmt.rowSuffix;
0223     if( i < m.rows() - 1)
0224       s << fmt.rowSeparator;
0225   }
0226   s << fmt.matSuffix;
0227   if(explicit_precision) s.precision(old_precision);
0228   if(width) {
0229     s.fill(old_fill_character);
0230     s.width(old_width);
0231   }
0232   return s;
0233 }
0234 
0235 } // end namespace internal
0236 
0237 /** \relates DenseBase
0238   *
0239   * Outputs the matrix, to the given stream.
0240   *
0241   * If you wish to print the matrix with a format different than the default, use DenseBase::format().
0242   *
0243   * It is also possible to change the default format by defining EIGEN_DEFAULT_IO_FORMAT before including Eigen headers.
0244   * If not defined, this will automatically be defined to Eigen::IOFormat(), that is the Eigen::IOFormat with default parameters.
0245   *
0246   * \sa DenseBase::format()
0247   */
0248 template<typename Derived>
0249 std::ostream & operator <<
0250 (std::ostream & s,
0251  const DenseBase<Derived> & m)
0252 {
0253   return internal::print_matrix(s, m.eval(), EIGEN_DEFAULT_IO_FORMAT);
0254 }
0255 
0256 } // end namespace Eigen
0257 
0258 #endif // EIGEN_IO_H