-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnitSmoothFunction.h
525 lines (447 loc) · 17.2 KB
/
UnitSmoothFunction.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
//---------------------------------------------------------------------------
#ifndef UnitSmoothFunctionH
#define UnitSmoothFunctionH
//---------------------------------------------------------------------------
// Include
//---------------------------------------------------------------------------
#include <cmath>
//---------------------------------------------------------------------------
namespace NSApplication {
namespace NSLibrary {
// ---------------------------------------------------------------------------
// Rad Studio is unable to specialize template parameters correctly
// in AuxiliaryPower function template.
// Therefore I have to use them explicitly
template<int Number>
class ResidueMod2 {
public:
static const int Result = Number % 2;
};
// ---------------------------------------------------------------------------
template<int Number, int ResidueMod2 = ResidueMod2<Number>::Result>
class AuxiliaryPower {
public:
inline static double evaluate(double base, double semiResult) {
return semiResult;
}
};
// ---------------------------------------------------------------------------
template<int Number>
class AuxiliaryPower<Number, 0> {
public:
inline static double evaluate(double base, double semiResult) {
return AuxiliaryPower<Number / 2,
ResidueMod2<Number / 2>::Result>
::evaluate(base * base, semiResult);
}
};
// ---------------------------------------------------------------------------
template<int Number>
class AuxiliaryPower<Number, 1> {
public:
inline static double evaluate(double base, double semiResult) {
return AuxiliaryPower<Number - 1,
ResidueMod2<Number - 1>::Result>
::evaluate(base, base * semiResult);
}
};
// ---------------------------------------------------------------------------
template<int Number>
class AuxiliaryPower<Number, -1> {
public:
inline static double evaluate(double base, double semiResult) {
return AuxiliaryPower<Number + 1,
ResidueMod2<Number + 1>::Result>
::evaluate(base, semiResult / base);
}
};
// ---------------------------------------------------------------------------
template<>
class AuxiliaryPower<0, 0> {
public:
inline static double evaluate(double base, double semiResult) {
return semiResult;
}
};
// ---------------------------------------------------------------------------
// Fast Power Rising function (including negative ones)
template<int Number>
class Power {
public:
inline static double evaluate(double argument) {
return AuxiliaryPower<Number>::evaluate(argument, 1.0);
}
};
// ---------------------------------------------------------------------------
// Declaration of EFunctionType
// ---------------------------------------------------------------------------
// Function marker is needed for correct computation of a derivative
enum class EFunctionType {
Argument, Constant, Minus, Power, Exp, Log, Sin, Cos, Sqrt,
Sum, Difference, Product, Quotient, Composition
};
// ---------------------------------------------------------------------------
// Basic Smooth Functions
// ---------------------------------------------------------------------------
// Smooth function is a class with two members:
// static constant FunctionType, which is a marker of the function
// static method ::evaluate(argument), which computes the function
// Smooth functions are divided into two groups: (1) Basic (2) Operations
// Basic Functions:
// CTArg::evaluate(x) = x
// CTConstant::evaluate(x) = CTConstant::Constant
// <- is a compilation time constant
// CTZero::evaluate(x) = 0
// CTOne::evaluate(x) = 1
// CTPower<n>::evaluate(x) = x^n
// CTExponent::evaluate(x) = e^x
// CTLogarithm::evaluate(x) = ln(x)
// CTSinus::evaluate(x) = sin(x)
// CTCosinus::evaluate(x) = cos(x)
// CTSqrt::evaluate(x) = sqrt(x)
// CTPi::evaluate(x) = \pi
// CTStandardNormalDensity::evaluate(x) = e^(-(x^2)/2) / sqrt(2 * M_PI)
// Operations allow construction any smooth functions from basic primitives
// Example, e^sin(x) is given by
// using Function = CTComposition<CTExponent, CTSinus>;
// Then, Function::evaluate(x) = e^sin(x)
class CTArg {
public:
static constexpr EFunctionType FunctionType = EFunctionType::Argument;
inline static double evaluate(double argument) {
return argument;
}
};
// ---------------------------------------------------------------------------
template<int constant>
class CTConstant {
public:
static constexpr EFunctionType FunctionType = EFunctionType::Constant;
inline static double evaluate(double argument) {
return static_cast<double>(constant);
}
};
// ---------------------------------------------------------------------------
using CTZero = CTConstant<0>;
// ---------------------------------------------------------------------------
using CTOne = CTConstant<1>;
// ---------------------------------------------------------------------------
class CTPi {
public:
static constexpr EFunctionType FunctionType = EFunctionType::Constant;
inline static double evaluate(double argument) {
return M_PI;
}
};
// ---------------------------------------------------------------------------
template<int power>
class CTPower {
public:
static constexpr int _Power = power;
static constexpr EFunctionType FunctionType = EFunctionType::Power;
inline static double evaluate(double argument) {
return Power<power>::evaluate(argument);
}
};
// ---------------------------------------------------------------------------
class CTExponent {
public:
static constexpr EFunctionType FunctionType = EFunctionType::Exp;
inline static double evaluate(double argument) {
return std::exp(argument);
}
};
// ---------------------------------------------------------------------------
class CTLogarithm {
public:
static constexpr EFunctionType FunctionType = EFunctionType::Log;
inline static double evaluate(double argument) {
return std::log(argument);
}
};
// ---------------------------------------------------------------------------
class CTSinus {
public:
static constexpr EFunctionType FunctionType = EFunctionType::Sin;
inline static double evaluate(double argument) {
return std::sin(argument);
}
};
// ---------------------------------------------------------------------------
class CTCosinus {
public:
static constexpr EFunctionType FunctionType = EFunctionType::Cos;
inline static double evaluate(double argument) {
return std::cos(argument);
}
};
// ---------------------------------------------------------------------------
class CTSqrt {
public:
static constexpr EFunctionType FunctionType = EFunctionType::Sqrt;
inline static double evaluate(double argument) {
return std::sqrt(argument);
}
};
// ---------------------------------------------------------------------------
// Smooth Function Operators
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// Binary
// ---------------------------------------------------------------------------
template<class F, class S>
class CTSum {
public:
using First = F;
using Second = S;
static constexpr EFunctionType FunctionType = EFunctionType::Sum;
inline static double evaluate(double argument) {
return First::evaluate(argument) + Second::evaluate(argument);
}
};
// ---------------------------------------------------------------------------
template<class F, class S>
class CTDifference {
public:
using First = F;
using Second = S;
static constexpr EFunctionType FunctionType = EFunctionType::Difference;
inline static double evaluate(double argument) {
return First::evaluate(argument) - Second::evaluate(argument);
}
};
// ---------------------------------------------------------------------------
template<class F, class S>
class CTProduct {
public:
using First = F;
using Second = S;
static constexpr EFunctionType FunctionType = EFunctionType::Product;
inline static double evaluate(double argument) {
return First::evaluate(argument) * Second::evaluate(argument);
}
};
// ---------------------------------------------------------------------------
template<class F, class S>
class CTQuotient {
public:
using First = F;
using Second = S;
static constexpr EFunctionType FunctionType = EFunctionType::Quotient;
inline static double evaluate(double argument) {
return First::evaluate(argument) / Second::evaluate(argument);
}
};
// ---------------------------------------------------------------------------
template<class F, class S>
class CTComposition {
public:
using First = F;
using Second = S;
static constexpr EFunctionType FunctionType = EFunctionType::Composition;
inline static double evaluate(double argument) {
return First::evaluate(Second::evaluate(argument));
}
};
// ---------------------------------------------------------------------------
// Unary
// ---------------------------------------------------------------------------
template<class F>
class CTMinus {
public:
using First = F;
static constexpr EFunctionType FunctionType = EFunctionType::Minus;
inline static double evaluate(double argument) {
return -First::evaluate(argument);
}
};
// ---------------------------------------------------------------------------
using CTTangent = CTQuotient<CTSinus, CTCosinus>;
// ---------------------------------------------------------------------------
using CTCotangent = CTQuotient<CTCosinus, CTSinus>;
// ---------------------------------------------------------------------------
// The namespace is required to construct standard normal distribution
namespace NSSmoothFunctionTemplate {
using TWO = CTConstant<2>;
using Numerator = CTProduct<CTArg, CTArg>; // x^2
using Fraction = CTQuotient<Numerator, TWO>; // (x^2)/2
using ExpArg = CTMinus<Fraction>; // -(x^2)/2
using Exponent = CTComposition<CTExponent, ExpArg>; // e^(-(x^2)/2)
using TwoPi = CTProduct<TWO, CTPi>; // 2 * M_PI
using SqrtTwoPi = CTComposition<CTSqrt, TwoPi>; // sqrt(2 * M_PI)
using StandardNormalDensity = CTQuotient< Exponent,
SqrtTwoPi>; // e^(-(x^2)/2) / sqrt(2 * M_PI)
};
// ---------------------------------------------------------------------------
using CTStandardNormalDensity =
NSSmoothFunctionTemplate::StandardNormalDensity;
// ---------------------------------------------------------------------------
// Derivative of Smooth Function
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// General Template
// ---------------------------------------------------------------------------
template<class F, EFunctionType Type = F::FunctionType>
class CTDerivative {
};
// ---------------------------------------------------------------------------
// Derivative of Basic Function
// ---------------------------------------------------------------------------
template<class F>
class CTDerivative<F, EFunctionType::Constant> {
public:
using Result = CTZero;
};
// ---------------------------------------------------------------------------
template<class F>
class CTDerivative<F, EFunctionType::Argument> {
public:
using Result = CTOne;
};
// ---------------------------------------------------------------------------
template<class F>
class CTDerivative<F, EFunctionType::Exp> {
public:
using Result = CTExponent;
};
// ---------------------------------------------------------------------------
template<class F>
class CTDerivative<F, EFunctionType::Log> {
public:
using Result = CTComposition<CTPower<-1>, CTArg>;
};
// ---------------------------------------------------------------------------
template<class F>
class CTDerivative<F, EFunctionType::Sin> {
public:
using Result = CTCosinus;
};
// ---------------------------------------------------------------------------
template<class F>
class CTDerivative<F, EFunctionType::Cos> {
public:
using Result = CTMinus<CTSinus>;
};
// ---------------------------------------------------------------------------
template<class F>
class CTDerivative<F, EFunctionType::Sqrt> {
using TWO = CTConstant<2>;
using Denominator = CTProduct<TWO, CTSqrt>;
public:
using Result = CTQuotient<CTOne, Denominator>;
};
// ---------------------------------------------------------------------------
template<class F>
class CTDerivative<F, EFunctionType::Power> {
public:
using Result = CTProduct< CTConstant<F::_Power>,
CTPower<F::_Power - 1>>;
};
// ---------------------------------------------------------------------------
// Derivative of Operation
// ---------------------------------------------------------------------------
template<class F>
class CTDerivative<F, EFunctionType::Minus> {
public:
using Result = CTMinus<typename CTDerivative<typename F::First>::Result>;
};
// ---------------------------------------------------------------------------
template<class F>
class CTDerivative<F, EFunctionType::Sum> {
using FirstDerivative =
typename CTDerivative<typename F::First>::Result;
using SecondDerivative =
typename CTDerivative<typename F::Second>::Result;
public:
using Result = CTSum<FirstDerivative, SecondDerivative>;
};
// ---------------------------------------------------------------------------
template<class F>
class CTDerivative<F, EFunctionType::Difference> {
using FirstDerivative =
typename CTDerivative<typename F::First>::Result;
using SecondDerivative =
typename CTDerivative<typename F::Second>::Result;
public:
using Result = CTDifference<FirstDerivative, SecondDerivative>;
};
// ---------------------------------------------------------------------------
template<class F>
class CTDerivative<F, EFunctionType::Product> {
using FirstDerivative =
typename CTDerivative<typename F::First>::Result;
using SecondDerivative =
typename CTDerivative<typename F::Second>::Result;
using FirstSummand =
CTProduct<FirstDerivative, typename F::Second>;
using SecondSummand =
CTProduct<typename F::First, SecondDerivative>;
public:
using Result = CTSum<FirstSummand, SecondSummand>;
};
// ---------------------------------------------------------------------------
template<class F>
class CTDerivative<F, EFunctionType::Quotient> {
using Numerator = typename F::First;
using Denominator = typename F::Second;
using NumeratorDerivative =
typename CTDerivative<Numerator>::Result;
using DenominatorDerivative =
typename CTDerivative<Denominator>::Result;
using FirstNumeratorSummand =
CTProduct<NumeratorDerivative, Denominator>;
using SecondNumeratorSummand =
CTProduct<Numerator, DenominatorDerivative>;
using newNumerator =
CTDifference< FirstNumeratorSummand, SecondNumeratorSummand>;
using newDenominator = CTProduct<Denominator, Denominator>;
public:
using Result = CTQuotient<newNumerator, newDenominator>;
};
// ---------------------------------------------------------------------------
template<class F>
class CTDerivative<F, EFunctionType::Composition> {
using OuterFunction = typename F::First;
using InnerFunction = typename F::Second;
using OuterDerivative =
typename CTDerivative<OuterFunction>::Result;
using InnerDerivative =
typename CTDerivative<InnerFunction>::Result;
using FirstMultiple =
CTComposition<OuterDerivative, InnerFunction>;
public:
using Result = CTProduct<FirstMultiple, InnerDerivative>;
};
// ---------------------------------------------------------------------------
// Successive derivative of Basic Function
// ---------------------------------------------------------------------------
namespace NSSuccessiveDerivative {
template<class F, unsigned int derivative>
class CTSuccessiveDerivative {
public:
using Result =
typename CTDerivative<
typename CTSuccessiveDerivative<F, derivative - 1>::Result
>::Result;
};
// ---------------------------------------------------------------------------
template<class F>
class CTSuccessiveDerivative<F, 0> {
public:
using Result = F;
};
//---------------------------------------------------------------------------
}
//---------------------------------------------------------------------------
// CTSuccessiveDerivative
//---------------------------------------------------------------------------
template<class F, unsigned int derivative>
using CTSuccessiveDerivative =
typename NSSuccessiveDerivative::
CTSuccessiveDerivative<F, derivative>::Result;
//---------------------------------------------------------------------------
} // NSLibrary
//---------------------------------------------------------------------------
} // NSApplication
//---------------------------------------------------------------------------
#endif