-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqm_derivative.h
176 lines (156 loc) · 4 KB
/
qm_derivative.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#ifndef QM_DERIVATIVE_H_
#define QM_DERIVATIVE_H_
#include "quick_math.h"
#include <array>
#include <functional>
namespace QM{
struct FRetVal{
float val;
float slope;
};
using FCONST=float;
using FVAR=float;
template <size_t N>
struct FDynamicInput
{
std::array<FRetVal, N> param;
size_t currVar;
};
constexpr FRetVal ad_make_const(float c)noexcept
{
return {c,0};
}
constexpr FRetVal ad_make_var(FCONST k, FVAR x, FCONST d)noexcept
{
return {k*x+d,k};
}
constexpr FRetVal operator+(const FRetVal& A, const FRetVal& B)
{
return {A.val+B.val,A.slope+B.slope};
}
constexpr FRetVal operator-(const FRetVal& A, const FRetVal& B)
{
return {A.val-B.val,A.slope-B.slope};
}
constexpr FRetVal operator*(const FRetVal& A, const FRetVal& B)
{
return {A.val*B.val,A.slope*B.val+A.val*B.slope};
}
constexpr FRetVal operator/(const FRetVal& A, const FRetVal& B)
{
return {A.val/B.val,(A.slope*B.val-A.val*B.slope)/(B.val*B.val)};
}
constexpr FRetVal operator+(const FRetVal& A, FCONST c)
{
return {A.val+c,A.slope};
}
constexpr FRetVal operator+(FCONST c, const FRetVal& A)
{
return {A.val+c,A.slope};
}
constexpr FRetVal operator-(const FRetVal& A, FCONST c)
{
return {A.val-c,A.slope};
}
constexpr FRetVal operator*(const FRetVal& A, FCONST c)
{
return {A.val*c,A.slope*c};
}
constexpr FRetVal operator*(FCONST c,const FRetVal& A)
{
return {A.val*c,A.slope*c};
}
constexpr FRetVal operator/(const FRetVal& A, FCONST c)
{
return {A.val/c,A.slope/c};
}
constexpr FRetVal ad_sin(const FRetVal& v)noexcept
{
return {QM::sin(v.val),QM::cos(v.val)*v.slope};
}
constexpr FRetVal ad_cos(const FRetVal& v)noexcept
{
return {QM::cos(v.val),-QM::sin(v.val)*v.slope};
}
constexpr FRetVal ad_sinh(const FRetVal& v)noexcept
{
return {QM::sinh(v.val),QM::cosh(v.val)*v.slope};
}
constexpr FRetVal ad_cosh(const FRetVal& v)noexcept
{
return {QM::cosh(v.val),QM::sinh(v.val)*v.slope};
}
constexpr FRetVal ad_exp(const FRetVal& v)noexcept
{
return {QM::exp(v.val),QM::exp(v.val)*v.slope};
}
constexpr FRetVal ad_ln(const FRetVal& v)noexcept
{
return {QM::ln(v.val),v.slope/v.val};
}
constexpr FRetVal ad_pow(const FRetVal& v, FCONST y)noexcept
{
return {QM::pow(v.val,y),y*QM::pow(v.val,y-1)*v.slope};
}
constexpr FRetVal ad_sqrt(const FRetVal& v)noexcept
{
const float res{QM::sqrt(v.val)};
return {res,0.5f*v.slope/res};
}
constexpr FRetVal ad_one_over(const FRetVal& v)noexcept
{
return {1/v.val,-1.0f/(v.val*v.val)*v.slope};
}
constexpr FRetVal ad_tan(const FRetVal& v)noexcept
{
const float res{QM::tan(v.val)};
return {res,(1+res*res)*v.slope};
}
constexpr FRetVal ad_tanh(const FRetVal& v)noexcept
{
const float res{QM::tanh(v.val)};
return {res,(1-res*res)*v.slope};
}
template<size_t N>
constexpr std::array<FRetVal, N> ad_gradient(const std::function<FRetVal(FDynamicInput<N>)>& fun,const std::array<FRetVal, N>& params)noexcept
{
std::array<FRetVal, N> retVal;
for(size_t i=0; i<N; ++i)retVal.at(i)=fun({params,i});
return retVal;
}
template<size_t N>
constexpr float finite_difference(const std::function<float(float)>& fun, float param)
{
const float eps{0.5f/N};
return N*fun(param+eps)-N*fun(param-eps);
}
template <size_t N>
constexpr float newton_iteration(const std::function<FRetVal(FRetVal)>& fun, float guess)
{
for(size_t i=0; i<N; ++i)
{
const FRetVal y=fun(guess);
guess-=y.val/y.slope;
}
return guess;
}
template <size_t N>
constexpr FRetVal fixpoint_iteration(const std::function<FRetVal(FRetVal)>& fun, FRetVal guess)
{
for(size_t i=0; i<N; ++i)guess=fun(guess);
return guess;
}
template <size_t N>
constexpr float fixpoint_iteration(const std::function<float(float)>& fun, float guess)
{
for(size_t i=0; i<N; ++i)guess=fun(guess);
return guess;
}
template <size_t N>
constexpr float newton_iteration(const std::function<float(float)>& fun, float guess)
{
for(size_t i=0; i<N; ++i)guess-=fun(guess)/finite_difference<1000>(fun,guess);
return guess;
}
}
#endif