numo-narray-alt 0.9.10 → 0.9.12
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 +0 -1
- data/LICENSE +1 -1
- data/README.md +7 -0
- data/ext/numo/narray/numo/narray.h +2 -2
- data/ext/numo/narray/numo/types/robj_macro.h +1 -1
- data/ext/numo/narray/numo/types/robject.h +1 -1
- data/ext/numo/narray/src/mh/argmax.h +154 -0
- data/ext/numo/narray/src/mh/argmin.h +154 -0
- data/ext/numo/narray/src/mh/bincount.h +233 -0
- data/ext/numo/narray/src/mh/bit/and.h +225 -0
- data/ext/numo/narray/src/mh/bit/left_shift.h +225 -0
- data/ext/numo/narray/src/mh/bit/not.h +173 -0
- data/ext/numo/narray/src/mh/bit/or.h +225 -0
- data/ext/numo/narray/src/mh/bit/right_shift.h +225 -0
- data/ext/numo/narray/src/mh/bit/xor.h +225 -0
- data/ext/numo/narray/src/mh/clip.h +115 -0
- data/ext/numo/narray/src/mh/coerce_cast.h +9 -0
- data/ext/numo/narray/src/mh/comp/binary_func.h +37 -0
- data/ext/numo/narray/src/mh/comp/eq.h +26 -0
- data/ext/numo/narray/src/mh/comp/ge.h +26 -0
- data/ext/numo/narray/src/mh/comp/gt.h +26 -0
- data/ext/numo/narray/src/mh/comp/le.h +26 -0
- data/ext/numo/narray/src/mh/comp/lt.h +26 -0
- data/ext/numo/narray/src/mh/comp/ne.h +26 -0
- data/ext/numo/narray/src/mh/comp/nearly_eq.h +26 -0
- data/ext/numo/narray/src/mh/cumprod.h +98 -0
- data/ext/numo/narray/src/mh/cumsum.h +98 -0
- data/ext/numo/narray/src/mh/divmod.h +142 -0
- data/ext/numo/narray/src/mh/eye.h +82 -0
- data/ext/numo/narray/src/mh/fill.h +94 -0
- data/ext/numo/narray/src/mh/format.h +108 -0
- data/ext/numo/narray/src/mh/format_to_a.h +89 -0
- data/ext/numo/narray/src/mh/inspect.h +33 -0
- data/ext/numo/narray/src/mh/isfinite.h +42 -0
- data/ext/numo/narray/src/mh/isinf.h +42 -0
- data/ext/numo/narray/src/mh/isnan.h +42 -0
- data/ext/numo/narray/src/mh/isneginf.h +42 -0
- data/ext/numo/narray/src/mh/isposinf.h +42 -0
- data/ext/numo/narray/src/mh/logseq.h +69 -0
- data/ext/numo/narray/src/mh/math/acos.h +2 -2
- data/ext/numo/narray/src/mh/math/acosh.h +2 -2
- data/ext/numo/narray/src/mh/math/asin.h +2 -2
- data/ext/numo/narray/src/mh/math/asinh.h +2 -2
- data/ext/numo/narray/src/mh/math/atan.h +2 -2
- data/ext/numo/narray/src/mh/math/atan2.h +3 -3
- data/ext/numo/narray/src/mh/math/atanh.h +2 -2
- data/ext/numo/narray/src/mh/math/cbrt.h +2 -2
- data/ext/numo/narray/src/mh/math/cos.h +2 -2
- data/ext/numo/narray/src/mh/math/cosh.h +2 -2
- data/ext/numo/narray/src/mh/math/erf.h +2 -2
- data/ext/numo/narray/src/mh/math/erfc.h +2 -2
- data/ext/numo/narray/src/mh/math/exp.h +2 -2
- data/ext/numo/narray/src/mh/math/exp10.h +2 -2
- data/ext/numo/narray/src/mh/math/exp2.h +2 -2
- data/ext/numo/narray/src/mh/math/expm1.h +2 -2
- data/ext/numo/narray/src/mh/math/frexp.h +3 -3
- data/ext/numo/narray/src/mh/math/hypot.h +3 -3
- data/ext/numo/narray/src/mh/math/ldexp.h +3 -3
- data/ext/numo/narray/src/mh/math/log.h +2 -2
- data/ext/numo/narray/src/mh/math/log10.h +2 -2
- data/ext/numo/narray/src/mh/math/log1p.h +2 -2
- data/ext/numo/narray/src/mh/math/log2.h +2 -2
- data/ext/numo/narray/src/mh/math/sin.h +2 -2
- data/ext/numo/narray/src/mh/math/sinc.h +2 -2
- data/ext/numo/narray/src/mh/math/sinh.h +2 -2
- data/ext/numo/narray/src/mh/math/sqrt.h +8 -8
- data/ext/numo/narray/src/mh/math/tan.h +2 -2
- data/ext/numo/narray/src/mh/math/tanh.h +2 -2
- data/ext/numo/narray/src/mh/math/unary_func.h +3 -3
- data/ext/numo/narray/src/mh/max.h +69 -0
- data/ext/numo/narray/src/mh/max_index.h +184 -0
- data/ext/numo/narray/src/mh/maximum.h +116 -0
- data/ext/numo/narray/src/mh/min.h +69 -0
- data/ext/numo/narray/src/mh/min_index.h +184 -0
- data/ext/numo/narray/src/mh/minimum.h +116 -0
- data/ext/numo/narray/src/mh/minmax.h +77 -0
- data/ext/numo/narray/src/mh/mulsum.h +185 -0
- data/ext/numo/narray/src/mh/op/add.h +78 -0
- data/ext/numo/narray/src/mh/op/binary_func.h +423 -0
- data/ext/numo/narray/src/mh/op/div.h +118 -0
- data/ext/numo/narray/src/mh/op/mod.h +108 -0
- data/ext/numo/narray/src/mh/op/mul.h +78 -0
- data/ext/numo/narray/src/mh/op/sub.h +78 -0
- data/ext/numo/narray/src/mh/prod.h +69 -0
- data/ext/numo/narray/src/mh/ptp.h +69 -0
- data/ext/numo/narray/src/mh/rand.h +315 -0
- data/ext/numo/narray/src/mh/round/ceil.h +11 -0
- data/ext/numo/narray/src/mh/round/floor.h +11 -0
- data/ext/numo/narray/src/mh/round/rint.h +9 -0
- data/ext/numo/narray/src/mh/round/round.h +11 -0
- data/ext/numo/narray/src/mh/round/trunc.h +11 -0
- data/ext/numo/narray/src/mh/round/unary_func.h +127 -0
- data/ext/numo/narray/src/mh/seq.h +130 -0
- data/ext/numo/narray/src/mh/sum.h +69 -0
- data/ext/numo/narray/src/mh/to_a.h +78 -0
- data/ext/numo/narray/src/t_bit.c +45 -234
- data/ext/numo/narray/src/t_dcomplex.c +608 -2369
- data/ext/numo/narray/src/t_dfloat.c +485 -3736
- data/ext/numo/narray/src/t_int16.c +743 -3444
- data/ext/numo/narray/src/t_int32.c +745 -3445
- data/ext/numo/narray/src/t_int64.c +743 -3446
- data/ext/numo/narray/src/t_int8.c +678 -3040
- data/ext/numo/narray/src/t_robject.c +771 -3548
- data/ext/numo/narray/src/t_scomplex.c +607 -2368
- data/ext/numo/narray/src/t_sfloat.c +440 -3693
- data/ext/numo/narray/src/t_uint16.c +743 -3440
- data/ext/numo/narray/src/t_uint32.c +743 -3440
- data/ext/numo/narray/src/t_uint64.c +743 -3442
- data/ext/numo/narray/src/t_uint8.c +678 -3038
- data/lib/numo/narray.rb +2 -3
- metadata +62 -3
|
@@ -43,6 +43,50 @@ static ID id_to_a;
|
|
|
43
43
|
VALUE cT;
|
|
44
44
|
extern VALUE cRT;
|
|
45
45
|
|
|
46
|
+
#include "mh/coerce_cast.h"
|
|
47
|
+
#include "mh/to_a.h"
|
|
48
|
+
#include "mh/fill.h"
|
|
49
|
+
#include "mh/format.h"
|
|
50
|
+
#include "mh/format_to_a.h"
|
|
51
|
+
#include "mh/inspect.h"
|
|
52
|
+
#include "mh/op/add.h"
|
|
53
|
+
#include "mh/op/sub.h"
|
|
54
|
+
#include "mh/op/mul.h"
|
|
55
|
+
#include "mh/op/div.h"
|
|
56
|
+
#include "mh/op/mod.h"
|
|
57
|
+
#include "mh/divmod.h"
|
|
58
|
+
#include "mh/comp/eq.h"
|
|
59
|
+
#include "mh/comp/ne.h"
|
|
60
|
+
#include "mh/comp/gt.h"
|
|
61
|
+
#include "mh/comp/ge.h"
|
|
62
|
+
#include "mh/comp/lt.h"
|
|
63
|
+
#include "mh/comp/le.h"
|
|
64
|
+
#include "mh/bit/and.h"
|
|
65
|
+
#include "mh/bit/or.h"
|
|
66
|
+
#include "mh/bit/xor.h"
|
|
67
|
+
#include "mh/bit/not.h"
|
|
68
|
+
#include "mh/bit/left_shift.h"
|
|
69
|
+
#include "mh/bit/right_shift.h"
|
|
70
|
+
#include "mh/clip.h"
|
|
71
|
+
#include "mh/sum.h"
|
|
72
|
+
#include "mh/prod.h"
|
|
73
|
+
#include "mh/min.h"
|
|
74
|
+
#include "mh/max.h"
|
|
75
|
+
#include "mh/ptp.h"
|
|
76
|
+
#include "mh/max_index.h"
|
|
77
|
+
#include "mh/min_index.h"
|
|
78
|
+
#include "mh/argmax.h"
|
|
79
|
+
#include "mh/argmin.h"
|
|
80
|
+
#include "mh/maximum.h"
|
|
81
|
+
#include "mh/minimum.h"
|
|
82
|
+
#include "mh/minmax.h"
|
|
83
|
+
#include "mh/bincount.h"
|
|
84
|
+
#include "mh/cumsum.h"
|
|
85
|
+
#include "mh/cumprod.h"
|
|
86
|
+
#include "mh/mulsum.h"
|
|
87
|
+
#include "mh/seq.h"
|
|
88
|
+
#include "mh/eye.h"
|
|
89
|
+
#include "mh/rand.h"
|
|
46
90
|
#include "mh/mean.h"
|
|
47
91
|
#include "mh/var.h"
|
|
48
92
|
#include "mh/stddev.h"
|
|
@@ -50,6 +94,50 @@ extern VALUE cRT;
|
|
|
50
94
|
|
|
51
95
|
typedef u_int8_t uint8; // Type aliases for shorter notation
|
|
52
96
|
// following the codebase naming convention.
|
|
97
|
+
DEF_NARRAY_COERCE_CAST_METHOD_FUNC(uint8)
|
|
98
|
+
DEF_NARRAY_TO_A_METHOD_FUNC(uint8)
|
|
99
|
+
DEF_NARRAY_FILL_METHOD_FUNC(uint8)
|
|
100
|
+
DEF_NARRAY_FORMAT_METHOD_FUNC(uint8)
|
|
101
|
+
DEF_NARRAY_FORMAT_TO_A_METHOD_FUNC(uint8)
|
|
102
|
+
DEF_NARRAY_INSPECT_METHOD_FUNC(uint8)
|
|
103
|
+
DEF_NARRAY_INT8_ADD_METHOD_FUNC(uint8, numo_cUInt8)
|
|
104
|
+
DEF_NARRAY_INT8_SUB_METHOD_FUNC(uint8, numo_cUInt8)
|
|
105
|
+
DEF_NARRAY_INT8_MUL_METHOD_FUNC(uint8, numo_cUInt8)
|
|
106
|
+
DEF_NARRAY_INT8_DIV_METHOD_FUNC(uint8, numo_cUInt8)
|
|
107
|
+
DEF_NARRAY_INT8_MOD_METHOD_FUNC(uint8, numo_cUInt8)
|
|
108
|
+
DEF_NARRAY_INT_DIVMOD_METHOD_FUNC(uint8, numo_cUInt8)
|
|
109
|
+
DEF_NARRAY_EQ_METHOD_FUNC(uint8, numo_cUInt8)
|
|
110
|
+
DEF_NARRAY_NE_METHOD_FUNC(uint8, numo_cUInt8)
|
|
111
|
+
DEF_NARRAY_GT_METHOD_FUNC(uint8, numo_cUInt8)
|
|
112
|
+
DEF_NARRAY_GE_METHOD_FUNC(uint8, numo_cUInt8)
|
|
113
|
+
DEF_NARRAY_LT_METHOD_FUNC(uint8, numo_cUInt8)
|
|
114
|
+
DEF_NARRAY_LE_METHOD_FUNC(uint8, numo_cUInt8)
|
|
115
|
+
DEF_NARRAY_INT8_BIT_AND_METHOD_FUNC(uint8, numo_cUInt8)
|
|
116
|
+
DEF_NARRAY_INT8_BIT_OR_METHOD_FUNC(uint8, numo_cUInt8)
|
|
117
|
+
DEF_NARRAY_INT8_BIT_XOR_METHOD_FUNC(uint8, numo_cUInt8)
|
|
118
|
+
DEF_NARRAY_INT8_BIT_NOT_METHOD_FUNC(uint8, numo_cUInt8)
|
|
119
|
+
DEF_NARRAY_INT8_LEFT_SHIFT_METHOD_FUNC(uint8, numo_cUInt8)
|
|
120
|
+
DEF_NARRAY_INT8_RIGHT_SHIFT_METHOD_FUNC(uint8, numo_cUInt8)
|
|
121
|
+
DEF_NARRAY_CLIP_METHOD_FUNC(uint8, numo_cUInt8)
|
|
122
|
+
DEF_NARRAY_INT_SUM_METHOD_FUNC(uint8, numo_cUInt8, u_int64_t, numo_cUInt64)
|
|
123
|
+
DEF_NARRAY_INT_PROD_METHOD_FUNC(uint8, numo_cUInt8, u_int64_t, numo_cUInt64)
|
|
124
|
+
DEF_NARRAY_INT_MIN_METHOD_FUNC(uint8, numo_cUInt8)
|
|
125
|
+
DEF_NARRAY_INT_MAX_METHOD_FUNC(uint8, numo_cUInt8)
|
|
126
|
+
DEF_NARRAY_INT_PTP_METHOD_FUNC(uint8, numo_cUInt8)
|
|
127
|
+
DEF_NARRAY_INT_MAX_INDEX_METHOD_FUNC(uint8)
|
|
128
|
+
DEF_NARRAY_INT_MIN_INDEX_METHOD_FUNC(uint8)
|
|
129
|
+
DEF_NARRAY_INT_ARGMAX_METHOD_FUNC(uint8)
|
|
130
|
+
DEF_NARRAY_INT_ARGMIN_METHOD_FUNC(uint8)
|
|
131
|
+
DEF_NARRAY_INT_MAXIMUM_METHOD_FUNC(uint8, numo_cUInt8)
|
|
132
|
+
DEF_NARRAY_INT_MINIMUM_METHOD_FUNC(uint8, numo_cUInt8)
|
|
133
|
+
DEF_NARRAY_INT_MINMAX_METHOD_FUNC(uint8, numo_cUInt8)
|
|
134
|
+
DEF_NARRAY_UINT_BINCOUNT_METHOD_FUNC(uint8, numo_cUInt8)
|
|
135
|
+
DEF_NARRAY_INT_CUMSUM_METHOD_FUNC(uint8, numo_cUInt8)
|
|
136
|
+
DEF_NARRAY_INT_CUMPROD_METHOD_FUNC(uint8, numo_cUInt8)
|
|
137
|
+
DEF_NARRAY_INT_MULSUM_METHOD_FUNC(uint8, numo_cUInt8)
|
|
138
|
+
DEF_NARRAY_INT_SEQ_METHOD_FUNC(uint8)
|
|
139
|
+
DEF_NARRAY_EYE_METHOD_FUNC(uint8)
|
|
140
|
+
DEF_NARRAY_INT_RAND_METHOD_FUNC(uint8)
|
|
53
141
|
DEF_NARRAY_INT_MEAN_METHOD_FUNC(uint8, numo_cUInt8)
|
|
54
142
|
DEF_NARRAY_INT_VAR_METHOD_FUNC(uint8, numo_cUInt8)
|
|
55
143
|
DEF_NARRAY_INT_STDDEV_METHOD_FUNC(uint8, numo_cUInt8)
|
|
@@ -1214,204 +1302,6 @@ static VALUE uint8_aset(int argc, VALUE* argv, VALUE self) {
|
|
|
1214
1302
|
return argv[argc];
|
|
1215
1303
|
}
|
|
1216
1304
|
|
|
1217
|
-
/*
|
|
1218
|
-
return NArray with cast to the type of self.
|
|
1219
|
-
@overload coerce_cast(type)
|
|
1220
|
-
@return [nil]
|
|
1221
|
-
*/
|
|
1222
|
-
static VALUE uint8_coerce_cast(VALUE self, VALUE type) {
|
|
1223
|
-
return Qnil;
|
|
1224
|
-
}
|
|
1225
|
-
|
|
1226
|
-
static void iter_uint8_to_a(na_loop_t* const lp) {
|
|
1227
|
-
size_t i, s1;
|
|
1228
|
-
char* p1;
|
|
1229
|
-
size_t* idx1;
|
|
1230
|
-
dtype x;
|
|
1231
|
-
volatile VALUE a, y;
|
|
1232
|
-
|
|
1233
|
-
INIT_COUNTER(lp, i);
|
|
1234
|
-
INIT_PTR_IDX(lp, 0, p1, s1, idx1);
|
|
1235
|
-
a = rb_ary_new2(i);
|
|
1236
|
-
rb_ary_push(lp->args[1].value, a);
|
|
1237
|
-
if (idx1) {
|
|
1238
|
-
for (; i--;) {
|
|
1239
|
-
GET_DATA_INDEX(p1, idx1, dtype, x);
|
|
1240
|
-
y = m_data_to_num(x);
|
|
1241
|
-
rb_ary_push(a, y);
|
|
1242
|
-
}
|
|
1243
|
-
} else {
|
|
1244
|
-
for (; i--;) {
|
|
1245
|
-
GET_DATA_STRIDE(p1, s1, dtype, x);
|
|
1246
|
-
y = m_data_to_num(x);
|
|
1247
|
-
rb_ary_push(a, y);
|
|
1248
|
-
}
|
|
1249
|
-
}
|
|
1250
|
-
}
|
|
1251
|
-
|
|
1252
|
-
/*
|
|
1253
|
-
Convert self to Array.
|
|
1254
|
-
@overload to_a
|
|
1255
|
-
@return [Array]
|
|
1256
|
-
*/
|
|
1257
|
-
static VALUE uint8_to_a(VALUE self) {
|
|
1258
|
-
ndfunc_arg_in_t ain[3] = { { Qnil, 0 }, { sym_loop_opt }, { sym_option } };
|
|
1259
|
-
ndfunc_arg_out_t aout[1] = { { rb_cArray, 0 } }; // dummy?
|
|
1260
|
-
ndfunc_t ndf = { iter_uint8_to_a, FULL_LOOP_NIP, 3, 1, ain, aout };
|
|
1261
|
-
return na_ndloop_cast_narray_to_rarray(&ndf, self, Qnil);
|
|
1262
|
-
}
|
|
1263
|
-
|
|
1264
|
-
static void iter_uint8_fill(na_loop_t* const lp) {
|
|
1265
|
-
size_t i;
|
|
1266
|
-
char* p1;
|
|
1267
|
-
ssize_t s1;
|
|
1268
|
-
size_t* idx1;
|
|
1269
|
-
VALUE x = lp->option;
|
|
1270
|
-
dtype y;
|
|
1271
|
-
INIT_COUNTER(lp, i);
|
|
1272
|
-
INIT_PTR_IDX(lp, 0, p1, s1, idx1);
|
|
1273
|
-
y = m_num_to_data(x);
|
|
1274
|
-
if (idx1) {
|
|
1275
|
-
for (; i--;) {
|
|
1276
|
-
SET_DATA_INDEX(p1, idx1, dtype, y);
|
|
1277
|
-
}
|
|
1278
|
-
} else {
|
|
1279
|
-
for (; i--;) {
|
|
1280
|
-
SET_DATA_STRIDE(p1, s1, dtype, y);
|
|
1281
|
-
}
|
|
1282
|
-
}
|
|
1283
|
-
}
|
|
1284
|
-
|
|
1285
|
-
/*
|
|
1286
|
-
Fill elements with other.
|
|
1287
|
-
@overload fill other
|
|
1288
|
-
@param [Numeric] other
|
|
1289
|
-
@return [Numo::UInt8] self.
|
|
1290
|
-
*/
|
|
1291
|
-
static VALUE uint8_fill(VALUE self, VALUE val) {
|
|
1292
|
-
ndfunc_arg_in_t ain[2] = { { OVERWRITE, 0 }, { sym_option } };
|
|
1293
|
-
ndfunc_t ndf = { iter_uint8_fill, FULL_LOOP, 2, 0, ain, 0 };
|
|
1294
|
-
|
|
1295
|
-
na_ndloop(&ndf, 2, self, val);
|
|
1296
|
-
return self;
|
|
1297
|
-
}
|
|
1298
|
-
|
|
1299
|
-
static VALUE format_uint8(VALUE fmt, dtype* x) {
|
|
1300
|
-
// fix-me
|
|
1301
|
-
char s[48];
|
|
1302
|
-
int n;
|
|
1303
|
-
|
|
1304
|
-
if (NIL_P(fmt)) {
|
|
1305
|
-
n = m_sprintf(s, *x);
|
|
1306
|
-
return rb_str_new(s, n);
|
|
1307
|
-
}
|
|
1308
|
-
return rb_funcall(fmt, '%', 1, m_data_to_num(*x));
|
|
1309
|
-
}
|
|
1310
|
-
|
|
1311
|
-
static void iter_uint8_format(na_loop_t* const lp) {
|
|
1312
|
-
size_t i;
|
|
1313
|
-
char *p1, *p2;
|
|
1314
|
-
ssize_t s1, s2;
|
|
1315
|
-
size_t* idx1;
|
|
1316
|
-
dtype* x;
|
|
1317
|
-
VALUE y;
|
|
1318
|
-
VALUE fmt = lp->option;
|
|
1319
|
-
INIT_COUNTER(lp, i);
|
|
1320
|
-
INIT_PTR_IDX(lp, 0, p1, s1, idx1);
|
|
1321
|
-
INIT_PTR(lp, 1, p2, s2);
|
|
1322
|
-
if (idx1) {
|
|
1323
|
-
for (; i--;) {
|
|
1324
|
-
x = (dtype*)(p1 + *idx1);
|
|
1325
|
-
idx1++;
|
|
1326
|
-
y = format_uint8(fmt, x);
|
|
1327
|
-
SET_DATA_STRIDE(p2, s2, VALUE, y);
|
|
1328
|
-
}
|
|
1329
|
-
} else {
|
|
1330
|
-
for (; i--;) {
|
|
1331
|
-
x = (dtype*)p1;
|
|
1332
|
-
p1 += s1;
|
|
1333
|
-
y = format_uint8(fmt, x);
|
|
1334
|
-
SET_DATA_STRIDE(p2, s2, VALUE, y);
|
|
1335
|
-
}
|
|
1336
|
-
}
|
|
1337
|
-
}
|
|
1338
|
-
|
|
1339
|
-
/*
|
|
1340
|
-
Format elements into strings.
|
|
1341
|
-
@overload format format
|
|
1342
|
-
@param [String] format
|
|
1343
|
-
@return [Numo::RObject] array of formatted strings.
|
|
1344
|
-
*/
|
|
1345
|
-
static VALUE uint8_format(int argc, VALUE* argv, VALUE self) {
|
|
1346
|
-
VALUE fmt = Qnil;
|
|
1347
|
-
|
|
1348
|
-
ndfunc_arg_in_t ain[2] = { { Qnil, 0 }, { sym_option } };
|
|
1349
|
-
ndfunc_arg_out_t aout[1] = { { numo_cRObject, 0 } };
|
|
1350
|
-
ndfunc_t ndf = { iter_uint8_format, FULL_LOOP_NIP, 2, 1, ain, aout };
|
|
1351
|
-
|
|
1352
|
-
rb_scan_args(argc, argv, "01", &fmt);
|
|
1353
|
-
return na_ndloop(&ndf, 2, self, fmt);
|
|
1354
|
-
}
|
|
1355
|
-
|
|
1356
|
-
static void iter_uint8_format_to_a(na_loop_t* const lp) {
|
|
1357
|
-
size_t i;
|
|
1358
|
-
char* p1;
|
|
1359
|
-
ssize_t s1;
|
|
1360
|
-
size_t* idx1;
|
|
1361
|
-
dtype* x;
|
|
1362
|
-
VALUE y;
|
|
1363
|
-
volatile VALUE a;
|
|
1364
|
-
VALUE fmt = lp->option;
|
|
1365
|
-
INIT_COUNTER(lp, i);
|
|
1366
|
-
INIT_PTR_IDX(lp, 0, p1, s1, idx1);
|
|
1367
|
-
a = rb_ary_new2(i);
|
|
1368
|
-
rb_ary_push(lp->args[1].value, a);
|
|
1369
|
-
if (idx1) {
|
|
1370
|
-
for (; i--;) {
|
|
1371
|
-
x = (dtype*)(p1 + *idx1);
|
|
1372
|
-
idx1++;
|
|
1373
|
-
y = format_uint8(fmt, x);
|
|
1374
|
-
rb_ary_push(a, y);
|
|
1375
|
-
}
|
|
1376
|
-
} else {
|
|
1377
|
-
for (; i--;) {
|
|
1378
|
-
x = (dtype*)p1;
|
|
1379
|
-
p1 += s1;
|
|
1380
|
-
y = format_uint8(fmt, x);
|
|
1381
|
-
rb_ary_push(a, y);
|
|
1382
|
-
}
|
|
1383
|
-
}
|
|
1384
|
-
}
|
|
1385
|
-
|
|
1386
|
-
/*
|
|
1387
|
-
Format elements into strings.
|
|
1388
|
-
@overload format_to_a format
|
|
1389
|
-
@param [String] format
|
|
1390
|
-
@return [Array] array of formatted strings.
|
|
1391
|
-
*/
|
|
1392
|
-
static VALUE uint8_format_to_a(int argc, VALUE* argv, VALUE self) {
|
|
1393
|
-
VALUE fmt = Qnil;
|
|
1394
|
-
ndfunc_arg_in_t ain[3] = { { Qnil, 0 }, { sym_loop_opt }, { sym_option } };
|
|
1395
|
-
ndfunc_arg_out_t aout[1] = { { rb_cArray, 0 } }; // dummy?
|
|
1396
|
-
ndfunc_t ndf = { iter_uint8_format_to_a, FULL_LOOP_NIP, 3, 1, ain, aout };
|
|
1397
|
-
|
|
1398
|
-
rb_scan_args(argc, argv, "01", &fmt);
|
|
1399
|
-
return na_ndloop_cast_narray_to_rarray(&ndf, self, fmt);
|
|
1400
|
-
}
|
|
1401
|
-
|
|
1402
|
-
static VALUE iter_uint8_inspect(char* ptr, size_t pos, VALUE fmt) {
|
|
1403
|
-
return format_uint8(fmt, (dtype*)(ptr + pos));
|
|
1404
|
-
}
|
|
1405
|
-
|
|
1406
|
-
/*
|
|
1407
|
-
Returns a string containing a human-readable representation of NArray.
|
|
1408
|
-
@overload inspect
|
|
1409
|
-
@return [String]
|
|
1410
|
-
*/
|
|
1411
|
-
static VALUE uint8_inspect(VALUE ary) {
|
|
1412
|
-
return na_ndloop_inspect(ary, iter_uint8_inspect, Qnil);
|
|
1413
|
-
}
|
|
1414
|
-
|
|
1415
1305
|
static void iter_uint8_each(na_loop_t* const lp) {
|
|
1416
1306
|
size_t i, s1;
|
|
1417
1307
|
char* p1;
|
|
@@ -1721,2949 +1611,304 @@ static VALUE uint8_abs(VALUE self) {
|
|
|
1721
1611
|
return na_ndloop(&ndf, 1, self);
|
|
1722
1612
|
}
|
|
1723
1613
|
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
static void iter_uint8_add(na_loop_t* const lp) {
|
|
1728
|
-
size_t i = 0;
|
|
1729
|
-
size_t n;
|
|
1614
|
+
static void iter_uint8_pow(na_loop_t* const lp) {
|
|
1615
|
+
size_t i;
|
|
1730
1616
|
char *p1, *p2, *p3;
|
|
1731
1617
|
ssize_t s1, s2, s3;
|
|
1732
|
-
|
|
1733
|
-
INIT_COUNTER(lp,
|
|
1618
|
+
dtype x, y;
|
|
1619
|
+
INIT_COUNTER(lp, i);
|
|
1734
1620
|
INIT_PTR(lp, 0, p1, s1);
|
|
1735
1621
|
INIT_PTR(lp, 1, p2, s2);
|
|
1736
1622
|
INIT_PTR(lp, 2, p3, s3);
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
if (s1 == sizeof(dtype) && s3 == sizeof(dtype)) {
|
|
1743
|
-
if (p1 == p3) { // inplace case
|
|
1744
|
-
for (; i < n; i++) {
|
|
1745
|
-
((dtype*)p1)[i] = m_add(((dtype*)p1)[i], *(dtype*)p2);
|
|
1746
|
-
}
|
|
1747
|
-
} else {
|
|
1748
|
-
for (; i < n; i++) {
|
|
1749
|
-
((dtype*)p3)[i] = m_add(((dtype*)p1)[i], *(dtype*)p2);
|
|
1750
|
-
}
|
|
1751
|
-
}
|
|
1752
|
-
} else {
|
|
1753
|
-
for (i = 0; i < n; i++) {
|
|
1754
|
-
*(dtype*)p3 = m_add(*(dtype*)p1, *(dtype*)p2);
|
|
1755
|
-
p1 += s1;
|
|
1756
|
-
p3 += s3;
|
|
1757
|
-
}
|
|
1758
|
-
}
|
|
1759
|
-
} else {
|
|
1760
|
-
if (p1 == p3) { // inplace case
|
|
1761
|
-
for (i = 0; i < n; i++) {
|
|
1762
|
-
check_intdivzero(*(dtype*)p2);
|
|
1763
|
-
*(dtype*)p1 = m_add(*(dtype*)p1, *(dtype*)p2);
|
|
1764
|
-
p1 += s1;
|
|
1765
|
-
p2 += s2;
|
|
1766
|
-
}
|
|
1767
|
-
} else {
|
|
1768
|
-
for (i = 0; i < n; i++) {
|
|
1769
|
-
check_intdivzero(*(dtype*)p2);
|
|
1770
|
-
*(dtype*)p3 = m_add(*(dtype*)p1, *(dtype*)p2);
|
|
1771
|
-
p1 += s1;
|
|
1772
|
-
p2 += s2;
|
|
1773
|
-
p3 += s3;
|
|
1774
|
-
}
|
|
1775
|
-
}
|
|
1623
|
+
for (; i--;) {
|
|
1624
|
+
GET_DATA_STRIDE(p1, s1, dtype, x);
|
|
1625
|
+
GET_DATA_STRIDE(p2, s2, dtype, y);
|
|
1626
|
+
x = m_pow(x, y);
|
|
1627
|
+
SET_DATA_STRIDE(p3, s3, dtype, x);
|
|
1776
1628
|
}
|
|
1629
|
+
}
|
|
1777
1630
|
|
|
1778
|
-
|
|
1779
|
-
|
|
1631
|
+
static void iter_uint8_pow_int32(na_loop_t* const lp) {
|
|
1632
|
+
size_t i;
|
|
1633
|
+
char *p1, *p2, *p3;
|
|
1634
|
+
ssize_t s1, s2, s3;
|
|
1635
|
+
dtype x;
|
|
1636
|
+
int32_t y;
|
|
1637
|
+
INIT_COUNTER(lp, i);
|
|
1638
|
+
INIT_PTR(lp, 0, p1, s1);
|
|
1639
|
+
INIT_PTR(lp, 1, p2, s2);
|
|
1640
|
+
INIT_PTR(lp, 2, p3, s3);
|
|
1641
|
+
for (; i--;) {
|
|
1642
|
+
GET_DATA_STRIDE(p1, s1, dtype, x);
|
|
1643
|
+
GET_DATA_STRIDE(p2, s2, int32_t, y);
|
|
1644
|
+
x = m_pow_int(x, y);
|
|
1645
|
+
SET_DATA_STRIDE(p3, s3, dtype, x);
|
|
1646
|
+
}
|
|
1780
1647
|
}
|
|
1781
|
-
#undef check_intdivzero
|
|
1782
1648
|
|
|
1783
|
-
static VALUE
|
|
1649
|
+
static VALUE uint8_pow_self(VALUE self, VALUE other) {
|
|
1784
1650
|
ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
|
|
1651
|
+
ndfunc_arg_in_t ain_i[2] = { { cT, 0 }, { numo_cInt32, 0 } };
|
|
1785
1652
|
ndfunc_arg_out_t aout[1] = { { cT, 0 } };
|
|
1786
|
-
ndfunc_t ndf = {
|
|
1653
|
+
ndfunc_t ndf = { iter_uint8_pow, STRIDE_LOOP, 2, 1, ain, aout };
|
|
1654
|
+
ndfunc_t ndf_i = { iter_uint8_pow_int32, STRIDE_LOOP, 2, 1, ain_i, aout };
|
|
1787
1655
|
|
|
1788
|
-
|
|
1656
|
+
// fixme : use na.integer?
|
|
1657
|
+
if (FIXNUM_P(other) || rb_obj_is_kind_of(other, numo_cInt32)) {
|
|
1658
|
+
return na_ndloop(&ndf_i, 2, self, other);
|
|
1659
|
+
} else {
|
|
1660
|
+
return na_ndloop(&ndf, 2, self, other);
|
|
1661
|
+
}
|
|
1789
1662
|
}
|
|
1790
1663
|
|
|
1791
1664
|
/*
|
|
1792
|
-
Binary
|
|
1793
|
-
@overload
|
|
1665
|
+
Binary power.
|
|
1666
|
+
@overload ** other
|
|
1794
1667
|
@param [Numo::NArray,Numeric] other
|
|
1795
|
-
@return [Numo::NArray] self
|
|
1668
|
+
@return [Numo::NArray] self to the other-th power.
|
|
1796
1669
|
*/
|
|
1797
|
-
static VALUE
|
|
1670
|
+
static VALUE uint8_pow(VALUE self, VALUE other) {
|
|
1798
1671
|
|
|
1799
1672
|
VALUE klass, v;
|
|
1800
|
-
|
|
1801
1673
|
klass = na_upcast(rb_obj_class(self), rb_obj_class(other));
|
|
1802
1674
|
if (klass == cT) {
|
|
1803
|
-
return
|
|
1675
|
+
return uint8_pow_self(self, other);
|
|
1804
1676
|
} else {
|
|
1805
1677
|
v = rb_funcall(klass, id_cast, 1, self);
|
|
1806
|
-
return rb_funcall(v,
|
|
1678
|
+
return rb_funcall(v, id_pow, 1, other);
|
|
1807
1679
|
}
|
|
1808
1680
|
}
|
|
1809
1681
|
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
size_t
|
|
1815
|
-
|
|
1816
|
-
char *p1, *p2, *p3;
|
|
1817
|
-
ssize_t s1, s2, s3;
|
|
1682
|
+
static void iter_uint8_minus(na_loop_t* const lp) {
|
|
1683
|
+
size_t i, n;
|
|
1684
|
+
char *p1, *p2;
|
|
1685
|
+
ssize_t s1, s2;
|
|
1686
|
+
size_t *idx1, *idx2;
|
|
1687
|
+
dtype x;
|
|
1818
1688
|
|
|
1819
1689
|
INIT_COUNTER(lp, n);
|
|
1820
|
-
|
|
1821
|
-
|
|
1822
|
-
INIT_PTR(lp, 2, p3, s3);
|
|
1823
|
-
|
|
1824
|
-
//
|
|
1690
|
+
INIT_PTR_IDX(lp, 0, p1, s1, idx1);
|
|
1691
|
+
INIT_PTR_IDX(lp, 1, p2, s2, idx2);
|
|
1825
1692
|
|
|
1826
|
-
if (
|
|
1827
|
-
|
|
1828
|
-
|
|
1829
|
-
|
|
1830
|
-
|
|
1831
|
-
|
|
1832
|
-
}
|
|
1833
|
-
} else {
|
|
1834
|
-
for (; i < n; i++) {
|
|
1835
|
-
((dtype*)p3)[i] = m_sub(((dtype*)p1)[i], *(dtype*)p2);
|
|
1836
|
-
}
|
|
1693
|
+
if (idx1) {
|
|
1694
|
+
if (idx2) {
|
|
1695
|
+
for (i = 0; i < n; i++) {
|
|
1696
|
+
GET_DATA_INDEX(p1, idx1, dtype, x);
|
|
1697
|
+
x = m_minus(x);
|
|
1698
|
+
SET_DATA_INDEX(p2, idx2, dtype, x);
|
|
1837
1699
|
}
|
|
1838
1700
|
} else {
|
|
1839
1701
|
for (i = 0; i < n; i++) {
|
|
1840
|
-
|
|
1841
|
-
|
|
1842
|
-
|
|
1702
|
+
GET_DATA_INDEX(p1, idx1, dtype, x);
|
|
1703
|
+
x = m_minus(x);
|
|
1704
|
+
SET_DATA_STRIDE(p2, s2, dtype, x);
|
|
1843
1705
|
}
|
|
1844
1706
|
}
|
|
1845
1707
|
} else {
|
|
1846
|
-
if (
|
|
1708
|
+
if (idx2) {
|
|
1847
1709
|
for (i = 0; i < n; i++) {
|
|
1848
|
-
|
|
1849
|
-
|
|
1850
|
-
|
|
1851
|
-
p2 += s2;
|
|
1710
|
+
GET_DATA_STRIDE(p1, s1, dtype, x);
|
|
1711
|
+
x = m_minus(x);
|
|
1712
|
+
SET_DATA_INDEX(p2, idx2, dtype, x);
|
|
1852
1713
|
}
|
|
1853
1714
|
} else {
|
|
1715
|
+
//
|
|
1854
1716
|
for (i = 0; i < n; i++) {
|
|
1855
|
-
|
|
1856
|
-
*(dtype*)p3 = m_sub(*(dtype*)p1, *(dtype*)p2);
|
|
1717
|
+
*(dtype*)p2 = m_minus(*(dtype*)p1);
|
|
1857
1718
|
p1 += s1;
|
|
1858
1719
|
p2 += s2;
|
|
1859
|
-
p3 += s3;
|
|
1860
1720
|
}
|
|
1721
|
+
return;
|
|
1722
|
+
//
|
|
1861
1723
|
}
|
|
1862
1724
|
}
|
|
1863
|
-
|
|
1864
|
-
return;
|
|
1865
|
-
//
|
|
1866
|
-
}
|
|
1867
|
-
#undef check_intdivzero
|
|
1868
|
-
|
|
1869
|
-
static VALUE uint8_sub_self(VALUE self, VALUE other) {
|
|
1870
|
-
ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
|
|
1871
|
-
ndfunc_arg_out_t aout[1] = { { cT, 0 } };
|
|
1872
|
-
ndfunc_t ndf = { iter_uint8_sub, STRIDE_LOOP, 2, 1, ain, aout };
|
|
1873
|
-
|
|
1874
|
-
return na_ndloop(&ndf, 2, self, other);
|
|
1875
1725
|
}
|
|
1876
1726
|
|
|
1877
1727
|
/*
|
|
1878
|
-
|
|
1879
|
-
@overload
|
|
1880
|
-
@
|
|
1881
|
-
@return [Numo::NArray] self - other
|
|
1728
|
+
Unary minus.
|
|
1729
|
+
@overload -@
|
|
1730
|
+
@return [Numo::UInt8] minus of self.
|
|
1882
1731
|
*/
|
|
1883
|
-
static VALUE
|
|
1884
|
-
|
|
1885
|
-
|
|
1732
|
+
static VALUE uint8_minus(VALUE self) {
|
|
1733
|
+
ndfunc_arg_in_t ain[1] = { { cT, 0 } };
|
|
1734
|
+
ndfunc_arg_out_t aout[1] = { { cT, 0 } };
|
|
1735
|
+
ndfunc_t ndf = { iter_uint8_minus, FULL_LOOP, 1, 1, ain, aout };
|
|
1886
1736
|
|
|
1887
|
-
|
|
1888
|
-
if (klass == cT) {
|
|
1889
|
-
return uint8_sub_self(self, other);
|
|
1890
|
-
} else {
|
|
1891
|
-
v = rb_funcall(klass, id_cast, 1, self);
|
|
1892
|
-
return rb_funcall(v, '-', 1, other);
|
|
1893
|
-
}
|
|
1737
|
+
return na_ndloop(&ndf, 1, self);
|
|
1894
1738
|
}
|
|
1895
1739
|
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
size_t
|
|
1901
|
-
|
|
1902
|
-
char *p1, *p2, *p3;
|
|
1903
|
-
ssize_t s1, s2, s3;
|
|
1740
|
+
static void iter_uint8_reciprocal(na_loop_t* const lp) {
|
|
1741
|
+
size_t i, n;
|
|
1742
|
+
char *p1, *p2;
|
|
1743
|
+
ssize_t s1, s2;
|
|
1744
|
+
size_t *idx1, *idx2;
|
|
1745
|
+
dtype x;
|
|
1904
1746
|
|
|
1905
1747
|
INIT_COUNTER(lp, n);
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
INIT_PTR(lp, 2, p3, s3);
|
|
1909
|
-
|
|
1910
|
-
//
|
|
1748
|
+
INIT_PTR_IDX(lp, 0, p1, s1, idx1);
|
|
1749
|
+
INIT_PTR_IDX(lp, 1, p2, s2, idx2);
|
|
1911
1750
|
|
|
1912
|
-
if (
|
|
1913
|
-
|
|
1914
|
-
if (s1 == sizeof(dtype) && s3 == sizeof(dtype)) {
|
|
1915
|
-
if (p1 == p3) { // inplace case
|
|
1916
|
-
for (; i < n; i++) {
|
|
1917
|
-
((dtype*)p1)[i] = m_mul(((dtype*)p1)[i], *(dtype*)p2);
|
|
1918
|
-
}
|
|
1919
|
-
} else {
|
|
1920
|
-
for (; i < n; i++) {
|
|
1921
|
-
((dtype*)p3)[i] = m_mul(((dtype*)p1)[i], *(dtype*)p2);
|
|
1922
|
-
}
|
|
1923
|
-
}
|
|
1924
|
-
} else {
|
|
1751
|
+
if (idx1) {
|
|
1752
|
+
if (idx2) {
|
|
1925
1753
|
for (i = 0; i < n; i++) {
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
}
|
|
1930
|
-
}
|
|
1931
|
-
} else {
|
|
1932
|
-
if (p1 == p3) { // inplace case
|
|
1933
|
-
for (i = 0; i < n; i++) {
|
|
1934
|
-
check_intdivzero(*(dtype*)p2);
|
|
1935
|
-
*(dtype*)p1 = m_mul(*(dtype*)p1, *(dtype*)p2);
|
|
1936
|
-
p1 += s1;
|
|
1937
|
-
p2 += s2;
|
|
1938
|
-
}
|
|
1939
|
-
} else {
|
|
1940
|
-
for (i = 0; i < n; i++) {
|
|
1941
|
-
check_intdivzero(*(dtype*)p2);
|
|
1942
|
-
*(dtype*)p3 = m_mul(*(dtype*)p1, *(dtype*)p2);
|
|
1943
|
-
p1 += s1;
|
|
1944
|
-
p2 += s2;
|
|
1945
|
-
p3 += s3;
|
|
1946
|
-
}
|
|
1947
|
-
}
|
|
1948
|
-
}
|
|
1949
|
-
|
|
1950
|
-
return;
|
|
1951
|
-
//
|
|
1952
|
-
}
|
|
1953
|
-
#undef check_intdivzero
|
|
1954
|
-
|
|
1955
|
-
static VALUE uint8_mul_self(VALUE self, VALUE other) {
|
|
1956
|
-
ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
|
|
1957
|
-
ndfunc_arg_out_t aout[1] = { { cT, 0 } };
|
|
1958
|
-
ndfunc_t ndf = { iter_uint8_mul, STRIDE_LOOP, 2, 1, ain, aout };
|
|
1959
|
-
|
|
1960
|
-
return na_ndloop(&ndf, 2, self, other);
|
|
1961
|
-
}
|
|
1962
|
-
|
|
1963
|
-
/*
|
|
1964
|
-
Binary mul.
|
|
1965
|
-
@overload * other
|
|
1966
|
-
@param [Numo::NArray,Numeric] other
|
|
1967
|
-
@return [Numo::NArray] self * other
|
|
1968
|
-
*/
|
|
1969
|
-
static VALUE uint8_mul(VALUE self, VALUE other) {
|
|
1970
|
-
|
|
1971
|
-
VALUE klass, v;
|
|
1972
|
-
|
|
1973
|
-
klass = na_upcast(rb_obj_class(self), rb_obj_class(other));
|
|
1974
|
-
if (klass == cT) {
|
|
1975
|
-
return uint8_mul_self(self, other);
|
|
1976
|
-
} else {
|
|
1977
|
-
v = rb_funcall(klass, id_cast, 1, self);
|
|
1978
|
-
return rb_funcall(v, '*', 1, other);
|
|
1979
|
-
}
|
|
1980
|
-
}
|
|
1981
|
-
|
|
1982
|
-
#define check_intdivzero(y) \
|
|
1983
|
-
if ((y) == 0) { \
|
|
1984
|
-
lp->err_type = rb_eZeroDivError; \
|
|
1985
|
-
return; \
|
|
1986
|
-
}
|
|
1987
|
-
|
|
1988
|
-
static void iter_uint8_div(na_loop_t* const lp) {
|
|
1989
|
-
size_t i = 0;
|
|
1990
|
-
size_t n;
|
|
1991
|
-
char *p1, *p2, *p3;
|
|
1992
|
-
ssize_t s1, s2, s3;
|
|
1993
|
-
|
|
1994
|
-
INIT_COUNTER(lp, n);
|
|
1995
|
-
INIT_PTR(lp, 0, p1, s1);
|
|
1996
|
-
INIT_PTR(lp, 1, p2, s2);
|
|
1997
|
-
INIT_PTR(lp, 2, p3, s3);
|
|
1998
|
-
|
|
1999
|
-
//
|
|
2000
|
-
|
|
2001
|
-
if (s2 == 0) { // Broadcasting from scalar value.
|
|
2002
|
-
check_intdivzero(*(dtype*)p2);
|
|
2003
|
-
if (s1 == sizeof(dtype) && s3 == sizeof(dtype)) {
|
|
2004
|
-
if (p1 == p3) { // inplace case
|
|
2005
|
-
for (; i < n; i++) {
|
|
2006
|
-
((dtype*)p1)[i] = m_div(((dtype*)p1)[i], *(dtype*)p2);
|
|
2007
|
-
}
|
|
2008
|
-
} else {
|
|
2009
|
-
for (; i < n; i++) {
|
|
2010
|
-
((dtype*)p3)[i] = m_div(((dtype*)p1)[i], *(dtype*)p2);
|
|
2011
|
-
}
|
|
2012
|
-
}
|
|
2013
|
-
} else {
|
|
2014
|
-
for (i = 0; i < n; i++) {
|
|
2015
|
-
*(dtype*)p3 = m_div(*(dtype*)p1, *(dtype*)p2);
|
|
2016
|
-
p1 += s1;
|
|
2017
|
-
p3 += s3;
|
|
2018
|
-
}
|
|
2019
|
-
}
|
|
2020
|
-
} else {
|
|
2021
|
-
if (p1 == p3) { // inplace case
|
|
2022
|
-
for (i = 0; i < n; i++) {
|
|
2023
|
-
check_intdivzero(*(dtype*)p2);
|
|
2024
|
-
*(dtype*)p1 = m_div(*(dtype*)p1, *(dtype*)p2);
|
|
2025
|
-
p1 += s1;
|
|
2026
|
-
p2 += s2;
|
|
2027
|
-
}
|
|
2028
|
-
} else {
|
|
2029
|
-
for (i = 0; i < n; i++) {
|
|
2030
|
-
check_intdivzero(*(dtype*)p2);
|
|
2031
|
-
*(dtype*)p3 = m_div(*(dtype*)p1, *(dtype*)p2);
|
|
2032
|
-
p1 += s1;
|
|
2033
|
-
p2 += s2;
|
|
2034
|
-
p3 += s3;
|
|
2035
|
-
}
|
|
2036
|
-
}
|
|
2037
|
-
}
|
|
2038
|
-
|
|
2039
|
-
return;
|
|
2040
|
-
//
|
|
2041
|
-
}
|
|
2042
|
-
#undef check_intdivzero
|
|
2043
|
-
|
|
2044
|
-
static VALUE uint8_div_self(VALUE self, VALUE other) {
|
|
2045
|
-
ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
|
|
2046
|
-
ndfunc_arg_out_t aout[1] = { { cT, 0 } };
|
|
2047
|
-
ndfunc_t ndf = { iter_uint8_div, STRIDE_LOOP, 2, 1, ain, aout };
|
|
2048
|
-
|
|
2049
|
-
return na_ndloop(&ndf, 2, self, other);
|
|
2050
|
-
}
|
|
2051
|
-
|
|
2052
|
-
/*
|
|
2053
|
-
Binary div.
|
|
2054
|
-
@overload / other
|
|
2055
|
-
@param [Numo::NArray,Numeric] other
|
|
2056
|
-
@return [Numo::NArray] self / other
|
|
2057
|
-
*/
|
|
2058
|
-
static VALUE uint8_div(VALUE self, VALUE other) {
|
|
2059
|
-
|
|
2060
|
-
VALUE klass, v;
|
|
2061
|
-
|
|
2062
|
-
klass = na_upcast(rb_obj_class(self), rb_obj_class(other));
|
|
2063
|
-
if (klass == cT) {
|
|
2064
|
-
return uint8_div_self(self, other);
|
|
2065
|
-
} else {
|
|
2066
|
-
v = rb_funcall(klass, id_cast, 1, self);
|
|
2067
|
-
return rb_funcall(v, '/', 1, other);
|
|
2068
|
-
}
|
|
2069
|
-
}
|
|
2070
|
-
|
|
2071
|
-
#define check_intdivzero(y) \
|
|
2072
|
-
if ((y) == 0) { \
|
|
2073
|
-
lp->err_type = rb_eZeroDivError; \
|
|
2074
|
-
return; \
|
|
2075
|
-
}
|
|
2076
|
-
|
|
2077
|
-
static void iter_uint8_mod(na_loop_t* const lp) {
|
|
2078
|
-
size_t i = 0;
|
|
2079
|
-
size_t n;
|
|
2080
|
-
char *p1, *p2, *p3;
|
|
2081
|
-
ssize_t s1, s2, s3;
|
|
2082
|
-
|
|
2083
|
-
INIT_COUNTER(lp, n);
|
|
2084
|
-
INIT_PTR(lp, 0, p1, s1);
|
|
2085
|
-
INIT_PTR(lp, 1, p2, s2);
|
|
2086
|
-
INIT_PTR(lp, 2, p3, s3);
|
|
2087
|
-
|
|
2088
|
-
//
|
|
2089
|
-
|
|
2090
|
-
if (s2 == 0) { // Broadcasting from scalar value.
|
|
2091
|
-
check_intdivzero(*(dtype*)p2);
|
|
2092
|
-
if (s1 == sizeof(dtype) && s3 == sizeof(dtype)) {
|
|
2093
|
-
if (p1 == p3) { // inplace case
|
|
2094
|
-
for (; i < n; i++) {
|
|
2095
|
-
((dtype*)p1)[i] = m_mod(((dtype*)p1)[i], *(dtype*)p2);
|
|
2096
|
-
}
|
|
2097
|
-
} else {
|
|
2098
|
-
for (; i < n; i++) {
|
|
2099
|
-
((dtype*)p3)[i] = m_mod(((dtype*)p1)[i], *(dtype*)p2);
|
|
2100
|
-
}
|
|
2101
|
-
}
|
|
2102
|
-
} else {
|
|
2103
|
-
for (i = 0; i < n; i++) {
|
|
2104
|
-
*(dtype*)p3 = m_mod(*(dtype*)p1, *(dtype*)p2);
|
|
2105
|
-
p1 += s1;
|
|
2106
|
-
p3 += s3;
|
|
2107
|
-
}
|
|
2108
|
-
}
|
|
2109
|
-
} else {
|
|
2110
|
-
if (p1 == p3) { // inplace case
|
|
2111
|
-
for (i = 0; i < n; i++) {
|
|
2112
|
-
check_intdivzero(*(dtype*)p2);
|
|
2113
|
-
*(dtype*)p1 = m_mod(*(dtype*)p1, *(dtype*)p2);
|
|
2114
|
-
p1 += s1;
|
|
2115
|
-
p2 += s2;
|
|
1754
|
+
GET_DATA_INDEX(p1, idx1, dtype, x);
|
|
1755
|
+
x = m_reciprocal(x);
|
|
1756
|
+
SET_DATA_INDEX(p2, idx2, dtype, x);
|
|
2116
1757
|
}
|
|
2117
1758
|
} else {
|
|
2118
1759
|
for (i = 0; i < n; i++) {
|
|
2119
|
-
|
|
2120
|
-
|
|
2121
|
-
|
|
2122
|
-
p2 += s2;
|
|
2123
|
-
p3 += s3;
|
|
1760
|
+
GET_DATA_INDEX(p1, idx1, dtype, x);
|
|
1761
|
+
x = m_reciprocal(x);
|
|
1762
|
+
SET_DATA_STRIDE(p2, s2, dtype, x);
|
|
2124
1763
|
}
|
|
2125
1764
|
}
|
|
2126
|
-
}
|
|
2127
|
-
|
|
2128
|
-
return;
|
|
2129
|
-
//
|
|
2130
|
-
}
|
|
2131
|
-
#undef check_intdivzero
|
|
2132
|
-
|
|
2133
|
-
static VALUE uint8_mod_self(VALUE self, VALUE other) {
|
|
2134
|
-
ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
|
|
2135
|
-
ndfunc_arg_out_t aout[1] = { { cT, 0 } };
|
|
2136
|
-
ndfunc_t ndf = { iter_uint8_mod, STRIDE_LOOP, 2, 1, ain, aout };
|
|
2137
|
-
|
|
2138
|
-
return na_ndloop(&ndf, 2, self, other);
|
|
2139
|
-
}
|
|
2140
|
-
|
|
2141
|
-
/*
|
|
2142
|
-
Binary mod.
|
|
2143
|
-
@overload % other
|
|
2144
|
-
@param [Numo::NArray,Numeric] other
|
|
2145
|
-
@return [Numo::NArray] self % other
|
|
2146
|
-
*/
|
|
2147
|
-
static VALUE uint8_mod(VALUE self, VALUE other) {
|
|
2148
|
-
|
|
2149
|
-
VALUE klass, v;
|
|
2150
|
-
|
|
2151
|
-
klass = na_upcast(rb_obj_class(self), rb_obj_class(other));
|
|
2152
|
-
if (klass == cT) {
|
|
2153
|
-
return uint8_mod_self(self, other);
|
|
2154
|
-
} else {
|
|
2155
|
-
v = rb_funcall(klass, id_cast, 1, self);
|
|
2156
|
-
return rb_funcall(v, '%', 1, other);
|
|
2157
|
-
}
|
|
2158
|
-
}
|
|
2159
|
-
|
|
2160
|
-
static void iter_uint8_divmod(na_loop_t* const lp) {
|
|
2161
|
-
size_t i, n;
|
|
2162
|
-
char *p1, *p2, *p3, *p4;
|
|
2163
|
-
ssize_t s1, s2, s3, s4;
|
|
2164
|
-
dtype x, y, a, b;
|
|
2165
|
-
INIT_COUNTER(lp, n);
|
|
2166
|
-
INIT_PTR(lp, 0, p1, s1);
|
|
2167
|
-
INIT_PTR(lp, 1, p2, s2);
|
|
2168
|
-
INIT_PTR(lp, 2, p3, s3);
|
|
2169
|
-
INIT_PTR(lp, 3, p4, s4);
|
|
2170
|
-
for (i = n; i--;) {
|
|
2171
|
-
GET_DATA_STRIDE(p1, s1, dtype, x);
|
|
2172
|
-
GET_DATA_STRIDE(p2, s2, dtype, y);
|
|
2173
|
-
if (y == 0) {
|
|
2174
|
-
lp->err_type = rb_eZeroDivError;
|
|
2175
|
-
return;
|
|
2176
|
-
}
|
|
2177
|
-
m_divmod(x, y, a, b);
|
|
2178
|
-
SET_DATA_STRIDE(p3, s3, dtype, a);
|
|
2179
|
-
SET_DATA_STRIDE(p4, s4, dtype, b);
|
|
2180
|
-
}
|
|
2181
|
-
}
|
|
2182
|
-
|
|
2183
|
-
static VALUE uint8_divmod_self(VALUE self, VALUE other) {
|
|
2184
|
-
ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
|
|
2185
|
-
ndfunc_arg_out_t aout[2] = { { cT, 0 }, { cT, 0 } };
|
|
2186
|
-
ndfunc_t ndf = { iter_uint8_divmod, STRIDE_LOOP, 2, 2, ain, aout };
|
|
2187
|
-
|
|
2188
|
-
return na_ndloop(&ndf, 2, self, other);
|
|
2189
|
-
}
|
|
2190
|
-
|
|
2191
|
-
/*
|
|
2192
|
-
Binary divmod.
|
|
2193
|
-
@overload divmod other
|
|
2194
|
-
@param [Numo::NArray,Numeric] other
|
|
2195
|
-
@return [Numo::NArray] divmod of self and other.
|
|
2196
|
-
*/
|
|
2197
|
-
static VALUE uint8_divmod(VALUE self, VALUE other) {
|
|
2198
|
-
|
|
2199
|
-
VALUE klass, v;
|
|
2200
|
-
klass = na_upcast(rb_obj_class(self), rb_obj_class(other));
|
|
2201
|
-
if (klass == cT) {
|
|
2202
|
-
return uint8_divmod_self(self, other);
|
|
2203
|
-
} else {
|
|
2204
|
-
v = rb_funcall(klass, id_cast, 1, self);
|
|
2205
|
-
return rb_funcall(v, id_divmod, 1, other);
|
|
2206
|
-
}
|
|
2207
|
-
}
|
|
2208
|
-
|
|
2209
|
-
static void iter_uint8_pow(na_loop_t* const lp) {
|
|
2210
|
-
size_t i;
|
|
2211
|
-
char *p1, *p2, *p3;
|
|
2212
|
-
ssize_t s1, s2, s3;
|
|
2213
|
-
dtype x, y;
|
|
2214
|
-
INIT_COUNTER(lp, i);
|
|
2215
|
-
INIT_PTR(lp, 0, p1, s1);
|
|
2216
|
-
INIT_PTR(lp, 1, p2, s2);
|
|
2217
|
-
INIT_PTR(lp, 2, p3, s3);
|
|
2218
|
-
for (; i--;) {
|
|
2219
|
-
GET_DATA_STRIDE(p1, s1, dtype, x);
|
|
2220
|
-
GET_DATA_STRIDE(p2, s2, dtype, y);
|
|
2221
|
-
x = m_pow(x, y);
|
|
2222
|
-
SET_DATA_STRIDE(p3, s3, dtype, x);
|
|
2223
|
-
}
|
|
2224
|
-
}
|
|
2225
|
-
|
|
2226
|
-
static void iter_uint8_pow_int32(na_loop_t* const lp) {
|
|
2227
|
-
size_t i;
|
|
2228
|
-
char *p1, *p2, *p3;
|
|
2229
|
-
ssize_t s1, s2, s3;
|
|
2230
|
-
dtype x;
|
|
2231
|
-
int32_t y;
|
|
2232
|
-
INIT_COUNTER(lp, i);
|
|
2233
|
-
INIT_PTR(lp, 0, p1, s1);
|
|
2234
|
-
INIT_PTR(lp, 1, p2, s2);
|
|
2235
|
-
INIT_PTR(lp, 2, p3, s3);
|
|
2236
|
-
for (; i--;) {
|
|
2237
|
-
GET_DATA_STRIDE(p1, s1, dtype, x);
|
|
2238
|
-
GET_DATA_STRIDE(p2, s2, int32_t, y);
|
|
2239
|
-
x = m_pow_int(x, y);
|
|
2240
|
-
SET_DATA_STRIDE(p3, s3, dtype, x);
|
|
2241
|
-
}
|
|
2242
|
-
}
|
|
2243
|
-
|
|
2244
|
-
static VALUE uint8_pow_self(VALUE self, VALUE other) {
|
|
2245
|
-
ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
|
|
2246
|
-
ndfunc_arg_in_t ain_i[2] = { { cT, 0 }, { numo_cInt32, 0 } };
|
|
2247
|
-
ndfunc_arg_out_t aout[1] = { { cT, 0 } };
|
|
2248
|
-
ndfunc_t ndf = { iter_uint8_pow, STRIDE_LOOP, 2, 1, ain, aout };
|
|
2249
|
-
ndfunc_t ndf_i = { iter_uint8_pow_int32, STRIDE_LOOP, 2, 1, ain_i, aout };
|
|
2250
|
-
|
|
2251
|
-
// fixme : use na.integer?
|
|
2252
|
-
if (FIXNUM_P(other) || rb_obj_is_kind_of(other, numo_cInt32)) {
|
|
2253
|
-
return na_ndloop(&ndf_i, 2, self, other);
|
|
2254
|
-
} else {
|
|
2255
|
-
return na_ndloop(&ndf, 2, self, other);
|
|
2256
|
-
}
|
|
2257
|
-
}
|
|
2258
|
-
|
|
2259
|
-
/*
|
|
2260
|
-
Binary power.
|
|
2261
|
-
@overload ** other
|
|
2262
|
-
@param [Numo::NArray,Numeric] other
|
|
2263
|
-
@return [Numo::NArray] self to the other-th power.
|
|
2264
|
-
*/
|
|
2265
|
-
static VALUE uint8_pow(VALUE self, VALUE other) {
|
|
2266
|
-
|
|
2267
|
-
VALUE klass, v;
|
|
2268
|
-
klass = na_upcast(rb_obj_class(self), rb_obj_class(other));
|
|
2269
|
-
if (klass == cT) {
|
|
2270
|
-
return uint8_pow_self(self, other);
|
|
2271
1765
|
} else {
|
|
2272
|
-
v = rb_funcall(klass, id_cast, 1, self);
|
|
2273
|
-
return rb_funcall(v, id_pow, 1, other);
|
|
2274
|
-
}
|
|
2275
|
-
}
|
|
2276
|
-
|
|
2277
|
-
static void iter_uint8_minus(na_loop_t* const lp) {
|
|
2278
|
-
size_t i, n;
|
|
2279
|
-
char *p1, *p2;
|
|
2280
|
-
ssize_t s1, s2;
|
|
2281
|
-
size_t *idx1, *idx2;
|
|
2282
|
-
dtype x;
|
|
2283
|
-
|
|
2284
|
-
INIT_COUNTER(lp, n);
|
|
2285
|
-
INIT_PTR_IDX(lp, 0, p1, s1, idx1);
|
|
2286
|
-
INIT_PTR_IDX(lp, 1, p2, s2, idx2);
|
|
2287
|
-
|
|
2288
|
-
if (idx1) {
|
|
2289
1766
|
if (idx2) {
|
|
2290
1767
|
for (i = 0; i < n; i++) {
|
|
2291
|
-
|
|
2292
|
-
x =
|
|
1768
|
+
GET_DATA_STRIDE(p1, s1, dtype, x);
|
|
1769
|
+
x = m_reciprocal(x);
|
|
2293
1770
|
SET_DATA_INDEX(p2, idx2, dtype, x);
|
|
2294
1771
|
}
|
|
2295
1772
|
} else {
|
|
2296
|
-
|
|
2297
|
-
GET_DATA_INDEX(p1, idx1, dtype, x);
|
|
2298
|
-
x = m_minus(x);
|
|
2299
|
-
SET_DATA_STRIDE(p2, s2, dtype, x);
|
|
2300
|
-
}
|
|
2301
|
-
}
|
|
2302
|
-
} else {
|
|
2303
|
-
if (idx2) {
|
|
2304
|
-
for (i = 0; i < n; i++) {
|
|
2305
|
-
GET_DATA_STRIDE(p1, s1, dtype, x);
|
|
2306
|
-
x = m_minus(x);
|
|
2307
|
-
SET_DATA_INDEX(p2, idx2, dtype, x);
|
|
2308
|
-
}
|
|
2309
|
-
} else {
|
|
2310
|
-
//
|
|
2311
|
-
for (i = 0; i < n; i++) {
|
|
2312
|
-
*(dtype*)p2 = m_minus(*(dtype*)p1);
|
|
2313
|
-
p1 += s1;
|
|
2314
|
-
p2 += s2;
|
|
2315
|
-
}
|
|
2316
|
-
return;
|
|
2317
|
-
//
|
|
2318
|
-
}
|
|
2319
|
-
}
|
|
2320
|
-
}
|
|
2321
|
-
|
|
2322
|
-
/*
|
|
2323
|
-
Unary minus.
|
|
2324
|
-
@overload -@
|
|
2325
|
-
@return [Numo::UInt8] minus of self.
|
|
2326
|
-
*/
|
|
2327
|
-
static VALUE uint8_minus(VALUE self) {
|
|
2328
|
-
ndfunc_arg_in_t ain[1] = { { cT, 0 } };
|
|
2329
|
-
ndfunc_arg_out_t aout[1] = { { cT, 0 } };
|
|
2330
|
-
ndfunc_t ndf = { iter_uint8_minus, FULL_LOOP, 1, 1, ain, aout };
|
|
2331
|
-
|
|
2332
|
-
return na_ndloop(&ndf, 1, self);
|
|
2333
|
-
}
|
|
2334
|
-
|
|
2335
|
-
static void iter_uint8_reciprocal(na_loop_t* const lp) {
|
|
2336
|
-
size_t i, n;
|
|
2337
|
-
char *p1, *p2;
|
|
2338
|
-
ssize_t s1, s2;
|
|
2339
|
-
size_t *idx1, *idx2;
|
|
2340
|
-
dtype x;
|
|
2341
|
-
|
|
2342
|
-
INIT_COUNTER(lp, n);
|
|
2343
|
-
INIT_PTR_IDX(lp, 0, p1, s1, idx1);
|
|
2344
|
-
INIT_PTR_IDX(lp, 1, p2, s2, idx2);
|
|
2345
|
-
|
|
2346
|
-
if (idx1) {
|
|
2347
|
-
if (idx2) {
|
|
2348
|
-
for (i = 0; i < n; i++) {
|
|
2349
|
-
GET_DATA_INDEX(p1, idx1, dtype, x);
|
|
2350
|
-
x = m_reciprocal(x);
|
|
2351
|
-
SET_DATA_INDEX(p2, idx2, dtype, x);
|
|
2352
|
-
}
|
|
2353
|
-
} else {
|
|
2354
|
-
for (i = 0; i < n; i++) {
|
|
2355
|
-
GET_DATA_INDEX(p1, idx1, dtype, x);
|
|
2356
|
-
x = m_reciprocal(x);
|
|
2357
|
-
SET_DATA_STRIDE(p2, s2, dtype, x);
|
|
2358
|
-
}
|
|
2359
|
-
}
|
|
2360
|
-
} else {
|
|
2361
|
-
if (idx2) {
|
|
2362
|
-
for (i = 0; i < n; i++) {
|
|
2363
|
-
GET_DATA_STRIDE(p1, s1, dtype, x);
|
|
2364
|
-
x = m_reciprocal(x);
|
|
2365
|
-
SET_DATA_INDEX(p2, idx2, dtype, x);
|
|
2366
|
-
}
|
|
2367
|
-
} else {
|
|
2368
|
-
//
|
|
1773
|
+
//
|
|
2369
1774
|
for (i = 0; i < n; i++) {
|
|
2370
1775
|
*(dtype*)p2 = m_reciprocal(*(dtype*)p1);
|
|
2371
1776
|
p1 += s1;
|
|
2372
|
-
p2 += s2;
|
|
2373
|
-
}
|
|
2374
|
-
return;
|
|
2375
|
-
//
|
|
2376
|
-
}
|
|
2377
|
-
}
|
|
2378
|
-
}
|
|
2379
|
-
|
|
2380
|
-
/*
|
|
2381
|
-
Unary reciprocal.
|
|
2382
|
-
@overload reciprocal
|
|
2383
|
-
@return [Numo::UInt8] reciprocal of self.
|
|
2384
|
-
*/
|
|
2385
|
-
static VALUE uint8_reciprocal(VALUE self) {
|
|
2386
|
-
ndfunc_arg_in_t ain[1] = { { cT, 0 } };
|
|
2387
|
-
ndfunc_arg_out_t aout[1] = { { cT, 0 } };
|
|
2388
|
-
ndfunc_t ndf = { iter_uint8_reciprocal, FULL_LOOP, 1, 1, ain, aout };
|
|
2389
|
-
|
|
2390
|
-
return na_ndloop(&ndf, 1, self);
|
|
2391
|
-
}
|
|
2392
|
-
|
|
2393
|
-
static void iter_uint8_sign(na_loop_t* const lp) {
|
|
2394
|
-
size_t i, n;
|
|
2395
|
-
char *p1, *p2;
|
|
2396
|
-
ssize_t s1, s2;
|
|
2397
|
-
size_t *idx1, *idx2;
|
|
2398
|
-
dtype x;
|
|
2399
|
-
|
|
2400
|
-
INIT_COUNTER(lp, n);
|
|
2401
|
-
INIT_PTR_IDX(lp, 0, p1, s1, idx1);
|
|
2402
|
-
INIT_PTR_IDX(lp, 1, p2, s2, idx2);
|
|
2403
|
-
|
|
2404
|
-
if (idx1) {
|
|
2405
|
-
if (idx2) {
|
|
2406
|
-
for (i = 0; i < n; i++) {
|
|
2407
|
-
GET_DATA_INDEX(p1, idx1, dtype, x);
|
|
2408
|
-
x = m_sign(x);
|
|
2409
|
-
SET_DATA_INDEX(p2, idx2, dtype, x);
|
|
2410
|
-
}
|
|
2411
|
-
} else {
|
|
2412
|
-
for (i = 0; i < n; i++) {
|
|
2413
|
-
GET_DATA_INDEX(p1, idx1, dtype, x);
|
|
2414
|
-
x = m_sign(x);
|
|
2415
|
-
SET_DATA_STRIDE(p2, s2, dtype, x);
|
|
2416
|
-
}
|
|
2417
|
-
}
|
|
2418
|
-
} else {
|
|
2419
|
-
if (idx2) {
|
|
2420
|
-
for (i = 0; i < n; i++) {
|
|
2421
|
-
GET_DATA_STRIDE(p1, s1, dtype, x);
|
|
2422
|
-
x = m_sign(x);
|
|
2423
|
-
SET_DATA_INDEX(p2, idx2, dtype, x);
|
|
2424
|
-
}
|
|
2425
|
-
} else {
|
|
2426
|
-
//
|
|
2427
|
-
for (i = 0; i < n; i++) {
|
|
2428
|
-
*(dtype*)p2 = m_sign(*(dtype*)p1);
|
|
2429
|
-
p1 += s1;
|
|
2430
|
-
p2 += s2;
|
|
2431
|
-
}
|
|
2432
|
-
return;
|
|
2433
|
-
//
|
|
2434
|
-
}
|
|
2435
|
-
}
|
|
2436
|
-
}
|
|
2437
|
-
|
|
2438
|
-
/*
|
|
2439
|
-
Unary sign.
|
|
2440
|
-
@overload sign
|
|
2441
|
-
@return [Numo::UInt8] sign of self.
|
|
2442
|
-
*/
|
|
2443
|
-
static VALUE uint8_sign(VALUE self) {
|
|
2444
|
-
ndfunc_arg_in_t ain[1] = { { cT, 0 } };
|
|
2445
|
-
ndfunc_arg_out_t aout[1] = { { cT, 0 } };
|
|
2446
|
-
ndfunc_t ndf = { iter_uint8_sign, FULL_LOOP, 1, 1, ain, aout };
|
|
2447
|
-
|
|
2448
|
-
return na_ndloop(&ndf, 1, self);
|
|
2449
|
-
}
|
|
2450
|
-
|
|
2451
|
-
static void iter_uint8_square(na_loop_t* const lp) {
|
|
2452
|
-
size_t i, n;
|
|
2453
|
-
char *p1, *p2;
|
|
2454
|
-
ssize_t s1, s2;
|
|
2455
|
-
size_t *idx1, *idx2;
|
|
2456
|
-
dtype x;
|
|
2457
|
-
|
|
2458
|
-
INIT_COUNTER(lp, n);
|
|
2459
|
-
INIT_PTR_IDX(lp, 0, p1, s1, idx1);
|
|
2460
|
-
INIT_PTR_IDX(lp, 1, p2, s2, idx2);
|
|
2461
|
-
|
|
2462
|
-
if (idx1) {
|
|
2463
|
-
if (idx2) {
|
|
2464
|
-
for (i = 0; i < n; i++) {
|
|
2465
|
-
GET_DATA_INDEX(p1, idx1, dtype, x);
|
|
2466
|
-
x = m_square(x);
|
|
2467
|
-
SET_DATA_INDEX(p2, idx2, dtype, x);
|
|
2468
|
-
}
|
|
2469
|
-
} else {
|
|
2470
|
-
for (i = 0; i < n; i++) {
|
|
2471
|
-
GET_DATA_INDEX(p1, idx1, dtype, x);
|
|
2472
|
-
x = m_square(x);
|
|
2473
|
-
SET_DATA_STRIDE(p2, s2, dtype, x);
|
|
2474
|
-
}
|
|
2475
|
-
}
|
|
2476
|
-
} else {
|
|
2477
|
-
if (idx2) {
|
|
2478
|
-
for (i = 0; i < n; i++) {
|
|
2479
|
-
GET_DATA_STRIDE(p1, s1, dtype, x);
|
|
2480
|
-
x = m_square(x);
|
|
2481
|
-
SET_DATA_INDEX(p2, idx2, dtype, x);
|
|
2482
|
-
}
|
|
2483
|
-
} else {
|
|
2484
|
-
//
|
|
2485
|
-
for (i = 0; i < n; i++) {
|
|
2486
|
-
*(dtype*)p2 = m_square(*(dtype*)p1);
|
|
2487
|
-
p1 += s1;
|
|
2488
|
-
p2 += s2;
|
|
2489
|
-
}
|
|
2490
|
-
return;
|
|
2491
|
-
//
|
|
2492
|
-
}
|
|
2493
|
-
}
|
|
2494
|
-
}
|
|
2495
|
-
|
|
2496
|
-
/*
|
|
2497
|
-
Unary square.
|
|
2498
|
-
@overload square
|
|
2499
|
-
@return [Numo::UInt8] square of self.
|
|
2500
|
-
*/
|
|
2501
|
-
static VALUE uint8_square(VALUE self) {
|
|
2502
|
-
ndfunc_arg_in_t ain[1] = { { cT, 0 } };
|
|
2503
|
-
ndfunc_arg_out_t aout[1] = { { cT, 0 } };
|
|
2504
|
-
ndfunc_t ndf = { iter_uint8_square, FULL_LOOP, 1, 1, ain, aout };
|
|
2505
|
-
|
|
2506
|
-
return na_ndloop(&ndf, 1, self);
|
|
2507
|
-
}
|
|
2508
|
-
|
|
2509
|
-
static void iter_uint8_eq(na_loop_t* const lp) {
|
|
2510
|
-
size_t i;
|
|
2511
|
-
char *p1, *p2;
|
|
2512
|
-
BIT_DIGIT* a3;
|
|
2513
|
-
size_t p3;
|
|
2514
|
-
ssize_t s1, s2, s3;
|
|
2515
|
-
dtype x, y;
|
|
2516
|
-
BIT_DIGIT b;
|
|
2517
|
-
INIT_COUNTER(lp, i);
|
|
2518
|
-
INIT_PTR(lp, 0, p1, s1);
|
|
2519
|
-
INIT_PTR(lp, 1, p2, s2);
|
|
2520
|
-
INIT_PTR_BIT(lp, 2, a3, p3, s3);
|
|
2521
|
-
for (; i--;) {
|
|
2522
|
-
GET_DATA_STRIDE(p1, s1, dtype, x);
|
|
2523
|
-
GET_DATA_STRIDE(p2, s2, dtype, y);
|
|
2524
|
-
b = (m_eq(x, y)) ? 1 : 0;
|
|
2525
|
-
STORE_BIT(a3, p3, b);
|
|
2526
|
-
p3 += s3;
|
|
2527
|
-
}
|
|
2528
|
-
}
|
|
2529
|
-
|
|
2530
|
-
static VALUE uint8_eq_self(VALUE self, VALUE other) {
|
|
2531
|
-
ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
|
|
2532
|
-
ndfunc_arg_out_t aout[1] = { { numo_cBit, 0 } };
|
|
2533
|
-
ndfunc_t ndf = { iter_uint8_eq, STRIDE_LOOP, 2, 1, ain, aout };
|
|
2534
|
-
|
|
2535
|
-
return na_ndloop(&ndf, 2, self, other);
|
|
2536
|
-
}
|
|
2537
|
-
|
|
2538
|
-
/*
|
|
2539
|
-
Comparison eq other.
|
|
2540
|
-
@overload eq other
|
|
2541
|
-
@param [Numo::NArray,Numeric] other
|
|
2542
|
-
@return [Numo::Bit] result of self eq other.
|
|
2543
|
-
*/
|
|
2544
|
-
static VALUE uint8_eq(VALUE self, VALUE other) {
|
|
2545
|
-
|
|
2546
|
-
VALUE klass, v;
|
|
2547
|
-
klass = na_upcast(rb_obj_class(self), rb_obj_class(other));
|
|
2548
|
-
if (klass == cT) {
|
|
2549
|
-
return uint8_eq_self(self, other);
|
|
2550
|
-
} else {
|
|
2551
|
-
v = rb_funcall(klass, id_cast, 1, self);
|
|
2552
|
-
return rb_funcall(v, id_eq, 1, other);
|
|
2553
|
-
}
|
|
2554
|
-
}
|
|
2555
|
-
|
|
2556
|
-
static void iter_uint8_ne(na_loop_t* const lp) {
|
|
2557
|
-
size_t i;
|
|
2558
|
-
char *p1, *p2;
|
|
2559
|
-
BIT_DIGIT* a3;
|
|
2560
|
-
size_t p3;
|
|
2561
|
-
ssize_t s1, s2, s3;
|
|
2562
|
-
dtype x, y;
|
|
2563
|
-
BIT_DIGIT b;
|
|
2564
|
-
INIT_COUNTER(lp, i);
|
|
2565
|
-
INIT_PTR(lp, 0, p1, s1);
|
|
2566
|
-
INIT_PTR(lp, 1, p2, s2);
|
|
2567
|
-
INIT_PTR_BIT(lp, 2, a3, p3, s3);
|
|
2568
|
-
for (; i--;) {
|
|
2569
|
-
GET_DATA_STRIDE(p1, s1, dtype, x);
|
|
2570
|
-
GET_DATA_STRIDE(p2, s2, dtype, y);
|
|
2571
|
-
b = (m_ne(x, y)) ? 1 : 0;
|
|
2572
|
-
STORE_BIT(a3, p3, b);
|
|
2573
|
-
p3 += s3;
|
|
2574
|
-
}
|
|
2575
|
-
}
|
|
2576
|
-
|
|
2577
|
-
static VALUE uint8_ne_self(VALUE self, VALUE other) {
|
|
2578
|
-
ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
|
|
2579
|
-
ndfunc_arg_out_t aout[1] = { { numo_cBit, 0 } };
|
|
2580
|
-
ndfunc_t ndf = { iter_uint8_ne, STRIDE_LOOP, 2, 1, ain, aout };
|
|
2581
|
-
|
|
2582
|
-
return na_ndloop(&ndf, 2, self, other);
|
|
2583
|
-
}
|
|
2584
|
-
|
|
2585
|
-
/*
|
|
2586
|
-
Comparison ne other.
|
|
2587
|
-
@overload ne other
|
|
2588
|
-
@param [Numo::NArray,Numeric] other
|
|
2589
|
-
@return [Numo::Bit] result of self ne other.
|
|
2590
|
-
*/
|
|
2591
|
-
static VALUE uint8_ne(VALUE self, VALUE other) {
|
|
2592
|
-
|
|
2593
|
-
VALUE klass, v;
|
|
2594
|
-
klass = na_upcast(rb_obj_class(self), rb_obj_class(other));
|
|
2595
|
-
if (klass == cT) {
|
|
2596
|
-
return uint8_ne_self(self, other);
|
|
2597
|
-
} else {
|
|
2598
|
-
v = rb_funcall(klass, id_cast, 1, self);
|
|
2599
|
-
return rb_funcall(v, id_ne, 1, other);
|
|
2600
|
-
}
|
|
2601
|
-
}
|
|
2602
|
-
|
|
2603
|
-
#define check_intdivzero(y) \
|
|
2604
|
-
{}
|
|
2605
|
-
|
|
2606
|
-
static void iter_uint8_bit_and(na_loop_t* const lp) {
|
|
2607
|
-
size_t i = 0;
|
|
2608
|
-
size_t n;
|
|
2609
|
-
char *p1, *p2, *p3;
|
|
2610
|
-
ssize_t s1, s2, s3;
|
|
2611
|
-
|
|
2612
|
-
INIT_COUNTER(lp, n);
|
|
2613
|
-
INIT_PTR(lp, 0, p1, s1);
|
|
2614
|
-
INIT_PTR(lp, 1, p2, s2);
|
|
2615
|
-
INIT_PTR(lp, 2, p3, s3);
|
|
2616
|
-
|
|
2617
|
-
//
|
|
2618
|
-
|
|
2619
|
-
if (s2 == 0) { // Broadcasting from scalar value.
|
|
2620
|
-
check_intdivzero(*(dtype*)p2);
|
|
2621
|
-
if (s1 == sizeof(dtype) && s3 == sizeof(dtype)) {
|
|
2622
|
-
if (p1 == p3) { // inplace case
|
|
2623
|
-
for (; i < n; i++) {
|
|
2624
|
-
((dtype*)p1)[i] = m_bit_and(((dtype*)p1)[i], *(dtype*)p2);
|
|
2625
|
-
}
|
|
2626
|
-
} else {
|
|
2627
|
-
for (; i < n; i++) {
|
|
2628
|
-
((dtype*)p3)[i] = m_bit_and(((dtype*)p1)[i], *(dtype*)p2);
|
|
2629
|
-
}
|
|
2630
|
-
}
|
|
2631
|
-
} else {
|
|
2632
|
-
for (i = 0; i < n; i++) {
|
|
2633
|
-
*(dtype*)p3 = m_bit_and(*(dtype*)p1, *(dtype*)p2);
|
|
2634
|
-
p1 += s1;
|
|
2635
|
-
p3 += s3;
|
|
2636
|
-
}
|
|
2637
|
-
}
|
|
2638
|
-
} else {
|
|
2639
|
-
if (p1 == p3) { // inplace case
|
|
2640
|
-
for (i = 0; i < n; i++) {
|
|
2641
|
-
check_intdivzero(*(dtype*)p2);
|
|
2642
|
-
*(dtype*)p1 = m_bit_and(*(dtype*)p1, *(dtype*)p2);
|
|
2643
|
-
p1 += s1;
|
|
2644
|
-
p2 += s2;
|
|
2645
|
-
}
|
|
2646
|
-
} else {
|
|
2647
|
-
for (i = 0; i < n; i++) {
|
|
2648
|
-
check_intdivzero(*(dtype*)p2);
|
|
2649
|
-
*(dtype*)p3 = m_bit_and(*(dtype*)p1, *(dtype*)p2);
|
|
2650
|
-
p1 += s1;
|
|
2651
|
-
p2 += s2;
|
|
2652
|
-
p3 += s3;
|
|
2653
|
-
}
|
|
2654
|
-
}
|
|
2655
|
-
}
|
|
2656
|
-
|
|
2657
|
-
return;
|
|
2658
|
-
//
|
|
2659
|
-
}
|
|
2660
|
-
#undef check_intdivzero
|
|
2661
|
-
|
|
2662
|
-
static VALUE uint8_bit_and_self(VALUE self, VALUE other) {
|
|
2663
|
-
ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
|
|
2664
|
-
ndfunc_arg_out_t aout[1] = { { cT, 0 } };
|
|
2665
|
-
ndfunc_t ndf = { iter_uint8_bit_and, STRIDE_LOOP, 2, 1, ain, aout };
|
|
2666
|
-
|
|
2667
|
-
return na_ndloop(&ndf, 2, self, other);
|
|
2668
|
-
}
|
|
2669
|
-
|
|
2670
|
-
/*
|
|
2671
|
-
Binary bit_and.
|
|
2672
|
-
@overload & other
|
|
2673
|
-
@param [Numo::NArray,Numeric] other
|
|
2674
|
-
@return [Numo::NArray] self & other
|
|
2675
|
-
*/
|
|
2676
|
-
static VALUE uint8_bit_and(VALUE self, VALUE other) {
|
|
2677
|
-
|
|
2678
|
-
VALUE klass, v;
|
|
2679
|
-
|
|
2680
|
-
klass = na_upcast(rb_obj_class(self), rb_obj_class(other));
|
|
2681
|
-
if (klass == cT) {
|
|
2682
|
-
return uint8_bit_and_self(self, other);
|
|
2683
|
-
} else {
|
|
2684
|
-
v = rb_funcall(klass, id_cast, 1, self);
|
|
2685
|
-
return rb_funcall(v, '&', 1, other);
|
|
2686
|
-
}
|
|
2687
|
-
}
|
|
2688
|
-
|
|
2689
|
-
#define check_intdivzero(y) \
|
|
2690
|
-
{}
|
|
2691
|
-
|
|
2692
|
-
static void iter_uint8_bit_or(na_loop_t* const lp) {
|
|
2693
|
-
size_t i = 0;
|
|
2694
|
-
size_t n;
|
|
2695
|
-
char *p1, *p2, *p3;
|
|
2696
|
-
ssize_t s1, s2, s3;
|
|
2697
|
-
|
|
2698
|
-
INIT_COUNTER(lp, n);
|
|
2699
|
-
INIT_PTR(lp, 0, p1, s1);
|
|
2700
|
-
INIT_PTR(lp, 1, p2, s2);
|
|
2701
|
-
INIT_PTR(lp, 2, p3, s3);
|
|
2702
|
-
|
|
2703
|
-
//
|
|
2704
|
-
|
|
2705
|
-
if (s2 == 0) { // Broadcasting from scalar value.
|
|
2706
|
-
check_intdivzero(*(dtype*)p2);
|
|
2707
|
-
if (s1 == sizeof(dtype) && s3 == sizeof(dtype)) {
|
|
2708
|
-
if (p1 == p3) { // inplace case
|
|
2709
|
-
for (; i < n; i++) {
|
|
2710
|
-
((dtype*)p1)[i] = m_bit_or(((dtype*)p1)[i], *(dtype*)p2);
|
|
2711
|
-
}
|
|
2712
|
-
} else {
|
|
2713
|
-
for (; i < n; i++) {
|
|
2714
|
-
((dtype*)p3)[i] = m_bit_or(((dtype*)p1)[i], *(dtype*)p2);
|
|
2715
|
-
}
|
|
2716
|
-
}
|
|
2717
|
-
} else {
|
|
2718
|
-
for (i = 0; i < n; i++) {
|
|
2719
|
-
*(dtype*)p3 = m_bit_or(*(dtype*)p1, *(dtype*)p2);
|
|
2720
|
-
p1 += s1;
|
|
2721
|
-
p3 += s3;
|
|
2722
|
-
}
|
|
2723
|
-
}
|
|
2724
|
-
} else {
|
|
2725
|
-
if (p1 == p3) { // inplace case
|
|
2726
|
-
for (i = 0; i < n; i++) {
|
|
2727
|
-
check_intdivzero(*(dtype*)p2);
|
|
2728
|
-
*(dtype*)p1 = m_bit_or(*(dtype*)p1, *(dtype*)p2);
|
|
2729
|
-
p1 += s1;
|
|
2730
|
-
p2 += s2;
|
|
2731
|
-
}
|
|
2732
|
-
} else {
|
|
2733
|
-
for (i = 0; i < n; i++) {
|
|
2734
|
-
check_intdivzero(*(dtype*)p2);
|
|
2735
|
-
*(dtype*)p3 = m_bit_or(*(dtype*)p1, *(dtype*)p2);
|
|
2736
|
-
p1 += s1;
|
|
2737
|
-
p2 += s2;
|
|
2738
|
-
p3 += s3;
|
|
2739
|
-
}
|
|
2740
|
-
}
|
|
2741
|
-
}
|
|
2742
|
-
|
|
2743
|
-
return;
|
|
2744
|
-
//
|
|
2745
|
-
}
|
|
2746
|
-
#undef check_intdivzero
|
|
2747
|
-
|
|
2748
|
-
static VALUE uint8_bit_or_self(VALUE self, VALUE other) {
|
|
2749
|
-
ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
|
|
2750
|
-
ndfunc_arg_out_t aout[1] = { { cT, 0 } };
|
|
2751
|
-
ndfunc_t ndf = { iter_uint8_bit_or, STRIDE_LOOP, 2, 1, ain, aout };
|
|
2752
|
-
|
|
2753
|
-
return na_ndloop(&ndf, 2, self, other);
|
|
2754
|
-
}
|
|
2755
|
-
|
|
2756
|
-
/*
|
|
2757
|
-
Binary bit_or.
|
|
2758
|
-
@overload | other
|
|
2759
|
-
@param [Numo::NArray,Numeric] other
|
|
2760
|
-
@return [Numo::NArray] self | other
|
|
2761
|
-
*/
|
|
2762
|
-
static VALUE uint8_bit_or(VALUE self, VALUE other) {
|
|
2763
|
-
|
|
2764
|
-
VALUE klass, v;
|
|
2765
|
-
|
|
2766
|
-
klass = na_upcast(rb_obj_class(self), rb_obj_class(other));
|
|
2767
|
-
if (klass == cT) {
|
|
2768
|
-
return uint8_bit_or_self(self, other);
|
|
2769
|
-
} else {
|
|
2770
|
-
v = rb_funcall(klass, id_cast, 1, self);
|
|
2771
|
-
return rb_funcall(v, '|', 1, other);
|
|
2772
|
-
}
|
|
2773
|
-
}
|
|
2774
|
-
|
|
2775
|
-
#define check_intdivzero(y) \
|
|
2776
|
-
{}
|
|
2777
|
-
|
|
2778
|
-
static void iter_uint8_bit_xor(na_loop_t* const lp) {
|
|
2779
|
-
size_t i = 0;
|
|
2780
|
-
size_t n;
|
|
2781
|
-
char *p1, *p2, *p3;
|
|
2782
|
-
ssize_t s1, s2, s3;
|
|
2783
|
-
|
|
2784
|
-
INIT_COUNTER(lp, n);
|
|
2785
|
-
INIT_PTR(lp, 0, p1, s1);
|
|
2786
|
-
INIT_PTR(lp, 1, p2, s2);
|
|
2787
|
-
INIT_PTR(lp, 2, p3, s3);
|
|
2788
|
-
|
|
2789
|
-
//
|
|
2790
|
-
|
|
2791
|
-
if (s2 == 0) { // Broadcasting from scalar value.
|
|
2792
|
-
check_intdivzero(*(dtype*)p2);
|
|
2793
|
-
if (s1 == sizeof(dtype) && s3 == sizeof(dtype)) {
|
|
2794
|
-
if (p1 == p3) { // inplace case
|
|
2795
|
-
for (; i < n; i++) {
|
|
2796
|
-
((dtype*)p1)[i] = m_bit_xor(((dtype*)p1)[i], *(dtype*)p2);
|
|
2797
|
-
}
|
|
2798
|
-
} else {
|
|
2799
|
-
for (; i < n; i++) {
|
|
2800
|
-
((dtype*)p3)[i] = m_bit_xor(((dtype*)p1)[i], *(dtype*)p2);
|
|
2801
|
-
}
|
|
2802
|
-
}
|
|
2803
|
-
} else {
|
|
2804
|
-
for (i = 0; i < n; i++) {
|
|
2805
|
-
*(dtype*)p3 = m_bit_xor(*(dtype*)p1, *(dtype*)p2);
|
|
2806
|
-
p1 += s1;
|
|
2807
|
-
p3 += s3;
|
|
2808
|
-
}
|
|
2809
|
-
}
|
|
2810
|
-
} else {
|
|
2811
|
-
if (p1 == p3) { // inplace case
|
|
2812
|
-
for (i = 0; i < n; i++) {
|
|
2813
|
-
check_intdivzero(*(dtype*)p2);
|
|
2814
|
-
*(dtype*)p1 = m_bit_xor(*(dtype*)p1, *(dtype*)p2);
|
|
2815
|
-
p1 += s1;
|
|
2816
|
-
p2 += s2;
|
|
2817
|
-
}
|
|
2818
|
-
} else {
|
|
2819
|
-
for (i = 0; i < n; i++) {
|
|
2820
|
-
check_intdivzero(*(dtype*)p2);
|
|
2821
|
-
*(dtype*)p3 = m_bit_xor(*(dtype*)p1, *(dtype*)p2);
|
|
2822
|
-
p1 += s1;
|
|
2823
|
-
p2 += s2;
|
|
2824
|
-
p3 += s3;
|
|
2825
|
-
}
|
|
2826
|
-
}
|
|
2827
|
-
}
|
|
2828
|
-
|
|
2829
|
-
return;
|
|
2830
|
-
//
|
|
2831
|
-
}
|
|
2832
|
-
#undef check_intdivzero
|
|
2833
|
-
|
|
2834
|
-
static VALUE uint8_bit_xor_self(VALUE self, VALUE other) {
|
|
2835
|
-
ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
|
|
2836
|
-
ndfunc_arg_out_t aout[1] = { { cT, 0 } };
|
|
2837
|
-
ndfunc_t ndf = { iter_uint8_bit_xor, STRIDE_LOOP, 2, 1, ain, aout };
|
|
2838
|
-
|
|
2839
|
-
return na_ndloop(&ndf, 2, self, other);
|
|
2840
|
-
}
|
|
2841
|
-
|
|
2842
|
-
/*
|
|
2843
|
-
Binary bit_xor.
|
|
2844
|
-
@overload ^ other
|
|
2845
|
-
@param [Numo::NArray,Numeric] other
|
|
2846
|
-
@return [Numo::NArray] self ^ other
|
|
2847
|
-
*/
|
|
2848
|
-
static VALUE uint8_bit_xor(VALUE self, VALUE other) {
|
|
2849
|
-
|
|
2850
|
-
VALUE klass, v;
|
|
2851
|
-
|
|
2852
|
-
klass = na_upcast(rb_obj_class(self), rb_obj_class(other));
|
|
2853
|
-
if (klass == cT) {
|
|
2854
|
-
return uint8_bit_xor_self(self, other);
|
|
2855
|
-
} else {
|
|
2856
|
-
v = rb_funcall(klass, id_cast, 1, self);
|
|
2857
|
-
return rb_funcall(v, '^', 1, other);
|
|
2858
|
-
}
|
|
2859
|
-
}
|
|
2860
|
-
|
|
2861
|
-
static void iter_uint8_bit_not(na_loop_t* const lp) {
|
|
2862
|
-
size_t i, n;
|
|
2863
|
-
char *p1, *p2;
|
|
2864
|
-
ssize_t s1, s2;
|
|
2865
|
-
size_t *idx1, *idx2;
|
|
2866
|
-
dtype x;
|
|
2867
|
-
|
|
2868
|
-
INIT_COUNTER(lp, n);
|
|
2869
|
-
INIT_PTR_IDX(lp, 0, p1, s1, idx1);
|
|
2870
|
-
INIT_PTR_IDX(lp, 1, p2, s2, idx2);
|
|
2871
|
-
|
|
2872
|
-
if (idx1) {
|
|
2873
|
-
if (idx2) {
|
|
2874
|
-
for (i = 0; i < n; i++) {
|
|
2875
|
-
GET_DATA_INDEX(p1, idx1, dtype, x);
|
|
2876
|
-
x = m_bit_not(x);
|
|
2877
|
-
SET_DATA_INDEX(p2, idx2, dtype, x);
|
|
2878
|
-
}
|
|
2879
|
-
} else {
|
|
2880
|
-
for (i = 0; i < n; i++) {
|
|
2881
|
-
GET_DATA_INDEX(p1, idx1, dtype, x);
|
|
2882
|
-
x = m_bit_not(x);
|
|
2883
|
-
SET_DATA_STRIDE(p2, s2, dtype, x);
|
|
2884
|
-
}
|
|
2885
|
-
}
|
|
2886
|
-
} else {
|
|
2887
|
-
if (idx2) {
|
|
2888
|
-
for (i = 0; i < n; i++) {
|
|
2889
|
-
GET_DATA_STRIDE(p1, s1, dtype, x);
|
|
2890
|
-
x = m_bit_not(x);
|
|
2891
|
-
SET_DATA_INDEX(p2, idx2, dtype, x);
|
|
2892
|
-
}
|
|
2893
|
-
} else {
|
|
2894
|
-
//
|
|
2895
|
-
for (i = 0; i < n; i++) {
|
|
2896
|
-
*(dtype*)p2 = m_bit_not(*(dtype*)p1);
|
|
2897
|
-
p1 += s1;
|
|
2898
|
-
p2 += s2;
|
|
2899
|
-
}
|
|
2900
|
-
return;
|
|
2901
|
-
//
|
|
2902
|
-
}
|
|
2903
|
-
}
|
|
2904
|
-
}
|
|
2905
|
-
|
|
2906
|
-
/*
|
|
2907
|
-
Unary bit_not.
|
|
2908
|
-
@overload ~
|
|
2909
|
-
@return [Numo::UInt8] bit_not of self.
|
|
2910
|
-
*/
|
|
2911
|
-
static VALUE uint8_bit_not(VALUE self) {
|
|
2912
|
-
ndfunc_arg_in_t ain[1] = { { cT, 0 } };
|
|
2913
|
-
ndfunc_arg_out_t aout[1] = { { cT, 0 } };
|
|
2914
|
-
ndfunc_t ndf = { iter_uint8_bit_not, FULL_LOOP, 1, 1, ain, aout };
|
|
2915
|
-
|
|
2916
|
-
return na_ndloop(&ndf, 1, self);
|
|
2917
|
-
}
|
|
2918
|
-
|
|
2919
|
-
#define check_intdivzero(y) \
|
|
2920
|
-
{}
|
|
2921
|
-
|
|
2922
|
-
static void iter_uint8_left_shift(na_loop_t* const lp) {
|
|
2923
|
-
size_t i = 0;
|
|
2924
|
-
size_t n;
|
|
2925
|
-
char *p1, *p2, *p3;
|
|
2926
|
-
ssize_t s1, s2, s3;
|
|
2927
|
-
|
|
2928
|
-
INIT_COUNTER(lp, n);
|
|
2929
|
-
INIT_PTR(lp, 0, p1, s1);
|
|
2930
|
-
INIT_PTR(lp, 1, p2, s2);
|
|
2931
|
-
INIT_PTR(lp, 2, p3, s3);
|
|
2932
|
-
|
|
2933
|
-
//
|
|
2934
|
-
|
|
2935
|
-
if (s2 == 0) { // Broadcasting from scalar value.
|
|
2936
|
-
check_intdivzero(*(dtype*)p2);
|
|
2937
|
-
if (s1 == sizeof(dtype) && s3 == sizeof(dtype)) {
|
|
2938
|
-
if (p1 == p3) { // inplace case
|
|
2939
|
-
for (; i < n; i++) {
|
|
2940
|
-
((dtype*)p1)[i] = m_left_shift(((dtype*)p1)[i], *(dtype*)p2);
|
|
2941
|
-
}
|
|
2942
|
-
} else {
|
|
2943
|
-
for (; i < n; i++) {
|
|
2944
|
-
((dtype*)p3)[i] = m_left_shift(((dtype*)p1)[i], *(dtype*)p2);
|
|
2945
|
-
}
|
|
2946
|
-
}
|
|
2947
|
-
} else {
|
|
2948
|
-
for (i = 0; i < n; i++) {
|
|
2949
|
-
*(dtype*)p3 = m_left_shift(*(dtype*)p1, *(dtype*)p2);
|
|
2950
|
-
p1 += s1;
|
|
2951
|
-
p3 += s3;
|
|
2952
|
-
}
|
|
2953
|
-
}
|
|
2954
|
-
} else {
|
|
2955
|
-
if (p1 == p3) { // inplace case
|
|
2956
|
-
for (i = 0; i < n; i++) {
|
|
2957
|
-
check_intdivzero(*(dtype*)p2);
|
|
2958
|
-
*(dtype*)p1 = m_left_shift(*(dtype*)p1, *(dtype*)p2);
|
|
2959
|
-
p1 += s1;
|
|
2960
|
-
p2 += s2;
|
|
2961
|
-
}
|
|
2962
|
-
} else {
|
|
2963
|
-
for (i = 0; i < n; i++) {
|
|
2964
|
-
check_intdivzero(*(dtype*)p2);
|
|
2965
|
-
*(dtype*)p3 = m_left_shift(*(dtype*)p1, *(dtype*)p2);
|
|
2966
|
-
p1 += s1;
|
|
2967
|
-
p2 += s2;
|
|
2968
|
-
p3 += s3;
|
|
2969
|
-
}
|
|
2970
|
-
}
|
|
2971
|
-
}
|
|
2972
|
-
|
|
2973
|
-
return;
|
|
2974
|
-
//
|
|
2975
|
-
}
|
|
2976
|
-
#undef check_intdivzero
|
|
2977
|
-
|
|
2978
|
-
static VALUE uint8_left_shift_self(VALUE self, VALUE other) {
|
|
2979
|
-
ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
|
|
2980
|
-
ndfunc_arg_out_t aout[1] = { { cT, 0 } };
|
|
2981
|
-
ndfunc_t ndf = { iter_uint8_left_shift, STRIDE_LOOP, 2, 1, ain, aout };
|
|
2982
|
-
|
|
2983
|
-
return na_ndloop(&ndf, 2, self, other);
|
|
2984
|
-
}
|
|
2985
|
-
|
|
2986
|
-
/*
|
|
2987
|
-
Binary left_shift.
|
|
2988
|
-
@overload << other
|
|
2989
|
-
@param [Numo::NArray,Numeric] other
|
|
2990
|
-
@return [Numo::NArray] self << other
|
|
2991
|
-
*/
|
|
2992
|
-
static VALUE uint8_left_shift(VALUE self, VALUE other) {
|
|
2993
|
-
|
|
2994
|
-
VALUE klass, v;
|
|
2995
|
-
|
|
2996
|
-
klass = na_upcast(rb_obj_class(self), rb_obj_class(other));
|
|
2997
|
-
if (klass == cT) {
|
|
2998
|
-
return uint8_left_shift_self(self, other);
|
|
2999
|
-
} else {
|
|
3000
|
-
v = rb_funcall(klass, id_cast, 1, self);
|
|
3001
|
-
return rb_funcall(v, id_left_shift, 1, other);
|
|
3002
|
-
}
|
|
3003
|
-
}
|
|
3004
|
-
|
|
3005
|
-
#define check_intdivzero(y) \
|
|
3006
|
-
{}
|
|
3007
|
-
|
|
3008
|
-
static void iter_uint8_right_shift(na_loop_t* const lp) {
|
|
3009
|
-
size_t i = 0;
|
|
3010
|
-
size_t n;
|
|
3011
|
-
char *p1, *p2, *p3;
|
|
3012
|
-
ssize_t s1, s2, s3;
|
|
3013
|
-
|
|
3014
|
-
INIT_COUNTER(lp, n);
|
|
3015
|
-
INIT_PTR(lp, 0, p1, s1);
|
|
3016
|
-
INIT_PTR(lp, 1, p2, s2);
|
|
3017
|
-
INIT_PTR(lp, 2, p3, s3);
|
|
3018
|
-
|
|
3019
|
-
//
|
|
3020
|
-
|
|
3021
|
-
if (s2 == 0) { // Broadcasting from scalar value.
|
|
3022
|
-
check_intdivzero(*(dtype*)p2);
|
|
3023
|
-
if (s1 == sizeof(dtype) && s3 == sizeof(dtype)) {
|
|
3024
|
-
if (p1 == p3) { // inplace case
|
|
3025
|
-
for (; i < n; i++) {
|
|
3026
|
-
((dtype*)p1)[i] = m_right_shift(((dtype*)p1)[i], *(dtype*)p2);
|
|
3027
|
-
}
|
|
3028
|
-
} else {
|
|
3029
|
-
for (; i < n; i++) {
|
|
3030
|
-
((dtype*)p3)[i] = m_right_shift(((dtype*)p1)[i], *(dtype*)p2);
|
|
3031
|
-
}
|
|
3032
|
-
}
|
|
3033
|
-
} else {
|
|
3034
|
-
for (i = 0; i < n; i++) {
|
|
3035
|
-
*(dtype*)p3 = m_right_shift(*(dtype*)p1, *(dtype*)p2);
|
|
3036
|
-
p1 += s1;
|
|
3037
|
-
p3 += s3;
|
|
3038
|
-
}
|
|
3039
|
-
}
|
|
3040
|
-
} else {
|
|
3041
|
-
if (p1 == p3) { // inplace case
|
|
3042
|
-
for (i = 0; i < n; i++) {
|
|
3043
|
-
check_intdivzero(*(dtype*)p2);
|
|
3044
|
-
*(dtype*)p1 = m_right_shift(*(dtype*)p1, *(dtype*)p2);
|
|
3045
|
-
p1 += s1;
|
|
3046
|
-
p2 += s2;
|
|
3047
|
-
}
|
|
3048
|
-
} else {
|
|
3049
|
-
for (i = 0; i < n; i++) {
|
|
3050
|
-
check_intdivzero(*(dtype*)p2);
|
|
3051
|
-
*(dtype*)p3 = m_right_shift(*(dtype*)p1, *(dtype*)p2);
|
|
3052
|
-
p1 += s1;
|
|
3053
|
-
p2 += s2;
|
|
3054
|
-
p3 += s3;
|
|
3055
|
-
}
|
|
3056
|
-
}
|
|
3057
|
-
}
|
|
3058
|
-
|
|
3059
|
-
return;
|
|
3060
|
-
//
|
|
3061
|
-
}
|
|
3062
|
-
#undef check_intdivzero
|
|
3063
|
-
|
|
3064
|
-
static VALUE uint8_right_shift_self(VALUE self, VALUE other) {
|
|
3065
|
-
ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
|
|
3066
|
-
ndfunc_arg_out_t aout[1] = { { cT, 0 } };
|
|
3067
|
-
ndfunc_t ndf = { iter_uint8_right_shift, STRIDE_LOOP, 2, 1, ain, aout };
|
|
3068
|
-
|
|
3069
|
-
return na_ndloop(&ndf, 2, self, other);
|
|
3070
|
-
}
|
|
3071
|
-
|
|
3072
|
-
/*
|
|
3073
|
-
Binary right_shift.
|
|
3074
|
-
@overload >> other
|
|
3075
|
-
@param [Numo::NArray,Numeric] other
|
|
3076
|
-
@return [Numo::NArray] self >> other
|
|
3077
|
-
*/
|
|
3078
|
-
static VALUE uint8_right_shift(VALUE self, VALUE other) {
|
|
3079
|
-
|
|
3080
|
-
VALUE klass, v;
|
|
3081
|
-
|
|
3082
|
-
klass = na_upcast(rb_obj_class(self), rb_obj_class(other));
|
|
3083
|
-
if (klass == cT) {
|
|
3084
|
-
return uint8_right_shift_self(self, other);
|
|
3085
|
-
} else {
|
|
3086
|
-
v = rb_funcall(klass, id_cast, 1, self);
|
|
3087
|
-
return rb_funcall(v, id_right_shift, 1, other);
|
|
3088
|
-
}
|
|
3089
|
-
}
|
|
3090
|
-
|
|
3091
|
-
static void iter_uint8_gt(na_loop_t* const lp) {
|
|
3092
|
-
size_t i;
|
|
3093
|
-
char *p1, *p2;
|
|
3094
|
-
BIT_DIGIT* a3;
|
|
3095
|
-
size_t p3;
|
|
3096
|
-
ssize_t s1, s2, s3;
|
|
3097
|
-
dtype x, y;
|
|
3098
|
-
BIT_DIGIT b;
|
|
3099
|
-
INIT_COUNTER(lp, i);
|
|
3100
|
-
INIT_PTR(lp, 0, p1, s1);
|
|
3101
|
-
INIT_PTR(lp, 1, p2, s2);
|
|
3102
|
-
INIT_PTR_BIT(lp, 2, a3, p3, s3);
|
|
3103
|
-
for (; i--;) {
|
|
3104
|
-
GET_DATA_STRIDE(p1, s1, dtype, x);
|
|
3105
|
-
GET_DATA_STRIDE(p2, s2, dtype, y);
|
|
3106
|
-
b = (m_gt(x, y)) ? 1 : 0;
|
|
3107
|
-
STORE_BIT(a3, p3, b);
|
|
3108
|
-
p3 += s3;
|
|
3109
|
-
}
|
|
3110
|
-
}
|
|
3111
|
-
|
|
3112
|
-
static VALUE uint8_gt_self(VALUE self, VALUE other) {
|
|
3113
|
-
ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
|
|
3114
|
-
ndfunc_arg_out_t aout[1] = { { numo_cBit, 0 } };
|
|
3115
|
-
ndfunc_t ndf = { iter_uint8_gt, STRIDE_LOOP, 2, 1, ain, aout };
|
|
3116
|
-
|
|
3117
|
-
return na_ndloop(&ndf, 2, self, other);
|
|
3118
|
-
}
|
|
3119
|
-
|
|
3120
|
-
/*
|
|
3121
|
-
Comparison gt other.
|
|
3122
|
-
@overload gt other
|
|
3123
|
-
@param [Numo::NArray,Numeric] other
|
|
3124
|
-
@return [Numo::Bit] result of self gt other.
|
|
3125
|
-
*/
|
|
3126
|
-
static VALUE uint8_gt(VALUE self, VALUE other) {
|
|
3127
|
-
|
|
3128
|
-
VALUE klass, v;
|
|
3129
|
-
klass = na_upcast(rb_obj_class(self), rb_obj_class(other));
|
|
3130
|
-
if (klass == cT) {
|
|
3131
|
-
return uint8_gt_self(self, other);
|
|
3132
|
-
} else {
|
|
3133
|
-
v = rb_funcall(klass, id_cast, 1, self);
|
|
3134
|
-
return rb_funcall(v, id_gt, 1, other);
|
|
3135
|
-
}
|
|
3136
|
-
}
|
|
3137
|
-
|
|
3138
|
-
static void iter_uint8_ge(na_loop_t* const lp) {
|
|
3139
|
-
size_t i;
|
|
3140
|
-
char *p1, *p2;
|
|
3141
|
-
BIT_DIGIT* a3;
|
|
3142
|
-
size_t p3;
|
|
3143
|
-
ssize_t s1, s2, s3;
|
|
3144
|
-
dtype x, y;
|
|
3145
|
-
BIT_DIGIT b;
|
|
3146
|
-
INIT_COUNTER(lp, i);
|
|
3147
|
-
INIT_PTR(lp, 0, p1, s1);
|
|
3148
|
-
INIT_PTR(lp, 1, p2, s2);
|
|
3149
|
-
INIT_PTR_BIT(lp, 2, a3, p3, s3);
|
|
3150
|
-
for (; i--;) {
|
|
3151
|
-
GET_DATA_STRIDE(p1, s1, dtype, x);
|
|
3152
|
-
GET_DATA_STRIDE(p2, s2, dtype, y);
|
|
3153
|
-
b = (m_ge(x, y)) ? 1 : 0;
|
|
3154
|
-
STORE_BIT(a3, p3, b);
|
|
3155
|
-
p3 += s3;
|
|
3156
|
-
}
|
|
3157
|
-
}
|
|
3158
|
-
|
|
3159
|
-
static VALUE uint8_ge_self(VALUE self, VALUE other) {
|
|
3160
|
-
ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
|
|
3161
|
-
ndfunc_arg_out_t aout[1] = { { numo_cBit, 0 } };
|
|
3162
|
-
ndfunc_t ndf = { iter_uint8_ge, STRIDE_LOOP, 2, 1, ain, aout };
|
|
3163
|
-
|
|
3164
|
-
return na_ndloop(&ndf, 2, self, other);
|
|
3165
|
-
}
|
|
3166
|
-
|
|
3167
|
-
/*
|
|
3168
|
-
Comparison ge other.
|
|
3169
|
-
@overload ge other
|
|
3170
|
-
@param [Numo::NArray,Numeric] other
|
|
3171
|
-
@return [Numo::Bit] result of self ge other.
|
|
3172
|
-
*/
|
|
3173
|
-
static VALUE uint8_ge(VALUE self, VALUE other) {
|
|
3174
|
-
|
|
3175
|
-
VALUE klass, v;
|
|
3176
|
-
klass = na_upcast(rb_obj_class(self), rb_obj_class(other));
|
|
3177
|
-
if (klass == cT) {
|
|
3178
|
-
return uint8_ge_self(self, other);
|
|
3179
|
-
} else {
|
|
3180
|
-
v = rb_funcall(klass, id_cast, 1, self);
|
|
3181
|
-
return rb_funcall(v, id_ge, 1, other);
|
|
3182
|
-
}
|
|
3183
|
-
}
|
|
3184
|
-
|
|
3185
|
-
static void iter_uint8_lt(na_loop_t* const lp) {
|
|
3186
|
-
size_t i;
|
|
3187
|
-
char *p1, *p2;
|
|
3188
|
-
BIT_DIGIT* a3;
|
|
3189
|
-
size_t p3;
|
|
3190
|
-
ssize_t s1, s2, s3;
|
|
3191
|
-
dtype x, y;
|
|
3192
|
-
BIT_DIGIT b;
|
|
3193
|
-
INIT_COUNTER(lp, i);
|
|
3194
|
-
INIT_PTR(lp, 0, p1, s1);
|
|
3195
|
-
INIT_PTR(lp, 1, p2, s2);
|
|
3196
|
-
INIT_PTR_BIT(lp, 2, a3, p3, s3);
|
|
3197
|
-
for (; i--;) {
|
|
3198
|
-
GET_DATA_STRIDE(p1, s1, dtype, x);
|
|
3199
|
-
GET_DATA_STRIDE(p2, s2, dtype, y);
|
|
3200
|
-
b = (m_lt(x, y)) ? 1 : 0;
|
|
3201
|
-
STORE_BIT(a3, p3, b);
|
|
3202
|
-
p3 += s3;
|
|
3203
|
-
}
|
|
3204
|
-
}
|
|
3205
|
-
|
|
3206
|
-
static VALUE uint8_lt_self(VALUE self, VALUE other) {
|
|
3207
|
-
ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
|
|
3208
|
-
ndfunc_arg_out_t aout[1] = { { numo_cBit, 0 } };
|
|
3209
|
-
ndfunc_t ndf = { iter_uint8_lt, STRIDE_LOOP, 2, 1, ain, aout };
|
|
3210
|
-
|
|
3211
|
-
return na_ndloop(&ndf, 2, self, other);
|
|
3212
|
-
}
|
|
3213
|
-
|
|
3214
|
-
/*
|
|
3215
|
-
Comparison lt other.
|
|
3216
|
-
@overload lt other
|
|
3217
|
-
@param [Numo::NArray,Numeric] other
|
|
3218
|
-
@return [Numo::Bit] result of self lt other.
|
|
3219
|
-
*/
|
|
3220
|
-
static VALUE uint8_lt(VALUE self, VALUE other) {
|
|
3221
|
-
|
|
3222
|
-
VALUE klass, v;
|
|
3223
|
-
klass = na_upcast(rb_obj_class(self), rb_obj_class(other));
|
|
3224
|
-
if (klass == cT) {
|
|
3225
|
-
return uint8_lt_self(self, other);
|
|
3226
|
-
} else {
|
|
3227
|
-
v = rb_funcall(klass, id_cast, 1, self);
|
|
3228
|
-
return rb_funcall(v, id_lt, 1, other);
|
|
3229
|
-
}
|
|
3230
|
-
}
|
|
3231
|
-
|
|
3232
|
-
static void iter_uint8_le(na_loop_t* const lp) {
|
|
3233
|
-
size_t i;
|
|
3234
|
-
char *p1, *p2;
|
|
3235
|
-
BIT_DIGIT* a3;
|
|
3236
|
-
size_t p3;
|
|
3237
|
-
ssize_t s1, s2, s3;
|
|
3238
|
-
dtype x, y;
|
|
3239
|
-
BIT_DIGIT b;
|
|
3240
|
-
INIT_COUNTER(lp, i);
|
|
3241
|
-
INIT_PTR(lp, 0, p1, s1);
|
|
3242
|
-
INIT_PTR(lp, 1, p2, s2);
|
|
3243
|
-
INIT_PTR_BIT(lp, 2, a3, p3, s3);
|
|
3244
|
-
for (; i--;) {
|
|
3245
|
-
GET_DATA_STRIDE(p1, s1, dtype, x);
|
|
3246
|
-
GET_DATA_STRIDE(p2, s2, dtype, y);
|
|
3247
|
-
b = (m_le(x, y)) ? 1 : 0;
|
|
3248
|
-
STORE_BIT(a3, p3, b);
|
|
3249
|
-
p3 += s3;
|
|
3250
|
-
}
|
|
3251
|
-
}
|
|
3252
|
-
|
|
3253
|
-
static VALUE uint8_le_self(VALUE self, VALUE other) {
|
|
3254
|
-
ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
|
|
3255
|
-
ndfunc_arg_out_t aout[1] = { { numo_cBit, 0 } };
|
|
3256
|
-
ndfunc_t ndf = { iter_uint8_le, STRIDE_LOOP, 2, 1, ain, aout };
|
|
3257
|
-
|
|
3258
|
-
return na_ndloop(&ndf, 2, self, other);
|
|
3259
|
-
}
|
|
3260
|
-
|
|
3261
|
-
/*
|
|
3262
|
-
Comparison le other.
|
|
3263
|
-
@overload le other
|
|
3264
|
-
@param [Numo::NArray,Numeric] other
|
|
3265
|
-
@return [Numo::Bit] result of self le other.
|
|
3266
|
-
*/
|
|
3267
|
-
static VALUE uint8_le(VALUE self, VALUE other) {
|
|
3268
|
-
|
|
3269
|
-
VALUE klass, v;
|
|
3270
|
-
klass = na_upcast(rb_obj_class(self), rb_obj_class(other));
|
|
3271
|
-
if (klass == cT) {
|
|
3272
|
-
return uint8_le_self(self, other);
|
|
3273
|
-
} else {
|
|
3274
|
-
v = rb_funcall(klass, id_cast, 1, self);
|
|
3275
|
-
return rb_funcall(v, id_le, 1, other);
|
|
3276
|
-
}
|
|
3277
|
-
}
|
|
3278
|
-
|
|
3279
|
-
static void iter_uint8_clip(na_loop_t* const lp) {
|
|
3280
|
-
size_t i;
|
|
3281
|
-
char *p1, *p2, *p3, *p4;
|
|
3282
|
-
ssize_t s1, s2, s3, s4;
|
|
3283
|
-
dtype x, min, max;
|
|
3284
|
-
INIT_COUNTER(lp, i);
|
|
3285
|
-
INIT_PTR(lp, 0, p1, s1);
|
|
3286
|
-
INIT_PTR(lp, 1, p2, s2);
|
|
3287
|
-
INIT_PTR(lp, 2, p3, s3);
|
|
3288
|
-
INIT_PTR(lp, 3, p4, s4);
|
|
3289
|
-
for (; i--;) {
|
|
3290
|
-
GET_DATA_STRIDE(p1, s1, dtype, x);
|
|
3291
|
-
GET_DATA_STRIDE(p2, s2, dtype, min);
|
|
3292
|
-
GET_DATA_STRIDE(p3, s3, dtype, max);
|
|
3293
|
-
if (m_gt(min, max)) {
|
|
3294
|
-
rb_raise(nary_eOperationError, "min is greater than max");
|
|
3295
|
-
}
|
|
3296
|
-
if (m_lt(x, min)) {
|
|
3297
|
-
x = min;
|
|
3298
|
-
}
|
|
3299
|
-
if (m_gt(x, max)) {
|
|
3300
|
-
x = max;
|
|
3301
|
-
}
|
|
3302
|
-
SET_DATA_STRIDE(p4, s4, dtype, x);
|
|
3303
|
-
}
|
|
3304
|
-
}
|
|
3305
|
-
|
|
3306
|
-
static void iter_uint8_clip_min(na_loop_t* const lp) {
|
|
3307
|
-
size_t i;
|
|
3308
|
-
char *p1, *p2, *p3;
|
|
3309
|
-
ssize_t s1, s2, s3;
|
|
3310
|
-
dtype x, min;
|
|
3311
|
-
INIT_COUNTER(lp, i);
|
|
3312
|
-
INIT_PTR(lp, 0, p1, s1);
|
|
3313
|
-
INIT_PTR(lp, 1, p2, s2);
|
|
3314
|
-
INIT_PTR(lp, 2, p3, s3);
|
|
3315
|
-
for (; i--;) {
|
|
3316
|
-
GET_DATA_STRIDE(p1, s1, dtype, x);
|
|
3317
|
-
GET_DATA_STRIDE(p2, s2, dtype, min);
|
|
3318
|
-
if (m_lt(x, min)) {
|
|
3319
|
-
x = min;
|
|
3320
|
-
}
|
|
3321
|
-
SET_DATA_STRIDE(p3, s3, dtype, x);
|
|
3322
|
-
}
|
|
3323
|
-
}
|
|
3324
|
-
|
|
3325
|
-
static void iter_uint8_clip_max(na_loop_t* const lp) {
|
|
3326
|
-
size_t i;
|
|
3327
|
-
char *p1, *p2, *p3;
|
|
3328
|
-
ssize_t s1, s2, s3;
|
|
3329
|
-
dtype x, max;
|
|
3330
|
-
INIT_COUNTER(lp, i);
|
|
3331
|
-
INIT_PTR(lp, 0, p1, s1);
|
|
3332
|
-
INIT_PTR(lp, 1, p2, s2);
|
|
3333
|
-
INIT_PTR(lp, 2, p3, s3);
|
|
3334
|
-
for (; i--;) {
|
|
3335
|
-
GET_DATA_STRIDE(p1, s1, dtype, x);
|
|
3336
|
-
GET_DATA_STRIDE(p2, s2, dtype, max);
|
|
3337
|
-
if (m_gt(x, max)) {
|
|
3338
|
-
x = max;
|
|
3339
|
-
}
|
|
3340
|
-
SET_DATA_STRIDE(p3, s3, dtype, x);
|
|
3341
|
-
}
|
|
3342
|
-
}
|
|
3343
|
-
|
|
3344
|
-
/*
|
|
3345
|
-
Clip array elements by [min,max].
|
|
3346
|
-
If either of min or max is nil, one side is clipped.
|
|
3347
|
-
@overload clip(min,max)
|
|
3348
|
-
@param [Numo::NArray,Numeric] min
|
|
3349
|
-
@param [Numo::NArray,Numeric] max
|
|
3350
|
-
@return [Numo::NArray] result of clip.
|
|
3351
|
-
|
|
3352
|
-
@example
|
|
3353
|
-
a = Numo::Int32.new(10).seq
|
|
3354
|
-
# => Numo::Int32#shape=[10]
|
|
3355
|
-
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
|
3356
|
-
|
|
3357
|
-
a.clip(1,8)
|
|
3358
|
-
# => Numo::Int32#shape=[10]
|
|
3359
|
-
# [1, 1, 2, 3, 4, 5, 6, 7, 8, 8]
|
|
3360
|
-
|
|
3361
|
-
a.inplace.clip(3,6)
|
|
3362
|
-
a
|
|
3363
|
-
# => Numo::Int32#shape=[10]
|
|
3364
|
-
# [3, 3, 3, 3, 4, 5, 6, 6, 6, 6]
|
|
3365
|
-
|
|
3366
|
-
b = Numo::Int32.new(10).seq
|
|
3367
|
-
# => Numo::Int32#shape=[10]
|
|
3368
|
-
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
|
3369
|
-
|
|
3370
|
-
b.clip([3,4,1,1,1,4,4,4,4,4], 8)
|
|
3371
|
-
# => Numo::Int32#shape=[10]
|
|
3372
|
-
# [3, 4, 2, 3, 4, 5, 6, 7, 8, 8]
|
|
3373
|
-
*/
|
|
3374
|
-
static VALUE uint8_clip(VALUE self, VALUE min, VALUE max) {
|
|
3375
|
-
ndfunc_arg_in_t ain[3] = { { Qnil, 0 }, { cT, 0 }, { cT, 0 } };
|
|
3376
|
-
ndfunc_arg_out_t aout[1] = { { cT, 0 } };
|
|
3377
|
-
ndfunc_t ndf_min = { iter_uint8_clip_min, STRIDE_LOOP, 2, 1, ain, aout };
|
|
3378
|
-
ndfunc_t ndf_max = { iter_uint8_clip_max, STRIDE_LOOP, 2, 1, ain, aout };
|
|
3379
|
-
ndfunc_t ndf_both = { iter_uint8_clip, STRIDE_LOOP, 3, 1, ain, aout };
|
|
3380
|
-
|
|
3381
|
-
if (RTEST(min)) {
|
|
3382
|
-
if (RTEST(max)) {
|
|
3383
|
-
return na_ndloop(&ndf_both, 3, self, min, max);
|
|
3384
|
-
} else {
|
|
3385
|
-
return na_ndloop(&ndf_min, 2, self, min);
|
|
3386
|
-
}
|
|
3387
|
-
} else {
|
|
3388
|
-
if (RTEST(max)) {
|
|
3389
|
-
return na_ndloop(&ndf_max, 2, self, max);
|
|
3390
|
-
}
|
|
3391
|
-
}
|
|
3392
|
-
rb_raise(rb_eArgError, "min and max are not given");
|
|
3393
|
-
return Qnil;
|
|
3394
|
-
}
|
|
3395
|
-
|
|
3396
|
-
static void iter_uint8_sum(na_loop_t* const lp) {
|
|
3397
|
-
size_t n;
|
|
3398
|
-
char *p1, *p2;
|
|
3399
|
-
ssize_t s1;
|
|
3400
|
-
|
|
3401
|
-
INIT_COUNTER(lp, n);
|
|
3402
|
-
INIT_PTR(lp, 0, p1, s1);
|
|
3403
|
-
p2 = lp->args[1].ptr + lp->args[1].iter[0].pos;
|
|
3404
|
-
|
|
3405
|
-
*(u_int64_t*)p2 = f_sum(n, p1, s1);
|
|
3406
|
-
}
|
|
3407
|
-
|
|
3408
|
-
/*
|
|
3409
|
-
sum of self.
|
|
3410
|
-
@overload sum(axis:nil, keepdims:false)
|
|
3411
|
-
@param [Numeric,Array,Range] axis Performs sum along the axis.
|
|
3412
|
-
@param [TrueClass] keepdims If true, the reduced axes are left in the result array as
|
|
3413
|
-
dimensions with size one.
|
|
3414
|
-
@return [Numo::UInt8] returns result of sum.
|
|
3415
|
-
*/
|
|
3416
|
-
static VALUE uint8_sum(int argc, VALUE* argv, VALUE self) {
|
|
3417
|
-
VALUE v, reduce;
|
|
3418
|
-
ndfunc_arg_in_t ain[2] = { { cT, 0 }, { sym_reduce, 0 } };
|
|
3419
|
-
ndfunc_arg_out_t aout[1] = { { numo_cUInt64, 0 } };
|
|
3420
|
-
ndfunc_t ndf = { iter_uint8_sum, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE, 2, 1, ain, aout };
|
|
3421
|
-
|
|
3422
|
-
reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
|
|
3423
|
-
|
|
3424
|
-
v = na_ndloop(&ndf, 2, self, reduce);
|
|
3425
|
-
|
|
3426
|
-
return rb_funcall(v, rb_intern("extract"), 0);
|
|
3427
|
-
}
|
|
3428
|
-
|
|
3429
|
-
static void iter_uint8_prod(na_loop_t* const lp) {
|
|
3430
|
-
size_t n;
|
|
3431
|
-
char *p1, *p2;
|
|
3432
|
-
ssize_t s1;
|
|
3433
|
-
|
|
3434
|
-
INIT_COUNTER(lp, n);
|
|
3435
|
-
INIT_PTR(lp, 0, p1, s1);
|
|
3436
|
-
p2 = lp->args[1].ptr + lp->args[1].iter[0].pos;
|
|
3437
|
-
|
|
3438
|
-
*(u_int64_t*)p2 = f_prod(n, p1, s1);
|
|
3439
|
-
}
|
|
3440
|
-
|
|
3441
|
-
/*
|
|
3442
|
-
prod of self.
|
|
3443
|
-
@overload prod(axis:nil, keepdims:false)
|
|
3444
|
-
@param [Numeric,Array,Range] axis Performs prod along the axis.
|
|
3445
|
-
@param [TrueClass] keepdims If true, the reduced axes are left in the result array as
|
|
3446
|
-
dimensions with size one.
|
|
3447
|
-
@return [Numo::UInt8] returns result of prod.
|
|
3448
|
-
*/
|
|
3449
|
-
static VALUE uint8_prod(int argc, VALUE* argv, VALUE self) {
|
|
3450
|
-
VALUE v, reduce;
|
|
3451
|
-
ndfunc_arg_in_t ain[2] = { { cT, 0 }, { sym_reduce, 0 } };
|
|
3452
|
-
ndfunc_arg_out_t aout[1] = { { numo_cUInt64, 0 } };
|
|
3453
|
-
ndfunc_t ndf = { iter_uint8_prod, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE, 2, 1, ain, aout };
|
|
3454
|
-
|
|
3455
|
-
reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
|
|
3456
|
-
|
|
3457
|
-
v = na_ndloop(&ndf, 2, self, reduce);
|
|
3458
|
-
|
|
3459
|
-
return rb_funcall(v, rb_intern("extract"), 0);
|
|
3460
|
-
}
|
|
3461
|
-
|
|
3462
|
-
static void iter_uint8_min(na_loop_t* const lp) {
|
|
3463
|
-
size_t n;
|
|
3464
|
-
char *p1, *p2;
|
|
3465
|
-
ssize_t s1;
|
|
3466
|
-
|
|
3467
|
-
INIT_COUNTER(lp, n);
|
|
3468
|
-
INIT_PTR(lp, 0, p1, s1);
|
|
3469
|
-
p2 = lp->args[1].ptr + lp->args[1].iter[0].pos;
|
|
3470
|
-
|
|
3471
|
-
*(dtype*)p2 = f_min(n, p1, s1);
|
|
3472
|
-
}
|
|
3473
|
-
|
|
3474
|
-
/*
|
|
3475
|
-
min of self.
|
|
3476
|
-
@overload min(axis:nil, keepdims:false)
|
|
3477
|
-
@param [Numeric,Array,Range] axis Performs min along the axis.
|
|
3478
|
-
@param [TrueClass] keepdims If true, the reduced axes are left in the result array as
|
|
3479
|
-
dimensions with size one.
|
|
3480
|
-
@return [Numo::UInt8] returns result of min.
|
|
3481
|
-
*/
|
|
3482
|
-
static VALUE uint8_min(int argc, VALUE* argv, VALUE self) {
|
|
3483
|
-
VALUE v, reduce;
|
|
3484
|
-
ndfunc_arg_in_t ain[2] = { { cT, 0 }, { sym_reduce, 0 } };
|
|
3485
|
-
ndfunc_arg_out_t aout[1] = { { cT, 0 } };
|
|
3486
|
-
ndfunc_t ndf = { iter_uint8_min, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE, 2, 1, ain, aout };
|
|
3487
|
-
|
|
3488
|
-
reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
|
|
3489
|
-
|
|
3490
|
-
v = na_ndloop(&ndf, 2, self, reduce);
|
|
3491
|
-
|
|
3492
|
-
return uint8_extract(v);
|
|
3493
|
-
}
|
|
3494
|
-
|
|
3495
|
-
static void iter_uint8_max(na_loop_t* const lp) {
|
|
3496
|
-
size_t n;
|
|
3497
|
-
char *p1, *p2;
|
|
3498
|
-
ssize_t s1;
|
|
3499
|
-
|
|
3500
|
-
INIT_COUNTER(lp, n);
|
|
3501
|
-
INIT_PTR(lp, 0, p1, s1);
|
|
3502
|
-
p2 = lp->args[1].ptr + lp->args[1].iter[0].pos;
|
|
3503
|
-
|
|
3504
|
-
*(dtype*)p2 = f_max(n, p1, s1);
|
|
3505
|
-
}
|
|
3506
|
-
|
|
3507
|
-
/*
|
|
3508
|
-
max of self.
|
|
3509
|
-
@overload max(axis:nil, keepdims:false)
|
|
3510
|
-
@param [Numeric,Array,Range] axis Performs max along the axis.
|
|
3511
|
-
@param [TrueClass] keepdims If true, the reduced axes are left in the result array as
|
|
3512
|
-
dimensions with size one.
|
|
3513
|
-
@return [Numo::UInt8] returns result of max.
|
|
3514
|
-
*/
|
|
3515
|
-
static VALUE uint8_max(int argc, VALUE* argv, VALUE self) {
|
|
3516
|
-
VALUE v, reduce;
|
|
3517
|
-
ndfunc_arg_in_t ain[2] = { { cT, 0 }, { sym_reduce, 0 } };
|
|
3518
|
-
ndfunc_arg_out_t aout[1] = { { cT, 0 } };
|
|
3519
|
-
ndfunc_t ndf = { iter_uint8_max, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE, 2, 1, ain, aout };
|
|
3520
|
-
|
|
3521
|
-
reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
|
|
3522
|
-
|
|
3523
|
-
v = na_ndloop(&ndf, 2, self, reduce);
|
|
3524
|
-
|
|
3525
|
-
return uint8_extract(v);
|
|
3526
|
-
}
|
|
3527
|
-
|
|
3528
|
-
static void iter_uint8_ptp(na_loop_t* const lp) {
|
|
3529
|
-
size_t n;
|
|
3530
|
-
char *p1, *p2;
|
|
3531
|
-
ssize_t s1;
|
|
3532
|
-
|
|
3533
|
-
INIT_COUNTER(lp, n);
|
|
3534
|
-
INIT_PTR(lp, 0, p1, s1);
|
|
3535
|
-
p2 = lp->args[1].ptr + lp->args[1].iter[0].pos;
|
|
3536
|
-
|
|
3537
|
-
*(dtype*)p2 = f_ptp(n, p1, s1);
|
|
3538
|
-
}
|
|
3539
|
-
|
|
3540
|
-
/*
|
|
3541
|
-
ptp of self.
|
|
3542
|
-
@overload ptp(axis:nil, keepdims:false)
|
|
3543
|
-
@param [Numeric,Array,Range] axis Performs ptp along the axis.
|
|
3544
|
-
@param [TrueClass] keepdims If true, the reduced axes are left in the result array as
|
|
3545
|
-
dimensions with size one.
|
|
3546
|
-
@return [Numo::UInt8] returns result of ptp.
|
|
3547
|
-
*/
|
|
3548
|
-
static VALUE uint8_ptp(int argc, VALUE* argv, VALUE self) {
|
|
3549
|
-
VALUE v, reduce;
|
|
3550
|
-
ndfunc_arg_in_t ain[2] = { { cT, 0 }, { sym_reduce, 0 } };
|
|
3551
|
-
ndfunc_arg_out_t aout[1] = { { cT, 0 } };
|
|
3552
|
-
ndfunc_t ndf = { iter_uint8_ptp, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE, 2, 1, ain, aout };
|
|
3553
|
-
|
|
3554
|
-
reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
|
|
3555
|
-
|
|
3556
|
-
v = na_ndloop(&ndf, 2, self, reduce);
|
|
3557
|
-
|
|
3558
|
-
return uint8_extract(v);
|
|
3559
|
-
}
|
|
3560
|
-
|
|
3561
|
-
#define idx_t int64_t
|
|
3562
|
-
static void iter_uint8_max_index_index64(na_loop_t* const lp) {
|
|
3563
|
-
size_t n, idx;
|
|
3564
|
-
char *d_ptr, *i_ptr, *o_ptr;
|
|
3565
|
-
ssize_t d_step, i_step;
|
|
3566
|
-
|
|
3567
|
-
INIT_COUNTER(lp, n);
|
|
3568
|
-
INIT_PTR(lp, 0, d_ptr, d_step);
|
|
3569
|
-
|
|
3570
|
-
idx = f_max_index(n, d_ptr, d_step);
|
|
3571
|
-
|
|
3572
|
-
INIT_PTR(lp, 1, i_ptr, i_step);
|
|
3573
|
-
o_ptr = NDL_PTR(lp, 2);
|
|
3574
|
-
*(idx_t*)o_ptr = *(idx_t*)(i_ptr + i_step * idx);
|
|
3575
|
-
}
|
|
3576
|
-
#undef idx_t
|
|
3577
|
-
|
|
3578
|
-
#define idx_t int32_t
|
|
3579
|
-
static void iter_uint8_max_index_index32(na_loop_t* const lp) {
|
|
3580
|
-
size_t n, idx;
|
|
3581
|
-
char *d_ptr, *i_ptr, *o_ptr;
|
|
3582
|
-
ssize_t d_step, i_step;
|
|
3583
|
-
|
|
3584
|
-
INIT_COUNTER(lp, n);
|
|
3585
|
-
INIT_PTR(lp, 0, d_ptr, d_step);
|
|
3586
|
-
|
|
3587
|
-
idx = f_max_index(n, d_ptr, d_step);
|
|
3588
|
-
|
|
3589
|
-
INIT_PTR(lp, 1, i_ptr, i_step);
|
|
3590
|
-
o_ptr = NDL_PTR(lp, 2);
|
|
3591
|
-
*(idx_t*)o_ptr = *(idx_t*)(i_ptr + i_step * idx);
|
|
3592
|
-
}
|
|
3593
|
-
#undef idx_t
|
|
3594
|
-
|
|
3595
|
-
/*
|
|
3596
|
-
Index of the maximum value.
|
|
3597
|
-
@overload max_index(axis:nil)
|
|
3598
|
-
@param [Numeric,Array,Range] axis Finds maximum values along the axis and returns **flat
|
|
3599
|
-
1-d indices**.
|
|
3600
|
-
@return [Integer,Numo::Int] returns result indices.
|
|
3601
|
-
@see #argmax
|
|
3602
|
-
@see #max
|
|
3603
|
-
|
|
3604
|
-
@example
|
|
3605
|
-
a = Numo::NArray[3,4,1,2]
|
|
3606
|
-
a.max_index #=> 1
|
|
3607
|
-
|
|
3608
|
-
b = Numo::NArray[[3,4,1],[2,0,5]]
|
|
3609
|
-
b.max_index #=> 5
|
|
3610
|
-
b.max_index(axis:1) #=> [1, 5]
|
|
3611
|
-
b.max_index(axis:0) #=> [0, 1, 5]
|
|
3612
|
-
b[b.max_index(axis:0)] #=> [3, 4, 5]
|
|
3613
|
-
*/
|
|
3614
|
-
static VALUE uint8_max_index(int argc, VALUE* argv, VALUE self) {
|
|
3615
|
-
narray_t* na;
|
|
3616
|
-
VALUE idx, reduce;
|
|
3617
|
-
ndfunc_arg_in_t ain[3] = { { Qnil, 0 }, { Qnil, 0 }, { sym_reduce, 0 } };
|
|
3618
|
-
ndfunc_arg_out_t aout[1] = { { 0, 0, 0 } };
|
|
3619
|
-
ndfunc_t ndf = { 0, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE | NDF_EXTRACT, 3, 1, ain, aout };
|
|
3620
|
-
|
|
3621
|
-
GetNArray(self, na);
|
|
3622
|
-
if (na->ndim == 0) {
|
|
3623
|
-
return INT2FIX(0);
|
|
3624
|
-
}
|
|
3625
|
-
if (na->size > (~(u_int32_t)0)) {
|
|
3626
|
-
aout[0].type = numo_cInt64;
|
|
3627
|
-
idx = nary_new(numo_cInt64, na->ndim, na->shape);
|
|
3628
|
-
ndf.func = iter_uint8_max_index_index64;
|
|
3629
|
-
|
|
3630
|
-
reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
|
|
3631
|
-
|
|
3632
|
-
} else {
|
|
3633
|
-
aout[0].type = numo_cInt32;
|
|
3634
|
-
idx = nary_new(numo_cInt32, na->ndim, na->shape);
|
|
3635
|
-
ndf.func = iter_uint8_max_index_index32;
|
|
3636
|
-
|
|
3637
|
-
reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
|
|
3638
|
-
}
|
|
3639
|
-
rb_funcall(idx, rb_intern("seq"), 0);
|
|
3640
|
-
|
|
3641
|
-
return na_ndloop(&ndf, 3, self, idx, reduce);
|
|
3642
|
-
}
|
|
3643
|
-
|
|
3644
|
-
#define idx_t int64_t
|
|
3645
|
-
static void iter_uint8_min_index_index64(na_loop_t* const lp) {
|
|
3646
|
-
size_t n, idx;
|
|
3647
|
-
char *d_ptr, *i_ptr, *o_ptr;
|
|
3648
|
-
ssize_t d_step, i_step;
|
|
3649
|
-
|
|
3650
|
-
INIT_COUNTER(lp, n);
|
|
3651
|
-
INIT_PTR(lp, 0, d_ptr, d_step);
|
|
3652
|
-
|
|
3653
|
-
idx = f_min_index(n, d_ptr, d_step);
|
|
3654
|
-
|
|
3655
|
-
INIT_PTR(lp, 1, i_ptr, i_step);
|
|
3656
|
-
o_ptr = NDL_PTR(lp, 2);
|
|
3657
|
-
*(idx_t*)o_ptr = *(idx_t*)(i_ptr + i_step * idx);
|
|
3658
|
-
}
|
|
3659
|
-
#undef idx_t
|
|
3660
|
-
|
|
3661
|
-
#define idx_t int32_t
|
|
3662
|
-
static void iter_uint8_min_index_index32(na_loop_t* const lp) {
|
|
3663
|
-
size_t n, idx;
|
|
3664
|
-
char *d_ptr, *i_ptr, *o_ptr;
|
|
3665
|
-
ssize_t d_step, i_step;
|
|
3666
|
-
|
|
3667
|
-
INIT_COUNTER(lp, n);
|
|
3668
|
-
INIT_PTR(lp, 0, d_ptr, d_step);
|
|
3669
|
-
|
|
3670
|
-
idx = f_min_index(n, d_ptr, d_step);
|
|
3671
|
-
|
|
3672
|
-
INIT_PTR(lp, 1, i_ptr, i_step);
|
|
3673
|
-
o_ptr = NDL_PTR(lp, 2);
|
|
3674
|
-
*(idx_t*)o_ptr = *(idx_t*)(i_ptr + i_step * idx);
|
|
3675
|
-
}
|
|
3676
|
-
#undef idx_t
|
|
3677
|
-
|
|
3678
|
-
/*
|
|
3679
|
-
Index of the minimum value.
|
|
3680
|
-
@overload min_index(axis:nil)
|
|
3681
|
-
@param [Numeric,Array,Range] axis Finds minimum values along the axis and returns **flat
|
|
3682
|
-
1-d indices**.
|
|
3683
|
-
@return [Integer,Numo::Int] returns result indices.
|
|
3684
|
-
@see #argmin
|
|
3685
|
-
@see #min
|
|
3686
|
-
|
|
3687
|
-
@example
|
|
3688
|
-
a = Numo::NArray[3,4,1,2]
|
|
3689
|
-
a.min_index #=> 2
|
|
3690
|
-
|
|
3691
|
-
b = Numo::NArray[[3,4,1],[2,0,5]]
|
|
3692
|
-
b.min_index #=> 4
|
|
3693
|
-
b.min_index(axis:1) #=> [2, 4]
|
|
3694
|
-
b.min_index(axis:0) #=> [3, 4, 2]
|
|
3695
|
-
b[b.min_index(axis:0)] #=> [2, 0, 1]
|
|
3696
|
-
*/
|
|
3697
|
-
static VALUE uint8_min_index(int argc, VALUE* argv, VALUE self) {
|
|
3698
|
-
narray_t* na;
|
|
3699
|
-
VALUE idx, reduce;
|
|
3700
|
-
ndfunc_arg_in_t ain[3] = { { Qnil, 0 }, { Qnil, 0 }, { sym_reduce, 0 } };
|
|
3701
|
-
ndfunc_arg_out_t aout[1] = { { 0, 0, 0 } };
|
|
3702
|
-
ndfunc_t ndf = { 0, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE | NDF_EXTRACT, 3, 1, ain, aout };
|
|
3703
|
-
|
|
3704
|
-
GetNArray(self, na);
|
|
3705
|
-
if (na->ndim == 0) {
|
|
3706
|
-
return INT2FIX(0);
|
|
3707
|
-
}
|
|
3708
|
-
if (na->size > (~(u_int32_t)0)) {
|
|
3709
|
-
aout[0].type = numo_cInt64;
|
|
3710
|
-
idx = nary_new(numo_cInt64, na->ndim, na->shape);
|
|
3711
|
-
ndf.func = iter_uint8_min_index_index64;
|
|
3712
|
-
|
|
3713
|
-
reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
|
|
3714
|
-
|
|
3715
|
-
} else {
|
|
3716
|
-
aout[0].type = numo_cInt32;
|
|
3717
|
-
idx = nary_new(numo_cInt32, na->ndim, na->shape);
|
|
3718
|
-
ndf.func = iter_uint8_min_index_index32;
|
|
3719
|
-
|
|
3720
|
-
reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
|
|
3721
|
-
}
|
|
3722
|
-
rb_funcall(idx, rb_intern("seq"), 0);
|
|
3723
|
-
|
|
3724
|
-
return na_ndloop(&ndf, 3, self, idx, reduce);
|
|
3725
|
-
}
|
|
3726
|
-
|
|
3727
|
-
#define idx_t int64_t
|
|
3728
|
-
static void iter_uint8_argmax_arg64(na_loop_t* const lp) {
|
|
3729
|
-
size_t n, idx;
|
|
3730
|
-
char *d_ptr, *o_ptr;
|
|
3731
|
-
ssize_t d_step;
|
|
3732
|
-
|
|
3733
|
-
INIT_COUNTER(lp, n);
|
|
3734
|
-
INIT_PTR(lp, 0, d_ptr, d_step);
|
|
3735
|
-
|
|
3736
|
-
idx = f_max_index(n, d_ptr, d_step);
|
|
3737
|
-
|
|
3738
|
-
o_ptr = NDL_PTR(lp, 1);
|
|
3739
|
-
*(idx_t*)o_ptr = (idx_t)idx;
|
|
3740
|
-
}
|
|
3741
|
-
#undef idx_t
|
|
3742
|
-
|
|
3743
|
-
#define idx_t int32_t
|
|
3744
|
-
static void iter_uint8_argmax_arg32(na_loop_t* const lp) {
|
|
3745
|
-
size_t n, idx;
|
|
3746
|
-
char *d_ptr, *o_ptr;
|
|
3747
|
-
ssize_t d_step;
|
|
3748
|
-
|
|
3749
|
-
INIT_COUNTER(lp, n);
|
|
3750
|
-
INIT_PTR(lp, 0, d_ptr, d_step);
|
|
3751
|
-
|
|
3752
|
-
idx = f_max_index(n, d_ptr, d_step);
|
|
3753
|
-
|
|
3754
|
-
o_ptr = NDL_PTR(lp, 1);
|
|
3755
|
-
*(idx_t*)o_ptr = (idx_t)idx;
|
|
3756
|
-
}
|
|
3757
|
-
#undef idx_t
|
|
3758
|
-
|
|
3759
|
-
/*
|
|
3760
|
-
Index of the maximum value.
|
|
3761
|
-
@overload argmax(axis:nil)
|
|
3762
|
-
@param [Numeric,Array,Range] axis Finds maximum values along the axis and returns **indices
|
|
3763
|
-
along the axis**.
|
|
3764
|
-
@return [Integer,Numo::Int] returns the result indices.
|
|
3765
|
-
@see #max_index
|
|
3766
|
-
@see #max
|
|
3767
|
-
|
|
3768
|
-
@example
|
|
3769
|
-
a = Numo::NArray[3,4,1,2]
|
|
3770
|
-
a.argmax #=> 1
|
|
3771
|
-
|
|
3772
|
-
b = Numo::NArray[[3,4,1],[2,0,5]]
|
|
3773
|
-
b.argmax #=> 5
|
|
3774
|
-
b.argmax(axis:1) #=> [1, 2]
|
|
3775
|
-
b.argmax(axis:0) #=> [0, 0, 1]
|
|
3776
|
-
b.at(b.argmax(axis:0), 0..-1) #=> [3, 4, 5]
|
|
3777
|
-
*/
|
|
3778
|
-
static VALUE uint8_argmax(int argc, VALUE* argv, VALUE self) {
|
|
3779
|
-
narray_t* na;
|
|
3780
|
-
VALUE reduce;
|
|
3781
|
-
ndfunc_arg_in_t ain[2] = { { Qnil, 0 }, { sym_reduce, 0 } };
|
|
3782
|
-
ndfunc_arg_out_t aout[1] = { { 0, 0, 0 } };
|
|
3783
|
-
ndfunc_t ndf = { 0, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE | NDF_EXTRACT, 2, 1, ain, aout };
|
|
3784
|
-
|
|
3785
|
-
GetNArray(self, na);
|
|
3786
|
-
if (na->ndim == 0) {
|
|
3787
|
-
return INT2FIX(0);
|
|
3788
|
-
}
|
|
3789
|
-
if (na->size > (~(u_int32_t)0)) {
|
|
3790
|
-
aout[0].type = numo_cInt64;
|
|
3791
|
-
ndf.func = iter_uint8_argmax_arg64;
|
|
3792
|
-
|
|
3793
|
-
reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
|
|
3794
|
-
|
|
3795
|
-
} else {
|
|
3796
|
-
aout[0].type = numo_cInt32;
|
|
3797
|
-
ndf.func = iter_uint8_argmax_arg32;
|
|
3798
|
-
|
|
3799
|
-
reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
|
|
3800
|
-
}
|
|
3801
|
-
|
|
3802
|
-
return na_ndloop(&ndf, 2, self, reduce);
|
|
3803
|
-
}
|
|
3804
|
-
|
|
3805
|
-
#define idx_t int64_t
|
|
3806
|
-
static void iter_uint8_argmin_arg64(na_loop_t* const lp) {
|
|
3807
|
-
size_t n, idx;
|
|
3808
|
-
char *d_ptr, *o_ptr;
|
|
3809
|
-
ssize_t d_step;
|
|
3810
|
-
|
|
3811
|
-
INIT_COUNTER(lp, n);
|
|
3812
|
-
INIT_PTR(lp, 0, d_ptr, d_step);
|
|
3813
|
-
|
|
3814
|
-
idx = f_min_index(n, d_ptr, d_step);
|
|
3815
|
-
|
|
3816
|
-
o_ptr = NDL_PTR(lp, 1);
|
|
3817
|
-
*(idx_t*)o_ptr = (idx_t)idx;
|
|
3818
|
-
}
|
|
3819
|
-
#undef idx_t
|
|
3820
|
-
|
|
3821
|
-
#define idx_t int32_t
|
|
3822
|
-
static void iter_uint8_argmin_arg32(na_loop_t* const lp) {
|
|
3823
|
-
size_t n, idx;
|
|
3824
|
-
char *d_ptr, *o_ptr;
|
|
3825
|
-
ssize_t d_step;
|
|
3826
|
-
|
|
3827
|
-
INIT_COUNTER(lp, n);
|
|
3828
|
-
INIT_PTR(lp, 0, d_ptr, d_step);
|
|
3829
|
-
|
|
3830
|
-
idx = f_min_index(n, d_ptr, d_step);
|
|
3831
|
-
|
|
3832
|
-
o_ptr = NDL_PTR(lp, 1);
|
|
3833
|
-
*(idx_t*)o_ptr = (idx_t)idx;
|
|
3834
|
-
}
|
|
3835
|
-
#undef idx_t
|
|
3836
|
-
|
|
3837
|
-
/*
|
|
3838
|
-
Index of the minimum value.
|
|
3839
|
-
@overload argmin(axis:nil)
|
|
3840
|
-
@param [Numeric,Array,Range] axis Finds minimum values along the axis and returns **indices
|
|
3841
|
-
along the axis**.
|
|
3842
|
-
@return [Integer,Numo::Int] returns the result indices.
|
|
3843
|
-
@see #min_index
|
|
3844
|
-
@see #min
|
|
3845
|
-
|
|
3846
|
-
@example
|
|
3847
|
-
a = Numo::NArray[3,4,1,2]
|
|
3848
|
-
a.argmin #=> 2
|
|
3849
|
-
|
|
3850
|
-
b = Numo::NArray[[3,4,1],[2,0,5]]
|
|
3851
|
-
b.argmin #=> 4
|
|
3852
|
-
b.argmin(axis:1) #=> [2, 1]
|
|
3853
|
-
b.argmin(axis:0) #=> [1, 1, 0]
|
|
3854
|
-
b.at(b.argmin(axis:0), 0..-1) #=> [2, 0, 1]
|
|
3855
|
-
*/
|
|
3856
|
-
static VALUE uint8_argmin(int argc, VALUE* argv, VALUE self) {
|
|
3857
|
-
narray_t* na;
|
|
3858
|
-
VALUE reduce;
|
|
3859
|
-
ndfunc_arg_in_t ain[2] = { { Qnil, 0 }, { sym_reduce, 0 } };
|
|
3860
|
-
ndfunc_arg_out_t aout[1] = { { 0, 0, 0 } };
|
|
3861
|
-
ndfunc_t ndf = { 0, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE | NDF_EXTRACT, 2, 1, ain, aout };
|
|
3862
|
-
|
|
3863
|
-
GetNArray(self, na);
|
|
3864
|
-
if (na->ndim == 0) {
|
|
3865
|
-
return INT2FIX(0);
|
|
3866
|
-
}
|
|
3867
|
-
if (na->size > (~(u_int32_t)0)) {
|
|
3868
|
-
aout[0].type = numo_cInt64;
|
|
3869
|
-
ndf.func = iter_uint8_argmin_arg64;
|
|
3870
|
-
|
|
3871
|
-
reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
|
|
3872
|
-
|
|
3873
|
-
} else {
|
|
3874
|
-
aout[0].type = numo_cInt32;
|
|
3875
|
-
ndf.func = iter_uint8_argmin_arg32;
|
|
3876
|
-
|
|
3877
|
-
reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
|
|
3878
|
-
}
|
|
3879
|
-
|
|
3880
|
-
return na_ndloop(&ndf, 2, self, reduce);
|
|
3881
|
-
}
|
|
3882
|
-
|
|
3883
|
-
static void iter_uint8_minmax(na_loop_t* const lp) {
|
|
3884
|
-
size_t n;
|
|
3885
|
-
char* p1;
|
|
3886
|
-
ssize_t s1;
|
|
3887
|
-
dtype xmin, xmax;
|
|
3888
|
-
|
|
3889
|
-
INIT_COUNTER(lp, n);
|
|
3890
|
-
INIT_PTR(lp, 0, p1, s1);
|
|
3891
|
-
|
|
3892
|
-
f_minmax(n, p1, s1, &xmin, &xmax);
|
|
3893
|
-
|
|
3894
|
-
*(dtype*)(lp->args[1].ptr + lp->args[1].iter[0].pos) = xmin;
|
|
3895
|
-
*(dtype*)(lp->args[2].ptr + lp->args[2].iter[0].pos) = xmax;
|
|
3896
|
-
}
|
|
3897
|
-
|
|
3898
|
-
/*
|
|
3899
|
-
minmax of self.
|
|
3900
|
-
@overload minmax(axis:nil, keepdims:false)
|
|
3901
|
-
@param [Numeric,Array,Range] axis Finds min-max along the axis.
|
|
3902
|
-
@param [TrueClass] keepdims (keyword) If true, the reduced axes are left in the result array
|
|
3903
|
-
as dimensions with size one.
|
|
3904
|
-
@return [Numo::UInt8,Numo::UInt8] min and max of self.
|
|
3905
|
-
*/
|
|
3906
|
-
static VALUE uint8_minmax(int argc, VALUE* argv, VALUE self) {
|
|
3907
|
-
VALUE reduce;
|
|
3908
|
-
ndfunc_arg_in_t ain[2] = { { cT, 0 }, { sym_reduce, 0 } };
|
|
3909
|
-
ndfunc_arg_out_t aout[2] = { { cT, 0 }, { cT, 0 } };
|
|
3910
|
-
ndfunc_t ndf = {
|
|
3911
|
-
iter_uint8_minmax, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE | NDF_EXTRACT, 2, 2, ain, aout
|
|
3912
|
-
};
|
|
3913
|
-
|
|
3914
|
-
reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
|
|
3915
|
-
|
|
3916
|
-
return na_ndloop(&ndf, 2, self, reduce);
|
|
3917
|
-
}
|
|
3918
|
-
|
|
3919
|
-
static void iter_uint8_s_maximum(na_loop_t* const lp) {
|
|
3920
|
-
size_t i, n;
|
|
3921
|
-
char *p1, *p2, *p3;
|
|
3922
|
-
ssize_t s1, s2, s3;
|
|
3923
|
-
|
|
3924
|
-
INIT_COUNTER(lp, n);
|
|
3925
|
-
INIT_PTR(lp, 0, p1, s1);
|
|
3926
|
-
INIT_PTR(lp, 1, p2, s2);
|
|
3927
|
-
INIT_PTR(lp, 2, p3, s3);
|
|
3928
|
-
|
|
3929
|
-
for (i = 0; i < n; i++) {
|
|
3930
|
-
dtype x, y, z;
|
|
3931
|
-
GET_DATA_STRIDE(p1, s1, dtype, x);
|
|
3932
|
-
GET_DATA_STRIDE(p2, s2, dtype, y);
|
|
3933
|
-
GET_DATA(p3, dtype, z);
|
|
3934
|
-
z = f_maximum(x, y);
|
|
3935
|
-
SET_DATA_STRIDE(p3, s3, dtype, z);
|
|
3936
|
-
}
|
|
3937
|
-
}
|
|
3938
|
-
|
|
3939
|
-
static VALUE uint8_s_maximum(int argc, VALUE* argv, VALUE mod) {
|
|
3940
|
-
VALUE a1 = Qnil;
|
|
3941
|
-
VALUE a2 = Qnil;
|
|
3942
|
-
ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
|
|
3943
|
-
ndfunc_arg_out_t aout[1] = { { cT, 0 } };
|
|
3944
|
-
ndfunc_t ndf = { iter_uint8_s_maximum, STRIDE_LOOP_NIP, 2, 1, ain, aout };
|
|
3945
|
-
|
|
3946
|
-
rb_scan_args(argc, argv, "20", &a1, &a2);
|
|
3947
|
-
|
|
3948
|
-
return na_ndloop(&ndf, 2, a1, a2);
|
|
3949
|
-
}
|
|
3950
|
-
|
|
3951
|
-
static void iter_uint8_s_minimum(na_loop_t* const lp) {
|
|
3952
|
-
size_t i, n;
|
|
3953
|
-
char *p1, *p2, *p3;
|
|
3954
|
-
ssize_t s1, s2, s3;
|
|
3955
|
-
|
|
3956
|
-
INIT_COUNTER(lp, n);
|
|
3957
|
-
INIT_PTR(lp, 0, p1, s1);
|
|
3958
|
-
INIT_PTR(lp, 1, p2, s2);
|
|
3959
|
-
INIT_PTR(lp, 2, p3, s3);
|
|
3960
|
-
|
|
3961
|
-
for (i = 0; i < n; i++) {
|
|
3962
|
-
dtype x, y, z;
|
|
3963
|
-
GET_DATA_STRIDE(p1, s1, dtype, x);
|
|
3964
|
-
GET_DATA_STRIDE(p2, s2, dtype, y);
|
|
3965
|
-
GET_DATA(p3, dtype, z);
|
|
3966
|
-
z = f_minimum(x, y);
|
|
3967
|
-
SET_DATA_STRIDE(p3, s3, dtype, z);
|
|
3968
|
-
}
|
|
3969
|
-
}
|
|
3970
|
-
|
|
3971
|
-
static VALUE uint8_s_minimum(int argc, VALUE* argv, VALUE mod) {
|
|
3972
|
-
VALUE a1 = Qnil;
|
|
3973
|
-
VALUE a2 = Qnil;
|
|
3974
|
-
ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
|
|
3975
|
-
ndfunc_arg_out_t aout[1] = { { cT, 0 } };
|
|
3976
|
-
ndfunc_t ndf = { iter_uint8_s_minimum, STRIDE_LOOP_NIP, 2, 1, ain, aout };
|
|
3977
|
-
|
|
3978
|
-
rb_scan_args(argc, argv, "20", &a1, &a2);
|
|
3979
|
-
|
|
3980
|
-
return na_ndloop(&ndf, 2, a1, a2);
|
|
3981
|
-
}
|
|
3982
|
-
|
|
3983
|
-
// ------- Integer count without weights -------
|
|
3984
|
-
|
|
3985
|
-
static void iter_uint8_bincount_32(na_loop_t* const lp) {
|
|
3986
|
-
size_t i, x, n;
|
|
3987
|
-
char *p1, *p2;
|
|
3988
|
-
ssize_t s1, s2;
|
|
3989
|
-
size_t* idx1;
|
|
3990
|
-
|
|
3991
|
-
INIT_PTR_IDX(lp, 0, p1, s1, idx1);
|
|
3992
|
-
INIT_PTR(lp, 1, p2, s2);
|
|
3993
|
-
i = lp->args[0].shape[0];
|
|
3994
|
-
n = lp->args[1].shape[0];
|
|
3995
|
-
|
|
3996
|
-
// initialize
|
|
3997
|
-
for (x = 0; x < n; x++) {
|
|
3998
|
-
*(u_int32_t*)(p2 + s2 * x) = 0;
|
|
3999
|
-
}
|
|
4000
|
-
|
|
4001
|
-
if (idx1) {
|
|
4002
|
-
for (; i--;) {
|
|
4003
|
-
GET_DATA_INDEX(p1, idx1, dtype, x);
|
|
4004
|
-
(*(u_int32_t*)(p2 + s2 * x))++;
|
|
4005
|
-
}
|
|
4006
|
-
} else {
|
|
4007
|
-
for (; i--;) {
|
|
4008
|
-
GET_DATA_STRIDE(p1, s1, dtype, x);
|
|
4009
|
-
(*(u_int32_t*)(p2 + s2 * x))++;
|
|
4010
|
-
}
|
|
4011
|
-
}
|
|
4012
|
-
}
|
|
4013
|
-
|
|
4014
|
-
static VALUE uint8_bincount_32(VALUE self, size_t length) {
|
|
4015
|
-
size_t shape_out[1] = { length };
|
|
4016
|
-
ndfunc_arg_in_t ain[1] = { { cT, 1 } };
|
|
4017
|
-
ndfunc_arg_out_t aout[1] = { { numo_cUInt32, 1, shape_out } };
|
|
4018
|
-
ndfunc_t ndf = {
|
|
4019
|
-
iter_uint8_bincount_32, NO_LOOP | NDF_STRIDE_LOOP | NDF_INDEX_LOOP, 1, 1, ain, aout
|
|
4020
|
-
};
|
|
4021
|
-
|
|
4022
|
-
return na_ndloop(&ndf, 1, self);
|
|
4023
|
-
}
|
|
4024
|
-
|
|
4025
|
-
static void iter_uint8_bincount_64(na_loop_t* const lp) {
|
|
4026
|
-
size_t i, x, n;
|
|
4027
|
-
char *p1, *p2;
|
|
4028
|
-
ssize_t s1, s2;
|
|
4029
|
-
size_t* idx1;
|
|
4030
|
-
|
|
4031
|
-
INIT_PTR_IDX(lp, 0, p1, s1, idx1);
|
|
4032
|
-
INIT_PTR(lp, 1, p2, s2);
|
|
4033
|
-
i = lp->args[0].shape[0];
|
|
4034
|
-
n = lp->args[1].shape[0];
|
|
4035
|
-
|
|
4036
|
-
// initialize
|
|
4037
|
-
for (x = 0; x < n; x++) {
|
|
4038
|
-
*(u_int64_t*)(p2 + s2 * x) = 0;
|
|
4039
|
-
}
|
|
4040
|
-
|
|
4041
|
-
if (idx1) {
|
|
4042
|
-
for (; i--;) {
|
|
4043
|
-
GET_DATA_INDEX(p1, idx1, dtype, x);
|
|
4044
|
-
(*(u_int64_t*)(p2 + s2 * x))++;
|
|
4045
|
-
}
|
|
4046
|
-
} else {
|
|
4047
|
-
for (; i--;) {
|
|
4048
|
-
GET_DATA_STRIDE(p1, s1, dtype, x);
|
|
4049
|
-
(*(u_int64_t*)(p2 + s2 * x))++;
|
|
4050
|
-
}
|
|
4051
|
-
}
|
|
4052
|
-
}
|
|
4053
|
-
|
|
4054
|
-
static VALUE uint8_bincount_64(VALUE self, size_t length) {
|
|
4055
|
-
size_t shape_out[1] = { length };
|
|
4056
|
-
ndfunc_arg_in_t ain[1] = { { cT, 1 } };
|
|
4057
|
-
ndfunc_arg_out_t aout[1] = { { numo_cUInt64, 1, shape_out } };
|
|
4058
|
-
ndfunc_t ndf = {
|
|
4059
|
-
iter_uint8_bincount_64, NO_LOOP | NDF_STRIDE_LOOP | NDF_INDEX_LOOP, 1, 1, ain, aout
|
|
4060
|
-
};
|
|
4061
|
-
|
|
4062
|
-
return na_ndloop(&ndf, 1, self);
|
|
4063
|
-
}
|
|
4064
|
-
// ------- end of Integer count without weights -------
|
|
4065
|
-
|
|
4066
|
-
// ------- Float count with weights -------
|
|
4067
|
-
|
|
4068
|
-
static void iter_uint8_bincount_sf(na_loop_t* const lp) {
|
|
4069
|
-
float w;
|
|
4070
|
-
size_t i, x, n, m;
|
|
4071
|
-
char *p1, *p2, *p3;
|
|
4072
|
-
ssize_t s1, s2, s3;
|
|
4073
|
-
|
|
4074
|
-
INIT_PTR(lp, 0, p1, s1);
|
|
4075
|
-
INIT_PTR(lp, 1, p2, s2);
|
|
4076
|
-
INIT_PTR(lp, 2, p3, s3);
|
|
4077
|
-
i = lp->args[0].shape[0];
|
|
4078
|
-
m = lp->args[1].shape[0];
|
|
4079
|
-
n = lp->args[2].shape[0];
|
|
4080
|
-
|
|
4081
|
-
if (i != m) {
|
|
4082
|
-
rb_raise(nary_eShapeError, "size mismatch along last axis between self and weight");
|
|
4083
|
-
}
|
|
4084
|
-
|
|
4085
|
-
// initialize
|
|
4086
|
-
for (x = 0; x < n; x++) {
|
|
4087
|
-
*(float*)(p3 + s3 * x) = 0;
|
|
4088
|
-
}
|
|
4089
|
-
for (; i--;) {
|
|
4090
|
-
GET_DATA_STRIDE(p1, s1, dtype, x);
|
|
4091
|
-
GET_DATA_STRIDE(p2, s2, float, w);
|
|
4092
|
-
(*(float*)(p3 + s3 * x)) += w;
|
|
4093
|
-
}
|
|
4094
|
-
}
|
|
4095
|
-
|
|
4096
|
-
static VALUE uint8_bincount_sf(VALUE self, VALUE weight, size_t length) {
|
|
4097
|
-
size_t shape_out[1] = { length };
|
|
4098
|
-
ndfunc_arg_in_t ain[2] = { { cT, 1 }, { numo_cSFloat, 1 } };
|
|
4099
|
-
ndfunc_arg_out_t aout[1] = { { numo_cSFloat, 1, shape_out } };
|
|
4100
|
-
ndfunc_t ndf = { iter_uint8_bincount_sf, NO_LOOP | NDF_STRIDE_LOOP, 2, 1, ain, aout };
|
|
4101
|
-
|
|
4102
|
-
return na_ndloop(&ndf, 2, self, weight);
|
|
4103
|
-
}
|
|
4104
|
-
|
|
4105
|
-
static void iter_uint8_bincount_df(na_loop_t* const lp) {
|
|
4106
|
-
double w;
|
|
4107
|
-
size_t i, x, n, m;
|
|
4108
|
-
char *p1, *p2, *p3;
|
|
4109
|
-
ssize_t s1, s2, s3;
|
|
4110
|
-
|
|
4111
|
-
INIT_PTR(lp, 0, p1, s1);
|
|
4112
|
-
INIT_PTR(lp, 1, p2, s2);
|
|
4113
|
-
INIT_PTR(lp, 2, p3, s3);
|
|
4114
|
-
i = lp->args[0].shape[0];
|
|
4115
|
-
m = lp->args[1].shape[0];
|
|
4116
|
-
n = lp->args[2].shape[0];
|
|
4117
|
-
|
|
4118
|
-
if (i != m) {
|
|
4119
|
-
rb_raise(nary_eShapeError, "size mismatch along last axis between self and weight");
|
|
4120
|
-
}
|
|
4121
|
-
|
|
4122
|
-
// initialize
|
|
4123
|
-
for (x = 0; x < n; x++) {
|
|
4124
|
-
*(double*)(p3 + s3 * x) = 0;
|
|
4125
|
-
}
|
|
4126
|
-
for (; i--;) {
|
|
4127
|
-
GET_DATA_STRIDE(p1, s1, dtype, x);
|
|
4128
|
-
GET_DATA_STRIDE(p2, s2, double, w);
|
|
4129
|
-
(*(double*)(p3 + s3 * x)) += w;
|
|
4130
|
-
}
|
|
4131
|
-
}
|
|
4132
|
-
|
|
4133
|
-
static VALUE uint8_bincount_df(VALUE self, VALUE weight, size_t length) {
|
|
4134
|
-
size_t shape_out[1] = { length };
|
|
4135
|
-
ndfunc_arg_in_t ain[2] = { { cT, 1 }, { numo_cDFloat, 1 } };
|
|
4136
|
-
ndfunc_arg_out_t aout[1] = { { numo_cDFloat, 1, shape_out } };
|
|
4137
|
-
ndfunc_t ndf = { iter_uint8_bincount_df, NO_LOOP | NDF_STRIDE_LOOP, 2, 1, ain, aout };
|
|
4138
|
-
|
|
4139
|
-
return na_ndloop(&ndf, 2, self, weight);
|
|
4140
|
-
}
|
|
4141
|
-
// ------- end of Float count with weights -------
|
|
4142
|
-
|
|
4143
|
-
/*
|
|
4144
|
-
Count the number of occurrences of each non-negative integer value.
|
|
4145
|
-
Only Integer-types has this method.
|
|
4146
|
-
|
|
4147
|
-
@overload bincount([weight], minlength:nil)
|
|
4148
|
-
@param [SFloat or DFloat or Array] weight (optional) Array of
|
|
4149
|
-
float values. Its size along last axis should be same as that of self.
|
|
4150
|
-
@param [Integer] minlength (keyword, optional) Minimum size along
|
|
4151
|
-
last axis for the output array.
|
|
4152
|
-
@return [UInt32 or UInt64 or SFloat or DFloat]
|
|
4153
|
-
Returns Float NArray if weight array is supplied,
|
|
4154
|
-
otherwise returns UInt32 or UInt64 depending on the size along last axis.
|
|
4155
|
-
@example
|
|
4156
|
-
Numo::Int32[0..4].bincount
|
|
4157
|
-
# => Numo::UInt32#shape=[5]
|
|
4158
|
-
# [1, 1, 1, 1, 1]
|
|
4159
|
-
|
|
4160
|
-
Numo::Int32[0, 1, 1, 3, 2, 1, 7].bincount
|
|
4161
|
-
# => Numo::UInt32#shape=[8]
|
|
4162
|
-
# [1, 3, 1, 1, 0, 0, 0, 1]
|
|
4163
|
-
|
|
4164
|
-
x = Numo::Int32[0, 1, 1, 3, 2, 1, 7, 23]
|
|
4165
|
-
x.bincount.size == x.max+1
|
|
4166
|
-
# => true
|
|
4167
|
-
|
|
4168
|
-
w = Numo::DFloat[0.3, 0.5, 0.2, 0.7, 1.0, -0.6]
|
|
4169
|
-
x = Numo::Int32[0, 1, 1, 2, 2, 2]
|
|
4170
|
-
x.bincount(w)
|
|
4171
|
-
# => Numo::DFloat#shape=[3]
|
|
4172
|
-
# [0.3, 0.7, 1.1]
|
|
4173
|
-
|
|
4174
|
-
*/
|
|
4175
|
-
static VALUE uint8_bincount(int argc, VALUE* argv, VALUE self) {
|
|
4176
|
-
VALUE weight = Qnil, kw = Qnil;
|
|
4177
|
-
VALUE opts[1] = { Qundef };
|
|
4178
|
-
VALUE v, wclass;
|
|
4179
|
-
ID table[1] = { id_minlength };
|
|
4180
|
-
size_t length, minlength;
|
|
4181
|
-
|
|
4182
|
-
rb_scan_args(argc, argv, "01:", &weight, &kw);
|
|
4183
|
-
rb_get_kwargs(kw, table, 0, 1, opts);
|
|
4184
|
-
|
|
4185
|
-
v = uint8_max(0, 0, self);
|
|
4186
|
-
|
|
4187
|
-
length = NUM2SIZET(v) + 1;
|
|
4188
|
-
|
|
4189
|
-
if (opts[0] != Qundef) {
|
|
4190
|
-
minlength = NUM2SIZET(opts[0]);
|
|
4191
|
-
if (minlength > length) {
|
|
4192
|
-
length = minlength;
|
|
4193
|
-
}
|
|
4194
|
-
}
|
|
4195
|
-
|
|
4196
|
-
if (NIL_P(weight)) {
|
|
4197
|
-
if (length > 4294967295ul) {
|
|
4198
|
-
return uint8_bincount_64(self, length);
|
|
4199
|
-
} else {
|
|
4200
|
-
return uint8_bincount_32(self, length);
|
|
4201
|
-
}
|
|
4202
|
-
} else {
|
|
4203
|
-
wclass = rb_obj_class(weight);
|
|
4204
|
-
if (wclass == numo_cSFloat) {
|
|
4205
|
-
return uint8_bincount_sf(self, weight, length);
|
|
4206
|
-
} else {
|
|
4207
|
-
return uint8_bincount_df(self, weight, length);
|
|
4208
|
-
}
|
|
4209
|
-
}
|
|
4210
|
-
}
|
|
4211
|
-
|
|
4212
|
-
static void iter_uint8_cumsum(na_loop_t* const lp) {
|
|
4213
|
-
size_t i;
|
|
4214
|
-
char *p1, *p2;
|
|
4215
|
-
ssize_t s1, s2;
|
|
4216
|
-
dtype x, y;
|
|
4217
|
-
|
|
4218
|
-
INIT_COUNTER(lp, i);
|
|
4219
|
-
INIT_PTR(lp, 0, p1, s1);
|
|
4220
|
-
INIT_PTR(lp, 1, p2, s2);
|
|
4221
|
-
|
|
4222
|
-
GET_DATA_STRIDE(p1, s1, dtype, x);
|
|
4223
|
-
SET_DATA_STRIDE(p2, s2, dtype, x);
|
|
4224
|
-
for (i--; i--;) {
|
|
4225
|
-
GET_DATA_STRIDE(p1, s1, dtype, y);
|
|
4226
|
-
m_cumsum(x, y);
|
|
4227
|
-
SET_DATA_STRIDE(p2, s2, dtype, x);
|
|
4228
|
-
}
|
|
4229
|
-
}
|
|
4230
|
-
|
|
4231
|
-
/*
|
|
4232
|
-
cumsum of self.
|
|
4233
|
-
@overload cumsum(axis:nil, nan:false)
|
|
4234
|
-
@param [Numeric,Array,Range] axis Performs cumsum along the axis.
|
|
4235
|
-
@param [TrueClass] nan If true, apply NaN-aware algorithm (avoid NaN if exists).
|
|
4236
|
-
@return [Numo::UInt8] cumsum of self.
|
|
4237
|
-
*/
|
|
4238
|
-
static VALUE uint8_cumsum(int argc, VALUE* argv, VALUE self) {
|
|
4239
|
-
VALUE reduce;
|
|
4240
|
-
ndfunc_arg_in_t ain[2] = { { cT, 0 }, { sym_reduce, 0 } };
|
|
4241
|
-
ndfunc_arg_out_t aout[1] = { { cT, 0 } };
|
|
4242
|
-
ndfunc_t ndf = {
|
|
4243
|
-
iter_uint8_cumsum, STRIDE_LOOP | NDF_FLAT_REDUCE | NDF_CUM, 2, 1, ain, aout
|
|
4244
|
-
};
|
|
4245
|
-
|
|
4246
|
-
reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
|
|
4247
|
-
|
|
4248
|
-
return na_ndloop(&ndf, 2, self, reduce);
|
|
4249
|
-
}
|
|
4250
|
-
|
|
4251
|
-
static void iter_uint8_cumprod(na_loop_t* const lp) {
|
|
4252
|
-
size_t i;
|
|
4253
|
-
char *p1, *p2;
|
|
4254
|
-
ssize_t s1, s2;
|
|
4255
|
-
dtype x, y;
|
|
4256
|
-
|
|
4257
|
-
INIT_COUNTER(lp, i);
|
|
4258
|
-
INIT_PTR(lp, 0, p1, s1);
|
|
4259
|
-
INIT_PTR(lp, 1, p2, s2);
|
|
4260
|
-
|
|
4261
|
-
GET_DATA_STRIDE(p1, s1, dtype, x);
|
|
4262
|
-
SET_DATA_STRIDE(p2, s2, dtype, x);
|
|
4263
|
-
for (i--; i--;) {
|
|
4264
|
-
GET_DATA_STRIDE(p1, s1, dtype, y);
|
|
4265
|
-
m_cumprod(x, y);
|
|
4266
|
-
SET_DATA_STRIDE(p2, s2, dtype, x);
|
|
4267
|
-
}
|
|
4268
|
-
}
|
|
4269
|
-
|
|
4270
|
-
/*
|
|
4271
|
-
cumprod of self.
|
|
4272
|
-
@overload cumprod(axis:nil, nan:false)
|
|
4273
|
-
@param [Numeric,Array,Range] axis Performs cumprod along the axis.
|
|
4274
|
-
@param [TrueClass] nan If true, apply NaN-aware algorithm (avoid NaN if exists).
|
|
4275
|
-
@return [Numo::UInt8] cumprod of self.
|
|
4276
|
-
*/
|
|
4277
|
-
static VALUE uint8_cumprod(int argc, VALUE* argv, VALUE self) {
|
|
4278
|
-
VALUE reduce;
|
|
4279
|
-
ndfunc_arg_in_t ain[2] = { { cT, 0 }, { sym_reduce, 0 } };
|
|
4280
|
-
ndfunc_arg_out_t aout[1] = { { cT, 0 } };
|
|
4281
|
-
ndfunc_t ndf = {
|
|
4282
|
-
iter_uint8_cumprod, STRIDE_LOOP | NDF_FLAT_REDUCE | NDF_CUM, 2, 1, ain, aout
|
|
4283
|
-
};
|
|
4284
|
-
|
|
4285
|
-
reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
|
|
4286
|
-
|
|
4287
|
-
return na_ndloop(&ndf, 2, self, reduce);
|
|
4288
|
-
}
|
|
4289
|
-
|
|
4290
|
-
//
|
|
4291
|
-
static void iter_uint8_mulsum(na_loop_t* const lp) {
|
|
4292
|
-
size_t i, n;
|
|
4293
|
-
char *p1, *p2, *p3;
|
|
4294
|
-
ssize_t s1, s2, s3;
|
|
4295
|
-
|
|
4296
|
-
INIT_COUNTER(lp, n);
|
|
4297
|
-
INIT_PTR(lp, 0, p1, s1);
|
|
4298
|
-
INIT_PTR(lp, 1, p2, s2);
|
|
4299
|
-
INIT_PTR(lp, 2, p3, s3);
|
|
4300
|
-
|
|
4301
|
-
if (s3 == 0) {
|
|
4302
|
-
dtype z;
|
|
4303
|
-
// Reduce loop
|
|
4304
|
-
GET_DATA(p3, dtype, z);
|
|
4305
|
-
for (i = 0; i < n; i++) {
|
|
4306
|
-
dtype x, y;
|
|
4307
|
-
GET_DATA_STRIDE(p1, s1, dtype, x);
|
|
4308
|
-
GET_DATA_STRIDE(p2, s2, dtype, y);
|
|
4309
|
-
m_mulsum(x, y, z);
|
|
4310
|
-
}
|
|
4311
|
-
SET_DATA(p3, dtype, z);
|
|
4312
|
-
return;
|
|
4313
|
-
} else {
|
|
4314
|
-
for (i = 0; i < n; i++) {
|
|
4315
|
-
dtype x, y, z;
|
|
4316
|
-
GET_DATA_STRIDE(p1, s1, dtype, x);
|
|
4317
|
-
GET_DATA_STRIDE(p2, s2, dtype, y);
|
|
4318
|
-
GET_DATA(p3, dtype, z);
|
|
4319
|
-
m_mulsum(x, y, z);
|
|
4320
|
-
SET_DATA_STRIDE(p3, s3, dtype, z);
|
|
4321
|
-
}
|
|
4322
|
-
}
|
|
4323
|
-
}
|
|
4324
|
-
//
|
|
4325
|
-
|
|
4326
|
-
static VALUE uint8_mulsum_self(int argc, VALUE* argv, VALUE self) {
|
|
4327
|
-
VALUE v, reduce;
|
|
4328
|
-
VALUE naryv[2];
|
|
4329
|
-
ndfunc_arg_in_t ain[4] = { { cT, 0 }, { cT, 0 }, { sym_reduce, 0 }, { sym_init, 0 } };
|
|
4330
|
-
ndfunc_arg_out_t aout[1] = { { cT, 0 } };
|
|
4331
|
-
ndfunc_t ndf = { iter_uint8_mulsum, STRIDE_LOOP_NIP, 4, 1, ain, aout };
|
|
4332
|
-
|
|
4333
|
-
if (argc < 1) {
|
|
4334
|
-
rb_raise(rb_eArgError, "wrong number of arguments (%d for >=1)", argc);
|
|
4335
|
-
}
|
|
4336
|
-
// should fix below: [self.ndim,other.ndim].max or?
|
|
4337
|
-
naryv[0] = self;
|
|
4338
|
-
naryv[1] = argv[0];
|
|
4339
|
-
//
|
|
4340
|
-
reduce = na_reduce_dimension(argc - 1, argv + 1, 2, naryv, &ndf, 0);
|
|
4341
|
-
//
|
|
4342
|
-
|
|
4343
|
-
v = na_ndloop(&ndf, 4, self, argv[0], reduce, m_mulsum_init);
|
|
4344
|
-
return uint8_extract(v);
|
|
4345
|
-
}
|
|
4346
|
-
|
|
4347
|
-
/*
|
|
4348
|
-
Binary mulsum.
|
|
4349
|
-
|
|
4350
|
-
@overload mulsum(other, axis:nil, keepdims:false)
|
|
4351
|
-
@param [Numo::NArray,Numeric] other
|
|
4352
|
-
@param [Numeric,Array,Range] axis Performs mulsum along the axis.
|
|
4353
|
-
@param [TrueClass] keepdims (keyword) If true, the reduced axes are left in the result array
|
|
4354
|
-
as dimensions with size one.
|
|
4355
|
-
@return [Numo::NArray] mulsum of self and other.
|
|
4356
|
-
*/
|
|
4357
|
-
static VALUE uint8_mulsum(int argc, VALUE* argv, VALUE self) {
|
|
4358
|
-
//
|
|
4359
|
-
VALUE klass, v;
|
|
4360
|
-
//
|
|
4361
|
-
if (argc < 1) {
|
|
4362
|
-
rb_raise(rb_eArgError, "wrong number of arguments (%d for >=1)", argc);
|
|
4363
|
-
}
|
|
4364
|
-
//
|
|
4365
|
-
klass = na_upcast(rb_obj_class(self), rb_obj_class(argv[0]));
|
|
4366
|
-
if (klass == cT) {
|
|
4367
|
-
return uint8_mulsum_self(argc, argv, self);
|
|
4368
|
-
} else {
|
|
4369
|
-
v = rb_funcall(klass, id_cast, 1, self);
|
|
4370
|
-
//
|
|
4371
|
-
return rb_funcallv_kw(v, rb_intern("mulsum"), argc, argv, RB_PASS_CALLED_KEYWORDS);
|
|
4372
|
-
//
|
|
4373
|
-
}
|
|
4374
|
-
//
|
|
4375
|
-
}
|
|
4376
|
-
|
|
4377
|
-
typedef double seq_data_t;
|
|
4378
|
-
|
|
4379
|
-
typedef double seq_count_t;
|
|
4380
|
-
|
|
4381
|
-
typedef struct {
|
|
4382
|
-
seq_data_t beg;
|
|
4383
|
-
seq_data_t step;
|
|
4384
|
-
seq_count_t count;
|
|
4385
|
-
} seq_opt_t;
|
|
4386
|
-
|
|
4387
|
-
static void iter_uint8_seq(na_loop_t* const lp) {
|
|
4388
|
-
size_t i;
|
|
4389
|
-
char* p1;
|
|
4390
|
-
ssize_t s1;
|
|
4391
|
-
size_t* idx1;
|
|
4392
|
-
dtype x;
|
|
4393
|
-
seq_data_t beg, step;
|
|
4394
|
-
seq_count_t c;
|
|
4395
|
-
seq_opt_t* g;
|
|
4396
|
-
|
|
4397
|
-
INIT_COUNTER(lp, i);
|
|
4398
|
-
INIT_PTR_IDX(lp, 0, p1, s1, idx1);
|
|
4399
|
-
g = (seq_opt_t*)(lp->opt_ptr);
|
|
4400
|
-
beg = g->beg;
|
|
4401
|
-
step = g->step;
|
|
4402
|
-
c = g->count;
|
|
4403
|
-
if (idx1) {
|
|
4404
|
-
for (; i--;) {
|
|
4405
|
-
x = f_seq(beg, step, c++);
|
|
4406
|
-
*(dtype*)(p1 + *idx1) = x;
|
|
4407
|
-
idx1++;
|
|
4408
|
-
}
|
|
4409
|
-
} else {
|
|
4410
|
-
for (; i--;) {
|
|
4411
|
-
x = f_seq(beg, step, c++);
|
|
4412
|
-
*(dtype*)(p1) = x;
|
|
4413
|
-
p1 += s1;
|
|
1777
|
+
p2 += s2;
|
|
1778
|
+
}
|
|
1779
|
+
return;
|
|
1780
|
+
//
|
|
4414
1781
|
}
|
|
4415
1782
|
}
|
|
4416
|
-
g->count = c;
|
|
4417
1783
|
}
|
|
4418
1784
|
|
|
4419
1785
|
/*
|
|
4420
|
-
|
|
4421
|
-
|
|
4422
|
-
|
|
4423
|
-
@overload seq([beg,[step]])
|
|
4424
|
-
@param [Numeric] beg beginning of sequence. (default=0)
|
|
4425
|
-
@param [Numeric] step step of sequence. (default=1)
|
|
4426
|
-
@return [Numo::UInt8] self.
|
|
4427
|
-
@example
|
|
4428
|
-
Numo::DFloat.new(6).seq(1,-0.2)
|
|
4429
|
-
# => Numo::DFloat#shape=[6]
|
|
4430
|
-
# [1, 0.8, 0.6, 0.4, 0.2, 0]
|
|
4431
|
-
|
|
4432
|
-
Numo::DComplex.new(6).seq(1,-0.2+0.2i)
|
|
4433
|
-
# => Numo::DComplex#shape=[6]
|
|
4434
|
-
# [1+0i, 0.8+0.2i, 0.6+0.4i, 0.4+0.6i, 0.2+0.8i, 0+1i]
|
|
1786
|
+
Unary reciprocal.
|
|
1787
|
+
@overload reciprocal
|
|
1788
|
+
@return [Numo::UInt8] reciprocal of self.
|
|
4435
1789
|
*/
|
|
4436
|
-
static VALUE
|
|
4437
|
-
|
|
4438
|
-
|
|
4439
|
-
|
|
4440
|
-
ndfunc_t ndf = { iter_uint8_seq, FULL_LOOP, 1, 0, ain, 0 };
|
|
4441
|
-
|
|
4442
|
-
g = ALLOCA_N(seq_opt_t, 1);
|
|
4443
|
-
g->beg = m_zero;
|
|
4444
|
-
g->step = m_one;
|
|
4445
|
-
g->count = 0;
|
|
4446
|
-
rb_scan_args(argc, argv, "02", &vbeg, &vstep);
|
|
4447
|
-
if (vbeg != Qnil) {
|
|
4448
|
-
g->beg = NUM2DBL(vbeg);
|
|
4449
|
-
}
|
|
4450
|
-
if (vstep != Qnil) {
|
|
4451
|
-
g->step = NUM2DBL(vstep);
|
|
4452
|
-
}
|
|
4453
|
-
|
|
4454
|
-
na_ndloop3(&ndf, g, 1, self);
|
|
4455
|
-
return self;
|
|
4456
|
-
}
|
|
1790
|
+
static VALUE uint8_reciprocal(VALUE self) {
|
|
1791
|
+
ndfunc_arg_in_t ain[1] = { { cT, 0 } };
|
|
1792
|
+
ndfunc_arg_out_t aout[1] = { { cT, 0 } };
|
|
1793
|
+
ndfunc_t ndf = { iter_uint8_reciprocal, FULL_LOOP, 1, 1, ain, aout };
|
|
4457
1794
|
|
|
4458
|
-
|
|
4459
|
-
size_t n0, n1;
|
|
4460
|
-
size_t i0, i1;
|
|
4461
|
-
ssize_t s0, s1;
|
|
4462
|
-
char *p0, *p1;
|
|
4463
|
-
char* g;
|
|
4464
|
-
ssize_t kofs;
|
|
4465
|
-
dtype data;
|
|
4466
|
-
|
|
4467
|
-
g = (char*)(lp->opt_ptr);
|
|
4468
|
-
kofs = *(ssize_t*)g;
|
|
4469
|
-
data = *(dtype*)(g + sizeof(ssize_t));
|
|
4470
|
-
|
|
4471
|
-
n0 = lp->args[0].shape[0];
|
|
4472
|
-
n1 = lp->args[0].shape[1];
|
|
4473
|
-
s0 = lp->args[0].iter[0].step;
|
|
4474
|
-
s1 = lp->args[0].iter[1].step;
|
|
4475
|
-
p0 = NDL_PTR(lp, 0);
|
|
4476
|
-
|
|
4477
|
-
for (i0 = 0; i0 < n0; i0++) {
|
|
4478
|
-
p1 = p0;
|
|
4479
|
-
for (i1 = 0; i1 < n1; i1++) {
|
|
4480
|
-
*(dtype*)p1 = (i0 + kofs == i1) ? data : m_zero;
|
|
4481
|
-
p1 += s1;
|
|
4482
|
-
}
|
|
4483
|
-
p0 += s0;
|
|
4484
|
-
}
|
|
1795
|
+
return na_ndloop(&ndf, 1, self);
|
|
4485
1796
|
}
|
|
4486
1797
|
|
|
4487
|
-
|
|
4488
|
-
|
|
4489
|
-
|
|
4490
|
-
|
|
4491
|
-
|
|
4492
|
-
|
|
4493
|
-
for diagonals below the main diagonal.
|
|
4494
|
-
@return [Numo::UInt8] eye of self.
|
|
4495
|
-
*/
|
|
4496
|
-
static VALUE uint8_eye(int argc, VALUE* argv, VALUE self) {
|
|
4497
|
-
ndfunc_arg_in_t ain[1] = { { OVERWRITE, 2 } };
|
|
4498
|
-
ndfunc_t ndf = { iter_uint8_eye, NO_LOOP, 1, 0, ain, 0 };
|
|
4499
|
-
ssize_t kofs;
|
|
4500
|
-
dtype data;
|
|
4501
|
-
char* g;
|
|
4502
|
-
int nd;
|
|
4503
|
-
narray_t* na;
|
|
4504
|
-
|
|
4505
|
-
// check arguments
|
|
4506
|
-
if (argc > 2) {
|
|
4507
|
-
rb_raise(rb_eArgError, "too many arguments (%d for 0..2)", argc);
|
|
4508
|
-
} else if (argc == 2) {
|
|
4509
|
-
data = m_num_to_data(argv[0]);
|
|
4510
|
-
kofs = NUM2SSIZET(argv[1]);
|
|
4511
|
-
} else if (argc == 1) {
|
|
4512
|
-
data = m_num_to_data(argv[0]);
|
|
4513
|
-
kofs = 0;
|
|
4514
|
-
} else {
|
|
4515
|
-
data = m_one;
|
|
4516
|
-
kofs = 0;
|
|
4517
|
-
}
|
|
1798
|
+
static void iter_uint8_sign(na_loop_t* const lp) {
|
|
1799
|
+
size_t i, n;
|
|
1800
|
+
char *p1, *p2;
|
|
1801
|
+
ssize_t s1, s2;
|
|
1802
|
+
size_t *idx1, *idx2;
|
|
1803
|
+
dtype x;
|
|
4518
1804
|
|
|
4519
|
-
|
|
4520
|
-
|
|
4521
|
-
|
|
4522
|
-
rb_raise(nary_eDimensionError, "less than 2-d array");
|
|
4523
|
-
}
|
|
1805
|
+
INIT_COUNTER(lp, n);
|
|
1806
|
+
INIT_PTR_IDX(lp, 0, p1, s1, idx1);
|
|
1807
|
+
INIT_PTR_IDX(lp, 1, p2, s2, idx2);
|
|
4524
1808
|
|
|
4525
|
-
|
|
4526
|
-
|
|
4527
|
-
|
|
4528
|
-
|
|
4529
|
-
|
|
4530
|
-
|
|
4531
|
-
|
|
4532
|
-
|
|
4533
|
-
)
|
|
1809
|
+
if (idx1) {
|
|
1810
|
+
if (idx2) {
|
|
1811
|
+
for (i = 0; i < n; i++) {
|
|
1812
|
+
GET_DATA_INDEX(p1, idx1, dtype, x);
|
|
1813
|
+
x = m_sign(x);
|
|
1814
|
+
SET_DATA_INDEX(p2, idx2, dtype, x);
|
|
1815
|
+
}
|
|
1816
|
+
} else {
|
|
1817
|
+
for (i = 0; i < n; i++) {
|
|
1818
|
+
GET_DATA_INDEX(p1, idx1, dtype, x);
|
|
1819
|
+
x = m_sign(x);
|
|
1820
|
+
SET_DATA_STRIDE(p2, s2, dtype, x);
|
|
1821
|
+
}
|
|
4534
1822
|
}
|
|
4535
1823
|
} else {
|
|
4536
|
-
if (
|
|
4537
|
-
|
|
4538
|
-
|
|
4539
|
-
|
|
4540
|
-
|
|
4541
|
-
|
|
4542
|
-
);
|
|
4543
|
-
}
|
|
4544
|
-
}
|
|
4545
|
-
|
|
4546
|
-
g = ALLOCA_N(char, sizeof(ssize_t) + sizeof(dtype));
|
|
4547
|
-
*(ssize_t*)g = kofs;
|
|
4548
|
-
*(dtype*)(g + sizeof(ssize_t)) = data;
|
|
4549
|
-
|
|
4550
|
-
na_ndloop3(&ndf, g, 1, self);
|
|
4551
|
-
return self;
|
|
4552
|
-
}
|
|
4553
|
-
|
|
4554
|
-
#define HWID (sizeof(dtype) * 4)
|
|
4555
|
-
|
|
4556
|
-
static int msb_pos(uint32_t a) {
|
|
4557
|
-
int width = HWID;
|
|
4558
|
-
int pos = 0;
|
|
4559
|
-
uint32_t mask = (((dtype)1 << HWID) - 1) << HWID;
|
|
4560
|
-
|
|
4561
|
-
if (a == 0) {
|
|
4562
|
-
return -1;
|
|
4563
|
-
}
|
|
4564
|
-
|
|
4565
|
-
while (width) {
|
|
4566
|
-
if (a & mask) {
|
|
4567
|
-
pos += width;
|
|
1824
|
+
if (idx2) {
|
|
1825
|
+
for (i = 0; i < n; i++) {
|
|
1826
|
+
GET_DATA_STRIDE(p1, s1, dtype, x);
|
|
1827
|
+
x = m_sign(x);
|
|
1828
|
+
SET_DATA_INDEX(p2, idx2, dtype, x);
|
|
1829
|
+
}
|
|
4568
1830
|
} else {
|
|
4569
|
-
|
|
1831
|
+
//
|
|
1832
|
+
for (i = 0; i < n; i++) {
|
|
1833
|
+
*(dtype*)p2 = m_sign(*(dtype*)p1);
|
|
1834
|
+
p1 += s1;
|
|
1835
|
+
p2 += s2;
|
|
1836
|
+
}
|
|
1837
|
+
return;
|
|
1838
|
+
//
|
|
4570
1839
|
}
|
|
4571
|
-
width >>= 1;
|
|
4572
|
-
mask &= mask << width;
|
|
4573
1840
|
}
|
|
4574
|
-
return pos;
|
|
4575
1841
|
}
|
|
4576
1842
|
|
|
4577
|
-
/*
|
|
4578
|
-
|
|
4579
|
-
|
|
4580
|
-
|
|
4581
|
-
|
|
4582
|
-
|
|
4583
|
-
|
|
4584
|
-
|
|
4585
|
-
}
|
|
1843
|
+
/*
|
|
1844
|
+
Unary sign.
|
|
1845
|
+
@overload sign
|
|
1846
|
+
@return [Numo::UInt8] sign of self.
|
|
1847
|
+
*/
|
|
1848
|
+
static VALUE uint8_sign(VALUE self) {
|
|
1849
|
+
ndfunc_arg_in_t ain[1] = { { cT, 0 } };
|
|
1850
|
+
ndfunc_arg_out_t aout[1] = { { cT, 0 } };
|
|
1851
|
+
ndfunc_t ndf = { iter_uint8_sign, FULL_LOOP, 1, 1, ain, aout };
|
|
4586
1852
|
|
|
4587
|
-
|
|
4588
|
-
|
|
4589
|
-
uint32_t max;
|
|
4590
|
-
} rand_opt_t;
|
|
1853
|
+
return na_ndloop(&ndf, 1, self);
|
|
1854
|
+
}
|
|
4591
1855
|
|
|
4592
|
-
static void
|
|
4593
|
-
size_t i;
|
|
4594
|
-
char*
|
|
4595
|
-
ssize_t s1;
|
|
4596
|
-
size_t*
|
|
1856
|
+
static void iter_uint8_square(na_loop_t* const lp) {
|
|
1857
|
+
size_t i, n;
|
|
1858
|
+
char *p1, *p2;
|
|
1859
|
+
ssize_t s1, s2;
|
|
1860
|
+
size_t *idx1, *idx2;
|
|
4597
1861
|
dtype x;
|
|
4598
|
-
rand_opt_t* g;
|
|
4599
|
-
dtype low;
|
|
4600
|
-
uint32_t max;
|
|
4601
|
-
int shift;
|
|
4602
1862
|
|
|
4603
|
-
INIT_COUNTER(lp,
|
|
1863
|
+
INIT_COUNTER(lp, n);
|
|
4604
1864
|
INIT_PTR_IDX(lp, 0, p1, s1, idx1);
|
|
4605
|
-
|
|
4606
|
-
low = g->low;
|
|
4607
|
-
max = g->max;
|
|
4608
|
-
shift = 31 - msb_pos(max);
|
|
1865
|
+
INIT_PTR_IDX(lp, 1, p2, s2, idx2);
|
|
4609
1866
|
|
|
4610
1867
|
if (idx1) {
|
|
4611
|
-
|
|
4612
|
-
|
|
4613
|
-
|
|
1868
|
+
if (idx2) {
|
|
1869
|
+
for (i = 0; i < n; i++) {
|
|
1870
|
+
GET_DATA_INDEX(p1, idx1, dtype, x);
|
|
1871
|
+
x = m_square(x);
|
|
1872
|
+
SET_DATA_INDEX(p2, idx2, dtype, x);
|
|
1873
|
+
}
|
|
1874
|
+
} else {
|
|
1875
|
+
for (i = 0; i < n; i++) {
|
|
1876
|
+
GET_DATA_INDEX(p1, idx1, dtype, x);
|
|
1877
|
+
x = m_square(x);
|
|
1878
|
+
SET_DATA_STRIDE(p2, s2, dtype, x);
|
|
1879
|
+
}
|
|
4614
1880
|
}
|
|
4615
1881
|
} else {
|
|
4616
|
-
|
|
4617
|
-
|
|
4618
|
-
|
|
1882
|
+
if (idx2) {
|
|
1883
|
+
for (i = 0; i < n; i++) {
|
|
1884
|
+
GET_DATA_STRIDE(p1, s1, dtype, x);
|
|
1885
|
+
x = m_square(x);
|
|
1886
|
+
SET_DATA_INDEX(p2, idx2, dtype, x);
|
|
1887
|
+
}
|
|
1888
|
+
} else {
|
|
1889
|
+
//
|
|
1890
|
+
for (i = 0; i < n; i++) {
|
|
1891
|
+
*(dtype*)p2 = m_square(*(dtype*)p1);
|
|
1892
|
+
p1 += s1;
|
|
1893
|
+
p2 += s2;
|
|
1894
|
+
}
|
|
1895
|
+
return;
|
|
1896
|
+
//
|
|
4619
1897
|
}
|
|
4620
1898
|
}
|
|
4621
1899
|
}
|
|
4622
1900
|
|
|
4623
1901
|
/*
|
|
4624
|
-
|
|
4625
|
-
@overload
|
|
4626
|
-
@
|
|
4627
|
-
@param [Numeric] high upper exclusive boundary of random numbers. (default=1 or 1+1i for
|
|
4628
|
-
complex types)
|
|
4629
|
-
@return [Numo::UInt8] self.
|
|
4630
|
-
@example
|
|
4631
|
-
Numo::DFloat.new(6).rand
|
|
4632
|
-
# => Numo::DFloat#shape=[6]
|
|
4633
|
-
# [0.0617545, 0.373067, 0.794815, 0.201042, 0.116041, 0.344032]
|
|
4634
|
-
|
|
4635
|
-
Numo::DComplex.new(6).rand(5+5i)
|
|
4636
|
-
# => Numo::DComplex#shape=[6]
|
|
4637
|
-
# [2.69974+3.68908i, 0.825443+0.254414i, 0.540323+0.34354i, 4.52061+2.39322i, ...]
|
|
4638
|
-
|
|
4639
|
-
Numo::Int32.new(6).rand(2,5)
|
|
4640
|
-
# => Numo::Int32#shape=[6]
|
|
4641
|
-
# [4, 3, 3, 2, 4, 2]
|
|
1902
|
+
Unary square.
|
|
1903
|
+
@overload square
|
|
1904
|
+
@return [Numo::UInt8] square of self.
|
|
4642
1905
|
*/
|
|
4643
|
-
static VALUE
|
|
4644
|
-
|
|
4645
|
-
|
|
4646
|
-
|
|
4647
|
-
ndfunc_arg_in_t ain[1] = { { OVERWRITE, 0 } };
|
|
4648
|
-
ndfunc_t ndf = { iter_uint8_rand, FULL_LOOP, 1, 0, ain, 0 };
|
|
4649
|
-
|
|
4650
|
-
rb_scan_args(argc, argv, "11", &v1, &v2);
|
|
4651
|
-
if (v2 == Qnil) {
|
|
4652
|
-
g.low = m_zero;
|
|
4653
|
-
g.max = high = m_num_to_data(v1);
|
|
4654
|
-
|
|
4655
|
-
} else {
|
|
4656
|
-
g.low = m_num_to_data(v1);
|
|
4657
|
-
high = m_num_to_data(v2);
|
|
4658
|
-
g.max = m_sub(high, g.low);
|
|
4659
|
-
}
|
|
4660
|
-
|
|
4661
|
-
if (high <= g.low) {
|
|
4662
|
-
rb_raise(rb_eArgError, "high must be larger than low");
|
|
4663
|
-
}
|
|
1906
|
+
static VALUE uint8_square(VALUE self) {
|
|
1907
|
+
ndfunc_arg_in_t ain[1] = { { cT, 0 } };
|
|
1908
|
+
ndfunc_arg_out_t aout[1] = { { cT, 0 } };
|
|
1909
|
+
ndfunc_t ndf = { iter_uint8_square, FULL_LOOP, 1, 1, ain, aout };
|
|
4664
1910
|
|
|
4665
|
-
|
|
4666
|
-
return self;
|
|
1911
|
+
return na_ndloop(&ndf, 1, self);
|
|
4667
1912
|
}
|
|
4668
1913
|
|
|
4669
1914
|
static void iter_uint8_poly(na_loop_t* const lp) {
|
|
@@ -5289,22 +2534,91 @@ void Init_numo_uint8(void) {
|
|
|
5289
2534
|
rb_define_singleton_method(cT, "cast", uint8_s_cast, 1);
|
|
5290
2535
|
rb_define_method(cT, "[]", uint8_aref, -1);
|
|
5291
2536
|
rb_define_method(cT, "[]=", uint8_aset, -1);
|
|
2537
|
+
/**
|
|
2538
|
+
* return NArray with cast to the type of self.
|
|
2539
|
+
* @overload coerce_cast(type)
|
|
2540
|
+
* @return [nil]
|
|
2541
|
+
*/
|
|
5292
2542
|
rb_define_method(cT, "coerce_cast", uint8_coerce_cast, 1);
|
|
2543
|
+
/**
|
|
2544
|
+
* Convert self to Array.
|
|
2545
|
+
* @overload to_a
|
|
2546
|
+
* @return [Array]
|
|
2547
|
+
*/
|
|
5293
2548
|
rb_define_method(cT, "to_a", uint8_to_a, 0);
|
|
2549
|
+
/**
|
|
2550
|
+
* Fill elements with other.
|
|
2551
|
+
* @overload fill other
|
|
2552
|
+
* @param [Numeric] other
|
|
2553
|
+
* @return [Numo::UInt8] self.
|
|
2554
|
+
*/
|
|
5294
2555
|
rb_define_method(cT, "fill", uint8_fill, 1);
|
|
2556
|
+
/**
|
|
2557
|
+
* Format elements into strings.
|
|
2558
|
+
* @overload format format
|
|
2559
|
+
* @param [String] format
|
|
2560
|
+
* @return [Numo::RObject] array of formatted strings.
|
|
2561
|
+
*/
|
|
5295
2562
|
rb_define_method(cT, "format", uint8_format, -1);
|
|
2563
|
+
/**
|
|
2564
|
+
* Format elements into strings.
|
|
2565
|
+
* @overload format_to_a format
|
|
2566
|
+
* @param [String] format
|
|
2567
|
+
* @return [Array] array of formatted strings.
|
|
2568
|
+
*/
|
|
5296
2569
|
rb_define_method(cT, "format_to_a", uint8_format_to_a, -1);
|
|
2570
|
+
/**
|
|
2571
|
+
* Returns a string containing a human-readable representation of NArray.
|
|
2572
|
+
* @overload inspect
|
|
2573
|
+
* @return [String]
|
|
2574
|
+
*/
|
|
5297
2575
|
rb_define_method(cT, "inspect", uint8_inspect, 0);
|
|
5298
2576
|
rb_define_method(cT, "each", uint8_each, 0);
|
|
5299
2577
|
rb_define_method(cT, "map", uint8_map, 0);
|
|
5300
2578
|
rb_define_method(cT, "each_with_index", uint8_each_with_index, 0);
|
|
5301
2579
|
rb_define_method(cT, "map_with_index", uint8_map_with_index, 0);
|
|
5302
2580
|
rb_define_method(cT, "abs", uint8_abs, 0);
|
|
2581
|
+
/**
|
|
2582
|
+
* Binary add.
|
|
2583
|
+
* @overload + other
|
|
2584
|
+
* @param [Numo::NArray,Numeric] other
|
|
2585
|
+
* @return [Numo::NArray] self + other
|
|
2586
|
+
*/
|
|
5303
2587
|
rb_define_method(cT, "+", uint8_add, 1);
|
|
2588
|
+
/**
|
|
2589
|
+
* Binary sub.
|
|
2590
|
+
* @overload - other
|
|
2591
|
+
* @param [Numo::NArray,Numeric] other
|
|
2592
|
+
* @return [Numo::NArray] self - other
|
|
2593
|
+
*/
|
|
5304
2594
|
rb_define_method(cT, "-", uint8_sub, 1);
|
|
2595
|
+
/**
|
|
2596
|
+
* Binary mul.
|
|
2597
|
+
* @overload * other
|
|
2598
|
+
* @param [Numo::NArray,Numeric] other
|
|
2599
|
+
* @return [Numo::NArray] self * other
|
|
2600
|
+
*/
|
|
5305
2601
|
rb_define_method(cT, "*", uint8_mul, 1);
|
|
2602
|
+
/**
|
|
2603
|
+
* Binary div.
|
|
2604
|
+
* @overload / other
|
|
2605
|
+
* @param [Numo::NArray,Numeric] other
|
|
2606
|
+
* @return [Numo::NArray] self / other
|
|
2607
|
+
*/
|
|
5306
2608
|
rb_define_method(cT, "/", uint8_div, 1);
|
|
2609
|
+
/**
|
|
2610
|
+
* Binary mod.
|
|
2611
|
+
* @overload % other
|
|
2612
|
+
* @param [Numo::NArray,Numeric] other
|
|
2613
|
+
* @return [Numo::NArray] self % other
|
|
2614
|
+
*/
|
|
5307
2615
|
rb_define_method(cT, "%", uint8_mod, 1);
|
|
2616
|
+
/**
|
|
2617
|
+
* Binary divmod.
|
|
2618
|
+
* @overload divmod other
|
|
2619
|
+
* @param [Numo::NArray,Numeric] other
|
|
2620
|
+
* @return [Numo::NArray] divmod of self and other.
|
|
2621
|
+
*/
|
|
5308
2622
|
rb_define_method(cT, "divmod", uint8_divmod, 1);
|
|
5309
2623
|
rb_define_method(cT, "**", uint8_pow, 1);
|
|
5310
2624
|
rb_define_alias(cT, "pow", "**");
|
|
@@ -5315,39 +2629,264 @@ void Init_numo_uint8(void) {
|
|
|
5315
2629
|
rb_define_alias(cT, "conj", "view");
|
|
5316
2630
|
rb_define_alias(cT, "im", "view");
|
|
5317
2631
|
rb_define_alias(cT, "conjugate", "conj");
|
|
2632
|
+
/**
|
|
2633
|
+
* Comparison eq other.
|
|
2634
|
+
* @overload eq other
|
|
2635
|
+
* @param [Numo::NArray,Numeric] other
|
|
2636
|
+
* @return [Numo::Bit] result of self eq other.
|
|
2637
|
+
*/
|
|
5318
2638
|
rb_define_method(cT, "eq", uint8_eq, 1);
|
|
2639
|
+
/**
|
|
2640
|
+
* Comparison ne other.
|
|
2641
|
+
* @overload ne other
|
|
2642
|
+
* @param [Numo::NArray,Numeric] other
|
|
2643
|
+
* @return [Numo::Bit] result of self ne other.
|
|
2644
|
+
*/
|
|
5319
2645
|
rb_define_method(cT, "ne", uint8_ne, 1);
|
|
5320
2646
|
rb_define_alias(cT, "nearly_eq", "eq");
|
|
5321
2647
|
rb_define_alias(cT, "close_to", "nearly_eq");
|
|
2648
|
+
/**
|
|
2649
|
+
* Binary bit_and.
|
|
2650
|
+
* @overload & other
|
|
2651
|
+
* @param [Numo::NArray,Numeric] other
|
|
2652
|
+
* @return [Numo::NArray] self & other
|
|
2653
|
+
*/
|
|
5322
2654
|
rb_define_method(cT, "&", uint8_bit_and, 1);
|
|
2655
|
+
/**
|
|
2656
|
+
* Binary bit_or.
|
|
2657
|
+
* @overload | other
|
|
2658
|
+
* @param [Numo::NArray,Numeric] other
|
|
2659
|
+
* @return [Numo::NArray] self | other
|
|
2660
|
+
*/
|
|
5323
2661
|
rb_define_method(cT, "|", uint8_bit_or, 1);
|
|
2662
|
+
/**
|
|
2663
|
+
* Binary bit_xor.
|
|
2664
|
+
* @overload ^ other
|
|
2665
|
+
* @param [Numo::NArray,Numeric] other
|
|
2666
|
+
* @return [Numo::NArray] self ^ other
|
|
2667
|
+
*/
|
|
5324
2668
|
rb_define_method(cT, "^", uint8_bit_xor, 1);
|
|
2669
|
+
/**
|
|
2670
|
+
* Unary bit_not.
|
|
2671
|
+
* @overload ~
|
|
2672
|
+
* @return [Numo::UInt8] bit_not of self.
|
|
2673
|
+
*/
|
|
5325
2674
|
rb_define_method(cT, "~", uint8_bit_not, 0);
|
|
2675
|
+
/**
|
|
2676
|
+
* Binary left_shift.
|
|
2677
|
+
* @overload << other
|
|
2678
|
+
* @param [Numo::NArray,Numeric] other
|
|
2679
|
+
* @return [Numo::NArray] self << other
|
|
2680
|
+
*/
|
|
5326
2681
|
rb_define_method(cT, "<<", uint8_left_shift, 1);
|
|
2682
|
+
/**
|
|
2683
|
+
* Binary right_shift.
|
|
2684
|
+
* @overload >> other
|
|
2685
|
+
* @param [Numo::NArray,Numeric] other
|
|
2686
|
+
* @return [Numo::NArray] self >> other
|
|
2687
|
+
*/
|
|
5327
2688
|
rb_define_method(cT, ">>", uint8_right_shift, 1);
|
|
5328
2689
|
rb_define_alias(cT, "floor", "view");
|
|
5329
2690
|
rb_define_alias(cT, "round", "view");
|
|
5330
2691
|
rb_define_alias(cT, "ceil", "view");
|
|
5331
2692
|
rb_define_alias(cT, "trunc", "view");
|
|
5332
2693
|
rb_define_alias(cT, "rint", "view");
|
|
2694
|
+
/**
|
|
2695
|
+
* Comparison gt other.
|
|
2696
|
+
* @overload gt other
|
|
2697
|
+
* @param [Numo::NArray,Numeric] other
|
|
2698
|
+
* @return [Numo::Bit] result of self gt other.
|
|
2699
|
+
*/
|
|
5333
2700
|
rb_define_method(cT, "gt", uint8_gt, 1);
|
|
2701
|
+
/**
|
|
2702
|
+
* Comparison ge other.
|
|
2703
|
+
* @overload ge other
|
|
2704
|
+
* @param [Numo::NArray,Numeric] other
|
|
2705
|
+
* @return [Numo::Bit] result of self ge other.
|
|
2706
|
+
*/
|
|
5334
2707
|
rb_define_method(cT, "ge", uint8_ge, 1);
|
|
2708
|
+
/**
|
|
2709
|
+
* Comparison lt other.
|
|
2710
|
+
* @overload lt other
|
|
2711
|
+
* @param [Numo::NArray,Numeric] other
|
|
2712
|
+
* @return [Numo::Bit] result of self lt other.
|
|
2713
|
+
*/
|
|
5335
2714
|
rb_define_method(cT, "lt", uint8_lt, 1);
|
|
2715
|
+
/**
|
|
2716
|
+
* Comparison le other.
|
|
2717
|
+
* @overload le other
|
|
2718
|
+
* @param [Numo::NArray,Numeric] other
|
|
2719
|
+
* @return [Numo::Bit] result of self le other.
|
|
2720
|
+
*/
|
|
5336
2721
|
rb_define_method(cT, "le", uint8_le, 1);
|
|
5337
2722
|
rb_define_alias(cT, ">", "gt");
|
|
5338
2723
|
rb_define_alias(cT, ">=", "ge");
|
|
5339
2724
|
rb_define_alias(cT, "<", "lt");
|
|
5340
2725
|
rb_define_alias(cT, "<=", "le");
|
|
2726
|
+
/**
|
|
2727
|
+
* Clip array elements by [min,max].
|
|
2728
|
+
* If either of min or max is nil, one side is clipped.
|
|
2729
|
+
* @overload clip(min,max)
|
|
2730
|
+
* @param [Numo::NArray,Numeric] min
|
|
2731
|
+
* @param [Numo::NArray,Numeric] max
|
|
2732
|
+
* @return [Numo::NArray] result of clip.
|
|
2733
|
+
*
|
|
2734
|
+
* @example
|
|
2735
|
+
* a = Numo::Int32.new(10).seq
|
|
2736
|
+
* # => Numo::Int32#shape=[10]
|
|
2737
|
+
* # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
|
2738
|
+
*
|
|
2739
|
+
* a.clip(1,8)
|
|
2740
|
+
* # => Numo::Int32#shape=[10]
|
|
2741
|
+
* # [1, 1, 2, 3, 4, 5, 6, 7, 8, 8]
|
|
2742
|
+
*
|
|
2743
|
+
* a.inplace.clip(3,6)
|
|
2744
|
+
* a
|
|
2745
|
+
* # => Numo::Int32#shape=[10]
|
|
2746
|
+
* # [3, 3, 3, 3, 4, 5, 6, 6, 6, 6]
|
|
2747
|
+
*
|
|
2748
|
+
* b = Numo::Int32.new(10).seq
|
|
2749
|
+
* # => Numo::Int32#shape=[10]
|
|
2750
|
+
* # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
|
2751
|
+
*
|
|
2752
|
+
* b.clip([3,4,1,1,1,4,4,4,4,4], 8)
|
|
2753
|
+
* # => Numo::Int32#shape=[10]
|
|
2754
|
+
* # [3, 4, 2, 3, 4, 5, 6, 7, 8, 8]
|
|
2755
|
+
*/
|
|
5341
2756
|
rb_define_method(cT, "clip", uint8_clip, 2);
|
|
2757
|
+
/**
|
|
2758
|
+
* sum of self.
|
|
2759
|
+
* @overload sum(axis:nil, keepdims:false)
|
|
2760
|
+
* @param [Numeric,Array,Range] axis Performs sum along the axis.
|
|
2761
|
+
* @param [TrueClass] keepdims If true, the reduced axes are left in the result array as
|
|
2762
|
+
* dimensions with size one.
|
|
2763
|
+
* @return [Numo::UInt64] returns result of sum.
|
|
2764
|
+
*/
|
|
5342
2765
|
rb_define_method(cT, "sum", uint8_sum, -1);
|
|
2766
|
+
/**
|
|
2767
|
+
* prod of self.
|
|
2768
|
+
* @overload prod(axis:nil, keepdims:false)
|
|
2769
|
+
* @param [Numeric,Array,Range] axis Performs prod along the axis.
|
|
2770
|
+
* @param [TrueClass] keepdims If true, the reduced axes are left in the result array as
|
|
2771
|
+
* dimensions with size one.
|
|
2772
|
+
* @return [Numo::UInt64] returns result of prod.
|
|
2773
|
+
*/
|
|
5343
2774
|
rb_define_method(cT, "prod", uint8_prod, -1);
|
|
2775
|
+
/**
|
|
2776
|
+
* min of self.
|
|
2777
|
+
* @overload min(axis:nil, keepdims:false)
|
|
2778
|
+
* @param [Numeric,Array,Range] axis Performs min along the axis.
|
|
2779
|
+
* @param [TrueClass] keepdims If true, the reduced axes are left in the result array as
|
|
2780
|
+
* dimensions with size one.
|
|
2781
|
+
* @return [Numo::UInt8] returns result of min.
|
|
2782
|
+
*/
|
|
5344
2783
|
rb_define_method(cT, "min", uint8_min, -1);
|
|
2784
|
+
/**
|
|
2785
|
+
* max of self.
|
|
2786
|
+
* @overload max(axis:nil, keepdims:false)
|
|
2787
|
+
* @param [Numeric,Array,Range] axis Performs max along the axis.
|
|
2788
|
+
* @param [TrueClass] keepdims If true, the reduced axes are left in the result array as
|
|
2789
|
+
* dimensions with size one.
|
|
2790
|
+
* @return [Numo::UInt8] returns result of max.
|
|
2791
|
+
*/
|
|
5345
2792
|
rb_define_method(cT, "max", uint8_max, -1);
|
|
2793
|
+
/**
|
|
2794
|
+
* ptp of self.
|
|
2795
|
+
* @overload ptp(axis:nil, keepdims:false)
|
|
2796
|
+
* @param [Numeric,Array,Range] axis Performs ptp along the axis.
|
|
2797
|
+
* @param [TrueClass] keepdims If true, the reduced axes are left in the result array as
|
|
2798
|
+
* dimensions with size one.
|
|
2799
|
+
* @return [Numo::UInt8] returns result of ptp.
|
|
2800
|
+
*/
|
|
5346
2801
|
rb_define_method(cT, "ptp", uint8_ptp, -1);
|
|
2802
|
+
/**
|
|
2803
|
+
* Index of the maximum value.
|
|
2804
|
+
* @overload max_index(axis:nil)
|
|
2805
|
+
* @param [Numeric,Array,Range] axis Finds maximum values along the axis and
|
|
2806
|
+
* returns **flat 1-d indices**.
|
|
2807
|
+
* @return [Integer,Numo::Int] returns result indices.
|
|
2808
|
+
* @see #argmax
|
|
2809
|
+
* @see #max
|
|
2810
|
+
*
|
|
2811
|
+
* @example
|
|
2812
|
+
* a = Numo::NArray[3,4,1,2]
|
|
2813
|
+
* a.max_index #=> 1
|
|
2814
|
+
*
|
|
2815
|
+
* b = Numo::NArray[[3,4,1],[2,0,5]]
|
|
2816
|
+
* b.max_index #=> 5
|
|
2817
|
+
* b.max_index(axis:1) #=> [1, 5]
|
|
2818
|
+
* b.max_index(axis:0) #=> [0, 1, 5]
|
|
2819
|
+
* b[b.max_index(axis:0)] #=> [3, 4, 5]
|
|
2820
|
+
*/
|
|
5347
2821
|
rb_define_method(cT, "max_index", uint8_max_index, -1);
|
|
2822
|
+
/**
|
|
2823
|
+
* Index of the minimum value.
|
|
2824
|
+
* @overload min_index(axis:nil)
|
|
2825
|
+
* @param [Numeric,Array,Range] axis Finds minimum values along the axis and
|
|
2826
|
+
* returns **flat 1-d indices**.
|
|
2827
|
+
* @return [Integer,Numo::Int] returns result indices.
|
|
2828
|
+
* @see #argmin
|
|
2829
|
+
* @see #min
|
|
2830
|
+
*
|
|
2831
|
+
* @example
|
|
2832
|
+
* a = Numo::NArray[3,4,1,2]
|
|
2833
|
+
* a.min_index #=> 2
|
|
2834
|
+
*
|
|
2835
|
+
* b = Numo::NArray[[3,4,1],[2,0,5]]
|
|
2836
|
+
* b.min_index #=> 4
|
|
2837
|
+
* b.min_index(axis:1) #=> [2, 4]
|
|
2838
|
+
* b.min_index(axis:0) #=> [3, 4, 2]
|
|
2839
|
+
* b[b.min_index(axis:0)] #=> [2, 0, 1]
|
|
2840
|
+
*/
|
|
5348
2841
|
rb_define_method(cT, "min_index", uint8_min_index, -1);
|
|
2842
|
+
/**
|
|
2843
|
+
* Index of the maximum value.
|
|
2844
|
+
* @overload argmax(axis:nil)
|
|
2845
|
+
* @param [Numeric,Array,Range] axis Finds maximum values along the axis and
|
|
2846
|
+
* returns **indices along the axis**.
|
|
2847
|
+
* @return [Integer,Numo::Int] returns the result indices.
|
|
2848
|
+
* @see #max_index
|
|
2849
|
+
* @see #max
|
|
2850
|
+
*
|
|
2851
|
+
* @example
|
|
2852
|
+
* a = Numo::NArray[3,4,1,2]
|
|
2853
|
+
* a.argmax #=> 1
|
|
2854
|
+
*
|
|
2855
|
+
* b = Numo::NArray[[3,4,1],[2,0,5]]
|
|
2856
|
+
* b.argmax #=> 5
|
|
2857
|
+
* b.argmax(axis:1) #=> [1, 2]
|
|
2858
|
+
* b.argmax(axis:0) #=> [0, 0, 1]
|
|
2859
|
+
* b.at(b.argmax(axis:0), 0..-1) #=> [3, 4, 5]
|
|
2860
|
+
*/
|
|
5349
2861
|
rb_define_method(cT, "argmax", uint8_argmax, -1);
|
|
2862
|
+
/**
|
|
2863
|
+
* Index of the minimum value.
|
|
2864
|
+
* @overload argmin(axis:nil)
|
|
2865
|
+
* @param [Numeric,Array,Range] axis Finds minimum values along the axis and
|
|
2866
|
+
* returns **indices along the axis**.
|
|
2867
|
+
* @return [Integer,Numo::Int] returns the result indices.
|
|
2868
|
+
* @see #min_index
|
|
2869
|
+
* @see #min
|
|
2870
|
+
*
|
|
2871
|
+
* @example
|
|
2872
|
+
* a = Numo::NArray[3,4,1,2]
|
|
2873
|
+
* a.argmin #=> 2
|
|
2874
|
+
*
|
|
2875
|
+
* b = Numo::NArray[[3,4,1],[2,0,5]]
|
|
2876
|
+
* b.argmin #=> 4
|
|
2877
|
+
* b.argmin(axis:1) #=> [2, 1]
|
|
2878
|
+
* b.argmin(axis:0) #=> [1, 1, 0]
|
|
2879
|
+
* b.at(b.argmin(axis:0), 0..-1) #=> [2, 0, 1]
|
|
2880
|
+
*/
|
|
5350
2881
|
rb_define_method(cT, "argmin", uint8_argmin, -1);
|
|
2882
|
+
/**
|
|
2883
|
+
* minmax of self.
|
|
2884
|
+
* @overload minmax(axis:nil, keepdims:false)
|
|
2885
|
+
* @param [Numeric,Array,Range] axis Finds min-max along the axis.
|
|
2886
|
+
* @param [TrueClass] keepdims (keyword) If true, the reduced axes are left in
|
|
2887
|
+
* the result array as dimensions with size one.
|
|
2888
|
+
* @return [Numo::UInt8,Numo::UInt8] min and max of self.
|
|
2889
|
+
*/
|
|
5351
2890
|
rb_define_method(cT, "minmax", uint8_minmax, -1);
|
|
5352
2891
|
/**
|
|
5353
2892
|
* Element-wise maximum of two arrays.
|
|
@@ -5363,13 +2902,114 @@ void Init_numo_uint8(void) {
|
|
|
5363
2902
|
* @return [Numo::UInt8]
|
|
5364
2903
|
*/
|
|
5365
2904
|
rb_define_module_function(cT, "minimum", uint8_s_minimum, -1);
|
|
2905
|
+
/**
|
|
2906
|
+
* Count the number of occurrences of each non-negative integer value.
|
|
2907
|
+
* Only Integer-types has this method.
|
|
2908
|
+
*
|
|
2909
|
+
* @overload bincount([weight], minlength:nil)
|
|
2910
|
+
* @param [SFloat or DFloat or Array] weight (optional) Array of
|
|
2911
|
+
* float values. Its size along last axis should be same as that of self.
|
|
2912
|
+
* @param [Integer] minlength (keyword, optional) Minimum size along
|
|
2913
|
+
* last axis for the output array.
|
|
2914
|
+
* @return [UInt32 or UInt64 or SFloat or DFloat]
|
|
2915
|
+
* Returns Float NArray if weight array is supplied,
|
|
2916
|
+
* otherwise returns UInt32 or UInt64 depending on the size along last axis.
|
|
2917
|
+
* @example
|
|
2918
|
+
* Numo::Int32[0..4].bincount
|
|
2919
|
+
* # => Numo::UInt32#shape=[5]
|
|
2920
|
+
* # [1, 1, 1, 1, 1]
|
|
2921
|
+
*
|
|
2922
|
+
* Numo::Int32[0, 1, 1, 3, 2, 1, 7].bincount
|
|
2923
|
+
* # => Numo::UInt32#shape=[8]
|
|
2924
|
+
* # [1, 3, 1, 1, 0, 0, 0, 1]
|
|
2925
|
+
*
|
|
2926
|
+
* x = Numo::Int32[0, 1, 1, 3, 2, 1, 7, 23]
|
|
2927
|
+
* x.bincount.size == x.max+1
|
|
2928
|
+
* # => true
|
|
2929
|
+
*
|
|
2930
|
+
* w = Numo::DFloat[0.3, 0.5, 0.2, 0.7, 1.0, -0.6]
|
|
2931
|
+
* x = Numo::Int32[0, 1, 1, 2, 2, 2]
|
|
2932
|
+
* x.bincount(w)
|
|
2933
|
+
* # => Numo::DFloat#shape=[3]
|
|
2934
|
+
* # [0.3, 0.7, 1.1]
|
|
2935
|
+
*/
|
|
5366
2936
|
rb_define_method(cT, "bincount", uint8_bincount, -1);
|
|
2937
|
+
/**
|
|
2938
|
+
* cumsum of self.
|
|
2939
|
+
* @overload cumsum(axis:nil, nan:false)
|
|
2940
|
+
* @param [Numeric,Array,Range] axis Performs cumsum along the axis.
|
|
2941
|
+
* @param [TrueClass] nan If true, apply NaN-aware algorithm (avoid NaN if exists).
|
|
2942
|
+
* @return [Numo::UInt8] cumsum of self.
|
|
2943
|
+
*/
|
|
5367
2944
|
rb_define_method(cT, "cumsum", uint8_cumsum, -1);
|
|
2945
|
+
/**
|
|
2946
|
+
* cumprod of self.
|
|
2947
|
+
* @overload cumprod(axis:nil, nan:false)
|
|
2948
|
+
* @param [Numeric,Array,Range] axis Performs cumprod along the axis.
|
|
2949
|
+
* @param [TrueClass] nan If true, apply NaN-aware algorithm (avoid NaN if exists).
|
|
2950
|
+
* @return [Numo::UInt8] cumprod of self.
|
|
2951
|
+
*/
|
|
5368
2952
|
rb_define_method(cT, "cumprod", uint8_cumprod, -1);
|
|
2953
|
+
/**
|
|
2954
|
+
* Binary mulsum.
|
|
2955
|
+
*
|
|
2956
|
+
* @overload mulsum(other, axis:nil, keepdims:false)
|
|
2957
|
+
* @param [Numo::NArray,Numeric] other
|
|
2958
|
+
* @param [Numeric,Array,Range] axis Performs mulsum along the axis.
|
|
2959
|
+
* @param [TrueClass] keepdims (keyword) If true, the reduced axes are left in
|
|
2960
|
+
* the result array as dimensions with size one.
|
|
2961
|
+
* @return [Numo::NArray] mulsum of self and other.
|
|
2962
|
+
*/
|
|
5369
2963
|
rb_define_method(cT, "mulsum", uint8_mulsum, -1);
|
|
2964
|
+
/**
|
|
2965
|
+
* Set linear sequence of numbers to self. The sequence is obtained from
|
|
2966
|
+
* beg+i*step
|
|
2967
|
+
* where i is 1-dimensional index.
|
|
2968
|
+
* @overload seq([beg,[step]])
|
|
2969
|
+
* @param [Numeric] beg beginning of sequence. (default=0)
|
|
2970
|
+
* @param [Numeric] step step of sequence. (default=1)
|
|
2971
|
+
* @return [Numo::UInt8] self.
|
|
2972
|
+
* @example
|
|
2973
|
+
* Numo::DFloat.new(6).seq(1,-0.2)
|
|
2974
|
+
* # => Numo::DFloat#shape=[6]
|
|
2975
|
+
* # [1, 0.8, 0.6, 0.4, 0.2, 0]
|
|
2976
|
+
*
|
|
2977
|
+
* Numo::DComplex.new(6).seq(1,-0.2+0.2i)
|
|
2978
|
+
* # => Numo::DComplex#shape=[6]
|
|
2979
|
+
* # [1+0i, 0.8+0.2i, 0.6+0.4i, 0.4+0.6i, 0.2+0.8i, 0+1i]
|
|
2980
|
+
*/
|
|
5370
2981
|
rb_define_method(cT, "seq", uint8_seq, -1);
|
|
2982
|
+
/**
|
|
2983
|
+
* Eye: Set a value to diagonal components, set 0 to non-diagonal components.
|
|
2984
|
+
* @overload eye([element,offset])
|
|
2985
|
+
* @param [Numeric] element Diagonal element to be stored. Default is 1.
|
|
2986
|
+
* @param [Integer] offset Diagonal offset from the main diagonal. The
|
|
2987
|
+
* default is 0. k>0 for diagonals above the main diagonal, and k<0
|
|
2988
|
+
* for diagonals below the main diagonal.
|
|
2989
|
+
* @return [Numo::UInt8] eye of self.
|
|
2990
|
+
*/
|
|
5371
2991
|
rb_define_method(cT, "eye", uint8_eye, -1);
|
|
5372
2992
|
rb_define_alias(cT, "indgen", "seq");
|
|
2993
|
+
/**
|
|
2994
|
+
* Generate uniformly distributed random numbers on self narray.
|
|
2995
|
+
* @overload rand([[low],high])
|
|
2996
|
+
* @param [Numeric] low lower inclusive boundary of random numbers. (default=0)
|
|
2997
|
+
* @param [Numeric] high upper exclusive boundary of random numbers.
|
|
2998
|
+
* (default=1 or 1+1i for complex types)
|
|
2999
|
+
* @return [Numo::UInt8] self.
|
|
3000
|
+
* @example
|
|
3001
|
+
* Numo::DFloat.new(6).rand
|
|
3002
|
+
* # => Numo::DFloat#shape=[6]
|
|
3003
|
+
* # [0.0617545, 0.373067, 0.794815, 0.201042, 0.116041, 0.344032]
|
|
3004
|
+
*
|
|
3005
|
+
* Numo::DComplex.new(6).rand(5+5i)
|
|
3006
|
+
* # => Numo::DComplex#shape=[6]
|
|
3007
|
+
* # [2.69974+3.68908i, 0.825443+0.254414i, 0.540323+0.34354i, 4.52061+2.39322i, ...]
|
|
3008
|
+
*
|
|
3009
|
+
* Numo::Int32.new(6).rand(2,5)
|
|
3010
|
+
* # => Numo::Int32#shape=[6]
|
|
3011
|
+
* # [4, 3, 3, 2, 4, 2]
|
|
3012
|
+
*/
|
|
5373
3013
|
rb_define_method(cT, "rand", uint8_rand, -1);
|
|
5374
3014
|
rb_define_method(cT, "poly", uint8_poly, -2);
|
|
5375
3015
|
|