numo-narray-alt 0.9.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +14 -0
  3. data/LICENSE +30 -0
  4. data/README.md +71 -0
  5. data/Rakefile +24 -0
  6. data/ext/numo/narray/SFMT-params.h +97 -0
  7. data/ext/numo/narray/SFMT-params19937.h +48 -0
  8. data/ext/numo/narray/SFMT.c +602 -0
  9. data/ext/numo/narray/SFMT.h +147 -0
  10. data/ext/numo/narray/array.c +575 -0
  11. data/ext/numo/narray/data.c +958 -0
  12. data/ext/numo/narray/extconf.rb +84 -0
  13. data/ext/numo/narray/index.c +1092 -0
  14. data/ext/numo/narray/kwargs.c +142 -0
  15. data/ext/numo/narray/math.c +133 -0
  16. data/ext/numo/narray/narray.c +1976 -0
  17. data/ext/numo/narray/narray.def +28 -0
  18. data/ext/numo/narray/ndloop.c +1840 -0
  19. data/ext/numo/narray/numo/compat.h +23 -0
  20. data/ext/numo/narray/numo/intern.h +115 -0
  21. data/ext/numo/narray/numo/narray.h +480 -0
  22. data/ext/numo/narray/numo/ndloop.h +93 -0
  23. data/ext/numo/narray/numo/template.h +149 -0
  24. data/ext/numo/narray/numo/types/bit.h +38 -0
  25. data/ext/numo/narray/numo/types/complex.h +404 -0
  26. data/ext/numo/narray/numo/types/complex_macro.h +384 -0
  27. data/ext/numo/narray/numo/types/dcomplex.h +42 -0
  28. data/ext/numo/narray/numo/types/dfloat.h +44 -0
  29. data/ext/numo/narray/numo/types/float_def.h +34 -0
  30. data/ext/numo/narray/numo/types/float_macro.h +202 -0
  31. data/ext/numo/narray/numo/types/int16.h +27 -0
  32. data/ext/numo/narray/numo/types/int32.h +23 -0
  33. data/ext/numo/narray/numo/types/int64.h +23 -0
  34. data/ext/numo/narray/numo/types/int8.h +23 -0
  35. data/ext/numo/narray/numo/types/int_macro.h +66 -0
  36. data/ext/numo/narray/numo/types/real_accum.h +481 -0
  37. data/ext/numo/narray/numo/types/robj_macro.h +78 -0
  38. data/ext/numo/narray/numo/types/robject.h +25 -0
  39. data/ext/numo/narray/numo/types/scomplex.h +42 -0
  40. data/ext/numo/narray/numo/types/sfloat.h +45 -0
  41. data/ext/numo/narray/numo/types/uint16.h +24 -0
  42. data/ext/numo/narray/numo/types/uint32.h +20 -0
  43. data/ext/numo/narray/numo/types/uint64.h +20 -0
  44. data/ext/numo/narray/numo/types/uint8.h +20 -0
  45. data/ext/numo/narray/numo/types/uint_macro.h +57 -0
  46. data/ext/numo/narray/numo/types/xint_macro.h +166 -0
  47. data/ext/numo/narray/rand.c +40 -0
  48. data/ext/numo/narray/src/t_bit.c +3236 -0
  49. data/ext/numo/narray/src/t_dcomplex.c +6776 -0
  50. data/ext/numo/narray/src/t_dfloat.c +9417 -0
  51. data/ext/numo/narray/src/t_int16.c +5757 -0
  52. data/ext/numo/narray/src/t_int32.c +5757 -0
  53. data/ext/numo/narray/src/t_int64.c +5759 -0
  54. data/ext/numo/narray/src/t_int8.c +5355 -0
  55. data/ext/numo/narray/src/t_robject.c +5567 -0
  56. data/ext/numo/narray/src/t_scomplex.c +6731 -0
  57. data/ext/numo/narray/src/t_sfloat.c +9374 -0
  58. data/ext/numo/narray/src/t_uint16.c +5753 -0
  59. data/ext/numo/narray/src/t_uint32.c +5753 -0
  60. data/ext/numo/narray/src/t_uint64.c +5755 -0
  61. data/ext/numo/narray/src/t_uint8.c +5351 -0
  62. data/ext/numo/narray/step.c +266 -0
  63. data/ext/numo/narray/struct.c +814 -0
  64. data/lib/numo/narray/extra.rb +1266 -0
  65. data/lib/numo/narray.rb +4 -0
  66. metadata +106 -0
@@ -0,0 +1,384 @@
1
+ #include "float_def.h"
2
+
3
+ extern double round(double);
4
+ extern double log2(double);
5
+ extern double exp2(double);
6
+ extern double exp10(double);
7
+
8
+ #define r_abs(x) fabs(x)
9
+ #define r_sqrt(x) sqrt(x)
10
+ #define r_exp(x) exp(x)
11
+ #define r_log(x) log(x)
12
+ #define r_sin(x) sin(x)
13
+ #define r_cos(x) cos(x)
14
+ #define r_sinh(x) sinh(x)
15
+ #define r_cosh(x) cosh(x)
16
+ #define r_tanh(x) tanh(x)
17
+ #define r_atan2(y, x) atan2(y, x)
18
+ #define r_hypot(x, y) hypot(x, y)
19
+
20
+ #include "complex.h"
21
+
22
+ static inline dtype c_from_scomplex(scomplex x) {
23
+ dtype z;
24
+ REAL(z) = REAL(x);
25
+ IMAG(z) = IMAG(x);
26
+ return z;
27
+ }
28
+
29
+ static inline dtype c_from_dcomplex(dcomplex x) {
30
+ dtype z;
31
+ REAL(z) = REAL(x);
32
+ IMAG(z) = IMAG(x);
33
+ return z;
34
+ }
35
+
36
+ /* --------------------------- */
37
+
38
+ #define m_zero c_zero()
39
+ #define m_one c_one()
40
+
41
+ #define m_num_to_data(x) NUM2COMP(x)
42
+ #define m_data_to_num(x) COMP2NUM(x)
43
+
44
+ #define m_from_double(x) c_new(x, 0)
45
+ #define m_from_real(x) c_new(x, 0)
46
+ #define m_from_sint(x) c_new(x, 0)
47
+ #define m_from_int32(x) c_new(x, 0)
48
+ #define m_from_int64(x) c_new(x, 0)
49
+ #define m_from_uint32(x) c_new(x, 0)
50
+ #define m_from_uint64(x) c_new(x, 0)
51
+ #define m_from_scomplex(x) c_from_scomplex(x)
52
+ #define m_from_dcomplex(x) c_from_dcomplex(x)
53
+
54
+ #define m_extract(x) COMP2NUM(*(dtype*)x)
55
+
56
+ #define m_real(x) REAL(x)
57
+ #define m_imag(x) IMAG(x)
58
+ #define m_set_real(x, y) c_set_real(x, y)
59
+ #define m_set_imag(x, y) c_set_imag(x, y)
60
+
61
+ #define m_add(x, y) c_add(x, y)
62
+ #define m_sub(x, y) c_sub(x, y)
63
+ #define m_mul(x, y) c_mul(x, y)
64
+ #define m_div(x, y) c_div(x, y)
65
+ #define m_mod(x, y) c_mod(x, y)
66
+ #define m_pow(x, y) c_pow(x, y)
67
+ #define m_pow_int(x, y) c_pow_int(x, y)
68
+
69
+ #define m_abs(x) c_abs(x)
70
+ #define m_minus(x) c_minus(x)
71
+ #define m_reciprocal(x) c_reciprocal(x)
72
+ #define m_square(x) c_square(x)
73
+ #define m_floor(x) c_new(floor(REAL(x)), floor(IMAG(x)))
74
+ #define m_round(x) c_new(round(REAL(x)), round(IMAG(x)))
75
+ #define m_ceil(x) c_new(ceil(REAL(x)), ceil(IMAG(x)))
76
+ #define m_trunc(x) c_new(trunc(REAL(x)), trunc(IMAG(x)))
77
+ #define m_rint(x) c_new(rint(REAL(x)), rint(IMAG(x)))
78
+ #define m_sign(x) \
79
+ c_new(((REAL(x) == 0) ? 0.0 : ((REAL(x) > 0) ? 1.0 : ((REAL(x) < 0) ? -1.0 : REAL(x)))), \
80
+ ((IMAG(x) == 0) ? 0.0 : ((IMAG(x) > 0) ? 1.0 : ((IMAG(x) < 0) ? -1.0 : IMAG(x)))))
81
+ #define m_copysign(x, y) c_new(copysign(REAL(x), REAL(y)), copysign(IMAG(x), IMAG(y)))
82
+
83
+ #define m_im(x) c_im(x)
84
+ #define m_conj(x) c_new(REAL(x), -IMAG(x))
85
+ #define m_arg(x) atan2(IMAG(x), REAL(x))
86
+
87
+ #define m_eq(x, y) c_eq(x, y)
88
+ #define m_ne(x, y) c_ne(x, y)
89
+ #define m_nearly_eq(x, y) c_nearly_eq(x, y)
90
+
91
+ #define m_isnan(x) c_isnan(x)
92
+ #define m_isinf(x) c_isinf(x)
93
+ #define m_isposinf(x) c_isposinf(x)
94
+ #define m_isneginf(x) c_isneginf(x)
95
+ #define m_isfinite(x) c_isfinite(x)
96
+
97
+ #define m_sprintf(s, x) sprintf(s, "%g%+gi", REAL(x), IMAG(x))
98
+
99
+ #define m_sqrt(x) c_sqrt(x)
100
+ #define m_cbrt(x) c_cbrt(x)
101
+ #define m_log(x) c_log(x)
102
+ #define m_log2(x) c_log2(x)
103
+ #define m_log10(x) c_log10(x)
104
+ #define m_exp(x) c_exp(x)
105
+ #define m_exp2(x) c_exp2(x)
106
+ #define m_exp10(x) c_exp10(x)
107
+ #define m_sin(x) c_sin(x)
108
+ #define m_cos(x) c_cos(x)
109
+ #define m_tan(x) c_tan(x)
110
+ #define m_asin(x) c_asin(x)
111
+ #define m_acos(x) c_acos(x)
112
+ #define m_atan(x) c_atan(x)
113
+ #define m_sinh(x) c_sinh(x)
114
+ #define m_cosh(x) c_cosh(x)
115
+ #define m_tanh(x) c_tanh(x)
116
+ #define m_asinh(x) c_asinh(x)
117
+ #define m_acosh(x) c_acosh(x)
118
+ #define m_atanh(x) c_atanh(x)
119
+ #define m_hypot(x, y) c_hypot(x, y)
120
+ #define m_sinc(x) ((REAL(x) == 0 && IMAG(x) == 0) ? (c_new(1, 0)) : (c_div(c_sin(x), x)))
121
+
122
+ #define m_sum_init INT2FIX(0)
123
+ #define m_mulsum_init INT2FIX(0)
124
+
125
+ #define not_nan(x) (REAL(x) == REAL(x) && IMAG(x) == IMAG(x))
126
+
127
+ #define m_mulsum(x, y, z) \
128
+ { z = m_add(m_mul(x, y), z); }
129
+ #define m_mulsum_nan(x, y, z) \
130
+ { \
131
+ if (not_nan(x) && not_nan(y)) { \
132
+ z = m_add(m_mul(x, y), z); \
133
+ } \
134
+ }
135
+
136
+ #define m_cumsum(x, y) \
137
+ { (x) = m_add(x, y); }
138
+ #define m_cumsum_nan(x, y) \
139
+ { \
140
+ if (!not_nan(x)) { \
141
+ (x) = (y); \
142
+ } else if (not_nan(y)) { \
143
+ (x) = m_add(x, y); \
144
+ } \
145
+ }
146
+
147
+ #define m_cumprod(x, y) \
148
+ { (x) = m_mul(x, y); }
149
+ #define m_cumprod_nan(x, y) \
150
+ { \
151
+ if (!not_nan(x)) { \
152
+ (x) = (y); \
153
+ } else if (not_nan(y)) { \
154
+ (x) = m_mul(x, y); \
155
+ } \
156
+ }
157
+
158
+ static inline dtype f_sum(size_t n, char* p, ssize_t stride) {
159
+ size_t i = n;
160
+ dtype x, y;
161
+
162
+ y = c_zero();
163
+ for (; i--;) {
164
+ x = *(dtype*)p;
165
+ y = c_add(x, y);
166
+ p += stride;
167
+ }
168
+ return y;
169
+ }
170
+
171
+ static inline dtype f_sum_nan(size_t n, char* p, ssize_t stride) {
172
+ size_t i = n;
173
+ dtype x, y;
174
+
175
+ y = c_zero();
176
+ for (; i--;) {
177
+ x = *(dtype*)p;
178
+ if (not_nan(x)) {
179
+ y = c_add(x, y);
180
+ }
181
+ p += stride;
182
+ }
183
+ return y;
184
+ }
185
+
186
+ static inline dtype f_kahan_sum(size_t n, char* p, ssize_t stride) {
187
+ size_t i = n;
188
+ dtype x;
189
+ volatile dtype y, t, r;
190
+
191
+ y = c_zero();
192
+ r = c_zero();
193
+ for (; i--;) {
194
+ x = *(dtype*)p;
195
+ if (fabs(REAL(x)) > fabs(REAL(y))) {
196
+ double z = REAL(x);
197
+ REAL(x) = REAL(y);
198
+ REAL(y) = z;
199
+ }
200
+ if (fabs(IMAG(x)) > fabs(IMAG(y))) {
201
+ double z = IMAG(x);
202
+ IMAG(x) = IMAG(y);
203
+ IMAG(y) = z;
204
+ }
205
+ r = c_add(x, r);
206
+ t = y;
207
+ y = c_add(r, y);
208
+ t = c_sub(y, t);
209
+ r = c_sub(r, t);
210
+ p += stride;
211
+ }
212
+ return y;
213
+ }
214
+
215
+ static inline dtype f_kahan_sum_nan(size_t n, char* p, ssize_t stride) {
216
+ size_t i = n;
217
+ dtype x;
218
+ volatile dtype y, t, r;
219
+
220
+ y = c_zero();
221
+ r = c_zero();
222
+ for (; i--;) {
223
+ x = *(dtype*)p;
224
+ if (not_nan(x)) {
225
+ if (fabs(REAL(x)) > fabs(REAL(y))) {
226
+ double z = REAL(x);
227
+ REAL(x) = REAL(y);
228
+ REAL(y) = z;
229
+ }
230
+ if (fabs(IMAG(x)) > fabs(IMAG(y))) {
231
+ double z = IMAG(x);
232
+ IMAG(x) = IMAG(y);
233
+ IMAG(y) = z;
234
+ }
235
+ r = c_add(x, r);
236
+ t = y;
237
+ y = c_add(r, y);
238
+ t = c_sub(y, t);
239
+ r = c_sub(r, t);
240
+ }
241
+ p += stride;
242
+ }
243
+ return y;
244
+ }
245
+
246
+ static inline dtype f_prod(size_t n, char* p, ssize_t stride) {
247
+ size_t i = n;
248
+ dtype x, y;
249
+
250
+ y = c_one();
251
+ for (; i--;) {
252
+ x = *(dtype*)p;
253
+ y = c_mul(x, y);
254
+ p += stride;
255
+ }
256
+ return y;
257
+ }
258
+
259
+ static inline dtype f_prod_nan(size_t n, char* p, ssize_t stride) {
260
+ size_t i = n;
261
+ dtype x, y;
262
+
263
+ y = c_one();
264
+ for (; i--;) {
265
+ x = *(dtype*)p;
266
+ if (not_nan(x)) {
267
+ y = c_mul(x, y);
268
+ }
269
+ p += stride;
270
+ }
271
+ return y;
272
+ }
273
+
274
+ static inline dtype f_mean(size_t n, char* p, ssize_t stride) {
275
+ size_t i = n;
276
+ size_t count = 0;
277
+ dtype x, y;
278
+
279
+ y = c_zero();
280
+ for (; i--;) {
281
+ x = *(dtype*)p;
282
+ y = c_add(x, y);
283
+ count++;
284
+ p += stride;
285
+ }
286
+ return c_div_r(y, count);
287
+ }
288
+
289
+ static inline dtype f_mean_nan(size_t n, char* p, ssize_t stride) {
290
+ size_t i = n;
291
+ size_t count = 0;
292
+ dtype x, y;
293
+
294
+ y = c_zero();
295
+ for (; i--;) {
296
+ x = *(dtype*)p;
297
+ if (not_nan(x)) {
298
+ y = c_add(x, y);
299
+ count++;
300
+ }
301
+ p += stride;
302
+ }
303
+ return c_div_r(y, count);
304
+ }
305
+
306
+ static inline rtype f_var(size_t n, char* p, ssize_t stride) {
307
+ size_t i = n;
308
+ size_t count = 0;
309
+ dtype x, m;
310
+ rtype y = 0;
311
+
312
+ m = f_mean(n, p, stride);
313
+
314
+ for (; i--;) {
315
+ x = *(dtype*)p;
316
+ y += c_abs_square(c_sub(x, m));
317
+ count++;
318
+ p += stride;
319
+ }
320
+ return y / (count - 1);
321
+ }
322
+
323
+ static inline rtype f_var_nan(size_t n, char* p, ssize_t stride) {
324
+ size_t i = n;
325
+ size_t count = 0;
326
+ dtype x, m;
327
+ rtype y = 0;
328
+
329
+ m = f_mean_nan(n, p, stride);
330
+
331
+ for (; i--;) {
332
+ x = *(dtype*)p;
333
+ if (not_nan(x)) {
334
+ y += c_abs_square(c_sub(x, m));
335
+ count++;
336
+ }
337
+ p += stride;
338
+ }
339
+ return y / (count - 1);
340
+ }
341
+
342
+ static inline rtype f_stddev(size_t n, char* p, ssize_t stride) {
343
+ return r_sqrt(f_var(n, p, stride));
344
+ }
345
+
346
+ static inline rtype f_stddev_nan(size_t n, char* p, ssize_t stride) {
347
+ return r_sqrt(f_var_nan(n, p, stride));
348
+ }
349
+
350
+ static inline rtype f_rms(size_t n, char* p, ssize_t stride) {
351
+ size_t i = n;
352
+ size_t count = 0;
353
+ dtype x;
354
+ rtype y = 0;
355
+
356
+ for (; i--;) {
357
+ x = *(dtype*)p;
358
+ y += c_abs_square(x);
359
+ count++;
360
+ p += stride;
361
+ }
362
+ return r_sqrt(y / count);
363
+ }
364
+
365
+ static inline rtype f_rms_nan(size_t n, char* p, ssize_t stride) {
366
+ size_t i = n;
367
+ size_t count = 0;
368
+ dtype x;
369
+ rtype y = 0;
370
+
371
+ for (; i--;) {
372
+ x = *(dtype*)p;
373
+ if (not_nan(x)) {
374
+ y += c_abs_square(x);
375
+ count++;
376
+ }
377
+ p += stride;
378
+ }
379
+ return r_sqrt(y / count);
380
+ }
381
+
382
+ static inline dtype f_seq(dtype x, dtype y, double c) {
383
+ return c_add(x, c_mul_r(y, c));
384
+ }
@@ -0,0 +1,42 @@
1
+ typedef dcomplex dtype;
2
+ typedef double rtype;
3
+ #define cT numo_cDComplex
4
+ #define cRT numo_cDFloat
5
+ #define mTM numo_mDComplexMath
6
+
7
+ #include "complex_macro.h"
8
+
9
+ static inline bool c_nearly_eq(dtype x, dtype y) {
10
+ return c_abs(c_sub(x, y)) <= (c_abs(x) + c_abs(y)) * DBL_EPSILON * 2;
11
+ }
12
+
13
+ #ifdef SFMT_H
14
+ /* generates a random number on [0,1)-real-interval */
15
+ inline static dtype m_rand(dtype max) {
16
+ dtype z;
17
+ REAL(z) = genrand_res53_mix() * REAL(max);
18
+ IMAG(z) = genrand_res53_mix() * IMAG(max);
19
+ return z;
20
+ }
21
+
22
+ /* generates random numbers from the normal distribution
23
+ using Box-Muller Transformation.
24
+ */
25
+ inline static void m_rand_norm(dtype mu, rtype sigma, dtype* a0) {
26
+ rtype x1, x2, w;
27
+ do {
28
+ x1 = genrand_res53_mix();
29
+ x1 = x1 * 2 - 1;
30
+ x2 = genrand_res53_mix();
31
+ x2 = x2 * 2 - 1;
32
+ w = x1 * x1 + x2 * x2;
33
+ } while (w >= 1);
34
+ w = sqrt((-2 * log(w)) / w);
35
+ REAL(*a0) = x1 * w * sigma + REAL(mu);
36
+ IMAG(*a0) = x2 * w * sigma + IMAG(mu);
37
+ }
38
+ #endif
39
+
40
+ #define M_EPSILON rb_float_new(2.2204460492503131e-16)
41
+ #define M_MIN rb_float_new(2.2250738585072014e-308)
42
+ #define M_MAX rb_float_new(1.7976931348623157e+308)
@@ -0,0 +1,44 @@
1
+ typedef double dtype;
2
+ typedef double rtype;
3
+ #define cT numo_cDFloat
4
+ #define cRT numo_cDFloat
5
+ #define mTM numo_mDFloatMath
6
+
7
+ #include "float_macro.h"
8
+
9
+ #ifdef SFMT_H
10
+ /* generates a random number on [0,1)-real-interval */
11
+ inline static dtype m_rand(dtype max) {
12
+ return genrand_res53_mix() * max;
13
+ }
14
+
15
+ /* generates random numbers from the normal distribution
16
+ using Box-Muller Transformation.
17
+ */
18
+ inline static void m_rand_norm(dtype mu, dtype sigma, dtype* a0, dtype* a1) {
19
+ dtype x1, x2, w;
20
+ do {
21
+ x1 = genrand_res53_mix();
22
+ x1 = x1 * 2 - 1;
23
+ x2 = genrand_res53_mix();
24
+ x2 = x2 * 2 - 1;
25
+ w = x1 * x1 + x2 * x2;
26
+ } while (w >= 1);
27
+ w = sqrt((-2 * log(w)) / w);
28
+ if (a0) {
29
+ *a0 = x1 * w * sigma + mu;
30
+ }
31
+ if (a1) {
32
+ *a1 = x2 * w * sigma + mu;
33
+ }
34
+ }
35
+ #endif
36
+
37
+ #define m_min_init numo_dfloat_new_dim0(0.0 / 0.0)
38
+ #define m_max_init numo_dfloat_new_dim0(0.0 / 0.0)
39
+ #define m_extract(x) rb_float_new(*(double*)x)
40
+ #define m_nearly_eq(x, y) (fabs(x - y) <= (fabs(x) + fabs(y)) * DBL_EPSILON * 2)
41
+
42
+ #define M_EPSILON rb_float_new(2.2204460492503131e-16)
43
+ #define M_MIN rb_float_new(2.2250738585072014e-308)
44
+ #define M_MAX rb_float_new(1.7976931348623157e+308)
@@ -0,0 +1,34 @@
1
+ #ifndef DBL_EPSILON
2
+ #define DBL_EPSILON 2.2204460492503131e-16
3
+ #endif
4
+ #ifndef FLT_EPSILON
5
+ #define FLT_EPSILON 1.1920928955078125e-07
6
+ #endif
7
+ #ifndef DBL_MAX
8
+ #define DBL_MAX 1.7976931348623157e+308
9
+ #endif
10
+ #ifndef DBL_MAX
11
+ #define DBL_MAX 1.7976931348623157e+308
12
+ #endif
13
+ #ifndef FLT_MIN
14
+ #define FLT_MIN 1.1754943508222875e-38
15
+ #endif
16
+ #ifndef FLT_MAX
17
+ #define FLT_MAX 3.4028234663852886e+38
18
+ #endif
19
+
20
+ #ifndef M_PI_2
21
+ #define M_PI_2 1.57079632679489661923 /* pi/2 */
22
+ #endif
23
+ #ifndef M_LOG2E
24
+ #define M_LOG2E 1.4426950408889634074 /* log_2 e */
25
+ #endif
26
+ #ifndef M_LOG10E
27
+ #define M_LOG10E 0.43429448190325182765 /* log_10 e */
28
+ #endif
29
+ #ifndef M_LN2
30
+ #define M_LN2 0.69314718055994530942 /* log_e 2 */
31
+ #endif
32
+ #ifndef M_LN10
33
+ #define M_LN10 2.30258509299404568402 /* log_e 10 */
34
+ #endif
@@ -0,0 +1,202 @@
1
+ #include "float_def.h"
2
+
3
+ extern double round(double);
4
+ extern double log2(double);
5
+ extern double exp2(double);
6
+ #ifdef HAVE_EXP10
7
+ extern double exp10(double);
8
+ #else
9
+ extern double pow(double, double);
10
+ #endif
11
+
12
+ #define m_zero 0.0
13
+ #define m_one 1.0
14
+
15
+ #define m_num_to_data(x) (NIL_P(x) ? nan("") : NUM2DBL(x))
16
+ #define m_data_to_num(x) rb_float_new(x)
17
+
18
+ #define m_from_double(x) (x)
19
+ #define m_from_real(x) (x)
20
+ #define m_from_sint(x) (x)
21
+ #define m_from_int32(x) (x)
22
+ #define m_from_int64(x) (x)
23
+ #define m_from_uint32(x) (x)
24
+ #define m_from_uint64(x) (x)
25
+
26
+ #define m_add(x, y) ((x) + (y))
27
+ #define m_sub(x, y) ((x) - (y))
28
+ #define m_mul(x, y) ((x) * (y))
29
+ #define m_div(x, y) ((x) / (y))
30
+ #define m_div_check(x, y) ((y) == 0)
31
+ #define m_mod(x, y) fmod(x, y)
32
+ #define m_divmod(x, y, a, b) \
33
+ { \
34
+ a = (x) / (y); \
35
+ b = m_mod(x, y); \
36
+ }
37
+ #define m_pow(x, y) pow(x, y)
38
+ #define m_pow_int(x, y) pow_int(x, y)
39
+
40
+ #define m_abs(x) fabs(x)
41
+ #define m_minus(x) (-(x))
42
+ #define m_reciprocal(x) (1 / (x))
43
+ #define m_square(x) ((x) * (x))
44
+ #define m_floor(x) floor(x)
45
+ #define m_round(x) round(x)
46
+ #define m_ceil(x) ceil(x)
47
+ #define m_trunc(x) trunc(x)
48
+ #define m_rint(x) rint(x)
49
+ #define m_sign(x) (((x) == 0) ? 0.0 : (((x) > 0) ? 1.0 : (((x) < 0) ? -1.0 : (x))))
50
+ #define m_copysign(x, y) copysign(x, y)
51
+ #define m_signbit(x) signbit(x)
52
+ #define m_modf(x, y, z) \
53
+ { \
54
+ double d; \
55
+ y = modf(x, &d); \
56
+ z = d; \
57
+ }
58
+
59
+ #define m_eq(x, y) ((x) == (y))
60
+ #define m_ne(x, y) ((x) != (y))
61
+ #define m_gt(x, y) ((x) > (y))
62
+ #define m_ge(x, y) ((x) >= (y))
63
+ #define m_lt(x, y) ((x) < (y))
64
+ #define m_le(x, y) ((x) <= (y))
65
+
66
+ #define m_isnan(x) isnan(x)
67
+ #define m_isinf(x) isinf(x)
68
+ #define m_isposinf(x) (isinf(x) && signbit(x) == 0)
69
+ #define m_isneginf(x) (isinf(x) && signbit(x))
70
+ #define m_isfinite(x) isfinite(x)
71
+
72
+ #define m_mulsum_init INT2FIX(0)
73
+
74
+ #define m_sprintf(s, x) sprintf(s, "%g", x)
75
+
76
+ #define cmp_prnan(a, b) ((qsort_cast(a) == qsort_cast(b)) ? 0 : (qsort_cast(a) > qsort_cast(b)) ? 1 : -1)
77
+
78
+ #define cmp_ignan(a, b) \
79
+ (m_isnan(qsort_cast(a)) ? (m_isnan(qsort_cast(b)) ? 0 : 1) \
80
+ : (m_isnan(qsort_cast(b)) ? -1 \
81
+ : ((qsort_cast(a) == qsort_cast(b)) ? 0 \
82
+ : (qsort_cast(a) > qsort_cast(b)) ? 1 \
83
+ : -1)))
84
+
85
+ #define cmpgt_prnan(a, b) (qsort_cast(a) > qsort_cast(b))
86
+
87
+ #define cmpgt_ignan(a, b) ((m_isnan(qsort_cast(a)) && !m_isnan(qsort_cast(b))) || (qsort_cast(a) > qsort_cast(b)))
88
+
89
+ #define m_sqrt(x) sqrt(x)
90
+ #define m_cbrt(x) cbrt(x)
91
+ #define m_log(x) log(x)
92
+ #define m_log2(x) log2(x)
93
+ #define m_log10(x) log10(x)
94
+ #define m_exp(x) exp(x)
95
+ #define m_exp2(x) exp2(x)
96
+ #ifdef HAVE_EXP10
97
+ #define m_exp10(x) exp10(x)
98
+ #else
99
+ #define m_exp10(x) pow(10, x)
100
+ #endif
101
+ #define m_expm1(x) expm1(x)
102
+ #define m_log1p(x) log1p(x)
103
+
104
+ #define m_sin(x) sin(x)
105
+ #define m_cos(x) cos(x)
106
+ #define m_tan(x) tan(x)
107
+ #define m_asin(x) asin(x)
108
+ #define m_acos(x) acos(x)
109
+ #define m_atan(x) atan(x)
110
+ #define m_sinh(x) sinh(x)
111
+ #define m_cosh(x) cosh(x)
112
+ #define m_tanh(x) tanh(x)
113
+ #define m_asinh(x) asinh(x)
114
+ #define m_acosh(x) acosh(x)
115
+ #define m_atanh(x) atanh(x)
116
+ #define m_atan2(x, y) atan2(x, y)
117
+ #define m_hypot(x, y) hypot(x, y)
118
+ #define m_sinc(x) (((x) == 0) ? 1.0 : (sin(x) / (x)))
119
+
120
+ #define m_erf(x) erf(x)
121
+ #define m_erfc(x) erfc(x)
122
+ #define m_ldexp(x, y) ldexp(x, y)
123
+ #define m_frexp(x, exp) frexp(x, exp)
124
+
125
+ static inline dtype pow_int(dtype x, int p) {
126
+ dtype r = 1;
127
+ switch (p) {
128
+ case 0:
129
+ return 1;
130
+ case 1:
131
+ return x;
132
+ case 2:
133
+ return x * x;
134
+ case 3:
135
+ return x * x * x;
136
+ case 4:
137
+ x = x * x;
138
+ return x * x;
139
+ }
140
+ if (p < 0) return 1 / pow_int(x, -p);
141
+ if (p > 64) return pow(x, p);
142
+ while (p) {
143
+ if (p & 1) r *= x;
144
+ x *= x;
145
+ p >>= 1;
146
+ }
147
+ return r;
148
+ }
149
+
150
+ static inline dtype f_seq(dtype x, dtype y, double c) {
151
+ return x + y * c;
152
+ }
153
+
154
+ static inline dtype f_kahan_sum(size_t n, char* p, ssize_t stride) {
155
+ size_t i = n;
156
+ dtype x;
157
+ volatile dtype y = 0;
158
+ volatile dtype t, r = 0;
159
+
160
+ for (; i--;) {
161
+ x = *(dtype*)p;
162
+ p += stride;
163
+ if (fabs(x) > fabs(y)) {
164
+ dtype z = x;
165
+ x = y;
166
+ y = z;
167
+ }
168
+ r += x;
169
+ t = y;
170
+ y += r;
171
+ t = y - t;
172
+ r -= t;
173
+ }
174
+ return y;
175
+ }
176
+
177
+ static inline dtype f_kahan_sum_nan(size_t n, char* p, ssize_t stride) {
178
+ size_t i = n;
179
+ dtype x;
180
+ volatile dtype y = 0;
181
+ volatile dtype t, r = 0;
182
+
183
+ for (; i--;) {
184
+ x = *(dtype*)p;
185
+ p += stride;
186
+ if (!m_isnan(x)) {
187
+ if (fabs(x) > fabs(y)) {
188
+ dtype z = x;
189
+ x = y;
190
+ y = z;
191
+ }
192
+ r += x;
193
+ t = y;
194
+ y += r;
195
+ t = y - t;
196
+ r -= t;
197
+ }
198
+ }
199
+ return y;
200
+ }
201
+
202
+ #include "real_accum.h"