CoDiPack  2.3.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
unaryOperators.hpp
1/*
2 * CoDiPack, a Code Differentiation Package
3 *
4 * Copyright (C) 2015-2024 Chair for Scientific Computing (SciComp), University of Kaiserslautern-Landau
5 * Homepage: http://scicomp.rptu.de
6 * Contact: Prof. Nicolas R. Gauger (codi@scicomp.uni-kl.de)
7 *
8 * Lead developers: Max Sagebaum, Johannes Blühdorn (SciComp, University of Kaiserslautern-Landau)
9 *
10 * This file is part of CoDiPack (http://scicomp.rptu.de/software/codi).
11 *
12 * CoDiPack is free software: you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, either version 3 of the
15 * License, or (at your option) any later version.
16 *
17 * CoDiPack is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty
19 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20 *
21 * See the GNU General Public License for more details.
22 * You should have received a copy of the GNU
23 * General Public License along with CoDiPack.
24 * If not, see <http://www.gnu.org/licenses/>.
25 *
26 * For other licensing options please contact us.
27 *
28 * Authors:
29 * - SciComp, University of Kaiserslautern-Landau:
30 * - Max Sagebaum
31 * - Johannes Blühdorn
32 * - Former members:
33 * - Tim Albring
34 */
35#pragma once
36
37#include <cmath>
38
39#include "../../config.h"
40#include "../../misc/exceptions.hpp"
41#include "../../misc/macros.hpp"
42#include "../../traits/realTraits.hpp"
43#include "../unaryExpression.hpp"
44
46namespace codi {
47
48 /*******************************************************************************/
51
53 template<typename T_Real>
54 struct OperationUnaryMinus : public UnaryOperation<T_Real> {
55 public:
56
57 using Real = CODI_DD(T_Real, double);
58
60 template<typename Arg>
61 static CODI_INLINE Real primal(Arg const& arg) {
62 return -arg;
63 }
64
66 template<typename Arg>
67 static CODI_INLINE RealTraits::PassiveReal<Real> gradient(Arg const& arg, Real const& result) {
68 CODI_UNUSED(arg);
69 CODI_UNUSED(result);
70 return -1.0;
71 }
72
74 static CODI_INLINE std::string getMathRep() {
75 return "-";
76 }
77 };
78#define OPERATION_LOGIC OperationUnaryMinus
79#define FUNCTION operator-
80#include "unaryOverloads.tpp"
81
83 template<typename Real, typename Arg>
87
89 /*******************************************************************************/
92
93 using std::abs;
94 using std::acos;
95 using std::acosh;
96 using std::asin;
97 using std::asinh;
98 using std::atan;
99 using std::atanh;
100 using std::cbrt;
101 using std::ceil;
102 using std::cos;
103 using std::cosh;
104 using std::erf;
105 using std::erfc;
106 using std::exp;
107 using std::fabs;
108 using std::floor;
109 using std::isfinite;
110 using std::isinf;
111 using std::isnan;
112 using std::isnormal;
113 using std::log;
114 using std::log10;
115 using std::log1p;
116 using std::log2;
117 using std::round;
118 using std::sin;
119 using std::sinh;
120 using std::sqrt;
121 using std::tan;
122 using std::tanh;
123 using std::tgamma;
124
126 template<typename T_Real>
127 struct OperationAbs : public UnaryOperation<T_Real> {
128 public:
129
130 using Real = CODI_DD(T_Real, double);
131
133 template<typename Arg>
134 static CODI_INLINE Real primal(Arg const& arg) {
135 return abs(arg);
136 }
137
139 template<typename Arg>
140 static CODI_INLINE RealTraits::PassiveReal<Real> gradient(Arg const& arg, Real const& result) {
141 CODI_UNUSED(result);
142 if (arg < 0.0) {
143 return -1.0;
144 } else if (arg > 0.0) {
145 return 1.0;
146 } else {
147 return 0.0;
148 }
149 }
150
152 static CODI_INLINE std::string getMathRep() {
153 return "abs";
154 }
155 };
156#define OPERATION_LOGIC OperationAbs
157#define FUNCTION abs
158#include "unaryOverloads.tpp"
159
160#define OPERATION_LOGIC OperationAbs
161#define FUNCTION fabs
162#include "unaryOverloads.tpp"
163
164#define OPERATION_LOGIC OperationAbs
165#define FUNCTION fabsf
166#include "unaryOverloads.tpp"
167
168#define OPERATION_LOGIC OperationAbs
169#define FUNCTION fabsl
170#include "unaryOverloads.tpp"
171
173 template<typename T_Real>
174 struct OperationAcos : public UnaryOperation<T_Real> {
175 public:
176
177 using Real = CODI_DD(T_Real, double);
178
180 template<typename Arg>
181 static CODI_INLINE Real primal(Arg const& arg) {
182 return acos(arg);
183 }
184
186 template<typename Arg>
187 static CODI_INLINE Real gradient(Arg const& arg, Real const& result) {
188 CODI_UNUSED(result);
190 if (RealTraits::getPassiveValue(arg) <= -1.0 || 1.0 <= RealTraits::getPassiveValue(arg)) {
191 CODI_EXCEPTION("acos outside of (-1, 1).(Value: %0.15e)", RealTraits::getPassiveValue(arg));
192 }
193 }
194 return -1.0 / sqrt(1.0 - arg * arg);
195 }
196
198 static CODI_INLINE std::string getMathRep() {
199 return "acos";
200 }
201 };
202#define OPERATION_LOGIC OperationAcos
203#define FUNCTION acos
204#include "unaryOverloads.tpp"
205
206#define OPERATION_LOGIC OperationAcos
207#define FUNCTION acosf
208#include "unaryOverloads.tpp"
209
210#define OPERATION_LOGIC OperationAcos
211#define FUNCTION acosl
212#include "unaryOverloads.tpp"
213
215 template<typename T_Real>
216 struct OperationAcosh : public UnaryOperation<T_Real> {
217 public:
218
219 using Real = CODI_DD(T_Real, double);
220
222 template<typename Arg>
223 static CODI_INLINE Real primal(Arg const& arg) {
224 return acosh(arg);
225 }
226
228 template<typename Arg>
229 static CODI_INLINE Real gradient(Arg const& arg, Real const& result) {
230 CODI_UNUSED(result);
232 if (RealTraits::getPassiveValue(arg) <= 1.0) {
233 CODI_EXCEPTION("acosh outside of (1, inf).(Value: %0.15e)", RealTraits::getPassiveValue(arg));
234 }
235 }
236 return 1.0 / sqrt(arg * arg - 1.0);
237 }
238
240 static CODI_INLINE std::string getMathRep() {
241 return "acosh";
242 }
243 };
244#define OPERATION_LOGIC OperationAcosh
245#define FUNCTION acosh
246#include "unaryOverloads.tpp"
247
248#define OPERATION_LOGIC OperationAcosh
249#define FUNCTION acoshf
250#include "unaryOverloads.tpp"
251
252#define OPERATION_LOGIC OperationAcosh
253#define FUNCTION acoshl
254#include "unaryOverloads.tpp"
255
257 template<typename T_Real>
258 struct OperationAsin : public UnaryOperation<T_Real> {
259 public:
260
261 using Real = CODI_DD(T_Real, double);
262
264 template<typename Arg>
265 static CODI_INLINE Real primal(Arg const& arg) {
266 return asin(arg);
267 }
268
270 template<typename Arg>
271 static CODI_INLINE Real gradient(Arg const& arg, Real const& result) {
272 CODI_UNUSED(result);
274 if (RealTraits::getPassiveValue(arg) <= -1.0 || 1.0 <= RealTraits::getPassiveValue(arg)) {
275 CODI_EXCEPTION("asin outside of (-1, 1).(Value: %0.15e)", RealTraits::getPassiveValue(arg));
276 }
277 }
278 return 1.0 / sqrt(1.0 - arg * arg);
279 }
280
282 static CODI_INLINE std::string getMathRep() {
283 return "asin";
284 }
285 };
286#define OPERATION_LOGIC OperationAsin
287#define FUNCTION asin
288#include "unaryOverloads.tpp"
289
290#define OPERATION_LOGIC OperationAsin
291#define FUNCTION asinf
292#include "unaryOverloads.tpp"
293
294#define OPERATION_LOGIC OperationAsin
295#define FUNCTION asinl
296#include "unaryOverloads.tpp"
297
299 template<typename T_Real>
300 struct OperationAsinh : public UnaryOperation<T_Real> {
301 public:
302
303 using Real = CODI_DD(T_Real, double);
304
306 template<typename Arg>
307 static CODI_INLINE Real primal(Arg const& arg) {
308 return asinh(arg);
309 }
310
312 template<typename Arg>
313 static CODI_INLINE Real gradient(Arg const& arg, Real const& result) {
314 CODI_UNUSED(result);
315 return 1.0 / sqrt(arg * arg + 1.0);
316 }
317
319 static CODI_INLINE std::string getMathRep() {
320 return "asinh";
321 }
322 };
323#define OPERATION_LOGIC OperationAsinh
324#define FUNCTION asinh
325#include "unaryOverloads.tpp"
326
327#define OPERATION_LOGIC OperationAsinh
328#define FUNCTION asinhf
329#include "unaryOverloads.tpp"
330
331#define OPERATION_LOGIC OperationAsinh
332#define FUNCTION asinhl
333#include "unaryOverloads.tpp"
334
336 template<typename T_Real>
337 struct OperationAtan : public UnaryOperation<T_Real> {
338 public:
339
340 using Real = CODI_DD(T_Real, double);
341
343 template<typename Arg>
344 static CODI_INLINE Real primal(Arg const& arg) {
345 return atan(arg);
346 }
347
349 template<typename Arg>
350 static CODI_INLINE Real gradient(Arg const& arg, Real const& result) {
351 CODI_UNUSED(result);
352 return 1.0 / (1.0 + arg * arg);
353 }
354
356 static CODI_INLINE std::string getMathRep() {
357 return "atan";
358 }
359 };
360#define OPERATION_LOGIC OperationAtan
361#define FUNCTION atan
362#include "unaryOverloads.tpp"
363
364#define OPERATION_LOGIC OperationAtan
365#define FUNCTION atanf
366#include "unaryOverloads.tpp"
367
368#define OPERATION_LOGIC OperationAtan
369#define FUNCTION atanl
370#include "unaryOverloads.tpp"
371
373 template<typename T_Real>
374 struct OperationAtanh : public UnaryOperation<T_Real> {
375 public:
376
377 using Real = CODI_DD(T_Real, double);
378
380 template<typename Arg>
381 static CODI_INLINE Real primal(Arg const& arg) {
382 return atanh(arg);
383 }
384
386 template<typename Arg>
387 static CODI_INLINE Real gradient(Arg const& arg, Real const& result) {
388 CODI_UNUSED(result);
390 if (RealTraits::getPassiveValue(arg) <= -1.0 || 1.0 <= RealTraits::getPassiveValue(arg)) {
391 CODI_EXCEPTION("atanh outside of (-1, 1).(Value: %0.15e)", RealTraits::getPassiveValue(arg));
392 }
393 }
394 return 1.0 / (1.0 - arg * arg);
395 }
396
398 static CODI_INLINE std::string getMathRep() {
399 return "atanh";
400 }
401 };
402#define OPERATION_LOGIC OperationAtanh
403#define FUNCTION atanh
404#include "unaryOverloads.tpp"
405
406#define OPERATION_LOGIC OperationAtanh
407#define FUNCTION atanhf
408#include "unaryOverloads.tpp"
409
410#define OPERATION_LOGIC OperationAtanh
411#define FUNCTION atanhl
412#include "unaryOverloads.tpp"
413
415 template<typename T_Real>
416 struct OperationCbrt : public UnaryOperation<T_Real> {
417 public:
418
419 using Real = CODI_DD(T_Real, double);
420
422 template<typename Arg>
423 static CODI_INLINE Real primal(Arg const& arg) {
424 return cbrt(arg);
425 }
426
428 template<typename Arg>
429 static CODI_INLINE Real gradient(Arg const& arg, Real const& result) {
431 if (0.0 == RealTraits::getPassiveValue(arg)) {
432 CODI_EXCEPTION("Cbrt of zero value.(Value: %0.15e)", RealTraits::getPassiveValue(arg));
433 }
434 }
435 if (result != 0.0) {
436 return 1.0 / (3.0 * result * result);
437 } else {
438 return (Real)0.0;
439 }
440 }
441
443 static CODI_INLINE std::string getMathRep() {
444 return "cbrt";
445 }
446 };
447#define OPERATION_LOGIC OperationCbrt
448#define FUNCTION cbrt
449#include "unaryOverloads.tpp"
450
451#define OPERATION_LOGIC OperationCbrt
452#define FUNCTION cbrtf
453#include "unaryOverloads.tpp"
454
455#define OPERATION_LOGIC OperationCbrt
456#define FUNCTION cbrtl
457#include "unaryOverloads.tpp"
458
460 template<typename Real, typename Arg>
464
466 template<typename Real, typename Arg>
470
472 template<typename Real, typename Arg>
476
478 template<typename T_Real>
479 struct OperationCos : public UnaryOperation<T_Real> {
480 public:
481
482 using Real = CODI_DD(T_Real, double);
483
485 template<typename Arg>
486 static CODI_INLINE Real primal(Arg const& arg) {
487 return cos(arg);
488 }
489
491 template<typename Arg>
492 static CODI_INLINE Real gradient(Arg const& arg, Real const& result) {
493 CODI_UNUSED(result);
494 return -sin(arg);
495 }
496
498 static CODI_INLINE std::string getMathRep() {
499 return "cos";
500 }
501 };
502#define OPERATION_LOGIC OperationCos
503#define FUNCTION cos
504#include "unaryOverloads.tpp"
505
506#define OPERATION_LOGIC OperationCos
507#define FUNCTION cosf
508#include "unaryOverloads.tpp"
509
510#define OPERATION_LOGIC OperationCos
511#define FUNCTION cosl
512#include "unaryOverloads.tpp"
513
515 template<typename T_Real>
516 struct OperationCosh : public UnaryOperation<T_Real> {
517 public:
518
519 using Real = CODI_DD(T_Real, double);
520
522 template<typename Arg>
523 static CODI_INLINE Real primal(Arg const& arg) {
524 return cosh(arg);
525 }
526
528 template<typename Arg>
529 static CODI_INLINE Real gradient(Arg const& arg, Real const& result) {
530 CODI_UNUSED(result);
531 return sinh(arg);
532 }
533
535 static CODI_INLINE std::string getMathRep() {
536 return "sinh";
537 }
538 };
539#define OPERATION_LOGIC OperationCosh
540#define FUNCTION cosh
541#include "unaryOverloads.tpp"
542
543#define OPERATION_LOGIC OperationCosh
544#define FUNCTION coshf
545#include "unaryOverloads.tpp"
546
547#define OPERATION_LOGIC OperationCosh
548#define FUNCTION coshl
549#include "unaryOverloads.tpp"
550
552 template<typename T_Real>
553 struct OperationErf : public UnaryOperation<T_Real> {
554 public:
555
556 using Real = CODI_DD(T_Real, double);
557
559 template<typename Arg>
560 static CODI_INLINE Real primal(Arg const& arg) {
561 return erf(arg);
562 }
563
565 template<typename Arg>
566 static CODI_INLINE Real gradient(Arg const& arg, Real const& result) {
567 CODI_UNUSED(result);
568 return 1.128379167095513 * exp(-(arg * arg)); // erf'(arg) = 2.0 / sqrt(pi) * exp(-arg^2)
569 }
570
572 static CODI_INLINE std::string getMathRep() {
573 return "erf";
574 }
575 };
576#define OPERATION_LOGIC OperationErf
577#define FUNCTION erf
578#include "unaryOverloads.tpp"
579
580#define OPERATION_LOGIC OperationErf
581#define FUNCTION erff
582#include "unaryOverloads.tpp"
583
584#define OPERATION_LOGIC OperationErf
585#define FUNCTION erfl
586#include "unaryOverloads.tpp"
587
589 template<typename T_Real>
590 struct OperationErfc : public UnaryOperation<T_Real> {
591 public:
592
593 using Real = CODI_DD(T_Real, double);
594
596 template<typename Arg>
597 static CODI_INLINE Real primal(Arg const& arg) {
598 return erfc(arg);
599 }
600
602 template<typename Arg>
603 static CODI_INLINE Real gradient(Arg const& arg, Real const& result) {
604 CODI_UNUSED(result);
605 return -1.128379167095513 * exp(-(arg * arg)); // erfc'(arg) = - 2.0 / sqrt(pi) * exp(-arg^2)
606 }
607
609 static CODI_INLINE std::string getMathRep() {
610 return "erfc";
611 }
612 };
613#define OPERATION_LOGIC OperationErfc
614#define FUNCTION erfc
615#include "unaryOverloads.tpp"
616
617#define OPERATION_LOGIC OperationErfc
618#define FUNCTION erfcf
619#include "unaryOverloads.tpp"
620
621#define OPERATION_LOGIC OperationErfc
622#define FUNCTION erfcl
623#include "unaryOverloads.tpp"
624
626 template<typename T_Real>
627 struct OperationExp : public UnaryOperation<T_Real> {
628 public:
629
630 using Real = CODI_DD(T_Real, double);
631
633 template<typename Arg>
634 static CODI_INLINE Real primal(Arg const& arg) {
635 return exp(arg);
636 }
637
639 template<typename Arg>
640 static CODI_INLINE Real gradient(Arg const& arg, Real const& result) {
641 CODI_UNUSED(arg);
642 return result;
643 }
644
646 static CODI_INLINE std::string getMathRep() {
647 return "exp";
648 }
649 };
650#define OPERATION_LOGIC OperationExp
651#define FUNCTION exp
652#include "unaryOverloads.tpp"
653
654#define OPERATION_LOGIC OperationExp
655#define FUNCTION expf
656#include "unaryOverloads.tpp"
657
658#define OPERATION_LOGIC OperationExp
659#define FUNCTION expl
660#include "unaryOverloads.tpp"
661
663 template<typename Real, typename Arg>
667
669 template<typename Real, typename Arg>
673
675 template<typename Real, typename Arg>
679
681 template<typename Real, typename Arg>
685
687 template<typename Real, typename Arg>
691
693 template<typename Real, typename Arg>
697
699 template<typename Real, typename Arg>
703
705 template<typename T_Real>
706 struct OperationLog : public UnaryOperation<T_Real> {
707 public:
708
709 using Real = CODI_DD(T_Real, double);
710
712 template<typename Arg>
713 static CODI_INLINE Real primal(Arg const& arg) {
714 return log(arg);
715 }
716
718 template<typename Arg>
719 static CODI_INLINE Real gradient(Arg const& arg, Real const& result) {
720 CODI_UNUSED(result);
722 if (0.0 > RealTraits::getPassiveValue(arg)) {
723 CODI_EXCEPTION("Logarithm of negative value or zero.(Value: %0.15e)", RealTraits::getPassiveValue(arg));
724 }
725 }
726 return 1.0 / arg;
727 }
728
730 static CODI_INLINE std::string getMathRep() {
731 return "log";
732 }
733 };
734#define OPERATION_LOGIC OperationLog
735#define FUNCTION log
736#include "unaryOverloads.tpp"
737
738#define OPERATION_LOGIC OperationLog
739#define FUNCTION logf
740#include "unaryOverloads.tpp"
741
742#define OPERATION_LOGIC OperationLog
743#define FUNCTION logl
744#include "unaryOverloads.tpp"
745
747 template<typename T_Real>
748 struct OperationLog10 : public UnaryOperation<T_Real> {
749 public:
750
751 using Real = CODI_DD(T_Real, double);
752
754 template<typename Arg>
755 static CODI_INLINE Real primal(Arg const& arg) {
756 return log10(arg);
757 }
758
760 template<typename Arg>
761 static CODI_INLINE Real gradient(Arg const& arg, Real const& result) {
762 CODI_UNUSED(result);
764 if (0.0 > RealTraits::getPassiveValue(arg)) {
765 CODI_EXCEPTION("Logarithm of negative value or zero.(Value: %0.15e)", RealTraits::getPassiveValue(arg));
766 }
767 }
768 return 0.434294481903252 / arg;
769 }
770
772 static CODI_INLINE std::string getMathRep() {
773 return "log10";
774 }
775 };
776#define OPERATION_LOGIC OperationLog10
777#define FUNCTION log10
778#include "unaryOverloads.tpp"
779
780#define OPERATION_LOGIC OperationLog10
781#define FUNCTION log10f
782#include "unaryOverloads.tpp"
783
784#define OPERATION_LOGIC OperationLog10
785#define FUNCTION log10l
786#include "unaryOverloads.tpp"
787
789 template<typename T_Real>
790 struct OperationLog1p : public UnaryOperation<T_Real> {
791 public:
792
793 using Real = CODI_DD(T_Real, double);
794
796 template<typename Arg>
797 static CODI_INLINE Real primal(Arg const& arg) {
798 return log1p(arg);
799 }
800
802 template<typename Arg>
803 static CODI_INLINE Real gradient(Arg const& arg, Real const& result) {
804 CODI_UNUSED(result);
806 if (0.0 > RealTraits::getPassiveValue(arg)) {
807 CODI_EXCEPTION("Logarithm of negative value or zero.(Value: %0.15e)", RealTraits::getPassiveValue(arg));
808 }
809 }
810 return 1.0 / (arg + 1.0);
811 }
812
814 static CODI_INLINE std::string getMathRep() {
815 return "log1p";
816 }
817 };
818#define OPERATION_LOGIC OperationLog1p
819#define FUNCTION log1p
820#include "unaryOverloads.tpp"
821
822#define OPERATION_LOGIC OperationLog1p
823#define FUNCTION log1pf
824#include "unaryOverloads.tpp"
825
826#define OPERATION_LOGIC OperationLog1p
827#define FUNCTION log1pl
828#include "unaryOverloads.tpp"
829
831 template<typename T_Real>
832 struct OperationLog2 : public UnaryOperation<T_Real> {
833 public:
834
835 using Real = CODI_DD(T_Real, double);
836
838 template<typename Arg>
839 static CODI_INLINE Real primal(Arg const& arg) {
840 return log2(arg);
841 }
842
844 template<typename Arg>
845 static CODI_INLINE Real gradient(Arg const& arg, Real const& result) {
846 CODI_UNUSED(result);
848 if (0.0 > RealTraits::getPassiveValue(arg)) {
849 CODI_EXCEPTION("Logarithm of negative value or zero.(Value: %0.15e)", RealTraits::getPassiveValue(arg));
850 }
851 }
852 return 1.442695040888963 / arg;
853 }
854
856 static CODI_INLINE std::string getMathRep() {
857 return "log2";
858 }
859 };
860#define OPERATION_LOGIC OperationLog2
861#define FUNCTION log2
862#include "unaryOverloads.tpp"
863
864#define OPERATION_LOGIC OperationLog2
865#define FUNCTION log2f
866#include "unaryOverloads.tpp"
867
868#define OPERATION_LOGIC OperationLog2
869#define FUNCTION log2l
870#include "unaryOverloads.tpp"
871
873 template<typename Real, typename Arg>
877
879 template<typename Real, typename Arg>
883
885 template<typename Real, typename Arg>
889
891 template<typename T_Real>
892 struct OperationSin : public UnaryOperation<T_Real> {
893 public:
894
895 using Real = CODI_DD(T_Real, double);
896
898 template<typename Arg>
899 static CODI_INLINE Real primal(Arg const& arg) {
900 return sin(arg);
901 }
902
904 template<typename Arg>
905 static CODI_INLINE Real gradient(Arg const& arg, Real const& result) {
906 CODI_UNUSED(result);
907 return cos(arg);
908 }
909
911 static CODI_INLINE std::string getMathRep() {
912 return "sin";
913 }
914 };
915#define OPERATION_LOGIC OperationSin
916#define FUNCTION sin
917#include "unaryOverloads.tpp"
918
919#define OPERATION_LOGIC OperationSin
920#define FUNCTION sinf
921#include "unaryOverloads.tpp"
922
923#define OPERATION_LOGIC OperationSin
924#define FUNCTION sinl
925#include "unaryOverloads.tpp"
926
928 template<typename T_Real>
929 struct OperationSinh : public UnaryOperation<T_Real> {
930 public:
931
932 using Real = CODI_DD(T_Real, double);
933
935 template<typename Arg>
936 static CODI_INLINE Real primal(Arg const& arg) {
937 return sinh(arg);
938 }
939
941 template<typename Arg>
942 static CODI_INLINE Real gradient(Arg const& arg, Real const& result) {
943 CODI_UNUSED(result);
944 return cosh(arg);
945 }
946
948 static CODI_INLINE std::string getMathRep() {
949 return "sinh";
950 }
951 };
952#define OPERATION_LOGIC OperationSinh
953#define FUNCTION sinh
954#include "unaryOverloads.tpp"
955
956#define OPERATION_LOGIC OperationSinh
957#define FUNCTION sinhf
958#include "unaryOverloads.tpp"
959
960#define OPERATION_LOGIC OperationSinh
961#define FUNCTION sinhl
962#include "unaryOverloads.tpp"
963
965 template<typename T_Real>
966 struct OperationSqrt : public UnaryOperation<T_Real> {
967 public:
968
969 using Real = CODI_DD(T_Real, double);
970
972 template<typename Arg>
973 static CODI_INLINE Real primal(Arg const& arg) {
974 return sqrt(arg);
975 }
976
978 template<typename Arg>
979 static CODI_INLINE Real gradient(Arg const& arg, Real const& result) {
981 if (0.0 > RealTraits::getPassiveValue(arg)) {
982 CODI_EXCEPTION("Sqrt of negative value or zero.(Value: %0.15e)", RealTraits::getPassiveValue(arg));
983 }
984 }
985 if (result != 0.0) {
986 return 0.5 / result;
987 } else {
988 return (Real)0.0;
989 }
990 }
991
993 static CODI_INLINE std::string getMathRep() {
994 return "sqrt";
995 }
996 };
997#define OPERATION_LOGIC OperationSqrt
998#define FUNCTION sqrt
999#include "unaryOverloads.tpp"
1000
1001#define OPERATION_LOGIC OperationSqrt
1002#define FUNCTION sqrtf
1003#include "unaryOverloads.tpp"
1004
1005#define OPERATION_LOGIC OperationSqrt
1006#define FUNCTION sqrtl
1007#include "unaryOverloads.tpp"
1008
1010 template<typename T_Real>
1011 struct OperationTan : public UnaryOperation<T_Real> {
1012 public:
1013
1014 using Real = CODI_DD(T_Real, double);
1015
1017 template<typename Arg>
1018 static CODI_INLINE Real primal(Arg const& arg) {
1019 return tan(arg);
1020 }
1021
1023 template<typename Arg>
1024 static CODI_INLINE Real gradient(Arg const& arg, Real const& result) {
1025 CODI_UNUSED(result);
1027 if (0.0 == cos(RealTraits::getPassiveValue(arg))) {
1028 CODI_EXCEPTION("Tan evaluated at (0.5 + i) * PI.(Value: %0.15e)", RealTraits::getPassiveValue(arg));
1029 }
1030 }
1031 Real tmp = 1.0 / cos(arg);
1032 return tmp * tmp;
1033 }
1034
1036 static CODI_INLINE std::string getMathRep() {
1037 return "tan";
1038 }
1039 };
1040#define OPERATION_LOGIC OperationTan
1041#define FUNCTION tan
1042#include "unaryOverloads.tpp"
1043
1044#define OPERATION_LOGIC OperationTan
1045#define FUNCTION tanf
1046#include "unaryOverloads.tpp"
1047
1048#define OPERATION_LOGIC OperationTan
1049#define FUNCTION tanl
1050#include "unaryOverloads.tpp"
1051
1053 template<typename T_Real>
1054 struct OperationTanh : public UnaryOperation<T_Real> {
1055 public:
1056
1057 using Real = CODI_DD(T_Real, double);
1058
1060 template<typename Arg>
1061 static CODI_INLINE Real primal(Arg const& arg) {
1062 return tanh(arg);
1063 }
1064
1066 template<typename Arg>
1067 static CODI_INLINE Real gradient(Arg const& arg, Real const& result) {
1068 CODI_UNUSED(arg);
1069 return 1.0 - result * result;
1070 }
1071
1073 static CODI_INLINE std::string getMathRep() {
1074 return "tanh";
1075 }
1076 };
1077#define OPERATION_LOGIC OperationTanh
1078#define FUNCTION tanh
1079#include "unaryOverloads.tpp"
1080
1081#define OPERATION_LOGIC OperationTanh
1082#define FUNCTION tanhf
1083#include "unaryOverloads.tpp"
1084
1085#define OPERATION_LOGIC OperationTanh
1086#define FUNCTION tanhl
1087#include "unaryOverloads.tpp"
1088
1090 template<typename T_Real>
1091 struct OperationTgamma : public UnaryOperation<T_Real> {
1092 public:
1093
1094 using Real = CODI_DD(T_Real, double);
1095
1097 template<typename Arg>
1098 static CODI_INLINE Real primal(Arg const& arg) {
1099 return tgamma(arg);
1100 }
1101
1103 template<typename Arg>
1104 static CODI_INLINE Real gradient(Arg const& arg, Real const& result) {
1105 if (arg <= 0.0) {
1106 std::cout << "Derivative for gamma function only for positive arguments at the moment" << std::endl;
1107 std::exit(1);
1108 }
1109
1110 // Implementation of the digamma function is taken from John Burkardt,
1111 // http://people.sc.fsu.edu/~jburkardt/cpp_src/asa103/asa103.cpp
1112 //
1113 // Definition of Gamma(arg): https://en.wikipedia.org/wiki/Gamma_function
1114 // Definition of DiGamma(arg): https://en.wikipedia.org/wiki/Digamma_function
1115 // Differentation is Gamma'(arg) = Gamma(arg) * DiGamma(arg)
1116
1117 Real diGamma = 0.0;
1118 if (arg <= 0.000001) { // Special case for small numbers.
1119 const Real eulerMascheroni = 0.57721566490153286060;
1120 diGamma = -eulerMascheroni - 1.0 / arg + 1.6449340668482264365 * arg;
1121 } else {
1122 // Shift DiGamma(arg) = DiGamma(arg + 1) - 1/arg
1123 // We require arg large such that the approximation below is more accurate.
1124 Real shiftBound = 8.5;
1125
1126 Real shiftedValue = arg;
1127 while (shiftedValue < shiftBound) {
1128 diGamma -= 1.0 / shiftedValue;
1129 shiftedValue += 1.0;
1130 }
1131
1132 // Now compute the approximation via an asymptotic series.
1133 Real r = 1.0 / shiftedValue;
1134 diGamma += log(shiftedValue) - 0.5 * r;
1135
1136 Real rSqr = r * r;
1137 diGamma -= rSqr * (1.0 / 12.0 -
1138 rSqr * (1.0 / 120.0 - rSqr * (1.0 / 252.0 - rSqr * (1.0 / 240.0 - rSqr * (1.0 / 132.0)))));
1139 }
1140
1141 return diGamma * result;
1142 }
1143
1145 static CODI_INLINE std::string getMathRep() {
1146 return "tgamma";
1147 }
1148 };
1149#define OPERATION_LOGIC OperationTgamma
1150#define FUNCTION tgamma
1151#include "unaryOverloads.tpp"
1152
1153#define OPERATION_LOGIC OperationTgamma
1154#define FUNCTION tgammaf
1155#include "unaryOverloads.tpp"
1156
1157#define OPERATION_LOGIC OperationTgamma
1158#define FUNCTION tgammal
1159#include "unaryOverloads.tpp"
1160
1162 /*******************************************************************************/
1165
1167 template<typename Real, typename Arg>
1169 using std::to_string;
1170
1172 }
1173
1175}
1176
1177namespace std {
1178
1179 using codi::abs;
1180 using codi::acos;
1181 using codi::acosf;
1182 using codi::acosh;
1183 using codi::acoshf;
1184 using codi::acoshl;
1185 using codi::acosl;
1186 using codi::asin;
1187 using codi::asinf;
1188 using codi::asinh;
1189 using codi::asinhf;
1190 using codi::asinhl;
1191 using codi::asinl;
1192 using codi::atan;
1193 using codi::atanf;
1194 using codi::atanh;
1195 using codi::atanhf;
1196 using codi::atanhl;
1197 using codi::atanl;
1198 using codi::cbrt;
1199 using codi::cbrtf;
1200 using codi::cbrtl;
1201 using codi::ceil;
1202 using codi::ceilf;
1203 using codi::ceill;
1204 using codi::cos;
1205 using codi::cosf;
1206 using codi::cosh;
1207 using codi::coshf;
1208 using codi::coshl;
1209 using codi::cosl;
1210 using codi::erf;
1211 using codi::erfc;
1212 using codi::erfcf;
1213 using codi::erfcl;
1214 using codi::erff;
1215 using codi::erfl;
1216 using codi::exp;
1217 using codi::expf;
1218 using codi::expl;
1219 using codi::fabs;
1220 using codi::fabsf;
1221 using codi::fabsl;
1222 using codi::floor;
1223 using codi::floorf;
1224 using codi::floorl;
1225 using codi::isfinite;
1226 using codi::isinf;
1227 using codi::isnan;
1228 using codi::isnormal;
1229 using codi::log;
1230 using codi::log10;
1231 using codi::log10f;
1232 using codi::log10l;
1233 using codi::log1p;
1234 using codi::log1pf;
1235 using codi::log1pl;
1236 using codi::log2;
1237 using codi::log2f;
1238 using codi::log2l;
1239 using codi::logf;
1240 using codi::logl;
1241 using codi::round;
1242 using codi::roundf;
1243 using codi::roundl;
1244 using codi::sin;
1245 using codi::sinf;
1246 using codi::sinh;
1247 using codi::sinhf;
1248 using codi::sinhl;
1249 using codi::sinl;
1250 using codi::sqrt;
1251 using codi::sqrtf;
1252 using codi::sqrtl;
1253 using codi::tan;
1254 using codi::tanf;
1255 using codi::tanh;
1256 using codi::tanhf;
1257 using codi::tanhl;
1258 using codi::tanl;
1259 using codi::tgamma;
1260 using codi::tgammaf;
1261 using codi::tgammal;
1262
1263 using codi::to_string;
1264}
#define CODI_INLINE
See codi::Config::ForcedInlines.
Definition config.h:457
#define CODI_DD(Type, Default)
Abbreviation for CODI_DECLARE_DEFAULT.
Definition macros.hpp:94
bool constexpr CheckExpressionArguments
Check for invalid arguments to expressions like division by zero.
Definition config.h:146
PassiveReal< Type > const & getPassiveValue(Type const &v)
Get the basic primal value of the type.
Definition realTraits.hpp:127
typename TraitsImplementation< Type >::PassiveReal PassiveReal
The original computation type, that was used in the application.
Definition realTraits.hpp:117
CoDiPack - Code Differentiation Package.
Definition codi.hpp:91
RealTraits::PassiveReal< Real > round(ExpressionInterface< Real, Arg > const &arg)
Function overload for round.
Definition unaryOperators.hpp:874
RealTraits::PassiveReal< Real > ceil(ExpressionInterface< Real, Arg > const &arg)
Function overload for ceil.
Definition unaryOperators.hpp:461
void CODI_UNUSED(Args const &...)
Disable unused warnings for an arbitrary number of arguments.
Definition macros.hpp:46
RealTraits::PassiveReal< Real > roundf(ExpressionInterface< Real, Arg > const &arg)
Function overload for roundf.
Definition unaryOperators.hpp:880
bool isfinite(ExpressionInterface< Real, Arg > const &arg)
Function overload for isfinite.
Definition unaryOperators.hpp:682
RealTraits::PassiveReal< Real > floorf(ExpressionInterface< Real, Arg > const &arg)
Function overload for floorf.
Definition unaryOperators.hpp:670
bool isnan(ExpressionInterface< Real, Arg > const &arg)
Function overload for isnan.
Definition unaryOperators.hpp:694
std::string to_string(ExpressionInterface< Real, Arg > const &arg)
Function overload for to_string.
Definition unaryOperators.hpp:1168
RealTraits::PassiveReal< Real > roundl(ExpressionInterface< Real, Arg > const &arg)
Function overload for roundl.
Definition unaryOperators.hpp:886
RealTraits::PassiveReal< Real > floorl(ExpressionInterface< Real, Arg > const &arg)
Function overload for floorl.
Definition unaryOperators.hpp:676
ExpressionInterface< Real, Arg > const & operator+(ExpressionInterface< Real, Arg > const &arg)
Function overload for operator +.
Definition unaryOperators.hpp:84
RealTraits::PassiveReal< Real > ceilf(ExpressionInterface< Real, Arg > const &arg)
Function overload for ceilf.
Definition unaryOperators.hpp:467
RealTraits::PassiveReal< Real > floor(ExpressionInterface< Real, Arg > const &arg)
Function overload for floor.
Definition unaryOperators.hpp:664
bool isinf(ExpressionInterface< Real, Arg > const &arg)
Function overload for isinf.
Definition unaryOperators.hpp:688
bool isnormal(ExpressionInterface< Real, Arg > const &arg)
Function overload for isnormal.
Definition unaryOperators.hpp:700
RealTraits::PassiveReal< Real > ceill(ExpressionInterface< Real, Arg > const &arg)
Function overload for ceill.
Definition unaryOperators.hpp:473
Base class for all CoDiPack expressions.
Definition expressionInterface.hpp:59
Impl const & cast() const
Cast to the implementation.
Definition expressionInterface.hpp:75
UnaryOperation implementation for abs.
Definition unaryOperators.hpp:127
static Real primal(Arg const &arg)
Definition unaryOperators.hpp:134
static std::string getMathRep()
Get the math symbol of the unary operation. E.g. sin() for functions.
Definition unaryOperators.hpp:152
T_Real Real
See BinaryOperation.
Definition unaryOperators.hpp:130
static RealTraits::PassiveReal< Real > gradient(Arg const &arg, Real const &result)
Definition unaryOperators.hpp:140
UnaryOperation implementation for acos.
Definition unaryOperators.hpp:174
static Real gradient(Arg const &arg, Real const &result)
Definition unaryOperators.hpp:187
static std::string getMathRep()
Get the math symbol of the unary operation. E.g. sin() for functions.
Definition unaryOperators.hpp:198
static Real primal(Arg const &arg)
Definition unaryOperators.hpp:181
T_Real Real
See BinaryOperation.
Definition unaryOperators.hpp:177
UnaryOperation implementation for acosh.
Definition unaryOperators.hpp:216
static Real gradient(Arg const &arg, Real const &result)
Definition unaryOperators.hpp:229
T_Real Real
See BinaryOperation.
Definition unaryOperators.hpp:219
static std::string getMathRep()
Get the math symbol of the unary operation. E.g. sin() for functions.
Definition unaryOperators.hpp:240
static Real primal(Arg const &arg)
Definition unaryOperators.hpp:223
UnaryOperation implementation for asin.
Definition unaryOperators.hpp:258
static Real primal(Arg const &arg)
Definition unaryOperators.hpp:265
static std::string getMathRep()
Get the math symbol of the unary operation. E.g. sin() for functions.
Definition unaryOperators.hpp:282
static Real gradient(Arg const &arg, Real const &result)
Definition unaryOperators.hpp:271
T_Real Real
See BinaryOperation.
Definition unaryOperators.hpp:261
UnaryOperation implementation for asinh.
Definition unaryOperators.hpp:300
static Real gradient(Arg const &arg, Real const &result)
Definition unaryOperators.hpp:313
static std::string getMathRep()
Get the math symbol of the unary operation. E.g. sin() for functions.
Definition unaryOperators.hpp:319
T_Real Real
See BinaryOperation.
Definition unaryOperators.hpp:303
static Real primal(Arg const &arg)
Definition unaryOperators.hpp:307
UnaryOperation implementation for atan.
Definition unaryOperators.hpp:337
static Real primal(Arg const &arg)
Definition unaryOperators.hpp:344
T_Real Real
See BinaryOperation.
Definition unaryOperators.hpp:340
static Real gradient(Arg const &arg, Real const &result)
Definition unaryOperators.hpp:350
static std::string getMathRep()
Get the math symbol of the unary operation. E.g. sin() for functions.
Definition unaryOperators.hpp:356
UnaryOperation implementation for atanh.
Definition unaryOperators.hpp:374
static Real gradient(Arg const &arg, Real const &result)
Definition unaryOperators.hpp:387
T_Real Real
See BinaryOperation.
Definition unaryOperators.hpp:377
static Real primal(Arg const &arg)
Definition unaryOperators.hpp:381
static std::string getMathRep()
Get the math symbol of the unary operation. E.g. sin() for functions.
Definition unaryOperators.hpp:398
UnaryOperation implementation for cbrt.
Definition unaryOperators.hpp:416
static std::string getMathRep()
Get the math symbol of the unary operation. E.g. sin() for functions.
Definition unaryOperators.hpp:443
T_Real Real
See BinaryOperation.
Definition unaryOperators.hpp:419
static Real gradient(Arg const &arg, Real const &result)
Definition unaryOperators.hpp:429
static Real primal(Arg const &arg)
Definition unaryOperators.hpp:423
UnaryOperation implementation for cos.
Definition unaryOperators.hpp:479
static std::string getMathRep()
Get the math symbol of the unary operation. E.g. sin() for functions.
Definition unaryOperators.hpp:498
T_Real Real
See BinaryOperation.
Definition unaryOperators.hpp:482
static Real primal(Arg const &arg)
Definition unaryOperators.hpp:486
static Real gradient(Arg const &arg, Real const &result)
Definition unaryOperators.hpp:492
UnaryOperation implementation for cosh.
Definition unaryOperators.hpp:516
T_Real Real
See BinaryOperation.
Definition unaryOperators.hpp:519
static std::string getMathRep()
Get the math symbol of the unary operation. E.g. sin() for functions.
Definition unaryOperators.hpp:535
static Real gradient(Arg const &arg, Real const &result)
Definition unaryOperators.hpp:529
static Real primal(Arg const &arg)
Definition unaryOperators.hpp:523
UnaryOperation implementation for erf.
Definition unaryOperators.hpp:553
static Real primal(Arg const &arg)
Definition unaryOperators.hpp:560
T_Real Real
See BinaryOperation.
Definition unaryOperators.hpp:556
static Real gradient(Arg const &arg, Real const &result)
Definition unaryOperators.hpp:566
static std::string getMathRep()
Get the math symbol of the unary operation. E.g. sin() for functions.
Definition unaryOperators.hpp:572
UnaryOperation implementation for erfc.
Definition unaryOperators.hpp:590
static Real primal(Arg const &arg)
Definition unaryOperators.hpp:597
static std::string getMathRep()
Get the math symbol of the unary operation. E.g. sin() for functions.
Definition unaryOperators.hpp:609
static Real gradient(Arg const &arg, Real const &result)
Definition unaryOperators.hpp:603
T_Real Real
See BinaryOperation.
Definition unaryOperators.hpp:593
UnaryOperation implementation for exp.
Definition unaryOperators.hpp:627
static Real primal(Arg const &arg)
Definition unaryOperators.hpp:634
static std::string getMathRep()
Get the math symbol of the unary operation. E.g. sin() for functions.
Definition unaryOperators.hpp:646
T_Real Real
See BinaryOperation.
Definition unaryOperators.hpp:630
static Real gradient(Arg const &arg, Real const &result)
Definition unaryOperators.hpp:640
UnaryOperation implementation for log10.
Definition unaryOperators.hpp:748
T_Real Real
See BinaryOperation.
Definition unaryOperators.hpp:751
static Real primal(Arg const &arg)
Definition unaryOperators.hpp:755
static std::string getMathRep()
Get the math symbol of the unary operation. E.g. sin() for functions.
Definition unaryOperators.hpp:772
static Real gradient(Arg const &arg, Real const &result)
Definition unaryOperators.hpp:761
UnaryOperation implementation for log1p.
Definition unaryOperators.hpp:790
static std::string getMathRep()
Get the math symbol of the unary operation. E.g. sin() for functions.
Definition unaryOperators.hpp:814
static Real gradient(Arg const &arg, Real const &result)
Definition unaryOperators.hpp:803
T_Real Real
See BinaryOperation.
Definition unaryOperators.hpp:793
static Real primal(Arg const &arg)
Definition unaryOperators.hpp:797
UnaryOperation implementation for log2.
Definition unaryOperators.hpp:832
static std::string getMathRep()
Get the math symbol of the unary operation. E.g. sin() for functions.
Definition unaryOperators.hpp:856
static Real primal(Arg const &arg)
Definition unaryOperators.hpp:839
static Real gradient(Arg const &arg, Real const &result)
Definition unaryOperators.hpp:845
T_Real Real
See BinaryOperation.
Definition unaryOperators.hpp:835
UnaryOperation implementation for log.
Definition unaryOperators.hpp:706
static Real primal(Arg const &arg)
Definition unaryOperators.hpp:713
static Real gradient(Arg const &arg, Real const &result)
Definition unaryOperators.hpp:719
T_Real Real
See BinaryOperation.
Definition unaryOperators.hpp:709
static std::string getMathRep()
Get the math symbol of the unary operation. E.g. sin() for functions.
Definition unaryOperators.hpp:730
UnaryOperation implementation for sin.
Definition unaryOperators.hpp:892
static Real primal(Arg const &arg)
Definition unaryOperators.hpp:899
static Real gradient(Arg const &arg, Real const &result)
Definition unaryOperators.hpp:905
static std::string getMathRep()
Get the math symbol of the unary operation. E.g. sin() for functions.
Definition unaryOperators.hpp:911
T_Real Real
See BinaryOperation.
Definition unaryOperators.hpp:895
UnaryOperation implementation for sinh.
Definition unaryOperators.hpp:929
T_Real Real
See BinaryOperation.
Definition unaryOperators.hpp:932
static std::string getMathRep()
Get the math symbol of the unary operation. E.g. sin() for functions.
Definition unaryOperators.hpp:948
static Real primal(Arg const &arg)
Definition unaryOperators.hpp:936
static Real gradient(Arg const &arg, Real const &result)
Definition unaryOperators.hpp:942
UnaryOperation implementation for sqrt.
Definition unaryOperators.hpp:966
static Real gradient(Arg const &arg, Real const &result)
Definition unaryOperators.hpp:979
static std::string getMathRep()
Get the math symbol of the unary operation. E.g. sin() for functions.
Definition unaryOperators.hpp:993
static Real primal(Arg const &arg)
Definition unaryOperators.hpp:973
T_Real Real
See BinaryOperation.
Definition unaryOperators.hpp:969
UnaryOperation implementation for tan.
Definition unaryOperators.hpp:1011
static Real primal(Arg const &arg)
Definition unaryOperators.hpp:1018
T_Real Real
See BinaryOperation.
Definition unaryOperators.hpp:1014
static Real gradient(Arg const &arg, Real const &result)
Definition unaryOperators.hpp:1024
static std::string getMathRep()
Get the math symbol of the unary operation. E.g. sin() for functions.
Definition unaryOperators.hpp:1036
UnaryOperation implementation for tanh.
Definition unaryOperators.hpp:1054
static Real gradient(Arg const &arg, Real const &result)
Definition unaryOperators.hpp:1067
static std::string getMathRep()
Get the math symbol of the unary operation. E.g. sin() for functions.
Definition unaryOperators.hpp:1073
static Real primal(Arg const &arg)
Definition unaryOperators.hpp:1061
T_Real Real
See BinaryOperation.
Definition unaryOperators.hpp:1057
UnaryOperation implementation for tgamma.
Definition unaryOperators.hpp:1091
static Real gradient(Arg const &arg, Real const &result)
Definition unaryOperators.hpp:1104
static Real primal(Arg const &arg)
Definition unaryOperators.hpp:1098
T_Real Real
See BinaryOperation.
Definition unaryOperators.hpp:1094
static std::string getMathRep()
Get the math symbol of the unary operation. E.g. sin() for functions.
Definition unaryOperators.hpp:1145
UnaryOperation implementation for operator -.
Definition unaryOperators.hpp:54
static RealTraits::PassiveReal< Real > gradient(Arg const &arg, Real const &result)
Definition unaryOperators.hpp:67
static Real primal(Arg const &arg)
Definition unaryOperators.hpp:61
T_Real Real
See BinaryOperation.
Definition unaryOperators.hpp:57
static std::string getMathRep()
Get the math symbol of the unary operation. E.g. sin() for functions.
Definition unaryOperators.hpp:74
Interface for implementing the logic for a UnaryExpression.
Definition unaryExpression.hpp:55