Theoretica
A C++ numerical and automatic mathematical library
dual2_functions.h
Go to the documentation of this file.
1 
6 
7 
8 #ifndef THEORETICA_DUAL2_FUNCTIONS_H
9 #define THEORETICA_DUAL2_FUNCTIONS_H
10 
11 #include "./dual2.h"
12 #include "../core/real_analysis.h"
13 
14 
15 namespace theoretica {
16 
17 
18  // Second derivative of the composite function:
19  // (f(g))'' = f''(g) * g'' + f'(g) * g''
20 
21 
23  inline dual2 square(dual2 x) {
24  return x * x;
25  }
26 
27 
29  inline dual2 cube(dual2 x) {
30  return x * x * x;
31  }
32 
33 
35  inline dual2 conjugate(dual2 x) {
36  return x.conjugate();
37  }
38 
39 
41  inline dual2 pow(dual2 x, int n) {
42 
43  const real pow_n_2_x = pow(x.Re(), n - 2);
44 
45  real f = pow_n_2_x * square(x.Re());
46  real df = pow_n_2_x * x.Re() * n * x.Dual1();
47  real d2f = (pow_n_2_x * n * (n - 1)) * square(x.Dual1()) + df * x.Dual2();
48 
49  return dual2(f, df, d2f);
50  }
51 
52 
54  inline dual2 sqrt(dual2 x) {
55 
56  const real sqrt_x = sqrt(x.Re());
57 
58  if(sqrt_x == 0) {
59  TH_MATH_ERROR("sqrt(dual2)", sqrt_x, DIV_BY_ZERO);
60  return dual2(nan(), nan());
61  }
62 
63  return dual2(
64  sqrt_x,
65  0.5 / sqrt_x * x.Dual1(),
66  -0.25 / x.Re() / sqrt_x * square(x.Dual1()) + 0.5 / sqrt_x * x.Dual2()
67  );
68  }
69 
70 
72  inline dual2 sin(dual2 x) {
73 
74  const real sin_x = sin(x.Re());
75  const real cos_x = cos(x.Re());
76 
77  return dual2(
78  sin_x,
79  cos_x * x.Dual1(),
80  -sin_x * square(x.Dual1()) + cos_x * x.Dual2()
81  );
82  }
83 
84 
86  inline dual2 cos(dual2 x) {
87 
88  const real sin_x = sin(x.Re());
89  const real cos_x = cos(x.Re());
90 
91  return dual2(
92  cos_x,
93  -sin_x * x.Dual1(),
94  -cos_x * square(x.Dual1()) - sin_x * x.Dual2()
95  );
96  }
97 
98 
100  inline dual2 tan(dual2 x) {
101 
102  const real sin_x = sin(x.Re());
103  const real cos_x = cos(x.Re());
104 
105  if(cos_x == 0) {
106  TH_MATH_ERROR("tan(dual2)", cos_x, DIV_BY_ZERO);
107  return dual2(nan(), nan());
108  }
109 
110  return dual2(
111  tan(x.Re()),
112  x.Dual1() / square(cos_x),
113  2.0 * sin_x / cube(cos_x) * square(x.Dual1()) + x.Dual2() / square(cos_x)
114  );
115  }
116 
117 
119  inline dual2 cot(dual2 x) {
120 
121  const real sin_x = sin(x.Re());
122  const real cos_x = cos(x.Re());
123 
124  if(sin_x == 0) {
125  TH_MATH_ERROR("cot(dual2)", sin_x, DIV_BY_ZERO);
126  return dual2(nan(), nan());
127  }
128 
129  return dual2(
130  cot(x.Re()),
131  -x.Dual1() / square(sin_x),
132  -2.0 * cos_x / cube(sin_x) * square(x.Dual1()) - x.Dual2() / square(sin_x)
133  );
134  }
135 
136 
138  inline dual2 exp(dual2 x) {
139 
140  const real exp_x = exp(x.Re());
141 
142  return dual2(
143  exp_x,
144  x.Dual1() * exp_x,
145  square(x.Dual1()) * exp_x + x.Dual2() * exp_x
146  );
147  }
148 
149 
151  inline dual2 ln(dual2 x) {
152 
153  if(x.Re() <= 0) {
154  TH_MATH_ERROR("ln(dual2)", x.Re(), OUT_OF_DOMAIN);
155  return dual2(nan(), nan());
156  }
157 
158  return dual2(ln(x.Re()),
159  x.Dual1() / x.Re(),
160  -square(x.Dual1()) / square(x.Re()) + x.Dual2() / x.Re()
161  );
162  }
163 
164 
166  inline dual2 log2(dual2 x) {
167 
168  if(x.Re() <= 0) {
169  TH_MATH_ERROR("log2(dual2)", x.Re(), OUT_OF_DOMAIN);
170  return dual2(nan(), nan());
171  }
172 
173  return dual2(
174  log2(x.Re()),
175  x.Dual1() * LOG2E / x.Re(),
176  -square(x.Dual1()) * LOG2E / square(x.Re()) + x.Dual2() * LOG2E / x.Re()
177  );
178  }
179 
180 
182  inline dual2 log10(dual2 x) {
183 
184  if(x.Re() <= 0) {
185  TH_MATH_ERROR("log10(dual2)", x.Re(), OUT_OF_DOMAIN);
186  return dual2(nan(), nan());
187  }
188 
189  return dual2(
190  log10(x.Re()),
191  x.Dual1() * LOG10E / x.Re(),
192  -square(x.Dual1()) * LOG10E / square(x.Re()) + x.Dual2() * LOG10E / x.Re()
193  );
194  }
195 
196 
198  inline dual2 abs(dual2 x) {
199  return dual2(abs(x.Re()), x.Dual1() * sgn(x.Re()), 0);
200  }
201 
202 
204  inline dual2 asin(dual2 x) {
205 
206  if(x.Re() >= 1) {
207  TH_MATH_ERROR("asin(dual2)", x.Re(), OUT_OF_DOMAIN);
208  return dual2(nan(), nan());
209  }
210 
211  const real sqrt_1mx2 = sqrt(1 - square(x.Re()));
212 
213  return dual2(
214  asin(x.Re()),
215  x.Dual1() / sqrt_1mx2,
216  square(x.Dual1()) * x.Re() / ((1 - square(x.Re())) * sqrt_1mx2)
217  + x.Dual2() / sqrt_1mx2
218  );
219  }
220 
221 
223  inline dual2 acos(dual2 x) {
224 
225  if(x.Re() >= 1) {
226  TH_MATH_ERROR("acos(dual2)", x.Re(), OUT_OF_DOMAIN);
227  return dual2(nan(), nan());
228  }
229 
230  const real sqrt_1mx2 = sqrt(1 - square(x.Re()));
231 
232  return dual2(
233  acos(x.Re()),
234  -x.Dual1() / sqrt_1mx2,
235  -square(x.Dual1()) * x.Re() / ((1 - square(x.Re())) * sqrt_1mx2)
236  - x.Dual2() / sqrt_1mx2
237  );
238  }
239 
240 
242  inline dual2 atan(dual2 x) {
243 
244  return dual2(
245  atan(x.Re()),
246  x.Dual1() / (1 + square(x.Re())),
247  square(x.Dual1()) * 2 * x.Re() / square(1 + square(x.Re()))
248  + x.Dual2() / (1 + square(x.Re()))
249  );
250  }
251 
252 }
253 
254 
255 #endif
Second order dual number class.
Definition: dual2.h:29
dual2 conjugate() const
Get the dual conjugate.
Definition: dual2.h:100
real Dual2() const
Return second order dual part.
Definition: dual2.h:95
real Re() const
Return real part.
Definition: dual2.h:85
real Dual1() const
Return first order dual part.
Definition: dual2.h:90
Second order 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
dual2 ln(dual2 x)
Compute the natural logarithm of a second order dual number.
Definition: dual2_functions.h:151
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
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