numo-narray-alt 0.9.11 → 0.9.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (97) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +0 -1
  3. data/README.md +7 -0
  4. data/ext/numo/narray/numo/narray.h +2 -2
  5. data/ext/numo/narray/numo/types/robj_macro.h +1 -1
  6. data/ext/numo/narray/src/mh/bincount.h +233 -0
  7. data/ext/numo/narray/src/mh/bit/and.h +225 -0
  8. data/ext/numo/narray/src/mh/bit/left_shift.h +225 -0
  9. data/ext/numo/narray/src/mh/bit/not.h +173 -0
  10. data/ext/numo/narray/src/mh/bit/or.h +225 -0
  11. data/ext/numo/narray/src/mh/bit/right_shift.h +225 -0
  12. data/ext/numo/narray/src/mh/bit/xor.h +225 -0
  13. data/ext/numo/narray/src/mh/coerce_cast.h +9 -0
  14. data/ext/numo/narray/src/mh/comp/binary_func.h +37 -0
  15. data/ext/numo/narray/src/mh/comp/eq.h +26 -0
  16. data/ext/numo/narray/src/mh/comp/ge.h +26 -0
  17. data/ext/numo/narray/src/mh/comp/gt.h +26 -0
  18. data/ext/numo/narray/src/mh/comp/le.h +26 -0
  19. data/ext/numo/narray/src/mh/comp/lt.h +26 -0
  20. data/ext/numo/narray/src/mh/comp/ne.h +26 -0
  21. data/ext/numo/narray/src/mh/comp/nearly_eq.h +26 -0
  22. data/ext/numo/narray/src/mh/divmod.h +142 -0
  23. data/ext/numo/narray/src/mh/eye.h +1 -1
  24. data/ext/numo/narray/src/mh/fill.h +94 -0
  25. data/ext/numo/narray/src/mh/format.h +108 -0
  26. data/ext/numo/narray/src/mh/format_to_a.h +89 -0
  27. data/ext/numo/narray/src/mh/inspect.h +33 -0
  28. data/ext/numo/narray/src/mh/isfinite.h +42 -0
  29. data/ext/numo/narray/src/mh/isinf.h +42 -0
  30. data/ext/numo/narray/src/mh/isnan.h +42 -0
  31. data/ext/numo/narray/src/mh/isneginf.h +42 -0
  32. data/ext/numo/narray/src/mh/isposinf.h +42 -0
  33. data/ext/numo/narray/src/mh/math/acos.h +2 -2
  34. data/ext/numo/narray/src/mh/math/acosh.h +2 -2
  35. data/ext/numo/narray/src/mh/math/asin.h +2 -2
  36. data/ext/numo/narray/src/mh/math/asinh.h +2 -2
  37. data/ext/numo/narray/src/mh/math/atan.h +2 -2
  38. data/ext/numo/narray/src/mh/math/atan2.h +3 -3
  39. data/ext/numo/narray/src/mh/math/atanh.h +2 -2
  40. data/ext/numo/narray/src/mh/math/cbrt.h +2 -2
  41. data/ext/numo/narray/src/mh/math/cos.h +2 -2
  42. data/ext/numo/narray/src/mh/math/cosh.h +2 -2
  43. data/ext/numo/narray/src/mh/math/erf.h +2 -2
  44. data/ext/numo/narray/src/mh/math/erfc.h +2 -2
  45. data/ext/numo/narray/src/mh/math/exp.h +2 -2
  46. data/ext/numo/narray/src/mh/math/exp10.h +2 -2
  47. data/ext/numo/narray/src/mh/math/exp2.h +2 -2
  48. data/ext/numo/narray/src/mh/math/expm1.h +2 -2
  49. data/ext/numo/narray/src/mh/math/frexp.h +3 -3
  50. data/ext/numo/narray/src/mh/math/hypot.h +3 -3
  51. data/ext/numo/narray/src/mh/math/ldexp.h +3 -3
  52. data/ext/numo/narray/src/mh/math/log.h +2 -2
  53. data/ext/numo/narray/src/mh/math/log10.h +2 -2
  54. data/ext/numo/narray/src/mh/math/log1p.h +2 -2
  55. data/ext/numo/narray/src/mh/math/log2.h +2 -2
  56. data/ext/numo/narray/src/mh/math/sin.h +2 -2
  57. data/ext/numo/narray/src/mh/math/sinc.h +2 -2
  58. data/ext/numo/narray/src/mh/math/sinh.h +2 -2
  59. data/ext/numo/narray/src/mh/math/sqrt.h +8 -8
  60. data/ext/numo/narray/src/mh/math/tan.h +2 -2
  61. data/ext/numo/narray/src/mh/math/tanh.h +2 -2
  62. data/ext/numo/narray/src/mh/math/unary_func.h +3 -3
  63. data/ext/numo/narray/src/mh/minus.h +125 -0
  64. data/ext/numo/narray/src/mh/op/add.h +78 -0
  65. data/ext/numo/narray/src/mh/op/binary_func.h +423 -0
  66. data/ext/numo/narray/src/mh/op/div.h +118 -0
  67. data/ext/numo/narray/src/mh/op/mod.h +108 -0
  68. data/ext/numo/narray/src/mh/op/mul.h +78 -0
  69. data/ext/numo/narray/src/mh/op/sub.h +78 -0
  70. data/ext/numo/narray/src/mh/pow.h +197 -0
  71. data/ext/numo/narray/src/mh/rand.h +2 -2
  72. data/ext/numo/narray/src/mh/rand_norm.h +125 -0
  73. data/ext/numo/narray/src/mh/reciprocal.h +125 -0
  74. data/ext/numo/narray/src/mh/round/ceil.h +11 -0
  75. data/ext/numo/narray/src/mh/round/floor.h +11 -0
  76. data/ext/numo/narray/src/mh/round/rint.h +9 -0
  77. data/ext/numo/narray/src/mh/round/round.h +11 -0
  78. data/ext/numo/narray/src/mh/round/trunc.h +11 -0
  79. data/ext/numo/narray/src/mh/round/unary_func.h +127 -0
  80. data/ext/numo/narray/src/mh/sign.h +125 -0
  81. data/ext/numo/narray/src/mh/square.h +125 -0
  82. data/ext/numo/narray/src/mh/to_a.h +78 -0
  83. data/ext/numo/narray/src/t_bit.c +45 -234
  84. data/ext/numo/narray/src/t_dcomplex.c +457 -2075
  85. data/ext/numo/narray/src/t_dfloat.c +154 -2560
  86. data/ext/numo/narray/src/t_int16.c +408 -2542
  87. data/ext/numo/narray/src/t_int32.c +408 -2542
  88. data/ext/numo/narray/src/t_int64.c +408 -2542
  89. data/ext/numo/narray/src/t_int8.c +409 -2138
  90. data/ext/numo/narray/src/t_robject.c +376 -2161
  91. data/ext/numo/narray/src/t_scomplex.c +435 -2053
  92. data/ext/numo/narray/src/t_sfloat.c +149 -2557
  93. data/ext/numo/narray/src/t_uint16.c +407 -2537
  94. data/ext/numo/narray/src/t_uint32.c +407 -2537
  95. data/ext/numo/narray/src/t_uint64.c +407 -2537
  96. data/ext/numo/narray/src/t_uint8.c +407 -2132
  97. metadata +47 -2
@@ -43,6 +43,35 @@ 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/pow.h"
59
+ #include "mh/minus.h"
60
+ #include "mh/reciprocal.h"
61
+ #include "mh/sign.h"
62
+ #include "mh/square.h"
63
+ #include "mh/comp/eq.h"
64
+ #include "mh/comp/ne.h"
65
+ #include "mh/comp/gt.h"
66
+ #include "mh/comp/ge.h"
67
+ #include "mh/comp/lt.h"
68
+ #include "mh/comp/le.h"
69
+ #include "mh/bit/and.h"
70
+ #include "mh/bit/or.h"
71
+ #include "mh/bit/xor.h"
72
+ #include "mh/bit/not.h"
73
+ #include "mh/bit/left_shift.h"
74
+ #include "mh/bit/right_shift.h"
46
75
  #include "mh/clip.h"
47
76
  #include "mh/sum.h"
48
77
  #include "mh/prod.h"
@@ -56,6 +85,7 @@ extern VALUE cRT;
56
85
  #include "mh/maximum.h"
57
86
  #include "mh/minimum.h"
58
87
  #include "mh/minmax.h"
88
+ #include "mh/bincount.h"
59
89
  #include "mh/cumsum.h"
60
90
  #include "mh/cumprod.h"
61
91
  #include "mh/mulsum.h"
@@ -69,6 +99,35 @@ extern VALUE cRT;
69
99
 
70
100
  typedef u_int8_t uint8; // Type aliases for shorter notation
71
101
  // following the codebase naming convention.
102
+ DEF_NARRAY_COERCE_CAST_METHOD_FUNC(uint8)
103
+ DEF_NARRAY_TO_A_METHOD_FUNC(uint8)
104
+ DEF_NARRAY_FILL_METHOD_FUNC(uint8)
105
+ DEF_NARRAY_FORMAT_METHOD_FUNC(uint8)
106
+ DEF_NARRAY_FORMAT_TO_A_METHOD_FUNC(uint8)
107
+ DEF_NARRAY_INSPECT_METHOD_FUNC(uint8)
108
+ DEF_NARRAY_INT8_ADD_METHOD_FUNC(uint8, numo_cUInt8)
109
+ DEF_NARRAY_INT8_SUB_METHOD_FUNC(uint8, numo_cUInt8)
110
+ DEF_NARRAY_INT8_MUL_METHOD_FUNC(uint8, numo_cUInt8)
111
+ DEF_NARRAY_INT8_DIV_METHOD_FUNC(uint8, numo_cUInt8)
112
+ DEF_NARRAY_INT8_MOD_METHOD_FUNC(uint8, numo_cUInt8)
113
+ DEF_NARRAY_INT_DIVMOD_METHOD_FUNC(uint8, numo_cUInt8)
114
+ DEF_NARRAY_POW_METHOD_FUNC(uint8, numo_cUInt8)
115
+ DEF_NARRAY_INT8_MINUS_METHOD_FUNC(uint8, numo_cUInt8)
116
+ DEF_NARRAY_INT8_RECIPROCAL_METHOD_FUNC(uint8, numo_cUInt8)
117
+ DEF_NARRAY_INT8_SIGN_METHOD_FUNC(uint8, numo_cUInt8)
118
+ DEF_NARRAY_INT8_SQUARE_METHOD_FUNC(uint8, numo_cUInt8)
119
+ DEF_NARRAY_EQ_METHOD_FUNC(uint8, numo_cUInt8)
120
+ DEF_NARRAY_NE_METHOD_FUNC(uint8, numo_cUInt8)
121
+ DEF_NARRAY_GT_METHOD_FUNC(uint8, numo_cUInt8)
122
+ DEF_NARRAY_GE_METHOD_FUNC(uint8, numo_cUInt8)
123
+ DEF_NARRAY_LT_METHOD_FUNC(uint8, numo_cUInt8)
124
+ DEF_NARRAY_LE_METHOD_FUNC(uint8, numo_cUInt8)
125
+ DEF_NARRAY_INT8_BIT_AND_METHOD_FUNC(uint8, numo_cUInt8)
126
+ DEF_NARRAY_INT8_BIT_OR_METHOD_FUNC(uint8, numo_cUInt8)
127
+ DEF_NARRAY_INT8_BIT_XOR_METHOD_FUNC(uint8, numo_cUInt8)
128
+ DEF_NARRAY_INT8_BIT_NOT_METHOD_FUNC(uint8, numo_cUInt8)
129
+ DEF_NARRAY_INT8_LEFT_SHIFT_METHOD_FUNC(uint8, numo_cUInt8)
130
+ DEF_NARRAY_INT8_RIGHT_SHIFT_METHOD_FUNC(uint8, numo_cUInt8)
72
131
  DEF_NARRAY_CLIP_METHOD_FUNC(uint8, numo_cUInt8)
73
132
  DEF_NARRAY_INT_SUM_METHOD_FUNC(uint8, numo_cUInt8, u_int64_t, numo_cUInt64)
74
133
  DEF_NARRAY_INT_PROD_METHOD_FUNC(uint8, numo_cUInt8, u_int64_t, numo_cUInt64)
@@ -82,6 +141,7 @@ DEF_NARRAY_INT_ARGMIN_METHOD_FUNC(uint8)
82
141
  DEF_NARRAY_INT_MAXIMUM_METHOD_FUNC(uint8, numo_cUInt8)
83
142
  DEF_NARRAY_INT_MINIMUM_METHOD_FUNC(uint8, numo_cUInt8)
84
143
  DEF_NARRAY_INT_MINMAX_METHOD_FUNC(uint8, numo_cUInt8)
144
+ DEF_NARRAY_UINT_BINCOUNT_METHOD_FUNC(uint8, numo_cUInt8)
85
145
  DEF_NARRAY_INT_CUMSUM_METHOD_FUNC(uint8, numo_cUInt8)
86
146
  DEF_NARRAY_INT_CUMPROD_METHOD_FUNC(uint8, numo_cUInt8)
87
147
  DEF_NARRAY_INT_MULSUM_METHOD_FUNC(uint8, numo_cUInt8)
@@ -1252,204 +1312,6 @@ static VALUE uint8_aset(int argc, VALUE* argv, VALUE self) {
1252
1312
  return argv[argc];
1253
1313
  }
1254
1314
 
1255
- /*
1256
- return NArray with cast to the type of self.
1257
- @overload coerce_cast(type)
1258
- @return [nil]
1259
- */
1260
- static VALUE uint8_coerce_cast(VALUE self, VALUE type) {
1261
- return Qnil;
1262
- }
1263
-
1264
- static void iter_uint8_to_a(na_loop_t* const lp) {
1265
- size_t i, s1;
1266
- char* p1;
1267
- size_t* idx1;
1268
- dtype x;
1269
- volatile VALUE a, y;
1270
-
1271
- INIT_COUNTER(lp, i);
1272
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
1273
- a = rb_ary_new2(i);
1274
- rb_ary_push(lp->args[1].value, a);
1275
- if (idx1) {
1276
- for (; i--;) {
1277
- GET_DATA_INDEX(p1, idx1, dtype, x);
1278
- y = m_data_to_num(x);
1279
- rb_ary_push(a, y);
1280
- }
1281
- } else {
1282
- for (; i--;) {
1283
- GET_DATA_STRIDE(p1, s1, dtype, x);
1284
- y = m_data_to_num(x);
1285
- rb_ary_push(a, y);
1286
- }
1287
- }
1288
- }
1289
-
1290
- /*
1291
- Convert self to Array.
1292
- @overload to_a
1293
- @return [Array]
1294
- */
1295
- static VALUE uint8_to_a(VALUE self) {
1296
- ndfunc_arg_in_t ain[3] = { { Qnil, 0 }, { sym_loop_opt }, { sym_option } };
1297
- ndfunc_arg_out_t aout[1] = { { rb_cArray, 0 } }; // dummy?
1298
- ndfunc_t ndf = { iter_uint8_to_a, FULL_LOOP_NIP, 3, 1, ain, aout };
1299
- return na_ndloop_cast_narray_to_rarray(&ndf, self, Qnil);
1300
- }
1301
-
1302
- static void iter_uint8_fill(na_loop_t* const lp) {
1303
- size_t i;
1304
- char* p1;
1305
- ssize_t s1;
1306
- size_t* idx1;
1307
- VALUE x = lp->option;
1308
- dtype y;
1309
- INIT_COUNTER(lp, i);
1310
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
1311
- y = m_num_to_data(x);
1312
- if (idx1) {
1313
- for (; i--;) {
1314
- SET_DATA_INDEX(p1, idx1, dtype, y);
1315
- }
1316
- } else {
1317
- for (; i--;) {
1318
- SET_DATA_STRIDE(p1, s1, dtype, y);
1319
- }
1320
- }
1321
- }
1322
-
1323
- /*
1324
- Fill elements with other.
1325
- @overload fill other
1326
- @param [Numeric] other
1327
- @return [Numo::UInt8] self.
1328
- */
1329
- static VALUE uint8_fill(VALUE self, VALUE val) {
1330
- ndfunc_arg_in_t ain[2] = { { OVERWRITE, 0 }, { sym_option } };
1331
- ndfunc_t ndf = { iter_uint8_fill, FULL_LOOP, 2, 0, ain, 0 };
1332
-
1333
- na_ndloop(&ndf, 2, self, val);
1334
- return self;
1335
- }
1336
-
1337
- static VALUE format_uint8(VALUE fmt, dtype* x) {
1338
- // fix-me
1339
- char s[48];
1340
- int n;
1341
-
1342
- if (NIL_P(fmt)) {
1343
- n = m_sprintf(s, *x);
1344
- return rb_str_new(s, n);
1345
- }
1346
- return rb_funcall(fmt, '%', 1, m_data_to_num(*x));
1347
- }
1348
-
1349
- static void iter_uint8_format(na_loop_t* const lp) {
1350
- size_t i;
1351
- char *p1, *p2;
1352
- ssize_t s1, s2;
1353
- size_t* idx1;
1354
- dtype* x;
1355
- VALUE y;
1356
- VALUE fmt = lp->option;
1357
- INIT_COUNTER(lp, i);
1358
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
1359
- INIT_PTR(lp, 1, p2, s2);
1360
- if (idx1) {
1361
- for (; i--;) {
1362
- x = (dtype*)(p1 + *idx1);
1363
- idx1++;
1364
- y = format_uint8(fmt, x);
1365
- SET_DATA_STRIDE(p2, s2, VALUE, y);
1366
- }
1367
- } else {
1368
- for (; i--;) {
1369
- x = (dtype*)p1;
1370
- p1 += s1;
1371
- y = format_uint8(fmt, x);
1372
- SET_DATA_STRIDE(p2, s2, VALUE, y);
1373
- }
1374
- }
1375
- }
1376
-
1377
- /*
1378
- Format elements into strings.
1379
- @overload format format
1380
- @param [String] format
1381
- @return [Numo::RObject] array of formatted strings.
1382
- */
1383
- static VALUE uint8_format(int argc, VALUE* argv, VALUE self) {
1384
- VALUE fmt = Qnil;
1385
-
1386
- ndfunc_arg_in_t ain[2] = { { Qnil, 0 }, { sym_option } };
1387
- ndfunc_arg_out_t aout[1] = { { numo_cRObject, 0 } };
1388
- ndfunc_t ndf = { iter_uint8_format, FULL_LOOP_NIP, 2, 1, ain, aout };
1389
-
1390
- rb_scan_args(argc, argv, "01", &fmt);
1391
- return na_ndloop(&ndf, 2, self, fmt);
1392
- }
1393
-
1394
- static void iter_uint8_format_to_a(na_loop_t* const lp) {
1395
- size_t i;
1396
- char* p1;
1397
- ssize_t s1;
1398
- size_t* idx1;
1399
- dtype* x;
1400
- VALUE y;
1401
- volatile VALUE a;
1402
- VALUE fmt = lp->option;
1403
- INIT_COUNTER(lp, i);
1404
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
1405
- a = rb_ary_new2(i);
1406
- rb_ary_push(lp->args[1].value, a);
1407
- if (idx1) {
1408
- for (; i--;) {
1409
- x = (dtype*)(p1 + *idx1);
1410
- idx1++;
1411
- y = format_uint8(fmt, x);
1412
- rb_ary_push(a, y);
1413
- }
1414
- } else {
1415
- for (; i--;) {
1416
- x = (dtype*)p1;
1417
- p1 += s1;
1418
- y = format_uint8(fmt, x);
1419
- rb_ary_push(a, y);
1420
- }
1421
- }
1422
- }
1423
-
1424
- /*
1425
- Format elements into strings.
1426
- @overload format_to_a format
1427
- @param [String] format
1428
- @return [Array] array of formatted strings.
1429
- */
1430
- static VALUE uint8_format_to_a(int argc, VALUE* argv, VALUE self) {
1431
- VALUE fmt = Qnil;
1432
- ndfunc_arg_in_t ain[3] = { { Qnil, 0 }, { sym_loop_opt }, { sym_option } };
1433
- ndfunc_arg_out_t aout[1] = { { rb_cArray, 0 } }; // dummy?
1434
- ndfunc_t ndf = { iter_uint8_format_to_a, FULL_LOOP_NIP, 3, 1, ain, aout };
1435
-
1436
- rb_scan_args(argc, argv, "01", &fmt);
1437
- return na_ndloop_cast_narray_to_rarray(&ndf, self, fmt);
1438
- }
1439
-
1440
- static VALUE iter_uint8_inspect(char* ptr, size_t pos, VALUE fmt) {
1441
- return format_uint8(fmt, (dtype*)(ptr + pos));
1442
- }
1443
-
1444
- /*
1445
- Returns a string containing a human-readable representation of NArray.
1446
- @overload inspect
1447
- @return [String]
1448
- */
1449
- static VALUE uint8_inspect(VALUE ary) {
1450
- return na_ndloop_inspect(ary, iter_uint8_inspect, Qnil);
1451
- }
1452
-
1453
1315
  static void iter_uint8_each(na_loop_t* const lp) {
1454
1316
  size_t i, s1;
1455
1317
  char* p1;
@@ -1571,1978 +1433,194 @@ static void iter_uint8_each_with_index(na_loop_t* const lp) {
1571
1433
 
1572
1434
  c = (size_t*)(lp->opt_ptr);
1573
1435
  nd = lp->ndim;
1574
- if (nd > 0) {
1575
- nd--;
1576
- }
1577
- md = nd + 2;
1578
- a = ALLOCA_N(VALUE, md);
1579
-
1580
- INIT_COUNTER(lp, i);
1581
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
1582
- c[nd] = 0;
1583
- if (idx1) {
1584
- for (; i--;) {
1585
- GET_DATA_INDEX(p1, idx1, dtype, x);
1586
- yield_each_with_index(x, c, a, nd, md);
1587
- c[nd]++;
1588
- }
1589
- } else {
1590
- for (; i--;) {
1591
- GET_DATA_STRIDE(p1, s1, dtype, x);
1592
- yield_each_with_index(x, c, a, nd, md);
1593
- c[nd]++;
1594
- }
1595
- }
1596
- }
1597
-
1598
- /*
1599
- Invokes the given block once for each element of self,
1600
- passing that element and indices along each axis as parameters.
1601
- @overload each_with_index
1602
- For a block `{|x,i,j,...| ... }`,
1603
- @yieldparam [Numeric] x an element
1604
- @yieldparam [Integer] i,j,... multitimensional indices
1605
- @return [Numo::NArray] self
1606
- @see #each
1607
- @see #map_with_index
1608
- */
1609
- static VALUE uint8_each_with_index(VALUE self) {
1610
- ndfunc_arg_in_t ain[1] = { { Qnil, 0 } };
1611
- ndfunc_t ndf = { iter_uint8_each_with_index, FULL_LOOP_NIP, 1, 0, ain, 0 };
1612
-
1613
- na_ndloop_with_index(&ndf, 1, self);
1614
- return self;
1615
- }
1616
-
1617
- static inline dtype yield_map_with_index(dtype x, size_t* c, VALUE* a, int nd, int md) {
1618
- int j;
1619
- VALUE y;
1620
-
1621
- a[0] = m_data_to_num(x);
1622
- for (j = 0; j <= nd; j++) {
1623
- a[j + 1] = SIZET2NUM(c[j]);
1624
- }
1625
- y = rb_yield(rb_ary_new4(md, a));
1626
- return m_num_to_data(y);
1627
- }
1628
-
1629
- static void iter_uint8_map_with_index(na_loop_t* const lp) {
1630
- size_t i;
1631
- char *p1, *p2;
1632
- ssize_t s1, s2;
1633
- size_t *idx1, *idx2;
1634
- dtype x;
1635
- VALUE* a;
1636
- size_t* c;
1637
- int nd, md;
1638
-
1639
- c = (size_t*)(lp->opt_ptr);
1640
- nd = lp->ndim;
1641
- if (nd > 0) {
1642
- nd--;
1643
- }
1644
- md = nd + 2;
1645
- a = ALLOCA_N(VALUE, md);
1646
-
1647
- INIT_COUNTER(lp, i);
1648
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
1649
- INIT_PTR_IDX(lp, 1, p2, s2, idx2);
1650
-
1651
- c[nd] = 0;
1652
- if (idx1) {
1653
- if (idx2) {
1654
- for (; i--;) {
1655
- GET_DATA_INDEX(p1, idx1, dtype, x);
1656
- x = yield_map_with_index(x, c, a, nd, md);
1657
- SET_DATA_INDEX(p2, idx2, dtype, x);
1658
- c[nd]++;
1659
- }
1660
- } else {
1661
- for (; i--;) {
1662
- GET_DATA_INDEX(p1, idx1, dtype, x);
1663
- x = yield_map_with_index(x, c, a, nd, md);
1664
- SET_DATA_STRIDE(p2, s2, dtype, x);
1665
- c[nd]++;
1666
- }
1667
- }
1668
- } else {
1669
- if (idx2) {
1670
- for (; i--;) {
1671
- GET_DATA_STRIDE(p1, s1, dtype, x);
1672
- x = yield_map_with_index(x, c, a, nd, md);
1673
- SET_DATA_INDEX(p2, idx2, dtype, x);
1674
- c[nd]++;
1675
- }
1676
- } else {
1677
- for (; i--;) {
1678
- GET_DATA_STRIDE(p1, s1, dtype, x);
1679
- x = yield_map_with_index(x, c, a, nd, md);
1680
- SET_DATA_STRIDE(p2, s2, dtype, x);
1681
- c[nd]++;
1682
- }
1683
- }
1684
- }
1685
- }
1686
-
1687
- /*
1688
- Invokes the given block once for each element of self,
1689
- passing that element and indices along each axis as parameters.
1690
- Creates a new NArray containing the values returned by the block.
1691
- Inplace option is allowed, i.e., `nary.inplace.map` overwrites `nary`.
1692
- @overload map_with_index
1693
- For a block `{|x,i,j,...| ... }`,
1694
- @yieldparam [Numeric] x an element
1695
- @yieldparam [Integer] i,j,... multitimensional indices
1696
- @return [Numo::NArray] mapped array
1697
- @see #map
1698
- @see #each_with_index
1699
- */
1700
- static VALUE uint8_map_with_index(VALUE self) {
1701
- ndfunc_arg_in_t ain[1] = { { Qnil, 0 } };
1702
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
1703
- ndfunc_t ndf = { iter_uint8_map_with_index, FULL_LOOP, 1, 1, ain, aout };
1704
-
1705
- return na_ndloop_with_index(&ndf, 1, self);
1706
- }
1707
-
1708
- static void iter_uint8_abs(na_loop_t* const lp) {
1709
- size_t i;
1710
- char *p1, *p2;
1711
- ssize_t s1, s2;
1712
- size_t *idx1, *idx2;
1713
- dtype x;
1714
- rtype y;
1715
- INIT_COUNTER(lp, i);
1716
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
1717
- INIT_PTR_IDX(lp, 1, p2, s2, idx2);
1718
- if (idx1) {
1719
- if (idx2) {
1720
- for (; i--;) {
1721
- GET_DATA_INDEX(p1, idx1, dtype, x);
1722
- y = m_abs(x);
1723
- SET_DATA_INDEX(p2, idx2, rtype, y);
1724
- }
1725
- } else {
1726
- for (; i--;) {
1727
- GET_DATA_INDEX(p1, idx1, dtype, x);
1728
- y = m_abs(x);
1729
- SET_DATA_STRIDE(p2, s2, rtype, y);
1730
- }
1731
- }
1732
- } else {
1733
- if (idx2) {
1734
- for (; i--;) {
1735
- GET_DATA_STRIDE(p1, s1, dtype, x);
1736
- y = m_abs(x);
1737
- SET_DATA_INDEX(p2, idx2, rtype, y);
1738
- }
1739
- } else {
1740
- for (; i--;) {
1741
- GET_DATA_STRIDE(p1, s1, dtype, x);
1742
- y = m_abs(x);
1743
- SET_DATA_STRIDE(p2, s2, rtype, y);
1744
- }
1745
- }
1746
- }
1747
- }
1748
-
1749
- /*
1750
- abs of self.
1751
- @overload abs
1752
- @return [Numo::UInt8] abs of self.
1753
- */
1754
- static VALUE uint8_abs(VALUE self) {
1755
- ndfunc_arg_in_t ain[1] = { { cT, 0 } };
1756
- ndfunc_arg_out_t aout[1] = { { cRT, 0 } };
1757
- ndfunc_t ndf = { iter_uint8_abs, FULL_LOOP, 1, 1, ain, aout };
1758
-
1759
- return na_ndloop(&ndf, 1, self);
1760
- }
1761
-
1762
- #define check_intdivzero(y) \
1763
- {}
1764
-
1765
- static void iter_uint8_add(na_loop_t* const lp) {
1766
- size_t i = 0;
1767
- size_t n;
1768
- char *p1, *p2, *p3;
1769
- ssize_t s1, s2, s3;
1770
-
1771
- INIT_COUNTER(lp, n);
1772
- INIT_PTR(lp, 0, p1, s1);
1773
- INIT_PTR(lp, 1, p2, s2);
1774
- INIT_PTR(lp, 2, p3, s3);
1775
-
1776
- //
1777
-
1778
- if (s2 == 0) { // Broadcasting from scalar value.
1779
- check_intdivzero(*(dtype*)p2);
1780
- if (s1 == sizeof(dtype) && s3 == sizeof(dtype)) {
1781
- if (p1 == p3) { // inplace case
1782
- for (; i < n; i++) {
1783
- ((dtype*)p1)[i] = m_add(((dtype*)p1)[i], *(dtype*)p2);
1784
- }
1785
- } else {
1786
- for (; i < n; i++) {
1787
- ((dtype*)p3)[i] = m_add(((dtype*)p1)[i], *(dtype*)p2);
1788
- }
1789
- }
1790
- } else {
1791
- for (i = 0; i < n; i++) {
1792
- *(dtype*)p3 = m_add(*(dtype*)p1, *(dtype*)p2);
1793
- p1 += s1;
1794
- p3 += s3;
1795
- }
1796
- }
1797
- } else {
1798
- if (p1 == p3) { // inplace case
1799
- for (i = 0; i < n; i++) {
1800
- check_intdivzero(*(dtype*)p2);
1801
- *(dtype*)p1 = m_add(*(dtype*)p1, *(dtype*)p2);
1802
- p1 += s1;
1803
- p2 += s2;
1804
- }
1805
- } else {
1806
- for (i = 0; i < n; i++) {
1807
- check_intdivzero(*(dtype*)p2);
1808
- *(dtype*)p3 = m_add(*(dtype*)p1, *(dtype*)p2);
1809
- p1 += s1;
1810
- p2 += s2;
1811
- p3 += s3;
1812
- }
1813
- }
1814
- }
1815
-
1816
- return;
1817
- //
1818
- }
1819
- #undef check_intdivzero
1820
-
1821
- static VALUE uint8_add_self(VALUE self, VALUE other) {
1822
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
1823
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
1824
- ndfunc_t ndf = { iter_uint8_add, STRIDE_LOOP, 2, 1, ain, aout };
1825
-
1826
- return na_ndloop(&ndf, 2, self, other);
1827
- }
1828
-
1829
- /*
1830
- Binary add.
1831
- @overload + other
1832
- @param [Numo::NArray,Numeric] other
1833
- @return [Numo::NArray] self + other
1834
- */
1835
- static VALUE uint8_add(VALUE self, VALUE other) {
1836
-
1837
- VALUE klass, v;
1838
-
1839
- klass = na_upcast(rb_obj_class(self), rb_obj_class(other));
1840
- if (klass == cT) {
1841
- return uint8_add_self(self, other);
1842
- } else {
1843
- v = rb_funcall(klass, id_cast, 1, self);
1844
- return rb_funcall(v, '+', 1, other);
1845
- }
1846
- }
1847
-
1848
- #define check_intdivzero(y) \
1849
- {}
1850
-
1851
- static void iter_uint8_sub(na_loop_t* const lp) {
1852
- size_t i = 0;
1853
- size_t n;
1854
- char *p1, *p2, *p3;
1855
- ssize_t s1, s2, s3;
1856
-
1857
- INIT_COUNTER(lp, n);
1858
- INIT_PTR(lp, 0, p1, s1);
1859
- INIT_PTR(lp, 1, p2, s2);
1860
- INIT_PTR(lp, 2, p3, s3);
1861
-
1862
- //
1863
-
1864
- if (s2 == 0) { // Broadcasting from scalar value.
1865
- check_intdivzero(*(dtype*)p2);
1866
- if (s1 == sizeof(dtype) && s3 == sizeof(dtype)) {
1867
- if (p1 == p3) { // inplace case
1868
- for (; i < n; i++) {
1869
- ((dtype*)p1)[i] = m_sub(((dtype*)p1)[i], *(dtype*)p2);
1870
- }
1871
- } else {
1872
- for (; i < n; i++) {
1873
- ((dtype*)p3)[i] = m_sub(((dtype*)p1)[i], *(dtype*)p2);
1874
- }
1875
- }
1876
- } else {
1877
- for (i = 0; i < n; i++) {
1878
- *(dtype*)p3 = m_sub(*(dtype*)p1, *(dtype*)p2);
1879
- p1 += s1;
1880
- p3 += s3;
1881
- }
1882
- }
1883
- } else {
1884
- if (p1 == p3) { // inplace case
1885
- for (i = 0; i < n; i++) {
1886
- check_intdivzero(*(dtype*)p2);
1887
- *(dtype*)p1 = m_sub(*(dtype*)p1, *(dtype*)p2);
1888
- p1 += s1;
1889
- p2 += s2;
1890
- }
1891
- } else {
1892
- for (i = 0; i < n; i++) {
1893
- check_intdivzero(*(dtype*)p2);
1894
- *(dtype*)p3 = m_sub(*(dtype*)p1, *(dtype*)p2);
1895
- p1 += s1;
1896
- p2 += s2;
1897
- p3 += s3;
1898
- }
1899
- }
1900
- }
1901
-
1902
- return;
1903
- //
1904
- }
1905
- #undef check_intdivzero
1906
-
1907
- static VALUE uint8_sub_self(VALUE self, VALUE other) {
1908
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
1909
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
1910
- ndfunc_t ndf = { iter_uint8_sub, STRIDE_LOOP, 2, 1, ain, aout };
1911
-
1912
- return na_ndloop(&ndf, 2, self, other);
1913
- }
1914
-
1915
- /*
1916
- Binary sub.
1917
- @overload - other
1918
- @param [Numo::NArray,Numeric] other
1919
- @return [Numo::NArray] self - other
1920
- */
1921
- static VALUE uint8_sub(VALUE self, VALUE other) {
1922
-
1923
- VALUE klass, v;
1924
-
1925
- klass = na_upcast(rb_obj_class(self), rb_obj_class(other));
1926
- if (klass == cT) {
1927
- return uint8_sub_self(self, other);
1928
- } else {
1929
- v = rb_funcall(klass, id_cast, 1, self);
1930
- return rb_funcall(v, '-', 1, other);
1931
- }
1932
- }
1933
-
1934
- #define check_intdivzero(y) \
1935
- {}
1936
-
1937
- static void iter_uint8_mul(na_loop_t* const lp) {
1938
- size_t i = 0;
1939
- size_t n;
1940
- char *p1, *p2, *p3;
1941
- ssize_t s1, s2, s3;
1942
-
1943
- INIT_COUNTER(lp, n);
1944
- INIT_PTR(lp, 0, p1, s1);
1945
- INIT_PTR(lp, 1, p2, s2);
1946
- INIT_PTR(lp, 2, p3, s3);
1947
-
1948
- //
1949
-
1950
- if (s2 == 0) { // Broadcasting from scalar value.
1951
- check_intdivzero(*(dtype*)p2);
1952
- if (s1 == sizeof(dtype) && s3 == sizeof(dtype)) {
1953
- if (p1 == p3) { // inplace case
1954
- for (; i < n; i++) {
1955
- ((dtype*)p1)[i] = m_mul(((dtype*)p1)[i], *(dtype*)p2);
1956
- }
1957
- } else {
1958
- for (; i < n; i++) {
1959
- ((dtype*)p3)[i] = m_mul(((dtype*)p1)[i], *(dtype*)p2);
1960
- }
1961
- }
1962
- } else {
1963
- for (i = 0; i < n; i++) {
1964
- *(dtype*)p3 = m_mul(*(dtype*)p1, *(dtype*)p2);
1965
- p1 += s1;
1966
- p3 += s3;
1967
- }
1968
- }
1969
- } else {
1970
- if (p1 == p3) { // inplace case
1971
- for (i = 0; i < n; i++) {
1972
- check_intdivzero(*(dtype*)p2);
1973
- *(dtype*)p1 = m_mul(*(dtype*)p1, *(dtype*)p2);
1974
- p1 += s1;
1975
- p2 += s2;
1976
- }
1977
- } else {
1978
- for (i = 0; i < n; i++) {
1979
- check_intdivzero(*(dtype*)p2);
1980
- *(dtype*)p3 = m_mul(*(dtype*)p1, *(dtype*)p2);
1981
- p1 += s1;
1982
- p2 += s2;
1983
- p3 += s3;
1984
- }
1985
- }
1986
- }
1987
-
1988
- return;
1989
- //
1990
- }
1991
- #undef check_intdivzero
1992
-
1993
- static VALUE uint8_mul_self(VALUE self, VALUE other) {
1994
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
1995
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
1996
- ndfunc_t ndf = { iter_uint8_mul, STRIDE_LOOP, 2, 1, ain, aout };
1997
-
1998
- return na_ndloop(&ndf, 2, self, other);
1999
- }
2000
-
2001
- /*
2002
- Binary mul.
2003
- @overload * other
2004
- @param [Numo::NArray,Numeric] other
2005
- @return [Numo::NArray] self * other
2006
- */
2007
- static VALUE uint8_mul(VALUE self, VALUE other) {
2008
-
2009
- VALUE klass, v;
2010
-
2011
- klass = na_upcast(rb_obj_class(self), rb_obj_class(other));
2012
- if (klass == cT) {
2013
- return uint8_mul_self(self, other);
2014
- } else {
2015
- v = rb_funcall(klass, id_cast, 1, self);
2016
- return rb_funcall(v, '*', 1, other);
2017
- }
2018
- }
2019
-
2020
- #define check_intdivzero(y) \
2021
- if ((y) == 0) { \
2022
- lp->err_type = rb_eZeroDivError; \
2023
- return; \
2024
- }
2025
-
2026
- static void iter_uint8_div(na_loop_t* const lp) {
2027
- size_t i = 0;
2028
- size_t n;
2029
- char *p1, *p2, *p3;
2030
- ssize_t s1, s2, s3;
2031
-
2032
- INIT_COUNTER(lp, n);
2033
- INIT_PTR(lp, 0, p1, s1);
2034
- INIT_PTR(lp, 1, p2, s2);
2035
- INIT_PTR(lp, 2, p3, s3);
2036
-
2037
- //
2038
-
2039
- if (s2 == 0) { // Broadcasting from scalar value.
2040
- check_intdivzero(*(dtype*)p2);
2041
- if (s1 == sizeof(dtype) && s3 == sizeof(dtype)) {
2042
- if (p1 == p3) { // inplace case
2043
- for (; i < n; i++) {
2044
- ((dtype*)p1)[i] = m_div(((dtype*)p1)[i], *(dtype*)p2);
2045
- }
2046
- } else {
2047
- for (; i < n; i++) {
2048
- ((dtype*)p3)[i] = m_div(((dtype*)p1)[i], *(dtype*)p2);
2049
- }
2050
- }
2051
- } else {
2052
- for (i = 0; i < n; i++) {
2053
- *(dtype*)p3 = m_div(*(dtype*)p1, *(dtype*)p2);
2054
- p1 += s1;
2055
- p3 += s3;
2056
- }
2057
- }
2058
- } else {
2059
- if (p1 == p3) { // inplace case
2060
- for (i = 0; i < n; i++) {
2061
- check_intdivzero(*(dtype*)p2);
2062
- *(dtype*)p1 = m_div(*(dtype*)p1, *(dtype*)p2);
2063
- p1 += s1;
2064
- p2 += s2;
2065
- }
2066
- } else {
2067
- for (i = 0; i < n; i++) {
2068
- check_intdivzero(*(dtype*)p2);
2069
- *(dtype*)p3 = m_div(*(dtype*)p1, *(dtype*)p2);
2070
- p1 += s1;
2071
- p2 += s2;
2072
- p3 += s3;
2073
- }
2074
- }
2075
- }
2076
-
2077
- return;
2078
- //
2079
- }
2080
- #undef check_intdivzero
2081
-
2082
- static VALUE uint8_div_self(VALUE self, VALUE other) {
2083
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
2084
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
2085
- ndfunc_t ndf = { iter_uint8_div, STRIDE_LOOP, 2, 1, ain, aout };
2086
-
2087
- return na_ndloop(&ndf, 2, self, other);
2088
- }
2089
-
2090
- /*
2091
- Binary div.
2092
- @overload / other
2093
- @param [Numo::NArray,Numeric] other
2094
- @return [Numo::NArray] self / other
2095
- */
2096
- static VALUE uint8_div(VALUE self, VALUE other) {
2097
-
2098
- VALUE klass, v;
2099
-
2100
- klass = na_upcast(rb_obj_class(self), rb_obj_class(other));
2101
- if (klass == cT) {
2102
- return uint8_div_self(self, other);
2103
- } else {
2104
- v = rb_funcall(klass, id_cast, 1, self);
2105
- return rb_funcall(v, '/', 1, other);
2106
- }
2107
- }
2108
-
2109
- #define check_intdivzero(y) \
2110
- if ((y) == 0) { \
2111
- lp->err_type = rb_eZeroDivError; \
2112
- return; \
2113
- }
2114
-
2115
- static void iter_uint8_mod(na_loop_t* const lp) {
2116
- size_t i = 0;
2117
- size_t n;
2118
- char *p1, *p2, *p3;
2119
- ssize_t s1, s2, s3;
2120
-
2121
- INIT_COUNTER(lp, n);
2122
- INIT_PTR(lp, 0, p1, s1);
2123
- INIT_PTR(lp, 1, p2, s2);
2124
- INIT_PTR(lp, 2, p3, s3);
2125
-
2126
- //
2127
-
2128
- if (s2 == 0) { // Broadcasting from scalar value.
2129
- check_intdivzero(*(dtype*)p2);
2130
- if (s1 == sizeof(dtype) && s3 == sizeof(dtype)) {
2131
- if (p1 == p3) { // inplace case
2132
- for (; i < n; i++) {
2133
- ((dtype*)p1)[i] = m_mod(((dtype*)p1)[i], *(dtype*)p2);
2134
- }
2135
- } else {
2136
- for (; i < n; i++) {
2137
- ((dtype*)p3)[i] = m_mod(((dtype*)p1)[i], *(dtype*)p2);
2138
- }
2139
- }
2140
- } else {
2141
- for (i = 0; i < n; i++) {
2142
- *(dtype*)p3 = m_mod(*(dtype*)p1, *(dtype*)p2);
2143
- p1 += s1;
2144
- p3 += s3;
2145
- }
2146
- }
2147
- } else {
2148
- if (p1 == p3) { // inplace case
2149
- for (i = 0; i < n; i++) {
2150
- check_intdivzero(*(dtype*)p2);
2151
- *(dtype*)p1 = m_mod(*(dtype*)p1, *(dtype*)p2);
2152
- p1 += s1;
2153
- p2 += s2;
2154
- }
2155
- } else {
2156
- for (i = 0; i < n; i++) {
2157
- check_intdivzero(*(dtype*)p2);
2158
- *(dtype*)p3 = m_mod(*(dtype*)p1, *(dtype*)p2);
2159
- p1 += s1;
2160
- p2 += s2;
2161
- p3 += s3;
2162
- }
2163
- }
2164
- }
2165
-
2166
- return;
2167
- //
2168
- }
2169
- #undef check_intdivzero
2170
-
2171
- static VALUE uint8_mod_self(VALUE self, VALUE other) {
2172
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
2173
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
2174
- ndfunc_t ndf = { iter_uint8_mod, STRIDE_LOOP, 2, 1, ain, aout };
2175
-
2176
- return na_ndloop(&ndf, 2, self, other);
2177
- }
2178
-
2179
- /*
2180
- Binary mod.
2181
- @overload % other
2182
- @param [Numo::NArray,Numeric] other
2183
- @return [Numo::NArray] self % other
2184
- */
2185
- static VALUE uint8_mod(VALUE self, VALUE other) {
2186
-
2187
- VALUE klass, v;
2188
-
2189
- klass = na_upcast(rb_obj_class(self), rb_obj_class(other));
2190
- if (klass == cT) {
2191
- return uint8_mod_self(self, other);
2192
- } else {
2193
- v = rb_funcall(klass, id_cast, 1, self);
2194
- return rb_funcall(v, '%', 1, other);
2195
- }
2196
- }
2197
-
2198
- static void iter_uint8_divmod(na_loop_t* const lp) {
2199
- size_t i, n;
2200
- char *p1, *p2, *p3, *p4;
2201
- ssize_t s1, s2, s3, s4;
2202
- dtype x, y, a, b;
2203
- INIT_COUNTER(lp, n);
2204
- INIT_PTR(lp, 0, p1, s1);
2205
- INIT_PTR(lp, 1, p2, s2);
2206
- INIT_PTR(lp, 2, p3, s3);
2207
- INIT_PTR(lp, 3, p4, s4);
2208
- for (i = n; i--;) {
2209
- GET_DATA_STRIDE(p1, s1, dtype, x);
2210
- GET_DATA_STRIDE(p2, s2, dtype, y);
2211
- if (y == 0) {
2212
- lp->err_type = rb_eZeroDivError;
2213
- return;
2214
- }
2215
- m_divmod(x, y, a, b);
2216
- SET_DATA_STRIDE(p3, s3, dtype, a);
2217
- SET_DATA_STRIDE(p4, s4, dtype, b);
2218
- }
2219
- }
2220
-
2221
- static VALUE uint8_divmod_self(VALUE self, VALUE other) {
2222
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
2223
- ndfunc_arg_out_t aout[2] = { { cT, 0 }, { cT, 0 } };
2224
- ndfunc_t ndf = { iter_uint8_divmod, STRIDE_LOOP, 2, 2, ain, aout };
2225
-
2226
- return na_ndloop(&ndf, 2, self, other);
2227
- }
2228
-
2229
- /*
2230
- Binary divmod.
2231
- @overload divmod other
2232
- @param [Numo::NArray,Numeric] other
2233
- @return [Numo::NArray] divmod of self and other.
2234
- */
2235
- static VALUE uint8_divmod(VALUE self, VALUE other) {
2236
-
2237
- VALUE klass, v;
2238
- klass = na_upcast(rb_obj_class(self), rb_obj_class(other));
2239
- if (klass == cT) {
2240
- return uint8_divmod_self(self, other);
2241
- } else {
2242
- v = rb_funcall(klass, id_cast, 1, self);
2243
- return rb_funcall(v, id_divmod, 1, other);
2244
- }
2245
- }
2246
-
2247
- static void iter_uint8_pow(na_loop_t* const lp) {
2248
- size_t i;
2249
- char *p1, *p2, *p3;
2250
- ssize_t s1, s2, s3;
2251
- dtype x, y;
2252
- INIT_COUNTER(lp, i);
2253
- INIT_PTR(lp, 0, p1, s1);
2254
- INIT_PTR(lp, 1, p2, s2);
2255
- INIT_PTR(lp, 2, p3, s3);
2256
- for (; i--;) {
2257
- GET_DATA_STRIDE(p1, s1, dtype, x);
2258
- GET_DATA_STRIDE(p2, s2, dtype, y);
2259
- x = m_pow(x, y);
2260
- SET_DATA_STRIDE(p3, s3, dtype, x);
2261
- }
2262
- }
2263
-
2264
- static void iter_uint8_pow_int32(na_loop_t* const lp) {
2265
- size_t i;
2266
- char *p1, *p2, *p3;
2267
- ssize_t s1, s2, s3;
2268
- dtype x;
2269
- int32_t y;
2270
- INIT_COUNTER(lp, i);
2271
- INIT_PTR(lp, 0, p1, s1);
2272
- INIT_PTR(lp, 1, p2, s2);
2273
- INIT_PTR(lp, 2, p3, s3);
2274
- for (; i--;) {
2275
- GET_DATA_STRIDE(p1, s1, dtype, x);
2276
- GET_DATA_STRIDE(p2, s2, int32_t, y);
2277
- x = m_pow_int(x, y);
2278
- SET_DATA_STRIDE(p3, s3, dtype, x);
2279
- }
2280
- }
2281
-
2282
- static VALUE uint8_pow_self(VALUE self, VALUE other) {
2283
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
2284
- ndfunc_arg_in_t ain_i[2] = { { cT, 0 }, { numo_cInt32, 0 } };
2285
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
2286
- ndfunc_t ndf = { iter_uint8_pow, STRIDE_LOOP, 2, 1, ain, aout };
2287
- ndfunc_t ndf_i = { iter_uint8_pow_int32, STRIDE_LOOP, 2, 1, ain_i, aout };
2288
-
2289
- // fixme : use na.integer?
2290
- if (FIXNUM_P(other) || rb_obj_is_kind_of(other, numo_cInt32)) {
2291
- return na_ndloop(&ndf_i, 2, self, other);
2292
- } else {
2293
- return na_ndloop(&ndf, 2, self, other);
2294
- }
2295
- }
2296
-
2297
- /*
2298
- Binary power.
2299
- @overload ** other
2300
- @param [Numo::NArray,Numeric] other
2301
- @return [Numo::NArray] self to the other-th power.
2302
- */
2303
- static VALUE uint8_pow(VALUE self, VALUE other) {
2304
-
2305
- VALUE klass, v;
2306
- klass = na_upcast(rb_obj_class(self), rb_obj_class(other));
2307
- if (klass == cT) {
2308
- return uint8_pow_self(self, other);
2309
- } else {
2310
- v = rb_funcall(klass, id_cast, 1, self);
2311
- return rb_funcall(v, id_pow, 1, other);
2312
- }
2313
- }
2314
-
2315
- static void iter_uint8_minus(na_loop_t* const lp) {
2316
- size_t i, n;
2317
- char *p1, *p2;
2318
- ssize_t s1, s2;
2319
- size_t *idx1, *idx2;
2320
- dtype x;
2321
-
2322
- INIT_COUNTER(lp, n);
2323
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
2324
- INIT_PTR_IDX(lp, 1, p2, s2, idx2);
2325
-
2326
- if (idx1) {
2327
- if (idx2) {
2328
- for (i = 0; i < n; i++) {
2329
- GET_DATA_INDEX(p1, idx1, dtype, x);
2330
- x = m_minus(x);
2331
- SET_DATA_INDEX(p2, idx2, dtype, x);
2332
- }
2333
- } else {
2334
- for (i = 0; i < n; i++) {
2335
- GET_DATA_INDEX(p1, idx1, dtype, x);
2336
- x = m_minus(x);
2337
- SET_DATA_STRIDE(p2, s2, dtype, x);
2338
- }
2339
- }
2340
- } else {
2341
- if (idx2) {
2342
- for (i = 0; i < n; i++) {
2343
- GET_DATA_STRIDE(p1, s1, dtype, x);
2344
- x = m_minus(x);
2345
- SET_DATA_INDEX(p2, idx2, dtype, x);
2346
- }
2347
- } else {
2348
- //
2349
- for (i = 0; i < n; i++) {
2350
- *(dtype*)p2 = m_minus(*(dtype*)p1);
2351
- p1 += s1;
2352
- p2 += s2;
2353
- }
2354
- return;
2355
- //
2356
- }
2357
- }
2358
- }
2359
-
2360
- /*
2361
- Unary minus.
2362
- @overload -@
2363
- @return [Numo::UInt8] minus of self.
2364
- */
2365
- static VALUE uint8_minus(VALUE self) {
2366
- ndfunc_arg_in_t ain[1] = { { cT, 0 } };
2367
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
2368
- ndfunc_t ndf = { iter_uint8_minus, FULL_LOOP, 1, 1, ain, aout };
2369
-
2370
- return na_ndloop(&ndf, 1, self);
2371
- }
2372
-
2373
- static void iter_uint8_reciprocal(na_loop_t* const lp) {
2374
- size_t i, n;
2375
- char *p1, *p2;
2376
- ssize_t s1, s2;
2377
- size_t *idx1, *idx2;
2378
- dtype x;
2379
-
2380
- INIT_COUNTER(lp, n);
2381
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
2382
- INIT_PTR_IDX(lp, 1, p2, s2, idx2);
2383
-
2384
- if (idx1) {
2385
- if (idx2) {
2386
- for (i = 0; i < n; i++) {
2387
- GET_DATA_INDEX(p1, idx1, dtype, x);
2388
- x = m_reciprocal(x);
2389
- SET_DATA_INDEX(p2, idx2, dtype, x);
2390
- }
2391
- } else {
2392
- for (i = 0; i < n; i++) {
2393
- GET_DATA_INDEX(p1, idx1, dtype, x);
2394
- x = m_reciprocal(x);
2395
- SET_DATA_STRIDE(p2, s2, dtype, x);
2396
- }
2397
- }
2398
- } else {
2399
- if (idx2) {
2400
- for (i = 0; i < n; i++) {
2401
- GET_DATA_STRIDE(p1, s1, dtype, x);
2402
- x = m_reciprocal(x);
2403
- SET_DATA_INDEX(p2, idx2, dtype, x);
2404
- }
2405
- } else {
2406
- //
2407
- for (i = 0; i < n; i++) {
2408
- *(dtype*)p2 = m_reciprocal(*(dtype*)p1);
2409
- p1 += s1;
2410
- p2 += s2;
2411
- }
2412
- return;
2413
- //
2414
- }
2415
- }
2416
- }
2417
-
2418
- /*
2419
- Unary reciprocal.
2420
- @overload reciprocal
2421
- @return [Numo::UInt8] reciprocal of self.
2422
- */
2423
- static VALUE uint8_reciprocal(VALUE self) {
2424
- ndfunc_arg_in_t ain[1] = { { cT, 0 } };
2425
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
2426
- ndfunc_t ndf = { iter_uint8_reciprocal, FULL_LOOP, 1, 1, ain, aout };
2427
-
2428
- return na_ndloop(&ndf, 1, self);
2429
- }
2430
-
2431
- static void iter_uint8_sign(na_loop_t* const lp) {
2432
- size_t i, n;
2433
- char *p1, *p2;
2434
- ssize_t s1, s2;
2435
- size_t *idx1, *idx2;
2436
- dtype x;
2437
-
2438
- INIT_COUNTER(lp, n);
2439
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
2440
- INIT_PTR_IDX(lp, 1, p2, s2, idx2);
2441
-
2442
- if (idx1) {
2443
- if (idx2) {
2444
- for (i = 0; i < n; i++) {
2445
- GET_DATA_INDEX(p1, idx1, dtype, x);
2446
- x = m_sign(x);
2447
- SET_DATA_INDEX(p2, idx2, dtype, x);
2448
- }
2449
- } else {
2450
- for (i = 0; i < n; i++) {
2451
- GET_DATA_INDEX(p1, idx1, dtype, x);
2452
- x = m_sign(x);
2453
- SET_DATA_STRIDE(p2, s2, dtype, x);
2454
- }
2455
- }
2456
- } else {
2457
- if (idx2) {
2458
- for (i = 0; i < n; i++) {
2459
- GET_DATA_STRIDE(p1, s1, dtype, x);
2460
- x = m_sign(x);
2461
- SET_DATA_INDEX(p2, idx2, dtype, x);
2462
- }
2463
- } else {
2464
- //
2465
- for (i = 0; i < n; i++) {
2466
- *(dtype*)p2 = m_sign(*(dtype*)p1);
2467
- p1 += s1;
2468
- p2 += s2;
2469
- }
2470
- return;
2471
- //
2472
- }
2473
- }
2474
- }
2475
-
2476
- /*
2477
- Unary sign.
2478
- @overload sign
2479
- @return [Numo::UInt8] sign of self.
2480
- */
2481
- static VALUE uint8_sign(VALUE self) {
2482
- ndfunc_arg_in_t ain[1] = { { cT, 0 } };
2483
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
2484
- ndfunc_t ndf = { iter_uint8_sign, FULL_LOOP, 1, 1, ain, aout };
2485
-
2486
- return na_ndloop(&ndf, 1, self);
2487
- }
2488
-
2489
- static void iter_uint8_square(na_loop_t* const lp) {
2490
- size_t i, n;
2491
- char *p1, *p2;
2492
- ssize_t s1, s2;
2493
- size_t *idx1, *idx2;
2494
- dtype x;
2495
-
2496
- INIT_COUNTER(lp, n);
2497
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
2498
- INIT_PTR_IDX(lp, 1, p2, s2, idx2);
2499
-
2500
- if (idx1) {
2501
- if (idx2) {
2502
- for (i = 0; i < n; i++) {
2503
- GET_DATA_INDEX(p1, idx1, dtype, x);
2504
- x = m_square(x);
2505
- SET_DATA_INDEX(p2, idx2, dtype, x);
2506
- }
2507
- } else {
2508
- for (i = 0; i < n; i++) {
2509
- GET_DATA_INDEX(p1, idx1, dtype, x);
2510
- x = m_square(x);
2511
- SET_DATA_STRIDE(p2, s2, dtype, x);
2512
- }
2513
- }
2514
- } else {
2515
- if (idx2) {
2516
- for (i = 0; i < n; i++) {
2517
- GET_DATA_STRIDE(p1, s1, dtype, x);
2518
- x = m_square(x);
2519
- SET_DATA_INDEX(p2, idx2, dtype, x);
2520
- }
2521
- } else {
2522
- //
2523
- for (i = 0; i < n; i++) {
2524
- *(dtype*)p2 = m_square(*(dtype*)p1);
2525
- p1 += s1;
2526
- p2 += s2;
2527
- }
2528
- return;
2529
- //
2530
- }
2531
- }
2532
- }
2533
-
2534
- /*
2535
- Unary square.
2536
- @overload square
2537
- @return [Numo::UInt8] square of self.
2538
- */
2539
- static VALUE uint8_square(VALUE self) {
2540
- ndfunc_arg_in_t ain[1] = { { cT, 0 } };
2541
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
2542
- ndfunc_t ndf = { iter_uint8_square, FULL_LOOP, 1, 1, ain, aout };
2543
-
2544
- return na_ndloop(&ndf, 1, self);
2545
- }
2546
-
2547
- static void iter_uint8_eq(na_loop_t* const lp) {
2548
- size_t i;
2549
- char *p1, *p2;
2550
- BIT_DIGIT* a3;
2551
- size_t p3;
2552
- ssize_t s1, s2, s3;
2553
- dtype x, y;
2554
- BIT_DIGIT b;
2555
- INIT_COUNTER(lp, i);
2556
- INIT_PTR(lp, 0, p1, s1);
2557
- INIT_PTR(lp, 1, p2, s2);
2558
- INIT_PTR_BIT(lp, 2, a3, p3, s3);
2559
- for (; i--;) {
2560
- GET_DATA_STRIDE(p1, s1, dtype, x);
2561
- GET_DATA_STRIDE(p2, s2, dtype, y);
2562
- b = (m_eq(x, y)) ? 1 : 0;
2563
- STORE_BIT(a3, p3, b);
2564
- p3 += s3;
2565
- }
2566
- }
2567
-
2568
- static VALUE uint8_eq_self(VALUE self, VALUE other) {
2569
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
2570
- ndfunc_arg_out_t aout[1] = { { numo_cBit, 0 } };
2571
- ndfunc_t ndf = { iter_uint8_eq, STRIDE_LOOP, 2, 1, ain, aout };
2572
-
2573
- return na_ndloop(&ndf, 2, self, other);
2574
- }
2575
-
2576
- /*
2577
- Comparison eq other.
2578
- @overload eq other
2579
- @param [Numo::NArray,Numeric] other
2580
- @return [Numo::Bit] result of self eq other.
2581
- */
2582
- static VALUE uint8_eq(VALUE self, VALUE other) {
2583
-
2584
- VALUE klass, v;
2585
- klass = na_upcast(rb_obj_class(self), rb_obj_class(other));
2586
- if (klass == cT) {
2587
- return uint8_eq_self(self, other);
2588
- } else {
2589
- v = rb_funcall(klass, id_cast, 1, self);
2590
- return rb_funcall(v, id_eq, 1, other);
2591
- }
2592
- }
2593
-
2594
- static void iter_uint8_ne(na_loop_t* const lp) {
2595
- size_t i;
2596
- char *p1, *p2;
2597
- BIT_DIGIT* a3;
2598
- size_t p3;
2599
- ssize_t s1, s2, s3;
2600
- dtype x, y;
2601
- BIT_DIGIT b;
2602
- INIT_COUNTER(lp, i);
2603
- INIT_PTR(lp, 0, p1, s1);
2604
- INIT_PTR(lp, 1, p2, s2);
2605
- INIT_PTR_BIT(lp, 2, a3, p3, s3);
2606
- for (; i--;) {
2607
- GET_DATA_STRIDE(p1, s1, dtype, x);
2608
- GET_DATA_STRIDE(p2, s2, dtype, y);
2609
- b = (m_ne(x, y)) ? 1 : 0;
2610
- STORE_BIT(a3, p3, b);
2611
- p3 += s3;
2612
- }
2613
- }
2614
-
2615
- static VALUE uint8_ne_self(VALUE self, VALUE other) {
2616
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
2617
- ndfunc_arg_out_t aout[1] = { { numo_cBit, 0 } };
2618
- ndfunc_t ndf = { iter_uint8_ne, STRIDE_LOOP, 2, 1, ain, aout };
2619
-
2620
- return na_ndloop(&ndf, 2, self, other);
2621
- }
2622
-
2623
- /*
2624
- Comparison ne other.
2625
- @overload ne other
2626
- @param [Numo::NArray,Numeric] other
2627
- @return [Numo::Bit] result of self ne other.
2628
- */
2629
- static VALUE uint8_ne(VALUE self, VALUE other) {
2630
-
2631
- VALUE klass, v;
2632
- klass = na_upcast(rb_obj_class(self), rb_obj_class(other));
2633
- if (klass == cT) {
2634
- return uint8_ne_self(self, other);
2635
- } else {
2636
- v = rb_funcall(klass, id_cast, 1, self);
2637
- return rb_funcall(v, id_ne, 1, other);
2638
- }
2639
- }
2640
-
2641
- #define check_intdivzero(y) \
2642
- {}
2643
-
2644
- static void iter_uint8_bit_and(na_loop_t* const lp) {
2645
- size_t i = 0;
2646
- size_t n;
2647
- char *p1, *p2, *p3;
2648
- ssize_t s1, s2, s3;
2649
-
2650
- INIT_COUNTER(lp, n);
2651
- INIT_PTR(lp, 0, p1, s1);
2652
- INIT_PTR(lp, 1, p2, s2);
2653
- INIT_PTR(lp, 2, p3, s3);
2654
-
2655
- //
2656
-
2657
- if (s2 == 0) { // Broadcasting from scalar value.
2658
- check_intdivzero(*(dtype*)p2);
2659
- if (s1 == sizeof(dtype) && s3 == sizeof(dtype)) {
2660
- if (p1 == p3) { // inplace case
2661
- for (; i < n; i++) {
2662
- ((dtype*)p1)[i] = m_bit_and(((dtype*)p1)[i], *(dtype*)p2);
2663
- }
2664
- } else {
2665
- for (; i < n; i++) {
2666
- ((dtype*)p3)[i] = m_bit_and(((dtype*)p1)[i], *(dtype*)p2);
2667
- }
2668
- }
2669
- } else {
2670
- for (i = 0; i < n; i++) {
2671
- *(dtype*)p3 = m_bit_and(*(dtype*)p1, *(dtype*)p2);
2672
- p1 += s1;
2673
- p3 += s3;
2674
- }
2675
- }
2676
- } else {
2677
- if (p1 == p3) { // inplace case
2678
- for (i = 0; i < n; i++) {
2679
- check_intdivzero(*(dtype*)p2);
2680
- *(dtype*)p1 = m_bit_and(*(dtype*)p1, *(dtype*)p2);
2681
- p1 += s1;
2682
- p2 += s2;
2683
- }
2684
- } else {
2685
- for (i = 0; i < n; i++) {
2686
- check_intdivzero(*(dtype*)p2);
2687
- *(dtype*)p3 = m_bit_and(*(dtype*)p1, *(dtype*)p2);
2688
- p1 += s1;
2689
- p2 += s2;
2690
- p3 += s3;
2691
- }
2692
- }
2693
- }
2694
-
2695
- return;
2696
- //
2697
- }
2698
- #undef check_intdivzero
2699
-
2700
- static VALUE uint8_bit_and_self(VALUE self, VALUE other) {
2701
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
2702
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
2703
- ndfunc_t ndf = { iter_uint8_bit_and, STRIDE_LOOP, 2, 1, ain, aout };
2704
-
2705
- return na_ndloop(&ndf, 2, self, other);
2706
- }
2707
-
2708
- /*
2709
- Binary bit_and.
2710
- @overload & other
2711
- @param [Numo::NArray,Numeric] other
2712
- @return [Numo::NArray] self & other
2713
- */
2714
- static VALUE uint8_bit_and(VALUE self, VALUE other) {
2715
-
2716
- VALUE klass, v;
2717
-
2718
- klass = na_upcast(rb_obj_class(self), rb_obj_class(other));
2719
- if (klass == cT) {
2720
- return uint8_bit_and_self(self, other);
2721
- } else {
2722
- v = rb_funcall(klass, id_cast, 1, self);
2723
- return rb_funcall(v, '&', 1, other);
2724
- }
2725
- }
2726
-
2727
- #define check_intdivzero(y) \
2728
- {}
2729
-
2730
- static void iter_uint8_bit_or(na_loop_t* const lp) {
2731
- size_t i = 0;
2732
- size_t n;
2733
- char *p1, *p2, *p3;
2734
- ssize_t s1, s2, s3;
2735
-
2736
- INIT_COUNTER(lp, n);
2737
- INIT_PTR(lp, 0, p1, s1);
2738
- INIT_PTR(lp, 1, p2, s2);
2739
- INIT_PTR(lp, 2, p3, s3);
2740
-
2741
- //
2742
-
2743
- if (s2 == 0) { // Broadcasting from scalar value.
2744
- check_intdivzero(*(dtype*)p2);
2745
- if (s1 == sizeof(dtype) && s3 == sizeof(dtype)) {
2746
- if (p1 == p3) { // inplace case
2747
- for (; i < n; i++) {
2748
- ((dtype*)p1)[i] = m_bit_or(((dtype*)p1)[i], *(dtype*)p2);
2749
- }
2750
- } else {
2751
- for (; i < n; i++) {
2752
- ((dtype*)p3)[i] = m_bit_or(((dtype*)p1)[i], *(dtype*)p2);
2753
- }
2754
- }
2755
- } else {
2756
- for (i = 0; i < n; i++) {
2757
- *(dtype*)p3 = m_bit_or(*(dtype*)p1, *(dtype*)p2);
2758
- p1 += s1;
2759
- p3 += s3;
2760
- }
2761
- }
2762
- } else {
2763
- if (p1 == p3) { // inplace case
2764
- for (i = 0; i < n; i++) {
2765
- check_intdivzero(*(dtype*)p2);
2766
- *(dtype*)p1 = m_bit_or(*(dtype*)p1, *(dtype*)p2);
2767
- p1 += s1;
2768
- p2 += s2;
2769
- }
2770
- } else {
2771
- for (i = 0; i < n; i++) {
2772
- check_intdivzero(*(dtype*)p2);
2773
- *(dtype*)p3 = m_bit_or(*(dtype*)p1, *(dtype*)p2);
2774
- p1 += s1;
2775
- p2 += s2;
2776
- p3 += s3;
2777
- }
2778
- }
2779
- }
2780
-
2781
- return;
2782
- //
2783
- }
2784
- #undef check_intdivzero
2785
-
2786
- static VALUE uint8_bit_or_self(VALUE self, VALUE other) {
2787
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
2788
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
2789
- ndfunc_t ndf = { iter_uint8_bit_or, STRIDE_LOOP, 2, 1, ain, aout };
2790
-
2791
- return na_ndloop(&ndf, 2, self, other);
2792
- }
2793
-
2794
- /*
2795
- Binary bit_or.
2796
- @overload | other
2797
- @param [Numo::NArray,Numeric] other
2798
- @return [Numo::NArray] self | other
2799
- */
2800
- static VALUE uint8_bit_or(VALUE self, VALUE other) {
2801
-
2802
- VALUE klass, v;
2803
-
2804
- klass = na_upcast(rb_obj_class(self), rb_obj_class(other));
2805
- if (klass == cT) {
2806
- return uint8_bit_or_self(self, other);
2807
- } else {
2808
- v = rb_funcall(klass, id_cast, 1, self);
2809
- return rb_funcall(v, '|', 1, other);
2810
- }
2811
- }
2812
-
2813
- #define check_intdivzero(y) \
2814
- {}
2815
-
2816
- static void iter_uint8_bit_xor(na_loop_t* const lp) {
2817
- size_t i = 0;
2818
- size_t n;
2819
- char *p1, *p2, *p3;
2820
- ssize_t s1, s2, s3;
2821
-
2822
- INIT_COUNTER(lp, n);
2823
- INIT_PTR(lp, 0, p1, s1);
2824
- INIT_PTR(lp, 1, p2, s2);
2825
- INIT_PTR(lp, 2, p3, s3);
2826
-
2827
- //
2828
-
2829
- if (s2 == 0) { // Broadcasting from scalar value.
2830
- check_intdivzero(*(dtype*)p2);
2831
- if (s1 == sizeof(dtype) && s3 == sizeof(dtype)) {
2832
- if (p1 == p3) { // inplace case
2833
- for (; i < n; i++) {
2834
- ((dtype*)p1)[i] = m_bit_xor(((dtype*)p1)[i], *(dtype*)p2);
2835
- }
2836
- } else {
2837
- for (; i < n; i++) {
2838
- ((dtype*)p3)[i] = m_bit_xor(((dtype*)p1)[i], *(dtype*)p2);
2839
- }
2840
- }
2841
- } else {
2842
- for (i = 0; i < n; i++) {
2843
- *(dtype*)p3 = m_bit_xor(*(dtype*)p1, *(dtype*)p2);
2844
- p1 += s1;
2845
- p3 += s3;
2846
- }
2847
- }
2848
- } else {
2849
- if (p1 == p3) { // inplace case
2850
- for (i = 0; i < n; i++) {
2851
- check_intdivzero(*(dtype*)p2);
2852
- *(dtype*)p1 = m_bit_xor(*(dtype*)p1, *(dtype*)p2);
2853
- p1 += s1;
2854
- p2 += s2;
2855
- }
2856
- } else {
2857
- for (i = 0; i < n; i++) {
2858
- check_intdivzero(*(dtype*)p2);
2859
- *(dtype*)p3 = m_bit_xor(*(dtype*)p1, *(dtype*)p2);
2860
- p1 += s1;
2861
- p2 += s2;
2862
- p3 += s3;
2863
- }
2864
- }
2865
- }
2866
-
2867
- return;
2868
- //
2869
- }
2870
- #undef check_intdivzero
2871
-
2872
- static VALUE uint8_bit_xor_self(VALUE self, VALUE other) {
2873
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
2874
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
2875
- ndfunc_t ndf = { iter_uint8_bit_xor, STRIDE_LOOP, 2, 1, ain, aout };
2876
-
2877
- return na_ndloop(&ndf, 2, self, other);
2878
- }
2879
-
2880
- /*
2881
- Binary bit_xor.
2882
- @overload ^ other
2883
- @param [Numo::NArray,Numeric] other
2884
- @return [Numo::NArray] self ^ other
2885
- */
2886
- static VALUE uint8_bit_xor(VALUE self, VALUE other) {
2887
-
2888
- VALUE klass, v;
2889
-
2890
- klass = na_upcast(rb_obj_class(self), rb_obj_class(other));
2891
- if (klass == cT) {
2892
- return uint8_bit_xor_self(self, other);
2893
- } else {
2894
- v = rb_funcall(klass, id_cast, 1, self);
2895
- return rb_funcall(v, '^', 1, other);
2896
- }
2897
- }
2898
-
2899
- static void iter_uint8_bit_not(na_loop_t* const lp) {
2900
- size_t i, n;
2901
- char *p1, *p2;
2902
- ssize_t s1, s2;
2903
- size_t *idx1, *idx2;
2904
- dtype x;
2905
-
2906
- INIT_COUNTER(lp, n);
2907
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
2908
- INIT_PTR_IDX(lp, 1, p2, s2, idx2);
2909
-
2910
- if (idx1) {
2911
- if (idx2) {
2912
- for (i = 0; i < n; i++) {
2913
- GET_DATA_INDEX(p1, idx1, dtype, x);
2914
- x = m_bit_not(x);
2915
- SET_DATA_INDEX(p2, idx2, dtype, x);
2916
- }
2917
- } else {
2918
- for (i = 0; i < n; i++) {
2919
- GET_DATA_INDEX(p1, idx1, dtype, x);
2920
- x = m_bit_not(x);
2921
- SET_DATA_STRIDE(p2, s2, dtype, x);
2922
- }
2923
- }
2924
- } else {
2925
- if (idx2) {
2926
- for (i = 0; i < n; i++) {
2927
- GET_DATA_STRIDE(p1, s1, dtype, x);
2928
- x = m_bit_not(x);
2929
- SET_DATA_INDEX(p2, idx2, dtype, x);
2930
- }
2931
- } else {
2932
- //
2933
- for (i = 0; i < n; i++) {
2934
- *(dtype*)p2 = m_bit_not(*(dtype*)p1);
2935
- p1 += s1;
2936
- p2 += s2;
2937
- }
2938
- return;
2939
- //
2940
- }
2941
- }
2942
- }
2943
-
2944
- /*
2945
- Unary bit_not.
2946
- @overload ~
2947
- @return [Numo::UInt8] bit_not of self.
2948
- */
2949
- static VALUE uint8_bit_not(VALUE self) {
2950
- ndfunc_arg_in_t ain[1] = { { cT, 0 } };
2951
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
2952
- ndfunc_t ndf = { iter_uint8_bit_not, FULL_LOOP, 1, 1, ain, aout };
2953
-
2954
- return na_ndloop(&ndf, 1, self);
2955
- }
2956
-
2957
- #define check_intdivzero(y) \
2958
- {}
2959
-
2960
- static void iter_uint8_left_shift(na_loop_t* const lp) {
2961
- size_t i = 0;
2962
- size_t n;
2963
- char *p1, *p2, *p3;
2964
- ssize_t s1, s2, s3;
2965
-
2966
- INIT_COUNTER(lp, n);
2967
- INIT_PTR(lp, 0, p1, s1);
2968
- INIT_PTR(lp, 1, p2, s2);
2969
- INIT_PTR(lp, 2, p3, s3);
2970
-
2971
- //
2972
-
2973
- if (s2 == 0) { // Broadcasting from scalar value.
2974
- check_intdivzero(*(dtype*)p2);
2975
- if (s1 == sizeof(dtype) && s3 == sizeof(dtype)) {
2976
- if (p1 == p3) { // inplace case
2977
- for (; i < n; i++) {
2978
- ((dtype*)p1)[i] = m_left_shift(((dtype*)p1)[i], *(dtype*)p2);
2979
- }
2980
- } else {
2981
- for (; i < n; i++) {
2982
- ((dtype*)p3)[i] = m_left_shift(((dtype*)p1)[i], *(dtype*)p2);
2983
- }
2984
- }
2985
- } else {
2986
- for (i = 0; i < n; i++) {
2987
- *(dtype*)p3 = m_left_shift(*(dtype*)p1, *(dtype*)p2);
2988
- p1 += s1;
2989
- p3 += s3;
2990
- }
2991
- }
2992
- } else {
2993
- if (p1 == p3) { // inplace case
2994
- for (i = 0; i < n; i++) {
2995
- check_intdivzero(*(dtype*)p2);
2996
- *(dtype*)p1 = m_left_shift(*(dtype*)p1, *(dtype*)p2);
2997
- p1 += s1;
2998
- p2 += s2;
2999
- }
3000
- } else {
3001
- for (i = 0; i < n; i++) {
3002
- check_intdivzero(*(dtype*)p2);
3003
- *(dtype*)p3 = m_left_shift(*(dtype*)p1, *(dtype*)p2);
3004
- p1 += s1;
3005
- p2 += s2;
3006
- p3 += s3;
3007
- }
3008
- }
3009
- }
3010
-
3011
- return;
3012
- //
3013
- }
3014
- #undef check_intdivzero
3015
-
3016
- static VALUE uint8_left_shift_self(VALUE self, VALUE other) {
3017
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
3018
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
3019
- ndfunc_t ndf = { iter_uint8_left_shift, STRIDE_LOOP, 2, 1, ain, aout };
3020
-
3021
- return na_ndloop(&ndf, 2, self, other);
3022
- }
3023
-
3024
- /*
3025
- Binary left_shift.
3026
- @overload << other
3027
- @param [Numo::NArray,Numeric] other
3028
- @return [Numo::NArray] self << other
3029
- */
3030
- static VALUE uint8_left_shift(VALUE self, VALUE other) {
3031
-
3032
- VALUE klass, v;
3033
-
3034
- klass = na_upcast(rb_obj_class(self), rb_obj_class(other));
3035
- if (klass == cT) {
3036
- return uint8_left_shift_self(self, other);
3037
- } else {
3038
- v = rb_funcall(klass, id_cast, 1, self);
3039
- return rb_funcall(v, id_left_shift, 1, other);
3040
- }
3041
- }
3042
-
3043
- #define check_intdivzero(y) \
3044
- {}
3045
-
3046
- static void iter_uint8_right_shift(na_loop_t* const lp) {
3047
- size_t i = 0;
3048
- size_t n;
3049
- char *p1, *p2, *p3;
3050
- ssize_t s1, s2, s3;
3051
-
3052
- INIT_COUNTER(lp, n);
3053
- INIT_PTR(lp, 0, p1, s1);
3054
- INIT_PTR(lp, 1, p2, s2);
3055
- INIT_PTR(lp, 2, p3, s3);
3056
-
3057
- //
3058
-
3059
- if (s2 == 0) { // Broadcasting from scalar value.
3060
- check_intdivzero(*(dtype*)p2);
3061
- if (s1 == sizeof(dtype) && s3 == sizeof(dtype)) {
3062
- if (p1 == p3) { // inplace case
3063
- for (; i < n; i++) {
3064
- ((dtype*)p1)[i] = m_right_shift(((dtype*)p1)[i], *(dtype*)p2);
3065
- }
3066
- } else {
3067
- for (; i < n; i++) {
3068
- ((dtype*)p3)[i] = m_right_shift(((dtype*)p1)[i], *(dtype*)p2);
3069
- }
3070
- }
3071
- } else {
3072
- for (i = 0; i < n; i++) {
3073
- *(dtype*)p3 = m_right_shift(*(dtype*)p1, *(dtype*)p2);
3074
- p1 += s1;
3075
- p3 += s3;
3076
- }
3077
- }
3078
- } else {
3079
- if (p1 == p3) { // inplace case
3080
- for (i = 0; i < n; i++) {
3081
- check_intdivzero(*(dtype*)p2);
3082
- *(dtype*)p1 = m_right_shift(*(dtype*)p1, *(dtype*)p2);
3083
- p1 += s1;
3084
- p2 += s2;
3085
- }
3086
- } else {
3087
- for (i = 0; i < n; i++) {
3088
- check_intdivzero(*(dtype*)p2);
3089
- *(dtype*)p3 = m_right_shift(*(dtype*)p1, *(dtype*)p2);
3090
- p1 += s1;
3091
- p2 += s2;
3092
- p3 += s3;
3093
- }
3094
- }
3095
- }
3096
-
3097
- return;
3098
- //
3099
- }
3100
- #undef check_intdivzero
3101
-
3102
- static VALUE uint8_right_shift_self(VALUE self, VALUE other) {
3103
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
3104
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
3105
- ndfunc_t ndf = { iter_uint8_right_shift, STRIDE_LOOP, 2, 1, ain, aout };
3106
-
3107
- return na_ndloop(&ndf, 2, self, other);
3108
- }
3109
-
3110
- /*
3111
- Binary right_shift.
3112
- @overload >> other
3113
- @param [Numo::NArray,Numeric] other
3114
- @return [Numo::NArray] self >> other
3115
- */
3116
- static VALUE uint8_right_shift(VALUE self, VALUE other) {
3117
-
3118
- VALUE klass, v;
3119
-
3120
- klass = na_upcast(rb_obj_class(self), rb_obj_class(other));
3121
- if (klass == cT) {
3122
- return uint8_right_shift_self(self, other);
3123
- } else {
3124
- v = rb_funcall(klass, id_cast, 1, self);
3125
- return rb_funcall(v, id_right_shift, 1, other);
3126
- }
3127
- }
3128
-
3129
- static void iter_uint8_gt(na_loop_t* const lp) {
3130
- size_t i;
3131
- char *p1, *p2;
3132
- BIT_DIGIT* a3;
3133
- size_t p3;
3134
- ssize_t s1, s2, s3;
3135
- dtype x, y;
3136
- BIT_DIGIT b;
3137
- INIT_COUNTER(lp, i);
3138
- INIT_PTR(lp, 0, p1, s1);
3139
- INIT_PTR(lp, 1, p2, s2);
3140
- INIT_PTR_BIT(lp, 2, a3, p3, s3);
3141
- for (; i--;) {
3142
- GET_DATA_STRIDE(p1, s1, dtype, x);
3143
- GET_DATA_STRIDE(p2, s2, dtype, y);
3144
- b = (m_gt(x, y)) ? 1 : 0;
3145
- STORE_BIT(a3, p3, b);
3146
- p3 += s3;
3147
- }
3148
- }
3149
-
3150
- static VALUE uint8_gt_self(VALUE self, VALUE other) {
3151
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
3152
- ndfunc_arg_out_t aout[1] = { { numo_cBit, 0 } };
3153
- ndfunc_t ndf = { iter_uint8_gt, STRIDE_LOOP, 2, 1, ain, aout };
3154
-
3155
- return na_ndloop(&ndf, 2, self, other);
3156
- }
3157
-
3158
- /*
3159
- Comparison gt other.
3160
- @overload gt other
3161
- @param [Numo::NArray,Numeric] other
3162
- @return [Numo::Bit] result of self gt other.
3163
- */
3164
- static VALUE uint8_gt(VALUE self, VALUE other) {
3165
-
3166
- VALUE klass, v;
3167
- klass = na_upcast(rb_obj_class(self), rb_obj_class(other));
3168
- if (klass == cT) {
3169
- return uint8_gt_self(self, other);
3170
- } else {
3171
- v = rb_funcall(klass, id_cast, 1, self);
3172
- return rb_funcall(v, id_gt, 1, other);
3173
- }
3174
- }
3175
-
3176
- static void iter_uint8_ge(na_loop_t* const lp) {
3177
- size_t i;
3178
- char *p1, *p2;
3179
- BIT_DIGIT* a3;
3180
- size_t p3;
3181
- ssize_t s1, s2, s3;
3182
- dtype x, y;
3183
- BIT_DIGIT b;
3184
- INIT_COUNTER(lp, i);
3185
- INIT_PTR(lp, 0, p1, s1);
3186
- INIT_PTR(lp, 1, p2, s2);
3187
- INIT_PTR_BIT(lp, 2, a3, p3, s3);
3188
- for (; i--;) {
3189
- GET_DATA_STRIDE(p1, s1, dtype, x);
3190
- GET_DATA_STRIDE(p2, s2, dtype, y);
3191
- b = (m_ge(x, y)) ? 1 : 0;
3192
- STORE_BIT(a3, p3, b);
3193
- p3 += s3;
3194
- }
3195
- }
3196
-
3197
- static VALUE uint8_ge_self(VALUE self, VALUE other) {
3198
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
3199
- ndfunc_arg_out_t aout[1] = { { numo_cBit, 0 } };
3200
- ndfunc_t ndf = { iter_uint8_ge, STRIDE_LOOP, 2, 1, ain, aout };
3201
-
3202
- return na_ndloop(&ndf, 2, self, other);
3203
- }
3204
-
3205
- /*
3206
- Comparison ge other.
3207
- @overload ge other
3208
- @param [Numo::NArray,Numeric] other
3209
- @return [Numo::Bit] result of self ge other.
3210
- */
3211
- static VALUE uint8_ge(VALUE self, VALUE other) {
3212
-
3213
- VALUE klass, v;
3214
- klass = na_upcast(rb_obj_class(self), rb_obj_class(other));
3215
- if (klass == cT) {
3216
- return uint8_ge_self(self, other);
3217
- } else {
3218
- v = rb_funcall(klass, id_cast, 1, self);
3219
- return rb_funcall(v, id_ge, 1, other);
3220
- }
3221
- }
3222
-
3223
- static void iter_uint8_lt(na_loop_t* const lp) {
3224
- size_t i;
3225
- char *p1, *p2;
3226
- BIT_DIGIT* a3;
3227
- size_t p3;
3228
- ssize_t s1, s2, s3;
3229
- dtype x, y;
3230
- BIT_DIGIT b;
3231
- INIT_COUNTER(lp, i);
3232
- INIT_PTR(lp, 0, p1, s1);
3233
- INIT_PTR(lp, 1, p2, s2);
3234
- INIT_PTR_BIT(lp, 2, a3, p3, s3);
3235
- for (; i--;) {
3236
- GET_DATA_STRIDE(p1, s1, dtype, x);
3237
- GET_DATA_STRIDE(p2, s2, dtype, y);
3238
- b = (m_lt(x, y)) ? 1 : 0;
3239
- STORE_BIT(a3, p3, b);
3240
- p3 += s3;
3241
- }
3242
- }
3243
-
3244
- static VALUE uint8_lt_self(VALUE self, VALUE other) {
3245
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
3246
- ndfunc_arg_out_t aout[1] = { { numo_cBit, 0 } };
3247
- ndfunc_t ndf = { iter_uint8_lt, STRIDE_LOOP, 2, 1, ain, aout };
3248
-
3249
- return na_ndloop(&ndf, 2, self, other);
3250
- }
3251
-
3252
- /*
3253
- Comparison lt other.
3254
- @overload lt other
3255
- @param [Numo::NArray,Numeric] other
3256
- @return [Numo::Bit] result of self lt other.
3257
- */
3258
- static VALUE uint8_lt(VALUE self, VALUE other) {
3259
-
3260
- VALUE klass, v;
3261
- klass = na_upcast(rb_obj_class(self), rb_obj_class(other));
3262
- if (klass == cT) {
3263
- return uint8_lt_self(self, other);
3264
- } else {
3265
- v = rb_funcall(klass, id_cast, 1, self);
3266
- return rb_funcall(v, id_lt, 1, other);
3267
- }
3268
- }
3269
-
3270
- static void iter_uint8_le(na_loop_t* const lp) {
3271
- size_t i;
3272
- char *p1, *p2;
3273
- BIT_DIGIT* a3;
3274
- size_t p3;
3275
- ssize_t s1, s2, s3;
3276
- dtype x, y;
3277
- BIT_DIGIT b;
3278
- INIT_COUNTER(lp, i);
3279
- INIT_PTR(lp, 0, p1, s1);
3280
- INIT_PTR(lp, 1, p2, s2);
3281
- INIT_PTR_BIT(lp, 2, a3, p3, s3);
3282
- for (; i--;) {
3283
- GET_DATA_STRIDE(p1, s1, dtype, x);
3284
- GET_DATA_STRIDE(p2, s2, dtype, y);
3285
- b = (m_le(x, y)) ? 1 : 0;
3286
- STORE_BIT(a3, p3, b);
3287
- p3 += s3;
3288
- }
3289
- }
3290
-
3291
- static VALUE uint8_le_self(VALUE self, VALUE other) {
3292
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
3293
- ndfunc_arg_out_t aout[1] = { { numo_cBit, 0 } };
3294
- ndfunc_t ndf = { iter_uint8_le, STRIDE_LOOP, 2, 1, ain, aout };
3295
-
3296
- return na_ndloop(&ndf, 2, self, other);
3297
- }
3298
-
3299
- /*
3300
- Comparison le other.
3301
- @overload le other
3302
- @param [Numo::NArray,Numeric] other
3303
- @return [Numo::Bit] result of self le other.
3304
- */
3305
- static VALUE uint8_le(VALUE self, VALUE other) {
3306
-
3307
- VALUE klass, v;
3308
- klass = na_upcast(rb_obj_class(self), rb_obj_class(other));
3309
- if (klass == cT) {
3310
- return uint8_le_self(self, other);
3311
- } else {
3312
- v = rb_funcall(klass, id_cast, 1, self);
3313
- return rb_funcall(v, id_le, 1, other);
3314
- }
3315
- }
3316
-
3317
- // ------- Integer count without weights -------
3318
-
3319
- static void iter_uint8_bincount_32(na_loop_t* const lp) {
3320
- size_t i, x, n;
3321
- char *p1, *p2;
3322
- ssize_t s1, s2;
3323
- size_t* idx1;
3324
-
3325
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
3326
- INIT_PTR(lp, 1, p2, s2);
3327
- i = lp->args[0].shape[0];
3328
- n = lp->args[1].shape[0];
3329
-
3330
- // initialize
3331
- for (x = 0; x < n; x++) {
3332
- *(u_int32_t*)(p2 + s2 * x) = 0;
3333
- }
3334
-
3335
- if (idx1) {
3336
- for (; i--;) {
3337
- GET_DATA_INDEX(p1, idx1, dtype, x);
3338
- (*(u_int32_t*)(p2 + s2 * x))++;
3339
- }
3340
- } else {
3341
- for (; i--;) {
3342
- GET_DATA_STRIDE(p1, s1, dtype, x);
3343
- (*(u_int32_t*)(p2 + s2 * x))++;
3344
- }
3345
- }
3346
- }
3347
-
3348
- static VALUE uint8_bincount_32(VALUE self, size_t length) {
3349
- size_t shape_out[1] = { length };
3350
- ndfunc_arg_in_t ain[1] = { { cT, 1 } };
3351
- ndfunc_arg_out_t aout[1] = { { numo_cUInt32, 1, shape_out } };
3352
- ndfunc_t ndf = {
3353
- iter_uint8_bincount_32, NO_LOOP | NDF_STRIDE_LOOP | NDF_INDEX_LOOP, 1, 1, ain, aout
3354
- };
3355
-
3356
- return na_ndloop(&ndf, 1, self);
3357
- }
3358
-
3359
- static void iter_uint8_bincount_64(na_loop_t* const lp) {
3360
- size_t i, x, n;
3361
- char *p1, *p2;
3362
- ssize_t s1, s2;
3363
- size_t* idx1;
3364
-
3365
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
3366
- INIT_PTR(lp, 1, p2, s2);
3367
- i = lp->args[0].shape[0];
3368
- n = lp->args[1].shape[0];
3369
-
3370
- // initialize
3371
- for (x = 0; x < n; x++) {
3372
- *(u_int64_t*)(p2 + s2 * x) = 0;
1436
+ if (nd > 0) {
1437
+ nd--;
3373
1438
  }
1439
+ md = nd + 2;
1440
+ a = ALLOCA_N(VALUE, md);
3374
1441
 
1442
+ INIT_COUNTER(lp, i);
1443
+ INIT_PTR_IDX(lp, 0, p1, s1, idx1);
1444
+ c[nd] = 0;
3375
1445
  if (idx1) {
3376
1446
  for (; i--;) {
3377
1447
  GET_DATA_INDEX(p1, idx1, dtype, x);
3378
- (*(u_int64_t*)(p2 + s2 * x))++;
1448
+ yield_each_with_index(x, c, a, nd, md);
1449
+ c[nd]++;
3379
1450
  }
3380
1451
  } else {
3381
1452
  for (; i--;) {
3382
1453
  GET_DATA_STRIDE(p1, s1, dtype, x);
3383
- (*(u_int64_t*)(p2 + s2 * x))++;
1454
+ yield_each_with_index(x, c, a, nd, md);
1455
+ c[nd]++;
3384
1456
  }
3385
1457
  }
3386
1458
  }
3387
1459
 
3388
- static VALUE uint8_bincount_64(VALUE self, size_t length) {
3389
- size_t shape_out[1] = { length };
3390
- ndfunc_arg_in_t ain[1] = { { cT, 1 } };
3391
- ndfunc_arg_out_t aout[1] = { { numo_cUInt64, 1, shape_out } };
3392
- ndfunc_t ndf = {
3393
- iter_uint8_bincount_64, NO_LOOP | NDF_STRIDE_LOOP | NDF_INDEX_LOOP, 1, 1, ain, aout
3394
- };
1460
+ /*
1461
+ Invokes the given block once for each element of self,
1462
+ passing that element and indices along each axis as parameters.
1463
+ @overload each_with_index
1464
+ For a block `{|x,i,j,...| ... }`,
1465
+ @yieldparam [Numeric] x an element
1466
+ @yieldparam [Integer] i,j,... multitimensional indices
1467
+ @return [Numo::NArray] self
1468
+ @see #each
1469
+ @see #map_with_index
1470
+ */
1471
+ static VALUE uint8_each_with_index(VALUE self) {
1472
+ ndfunc_arg_in_t ain[1] = { { Qnil, 0 } };
1473
+ ndfunc_t ndf = { iter_uint8_each_with_index, FULL_LOOP_NIP, 1, 0, ain, 0 };
3395
1474
 
3396
- return na_ndloop(&ndf, 1, self);
1475
+ na_ndloop_with_index(&ndf, 1, self);
1476
+ return self;
3397
1477
  }
3398
- // ------- end of Integer count without weights -------
3399
1478
 
3400
- // ------- Float count with weights -------
3401
-
3402
- static void iter_uint8_bincount_sf(na_loop_t* const lp) {
3403
- float w;
3404
- size_t i, x, n, m;
3405
- char *p1, *p2, *p3;
3406
- ssize_t s1, s2, s3;
3407
-
3408
- INIT_PTR(lp, 0, p1, s1);
3409
- INIT_PTR(lp, 1, p2, s2);
3410
- INIT_PTR(lp, 2, p3, s3);
3411
- i = lp->args[0].shape[0];
3412
- m = lp->args[1].shape[0];
3413
- n = lp->args[2].shape[0];
3414
-
3415
- if (i != m) {
3416
- rb_raise(nary_eShapeError, "size mismatch along last axis between self and weight");
3417
- }
1479
+ static inline dtype yield_map_with_index(dtype x, size_t* c, VALUE* a, int nd, int md) {
1480
+ int j;
1481
+ VALUE y;
3418
1482
 
3419
- // initialize
3420
- for (x = 0; x < n; x++) {
3421
- *(float*)(p3 + s3 * x) = 0;
3422
- }
3423
- for (; i--;) {
3424
- GET_DATA_STRIDE(p1, s1, dtype, x);
3425
- GET_DATA_STRIDE(p2, s2, float, w);
3426
- (*(float*)(p3 + s3 * x)) += w;
1483
+ a[0] = m_data_to_num(x);
1484
+ for (j = 0; j <= nd; j++) {
1485
+ a[j + 1] = SIZET2NUM(c[j]);
3427
1486
  }
1487
+ y = rb_yield(rb_ary_new4(md, a));
1488
+ return m_num_to_data(y);
3428
1489
  }
3429
1490
 
3430
- static VALUE uint8_bincount_sf(VALUE self, VALUE weight, size_t length) {
3431
- size_t shape_out[1] = { length };
3432
- ndfunc_arg_in_t ain[2] = { { cT, 1 }, { numo_cSFloat, 1 } };
3433
- ndfunc_arg_out_t aout[1] = { { numo_cSFloat, 1, shape_out } };
3434
- ndfunc_t ndf = { iter_uint8_bincount_sf, NO_LOOP | NDF_STRIDE_LOOP, 2, 1, ain, aout };
3435
-
3436
- return na_ndloop(&ndf, 2, self, weight);
3437
- }
3438
-
3439
- static void iter_uint8_bincount_df(na_loop_t* const lp) {
3440
- double w;
3441
- size_t i, x, n, m;
3442
- char *p1, *p2, *p3;
3443
- ssize_t s1, s2, s3;
3444
-
3445
- INIT_PTR(lp, 0, p1, s1);
3446
- INIT_PTR(lp, 1, p2, s2);
3447
- INIT_PTR(lp, 2, p3, s3);
3448
- i = lp->args[0].shape[0];
3449
- m = lp->args[1].shape[0];
3450
- n = lp->args[2].shape[0];
3451
-
3452
- if (i != m) {
3453
- rb_raise(nary_eShapeError, "size mismatch along last axis between self and weight");
3454
- }
1491
+ static void iter_uint8_map_with_index(na_loop_t* const lp) {
1492
+ size_t i;
1493
+ char *p1, *p2;
1494
+ ssize_t s1, s2;
1495
+ size_t *idx1, *idx2;
1496
+ dtype x;
1497
+ VALUE* a;
1498
+ size_t* c;
1499
+ int nd, md;
3455
1500
 
3456
- // initialize
3457
- for (x = 0; x < n; x++) {
3458
- *(double*)(p3 + s3 * x) = 0;
3459
- }
3460
- for (; i--;) {
3461
- GET_DATA_STRIDE(p1, s1, dtype, x);
3462
- GET_DATA_STRIDE(p2, s2, double, w);
3463
- (*(double*)(p3 + s3 * x)) += w;
1501
+ c = (size_t*)(lp->opt_ptr);
1502
+ nd = lp->ndim;
1503
+ if (nd > 0) {
1504
+ nd--;
3464
1505
  }
3465
- }
1506
+ md = nd + 2;
1507
+ a = ALLOCA_N(VALUE, md);
3466
1508
 
3467
- static VALUE uint8_bincount_df(VALUE self, VALUE weight, size_t length) {
3468
- size_t shape_out[1] = { length };
3469
- ndfunc_arg_in_t ain[2] = { { cT, 1 }, { numo_cDFloat, 1 } };
3470
- ndfunc_arg_out_t aout[1] = { { numo_cDFloat, 1, shape_out } };
3471
- ndfunc_t ndf = { iter_uint8_bincount_df, NO_LOOP | NDF_STRIDE_LOOP, 2, 1, ain, aout };
1509
+ INIT_COUNTER(lp, i);
1510
+ INIT_PTR_IDX(lp, 0, p1, s1, idx1);
1511
+ INIT_PTR_IDX(lp, 1, p2, s2, idx2);
3472
1512
 
3473
- return na_ndloop(&ndf, 2, self, weight);
1513
+ c[nd] = 0;
1514
+ if (idx1) {
1515
+ if (idx2) {
1516
+ for (; i--;) {
1517
+ GET_DATA_INDEX(p1, idx1, dtype, x);
1518
+ x = yield_map_with_index(x, c, a, nd, md);
1519
+ SET_DATA_INDEX(p2, idx2, dtype, x);
1520
+ c[nd]++;
1521
+ }
1522
+ } else {
1523
+ for (; i--;) {
1524
+ GET_DATA_INDEX(p1, idx1, dtype, x);
1525
+ x = yield_map_with_index(x, c, a, nd, md);
1526
+ SET_DATA_STRIDE(p2, s2, dtype, x);
1527
+ c[nd]++;
1528
+ }
1529
+ }
1530
+ } else {
1531
+ if (idx2) {
1532
+ for (; i--;) {
1533
+ GET_DATA_STRIDE(p1, s1, dtype, x);
1534
+ x = yield_map_with_index(x, c, a, nd, md);
1535
+ SET_DATA_INDEX(p2, idx2, dtype, x);
1536
+ c[nd]++;
1537
+ }
1538
+ } else {
1539
+ for (; i--;) {
1540
+ GET_DATA_STRIDE(p1, s1, dtype, x);
1541
+ x = yield_map_with_index(x, c, a, nd, md);
1542
+ SET_DATA_STRIDE(p2, s2, dtype, x);
1543
+ c[nd]++;
1544
+ }
1545
+ }
1546
+ }
3474
1547
  }
3475
- // ------- end of Float count with weights -------
3476
1548
 
3477
1549
  /*
3478
- Count the number of occurrences of each non-negative integer value.
3479
- Only Integer-types has this method.
3480
-
3481
- @overload bincount([weight], minlength:nil)
3482
- @param [SFloat or DFloat or Array] weight (optional) Array of
3483
- float values. Its size along last axis should be same as that of self.
3484
- @param [Integer] minlength (keyword, optional) Minimum size along
3485
- last axis for the output array.
3486
- @return [UInt32 or UInt64 or SFloat or DFloat]
3487
- Returns Float NArray if weight array is supplied,
3488
- otherwise returns UInt32 or UInt64 depending on the size along last axis.
3489
- @example
3490
- Numo::Int32[0..4].bincount
3491
- # => Numo::UInt32#shape=[5]
3492
- # [1, 1, 1, 1, 1]
3493
-
3494
- Numo::Int32[0, 1, 1, 3, 2, 1, 7].bincount
3495
- # => Numo::UInt32#shape=[8]
3496
- # [1, 3, 1, 1, 0, 0, 0, 1]
3497
-
3498
- x = Numo::Int32[0, 1, 1, 3, 2, 1, 7, 23]
3499
- x.bincount.size == x.max+1
3500
- # => true
3501
-
3502
- w = Numo::DFloat[0.3, 0.5, 0.2, 0.7, 1.0, -0.6]
3503
- x = Numo::Int32[0, 1, 1, 2, 2, 2]
3504
- x.bincount(w)
3505
- # => Numo::DFloat#shape=[3]
3506
- # [0.3, 0.7, 1.1]
3507
-
1550
+ Invokes the given block once for each element of self,
1551
+ passing that element and indices along each axis as parameters.
1552
+ Creates a new NArray containing the values returned by the block.
1553
+ Inplace option is allowed, i.e., `nary.inplace.map` overwrites `nary`.
1554
+ @overload map_with_index
1555
+ For a block `{|x,i,j,...| ... }`,
1556
+ @yieldparam [Numeric] x an element
1557
+ @yieldparam [Integer] i,j,... multitimensional indices
1558
+ @return [Numo::NArray] mapped array
1559
+ @see #map
1560
+ @see #each_with_index
3508
1561
  */
3509
- static VALUE uint8_bincount(int argc, VALUE* argv, VALUE self) {
3510
- VALUE weight = Qnil, kw = Qnil;
3511
- VALUE opts[1] = { Qundef };
3512
- VALUE v, wclass;
3513
- ID table[1] = { id_minlength };
3514
- size_t length, minlength;
3515
-
3516
- rb_scan_args(argc, argv, "01:", &weight, &kw);
3517
- rb_get_kwargs(kw, table, 0, 1, opts);
3518
-
3519
- v = uint8_max(0, 0, self);
3520
-
3521
- length = NUM2SIZET(v) + 1;
1562
+ static VALUE uint8_map_with_index(VALUE self) {
1563
+ ndfunc_arg_in_t ain[1] = { { Qnil, 0 } };
1564
+ ndfunc_arg_out_t aout[1] = { { cT, 0 } };
1565
+ ndfunc_t ndf = { iter_uint8_map_with_index, FULL_LOOP, 1, 1, ain, aout };
3522
1566
 
3523
- if (opts[0] != Qundef) {
3524
- minlength = NUM2SIZET(opts[0]);
3525
- if (minlength > length) {
3526
- length = minlength;
3527
- }
3528
- }
1567
+ return na_ndloop_with_index(&ndf, 1, self);
1568
+ }
3529
1569
 
3530
- if (NIL_P(weight)) {
3531
- if (length > 4294967295ul) {
3532
- return uint8_bincount_64(self, length);
1570
+ static void iter_uint8_abs(na_loop_t* const lp) {
1571
+ size_t i;
1572
+ char *p1, *p2;
1573
+ ssize_t s1, s2;
1574
+ size_t *idx1, *idx2;
1575
+ dtype x;
1576
+ rtype y;
1577
+ INIT_COUNTER(lp, i);
1578
+ INIT_PTR_IDX(lp, 0, p1, s1, idx1);
1579
+ INIT_PTR_IDX(lp, 1, p2, s2, idx2);
1580
+ if (idx1) {
1581
+ if (idx2) {
1582
+ for (; i--;) {
1583
+ GET_DATA_INDEX(p1, idx1, dtype, x);
1584
+ y = m_abs(x);
1585
+ SET_DATA_INDEX(p2, idx2, rtype, y);
1586
+ }
3533
1587
  } else {
3534
- return uint8_bincount_32(self, length);
1588
+ for (; i--;) {
1589
+ GET_DATA_INDEX(p1, idx1, dtype, x);
1590
+ y = m_abs(x);
1591
+ SET_DATA_STRIDE(p2, s2, rtype, y);
1592
+ }
3535
1593
  }
3536
1594
  } else {
3537
- wclass = rb_obj_class(weight);
3538
- if (wclass == numo_cSFloat) {
3539
- return uint8_bincount_sf(self, weight, length);
1595
+ if (idx2) {
1596
+ for (; i--;) {
1597
+ GET_DATA_STRIDE(p1, s1, dtype, x);
1598
+ y = m_abs(x);
1599
+ SET_DATA_INDEX(p2, idx2, rtype, y);
1600
+ }
3540
1601
  } else {
3541
- return uint8_bincount_df(self, weight, length);
1602
+ for (; i--;) {
1603
+ GET_DATA_STRIDE(p1, s1, dtype, x);
1604
+ y = m_abs(x);
1605
+ SET_DATA_STRIDE(p2, s2, rtype, y);
1606
+ }
3542
1607
  }
3543
1608
  }
3544
1609
  }
3545
1610
 
1611
+ /*
1612
+ abs of self.
1613
+ @overload abs
1614
+ @return [Numo::UInt8] abs of self.
1615
+ */
1616
+ static VALUE uint8_abs(VALUE self) {
1617
+ ndfunc_arg_in_t ain[1] = { { cT, 0 } };
1618
+ ndfunc_arg_out_t aout[1] = { { cRT, 0 } };
1619
+ ndfunc_t ndf = { iter_uint8_abs, FULL_LOOP, 1, 1, ain, aout };
1620
+
1621
+ return na_ndloop(&ndf, 1, self);
1622
+ }
1623
+
3546
1624
  static void iter_uint8_poly(na_loop_t* const lp) {
3547
1625
  size_t i;
3548
1626
  dtype x, y, a;
@@ -4166,50 +2244,216 @@ void Init_numo_uint8(void) {
4166
2244
  rb_define_singleton_method(cT, "cast", uint8_s_cast, 1);
4167
2245
  rb_define_method(cT, "[]", uint8_aref, -1);
4168
2246
  rb_define_method(cT, "[]=", uint8_aset, -1);
2247
+ /**
2248
+ * return NArray with cast to the type of self.
2249
+ * @overload coerce_cast(type)
2250
+ * @return [nil]
2251
+ */
4169
2252
  rb_define_method(cT, "coerce_cast", uint8_coerce_cast, 1);
2253
+ /**
2254
+ * Convert self to Array.
2255
+ * @overload to_a
2256
+ * @return [Array]
2257
+ */
4170
2258
  rb_define_method(cT, "to_a", uint8_to_a, 0);
2259
+ /**
2260
+ * Fill elements with other.
2261
+ * @overload fill other
2262
+ * @param [Numeric] other
2263
+ * @return [Numo::UInt8] self.
2264
+ */
4171
2265
  rb_define_method(cT, "fill", uint8_fill, 1);
2266
+ /**
2267
+ * Format elements into strings.
2268
+ * @overload format format
2269
+ * @param [String] format
2270
+ * @return [Numo::RObject] array of formatted strings.
2271
+ */
4172
2272
  rb_define_method(cT, "format", uint8_format, -1);
2273
+ /**
2274
+ * Format elements into strings.
2275
+ * @overload format_to_a format
2276
+ * @param [String] format
2277
+ * @return [Array] array of formatted strings.
2278
+ */
4173
2279
  rb_define_method(cT, "format_to_a", uint8_format_to_a, -1);
2280
+ /**
2281
+ * Returns a string containing a human-readable representation of NArray.
2282
+ * @overload inspect
2283
+ * @return [String]
2284
+ */
4174
2285
  rb_define_method(cT, "inspect", uint8_inspect, 0);
4175
2286
  rb_define_method(cT, "each", uint8_each, 0);
4176
2287
  rb_define_method(cT, "map", uint8_map, 0);
4177
2288
  rb_define_method(cT, "each_with_index", uint8_each_with_index, 0);
4178
2289
  rb_define_method(cT, "map_with_index", uint8_map_with_index, 0);
4179
2290
  rb_define_method(cT, "abs", uint8_abs, 0);
2291
+ /**
2292
+ * Binary add.
2293
+ * @overload + other
2294
+ * @param [Numo::NArray,Numeric] other
2295
+ * @return [Numo::NArray] self + other
2296
+ */
4180
2297
  rb_define_method(cT, "+", uint8_add, 1);
2298
+ /**
2299
+ * Binary sub.
2300
+ * @overload - other
2301
+ * @param [Numo::NArray,Numeric] other
2302
+ * @return [Numo::NArray] self - other
2303
+ */
4181
2304
  rb_define_method(cT, "-", uint8_sub, 1);
2305
+ /**
2306
+ * Binary mul.
2307
+ * @overload * other
2308
+ * @param [Numo::NArray,Numeric] other
2309
+ * @return [Numo::NArray] self * other
2310
+ */
4182
2311
  rb_define_method(cT, "*", uint8_mul, 1);
2312
+ /**
2313
+ * Binary div.
2314
+ * @overload / other
2315
+ * @param [Numo::NArray,Numeric] other
2316
+ * @return [Numo::NArray] self / other
2317
+ */
4183
2318
  rb_define_method(cT, "/", uint8_div, 1);
2319
+ /**
2320
+ * Binary mod.
2321
+ * @overload % other
2322
+ * @param [Numo::NArray,Numeric] other
2323
+ * @return [Numo::NArray] self % other
2324
+ */
4184
2325
  rb_define_method(cT, "%", uint8_mod, 1);
2326
+ /**
2327
+ * Binary divmod.
2328
+ * @overload divmod other
2329
+ * @param [Numo::NArray,Numeric] other
2330
+ * @return [Numo::NArray] divmod of self and other.
2331
+ */
4185
2332
  rb_define_method(cT, "divmod", uint8_divmod, 1);
2333
+ /**
2334
+ * Binary power.
2335
+ * @overload ** other
2336
+ * @param [Numo::NArray,Numeric] other
2337
+ * @return [Numo::NArray] self to the other-th power.
2338
+ */
4186
2339
  rb_define_method(cT, "**", uint8_pow, 1);
4187
2340
  rb_define_alias(cT, "pow", "**");
2341
+ /**
2342
+ * Unary minus.
2343
+ * @overload -@
2344
+ * @return [Numo::UInt8] minus of self.
2345
+ */
4188
2346
  rb_define_method(cT, "-@", uint8_minus, 0);
2347
+ /**
2348
+ * Unary reciprocal.
2349
+ * @overload reciprocal
2350
+ * @return [Numo::UInt8] reciprocal of self.
2351
+ */
4189
2352
  rb_define_method(cT, "reciprocal", uint8_reciprocal, 0);
2353
+ /**
2354
+ * Unary sign.
2355
+ * @overload sign
2356
+ * @return [Numo::UInt8] sign of self.
2357
+ */
4190
2358
  rb_define_method(cT, "sign", uint8_sign, 0);
2359
+ /**
2360
+ * Unary square.
2361
+ * @overload square
2362
+ * @return [Numo::UInt8] square of self.
2363
+ */
4191
2364
  rb_define_method(cT, "square", uint8_square, 0);
4192
2365
  rb_define_alias(cT, "conj", "view");
4193
2366
  rb_define_alias(cT, "im", "view");
4194
2367
  rb_define_alias(cT, "conjugate", "conj");
2368
+ /**
2369
+ * Comparison eq other.
2370
+ * @overload eq other
2371
+ * @param [Numo::NArray,Numeric] other
2372
+ * @return [Numo::Bit] result of self eq other.
2373
+ */
4195
2374
  rb_define_method(cT, "eq", uint8_eq, 1);
2375
+ /**
2376
+ * Comparison ne other.
2377
+ * @overload ne other
2378
+ * @param [Numo::NArray,Numeric] other
2379
+ * @return [Numo::Bit] result of self ne other.
2380
+ */
4196
2381
  rb_define_method(cT, "ne", uint8_ne, 1);
4197
2382
  rb_define_alias(cT, "nearly_eq", "eq");
4198
2383
  rb_define_alias(cT, "close_to", "nearly_eq");
2384
+ /**
2385
+ * Binary bit_and.
2386
+ * @overload & other
2387
+ * @param [Numo::NArray,Numeric] other
2388
+ * @return [Numo::NArray] self & other
2389
+ */
4199
2390
  rb_define_method(cT, "&", uint8_bit_and, 1);
2391
+ /**
2392
+ * Binary bit_or.
2393
+ * @overload | other
2394
+ * @param [Numo::NArray,Numeric] other
2395
+ * @return [Numo::NArray] self | other
2396
+ */
4200
2397
  rb_define_method(cT, "|", uint8_bit_or, 1);
2398
+ /**
2399
+ * Binary bit_xor.
2400
+ * @overload ^ other
2401
+ * @param [Numo::NArray,Numeric] other
2402
+ * @return [Numo::NArray] self ^ other
2403
+ */
4201
2404
  rb_define_method(cT, "^", uint8_bit_xor, 1);
2405
+ /**
2406
+ * Unary bit_not.
2407
+ * @overload ~
2408
+ * @return [Numo::UInt8] bit_not of self.
2409
+ */
4202
2410
  rb_define_method(cT, "~", uint8_bit_not, 0);
2411
+ /**
2412
+ * Binary left_shift.
2413
+ * @overload << other
2414
+ * @param [Numo::NArray,Numeric] other
2415
+ * @return [Numo::NArray] self << other
2416
+ */
4203
2417
  rb_define_method(cT, "<<", uint8_left_shift, 1);
2418
+ /**
2419
+ * Binary right_shift.
2420
+ * @overload >> other
2421
+ * @param [Numo::NArray,Numeric] other
2422
+ * @return [Numo::NArray] self >> other
2423
+ */
4204
2424
  rb_define_method(cT, ">>", uint8_right_shift, 1);
4205
2425
  rb_define_alias(cT, "floor", "view");
4206
2426
  rb_define_alias(cT, "round", "view");
4207
2427
  rb_define_alias(cT, "ceil", "view");
4208
2428
  rb_define_alias(cT, "trunc", "view");
4209
2429
  rb_define_alias(cT, "rint", "view");
2430
+ /**
2431
+ * Comparison gt other.
2432
+ * @overload gt other
2433
+ * @param [Numo::NArray,Numeric] other
2434
+ * @return [Numo::Bit] result of self gt other.
2435
+ */
4210
2436
  rb_define_method(cT, "gt", uint8_gt, 1);
2437
+ /**
2438
+ * Comparison ge other.
2439
+ * @overload ge other
2440
+ * @param [Numo::NArray,Numeric] other
2441
+ * @return [Numo::Bit] result of self ge other.
2442
+ */
4211
2443
  rb_define_method(cT, "ge", uint8_ge, 1);
2444
+ /**
2445
+ * Comparison lt other.
2446
+ * @overload lt other
2447
+ * @param [Numo::NArray,Numeric] other
2448
+ * @return [Numo::Bit] result of self lt other.
2449
+ */
4212
2450
  rb_define_method(cT, "lt", uint8_lt, 1);
2451
+ /**
2452
+ * Comparison le other.
2453
+ * @overload le other
2454
+ * @param [Numo::NArray,Numeric] other
2455
+ * @return [Numo::Bit] result of self le other.
2456
+ */
4213
2457
  rb_define_method(cT, "le", uint8_le, 1);
4214
2458
  rb_define_alias(cT, ">", "gt");
4215
2459
  rb_define_alias(cT, ">=", "ge");
@@ -4394,6 +2638,37 @@ void Init_numo_uint8(void) {
4394
2638
  * @return [Numo::UInt8]
4395
2639
  */
4396
2640
  rb_define_module_function(cT, "minimum", uint8_s_minimum, -1);
2641
+ /**
2642
+ * Count the number of occurrences of each non-negative integer value.
2643
+ * Only Integer-types has this method.
2644
+ *
2645
+ * @overload bincount([weight], minlength:nil)
2646
+ * @param [SFloat or DFloat or Array] weight (optional) Array of
2647
+ * float values. Its size along last axis should be same as that of self.
2648
+ * @param [Integer] minlength (keyword, optional) Minimum size along
2649
+ * last axis for the output array.
2650
+ * @return [UInt32 or UInt64 or SFloat or DFloat]
2651
+ * Returns Float NArray if weight array is supplied,
2652
+ * otherwise returns UInt32 or UInt64 depending on the size along last axis.
2653
+ * @example
2654
+ * Numo::Int32[0..4].bincount
2655
+ * # => Numo::UInt32#shape=[5]
2656
+ * # [1, 1, 1, 1, 1]
2657
+ *
2658
+ * Numo::Int32[0, 1, 1, 3, 2, 1, 7].bincount
2659
+ * # => Numo::UInt32#shape=[8]
2660
+ * # [1, 3, 1, 1, 0, 0, 0, 1]
2661
+ *
2662
+ * x = Numo::Int32[0, 1, 1, 3, 2, 1, 7, 23]
2663
+ * x.bincount.size == x.max+1
2664
+ * # => true
2665
+ *
2666
+ * w = Numo::DFloat[0.3, 0.5, 0.2, 0.7, 1.0, -0.6]
2667
+ * x = Numo::Int32[0, 1, 1, 2, 2, 2]
2668
+ * x.bincount(w)
2669
+ * # => Numo::DFloat#shape=[3]
2670
+ * # [0.3, 0.7, 1.1]
2671
+ */
4397
2672
  rb_define_method(cT, "bincount", uint8_bincount, -1);
4398
2673
  /**
4399
2674
  * cumsum of self.