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,25 @@
1
+ typedef VALUE dtype;
2
+ typedef VALUE rtype;
3
+ #define cT numo_cRObject
4
+ #define cRT cT
5
+ // #define mTM mRObjectMath
6
+
7
+ #include "float_def.h"
8
+ #include "robj_macro.h"
9
+
10
+ #define m_min_init (0.0 / 0.0)
11
+ #define m_max_init (0.0 / 0.0)
12
+ #define m_extract(x) (*(VALUE*)x)
13
+ #define m_nearly_eq(x, y) robj_nearly_eq(x, y)
14
+
15
+ inline static int robj_nearly_eq(VALUE vx, VALUE vy) {
16
+ double x, y;
17
+ x = NUM2DBL(vx);
18
+ y = NUM2DBL(vy);
19
+ return (fabs(x - y) <= (fabs(x) + fabs(y)) * DBL_EPSILON * 2);
20
+ }
21
+
22
+ /* generates a random number on [0,1)-real-interval */
23
+ inline static dtype m_rand(dtype max) {
24
+ return DBL2NUM(genrand_res53_mix() * max);
25
+ }
@@ -0,0 +1,42 @@
1
+ typedef scomplex dtype;
2
+ typedef float rtype;
3
+ #define cT numo_cSComplex
4
+ #define cRT numo_cSFloat
5
+ #define mTM numo_mSComplexMath
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)) * FLT_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) = to_real2(gen_rand32()) * REAL(max);
18
+ IMAG(z) = to_real2(gen_rand32()) * 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 = to_real2(gen_rand32());
29
+ x1 = x1 * 2 - 1;
30
+ x2 = to_real2(gen_rand32());
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(1.1920928955078125e-07)
41
+ #define M_MIN rb_float_new(1.1754943508222875e-38)
42
+ #define M_MAX rb_float_new(3.4028234663852886e+38)
@@ -0,0 +1,45 @@
1
+ typedef float dtype;
2
+ typedef float rtype;
3
+ #define cT numo_cSFloat
4
+ #define cRT numo_cSFloat
5
+ #define mTM numo_mSFloatMath
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 to_real2(gen_rand32()) * 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 = to_real2(gen_rand32());
22
+ x1 = x1 * 2 - 1;
23
+ x2 = to_real2(gen_rand32());
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_sfloat_new_dim0(0.0 / 0.0)
38
+ #define m_max_init numo_sfloat_new_dim0(0.0 / 0.0)
39
+
40
+ #define m_extract(x) rb_float_new(*(float*)x)
41
+ #define m_nearly_eq(x, y) (fabs(x - y) <= (fabs(x) + fabs(y)) * FLT_EPSILON * 2)
42
+
43
+ #define M_EPSILON rb_float_new(1.1920928955078125e-07)
44
+ #define M_MIN rb_float_new(1.1754943508222875e-38)
45
+ #define M_MAX rb_float_new(3.4028234663852886e+38)
@@ -0,0 +1,24 @@
1
+ typedef u_int16_t dtype;
2
+ typedef u_int16_t rtype;
3
+ #define cT numo_cUInt16
4
+ #define cRT cT
5
+
6
+ #define m_num_to_data(x) ((dtype)NUM2UINT(x))
7
+ #if SIZEOF_INT > 2
8
+ #define m_data_to_num(x) INT2FIX(x)
9
+ #else
10
+ #define m_data_to_num(x) UINT2NUM(x)
11
+ #endif
12
+ #define m_sprintf(s, x) sprintf(s, "%u", (unsigned int)(x))
13
+
14
+ #ifndef UINT16_MAX
15
+ #define UINT16_MAX (65535)
16
+ #endif
17
+
18
+ #define DATA_MIN UINT16_MIN
19
+ #define DATA_MAX UINT16_MAX
20
+
21
+ #define M_MIN INT2FIX(0)
22
+ #define M_MAX m_data_to_num(UINT16_MAX)
23
+
24
+ #include "uint_macro.h"
@@ -0,0 +1,20 @@
1
+ typedef u_int32_t dtype;
2
+ typedef u_int32_t rtype;
3
+ #define cT numo_cUInt32
4
+ #define cRT cT
5
+
6
+ #define m_num_to_data(x) ((dtype)NUM2UINT32(x))
7
+ #define m_data_to_num(x) UINT322NUM((u_int32_t)(x))
8
+ #define m_sprintf(s, x) sprintf(s, "%" PRIu32, (u_int32_t)(x))
9
+
10
+ #ifndef UINT32_MAX
11
+ #define UINT32_MAX (4294967295u)
12
+ #endif
13
+
14
+ #define DATA_MIN UINT32_MIN
15
+ #define DATA_MAX UINT32_MAX
16
+
17
+ #define M_MIN INT2FIX(0)
18
+ #define M_MAX m_data_to_num(UINT32_MAX)
19
+
20
+ #include "uint_macro.h"
@@ -0,0 +1,20 @@
1
+ typedef u_int64_t dtype;
2
+ typedef u_int64_t rtype;
3
+ #define cT numo_cUInt64
4
+ #define cRT cT
5
+
6
+ #define m_num_to_data(x) ((dtype)NUM2UINT64(x))
7
+ #define m_data_to_num(x) UINT642NUM((u_int64_t)(x))
8
+ #define m_sprintf(s, x) sprintf(s, "%" PRIu64, (u_int64_t)(x))
9
+
10
+ #ifndef UINT64_MAX
11
+ #define UINT64_MAX (18446744073709551615ul)
12
+ #endif
13
+
14
+ #define DATA_MIN UINT64_MIN
15
+ #define DATA_MAX UINT64_MAX
16
+
17
+ #define M_MIN INT2FIX(0)
18
+ #define M_MAX m_data_to_num(UINT64_MAX)
19
+
20
+ #include "uint_macro.h"
@@ -0,0 +1,20 @@
1
+ typedef u_int8_t dtype;
2
+ typedef u_int8_t rtype;
3
+ #define cT numo_cUInt8
4
+ #define cRT cT
5
+
6
+ #define m_num_to_data(x) ((dtype)NUM2UINT(x))
7
+ #define m_data_to_num(x) INT2FIX(x)
8
+ #define m_sprintf(s, x) sprintf(s, "%u", (unsigned int)(x))
9
+
10
+ #ifndef UINT8_MAX
11
+ #define UINT8_MAX (255)
12
+ #endif
13
+
14
+ #define DATA_MIN UINT8_MIN
15
+ #define DATA_MAX UINT8_MAX
16
+
17
+ #define M_MIN INT2FIX(0)
18
+ #define M_MAX m_data_to_num(UINT8_MAX)
19
+
20
+ #include "uint_macro.h"
@@ -0,0 +1,57 @@
1
+ #include "xint_macro.h"
2
+
3
+ #define m_abs(x) (x)
4
+ #define m_sign(x) (((x) == 0) ? 0 : 1)
5
+
6
+ static inline dtype int_reciprocal(dtype x) {
7
+ switch (x) {
8
+ case 1:
9
+ return 1;
10
+ case 0:
11
+ rb_raise(rb_eZeroDivError, "divided by 0");
12
+ default:
13
+ return 0;
14
+ }
15
+ }
16
+
17
+ static dtype pow_int(dtype x, int p) {
18
+ dtype r = m_one;
19
+ switch (p) {
20
+ case 0:
21
+ return 1;
22
+ case 1:
23
+ return x;
24
+ case 2:
25
+ return x * x;
26
+ case 3:
27
+ return x * x * x;
28
+ }
29
+ while (p) {
30
+ if (p & 1) r *= x;
31
+ x *= x;
32
+ p >>= 1;
33
+ }
34
+ return r;
35
+ }
36
+
37
+ static inline u_int64_t f_sum(size_t n, char* p, ssize_t stride) {
38
+ u_int64_t x, y = 0;
39
+ size_t i = n;
40
+ for (; i--;) {
41
+ x = *(dtype*)p;
42
+ y += x;
43
+ p += stride;
44
+ }
45
+ return y;
46
+ }
47
+
48
+ static inline u_int64_t f_prod(size_t n, char* p, ssize_t stride) {
49
+ u_int64_t x, y = 1;
50
+ size_t i = n;
51
+ for (; i--;) {
52
+ x = *(dtype*)p;
53
+ y *= x;
54
+ p += stride;
55
+ }
56
+ return y;
57
+ }
@@ -0,0 +1,166 @@
1
+ #define m_zero 0
2
+ #define m_one 1
3
+
4
+ #define m_extract(x) m_data_to_num(*(dtype*)(x))
5
+
6
+ /* Handle negative values consistently across platforms for unsigned integer types */
7
+ #define m_from_double(x) ((x) < 0 ? (dtype)((long long)(x)) : (dtype)(x))
8
+ #define m_from_real(x) ((x) < 0 ? (dtype)((long long)(x)) : (dtype)(x))
9
+ #define m_from_sint(x) (x)
10
+ #define m_from_int32(x) (x)
11
+ #define m_from_int64(x) (x)
12
+ #define m_from_uint32(x) (x)
13
+ #define m_from_uint64(x) (x)
14
+
15
+ #define m_add(x, y) ((x) + (y))
16
+ #define m_sub(x, y) ((x) - (y))
17
+ #define m_mul(x, y) ((x) * (y))
18
+ #define m_div(x, y) ((x) / (y))
19
+ #define m_mod(x, y) ((x) % (y))
20
+ #define m_divmod(x, y, a, b) \
21
+ { \
22
+ a = (x) / (y); \
23
+ b = m_mod(x, y); \
24
+ }
25
+ #define m_pow(x, y) pow_int(x, y)
26
+ #define m_pow_int(x, y) pow_int(x, y)
27
+
28
+ #define m_bit_and(x, y) ((x) & (y))
29
+ #define m_bit_or(x, y) ((x) | (y))
30
+ #define m_bit_xor(x, y) ((x) ^ (y))
31
+ #define m_bit_not(x) (~(x))
32
+
33
+ #define m_minus(x) (-(x))
34
+ #define m_reciprocal(x) int_reciprocal(x)
35
+ #define m_square(x) ((x) * (x))
36
+
37
+ #define m_eq(x, y) ((x) == (y))
38
+ #define m_ne(x, y) ((x) != (y))
39
+ #define m_gt(x, y) ((x) > (y))
40
+ #define m_ge(x, y) ((x) >= (y))
41
+ #define m_lt(x, y) ((x) < (y))
42
+ #define m_le(x, y) ((x) <= (y))
43
+ #define m_left_shift(x, y) ((x) << (y))
44
+ #define m_right_shift(x, y) ((x) >> (y))
45
+
46
+ #define m_isnan(x) 0
47
+
48
+ #define m_mulsum(x, y, z) \
49
+ { z += x * y; }
50
+ #define m_mulsum_init INT2FIX(0)
51
+ #define m_cumsum(x, y) \
52
+ { x += y; }
53
+ #define m_cumprod(x, y) \
54
+ { x *= y; }
55
+
56
+ #define cmp(a, b) ((qsort_cast(a) == qsort_cast(b)) ? 0 : (qsort_cast(a) > qsort_cast(b)) ? 1 : -1)
57
+ #define cmpgt(a, b) (qsort_cast(a) > qsort_cast(b))
58
+
59
+ static inline dtype f_min(size_t n, char* p, ssize_t stride) {
60
+ dtype x, y;
61
+ size_t i = n;
62
+
63
+ y = *(dtype*)p;
64
+ p += stride;
65
+ i--;
66
+ for (; i--;) {
67
+ x = *(dtype*)p;
68
+ if (x < y) {
69
+ y = x;
70
+ }
71
+ p += stride;
72
+ }
73
+ return y;
74
+ }
75
+
76
+ static inline dtype f_max(size_t n, char* p, ssize_t stride) {
77
+ dtype x, y;
78
+ size_t i = n;
79
+
80
+ y = *(dtype*)p;
81
+ p += stride;
82
+ i--;
83
+ for (; i--;) {
84
+ x = *(dtype*)p;
85
+ if (x > y) {
86
+ y = x;
87
+ }
88
+ p += stride;
89
+ }
90
+ return y;
91
+ }
92
+
93
+ static inline size_t f_min_index(size_t n, char* p, ssize_t stride) {
94
+ dtype x, y;
95
+ size_t i, j = 0;
96
+
97
+ y = *(dtype*)p;
98
+ for (i = 1; i < n; i++) {
99
+ x = *(dtype*)(p + i * stride);
100
+ if (x < y) {
101
+ y = x;
102
+ j = i;
103
+ }
104
+ }
105
+ return j;
106
+ }
107
+
108
+ static inline size_t f_max_index(size_t n, char* p, ssize_t stride) {
109
+ dtype x, y;
110
+ size_t i, j = 0;
111
+
112
+ y = *(dtype*)p;
113
+ for (i = 1; i < n; i++) {
114
+ x = *(dtype*)(p + i * stride);
115
+ if (x > y) {
116
+ y = x;
117
+ j = i;
118
+ }
119
+ }
120
+ return j;
121
+ }
122
+
123
+ static inline void f_minmax(size_t n, char* p, ssize_t stride, dtype* amin, dtype* amax) {
124
+ dtype x, min, max;
125
+ size_t i = n;
126
+
127
+ min = max = *(dtype*)p;
128
+ p += stride;
129
+ for (i--; i--;) {
130
+ x = *(dtype*)p;
131
+ if (m_gt(x, max)) {
132
+ max = x;
133
+ }
134
+ if (m_lt(x, min)) {
135
+ min = x;
136
+ }
137
+ p += stride;
138
+ }
139
+ *amin = min;
140
+ *amax = max;
141
+ return;
142
+ }
143
+
144
+ static inline dtype f_ptp(size_t n, char* p, ssize_t stride) {
145
+ dtype min, max;
146
+ f_minmax(n, p, stride, &min, &max);
147
+ return m_sub(max, min);
148
+ }
149
+
150
+ static inline double f_seq(double x, double y, double c) {
151
+ return x + y * c;
152
+ }
153
+
154
+ static inline dtype f_maximum(dtype x, dtype y) {
155
+ if (m_ge(x, y)) {
156
+ return x;
157
+ }
158
+ return y;
159
+ }
160
+
161
+ static inline dtype f_minimum(dtype x, dtype y) {
162
+ if (m_le(x, y)) {
163
+ return x;
164
+ }
165
+ return y;
166
+ }
@@ -0,0 +1,40 @@
1
+ #include <ruby.h>
2
+
3
+ #ifdef HAVE_UNISTD_H
4
+ #include <unistd.h>
5
+ #endif
6
+ #include <time.h>
7
+ #ifdef HAVE_SYS_TIME_H
8
+ #include <sys/time.h>
9
+ #endif
10
+
11
+ #include "SFMT.h"
12
+ #include "numo/narray.h"
13
+
14
+ static u_int64_t random_seed(void) {
15
+ static int n = 0;
16
+ struct timeval tv;
17
+
18
+ gettimeofday(&tv, 0);
19
+ return tv.tv_sec ^ tv.tv_usec ^ getpid() ^ n++;
20
+ }
21
+
22
+ static VALUE nary_s_srand(int argc, VALUE* argv, VALUE obj) {
23
+ VALUE vseed;
24
+ u_int64_t seed;
25
+
26
+ // rb_secure(4);
27
+ if (rb_scan_args(argc, argv, "01", &vseed) == 0) {
28
+ seed = random_seed();
29
+ } else {
30
+ seed = NUM2UINT64(vseed);
31
+ }
32
+ init_gen_rand((uint32_t)seed);
33
+
34
+ return Qnil;
35
+ }
36
+
37
+ void Init_nary_rand(void) {
38
+ rb_define_singleton_method(cNArray, "srand", nary_s_srand, -1);
39
+ init_gen_rand(0);
40
+ }