00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifndef EIGEN_AUTODIFF_VECTOR_H
00011 #define EIGEN_AUTODIFF_VECTOR_H
00012
00013 namespace Eigen {
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 template<typename ValueType, typename JacobianType>
00033 class AutoDiffVector
00034 {
00035 public:
00036
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
00064 Scalar sum() const { 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
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
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