Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:57:02

0001 // This file is part of Eigen, a lightweight C++ template library
0002 // for linear algebra.
0003 //
0004 // Copyright (C) 2009 Gael Guennebaud <gael.guennebaud@inria.fr>
0005 //
0006 // This Source Code Form is subject to the terms of the Mozilla
0007 // Public License v. 2.0. If a copy of the MPL was not distributed
0008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
0009 
0010 #ifndef EIGEN_AUTODIFF_VECTOR_H
0011 #define EIGEN_AUTODIFF_VECTOR_H
0012 
0013 namespace Eigen {
0014 
0015 /* \class AutoDiffScalar
0016   * \brief A scalar type replacement with automatic differentation capability
0017   *
0018   * \param DerType the vector type used to store/represent the derivatives (e.g. Vector3f)
0019   *
0020   * This class represents a scalar value while tracking its respective derivatives.
0021   *
0022   * It supports the following list of global math function:
0023   *  - std::abs, std::sqrt, std::pow, std::exp, std::log, std::sin, std::cos,
0024   *  - internal::abs, internal::sqrt, numext::pow, internal::exp, internal::log, internal::sin, internal::cos,
0025   *  - internal::conj, internal::real, internal::imag, numext::abs2.
0026   *
0027   * AutoDiffScalar can be used as the scalar type of an Eigen::Matrix object. However,
0028   * in that case, the expression template mechanism only occurs at the top Matrix level,
0029   * while derivatives are computed right away.
0030   *
0031   */
0032 template<typename ValueType, typename JacobianType>
0033 class AutoDiffVector
0034 {
0035   public:
0036     //typedef typename internal::traits<ValueType>::Scalar Scalar;
0037     typedef typename internal::traits<ValueType>::Scalar BaseScalar;
0038     typedef AutoDiffScalar<Matrix<BaseScalar,JacobianType::RowsAtCompileTime,1> > ActiveScalar;
0039     typedef ActiveScalar Scalar;
0040     typedef AutoDiffScalar<typename JacobianType::ColXpr> CoeffType;
0041     typedef typename JacobianType::Index Index;
0042 
0043     inline AutoDiffVector() {}
0044 
0045     inline AutoDiffVector(const ValueType& values)
0046       : m_values(values)
0047     {
0048       m_jacobian.setZero();
0049     }
0050 
0051 
0052     CoeffType operator[] (Index i) { return CoeffType(m_values[i], m_jacobian.col(i)); }
0053     const CoeffType operator[] (Index i) const { return CoeffType(m_values[i], m_jacobian.col(i)); }
0054 
0055     CoeffType operator() (Index i) { return CoeffType(m_values[i], m_jacobian.col(i)); }
0056     const CoeffType operator() (Index i) const { return CoeffType(m_values[i], m_jacobian.col(i)); }
0057 
0058     CoeffType coeffRef(Index i) { return CoeffType(m_values[i], m_jacobian.col(i)); }
0059     const CoeffType coeffRef(Index i) const { return CoeffType(m_values[i], m_jacobian.col(i)); }
0060 
0061     Index size() const { return m_values.size(); }
0062 
0063     // FIXME here we could return an expression of the sum
0064     Scalar sum() const { /*std::cerr << "sum \n\n";*/ /*std::cerr << m_jacobian.rowwise().sum() << "\n\n";*/ return Scalar(m_values.sum(), m_jacobian.rowwise().sum()); }
0065 
0066 
0067     inline AutoDiffVector(const ValueType& values, const JacobianType& jac)
0068       : m_values(values), m_jacobian(jac)
0069     {}
0070 
0071     template<typename OtherValueType, typename OtherJacobianType>
0072     inline AutoDiffVector(const AutoDiffVector<OtherValueType, OtherJacobianType>& other)
0073       : m_values(other.values()), m_jacobian(other.jacobian())
0074     {}
0075 
0076     inline AutoDiffVector(const AutoDiffVector& other)
0077       : m_values(other.values()), m_jacobian(other.jacobian())
0078     {}
0079 
0080     template<typename OtherValueType, typename OtherJacobianType>
0081     inline AutoDiffVector& operator=(const AutoDiffVector<OtherValueType, OtherJacobianType>& other)
0082     {
0083       m_values = other.values();
0084       m_jacobian = other.jacobian();
0085       return *this;
0086     }
0087 
0088     inline AutoDiffVector& operator=(const AutoDiffVector& other)
0089     {
0090       m_values = other.values();
0091       m_jacobian = other.jacobian();
0092       return *this;
0093     }
0094 
0095     inline const ValueType& values() const { return m_values; }
0096     inline ValueType& values() { return m_values; }
0097 
0098     inline const JacobianType& jacobian() const { return m_jacobian; }
0099     inline JacobianType& jacobian() { return m_jacobian; }
0100 
0101     template<typename OtherValueType,typename OtherJacobianType>
0102     inline const AutoDiffVector<
0103       typename MakeCwiseBinaryOp<internal::scalar_sum_op<BaseScalar>,ValueType,OtherValueType>::Type,
0104       typename MakeCwiseBinaryOp<internal::scalar_sum_op<BaseScalar>,JacobianType,OtherJacobianType>::Type >
0105     operator+(const AutoDiffVector<OtherValueType,OtherJacobianType>& other) const
0106     {
0107       return AutoDiffVector<
0108       typename MakeCwiseBinaryOp<internal::scalar_sum_op<BaseScalar>,ValueType,OtherValueType>::Type,
0109       typename MakeCwiseBinaryOp<internal::scalar_sum_op<BaseScalar>,JacobianType,OtherJacobianType>::Type >(
0110         m_values + other.values(),
0111         m_jacobian + other.jacobian());
0112     }
0113 
0114     template<typename OtherValueType, typename OtherJacobianType>
0115     inline AutoDiffVector&
0116     operator+=(const AutoDiffVector<OtherValueType,OtherJacobianType>& other)
0117     {
0118       m_values += other.values();
0119       m_jacobian += other.jacobian();
0120       return *this;
0121     }
0122 
0123     template<typename OtherValueType,typename OtherJacobianType>
0124     inline const AutoDiffVector<
0125       typename MakeCwiseBinaryOp<internal::scalar_difference_op<Scalar>,ValueType,OtherValueType>::Type,
0126       typename MakeCwiseBinaryOp<internal::scalar_difference_op<Scalar>,JacobianType,OtherJacobianType>::Type >
0127     operator-(const AutoDiffVector<OtherValueType,OtherJacobianType>& other) const
0128     {
0129       return AutoDiffVector<
0130         typename MakeCwiseBinaryOp<internal::scalar_difference_op<Scalar>,ValueType,OtherValueType>::Type,
0131         typename MakeCwiseBinaryOp<internal::scalar_difference_op<Scalar>,JacobianType,OtherJacobianType>::Type >(
0132           m_values - other.values(),
0133           m_jacobian - other.jacobian());
0134     }
0135 
0136     template<typename OtherValueType, typename OtherJacobianType>
0137     inline AutoDiffVector&
0138     operator-=(const AutoDiffVector<OtherValueType,OtherJacobianType>& other)
0139     {
0140       m_values -= other.values();
0141       m_jacobian -= other.jacobian();
0142       return *this;
0143     }
0144 
0145     inline const AutoDiffVector<
0146       typename MakeCwiseUnaryOp<internal::scalar_opposite_op<Scalar>, ValueType>::Type,
0147       typename MakeCwiseUnaryOp<internal::scalar_opposite_op<Scalar>, JacobianType>::Type >
0148     operator-() const
0149     {
0150       return AutoDiffVector<
0151         typename MakeCwiseUnaryOp<internal::scalar_opposite_op<Scalar>, ValueType>::Type,
0152         typename MakeCwiseUnaryOp<internal::scalar_opposite_op<Scalar>, JacobianType>::Type >(
0153           -m_values,
0154           -m_jacobian);
0155     }
0156 
0157     inline const AutoDiffVector<
0158       typename MakeCwiseUnaryOp<internal::scalar_multiple_op<Scalar>, ValueType>::Type,
0159       typename MakeCwiseUnaryOp<internal::scalar_multiple_op<Scalar>, JacobianType>::Type>
0160     operator*(const BaseScalar& other) const
0161     {
0162       return AutoDiffVector<
0163         typename MakeCwiseUnaryOp<internal::scalar_multiple_op<Scalar>, ValueType>::Type,
0164         typename MakeCwiseUnaryOp<internal::scalar_multiple_op<Scalar>, JacobianType>::Type >(
0165           m_values * other,
0166           m_jacobian * other);
0167     }
0168 
0169     friend inline const AutoDiffVector<
0170       typename MakeCwiseUnaryOp<internal::scalar_multiple_op<Scalar>, ValueType>::Type,
0171       typename MakeCwiseUnaryOp<internal::scalar_multiple_op<Scalar>, JacobianType>::Type >
0172     operator*(const Scalar& other, const AutoDiffVector& v)
0173     {
0174       return AutoDiffVector<
0175         typename MakeCwiseUnaryOp<internal::scalar_multiple_op<Scalar>, ValueType>::Type,
0176         typename MakeCwiseUnaryOp<internal::scalar_multiple_op<Scalar>, JacobianType>::Type >(
0177           v.values() * other,
0178           v.jacobian() * other);
0179     }
0180 
0181 //     template<typename OtherValueType,typename OtherJacobianType>
0182 //     inline const AutoDiffVector<
0183 //       CwiseBinaryOp<internal::scalar_multiple_op<Scalar>, ValueType, OtherValueType>
0184 //       CwiseBinaryOp<internal::scalar_sum_op<Scalar>,
0185 //         CwiseUnaryOp<internal::scalar_multiple_op<Scalar>, JacobianType>,
0186 //         CwiseUnaryOp<internal::scalar_multiple_op<Scalar>, OtherJacobianType> > >
0187 //     operator*(const AutoDiffVector<OtherValueType,OtherJacobianType>& other) const
0188 //     {
0189 //       return AutoDiffVector<
0190 //         CwiseBinaryOp<internal::scalar_multiple_op<Scalar>, ValueType, OtherValueType>
0191 //         CwiseBinaryOp<internal::scalar_sum_op<Scalar>,
0192 //           CwiseUnaryOp<internal::scalar_multiple_op<Scalar>, JacobianType>,
0193 //           CwiseUnaryOp<internal::scalar_multiple_op<Scalar>, OtherJacobianType> > >(
0194 //             m_values.cwise() * other.values(),
0195 //             (m_jacobian * other.values()) + (m_values * other.jacobian()));
0196 //     }
0197 
0198     inline AutoDiffVector& operator*=(const Scalar& other)
0199     {
0200       m_values *= other;
0201       m_jacobian *= other;
0202       return *this;
0203     }
0204 
0205     template<typename OtherValueType,typename OtherJacobianType>
0206     inline AutoDiffVector& operator*=(const AutoDiffVector<OtherValueType,OtherJacobianType>& other)
0207     {
0208       *this = *this * other;
0209       return *this;
0210     }
0211 
0212   protected:
0213     ValueType m_values;
0214     JacobianType m_jacobian;
0215 
0216 };
0217 
0218 }
0219 
0220 #endif // EIGEN_AUTODIFF_VECTOR_H