TR-mbed 1.0
Loading...
Searching...
No Matches
AutoDiffVector.h
Go to the documentation of this file.
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2009 Gael Guennebaud <gael.guennebaud@inria.fr>
5//
6// This Source Code Form is subject to the terms of the Mozilla
7// Public License v. 2.0. If a copy of the MPL was not distributed
8// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9
10#ifndef EIGEN_AUTODIFF_VECTOR_H
11#define EIGEN_AUTODIFF_VECTOR_H
12
13namespace Eigen {
14
15/* \class AutoDiffScalar
16 * \brief A scalar type replacement with automatic differentation capability
17 *
18 * \param DerType the vector type used to store/represent the derivatives (e.g. Vector3f)
19 *
20 * This class represents a scalar value while tracking its respective derivatives.
21 *
22 * It supports the following list of global math function:
23 * - std::abs, std::sqrt, std::pow, std::exp, std::log, std::sin, std::cos,
24 * - internal::abs, internal::sqrt, numext::pow, internal::exp, internal::log, internal::sin, internal::cos,
25 * - internal::conj, internal::real, internal::imag, numext::abs2.
26 *
27 * AutoDiffScalar can be used as the scalar type of an Eigen::Matrix object. However,
28 * in that case, the expression template mechanism only occurs at the top Matrix level,
29 * while derivatives are computed right away.
30 *
31 */
32template<typename ValueType, typename JacobianType>
34{
35 public:
36 //typedef typename internal::traits<ValueType>::Scalar Scalar;
41 typedef typename JacobianType::Index Index;
42
43 inline AutoDiffVector() {}
44
45 inline AutoDiffVector(const ValueType& values)
47 {
48 m_jacobian.setZero();
49 }
50
51
53 const CoeffType operator[] (Index i) const { return CoeffType(m_values[i], m_jacobian.col(i)); }
54
56 const CoeffType operator() (Index i) const { return CoeffType(m_values[i], m_jacobian.col(i)); }
57
59 const CoeffType coeffRef(Index i) const { return CoeffType(m_values[i], m_jacobian.col(i)); }
60
61 Index size() const { return m_values.size(); }
62
63 // FIXME here we could return an expression of the sum
64 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()); }
65
66
67 inline AutoDiffVector(const ValueType& values, const JacobianType& jac)
69 {}
70
71 template<typename OtherValueType, typename OtherJacobianType>
75
76 inline AutoDiffVector(const AutoDiffVector& other)
77 : m_values(other.values()), m_jacobian(other.jacobian())
78 {}
79
80 template<typename OtherValueType, typename OtherJacobianType>
82 {
83 m_values = other.values();
84 m_jacobian = other.jacobian();
85 return *this;
86 }
87
89 {
90 m_values = other.values();
91 m_jacobian = other.jacobian();
92 return *this;
93 }
94
95 inline const ValueType& values() const { return m_values; }
96 inline ValueType& values() { return m_values; }
97
98 inline const JacobianType& jacobian() const { return m_jacobian; }
99 inline JacobianType& jacobian() { return m_jacobian; }
100
101 template<typename OtherValueType,typename OtherJacobianType>
102 inline const AutoDiffVector<
103 typename MakeCwiseBinaryOp<internal::scalar_sum_op<BaseScalar>,ValueType,OtherValueType>::Type,
104 typename MakeCwiseBinaryOp<internal::scalar_sum_op<BaseScalar>,JacobianType,OtherJacobianType>::Type >
106 {
107 return AutoDiffVector<
108 typename MakeCwiseBinaryOp<internal::scalar_sum_op<BaseScalar>,ValueType,OtherValueType>::Type,
109 typename MakeCwiseBinaryOp<internal::scalar_sum_op<BaseScalar>,JacobianType,OtherJacobianType>::Type >(
110 m_values + other.values(),
111 m_jacobian + other.jacobian());
112 }
113
114 template<typename OtherValueType, typename OtherJacobianType>
115 inline AutoDiffVector&
117 {
118 m_values += other.values();
119 m_jacobian += other.jacobian();
120 return *this;
121 }
122
123 template<typename OtherValueType,typename OtherJacobianType>
124 inline const AutoDiffVector<
125 typename MakeCwiseBinaryOp<internal::scalar_difference_op<Scalar>,ValueType,OtherValueType>::Type,
126 typename MakeCwiseBinaryOp<internal::scalar_difference_op<Scalar>,JacobianType,OtherJacobianType>::Type >
128 {
129 return AutoDiffVector<
130 typename MakeCwiseBinaryOp<internal::scalar_difference_op<Scalar>,ValueType,OtherValueType>::Type,
131 typename MakeCwiseBinaryOp<internal::scalar_difference_op<Scalar>,JacobianType,OtherJacobianType>::Type >(
132 m_values - other.values(),
133 m_jacobian - other.jacobian());
134 }
135
136 template<typename OtherValueType, typename OtherJacobianType>
137 inline AutoDiffVector&
139 {
140 m_values -= other.values();
141 m_jacobian -= other.jacobian();
142 return *this;
143 }
144
145 inline const AutoDiffVector<
146 typename MakeCwiseUnaryOp<internal::scalar_opposite_op<Scalar>, ValueType>::Type,
147 typename MakeCwiseUnaryOp<internal::scalar_opposite_op<Scalar>, JacobianType>::Type >
148 operator-() const
149 {
150 return AutoDiffVector<
151 typename MakeCwiseUnaryOp<internal::scalar_opposite_op<Scalar>, ValueType>::Type,
152 typename MakeCwiseUnaryOp<internal::scalar_opposite_op<Scalar>, JacobianType>::Type >(
153 -m_values,
154 -m_jacobian);
155 }
156
157 inline const AutoDiffVector<
158 typename MakeCwiseUnaryOp<internal::scalar_multiple_op<Scalar>, ValueType>::Type,
159 typename MakeCwiseUnaryOp<internal::scalar_multiple_op<Scalar>, JacobianType>::Type>
160 operator*(const BaseScalar& other) const
161 {
162 return AutoDiffVector<
163 typename MakeCwiseUnaryOp<internal::scalar_multiple_op<Scalar>, ValueType>::Type,
164 typename MakeCwiseUnaryOp<internal::scalar_multiple_op<Scalar>, JacobianType>::Type >(
165 m_values * other,
166 m_jacobian * other);
167 }
168
169 friend inline const AutoDiffVector<
170 typename MakeCwiseUnaryOp<internal::scalar_multiple_op<Scalar>, ValueType>::Type,
171 typename MakeCwiseUnaryOp<internal::scalar_multiple_op<Scalar>, JacobianType>::Type >
172 operator*(const Scalar& other, const AutoDiffVector& v)
173 {
174 return AutoDiffVector<
175 typename MakeCwiseUnaryOp<internal::scalar_multiple_op<Scalar>, ValueType>::Type,
176 typename MakeCwiseUnaryOp<internal::scalar_multiple_op<Scalar>, JacobianType>::Type >(
177 v.values() * other,
178 v.jacobian() * other);
179 }
180
181// template<typename OtherValueType,typename OtherJacobianType>
182// inline const AutoDiffVector<
183// CwiseBinaryOp<internal::scalar_multiple_op<Scalar>, ValueType, OtherValueType>
184// CwiseBinaryOp<internal::scalar_sum_op<Scalar>,
185// CwiseUnaryOp<internal::scalar_multiple_op<Scalar>, JacobianType>,
186// CwiseUnaryOp<internal::scalar_multiple_op<Scalar>, OtherJacobianType> > >
187// operator*(const AutoDiffVector<OtherValueType,OtherJacobianType>& other) const
188// {
189// return AutoDiffVector<
190// CwiseBinaryOp<internal::scalar_multiple_op<Scalar>, ValueType, OtherValueType>
191// CwiseBinaryOp<internal::scalar_sum_op<Scalar>,
192// CwiseUnaryOp<internal::scalar_multiple_op<Scalar>, JacobianType>,
193// CwiseUnaryOp<internal::scalar_multiple_op<Scalar>, OtherJacobianType> > >(
194// m_values.cwise() * other.values(),
195// (m_jacobian * other.values()) + (m_values * other.jacobian()));
196// }
197
198 inline AutoDiffVector& operator*=(const Scalar& other)
199 {
200 m_values *= other;
201 m_jacobian *= other;
202 return *this;
203 }
204
205 template<typename OtherValueType,typename OtherJacobianType>
207 {
208 *this = *this * other;
209 return *this;
210 }
211
212 protected:
213 ValueType m_values;
214 JacobianType m_jacobian;
215
216};
217
218}
219
220#endif // EIGEN_AUTODIFF_VECTOR_H
Array< int, Dynamic, 1 > v
Definition Array_initializer_list_vector_cxx11.cpp:1
int i
Definition BiCGSTAB_step_by_step.cpp:9
A scalar type replacement with automatic differentiation capability.
Definition AutoDiffScalar.h:71
Definition AutoDiffVector.h:34
AutoDiffScalar< Matrix< BaseScalar, JacobianType::RowsAtCompileTime, 1 > > ActiveScalar
Definition AutoDiffVector.h:38
AutoDiffVector & operator=(const AutoDiffVector< OtherValueType, OtherJacobianType > &other)
Definition AutoDiffVector.h:81
friend const AutoDiffVector< typename MakeCwiseUnaryOp< internal::scalar_multiple_op< Scalar >, ValueType >::Type, typename MakeCwiseUnaryOp< internal::scalar_multiple_op< Scalar >, JacobianType >::Type > operator*(const Scalar &other, const AutoDiffVector &v)
Definition AutoDiffVector.h:172
Index size() const
Definition AutoDiffVector.h:61
const AutoDiffVector< typename MakeCwiseUnaryOp< internal::scalar_multiple_op< Scalar >, ValueType >::Type, typename MakeCwiseUnaryOp< internal::scalar_multiple_op< Scalar >, JacobianType >::Type > operator*(const BaseScalar &other) const
Definition AutoDiffVector.h:160
ValueType m_values
Definition AutoDiffVector.h:213
const AutoDiffVector< typename MakeCwiseBinaryOp< internal::scalar_difference_op< Scalar >, ValueType, OtherValueType >::Type, typename MakeCwiseBinaryOp< internal::scalar_difference_op< Scalar >, JacobianType, OtherJacobianType >::Type > operator-(const AutoDiffVector< OtherValueType, OtherJacobianType > &other) const
Definition AutoDiffVector.h:127
AutoDiffScalar< typename JacobianType::ColXpr > CoeffType
Definition AutoDiffVector.h:40
AutoDiffVector & operator=(const AutoDiffVector &other)
Definition AutoDiffVector.h:88
const JacobianType & jacobian() const
Definition AutoDiffVector.h:98
CoeffType coeffRef(Index i)
Definition AutoDiffVector.h:58
AutoDiffVector(const ValueType &values)
Definition AutoDiffVector.h:45
CoeffType operator[](Index i)
Definition AutoDiffVector.h:52
JacobianType::Index Index
Definition AutoDiffVector.h:41
AutoDiffVector & operator+=(const AutoDiffVector< OtherValueType, OtherJacobianType > &other)
Definition AutoDiffVector.h:116
const AutoDiffVector< typename MakeCwiseBinaryOp< internal::scalar_sum_op< BaseScalar >, ValueType, OtherValueType >::Type, typename MakeCwiseBinaryOp< internal::scalar_sum_op< BaseScalar >, JacobianType, OtherJacobianType >::Type > operator+(const AutoDiffVector< OtherValueType, OtherJacobianType > &other) const
Definition AutoDiffVector.h:105
AutoDiffVector()
Definition AutoDiffVector.h:43
AutoDiffVector(const ValueType &values, const JacobianType &jac)
Definition AutoDiffVector.h:67
JacobianType & jacobian()
Definition AutoDiffVector.h:99
internal::traits< ValueType >::Scalar BaseScalar
Definition AutoDiffVector.h:37
AutoDiffVector & operator*=(const Scalar &other)
Definition AutoDiffVector.h:198
const ValueType & values() const
Definition AutoDiffVector.h:95
const CoeffType coeffRef(Index i) const
Definition AutoDiffVector.h:59
AutoDiffVector(const AutoDiffVector &other)
Definition AutoDiffVector.h:76
ValueType & values()
Definition AutoDiffVector.h:96
const AutoDiffVector< typename MakeCwiseUnaryOp< internal::scalar_opposite_op< Scalar >, ValueType >::Type, typename MakeCwiseUnaryOp< internal::scalar_opposite_op< Scalar >, JacobianType >::Type > operator-() const
Definition AutoDiffVector.h:148
AutoDiffVector & operator-=(const AutoDiffVector< OtherValueType, OtherJacobianType > &other)
Definition AutoDiffVector.h:138
AutoDiffVector & operator*=(const AutoDiffVector< OtherValueType, OtherJacobianType > &other)
Definition AutoDiffVector.h:206
AutoDiffVector(const AutoDiffVector< OtherValueType, OtherJacobianType > &other)
Definition AutoDiffVector.h:72
CoeffType operator()(Index i)
Definition AutoDiffVector.h:55
ActiveScalar Scalar
Definition AutoDiffVector.h:39
JacobianType m_jacobian
Definition AutoDiffVector.h:214
Scalar sum() const
Definition AutoDiffVector.h:64
Namespace containing all symbols from the Eigen library.
Definition bench_norm.cpp:85
Definition ForwardDeclarations.h:17