numo-narray-alt 0.9.4 → 0.9.6
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 +3 -0
- data/README.md +22 -3
- data/Rakefile +8 -0
- data/ext/numo/narray/SFMT-params19937.h +16 -12
- data/ext/numo/narray/SFMT.c +12 -5
- data/ext/numo/narray/array.c +51 -21
- data/ext/numo/narray/data.c +88 -86
- data/ext/numo/narray/index.c +51 -28
- data/ext/numo/narray/kwargs.c +11 -9
- data/ext/numo/narray/math.c +14 -6
- data/ext/numo/narray/narray.c +93 -58
- 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/bit.h +76 -0
- data/ext/numo/narray/numo/types/complex.h +7 -3
- data/ext/numo/narray/numo/types/complex_macro.h +28 -25
- data/ext/numo/narray/numo/types/float_macro.h +21 -17
- data/ext/numo/narray/numo/types/real_accum.h +22 -22
- data/ext/numo/narray/numo/types/robj_macro.h +20 -12
- data/ext/numo/narray/numo/types/xint_macro.h +51 -8
- data/ext/numo/narray/rand.c +7 -0
- data/ext/numo/narray/src/mh/mean.h +102 -0
- data/ext/numo/narray/src/mh/rms.h +102 -0
- data/ext/numo/narray/src/mh/stddev.h +103 -0
- data/ext/numo/narray/src/mh/var.h +102 -0
- data/ext/numo/narray/src/t_bit.c +206 -147
- data/ext/numo/narray/src/t_dcomplex.c +531 -641
- data/ext/numo/narray/src/t_dfloat.c +1341 -1421
- data/ext/numo/narray/src/t_int16.c +562 -468
- data/ext/numo/narray/src/t_int32.c +562 -468
- data/ext/numo/narray/src/t_int64.c +561 -467
- data/ext/numo/narray/src/t_int8.c +520 -448
- data/ext/numo/narray/src/t_robject.c +519 -619
- data/ext/numo/narray/src/t_scomplex.c +524 -659
- data/ext/numo/narray/src/t_sfloat.c +1332 -1410
- data/ext/numo/narray/src/t_uint16.c +562 -468
- data/ext/numo/narray/src/t_uint32.c +562 -468
- data/ext/numo/narray/src/t_uint64.c +562 -468
- data/ext/numo/narray/src/t_uint8.c +522 -448
- data/ext/numo/narray/step.c +7 -2
- data/ext/numo/narray/struct.c +31 -24
- data/lib/numo/narray/extra.rb +74 -30
- data/numo-narray-alt.gemspec +38 -0
- metadata +10 -1
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;
|
@@ -481,8 +493,10 @@ static void na_get_strides_nadata(const narray_data_t* na, ssize_t* strides, ssi
|
|
481
493
|
}
|
482
494
|
}
|
483
495
|
|
484
|
-
static void na_index_aref_nadata(
|
485
|
-
|
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
|
+
) {
|
486
500
|
int i, j;
|
487
501
|
ssize_t size, k, total = 1;
|
488
502
|
ssize_t stride1;
|
@@ -531,8 +545,10 @@ static void na_index_aref_nadata(narray_data_t* na1, narray_view_t* na2, na_inde
|
|
531
545
|
na2->base.size = total;
|
532
546
|
}
|
533
547
|
|
534
|
-
static void na_index_aref_naview(
|
535
|
-
|
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
|
+
) {
|
536
552
|
int i, j;
|
537
553
|
ssize_t total = 1;
|
538
554
|
|
@@ -779,10 +795,12 @@ static int check_index_count(int argc, int na_ndim, int count_new, int count_res
|
|
779
795
|
case 0:
|
780
796
|
if (argc == 1 && count_new == 0) return 1;
|
781
797
|
if (argc == result_nd) return result_nd;
|
782
|
-
rb_raise(
|
783
|
-
|
784
|
-
|
785
|
-
|
798
|
+
rb_raise(
|
799
|
+
rb_eIndexError,
|
800
|
+
"# of index(=%i) should be "
|
801
|
+
"equal to ndim(=%i) or 1",
|
802
|
+
argc, na_ndim
|
803
|
+
);
|
786
804
|
break;
|
787
805
|
case 1:
|
788
806
|
if (argc - 1 <= result_nd) return result_nd;
|
@@ -794,7 +812,9 @@ static int check_index_count(int argc, int na_ndim, int count_new, int count_res
|
|
794
812
|
return -1;
|
795
813
|
}
|
796
814
|
|
797
|
-
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
|
+
) {
|
798
818
|
int i, j;
|
799
819
|
int count_new = 0;
|
800
820
|
int count_rest = 0;
|
@@ -890,10 +910,12 @@ int na_get_result_dimension(VALUE self, int argc, VALUE* argv, ssize_t stride, s
|
|
890
910
|
return 0;
|
891
911
|
}
|
892
912
|
}
|
893
|
-
rb_raise(
|
894
|
-
|
895
|
-
|
896
|
-
|
913
|
+
rb_raise(
|
914
|
+
rb_eIndexError,
|
915
|
+
"# of index(=%i) should be "
|
916
|
+
"equal to ndim(=%i) or 1",
|
917
|
+
argc, na->ndim
|
918
|
+
);
|
897
919
|
return -1;
|
898
920
|
}
|
899
921
|
|
@@ -936,11 +958,12 @@ static VALUE na_slice(int argc, VALUE* argv, VALUE self) {
|
|
936
958
|
|
937
959
|
/*
|
938
960
|
Multi-dimensional element reference.
|
939
|
-
Returns an element at `dim0`, `dim1`, ... are Numeric indices for each dimension, or returns a
|
940
|
-
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.
|
941
964
|
@overload [](dim0,...,dimL)
|
942
|
-
@param [Numeric,Range,Array,Numo::Int32,Numo::Int64,Numo::Bit,TrueClass,FalseClass,Symbol]
|
943
|
-
indices.
|
965
|
+
@param [Numeric,Range,Array,Numo::Int32,Numo::Int64,Numo::Bit,TrueClass,FalseClass,Symbol]
|
966
|
+
dim0,...,dimL multi-dimensional indices.
|
944
967
|
@return [Numeric,Numo::NArray] an element or NArray view.
|
945
968
|
@see #[]=
|
946
969
|
@see #at
|
@@ -980,8 +1003,8 @@ static VALUE na_slice(int argc, VALUE* argv, VALUE self) {
|
|
980
1003
|
Replace element(s) at `dim0`, `dim1`, ... .
|
981
1004
|
Broadcasting mechanism is applied.
|
982
1005
|
@overload []=(dim0,...,dimL,val)
|
983
|
-
@param [Numeric,Range,Array,Numo::Int32,Numo::Int64,Numo::Bit,TrueClass,FalseClass,Symbol]
|
984
|
-
indices.
|
1006
|
+
@param [Numeric,Range,Array,Numo::Int32,Numo::Int64,Numo::Bit,TrueClass,FalseClass,Symbol]
|
1007
|
+
dim0,...,dimL multi-dimensional indices.
|
985
1008
|
@param [Numeric,Numo::NArray,Array] val Value(s) to be set to self.
|
986
1009
|
@return [Numeric,Numo::NArray,Array] returns `val` (last argument).
|
987
1010
|
@see #[]
|
@@ -1022,8 +1045,8 @@ static VALUE na_slice(int argc, VALUE* argv, VALUE self) {
|
|
1022
1045
|
Similar to numpy's tuple indexing, i.e., `a[[1,2,..],[3,4,..]]`
|
1023
1046
|
Same as Numo::NArray#[] for one-dimensional NArray.
|
1024
1047
|
@overload at(dim0,...,dimL)
|
1025
|
-
|
1026
|
-
|
1048
|
+
@param [Range,Array,Numo::Int32,Numo::Int64] dim0,...,dimL multi-dimensional index arrays.
|
1049
|
+
@return [Numo::NArray] one-dimensional NArray view.
|
1027
1050
|
@see #[]
|
1028
1051
|
|
1029
1052
|
@example
|
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;
|
@@ -70,9 +72,9 @@ static VALUE nary_mathcast(int argc, VALUE* argv) {
|
|
70
72
|
Dispatches method to Math module of upcasted type,
|
71
73
|
eg, Numo::DFloat::Math.
|
72
74
|
@overload method_missing(name,x,...)
|
73
|
-
|
74
|
-
|
75
|
-
|
75
|
+
@param [Symbol] name method name.
|
76
|
+
@param [NArray,Numeric] x input array.
|
77
|
+
@return [NArray] result.
|
76
78
|
*/
|
77
79
|
static VALUE nary_math_method_missing(int argc, VALUE* argv, VALUE mod) {
|
78
80
|
VALUE type, ans, typemod, hash;
|
@@ -99,11 +101,15 @@ static VALUE nary_math_method_missing(int argc, VALUE* argv, VALUE mod) {
|
|
99
101
|
void Init_nary_math(void) {
|
100
102
|
VALUE hCast;
|
101
103
|
|
104
|
+
/**
|
105
|
+
* Document-module: Numo::NMath
|
106
|
+
*
|
107
|
+
* This module provides mathematical functions for NArray.
|
108
|
+
*/
|
102
109
|
numo_mNMath = rb_define_module_under(mNumo, "NMath");
|
103
110
|
rb_define_singleton_method(numo_mNMath, "method_missing", nary_math_method_missing, -1);
|
104
111
|
|
105
112
|
hCast = rb_hash_new();
|
106
|
-
rb_define_const(numo_mNMath, "DISPATCH", hCast);
|
107
113
|
rb_hash_aset(hCast, numo_cInt64, numo_mDFloatMath);
|
108
114
|
rb_hash_aset(hCast, numo_cInt32, numo_mDFloatMath);
|
109
115
|
rb_hash_aset(hCast, numo_cInt16, numo_mDFloatMath);
|
@@ -125,6 +131,8 @@ void Init_nary_math(void) {
|
|
125
131
|
#endif
|
126
132
|
rb_hash_aset(hCast, rb_cFloat, rb_mMath);
|
127
133
|
rb_hash_aset(hCast, rb_cComplex, numo_mDComplexMath);
|
134
|
+
/* Dispatch table representing the corresponding Math module. */
|
135
|
+
rb_define_const(numo_mNMath, "DISPATCH", hCast);
|
128
136
|
|
129
137
|
id_send = rb_intern("send");
|
130
138
|
id_UPCAST = rb_intern("UPCAST");
|
data/ext/numo/narray/narray.c
CHANGED
@@ -321,9 +321,9 @@ static void na_setup(VALUE self, int ndim, size_t* shape) {
|
|
321
321
|
/*
|
322
322
|
@overload initialize(shape)
|
323
323
|
@overload initialize(size0, size1, ...)
|
324
|
-
|
325
|
-
|
326
|
-
|
324
|
+
@param [Array] shape (array of sizes along each dimension)
|
325
|
+
@param [Integer] sizeN (size along Nth-dimension)
|
326
|
+
@return [Numo::NArray] unallocated narray.
|
327
327
|
|
328
328
|
Constructs an instance of NArray class using the given
|
329
329
|
and <i>shape</i> or <i>sizes</i>.
|
@@ -398,8 +398,8 @@ nary_view_new(VALUE klass, int ndim, size_t* shape) {
|
|
398
398
|
Replaces the contents of self with the contents of other narray.
|
399
399
|
Used in dup and clone method.
|
400
400
|
@overload initialize_copy(other)
|
401
|
-
|
402
|
-
|
401
|
+
@param [Numo::NArray] other
|
402
|
+
@return [Numo::NArray] self
|
403
403
|
*/
|
404
404
|
static VALUE na_initialize_copy(VALUE self, VALUE orig) {
|
405
405
|
narray_t* na;
|
@@ -459,10 +459,10 @@ static VALUE na_s_ones(int argc, VALUE* argv, VALUE klass) {
|
|
459
459
|
but for typed NArray subclasses, e.g., DFloat, Int64.
|
460
460
|
|
461
461
|
@overload linspace(x1, x2, [n])
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
462
|
+
@param [Numeric] x1 The start value
|
463
|
+
@param [Numeric] x2 The end value
|
464
|
+
@param [Integer] n The number of elements. (default is 100).
|
465
|
+
@return [Numo::NArray] result array.
|
466
466
|
|
467
467
|
@example
|
468
468
|
a = Numo::DFloat.linspace(-5,5,7)
|
@@ -495,11 +495,11 @@ static VALUE na_s_linspace(int argc, VALUE* argv, VALUE klass) {
|
|
495
495
|
i.e., DFloat, SFloat, DComplex, and SComplex.
|
496
496
|
|
497
497
|
@overload logspace(a, b, [n, base])
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
502
|
-
|
498
|
+
@param [Numeric] a The start value
|
499
|
+
@param [Numeric] b The end value
|
500
|
+
@param [Integer] n The number of elements. (default is 50)
|
501
|
+
@param [Numeric] base The base of log space. (default is 10)
|
502
|
+
@return [Numo::NArray] result array.
|
503
503
|
|
504
504
|
@example
|
505
505
|
Numo::DFloat.logspace(4,0,5,2)
|
@@ -535,8 +535,8 @@ static VALUE na_s_logspace(int argc, VALUE* argv, VALUE klass) {
|
|
535
535
|
/*
|
536
536
|
Returns a NArray with shape=(n,n) whose diagonal elements are 1, otherwise 0.
|
537
537
|
@overload eye(n)
|
538
|
-
|
539
|
-
|
538
|
+
@param [Integer] n Size of NArray. Creates 2-D NArray with shape=(n,n)
|
539
|
+
@return [Numo::NArray] created NArray.
|
540
540
|
@example
|
541
541
|
a = Numo::DFloat.eye(3)
|
542
542
|
# => Numo::DFloat#shape=[3,3]
|
@@ -857,7 +857,8 @@ void na_copy_flags(VALUE src, VALUE dst) {
|
|
857
857
|
na2->flag[0] = na1->flag[0];
|
858
858
|
// na2->flag[1] = NA_FL1_INIT;
|
859
859
|
|
860
|
-
RBASIC(dst)->flags |= (RBASIC(src)->flags) & (FL_USER1 | FL_USER2 | FL_USER3 | FL_USER4 |
|
860
|
+
RBASIC(dst)->flags |= (RBASIC(src)->flags) & (FL_USER1 | FL_USER2 | FL_USER3 | FL_USER4 |
|
861
|
+
FL_USER5 | FL_USER6 | FL_USER7);
|
861
862
|
}
|
862
863
|
|
863
864
|
// fix name, ex, allow_stride_for_flatten_view
|
@@ -1021,8 +1022,9 @@ na_make_view(VALUE self) {
|
|
1021
1022
|
*
|
1022
1023
|
* Expand the shape of an array. Insert a new axis with size=1
|
1023
1024
|
* at a given dimension.
|
1024
|
-
* @
|
1025
|
-
*
|
1025
|
+
* @overload expand_dims(dim)
|
1026
|
+
* @param [Integer] dim dimension at which new axis is inserted.
|
1027
|
+
* @return [Numo::NArray] result narray view.
|
1026
1028
|
*/
|
1027
1029
|
static VALUE na_expand_dims(VALUE self, VALUE vdim) {
|
1028
1030
|
int i, j, nd, dim;
|
@@ -1188,8 +1190,8 @@ numo_na_upcast(VALUE type1, VALUE type2) {
|
|
1188
1190
|
Note that NArray has distinct UPCAST mechanism.
|
1189
1191
|
Coerce is used for operation between non-NArray and NArray.
|
1190
1192
|
@overload coerce(other)
|
1191
|
-
|
1192
|
-
|
1193
|
+
@param [Object] other numeric object.
|
1194
|
+
@return [Array] NArray-casted [other,self]
|
1193
1195
|
*/
|
1194
1196
|
static VALUE nary_coerce(VALUE x, VALUE y) {
|
1195
1197
|
VALUE type;
|
@@ -1226,9 +1228,9 @@ static VALUE nary_s_byte_size(VALUE type) {
|
|
1226
1228
|
/*
|
1227
1229
|
Returns a new 1-D array initialized from binary raw data in a string.
|
1228
1230
|
@overload from_binary(string,[shape])
|
1229
|
-
|
1230
|
-
|
1231
|
-
|
1231
|
+
@param [String] string Binary raw data.
|
1232
|
+
@param [Array] shape array of integers representing array shape.
|
1233
|
+
@return [Numo::NArray] NArray containing binary data.
|
1232
1234
|
*/
|
1233
1235
|
static VALUE nary_s_from_binary(int argc, VALUE* argv, VALUE type) {
|
1234
1236
|
size_t len, str_len, byte_size;
|
@@ -1301,9 +1303,9 @@ static VALUE nary_s_from_binary(int argc, VALUE* argv, VALUE type) {
|
|
1301
1303
|
/*
|
1302
1304
|
Returns a new 1-D array initialized from binary raw data in a string.
|
1303
1305
|
@overload store_binary(string,[offset])
|
1304
|
-
|
1305
|
-
|
1306
|
-
|
1306
|
+
@param [String] string Binary raw data.
|
1307
|
+
@param [Integer] (optional) offset Byte offset in string.
|
1308
|
+
@return [Integer] stored length.
|
1307
1309
|
*/
|
1308
1310
|
static VALUE nary_store_binary(int argc, VALUE* argv, VALUE self) {
|
1309
1311
|
size_t size, str_len, byte_size, offset;
|
@@ -1350,7 +1352,7 @@ static VALUE nary_store_binary(int argc, VALUE* argv, VALUE self) {
|
|
1350
1352
|
/*
|
1351
1353
|
Returns string containing the raw data bytes in NArray.
|
1352
1354
|
@overload to_binary()
|
1353
|
-
|
1355
|
+
@return [String] String object containing binary raw data.
|
1354
1356
|
*/
|
1355
1357
|
static VALUE nary_to_binary(VALUE self) {
|
1356
1358
|
size_t len, offset = 0;
|
@@ -1376,7 +1378,7 @@ static VALUE nary_to_binary(VALUE self) {
|
|
1376
1378
|
/*
|
1377
1379
|
Dump marshal data.
|
1378
1380
|
@overload marshal_dump()
|
1379
|
-
|
1381
|
+
@return [Array] Array containing marshal data.
|
1380
1382
|
*/
|
1381
1383
|
static VALUE nary_marshal_dump(VALUE self) {
|
1382
1384
|
VALUE a;
|
@@ -1410,8 +1412,8 @@ static VALUE na_inplace(VALUE self);
|
|
1410
1412
|
/*
|
1411
1413
|
Load marshal data.
|
1412
1414
|
@overload marshal_load(data)
|
1413
|
-
|
1414
|
-
|
1415
|
+
@param [Array] Array containing marshal data.
|
1416
|
+
@return [nil]
|
1415
1417
|
*/
|
1416
1418
|
static VALUE nary_marshal_load(VALUE self, VALUE a) {
|
1417
1419
|
VALUE v;
|
@@ -1423,10 +1425,12 @@ static VALUE nary_marshal_load(VALUE self, VALUE a) {
|
|
1423
1425
|
rb_raise(rb_eArgError, "marshal array size should be 4");
|
1424
1426
|
}
|
1425
1427
|
if (RARRAY_AREF(a, 0) != INT2FIX(1)) {
|
1426
|
-
rb_raise(
|
1427
|
-
|
1428
|
-
|
1429
|
-
|
1428
|
+
rb_raise(
|
1429
|
+
rb_eArgError,
|
1430
|
+
"NArray marshal version %d is not supported "
|
1431
|
+
"(only version 1)",
|
1432
|
+
NUM2INT(RARRAY_AREF(a, 0))
|
1433
|
+
);
|
1430
1434
|
}
|
1431
1435
|
na_initialize(self, RARRAY_AREF(a, 1));
|
1432
1436
|
NA_FL0_SET(self, FIX2INT(RARRAY_AREF(a, 2)));
|
@@ -1458,8 +1462,8 @@ static VALUE nary_marshal_load(VALUE self, VALUE a) {
|
|
1458
1462
|
/*
|
1459
1463
|
Cast self to another NArray datatype.
|
1460
1464
|
@overload cast_to(datatype)
|
1461
|
-
|
1462
|
-
|
1465
|
+
@param [Class] datatype NArray datatype.
|
1466
|
+
@return [Numo::NArray]
|
1463
1467
|
*/
|
1464
1468
|
static VALUE nary_cast_to(VALUE obj, VALUE type) {
|
1465
1469
|
return rb_funcall(type, id_cast, 1, obj);
|
@@ -1538,7 +1542,6 @@ static VALUE na_get_reduce_flag_from_axes(VALUE na_obj, VALUE axes) {
|
|
1538
1542
|
narg = RARRAY_LEN(axes);
|
1539
1543
|
for (i = 0; i < narg; i++) {
|
1540
1544
|
v = RARRAY_AREF(axes, i);
|
1541
|
-
// printf("argv[%d]=",i);rb_p(v);
|
1542
1545
|
if (TYPE(v) == T_FIXNUM) {
|
1543
1546
|
beg = FIX2INT(v);
|
1544
1547
|
if (beg < 0) beg += ndim;
|
@@ -1547,7 +1550,6 @@ static VALUE na_get_reduce_flag_from_axes(VALUE na_obj, VALUE axes) {
|
|
1547
1550
|
}
|
1548
1551
|
len = 1;
|
1549
1552
|
step = 0;
|
1550
|
-
// printf("beg=%d step=%d len=%d\n",beg,step,len);
|
1551
1553
|
} else if (rb_obj_is_kind_of(v, rb_cRange)
|
1552
1554
|
#ifdef HAVE_RB_ARITHMETIC_SEQUENCE_EXTRACT
|
1553
1555
|
|| rb_obj_is_kind_of(v, rb_cArithSeq)
|
@@ -1611,12 +1613,14 @@ nary_reduce_options(VALUE axes, VALUE* opts, int naryc, VALUE* naryv, ndfunc_t*
|
|
1611
1613
|
}
|
1612
1614
|
|
1613
1615
|
VALUE
|
1614
|
-
nary_reduce_dimension(
|
1616
|
+
nary_reduce_dimension(
|
1617
|
+
int argc, VALUE* argv, int naryc, VALUE* naryv, ndfunc_t* ndf, na_iter_func_t iter_nan
|
1618
|
+
) {
|
1615
1619
|
long narg;
|
1616
1620
|
VALUE axes;
|
1617
1621
|
VALUE kw_hash = Qnil;
|
1618
|
-
ID kw_table[3] = {id_axis, id_keepdims, id_nan};
|
1619
|
-
VALUE opts[3] = {Qundef, Qundef, Qundef};
|
1622
|
+
ID kw_table[3] = { id_axis, id_keepdims, id_nan };
|
1623
|
+
VALUE opts[3] = { Qundef, Qundef, Qundef };
|
1620
1624
|
|
1621
1625
|
narg = rb_scan_args(argc, argv, "*:", &axes, &kw_hash);
|
1622
1626
|
rb_get_kwargs(kw_hash, kw_table, 0, 3, opts);
|
@@ -1727,7 +1731,7 @@ static VALUE na_profile_set(VALUE mod, VALUE val) {
|
|
1727
1731
|
/*
|
1728
1732
|
Returns the number of rows used for NArray#inspect
|
1729
1733
|
@overload inspect_rows
|
1730
|
-
|
1734
|
+
@return [Integer or nil] the number of rows.
|
1731
1735
|
*/
|
1732
1736
|
static VALUE na_inspect_rows(VALUE mod) {
|
1733
1737
|
if (numo_na_inspect_rows > 0) {
|
@@ -1740,8 +1744,8 @@ static VALUE na_inspect_rows(VALUE mod) {
|
|
1740
1744
|
/*
|
1741
1745
|
Set the number of rows used for NArray#inspect
|
1742
1746
|
@overload inspect_rows=(rows)
|
1743
|
-
|
1744
|
-
|
1747
|
+
@param [Integer or nil] rows the number of rows
|
1748
|
+
@return [nil]
|
1745
1749
|
*/
|
1746
1750
|
static VALUE na_inspect_rows_set(VALUE mod, VALUE num) {
|
1747
1751
|
if (RTEST(num)) {
|
@@ -1755,7 +1759,7 @@ static VALUE na_inspect_rows_set(VALUE mod, VALUE num) {
|
|
1755
1759
|
/*
|
1756
1760
|
Returns the number of cols used for NArray#inspect
|
1757
1761
|
@overload inspect_cols
|
1758
|
-
|
1762
|
+
@return [Integer or nil] the number of cols.
|
1759
1763
|
*/
|
1760
1764
|
static VALUE na_inspect_cols(VALUE mod) {
|
1761
1765
|
if (numo_na_inspect_cols > 0) {
|
@@ -1768,8 +1772,8 @@ static VALUE na_inspect_cols(VALUE mod) {
|
|
1768
1772
|
/*
|
1769
1773
|
Set the number of cols used for NArray#inspect
|
1770
1774
|
@overload inspect_cols=(cols)
|
1771
|
-
|
1772
|
-
|
1775
|
+
@param [Integer or nil] cols the number of cols
|
1776
|
+
@return [nil]
|
1773
1777
|
*/
|
1774
1778
|
static VALUE na_inspect_cols_set(VALUE mod, VALUE num) {
|
1775
1779
|
if (RTEST(num)) {
|
@@ -1784,8 +1788,8 @@ static VALUE na_inspect_cols_set(VALUE mod, VALUE num) {
|
|
1784
1788
|
Equality of self and other in view of numerical array.
|
1785
1789
|
i.e., both arrays have same shape and corresponding elements are equal.
|
1786
1790
|
@overload == other
|
1787
|
-
|
1788
|
-
|
1791
|
+
@param [Object] other
|
1792
|
+
@return [Boolean] true if self and other is equal.
|
1789
1793
|
*/
|
1790
1794
|
static VALUE na_equal(VALUE self, volatile VALUE other) {
|
1791
1795
|
volatile VALUE vbool;
|
@@ -1820,16 +1824,21 @@ void Init_narray(void) {
|
|
1820
1824
|
rb_ext_ractor_safe(true);
|
1821
1825
|
#endif
|
1822
1826
|
|
1827
|
+
/**
|
1828
|
+
* Document-module: Numo
|
1829
|
+
*
|
1830
|
+
* Ruby/Numo (NUmerical MOdules)
|
1831
|
+
*/
|
1823
1832
|
mNumo = rb_define_module("Numo");
|
1824
1833
|
|
1825
|
-
|
1826
|
-
|
1827
|
-
|
1828
|
-
|
1829
|
-
|
1830
|
-
|
1831
|
-
|
1832
|
-
|
1834
|
+
/**
|
1835
|
+
* Document-class: Numo::NArray
|
1836
|
+
*
|
1837
|
+
* Numo::NArray is the abstract super class for
|
1838
|
+
* Numerical N-dimensional Array in the Ruby/Numo module.
|
1839
|
+
* Use Typed Subclasses of NArray (Numo::DFloat, Int32, etc)
|
1840
|
+
* to create data array instances.
|
1841
|
+
*/
|
1833
1842
|
cNArray = rb_define_class_under(mNumo, "NArray", rb_cObject);
|
1834
1843
|
|
1835
1844
|
#ifndef HAVE_RB_CCOMPLEX
|
@@ -1840,12 +1849,38 @@ void Init_narray(void) {
|
|
1840
1849
|
rb_cArithSeq = rb_path2class("Enumerator::ArithmeticSequence");
|
1841
1850
|
#endif
|
1842
1851
|
|
1852
|
+
/* The version of Numo::NArray Alternative. */
|
1843
1853
|
rb_define_const(cNArray, "VERSION", rb_str_new2(NARRAY_VERSION));
|
1844
1854
|
|
1855
|
+
/**
|
1856
|
+
* Document-class: Numo::NArray::CastError
|
1857
|
+
*
|
1858
|
+
* Exception raised when type casting is not possible.
|
1859
|
+
*/
|
1845
1860
|
nary_eCastError = rb_define_class_under(cNArray, "CastError", rb_eStandardError);
|
1861
|
+
/**
|
1862
|
+
* Document-class: Numo::NArray::ShapeError
|
1863
|
+
*
|
1864
|
+
* Exception raised when shape is invalid.
|
1865
|
+
*/
|
1846
1866
|
nary_eShapeError = rb_define_class_under(cNArray, "ShapeError", rb_eStandardError);
|
1867
|
+
/**
|
1868
|
+
* Document-class: Numo::NArray::OperationError
|
1869
|
+
*
|
1870
|
+
* Exception raised when operation is not appropriate.
|
1871
|
+
*/
|
1847
1872
|
nary_eOperationError = rb_define_class_under(cNArray, "OperationError", rb_eStandardError);
|
1873
|
+
/**
|
1874
|
+
* Document-class: Numo::NArray::DimensionError
|
1875
|
+
*
|
1876
|
+
* Exception raised when dimension is invalid.
|
1877
|
+
*/
|
1848
1878
|
nary_eDimensionError = rb_define_class_under(cNArray, "DimensionError", rb_eStandardError);
|
1879
|
+
/**
|
1880
|
+
* Document-class: Numo::NArray::ValueError
|
1881
|
+
*
|
1882
|
+
* Exception raised when occurred value error.
|
1883
|
+
*/
|
1849
1884
|
nary_eValueError = rb_define_class_under(cNArray, "ValueError", rb_eStandardError);
|
1850
1885
|
|
1851
1886
|
rb_define_singleton_method(cNArray, "debug=", na_debug_set, 1);
|