Theoretica
A C++ numerical and automatic mathematical library
dual_functions.h
Go to the documentation of this file.
1 
15 
16 
17 #ifndef THEORETICA_DUAL_FUNCTIONS_H
18 #define THEORETICA_DUAL_FUNCTIONS_H
19 
20 #include "./dual.h"
21 #include "../core/real_analysis.h"
22 
23 
24 namespace theoretica {
25 
26 
28  inline dual square(dual x) {
29  return x * x;
30  }
31 
32 
34  inline dual cube(dual x) {
35  return x * x * x;
36  }
37 
38 
40  inline dual conjugate(dual x) {
41  return x.conjugate();
42  }
43 
44 
46  inline dual pow(dual x, int n) {
47  real pow_n_1_x = pow(x.Re(), n - 1);
48  return dual(pow_n_1_x * x.Re(), pow_n_1_x * n * x.Dual());
49  }
50 
51 
53  inline dual sqrt(dual x) {
54 
55  real sqrt_x = sqrt(x.Re());
56 
57  if(sqrt_x == 0) {
58  TH_MATH_ERROR("sqrt(dual)", sqrt_x, DIV_BY_ZERO);
59  return dual(nan(), nan());
60  }
61 
62  return dual(sqrt_x, 0.5 / sqrt_x * x.Dual());
63  }
64 
65 
67  inline dual sin(dual x) {
68  return dual(sin(x.Re()), cos(x.Re()) * x.Dual());
69  }
70 
71 
73  inline dual cos(dual x) {
74  return dual(cos(x.Re()), -sin(x.Re()) * x.Dual());
75  }
76 
77 
79  inline dual tan(dual x) {
80 
81  real cos_x = cos(x.Re());
82 
83  if(cos_x == 0) {
84  TH_MATH_ERROR("tan(dual)", cos_x, DIV_BY_ZERO);
85  return dual(nan(), nan());
86  }
87 
88  return dual(tan(x.Re()), x.Dual() / square(cos_x));
89  }
90 
91 
93  inline dual cot(dual x) {
94 
95  real sin_x = sin(x.Re());
96 
97  if(sin_x == 0) {
98  TH_MATH_ERROR("cot(dual)", sin_x, DIV_BY_ZERO);
99  return dual(nan(), nan());
100  }
101 
102  return dual(cot(x.Re()), -x.Dual() / square(sin_x));
103  }
104 
105 
107  inline dual exp(dual x) {
108  real exp_x = exp(x.Re());
109  return dual(exp_x, x.Dual() * exp_x);
110  }
111 
112 
114  inline dual ln(dual x) {
115 
116  if(x.Re() <= 0) {
117  TH_MATH_ERROR("ln(dual)", x.Re(), OUT_OF_DOMAIN);
118  return dual(nan(), nan());
119  }
120 
121  return dual(ln(x.Re()), x.Dual() / x.Re());
122  }
123 
124 
126  inline dual log2(dual x) {
127 
128  if(x.Re() <= 0) {
129  TH_MATH_ERROR("log2(dual)", x.Re(), OUT_OF_DOMAIN);
130  return dual(nan(), nan());
131  }
132 
133  return dual(log2(x.Re()), x.Dual() * LOG2E / x.Re());
134  }
135 
136 
138  inline dual log10(dual x) {
139 
140  if(x.Re() <= 0) {
141  TH_MATH_ERROR("log10(dual)", x.Re(), OUT_OF_DOMAIN);
142  return dual(nan(), nan());
143  }
144 
145  return dual(log10(x.Re()), x.Dual() * LOG10E / x.Re());
146  }
147 
148 
150  inline dual abs(dual x) {
151  return dual(abs(x.Re()), x.Dual() * sgn(x.Re()));
152  }
153 
154 
156  inline dual asin(dual x) {
157 
158  if(x.Re() >= 1) {
159  TH_MATH_ERROR("asin(dual)", x.Re(), OUT_OF_DOMAIN);
160  return dual(nan(), nan());
161  }
162 
163  return dual(asin(x.Re()), x.Dual() / sqrt(1 - square(x.Re())));
164  }
165 
166 
168  inline dual acos(dual x) {
169 
170  if(x.Re() >= 1) {
171  TH_MATH_ERROR("acos(dual)", x.Re(), OUT_OF_DOMAIN);
172  return dual(nan(), nan());
173  }
174 
175  return dual(acos(x.Re()), -x.Dual() / sqrt(1 - square(x.Re())));
176  }
177 
178 
180  inline dual atan(dual x) {
181  return dual(atan(x.Re()), x.Dual() / (1 + square(x.Re())));
182  }
183 
184 
186  inline dual sinh(dual x) {
187 
188  real exp_x = exp(x.Re());
189  return dual((exp_x - 1.0 / exp_x) / 2.0, x.Dual() * (exp_x + 1.0 / exp_x) / 2.0);
190  }
191 
192 
194  inline dual cosh(dual x) {
195 
196  real exp_x = exp(x.Re());
197  return dual((exp_x + 1.0 / exp_x) / 2.0, x.Dual() * (exp_x - 1.0 / exp_x) / 2.0);
198  }
199 
200 
202  inline dual tanh(dual x) {
203 
204  real exp_x = exp(x.Re());
205  return dual(
206  (exp_x - 1.0 / exp_x) / (exp_x + 1.0 / exp_x),
207  x.Dual() / square(exp_x + 1.0 / exp_x));
208  }
209 
210 }
211 
212 
213 #endif
Dual number class.
Definition: dual.h:28
dual conjugate() const
Get the dual conjugate.
Definition: dual.h:117
const real & Re() const
Return real part.
Definition: dual.h:75
const real & Dual() const
Return dual part.
Definition: dual.h:96
Dual number class.
#define TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION)
TH_MATH_ERROR is a macro which throws exceptions or modifies errno (depending on which compiling opti...
Definition: error.h:219
Main namespace of the library which contains all functions and objects.
Definition: algebra.h:27
double real
A real number, defined as a floating point type.
Definition: constants.h:188
dual2 sqrt(dual2 x)
Compute the square root of a second order dual number.
Definition: dual2_functions.h:54
dual cosh(dual x)
Compute the hyperbolic cosine of a dual number.
Definition: dual_functions.h:194
dual2 ln(dual2 x)
Compute the natural logarithm of a second order dual number.
Definition: dual2_functions.h:139
dual sinh(dual x)
Compute the hyperbolic sine of a dual number.
Definition: dual_functions.h:186
dual2 abs(dual2 x)
Compute the absolute value of a second order dual number.
Definition: dual2_functions.h:183
dual2 log2(dual2 x)
Compute the natural logarithm of a second order dual number.
Definition: dual2_functions.h:153
dual2 asin(dual2 x)
Compute the arcsine of a second order dual number.
Definition: dual2_functions.h:189
dual2 exp(dual2 x)
Compute the exponential of a second order dual number.
Definition: dual2_functions.h:130
dual2 log10(dual2 x)
Compute the natural logarithm of a second order dual number.
Definition: dual2_functions.h:168
dual2 conjugate(dual2 x)
Return the conjugate of a second order dual number.
Definition: dual2_functions.h:35
constexpr real LOG10E
The base-10 logarithm of e.
Definition: constants.h:236
constexpr real LOG2E
The binary logarithm of e.
Definition: constants.h:230
dual2 cos(dual2 x)
Compute the cosine of a second order dual number.
Definition: dual2_functions.h:82
dual2 cot(dual2 x)
Compute the cotangent of a second order dual number.
Definition: dual2_functions.h:112
dual2 tan(dual2 x)
Compute the tangent of a second order dual number.
Definition: dual2_functions.h:94
dual2 acos(dual2 x)
Compute the arcosine of a second order dual number.
Definition: dual2_functions.h:206
int sgn(real x)
Return the sign of x (1 if positive, -1 if negative, 0 if null)
Definition: real_analysis.h:259
dual2 sin(dual2 x)
Compute the sine of a second order dual number.
Definition: dual2_functions.h:70
dual2 square(dual2 x)
Return the square of a second order dual number.
Definition: dual2_functions.h:23
real nan()
Return a quiet NaN number in floating point representation.
Definition: error.h:54
dual2 atan(dual2 x)
Compute the arctangent of a second order dual number.
Definition: dual2_functions.h:223
dual tanh(dual x)
Compute the hyperbolic tangent of a dual number.
Definition: dual_functions.h:202
dual2 pow(dual2 x, int n)
Compute the n-th power of a second order dual number.
Definition: dual2_functions.h:41
dual2 cube(dual2 x)
Return the cube of a second order dual number.
Definition: dual2_functions.h:29