Jpp 21.0.0
the software that should make you happy
Loading...
Searching...
No Matches
JMathlib.hh
Go to the documentation of this file.
1#ifndef __JMATH__JMATHLIB__
2#define __JMATH__JMATHLIB__
3
4#include <cstddef>
5#include <cmath>
6#include <array>
7#include <utility>
8#include <tuple>
9#include <memory>
10
11
12/**
13 * \file
14 * Functional algebra.
15 *
16 * \author mdejong
17 */
18
19namespace JMATH {}
20namespace JPP { using namespace JMATH; }
21
22namespace JMATH {
23
24
25 /**
26 * Type definition for list of parameters.
27 *
28 * The template argument refers to the function that could be fitted to some data.\n
29 * The parameters of the function should be data members of type <tt>double</tt>.
30 */
31 template<class JF1_t, size_t N>
33
34
35 /**
36 * Auxiliary method to join parameter and list of parameters.
37 *
38 * \param parameters parameter list
39 * \param ls indices of parameter list
40 * \param parameter parameter
41 * \return parameter list
42 */
43 template<typename U, typename V, typename W, size_t ...i>
44 constexpr parameter_list<U, sizeof...(i) + 1> join(const parameter_list<V, sizeof...(i)>& parameters,
45 const std::index_sequence<i...>& ls,
46 double W::* parameter)
47 {
48 return {parameters[i]..., parameter};
49 };
50
51
52 /**
53 * Join parameter and list of parameters.
54 *
55 * \param parameters parameter list
56 * \param parameter parameter
57 * \return parameter list
58 */
59 template<typename U, typename V, size_t N, typename W>
61 double W::* parameter)
62 {
63 return join<U>(parameters, std::make_index_sequence<N>{}, parameter);
64 }
65
66
67 /**
68 * Auxiliary method to join two parameter lists.
69 *
70 * \param v first parameter list
71 * \param lv indices of first parameter list
72 * \param w second parameter list
73 * \param lw indices of second parameter list
74 * \return parameter list
75 */
76 template<typename U, typename V, typename W, size_t ...iv, size_t ...iw>
77 constexpr parameter_list<U, sizeof...(iv) + sizeof...(iw)> join(const parameter_list<V, sizeof...(iv)>& v,
78 const std::index_sequence<iv...>& lv,
79 const parameter_list<W, sizeof...(iw)>& w,
80 const std::index_sequence<iw...>& lw)
81 {
82 return {v[iv]..., w[iw]... };
83 };
84
85
86 /**
87 * Join two parameter lists.
88 *
89 * \param v first parameter list
90 * \param w second parameter list
91 * \return parameter list
92 */
93 template<typename U, typename V, size_t NV, typename W, size_t NW>
95 const parameter_list<W, NW>& w)
96 {
97 return join<U>(v, std::make_index_sequence<NV>{},
98 w, std::make_index_sequence<NW>{});
99 }
100
101
102 /**
103 * Auxiliary method to convert parameter list.
104 *
105 * \param parameters parameter list
106 * \param ls indices of parameter list
107 * \return parameter list
108 */
109 template<typename U, typename V, size_t ...i>
110 constexpr parameter_list<U, sizeof...(i)> convert(const parameter_list<V, sizeof...(i)>& parameters,
111 const std::index_sequence<i...>& ls)
112 {
113 return {parameters[i]...};
114 };
115
116
117 /**
118 * Convert parameter list.
119 *
120 * \param parameters parameter list
121 * \return parameter list
122 */
123 template<typename U, typename V, size_t N>
125 {
126 return convert<U>(parameters, std::make_index_sequence<N>{});
127 }
128
129
130 /**
131 * Get number of parameters.
132 *
133 * \return number of parameters
134 */
135 template<class JF1_t>
136 constexpr size_t getNumberOfParameters()
137 {
138 return JF1_t::parameters.size();
139 }
140
141
142 /**
143 * Set values of all parameters.
144 *
145 * \param f1 pointer to function
146 * \param values pointer to list of values
147 */
148 template<class JF1_t>
149 inline void setParameters(JF1_t* f1, const double* values)
150 {
151 if (f1 != NULL && values != NULL) {
152 for (size_t i = 0; i != getNumberOfParameters<JF1_t>(); ++i) {
153 (*f1)[i] = values[i];
154 }
155 }
156 }
157
158
159 /**
160 * Auxiliary base class for mathematical operations on parameters of function.
161 */
162 template<class JF1_t>
163 struct JCalculus
164 {
165 /**
166 * Negate function.
167 *
168 * \return this function
169 */
170 JF1_t& negate()
171 {
172 for (const auto& i : JF1_t::parameters) {
173 static_cast<JF1_t&>(*this).*i = -(static_cast<JF1_t&>(*this).*i);
174 }
175
176 return static_cast<JF1_t&>(*this);
177 }
178
179
180 /**
181 * Add function.
182 *
183 * \param f1 function
184 * \return this function
185 */
186 JF1_t& add(const JF1_t& f1)
187 {
188 for (const auto& i : JF1_t::parameters) {
189 static_cast<JF1_t&>(*this).*i += f1.*i;
190 }
191
192 return static_cast<JF1_t&>(*this);
193 }
194
195
196 /**
197 * Subtract function.
198 *
199 * \param f1 function
200 * \return this function
201 */
202 JF1_t& sub(const JF1_t& f1)
203 {
204 for (const auto& i : JF1_t::parameters) {
205 static_cast<JF1_t&>(*this).*i -= f1.*i;
206 }
207
208 return static_cast<JF1_t&>(*this);
209 }
210
211
212 /**
213 * Scale function.
214 *
215 * \param factor factor
216 * \return this function
217 */
218 JF1_t& mul(const double factor)
219 {
220 for (const auto& i : JF1_t::parameters) {
221 static_cast<JF1_t&>(*this).*i *= factor;
222 }
223
224 return static_cast<JF1_t&>(*this);
225 }
226
227
228 /**
229 * Scale function.
230 *
231 * \param factor factor
232 * \return this function
233 */
234 JF1_t& div(const double factor)
235 {
236 for (const auto& i : JF1_t::parameters) {
237 static_cast<JF1_t&>(*this).*i /= factor;
238 }
239
240 return static_cast<JF1_t&>(*this);
241 }
242
243
244 /**
245 * Add function.
246 *
247 * \param function this function
248 * \param value value
249 * \return this function
250 */
251 friend JF1_t& operator+=(JF1_t& function, const JF1_t& value)
252 {
253 return function.add(value);
254 }
255
256
257 /**
258 * Subtract function.
259 *
260 * \param function this function
261 * \param value value
262 * \return this function
263 */
264 friend JF1_t& operator-=(JF1_t& function, const JF1_t& value)
265 {
266 return function.sub(value);
267 }
268
269
270 /**
271 * Scale function.
272 *
273 * \param function this function
274 * \param factor factor
275 * \return this function
276 */
277 friend JF1_t& operator*=(JF1_t& function, const double factor)
278 {
279 return function.mul(factor);
280 }
281
282
283 /**
284 * Scale function.
285 *
286 * \param function this function
287 * \param factor factor
288 * \return this function
289 */
290 friend JF1_t& operator/=(JF1_t& function, const double factor)
291 {
292 return function.div(factor);
293 }
294 };
295
296
297 template<class JF1_t>
298 struct JNegate; // forward declaration for negation of function.
299
300 template<class JF1_t, class JF2_t = double>
301 struct JAdd; // forward declaration for addition of fuction.
302
303 template<class JF1_t, class JF2_t = double>
304 struct JSub; // forward declaration for subtraction of fuction.
305
306 template<class JF1_t, class JF2_t = double>
307 struct JMul; // forward declaration for multiplication of fuction.
308
309 template<class JF1_t, class JF2_t = double>
310 struct JDiv; // forward declaration for division of fuction.
311
312 template<class JF1_t>
313 struct JFn; // forward declaration for fixed power of function.
314
315 template<class JF1_t>
316 struct JPf; // forward declaration for pointer to function.
317
318
319 /**
320 * Auxiliary base class for mathematical operations on functions.
321 */
322 template<class JF1_t>
323 struct JMathlib {
324 /**
325 * Function value.
326 *
327 * \param args abscissa value(s)
328 * \return function value
329 */
330 template<class ...Args>
331 double operator()(const Args& ...args) const
332 {
333 return static_cast<const JF1_t&>(*this).getValue(args...);
334 }
335
336
337 /**
338 * Affirm operator.
339 *
340 * \param function this function
341 * \return result function
342 */
343 friend const JF1_t& operator+(const JF1_t& function)
344 {
345 return function;
346 }
347
348
349 /**
350 * Negate operator.
351 *
352 * \param function this function
353 * \return result function
354 */
355 friend JNegate<JF1_t> operator-(const JF1_t& function)
356 {
357 return JNegate<JF1_t>(function);
358 }
359
360
361 /**
362 * Addition of constant value.
363 *
364 * \param f1 function
365 * \param value value
366 * \return result function
367 */
368 friend JAdd<JF1_t> operator+(const JF1_t& f1, const double value)
369 {
370 return JAdd<JF1_t>(f1, value);
371 }
372
373
374 /**
375 * Addition of constant value.
376 *
377 * \param value value
378 * \param f1 function
379 * \return result function
380 */
381 friend JAdd<JF1_t> operator+(const double value, const JF1_t& f1)
382 {
383 return JAdd<JF1_t>(f1, value);
384 }
385
386
387 /**
388 * Subtraction of constant value.
389 *
390 * \param f1 function
391 * \param value value
392 * \return result function
393 */
394 friend JSub<JF1_t> operator-(const JF1_t& f1, const double value)
395 {
396 return JSub<JF1_t>(f1, value);
397 }
398
399
400 /**
401 * Subtraction of constant value.
402 *
403 * \param f1 function
404 * \param value value
405 * \return result function
406 */
407 friend JAdd< JNegate<JF1_t> > operator-(const double value, const JF1_t& f1)
408 {
409 return JAdd< JNegate<JF1_t> >(JNegate<JF1_t>(f1), value);
410 }
411
412
413 /**
414 * Multiplication of constant value.
415 *
416 * \param f1 function
417 * \param value value
418 * \return result function
419 */
420 friend JMul<JF1_t> operator*(const JF1_t& f1, const double value)
421 {
422 return JMul<JF1_t>(f1, value);
423 }
424
425
426 /**
427 * Multiplication of constant value.
428 *
429 * \param value value
430 * \param f1 function
431 * \return result function
432 */
433 friend JMul<JF1_t> operator*(const double value, const JF1_t& f1)
434 {
435 return JMul<JF1_t>(f1, value);
436 }
437
438
439 /**
440 * Division of constant value.
441 *
442 * \param f1 function
443 * \param value value
444 * \return result function
445 */
446 friend JDiv<JF1_t> operator/(const JF1_t& f1, const double value)
447 {
448 return JDiv<JF1_t>(f1, value);
449 }
450
451
452 /**
453 * Addition of two functions.
454 *
455 * \param f1 first function
456 * \param f2 second function
457 * \return result function
458 */
459 template<class JF2_t>
460 friend JAdd<JF1_t, JF2_t> operator+(const JF1_t& f1, const JF2_t& f2)
461 {
462 return JAdd<JF1_t, JF2_t>(f1, f2);
463 }
464
465
466 /**
467 * Subtraction of two functions.
468 *
469 * \param f1 first function
470 * \param f2 second function
471 * \return result function
472 */
473 template<class JF2_t>
474 friend JSub<JF1_t, JF2_t> operator-(const JF1_t& f1, const JF2_t& f2)
475 {
476 return JSub<JF1_t, JF2_t>(f1, f2);
477 }
478
479
480 /**
481 * Multiplication of two functions.
482 *
483 * \param f1 first function
484 * \param f2 second function
485 * \return result function
486 */
487 template<class JF2_t>
488 friend JMul<JF1_t, JF2_t> operator*(const JF1_t& f1, const JF2_t& f2)
489 {
490 return JMul<JF1_t, JF2_t>(f1, f2);
491 }
492
493
494 /**
495 * Division of two functions.
496 *
497 * \param f1 first function
498 * \param f2 second function
499 * \return result function
500 */
501 template<class JF2_t>
502 friend JDiv<JF1_t, JF2_t> operator/(const JF1_t& f1, const JF2_t& f2)
503 {
504 return JDiv<JF1_t, JF2_t>(f1, f2);
505 }
506
507
508 /**
509 * Power-of operator.
510 *
511 * \param f1 function
512 * \param N power
513 * \return result function
514 */
515 friend JFn<JF1_t> operator^(const JF1_t& f1, int N)
516 {
517 return JFn<JF1_t>(f1, N);
518 }
519
520
521 /**
522 * Address-of operator.
523 *
524 * \param f1 function
525 * \return result function
526 */
527 friend JPf<JF1_t> operator&(const JF1_t& f1)
528 {
529 return JPf<JF1_t>(std::addressof(f1));
530 }
531
532
533 /**
534 * Get size of parameter.
535 *
536 * \return size
537 */
538 static size_t size()
539 {
540 return JF1_t::parameters.size();
541 }
542
543
544 /**
545 * Get value of parameter at given index.
546 *
547 * \param i index
548 * \return value
549 */
550 double operator[](const size_t i) const
551 {
552 return static_cast<const JF1_t&>(*this).*JF1_t::parameters[i];
553 }
554
555
556 /**
557 * Get value of parameter at given index.
558 *
559 * \param i index
560 * \return value
561 */
562 double& operator[](const size_t i)
563 {
564 return static_cast<JF1_t&>(*this).*JF1_t::parameters[i];
565 }
566 };
567
568
569 /**
570 * Negate of function.
571 */
572 template<class JF1_t>
573 struct JNegate :
574 public JMathlib< JNegate<JF1_t> >,
575 public JF1_t
576 {
577 using JMathlib< JNegate<JF1_t> >::operator();
578 using JMathlib< JNegate<JF1_t> >::operator[];
580
581
582 /**
583 * Default constructor.
584 */
586 {}
587
588
589 /**
590 * Constructor.
591 *
592 * \param f1 function
593 */
594 JNegate(const JF1_t& f1) :
595 JF1_t(f1)
596 {}
597
598
599 /**
600 * Function value.
601 *
602 * \param args abscissa value(s)
603 * \return function value
604 */
605 template<class ...Args>
606 double getValue(const Args& ...args) const
607 {
608 return -static_cast<const JF1_t&>(*this).getValue(args...);
609 }
610
611
612 /**
613 * Derivative value.
614 *
615 * \param x abscissa value
616 * \return derivative value
617 */
618 double getDerivative(const double x) const
619 {
620 return -static_cast<const JF1_t&>(*this).getDerivative(x);
621 }
622
623
624 /**
625 * Get gradient.
626 *
627 * \param args abscissa value(s)
628 * \return gradient
629 */
630 template<class ...Args>
631 JNegate getGradient(const Args& ...args) const
632 {
633 JNegate gradient;
634
635 static_cast<JF1_t&>(gradient) = static_cast<const JF1_t&>(*this).getGradient(args...);
636 static_cast<JF1_t&>(gradient).negate();
637
638 return gradient;
639 }
640 };
641
642
643 /**
644 * Addition of constant value.
645 */
646 template<class JF1_t>
647 struct JAdd<JF1_t, double> :
648 public JMathlib< JAdd<JF1_t, double> >,
649 public JF1_t
650 {
651 using JMathlib< JAdd<JF1_t, double> >::operator();
652 using JMathlib< JAdd<JF1_t, double> >::operator[];
654
655
656 /**
657 * Default constructor.
658 */
660 {}
661
662
663 /**
664 * Constructor.
665 *
666 * \param f1 function
667 * \param value value
668 */
669 JAdd(const JF1_t& f1, const double value) :
670 JF1_t(f1),
671 value(value)
672 {}
673
674
675 /**
676 * Function value.
677 *
678 * \param args abscissa value(s)
679 * \return function value
680 */
681 template<class ...Args>
682 double getValue(const Args& ...args) const
683 {
684 return static_cast<const JF1_t&>(*this).getValue(args...) + value;
685 }
686
687
688 /**
689 * Derivative value.
690 *
691 * \param x abscissa value
692 * \return derivative value
693 */
694 double getDerivative(const double x) const
695 {
696 return static_cast<const JF1_t&>(*this).getDerivative(x);
697 }
698
699
700 /**
701 * Get gradient.
702 *
703 * \param args abscissa value(s)
704 * \return gradient
705 */
706 template<class ...Args>
707 JAdd getGradient(const Args& ...args) const
708 {
709 JAdd gradient;
710
711 static_cast<JF1_t&>(gradient) = static_cast<const JF1_t&>(*this).getGradient(args...);
712
713 return gradient;
714 }
715
716 private:
717 double value;
718 };
719
720
721 /**
722 * Subtraction of constant value.
723 */
724 template<class JF1_t>
725 struct JSub<JF1_t, double> :
726 public JMathlib< JSub<JF1_t, double> >,
727 public JF1_t
728 {
729 using JMathlib< JSub<JF1_t, double> >::operator();
730 using JMathlib< JSub<JF1_t, double> >::operator[];
732
733
734 /**
735 * Default constructor.
736 */
738 {}
739
740
741 /**
742 * Constructor.
743 *
744 * \param f1 function
745 * \param value value
746 */
747 JSub(const JF1_t& f1, const double value) :
748 JF1_t(f1),
749 value(value)
750 {}
751
752
753 /**
754 * Function value.
755 *
756 * \param args abscissa value(s)
757 * \return function value
758 */
759 template<class ...Args>
760 double getValue(const Args& ...args) const
761 {
762 return static_cast<const JF1_t&>(*this).getValue(args...) - value;
763 }
764
765
766 /**
767 * Derivative value.
768 *
769 * \param x abscissa value
770 * \return derivative value
771 */
772 double getDerivative(const double x) const
773 {
774 return static_cast<const JF1_t&>(*this).getDerivative(x);
775 }
776
777
778 /**
779 * Get gradient.
780 *
781 * \param args abscissa value(s)
782 * \return gradient
783 */
784 template<class ...Args>
785 JSub getGradient(const Args& ...args) const
786 {
787 JSub gradient;
788
789 static_cast<JF1_t&>(gradient) = static_cast<const JF1_t&>(*this).getGradient(args...);
790
791 return gradient;
792 }
793
794 private:
795 double value;
796 };
797
798
799 /**
800 * Multiplication of constant value.
801 */
802 template<class JF1_t>
803 struct JMul<JF1_t, double> :
804 public JMathlib< JMul<JF1_t, double> >,
805 public JF1_t
806 {
807 using JMathlib< JMul<JF1_t, double> >::operator();
808 using JMathlib< JMul<JF1_t, double> >::operator[];
810
811
812 /**
813 * Default constructor.
814 */
816 {}
817
818
819 /**
820 * Constructor.
821 *
822 * \param f1 function
823 * \param value value
824 */
825 JMul(const JF1_t& f1, const double value) :
826 JF1_t(f1),
827 value(value)
828 {}
829
830
831 /**
832 * Function value.
833 *
834 * \param args abscissa value(s)
835 * \return function value
836 */
837 template<class ...Args>
838 double getValue(const Args& ...args) const
839 {
840 return static_cast<const JF1_t&>(*this).getValue(args...) * value;
841 }
842
843
844 /**
845 * Derivative value.
846 *
847 * \param x abscissa value
848 * \return derivative value
849 */
850 double getDerivative(const double x) const
851 {
852 return static_cast<const JF1_t&>(*this).getDerivative(x) * value;
853 }
854
855
856 /**
857 * Get gradient.
858 *
859 * \param args abscissa value(s)
860 * \return gradient
861 */
862 template<class ...Args>
863 JMul getGradient(const Args& ...args) const
864 {
865 JMul gradient;
866
867 static_cast<JF1_t&>(gradient) = static_cast<const JF1_t&>(*this).getGradient(args...);
868 static_cast<JF1_t&>(gradient) *= value;
869
870 return gradient;
871 }
872
873 private:
874 double value;
875 };
876
877
878 /**
879 * Division of constant value.
880 */
881 template<class JF1_t>
882 struct JDiv<JF1_t, double> :
883 public JMathlib< JDiv<JF1_t, double> >,
884 public JF1_t
885 {
886 using JMathlib< JDiv<JF1_t, double> >::operator();
887 using JMathlib< JDiv<JF1_t, double> >::operator[];
889
890
891 /**
892 * Default constructor.
893 */
895 {}
896
897
898 /**
899 * Constructor.
900 *
901 * \param f1 function
902 * \param value value
903 */
904 JDiv(const JF1_t& f1, const double value) :
905 JF1_t(f1),
906 value(value)
907 {}
908
909
910 /**
911 * Function value.
912 *
913 * \param args abscissa value(s)
914 * \return function value
915 */
916 template<class ...Args>
917 double getValue(const Args& ...args) const
918 {
919 return static_cast<const JF1_t&>(*this).getValue(args...) / value;
920 }
921
922
923 /**
924 * Derivative value.
925 *
926 * \param x abscissa value
927 * \return derivative value
928 */
929 double getDerivative(const double x) const
930 {
931 return static_cast<const JF1_t&>(*this).getDerivative(x) / value;
932 }
933
934
935 /**
936 * Get gradient.
937 *
938 * \param args abscissa value(s)
939 * \return gradient
940 */
941 template<class ...Args>
942 JDiv getGradient(const Args& ...args) const
943 {
944 JDiv gradient;
945
946 static_cast<JF1_t&>(gradient) = static_cast<const JF1_t&>(*this).getGradient(args...);
947 static_cast<JF1_t&>(gradient) /= value;
948
949 return gradient;
950 }
951
952 private:
953 double value;
954 };
955
956
957 /**
958 * Auxiliary data structure for pair of functions.
959 */
960 template<class JF1_t, class JF2_t,
961 bool vf1 = JF1_t::parameters.size() != 0, // != 0 -> base class, else data member
962 bool vf2 = JF2_t::parameters.size() != 0> // != 0 -> base class, else data member
963 struct JPair :
964 public JF1_t,
965 public JF2_t
966 {
967 /**
968 * Default constructor.
969 */
971 {}
972
973
974 /**
975 * Constructor.
976 *
977 * \param f1 first function
978 * \param f2 second function
979 */
980 JPair(const JF1_t& f1, const JF2_t& f2) :
981 JF1_t(f1),
982 JF2_t(f2)
983 {}
984
985
986 const JF1_t& f1() const { return static_cast<const JF1_t&>(*this); }
987 const JF2_t& f2() const { return static_cast<const JF2_t&>(*this); }
988
989 JF1_t& f1() { return static_cast<JF1_t&>(*this); }
990 JF2_t& f2() { return static_cast<JF2_t&>(*this); }
991
992
993 static constexpr parameter_list<JPair, JF1_t::parameters.size() + JF2_t::parameters.size()> parameters = join<JPair>(JF1_t::parameters, JF2_t::parameters);
994 };
995
996
997 /**
998 * Template specialisation for pair of functions.
999 */
1000 template<class JF1_t, class JF2_t>
1001 struct JPair<JF1_t, JF2_t, true, false> :
1002 public JF1_t
1003 {
1004 /**
1005 * Default constructor.
1006 */
1008 {}
1009
1010
1011 /**
1012 * Constructor.
1013 *
1014 * \param f1 first function
1015 * \param f2 second function
1016 */
1017 JPair(const JF1_t& f1, const JF2_t& f2) :
1018 JF1_t(f1),
1019 _f2(f2)
1020 {}
1021
1022
1023 const JF1_t& f1() const { return static_cast<const JF1_t&>(*this); }
1024 const JF2_t& f2() const { return _f2; }
1025
1026 JF1_t& f1() { return static_cast<JF1_t&>(*this); }
1027 JF2_t& f2() { return _f2; }
1028
1029
1030 static constexpr parameter_list<JPair, JF1_t::parameters.size()> parameters = convert<JPair>(JF1_t::parameters);
1031
1032 private:
1033 JF2_t _f2;
1034 };
1035
1036
1037 /**
1038 * Template specialisation for pair of functions.
1039 */
1040 template<class JF1_t, class JF2_t>
1041 struct JPair<JF1_t, JF2_t, false, true> :
1042 public JF2_t
1043 {
1044 /**
1045 * Default constructor.
1046 */
1048 {}
1049
1050
1051 /**
1052 * Constructor.
1053 *
1054 * \param f1 first function
1055 * \param f2 second function
1056 */
1057 JPair(const JF1_t& f1, const JF2_t& f2) :
1058 JF2_t(f2),
1059 _f1(f1)
1060 {}
1061
1062
1063 const JF1_t& f1() const { return _f1; }
1064 const JF2_t& f2() const { return static_cast<const JF2_t&>(*this); }
1065
1066 JF1_t& f1() { return _f1; }
1067 JF2_t& f2() { return static_cast<JF2_t&>(*this); }
1068
1069
1070 static constexpr parameter_list<JPair, JF2_t::parameters.size()> parameters = convert<JPair>(JF2_t::parameters);
1071
1072 private:
1073 JF1_t _f1;
1074 };
1075
1076
1077 /**
1078 * Template specialisation for pair of functions.
1079 */
1080 template<class JF1_t, class JF2_t>
1081 struct JPair<JF1_t, JF2_t, false, false>
1082 {
1083 /**
1084 * Default constructor.
1085 */
1087 {}
1088
1089
1090 /**
1091 * Constructor.
1092 *
1093 * \param f1 first function
1094 * \param f2 second function
1095 */
1096 JPair(const JF1_t& f1, const JF2_t& f2) :
1097 _f1(f1),
1098 _f2(f2)
1099 {}
1100
1101
1102 const JF1_t& f1() const { return _f1; }
1103 const JF2_t& f2() const { return _f2; }
1104
1105 JF1_t& f1() { return _f1; }
1106 JF2_t& f2() { return _f2; }
1107
1109
1110 private:
1111 JF1_t _f1;
1112 JF2_t _f2;
1113 };
1114
1115
1116 /**
1117 * Auxiliary data structure for pair of functions.
1118 */
1119 template<class JF1_t, class JF2_t>
1120 struct JPair_t :
1121 public JPair<JF1_t, JF2_t>,
1122 public JCalculus< JPair_t<JF1_t, JF2_t> >
1123 {
1129
1130
1131 /**
1132 * Default constructor.
1133 */
1135 {}
1136
1137
1138 /**
1139 * Constructor.
1140 *
1141 * \param f1 first function
1142 * \param f2 second function
1143 */
1144 JPair_t(const JF1_t& f1, const JF2_t& f2) :
1145 JPair<JF1_t, JF2_t>(f1, f2)
1146 {}
1147 };
1148
1149
1150 /**
1151 * Addition of two functions.
1152 */
1153 template<class JF1_t, class JF2_t>
1154 struct JAdd :
1155 public JMathlib< JAdd<JF1_t, JF2_t> >,
1156 public JPair_t<JF1_t, JF2_t>
1157 {
1158 using JMathlib< JAdd<JF1_t, JF2_t> >::operator();
1159 using JMathlib< JAdd<JF1_t, JF2_t> >::operator[];
1161
1162
1163 /**
1164 * Default constructor.
1165 */
1167 {}
1168
1169
1170 /**
1171 * Constructor.
1172 *
1173 * \param f1 first function
1174 * \param f2 second function
1175 */
1176 JAdd(const JF1_t& f1, const JF2_t& f2) :
1177 JPair_t<JF1_t, JF2_t>(f1, f2)
1178 {}
1179
1180
1181 /**
1182 * Function value.
1183 *
1184 * \param args abscissa value(s)
1185 * \return function value
1186 */
1187 template<class ...Args>
1188 double getValue(const Args& ...args) const
1189 {
1190 return (this->f1().getValue(args...) +
1191 this->f2().getValue(args...));
1192 }
1193
1194
1195 /**
1196 * Derivative value.
1197 *
1198 * \param x abscissa value
1199 * \return derivative value
1200 */
1201 double getDerivative(const double x) const
1202 {
1203 return (this->f1().getDerivative(x) +
1204 this->f2().getDerivative(x));
1205 }
1206
1207
1208 /**
1209 * Get gradient.
1210 *
1211 * \param args abscissa value(s)
1212 * \return gradient
1213 */
1214 template<class ...Args>
1215 JAdd getGradient(const Args& ...args) const
1216 {
1217 JAdd gradient;
1218
1219 gradient.f1() = this->f1().getGradient(args...);
1220 gradient.f2() = this->f2().getGradient(args...);
1221
1222 return gradient;
1223 }
1224 };
1225
1226
1227 /**
1228 * Subtraction of two functions.
1229 */
1230 template<class JF1_t, class JF2_t>
1231 struct JSub :
1232 public JMathlib< JSub<JF1_t, JF2_t> >,
1233 public JPair_t<JF1_t, JF2_t>
1234 {
1235 using JMathlib< JSub<JF1_t, JF2_t> >::operator();
1236 using JMathlib< JSub<JF1_t, JF2_t> >::operator[];
1238
1239
1240 /**
1241 * Default constructor.
1242 */
1244 {}
1245
1246
1247 /**
1248 * Constructor.
1249 *
1250 * \param f1 first function
1251 * \param f2 second function
1252 */
1253 JSub(const JF1_t& f1, const JF2_t& f2) :
1254 JPair_t<JF1_t, JF2_t>(f1, f2)
1255 {}
1256
1257
1258 /**
1259 * Function value.
1260 *
1261 * \param args abscissa value(s)
1262 * \return function value
1263 */
1264 template<class ...Args>
1265 double getValue(const Args& ...args) const
1266 {
1267 return (this->f1().getValue(args...) -
1268 this->f2().getValue(args...));
1269 }
1270
1271
1272 /**
1273 * Derivative value.
1274 *
1275 * \param x abscissa value
1276 * \return derivative value
1277 */
1278 double getDerivative(const double x) const
1279 {
1280 return (this->f1().getDerivative(x) -
1281 this->f2().getDerivative(x));
1282 }
1283
1284
1285 /**
1286 * Get gradient.
1287 *
1288 * \param args abscissa value(s)
1289 * \return gradient
1290 */
1291 template<class ...Args>
1292 JSub getGradient(const Args& ...args) const
1293 {
1294 JSub gradient;
1295
1296 gradient.f1() = this->f1().getGradient(args...);
1297 gradient.f2() = this->f2().getGradient(args...);
1298 gradient.f2().negate();
1299
1300 return gradient;
1301 }
1302 };
1303
1304
1305 /**
1306 * Multiplication of two functions.
1307 */
1308 template<class JF1_t, class JF2_t>
1309 struct JMul :
1310 public JMathlib< JMul<JF1_t, JF2_t> >,
1311 public JPair_t<JF1_t, JF2_t>
1312 {
1313 using JMathlib< JMul<JF1_t, JF2_t> >::operator();
1314 using JMathlib< JMul<JF1_t, JF2_t> >::operator[];
1316
1317
1318 /**
1319 * Default constructor.
1320 */
1322 {}
1323
1324
1325 /**
1326 * Constructor.
1327 *
1328 * \param f1 first function
1329 * \param f2 second function
1330 */
1331 JMul(const JF1_t& f1, const JF2_t& f2) :
1332 JPair_t<JF1_t, JF2_t>(f1, f2)
1333 {}
1334
1335
1336 /**
1337 * Function value.
1338 *
1339 * \param args abscissa value(s)
1340 * \return function value
1341 */
1342 template<class ...Args>
1343 double getValue(const Args& ...args) const
1344 {
1345 return (this->f1().getValue(args...) *
1346 this->f2().getValue(args...));
1347 }
1348
1349
1350 /**
1351 * Derivative value.
1352 *
1353 * \param x abscissa value
1354 * \return derivative value
1355 */
1356 double getDerivative(const double x) const
1357 {
1358 return (this->f1().getDerivative(x) * this->f2().getValue(x) +
1359 this->f1().getValue(x) * this->f2().getDerivative(x));
1360 }
1361
1362
1363 /**
1364 * Get gradient.
1365 *
1366 * \param args abscissa value(s)
1367 * \return gradient
1368 */
1369 template<class ...Args>
1370 JMul getGradient(const Args& ...args) const
1371 {
1372 JMul gradient;
1373
1374 gradient.f1() = this->f1().getGradient(args...);
1375 gradient.f1() *= this->f2().getValue(args...);
1376 gradient.f2() = this->f2().getGradient(args...);
1377 gradient.f2() *= this->f1().getValue(args...);
1378
1379 return gradient;
1380 }
1381 };
1382
1383
1384 /**
1385 * Division of two functions.
1386 */
1387 template<class JF1_t, class JF2_t>
1388 struct JDiv :
1389 public JMathlib< JDiv<JF1_t, JF2_t> >,
1390 public JPair_t<JF1_t, JF2_t>
1391 {
1392 using JMathlib< JDiv<JF1_t, JF2_t> >::operator();
1393 using JMathlib< JDiv<JF1_t, JF2_t> >::operator[];
1395
1396
1397 /**
1398 * Default constructor.
1399 */
1401 {}
1402
1403
1404 /**
1405 * Constructor.
1406 *
1407 * \param f1 first function
1408 * \param f2 second function
1409 */
1410 JDiv(const JF1_t& f1, const JF2_t& f2) :
1411 JPair_t<JF1_t, JF2_t>(f1, f2)
1412 {}
1413
1414
1415 /**
1416 * Function value.
1417 *
1418 * \param args abscissa value(s)
1419 * \return function value
1420 */
1421 template<class ...Args>
1422 double getValue(const Args& ...args) const
1423 {
1424 return (this->f1().getValue(args...) /
1425 this->f2().getValue(args...));
1426 }
1427
1428
1429 /**
1430 * Derivative value.
1431 *
1432 * \param x abscissa value
1433 * \return derivative value
1434 */
1435 double getDerivative(const double x) const
1436 {
1437 const double v = this->f1().getValue(x);
1438 const double w = this->f2().getValue(x);
1439
1440 return (this->f1().getDerivative(x) * w -
1441 v * this->f2().getDerivative(x)) / (w*w);
1442 }
1443
1444
1445 /**
1446 * Get gradient.
1447 *
1448 * \param args abscissa value(s)
1449 * \return gradient
1450 */
1451 template<class ...Args>
1452 JDiv getGradient(const Args& ...args) const
1453 {
1454 JDiv gradient;
1455
1456 const double v = this->f1().getValue(args...);
1457 const double w = this->f2().getValue(args...);
1458
1459 gradient.f1() = this->f1().getGradient(args...);
1460 gradient.f1() *= 1.0/w;
1461 gradient.f2() = this->f2().getGradient(args...);
1462 gradient.f2() *= -v/(w*w);
1463
1464 return gradient;
1465 }
1466 };
1467
1468
1469 /**
1470 * Fixed power of function.
1471 */
1472 template<class JF1_t>
1473 struct JFn :
1474 public JMathlib < JFn<JF1_t> >,
1475 public JF1_t
1476 {
1477 using JMathlib< JFn<JF1_t> >::operator();
1478 using JMathlib< JFn<JF1_t> >::operator[];
1479 using JMathlib< JFn<JF1_t> >::size;
1480
1481
1482 /**
1483 * Default constructor.
1484 */
1486 JF1_t(),
1487 N(1)
1488 {}
1489
1490
1491 /**
1492 * Constructor.
1493 *
1494 * \param f1 first function
1495 * \param N power
1496 */
1497 JFn(const JF1_t& f1, const int N) :
1498 JF1_t(f1),
1499 N(N)
1500 {}
1501
1502
1503 /**
1504 * Function value.
1505 *
1506 * \param args abscissa value(s)
1507 * \return function value
1508 */
1509 template<class ...Args>
1510 double getValue(const Args& ...args) const
1511 {
1512 const double u = static_cast<const JF1_t&>(*this).getValue(args...);
1513
1514 return pow(u, N);
1515 }
1516
1517
1518 /**
1519 * Derivative value.
1520 *
1521 * \param x abscissa value
1522 * \return derivative value
1523 */
1524 double getDerivative(const double x) const
1525 {
1526 const double u = static_cast<const JF1_t&>(*this).getValue(x);
1527 const double v = static_cast<const JF1_t&>(*this).getDerivative(x);
1528
1529 return N * pow(u, N - 1) * v;
1530 }
1531
1532
1533 /**
1534 * Get gradient.
1535 *
1536 * \param args abscissa value(s)
1537 * \return gradient
1538 */
1539 template<class ...Args>
1540 JFn getGradient(const Args& ...args) const
1541 {
1542 JFn gradient;
1543
1544 const double u = static_cast<const JF1_t&>(*this).getValue(args...);
1545 const double w = N * pow(u, N - 1);
1546
1547 static_cast<JF1_t&>(gradient) = static_cast<const JF1_t&>(*this).getGradient(args...);
1548 static_cast<JF1_t&>(gradient) *= w;
1549
1550 return gradient;
1551 }
1552
1553 private:
1554 int N;
1555 };
1556
1557
1558 /**
1559 * x.
1560 */
1561 struct JX :
1562 public JMathlib < JX >,
1563 public JCalculus< JX >
1564 {
1565 /**
1566 * Default constructor.
1567 */
1569 {}
1570
1571
1572 /**
1573 * Function value.
1574 *
1575 * \param x abscissa value
1576 * \return function value
1577 */
1578 double getValue(const double x) const
1579 {
1580 return x;
1581 }
1582
1583
1584 /**
1585 * Derivative value.
1586 *
1587 * \param x abscissa value
1588 * \return derivative value
1589 */
1590 double getDerivative(const double x) const
1591 {
1592 return 1.0;
1593 }
1594
1595
1596 /**
1597 * Get gradient.
1598 *
1599 * \param x abscissa value
1600 * \return gradient
1601 */
1602 JX getGradient(const double x) const
1603 {
1604 JX gradient;
1605
1606 return gradient;
1607 }
1608
1610 };
1611
1612
1613 static const JX X; //! Function object for x;
1614
1615
1616 /**
1617 * Fixed power of x.
1618 */
1619 template<int N>
1620 struct JXn :
1621 public JMathlib < JXn<N> >,
1622 public JCalculus< JXn<N> >
1623 {
1624 /**
1625 * Default constructor.
1626 */
1628 {}
1629
1630
1631 /**
1632 * Function value.
1633 *
1634 * \param x abscissa value
1635 * \return function value
1636 */
1637 double getValue(const double x) const
1638 {
1639 return pow(x, N);
1640 }
1641
1642
1643 /**
1644 * Derivative value.
1645 *
1646 * \param x abscissa value
1647 * \return derivative value
1648 */
1649 double getDerivative(const double x) const
1650 {
1651 return N * pow(x, N - 1);
1652 }
1653
1654
1655 /**
1656 * Get gradient.
1657 *
1658 * \param x abscissa value
1659 * \return gradient
1660 */
1661 JXn getGradient(const double x) const
1662 {
1663 JXn gradient;
1664
1665 return gradient;
1666 }
1667
1669 };
1670
1671
1672 template<>
1673 struct JXn<0>; //! Invalidate x^0.
1674 static const JXn<2> X2; //! Function object for x^2;
1675 static const JXn<3> X3; //! Function object for x^3;
1676 static const JXn<4> X4; //! Function object for x^4;
1677 static const JXn<5> X5; //! Function object for x^5;
1678
1679
1680 /**
1681 * Pointer to function.
1682 */
1683 template<class JF1_t>
1684 struct JPf :
1685 public JMathlib < JPf<JF1_t> >,
1686 public JCalculus< JPf<JF1_t> >
1687 {
1688 using JMathlib< JPf<JF1_t> >::operator();
1689 using JMathlib< JPf<JF1_t> >::operator[];
1690 using JMathlib< JPf<JF1_t> >::size;
1691
1692
1693 /**
1694 * Default constructor.
1695 */
1697 f1(NULL)
1698 {}
1699
1700
1701 /**
1702 * Constructor.
1703 *
1704 * \param f1 pointer to function
1705 */
1706 JPf(const JF1_t* f1) :
1707 f1(f1)
1708 {}
1709
1710
1711 /**
1712 * Function value.
1713 *
1714 * \param args abscissa value(s)
1715 * \return function value
1716 */
1717 template<class ...Args>
1718 double getValue(const Args& ...args) const
1719 {
1720 return f1->getValue(args...);
1721 }
1722
1723
1724 /**
1725 * Derivative value.
1726 *
1727 * \param x abscissa value
1728 * \return derivative value
1729 */
1730 double getDerivative(const double x) const
1731 {
1732 return f1->getDerivative(x);
1733 }
1734
1735
1736 /**
1737 * Get gradient.
1738 *
1739 * \param args abscissa value(s)
1740 * \return gradient
1741 */
1742 template<class ...Args>
1743 JPf getGradient(const Args& ...args) const
1744 {
1745 JPf gradient;
1746
1747 return gradient;
1748 }
1749
1751
1752 private:
1753 const JF1_t* f1;
1754 };
1755
1756
1757 /**
1758 * Recursive template class for polynomial function.
1759 */
1760 template<int ID_t, size_t N>
1761 struct JPolynome :
1762 public JMathlib < JPolynome<ID_t, N> >,
1763 public JCalculus< JPolynome<ID_t, N> >,
1764 public JPolynome<ID_t, N - 1>
1765 {
1766 static const int ID = ID_t;
1767
1768 static const size_t NUMBER_OF_DEGREES = N;
1769
1775 using JMathlib < JPolynome<ID_t, N> >::operator();
1776 using JMathlib < JPolynome<ID_t, N> >::operator[];
1778
1779
1780 /**
1781 * Default constructor.
1782 */
1784 a(0.0)
1785 {}
1786
1787
1788 /**
1789 * Constructor.
1790 *
1791 * \param args list of values
1792 */
1793 template<class ...Args>
1794 JPolynome(const Args& ...args) :
1795 JPolynome()
1796 {
1797 set(std::array<std::tuple_element_t<0, std::tuple<Args...> >, sizeof...(Args)>{args...});
1798 }
1799
1800
1801 /**
1802 * Constructor.
1803 *
1804 * \param args list of values
1805 */
1806 template<class T>
1808 JPolynome()
1809 {
1810 set(args);
1811 }
1812
1813
1814 /**
1815 * Set values.
1816 *
1817 * \param args values
1818 */
1819 template<class T>
1820 void set(const std::array<T, N+1>& args)
1821 {
1822 a = args[N];
1823
1824 set(args, std::make_index_sequence<N>{});
1825 }
1826
1827
1828 /**
1829 * Function value.
1830 *
1831 * \param x abscissa value
1832 * \return function value
1833 */
1834 double getValue(const double x) const
1835 {
1836 return a * pow(x,N) + static_cast<const JPolynome<ID_t, N - 1>&>(*this).getValue(x);
1837 }
1838
1839
1840 /**
1841 * Derivative value.
1842 *
1843 * \param x abscissa value
1844 * \return derivative value
1845 */
1846 double getDerivative(const double x) const
1847 {
1848 return N * a * pow(x,N-1) + static_cast<const JPolynome<ID_t, N - 1>&>(*this).getDerivative(x);
1849 }
1850
1851
1852 /**
1853 * Get gradient.
1854 *
1855 * \param x abscissa value
1856 * \return gradient
1857 */
1858 JPolynome getGradient(const double x) const
1859 {
1860 JPolynome gradient;
1861
1862 gradient.a = pow(x,N);
1863
1864 static_cast<JPolynome<ID_t, N - 1>&>(gradient) = static_cast<const JPolynome<ID_t, N - 1>&>(*this).getGradient(x);
1865
1866 return gradient;
1867 }
1868
1869 double a; //!< a[N]
1870
1872
1873
1874 private:
1875 /**
1876 * Set values.
1877 *
1878 * \param args values
1879 * \param ls indices
1880 */
1881 template<class T, size_t ...i>
1882 void set(const std::array<T, N+1>& args, const std::index_sequence<i...>& ls)
1883 {
1884 static_cast<JPolynome<ID_t, N-1>&>(*this).set(std::array<T,N>{args[i]...});
1885 }
1886 };
1887
1888
1889 /**
1890 * Termination class for polynomial function.
1891 * This function consititutes a constant term and is thereby multi-dimensional by nature.
1892 */
1893 template<int ID_t>
1894 struct JPolynome<ID_t, 0> :
1895 public JMathlib < JPolynome<ID_t, 0> >,
1896 public JCalculus< JPolynome<ID_t, 0> >
1897 {
1898 static const int ID = ID_t;
1899
1900 static const size_t NUMBER_OF_DEGREES = 0;
1901
1902
1903 /**
1904 * Default constructor.
1905 */
1907 a(0.0)
1908 {}
1909
1910
1911 /**
1912 * Constructor.
1913 *
1914 * \param a value
1915 */
1916 JPolynome(const double a) :
1917 a(a)
1918 {}
1919
1920
1921 /**
1922 * Constructor.
1923 *
1924 * \param args list of values
1925 */
1926 template<class T>
1928 JPolynome()
1929 {
1930 set(args);
1931 }
1932
1933
1934 /**
1935 * Set value.
1936 *
1937 * \param args value
1938 */
1939 template<class T>
1940 void set(const std::array<T, 1>& args)
1941 {
1942 a = args[0];
1943 }
1944
1945
1946 /**
1947 * Function value.
1948 *
1949 * \return function value
1950 */
1951 double getValue(...) const
1952 {
1953 return a;
1954 }
1955
1956
1957 /**
1958 * Derivative value.
1959 *
1960 * \return derivative value
1961 */
1962 double getDerivative(...) const
1963 {
1964 return 0.0;
1965 }
1966
1967
1968 /**
1969 * Get gradient.
1970 *
1971 * \return gradient
1972 */
1974 {
1975 JPolynome gradient;
1976
1977 gradient.a = 1.0;
1978
1979 return gradient;
1980 }
1981
1982 double a; //!< a[0]
1983
1985 };
1986
1987
1988 template<int ID_t>
1989 using JP0 = JPolynome<ID_t, 0>; //!< shorthand for 0th degree polynome
1990
1991 template<int ID_t>
1992 using P0 = JP0<ID_t>; //!< shorthand for constant term
1993
1994 template<int ID_t>
1995 using JP1 = JPolynome<ID_t, 1>; //!< shorthand for 1st degree polynome
1996
1997 template<int ID_t>
1998 using JP2 = JPolynome<ID_t, 2>; //!< shorthand for 2nd degree polynome
1999
2000 template<int ID_t>
2001 using JP3 = JPolynome<ID_t, 3>; //!< shorthand for 3rd degree polynome
2002
2003
2004 /**
2005 * Gauss function.
2006 */
2007 template<int ID_t, bool normalised = false>
2008 struct JGauss :
2009 public JMathlib < JGauss<ID_t> >,
2010 public JCalculus< JGauss<ID_t> >
2011 {
2012 static const int ID = ID_t;
2013
2014
2015 /**
2016 * Default constructor.
2017 */
2019 center(0.0),
2020 sigma (0.0)
2021 {}
2022
2023
2024 /**
2025 * Constructor.
2026 *
2027 * \param center center
2028 * \param sigma sigma
2029 */
2030 JGauss(const double center,
2031 const double sigma) :
2032 center(center),
2033 sigma (sigma)
2034 {}
2035
2036
2037 /**
2038 * Function value.
2039 *
2040 * \param x abscissa value
2041 * \return function value
2042 */
2043 double getValue(const double x) const
2044 {
2045 const double u = (x - center) / sigma;
2046
2047 return get(u);
2048 }
2049
2050
2051 /**
2052 * Derivative value.
2053 *
2054 * \param x abscissa value
2055 * \return derivative value
2056 */
2057 double getDerivative(const double x) const
2058 {
2059 const double w = 1.0 / sigma;
2060 const double u = (x - center) / sigma;
2061
2062 return get(u) * -u * w;
2063 }
2064
2065
2066 /**
2067 * Get gradient.
2068 *
2069 * \param x abscissa value
2070 * \return gradient
2071 */
2072 JGauss getGradient(const double x) const
2073 {
2075
2076 const double w = 1.0 / sigma;
2077 const double u = (x - center) * w;
2078 const double f0 = get(u);
2079
2080 gradient.center = f0 * u * w; // d(f)/d(center)
2081 gradient.sigma = f0 * u * u * w; // d(f)/d(sigma)
2082
2083 return gradient;
2084 }
2085
2086 double center; //!< center
2087 double sigma; //!< sigma
2088
2090
2091 private:
2092 /**
2093 * Get ordinate value.
2094 *
2095 * \param u abscissa value
2096 * \return ordinate value
2097 */
2098 inline double get(const double u) const
2099 {
2100 return exp(-0.5*u*u);
2101 }
2102 };
2103
2104
2105 /**
2106 * Gauss function.
2107 */
2108 template<int ID_t>
2109 struct JGauss<ID_t, true> :
2110 public JMathlib < JGauss<ID_t, true> >,
2111 public JCalculus< JGauss<ID_t, true> >
2112 {
2113 static const int ID = ID_t;
2114
2115
2116 /**
2117 * Default constructor.
2118 */
2120 center(0.0),
2121 sigma (0.0)
2122 {}
2123
2124
2125 /**
2126 * Constructor.
2127 *
2128 * \param center center
2129 * \param sigma sigma
2130 */
2131 JGauss(const double center,
2132 const double sigma) :
2133 center(center),
2134 sigma (sigma)
2135 {}
2136
2137
2138 /**
2139 * Function value.
2140 *
2141 * \param x abscissa value
2142 * \return function value
2143 */
2144 double getValue(const double x) const
2145 {
2146 const double u = (x - center) / sigma;
2147
2148 return get(u);
2149 }
2150
2151
2152 /**
2153 * Derivative value.
2154 *
2155 * \param x abscissa value
2156 * \return derivative value
2157 */
2158 double getDerivative(const double x) const
2159 {
2160 const double w = 1.0 / sigma;
2161 const double u = (x - center) / sigma;
2162
2163 return get(u) * -u * w;
2164 }
2165
2166
2167 /**
2168 * Get gradient.
2169 *
2170 * \param x abscissa value
2171 * \return gradient
2172 */
2173 JGauss getGradient(const double x) const
2174 {
2176
2177 const double w = 1.0 / sigma;
2178 const double u = (x - center) * w;
2179 const double f0 = get(u);
2180
2181 gradient.center = f0 * (u) * w; // d(f)/d(center)
2182 gradient.sigma = f0 * (u + 1.0) * (u - 1.0) * w; // d(f)/d(sigma)
2183
2184 return gradient;
2185 }
2186
2187 double center; //!< center
2188 double sigma; //!< sigma
2189
2191
2192 private:
2193 /**
2194 * Get ordinate value.
2195 *
2196 * \param u abscissa value
2197 * \return ordinate value
2198 */
2199 inline double get(const double u) const
2200 {
2201 static const double W = 1.0 / sqrt(2.0 * acos(-1.0));
2202
2203 return exp(-0.5*u*u) * W / sigma;
2204 }
2205 };
2206
2207
2208 /**
2209 * Power of function.
2210 */
2211 template<int ID_t, class JF1_t = JX>
2212 struct JPow :
2213 public JMathlib < JPow<ID_t, JF1_t> >,
2214 public JF1_t
2215 {
2216 static const int ID = ID_t;
2217
2218 using JMathlib< JPow<ID_t, JF1_t> >::operator();
2219 using JMathlib< JPow<ID_t, JF1_t> >::operator[];
2221
2222
2223 /**
2224 * Default constructor.
2225 */
2227 JF1_t(),
2228 alpha(0.0)
2229 {}
2230
2231
2232 /**
2233 * Constructor.
2234 *
2235 * \param alpha value
2236 */
2237 JPow(const double alpha) :
2238 JF1_t(),
2239 alpha(alpha)
2240 {}
2241
2242
2243 /**
2244 * Constructor.
2245 *
2246 * \param f1 function
2247 * \param alpha value
2248 */
2249 JPow(const JF1_t& f1, const double alpha) :
2250 JF1_t(f1),
2251 alpha(alpha)
2252 {}
2253
2254
2255 /**
2256 * Function value.
2257 *
2258 * \param x abscissa value
2259 * \return function value
2260 */
2261 double getValue(const double x) const
2262 {
2263 const double u = static_cast<const JF1_t&>(*this).getValue(x);
2264
2265 return pow(u, alpha);
2266 }
2267
2268
2269 /**
2270 * Derivative value.
2271 *
2272 * \param x abscissa value
2273 * \return derivative value
2274 */
2275 double getDerivative(const double x) const
2276 {
2277 const double u = static_cast<const JF1_t&>(*this).getValue(x);
2278 const double v = static_cast<const JF1_t&>(*this).getDerivative(x);
2279
2280 return alpha * pow(u, alpha - 1) * v;
2281 }
2282
2283
2284 /**
2285 * Get gradient.
2286 *
2287 * \param x abscissa value
2288 * \return gradient
2289 */
2290 JPow getGradient(const double x) const
2291 {
2292 JPow gradient;
2293
2294 const double u = static_cast<const JF1_t&>(*this).getValue(x);
2295 const double w = pow(u, alpha);
2296
2297 gradient.alpha = w * log(u);
2298
2299 static_cast<JF1_t&>(gradient) = static_cast<const JF1_t&>(*this).getGradient(x);
2300 static_cast<JF1_t&>(gradient) *= alpha * w / u;
2301
2302 return gradient;
2303 }
2304
2305 double alpha; //!< f(x)^alpha
2306
2307 static constexpr parameter_list<JPow, JF1_t::parameters.size() + 1> parameters = join<JPow>(JF1_t::parameters, &JPow::alpha);
2308 };
2309
2310
2311 /**
2312 * Power of function.
2313 *
2314 * \param f1 function
2315 * \param alpha alpha
2316 * \return result
2317 */
2318 template<class JF1_t>
2319 JPow<JF1_t::ID, JF1_t> Pow(const JF1_t& f1, const double alpha)
2320 {
2321 return JPow<JF1_t::ID, JF1_t>(f1, alpha);
2322 }
2323
2324
2325 /**
2326 * Square root of function.
2327 */
2328 template<class JF1_t = JX>
2329 struct JSqrt :
2330 public JMathlib < JSqrt<JF1_t> >,
2331 public JF1_t
2332 {
2333 using JMathlib< JSqrt<JF1_t> >::operator();
2334 using JMathlib< JSqrt<JF1_t> >::operator[];
2336
2337
2338 /**
2339 * Default constructor.
2340 */
2342 JF1_t()
2343 {}
2344
2345
2346 /**
2347 * Constructor.
2348 *
2349 * \param f1 function
2350 */
2351 JSqrt(const JF1_t& f1) :
2352 JF1_t(f1)
2353 {}
2354
2355
2356 /**
2357 * Function value.
2358 *
2359 * \param x abscissa value
2360 * \return function value
2361 */
2362 double getValue(const double x) const
2363 {
2364 const double u = static_cast<const JF1_t&>(*this).getValue(x);
2365
2366 return sqrt(u);
2367 }
2368
2369
2370 /**
2371 * Derivative value.
2372 *
2373 * \param x abscissa value
2374 * \return derivative value
2375 */
2376 double getDerivative(const double x) const
2377 {
2378 const double u = static_cast<const JF1_t&>(*this).getValue(x);
2379 const double v = static_cast<const JF1_t&>(*this).getDerivative(x);
2380
2381 return 0.5 * v / sqrt(u);
2382 }
2383
2384
2385 /**
2386 * Get gradient.
2387 *
2388 * \param x abscissa value
2389 * \return gradient
2390 */
2391 JSqrt getGradient(const double x) const
2392 {
2393 JSqrt gradient;
2394
2395 const double u = static_cast<const JF1_t&>(*this).getValue(x);
2396
2397 static_cast<JF1_t&>(gradient) = static_cast<const JF1_t&>(*this).getGradient(x);
2398 static_cast<JF1_t&>(gradient) *= 0.5 / sqrt(u);
2399
2400 return gradient;
2401 }
2402 };
2403
2404
2405 /**
2406 * Square root of function.
2407 *
2408 * \param f1 function
2409 * \return result
2410 */
2411 template<class JF1_t>
2412 JSqrt<JF1_t> Sqrt(const JF1_t& f1)
2413 {
2414 return JSqrt<JF1_t>(f1);
2415 }
2416
2417
2418 /**
2419 * Sine of function.
2420 */
2421 template<class JF1_t = JX>
2422 struct JSin :
2423 public JMathlib < JSin<JF1_t> >,
2424 public JF1_t
2425 {
2426 using JMathlib< JSin<JF1_t> >::operator();
2427 using JMathlib< JSin<JF1_t> >::operator[];
2428 using JMathlib< JSin<JF1_t> >::size;
2429
2430
2431 /**
2432 * Default constructor.
2433 */
2435 JF1_t()
2436 {}
2437
2438
2439 /**
2440 * Constructor.
2441 *
2442 * \param f1 function
2443 */
2444 JSin(const JF1_t& f1) :
2445 JF1_t(f1)
2446 {}
2447
2448
2449 /**
2450 * Function value.
2451 *
2452 * \param x abscissa value
2453 * \return function value
2454 */
2455 double getValue(const double x) const
2456 {
2457 const double u = static_cast<const JF1_t&>(*this).getValue(x);
2458
2459 return sin(u);
2460 }
2461
2462
2463 /**
2464 * Derivative value.
2465 *
2466 * \param x abscissa value
2467 * \return derivative value
2468 */
2469 double getDerivative(const double x) const
2470 {
2471 const double u = static_cast<const JF1_t&>(*this).getValue(x);
2472 const double v = static_cast<const JF1_t&>(*this).getDerivative(x);
2473
2474 return v * cos(u);
2475 }
2476
2477
2478 /**
2479 * Get gradient.
2480 *
2481 * \param x abscissa value
2482 * \return gradient
2483 */
2484 JSin getGradient(const double x) const
2485 {
2486 JSin gradient;
2487
2488 const double u = static_cast<const JF1_t&>(*this).getValue(x);
2489
2490 static_cast<JF1_t&>(gradient) = static_cast<const JF1_t&>(*this).getGradient(x);
2491 static_cast<JF1_t&>(gradient) *= cos(u);
2492
2493 return gradient;
2494 }
2495 };
2496
2497
2498 /**
2499 * Sine of function.
2500 *
2501 * \param f1 function
2502 * \return result
2503 */
2504 template<class JF1_t>
2505 JSin<JF1_t> Sin(const JF1_t& f1)
2506 {
2507 return JSin<JF1_t>(f1);
2508 }
2509
2510
2511 /**
2512 * Cosine of function.
2513 */
2514 template<class JF1_t = JX>
2515 struct JCos :
2516 public JMathlib < JCos<JF1_t> >,
2517 public JF1_t
2518 {
2519 using JMathlib< JCos<JF1_t> >::operator();
2520 using JMathlib< JCos<JF1_t> >::operator[];
2521 using JMathlib< JCos<JF1_t> >::size;
2522
2523
2524 /**
2525 * Default constructor.
2526 */
2528 JF1_t()
2529 {}
2530
2531
2532 /**
2533 * Constructor.
2534 *
2535 * \param f1 function
2536 */
2537 JCos(const JF1_t& f1) :
2538 JF1_t(f1)
2539 {}
2540
2541
2542 /**
2543 * Function value.
2544 *
2545 * \param x abscissa value
2546 * \return function value
2547 */
2548 double getValue(const double x) const
2549 {
2550 const double u = static_cast<const JF1_t&>(*this).getValue(x);
2551
2552 return cos(u);
2553 }
2554
2555
2556 /**
2557 * Derivative value.
2558 *
2559 * \param x abscissa value
2560 * \return derivative value
2561 */
2562 double getDerivative(const double x) const
2563 {
2564 const double u = static_cast<const JF1_t&>(*this).getValue(x);
2565 const double v = static_cast<const JF1_t&>(*this).getDerivative(x);
2566
2567 return v * -sin(u);
2568 }
2569
2570
2571 /**
2572 * Get gradient.
2573 *
2574 * \param x abscissa value
2575 * \return gradient
2576 */
2577 JCos getGradient(const double x) const
2578 {
2579 JCos gradient;
2580
2581 const double u = static_cast<const JF1_t&>(*this).getValue(x);
2582
2583 static_cast<JF1_t&>(gradient) = static_cast<const JF1_t&>(*this).getGradient(x);
2584 static_cast<JF1_t&>(gradient) *= -sin(u);
2585
2586 return gradient;
2587 }
2588 };
2589
2590
2591 /**
2592 * Cosine of function.
2593 *
2594 * \param f1 function
2595 * \return result
2596 */
2597 template<class JF1_t>
2598 JCos<JF1_t> Cos(const JF1_t& f1)
2599 {
2600 return JCos<JF1_t>(f1);
2601 }
2602
2603
2604 /**
2605 * Exponent of function.
2606 */
2607 template<class JF1_t = JX>
2608 struct JExp :
2609 public JMathlib< JExp<JF1_t> >,
2610 public JF1_t
2611 {
2612 using JMathlib< JExp<JF1_t> >::operator();
2613 using JMathlib< JExp<JF1_t> >::operator[];
2614 using JMathlib< JExp<JF1_t> >::size;
2615
2616
2617 /**
2618 * Default constructor.
2619 */
2621 JF1_t()
2622 {}
2623
2624
2625 /**
2626 * Constructor.
2627 *
2628 * \param f1 function
2629 */
2630 JExp(const JF1_t& f1) :
2631 JF1_t(f1)
2632 {}
2633
2634
2635 /**
2636 * Function value.
2637 *
2638 * \param x abscissa value
2639 * \return function value
2640 */
2641 double getValue(const double x) const
2642 {
2643 return exp(static_cast<const JF1_t&>(*this).getValue(x));
2644 }
2645
2646
2647 /**
2648 * Derivative value.
2649 *
2650 * \param x abscissa value
2651 * \return derivative value
2652 */
2653 double getDerivative(const double x) const
2654 {
2655 return static_cast<const JF1_t&>(*this).getDerivative(x) * getValue(x);
2656 }
2657
2658
2659 /**
2660 * Get gradient.
2661 *
2662 * \param x abscissa value
2663 * \return gradient
2664 */
2665 JExp getGradient(const double x) const
2666 {
2667 JExp gradient;
2668
2669 static_cast<JF1_t&>(gradient) = static_cast<const JF1_t&>(*this).getGradient(x);
2670 static_cast<JF1_t&>(gradient) *= getValue(x);
2671
2672 return gradient;
2673 }
2674 };
2675
2676
2677 /**
2678 * Exponent of zero degree polynomial function.
2679 */
2680 template<int ID_t>
2681 struct JExp< JPolynome<ID_t, 0> > :
2682 public JMathlib< JExp< JPolynome<ID_t, 0> > >,
2683 public JPolynome<ID_t, 0>
2684 {
2685 using JMathlib< JExp< JPolynome<ID_t, 0> > >::operator();
2686 using JMathlib< JExp< JPolynome<ID_t, 0> > >::operator[];
2688
2689
2690 /**
2691 * Default constructor.
2692 */
2694 JPolynome<ID_t, 0>()
2695 {}
2696
2697
2698 /**
2699 * Constructor.
2700 *
2701 * \param f1 function
2702 */
2704 JPolynome<ID_t, 0>(f1)
2705 {}
2706
2707
2708 /**
2709 * Function value.
2710 *
2711 * \return function value
2712 */
2713 double getValue(...) const
2714 {
2715 return exp(static_cast<const JPolynome<ID_t, 0>&>(*this).getValue());
2716 }
2717
2718
2719 /**
2720 * Derivative value.
2721 *
2722 * \return derivative value
2723 */
2724 double getDerivative(...) const
2725 {
2726 return static_cast<const JPolynome<ID_t, 0>&>(*this).getDerivative() * getValue();
2727 }
2728
2729
2730 /**
2731 * Get gradient.
2732 *
2733 * \return gradient
2734 */
2736 {
2737 JExp gradient;
2738
2739 static_cast<JPolynome<ID_t, 0>&>(gradient) = static_cast<const JPolynome<ID_t, 0>&>(*this).getGradient();
2740 static_cast<JPolynome<ID_t, 0>&>(gradient) *= getValue();
2741
2742 return gradient;
2743 }
2744 };
2745
2746
2747 /**
2748 * Exponent of function.
2749 *
2750 * \param f1 function
2751 * \return result
2752 */
2753 template<class JF1_t>
2754 JExp<JF1_t> Exp(const JF1_t& f1)
2755 {
2756 return JExp<JF1_t>(f1);
2757 }
2758
2759
2760 /**
2761 * Logarithm of function.
2762 */
2763 template<class JF1_t = JX>
2764 struct JLog :
2765 public JMathlib< JLog<JF1_t> >,
2766 public JF1_t
2767 {
2768 using JMathlib< JLog<JF1_t> >::operator();
2769 using JMathlib< JLog<JF1_t> >::operator[];
2770 using JMathlib< JLog<JF1_t> >::size;
2771
2772
2773 /**
2774 * Default constructor.
2775 */
2777 JF1_t()
2778 {}
2779
2780
2781 /**
2782 * Constructor.
2783 *
2784 * \param f1 function
2785 */
2786 JLog(const JF1_t& f1) :
2787 JF1_t(f1)
2788 {}
2789
2790
2791 /**
2792 * Function value.
2793 *
2794 * \param x abscissa value
2795 * \return function value
2796 */
2797 double getValue(const double x) const
2798 {
2799 return log(static_cast<const JF1_t&>(*this).getValue(x));
2800 }
2801
2802
2803 /**
2804 * Derivative value.
2805 *
2806 * \param x abscissa value
2807 * \return derivative value
2808 */
2809 double getDerivative(const double x) const
2810 {
2811 return static_cast<const JF1_t&>(*this).getDerivative(x) / static_cast<const JF1_t&>(*this).getValue(x);
2812 }
2813
2814
2815 /**
2816 * Get gradient.
2817 *
2818 * \param x abscissa value
2819 * \return gradient
2820 */
2821 JLog getGradient(const double x) const
2822 {
2823 JLog gradient;
2824
2825 static_cast<JF1_t&>(gradient) = static_cast<const JF1_t&>(*this).getGradient(x);
2826 static_cast<JF1_t&>(gradient) /= (static_cast<const JF1_t&>(*this).getValue(x));
2827
2828 return gradient;
2829 }
2830 };
2831
2832
2833 /**
2834 * Logarithm of function.
2835 *
2836 * \param f1 function
2837 * \return result
2838 */
2839 template<class JF1_t>
2840 JLog<JF1_t> Log(const JF1_t& f1)
2841 {
2842 return JLog<JF1_t>(f1);
2843 }
2844}
2845
2846#endif
constexpr size_t getNumberOfParameters()
Get number of parameters.
Definition JMathlib.hh:136
void setParameters(JF1_t *f1, const double *values)
Set values of all parameters.
Definition JMathlib.hh:149
JPow< JF1_t::ID, JF1_t > Pow(const JF1_t &f1, const double alpha)
Power of function.
Definition JMathlib.hh:2319
static const JXn< 5 > X5
Function object for x^4;.
Definition JMathlib.hh:1677
JSqrt< JF1_t > Sqrt(const JF1_t &f1)
Square root of function.
Definition JMathlib.hh:2412
JExp< JF1_t > Exp(const JF1_t &f1)
Exponent of function.
Definition JMathlib.hh:2754
static const JXn< 3 > X3
Function object for x^2;.
Definition JMathlib.hh:1675
T pow(const T &x, const double y)
Power .
Definition JMath.hh:97
static const JXn< 4 > X4
Function object for x^3;.
Definition JMathlib.hh:1676
JCos< JF1_t > Cos(const JF1_t &f1)
Cosine of function.
Definition JMathlib.hh:2598
constexpr parameter_list< U, sizeof...(i)> convert(const parameter_list< V, sizeof...(i)> &parameters, const std::index_sequence< i... > &ls)
Auxiliary method to convert parameter list.
Definition JMathlib.hh:110
constexpr parameter_list< U, sizeof...(i)+1 > join(const parameter_list< V, sizeof...(i)> &parameters, const std::index_sequence< i... > &ls, double W::*parameter)
Auxiliary method to join parameter and list of parameters.
Definition JMathlib.hh:44
static const JXn< 2 > X2
Invalidate x^0.
Definition JMathlib.hh:1674
static const JX X
Definition JMathlib.hh:1613
JSin< JF1_t > Sin(const JF1_t &f1)
Sine of function.
Definition JMathlib.hh:2505
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition log.hh:13
double getDerivative(const double x) const
Derivative value.
Definition JMathlib.hh:694
JAdd getGradient(const Args &...args) const
Get gradient.
Definition JMathlib.hh:707
JAdd()
Default constructor.
Definition JMathlib.hh:659
JAdd(const JF1_t &f1, const double value)
Constructor.
Definition JMathlib.hh:669
double getValue(const Args &...args) const
Function value.
Definition JMathlib.hh:682
Addition of two functions.
Definition JMathlib.hh:1157
JAdd getGradient(const Args &...args) const
Get gradient.
Definition JMathlib.hh:1215
double getDerivative(const double x) const
Derivative value.
Definition JMathlib.hh:1201
JAdd()
Default constructor.
Definition JMathlib.hh:1166
double getValue(const Args &...args) const
Function value.
Definition JMathlib.hh:1188
JAdd(const JF1_t &f1, const JF2_t &f2)
Constructor.
Definition JMathlib.hh:1176
Auxiliary base class for mathematical operations on parameters of function.
Definition JMathlib.hh:164
JF1_t & div(const double factor)
Scale function.
Definition JMathlib.hh:234
JF1_t & mul(const double factor)
Scale function.
Definition JMathlib.hh:218
JF1_t & add(const JF1_t &f1)
Add function.
Definition JMathlib.hh:186
friend JF1_t & operator+=(JF1_t &function, const JF1_t &value)
Add function.
Definition JMathlib.hh:251
friend JF1_t & operator/=(JF1_t &function, const double factor)
Scale function.
Definition JMathlib.hh:290
friend JF1_t & operator*=(JF1_t &function, const double factor)
Scale function.
Definition JMathlib.hh:277
JF1_t & sub(const JF1_t &f1)
Subtract function.
Definition JMathlib.hh:202
JF1_t & negate()
Negate function.
Definition JMathlib.hh:170
friend JF1_t & operator-=(JF1_t &function, const JF1_t &value)
Subtract function.
Definition JMathlib.hh:264
Cosine of function.
Definition JMathlib.hh:2518
JCos(const JF1_t &f1)
Constructor.
Definition JMathlib.hh:2537
double getDerivative(const double x) const
Derivative value.
Definition JMathlib.hh:2562
JCos getGradient(const double x) const
Get gradient.
Definition JMathlib.hh:2577
double getValue(const double x) const
Function value.
Definition JMathlib.hh:2548
JCos()
Default constructor.
Definition JMathlib.hh:2527
JDiv(const JF1_t &f1, const double value)
Constructor.
Definition JMathlib.hh:904
double getValue(const Args &...args) const
Function value.
Definition JMathlib.hh:917
JDiv getGradient(const Args &...args) const
Get gradient.
Definition JMathlib.hh:942
JDiv()
Default constructor.
Definition JMathlib.hh:894
double getDerivative(const double x) const
Derivative value.
Definition JMathlib.hh:929
Division of two functions.
Definition JMathlib.hh:1391
double getValue(const Args &...args) const
Function value.
Definition JMathlib.hh:1422
JDiv getGradient(const Args &...args) const
Get gradient.
Definition JMathlib.hh:1452
JDiv()
Default constructor.
Definition JMathlib.hh:1400
JDiv(const JF1_t &f1, const JF2_t &f2)
Constructor.
Definition JMathlib.hh:1410
double getDerivative(const double x) const
Derivative value.
Definition JMathlib.hh:1435
double getDerivative(...) const
Derivative value.
Definition JMathlib.hh:2724
double getValue(...) const
Function value.
Definition JMathlib.hh:2713
JExp(const JPolynome< ID_t, 0 > &f1)
Constructor.
Definition JMathlib.hh:2703
JExp getGradient(...) const
Get gradient.
Definition JMathlib.hh:2735
Exponent of function.
Definition JMathlib.hh:2611
JExp()
Default constructor.
Definition JMathlib.hh:2620
JExp getGradient(const double x) const
Get gradient.
Definition JMathlib.hh:2665
double getDerivative(const double x) const
Derivative value.
Definition JMathlib.hh:2653
double getValue(const double x) const
Function value.
Definition JMathlib.hh:2641
JExp(const JF1_t &f1)
Constructor.
Definition JMathlib.hh:2630
Fixed power of function.
Definition JMathlib.hh:1476
JFn()
Default constructor.
Definition JMathlib.hh:1485
JFn(const JF1_t &f1, const int N)
Constructor.
Definition JMathlib.hh:1497
double getDerivative(const double x) const
Derivative value.
Definition JMathlib.hh:1524
JFn getGradient(const Args &...args) const
Get gradient.
Definition JMathlib.hh:1540
double getValue(const Args &...args) const
Function value.
Definition JMathlib.hh:1510
JGauss(const double center, const double sigma)
Constructor.
Definition JMathlib.hh:2131
JGauss getGradient(const double x) const
Get gradient.
Definition JMathlib.hh:2173
JGauss()
Default constructor.
Definition JMathlib.hh:2119
double getValue(const double x) const
Function value.
Definition JMathlib.hh:2144
double getDerivative(const double x) const
Derivative value.
Definition JMathlib.hh:2158
double get(const double u) const
Get ordinate value.
Definition JMathlib.hh:2199
Gauss function object.
Definition JMathlib.hh:2011
double sigma
sigma
Definition JMathlib.hh:2087
double getDerivative(const double x) const
Derivative value.
Definition JMathlib.hh:2057
JGauss()
Default constructor.
Definition JMathlib.hh:2018
double center
center
Definition JMathlib.hh:2086
static constexpr parameter_list< JGauss, 2 > parameters
Definition JMathlib.hh:2089
double getValue(const double x) const
Function value.
Definition JMathlib.hh:2043
JGauss_t gradient
Definition JGauss.hh:364
JGauss getGradient(const double x) const
Get gradient.
Definition JMathlib.hh:2072
double get(const double u) const
Get ordinate value.
Definition JGauss.hh:357
JGauss(const double center, const double sigma)
Constructor.
Definition JMathlib.hh:2030
Logarithm of function.
Definition JMathlib.hh:2767
double getValue(const double x) const
Function value.
Definition JMathlib.hh:2797
JLog getGradient(const double x) const
Get gradient.
Definition JMathlib.hh:2821
JLog()
Default constructor.
Definition JMathlib.hh:2776
JLog(const JF1_t &f1)
Constructor.
Definition JMathlib.hh:2786
double getDerivative(const double x) const
Derivative value.
Definition JMathlib.hh:2809
Auxiliary base class for mathematical operations on functions.
Definition JMathlib.hh:323
friend JMul< JF1_t, JF2_t > operator*(const JF1_t &f1, const JF2_t &f2)
Multiplication of two functions.
Definition JMathlib.hh:488
friend JDiv< JF1_t, JF2_t > operator/(const JF1_t &f1, const JF2_t &f2)
Division of two functions.
Definition JMathlib.hh:502
friend JAdd< JNegate< JF1_t > > operator-(const double value, const JF1_t &f1)
Subtraction of constant value.
Definition JMathlib.hh:407
friend JFn< JF1_t > operator^(const JF1_t &f1, int N)
Power-of operator.
Definition JMathlib.hh:515
double operator[](const size_t i) const
Get value of parameter at given index.
Definition JMathlib.hh:550
double & operator[](const size_t i)
Get value of parameter at given index.
Definition JMathlib.hh:562
friend JAdd< JF1_t > operator+(const double value, const JF1_t &f1)
Addition of constant value.
Definition JMathlib.hh:381
friend JSub< JF1_t > operator-(const JF1_t &f1, const double value)
Subtraction of constant value.
Definition JMathlib.hh:394
friend const JF1_t & operator+(const JF1_t &function)
Affirm operator.
Definition JMathlib.hh:343
friend JMul< JF1_t > operator*(const double value, const JF1_t &f1)
Multiplication of constant value.
Definition JMathlib.hh:433
friend JSub< JF1_t, JF2_t > operator-(const JF1_t &f1, const JF2_t &f2)
Subtraction of two functions.
Definition JMathlib.hh:474
friend JDiv< JF1_t > operator/(const JF1_t &f1, const double value)
Division of constant value.
Definition JMathlib.hh:446
double operator()(const Args &...args) const
Function value.
Definition JMathlib.hh:331
friend JAdd< JF1_t > operator+(const JF1_t &f1, const double value)
Addition of constant value.
Definition JMathlib.hh:368
static size_t size()
Get size of parameter.
Definition JMathlib.hh:538
friend JPf< JF1_t > operator&(const JF1_t &f1)
Address-of operator.
Definition JMathlib.hh:527
friend JNegate< JF1_t > operator-(const JF1_t &function)
Negate operator.
Definition JMathlib.hh:355
friend JAdd< JF1_t, JF2_t > operator+(const JF1_t &f1, const JF2_t &f2)
Addition of two functions.
Definition JMathlib.hh:460
friend JMul< JF1_t > operator*(const JF1_t &f1, const double value)
Multiplication of constant value.
Definition JMathlib.hh:420
double getValue(const Args &...args) const
Function value.
Definition JMathlib.hh:838
JMul(const JF1_t &f1, const double value)
Constructor.
Definition JMathlib.hh:825
double getDerivative(const double x) const
Derivative value.
Definition JMathlib.hh:850
JMul getGradient(const Args &...args) const
Get gradient.
Definition JMathlib.hh:863
JMul()
Default constructor.
Definition JMathlib.hh:815
Multiplication of two functions.
Definition JMathlib.hh:1312
double getValue(const Args &...args) const
Function value.
Definition JMathlib.hh:1343
JMul()
Default constructor.
Definition JMathlib.hh:1321
JMul(const JF1_t &f1, const JF2_t &f2)
Constructor.
Definition JMathlib.hh:1331
JMul getGradient(const Args &...args) const
Get gradient.
Definition JMathlib.hh:1370
double getDerivative(const double x) const
Derivative value.
Definition JMathlib.hh:1356
Negate of function.
Definition JMathlib.hh:576
double getValue(const Args &...args) const
Function value.
Definition JMathlib.hh:606
JNegate()
Default constructor.
Definition JMathlib.hh:585
JNegate(const JF1_t &f1)
Constructor.
Definition JMathlib.hh:594
JNegate getGradient(const Args &...args) const
Get gradient.
Definition JMathlib.hh:631
double getDerivative(const double x) const
Derivative value.
Definition JMathlib.hh:618
JPair(const JF1_t &f1, const JF2_t &f2)
Constructor.
Definition JMathlib.hh:1096
JPair(const JF1_t &f1, const JF2_t &f2)
Constructor.
Definition JMathlib.hh:1057
JPair(const JF1_t &f1, const JF2_t &f2)
Constructor.
Definition JMathlib.hh:1017
Auxiliary data structure for pair of functions.
Definition JMathlib.hh:1123
JPair_t()
Default constructor.
Definition JMathlib.hh:1134
JPair_t(const JF1_t &f1, const JF2_t &f2)
Constructor.
Definition JMathlib.hh:1144
Auxiliary data structure for pair of functions.
Definition JMathlib.hh:966
JF1_t & f1()
Definition JMathlib.hh:989
const JF2_t & f2() const
Definition JMathlib.hh:987
const JF1_t & f1() const
Definition JMathlib.hh:986
JPair(const JF1_t &f1, const JF2_t &f2)
Constructor.
Definition JMathlib.hh:980
JF2_t & f2()
Definition JMathlib.hh:990
JPair()
Default constructor.
Definition JMathlib.hh:970
static constexpr parameter_list< JPair, JF1_t::parameters.size()+JF2_t::parameters.size()> parameters
Definition JMathlib.hh:993
Function object for x^5;.
Definition JMathlib.hh:1687
const JF1_t * f1
Definition JMathlib.hh:1753
JPf getGradient(const Args &...args) const
Get gradient.
Definition JMathlib.hh:1743
JPf()
Default constructor.
Definition JMathlib.hh:1696
JPf(const JF1_t *f1)
Constructor.
Definition JMathlib.hh:1706
double getValue(const Args &...args) const
Function value.
Definition JMathlib.hh:1718
static constexpr parameter_list< JPf, 0 > parameters
Definition JMathlib.hh:1750
double getDerivative(const double x) const
Derivative value.
Definition JMathlib.hh:1730
Termination class for polynomial function.
Definition JMathlib.hh:1897
JPolynome(const std::array< T, 1 > &args)
Constructor.
Definition JMathlib.hh:1927
void set(const std::array< T, 1 > &args)
Set value.
Definition JMathlib.hh:1940
JPolynome getGradient(...) const
Get gradient.
Definition JMathlib.hh:1973
double getValue(...) const
Function value.
Definition JMathlib.hh:1951
double getDerivative(...) const
Derivative value.
Definition JMathlib.hh:1962
JPolynome()
Default constructor.
Definition JMathlib.hh:1906
JPolynome(const double a)
Constructor.
Definition JMathlib.hh:1916
JPolynome_t & mul(const double factor)
Scale polynome.
Definition JPolynome.hh:113
JPolynome_t & sub(const JPolynome_t &polynome)
Subtract polynome.
Definition JPolynome.hh:93
JPolynome_t & add(const JPolynome_t &polynome)
Add polynome.
Definition JPolynome.hh:73
Recursive template class for polynomial function.
Definition JPolynome.hh:165
static const size_t NUMBER_OF_DEGREES
Definition JMathlib.hh:1768
JPolynome()
Default constructor.
Definition JMathlib.hh:1783
JPolynome(const Args &...args)
Constructor.
Definition JMathlib.hh:1794
double getValue(const double x) const
Function value.
Definition JMathlib.hh:1834
void set(const std::array< T, N+1 > &args, const std::index_sequence< i... > &ls)
Set values.
Definition JMathlib.hh:1882
void set(const std::array< T, N+1 > &args)
Set values.
Definition JMathlib.hh:1820
JPolynome getGradient(const double x) const
Get gradient.
Definition JMathlib.hh:1858
double getDerivative(const double x) const
Derivative value.
Definition JMathlib.hh:1846
static constexpr parameter_list< JPolynome, N+1 > parameters
Definition JMathlib.hh:1871
JPolynome(const std::array< T, N+1 > &args)
Constructor.
Definition JMathlib.hh:1807
Power of function.
Definition JMathlib.hh:2215
double getValue(const double x) const
Function value.
Definition JMathlib.hh:2261
JPow(const JF1_t &f1, const double alpha)
Constructor.
Definition JMathlib.hh:2249
double alpha
f(x)^alpha
Definition JMathlib.hh:2305
JPow getGradient(const double x) const
Get gradient.
Definition JMathlib.hh:2290
JPow(const double alpha)
Constructor.
Definition JMathlib.hh:2237
JPow()
Default constructor.
Definition JMathlib.hh:2226
static constexpr parameter_list< JPow, JF1_t::parameters.size()+1 > parameters
Definition JMathlib.hh:2307
double getDerivative(const double x) const
Derivative value.
Definition JMathlib.hh:2275
Sine of function.
Definition JMathlib.hh:2425
JSin()
Default constructor.
Definition JMathlib.hh:2434
double getDerivative(const double x) const
Derivative value.
Definition JMathlib.hh:2469
double getValue(const double x) const
Function value.
Definition JMathlib.hh:2455
JSin getGradient(const double x) const
Get gradient.
Definition JMathlib.hh:2484
JSin(const JF1_t &f1)
Constructor.
Definition JMathlib.hh:2444
Square root of function.
Definition JMathlib.hh:2332
JSqrt()
Default constructor.
Definition JMathlib.hh:2341
double getValue(const double x) const
Function value.
Definition JMathlib.hh:2362
double getDerivative(const double x) const
Derivative value.
Definition JMathlib.hh:2376
JSqrt getGradient(const double x) const
Get gradient.
Definition JMathlib.hh:2391
JSqrt(const JF1_t &f1)
Constructor.
Definition JMathlib.hh:2351
double getValue(const Args &...args) const
Function value.
Definition JMathlib.hh:760
JSub getGradient(const Args &...args) const
Get gradient.
Definition JMathlib.hh:785
JSub()
Default constructor.
Definition JMathlib.hh:737
double getDerivative(const double x) const
Derivative value.
Definition JMathlib.hh:772
JSub(const JF1_t &f1, const double value)
Constructor.
Definition JMathlib.hh:747
Subtraction of two functions.
Definition JMathlib.hh:1234
double getDerivative(const double x) const
Derivative value.
Definition JMathlib.hh:1278
double getValue(const Args &...args) const
Function value.
Definition JMathlib.hh:1265
JSub getGradient(const Args &...args) const
Get gradient.
Definition JMathlib.hh:1292
JSub()
Default constructor.
Definition JMathlib.hh:1243
JSub(const JF1_t &f1, const JF2_t &f2)
Constructor.
Definition JMathlib.hh:1253
double getValue(const double x) const
Function value.
Definition JMathlib.hh:1578
JX getGradient(const double x) const
Get gradient.
Definition JMathlib.hh:1602
JX()
Default constructor.
Definition JMathlib.hh:1568
static constexpr parameter_list< JX, 0 > parameters
Definition JMathlib.hh:1609
double getDerivative(const double x) const
Derivative value.
Definition JMathlib.hh:1590
Function object for x;.
Definition JMathlib.hh:1623
double getValue(const double x) const
Function value.
Definition JMathlib.hh:1637
double getDerivative(const double x) const
Derivative value.
Definition JMathlib.hh:1649
JXn()
Default constructor.
Definition JMathlib.hh:1627
JXn getGradient(const double x) const
Get gradient.
Definition JMathlib.hh:1661
static constexpr parameter_list< JXn, 0 > parameters
Definition JMathlib.hh:1668