Eigne Matrix Class Library

Dependents:   MPC_current_control HydraulicControlBoard_SW AHRS Test_ekf ... more

Committer:
jsoh91
Date:
Tue Sep 24 00:18:23 2019 +0000
Revision:
1:3b8049da21b8
Parent:
0:13a5d365ba16
ignore and revise some of error parts

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ykuroda 0:13a5d365ba16 1 // This file is part of Eigen, a lightweight C++ template library
ykuroda 0:13a5d365ba16 2 // for linear algebra.
ykuroda 0:13a5d365ba16 3 //
ykuroda 0:13a5d365ba16 4 // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
ykuroda 0:13a5d365ba16 5 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
ykuroda 0:13a5d365ba16 6 //
ykuroda 0:13a5d365ba16 7 // This Source Code Form is subject to the terms of the Mozilla
ykuroda 0:13a5d365ba16 8 // Public License v. 2.0. If a copy of the MPL was not distributed
ykuroda 0:13a5d365ba16 9 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
ykuroda 0:13a5d365ba16 10
ykuroda 0:13a5d365ba16 11 #ifndef EIGEN_IO_H
ykuroda 0:13a5d365ba16 12 #define EIGEN_IO_H
ykuroda 0:13a5d365ba16 13
ykuroda 0:13a5d365ba16 14 namespace Eigen {
ykuroda 0:13a5d365ba16 15
ykuroda 0:13a5d365ba16 16 enum { DontAlignCols = 1 };
ykuroda 0:13a5d365ba16 17 enum { StreamPrecision = -1,
ykuroda 0:13a5d365ba16 18 FullPrecision = -2 };
ykuroda 0:13a5d365ba16 19
ykuroda 0:13a5d365ba16 20 namespace internal {
ykuroda 0:13a5d365ba16 21 template<typename Derived>
ykuroda 0:13a5d365ba16 22 std::ostream & print_matrix(std::ostream & s, const Derived& _m, const IOFormat& fmt);
ykuroda 0:13a5d365ba16 23 }
ykuroda 0:13a5d365ba16 24
ykuroda 0:13a5d365ba16 25 /** \class IOFormat
ykuroda 0:13a5d365ba16 26 * \ingroup Core_Module
ykuroda 0:13a5d365ba16 27 *
ykuroda 0:13a5d365ba16 28 * \brief Stores a set of parameters controlling the way matrices are printed
ykuroda 0:13a5d365ba16 29 *
ykuroda 0:13a5d365ba16 30 * List of available parameters:
ykuroda 0:13a5d365ba16 31 * - \b precision number of digits for floating point values, or one of the special constants \c StreamPrecision and \c FullPrecision.
ykuroda 0:13a5d365ba16 32 * The default is the special value \c StreamPrecision which means to use the
ykuroda 0:13a5d365ba16 33 * stream's own precision setting, as set for instance using \c cout.precision(3). The other special value
ykuroda 0:13a5d365ba16 34 * \c FullPrecision means that the number of digits will be computed to match the full precision of each floating-point
ykuroda 0:13a5d365ba16 35 * type.
ykuroda 0:13a5d365ba16 36 * - \b flags an OR-ed combination of flags, the default value is 0, the only currently available flag is \c DontAlignCols which
ykuroda 0:13a5d365ba16 37 * allows to disable the alignment of columns, resulting in faster code.
ykuroda 0:13a5d365ba16 38 * - \b coeffSeparator string printed between two coefficients of the same row
ykuroda 0:13a5d365ba16 39 * - \b rowSeparator string printed between two rows
ykuroda 0:13a5d365ba16 40 * - \b rowPrefix string printed at the beginning of each row
ykuroda 0:13a5d365ba16 41 * - \b rowSuffix string printed at the end of each row
ykuroda 0:13a5d365ba16 42 * - \b matPrefix string printed at the beginning of the matrix
ykuroda 0:13a5d365ba16 43 * - \b matSuffix string printed at the end of the matrix
ykuroda 0:13a5d365ba16 44 *
ykuroda 0:13a5d365ba16 45 * Example: \include IOFormat.cpp
ykuroda 0:13a5d365ba16 46 * Output: \verbinclude IOFormat.out
ykuroda 0:13a5d365ba16 47 *
ykuroda 0:13a5d365ba16 48 * \sa DenseBase::format(), class WithFormat
ykuroda 0:13a5d365ba16 49 */
ykuroda 0:13a5d365ba16 50 struct IOFormat
ykuroda 0:13a5d365ba16 51 {
ykuroda 0:13a5d365ba16 52 /** Default contructor, see class IOFormat for the meaning of the parameters */
ykuroda 0:13a5d365ba16 53 IOFormat(int _precision = StreamPrecision, int _flags = 0,
ykuroda 0:13a5d365ba16 54 const std::string& _coeffSeparator = " ",
ykuroda 0:13a5d365ba16 55 const std::string& _rowSeparator = "\n", const std::string& _rowPrefix="", const std::string& _rowSuffix="",
ykuroda 0:13a5d365ba16 56 const std::string& _matPrefix="", const std::string& _matSuffix="")
ykuroda 0:13a5d365ba16 57 : matPrefix(_matPrefix), matSuffix(_matSuffix), rowPrefix(_rowPrefix), rowSuffix(_rowSuffix), rowSeparator(_rowSeparator),
ykuroda 0:13a5d365ba16 58 rowSpacer(""), coeffSeparator(_coeffSeparator), precision(_precision), flags(_flags)
ykuroda 0:13a5d365ba16 59 {
ykuroda 0:13a5d365ba16 60 int i = int(matSuffix.length())-1;
ykuroda 0:13a5d365ba16 61 while (i>=0 && matSuffix[i]!='\n')
ykuroda 0:13a5d365ba16 62 {
ykuroda 0:13a5d365ba16 63 rowSpacer += ' ';
ykuroda 0:13a5d365ba16 64 i--;
ykuroda 0:13a5d365ba16 65 }
ykuroda 0:13a5d365ba16 66 }
ykuroda 0:13a5d365ba16 67 std::string matPrefix, matSuffix;
ykuroda 0:13a5d365ba16 68 std::string rowPrefix, rowSuffix, rowSeparator, rowSpacer;
ykuroda 0:13a5d365ba16 69 std::string coeffSeparator;
ykuroda 0:13a5d365ba16 70 int precision;
ykuroda 0:13a5d365ba16 71 int flags;
ykuroda 0:13a5d365ba16 72 };
ykuroda 0:13a5d365ba16 73
ykuroda 0:13a5d365ba16 74 /** \class WithFormat
ykuroda 0:13a5d365ba16 75 * \ingroup Core_Module
ykuroda 0:13a5d365ba16 76 *
ykuroda 0:13a5d365ba16 77 * \brief Pseudo expression providing matrix output with given format
ykuroda 0:13a5d365ba16 78 *
ykuroda 0:13a5d365ba16 79 * \param ExpressionType the type of the object on which IO stream operations are performed
ykuroda 0:13a5d365ba16 80 *
ykuroda 0:13a5d365ba16 81 * This class represents an expression with stream operators controlled by a given IOFormat.
ykuroda 0:13a5d365ba16 82 * It is the return type of DenseBase::format()
ykuroda 0:13a5d365ba16 83 * and most of the time this is the only way it is used.
ykuroda 0:13a5d365ba16 84 *
ykuroda 0:13a5d365ba16 85 * See class IOFormat for some examples.
ykuroda 0:13a5d365ba16 86 *
ykuroda 0:13a5d365ba16 87 * \sa DenseBase::format(), class IOFormat
ykuroda 0:13a5d365ba16 88 */
ykuroda 0:13a5d365ba16 89 template<typename ExpressionType>
ykuroda 0:13a5d365ba16 90 class WithFormat
ykuroda 0:13a5d365ba16 91 {
ykuroda 0:13a5d365ba16 92 public:
ykuroda 0:13a5d365ba16 93
ykuroda 0:13a5d365ba16 94 WithFormat(const ExpressionType& matrix, const IOFormat& format)
ykuroda 0:13a5d365ba16 95 : m_matrix(matrix), m_format(format)
ykuroda 0:13a5d365ba16 96 {}
ykuroda 0:13a5d365ba16 97
ykuroda 0:13a5d365ba16 98 friend std::ostream & operator << (std::ostream & s, const WithFormat& wf)
ykuroda 0:13a5d365ba16 99 {
ykuroda 0:13a5d365ba16 100 return internal::print_matrix(s, wf.m_matrix.eval(), wf.m_format);
ykuroda 0:13a5d365ba16 101 }
ykuroda 0:13a5d365ba16 102
ykuroda 0:13a5d365ba16 103 protected:
ykuroda 0:13a5d365ba16 104 const typename ExpressionType::Nested m_matrix;
ykuroda 0:13a5d365ba16 105 IOFormat m_format;
ykuroda 0:13a5d365ba16 106 };
ykuroda 0:13a5d365ba16 107
ykuroda 0:13a5d365ba16 108 /** \returns a WithFormat proxy object allowing to print a matrix the with given
ykuroda 0:13a5d365ba16 109 * format \a fmt.
ykuroda 0:13a5d365ba16 110 *
ykuroda 0:13a5d365ba16 111 * See class IOFormat for some examples.
ykuroda 0:13a5d365ba16 112 *
ykuroda 0:13a5d365ba16 113 * \sa class IOFormat, class WithFormat
ykuroda 0:13a5d365ba16 114 */
ykuroda 0:13a5d365ba16 115 template<typename Derived>
ykuroda 0:13a5d365ba16 116 inline const WithFormat<Derived>
ykuroda 0:13a5d365ba16 117 DenseBase<Derived>::format(const IOFormat& fmt) const
ykuroda 0:13a5d365ba16 118 {
ykuroda 0:13a5d365ba16 119 return WithFormat<Derived>(derived(), fmt);
ykuroda 0:13a5d365ba16 120 }
ykuroda 0:13a5d365ba16 121
ykuroda 0:13a5d365ba16 122 namespace internal {
ykuroda 0:13a5d365ba16 123
ykuroda 0:13a5d365ba16 124 template<typename Scalar, bool IsInteger>
ykuroda 0:13a5d365ba16 125 struct significant_decimals_default_impl
ykuroda 0:13a5d365ba16 126 {
ykuroda 0:13a5d365ba16 127 typedef typename NumTraits<Scalar>::Real RealScalar;
ykuroda 0:13a5d365ba16 128 static inline int run()
ykuroda 0:13a5d365ba16 129 {
ykuroda 0:13a5d365ba16 130 using std::ceil;
ykuroda 0:13a5d365ba16 131 using std::log;
ykuroda 0:13a5d365ba16 132 return cast<RealScalar,int>(ceil(-log(NumTraits<RealScalar>::epsilon())/log(RealScalar(10))));
ykuroda 0:13a5d365ba16 133 }
ykuroda 0:13a5d365ba16 134 };
ykuroda 0:13a5d365ba16 135
ykuroda 0:13a5d365ba16 136 template<typename Scalar>
ykuroda 0:13a5d365ba16 137 struct significant_decimals_default_impl<Scalar, true>
ykuroda 0:13a5d365ba16 138 {
ykuroda 0:13a5d365ba16 139 static inline int run()
ykuroda 0:13a5d365ba16 140 {
ykuroda 0:13a5d365ba16 141 return 0;
ykuroda 0:13a5d365ba16 142 }
ykuroda 0:13a5d365ba16 143 };
ykuroda 0:13a5d365ba16 144
ykuroda 0:13a5d365ba16 145 template<typename Scalar>
ykuroda 0:13a5d365ba16 146 struct significant_decimals_impl
ykuroda 0:13a5d365ba16 147 : significant_decimals_default_impl<Scalar, NumTraits<Scalar>::IsInteger>
ykuroda 0:13a5d365ba16 148 {};
ykuroda 0:13a5d365ba16 149
ykuroda 0:13a5d365ba16 150 /** \internal
ykuroda 0:13a5d365ba16 151 * print the matrix \a _m to the output stream \a s using the output format \a fmt */
ykuroda 0:13a5d365ba16 152 template<typename Derived>
ykuroda 0:13a5d365ba16 153 std::ostream & print_matrix(std::ostream & s, const Derived& _m, const IOFormat& fmt)
ykuroda 0:13a5d365ba16 154 {
ykuroda 0:13a5d365ba16 155 if(_m.size() == 0)
ykuroda 0:13a5d365ba16 156 {
ykuroda 0:13a5d365ba16 157 s << fmt.matPrefix << fmt.matSuffix;
ykuroda 0:13a5d365ba16 158 return s;
ykuroda 0:13a5d365ba16 159 }
ykuroda 0:13a5d365ba16 160
ykuroda 0:13a5d365ba16 161 typename Derived::Nested m = _m;
ykuroda 0:13a5d365ba16 162 typedef typename Derived::Scalar Scalar;
ykuroda 0:13a5d365ba16 163 typedef typename Derived::Index Index;
ykuroda 0:13a5d365ba16 164
ykuroda 0:13a5d365ba16 165 Index width = 0;
ykuroda 0:13a5d365ba16 166
ykuroda 0:13a5d365ba16 167 std::streamsize explicit_precision;
ykuroda 0:13a5d365ba16 168 if(fmt.precision == StreamPrecision)
ykuroda 0:13a5d365ba16 169 {
ykuroda 0:13a5d365ba16 170 explicit_precision = 0;
ykuroda 0:13a5d365ba16 171 }
ykuroda 0:13a5d365ba16 172 else if(fmt.precision == FullPrecision)
ykuroda 0:13a5d365ba16 173 {
ykuroda 0:13a5d365ba16 174 if (NumTraits<Scalar>::IsInteger)
ykuroda 0:13a5d365ba16 175 {
ykuroda 0:13a5d365ba16 176 explicit_precision = 0;
ykuroda 0:13a5d365ba16 177 }
ykuroda 0:13a5d365ba16 178 else
ykuroda 0:13a5d365ba16 179 {
ykuroda 0:13a5d365ba16 180 explicit_precision = significant_decimals_impl<Scalar>::run();
ykuroda 0:13a5d365ba16 181 }
ykuroda 0:13a5d365ba16 182 }
ykuroda 0:13a5d365ba16 183 else
ykuroda 0:13a5d365ba16 184 {
ykuroda 0:13a5d365ba16 185 explicit_precision = fmt.precision;
ykuroda 0:13a5d365ba16 186 }
ykuroda 0:13a5d365ba16 187
ykuroda 0:13a5d365ba16 188 std::streamsize old_precision = 0;
ykuroda 0:13a5d365ba16 189 if(explicit_precision) old_precision = s.precision(explicit_precision);
ykuroda 0:13a5d365ba16 190
ykuroda 0:13a5d365ba16 191 bool align_cols = !(fmt.flags & DontAlignCols);
ykuroda 0:13a5d365ba16 192 if(align_cols)
ykuroda 0:13a5d365ba16 193 {
ykuroda 0:13a5d365ba16 194 // compute the largest width
ykuroda 0:13a5d365ba16 195 for(Index j = 0; j < m.cols(); ++j)
ykuroda 0:13a5d365ba16 196 for(Index i = 0; i < m.rows(); ++i)
ykuroda 0:13a5d365ba16 197 {
ykuroda 0:13a5d365ba16 198 std::stringstream sstr;
ykuroda 0:13a5d365ba16 199 sstr.copyfmt(s);
ykuroda 0:13a5d365ba16 200 sstr << m.coeff(i,j);
ykuroda 0:13a5d365ba16 201 width = std::max<Index>(width, Index(sstr.str().length()));
ykuroda 0:13a5d365ba16 202 }
ykuroda 0:13a5d365ba16 203 }
ykuroda 0:13a5d365ba16 204 s << fmt.matPrefix;
ykuroda 0:13a5d365ba16 205 for(Index i = 0; i < m.rows(); ++i)
ykuroda 0:13a5d365ba16 206 {
ykuroda 0:13a5d365ba16 207 if (i)
ykuroda 0:13a5d365ba16 208 s << fmt.rowSpacer;
ykuroda 0:13a5d365ba16 209 s << fmt.rowPrefix;
ykuroda 0:13a5d365ba16 210 if(width) s.width(width);
ykuroda 0:13a5d365ba16 211 s << m.coeff(i, 0);
ykuroda 0:13a5d365ba16 212 for(Index j = 1; j < m.cols(); ++j)
ykuroda 0:13a5d365ba16 213 {
ykuroda 0:13a5d365ba16 214 s << fmt.coeffSeparator;
ykuroda 0:13a5d365ba16 215 if (width) s.width(width);
ykuroda 0:13a5d365ba16 216 s << m.coeff(i, j);
ykuroda 0:13a5d365ba16 217 }
ykuroda 0:13a5d365ba16 218 s << fmt.rowSuffix;
ykuroda 0:13a5d365ba16 219 if( i < m.rows() - 1)
ykuroda 0:13a5d365ba16 220 s << fmt.rowSeparator;
ykuroda 0:13a5d365ba16 221 }
ykuroda 0:13a5d365ba16 222 s << fmt.matSuffix;
ykuroda 0:13a5d365ba16 223 if(explicit_precision) s.precision(old_precision);
ykuroda 0:13a5d365ba16 224 return s;
ykuroda 0:13a5d365ba16 225 }
ykuroda 0:13a5d365ba16 226
ykuroda 0:13a5d365ba16 227 } // end namespace internal
ykuroda 0:13a5d365ba16 228
ykuroda 0:13a5d365ba16 229 /** \relates DenseBase
ykuroda 0:13a5d365ba16 230 *
ykuroda 0:13a5d365ba16 231 * Outputs the matrix, to the given stream.
ykuroda 0:13a5d365ba16 232 *
ykuroda 0:13a5d365ba16 233 * If you wish to print the matrix with a format different than the default, use DenseBase::format().
ykuroda 0:13a5d365ba16 234 *
ykuroda 0:13a5d365ba16 235 * It is also possible to change the default format by defining EIGEN_DEFAULT_IO_FORMAT before including Eigen headers.
ykuroda 0:13a5d365ba16 236 * If not defined, this will automatically be defined to Eigen::IOFormat(), that is the Eigen::IOFormat with default parameters.
ykuroda 0:13a5d365ba16 237 *
ykuroda 0:13a5d365ba16 238 * \sa DenseBase::format()
ykuroda 0:13a5d365ba16 239 */
ykuroda 0:13a5d365ba16 240 template<typename Derived>
ykuroda 0:13a5d365ba16 241 std::ostream & operator <<
ykuroda 0:13a5d365ba16 242 (std::ostream & s,
ykuroda 0:13a5d365ba16 243 const DenseBase<Derived> & m)
ykuroda 0:13a5d365ba16 244 {
ykuroda 0:13a5d365ba16 245 return internal::print_matrix(s, m.eval(), EIGEN_DEFAULT_IO_FORMAT);
ykuroda 0:13a5d365ba16 246 }
ykuroda 0:13a5d365ba16 247
ykuroda 0:13a5d365ba16 248 } // end namespace Eigen
ykuroda 0:13a5d365ba16 249
ykuroda 0:13a5d365ba16 250 #endif // EIGEN_IO_H