Eigne Matrix Class Library
Dependents: MPC_current_control HydraulicControlBoard_SW AHRS Test_ekf ... more
src/Core/Visitor.h@1:3b8049da21b8, 2019-09-24 (annotated)
- 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?
User | Revision | Line number | New 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) 2008 Gael Guennebaud <gael.guennebaud@inria.fr> |
ykuroda | 0:13a5d365ba16 | 5 | // |
ykuroda | 0:13a5d365ba16 | 6 | // This Source Code Form is subject to the terms of the Mozilla |
ykuroda | 0:13a5d365ba16 | 7 | // Public License v. 2.0. If a copy of the MPL was not distributed |
ykuroda | 0:13a5d365ba16 | 8 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. |
ykuroda | 0:13a5d365ba16 | 9 | |
ykuroda | 0:13a5d365ba16 | 10 | #ifndef EIGEN_VISITOR_H |
ykuroda | 0:13a5d365ba16 | 11 | #define EIGEN_VISITOR_H |
ykuroda | 0:13a5d365ba16 | 12 | |
ykuroda | 0:13a5d365ba16 | 13 | namespace Eigen { |
ykuroda | 0:13a5d365ba16 | 14 | |
ykuroda | 0:13a5d365ba16 | 15 | namespace internal { |
ykuroda | 0:13a5d365ba16 | 16 | |
ykuroda | 0:13a5d365ba16 | 17 | template<typename Visitor, typename Derived, int UnrollCount> |
ykuroda | 0:13a5d365ba16 | 18 | struct visitor_impl |
ykuroda | 0:13a5d365ba16 | 19 | { |
ykuroda | 0:13a5d365ba16 | 20 | enum { |
ykuroda | 0:13a5d365ba16 | 21 | col = (UnrollCount-1) / Derived::RowsAtCompileTime, |
ykuroda | 0:13a5d365ba16 | 22 | row = (UnrollCount-1) % Derived::RowsAtCompileTime |
ykuroda | 0:13a5d365ba16 | 23 | }; |
ykuroda | 0:13a5d365ba16 | 24 | |
ykuroda | 0:13a5d365ba16 | 25 | static inline void run(const Derived &mat, Visitor& visitor) |
ykuroda | 0:13a5d365ba16 | 26 | { |
ykuroda | 0:13a5d365ba16 | 27 | visitor_impl<Visitor, Derived, UnrollCount-1>::run(mat, visitor); |
ykuroda | 0:13a5d365ba16 | 28 | visitor(mat.coeff(row, col), row, col); |
ykuroda | 0:13a5d365ba16 | 29 | } |
ykuroda | 0:13a5d365ba16 | 30 | }; |
ykuroda | 0:13a5d365ba16 | 31 | |
ykuroda | 0:13a5d365ba16 | 32 | template<typename Visitor, typename Derived> |
ykuroda | 0:13a5d365ba16 | 33 | struct visitor_impl<Visitor, Derived, 1> |
ykuroda | 0:13a5d365ba16 | 34 | { |
ykuroda | 0:13a5d365ba16 | 35 | static inline void run(const Derived &mat, Visitor& visitor) |
ykuroda | 0:13a5d365ba16 | 36 | { |
ykuroda | 0:13a5d365ba16 | 37 | return visitor.init(mat.coeff(0, 0), 0, 0); |
ykuroda | 0:13a5d365ba16 | 38 | } |
ykuroda | 0:13a5d365ba16 | 39 | }; |
ykuroda | 0:13a5d365ba16 | 40 | |
ykuroda | 0:13a5d365ba16 | 41 | template<typename Visitor, typename Derived> |
ykuroda | 0:13a5d365ba16 | 42 | struct visitor_impl<Visitor, Derived, Dynamic> |
ykuroda | 0:13a5d365ba16 | 43 | { |
ykuroda | 0:13a5d365ba16 | 44 | typedef typename Derived::Index Index; |
ykuroda | 0:13a5d365ba16 | 45 | static inline void run(const Derived& mat, Visitor& visitor) |
ykuroda | 0:13a5d365ba16 | 46 | { |
ykuroda | 0:13a5d365ba16 | 47 | visitor.init(mat.coeff(0,0), 0, 0); |
ykuroda | 0:13a5d365ba16 | 48 | for(Index i = 1; i < mat.rows(); ++i) |
ykuroda | 0:13a5d365ba16 | 49 | visitor(mat.coeff(i, 0), i, 0); |
ykuroda | 0:13a5d365ba16 | 50 | for(Index j = 1; j < mat.cols(); ++j) |
ykuroda | 0:13a5d365ba16 | 51 | for(Index i = 0; i < mat.rows(); ++i) |
ykuroda | 0:13a5d365ba16 | 52 | visitor(mat.coeff(i, j), i, j); |
ykuroda | 0:13a5d365ba16 | 53 | } |
ykuroda | 0:13a5d365ba16 | 54 | }; |
ykuroda | 0:13a5d365ba16 | 55 | |
ykuroda | 0:13a5d365ba16 | 56 | } // end namespace internal |
ykuroda | 0:13a5d365ba16 | 57 | |
ykuroda | 0:13a5d365ba16 | 58 | /** Applies the visitor \a visitor to the whole coefficients of the matrix or vector. |
ykuroda | 0:13a5d365ba16 | 59 | * |
ykuroda | 0:13a5d365ba16 | 60 | * The template parameter \a Visitor is the type of the visitor and provides the following interface: |
ykuroda | 0:13a5d365ba16 | 61 | * \code |
ykuroda | 0:13a5d365ba16 | 62 | * struct MyVisitor { |
ykuroda | 0:13a5d365ba16 | 63 | * // called for the first coefficient |
ykuroda | 0:13a5d365ba16 | 64 | * void init(const Scalar& value, Index i, Index j); |
ykuroda | 0:13a5d365ba16 | 65 | * // called for all other coefficients |
ykuroda | 0:13a5d365ba16 | 66 | * void operator() (const Scalar& value, Index i, Index j); |
ykuroda | 0:13a5d365ba16 | 67 | * }; |
ykuroda | 0:13a5d365ba16 | 68 | * \endcode |
ykuroda | 0:13a5d365ba16 | 69 | * |
ykuroda | 0:13a5d365ba16 | 70 | * \note compared to one or two \em for \em loops, visitors offer automatic |
ykuroda | 0:13a5d365ba16 | 71 | * unrolling for small fixed size matrix. |
ykuroda | 0:13a5d365ba16 | 72 | * |
ykuroda | 0:13a5d365ba16 | 73 | * \sa minCoeff(Index*,Index*), maxCoeff(Index*,Index*), DenseBase::redux() |
ykuroda | 0:13a5d365ba16 | 74 | */ |
ykuroda | 0:13a5d365ba16 | 75 | template<typename Derived> |
ykuroda | 0:13a5d365ba16 | 76 | template<typename Visitor> |
ykuroda | 0:13a5d365ba16 | 77 | void DenseBase<Derived>::visit(Visitor& visitor) const |
ykuroda | 0:13a5d365ba16 | 78 | { |
ykuroda | 0:13a5d365ba16 | 79 | typedef typename internal::remove_all<typename Derived::Nested>::type ThisNested; |
ykuroda | 0:13a5d365ba16 | 80 | typename Derived::Nested thisNested(derived()); |
ykuroda | 0:13a5d365ba16 | 81 | |
ykuroda | 0:13a5d365ba16 | 82 | enum { unroll = SizeAtCompileTime != Dynamic |
ykuroda | 0:13a5d365ba16 | 83 | && CoeffReadCost != Dynamic |
ykuroda | 0:13a5d365ba16 | 84 | && (SizeAtCompileTime == 1 || internal::functor_traits<Visitor>::Cost != Dynamic) |
ykuroda | 0:13a5d365ba16 | 85 | && SizeAtCompileTime * CoeffReadCost + (SizeAtCompileTime-1) * internal::functor_traits<Visitor>::Cost |
ykuroda | 0:13a5d365ba16 | 86 | <= EIGEN_UNROLLING_LIMIT }; |
ykuroda | 0:13a5d365ba16 | 87 | return internal::visitor_impl<Visitor, ThisNested, |
ykuroda | 0:13a5d365ba16 | 88 | unroll ? int(SizeAtCompileTime) : Dynamic |
ykuroda | 0:13a5d365ba16 | 89 | >::run(thisNested, visitor); |
ykuroda | 0:13a5d365ba16 | 90 | } |
ykuroda | 0:13a5d365ba16 | 91 | |
ykuroda | 0:13a5d365ba16 | 92 | namespace internal { |
ykuroda | 0:13a5d365ba16 | 93 | |
ykuroda | 0:13a5d365ba16 | 94 | /** \internal |
ykuroda | 0:13a5d365ba16 | 95 | * \brief Base class to implement min and max visitors |
ykuroda | 0:13a5d365ba16 | 96 | */ |
ykuroda | 0:13a5d365ba16 | 97 | template <typename Derived> |
ykuroda | 0:13a5d365ba16 | 98 | struct coeff_visitor |
ykuroda | 0:13a5d365ba16 | 99 | { |
ykuroda | 0:13a5d365ba16 | 100 | typedef typename Derived::Index Index; |
ykuroda | 0:13a5d365ba16 | 101 | typedef typename Derived::Scalar Scalar; |
ykuroda | 0:13a5d365ba16 | 102 | Index row, col; |
ykuroda | 0:13a5d365ba16 | 103 | Scalar res; |
ykuroda | 0:13a5d365ba16 | 104 | inline void init(const Scalar& value, Index i, Index j) |
ykuroda | 0:13a5d365ba16 | 105 | { |
ykuroda | 0:13a5d365ba16 | 106 | res = value; |
ykuroda | 0:13a5d365ba16 | 107 | row = i; |
ykuroda | 0:13a5d365ba16 | 108 | col = j; |
ykuroda | 0:13a5d365ba16 | 109 | } |
ykuroda | 0:13a5d365ba16 | 110 | }; |
ykuroda | 0:13a5d365ba16 | 111 | |
ykuroda | 0:13a5d365ba16 | 112 | /** \internal |
ykuroda | 0:13a5d365ba16 | 113 | * \brief Visitor computing the min coefficient with its value and coordinates |
ykuroda | 0:13a5d365ba16 | 114 | * |
ykuroda | 0:13a5d365ba16 | 115 | * \sa DenseBase::minCoeff(Index*, Index*) |
ykuroda | 0:13a5d365ba16 | 116 | */ |
ykuroda | 0:13a5d365ba16 | 117 | template <typename Derived> |
ykuroda | 0:13a5d365ba16 | 118 | struct min_coeff_visitor : coeff_visitor<Derived> |
ykuroda | 0:13a5d365ba16 | 119 | { |
ykuroda | 0:13a5d365ba16 | 120 | typedef typename Derived::Index Index; |
ykuroda | 0:13a5d365ba16 | 121 | typedef typename Derived::Scalar Scalar; |
ykuroda | 0:13a5d365ba16 | 122 | void operator() (const Scalar& value, Index i, Index j) |
ykuroda | 0:13a5d365ba16 | 123 | { |
ykuroda | 0:13a5d365ba16 | 124 | if(value < this->res) |
ykuroda | 0:13a5d365ba16 | 125 | { |
ykuroda | 0:13a5d365ba16 | 126 | this->res = value; |
ykuroda | 0:13a5d365ba16 | 127 | this->row = i; |
ykuroda | 0:13a5d365ba16 | 128 | this->col = j; |
ykuroda | 0:13a5d365ba16 | 129 | } |
ykuroda | 0:13a5d365ba16 | 130 | } |
ykuroda | 0:13a5d365ba16 | 131 | }; |
ykuroda | 0:13a5d365ba16 | 132 | |
ykuroda | 0:13a5d365ba16 | 133 | template<typename Scalar> |
ykuroda | 0:13a5d365ba16 | 134 | struct functor_traits<min_coeff_visitor<Scalar> > { |
ykuroda | 0:13a5d365ba16 | 135 | enum { |
ykuroda | 0:13a5d365ba16 | 136 | Cost = NumTraits<Scalar>::AddCost |
ykuroda | 0:13a5d365ba16 | 137 | }; |
ykuroda | 0:13a5d365ba16 | 138 | }; |
ykuroda | 0:13a5d365ba16 | 139 | |
ykuroda | 0:13a5d365ba16 | 140 | /** \internal |
ykuroda | 0:13a5d365ba16 | 141 | * \brief Visitor computing the max coefficient with its value and coordinates |
ykuroda | 0:13a5d365ba16 | 142 | * |
ykuroda | 0:13a5d365ba16 | 143 | * \sa DenseBase::maxCoeff(Index*, Index*) |
ykuroda | 0:13a5d365ba16 | 144 | */ |
ykuroda | 0:13a5d365ba16 | 145 | template <typename Derived> |
ykuroda | 0:13a5d365ba16 | 146 | struct max_coeff_visitor : coeff_visitor<Derived> |
ykuroda | 0:13a5d365ba16 | 147 | { |
ykuroda | 0:13a5d365ba16 | 148 | typedef typename Derived::Index Index; |
ykuroda | 0:13a5d365ba16 | 149 | typedef typename Derived::Scalar Scalar; |
ykuroda | 0:13a5d365ba16 | 150 | void operator() (const Scalar& value, Index i, Index j) |
ykuroda | 0:13a5d365ba16 | 151 | { |
ykuroda | 0:13a5d365ba16 | 152 | if(value > this->res) |
ykuroda | 0:13a5d365ba16 | 153 | { |
ykuroda | 0:13a5d365ba16 | 154 | this->res = value; |
ykuroda | 0:13a5d365ba16 | 155 | this->row = i; |
ykuroda | 0:13a5d365ba16 | 156 | this->col = j; |
ykuroda | 0:13a5d365ba16 | 157 | } |
ykuroda | 0:13a5d365ba16 | 158 | } |
ykuroda | 0:13a5d365ba16 | 159 | }; |
ykuroda | 0:13a5d365ba16 | 160 | |
ykuroda | 0:13a5d365ba16 | 161 | template<typename Scalar> |
ykuroda | 0:13a5d365ba16 | 162 | struct functor_traits<max_coeff_visitor<Scalar> > { |
ykuroda | 0:13a5d365ba16 | 163 | enum { |
ykuroda | 0:13a5d365ba16 | 164 | Cost = NumTraits<Scalar>::AddCost |
ykuroda | 0:13a5d365ba16 | 165 | }; |
ykuroda | 0:13a5d365ba16 | 166 | }; |
ykuroda | 0:13a5d365ba16 | 167 | |
ykuroda | 0:13a5d365ba16 | 168 | } // end namespace internal |
ykuroda | 0:13a5d365ba16 | 169 | |
ykuroda | 0:13a5d365ba16 | 170 | /** \returns the minimum of all coefficients of *this and puts in *row and *col its location. |
ykuroda | 0:13a5d365ba16 | 171 | * \warning the result is undefined if \c *this contains NaN. |
ykuroda | 0:13a5d365ba16 | 172 | * |
ykuroda | 0:13a5d365ba16 | 173 | * \sa DenseBase::minCoeff(Index*), DenseBase::maxCoeff(Index*,Index*), DenseBase::visitor(), DenseBase::minCoeff() |
ykuroda | 0:13a5d365ba16 | 174 | */ |
ykuroda | 0:13a5d365ba16 | 175 | template<typename Derived> |
ykuroda | 0:13a5d365ba16 | 176 | template<typename IndexType> |
ykuroda | 0:13a5d365ba16 | 177 | typename internal::traits<Derived>::Scalar |
ykuroda | 0:13a5d365ba16 | 178 | DenseBase<Derived>::minCoeff(IndexType* rowId, IndexType* colId) const |
ykuroda | 0:13a5d365ba16 | 179 | { |
ykuroda | 0:13a5d365ba16 | 180 | internal::min_coeff_visitor<Derived> minVisitor; |
ykuroda | 0:13a5d365ba16 | 181 | this->visit(minVisitor); |
ykuroda | 0:13a5d365ba16 | 182 | *rowId = minVisitor.row; |
ykuroda | 0:13a5d365ba16 | 183 | if (colId) *colId = minVisitor.col; |
ykuroda | 0:13a5d365ba16 | 184 | return minVisitor.res; |
ykuroda | 0:13a5d365ba16 | 185 | } |
ykuroda | 0:13a5d365ba16 | 186 | |
ykuroda | 0:13a5d365ba16 | 187 | /** \returns the minimum of all coefficients of *this and puts in *index its location. |
ykuroda | 0:13a5d365ba16 | 188 | * \warning the result is undefined if \c *this contains NaN. |
ykuroda | 0:13a5d365ba16 | 189 | * |
ykuroda | 0:13a5d365ba16 | 190 | * \sa DenseBase::minCoeff(IndexType*,IndexType*), DenseBase::maxCoeff(IndexType*,IndexType*), DenseBase::visitor(), DenseBase::minCoeff() |
ykuroda | 0:13a5d365ba16 | 191 | */ |
ykuroda | 0:13a5d365ba16 | 192 | template<typename Derived> |
ykuroda | 0:13a5d365ba16 | 193 | template<typename IndexType> |
ykuroda | 0:13a5d365ba16 | 194 | typename internal::traits<Derived>::Scalar |
ykuroda | 0:13a5d365ba16 | 195 | DenseBase<Derived>::minCoeff(IndexType* index) const |
ykuroda | 0:13a5d365ba16 | 196 | { |
ykuroda | 0:13a5d365ba16 | 197 | EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived) |
ykuroda | 0:13a5d365ba16 | 198 | internal::min_coeff_visitor<Derived> minVisitor; |
ykuroda | 0:13a5d365ba16 | 199 | this->visit(minVisitor); |
ykuroda | 0:13a5d365ba16 | 200 | *index = (RowsAtCompileTime==1) ? minVisitor.col : minVisitor.row; |
ykuroda | 0:13a5d365ba16 | 201 | return minVisitor.res; |
ykuroda | 0:13a5d365ba16 | 202 | } |
ykuroda | 0:13a5d365ba16 | 203 | |
ykuroda | 0:13a5d365ba16 | 204 | /** \returns the maximum of all coefficients of *this and puts in *row and *col its location. |
ykuroda | 0:13a5d365ba16 | 205 | * \warning the result is undefined if \c *this contains NaN. |
ykuroda | 0:13a5d365ba16 | 206 | * |
ykuroda | 0:13a5d365ba16 | 207 | * \sa DenseBase::minCoeff(IndexType*,IndexType*), DenseBase::visitor(), DenseBase::maxCoeff() |
ykuroda | 0:13a5d365ba16 | 208 | */ |
ykuroda | 0:13a5d365ba16 | 209 | template<typename Derived> |
ykuroda | 0:13a5d365ba16 | 210 | template<typename IndexType> |
ykuroda | 0:13a5d365ba16 | 211 | typename internal::traits<Derived>::Scalar |
ykuroda | 0:13a5d365ba16 | 212 | DenseBase<Derived>::maxCoeff(IndexType* rowPtr, IndexType* colPtr) const |
ykuroda | 0:13a5d365ba16 | 213 | { |
ykuroda | 0:13a5d365ba16 | 214 | internal::max_coeff_visitor<Derived> maxVisitor; |
ykuroda | 0:13a5d365ba16 | 215 | this->visit(maxVisitor); |
ykuroda | 0:13a5d365ba16 | 216 | *rowPtr = maxVisitor.row; |
ykuroda | 0:13a5d365ba16 | 217 | if (colPtr) *colPtr = maxVisitor.col; |
ykuroda | 0:13a5d365ba16 | 218 | return maxVisitor.res; |
ykuroda | 0:13a5d365ba16 | 219 | } |
ykuroda | 0:13a5d365ba16 | 220 | |
ykuroda | 0:13a5d365ba16 | 221 | /** \returns the maximum of all coefficients of *this and puts in *index its location. |
ykuroda | 0:13a5d365ba16 | 222 | * \warning the result is undefined if \c *this contains NaN. |
ykuroda | 0:13a5d365ba16 | 223 | * |
ykuroda | 0:13a5d365ba16 | 224 | * \sa DenseBase::maxCoeff(IndexType*,IndexType*), DenseBase::minCoeff(IndexType*,IndexType*), DenseBase::visitor(), DenseBase::maxCoeff() |
ykuroda | 0:13a5d365ba16 | 225 | */ |
ykuroda | 0:13a5d365ba16 | 226 | template<typename Derived> |
ykuroda | 0:13a5d365ba16 | 227 | template<typename IndexType> |
ykuroda | 0:13a5d365ba16 | 228 | typename internal::traits<Derived>::Scalar |
ykuroda | 0:13a5d365ba16 | 229 | DenseBase<Derived>::maxCoeff(IndexType* index) const |
ykuroda | 0:13a5d365ba16 | 230 | { |
ykuroda | 0:13a5d365ba16 | 231 | EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived) |
ykuroda | 0:13a5d365ba16 | 232 | internal::max_coeff_visitor<Derived> maxVisitor; |
ykuroda | 0:13a5d365ba16 | 233 | this->visit(maxVisitor); |
ykuroda | 0:13a5d365ba16 | 234 | *index = (RowsAtCompileTime==1) ? maxVisitor.col : maxVisitor.row; |
ykuroda | 0:13a5d365ba16 | 235 | return maxVisitor.res; |
ykuroda | 0:13a5d365ba16 | 236 | } |
ykuroda | 0:13a5d365ba16 | 237 | |
ykuroda | 0:13a5d365ba16 | 238 | } // end namespace Eigen |
ykuroda | 0:13a5d365ba16 | 239 | |
ykuroda | 0:13a5d365ba16 | 240 | #endif // EIGEN_VISITOR_H |