File indexing completed on 2025-01-18 09:57:02
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef EIGEN_AUTODIFF_VECTOR_H
0011 #define EIGEN_AUTODIFF_VECTOR_H
0012
0013 namespace Eigen {
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032 template<typename ValueType, typename JacobianType>
0033 class AutoDiffVector
0034 {
0035 public:
0036
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
0064 Scalar sum() const { 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
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194
0195
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