Eigen-unsupported  5.0.1-dev+284dcc12
 
Loading...
Searching...
No Matches
AutoDiffVector.h
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
13// IWYU pragma: private
14#include "./InternalHeaderCheck.h"
15
16namespace Eigen {
17
18/* \class AutoDiffScalar
19 * \brief A scalar type replacement with automatic differentation capability
20 *
21 * \param DerType the vector type used to store/represent the derivatives (e.g. Vector3f)
22 *
23 * This class represents a scalar value while tracking its respective derivatives.
24 *
25 * It supports the following list of global math function:
26 * - std::abs, std::sqrt, std::pow, std::exp, std::log, std::sin, std::cos,
27 * - internal::abs, internal::sqrt, numext::pow, internal::exp, internal::log, internal::sin, internal::cos,
28 * - internal::conj, internal::real, internal::imag, numext::abs2.
29 *
30 * AutoDiffScalar can be used as the scalar type of an Eigen::Matrix object. However,
31 * in that case, the expression template mechanism only occurs at the top Matrix level,
32 * while derivatives are computed right away.
33 *
34 */
35template <typename ValueType, typename JacobianType>
36class AutoDiffVector {
37 public:
38 // typedef typename internal::traits<ValueType>::Scalar Scalar;
39 typedef typename internal::traits<ValueType>::Scalar BaseScalar;
40 typedef AutoDiffScalar<Matrix<BaseScalar, JacobianType::RowsAtCompileTime, 1> > ActiveScalar;
41 typedef ActiveScalar Scalar;
42 typedef AutoDiffScalar<typename JacobianType::ColXpr> CoeffType;
43 typedef typename JacobianType::Index Index;
44
45 inline AutoDiffVector() {}
46
47 inline AutoDiffVector(const ValueType& values) : m_values(values) { m_jacobian.setZero(); }
48
49 CoeffType operator[](Index i) { return CoeffType(m_values[i], m_jacobian.col(i)); }
50 const CoeffType operator[](Index i) const { return CoeffType(m_values[i], m_jacobian.col(i)); }
51
52 CoeffType operator()(Index i) { return CoeffType(m_values[i], m_jacobian.col(i)); }
53 const CoeffType operator()(Index i) const { return CoeffType(m_values[i], m_jacobian.col(i)); }
54
55 CoeffType coeffRef(Index i) { return CoeffType(m_values[i], m_jacobian.col(i)); }
56 const CoeffType coeffRef(Index i) const { return CoeffType(m_values[i], m_jacobian.col(i)); }
57
58 Index size() const { return m_values.size(); }
59
60 // FIXME here we could return an expression of the sum
61 Scalar sum() const { /*std::cerr << "sum \n\n";*/ /*std::cerr << m_jacobian.rowwise().sum() << "\n\n";*/
62 return Scalar(m_values.sum(), m_jacobian.rowwise().sum());
63 }
64
65 inline AutoDiffVector(const ValueType& values, const JacobianType& jac) : m_values(values), m_jacobian(jac) {}
66
67 template <typename OtherValueType, typename OtherJacobianType>
68 inline AutoDiffVector(const AutoDiffVector<OtherValueType, OtherJacobianType>& other)
69 : m_values(other.values()), m_jacobian(other.jacobian()) {}
70
71 inline AutoDiffVector(const AutoDiffVector& other) : m_values(other.values()), m_jacobian(other.jacobian()) {}
72
73 template <typename OtherValueType, typename OtherJacobianType>
74 inline AutoDiffVector& operator=(const AutoDiffVector<OtherValueType, OtherJacobianType>& other) {
75 m_values = other.values();
76 m_jacobian = other.jacobian();
77 return *this;
78 }
79
80 inline AutoDiffVector& operator=(const AutoDiffVector& other) {
81 m_values = other.values();
82 m_jacobian = other.jacobian();
83 return *this;
84 }
85
86 inline const ValueType& values() const { return m_values; }
87 inline ValueType& values() { return m_values; }
88
89 inline const JacobianType& jacobian() const { return m_jacobian; }
90 inline JacobianType& jacobian() { return m_jacobian; }
91
92 template <typename OtherValueType, typename OtherJacobianType>
93 inline const AutoDiffVector<
94 typename MakeCwiseBinaryOp<internal::scalar_sum_op<BaseScalar>, ValueType, OtherValueType>::Type,
95 typename MakeCwiseBinaryOp<internal::scalar_sum_op<BaseScalar>, JacobianType, OtherJacobianType>::Type>
96 operator+(const AutoDiffVector<OtherValueType, OtherJacobianType>& other) const {
97 return AutoDiffVector<
98 typename MakeCwiseBinaryOp<internal::scalar_sum_op<BaseScalar>, ValueType, OtherValueType>::Type,
99 typename MakeCwiseBinaryOp<internal::scalar_sum_op<BaseScalar>, JacobianType, OtherJacobianType>::Type>(
100 m_values + other.values(), m_jacobian + other.jacobian());
101 }
102
103 template <typename OtherValueType, typename OtherJacobianType>
104 inline AutoDiffVector& operator+=(const AutoDiffVector<OtherValueType, OtherJacobianType>& other) {
105 m_values += other.values();
106 m_jacobian += other.jacobian();
107 return *this;
108 }
109
110 template <typename OtherValueType, typename OtherJacobianType>
111 inline const AutoDiffVector<
112 typename MakeCwiseBinaryOp<internal::scalar_difference_op<Scalar>, ValueType, OtherValueType>::Type,
113 typename MakeCwiseBinaryOp<internal::scalar_difference_op<Scalar>, JacobianType, OtherJacobianType>::Type>
114 operator-(const AutoDiffVector<OtherValueType, OtherJacobianType>& other) const {
115 return AutoDiffVector<
116 typename MakeCwiseBinaryOp<internal::scalar_difference_op<Scalar>, ValueType, OtherValueType>::Type,
117 typename MakeCwiseBinaryOp<internal::scalar_difference_op<Scalar>, JacobianType, OtherJacobianType>::Type>(
118 m_values - other.values(), m_jacobian - other.jacobian());
119 }
120
121 template <typename OtherValueType, typename OtherJacobianType>
122 inline AutoDiffVector& operator-=(const AutoDiffVector<OtherValueType, OtherJacobianType>& other) {
123 m_values -= other.values();
124 m_jacobian -= other.jacobian();
125 return *this;
126 }
127
128 inline const AutoDiffVector<typename MakeCwiseUnaryOp<internal::scalar_opposite_op<Scalar>, ValueType>::Type,
129 typename MakeCwiseUnaryOp<internal::scalar_opposite_op<Scalar>, JacobianType>::Type>
130 operator-() const {
131 return AutoDiffVector<typename MakeCwiseUnaryOp<internal::scalar_opposite_op<Scalar>, ValueType>::Type,
132 typename MakeCwiseUnaryOp<internal::scalar_opposite_op<Scalar>, JacobianType>::Type>(
133 -m_values, -m_jacobian);
134 }
135
136 inline const AutoDiffVector<typename MakeCwiseUnaryOp<internal::scalar_multiple_op<Scalar>, ValueType>::Type,
137 typename MakeCwiseUnaryOp<internal::scalar_multiple_op<Scalar>, JacobianType>::Type>
138 operator*(const BaseScalar& other) const {
139 return AutoDiffVector<typename MakeCwiseUnaryOp<internal::scalar_multiple_op<Scalar>, ValueType>::Type,
140 typename MakeCwiseUnaryOp<internal::scalar_multiple_op<Scalar>, JacobianType>::Type>(
141 m_values * other, m_jacobian * other);
142 }
143
144 friend inline const AutoDiffVector<
145 typename MakeCwiseUnaryOp<internal::scalar_multiple_op<Scalar>, ValueType>::Type,
146 typename MakeCwiseUnaryOp<internal::scalar_multiple_op<Scalar>, JacobianType>::Type>
147 operator*(const Scalar& other, const AutoDiffVector& v) {
148 return AutoDiffVector<typename MakeCwiseUnaryOp<internal::scalar_multiple_op<Scalar>, ValueType>::Type,
149 typename MakeCwiseUnaryOp<internal::scalar_multiple_op<Scalar>, JacobianType>::Type>(
150 v.values() * other, v.jacobian() * other);
151 }
152
153 // template<typename OtherValueType,typename OtherJacobianType>
154 // inline const AutoDiffVector<
155 // CwiseBinaryOp<internal::scalar_multiple_op<Scalar>, ValueType, OtherValueType>
156 // CwiseBinaryOp<internal::scalar_sum_op<Scalar>,
157 // CwiseUnaryOp<internal::scalar_multiple_op<Scalar>, JacobianType>,
158 // CwiseUnaryOp<internal::scalar_multiple_op<Scalar>, OtherJacobianType> > >
159 // operator*(const AutoDiffVector<OtherValueType,OtherJacobianType>& other) const
160 // {
161 // return AutoDiffVector<
162 // CwiseBinaryOp<internal::scalar_multiple_op<Scalar>, ValueType, OtherValueType>
163 // CwiseBinaryOp<internal::scalar_sum_op<Scalar>,
164 // CwiseUnaryOp<internal::scalar_multiple_op<Scalar>, JacobianType>,
165 // CwiseUnaryOp<internal::scalar_multiple_op<Scalar>, OtherJacobianType> > >(
166 // m_values.cwise() * other.values(),
167 // (m_jacobian * other.values()) + (m_values * other.jacobian()));
168 // }
169
170 inline AutoDiffVector& operator*=(const Scalar& other) {
171 m_values *= other;
172 m_jacobian *= other;
173 return *this;
174 }
175
176 template <typename OtherValueType, typename OtherJacobianType>
177 inline AutoDiffVector& operator*=(const AutoDiffVector<OtherValueType, OtherJacobianType>& other) {
178 *this = *this * other;
179 return *this;
180 }
181
182 protected:
183 ValueType m_values;
184 JacobianType m_jacobian;
185};
186
187} // namespace Eigen
188
189#endif // EIGEN_AUTODIFF_VECTOR_H
Namespace containing all symbols from the Eigen library.