Theoretica
A C++ numerical and automatic mathematical library
multidual_functions.h
Go to the documentation of this file.
1 
5 
6 
7 #ifndef THEORETICA_MULTIDUAL_FUNCTIONS_H
8 #define THEORETICA_MULTIDUAL_FUNCTIONS_H
9 
10 #include "./multidual.h"
11 #include "../core/real_analysis.h"
12 
13 
14 namespace theoretica {
15 
16 
18  template<unsigned int N>
20  return x * x;
21  }
22 
23 
25  template<unsigned int N>
27  return x * x * x;
28  }
29 
30 
32  template<unsigned int N>
34  return x.conjugate();
35  }
36 
37 
39  template<unsigned int N>
41  real pow_n_1_x = pow(x.Re(), n - 1);
42  return multidual<N>(pow_n_1_x * x.Re(), x.Dual() * pow_n_1_x * n);
43  }
44 
45 
47  template<unsigned int N>
49 
50  real sqrt_x = sqrt(x.Re());
51 
52  if(sqrt_x == 0) {
53  TH_MATH_ERROR("sqrt(multidual)", sqrt_x, DIV_BY_ZERO);
54  return multidual<N>(nan(), vec<real, N>(nan()));
55  }
56 
57  return multidual<N>(sqrt_x, x.Dual() * 0.5 / sqrt_x);
58  }
59 
60 
62  template<unsigned int N>
64  return multidual<N>(sin(x.Re()), x.Dual() * cos(x.Re()));
65  }
66 
67 
69  template<unsigned int N>
71  return multidual<N>(cos(x.Re()), x.Dual() * -sin(x.Re()));
72  }
73 
74 
76  template<unsigned int N>
78 
79  real cos_x = cos(x.Re());
80 
81  if(cos_x == 0) {
82  TH_MATH_ERROR("tan(multidual)", cos_x, DIV_BY_ZERO);
83  return multidual<N>(nan(), vec<real, N>(nan()));
84  }
85 
86  return multidual<N>(tan(x.Re()), x.Dual() / square(cos_x));
87  }
88 
89 
91  template<unsigned int N>
93 
94  real sin_x = sin(x.Re());
95 
96  if(sin_x == 0) {
97  TH_MATH_ERROR("cot(multidual)", sin_x, DIV_BY_ZERO);
98  return multidual<N>(nan(), vec<real, N>(nan()));
99  }
100 
101  return multidual<N>(cot(x.Re()), x.Dual() * (-1 / square(sin_x)));
102  }
103 
104 
106  template<unsigned int N>
108  real exp_x = exp(x.Re());
109  return multidual<N>(exp_x, x.Dual() * exp_x);
110  }
111 
112 
114  template<unsigned int N>
116 
117  if(x.Re() <= 0) {
118  TH_MATH_ERROR("ln(multidual)", x.Re(), OUT_OF_DOMAIN);
119  return multidual<N>(nan(), vec<real, N>(nan()));
120  }
121 
122  return multidual<N>(ln(x.Re()), x.Dual() / x.Re());
123  }
124 
125 
127  template<unsigned int N>
129 
130  if(x.Re() <= 0) {
131  TH_MATH_ERROR("log2(multidual)", x.Re(), OUT_OF_DOMAIN);
132  return multidual<N>(nan(), vec<real, N>(nan()));
133  }
134 
135  return multidual<N>(log2(x.Re()), x.Dual() * LOG2E / x.Re());
136  }
137 
138 
140  template<unsigned int N>
142 
143  if(x.Re() <= 0) {
144  TH_MATH_ERROR("log10(multidual)", x.Re(), OUT_OF_DOMAIN);
145  return multidual<N>(nan(), vec<real, N>(nan()));
146  }
147 
148  return multidual<N>(log10(x.Re()), x.Dual() * LOG10E / x.Re());
149  }
150 
151 
153  template<unsigned int N>
155  return multidual<N>(abs(x.Re()), x.Dual() * sgn(x.Re()));
156  }
157 
158 
160  template<unsigned int N>
162 
163  if(x.Re() >= 1) {
164  TH_MATH_ERROR("asin(multidual)", x.Re(), OUT_OF_DOMAIN);
165  return multidual<N>(nan(), vec<real, N>(nan()));
166  }
167 
168  return multidual<N>(asin(x.Re()), x.Dual() / sqrt(1 - square(x.Re())));
169  }
170 
171 
173  template<unsigned int N>
175 
176  if(x.Re() >= 1) {
177  TH_MATH_ERROR("acos(multidual)", x.Re(), OUT_OF_DOMAIN);
178  return multidual<N>(nan(), vec<real, N>(nan()));
179  }
180 
181  return multidual<N>(acos(x.Re()), x.Dual() * (-1 / sqrt(1 - square(x.Re()))));
182  }
183 
185  template<unsigned int N>
187  return multidual<N>(atan(x.Re()), x.Dual() / (1 + square(x.Re())));
188  }
189 
190 
192  template<unsigned int N>
194 
195  real exp_x = exp(x.Re());
196  return multidual<N>((exp_x - 1.0 / exp_x) / 2.0, x.Dual() * (exp_x + 1.0 / exp_x) / 2.0);
197  }
198 
199 
201  template<unsigned int N>
203 
204  real exp_x = exp(x.Re());
205  return multidual<N>((exp_x + 1.0 / exp_x) / 2.0, x.Dual() * (exp_x - 1.0 / exp_x) / 2.0);
206  }
207 
208 
210  template<unsigned int N>
212 
213  real exp_x = exp(x.Re());
214  return multidual<N>(
215  (exp_x - 1.0 / exp_x) / (exp_x + 1.0 / exp_x),
216  x.Dual() / square(exp_x + 1.0 / exp_x));
217  }
218 
219 }
220 
221 
222 #endif
Multidual number algebra for functions of the form .
Definition: multidual.h:26
vec< real, N > Dual() const
Get the multidual part.
Definition: multidual.h:79
real Re() const
Get the real part.
Definition: multidual.h:67
multidual conjugate() const
Get the multidual conjugate.
Definition: multidual.h:91
A statically allocated N-dimensional vector with elements of the given type.
Definition: vec.h:88
#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
Multidual numbers.
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