vSMC
vSMC: Scalable Monte Carlo
vmath.hpp
Go to the documentation of this file.
1 //============================================================================
2 // vSMC/include/vsmc/math/vmath.hpp
3 //----------------------------------------------------------------------------
4 // vSMC: Scalable Monte Carlo
5 //----------------------------------------------------------------------------
6 // Copyright (c) 2013-2015, Yan Zhou
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are met:
11 //
12 // Redistributions of source code must retain the above copyright notice,
13 // this list of conditions and the following disclaimer.
14 //
15 // Redistributions in binary form must reproduce the above copyright notice,
16 // this list of conditions and the following disclaimer in the documentation
17 // and/or other materials provided with the distribution.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
20 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 // POSSIBILITY OF SUCH DAMAGE.
30 //============================================================================
31 
32 #ifndef VSMC_MATH_VMATH_HPP
33 #define VSMC_MATH_VMATH_HPP
34 
35 #include <vsmc/internal/config.h>
36 #include <vsmc/math/constants.hpp>
37 #include <cmath>
38 
39 #if VSMC_USE_MKL_VML
40 #include <mkl.h>
41 #endif
42 
43 #define VSMC_DEFINE_MATH_VMATH_1(func, name) \
44  template <typename T> \
45  inline void name(std::size_t n, const T *a, T *y) \
46  { \
47  for (std::size_t i = 0; i != n; ++i) \
48  y[i] = func(a[i]); \
49  }
50 
51 #define VSMC_DEFINE_MATH_VMATH_2(func, name) \
52  template <typename T> \
53  inline void name(std::size_t n, const T *a, const T *b, T *y) \
54  { \
55  for (std::size_t i = 0; i != n; ++i) \
56  y[i] = func(a[i], b[i]); \
57  }
58 
59 #define VSMC_DEFINE_MATH_VMATH_B(op, name) \
60  template <typename T> \
61  inline void name(std::size_t n, const T *a, const T *b, T *y) \
62  { \
63  for (std::size_t i = 0; i != n; ++i) \
64  y[i] = a[i] op b[i]; \
65  }
66 
67 #define VSMC_DEFINE_MATH_VMATH_VS(op, name) \
68  template <typename T> \
69  inline void name(std::size_t n, const T *a, T b, T *y) \
70  { \
71  for (std::size_t i = 0; i != n; ++i) \
72  y[i] = a[i] op b; \
73  }
74 
75 #define VSMC_DEFINE_MATH_VMATH_SV(op, name) \
76  template <typename T> \
77  inline void name(std::size_t n, T a, const T *b, T *y) \
78  { \
79  for (std::size_t i = 0; i != n; ++i) \
80  y[i] = a op b[i]; \
81  }
82 
83 #if VSMC_USE_MKL_VML
84 
85 #define VSMC_DEFINE_MATH_VMATH_VML_1(func, name) \
86  inline void name(std::size_t n, const float *a, float *y) \
87  { \
88  ::vs##func(static_cast<MKL_INT>(n), a, y); \
89  } \
90  inline void name(std::size_t n, const double *a, double *y) \
91  { \
92  ::vd##func(static_cast<MKL_INT>(n), a, y); \
93  }
94 
95 #define VSMC_DEFINE_MATH_VMATH_VML_2(func, name) \
96  inline void name(std::size_t n, const float *a, const float *b, float *y) \
97  { \
98  ::vs##func(static_cast<MKL_INT>(n), a, b, y); \
99  } \
100  inline void name( \
101  std::size_t n, const double *a, const double *b, double *y) \
102  { \
103  ::vd##func(static_cast<MKL_INT>(n), a, b, y); \
104  }
105 
106 namespace vsmc
107 {
108 
114 inline void linear_frac(std::size_t n, const float *a, const float *b,
115  float beta_a, float beta_b, float mu_a, float mu_b, float *y)
116 {
117  ::vsLinearFrac(
118  static_cast<MKL_INT>(n), a, b, beta_a, beta_b, mu_a, mu_b, y);
119 }
120 inline void linear_frac(std::size_t n, const double *a, const double *b,
121  double beta_a, double beta_b, double mu_a, double mu_b, double *y)
122 {
123  ::vdLinearFrac(
124  static_cast<MKL_INT>(n), a, b, beta_a, beta_b, mu_a, mu_b, y);
125 }
126 
136 inline void pow(std::size_t n, const float *a, float b, float *y)
137 {
138  ::vsPowx(static_cast<MKL_INT>(n), a, b, y);
139 }
140 inline void pow(std::size_t n, const double *a, double b, double *y)
141 {
142  ::vdPowx(static_cast<MKL_INT>(n), a, b, y);
143 }
145 
151 
154 inline void sincos(std::size_t n, const float *a, float *y, float *z)
155 {
156  ::vsSinCos(static_cast<MKL_INT>(n), a, y, z);
157 }
158 inline void sincos(std::size_t n, const double *a, double *y, double *z)
159 {
160  ::vdSinCos(static_cast<MKL_INT>(n), a, y, z);
161 }
167 
174 
183 
184 } // namespace vsmc
185 
186 #endif // VSMC_USE_MKL_VML
187 
188 namespace vsmc
189 {
190 
194 
197 
200 
203 
206 
208 VSMC_DEFINE_MATH_VMATH_VS(-, sub)
209 
212 
214 template <typename T>
215 inline void sqr(std::size_t n, const T *a, T *y)
216 {
217  for (std::size_t i = 0; i != n; ++i)
218  y[i] = a[i] * a[i];
219 }
220 
223 
226 
229 
232 
235 template <typename T>
236 inline void linear_frac(std::size_t n, const T *a, const T *b, T beta_a,
237  T beta_b, T mu_a, T mu_b, T *y)
238 {
239  const std::size_t k = 1000;
240  const std::size_t m = n / k;
241  const std::size_t l = n % k;
242  for (std::size_t i = 0; i != m; ++i, a += k, y += k) {
243  for (std::size_t j = 0; j != k; ++j)
244  y[j] = beta_a * a[j] + mu_a;
245  for (std::size_t j = 0; j != k; ++j)
246  y[j] /= beta_b * b[j] + mu_b;
247  }
248  for (std::size_t i = 0; i != l; ++i)
249  y[i] = beta_a * a[i] + mu_a;
250  for (std::size_t i = 0; i != l; ++i)
251  y[i] /= beta_b * b[i] + mu_b;
252 }
253 
256 template <typename T>
257 inline void fma(std::size_t n, const T *a, const T *b, const T *c, T *y)
258 {
259  for (std::size_t i = 0; i != n; ++i)
260  y[i] = a[i] * b[i] + c[i];
261 }
262 
265 template <typename T>
266 inline void fma(std::size_t n, const T *a, const T *b, T c, T *y)
267 {
268  for (std::size_t i = 0; i != n; ++i)
269  y[i] = a[i] * b[i] + c;
270 }
271 
274 template <typename T>
275 inline void fma(std::size_t n, const T *a, T b, const T *c, T *y)
276 {
277  for (std::size_t i = 0; i != n; ++i)
278  y[i] = a[i] * b + c[i];
279 }
280 
283 template <typename T>
284 inline void fma(std::size_t n, const T *a, T b, T c, T *y)
285 {
286  for (std::size_t i = 0; i != n; ++i)
287  y[i] = a[i] * b + c;
288 }
289 
292 template <typename T>
293 inline void fma(std::size_t n, T a, const T *b, const T *c, T *y)
294 {
295  for (std::size_t i = 0; i != n; ++i)
296  y[i] = a * b[i] + c[i];
297 }
298 
301 template <typename T>
302 inline void fma(std::size_t n, T a, const T *b, T c, T *y)
303 {
304  for (std::size_t i = 0; i != n; ++i)
305  y[i] = a * b[i] + c;
306 }
307 
310 template <typename T>
311 inline void fma(std::size_t n, T a, T b, const T *c, T *y)
312 {
313  add(n, a * b, c, y);
314 }
315 
317 
321 
323 template <typename T>
324 inline void inv(std::size_t n, const T *a, T *y)
325 {
326  const T one = static_cast<T>(1);
327  for (std::size_t i = 0; i != n; ++i)
328  y[i] = one / a[i];
329 }
330 
333 
336 
339 
342 
344 template <typename T>
345 inline void invsqrt(std::size_t n, const T *a, T *y)
346 {
347  const std::size_t k = 1000;
348  const std::size_t m = n / k;
349  const std::size_t l = n % k;
350  for (std::size_t i = 0; i != m; ++i, a += k, y += k) {
351  sqrt(k, a, y);
352  inv(k, y, y);
353  }
354  sqrt(l, a, y);
355  inv(l, y, y);
356 }
357 
360 
361 template <typename T>
363 inline void invcbrt(std::size_t n, const T *a, T *y)
364 {
365  const std::size_t k = 1000;
366  const std::size_t m = n / k;
367  const std::size_t l = n % k;
368  for (std::size_t i = 0; i != m; ++i, a += k, y += k) {
369  cbrt(k, a, y);
370  inv(k, y, y);
371  }
372  cbrt(l, a, y);
373  inv(l, y, y);
374 }
375 
377 template <typename T>
378 inline void pow2o3(std::size_t n, const T *a, T *y)
379 {
380  const std::size_t k = 1000;
381  const std::size_t m = n / k;
382  const std::size_t l = n % k;
383  for (std::size_t i = 0; i != m; ++i, a += k, y += k) {
384  cbrt(k, a, y);
385  sqr(k, y, y);
386  }
387  cbrt(l, a, y);
388  sqr(l, y, y);
389 }
390 
392 template <typename T>
393 inline void pow3o2(std::size_t n, const T *a, T *y)
394 {
395  const std::size_t k = 1000;
396  const std::size_t m = n / k;
397  const std::size_t l = n % k;
398  for (std::size_t i = 0; i != m; ++i, a += k, y += k) {
399  sqrt(k, a, y);
400  for (std::size_t j = 0; j != k; ++j)
401  y[j] = y[j] * y[j] * y[j];
402  }
403  sqrt(l, a, y);
404  for (std::size_t i = 0; i != l; ++i)
405  y[i] = y[i] * y[i] * y[i];
406 }
407 
410 
411 template <typename T>
413 inline void pow(std::size_t n, const T *a, T b, T *y)
414 {
415  for (std::size_t i = 0; i != n; ++i)
416  y[i] = std::pow(a[i], b);
417 }
418 
421 
422 
427 
430 
433 
435 template <typename T>
436 inline void exp10(std::size_t n, const T *a, T *y)
437 {
438  const std::size_t k = 1000;
439  const std::size_t m = n / k;
440  const std::size_t l = n % k;
441  for (std::size_t i = 0; i != m; ++i, a += k, y += k) {
442  mul(k, const_ln_10<T>(), a, y);
443  exp(k, y, y);
444  }
445  mul(l, const_ln_10<T>(), a, y);
446  exp(l, y, y);
447 }
448 
451 
454 
457 
460 
464 
468 
471 
474 
477 template <typename T>
478 inline void sincos(std::size_t n, const T *a, T *y, T *z)
479 {
480  const std::size_t k = 1000;
481  const std::size_t m = n / k;
482  const std::size_t l = n % k;
483  for (std::size_t i = 0; i != m; ++i, a += k, y += k) {
484  sin(k, a, y);
485  cos(k, a, z);
486  }
487  sin(l, a, y);
488  cos(l, a, z);
489 }
490 
493 
496 
499 
502 
506 
508 
512 
515 
518 
521 
524 
527 
530 
532 
536 
539 
543 
546 template <typename T>
547 inline void cdfnorm(std::size_t n, const T *a, T *y)
548 {
549  const std::size_t k = 1000;
550  const std::size_t m = n / k;
551  const std::size_t l = n % k;
552  for (std::size_t i = 0; i != m; ++i, a += k, y += k) {
553  mul(k, 1 / const_sqrt_2<T>(), a, y);
554  erf(k, y, y);
555  fma(k, static_cast<T>(0.5), static_cast<T>(0.5), y, y);
556  }
557  mul(l, 1 / const_sqrt_2<T>(), a, y);
558  erf(l, y, y);
559  fma(l, static_cast<T>(0.5), static_cast<T>(0.5), y, y);
560 }
561 
565 
570 
571 } // namespace vsmc
572 
573 #endif // VSMC_MATH_VMATH_HPP
void cdfnorm(std::size_t n, const float *a, float *y)
Definition: vmath.hpp:177
Definition: monitor.hpp:49
void erfcinv(std::size_t n, const float *a, float *y)
Definition: vmath.hpp:179
void cbrt(std::size_t n, const T *a, T *y)
For , compute .
Definition: vmath.hpp:359
#define VSMC_DEFINE_MATH_VMATH_1(func, name)
Definition: vmath.hpp:43
void mul(std::size_t n, const float *a, const float *b, float *y)
Definition: vmath.hpp:112
void log1p(std::size_t n, const float *a, float *y)
Definition: vmath.hpp:150
void acosh(std::size_t n, const float *a, float *y)
Definition: vmath.hpp:171
void sqrt(std::size_t n, const float *a, float *y)
Definition: vmath.hpp:129
void sinh(std::size_t n, const float *a, float *y)
Definition: vmath.hpp:169
void invsqrt(std::size_t n, const float *a, float *y)
Definition: vmath.hpp:130
void pow3o2(std::size_t n, const float *a, float *y)
Definition: vmath.hpp:134
void pow2o3(std::size_t n, const float *a, float *y)
Definition: vmath.hpp:133
void asin(std::size_t n, const float *a, float *y)
Definition: vmath.hpp:164
STL namespace.
void tan(std::size_t n, const T *a, T *y)
For , compute .
Definition: vmath.hpp:492
void cdfnorminv(std::size_t n, const float *a, float *y)
Definition: vmath.hpp:180
void exp2(std::size_t n, const T *a, T *y)
For , compute .
Definition: vmath.hpp:432
void log2(std::size_t n, const T *a, T *y)
For , compute .
Definition: vmath.hpp:456
void tanh(std::size_t n, const float *a, float *y)
Definition: vmath.hpp:170
void erfinv(std::size_t n, const float *a, float *y)
Definition: vmath.hpp:178
void linear_frac(std::size_t n, const float *a, const float *b, float beta_a, float beta_b, float mu_a, float mu_b, float *y)
Definition: vmath.hpp:114
#define VSMC_DEFINE_MATH_VMATH_2(func, name)
Definition: vmath.hpp:51
#define VSMC_DEFINE_MATH_VMATH_SV(op, name)
Definition: vmath.hpp:75
void invcbrt(std::size_t n, const float *a, float *y)
Definition: vmath.hpp:132
void erf(std::size_t n, const float *a, float *y)
Definition: vmath.hpp:175
void expm1(std::size_t n, const float *a, float *y)
Definition: vmath.hpp:147
void pow(std::size_t n, const float *a, const float *b, float *y)
Definition: vmath.hpp:135
void cos(std::size_t n, const float *a, float *y)
Definition: vmath.hpp:152
void lgamma(std::size_t n, const float *a, float *y)
Definition: vmath.hpp:181
void exp(std::size_t n, const float *a, float *y)
Definition: vmath.hpp:146
void atanh(std::size_t n, const float *a, float *y)
Definition: vmath.hpp:173
void inv(std::size_t n, const float *a, float *y)
Definition: vmath.hpp:127
void asinh(std::size_t n, const float *a, float *y)
Definition: vmath.hpp:172
#define VSMC_DEFINE_MATH_VMATH_VML_2(func, name)
Definition: vmath.hpp:95
void atan2(std::size_t n, const float *a, const float *b, float *y)
Definition: vmath.hpp:166
void div(std::size_t n, const float *a, const float *b, float *y)
Definition: vmath.hpp:128
void sub(std::size_t n, const float *a, const float *b, float *y)
Definition: vmath.hpp:110
void fma(std::size_t n, const T *a, const T *b, const T *c, T *y)
For , compute .
Definition: vmath.hpp:257
void tgamma(std::size_t n, const T *a, T *y)
For , compute , the Gamma function.
Definition: vmath.hpp:568
void sincos(std::size_t n, const float *a, float *y, float *z)
Definition: vmath.hpp:154
void sin(std::size_t n, const float *a, float *y)
Definition: vmath.hpp:153
void cosh(std::size_t n, const float *a, float *y)
Definition: vmath.hpp:168
void acos(std::size_t n, const float *a, float *y)
Definition: vmath.hpp:163
void add(std::size_t n, const float *a, const float *b, float *y)
Definition: vmath.hpp:109
void tgamm(std::size_t n, const float *a, float *y)
Definition: vmath.hpp:182
void atan(std::size_t n, const float *a, float *y)
Definition: vmath.hpp:165
#define VSMC_DEFINE_MATH_VMATH_VS(op, name)
Definition: vmath.hpp:67
void exp10(std::size_t n, const T *a, T *y)
For , compute .
Definition: vmath.hpp:436
void pow(std::size_t n, const T *a, T b, T *y)
For , compute .
Definition: vmath.hpp:413
void erfc(std::size_t n, const float *a, float *y)
Definition: vmath.hpp:176
void tan(std::size_t n, const float *a, float *y)
Definition: vmath.hpp:162
void hypot(std::size_t n, const T *a, const T *b, T *y)
For , compute .
Definition: vmath.hpp:420
void hypot(std::size_t n, const float *a, const float *b, float *y)
Definition: vmath.hpp:144
void cbrt(std::size_t n, const float *a, float *y)
Definition: vmath.hpp:131
#define VSMC_DEFINE_MATH_VMATH_VML_1(func, name)
Definition: vmath.hpp:85
#define VSMC_DEFINE_MATH_VMATH_B(op, name)
Definition: vmath.hpp:59
void log(std::size_t n, const float *a, float *y)
Definition: vmath.hpp:148
void lgamma(std::size_t n, const T *a, T *y)
For , compute , logarithm of the Gamma function.
Definition: vmath.hpp:564
void sqr(std::size_t n, const float *a, float *y)
Definition: vmath.hpp:111
void log10(std::size_t n, const float *a, float *y)
Definition: vmath.hpp:149
void abs(std::size_t n, const float *a, float *y)
Definition: vmath.hpp:113
void expm1(std::size_t n, const T *a, T *y)
For , compute .
Definition: vmath.hpp:450