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 
48  const real pow_n_1_x = pow(x.Re(), n - 1);
49  return dual(pow_n_1_x * x.Re(), pow_n_1_x * n * x.Dual());
50  }
51 
52 
54  inline dual sqrt(dual x) {
55 
56  const real sqrt_x = sqrt(x.Re());
57 
58  if(sqrt_x == 0) {
59  TH_MATH_ERROR("sqrt(dual)", sqrt_x, DIV_BY_ZERO);
60  return dual(nan(), nan());
61  }
62 
63  return dual(sqrt_x, 0.5 / sqrt_x * x.Dual());
64  }
65 
66 
68  inline dual sin(dual x) {
69  return dual(sin(x.Re()), cos(x.Re()) * x.Dual());
70  }
71 
72 
74  inline dual cos(dual x) {
75  return dual(cos(x.Re()), -sin(x.Re()) * x.Dual());
76  }
77 
78 
80  inline dual tan(dual x) {
81 
82  const real cos_x = cos(x.Re());
83 
84  if(cos_x == 0) {
85  TH_MATH_ERROR("tan(dual)", cos_x, DIV_BY_ZERO);
86  return dual(nan(), nan());
87  }
88 
89  return dual(tan(x.Re()), x.Dual() / square(cos_x));
90  }
91 
92 
94  inline dual cot(dual x) {
95 
96  const real sin_x = sin(x.Re());
97 
98  if(sin_x == 0) {
99  TH_MATH_ERROR("cot(dual)", sin_x, DIV_BY_ZERO);
100  return dual(nan(), nan());
101  }
102 
103  return dual(cot(x.Re()), -x.Dual() / square(sin_x));
104  }
105 
106 
108  inline dual exp(dual x) {
109 
110  const real exp_x = exp(x.Re());
111  return dual(exp_x, x.Dual() * exp_x);
112  }
113 
114 
116  inline dual ln(dual x) {
117 
118  if(x.Re() <= 0) {
119  TH_MATH_ERROR("ln(dual)", x.Re(), OUT_OF_DOMAIN);
120  return dual(nan(), nan());
121  }
122 
123  return dual(ln(x.Re()), x.Dual() / x.Re());
124  }
125 
126 
128  inline dual log2(dual x) {
129 
130  if(x.Re() <= 0) {
131  TH_MATH_ERROR("log2(dual)", x.Re(), OUT_OF_DOMAIN);
132  return dual(nan(), nan());
133  }
134 
135  return dual(log2(x.Re()), x.Dual() * LOG2E / x.Re());
136  }
137 
138 
140  inline dual log10(dual x) {
141 
142  if(x.Re() <= 0) {
143  TH_MATH_ERROR("log10(dual)", x.Re(), OUT_OF_DOMAIN);
144  return dual(nan(), nan());
145  }
146 
147  return dual(log10(x.Re()), x.Dual() * LOG10E / x.Re());
148  }
149 
150 
152  inline dual abs(dual x) {
153  return dual(abs(x.Re()), x.Dual() * sgn(x.Re()));
154  }
155 
156 
158  inline dual asin(dual x) {
159 
160  if(x.Re() >= 1) {
161  TH_MATH_ERROR("asin(dual)", x.Re(), OUT_OF_DOMAIN);
162  return dual(nan(), nan());
163  }
164 
165  return dual(asin(x.Re()), x.Dual() / sqrt(1 - square(x.Re())));
166  }
167 
168 
170  inline dual acos(dual x) {
171 
172  if(x.Re() >= 1) {
173  TH_MATH_ERROR("acos(dual)", x.Re(), OUT_OF_DOMAIN);
174  return dual(nan(), nan());
175  }
176 
177  return dual(
178  acos(x.Re()),
179  -x.Dual() / sqrt(1 - square(x.Re()))
180  );
181  }
182 
183 
185  inline dual atan(dual x) {
186  return dual(
187  atan(x.Re()),
188  x.Dual() / (1 + square(x.Re()))
189  );
190  }
191 
192 
194  inline dual sinh(dual x) {
195 
196  const real exp_x = exp(x.Re());
197  return dual(
198  (exp_x - 1.0 / exp_x) / 2.0,
199  x.Dual() * (exp_x + 1.0 / exp_x) / 2.0
200  );
201  }
202 
203 
205  inline dual cosh(dual x) {
206 
207  const real exp_x = exp(x.Re());
208  return dual(
209  (exp_x + 1.0 / exp_x) / 2.0,
210  x.Dual() * (exp_x - 1.0 / exp_x) / 2.0
211  );
212  }
213 
214 
216  inline dual tanh(dual x) {
217 
218  const real exp_x = exp(x.Re());
219  return dual(
220  (exp_x - 1.0 / exp_x) / (exp_x + 1.0 / exp_x),
221  x.Dual() / square(exp_x + 1.0 / exp_x)
222  );
223  }
224 
225 }
226 
227 
228 #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:198
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:205
dual2 ln(dual2 x)
Compute the natural logarithm of a second order dual number.
Definition: dual2_functions.h:151
dual sinh(dual x)
Compute the hyperbolic sine of a dual number.
Definition: dual_functions.h:194
dual2 abs(dual2 x)
Compute the absolute value of a second order dual number.
Definition: dual2_functions.h:198
dual2 log2(dual2 x)
Compute the natural logarithm of a second order dual number.
Definition: dual2_functions.h:166
dual2 asin(dual2 x)
Compute the arcsine of a second order dual number.
Definition: dual2_functions.h:204
dual2 exp(dual2 x)
Compute the exponential of a second order dual number.
Definition: dual2_functions.h:138
dual2 log10(dual2 x)
Compute the natural logarithm of a second order dual number.
Definition: dual2_functions.h:182
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:246
constexpr real LOG2E
The binary logarithm of e.
Definition: constants.h:240
dual2 cos(dual2 x)
Compute the cosine of a second order dual number.
Definition: dual2_functions.h:86
dual2 cot(dual2 x)
Compute the cotangent of a second order dual number.
Definition: dual2_functions.h:119
dual2 tan(dual2 x)
Compute the tangent of a second order dual number.
Definition: dual2_functions.h:100
dual2 acos(dual2 x)
Compute the arcosine of a second order dual number.
Definition: dual2_functions.h:223
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:72
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:242
dual tanh(dual x)
Compute the hyperbolic tangent of a dual number.
Definition: dual_functions.h:216
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