Theoretica
A C++ numerical and automatic mathematical library
parallel.h
Go to the documentation of this file.
1 
5 
6 #ifndef THEORETICA_PARALLEL_H
7 #define THEORETICA_PARALLEL_H
8 
9 #include "./vec.h"
10 #include "../core/dataset.h"
11 #include "../core/real_analysis.h"
12 #include "../complex/complex_analysis.h"
13 #include "../autodiff/dual_functions.h"
14 #include "../autodiff/dual2_functions.h"
15 #include "../autodiff/multidual_functions.h"
16 
17 
18 namespace theoretica {
19 
20 
22  namespace parallel {
23 
24 
25  // Unary operations
26 
27 
34  template<typename Function, typename Vector>
35  inline Vector apply_function(Function f, const Vector& v) {
36 
37  Vector res;
38  res.resize(v.size());
39 
40  #pragma omp parallel for
41  for (unsigned int i = 0; i < v.size(); i++)
42  res[i] = f(v[i]);
43 
44  return res;
45  }
46 
47 
52  template<typename Vector>
53  inline Vector square(const Vector& v) {
54 
55  Vector res;
56  res.resize(v.size());
57 
58  #pragma omp parallel for
59  for (unsigned int i = 0; i < v.size(); i++)
60  res[i] = theoretica::square(v[i]);
61 
62  return res;
63  }
64 
65 
70  template<typename Vector>
71  inline Vector cube(const Vector& v) {
72 
73  Vector res;
74  res.resize(v.size());
75 
76  #pragma omp parallel for
77  for (unsigned int i = 0; i < v.size(); i++)
78  res[i] = theoretica::cube(v[i]);
79 
80  return res;
81  }
82 
83 
88  template<typename Vector>
89  inline Vector abs(const Vector& v) {
90 
91  Vector res;
92  res.resize(v.size());
93 
94  #pragma omp parallel for
95  for (unsigned int i = 0; i < v.size(); i++)
96  res[i] = theoretica::abs(v[i]);
97 
98  return res;
99  }
100 
101 
106  template<typename Vector>
107  inline Vector pow(const Vector& v, int n) {
108 
109  Vector res;
110  res.resize(v.size());
111 
112  #pragma omp parallel for
113  for (unsigned int i = 0; i < v.size(); i++)
114  res[i] = theoretica::pow(v[i], n);
115 
116  return res;
117  }
118 
119 
124  template<typename Vector>
125  inline Vector powf(const Vector& v, real x) {
126 
127  Vector res;
128  res.resize(v.size());
129 
130  #pragma omp parallel for
131  for (unsigned int i = 0; i < v.size(); i++)
132  res[i] = theoretica::powf(v[i], x);
133 
134  return res;
135  }
136 
137 
142  template<typename Vector>
143  inline Vector sqrt(const Vector& v) {
144 
145  Vector res;
146  res.resize(v.size());
147 
148  #pragma omp parallel for
149  for (unsigned int i = 0; i < v.size(); i++)
150  res[i] = theoretica::sqrt(v[i]);
151 
152  return res;
153  }
154 
155 
160  template<typename Vector>
161  inline Vector cbrt(const Vector& v) {
162 
163  Vector res;
164  res.resize(v.size());
165 
166  #pragma omp parallel for
167  for (unsigned int i = 0; i < v.size(); i++)
168  res[i] = theoretica::cbrt(v[i]);
169 
170  return res;
171  }
172 
173 
178  template<typename Vector>
179  inline Vector exp(const Vector& v) {
180 
181  Vector res;
182  res.resize(v.size());
183 
184  #pragma omp parallel for
185  for (unsigned int i = 0; i < v.size(); i++)
186  res[i] = theoretica::exp(v[i]);
187 
188  return res;
189  }
190 
191 
196  template<typename Vector>
197  inline Vector ln(const Vector& v) {
198 
199  Vector res;
200  res.resize(v.size());
201 
202  #pragma omp parallel for
203  for (unsigned int i = 0; i < v.size(); i++)
204  res[i] = theoretica::ln(v[i]);
205 
206  return res;
207  }
208 
209 
214  template<typename Vector>
215  inline Vector log2(const Vector& v) {
216 
217  Vector res;
218  res.resize(v.size());
219 
220  #pragma omp parallel for
221  for (unsigned int i = 0; i < v.size(); i++)
222  res[i] = theoretica::log2(v[i]);
223 
224  return res;
225  }
226 
227 
232  template<typename Vector>
233  inline Vector log10(const Vector& v) {
234 
235  Vector res;
236  res.resize(v.size());
237 
238  #pragma omp parallel for
239  for (unsigned int i = 0; i < v.size(); i++)
240  res[i] = theoretica::log10(v[i]);
241 
242  return res;
243  }
244 
245 
250  template<typename Vector>
251  inline Vector sin(const Vector& v) {
252 
253  Vector res;
254  res.resize(v.size());
255 
256  #pragma omp parallel for
257  for (unsigned int i = 0; i < v.size(); i++)
258  res[i] = theoretica::sin(v[i]);
259 
260  return res;
261  }
262 
263 
268  template<typename Vector>
269  inline Vector cos(const Vector& v) {
270 
271  Vector res;
272  res.resize(v.size());
273 
274  #pragma omp parallel for
275  for (unsigned int i = 0; i < v.size(); i++)
276  res[i] = theoretica::cos(v[i]);
277 
278  return res;
279  }
280 
281 
286  template<typename Vector>
287  inline Vector tan(const Vector& v) {
288 
289  Vector res;
290  res.resize(v.size());
291 
292  #pragma omp parallel for
293  for (unsigned int i = 0; i < v.size(); i++)
294  res[i] = theoretica::tan(v[i]);
295 
296  return res;
297  }
298 
299 
304  template<typename Vector>
305  inline Vector cot(const Vector& v) {
306 
307  Vector res;
308  res.resize(v.size());
309 
310  #pragma omp parallel for
311  for (unsigned int i = 0; i < v.size(); i++)
312  res[i] = theoretica::cot(v[i]);
313 
314  return res;
315  }
316 
317 
322  template<typename Vector>
323  inline Vector asin(const Vector& v) {
324 
325  Vector res;
326  res.resize(v.size());
327 
328  #pragma omp parallel for
329  for (unsigned int i = 0; i < v.size(); i++)
330  res[i] = theoretica::asin(v[i]);
331 
332  return res;
333  }
334 
335 
340  template<typename Vector>
341  inline Vector acos(const Vector& v) {
342 
343  Vector res;
344  res.resize(v.size());
345 
346  #pragma omp parallel for
347  for (unsigned int i = 0; i < v.size(); i++)
348  res[i] = theoretica::acos(v[i]);
349 
350  return res;
351  }
352 
353 
358  template<typename Vector>
359  inline Vector atan(const Vector& v) {
360 
361  Vector res;
362  res.resize(v.size());
363 
364  #pragma omp parallel for
365  for (unsigned int i = 0; i < v.size(); i++)
366  res[i] = theoretica::atan(v[i]);
367 
368  return res;
369  }
370 
371 
376  template<typename Vector>
377  inline Vector sinh(const Vector& v) {
378 
379  Vector res;
380  res.resize(v.size());
381 
382  #pragma omp parallel for
383  for (unsigned int i = 0; i < v.size(); i++)
384  res[i] = theoretica::sinh(v[i]);
385 
386  return res;
387  }
388 
389 
394  template<typename Vector>
395  inline Vector cosh(const Vector& v) {
396 
397  Vector res;
398  res.resize(v.size());
399 
400  #pragma omp parallel for
401  for (unsigned int i = 0; i < v.size(); i++)
402  res[i] = theoretica::cosh(v[i]);
403 
404  return res;
405  }
406 
407 
412  template<typename Vector>
413  inline Vector tanh(const Vector& v) {
414 
415  Vector res;
416  res.resize(v.size());
417 
418  #pragma omp parallel for
419  for (unsigned int i = 0; i < v.size(); i++)
420  res[i] = theoretica::tanh(v[i]);
421 
422  return res;
423  }
424 
425 
430  template<typename Vector>
431  inline Vector coth(const Vector& v) {
432 
433  Vector res;
434  res.resize(v.size());
435 
436  #pragma omp parallel for
437  for (unsigned int i = 0; i < v.size(); i++)
438  res[i] = theoretica::coth(v[i]);
439 
440  return res;
441  }
442  }
443 }
444 
445 #endif
Vector exp(const Vector &v)
Parallel element-wise evaluation of the exp function.
Definition: parallel.h:179
Vector cbrt(const Vector &v)
Parallel element-wise evaluation of the cbrt function.
Definition: parallel.h:161
Vector tan(const Vector &v)
Vectorized (element-wise evaluation) of the tan function.
Definition: parallel.h:287
Vector cos(const Vector &v)
Parallel element-wise evaluation of the cos function.
Definition: parallel.h:269
Vector cot(const Vector &v)
Vectorized (element-wise evaluation) of the cot function.
Definition: parallel.h:305
Vector atan(const Vector &v)
Vectorized (element-wise evaluation) of the atan function.
Definition: parallel.h:359
Vector log10(const Vector &v)
Parallel element-wise evaluation of the log10 function.
Definition: parallel.h:233
Vector square(const Vector &v)
Parallel element-wise evaluation of the square function.
Definition: parallel.h:53
Vector tanh(const Vector &v)
Vectorized (element-wise evaluation) of the tanh function.
Definition: parallel.h:413
Vector cube(const Vector &v)
Parallel element-wise evaluation of the cube function.
Definition: parallel.h:71
Vector sinh(const Vector &v)
Vectorized (element-wise evaluation) of the sinh function.
Definition: parallel.h:377
Vector apply_function(Function f, const Vector &v)
Parallel element-wise evaluation of a function, using OpenMP to speed up execution over a vector.
Definition: parallel.h:35
Vector coth(const Vector &v)
Vectorized (element-wise evaluation) of the coth function.
Definition: parallel.h:431
Vector ln(const Vector &v)
Parallel element-wise evaluation of the ln function.
Definition: parallel.h:197
Vector abs(const Vector &v)
Parallel element-wise evaluation of the abs function.
Definition: parallel.h:89
Vector sqrt(const Vector &v)
Parallel element-wise evaluation of the sqrt function.
Definition: parallel.h:143
Vector pow(const Vector &v, int n)
Parallel element-wise evaluation of the pow function.
Definition: parallel.h:107
Vector sin(const Vector &v)
Parallel element-wise evaluation of the sin function.
Definition: parallel.h:251
Vector powf(const Vector &v, real x)
Parallel element-wise evaluation of the powf function.
Definition: parallel.h:125
Vector log2(const Vector &v)
Parallel element-wise evaluation of the log2 function.
Definition: parallel.h:215
Vector cosh(const Vector &v)
Vectorized (element-wise evaluation) of the cosh function.
Definition: parallel.h:395
Vector asin(const Vector &v)
Vectorized (element-wise evaluation) of the asin function.
Definition: parallel.h:323
Vector acos(const Vector &v)
Vectorized (element-wise evaluation) of the acos function.
Definition: parallel.h:341
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
real cbrt(real x)
Compute the cubic root of x.
Definition: real_analysis.h:199
real powf(real x, real a)
Approximate x elevated to a real exponent.
Definition: real_analysis.h:796
real coth(real x)
Compute the hyperbolic cotangent.
Definition: real_analysis.h:1153
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
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
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
Vector class and operations.