// This file is part of Eigen, a lightweight C++ template library // for linear algebra. // // Copyright (C) 2017 Gael Guennebaud <gael.guennebaud@inria.fr> // // This Source Code Form is subject to the terms of the Mozilla // Public License v. 2.0. If a copy of the MPL was not distributed // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
/** \namespace Eigen::symbolic * \ingroup Core_Module * * This namespace defines a set of classes and functions to build and evaluate symbolic expressions of scalar type Index. * Here is a simple example: * * \code * // First step, defines symbols: * struct x_tag {}; static const symbolic::SymbolExpr<x_tag> x; * struct y_tag {}; static const symbolic::SymbolExpr<y_tag> y; * struct z_tag {}; static const symbolic::SymbolExpr<z_tag> z; * * // Defines an expression: * auto expr = (x+3)/y+z; * * // And evaluate it: (c++14) * std::cout << expr.eval(x=6,y=3,z=-13) << "\n"; * * // In c++98/11, only one symbol per expression is supported for now: * auto expr98 = (3-x)/2; * std::cout << expr98.eval(x=6) << "\n"; * \endcode * * It is currently only used internally to define and manipulate the Eigen::last and Eigen::lastp1 symbols in Eigen::seq and Eigen::seqN. *
*/ namespace symbolic {
template<typename Tag> class Symbol; template<typename Arg0> class NegateExpr; template<typename Arg1,typename Arg2> class AddExpr; template<typename Arg1,typename Arg2> class ProductExpr; template<typename Arg1,typename Arg2> class QuotientExpr;
// A simple wrapper around an integral value to provide the eval method. // We could also use a free-function symbolic_eval... template<typename IndexType=Index> class ValueExpr { public:
ValueExpr(IndexType val) : m_value(val) {} template<typename T>
IndexType eval_impl(const T&) const { return m_value; } protected:
IndexType m_value;
};
// Specialization for compile-time value, // It is similar to ValueExpr(N) but this version helps the compiler to generate better code. template<int N> class ValueExpr<internal::FixedInt<N> > { public:
ValueExpr() {} template<typename T>
EIGEN_CONSTEXPR Index eval_impl(const T&) const { return N; }
};
/** \class BaseExpr * \ingroup Core_Module * Common base class of any symbolic expressions
*/ template<typename Derived> class BaseExpr
{ public: const Derived& derived() const { return *static_cast<const Derived*>(this); }
/** Evaluate the expression given the \a values of the symbols. * * \param values defines the values of the symbols, it can either be a SymbolValue or a std::tuple of SymbolValue * as constructed by SymbolExpr::operator= operator. *
*/ template<typename T>
Index eval(const T& values) const { return derived().eval_impl(values); }
template<typename T> struct is_symbolic { // BaseExpr has no conversion ctor, so we only have to check whether T can be statically cast to its base class BaseExpr<T>. enum { value = internal::is_convertible<T,BaseExpr<T> >::value };
};
/** Represents the actual value of a symbol identified by its tag * * It is the return type of SymbolValue::operator=, and most of the time this is only way it is used.
*/ template<typename Tag> class SymbolValue
{ public: /** Default constructor from the value \a val */
SymbolValue(Index val) : m_value(val) {}
/** \returns the stored value of the symbol */
Index value() const { return m_value; } protected:
Index m_value;
};
/** Expression of a symbol uniquely identified by the template parameter type \c tag */ template<typename tag> class SymbolExpr : public BaseExpr<SymbolExpr<tag> >
{ public: /** Alias to the template parameter \c tag */ typedef tag Tag;
SymbolExpr() {}
/** Associate the value \a val to the given symbol \c *this, uniquely identified by its \c Tag. * * The returned object should be passed to ExprBase::eval() to evaluate a given expression with this specified runtime-time value.
*/
SymbolValue<Tag> operator=(Index val) const { return SymbolValue<Tag>(val);
}
Index eval_impl(const SymbolValue<Tag> &values) const { return values.value(); }
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.