$treeview $search $mathjax
Eigen-unsupported
3.2.5
$projectbrief
|
$projectbrief
|
$searchbox |
00001 // This file is part of Eigen, a lightweight C++ template library 00002 // for linear algebra. 00003 // 00004 // Copyright (C) 2009 Gael Guennebaud <gael.guennebaud@inria.fr> 00005 // 00006 // This Source Code Form is subject to the terms of the Mozilla 00007 // Public License v. 2.0. If a copy of the MPL was not distributed 00008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 00009 00010 #ifndef EIGEN_AUTODIFF_VECTOR_H 00011 #define EIGEN_AUTODIFF_VECTOR_H 00012 00013 namespace Eigen { 00014 00015 /* \class AutoDiffScalar 00016 * \brief A scalar type replacement with automatic differentation capability 00017 * 00018 * \param DerType the vector type used to store/represent the derivatives (e.g. Vector3f) 00019 * 00020 * This class represents a scalar value while tracking its respective derivatives. 00021 * 00022 * It supports the following list of global math function: 00023 * - std::abs, std::sqrt, std::pow, std::exp, std::log, std::sin, std::cos, 00024 * - internal::abs, internal::sqrt, numext::pow, internal::exp, internal::log, internal::sin, internal::cos, 00025 * - internal::conj, internal::real, internal::imag, numext::abs2. 00026 * 00027 * AutoDiffScalar can be used as the scalar type of an Eigen::Matrix object. However, 00028 * in that case, the expression template mechanism only occurs at the top Matrix level, 00029 * while derivatives are computed right away. 00030 * 00031 */ 00032 template<typename ValueType, typename JacobianType> 00033 class AutoDiffVector 00034 { 00035 public: 00036 //typedef typename internal::traits<ValueType>::Scalar Scalar; 00037 typedef typename internal::traits<ValueType>::Scalar BaseScalar; 00038 typedef AutoDiffScalar<Matrix<BaseScalar,JacobianType::RowsAtCompileTime,1> > ActiveScalar; 00039 typedef ActiveScalar Scalar; 00040 typedef AutoDiffScalar<typename JacobianType::ColXpr> CoeffType; 00041 typedef typename JacobianType::Index Index; 00042 00043 inline AutoDiffVector() {} 00044 00045 inline AutoDiffVector(const ValueType& values) 00046 : m_values(values) 00047 { 00048 m_jacobian.setZero(); 00049 } 00050 00051 00052 CoeffType operator[] (Index i) { return CoeffType(m_values[i], m_jacobian.col(i)); } 00053 const CoeffType operator[] (Index i) const { return CoeffType(m_values[i], m_jacobian.col(i)); } 00054 00055 CoeffType operator() (Index i) { return CoeffType(m_values[i], m_jacobian.col(i)); } 00056 const CoeffType operator() (Index i) const { return CoeffType(m_values[i], m_jacobian.col(i)); } 00057 00058 CoeffType coeffRef(Index i) { return CoeffType(m_values[i], m_jacobian.col(i)); } 00059 const CoeffType coeffRef(Index i) const { return CoeffType(m_values[i], m_jacobian.col(i)); } 00060 00061 Index size() const { return m_values.size(); } 00062 00063 // FIXME here we could return an expression of the sum 00064 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()); } 00065 00066 00067 inline AutoDiffVector(const ValueType& values, const JacobianType& jac) 00068 : m_values(values), m_jacobian(jac) 00069 {} 00070 00071 template<typename OtherValueType, typename OtherJacobianType> 00072 inline AutoDiffVector(const AutoDiffVector<OtherValueType, OtherJacobianType>& other) 00073 : m_values(other.values()), m_jacobian(other.jacobian()) 00074 {} 00075 00076 inline AutoDiffVector(const AutoDiffVector& other) 00077 : m_values(other.values()), m_jacobian(other.jacobian()) 00078 {} 00079 00080 template<typename OtherValueType, typename OtherJacobianType> 00081 inline AutoDiffVector& operator=(const AutoDiffVector<OtherValueType, OtherJacobianType>& other) 00082 { 00083 m_values = other.values(); 00084 m_jacobian = other.jacobian(); 00085 return *this; 00086 } 00087 00088 inline AutoDiffVector& operator=(const AutoDiffVector& other) 00089 { 00090 m_values = other.values(); 00091 m_jacobian = other.jacobian(); 00092 return *this; 00093 } 00094 00095 inline const ValueType& values() const { return m_values; } 00096 inline ValueType& values() { return m_values; } 00097 00098 inline const JacobianType& jacobian() const { return m_jacobian; } 00099 inline JacobianType& jacobian() { return m_jacobian; } 00100 00101 template<typename OtherValueType,typename OtherJacobianType> 00102 inline const AutoDiffVector< 00103 typename MakeCwiseBinaryOp<internal::scalar_sum_op<BaseScalar>,ValueType,OtherValueType>::Type, 00104 typename MakeCwiseBinaryOp<internal::scalar_sum_op<BaseScalar>,JacobianType,OtherJacobianType>::Type > 00105 operator+(const AutoDiffVector<OtherValueType,OtherJacobianType>& other) const 00106 { 00107 return AutoDiffVector< 00108 typename MakeCwiseBinaryOp<internal::scalar_sum_op<BaseScalar>,ValueType,OtherValueType>::Type, 00109 typename MakeCwiseBinaryOp<internal::scalar_sum_op<BaseScalar>,JacobianType,OtherJacobianType>::Type >( 00110 m_values + other.values(), 00111 m_jacobian + other.jacobian()); 00112 } 00113 00114 template<typename OtherValueType, typename OtherJacobianType> 00115 inline AutoDiffVector& 00116 operator+=(const AutoDiffVector<OtherValueType,OtherJacobianType>& other) 00117 { 00118 m_values += other.values(); 00119 m_jacobian += other.jacobian(); 00120 return *this; 00121 } 00122 00123 template<typename OtherValueType,typename OtherJacobianType> 00124 inline const AutoDiffVector< 00125 typename MakeCwiseBinaryOp<internal::scalar_difference_op<Scalar>,ValueType,OtherValueType>::Type, 00126 typename MakeCwiseBinaryOp<internal::scalar_difference_op<Scalar>,JacobianType,OtherJacobianType>::Type > 00127 operator-(const AutoDiffVector<OtherValueType,OtherJacobianType>& other) const 00128 { 00129 return AutoDiffVector< 00130 typename MakeCwiseBinaryOp<internal::scalar_difference_op<Scalar>,ValueType,OtherValueType>::Type, 00131 typename MakeCwiseBinaryOp<internal::scalar_difference_op<Scalar>,JacobianType,OtherJacobianType>::Type >( 00132 m_values - other.values(), 00133 m_jacobian - other.jacobian()); 00134 } 00135 00136 template<typename OtherValueType, typename OtherJacobianType> 00137 inline AutoDiffVector& 00138 operator-=(const AutoDiffVector<OtherValueType,OtherJacobianType>& other) 00139 { 00140 m_values -= other.values(); 00141 m_jacobian -= other.jacobian(); 00142 return *this; 00143 } 00144 00145 inline const AutoDiffVector< 00146 typename MakeCwiseUnaryOp<internal::scalar_opposite_op<Scalar>, ValueType>::Type, 00147 typename MakeCwiseUnaryOp<internal::scalar_opposite_op<Scalar>, JacobianType>::Type > 00148 operator-() const 00149 { 00150 return AutoDiffVector< 00151 typename MakeCwiseUnaryOp<internal::scalar_opposite_op<Scalar>, ValueType>::Type, 00152 typename MakeCwiseUnaryOp<internal::scalar_opposite_op<Scalar>, JacobianType>::Type >( 00153 -m_values, 00154 -m_jacobian); 00155 } 00156 00157 inline const AutoDiffVector< 00158 typename MakeCwiseUnaryOp<internal::scalar_multiple_op<Scalar>, ValueType>::Type, 00159 typename MakeCwiseUnaryOp<internal::scalar_multiple_op<Scalar>, JacobianType>::Type> 00160 operator*(const BaseScalar& other) const 00161 { 00162 return AutoDiffVector< 00163 typename MakeCwiseUnaryOp<internal::scalar_multiple_op<Scalar>, ValueType>::Type, 00164 typename MakeCwiseUnaryOp<internal::scalar_multiple_op<Scalar>, JacobianType>::Type >( 00165 m_values * other, 00166 m_jacobian * other); 00167 } 00168 00169 friend inline const AutoDiffVector< 00170 typename MakeCwiseUnaryOp<internal::scalar_multiple_op<Scalar>, ValueType>::Type, 00171 typename MakeCwiseUnaryOp<internal::scalar_multiple_op<Scalar>, JacobianType>::Type > 00172 operator*(const Scalar& other, const AutoDiffVector& v) 00173 { 00174 return AutoDiffVector< 00175 typename MakeCwiseUnaryOp<internal::scalar_multiple_op<Scalar>, ValueType>::Type, 00176 typename MakeCwiseUnaryOp<internal::scalar_multiple_op<Scalar>, JacobianType>::Type >( 00177 v.values() * other, 00178 v.jacobian() * other); 00179 } 00180 00181 // template<typename OtherValueType,typename OtherJacobianType> 00182 // inline const AutoDiffVector< 00183 // CwiseBinaryOp<internal::scalar_multiple_op<Scalar>, ValueType, OtherValueType> 00184 // CwiseBinaryOp<internal::scalar_sum_op<Scalar>, 00185 // CwiseUnaryOp<internal::scalar_multiple_op<Scalar>, JacobianType>, 00186 // CwiseUnaryOp<internal::scalar_multiple_op<Scalar>, OtherJacobianType> > > 00187 // operator*(const AutoDiffVector<OtherValueType,OtherJacobianType>& other) const 00188 // { 00189 // return AutoDiffVector< 00190 // CwiseBinaryOp<internal::scalar_multiple_op<Scalar>, ValueType, OtherValueType> 00191 // CwiseBinaryOp<internal::scalar_sum_op<Scalar>, 00192 // CwiseUnaryOp<internal::scalar_multiple_op<Scalar>, JacobianType>, 00193 // CwiseUnaryOp<internal::scalar_multiple_op<Scalar>, OtherJacobianType> > >( 00194 // m_values.cwise() * other.values(), 00195 // (m_jacobian * other.values()) + (m_values * other.jacobian())); 00196 // } 00197 00198 inline AutoDiffVector& operator*=(const Scalar& other) 00199 { 00200 m_values *= other; 00201 m_jacobian *= other; 00202 return *this; 00203 } 00204 00205 template<typename OtherValueType,typename OtherJacobianType> 00206 inline AutoDiffVector& operator*=(const AutoDiffVector<OtherValueType,OtherJacobianType>& other) 00207 { 00208 *this = *this * other; 00209 return *this; 00210 } 00211 00212 protected: 00213 ValueType m_values; 00214 JacobianType m_jacobian; 00215 00216 }; 00217 00218 } 00219 00220 #endif // EIGEN_AUTODIFF_VECTOR_H