Loading...
Searching...
No Matches
NestedAutoDiff.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4
5#ifndef GPR_DERIVATIVES_NESTEDAUTODIFF_H_
6#define GPR_DERIVATIVES_NESTEDAUTODIFF_H_
7
8#include <cmath>
9#include <unsupported/Eigen/AutoDiff>
10
11namespace gpr::derivatives {
12
13template <typename T>
14struct Lift {
15 template <typename Value>
16 static T from(const Value& value)
17 {
18 return T(value);
19 }
20};
21
24template <typename Derivatives>
25struct Lift<Eigen::AutoDiffScalar<Derivatives>> {
26 using Type = Eigen::AutoDiffScalar<Derivatives>;
27 template <typename Value>
28 static Type from(const Value& value)
29 {
31 }
32};
33
34template <typename T, typename Value>
35T lift(const Value& value)
36{
37 return Lift<T>::from(value);
38}
39
40inline double nestedAtan2(double y, double x)
41{
42 return std::atan2(y, x);
43}
44
47template <typename Derivatives>
48Eigen::AutoDiffScalar<Derivatives> nestedAtan2(
49 const Eigen::AutoDiffScalar<Derivatives>& y,
50 const Eigen::AutoDiffScalar<Derivatives>& x)
51{
52 using Type = Eigen::AutoDiffScalar<Derivatives>;
53 const auto squared = x.value() * x.value() + y.value() * y.value();
54 return Type(
55 nestedAtan2(y.value(), x.value()),
56 (y.derivatives() * x.value() - x.derivatives() * y.value()) / squared);
57}
58
59} // namespace gpr::derivatives
60
61#endif // GPR_DERIVATIVES_NESTEDAUTODIFF_H_
@ x
Definition Enums.h:28
@ y
Definition Enums.h:28
double nestedAtan2(double y, double x)
T lift(const Value &value)
static T from(const Value &value)