numo-narray-alt 0.9.9 → 0.9.11

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 (39) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +1 -1
  3. data/ext/numo/narray/numo/narray.h +2 -2
  4. data/ext/numo/narray/numo/types/robject.h +1 -1
  5. data/ext/numo/narray/src/mh/argmax.h +154 -0
  6. data/ext/numo/narray/src/mh/argmin.h +154 -0
  7. data/ext/numo/narray/src/mh/clip.h +115 -0
  8. data/ext/numo/narray/src/mh/cumprod.h +98 -0
  9. data/ext/numo/narray/src/mh/cumsum.h +98 -0
  10. data/ext/numo/narray/src/mh/eye.h +82 -0
  11. data/ext/numo/narray/src/mh/logseq.h +69 -0
  12. data/ext/numo/narray/src/mh/max.h +69 -0
  13. data/ext/numo/narray/src/mh/max_index.h +184 -0
  14. data/ext/numo/narray/src/mh/maximum.h +116 -0
  15. data/ext/numo/narray/src/mh/min.h +69 -0
  16. data/ext/numo/narray/src/mh/min_index.h +184 -0
  17. data/ext/numo/narray/src/mh/minimum.h +116 -0
  18. data/ext/numo/narray/src/mh/minmax.h +77 -0
  19. data/ext/numo/narray/src/mh/mulsum.h +185 -0
  20. data/ext/numo/narray/src/mh/prod.h +69 -0
  21. data/ext/numo/narray/src/mh/ptp.h +69 -0
  22. data/ext/numo/narray/src/mh/rand.h +315 -0
  23. data/ext/numo/narray/src/mh/seq.h +130 -0
  24. data/ext/numo/narray/src/mh/sum.h +69 -0
  25. data/ext/numo/narray/src/t_dcomplex.c +131 -667
  26. data/ext/numo/narray/src/t_dfloat.c +40 -1288
  27. data/ext/numo/narray/src/t_int16.c +262 -1161
  28. data/ext/numo/narray/src/t_int32.c +263 -1161
  29. data/ext/numo/narray/src/t_int64.c +262 -1163
  30. data/ext/numo/narray/src/t_int8.c +262 -1159
  31. data/ext/numo/narray/src/t_robject.c +427 -1675
  32. data/ext/numo/narray/src/t_scomplex.c +131 -667
  33. data/ext/numo/narray/src/t_sfloat.c +40 -1288
  34. data/ext/numo/narray/src/t_uint16.c +262 -1161
  35. data/ext/numo/narray/src/t_uint32.c +262 -1161
  36. data/ext/numo/narray/src/t_uint64.c +262 -1163
  37. data/ext/numo/narray/src/t_uint8.c +262 -1161
  38. data/lib/numo/narray.rb +3 -1
  39. metadata +23 -3
@@ -0,0 +1,315 @@
1
+ #ifndef NUMO_NARRAY_MH_RAND_H
2
+ #define NUMO_NARRAY_MH_RAND_H 1
3
+
4
+ #define DEF_NARRAY_CMP_RAND_METHOD_FUNC(tDType) \
5
+ typedef struct { \
6
+ tDType low; \
7
+ tDType max; \
8
+ } rand_opt_t; \
9
+ \
10
+ static void iter_##tDType##_rand(na_loop_t* const lp) { \
11
+ size_t n; \
12
+ char* p1; \
13
+ ssize_t s1; \
14
+ size_t* idx1; \
15
+ tDType x; \
16
+ rand_opt_t* g; \
17
+ tDType low; \
18
+ tDType max; \
19
+ \
20
+ INIT_COUNTER(lp, n); \
21
+ INIT_PTR_IDX(lp, 0, p1, s1, idx1); \
22
+ g = (rand_opt_t*)(lp->opt_ptr); \
23
+ low = g->low; \
24
+ max = g->max; \
25
+ \
26
+ if (idx1) { \
27
+ for (size_t i = 0; i < n; i++) { \
28
+ x = m_add(m_rand(max), low); \
29
+ SET_DATA_INDEX(p1, idx1, dtype, x); \
30
+ } \
31
+ } else { \
32
+ for (size_t i = 0; i < n; i++) { \
33
+ x = m_add(m_rand(max), low); \
34
+ SET_DATA_STRIDE(p1, s1, dtype, x); \
35
+ } \
36
+ } \
37
+ } \
38
+ \
39
+ static VALUE tDType##_rand(int argc, VALUE* argv, VALUE self) { \
40
+ rand_opt_t g; \
41
+ VALUE v1 = Qnil; \
42
+ VALUE v2 = Qnil; \
43
+ tDType high; \
44
+ ndfunc_arg_in_t ain[1] = { { OVERWRITE, 0 } }; \
45
+ ndfunc_t ndf = { iter_##tDType##_rand, FULL_LOOP, 1, 0, ain, 0 }; \
46
+ \
47
+ rb_scan_args(argc, argv, "02", &v1, &v2); \
48
+ if (v2 == Qnil) { \
49
+ g.low = m_zero; \
50
+ if (v1 == Qnil) { \
51
+ g.max = high = c_new(1, 1); \
52
+ } else { \
53
+ g.max = high = m_num_to_data(v1); \
54
+ } \
55
+ } else { \
56
+ g.low = m_num_to_data(v1); \
57
+ high = m_num_to_data(v2); \
58
+ g.max = m_sub(high, g.low); \
59
+ } \
60
+ \
61
+ na_ndloop3(&ndf, &g, 1, self); \
62
+ return self; \
63
+ }
64
+
65
+ #define DEF_NARRAY_FLT_RAND_METHOD_FUNC(tDType) \
66
+ typedef struct { \
67
+ tDType low; \
68
+ tDType max; \
69
+ } rand_opt_t; \
70
+ \
71
+ static void iter_##tDType##_rand(na_loop_t* const lp) { \
72
+ size_t n; \
73
+ char* p1; \
74
+ ssize_t s1; \
75
+ size_t* idx1; \
76
+ tDType x; \
77
+ rand_opt_t* g; \
78
+ tDType low; \
79
+ tDType max; \
80
+ \
81
+ INIT_COUNTER(lp, n); \
82
+ INIT_PTR_IDX(lp, 0, p1, s1, idx1); \
83
+ g = (rand_opt_t*)(lp->opt_ptr); \
84
+ low = g->low; \
85
+ max = g->max; \
86
+ \
87
+ if (idx1) { \
88
+ for (size_t i = 0; i < n; i++) { \
89
+ x = m_add(m_rand(max), low); \
90
+ SET_DATA_INDEX(p1, idx1, tDType, x); \
91
+ } \
92
+ } else { \
93
+ for (size_t i = 0; i < n; i++) { \
94
+ x = m_add(m_rand(max), low); \
95
+ SET_DATA_STRIDE(p1, s1, tDType, x); \
96
+ } \
97
+ } \
98
+ } \
99
+ \
100
+ static VALUE tDType##_rand(int argc, VALUE* args, VALUE self) { \
101
+ rand_opt_t g; \
102
+ VALUE v1 = Qnil; \
103
+ VALUE v2 = Qnil; \
104
+ tDType high; \
105
+ ndfunc_arg_in_t ain[1] = { { OVERWRITE, 0 } }; \
106
+ ndfunc_t ndf = { iter_##tDType##_rand, FULL_LOOP, 1, 0, ain, 0 }; \
107
+ \
108
+ rb_scan_args(argc, args, "02", &v1, &v2); \
109
+ if (v2 == Qnil) { \
110
+ g.low = m_zero; \
111
+ if (v1 == Qnil) { \
112
+ g.max = high = m_one; \
113
+ } else { \
114
+ g.max = high = m_num_to_data(v1); \
115
+ } \
116
+ } else { \
117
+ g.low = m_num_to_data(v1); \
118
+ high = m_num_to_data(v2); \
119
+ g.max = m_sub(high, g.low); \
120
+ } \
121
+ \
122
+ na_ndloop3(&ndf, &g, 1, self); \
123
+ return self; \
124
+ }
125
+
126
+ #define DEF_NARRAY_INT_RAND_METHOD_FUNC(tDType) \
127
+ static int msb_pos(uint32_t a) { \
128
+ const int hwid = 4 * sizeof(tDType); \
129
+ int width = hwid; \
130
+ int pos = 0; \
131
+ uint32_t mask = (((tDType)1 << hwid) - 1) << hwid; \
132
+ \
133
+ if (a == 0) { \
134
+ return -1; \
135
+ } \
136
+ \
137
+ while (width) { \
138
+ if (a & mask) { \
139
+ pos += width; \
140
+ } else { \
141
+ mask >>= width; \
142
+ } \
143
+ width >>= 1; \
144
+ mask &= mask << width; \
145
+ } \
146
+ return pos; \
147
+ } \
148
+ \
149
+ inline static tDType m_rand(uint32_t max, int shift) { \
150
+ uint32_t x; \
151
+ do { \
152
+ x = gen_rand32(); \
153
+ x >>= shift; \
154
+ } while (x >= max); \
155
+ return x; \
156
+ } \
157
+ \
158
+ typedef struct { \
159
+ tDType low; \
160
+ uint32_t max; \
161
+ } rand_opt_t; \
162
+ \
163
+ static void iter_##tDType##_rand(na_loop_t* const lp) { \
164
+ size_t n; \
165
+ char* p1; \
166
+ ssize_t s1; \
167
+ size_t* idx1; \
168
+ tDType x; \
169
+ rand_opt_t* g; \
170
+ tDType low; \
171
+ uint32_t max; \
172
+ int shift; \
173
+ \
174
+ INIT_COUNTER(lp, n); \
175
+ INIT_PTR_IDX(lp, 0, p1, s1, idx1); \
176
+ g = (rand_opt_t*)(lp->opt_ptr); \
177
+ low = g->low; \
178
+ max = g->max; \
179
+ shift = 31 - msb_pos(max); \
180
+ \
181
+ if (idx1) { \
182
+ for (size_t i = 0; i < n; i++) { \
183
+ x = m_add(m_rand(max, shift), low); \
184
+ SET_DATA_INDEX(p1, idx1, tDType, x); \
185
+ } \
186
+ } else { \
187
+ for (size_t i = 0; i < n; i++) { \
188
+ x = m_add(m_rand(max, shift), low); \
189
+ SET_DATA_STRIDE(p1, s1, tDType, x); \
190
+ } \
191
+ } \
192
+ } \
193
+ \
194
+ static VALUE tDType##_rand(int argc, VALUE* argv, VALUE self) { \
195
+ rand_opt_t g; \
196
+ VALUE v1 = Qnil; \
197
+ VALUE v2 = Qnil; \
198
+ tDType high; \
199
+ ndfunc_arg_in_t ain[1] = { { OVERWRITE, 0 } }; \
200
+ ndfunc_t ndf = { iter_##tDType##_rand, FULL_LOOP, 1, 0, ain, 0 }; \
201
+ \
202
+ rb_scan_args(argc, argv, "11", &v1, &v2); \
203
+ if (v2 == Qnil) { \
204
+ g.low = m_zero; \
205
+ g.max = high = m_num_to_data(v1); \
206
+ } else { \
207
+ g.low = m_num_to_data(v1); \
208
+ high = m_num_to_data(v2); \
209
+ g.max = m_sub(high, g.low); \
210
+ } \
211
+ \
212
+ if (high <= g.low) { \
213
+ rb_raise(rb_eArgError, "high must be larger than low"); \
214
+ } \
215
+ \
216
+ na_ndloop3(&ndf, &g, 1, self); \
217
+ return self; \
218
+ }
219
+
220
+ #define DEF_NARRAY_INT64_RAND_METHOD_FUNC(tDType) \
221
+ static int msb_pos(uint64_t a) { \
222
+ const int hwid = 4 * sizeof(tDType); \
223
+ int width = hwid; \
224
+ int pos = 0; \
225
+ uint64_t mask = (((tDType)1 << hwid) - 1) << hwid; \
226
+ \
227
+ if (a == 0) { \
228
+ return -1; \
229
+ } \
230
+ \
231
+ while (width) { \
232
+ if (a & mask) { \
233
+ pos += width; \
234
+ } else { \
235
+ mask >>= width; \
236
+ } \
237
+ width >>= 1; \
238
+ mask &= mask << width; \
239
+ } \
240
+ return pos; \
241
+ } \
242
+ \
243
+ inline static tDType m_rand(uint64_t max, int shift) { \
244
+ uint64_t x; \
245
+ do { \
246
+ x = gen_rand32(); \
247
+ x = (x << 32) | gen_rand32(); \
248
+ x >>= shift; \
249
+ } while (x >= max); \
250
+ return x; \
251
+ } \
252
+ \
253
+ typedef struct { \
254
+ tDType low; \
255
+ uint64_t max; \
256
+ } rand_opt_t; \
257
+ \
258
+ static void iter_##tDType##_rand(na_loop_t* const lp) { \
259
+ size_t n; \
260
+ char* p1; \
261
+ ssize_t s1; \
262
+ size_t* idx1; \
263
+ tDType x; \
264
+ rand_opt_t* g; \
265
+ tDType low; \
266
+ uint64_t max; \
267
+ int shift; \
268
+ \
269
+ INIT_COUNTER(lp, n); \
270
+ INIT_PTR_IDX(lp, 0, p1, s1, idx1); \
271
+ g = (rand_opt_t*)(lp->opt_ptr); \
272
+ low = g->low; \
273
+ max = g->max; \
274
+ shift = 63 - msb_pos(max); \
275
+ \
276
+ if (idx1) { \
277
+ for (size_t i = 0; i < n; i++) { \
278
+ x = m_add(m_rand(max, shift), low); \
279
+ SET_DATA_INDEX(p1, idx1, tDType, x); \
280
+ } \
281
+ } else { \
282
+ for (size_t i = 0; i < n; i++) { \
283
+ x = m_add(m_rand(max, shift), low); \
284
+ SET_DATA_STRIDE(p1, s1, tDType, x); \
285
+ } \
286
+ } \
287
+ } \
288
+ \
289
+ static VALUE tDType##_rand(int argc, VALUE* argv, VALUE self) { \
290
+ rand_opt_t g; \
291
+ VALUE v1 = Qnil; \
292
+ VALUE v2 = Qnil; \
293
+ tDType high; \
294
+ ndfunc_arg_in_t ain[1] = { { OVERWRITE, 0 } }; \
295
+ ndfunc_t ndf = { iter_##tDType##_rand, FULL_LOOP, 1, 0, ain, 0 }; \
296
+ \
297
+ rb_scan_args(argc, argv, "11", &v1, &v2); \
298
+ if (v2 == Qnil) { \
299
+ g.low = m_zero; \
300
+ g.max = high = m_num_to_data(v1); \
301
+ } else { \
302
+ g.low = m_num_to_data(v1); \
303
+ high = m_num_to_data(v2); \
304
+ g.max = m_sub(high, g.low); \
305
+ } \
306
+ \
307
+ if (high <= g.low) { \
308
+ rb_raise(rb_eArgError, "high must be larger than low"); \
309
+ } \
310
+ \
311
+ na_ndloop3(&ndf, &g, 1, self); \
312
+ return self; \
313
+ }
314
+
315
+ #endif /* NUMO_NARRAY_MH_RAND_H */
@@ -0,0 +1,130 @@
1
+ #ifndef NUMO_NARRAY_MH_SEQ_H
2
+ #define NUMO_NARRAY_MH_SEQ_H 1
3
+
4
+ #define DEF_NARRAY_FLT_SEQ_METHOD_FUNC(tDType) \
5
+ typedef struct { \
6
+ tDType beg; \
7
+ tDType step; \
8
+ double count; \
9
+ } seq_opt_t; \
10
+ \
11
+ static void iter_##tDType##_seq(na_loop_t* const lp) { \
12
+ size_t n; \
13
+ char* p1; \
14
+ ssize_t s1; \
15
+ size_t* idx1; \
16
+ tDType x; \
17
+ tDType beg; \
18
+ tDType step; \
19
+ double c; \
20
+ seq_opt_t* g; \
21
+ \
22
+ INIT_COUNTER(lp, n); \
23
+ INIT_PTR_IDX(lp, 0, p1, s1, idx1); \
24
+ g = (seq_opt_t*)(lp->opt_ptr); \
25
+ beg = g->beg; \
26
+ step = g->step; \
27
+ c = g->count; \
28
+ if (idx1) { \
29
+ for (size_t i = 0; i < n; i++) { \
30
+ x = f_seq(beg, step, c++); \
31
+ *(tDType*)(p1 + *idx1) = x; \
32
+ idx1++; \
33
+ } \
34
+ } else { \
35
+ for (size_t i = 0; i < n; i++) { \
36
+ x = f_seq(beg, step, c++); \
37
+ *(tDType*)(p1) = x; \
38
+ p1 += s1; \
39
+ } \
40
+ } \
41
+ g->count = c; \
42
+ } \
43
+ \
44
+ static VALUE tDType##_seq(int argc, VALUE* args, VALUE self) { \
45
+ seq_opt_t* g; \
46
+ VALUE vbeg = Qnil; \
47
+ VALUE vstep = Qnil; \
48
+ ndfunc_arg_in_t ain[1] = { { OVERWRITE, 0 } }; \
49
+ ndfunc_t ndf = { iter_##tDType##_seq, FULL_LOOP, 1, 0, ain, 0 }; \
50
+ \
51
+ g = ALLOCA_N(seq_opt_t, 1); \
52
+ g->beg = m_zero; \
53
+ g->step = m_one; \
54
+ g->count = 0; \
55
+ rb_scan_args(argc, args, "02", &vbeg, &vstep); \
56
+ if (vbeg != Qnil) { \
57
+ g->beg = m_num_to_data(vbeg); \
58
+ } \
59
+ if (vstep != Qnil) { \
60
+ g->step = m_num_to_data(vstep); \
61
+ } \
62
+ \
63
+ na_ndloop3(&ndf, g, 1, self); \
64
+ return self; \
65
+ }
66
+
67
+ #define DEF_NARRAY_INT_SEQ_METHOD_FUNC(tDType) \
68
+ typedef struct { \
69
+ double beg; \
70
+ double step; \
71
+ double count; \
72
+ } seq_opt_t; \
73
+ \
74
+ static void iter_##tDType##_seq(na_loop_t* const lp) { \
75
+ size_t n; \
76
+ char* p1; \
77
+ ssize_t s1; \
78
+ size_t* idx1; \
79
+ tDType x; \
80
+ double beg; \
81
+ double step; \
82
+ double c; \
83
+ seq_opt_t* g; \
84
+ \
85
+ INIT_COUNTER(lp, n); \
86
+ INIT_PTR_IDX(lp, 0, p1, s1, idx1); \
87
+ g = (seq_opt_t*)(lp->opt_ptr); \
88
+ beg = g->beg; \
89
+ step = g->step; \
90
+ c = g->count; \
91
+ if (idx1) { \
92
+ for (size_t i = 0; i < n; i++) { \
93
+ x = f_seq(beg, step, c++); \
94
+ *(tDType*)(p1 + *idx1) = x; \
95
+ idx1++; \
96
+ } \
97
+ } else { \
98
+ for (size_t i = 0; i < n; i++) { \
99
+ x = f_seq(beg, step, c++); \
100
+ *(tDType*)(p1) = x; \
101
+ p1 += s1; \
102
+ } \
103
+ } \
104
+ g->count = c; \
105
+ } \
106
+ \
107
+ static VALUE tDType##_seq(int argc, VALUE* argv, VALUE self) { \
108
+ seq_opt_t* g; \
109
+ VALUE vbeg = Qnil; \
110
+ VALUE vstep = Qnil; \
111
+ ndfunc_arg_in_t ain[1] = { { OVERWRITE, 0 } }; \
112
+ ndfunc_t ndf = { iter_##tDType##_seq, FULL_LOOP, 1, 0, ain, 0 }; \
113
+ \
114
+ g = ALLOCA_N(seq_opt_t, 1); \
115
+ g->beg = m_zero; \
116
+ g->step = m_one; \
117
+ g->count = 0; \
118
+ rb_scan_args(argc, argv, "02", &vbeg, &vstep); \
119
+ if (vbeg != Qnil) { \
120
+ g->beg = NUM2DBL(vbeg); \
121
+ } \
122
+ if (vstep != Qnil) { \
123
+ g->step = NUM2DBL(vstep); \
124
+ } \
125
+ \
126
+ na_ndloop3(&ndf, g, 1, self); \
127
+ return self; \
128
+ }
129
+
130
+ #endif /* NUMO_NARRAY_MH_SEQ_H */
@@ -0,0 +1,69 @@
1
+ #ifndef NUMO_NARRAY_MH_SUM_H
2
+ #define NUMO_NARRAY_MH_SUM_H 1
3
+
4
+ #define DEF_NARRAY_FLT_SUM_METHOD_FUNC(tDType, tNAryClass) \
5
+ static void iter_##tDType##_sum(na_loop_t* const lp) { \
6
+ size_t n; \
7
+ char* p1; \
8
+ char* p2; \
9
+ ssize_t s1; \
10
+ \
11
+ INIT_COUNTER(lp, n); \
12
+ INIT_PTR(lp, 0, p1, s1); \
13
+ p2 = NDL_PTR(lp, 1); \
14
+ \
15
+ *(tDType*)p2 = f_sum(n, p1, s1); \
16
+ } \
17
+ \
18
+ static void iter_##tDType##_sum_nan(na_loop_t* const lp) { \
19
+ size_t n; \
20
+ char* p1; \
21
+ char* p2; \
22
+ ssize_t s1; \
23
+ \
24
+ INIT_COUNTER(lp, n); \
25
+ INIT_PTR(lp, 0, p1, s1); \
26
+ p2 = NDL_PTR(lp, 1); \
27
+ \
28
+ *(tDType*)p2 = f_sum_nan(n, p1, s1); \
29
+ } \
30
+ \
31
+ static VALUE tDType##_sum(int argc, VALUE* argv, VALUE self) { \
32
+ ndfunc_arg_in_t ain[2] = { { tNAryClass, 0 }, { sym_reduce, 0 } }; \
33
+ ndfunc_arg_out_t aout[1] = { { tNAryClass, 0 } }; \
34
+ ndfunc_t ndf = { \
35
+ iter_##tDType##_sum, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE, 2, 1, ain, aout \
36
+ }; \
37
+ VALUE reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, iter_##tDType##_sum_nan); \
38
+ VALUE v = na_ndloop(&ndf, 2, self, reduce); \
39
+ \
40
+ return rb_funcall(v, rb_intern("extract"), 0); \
41
+ }
42
+
43
+ #define DEF_NARRAY_INT_SUM_METHOD_FUNC(tDType, tNAryClass, tRtDType, tRtNAryClass) \
44
+ static void iter_##tDType##_sum(na_loop_t* const lp) { \
45
+ size_t n; \
46
+ char* p1; \
47
+ char* p2; \
48
+ ssize_t s1; \
49
+ \
50
+ INIT_COUNTER(lp, n); \
51
+ INIT_PTR(lp, 0, p1, s1); \
52
+ p2 = NDL_PTR(lp, 1); \
53
+ \
54
+ *(tRtDType*)p2 = f_sum(n, p1, s1); \
55
+ } \
56
+ \
57
+ static VALUE tDType##_sum(int argc, VALUE* argv, VALUE self) { \
58
+ ndfunc_arg_in_t ain[2] = { { tNAryClass, 0 }, { sym_reduce, 0 } }; \
59
+ ndfunc_arg_out_t aout[1] = { { tRtNAryClass, 0 } }; \
60
+ ndfunc_t ndf = { \
61
+ iter_##tDType##_sum, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE, 2, 1, ain, aout \
62
+ }; \
63
+ VALUE reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0); \
64
+ VALUE v = na_ndloop(&ndf, 2, self, reduce); \
65
+ \
66
+ return rb_funcall(v, rb_intern("extract"), 0); \
67
+ }
68
+
69
+ #endif /* NUMO_NARRAY_MH_SUM_H */