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  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  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(sqrt_x,
64  0.5 / sqrt_x * x.Dual1(),
65  -0.25 / x.Re() / sqrt_x * square(x.Dual1()) + 0.5 / sqrt_x * x.Dual2());
66  }
67 
68 
70  inline dual2 sin(dual2 x) {
71 
72  real sin_x = sin(x.Re());
73  real cos_x = cos(x.Re());
74 
75  return dual2(sin_x,
76  cos_x * x.Dual1(),
77  -sin_x * square(x.Dual1()) + cos_x * x.Dual2());
78  }
79 
80 
82  inline dual2 cos(dual2 x) {
83 
84  real sin_x = sin(x.Re());
85  real cos_x = cos(x.Re());
86 
87  return dual2(cos_x,
88  -sin_x * x.Dual1(),
89  -cos_x * square(x.Dual1()) - sin_x * x.Dual2());
90  }
91 
92 
94  inline dual2 tan(dual2 x) {
95 
96  real sin_x = sin(x.Re());
97  real cos_x = cos(x.Re());
98 
99  if(cos_x == 0) {
100  TH_MATH_ERROR("tan(dual2)", cos_x, DIV_BY_ZERO);
101  return dual2(nan(), nan());
102  }
103 
104  return dual2(tan(x.Re()),
105  x.Dual1() / square(cos_x),
106  2.0 * sin_x / cube(cos_x) * square(x.Dual1())
107  + x.Dual2() / square(cos_x));
108  }
109 
110 
112  inline dual2 cot(dual2 x) {
113 
114  real sin_x = sin(x.Re());
115  real cos_x = cos(x.Re());
116 
117  if(sin_x == 0) {
118  TH_MATH_ERROR("cot(dual2)", sin_x, DIV_BY_ZERO);
119  return dual2(nan(), nan());
120  }
121 
122  return dual2(cot(x.Re()),
123  -x.Dual1() / square(sin_x),
124  -2.0 * cos_x / cube(sin_x) * square(x.Dual1())
125  - x.Dual2() / square(sin_x));
126  }
127 
128 
130  inline dual2 exp(dual2 x) {
131  real exp_x = exp(x.Re());
132  return dual2(exp_x,
133  x.Dual1() * exp_x,
134  square(x.Dual1()) * exp_x + x.Dual2() * exp_x);
135  }
136 
137 
139  inline dual2 ln(dual2 x) {
140 
141  if(x.Re() <= 0) {
142  TH_MATH_ERROR("ln(dual2)", x.Re(), OUT_OF_DOMAIN);
143  return dual2(nan(), nan());
144  }
145 
146  return dual2(ln(x.Re()),
147  x.Dual1() / x.Re(),
148  -square(x.Dual1()) / square(x.Re()) + x.Dual2() / x.Re());
149  }
150 
151 
153  inline dual2 log2(dual2 x) {
154 
155  if(x.Re() <= 0) {
156  TH_MATH_ERROR("log2(dual2)", x.Re(), OUT_OF_DOMAIN);
157  return dual2(nan(), nan());
158  }
159 
160  return dual2(log2(x.Re()),
161  x.Dual1() * LOG2E / x.Re(),
162  -square(x.Dual1()) * LOG2E / square(x.Re())
163  + x.Dual2() * LOG2E / x.Re());
164  }
165 
166 
168  inline dual2 log10(dual2 x) {
169 
170  if(x.Re() <= 0) {
171  TH_MATH_ERROR("log10(dual2)", x.Re(), OUT_OF_DOMAIN);
172  return dual2(nan(), nan());
173  }
174 
175  return dual2(log10(x.Re()),
176  x.Dual1() * LOG10E / x.Re(),
177  -square(x.Dual1()) * LOG10E / square(x.Re())
178  + x.Dual2() * LOG10E / x.Re());
179  }
180 
181 
183  inline dual2 abs(dual2 x) {
184  return dual2(abs(x.Re()), x.Dual1() * sgn(x.Re()), 0);
185  }
186 
187 
189  inline dual2 asin(dual2 x) {
190 
191  if(x.Re() >= 1) {
192  TH_MATH_ERROR("asin(dual2)", x.Re(), OUT_OF_DOMAIN);
193  return dual2(nan(), nan());
194  }
195 
196  real sqrt_1mx2 = sqrt(1 - square(x.Re()));
197 
198  return dual2(asin(x.Re()),
199  x.Dual1() / sqrt_1mx2,
200  square(x.Dual1()) * x.Re() / ((1 - square(x.Re())) * sqrt_1mx2)
201  + x.Dual2() / sqrt_1mx2);
202  }
203 
204 
206  inline dual2 acos(dual2 x) {
207 
208  if(x.Re() >= 1) {
209  TH_MATH_ERROR("acos(dual2)", x.Re(), OUT_OF_DOMAIN);
210  return dual2(nan(), nan());
211  }
212 
213  real sqrt_1mx2 = sqrt(1 - square(x.Re()));
214 
215  return dual2(acos(x.Re()),
216  -x.Dual1() / sqrt_1mx2,
217  -square(x.Dual1()) * x.Re() / ((1 - square(x.Re())) * sqrt_1mx2)
218  - x.Dual2() / sqrt_1mx2);
219  }
220 
221 
223  inline dual2 atan(dual2 x) {
224  return dual2(atan(x.Re()),
225  x.Dual1() / (1 + square(x.Re())),
226  square(x.Dual1()) * 2 * x.Re() / square(1 + square(x.Re()))
227  + x.Dual2() / (1 + square(x.Re())));
228  }
229 
230 }
231 
232 
233 #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:188
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:139
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
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