numo-narray-alt 0.9.3 → 0.9.5
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.
- checksums.yaml +4 -4
- data/Gemfile +6 -0
- data/README.md +9 -3
- data/Rakefile +26 -6
- data/ext/numo/narray/SFMT-params19937.h +16 -12
- data/ext/numo/narray/SFMT.c +12 -5
- data/ext/numo/narray/array.c +25 -19
- data/ext/numo/narray/data.c +74 -70
- data/ext/numo/narray/extconf.rb +1 -0
- data/ext/numo/narray/index.c +54 -29
- data/ext/numo/narray/kwargs.c +11 -9
- data/ext/numo/narray/math.c +4 -2
- data/ext/numo/narray/narray.c +17 -10
- data/ext/numo/narray/ndloop.c +52 -63
- data/ext/numo/narray/numo/intern.h +9 -3
- data/ext/numo/narray/numo/narray.h +20 -20
- data/ext/numo/narray/numo/ndloop.h +1 -1
- data/ext/numo/narray/numo/template.h +85 -81
- data/ext/numo/narray/numo/types/complex.h +7 -3
- data/ext/numo/narray/numo/types/complex_macro.h +27 -25
- data/ext/numo/narray/numo/types/float_macro.h +20 -17
- data/ext/numo/narray/numo/types/real_accum.h +22 -22
- data/ext/numo/narray/numo/types/robj_macro.h +19 -12
- data/ext/numo/narray/numo/types/xint_macro.h +9 -8
- data/ext/numo/narray/src/t_bit.c +97 -88
- data/ext/numo/narray/src/t_dcomplex.c +336 -307
- data/ext/numo/narray/src/t_dfloat.c +522 -456
- data/ext/numo/narray/src/t_int16.c +351 -308
- data/ext/numo/narray/src/t_int32.c +351 -308
- data/ext/numo/narray/src/t_int64.c +351 -308
- data/ext/numo/narray/src/t_int8.c +309 -288
- data/ext/numo/narray/src/t_mean.c +105 -0
- data/ext/numo/narray/src/t_robject.c +323 -296
- data/ext/numo/narray/src/t_scomplex.c +327 -302
- data/ext/numo/narray/src/t_sfloat.c +515 -451
- data/ext/numo/narray/src/t_uint16.c +351 -308
- data/ext/numo/narray/src/t_uint32.c +351 -308
- data/ext/numo/narray/src/t_uint64.c +351 -308
- data/ext/numo/narray/src/t_uint8.c +311 -288
- data/ext/numo/narray/step.c +23 -2
- data/ext/numo/narray/struct.c +24 -22
- data/lib/numo/narray/extra.rb +66 -25
- data/numo-narray-alt.gemspec +38 -0
- metadata +8 -2
data/ext/numo/narray/index.c
CHANGED
@@ -71,7 +71,10 @@ static void na_index_set_step(na_index_arg_t* q, int i, size_t n, size_t beg, ss
|
|
71
71
|
}
|
72
72
|
|
73
73
|
static void na_index_set_scalar(na_index_arg_t* q, int i, ssize_t size, ssize_t x) {
|
74
|
-
if (x < -size || x >= size)
|
74
|
+
if (x < -size || x >= size)
|
75
|
+
rb_raise(
|
76
|
+
rb_eRangeError, "array index (%" SZF "d) is out of array size (%" SZF "d)", x, size
|
77
|
+
);
|
75
78
|
if (x < 0) x += size;
|
76
79
|
q->n = 1;
|
77
80
|
q->beg = x;
|
@@ -172,7 +175,8 @@ static void na_parse_narray_index(VALUE a, int orig_dim, ssize_t size, na_index_
|
|
172
175
|
q->orig_dim = orig_dim;
|
173
176
|
}
|
174
177
|
|
175
|
-
static void
|
178
|
+
static void
|
179
|
+
na_parse_range(VALUE range, ssize_t step, int orig_dim, ssize_t size, na_index_arg_t* q) {
|
176
180
|
int n;
|
177
181
|
ssize_t beg, end, beg_orig, end_orig;
|
178
182
|
const char *dot = "..", *edot = "...";
|
@@ -192,7 +196,9 @@ static void na_parse_range(VALUE range, ssize_t step, int orig_dim, ssize_t size
|
|
192
196
|
dot = edot;
|
193
197
|
}
|
194
198
|
if (beg < 0 || beg >= size) {
|
195
|
-
rb_raise(
|
199
|
+
rb_raise(
|
200
|
+
rb_eRangeError, "%" SZF "d%s is out of range for size=%" SZF "d", beg_orig, dot, size
|
201
|
+
);
|
196
202
|
}
|
197
203
|
} else {
|
198
204
|
end = end_orig = NUM2SSIZET(x.end);
|
@@ -204,7 +210,10 @@ static void na_parse_range(VALUE range, ssize_t step, int orig_dim, ssize_t size
|
|
204
210
|
dot = edot;
|
205
211
|
}
|
206
212
|
if (beg < 0 || beg >= size || end < 0 || end >= size) {
|
207
|
-
rb_raise(
|
213
|
+
rb_raise(
|
214
|
+
rb_eRangeError, "%" SZF "d%s%" SZF "d is out of range for size=%" SZF "d", beg_orig,
|
215
|
+
dot, end_orig, size
|
216
|
+
);
|
208
217
|
}
|
209
218
|
}
|
210
219
|
#else
|
@@ -224,7 +233,10 @@ static void na_parse_range(VALUE range, ssize_t step, int orig_dim, ssize_t size
|
|
224
233
|
dot = edot;
|
225
234
|
}
|
226
235
|
if (beg < 0 || beg >= size || end < 0 || end >= size) {
|
227
|
-
rb_raise(
|
236
|
+
rb_raise(
|
237
|
+
rb_eRangeError, "%" SZF "d%s%" SZF "d is out of range for size=%" SZF "d", beg_orig, dot,
|
238
|
+
end_orig, size
|
239
|
+
);
|
228
240
|
}
|
229
241
|
#endif
|
230
242
|
n = (int)((end - beg) / step + 1);
|
@@ -342,7 +354,8 @@ static void na_index_parse_each(volatile VALUE a, ssize_t size, int i, na_index_
|
|
342
354
|
}
|
343
355
|
}
|
344
356
|
|
345
|
-
static void
|
357
|
+
static void
|
358
|
+
na_at_parse_each(volatile VALUE a, ssize_t size, int i, VALUE* idx, ssize_t stride) {
|
346
359
|
na_index_arg_t q;
|
347
360
|
size_t n, k;
|
348
361
|
ssize_t* index;
|
@@ -444,7 +457,6 @@ static size_t na_index_parse_args(VALUE args, narray_t* na, na_index_arg_t* q, i
|
|
444
457
|
// rest (ellipsis) dimension
|
445
458
|
if (v == Qfalse) {
|
446
459
|
for (l = ndim - (nidx - 1); l > 0; l--) {
|
447
|
-
// printf("i=%d j=%d k=%d l=%d ndim=%d nidx=%d\n",i,j,k,l,ndim,nidx);
|
448
460
|
na_index_parse_each(Qtrue, na->shape[k], k, &q[j]);
|
449
461
|
if (q[j].n > 1) {
|
450
462
|
total *= q[j].n;
|
@@ -473,14 +485,18 @@ static size_t na_index_parse_args(VALUE args, narray_t* na, na_index_arg_t* q, i
|
|
473
485
|
|
474
486
|
static void na_get_strides_nadata(const narray_data_t* na, ssize_t* strides, ssize_t elmsz) {
|
475
487
|
int i = na->base.ndim - 1;
|
476
|
-
|
477
|
-
|
478
|
-
|
488
|
+
if (i >= 0) {
|
489
|
+
strides[i] = elmsz;
|
490
|
+
for (; i > 0; i--) {
|
491
|
+
strides[i - 1] = strides[i] * na->base.shape[i];
|
492
|
+
}
|
479
493
|
}
|
480
494
|
}
|
481
495
|
|
482
|
-
static void na_index_aref_nadata(
|
483
|
-
|
496
|
+
static void na_index_aref_nadata(
|
497
|
+
narray_data_t* na1, narray_view_t* na2, na_index_arg_t* q, ssize_t elmsz, int ndim,
|
498
|
+
int keep_dim
|
499
|
+
) {
|
484
500
|
int i, j;
|
485
501
|
ssize_t size, k, total = 1;
|
486
502
|
ssize_t stride1;
|
@@ -529,8 +545,10 @@ static void na_index_aref_nadata(narray_data_t* na1, narray_view_t* na2, na_inde
|
|
529
545
|
na2->base.size = total;
|
530
546
|
}
|
531
547
|
|
532
|
-
static void na_index_aref_naview(
|
533
|
-
|
548
|
+
static void na_index_aref_naview(
|
549
|
+
narray_view_t* na1, narray_view_t* na2, na_index_arg_t* q, ssize_t elmsz, int ndim,
|
550
|
+
int keep_dim
|
551
|
+
) {
|
534
552
|
int i, j;
|
535
553
|
ssize_t total = 1;
|
536
554
|
|
@@ -777,10 +795,12 @@ static int check_index_count(int argc, int na_ndim, int count_new, int count_res
|
|
777
795
|
case 0:
|
778
796
|
if (argc == 1 && count_new == 0) return 1;
|
779
797
|
if (argc == result_nd) return result_nd;
|
780
|
-
rb_raise(
|
781
|
-
|
782
|
-
|
783
|
-
|
798
|
+
rb_raise(
|
799
|
+
rb_eIndexError,
|
800
|
+
"# of index(=%i) should be "
|
801
|
+
"equal to ndim(=%i) or 1",
|
802
|
+
argc, na_ndim
|
803
|
+
);
|
784
804
|
break;
|
785
805
|
case 1:
|
786
806
|
if (argc - 1 <= result_nd) return result_nd;
|
@@ -792,7 +812,9 @@ static int check_index_count(int argc, int na_ndim, int count_new, int count_res
|
|
792
812
|
return -1;
|
793
813
|
}
|
794
814
|
|
795
|
-
int na_get_result_dimension(
|
815
|
+
int na_get_result_dimension(
|
816
|
+
VALUE self, int argc, VALUE* argv, ssize_t stride, size_t* pos_idx
|
817
|
+
) {
|
796
818
|
int i, j;
|
797
819
|
int count_new = 0;
|
798
820
|
int count_rest = 0;
|
@@ -888,10 +910,12 @@ int na_get_result_dimension(VALUE self, int argc, VALUE* argv, ssize_t stride, s
|
|
888
910
|
return 0;
|
889
911
|
}
|
890
912
|
}
|
891
|
-
rb_raise(
|
892
|
-
|
893
|
-
|
894
|
-
|
913
|
+
rb_raise(
|
914
|
+
rb_eIndexError,
|
915
|
+
"# of index(=%i) should be "
|
916
|
+
"equal to ndim(=%i) or 1",
|
917
|
+
argc, na->ndim
|
918
|
+
);
|
895
919
|
return -1;
|
896
920
|
}
|
897
921
|
|
@@ -934,11 +958,12 @@ static VALUE na_slice(int argc, VALUE* argv, VALUE self) {
|
|
934
958
|
|
935
959
|
/*
|
936
960
|
Multi-dimensional element reference.
|
937
|
-
Returns an element at `dim0`, `dim1`, ... are Numeric indices for each dimension, or returns a
|
938
|
-
if `dim0`, `dim1`, ... includes other than Numeric index, e.g.,
|
961
|
+
Returns an element at `dim0`, `dim1`, ... are Numeric indices for each dimension, or returns a
|
962
|
+
NArray View as a sliced array if `dim0`, `dim1`, ... includes other than Numeric index, e.g.,
|
963
|
+
Range or Array or true.
|
939
964
|
@overload [](dim0,...,dimL)
|
940
|
-
@param [Numeric,Range,Array,Numo::Int32,Numo::Int64,Numo::Bit,TrueClass,FalseClass,Symbol]
|
941
|
-
indices.
|
965
|
+
@param [Numeric,Range,Array,Numo::Int32,Numo::Int64,Numo::Bit,TrueClass,FalseClass,Symbol]
|
966
|
+
dim0,...,dimL multi-dimensional indices.
|
942
967
|
@return [Numeric,Numo::NArray] an element or NArray view.
|
943
968
|
@see #[]=
|
944
969
|
@see #at
|
@@ -978,8 +1003,8 @@ static VALUE na_slice(int argc, VALUE* argv, VALUE self) {
|
|
978
1003
|
Replace element(s) at `dim0`, `dim1`, ... .
|
979
1004
|
Broadcasting mechanism is applied.
|
980
1005
|
@overload []=(dim0,...,dimL,val)
|
981
|
-
@param [Numeric,Range,Array,Numo::Int32,Numo::Int64,Numo::Bit,TrueClass,FalseClass,Symbol]
|
982
|
-
indices.
|
1006
|
+
@param [Numeric,Range,Array,Numo::Int32,Numo::Int64,Numo::Bit,TrueClass,FalseClass,Symbol]
|
1007
|
+
dim0,...,dimL multi-dimensional indices.
|
983
1008
|
@param [Numeric,Numo::NArray,Array] val Value(s) to be set to self.
|
984
1009
|
@return [Numeric,Numo::NArray,Array] returns `val` (last argument).
|
985
1010
|
@see #[]
|
data/ext/numo/narray/kwargs.c
CHANGED
@@ -15,10 +15,10 @@ struct RBasicRaw {
|
|
15
15
|
VALUE klass;
|
16
16
|
};
|
17
17
|
|
18
|
-
#define RBASIC_SET_CLASS(obj, cls)
|
19
|
-
do {
|
20
|
-
VALUE _obj_ = (obj);
|
21
|
-
RB_OBJ_WRITE(_obj_, &((struct RBasicRaw*)(_obj_))->klass, cls);
|
18
|
+
#define RBASIC_SET_CLASS(obj, cls) \
|
19
|
+
do { \
|
20
|
+
VALUE _obj_ = (obj); \
|
21
|
+
RB_OBJ_WRITE(_obj_, &((struct RBasicRaw*)(_obj_))->klass, cls); \
|
22
22
|
} while (0)
|
23
23
|
|
24
24
|
/* from class.c */
|
@@ -69,7 +69,7 @@ static int separate_symbol(st_data_t key, st_data_t value, st_data_t arg) {
|
|
69
69
|
|
70
70
|
VALUE
|
71
71
|
rb_extract_keywords(VALUE* orighash) {
|
72
|
-
VALUE parthash[2] = {0, 0};
|
72
|
+
VALUE parthash[2] = { 0, 0 };
|
73
73
|
VALUE hash = *orighash;
|
74
74
|
|
75
75
|
if (RHASH_EMPTY_P(hash)) {
|
@@ -84,15 +84,17 @@ rb_extract_keywords(VALUE* orighash) {
|
|
84
84
|
return parthash[0];
|
85
85
|
}
|
86
86
|
|
87
|
-
int rb_get_kwargs(
|
87
|
+
int rb_get_kwargs(
|
88
|
+
VALUE keyword_hash, const ID* table, int required, int optional, VALUE* values
|
89
|
+
) {
|
88
90
|
int i = 0, j;
|
89
91
|
int rest = 0;
|
90
92
|
VALUE missing = Qnil;
|
91
93
|
st_data_t key;
|
92
94
|
|
93
|
-
#define extract_kwarg(keyword, val)
|
94
|
-
(key = (st_data_t)(keyword),
|
95
|
-
|
95
|
+
#define extract_kwarg(keyword, val) \
|
96
|
+
(key = (st_data_t)(keyword), values ? st_delete(rb_hash_tbl_raw(keyword_hash), &key, (val)) \
|
97
|
+
: st_lookup(rb_hash_tbl_raw(keyword_hash), key, (val)))
|
96
98
|
|
97
99
|
if (NIL_P(keyword_hash)) keyword_hash = 0;
|
98
100
|
|
data/ext/numo/narray/math.c
CHANGED
@@ -40,8 +40,10 @@ static VALUE nary_math_cast2(VALUE type1, VALUE type2) {
|
|
40
40
|
if (RTEST(rb_class_inherited_p(type2, cNArray))) {
|
41
41
|
return nary_type_s_upcast(type2, type1);
|
42
42
|
}
|
43
|
-
if (RTEST(rb_class_inherited_p(type1, rb_cNumeric)) &&
|
44
|
-
|
43
|
+
if (RTEST(rb_class_inherited_p(type1, rb_cNumeric)) &&
|
44
|
+
RTEST(rb_class_inherited_p(type2, rb_cNumeric))) {
|
45
|
+
if (RTEST(rb_class_inherited_p(type1, rb_cComplex)) ||
|
46
|
+
RTEST(rb_class_inherited_p(type2, rb_cComplex))) {
|
45
47
|
return rb_cComplex;
|
46
48
|
}
|
47
49
|
return rb_cFloat;
|
data/ext/numo/narray/narray.c
CHANGED
@@ -77,6 +77,8 @@ void Init_nary_math();
|
|
77
77
|
void Init_nary_rand();
|
78
78
|
void Init_nary_array();
|
79
79
|
void Init_nary_struct();
|
80
|
+
// define mean method for integer array
|
81
|
+
void Init_nary_mean();
|
80
82
|
|
81
83
|
const rb_data_type_t na_data_type = {
|
82
84
|
"Numo::NArray",
|
@@ -857,7 +859,8 @@ void na_copy_flags(VALUE src, VALUE dst) {
|
|
857
859
|
na2->flag[0] = na1->flag[0];
|
858
860
|
// na2->flag[1] = NA_FL1_INIT;
|
859
861
|
|
860
|
-
RBASIC(dst)->flags |= (RBASIC(src)->flags) & (FL_USER1 | FL_USER2 | FL_USER3 | FL_USER4 |
|
862
|
+
RBASIC(dst)->flags |= (RBASIC(src)->flags) & (FL_USER1 | FL_USER2 | FL_USER3 | FL_USER4 |
|
863
|
+
FL_USER5 | FL_USER6 | FL_USER7);
|
861
864
|
}
|
862
865
|
|
863
866
|
// fix name, ex, allow_stride_for_flatten_view
|
@@ -1423,10 +1426,12 @@ static VALUE nary_marshal_load(VALUE self, VALUE a) {
|
|
1423
1426
|
rb_raise(rb_eArgError, "marshal array size should be 4");
|
1424
1427
|
}
|
1425
1428
|
if (RARRAY_AREF(a, 0) != INT2FIX(1)) {
|
1426
|
-
rb_raise(
|
1427
|
-
|
1428
|
-
|
1429
|
-
|
1429
|
+
rb_raise(
|
1430
|
+
rb_eArgError,
|
1431
|
+
"NArray marshal version %d is not supported "
|
1432
|
+
"(only version 1)",
|
1433
|
+
NUM2INT(RARRAY_AREF(a, 0))
|
1434
|
+
);
|
1430
1435
|
}
|
1431
1436
|
na_initialize(self, RARRAY_AREF(a, 1));
|
1432
1437
|
NA_FL0_SET(self, FIX2INT(RARRAY_AREF(a, 2)));
|
@@ -1538,7 +1543,6 @@ static VALUE na_get_reduce_flag_from_axes(VALUE na_obj, VALUE axes) {
|
|
1538
1543
|
narg = RARRAY_LEN(axes);
|
1539
1544
|
for (i = 0; i < narg; i++) {
|
1540
1545
|
v = RARRAY_AREF(axes, i);
|
1541
|
-
// printf("argv[%d]=",i);rb_p(v);
|
1542
1546
|
if (TYPE(v) == T_FIXNUM) {
|
1543
1547
|
beg = FIX2INT(v);
|
1544
1548
|
if (beg < 0) beg += ndim;
|
@@ -1547,7 +1551,6 @@ static VALUE na_get_reduce_flag_from_axes(VALUE na_obj, VALUE axes) {
|
|
1547
1551
|
}
|
1548
1552
|
len = 1;
|
1549
1553
|
step = 0;
|
1550
|
-
// printf("beg=%d step=%d len=%d\n",beg,step,len);
|
1551
1554
|
} else if (rb_obj_is_kind_of(v, rb_cRange)
|
1552
1555
|
#ifdef HAVE_RB_ARITHMETIC_SEQUENCE_EXTRACT
|
1553
1556
|
|| rb_obj_is_kind_of(v, rb_cArithSeq)
|
@@ -1611,12 +1614,14 @@ nary_reduce_options(VALUE axes, VALUE* opts, int naryc, VALUE* naryv, ndfunc_t*
|
|
1611
1614
|
}
|
1612
1615
|
|
1613
1616
|
VALUE
|
1614
|
-
nary_reduce_dimension(
|
1617
|
+
nary_reduce_dimension(
|
1618
|
+
int argc, VALUE* argv, int naryc, VALUE* naryv, ndfunc_t* ndf, na_iter_func_t iter_nan
|
1619
|
+
) {
|
1615
1620
|
long narg;
|
1616
1621
|
VALUE axes;
|
1617
1622
|
VALUE kw_hash = Qnil;
|
1618
|
-
ID kw_table[3] = {id_axis, id_keepdims, id_nan};
|
1619
|
-
VALUE opts[3] = {Qundef, Qundef, Qundef};
|
1623
|
+
ID kw_table[3] = { id_axis, id_keepdims, id_nan };
|
1624
|
+
VALUE opts[3] = { Qundef, Qundef, Qundef };
|
1620
1625
|
|
1621
1626
|
narg = rb_scan_args(argc, argv, "*:", &axes, &kw_hash);
|
1622
1627
|
rb_get_kwargs(kw_hash, kw_table, 0, 3, opts);
|
@@ -1968,6 +1973,8 @@ void Init_narray(void) {
|
|
1968
1973
|
Init_numo_bit();
|
1969
1974
|
Init_numo_robject();
|
1970
1975
|
|
1976
|
+
Init_nary_mean();
|
1977
|
+
|
1971
1978
|
Init_nary_math();
|
1972
1979
|
|
1973
1980
|
Init_nary_rand();
|