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.
Files changed (112) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +0 -1
  3. data/LICENSE +1 -1
  4. data/README.md +7 -0
  5. data/ext/numo/narray/numo/narray.h +2 -2
  6. data/ext/numo/narray/numo/types/robj_macro.h +1 -1
  7. data/ext/numo/narray/numo/types/robject.h +1 -1
  8. data/ext/numo/narray/src/mh/argmax.h +154 -0
  9. data/ext/numo/narray/src/mh/argmin.h +154 -0
  10. data/ext/numo/narray/src/mh/bincount.h +233 -0
  11. data/ext/numo/narray/src/mh/bit/and.h +225 -0
  12. data/ext/numo/narray/src/mh/bit/left_shift.h +225 -0
  13. data/ext/numo/narray/src/mh/bit/not.h +173 -0
  14. data/ext/numo/narray/src/mh/bit/or.h +225 -0
  15. data/ext/numo/narray/src/mh/bit/right_shift.h +225 -0
  16. data/ext/numo/narray/src/mh/bit/xor.h +225 -0
  17. data/ext/numo/narray/src/mh/clip.h +115 -0
  18. data/ext/numo/narray/src/mh/coerce_cast.h +9 -0
  19. data/ext/numo/narray/src/mh/comp/binary_func.h +37 -0
  20. data/ext/numo/narray/src/mh/comp/eq.h +26 -0
  21. data/ext/numo/narray/src/mh/comp/ge.h +26 -0
  22. data/ext/numo/narray/src/mh/comp/gt.h +26 -0
  23. data/ext/numo/narray/src/mh/comp/le.h +26 -0
  24. data/ext/numo/narray/src/mh/comp/lt.h +26 -0
  25. data/ext/numo/narray/src/mh/comp/ne.h +26 -0
  26. data/ext/numo/narray/src/mh/comp/nearly_eq.h +26 -0
  27. data/ext/numo/narray/src/mh/cumprod.h +98 -0
  28. data/ext/numo/narray/src/mh/cumsum.h +98 -0
  29. data/ext/numo/narray/src/mh/divmod.h +142 -0
  30. data/ext/numo/narray/src/mh/eye.h +82 -0
  31. data/ext/numo/narray/src/mh/fill.h +94 -0
  32. data/ext/numo/narray/src/mh/format.h +108 -0
  33. data/ext/numo/narray/src/mh/format_to_a.h +89 -0
  34. data/ext/numo/narray/src/mh/inspect.h +33 -0
  35. data/ext/numo/narray/src/mh/isfinite.h +42 -0
  36. data/ext/numo/narray/src/mh/isinf.h +42 -0
  37. data/ext/numo/narray/src/mh/isnan.h +42 -0
  38. data/ext/numo/narray/src/mh/isneginf.h +42 -0
  39. data/ext/numo/narray/src/mh/isposinf.h +42 -0
  40. data/ext/numo/narray/src/mh/logseq.h +69 -0
  41. data/ext/numo/narray/src/mh/math/acos.h +2 -2
  42. data/ext/numo/narray/src/mh/math/acosh.h +2 -2
  43. data/ext/numo/narray/src/mh/math/asin.h +2 -2
  44. data/ext/numo/narray/src/mh/math/asinh.h +2 -2
  45. data/ext/numo/narray/src/mh/math/atan.h +2 -2
  46. data/ext/numo/narray/src/mh/math/atan2.h +3 -3
  47. data/ext/numo/narray/src/mh/math/atanh.h +2 -2
  48. data/ext/numo/narray/src/mh/math/cbrt.h +2 -2
  49. data/ext/numo/narray/src/mh/math/cos.h +2 -2
  50. data/ext/numo/narray/src/mh/math/cosh.h +2 -2
  51. data/ext/numo/narray/src/mh/math/erf.h +2 -2
  52. data/ext/numo/narray/src/mh/math/erfc.h +2 -2
  53. data/ext/numo/narray/src/mh/math/exp.h +2 -2
  54. data/ext/numo/narray/src/mh/math/exp10.h +2 -2
  55. data/ext/numo/narray/src/mh/math/exp2.h +2 -2
  56. data/ext/numo/narray/src/mh/math/expm1.h +2 -2
  57. data/ext/numo/narray/src/mh/math/frexp.h +3 -3
  58. data/ext/numo/narray/src/mh/math/hypot.h +3 -3
  59. data/ext/numo/narray/src/mh/math/ldexp.h +3 -3
  60. data/ext/numo/narray/src/mh/math/log.h +2 -2
  61. data/ext/numo/narray/src/mh/math/log10.h +2 -2
  62. data/ext/numo/narray/src/mh/math/log1p.h +2 -2
  63. data/ext/numo/narray/src/mh/math/log2.h +2 -2
  64. data/ext/numo/narray/src/mh/math/sin.h +2 -2
  65. data/ext/numo/narray/src/mh/math/sinc.h +2 -2
  66. data/ext/numo/narray/src/mh/math/sinh.h +2 -2
  67. data/ext/numo/narray/src/mh/math/sqrt.h +8 -8
  68. data/ext/numo/narray/src/mh/math/tan.h +2 -2
  69. data/ext/numo/narray/src/mh/math/tanh.h +2 -2
  70. data/ext/numo/narray/src/mh/math/unary_func.h +3 -3
  71. data/ext/numo/narray/src/mh/max.h +69 -0
  72. data/ext/numo/narray/src/mh/max_index.h +184 -0
  73. data/ext/numo/narray/src/mh/maximum.h +116 -0
  74. data/ext/numo/narray/src/mh/min.h +69 -0
  75. data/ext/numo/narray/src/mh/min_index.h +184 -0
  76. data/ext/numo/narray/src/mh/minimum.h +116 -0
  77. data/ext/numo/narray/src/mh/minmax.h +77 -0
  78. data/ext/numo/narray/src/mh/mulsum.h +185 -0
  79. data/ext/numo/narray/src/mh/op/add.h +78 -0
  80. data/ext/numo/narray/src/mh/op/binary_func.h +423 -0
  81. data/ext/numo/narray/src/mh/op/div.h +118 -0
  82. data/ext/numo/narray/src/mh/op/mod.h +108 -0
  83. data/ext/numo/narray/src/mh/op/mul.h +78 -0
  84. data/ext/numo/narray/src/mh/op/sub.h +78 -0
  85. data/ext/numo/narray/src/mh/prod.h +69 -0
  86. data/ext/numo/narray/src/mh/ptp.h +69 -0
  87. data/ext/numo/narray/src/mh/rand.h +315 -0
  88. data/ext/numo/narray/src/mh/round/ceil.h +11 -0
  89. data/ext/numo/narray/src/mh/round/floor.h +11 -0
  90. data/ext/numo/narray/src/mh/round/rint.h +9 -0
  91. data/ext/numo/narray/src/mh/round/round.h +11 -0
  92. data/ext/numo/narray/src/mh/round/trunc.h +11 -0
  93. data/ext/numo/narray/src/mh/round/unary_func.h +127 -0
  94. data/ext/numo/narray/src/mh/seq.h +130 -0
  95. data/ext/numo/narray/src/mh/sum.h +69 -0
  96. data/ext/numo/narray/src/mh/to_a.h +78 -0
  97. data/ext/numo/narray/src/t_bit.c +45 -234
  98. data/ext/numo/narray/src/t_dcomplex.c +608 -2369
  99. data/ext/numo/narray/src/t_dfloat.c +485 -3736
  100. data/ext/numo/narray/src/t_int16.c +743 -3444
  101. data/ext/numo/narray/src/t_int32.c +745 -3445
  102. data/ext/numo/narray/src/t_int64.c +743 -3446
  103. data/ext/numo/narray/src/t_int8.c +678 -3040
  104. data/ext/numo/narray/src/t_robject.c +771 -3548
  105. data/ext/numo/narray/src/t_scomplex.c +607 -2368
  106. data/ext/numo/narray/src/t_sfloat.c +440 -3693
  107. data/ext/numo/narray/src/t_uint16.c +743 -3440
  108. data/ext/numo/narray/src/t_uint32.c +743 -3440
  109. data/ext/numo/narray/src/t_uint64.c +743 -3442
  110. data/ext/numo/narray/src/t_uint8.c +678 -3038
  111. data/lib/numo/narray.rb +2 -3
  112. metadata +62 -3
@@ -60,17 +60,125 @@ static ID id_truncate;
60
60
  VALUE cT;
61
61
  extern VALUE cRT;
62
62
 
63
+ #include "mh/coerce_cast.h"
64
+ #include "mh/to_a.h"
65
+ #include "mh/fill.h"
66
+ #include "mh/format.h"
67
+ #include "mh/format_to_a.h"
68
+ #include "mh/inspect.h"
69
+ #include "mh/op/add.h"
70
+ #include "mh/op/sub.h"
71
+ #include "mh/op/mul.h"
72
+ #include "mh/op/div.h"
73
+ #include "mh/op/mod.h"
74
+ #include "mh/divmod.h"
75
+ #include "mh/round/floor.h"
76
+ #include "mh/round/round.h"
77
+ #include "mh/round/ceil.h"
78
+ #include "mh/round/trunc.h"
79
+ #include "mh/comp/eq.h"
80
+ #include "mh/comp/ne.h"
81
+ #include "mh/comp/nearly_eq.h"
82
+ #include "mh/comp/gt.h"
83
+ #include "mh/comp/ge.h"
84
+ #include "mh/comp/lt.h"
85
+ #include "mh/comp/le.h"
86
+ #include "mh/bit/and.h"
87
+ #include "mh/bit/or.h"
88
+ #include "mh/bit/xor.h"
89
+ #include "mh/bit/not.h"
90
+ #include "mh/bit/left_shift.h"
91
+ #include "mh/bit/right_shift.h"
92
+ #include "mh/clip.h"
93
+ #include "mh/isnan.h"
94
+ #include "mh/isinf.h"
95
+ #include "mh/isposinf.h"
96
+ #include "mh/isneginf.h"
97
+ #include "mh/isfinite.h"
98
+ #include "mh/sum.h"
99
+ #include "mh/prod.h"
63
100
  #include "mh/mean.h"
64
101
  #include "mh/var.h"
65
102
  #include "mh/stddev.h"
66
103
  #include "mh/rms.h"
104
+ #include "mh/min.h"
105
+ #include "mh/max.h"
106
+ #include "mh/ptp.h"
107
+ #include "mh/max_index.h"
108
+ #include "mh/min_index.h"
109
+ #include "mh/argmax.h"
110
+ #include "mh/argmin.h"
111
+ #include "mh/maximum.h"
112
+ #include "mh/minimum.h"
113
+ #include "mh/minmax.h"
114
+ #include "mh/cumsum.h"
115
+ #include "mh/cumprod.h"
116
+ #include "mh/mulsum.h"
117
+ #include "mh/seq.h"
118
+ #include "mh/logseq.h"
119
+ #include "mh/eye.h"
120
+ #include "mh/rand.h"
67
121
 
68
122
  typedef VALUE robject; // Type aliases for shorter notation
69
123
  // following the codebase naming convention.
124
+ DEF_NARRAY_COERCE_CAST_METHOD_FUNC(robject)
125
+ DEF_NARRAY_TO_A_METHOD_FUNC(robject)
126
+ DEF_NARRAY_FILL_METHOD_FUNC(robject)
127
+ DEF_NARRAY_FORMAT_METHOD_FUNC(robject)
128
+ DEF_NARRAY_FORMAT_TO_A_METHOD_FUNC(robject)
129
+ DEF_NARRAY_ROBJ_INSPECT_METHOD_FUNC()
130
+ DEF_NARRAY_ROBJ_ADD_METHOD_FUNC()
131
+ DEF_NARRAY_ROBJ_SUB_METHOD_FUNC()
132
+ DEF_NARRAY_ROBJ_MUL_METHOD_FUNC()
133
+ DEF_NARRAY_ROBJ_DIV_METHOD_FUNC()
134
+ DEF_NARRAY_ROBJ_MOD_METHOD_FUNC()
135
+ DEF_NARRAY_ROBJ_DIVMOD_METHOD_FUNC()
136
+ DEF_NARRAY_ROBJ_FLOOR_METHOD_FUNC()
137
+ DEF_NARRAY_ROBJ_ROUND_METHOD_FUNC()
138
+ DEF_NARRAY_ROBJ_CEIL_METHOD_FUNC()
139
+ DEF_NARRAY_ROBJ_TRUNC_METHOD_FUNC()
140
+ DEF_NARRAY_ROBJ_EQ_METHOD_FUNC()
141
+ DEF_NARRAY_ROBJ_NE_METHOD_FUNC()
142
+ DEF_NARRAY_ROBJ_NEARLY_EQ_METHOD_FUNC()
143
+ DEF_NARRAY_ROBJ_GT_METHOD_FUNC()
144
+ DEF_NARRAY_ROBJ_GE_METHOD_FUNC()
145
+ DEF_NARRAY_ROBJ_LT_METHOD_FUNC()
146
+ DEF_NARRAY_ROBJ_LE_METHOD_FUNC()
147
+ DEF_NARRAY_ROBJ_BIT_AND_METHOD_FUNC()
148
+ DEF_NARRAY_ROBJ_BIT_OR_METHOD_FUNC()
149
+ DEF_NARRAY_ROBJ_BIT_XOR_METHOD_FUNC()
150
+ DEF_NARRAY_ROBJ_BIT_NOT_METHOD_FUNC()
151
+ DEF_NARRAY_ROBJ_LEFT_SHIFT_METHOD_FUNC()
152
+ DEF_NARRAY_ROBJ_RIGHT_SHIFT_METHOD_FUNC()
153
+ DEF_NARRAY_CLIP_METHOD_FUNC(robject, numo_cRObject)
154
+ DEF_NARRAY_FLT_ISNAN_METHOD_FUNC(robject, numo_cRObject)
155
+ DEF_NARRAY_FLT_ISINF_METHOD_FUNC(robject, numo_cRObject)
156
+ DEF_NARRAY_FLT_ISPOSINF_METHOD_FUNC(robject, numo_cRObject)
157
+ DEF_NARRAY_FLT_ISNEGINF_METHOD_FUNC(robject, numo_cRObject)
158
+ DEF_NARRAY_FLT_ISFINITE_METHOD_FUNC(robject, numo_cRObject)
159
+ DEF_NARRAY_FLT_SUM_METHOD_FUNC(robject, numo_cRObject)
160
+ DEF_NARRAY_FLT_PROD_METHOD_FUNC(robject, numo_cRObject)
70
161
  DEF_NARRAY_FLT_MEAN_METHOD_FUNC(robject, numo_cRObject, VALUE, numo_cRObject)
71
162
  DEF_NARRAY_FLT_VAR_METHOD_FUNC(robject, numo_cRObject, VALUE, numo_cRObject)
72
163
  DEF_NARRAY_FLT_STDDEV_METHOD_FUNC(robject, numo_cRObject, VALUE, numo_cRObject)
73
164
  DEF_NARRAY_FLT_RMS_METHOD_FUNC(robject, numo_cRObject, VALUE, numo_cRObject)
165
+ DEF_NARRAY_FLT_MIN_METHOD_FUNC(robject, numo_cRObject)
166
+ DEF_NARRAY_FLT_MAX_METHOD_FUNC(robject, numo_cRObject)
167
+ DEF_NARRAY_FLT_PTP_METHOD_FUNC(robject, numo_cRObject)
168
+ DEF_NARRAY_FLT_MAX_INDEX_METHOD_FUNC(robject)
169
+ DEF_NARRAY_FLT_MIN_INDEX_METHOD_FUNC(robject)
170
+ DEF_NARRAY_FLT_ARGMAX_METHOD_FUNC(robject)
171
+ DEF_NARRAY_FLT_ARGMIN_METHOD_FUNC(robject)
172
+ DEF_NARRAY_FLT_MAXIMUM_METHOD_FUNC(robject, numo_cRObject)
173
+ DEF_NARRAY_FLT_MINIMUM_METHOD_FUNC(robject, numo_cRObject)
174
+ DEF_NARRAY_FLT_MINMAX_METHOD_FUNC(robject, numo_cRObject)
175
+ DEF_NARRAY_FLT_CUMSUM_METHOD_FUNC(robject, numo_cRObject)
176
+ DEF_NARRAY_FLT_CUMPROD_METHOD_FUNC(robject, numo_cRObject)
177
+ DEF_NARRAY_FLT_MULSUM_METHOD_FUNC(robject, numo_cRObject)
178
+ DEF_NARRAY_FLT_SEQ_METHOD_FUNC(robject)
179
+ DEF_NARRAY_FLT_LOGSEQ_METHOD_FUNC(robject)
180
+ DEF_NARRAY_EYE_METHOD_FUNC(robject)
181
+ DEF_NARRAY_FLT_RAND_METHOD_FUNC(robject)
74
182
 
75
183
  static VALUE robject_store(VALUE, VALUE);
76
184
 
@@ -1246,204 +1354,6 @@ static VALUE robject_aset(int argc, VALUE* argv, VALUE self) {
1246
1354
  return argv[argc];
1247
1355
  }
1248
1356
 
1249
- /*
1250
- return NArray with cast to the type of self.
1251
- @overload coerce_cast(type)
1252
- @return [nil]
1253
- */
1254
- static VALUE robject_coerce_cast(VALUE self, VALUE type) {
1255
- return Qnil;
1256
- }
1257
-
1258
- static void iter_robject_to_a(na_loop_t* const lp) {
1259
- size_t i, s1;
1260
- char* p1;
1261
- size_t* idx1;
1262
- dtype x;
1263
- volatile VALUE a, y;
1264
-
1265
- INIT_COUNTER(lp, i);
1266
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
1267
- a = rb_ary_new2(i);
1268
- rb_ary_push(lp->args[1].value, a);
1269
- if (idx1) {
1270
- for (; i--;) {
1271
- GET_DATA_INDEX(p1, idx1, dtype, x);
1272
- y = m_data_to_num(x);
1273
- rb_ary_push(a, y);
1274
- }
1275
- } else {
1276
- for (; i--;) {
1277
- GET_DATA_STRIDE(p1, s1, dtype, x);
1278
- y = m_data_to_num(x);
1279
- rb_ary_push(a, y);
1280
- }
1281
- }
1282
- }
1283
-
1284
- /*
1285
- Convert self to Array.
1286
- @overload to_a
1287
- @return [Array]
1288
- */
1289
- static VALUE robject_to_a(VALUE self) {
1290
- ndfunc_arg_in_t ain[3] = { { Qnil, 0 }, { sym_loop_opt }, { sym_option } };
1291
- ndfunc_arg_out_t aout[1] = { { rb_cArray, 0 } }; // dummy?
1292
- ndfunc_t ndf = { iter_robject_to_a, FULL_LOOP_NIP, 3, 1, ain, aout };
1293
- return na_ndloop_cast_narray_to_rarray(&ndf, self, Qnil);
1294
- }
1295
-
1296
- static void iter_robject_fill(na_loop_t* const lp) {
1297
- size_t i;
1298
- char* p1;
1299
- ssize_t s1;
1300
- size_t* idx1;
1301
- VALUE x = lp->option;
1302
- dtype y;
1303
- INIT_COUNTER(lp, i);
1304
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
1305
- y = m_num_to_data(x);
1306
- if (idx1) {
1307
- for (; i--;) {
1308
- SET_DATA_INDEX(p1, idx1, dtype, y);
1309
- }
1310
- } else {
1311
- for (; i--;) {
1312
- SET_DATA_STRIDE(p1, s1, dtype, y);
1313
- }
1314
- }
1315
- }
1316
-
1317
- /*
1318
- Fill elements with other.
1319
- @overload fill other
1320
- @param [Numeric] other
1321
- @return [Numo::RObject] self.
1322
- */
1323
- static VALUE robject_fill(VALUE self, VALUE val) {
1324
- ndfunc_arg_in_t ain[2] = { { OVERWRITE, 0 }, { sym_option } };
1325
- ndfunc_t ndf = { iter_robject_fill, FULL_LOOP, 2, 0, ain, 0 };
1326
-
1327
- na_ndloop(&ndf, 2, self, val);
1328
- return self;
1329
- }
1330
-
1331
- static VALUE format_robject(VALUE fmt, dtype* x) {
1332
- // fix-me
1333
- char s[48];
1334
- int n;
1335
-
1336
- if (NIL_P(fmt)) {
1337
- n = m_sprintf(s, *x);
1338
- return rb_str_new(s, n);
1339
- }
1340
- return rb_funcall(fmt, '%', 1, m_data_to_num(*x));
1341
- }
1342
-
1343
- static void iter_robject_format(na_loop_t* const lp) {
1344
- size_t i;
1345
- char *p1, *p2;
1346
- ssize_t s1, s2;
1347
- size_t* idx1;
1348
- dtype* x;
1349
- VALUE y;
1350
- VALUE fmt = lp->option;
1351
- INIT_COUNTER(lp, i);
1352
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
1353
- INIT_PTR(lp, 1, p2, s2);
1354
- if (idx1) {
1355
- for (; i--;) {
1356
- x = (dtype*)(p1 + *idx1);
1357
- idx1++;
1358
- y = format_robject(fmt, x);
1359
- SET_DATA_STRIDE(p2, s2, VALUE, y);
1360
- }
1361
- } else {
1362
- for (; i--;) {
1363
- x = (dtype*)p1;
1364
- p1 += s1;
1365
- y = format_robject(fmt, x);
1366
- SET_DATA_STRIDE(p2, s2, VALUE, y);
1367
- }
1368
- }
1369
- }
1370
-
1371
- /*
1372
- Format elements into strings.
1373
- @overload format format
1374
- @param [String] format
1375
- @return [Numo::RObject] array of formatted strings.
1376
- */
1377
- static VALUE robject_format(int argc, VALUE* argv, VALUE self) {
1378
- VALUE fmt = Qnil;
1379
-
1380
- ndfunc_arg_in_t ain[2] = { { Qnil, 0 }, { sym_option } };
1381
- ndfunc_arg_out_t aout[1] = { { numo_cRObject, 0 } };
1382
- ndfunc_t ndf = { iter_robject_format, FULL_LOOP_NIP, 2, 1, ain, aout };
1383
-
1384
- rb_scan_args(argc, argv, "01", &fmt);
1385
- return na_ndloop(&ndf, 2, self, fmt);
1386
- }
1387
-
1388
- static void iter_robject_format_to_a(na_loop_t* const lp) {
1389
- size_t i;
1390
- char* p1;
1391
- ssize_t s1;
1392
- size_t* idx1;
1393
- dtype* x;
1394
- VALUE y;
1395
- volatile VALUE a;
1396
- VALUE fmt = lp->option;
1397
- INIT_COUNTER(lp, i);
1398
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
1399
- a = rb_ary_new2(i);
1400
- rb_ary_push(lp->args[1].value, a);
1401
- if (idx1) {
1402
- for (; i--;) {
1403
- x = (dtype*)(p1 + *idx1);
1404
- idx1++;
1405
- y = format_robject(fmt, x);
1406
- rb_ary_push(a, y);
1407
- }
1408
- } else {
1409
- for (; i--;) {
1410
- x = (dtype*)p1;
1411
- p1 += s1;
1412
- y = format_robject(fmt, x);
1413
- rb_ary_push(a, y);
1414
- }
1415
- }
1416
- }
1417
-
1418
- /*
1419
- Format elements into strings.
1420
- @overload format_to_a format
1421
- @param [String] format
1422
- @return [Array] array of formatted strings.
1423
- */
1424
- static VALUE robject_format_to_a(int argc, VALUE* argv, VALUE self) {
1425
- VALUE fmt = Qnil;
1426
- ndfunc_arg_in_t ain[3] = { { Qnil, 0 }, { sym_loop_opt }, { sym_option } };
1427
- ndfunc_arg_out_t aout[1] = { { rb_cArray, 0 } }; // dummy?
1428
- ndfunc_t ndf = { iter_robject_format_to_a, FULL_LOOP_NIP, 3, 1, ain, aout };
1429
-
1430
- rb_scan_args(argc, argv, "01", &fmt);
1431
- return na_ndloop_cast_narray_to_rarray(&ndf, self, fmt);
1432
- }
1433
-
1434
- static VALUE iter_robject_inspect(char* ptr, size_t pos, VALUE fmt) {
1435
- return rb_inspect(*(VALUE*)(ptr + pos));
1436
- }
1437
-
1438
- /*
1439
- Returns a string containing a human-readable representation of NArray.
1440
- @overload inspect
1441
- @return [String]
1442
- */
1443
- static VALUE robject_inspect(VALUE ary) {
1444
- return na_ndloop_inspect(ary, iter_robject_inspect, Qnil);
1445
- }
1446
-
1447
1357
  static void iter_robject_each(na_loop_t* const lp) {
1448
1358
  size_t i, s1;
1449
1359
  char* p1;
@@ -1753,3439 +1663,296 @@ static VALUE robject_abs(VALUE self) {
1753
1663
  return na_ndloop(&ndf, 1, self);
1754
1664
  }
1755
1665
 
1756
- #define check_intdivzero(y) \
1757
- {}
1758
-
1759
- static void iter_robject_add(na_loop_t* const lp) {
1760
- size_t i = 0;
1761
- size_t n;
1666
+ static void iter_robject_pow(na_loop_t* const lp) {
1667
+ size_t i;
1762
1668
  char *p1, *p2, *p3;
1763
1669
  ssize_t s1, s2, s3;
1764
-
1765
- INIT_COUNTER(lp, n);
1670
+ dtype x, y;
1671
+ INIT_COUNTER(lp, i);
1766
1672
  INIT_PTR(lp, 0, p1, s1);
1767
1673
  INIT_PTR(lp, 1, p2, s2);
1768
1674
  INIT_PTR(lp, 2, p3, s3);
1769
-
1770
- //
1771
-
1772
- if (s2 == 0) { // Broadcasting from scalar value.
1773
- check_intdivzero(*(dtype*)p2);
1774
- if (s1 == sizeof(dtype) && s3 == sizeof(dtype)) {
1775
- if (p1 == p3) { // inplace case
1776
- for (; i < n; i++) {
1777
- ((dtype*)p1)[i] = m_add(((dtype*)p1)[i], *(dtype*)p2);
1778
- }
1779
- } else {
1780
- for (; i < n; i++) {
1781
- ((dtype*)p3)[i] = m_add(((dtype*)p1)[i], *(dtype*)p2);
1782
- }
1783
- }
1784
- } else {
1785
- for (i = 0; i < n; i++) {
1786
- *(dtype*)p3 = m_add(*(dtype*)p1, *(dtype*)p2);
1787
- p1 += s1;
1788
- p3 += s3;
1789
- }
1790
- }
1791
- } else {
1792
- if (p1 == p3) { // inplace case
1793
- for (i = 0; i < n; i++) {
1794
- check_intdivzero(*(dtype*)p2);
1795
- *(dtype*)p1 = m_add(*(dtype*)p1, *(dtype*)p2);
1796
- p1 += s1;
1797
- p2 += s2;
1798
- }
1799
- } else {
1800
- for (i = 0; i < n; i++) {
1801
- check_intdivzero(*(dtype*)p2);
1802
- *(dtype*)p3 = m_add(*(dtype*)p1, *(dtype*)p2);
1803
- p1 += s1;
1804
- p2 += s2;
1805
- p3 += s3;
1806
- }
1807
- }
1675
+ for (; i--;) {
1676
+ GET_DATA_STRIDE(p1, s1, dtype, x);
1677
+ GET_DATA_STRIDE(p2, s2, dtype, y);
1678
+ x = m_pow(x, y);
1679
+ SET_DATA_STRIDE(p3, s3, dtype, x);
1808
1680
  }
1681
+ }
1809
1682
 
1810
- return;
1811
- //
1683
+ static void iter_robject_pow_int32(na_loop_t* const lp) {
1684
+ size_t i;
1685
+ char *p1, *p2, *p3;
1686
+ ssize_t s1, s2, s3;
1687
+ dtype x;
1688
+ int32_t y;
1689
+ INIT_COUNTER(lp, i);
1690
+ INIT_PTR(lp, 0, p1, s1);
1691
+ INIT_PTR(lp, 1, p2, s2);
1692
+ INIT_PTR(lp, 2, p3, s3);
1693
+ for (; i--;) {
1694
+ GET_DATA_STRIDE(p1, s1, dtype, x);
1695
+ GET_DATA_STRIDE(p2, s2, int32_t, y);
1696
+ x = m_pow_int(x, y);
1697
+ SET_DATA_STRIDE(p3, s3, dtype, x);
1698
+ }
1812
1699
  }
1813
- #undef check_intdivzero
1814
1700
 
1815
- static VALUE robject_add_self(VALUE self, VALUE other) {
1701
+ static VALUE robject_pow_self(VALUE self, VALUE other) {
1816
1702
  ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
1703
+ ndfunc_arg_in_t ain_i[2] = { { cT, 0 }, { numo_cInt32, 0 } };
1817
1704
  ndfunc_arg_out_t aout[1] = { { cT, 0 } };
1818
- ndfunc_t ndf = { iter_robject_add, STRIDE_LOOP, 2, 1, ain, aout };
1705
+ ndfunc_t ndf = { iter_robject_pow, STRIDE_LOOP, 2, 1, ain, aout };
1706
+ ndfunc_t ndf_i = { iter_robject_pow_int32, STRIDE_LOOP, 2, 1, ain_i, aout };
1819
1707
 
1820
- return na_ndloop(&ndf, 2, self, other);
1708
+ // fixme : use na.integer?
1709
+ if (FIXNUM_P(other) || rb_obj_is_kind_of(other, numo_cInt32)) {
1710
+ return na_ndloop(&ndf_i, 2, self, other);
1711
+ } else {
1712
+ return na_ndloop(&ndf, 2, self, other);
1713
+ }
1821
1714
  }
1822
1715
 
1823
1716
  /*
1824
- Binary add.
1825
- @overload + other
1717
+ Binary power.
1718
+ @overload ** other
1826
1719
  @param [Numo::NArray,Numeric] other
1827
- @return [Numo::NArray] self + other
1720
+ @return [Numo::NArray] self to the other-th power.
1828
1721
  */
1829
- static VALUE robject_add(VALUE self, VALUE other) {
1830
- return robject_add_self(self, other);
1722
+ static VALUE robject_pow(VALUE self, VALUE other) {
1723
+ return robject_pow_self(self, other);
1831
1724
  }
1832
1725
 
1833
- #define check_intdivzero(y) \
1834
- {}
1835
-
1836
- static void iter_robject_sub(na_loop_t* const lp) {
1837
- size_t i = 0;
1838
- size_t n;
1839
- char *p1, *p2, *p3;
1840
- ssize_t s1, s2, s3;
1726
+ static void iter_robject_minus(na_loop_t* const lp) {
1727
+ size_t i, n;
1728
+ char *p1, *p2;
1729
+ ssize_t s1, s2;
1730
+ size_t *idx1, *idx2;
1731
+ dtype x;
1841
1732
 
1842
1733
  INIT_COUNTER(lp, n);
1843
- INIT_PTR(lp, 0, p1, s1);
1844
- INIT_PTR(lp, 1, p2, s2);
1845
- INIT_PTR(lp, 2, p3, s3);
1846
-
1847
- //
1734
+ INIT_PTR_IDX(lp, 0, p1, s1, idx1);
1735
+ INIT_PTR_IDX(lp, 1, p2, s2, idx2);
1848
1736
 
1849
- if (s2 == 0) { // Broadcasting from scalar value.
1850
- check_intdivzero(*(dtype*)p2);
1851
- if (s1 == sizeof(dtype) && s3 == sizeof(dtype)) {
1852
- if (p1 == p3) { // inplace case
1853
- for (; i < n; i++) {
1854
- ((dtype*)p1)[i] = m_sub(((dtype*)p1)[i], *(dtype*)p2);
1855
- }
1856
- } else {
1857
- for (; i < n; i++) {
1858
- ((dtype*)p3)[i] = m_sub(((dtype*)p1)[i], *(dtype*)p2);
1859
- }
1737
+ if (idx1) {
1738
+ if (idx2) {
1739
+ for (i = 0; i < n; i++) {
1740
+ GET_DATA_INDEX(p1, idx1, dtype, x);
1741
+ x = m_minus(x);
1742
+ SET_DATA_INDEX(p2, idx2, dtype, x);
1860
1743
  }
1861
1744
  } else {
1862
1745
  for (i = 0; i < n; i++) {
1863
- *(dtype*)p3 = m_sub(*(dtype*)p1, *(dtype*)p2);
1864
- p1 += s1;
1865
- p3 += s3;
1746
+ GET_DATA_INDEX(p1, idx1, dtype, x);
1747
+ x = m_minus(x);
1748
+ SET_DATA_STRIDE(p2, s2, dtype, x);
1866
1749
  }
1867
1750
  }
1868
1751
  } else {
1869
- if (p1 == p3) { // inplace case
1752
+ if (idx2) {
1870
1753
  for (i = 0; i < n; i++) {
1871
- check_intdivzero(*(dtype*)p2);
1872
- *(dtype*)p1 = m_sub(*(dtype*)p1, *(dtype*)p2);
1873
- p1 += s1;
1874
- p2 += s2;
1754
+ GET_DATA_STRIDE(p1, s1, dtype, x);
1755
+ x = m_minus(x);
1756
+ SET_DATA_INDEX(p2, idx2, dtype, x);
1875
1757
  }
1876
1758
  } else {
1759
+ //
1877
1760
  for (i = 0; i < n; i++) {
1878
- check_intdivzero(*(dtype*)p2);
1879
- *(dtype*)p3 = m_sub(*(dtype*)p1, *(dtype*)p2);
1761
+ *(dtype*)p2 = m_minus(*(dtype*)p1);
1880
1762
  p1 += s1;
1881
1763
  p2 += s2;
1882
- p3 += s3;
1883
1764
  }
1765
+ return;
1766
+ //
1884
1767
  }
1885
1768
  }
1886
-
1887
- return;
1888
- //
1889
- }
1890
- #undef check_intdivzero
1891
-
1892
- static VALUE robject_sub_self(VALUE self, VALUE other) {
1893
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
1894
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
1895
- ndfunc_t ndf = { iter_robject_sub, STRIDE_LOOP, 2, 1, ain, aout };
1896
-
1897
- return na_ndloop(&ndf, 2, self, other);
1898
1769
  }
1899
1770
 
1900
1771
  /*
1901
- Binary sub.
1902
- @overload - other
1903
- @param [Numo::NArray,Numeric] other
1904
- @return [Numo::NArray] self - other
1772
+ Unary minus.
1773
+ @overload -@
1774
+ @return [Numo::RObject] minus of self.
1905
1775
  */
1906
- static VALUE robject_sub(VALUE self, VALUE other) {
1907
- return robject_sub_self(self, other);
1908
- }
1776
+ static VALUE robject_minus(VALUE self) {
1777
+ ndfunc_arg_in_t ain[1] = { { cT, 0 } };
1778
+ ndfunc_arg_out_t aout[1] = { { cT, 0 } };
1779
+ ndfunc_t ndf = { iter_robject_minus, FULL_LOOP, 1, 1, ain, aout };
1909
1780
 
1910
- #define check_intdivzero(y) \
1911
- {}
1781
+ return na_ndloop(&ndf, 1, self);
1782
+ }
1912
1783
 
1913
- static void iter_robject_mul(na_loop_t* const lp) {
1914
- size_t i = 0;
1915
- size_t n;
1916
- char *p1, *p2, *p3;
1917
- ssize_t s1, s2, s3;
1784
+ static void iter_robject_reciprocal(na_loop_t* const lp) {
1785
+ size_t i, n;
1786
+ char *p1, *p2;
1787
+ ssize_t s1, s2;
1788
+ size_t *idx1, *idx2;
1789
+ dtype x;
1918
1790
 
1919
1791
  INIT_COUNTER(lp, n);
1920
- INIT_PTR(lp, 0, p1, s1);
1921
- INIT_PTR(lp, 1, p2, s2);
1922
- INIT_PTR(lp, 2, p3, s3);
1923
-
1924
- //
1792
+ INIT_PTR_IDX(lp, 0, p1, s1, idx1);
1793
+ INIT_PTR_IDX(lp, 1, p2, s2, idx2);
1925
1794
 
1926
- if (s2 == 0) { // Broadcasting from scalar value.
1927
- check_intdivzero(*(dtype*)p2);
1928
- if (s1 == sizeof(dtype) && s3 == sizeof(dtype)) {
1929
- if (p1 == p3) { // inplace case
1930
- for (; i < n; i++) {
1931
- ((dtype*)p1)[i] = m_mul(((dtype*)p1)[i], *(dtype*)p2);
1932
- }
1933
- } else {
1934
- for (; i < n; i++) {
1935
- ((dtype*)p3)[i] = m_mul(((dtype*)p1)[i], *(dtype*)p2);
1936
- }
1795
+ if (idx1) {
1796
+ if (idx2) {
1797
+ for (i = 0; i < n; i++) {
1798
+ GET_DATA_INDEX(p1, idx1, dtype, x);
1799
+ x = m_reciprocal(x);
1800
+ SET_DATA_INDEX(p2, idx2, dtype, x);
1937
1801
  }
1938
1802
  } else {
1939
1803
  for (i = 0; i < n; i++) {
1940
- *(dtype*)p3 = m_mul(*(dtype*)p1, *(dtype*)p2);
1941
- p1 += s1;
1942
- p3 += s3;
1804
+ GET_DATA_INDEX(p1, idx1, dtype, x);
1805
+ x = m_reciprocal(x);
1806
+ SET_DATA_STRIDE(p2, s2, dtype, x);
1943
1807
  }
1944
1808
  }
1945
1809
  } else {
1946
- if (p1 == p3) { // inplace case
1810
+ if (idx2) {
1947
1811
  for (i = 0; i < n; i++) {
1948
- check_intdivzero(*(dtype*)p2);
1949
- *(dtype*)p1 = m_mul(*(dtype*)p1, *(dtype*)p2);
1950
- p1 += s1;
1951
- p2 += s2;
1812
+ GET_DATA_STRIDE(p1, s1, dtype, x);
1813
+ x = m_reciprocal(x);
1814
+ SET_DATA_INDEX(p2, idx2, dtype, x);
1952
1815
  }
1953
1816
  } else {
1817
+ //
1954
1818
  for (i = 0; i < n; i++) {
1955
- check_intdivzero(*(dtype*)p2);
1956
- *(dtype*)p3 = m_mul(*(dtype*)p1, *(dtype*)p2);
1957
- p1 += s1;
1958
- p2 += s2;
1959
- p3 += s3;
1960
- }
1961
- }
1962
- }
1963
-
1964
- return;
1965
- //
1966
- }
1967
- #undef check_intdivzero
1968
-
1969
- static VALUE robject_mul_self(VALUE self, VALUE other) {
1970
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
1971
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
1972
- ndfunc_t ndf = { iter_robject_mul, STRIDE_LOOP, 2, 1, ain, aout };
1973
-
1974
- return na_ndloop(&ndf, 2, self, other);
1975
- }
1976
-
1977
- /*
1978
- Binary mul.
1979
- @overload * other
1980
- @param [Numo::NArray,Numeric] other
1981
- @return [Numo::NArray] self * other
1982
- */
1983
- static VALUE robject_mul(VALUE self, VALUE other) {
1984
- return robject_mul_self(self, other);
1985
- }
1986
-
1987
- #define check_intdivzero(y) \
1988
- if ((y) == 0) { \
1989
- lp->err_type = rb_eZeroDivError; \
1990
- return; \
1991
- }
1992
-
1993
- static void iter_robject_div(na_loop_t* const lp) {
1994
- size_t i = 0;
1995
- size_t n;
1996
- char *p1, *p2, *p3;
1997
- ssize_t s1, s2, s3;
1998
-
1999
- INIT_COUNTER(lp, n);
2000
- INIT_PTR(lp, 0, p1, s1);
2001
- INIT_PTR(lp, 1, p2, s2);
2002
- INIT_PTR(lp, 2, p3, s3);
2003
-
2004
- //
2005
-
2006
- if (s2 == 0) { // Broadcasting from scalar value.
2007
- check_intdivzero(*(dtype*)p2);
2008
- if (s1 == sizeof(dtype) && s3 == sizeof(dtype)) {
2009
- if (p1 == p3) { // inplace case
2010
- for (; i < n; i++) {
2011
- ((dtype*)p1)[i] = m_div(((dtype*)p1)[i], *(dtype*)p2);
2012
- }
2013
- } else {
2014
- for (; i < n; i++) {
2015
- ((dtype*)p3)[i] = m_div(((dtype*)p1)[i], *(dtype*)p2);
2016
- }
2017
- }
2018
- } else {
2019
- for (i = 0; i < n; i++) {
2020
- *(dtype*)p3 = m_div(*(dtype*)p1, *(dtype*)p2);
2021
- p1 += s1;
2022
- p3 += s3;
2023
- }
2024
- }
2025
- } else {
2026
- if (p1 == p3) { // inplace case
2027
- for (i = 0; i < n; i++) {
2028
- check_intdivzero(*(dtype*)p2);
2029
- *(dtype*)p1 = m_div(*(dtype*)p1, *(dtype*)p2);
2030
- p1 += s1;
2031
- p2 += s2;
2032
- }
2033
- } else {
2034
- for (i = 0; i < n; i++) {
2035
- check_intdivzero(*(dtype*)p2);
2036
- *(dtype*)p3 = m_div(*(dtype*)p1, *(dtype*)p2);
2037
- p1 += s1;
2038
- p2 += s2;
2039
- p3 += s3;
2040
- }
2041
- }
2042
- }
2043
-
2044
- return;
2045
- //
2046
- }
2047
- #undef check_intdivzero
2048
-
2049
- static VALUE robject_div_self(VALUE self, VALUE other) {
2050
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
2051
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
2052
- ndfunc_t ndf = { iter_robject_div, STRIDE_LOOP, 2, 1, ain, aout };
2053
-
2054
- return na_ndloop(&ndf, 2, self, other);
2055
- }
2056
-
2057
- /*
2058
- Binary div.
2059
- @overload / other
2060
- @param [Numo::NArray,Numeric] other
2061
- @return [Numo::NArray] self / other
2062
- */
2063
- static VALUE robject_div(VALUE self, VALUE other) {
2064
- return robject_div_self(self, other);
2065
- }
2066
-
2067
- #define check_intdivzero(y) \
2068
- if ((y) == 0) { \
2069
- lp->err_type = rb_eZeroDivError; \
2070
- return; \
2071
- }
2072
-
2073
- static void iter_robject_mod(na_loop_t* const lp) {
2074
- size_t i = 0;
2075
- size_t n;
2076
- char *p1, *p2, *p3;
2077
- ssize_t s1, s2, s3;
2078
-
2079
- INIT_COUNTER(lp, n);
2080
- INIT_PTR(lp, 0, p1, s1);
2081
- INIT_PTR(lp, 1, p2, s2);
2082
- INIT_PTR(lp, 2, p3, s3);
2083
-
2084
- //
2085
-
2086
- if (s2 == 0) { // Broadcasting from scalar value.
2087
- check_intdivzero(*(dtype*)p2);
2088
- if (s1 == sizeof(dtype) && s3 == sizeof(dtype)) {
2089
- if (p1 == p3) { // inplace case
2090
- for (; i < n; i++) {
2091
- ((dtype*)p1)[i] = m_mod(((dtype*)p1)[i], *(dtype*)p2);
2092
- }
2093
- } else {
2094
- for (; i < n; i++) {
2095
- ((dtype*)p3)[i] = m_mod(((dtype*)p1)[i], *(dtype*)p2);
2096
- }
2097
- }
2098
- } else {
2099
- for (i = 0; i < n; i++) {
2100
- *(dtype*)p3 = m_mod(*(dtype*)p1, *(dtype*)p2);
2101
- p1 += s1;
2102
- p3 += s3;
2103
- }
2104
- }
2105
- } else {
2106
- if (p1 == p3) { // inplace case
2107
- for (i = 0; i < n; i++) {
2108
- check_intdivzero(*(dtype*)p2);
2109
- *(dtype*)p1 = m_mod(*(dtype*)p1, *(dtype*)p2);
2110
- p1 += s1;
2111
- p2 += s2;
2112
- }
2113
- } else {
2114
- for (i = 0; i < n; i++) {
2115
- check_intdivzero(*(dtype*)p2);
2116
- *(dtype*)p3 = m_mod(*(dtype*)p1, *(dtype*)p2);
2117
- p1 += s1;
2118
- p2 += s2;
2119
- p3 += s3;
2120
- }
2121
- }
2122
- }
2123
-
2124
- return;
2125
- //
2126
- }
2127
- #undef check_intdivzero
2128
-
2129
- static VALUE robject_mod_self(VALUE self, VALUE other) {
2130
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
2131
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
2132
- ndfunc_t ndf = { iter_robject_mod, STRIDE_LOOP, 2, 1, ain, aout };
2133
-
2134
- return na_ndloop(&ndf, 2, self, other);
2135
- }
2136
-
2137
- /*
2138
- Binary mod.
2139
- @overload % other
2140
- @param [Numo::NArray,Numeric] other
2141
- @return [Numo::NArray] self % other
2142
- */
2143
- static VALUE robject_mod(VALUE self, VALUE other) {
2144
- return robject_mod_self(self, other);
2145
- }
2146
-
2147
- static void iter_robject_divmod(na_loop_t* const lp) {
2148
- size_t i, n;
2149
- char *p1, *p2, *p3, *p4;
2150
- ssize_t s1, s2, s3, s4;
2151
- dtype x, y, a, b;
2152
- INIT_COUNTER(lp, n);
2153
- INIT_PTR(lp, 0, p1, s1);
2154
- INIT_PTR(lp, 1, p2, s2);
2155
- INIT_PTR(lp, 2, p3, s3);
2156
- INIT_PTR(lp, 3, p4, s4);
2157
- for (i = n; i--;) {
2158
- GET_DATA_STRIDE(p1, s1, dtype, x);
2159
- GET_DATA_STRIDE(p2, s2, dtype, y);
2160
- if (y == 0) {
2161
- lp->err_type = rb_eZeroDivError;
2162
- return;
2163
- }
2164
- m_divmod(x, y, a, b);
2165
- SET_DATA_STRIDE(p3, s3, dtype, a);
2166
- SET_DATA_STRIDE(p4, s4, dtype, b);
2167
- }
2168
- }
2169
-
2170
- static VALUE robject_divmod_self(VALUE self, VALUE other) {
2171
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
2172
- ndfunc_arg_out_t aout[2] = { { cT, 0 }, { cT, 0 } };
2173
- ndfunc_t ndf = { iter_robject_divmod, STRIDE_LOOP, 2, 2, ain, aout };
2174
-
2175
- return na_ndloop(&ndf, 2, self, other);
2176
- }
2177
-
2178
- /*
2179
- Binary divmod.
2180
- @overload divmod other
2181
- @param [Numo::NArray,Numeric] other
2182
- @return [Numo::NArray] divmod of self and other.
2183
- */
2184
- static VALUE robject_divmod(VALUE self, VALUE other) {
2185
- return robject_divmod_self(self, other);
2186
- }
2187
-
2188
- static void iter_robject_pow(na_loop_t* const lp) {
2189
- size_t i;
2190
- char *p1, *p2, *p3;
2191
- ssize_t s1, s2, s3;
2192
- dtype x, y;
2193
- INIT_COUNTER(lp, i);
2194
- INIT_PTR(lp, 0, p1, s1);
2195
- INIT_PTR(lp, 1, p2, s2);
2196
- INIT_PTR(lp, 2, p3, s3);
2197
- for (; i--;) {
2198
- GET_DATA_STRIDE(p1, s1, dtype, x);
2199
- GET_DATA_STRIDE(p2, s2, dtype, y);
2200
- x = m_pow(x, y);
2201
- SET_DATA_STRIDE(p3, s3, dtype, x);
2202
- }
2203
- }
2204
-
2205
- static void iter_robject_pow_int32(na_loop_t* const lp) {
2206
- size_t i;
2207
- char *p1, *p2, *p3;
2208
- ssize_t s1, s2, s3;
2209
- dtype x;
2210
- int32_t y;
2211
- INIT_COUNTER(lp, i);
2212
- INIT_PTR(lp, 0, p1, s1);
2213
- INIT_PTR(lp, 1, p2, s2);
2214
- INIT_PTR(lp, 2, p3, s3);
2215
- for (; i--;) {
2216
- GET_DATA_STRIDE(p1, s1, dtype, x);
2217
- GET_DATA_STRIDE(p2, s2, int32_t, y);
2218
- x = m_pow_int(x, y);
2219
- SET_DATA_STRIDE(p3, s3, dtype, x);
2220
- }
2221
- }
2222
-
2223
- static VALUE robject_pow_self(VALUE self, VALUE other) {
2224
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
2225
- ndfunc_arg_in_t ain_i[2] = { { cT, 0 }, { numo_cInt32, 0 } };
2226
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
2227
- ndfunc_t ndf = { iter_robject_pow, STRIDE_LOOP, 2, 1, ain, aout };
2228
- ndfunc_t ndf_i = { iter_robject_pow_int32, STRIDE_LOOP, 2, 1, ain_i, aout };
2229
-
2230
- // fixme : use na.integer?
2231
- if (FIXNUM_P(other) || rb_obj_is_kind_of(other, numo_cInt32)) {
2232
- return na_ndloop(&ndf_i, 2, self, other);
2233
- } else {
2234
- return na_ndloop(&ndf, 2, self, other);
2235
- }
2236
- }
2237
-
2238
- /*
2239
- Binary power.
2240
- @overload ** other
2241
- @param [Numo::NArray,Numeric] other
2242
- @return [Numo::NArray] self to the other-th power.
2243
- */
2244
- static VALUE robject_pow(VALUE self, VALUE other) {
2245
- return robject_pow_self(self, other);
2246
- }
2247
-
2248
- static void iter_robject_minus(na_loop_t* const lp) {
2249
- size_t i, n;
2250
- char *p1, *p2;
2251
- ssize_t s1, s2;
2252
- size_t *idx1, *idx2;
2253
- dtype x;
2254
-
2255
- INIT_COUNTER(lp, n);
2256
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
2257
- INIT_PTR_IDX(lp, 1, p2, s2, idx2);
2258
-
2259
- if (idx1) {
2260
- if (idx2) {
2261
- for (i = 0; i < n; i++) {
2262
- GET_DATA_INDEX(p1, idx1, dtype, x);
2263
- x = m_minus(x);
2264
- SET_DATA_INDEX(p2, idx2, dtype, x);
2265
- }
2266
- } else {
2267
- for (i = 0; i < n; i++) {
2268
- GET_DATA_INDEX(p1, idx1, dtype, x);
2269
- x = m_minus(x);
2270
- SET_DATA_STRIDE(p2, s2, dtype, x);
2271
- }
2272
- }
2273
- } else {
2274
- if (idx2) {
2275
- for (i = 0; i < n; i++) {
2276
- GET_DATA_STRIDE(p1, s1, dtype, x);
2277
- x = m_minus(x);
2278
- SET_DATA_INDEX(p2, idx2, dtype, x);
2279
- }
2280
- } else {
2281
- //
2282
- for (i = 0; i < n; i++) {
2283
- *(dtype*)p2 = m_minus(*(dtype*)p1);
2284
- p1 += s1;
2285
- p2 += s2;
2286
- }
2287
- return;
2288
- //
2289
- }
2290
- }
2291
- }
2292
-
2293
- /*
2294
- Unary minus.
2295
- @overload -@
2296
- @return [Numo::RObject] minus of self.
2297
- */
2298
- static VALUE robject_minus(VALUE self) {
2299
- ndfunc_arg_in_t ain[1] = { { cT, 0 } };
2300
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
2301
- ndfunc_t ndf = { iter_robject_minus, FULL_LOOP, 1, 1, ain, aout };
2302
-
2303
- return na_ndloop(&ndf, 1, self);
2304
- }
2305
-
2306
- static void iter_robject_reciprocal(na_loop_t* const lp) {
2307
- size_t i, n;
2308
- char *p1, *p2;
2309
- ssize_t s1, s2;
2310
- size_t *idx1, *idx2;
2311
- dtype x;
2312
-
2313
- INIT_COUNTER(lp, n);
2314
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
2315
- INIT_PTR_IDX(lp, 1, p2, s2, idx2);
2316
-
2317
- if (idx1) {
2318
- if (idx2) {
2319
- for (i = 0; i < n; i++) {
2320
- GET_DATA_INDEX(p1, idx1, dtype, x);
2321
- x = m_reciprocal(x);
2322
- SET_DATA_INDEX(p2, idx2, dtype, x);
2323
- }
2324
- } else {
2325
- for (i = 0; i < n; i++) {
2326
- GET_DATA_INDEX(p1, idx1, dtype, x);
2327
- x = m_reciprocal(x);
2328
- SET_DATA_STRIDE(p2, s2, dtype, x);
2329
- }
2330
- }
2331
- } else {
2332
- if (idx2) {
2333
- for (i = 0; i < n; i++) {
2334
- GET_DATA_STRIDE(p1, s1, dtype, x);
2335
- x = m_reciprocal(x);
2336
- SET_DATA_INDEX(p2, idx2, dtype, x);
2337
- }
2338
- } else {
2339
- //
2340
- for (i = 0; i < n; i++) {
2341
- *(dtype*)p2 = m_reciprocal(*(dtype*)p1);
2342
- p1 += s1;
2343
- p2 += s2;
2344
- }
2345
- return;
2346
- //
2347
- }
2348
- }
2349
- }
2350
-
2351
- /*
2352
- Unary reciprocal.
2353
- @overload reciprocal
2354
- @return [Numo::RObject] reciprocal of self.
2355
- */
2356
- static VALUE robject_reciprocal(VALUE self) {
2357
- ndfunc_arg_in_t ain[1] = { { cT, 0 } };
2358
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
2359
- ndfunc_t ndf = { iter_robject_reciprocal, FULL_LOOP, 1, 1, ain, aout };
2360
-
2361
- return na_ndloop(&ndf, 1, self);
2362
- }
2363
-
2364
- static void iter_robject_sign(na_loop_t* const lp) {
2365
- size_t i, n;
2366
- char *p1, *p2;
2367
- ssize_t s1, s2;
2368
- size_t *idx1, *idx2;
2369
- dtype x;
2370
-
2371
- INIT_COUNTER(lp, n);
2372
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
2373
- INIT_PTR_IDX(lp, 1, p2, s2, idx2);
2374
-
2375
- if (idx1) {
2376
- if (idx2) {
2377
- for (i = 0; i < n; i++) {
2378
- GET_DATA_INDEX(p1, idx1, dtype, x);
2379
- x = m_sign(x);
2380
- SET_DATA_INDEX(p2, idx2, dtype, x);
2381
- }
2382
- } else {
2383
- for (i = 0; i < n; i++) {
2384
- GET_DATA_INDEX(p1, idx1, dtype, x);
2385
- x = m_sign(x);
2386
- SET_DATA_STRIDE(p2, s2, dtype, x);
2387
- }
2388
- }
2389
- } else {
2390
- if (idx2) {
2391
- for (i = 0; i < n; i++) {
2392
- GET_DATA_STRIDE(p1, s1, dtype, x);
2393
- x = m_sign(x);
2394
- SET_DATA_INDEX(p2, idx2, dtype, x);
2395
- }
2396
- } else {
2397
- //
2398
- for (i = 0; i < n; i++) {
2399
- *(dtype*)p2 = m_sign(*(dtype*)p1);
2400
- p1 += s1;
2401
- p2 += s2;
2402
- }
2403
- return;
2404
- //
2405
- }
2406
- }
2407
- }
2408
-
2409
- /*
2410
- Unary sign.
2411
- @overload sign
2412
- @return [Numo::RObject] sign of self.
2413
- */
2414
- static VALUE robject_sign(VALUE self) {
2415
- ndfunc_arg_in_t ain[1] = { { cT, 0 } };
2416
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
2417
- ndfunc_t ndf = { iter_robject_sign, FULL_LOOP, 1, 1, ain, aout };
2418
-
2419
- return na_ndloop(&ndf, 1, self);
2420
- }
2421
-
2422
- static void iter_robject_square(na_loop_t* const lp) {
2423
- size_t i, n;
2424
- char *p1, *p2;
2425
- ssize_t s1, s2;
2426
- size_t *idx1, *idx2;
2427
- dtype x;
2428
-
2429
- INIT_COUNTER(lp, n);
2430
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
2431
- INIT_PTR_IDX(lp, 1, p2, s2, idx2);
2432
-
2433
- if (idx1) {
2434
- if (idx2) {
2435
- for (i = 0; i < n; i++) {
2436
- GET_DATA_INDEX(p1, idx1, dtype, x);
2437
- x = m_square(x);
2438
- SET_DATA_INDEX(p2, idx2, dtype, x);
2439
- }
2440
- } else {
2441
- for (i = 0; i < n; i++) {
2442
- GET_DATA_INDEX(p1, idx1, dtype, x);
2443
- x = m_square(x);
2444
- SET_DATA_STRIDE(p2, s2, dtype, x);
2445
- }
2446
- }
2447
- } else {
2448
- if (idx2) {
2449
- for (i = 0; i < n; i++) {
2450
- GET_DATA_STRIDE(p1, s1, dtype, x);
2451
- x = m_square(x);
2452
- SET_DATA_INDEX(p2, idx2, dtype, x);
2453
- }
2454
- } else {
2455
- //
2456
- for (i = 0; i < n; i++) {
2457
- *(dtype*)p2 = m_square(*(dtype*)p1);
2458
- p1 += s1;
2459
- p2 += s2;
2460
- }
2461
- return;
2462
- //
2463
- }
2464
- }
2465
- }
2466
-
2467
- /*
2468
- Unary square.
2469
- @overload square
2470
- @return [Numo::RObject] square of self.
2471
- */
2472
- static VALUE robject_square(VALUE self) {
2473
- ndfunc_arg_in_t ain[1] = { { cT, 0 } };
2474
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
2475
- ndfunc_t ndf = { iter_robject_square, FULL_LOOP, 1, 1, ain, aout };
2476
-
2477
- return na_ndloop(&ndf, 1, self);
2478
- }
2479
-
2480
- static void iter_robject_eq(na_loop_t* const lp) {
2481
- size_t i;
2482
- char *p1, *p2;
2483
- BIT_DIGIT* a3;
2484
- size_t p3;
2485
- ssize_t s1, s2, s3;
2486
- dtype x, y;
2487
- BIT_DIGIT b;
2488
- INIT_COUNTER(lp, i);
2489
- INIT_PTR(lp, 0, p1, s1);
2490
- INIT_PTR(lp, 1, p2, s2);
2491
- INIT_PTR_BIT(lp, 2, a3, p3, s3);
2492
- for (; i--;) {
2493
- GET_DATA_STRIDE(p1, s1, dtype, x);
2494
- GET_DATA_STRIDE(p2, s2, dtype, y);
2495
- b = (m_eq(x, y)) ? 1 : 0;
2496
- STORE_BIT(a3, p3, b);
2497
- p3 += s3;
2498
- }
2499
- }
2500
-
2501
- static VALUE robject_eq_self(VALUE self, VALUE other) {
2502
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
2503
- ndfunc_arg_out_t aout[1] = { { numo_cBit, 0 } };
2504
- ndfunc_t ndf = { iter_robject_eq, STRIDE_LOOP, 2, 1, ain, aout };
2505
-
2506
- return na_ndloop(&ndf, 2, self, other);
2507
- }
2508
-
2509
- /*
2510
- Comparison eq other.
2511
- @overload eq other
2512
- @param [Numo::NArray,Numeric] other
2513
- @return [Numo::Bit] result of self eq other.
2514
- */
2515
- static VALUE robject_eq(VALUE self, VALUE other) {
2516
- return robject_eq_self(self, other);
2517
- }
2518
-
2519
- static void iter_robject_ne(na_loop_t* const lp) {
2520
- size_t i;
2521
- char *p1, *p2;
2522
- BIT_DIGIT* a3;
2523
- size_t p3;
2524
- ssize_t s1, s2, s3;
2525
- dtype x, y;
2526
- BIT_DIGIT b;
2527
- INIT_COUNTER(lp, i);
2528
- INIT_PTR(lp, 0, p1, s1);
2529
- INIT_PTR(lp, 1, p2, s2);
2530
- INIT_PTR_BIT(lp, 2, a3, p3, s3);
2531
- for (; i--;) {
2532
- GET_DATA_STRIDE(p1, s1, dtype, x);
2533
- GET_DATA_STRIDE(p2, s2, dtype, y);
2534
- b = (m_ne(x, y)) ? 1 : 0;
2535
- STORE_BIT(a3, p3, b);
2536
- p3 += s3;
2537
- }
2538
- }
2539
-
2540
- static VALUE robject_ne_self(VALUE self, VALUE other) {
2541
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
2542
- ndfunc_arg_out_t aout[1] = { { numo_cBit, 0 } };
2543
- ndfunc_t ndf = { iter_robject_ne, STRIDE_LOOP, 2, 1, ain, aout };
2544
-
2545
- return na_ndloop(&ndf, 2, self, other);
2546
- }
2547
-
2548
- /*
2549
- Comparison ne other.
2550
- @overload ne other
2551
- @param [Numo::NArray,Numeric] other
2552
- @return [Numo::Bit] result of self ne other.
2553
- */
2554
- static VALUE robject_ne(VALUE self, VALUE other) {
2555
- return robject_ne_self(self, other);
2556
- }
2557
-
2558
- static void iter_robject_nearly_eq(na_loop_t* const lp) {
2559
- size_t i;
2560
- char *p1, *p2;
2561
- BIT_DIGIT* a3;
2562
- size_t p3;
2563
- ssize_t s1, s2, s3;
2564
- dtype x, y;
2565
- BIT_DIGIT b;
2566
- INIT_COUNTER(lp, i);
2567
- INIT_PTR(lp, 0, p1, s1);
2568
- INIT_PTR(lp, 1, p2, s2);
2569
- INIT_PTR_BIT(lp, 2, a3, p3, s3);
2570
- for (; i--;) {
2571
- GET_DATA_STRIDE(p1, s1, dtype, x);
2572
- GET_DATA_STRIDE(p2, s2, dtype, y);
2573
- b = (m_nearly_eq(x, y)) ? 1 : 0;
2574
- STORE_BIT(a3, p3, b);
2575
- p3 += s3;
2576
- }
2577
- }
2578
-
2579
- static VALUE robject_nearly_eq_self(VALUE self, VALUE other) {
2580
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
2581
- ndfunc_arg_out_t aout[1] = { { numo_cBit, 0 } };
2582
- ndfunc_t ndf = { iter_robject_nearly_eq, STRIDE_LOOP, 2, 1, ain, aout };
2583
-
2584
- return na_ndloop(&ndf, 2, self, other);
2585
- }
2586
-
2587
- /*
2588
- Comparison nearly_eq other.
2589
- @overload nearly_eq other
2590
- @param [Numo::NArray,Numeric] other
2591
- @return [Numo::Bit] result of self nearly_eq other.
2592
- */
2593
- static VALUE robject_nearly_eq(VALUE self, VALUE other) {
2594
- return robject_nearly_eq_self(self, other);
2595
- }
2596
-
2597
- #define check_intdivzero(y) \
2598
- {}
2599
-
2600
- static void iter_robject_bit_and(na_loop_t* const lp) {
2601
- size_t i = 0;
2602
- size_t n;
2603
- char *p1, *p2, *p3;
2604
- ssize_t s1, s2, s3;
2605
-
2606
- INIT_COUNTER(lp, n);
2607
- INIT_PTR(lp, 0, p1, s1);
2608
- INIT_PTR(lp, 1, p2, s2);
2609
- INIT_PTR(lp, 2, p3, s3);
2610
-
2611
- //
2612
-
2613
- if (s2 == 0) { // Broadcasting from scalar value.
2614
- check_intdivzero(*(dtype*)p2);
2615
- if (s1 == sizeof(dtype) && s3 == sizeof(dtype)) {
2616
- if (p1 == p3) { // inplace case
2617
- for (; i < n; i++) {
2618
- ((dtype*)p1)[i] = m_bit_and(((dtype*)p1)[i], *(dtype*)p2);
2619
- }
2620
- } else {
2621
- for (; i < n; i++) {
2622
- ((dtype*)p3)[i] = m_bit_and(((dtype*)p1)[i], *(dtype*)p2);
2623
- }
2624
- }
2625
- } else {
2626
- for (i = 0; i < n; i++) {
2627
- *(dtype*)p3 = m_bit_and(*(dtype*)p1, *(dtype*)p2);
2628
- p1 += s1;
2629
- p3 += s3;
2630
- }
2631
- }
2632
- } else {
2633
- if (p1 == p3) { // inplace case
2634
- for (i = 0; i < n; i++) {
2635
- check_intdivzero(*(dtype*)p2);
2636
- *(dtype*)p1 = m_bit_and(*(dtype*)p1, *(dtype*)p2);
2637
- p1 += s1;
2638
- p2 += s2;
2639
- }
2640
- } else {
2641
- for (i = 0; i < n; i++) {
2642
- check_intdivzero(*(dtype*)p2);
2643
- *(dtype*)p3 = m_bit_and(*(dtype*)p1, *(dtype*)p2);
2644
- p1 += s1;
2645
- p2 += s2;
2646
- p3 += s3;
2647
- }
2648
- }
2649
- }
2650
-
2651
- return;
2652
- //
2653
- }
2654
- #undef check_intdivzero
2655
-
2656
- static VALUE robject_bit_and_self(VALUE self, VALUE other) {
2657
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
2658
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
2659
- ndfunc_t ndf = { iter_robject_bit_and, STRIDE_LOOP, 2, 1, ain, aout };
2660
-
2661
- return na_ndloop(&ndf, 2, self, other);
2662
- }
2663
-
2664
- /*
2665
- Binary bit_and.
2666
- @overload & other
2667
- @param [Numo::NArray,Numeric] other
2668
- @return [Numo::NArray] self & other
2669
- */
2670
- static VALUE robject_bit_and(VALUE self, VALUE other) {
2671
- return robject_bit_and_self(self, other);
2672
- }
2673
-
2674
- #define check_intdivzero(y) \
2675
- {}
2676
-
2677
- static void iter_robject_bit_or(na_loop_t* const lp) {
2678
- size_t i = 0;
2679
- size_t n;
2680
- char *p1, *p2, *p3;
2681
- ssize_t s1, s2, s3;
2682
-
2683
- INIT_COUNTER(lp, n);
2684
- INIT_PTR(lp, 0, p1, s1);
2685
- INIT_PTR(lp, 1, p2, s2);
2686
- INIT_PTR(lp, 2, p3, s3);
2687
-
2688
- //
2689
-
2690
- if (s2 == 0) { // Broadcasting from scalar value.
2691
- check_intdivzero(*(dtype*)p2);
2692
- if (s1 == sizeof(dtype) && s3 == sizeof(dtype)) {
2693
- if (p1 == p3) { // inplace case
2694
- for (; i < n; i++) {
2695
- ((dtype*)p1)[i] = m_bit_or(((dtype*)p1)[i], *(dtype*)p2);
2696
- }
2697
- } else {
2698
- for (; i < n; i++) {
2699
- ((dtype*)p3)[i] = m_bit_or(((dtype*)p1)[i], *(dtype*)p2);
2700
- }
2701
- }
2702
- } else {
2703
- for (i = 0; i < n; i++) {
2704
- *(dtype*)p3 = m_bit_or(*(dtype*)p1, *(dtype*)p2);
2705
- p1 += s1;
2706
- p3 += s3;
2707
- }
2708
- }
2709
- } else {
2710
- if (p1 == p3) { // inplace case
2711
- for (i = 0; i < n; i++) {
2712
- check_intdivzero(*(dtype*)p2);
2713
- *(dtype*)p1 = m_bit_or(*(dtype*)p1, *(dtype*)p2);
2714
- p1 += s1;
2715
- p2 += s2;
2716
- }
2717
- } else {
2718
- for (i = 0; i < n; i++) {
2719
- check_intdivzero(*(dtype*)p2);
2720
- *(dtype*)p3 = m_bit_or(*(dtype*)p1, *(dtype*)p2);
2721
- p1 += s1;
2722
- p2 += s2;
2723
- p3 += s3;
2724
- }
2725
- }
2726
- }
2727
-
2728
- return;
2729
- //
2730
- }
2731
- #undef check_intdivzero
2732
-
2733
- static VALUE robject_bit_or_self(VALUE self, VALUE other) {
2734
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
2735
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
2736
- ndfunc_t ndf = { iter_robject_bit_or, STRIDE_LOOP, 2, 1, ain, aout };
2737
-
2738
- return na_ndloop(&ndf, 2, self, other);
2739
- }
2740
-
2741
- /*
2742
- Binary bit_or.
2743
- @overload | other
2744
- @param [Numo::NArray,Numeric] other
2745
- @return [Numo::NArray] self | other
2746
- */
2747
- static VALUE robject_bit_or(VALUE self, VALUE other) {
2748
- return robject_bit_or_self(self, other);
2749
- }
2750
-
2751
- #define check_intdivzero(y) \
2752
- {}
2753
-
2754
- static void iter_robject_bit_xor(na_loop_t* const lp) {
2755
- size_t i = 0;
2756
- size_t n;
2757
- char *p1, *p2, *p3;
2758
- ssize_t s1, s2, s3;
2759
-
2760
- INIT_COUNTER(lp, n);
2761
- INIT_PTR(lp, 0, p1, s1);
2762
- INIT_PTR(lp, 1, p2, s2);
2763
- INIT_PTR(lp, 2, p3, s3);
2764
-
2765
- //
2766
-
2767
- if (s2 == 0) { // Broadcasting from scalar value.
2768
- check_intdivzero(*(dtype*)p2);
2769
- if (s1 == sizeof(dtype) && s3 == sizeof(dtype)) {
2770
- if (p1 == p3) { // inplace case
2771
- for (; i < n; i++) {
2772
- ((dtype*)p1)[i] = m_bit_xor(((dtype*)p1)[i], *(dtype*)p2);
2773
- }
2774
- } else {
2775
- for (; i < n; i++) {
2776
- ((dtype*)p3)[i] = m_bit_xor(((dtype*)p1)[i], *(dtype*)p2);
2777
- }
2778
- }
2779
- } else {
2780
- for (i = 0; i < n; i++) {
2781
- *(dtype*)p3 = m_bit_xor(*(dtype*)p1, *(dtype*)p2);
2782
- p1 += s1;
2783
- p3 += s3;
2784
- }
2785
- }
2786
- } else {
2787
- if (p1 == p3) { // inplace case
2788
- for (i = 0; i < n; i++) {
2789
- check_intdivzero(*(dtype*)p2);
2790
- *(dtype*)p1 = m_bit_xor(*(dtype*)p1, *(dtype*)p2);
2791
- p1 += s1;
2792
- p2 += s2;
2793
- }
2794
- } else {
2795
- for (i = 0; i < n; i++) {
2796
- check_intdivzero(*(dtype*)p2);
2797
- *(dtype*)p3 = m_bit_xor(*(dtype*)p1, *(dtype*)p2);
2798
- p1 += s1;
2799
- p2 += s2;
2800
- p3 += s3;
2801
- }
2802
- }
2803
- }
2804
-
2805
- return;
2806
- //
2807
- }
2808
- #undef check_intdivzero
2809
-
2810
- static VALUE robject_bit_xor_self(VALUE self, VALUE other) {
2811
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
2812
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
2813
- ndfunc_t ndf = { iter_robject_bit_xor, STRIDE_LOOP, 2, 1, ain, aout };
2814
-
2815
- return na_ndloop(&ndf, 2, self, other);
2816
- }
2817
-
2818
- /*
2819
- Binary bit_xor.
2820
- @overload ^ other
2821
- @param [Numo::NArray,Numeric] other
2822
- @return [Numo::NArray] self ^ other
2823
- */
2824
- static VALUE robject_bit_xor(VALUE self, VALUE other) {
2825
- return robject_bit_xor_self(self, other);
2826
- }
2827
-
2828
- static void iter_robject_bit_not(na_loop_t* const lp) {
2829
- size_t i, n;
2830
- char *p1, *p2;
2831
- ssize_t s1, s2;
2832
- size_t *idx1, *idx2;
2833
- dtype x;
2834
-
2835
- INIT_COUNTER(lp, n);
2836
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
2837
- INIT_PTR_IDX(lp, 1, p2, s2, idx2);
2838
-
2839
- if (idx1) {
2840
- if (idx2) {
2841
- for (i = 0; i < n; i++) {
2842
- GET_DATA_INDEX(p1, idx1, dtype, x);
2843
- x = m_bit_not(x);
2844
- SET_DATA_INDEX(p2, idx2, dtype, x);
2845
- }
2846
- } else {
2847
- for (i = 0; i < n; i++) {
2848
- GET_DATA_INDEX(p1, idx1, dtype, x);
2849
- x = m_bit_not(x);
2850
- SET_DATA_STRIDE(p2, s2, dtype, x);
2851
- }
2852
- }
2853
- } else {
2854
- if (idx2) {
2855
- for (i = 0; i < n; i++) {
2856
- GET_DATA_STRIDE(p1, s1, dtype, x);
2857
- x = m_bit_not(x);
2858
- SET_DATA_INDEX(p2, idx2, dtype, x);
2859
- }
2860
- } else {
2861
- //
2862
- for (i = 0; i < n; i++) {
2863
- *(dtype*)p2 = m_bit_not(*(dtype*)p1);
2864
- p1 += s1;
2865
- p2 += s2;
2866
- }
2867
- return;
2868
- //
2869
- }
2870
- }
2871
- }
2872
-
2873
- /*
2874
- Unary bit_not.
2875
- @overload ~
2876
- @return [Numo::RObject] bit_not of self.
2877
- */
2878
- static VALUE robject_bit_not(VALUE self) {
2879
- ndfunc_arg_in_t ain[1] = { { cT, 0 } };
2880
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
2881
- ndfunc_t ndf = { iter_robject_bit_not, FULL_LOOP, 1, 1, ain, aout };
2882
-
2883
- return na_ndloop(&ndf, 1, self);
2884
- }
2885
-
2886
- #define check_intdivzero(y) \
2887
- {}
2888
-
2889
- static void iter_robject_left_shift(na_loop_t* const lp) {
2890
- size_t i = 0;
2891
- size_t n;
2892
- char *p1, *p2, *p3;
2893
- ssize_t s1, s2, s3;
2894
-
2895
- INIT_COUNTER(lp, n);
2896
- INIT_PTR(lp, 0, p1, s1);
2897
- INIT_PTR(lp, 1, p2, s2);
2898
- INIT_PTR(lp, 2, p3, s3);
2899
-
2900
- //
2901
-
2902
- if (s2 == 0) { // Broadcasting from scalar value.
2903
- check_intdivzero(*(dtype*)p2);
2904
- if (s1 == sizeof(dtype) && s3 == sizeof(dtype)) {
2905
- if (p1 == p3) { // inplace case
2906
- for (; i < n; i++) {
2907
- ((dtype*)p1)[i] = m_left_shift(((dtype*)p1)[i], *(dtype*)p2);
2908
- }
2909
- } else {
2910
- for (; i < n; i++) {
2911
- ((dtype*)p3)[i] = m_left_shift(((dtype*)p1)[i], *(dtype*)p2);
2912
- }
2913
- }
2914
- } else {
2915
- for (i = 0; i < n; i++) {
2916
- *(dtype*)p3 = m_left_shift(*(dtype*)p1, *(dtype*)p2);
2917
- p1 += s1;
2918
- p3 += s3;
2919
- }
2920
- }
2921
- } else {
2922
- if (p1 == p3) { // inplace case
2923
- for (i = 0; i < n; i++) {
2924
- check_intdivzero(*(dtype*)p2);
2925
- *(dtype*)p1 = m_left_shift(*(dtype*)p1, *(dtype*)p2);
2926
- p1 += s1;
2927
- p2 += s2;
2928
- }
2929
- } else {
2930
- for (i = 0; i < n; i++) {
2931
- check_intdivzero(*(dtype*)p2);
2932
- *(dtype*)p3 = m_left_shift(*(dtype*)p1, *(dtype*)p2);
2933
- p1 += s1;
2934
- p2 += s2;
2935
- p3 += s3;
2936
- }
2937
- }
2938
- }
2939
-
2940
- return;
2941
- //
2942
- }
2943
- #undef check_intdivzero
2944
-
2945
- static VALUE robject_left_shift_self(VALUE self, VALUE other) {
2946
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
2947
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
2948
- ndfunc_t ndf = { iter_robject_left_shift, STRIDE_LOOP, 2, 1, ain, aout };
2949
-
2950
- return na_ndloop(&ndf, 2, self, other);
2951
- }
2952
-
2953
- /*
2954
- Binary left_shift.
2955
- @overload << other
2956
- @param [Numo::NArray,Numeric] other
2957
- @return [Numo::NArray] self << other
2958
- */
2959
- static VALUE robject_left_shift(VALUE self, VALUE other) {
2960
- return robject_left_shift_self(self, other);
2961
- }
2962
-
2963
- #define check_intdivzero(y) \
2964
- {}
2965
-
2966
- static void iter_robject_right_shift(na_loop_t* const lp) {
2967
- size_t i = 0;
2968
- size_t n;
2969
- char *p1, *p2, *p3;
2970
- ssize_t s1, s2, s3;
2971
-
2972
- INIT_COUNTER(lp, n);
2973
- INIT_PTR(lp, 0, p1, s1);
2974
- INIT_PTR(lp, 1, p2, s2);
2975
- INIT_PTR(lp, 2, p3, s3);
2976
-
2977
- //
2978
-
2979
- if (s2 == 0) { // Broadcasting from scalar value.
2980
- check_intdivzero(*(dtype*)p2);
2981
- if (s1 == sizeof(dtype) && s3 == sizeof(dtype)) {
2982
- if (p1 == p3) { // inplace case
2983
- for (; i < n; i++) {
2984
- ((dtype*)p1)[i] = m_right_shift(((dtype*)p1)[i], *(dtype*)p2);
2985
- }
2986
- } else {
2987
- for (; i < n; i++) {
2988
- ((dtype*)p3)[i] = m_right_shift(((dtype*)p1)[i], *(dtype*)p2);
2989
- }
2990
- }
2991
- } else {
2992
- for (i = 0; i < n; i++) {
2993
- *(dtype*)p3 = m_right_shift(*(dtype*)p1, *(dtype*)p2);
2994
- p1 += s1;
2995
- p3 += s3;
2996
- }
2997
- }
2998
- } else {
2999
- if (p1 == p3) { // inplace case
3000
- for (i = 0; i < n; i++) {
3001
- check_intdivzero(*(dtype*)p2);
3002
- *(dtype*)p1 = m_right_shift(*(dtype*)p1, *(dtype*)p2);
3003
- p1 += s1;
3004
- p2 += s2;
3005
- }
3006
- } else {
3007
- for (i = 0; i < n; i++) {
3008
- check_intdivzero(*(dtype*)p2);
3009
- *(dtype*)p3 = m_right_shift(*(dtype*)p1, *(dtype*)p2);
3010
- p1 += s1;
3011
- p2 += s2;
3012
- p3 += s3;
3013
- }
3014
- }
3015
- }
3016
-
3017
- return;
3018
- //
3019
- }
3020
- #undef check_intdivzero
3021
-
3022
- static VALUE robject_right_shift_self(VALUE self, VALUE other) {
3023
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
3024
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
3025
- ndfunc_t ndf = { iter_robject_right_shift, STRIDE_LOOP, 2, 1, ain, aout };
3026
-
3027
- return na_ndloop(&ndf, 2, self, other);
3028
- }
3029
-
3030
- /*
3031
- Binary right_shift.
3032
- @overload >> other
3033
- @param [Numo::NArray,Numeric] other
3034
- @return [Numo::NArray] self >> other
3035
- */
3036
- static VALUE robject_right_shift(VALUE self, VALUE other) {
3037
- return robject_right_shift_self(self, other);
3038
- }
3039
-
3040
- static void iter_robject_floor(na_loop_t* const lp) {
3041
- size_t i, n;
3042
- char *p1, *p2;
3043
- ssize_t s1, s2;
3044
- size_t *idx1, *idx2;
3045
- dtype x;
3046
-
3047
- INIT_COUNTER(lp, n);
3048
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
3049
- INIT_PTR_IDX(lp, 1, p2, s2, idx2);
3050
-
3051
- if (idx1) {
3052
- if (idx2) {
3053
- for (i = 0; i < n; i++) {
3054
- GET_DATA_INDEX(p1, idx1, dtype, x);
3055
- x = m_floor(x);
3056
- SET_DATA_INDEX(p2, idx2, dtype, x);
3057
- }
3058
- } else {
3059
- for (i = 0; i < n; i++) {
3060
- GET_DATA_INDEX(p1, idx1, dtype, x);
3061
- x = m_floor(x);
3062
- SET_DATA_STRIDE(p2, s2, dtype, x);
3063
- }
3064
- }
3065
- } else {
3066
- if (idx2) {
3067
- for (i = 0; i < n; i++) {
3068
- GET_DATA_STRIDE(p1, s1, dtype, x);
3069
- x = m_floor(x);
3070
- SET_DATA_INDEX(p2, idx2, dtype, x);
3071
- }
3072
- } else {
3073
- //
3074
- for (i = 0; i < n; i++) {
3075
- *(dtype*)p2 = m_floor(*(dtype*)p1);
3076
- p1 += s1;
3077
- p2 += s2;
3078
- }
3079
- return;
3080
- //
3081
- }
3082
- }
3083
- }
3084
-
3085
- /*
3086
- Unary floor.
3087
- @overload floor
3088
- @return [Numo::RObject] floor of self.
3089
- */
3090
- static VALUE robject_floor(VALUE self) {
3091
- ndfunc_arg_in_t ain[1] = { { cT, 0 } };
3092
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
3093
- ndfunc_t ndf = { iter_robject_floor, FULL_LOOP, 1, 1, ain, aout };
3094
-
3095
- return na_ndloop(&ndf, 1, self);
3096
- }
3097
-
3098
- static void iter_robject_round(na_loop_t* const lp) {
3099
- size_t i, n;
3100
- char *p1, *p2;
3101
- ssize_t s1, s2;
3102
- size_t *idx1, *idx2;
3103
- dtype x;
3104
-
3105
- INIT_COUNTER(lp, n);
3106
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
3107
- INIT_PTR_IDX(lp, 1, p2, s2, idx2);
3108
-
3109
- if (idx1) {
3110
- if (idx2) {
3111
- for (i = 0; i < n; i++) {
3112
- GET_DATA_INDEX(p1, idx1, dtype, x);
3113
- x = m_round(x);
3114
- SET_DATA_INDEX(p2, idx2, dtype, x);
3115
- }
3116
- } else {
3117
- for (i = 0; i < n; i++) {
3118
- GET_DATA_INDEX(p1, idx1, dtype, x);
3119
- x = m_round(x);
3120
- SET_DATA_STRIDE(p2, s2, dtype, x);
3121
- }
3122
- }
3123
- } else {
3124
- if (idx2) {
3125
- for (i = 0; i < n; i++) {
3126
- GET_DATA_STRIDE(p1, s1, dtype, x);
3127
- x = m_round(x);
3128
- SET_DATA_INDEX(p2, idx2, dtype, x);
3129
- }
3130
- } else {
3131
- //
3132
- for (i = 0; i < n; i++) {
3133
- *(dtype*)p2 = m_round(*(dtype*)p1);
3134
- p1 += s1;
3135
- p2 += s2;
3136
- }
3137
- return;
3138
- //
3139
- }
3140
- }
3141
- }
3142
-
3143
- /*
3144
- Unary round.
3145
- @overload round
3146
- @return [Numo::RObject] round of self.
3147
- */
3148
- static VALUE robject_round(VALUE self) {
3149
- ndfunc_arg_in_t ain[1] = { { cT, 0 } };
3150
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
3151
- ndfunc_t ndf = { iter_robject_round, FULL_LOOP, 1, 1, ain, aout };
3152
-
3153
- return na_ndloop(&ndf, 1, self);
3154
- }
3155
-
3156
- static void iter_robject_ceil(na_loop_t* const lp) {
3157
- size_t i, n;
3158
- char *p1, *p2;
3159
- ssize_t s1, s2;
3160
- size_t *idx1, *idx2;
3161
- dtype x;
3162
-
3163
- INIT_COUNTER(lp, n);
3164
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
3165
- INIT_PTR_IDX(lp, 1, p2, s2, idx2);
3166
-
3167
- if (idx1) {
3168
- if (idx2) {
3169
- for (i = 0; i < n; i++) {
3170
- GET_DATA_INDEX(p1, idx1, dtype, x);
3171
- x = m_ceil(x);
3172
- SET_DATA_INDEX(p2, idx2, dtype, x);
3173
- }
3174
- } else {
3175
- for (i = 0; i < n; i++) {
3176
- GET_DATA_INDEX(p1, idx1, dtype, x);
3177
- x = m_ceil(x);
3178
- SET_DATA_STRIDE(p2, s2, dtype, x);
3179
- }
3180
- }
3181
- } else {
3182
- if (idx2) {
3183
- for (i = 0; i < n; i++) {
3184
- GET_DATA_STRIDE(p1, s1, dtype, x);
3185
- x = m_ceil(x);
3186
- SET_DATA_INDEX(p2, idx2, dtype, x);
3187
- }
3188
- } else {
3189
- //
3190
- for (i = 0; i < n; i++) {
3191
- *(dtype*)p2 = m_ceil(*(dtype*)p1);
3192
- p1 += s1;
3193
- p2 += s2;
3194
- }
3195
- return;
3196
- //
3197
- }
3198
- }
3199
- }
3200
-
3201
- /*
3202
- Unary ceil.
3203
- @overload ceil
3204
- @return [Numo::RObject] ceil of self.
3205
- */
3206
- static VALUE robject_ceil(VALUE self) {
3207
- ndfunc_arg_in_t ain[1] = { { cT, 0 } };
3208
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
3209
- ndfunc_t ndf = { iter_robject_ceil, FULL_LOOP, 1, 1, ain, aout };
3210
-
3211
- return na_ndloop(&ndf, 1, self);
3212
- }
3213
-
3214
- static void iter_robject_trunc(na_loop_t* const lp) {
3215
- size_t i, n;
3216
- char *p1, *p2;
3217
- ssize_t s1, s2;
3218
- size_t *idx1, *idx2;
3219
- dtype x;
3220
-
3221
- INIT_COUNTER(lp, n);
3222
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
3223
- INIT_PTR_IDX(lp, 1, p2, s2, idx2);
3224
-
3225
- if (idx1) {
3226
- if (idx2) {
3227
- for (i = 0; i < n; i++) {
3228
- GET_DATA_INDEX(p1, idx1, dtype, x);
3229
- x = m_trunc(x);
3230
- SET_DATA_INDEX(p2, idx2, dtype, x);
3231
- }
3232
- } else {
3233
- for (i = 0; i < n; i++) {
3234
- GET_DATA_INDEX(p1, idx1, dtype, x);
3235
- x = m_trunc(x);
3236
- SET_DATA_STRIDE(p2, s2, dtype, x);
3237
- }
3238
- }
3239
- } else {
3240
- if (idx2) {
3241
- for (i = 0; i < n; i++) {
3242
- GET_DATA_STRIDE(p1, s1, dtype, x);
3243
- x = m_trunc(x);
3244
- SET_DATA_INDEX(p2, idx2, dtype, x);
3245
- }
3246
- } else {
3247
- //
3248
- for (i = 0; i < n; i++) {
3249
- *(dtype*)p2 = m_trunc(*(dtype*)p1);
3250
- p1 += s1;
3251
- p2 += s2;
3252
- }
3253
- return;
3254
- //
3255
- }
3256
- }
3257
- }
3258
-
3259
- /*
3260
- Unary trunc.
3261
- @overload trunc
3262
- @return [Numo::RObject] trunc of self.
3263
- */
3264
- static VALUE robject_trunc(VALUE self) {
3265
- ndfunc_arg_in_t ain[1] = { { cT, 0 } };
3266
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
3267
- ndfunc_t ndf = { iter_robject_trunc, FULL_LOOP, 1, 1, ain, aout };
3268
-
3269
- return na_ndloop(&ndf, 1, self);
3270
- }
3271
-
3272
- static void iter_robject_gt(na_loop_t* const lp) {
3273
- size_t i;
3274
- char *p1, *p2;
3275
- BIT_DIGIT* a3;
3276
- size_t p3;
3277
- ssize_t s1, s2, s3;
3278
- dtype x, y;
3279
- BIT_DIGIT b;
3280
- INIT_COUNTER(lp, i);
3281
- INIT_PTR(lp, 0, p1, s1);
3282
- INIT_PTR(lp, 1, p2, s2);
3283
- INIT_PTR_BIT(lp, 2, a3, p3, s3);
3284
- for (; i--;) {
3285
- GET_DATA_STRIDE(p1, s1, dtype, x);
3286
- GET_DATA_STRIDE(p2, s2, dtype, y);
3287
- b = (m_gt(x, y)) ? 1 : 0;
3288
- STORE_BIT(a3, p3, b);
3289
- p3 += s3;
3290
- }
3291
- }
3292
-
3293
- static VALUE robject_gt_self(VALUE self, VALUE other) {
3294
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
3295
- ndfunc_arg_out_t aout[1] = { { numo_cBit, 0 } };
3296
- ndfunc_t ndf = { iter_robject_gt, STRIDE_LOOP, 2, 1, ain, aout };
3297
-
3298
- return na_ndloop(&ndf, 2, self, other);
3299
- }
3300
-
3301
- /*
3302
- Comparison gt other.
3303
- @overload gt other
3304
- @param [Numo::NArray,Numeric] other
3305
- @return [Numo::Bit] result of self gt other.
3306
- */
3307
- static VALUE robject_gt(VALUE self, VALUE other) {
3308
- return robject_gt_self(self, other);
3309
- }
3310
-
3311
- static void iter_robject_ge(na_loop_t* const lp) {
3312
- size_t i;
3313
- char *p1, *p2;
3314
- BIT_DIGIT* a3;
3315
- size_t p3;
3316
- ssize_t s1, s2, s3;
3317
- dtype x, y;
3318
- BIT_DIGIT b;
3319
- INIT_COUNTER(lp, i);
3320
- INIT_PTR(lp, 0, p1, s1);
3321
- INIT_PTR(lp, 1, p2, s2);
3322
- INIT_PTR_BIT(lp, 2, a3, p3, s3);
3323
- for (; i--;) {
3324
- GET_DATA_STRIDE(p1, s1, dtype, x);
3325
- GET_DATA_STRIDE(p2, s2, dtype, y);
3326
- b = (m_ge(x, y)) ? 1 : 0;
3327
- STORE_BIT(a3, p3, b);
3328
- p3 += s3;
3329
- }
3330
- }
3331
-
3332
- static VALUE robject_ge_self(VALUE self, VALUE other) {
3333
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
3334
- ndfunc_arg_out_t aout[1] = { { numo_cBit, 0 } };
3335
- ndfunc_t ndf = { iter_robject_ge, STRIDE_LOOP, 2, 1, ain, aout };
3336
-
3337
- return na_ndloop(&ndf, 2, self, other);
3338
- }
3339
-
3340
- /*
3341
- Comparison ge other.
3342
- @overload ge other
3343
- @param [Numo::NArray,Numeric] other
3344
- @return [Numo::Bit] result of self ge other.
3345
- */
3346
- static VALUE robject_ge(VALUE self, VALUE other) {
3347
- return robject_ge_self(self, other);
3348
- }
3349
-
3350
- static void iter_robject_lt(na_loop_t* const lp) {
3351
- size_t i;
3352
- char *p1, *p2;
3353
- BIT_DIGIT* a3;
3354
- size_t p3;
3355
- ssize_t s1, s2, s3;
3356
- dtype x, y;
3357
- BIT_DIGIT b;
3358
- INIT_COUNTER(lp, i);
3359
- INIT_PTR(lp, 0, p1, s1);
3360
- INIT_PTR(lp, 1, p2, s2);
3361
- INIT_PTR_BIT(lp, 2, a3, p3, s3);
3362
- for (; i--;) {
3363
- GET_DATA_STRIDE(p1, s1, dtype, x);
3364
- GET_DATA_STRIDE(p2, s2, dtype, y);
3365
- b = (m_lt(x, y)) ? 1 : 0;
3366
- STORE_BIT(a3, p3, b);
3367
- p3 += s3;
3368
- }
3369
- }
3370
-
3371
- static VALUE robject_lt_self(VALUE self, VALUE other) {
3372
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
3373
- ndfunc_arg_out_t aout[1] = { { numo_cBit, 0 } };
3374
- ndfunc_t ndf = { iter_robject_lt, STRIDE_LOOP, 2, 1, ain, aout };
3375
-
3376
- return na_ndloop(&ndf, 2, self, other);
3377
- }
3378
-
3379
- /*
3380
- Comparison lt other.
3381
- @overload lt other
3382
- @param [Numo::NArray,Numeric] other
3383
- @return [Numo::Bit] result of self lt other.
3384
- */
3385
- static VALUE robject_lt(VALUE self, VALUE other) {
3386
- return robject_lt_self(self, other);
3387
- }
3388
-
3389
- static void iter_robject_le(na_loop_t* const lp) {
3390
- size_t i;
3391
- char *p1, *p2;
3392
- BIT_DIGIT* a3;
3393
- size_t p3;
3394
- ssize_t s1, s2, s3;
3395
- dtype x, y;
3396
- BIT_DIGIT b;
3397
- INIT_COUNTER(lp, i);
3398
- INIT_PTR(lp, 0, p1, s1);
3399
- INIT_PTR(lp, 1, p2, s2);
3400
- INIT_PTR_BIT(lp, 2, a3, p3, s3);
3401
- for (; i--;) {
3402
- GET_DATA_STRIDE(p1, s1, dtype, x);
3403
- GET_DATA_STRIDE(p2, s2, dtype, y);
3404
- b = (m_le(x, y)) ? 1 : 0;
3405
- STORE_BIT(a3, p3, b);
3406
- p3 += s3;
3407
- }
3408
- }
3409
-
3410
- static VALUE robject_le_self(VALUE self, VALUE other) {
3411
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
3412
- ndfunc_arg_out_t aout[1] = { { numo_cBit, 0 } };
3413
- ndfunc_t ndf = { iter_robject_le, STRIDE_LOOP, 2, 1, ain, aout };
3414
-
3415
- return na_ndloop(&ndf, 2, self, other);
3416
- }
3417
-
3418
- /*
3419
- Comparison le other.
3420
- @overload le other
3421
- @param [Numo::NArray,Numeric] other
3422
- @return [Numo::Bit] result of self le other.
3423
- */
3424
- static VALUE robject_le(VALUE self, VALUE other) {
3425
- return robject_le_self(self, other);
3426
- }
3427
-
3428
- static void iter_robject_clip(na_loop_t* const lp) {
3429
- size_t i;
3430
- char *p1, *p2, *p3, *p4;
3431
- ssize_t s1, s2, s3, s4;
3432
- dtype x, min, max;
3433
- INIT_COUNTER(lp, i);
3434
- INIT_PTR(lp, 0, p1, s1);
3435
- INIT_PTR(lp, 1, p2, s2);
3436
- INIT_PTR(lp, 2, p3, s3);
3437
- INIT_PTR(lp, 3, p4, s4);
3438
- for (; i--;) {
3439
- GET_DATA_STRIDE(p1, s1, dtype, x);
3440
- GET_DATA_STRIDE(p2, s2, dtype, min);
3441
- GET_DATA_STRIDE(p3, s3, dtype, max);
3442
- if (m_gt(min, max)) {
3443
- rb_raise(nary_eOperationError, "min is greater than max");
3444
- }
3445
- if (m_lt(x, min)) {
3446
- x = min;
3447
- }
3448
- if (m_gt(x, max)) {
3449
- x = max;
3450
- }
3451
- SET_DATA_STRIDE(p4, s4, dtype, x);
3452
- }
3453
- }
3454
-
3455
- static void iter_robject_clip_min(na_loop_t* const lp) {
3456
- size_t i;
3457
- char *p1, *p2, *p3;
3458
- ssize_t s1, s2, s3;
3459
- dtype x, min;
3460
- INIT_COUNTER(lp, i);
3461
- INIT_PTR(lp, 0, p1, s1);
3462
- INIT_PTR(lp, 1, p2, s2);
3463
- INIT_PTR(lp, 2, p3, s3);
3464
- for (; i--;) {
3465
- GET_DATA_STRIDE(p1, s1, dtype, x);
3466
- GET_DATA_STRIDE(p2, s2, dtype, min);
3467
- if (m_lt(x, min)) {
3468
- x = min;
3469
- }
3470
- SET_DATA_STRIDE(p3, s3, dtype, x);
3471
- }
3472
- }
3473
-
3474
- static void iter_robject_clip_max(na_loop_t* const lp) {
3475
- size_t i;
3476
- char *p1, *p2, *p3;
3477
- ssize_t s1, s2, s3;
3478
- dtype x, max;
3479
- INIT_COUNTER(lp, i);
3480
- INIT_PTR(lp, 0, p1, s1);
3481
- INIT_PTR(lp, 1, p2, s2);
3482
- INIT_PTR(lp, 2, p3, s3);
3483
- for (; i--;) {
3484
- GET_DATA_STRIDE(p1, s1, dtype, x);
3485
- GET_DATA_STRIDE(p2, s2, dtype, max);
3486
- if (m_gt(x, max)) {
3487
- x = max;
3488
- }
3489
- SET_DATA_STRIDE(p3, s3, dtype, x);
3490
- }
3491
- }
3492
-
3493
- /*
3494
- Clip array elements by [min,max].
3495
- If either of min or max is nil, one side is clipped.
3496
- @overload clip(min,max)
3497
- @param [Numo::NArray,Numeric] min
3498
- @param [Numo::NArray,Numeric] max
3499
- @return [Numo::NArray] result of clip.
3500
-
3501
- @example
3502
- a = Numo::Int32.new(10).seq
3503
- # => Numo::Int32#shape=[10]
3504
- # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
3505
-
3506
- a.clip(1,8)
3507
- # => Numo::Int32#shape=[10]
3508
- # [1, 1, 2, 3, 4, 5, 6, 7, 8, 8]
3509
-
3510
- a.inplace.clip(3,6)
3511
- a
3512
- # => Numo::Int32#shape=[10]
3513
- # [3, 3, 3, 3, 4, 5, 6, 6, 6, 6]
3514
-
3515
- b = Numo::Int32.new(10).seq
3516
- # => Numo::Int32#shape=[10]
3517
- # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
3518
-
3519
- b.clip([3,4,1,1,1,4,4,4,4,4], 8)
3520
- # => Numo::Int32#shape=[10]
3521
- # [3, 4, 2, 3, 4, 5, 6, 7, 8, 8]
3522
- */
3523
- static VALUE robject_clip(VALUE self, VALUE min, VALUE max) {
3524
- ndfunc_arg_in_t ain[3] = { { Qnil, 0 }, { cT, 0 }, { cT, 0 } };
3525
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
3526
- ndfunc_t ndf_min = { iter_robject_clip_min, STRIDE_LOOP, 2, 1, ain, aout };
3527
- ndfunc_t ndf_max = { iter_robject_clip_max, STRIDE_LOOP, 2, 1, ain, aout };
3528
- ndfunc_t ndf_both = { iter_robject_clip, STRIDE_LOOP, 3, 1, ain, aout };
3529
-
3530
- if (RTEST(min)) {
3531
- if (RTEST(max)) {
3532
- return na_ndloop(&ndf_both, 3, self, min, max);
3533
- } else {
3534
- return na_ndloop(&ndf_min, 2, self, min);
3535
- }
3536
- } else {
3537
- if (RTEST(max)) {
3538
- return na_ndloop(&ndf_max, 2, self, max);
3539
- }
3540
- }
3541
- rb_raise(rb_eArgError, "min and max are not given");
3542
- return Qnil;
3543
- }
3544
-
3545
- static void iter_robject_isnan(na_loop_t* const lp) {
3546
- size_t i;
3547
- char* p1;
3548
- BIT_DIGIT* a2;
3549
- size_t p2;
3550
- ssize_t s1, s2;
3551
- size_t* idx1;
3552
- dtype x;
3553
- BIT_DIGIT b;
3554
- INIT_COUNTER(lp, i);
3555
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
3556
- INIT_PTR_BIT(lp, 1, a2, p2, s2);
3557
- if (idx1) {
3558
- for (; i--;) {
3559
- GET_DATA_INDEX(p1, idx1, dtype, x);
3560
- b = (m_isnan(x)) ? 1 : 0;
3561
- STORE_BIT(a2, p2, b);
3562
- p2 += s2;
3563
- }
3564
- } else {
3565
- for (; i--;) {
3566
- GET_DATA_STRIDE(p1, s1, dtype, x);
3567
- b = (m_isnan(x)) ? 1 : 0;
3568
- STORE_BIT(a2, p2, b);
3569
- p2 += s2;
3570
- }
3571
- }
3572
- }
3573
-
3574
- /*
3575
- Condition of isnan.
3576
- @overload isnan
3577
- @return [Numo::Bit] Condition of isnan.
3578
- */
3579
- static VALUE robject_isnan(VALUE self) {
3580
- ndfunc_arg_in_t ain[1] = { { cT, 0 } };
3581
- ndfunc_arg_out_t aout[1] = { { numo_cBit, 0 } };
3582
- ndfunc_t ndf = { iter_robject_isnan, FULL_LOOP, 1, 1, ain, aout };
3583
-
3584
- return na_ndloop(&ndf, 1, self);
3585
- }
3586
-
3587
- static void iter_robject_isinf(na_loop_t* const lp) {
3588
- size_t i;
3589
- char* p1;
3590
- BIT_DIGIT* a2;
3591
- size_t p2;
3592
- ssize_t s1, s2;
3593
- size_t* idx1;
3594
- dtype x;
3595
- BIT_DIGIT b;
3596
- INIT_COUNTER(lp, i);
3597
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
3598
- INIT_PTR_BIT(lp, 1, a2, p2, s2);
3599
- if (idx1) {
3600
- for (; i--;) {
3601
- GET_DATA_INDEX(p1, idx1, dtype, x);
3602
- b = (m_isinf(x)) ? 1 : 0;
3603
- STORE_BIT(a2, p2, b);
3604
- p2 += s2;
3605
- }
3606
- } else {
3607
- for (; i--;) {
3608
- GET_DATA_STRIDE(p1, s1, dtype, x);
3609
- b = (m_isinf(x)) ? 1 : 0;
3610
- STORE_BIT(a2, p2, b);
3611
- p2 += s2;
3612
- }
3613
- }
3614
- }
3615
-
3616
- /*
3617
- Condition of isinf.
3618
- @overload isinf
3619
- @return [Numo::Bit] Condition of isinf.
3620
- */
3621
- static VALUE robject_isinf(VALUE self) {
3622
- ndfunc_arg_in_t ain[1] = { { cT, 0 } };
3623
- ndfunc_arg_out_t aout[1] = { { numo_cBit, 0 } };
3624
- ndfunc_t ndf = { iter_robject_isinf, FULL_LOOP, 1, 1, ain, aout };
3625
-
3626
- return na_ndloop(&ndf, 1, self);
3627
- }
3628
-
3629
- static void iter_robject_isposinf(na_loop_t* const lp) {
3630
- size_t i;
3631
- char* p1;
3632
- BIT_DIGIT* a2;
3633
- size_t p2;
3634
- ssize_t s1, s2;
3635
- size_t* idx1;
3636
- dtype x;
3637
- BIT_DIGIT b;
3638
- INIT_COUNTER(lp, i);
3639
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
3640
- INIT_PTR_BIT(lp, 1, a2, p2, s2);
3641
- if (idx1) {
3642
- for (; i--;) {
3643
- GET_DATA_INDEX(p1, idx1, dtype, x);
3644
- b = (m_isposinf(x)) ? 1 : 0;
3645
- STORE_BIT(a2, p2, b);
3646
- p2 += s2;
3647
- }
3648
- } else {
3649
- for (; i--;) {
3650
- GET_DATA_STRIDE(p1, s1, dtype, x);
3651
- b = (m_isposinf(x)) ? 1 : 0;
3652
- STORE_BIT(a2, p2, b);
3653
- p2 += s2;
3654
- }
3655
- }
3656
- }
3657
-
3658
- /*
3659
- Condition of isposinf.
3660
- @overload isposinf
3661
- @return [Numo::Bit] Condition of isposinf.
3662
- */
3663
- static VALUE robject_isposinf(VALUE self) {
3664
- ndfunc_arg_in_t ain[1] = { { cT, 0 } };
3665
- ndfunc_arg_out_t aout[1] = { { numo_cBit, 0 } };
3666
- ndfunc_t ndf = { iter_robject_isposinf, FULL_LOOP, 1, 1, ain, aout };
3667
-
3668
- return na_ndloop(&ndf, 1, self);
3669
- }
3670
-
3671
- static void iter_robject_isneginf(na_loop_t* const lp) {
3672
- size_t i;
3673
- char* p1;
3674
- BIT_DIGIT* a2;
3675
- size_t p2;
3676
- ssize_t s1, s2;
3677
- size_t* idx1;
3678
- dtype x;
3679
- BIT_DIGIT b;
3680
- INIT_COUNTER(lp, i);
3681
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
3682
- INIT_PTR_BIT(lp, 1, a2, p2, s2);
3683
- if (idx1) {
3684
- for (; i--;) {
3685
- GET_DATA_INDEX(p1, idx1, dtype, x);
3686
- b = (m_isneginf(x)) ? 1 : 0;
3687
- STORE_BIT(a2, p2, b);
3688
- p2 += s2;
3689
- }
3690
- } else {
3691
- for (; i--;) {
3692
- GET_DATA_STRIDE(p1, s1, dtype, x);
3693
- b = (m_isneginf(x)) ? 1 : 0;
3694
- STORE_BIT(a2, p2, b);
3695
- p2 += s2;
3696
- }
3697
- }
3698
- }
3699
-
3700
- /*
3701
- Condition of isneginf.
3702
- @overload isneginf
3703
- @return [Numo::Bit] Condition of isneginf.
3704
- */
3705
- static VALUE robject_isneginf(VALUE self) {
3706
- ndfunc_arg_in_t ain[1] = { { cT, 0 } };
3707
- ndfunc_arg_out_t aout[1] = { { numo_cBit, 0 } };
3708
- ndfunc_t ndf = { iter_robject_isneginf, FULL_LOOP, 1, 1, ain, aout };
3709
-
3710
- return na_ndloop(&ndf, 1, self);
3711
- }
3712
-
3713
- static void iter_robject_isfinite(na_loop_t* const lp) {
3714
- size_t i;
3715
- char* p1;
3716
- BIT_DIGIT* a2;
3717
- size_t p2;
3718
- ssize_t s1, s2;
3719
- size_t* idx1;
3720
- dtype x;
3721
- BIT_DIGIT b;
3722
- INIT_COUNTER(lp, i);
3723
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
3724
- INIT_PTR_BIT(lp, 1, a2, p2, s2);
3725
- if (idx1) {
3726
- for (; i--;) {
3727
- GET_DATA_INDEX(p1, idx1, dtype, x);
3728
- b = (m_isfinite(x)) ? 1 : 0;
3729
- STORE_BIT(a2, p2, b);
3730
- p2 += s2;
3731
- }
3732
- } else {
3733
- for (; i--;) {
3734
- GET_DATA_STRIDE(p1, s1, dtype, x);
3735
- b = (m_isfinite(x)) ? 1 : 0;
3736
- STORE_BIT(a2, p2, b);
3737
- p2 += s2;
3738
- }
3739
- }
3740
- }
3741
-
3742
- /*
3743
- Condition of isfinite.
3744
- @overload isfinite
3745
- @return [Numo::Bit] Condition of isfinite.
3746
- */
3747
- static VALUE robject_isfinite(VALUE self) {
3748
- ndfunc_arg_in_t ain[1] = { { cT, 0 } };
3749
- ndfunc_arg_out_t aout[1] = { { numo_cBit, 0 } };
3750
- ndfunc_t ndf = { iter_robject_isfinite, FULL_LOOP, 1, 1, ain, aout };
3751
-
3752
- return na_ndloop(&ndf, 1, self);
3753
- }
3754
-
3755
- static void iter_robject_sum(na_loop_t* const lp) {
3756
- size_t n;
3757
- char *p1, *p2;
3758
- ssize_t s1;
3759
-
3760
- INIT_COUNTER(lp, n);
3761
- INIT_PTR(lp, 0, p1, s1);
3762
- p2 = lp->args[1].ptr + lp->args[1].iter[0].pos;
3763
-
3764
- *(dtype*)p2 = f_sum(n, p1, s1);
3765
- }
3766
- static void iter_robject_sum_nan(na_loop_t* const lp) {
3767
- size_t n;
3768
- char *p1, *p2;
3769
- ssize_t s1;
3770
-
3771
- INIT_COUNTER(lp, n);
3772
- INIT_PTR(lp, 0, p1, s1);
3773
- p2 = lp->args[1].ptr + lp->args[1].iter[0].pos;
3774
-
3775
- *(dtype*)p2 = f_sum_nan(n, p1, s1);
3776
- }
3777
-
3778
- /*
3779
- sum of self.
3780
- @overload sum(axis:nil, keepdims:false, nan:false)
3781
- @param [TrueClass] nan If true, apply NaN-aware algorithm (avoid NaN for sum/mean etc, or,
3782
- return NaN for min/max etc).
3783
- @param [Numeric,Array,Range] axis Performs sum along the axis.
3784
- @param [TrueClass] keepdims If true, the reduced axes are left in the result array as
3785
- dimensions with size one.
3786
- @return [Numo::RObject] returns result of sum.
3787
- */
3788
- static VALUE robject_sum(int argc, VALUE* argv, VALUE self) {
3789
- VALUE v, reduce;
3790
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { sym_reduce, 0 } };
3791
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
3792
- ndfunc_t ndf = { iter_robject_sum, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE, 2, 1, ain, aout };
3793
-
3794
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, iter_robject_sum_nan);
3795
-
3796
- v = na_ndloop(&ndf, 2, self, reduce);
3797
-
3798
- return robject_extract(v);
3799
- }
3800
-
3801
- static void iter_robject_prod(na_loop_t* const lp) {
3802
- size_t n;
3803
- char *p1, *p2;
3804
- ssize_t s1;
3805
-
3806
- INIT_COUNTER(lp, n);
3807
- INIT_PTR(lp, 0, p1, s1);
3808
- p2 = lp->args[1].ptr + lp->args[1].iter[0].pos;
3809
-
3810
- *(dtype*)p2 = f_prod(n, p1, s1);
3811
- }
3812
- static void iter_robject_prod_nan(na_loop_t* const lp) {
3813
- size_t n;
3814
- char *p1, *p2;
3815
- ssize_t s1;
3816
-
3817
- INIT_COUNTER(lp, n);
3818
- INIT_PTR(lp, 0, p1, s1);
3819
- p2 = lp->args[1].ptr + lp->args[1].iter[0].pos;
3820
-
3821
- *(dtype*)p2 = f_prod_nan(n, p1, s1);
3822
- }
3823
-
3824
- /*
3825
- prod of self.
3826
- @overload prod(axis:nil, keepdims:false, nan:false)
3827
- @param [TrueClass] nan If true, apply NaN-aware algorithm (avoid NaN for sum/mean etc, or,
3828
- return NaN for min/max etc).
3829
- @param [Numeric,Array,Range] axis Performs prod along the axis.
3830
- @param [TrueClass] keepdims If true, the reduced axes are left in the result array as
3831
- dimensions with size one.
3832
- @return [Numo::RObject] returns result of prod.
3833
- */
3834
- static VALUE robject_prod(int argc, VALUE* argv, VALUE self) {
3835
- VALUE v, reduce;
3836
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { sym_reduce, 0 } };
3837
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
3838
- ndfunc_t ndf = { iter_robject_prod, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE, 2, 1, ain, aout };
3839
-
3840
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, iter_robject_prod_nan);
3841
-
3842
- v = na_ndloop(&ndf, 2, self, reduce);
3843
-
3844
- return robject_extract(v);
3845
- }
3846
-
3847
- static void iter_robject_min(na_loop_t* const lp) {
3848
- size_t n;
3849
- char *p1, *p2;
3850
- ssize_t s1;
3851
-
3852
- INIT_COUNTER(lp, n);
3853
- INIT_PTR(lp, 0, p1, s1);
3854
- p2 = lp->args[1].ptr + lp->args[1].iter[0].pos;
3855
-
3856
- *(dtype*)p2 = f_min(n, p1, s1);
3857
- }
3858
- static void iter_robject_min_nan(na_loop_t* const lp) {
3859
- size_t n;
3860
- char *p1, *p2;
3861
- ssize_t s1;
3862
-
3863
- INIT_COUNTER(lp, n);
3864
- INIT_PTR(lp, 0, p1, s1);
3865
- p2 = lp->args[1].ptr + lp->args[1].iter[0].pos;
3866
-
3867
- *(dtype*)p2 = f_min_nan(n, p1, s1);
3868
- }
3869
-
3870
- /*
3871
- min of self.
3872
- @overload min(axis:nil, keepdims:false, nan:false)
3873
- @param [TrueClass] nan If true, apply NaN-aware algorithm (avoid NaN for sum/mean etc, or,
3874
- return NaN for min/max etc).
3875
- @param [Numeric,Array,Range] axis Performs min along the axis.
3876
- @param [TrueClass] keepdims If true, the reduced axes are left in the result array as
3877
- dimensions with size one.
3878
- @return [Numo::RObject] returns result of min.
3879
- */
3880
- static VALUE robject_min(int argc, VALUE* argv, VALUE self) {
3881
- VALUE v, reduce;
3882
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { sym_reduce, 0 } };
3883
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
3884
- ndfunc_t ndf = { iter_robject_min, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE, 2, 1, ain, aout };
3885
-
3886
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, iter_robject_min_nan);
3887
-
3888
- v = na_ndloop(&ndf, 2, self, reduce);
3889
-
3890
- return robject_extract(v);
3891
- }
3892
-
3893
- static void iter_robject_max(na_loop_t* const lp) {
3894
- size_t n;
3895
- char *p1, *p2;
3896
- ssize_t s1;
3897
-
3898
- INIT_COUNTER(lp, n);
3899
- INIT_PTR(lp, 0, p1, s1);
3900
- p2 = lp->args[1].ptr + lp->args[1].iter[0].pos;
3901
-
3902
- *(dtype*)p2 = f_max(n, p1, s1);
3903
- }
3904
- static void iter_robject_max_nan(na_loop_t* const lp) {
3905
- size_t n;
3906
- char *p1, *p2;
3907
- ssize_t s1;
3908
-
3909
- INIT_COUNTER(lp, n);
3910
- INIT_PTR(lp, 0, p1, s1);
3911
- p2 = lp->args[1].ptr + lp->args[1].iter[0].pos;
3912
-
3913
- *(dtype*)p2 = f_max_nan(n, p1, s1);
3914
- }
3915
-
3916
- /*
3917
- max of self.
3918
- @overload max(axis:nil, keepdims:false, nan:false)
3919
- @param [TrueClass] nan If true, apply NaN-aware algorithm (avoid NaN for sum/mean etc, or,
3920
- return NaN for min/max etc).
3921
- @param [Numeric,Array,Range] axis Performs max along the axis.
3922
- @param [TrueClass] keepdims If true, the reduced axes are left in the result array as
3923
- dimensions with size one.
3924
- @return [Numo::RObject] returns result of max.
3925
- */
3926
- static VALUE robject_max(int argc, VALUE* argv, VALUE self) {
3927
- VALUE v, reduce;
3928
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { sym_reduce, 0 } };
3929
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
3930
- ndfunc_t ndf = { iter_robject_max, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE, 2, 1, ain, aout };
3931
-
3932
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, iter_robject_max_nan);
3933
-
3934
- v = na_ndloop(&ndf, 2, self, reduce);
3935
-
3936
- return robject_extract(v);
3937
- }
3938
-
3939
- static void iter_robject_ptp(na_loop_t* const lp) {
3940
- size_t n;
3941
- char *p1, *p2;
3942
- ssize_t s1;
3943
-
3944
- INIT_COUNTER(lp, n);
3945
- INIT_PTR(lp, 0, p1, s1);
3946
- p2 = lp->args[1].ptr + lp->args[1].iter[0].pos;
3947
-
3948
- *(dtype*)p2 = f_ptp(n, p1, s1);
3949
- }
3950
- static void iter_robject_ptp_nan(na_loop_t* const lp) {
3951
- size_t n;
3952
- char *p1, *p2;
3953
- ssize_t s1;
3954
-
3955
- INIT_COUNTER(lp, n);
3956
- INIT_PTR(lp, 0, p1, s1);
3957
- p2 = lp->args[1].ptr + lp->args[1].iter[0].pos;
3958
-
3959
- *(dtype*)p2 = f_ptp_nan(n, p1, s1);
3960
- }
3961
-
3962
- /*
3963
- ptp of self.
3964
- @overload ptp(axis:nil, keepdims:false, nan:false)
3965
- @param [TrueClass] nan If true, apply NaN-aware algorithm (avoid NaN for sum/mean etc, or,
3966
- return NaN for min/max etc).
3967
- @param [Numeric,Array,Range] axis Performs ptp along the axis.
3968
- @param [TrueClass] keepdims If true, the reduced axes are left in the result array as
3969
- dimensions with size one.
3970
- @return [Numo::RObject] returns result of ptp.
3971
- */
3972
- static VALUE robject_ptp(int argc, VALUE* argv, VALUE self) {
3973
- VALUE v, reduce;
3974
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { sym_reduce, 0 } };
3975
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
3976
- ndfunc_t ndf = { iter_robject_ptp, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE, 2, 1, ain, aout };
3977
-
3978
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, iter_robject_ptp_nan);
3979
-
3980
- v = na_ndloop(&ndf, 2, self, reduce);
3981
-
3982
- return robject_extract(v);
3983
- }
3984
-
3985
- #define idx_t int64_t
3986
- static void iter_robject_max_index_index64(na_loop_t* const lp) {
3987
- size_t n, idx;
3988
- char *d_ptr, *i_ptr, *o_ptr;
3989
- ssize_t d_step, i_step;
3990
-
3991
- INIT_COUNTER(lp, n);
3992
- INIT_PTR(lp, 0, d_ptr, d_step);
3993
-
3994
- idx = f_max_index(n, d_ptr, d_step);
3995
-
3996
- INIT_PTR(lp, 1, i_ptr, i_step);
3997
- o_ptr = NDL_PTR(lp, 2);
3998
- *(idx_t*)o_ptr = *(idx_t*)(i_ptr + i_step * idx);
3999
- }
4000
- #undef idx_t
4001
-
4002
- #define idx_t int32_t
4003
- static void iter_robject_max_index_index32(na_loop_t* const lp) {
4004
- size_t n, idx;
4005
- char *d_ptr, *i_ptr, *o_ptr;
4006
- ssize_t d_step, i_step;
4007
-
4008
- INIT_COUNTER(lp, n);
4009
- INIT_PTR(lp, 0, d_ptr, d_step);
4010
-
4011
- idx = f_max_index(n, d_ptr, d_step);
4012
-
4013
- INIT_PTR(lp, 1, i_ptr, i_step);
4014
- o_ptr = NDL_PTR(lp, 2);
4015
- *(idx_t*)o_ptr = *(idx_t*)(i_ptr + i_step * idx);
4016
- }
4017
- #undef idx_t
4018
-
4019
- #define idx_t int64_t
4020
- static void iter_robject_max_index_index64_nan(na_loop_t* const lp) {
4021
- size_t n, idx;
4022
- char *d_ptr, *i_ptr, *o_ptr;
4023
- ssize_t d_step, i_step;
4024
-
4025
- INIT_COUNTER(lp, n);
4026
- INIT_PTR(lp, 0, d_ptr, d_step);
4027
-
4028
- idx = f_max_index_nan(n, d_ptr, d_step);
4029
-
4030
- INIT_PTR(lp, 1, i_ptr, i_step);
4031
- o_ptr = NDL_PTR(lp, 2);
4032
- *(idx_t*)o_ptr = *(idx_t*)(i_ptr + i_step * idx);
4033
- }
4034
- #undef idx_t
4035
-
4036
- #define idx_t int32_t
4037
- static void iter_robject_max_index_index32_nan(na_loop_t* const lp) {
4038
- size_t n, idx;
4039
- char *d_ptr, *i_ptr, *o_ptr;
4040
- ssize_t d_step, i_step;
4041
-
4042
- INIT_COUNTER(lp, n);
4043
- INIT_PTR(lp, 0, d_ptr, d_step);
4044
-
4045
- idx = f_max_index_nan(n, d_ptr, d_step);
4046
-
4047
- INIT_PTR(lp, 1, i_ptr, i_step);
4048
- o_ptr = NDL_PTR(lp, 2);
4049
- *(idx_t*)o_ptr = *(idx_t*)(i_ptr + i_step * idx);
4050
- }
4051
- #undef idx_t
4052
-
4053
- /*
4054
- Index of the maximum value.
4055
- @overload max_index(axis:nil, nan:false)
4056
- @param [TrueClass] nan If true, apply NaN-aware algorithm (return NaN posision if exist).
4057
- @param [Numeric,Array,Range] axis Finds maximum values along the axis and returns **flat
4058
- 1-d indices**.
4059
- @return [Integer,Numo::Int] returns result indices.
4060
- @see #argmax
4061
- @see #max
4062
-
4063
- @example
4064
- a = Numo::NArray[3,4,1,2]
4065
- a.max_index #=> 1
4066
-
4067
- b = Numo::NArray[[3,4,1],[2,0,5]]
4068
- b.max_index #=> 5
4069
- b.max_index(axis:1) #=> [1, 5]
4070
- b.max_index(axis:0) #=> [0, 1, 5]
4071
- b[b.max_index(axis:0)] #=> [3, 4, 5]
4072
- */
4073
- static VALUE robject_max_index(int argc, VALUE* argv, VALUE self) {
4074
- narray_t* na;
4075
- VALUE idx, reduce;
4076
- ndfunc_arg_in_t ain[3] = { { Qnil, 0 }, { Qnil, 0 }, { sym_reduce, 0 } };
4077
- ndfunc_arg_out_t aout[1] = { { 0, 0, 0 } };
4078
- ndfunc_t ndf = { 0, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE | NDF_EXTRACT, 3, 1, ain, aout };
4079
-
4080
- GetNArray(self, na);
4081
- if (na->ndim == 0) {
4082
- return INT2FIX(0);
4083
- }
4084
- if (na->size > (~(u_int32_t)0)) {
4085
- aout[0].type = numo_cInt64;
4086
- idx = nary_new(numo_cInt64, na->ndim, na->shape);
4087
- ndf.func = iter_robject_max_index_index64;
4088
-
4089
- reduce =
4090
- na_reduce_dimension(argc, argv, 1, &self, &ndf, iter_robject_max_index_index64_nan);
4091
-
4092
- } else {
4093
- aout[0].type = numo_cInt32;
4094
- idx = nary_new(numo_cInt32, na->ndim, na->shape);
4095
- ndf.func = iter_robject_max_index_index32;
4096
-
4097
- reduce =
4098
- na_reduce_dimension(argc, argv, 1, &self, &ndf, iter_robject_max_index_index32_nan);
4099
- }
4100
- rb_funcall(idx, rb_intern("seq"), 0);
4101
-
4102
- return na_ndloop(&ndf, 3, self, idx, reduce);
4103
- }
4104
-
4105
- #define idx_t int64_t
4106
- static void iter_robject_min_index_index64(na_loop_t* const lp) {
4107
- size_t n, idx;
4108
- char *d_ptr, *i_ptr, *o_ptr;
4109
- ssize_t d_step, i_step;
4110
-
4111
- INIT_COUNTER(lp, n);
4112
- INIT_PTR(lp, 0, d_ptr, d_step);
4113
-
4114
- idx = f_min_index(n, d_ptr, d_step);
4115
-
4116
- INIT_PTR(lp, 1, i_ptr, i_step);
4117
- o_ptr = NDL_PTR(lp, 2);
4118
- *(idx_t*)o_ptr = *(idx_t*)(i_ptr + i_step * idx);
4119
- }
4120
- #undef idx_t
4121
-
4122
- #define idx_t int32_t
4123
- static void iter_robject_min_index_index32(na_loop_t* const lp) {
4124
- size_t n, idx;
4125
- char *d_ptr, *i_ptr, *o_ptr;
4126
- ssize_t d_step, i_step;
4127
-
4128
- INIT_COUNTER(lp, n);
4129
- INIT_PTR(lp, 0, d_ptr, d_step);
4130
-
4131
- idx = f_min_index(n, d_ptr, d_step);
4132
-
4133
- INIT_PTR(lp, 1, i_ptr, i_step);
4134
- o_ptr = NDL_PTR(lp, 2);
4135
- *(idx_t*)o_ptr = *(idx_t*)(i_ptr + i_step * idx);
4136
- }
4137
- #undef idx_t
4138
-
4139
- #define idx_t int64_t
4140
- static void iter_robject_min_index_index64_nan(na_loop_t* const lp) {
4141
- size_t n, idx;
4142
- char *d_ptr, *i_ptr, *o_ptr;
4143
- ssize_t d_step, i_step;
4144
-
4145
- INIT_COUNTER(lp, n);
4146
- INIT_PTR(lp, 0, d_ptr, d_step);
4147
-
4148
- idx = f_min_index_nan(n, d_ptr, d_step);
4149
-
4150
- INIT_PTR(lp, 1, i_ptr, i_step);
4151
- o_ptr = NDL_PTR(lp, 2);
4152
- *(idx_t*)o_ptr = *(idx_t*)(i_ptr + i_step * idx);
4153
- }
4154
- #undef idx_t
4155
-
4156
- #define idx_t int32_t
4157
- static void iter_robject_min_index_index32_nan(na_loop_t* const lp) {
4158
- size_t n, idx;
4159
- char *d_ptr, *i_ptr, *o_ptr;
4160
- ssize_t d_step, i_step;
4161
-
4162
- INIT_COUNTER(lp, n);
4163
- INIT_PTR(lp, 0, d_ptr, d_step);
4164
-
4165
- idx = f_min_index_nan(n, d_ptr, d_step);
4166
-
4167
- INIT_PTR(lp, 1, i_ptr, i_step);
4168
- o_ptr = NDL_PTR(lp, 2);
4169
- *(idx_t*)o_ptr = *(idx_t*)(i_ptr + i_step * idx);
4170
- }
4171
- #undef idx_t
4172
-
4173
- /*
4174
- Index of the minimum value.
4175
- @overload min_index(axis:nil, nan:false)
4176
- @param [TrueClass] nan If true, apply NaN-aware algorithm (return NaN posision if exist).
4177
- @param [Numeric,Array,Range] axis Finds minimum values along the axis and returns **flat
4178
- 1-d indices**.
4179
- @return [Integer,Numo::Int] returns result indices.
4180
- @see #argmin
4181
- @see #min
4182
-
4183
- @example
4184
- a = Numo::NArray[3,4,1,2]
4185
- a.min_index #=> 2
4186
-
4187
- b = Numo::NArray[[3,4,1],[2,0,5]]
4188
- b.min_index #=> 4
4189
- b.min_index(axis:1) #=> [2, 4]
4190
- b.min_index(axis:0) #=> [3, 4, 2]
4191
- b[b.min_index(axis:0)] #=> [2, 0, 1]
4192
- */
4193
- static VALUE robject_min_index(int argc, VALUE* argv, VALUE self) {
4194
- narray_t* na;
4195
- VALUE idx, reduce;
4196
- ndfunc_arg_in_t ain[3] = { { Qnil, 0 }, { Qnil, 0 }, { sym_reduce, 0 } };
4197
- ndfunc_arg_out_t aout[1] = { { 0, 0, 0 } };
4198
- ndfunc_t ndf = { 0, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE | NDF_EXTRACT, 3, 1, ain, aout };
4199
-
4200
- GetNArray(self, na);
4201
- if (na->ndim == 0) {
4202
- return INT2FIX(0);
4203
- }
4204
- if (na->size > (~(u_int32_t)0)) {
4205
- aout[0].type = numo_cInt64;
4206
- idx = nary_new(numo_cInt64, na->ndim, na->shape);
4207
- ndf.func = iter_robject_min_index_index64;
4208
-
4209
- reduce =
4210
- na_reduce_dimension(argc, argv, 1, &self, &ndf, iter_robject_min_index_index64_nan);
4211
-
4212
- } else {
4213
- aout[0].type = numo_cInt32;
4214
- idx = nary_new(numo_cInt32, na->ndim, na->shape);
4215
- ndf.func = iter_robject_min_index_index32;
4216
-
4217
- reduce =
4218
- na_reduce_dimension(argc, argv, 1, &self, &ndf, iter_robject_min_index_index32_nan);
4219
- }
4220
- rb_funcall(idx, rb_intern("seq"), 0);
4221
-
4222
- return na_ndloop(&ndf, 3, self, idx, reduce);
4223
- }
4224
-
4225
- #define idx_t int64_t
4226
- static void iter_robject_argmax_arg64(na_loop_t* const lp) {
4227
- size_t n, idx;
4228
- char *d_ptr, *o_ptr;
4229
- ssize_t d_step;
4230
-
4231
- INIT_COUNTER(lp, n);
4232
- INIT_PTR(lp, 0, d_ptr, d_step);
4233
-
4234
- idx = f_max_index(n, d_ptr, d_step);
4235
-
4236
- o_ptr = NDL_PTR(lp, 1);
4237
- *(idx_t*)o_ptr = (idx_t)idx;
4238
- }
4239
- #undef idx_t
4240
-
4241
- #define idx_t int32_t
4242
- static void iter_robject_argmax_arg32(na_loop_t* const lp) {
4243
- size_t n, idx;
4244
- char *d_ptr, *o_ptr;
4245
- ssize_t d_step;
4246
-
4247
- INIT_COUNTER(lp, n);
4248
- INIT_PTR(lp, 0, d_ptr, d_step);
4249
-
4250
- idx = f_max_index(n, d_ptr, d_step);
4251
-
4252
- o_ptr = NDL_PTR(lp, 1);
4253
- *(idx_t*)o_ptr = (idx_t)idx;
4254
- }
4255
- #undef idx_t
4256
-
4257
- #define idx_t int64_t
4258
- static void iter_robject_argmax_arg64_nan(na_loop_t* const lp) {
4259
- size_t n, idx;
4260
- char *d_ptr, *o_ptr;
4261
- ssize_t d_step;
4262
-
4263
- INIT_COUNTER(lp, n);
4264
- INIT_PTR(lp, 0, d_ptr, d_step);
4265
-
4266
- idx = f_max_index_nan(n, d_ptr, d_step);
4267
-
4268
- o_ptr = NDL_PTR(lp, 1);
4269
- *(idx_t*)o_ptr = (idx_t)idx;
4270
- }
4271
- #undef idx_t
4272
-
4273
- #define idx_t int32_t
4274
- static void iter_robject_argmax_arg32_nan(na_loop_t* const lp) {
4275
- size_t n, idx;
4276
- char *d_ptr, *o_ptr;
4277
- ssize_t d_step;
4278
-
4279
- INIT_COUNTER(lp, n);
4280
- INIT_PTR(lp, 0, d_ptr, d_step);
4281
-
4282
- idx = f_max_index_nan(n, d_ptr, d_step);
4283
-
4284
- o_ptr = NDL_PTR(lp, 1);
4285
- *(idx_t*)o_ptr = (idx_t)idx;
4286
- }
4287
- #undef idx_t
4288
-
4289
- /*
4290
- Index of the maximum value.
4291
- @overload argmax(axis:nil, nan:false)
4292
- @param [TrueClass] nan If true, apply NaN-aware algorithm (return NaN posision if exist).
4293
- @param [Numeric,Array,Range] axis Finds maximum values along the axis and returns **indices
4294
- along the axis**.
4295
- @return [Integer,Numo::Int] returns the result indices.
4296
- @see #max_index
4297
- @see #max
4298
-
4299
- @example
4300
- a = Numo::NArray[3,4,1,2]
4301
- a.argmax #=> 1
4302
-
4303
- b = Numo::NArray[[3,4,1],[2,0,5]]
4304
- b.argmax #=> 5
4305
- b.argmax(axis:1) #=> [1, 2]
4306
- b.argmax(axis:0) #=> [0, 0, 1]
4307
- b.at(b.argmax(axis:0), 0..-1) #=> [3, 4, 5]
4308
- */
4309
- static VALUE robject_argmax(int argc, VALUE* argv, VALUE self) {
4310
- narray_t* na;
4311
- VALUE reduce;
4312
- ndfunc_arg_in_t ain[2] = { { Qnil, 0 }, { sym_reduce, 0 } };
4313
- ndfunc_arg_out_t aout[1] = { { 0, 0, 0 } };
4314
- ndfunc_t ndf = { 0, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE | NDF_EXTRACT, 2, 1, ain, aout };
4315
-
4316
- GetNArray(self, na);
4317
- if (na->ndim == 0) {
4318
- return INT2FIX(0);
4319
- }
4320
- if (na->size > (~(u_int32_t)0)) {
4321
- aout[0].type = numo_cInt64;
4322
- ndf.func = iter_robject_argmax_arg64;
4323
-
4324
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, iter_robject_argmax_arg64_nan);
4325
-
4326
- } else {
4327
- aout[0].type = numo_cInt32;
4328
- ndf.func = iter_robject_argmax_arg32;
4329
-
4330
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, iter_robject_argmax_arg32_nan);
4331
- }
4332
-
4333
- return na_ndloop(&ndf, 2, self, reduce);
4334
- }
4335
-
4336
- #define idx_t int64_t
4337
- static void iter_robject_argmin_arg64(na_loop_t* const lp) {
4338
- size_t n, idx;
4339
- char *d_ptr, *o_ptr;
4340
- ssize_t d_step;
4341
-
4342
- INIT_COUNTER(lp, n);
4343
- INIT_PTR(lp, 0, d_ptr, d_step);
4344
-
4345
- idx = f_min_index(n, d_ptr, d_step);
4346
-
4347
- o_ptr = NDL_PTR(lp, 1);
4348
- *(idx_t*)o_ptr = (idx_t)idx;
4349
- }
4350
- #undef idx_t
4351
-
4352
- #define idx_t int32_t
4353
- static void iter_robject_argmin_arg32(na_loop_t* const lp) {
4354
- size_t n, idx;
4355
- char *d_ptr, *o_ptr;
4356
- ssize_t d_step;
4357
-
4358
- INIT_COUNTER(lp, n);
4359
- INIT_PTR(lp, 0, d_ptr, d_step);
4360
-
4361
- idx = f_min_index(n, d_ptr, d_step);
4362
-
4363
- o_ptr = NDL_PTR(lp, 1);
4364
- *(idx_t*)o_ptr = (idx_t)idx;
4365
- }
4366
- #undef idx_t
4367
-
4368
- #define idx_t int64_t
4369
- static void iter_robject_argmin_arg64_nan(na_loop_t* const lp) {
4370
- size_t n, idx;
4371
- char *d_ptr, *o_ptr;
4372
- ssize_t d_step;
4373
-
4374
- INIT_COUNTER(lp, n);
4375
- INIT_PTR(lp, 0, d_ptr, d_step);
4376
-
4377
- idx = f_min_index_nan(n, d_ptr, d_step);
4378
-
4379
- o_ptr = NDL_PTR(lp, 1);
4380
- *(idx_t*)o_ptr = (idx_t)idx;
4381
- }
4382
- #undef idx_t
4383
-
4384
- #define idx_t int32_t
4385
- static void iter_robject_argmin_arg32_nan(na_loop_t* const lp) {
4386
- size_t n, idx;
4387
- char *d_ptr, *o_ptr;
4388
- ssize_t d_step;
4389
-
4390
- INIT_COUNTER(lp, n);
4391
- INIT_PTR(lp, 0, d_ptr, d_step);
4392
-
4393
- idx = f_min_index_nan(n, d_ptr, d_step);
4394
-
4395
- o_ptr = NDL_PTR(lp, 1);
4396
- *(idx_t*)o_ptr = (idx_t)idx;
4397
- }
4398
- #undef idx_t
4399
-
4400
- /*
4401
- Index of the minimum value.
4402
- @overload argmin(axis:nil, nan:false)
4403
- @param [TrueClass] nan If true, apply NaN-aware algorithm (return NaN posision if exist).
4404
- @param [Numeric,Array,Range] axis Finds minimum values along the axis and returns **indices
4405
- along the axis**.
4406
- @return [Integer,Numo::Int] returns the result indices.
4407
- @see #min_index
4408
- @see #min
4409
-
4410
- @example
4411
- a = Numo::NArray[3,4,1,2]
4412
- a.argmin #=> 2
4413
-
4414
- b = Numo::NArray[[3,4,1],[2,0,5]]
4415
- b.argmin #=> 4
4416
- b.argmin(axis:1) #=> [2, 1]
4417
- b.argmin(axis:0) #=> [1, 1, 0]
4418
- b.at(b.argmin(axis:0), 0..-1) #=> [2, 0, 1]
4419
- */
4420
- static VALUE robject_argmin(int argc, VALUE* argv, VALUE self) {
4421
- narray_t* na;
4422
- VALUE reduce;
4423
- ndfunc_arg_in_t ain[2] = { { Qnil, 0 }, { sym_reduce, 0 } };
4424
- ndfunc_arg_out_t aout[1] = { { 0, 0, 0 } };
4425
- ndfunc_t ndf = { 0, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE | NDF_EXTRACT, 2, 1, ain, aout };
4426
-
4427
- GetNArray(self, na);
4428
- if (na->ndim == 0) {
4429
- return INT2FIX(0);
4430
- }
4431
- if (na->size > (~(u_int32_t)0)) {
4432
- aout[0].type = numo_cInt64;
4433
- ndf.func = iter_robject_argmin_arg64;
4434
-
4435
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, iter_robject_argmin_arg64_nan);
4436
-
4437
- } else {
4438
- aout[0].type = numo_cInt32;
4439
- ndf.func = iter_robject_argmin_arg32;
4440
-
4441
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, iter_robject_argmin_arg32_nan);
4442
- }
4443
-
4444
- return na_ndloop(&ndf, 2, self, reduce);
4445
- }
4446
-
4447
- static void iter_robject_minmax(na_loop_t* const lp) {
4448
- size_t n;
4449
- char* p1;
4450
- ssize_t s1;
4451
- dtype xmin, xmax;
4452
-
4453
- INIT_COUNTER(lp, n);
4454
- INIT_PTR(lp, 0, p1, s1);
4455
-
4456
- f_minmax(n, p1, s1, &xmin, &xmax);
4457
-
4458
- *(dtype*)(lp->args[1].ptr + lp->args[1].iter[0].pos) = xmin;
4459
- *(dtype*)(lp->args[2].ptr + lp->args[2].iter[0].pos) = xmax;
4460
- }
4461
- static void iter_robject_minmax_nan(na_loop_t* const lp) {
4462
- size_t n;
4463
- char* p1;
4464
- ssize_t s1;
4465
- dtype xmin, xmax;
4466
-
4467
- INIT_COUNTER(lp, n);
4468
- INIT_PTR(lp, 0, p1, s1);
4469
-
4470
- f_minmax_nan(n, p1, s1, &xmin, &xmax);
4471
-
4472
- *(dtype*)(lp->args[1].ptr + lp->args[1].iter[0].pos) = xmin;
4473
- *(dtype*)(lp->args[2].ptr + lp->args[2].iter[0].pos) = xmax;
4474
- }
4475
-
4476
- /*
4477
- minmax of self.
4478
- @overload minmax(axis:nil, keepdims:false, nan:false)
4479
- @param [TrueClass] nan If true, apply NaN-aware algorithm (return NaN if exist).
4480
- @param [Numeric,Array,Range] axis Finds min-max along the axis.
4481
- @param [TrueClass] keepdims (keyword) If true, the reduced axes are left in the result array
4482
- as dimensions with size one.
4483
- @return [Numo::RObject,Numo::RObject] min and max of self.
4484
- */
4485
- static VALUE robject_minmax(int argc, VALUE* argv, VALUE self) {
4486
- VALUE reduce;
4487
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { sym_reduce, 0 } };
4488
- ndfunc_arg_out_t aout[2] = { { cT, 0 }, { cT, 0 } };
4489
- ndfunc_t ndf = {
4490
- iter_robject_minmax, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE | NDF_EXTRACT, 2, 2, ain, aout
4491
- };
4492
-
4493
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, iter_robject_minmax_nan);
4494
-
4495
- return na_ndloop(&ndf, 2, self, reduce);
4496
- }
4497
-
4498
- static void iter_robject_s_maximum(na_loop_t* const lp) {
4499
- size_t i, n;
4500
- char *p1, *p2, *p3;
4501
- ssize_t s1, s2, s3;
4502
-
4503
- INIT_COUNTER(lp, n);
4504
- INIT_PTR(lp, 0, p1, s1);
4505
- INIT_PTR(lp, 1, p2, s2);
4506
- INIT_PTR(lp, 2, p3, s3);
4507
-
4508
- for (i = 0; i < n; i++) {
4509
- dtype x, y, z;
4510
- GET_DATA_STRIDE(p1, s1, dtype, x);
4511
- GET_DATA_STRIDE(p2, s2, dtype, y);
4512
- GET_DATA(p3, dtype, z);
4513
- z = f_maximum(x, y);
4514
- SET_DATA_STRIDE(p3, s3, dtype, z);
4515
- }
4516
- }
4517
- static void iter_robject_s_maximum_nan(na_loop_t* const lp) {
4518
- size_t i, n;
4519
- char *p1, *p2, *p3;
4520
- ssize_t s1, s2, s3;
4521
-
4522
- INIT_COUNTER(lp, n);
4523
- INIT_PTR(lp, 0, p1, s1);
4524
- INIT_PTR(lp, 1, p2, s2);
4525
- INIT_PTR(lp, 2, p3, s3);
4526
-
4527
- for (i = 0; i < n; i++) {
4528
- dtype x, y, z;
4529
- GET_DATA_STRIDE(p1, s1, dtype, x);
4530
- GET_DATA_STRIDE(p2, s2, dtype, y);
4531
- GET_DATA(p3, dtype, z);
4532
- z = f_maximum_nan(x, y);
4533
- SET_DATA_STRIDE(p3, s3, dtype, z);
4534
- }
4535
- }
4536
-
4537
- static VALUE robject_s_maximum(int argc, VALUE* argv, VALUE mod) {
4538
- VALUE a1 = Qnil;
4539
- VALUE a2 = Qnil;
4540
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
4541
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
4542
- ndfunc_t ndf = { iter_robject_s_maximum, STRIDE_LOOP_NIP, 2, 1, ain, aout };
4543
-
4544
- VALUE kw_hash = Qnil;
4545
- ID kw_table[1] = { id_nan };
4546
- VALUE opts[1] = { Qundef };
4547
-
4548
- rb_scan_args(argc, argv, "20:", &a1, &a2, &kw_hash);
4549
- rb_get_kwargs(kw_hash, kw_table, 0, 1, opts);
4550
- if (opts[0] != Qundef) {
4551
- ndf.func = iter_robject_s_maximum_nan;
4552
- }
4553
-
4554
- return na_ndloop(&ndf, 2, a1, a2);
4555
- }
4556
-
4557
- /*
4558
- Element-wise minimum of two arrays.
4559
-
4560
- @overload minimum(a1, a2, nan:false)
4561
- @param [Numo::NArray,Numeric] a1 The array to be compared.
4562
- @param [Numo::NArray,Numeric] a2 The array to be compared.
4563
- @param [TrueClass] nan If true, apply NaN-aware algorithm (return NaN if exist).
4564
- @return [Numo::RObject]
4565
- */
4566
-
4567
- static void iter_robject_s_minimum(na_loop_t* const lp) {
4568
- size_t i, n;
4569
- char *p1, *p2, *p3;
4570
- ssize_t s1, s2, s3;
4571
-
4572
- INIT_COUNTER(lp, n);
4573
- INIT_PTR(lp, 0, p1, s1);
4574
- INIT_PTR(lp, 1, p2, s2);
4575
- INIT_PTR(lp, 2, p3, s3);
4576
-
4577
- for (i = 0; i < n; i++) {
4578
- dtype x, y, z;
4579
- GET_DATA_STRIDE(p1, s1, dtype, x);
4580
- GET_DATA_STRIDE(p2, s2, dtype, y);
4581
- GET_DATA(p3, dtype, z);
4582
- z = f_minimum(x, y);
4583
- SET_DATA_STRIDE(p3, s3, dtype, z);
4584
- }
4585
- }
4586
- static void iter_robject_s_minimum_nan(na_loop_t* const lp) {
4587
- size_t i, n;
4588
- char *p1, *p2, *p3;
4589
- ssize_t s1, s2, s3;
4590
-
4591
- INIT_COUNTER(lp, n);
4592
- INIT_PTR(lp, 0, p1, s1);
4593
- INIT_PTR(lp, 1, p2, s2);
4594
- INIT_PTR(lp, 2, p3, s3);
4595
-
4596
- for (i = 0; i < n; i++) {
4597
- dtype x, y, z;
4598
- GET_DATA_STRIDE(p1, s1, dtype, x);
4599
- GET_DATA_STRIDE(p2, s2, dtype, y);
4600
- GET_DATA(p3, dtype, z);
4601
- z = f_minimum_nan(x, y);
4602
- SET_DATA_STRIDE(p3, s3, dtype, z);
4603
- }
4604
- }
4605
-
4606
- static VALUE robject_s_minimum(int argc, VALUE* argv, VALUE mod) {
4607
- VALUE a1 = Qnil;
4608
- VALUE a2 = Qnil;
4609
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
4610
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
4611
- ndfunc_t ndf = { iter_robject_s_minimum, STRIDE_LOOP_NIP, 2, 1, ain, aout };
4612
-
4613
- VALUE kw_hash = Qnil;
4614
- ID kw_table[1] = { id_nan };
4615
- VALUE opts[1] = { Qundef };
4616
-
4617
- rb_scan_args(argc, argv, "20:", &a1, &a2, &kw_hash);
4618
- rb_get_kwargs(kw_hash, kw_table, 0, 1, opts);
4619
- if (opts[0] != Qundef) {
4620
- ndf.func = iter_robject_s_minimum_nan;
4621
- }
4622
-
4623
- return na_ndloop(&ndf, 2, a1, a2);
4624
- }
4625
-
4626
- static void iter_robject_cumsum(na_loop_t* const lp) {
4627
- size_t i;
4628
- char *p1, *p2;
4629
- ssize_t s1, s2;
4630
- dtype x, y;
4631
-
4632
- INIT_COUNTER(lp, i);
4633
- INIT_PTR(lp, 0, p1, s1);
4634
- INIT_PTR(lp, 1, p2, s2);
4635
-
4636
- GET_DATA_STRIDE(p1, s1, dtype, x);
4637
- SET_DATA_STRIDE(p2, s2, dtype, x);
4638
- for (i--; i--;) {
4639
- GET_DATA_STRIDE(p1, s1, dtype, y);
4640
- m_cumsum(x, y);
4641
- SET_DATA_STRIDE(p2, s2, dtype, x);
4642
- }
4643
- }
4644
- static void iter_robject_cumsum_nan(na_loop_t* const lp) {
4645
- size_t i;
4646
- char *p1, *p2;
4647
- ssize_t s1, s2;
4648
- dtype x, y;
4649
-
4650
- INIT_COUNTER(lp, i);
4651
- INIT_PTR(lp, 0, p1, s1);
4652
- INIT_PTR(lp, 1, p2, s2);
4653
-
4654
- GET_DATA_STRIDE(p1, s1, dtype, x);
4655
- SET_DATA_STRIDE(p2, s2, dtype, x);
4656
- for (i--; i--;) {
4657
- GET_DATA_STRIDE(p1, s1, dtype, y);
4658
- m_cumsum_nan(x, y);
4659
- SET_DATA_STRIDE(p2, s2, dtype, x);
4660
- }
4661
- }
4662
-
4663
- /*
4664
- cumsum of self.
4665
- @overload cumsum(axis:nil, nan:false)
4666
- @param [Numeric,Array,Range] axis Performs cumsum along the axis.
4667
- @param [TrueClass] nan If true, apply NaN-aware algorithm (avoid NaN if exists).
4668
- @return [Numo::RObject] cumsum of self.
4669
- */
4670
- static VALUE robject_cumsum(int argc, VALUE* argv, VALUE self) {
4671
- VALUE reduce;
4672
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { sym_reduce, 0 } };
4673
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
4674
- ndfunc_t ndf = {
4675
- iter_robject_cumsum, STRIDE_LOOP | NDF_FLAT_REDUCE | NDF_CUM, 2, 1, ain, aout
4676
- };
4677
-
4678
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, iter_robject_cumsum_nan);
4679
-
4680
- return na_ndloop(&ndf, 2, self, reduce);
4681
- }
4682
-
4683
- static void iter_robject_cumprod(na_loop_t* const lp) {
4684
- size_t i;
4685
- char *p1, *p2;
4686
- ssize_t s1, s2;
4687
- dtype x, y;
4688
-
4689
- INIT_COUNTER(lp, i);
4690
- INIT_PTR(lp, 0, p1, s1);
4691
- INIT_PTR(lp, 1, p2, s2);
4692
-
4693
- GET_DATA_STRIDE(p1, s1, dtype, x);
4694
- SET_DATA_STRIDE(p2, s2, dtype, x);
4695
- for (i--; i--;) {
4696
- GET_DATA_STRIDE(p1, s1, dtype, y);
4697
- m_cumprod(x, y);
4698
- SET_DATA_STRIDE(p2, s2, dtype, x);
4699
- }
4700
- }
4701
- static void iter_robject_cumprod_nan(na_loop_t* const lp) {
4702
- size_t i;
4703
- char *p1, *p2;
4704
- ssize_t s1, s2;
4705
- dtype x, y;
4706
-
4707
- INIT_COUNTER(lp, i);
4708
- INIT_PTR(lp, 0, p1, s1);
4709
- INIT_PTR(lp, 1, p2, s2);
4710
-
4711
- GET_DATA_STRIDE(p1, s1, dtype, x);
4712
- SET_DATA_STRIDE(p2, s2, dtype, x);
4713
- for (i--; i--;) {
4714
- GET_DATA_STRIDE(p1, s1, dtype, y);
4715
- m_cumprod_nan(x, y);
4716
- SET_DATA_STRIDE(p2, s2, dtype, x);
4717
- }
4718
- }
4719
-
4720
- /*
4721
- cumprod of self.
4722
- @overload cumprod(axis:nil, nan:false)
4723
- @param [Numeric,Array,Range] axis Performs cumprod along the axis.
4724
- @param [TrueClass] nan If true, apply NaN-aware algorithm (avoid NaN if exists).
4725
- @return [Numo::RObject] cumprod of self.
4726
- */
4727
- static VALUE robject_cumprod(int argc, VALUE* argv, VALUE self) {
4728
- VALUE reduce;
4729
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { sym_reduce, 0 } };
4730
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
4731
- ndfunc_t ndf = {
4732
- iter_robject_cumprod, STRIDE_LOOP | NDF_FLAT_REDUCE | NDF_CUM, 2, 1, ain, aout
4733
- };
4734
-
4735
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, iter_robject_cumprod_nan);
4736
-
4737
- return na_ndloop(&ndf, 2, self, reduce);
4738
- }
4739
-
4740
- //
4741
- static void iter_robject_mulsum(na_loop_t* const lp) {
4742
- size_t i, n;
4743
- char *p1, *p2, *p3;
4744
- ssize_t s1, s2, s3;
4745
-
4746
- INIT_COUNTER(lp, n);
4747
- INIT_PTR(lp, 0, p1, s1);
4748
- INIT_PTR(lp, 1, p2, s2);
4749
- INIT_PTR(lp, 2, p3, s3);
4750
-
4751
- if (s3 == 0) {
4752
- dtype z;
4753
- // Reduce loop
4754
- GET_DATA(p3, dtype, z);
4755
- for (i = 0; i < n; i++) {
4756
- dtype x, y;
4757
- GET_DATA_STRIDE(p1, s1, dtype, x);
4758
- GET_DATA_STRIDE(p2, s2, dtype, y);
4759
- m_mulsum(x, y, z);
4760
- }
4761
- SET_DATA(p3, dtype, z);
4762
- return;
4763
- } else {
4764
- for (i = 0; i < n; i++) {
4765
- dtype x, y, z;
4766
- GET_DATA_STRIDE(p1, s1, dtype, x);
4767
- GET_DATA_STRIDE(p2, s2, dtype, y);
4768
- GET_DATA(p3, dtype, z);
4769
- m_mulsum(x, y, z);
4770
- SET_DATA_STRIDE(p3, s3, dtype, z);
4771
- }
4772
- }
4773
- }
4774
- //
4775
- static void iter_robject_mulsum_nan(na_loop_t* const lp) {
4776
- size_t i, n;
4777
- char *p1, *p2, *p3;
4778
- ssize_t s1, s2, s3;
4779
-
4780
- INIT_COUNTER(lp, n);
4781
- INIT_PTR(lp, 0, p1, s1);
4782
- INIT_PTR(lp, 1, p2, s2);
4783
- INIT_PTR(lp, 2, p3, s3);
4784
-
4785
- if (s3 == 0) {
4786
- dtype z;
4787
- // Reduce loop
4788
- GET_DATA(p3, dtype, z);
4789
- for (i = 0; i < n; i++) {
4790
- dtype x, y;
4791
- GET_DATA_STRIDE(p1, s1, dtype, x);
4792
- GET_DATA_STRIDE(p2, s2, dtype, y);
4793
- m_mulsum_nan(x, y, z);
4794
- }
4795
- SET_DATA(p3, dtype, z);
4796
- return;
4797
- } else {
4798
- for (i = 0; i < n; i++) {
4799
- dtype x, y, z;
4800
- GET_DATA_STRIDE(p1, s1, dtype, x);
4801
- GET_DATA_STRIDE(p2, s2, dtype, y);
4802
- GET_DATA(p3, dtype, z);
4803
- m_mulsum_nan(x, y, z);
4804
- SET_DATA_STRIDE(p3, s3, dtype, z);
4805
- }
4806
- }
4807
- }
4808
- //
4809
-
4810
- static VALUE robject_mulsum_self(int argc, VALUE* argv, VALUE self) {
4811
- VALUE v, reduce;
4812
- VALUE naryv[2];
4813
- ndfunc_arg_in_t ain[4] = { { cT, 0 }, { cT, 0 }, { sym_reduce, 0 }, { sym_init, 0 } };
4814
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
4815
- ndfunc_t ndf = { iter_robject_mulsum, STRIDE_LOOP_NIP, 4, 1, ain, aout };
4816
-
4817
- if (argc < 1) {
4818
- rb_raise(rb_eArgError, "wrong number of arguments (%d for >=1)", argc);
4819
- }
4820
- // should fix below: [self.ndim,other.ndim].max or?
4821
- naryv[0] = self;
4822
- naryv[1] = argv[0];
4823
- //
4824
- reduce = na_reduce_dimension(argc - 1, argv + 1, 2, naryv, &ndf, iter_robject_mulsum_nan);
4825
- //
4826
-
4827
- v = na_ndloop(&ndf, 4, self, argv[0], reduce, m_mulsum_init);
4828
- return robject_extract(v);
4829
- }
4830
-
4831
- /*
4832
- Binary mulsum.
4833
-
4834
- @overload mulsum(other, axis:nil, keepdims:false, nan:false)
4835
- @param [Numo::NArray,Numeric] other
4836
- @param [Numeric,Array,Range] axis Performs mulsum along the axis.
4837
- @param [TrueClass] keepdims (keyword) If true, the reduced axes are left in the result array
4838
- as dimensions with size one.
4839
- @param [TrueClass] nan (keyword) If true, apply NaN-aware algorithm (avoid NaN if exists).
4840
- @return [Numo::NArray] mulsum of self and other.
4841
- */
4842
- static VALUE robject_mulsum(int argc, VALUE* argv, VALUE self) {
4843
- //
4844
- if (argc < 1) {
4845
- rb_raise(rb_eArgError, "wrong number of arguments (%d for >=1)", argc);
4846
- }
4847
- //
4848
- return robject_mulsum_self(argc, argv, self);
4849
- //
4850
- }
4851
-
4852
- typedef dtype seq_data_t;
4853
-
4854
- typedef size_t seq_count_t;
4855
-
4856
- typedef struct {
4857
- seq_data_t beg;
4858
- seq_data_t step;
4859
- seq_count_t count;
4860
- } seq_opt_t;
4861
-
4862
- static void iter_robject_seq(na_loop_t* const lp) {
4863
- size_t i;
4864
- char* p1;
4865
- ssize_t s1;
4866
- size_t* idx1;
4867
- dtype x;
4868
- seq_data_t beg, step;
4869
- seq_count_t c;
4870
- seq_opt_t* g;
4871
-
4872
- INIT_COUNTER(lp, i);
4873
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
4874
- g = (seq_opt_t*)(lp->opt_ptr);
4875
- beg = g->beg;
4876
- step = g->step;
4877
- c = g->count;
4878
- if (idx1) {
4879
- for (; i--;) {
4880
- x = f_seq(beg, step, c++);
4881
- *(dtype*)(p1 + *idx1) = x;
4882
- idx1++;
4883
- }
4884
- } else {
4885
- for (; i--;) {
4886
- x = f_seq(beg, step, c++);
4887
- *(dtype*)(p1) = x;
4888
- p1 += s1;
4889
- }
4890
- }
4891
- g->count = c;
4892
- }
4893
-
4894
- /*
4895
- Set linear sequence of numbers to self. The sequence is obtained from
4896
- beg+i*step
4897
- where i is 1-dimensional index.
4898
- @overload seq([beg,[step]])
4899
- @param [Numeric] beg beginning of sequence. (default=0)
4900
- @param [Numeric] step step of sequence. (default=1)
4901
- @return [Numo::RObject] self.
4902
- @example
4903
- Numo::DFloat.new(6).seq(1,-0.2)
4904
- # => Numo::DFloat#shape=[6]
4905
- # [1, 0.8, 0.6, 0.4, 0.2, 0]
4906
-
4907
- Numo::DComplex.new(6).seq(1,-0.2+0.2i)
4908
- # => Numo::DComplex#shape=[6]
4909
- # [1+0i, 0.8+0.2i, 0.6+0.4i, 0.4+0.6i, 0.2+0.8i, 0+1i]
4910
- */
4911
- static VALUE robject_seq(int argc, VALUE* argv, VALUE self) {
4912
- seq_opt_t* g;
4913
- VALUE vbeg = Qnil, vstep = Qnil;
4914
- ndfunc_arg_in_t ain[1] = { { OVERWRITE, 0 } };
4915
- ndfunc_t ndf = { iter_robject_seq, FULL_LOOP, 1, 0, ain, 0 };
4916
-
4917
- g = ALLOCA_N(seq_opt_t, 1);
4918
- g->beg = m_zero;
4919
- g->step = m_one;
4920
- g->count = 0;
4921
- rb_scan_args(argc, argv, "02", &vbeg, &vstep);
4922
- if (vbeg != Qnil) {
4923
- g->beg = m_num_to_data(vbeg);
4924
- }
4925
- if (vstep != Qnil) {
4926
- g->step = m_num_to_data(vstep);
4927
- }
4928
-
4929
- na_ndloop3(&ndf, g, 1, self);
4930
- return self;
4931
- }
4932
-
4933
- typedef struct {
4934
- seq_data_t beg;
4935
- seq_data_t step;
4936
- seq_data_t base;
4937
- seq_count_t count;
4938
- } logseq_opt_t;
4939
-
4940
- static void iter_robject_logseq(na_loop_t* const lp) {
4941
- size_t i;
4942
- char* p1;
4943
- ssize_t s1;
4944
- size_t* idx1;
4945
- dtype x;
4946
- seq_data_t beg, step, base;
4947
- seq_count_t c;
4948
- logseq_opt_t* g;
4949
-
4950
- INIT_COUNTER(lp, i);
4951
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
4952
- g = (logseq_opt_t*)(lp->opt_ptr);
4953
- beg = g->beg;
4954
- step = g->step;
4955
- base = g->base;
4956
- c = g->count;
4957
- if (idx1) {
4958
- for (; i--;) {
4959
- x = f_seq(beg, step, c++);
4960
- *(dtype*)(p1 + *idx1) = m_pow(base, x);
4961
- idx1++;
4962
- }
4963
- } else {
4964
- for (; i--;) {
4965
- x = f_seq(beg, step, c++);
4966
- *(dtype*)(p1) = m_pow(base, x);
4967
- p1 += s1;
4968
- }
4969
- }
4970
- g->count = c;
4971
- }
4972
-
4973
- /*
4974
- Set logarithmic sequence of numbers to self. The sequence is obtained from
4975
- `base**(beg+i*step)`
4976
- where i is 1-dimensional index.
4977
- Applicable classes: DFloat, SFloat, DComplex, SCopmplex.
4978
-
4979
- @overload logseq(beg,step,[base])
4980
- @param [Numeric] beg The beginning of sequence.
4981
- @param [Numeric] step The step of sequence.
4982
- @param [Numeric] base The base of log space. (default=10)
4983
- @return [Numo::RObject] self.
4984
-
4985
- @example
4986
- Numo::DFloat.new(5).logseq(4,-1,2)
4987
- # => Numo::DFloat#shape=[5]
4988
- # [16, 8, 4, 2, 1]
4989
-
4990
- Numo::DComplex.new(5).logseq(0,1i*Math::PI/3,Math::E)
4991
- # => Numo::DComplex#shape=[5]
4992
- # [1+7.26156e-310i, 0.5+0.866025i, -0.5+0.866025i, -1+1.22465e-16i, ...]
4993
- */
4994
- static VALUE robject_logseq(int argc, VALUE* argv, VALUE self) {
4995
- logseq_opt_t* g;
4996
- VALUE vbeg, vstep, vbase;
4997
- ndfunc_arg_in_t ain[1] = { { OVERWRITE, 0 } };
4998
- ndfunc_t ndf = { iter_robject_logseq, FULL_LOOP, 1, 0, ain, 0 };
4999
-
5000
- g = ALLOCA_N(logseq_opt_t, 1);
5001
- rb_scan_args(argc, argv, "21", &vbeg, &vstep, &vbase);
5002
- g->beg = m_num_to_data(vbeg);
5003
- g->step = m_num_to_data(vstep);
5004
- if (vbase == Qnil) {
5005
- g->base = m_from_real(10);
5006
- } else {
5007
- g->base = m_num_to_data(vbase);
5008
- }
5009
- na_ndloop3(&ndf, g, 1, self);
5010
- return self;
5011
- }
5012
-
5013
- static void iter_robject_eye(na_loop_t* const lp) {
5014
- size_t n0, n1;
5015
- size_t i0, i1;
5016
- ssize_t s0, s1;
5017
- char *p0, *p1;
5018
- char* g;
5019
- ssize_t kofs;
5020
- dtype data;
5021
-
5022
- g = (char*)(lp->opt_ptr);
5023
- kofs = *(ssize_t*)g;
5024
- data = *(dtype*)(g + sizeof(ssize_t));
5025
-
5026
- n0 = lp->args[0].shape[0];
5027
- n1 = lp->args[0].shape[1];
5028
- s0 = lp->args[0].iter[0].step;
5029
- s1 = lp->args[0].iter[1].step;
5030
- p0 = NDL_PTR(lp, 0);
5031
-
5032
- for (i0 = 0; i0 < n0; i0++) {
5033
- p1 = p0;
5034
- for (i1 = 0; i1 < n1; i1++) {
5035
- *(dtype*)p1 = (i0 + kofs == i1) ? data : m_zero;
5036
- p1 += s1;
5037
- }
5038
- p0 += s0;
5039
- }
5040
- }
5041
-
5042
- /*
5043
- Eye: Set a value to diagonal components, set 0 to non-diagonal components.
5044
- @overload eye([element,offset])
5045
- @param [Numeric] element Diagonal element to be stored. Default is 1.
5046
- @param [Integer] offset Diagonal offset from the main diagonal. The
5047
- default is 0. k>0 for diagonals above the main diagonal, and k<0
5048
- for diagonals below the main diagonal.
5049
- @return [Numo::RObject] eye of self.
5050
- */
5051
- static VALUE robject_eye(int argc, VALUE* argv, VALUE self) {
5052
- ndfunc_arg_in_t ain[1] = { { OVERWRITE, 2 } };
5053
- ndfunc_t ndf = { iter_robject_eye, NO_LOOP, 1, 0, ain, 0 };
5054
- ssize_t kofs;
5055
- dtype data;
5056
- char* g;
5057
- int nd;
5058
- narray_t* na;
5059
-
5060
- // check arguments
5061
- if (argc > 2) {
5062
- rb_raise(rb_eArgError, "too many arguments (%d for 0..2)", argc);
5063
- } else if (argc == 2) {
5064
- data = m_num_to_data(argv[0]);
5065
- kofs = NUM2SSIZET(argv[1]);
5066
- } else if (argc == 1) {
5067
- data = m_num_to_data(argv[0]);
5068
- kofs = 0;
5069
- } else {
5070
- data = m_one;
5071
- kofs = 0;
5072
- }
5073
-
5074
- GetNArray(self, na);
5075
- nd = na->ndim;
5076
- if (nd < 2) {
5077
- rb_raise(nary_eDimensionError, "less than 2-d array");
5078
- }
5079
-
5080
- // Diagonal offset from the main diagonal.
5081
- if (kofs >= 0) {
5082
- if ((size_t)(kofs) >= na->shape[nd - 1]) {
5083
- rb_raise(
5084
- rb_eArgError,
5085
- "invalid diagonal offset(%" SZF "d) for "
5086
- "last dimension size(%" SZF "d)",
5087
- kofs, na->shape[nd - 1]
5088
- );
5089
- }
5090
- } else {
5091
- if ((size_t)(-kofs) >= na->shape[nd - 2]) {
5092
- rb_raise(
5093
- rb_eArgError,
5094
- "invalid diagonal offset(%" SZF "d) for "
5095
- "last-1 dimension size(%" SZF "d)",
5096
- kofs, na->shape[nd - 2]
5097
- );
1819
+ *(dtype*)p2 = m_reciprocal(*(dtype*)p1);
1820
+ p1 += s1;
1821
+ p2 += s2;
1822
+ }
1823
+ return;
1824
+ //
5098
1825
  }
5099
1826
  }
1827
+ }
5100
1828
 
5101
- g = ALLOCA_N(char, sizeof(ssize_t) + sizeof(dtype));
5102
- *(ssize_t*)g = kofs;
5103
- *(dtype*)(g + sizeof(ssize_t)) = data;
1829
+ /*
1830
+ Unary reciprocal.
1831
+ @overload reciprocal
1832
+ @return [Numo::RObject] reciprocal of self.
1833
+ */
1834
+ static VALUE robject_reciprocal(VALUE self) {
1835
+ ndfunc_arg_in_t ain[1] = { { cT, 0 } };
1836
+ ndfunc_arg_out_t aout[1] = { { cT, 0 } };
1837
+ ndfunc_t ndf = { iter_robject_reciprocal, FULL_LOOP, 1, 1, ain, aout };
5104
1838
 
5105
- na_ndloop3(&ndf, g, 1, self);
5106
- return self;
1839
+ return na_ndloop(&ndf, 1, self);
5107
1840
  }
5108
1841
 
5109
- typedef struct {
5110
- dtype low;
5111
- dtype max;
5112
- } rand_opt_t;
5113
-
5114
- static void iter_robject_rand(na_loop_t* const lp) {
5115
- size_t i;
5116
- char* p1;
5117
- ssize_t s1;
5118
- size_t* idx1;
1842
+ static void iter_robject_sign(na_loop_t* const lp) {
1843
+ size_t i, n;
1844
+ char *p1, *p2;
1845
+ ssize_t s1, s2;
1846
+ size_t *idx1, *idx2;
5119
1847
  dtype x;
5120
- rand_opt_t* g;
5121
- dtype low;
5122
- dtype max;
5123
1848
 
5124
- INIT_COUNTER(lp, i);
1849
+ INIT_COUNTER(lp, n);
5125
1850
  INIT_PTR_IDX(lp, 0, p1, s1, idx1);
5126
- g = (rand_opt_t*)(lp->opt_ptr);
5127
- low = g->low;
5128
- max = g->max;
1851
+ INIT_PTR_IDX(lp, 1, p2, s2, idx2);
5129
1852
 
5130
1853
  if (idx1) {
5131
- for (; i--;) {
5132
- x = m_add(m_rand(max), low);
5133
- SET_DATA_INDEX(p1, idx1, dtype, x);
1854
+ if (idx2) {
1855
+ for (i = 0; i < n; i++) {
1856
+ GET_DATA_INDEX(p1, idx1, dtype, x);
1857
+ x = m_sign(x);
1858
+ SET_DATA_INDEX(p2, idx2, dtype, x);
1859
+ }
1860
+ } else {
1861
+ for (i = 0; i < n; i++) {
1862
+ GET_DATA_INDEX(p1, idx1, dtype, x);
1863
+ x = m_sign(x);
1864
+ SET_DATA_STRIDE(p2, s2, dtype, x);
1865
+ }
5134
1866
  }
5135
1867
  } else {
5136
- for (; i--;) {
5137
- x = m_add(m_rand(max), low);
5138
- SET_DATA_STRIDE(p1, s1, dtype, x);
1868
+ if (idx2) {
1869
+ for (i = 0; i < n; i++) {
1870
+ GET_DATA_STRIDE(p1, s1, dtype, x);
1871
+ x = m_sign(x);
1872
+ SET_DATA_INDEX(p2, idx2, dtype, x);
1873
+ }
1874
+ } else {
1875
+ //
1876
+ for (i = 0; i < n; i++) {
1877
+ *(dtype*)p2 = m_sign(*(dtype*)p1);
1878
+ p1 += s1;
1879
+ p2 += s2;
1880
+ }
1881
+ return;
1882
+ //
5139
1883
  }
5140
1884
  }
5141
1885
  }
5142
1886
 
5143
1887
  /*
5144
- Generate uniformly distributed random numbers on self narray.
5145
- @overload rand([[low],high])
5146
- @param [Numeric] low lower inclusive boundary of random numbers. (default=0)
5147
- @param [Numeric] high upper exclusive boundary of random numbers. (default=1 or 1+1i for
5148
- complex types)
5149
- @return [Numo::RObject] self.
5150
- @example
5151
- Numo::DFloat.new(6).rand
5152
- # => Numo::DFloat#shape=[6]
5153
- # [0.0617545, 0.373067, 0.794815, 0.201042, 0.116041, 0.344032]
5154
-
5155
- Numo::DComplex.new(6).rand(5+5i)
5156
- # => Numo::DComplex#shape=[6]
5157
- # [2.69974+3.68908i, 0.825443+0.254414i, 0.540323+0.34354i, 4.52061+2.39322i, ...]
5158
-
5159
- Numo::Int32.new(6).rand(2,5)
5160
- # => Numo::Int32#shape=[6]
5161
- # [4, 3, 3, 2, 4, 2]
1888
+ Unary sign.
1889
+ @overload sign
1890
+ @return [Numo::RObject] sign of self.
5162
1891
  */
5163
- static VALUE robject_rand(int argc, VALUE* argv, VALUE self) {
5164
- rand_opt_t g;
5165
- VALUE v1 = Qnil, v2 = Qnil;
5166
- dtype high;
5167
- ndfunc_arg_in_t ain[1] = { { OVERWRITE, 0 } };
5168
- ndfunc_t ndf = { iter_robject_rand, FULL_LOOP, 1, 0, ain, 0 };
1892
+ static VALUE robject_sign(VALUE self) {
1893
+ ndfunc_arg_in_t ain[1] = { { cT, 0 } };
1894
+ ndfunc_arg_out_t aout[1] = { { cT, 0 } };
1895
+ ndfunc_t ndf = { iter_robject_sign, FULL_LOOP, 1, 1, ain, aout };
5169
1896
 
5170
- rb_scan_args(argc, argv, "02", &v1, &v2);
5171
- if (v2 == Qnil) {
5172
- g.low = m_zero;
5173
- if (v1 == Qnil) {
1897
+ return na_ndloop(&ndf, 1, self);
1898
+ }
1899
+
1900
+ static void iter_robject_square(na_loop_t* const lp) {
1901
+ size_t i, n;
1902
+ char *p1, *p2;
1903
+ ssize_t s1, s2;
1904
+ size_t *idx1, *idx2;
1905
+ dtype x;
5174
1906
 
5175
- g.max = high = m_one;
1907
+ INIT_COUNTER(lp, n);
1908
+ INIT_PTR_IDX(lp, 0, p1, s1, idx1);
1909
+ INIT_PTR_IDX(lp, 1, p2, s2, idx2);
5176
1910
 
1911
+ if (idx1) {
1912
+ if (idx2) {
1913
+ for (i = 0; i < n; i++) {
1914
+ GET_DATA_INDEX(p1, idx1, dtype, x);
1915
+ x = m_square(x);
1916
+ SET_DATA_INDEX(p2, idx2, dtype, x);
1917
+ }
5177
1918
  } else {
5178
- g.max = high = m_num_to_data(v1);
1919
+ for (i = 0; i < n; i++) {
1920
+ GET_DATA_INDEX(p1, idx1, dtype, x);
1921
+ x = m_square(x);
1922
+ SET_DATA_STRIDE(p2, s2, dtype, x);
1923
+ }
5179
1924
  }
5180
-
5181
1925
  } else {
5182
- g.low = m_num_to_data(v1);
5183
- high = m_num_to_data(v2);
5184
- g.max = m_sub(high, g.low);
1926
+ if (idx2) {
1927
+ for (i = 0; i < n; i++) {
1928
+ GET_DATA_STRIDE(p1, s1, dtype, x);
1929
+ x = m_square(x);
1930
+ SET_DATA_INDEX(p2, idx2, dtype, x);
1931
+ }
1932
+ } else {
1933
+ //
1934
+ for (i = 0; i < n; i++) {
1935
+ *(dtype*)p2 = m_square(*(dtype*)p1);
1936
+ p1 += s1;
1937
+ p2 += s2;
1938
+ }
1939
+ return;
1940
+ //
1941
+ }
5185
1942
  }
1943
+ }
5186
1944
 
5187
- na_ndloop3(&ndf, &g, 1, self);
5188
- return self;
1945
+ /*
1946
+ Unary square.
1947
+ @overload square
1948
+ @return [Numo::RObject] square of self.
1949
+ */
1950
+ static VALUE robject_square(VALUE self) {
1951
+ ndfunc_arg_in_t ain[1] = { { cT, 0 } };
1952
+ ndfunc_arg_out_t aout[1] = { { cT, 0 } };
1953
+ ndfunc_t ndf = { iter_robject_square, FULL_LOOP, 1, 1, ain, aout };
1954
+
1955
+ return na_ndloop(&ndf, 1, self);
5189
1956
  }
5190
1957
 
5191
1958
  static void iter_robject_poly(na_loop_t* const lp) {
@@ -5328,22 +2095,91 @@ void Init_numo_robject(void) {
5328
2095
  rb_define_singleton_method(cT, "cast", robject_s_cast, 1);
5329
2096
  rb_define_method(cT, "[]", robject_aref, -1);
5330
2097
  rb_define_method(cT, "[]=", robject_aset, -1);
2098
+ /**
2099
+ * return NArray with cast to the type of self.
2100
+ * @overload coerce_cast(type)
2101
+ * @return [nil]
2102
+ */
5331
2103
  rb_define_method(cT, "coerce_cast", robject_coerce_cast, 1);
2104
+ /**
2105
+ * Convert self to Array.
2106
+ * @overload to_a
2107
+ * @return [Array]
2108
+ */
5332
2109
  rb_define_method(cT, "to_a", robject_to_a, 0);
2110
+ /**
2111
+ * Fill elements with other.
2112
+ * @overload fill other
2113
+ * @param [Numeric] other
2114
+ * @return [Numo::RObject] self.
2115
+ */
5333
2116
  rb_define_method(cT, "fill", robject_fill, 1);
2117
+ /**
2118
+ * Format elements into strings.
2119
+ * @overload format format
2120
+ * @param [String] format
2121
+ * @return [Numo::RObject] array of formatted strings.
2122
+ */
5334
2123
  rb_define_method(cT, "format", robject_format, -1);
2124
+ /**
2125
+ * Format elements into strings.
2126
+ * @overload format_to_a format
2127
+ * @param [String] format
2128
+ * @return [Array] array of formatted strings.
2129
+ */
5335
2130
  rb_define_method(cT, "format_to_a", robject_format_to_a, -1);
2131
+ /**
2132
+ * Returns a string containing a human-readable representation of NArray.
2133
+ * @overload inspect
2134
+ * @return [String]
2135
+ */
5336
2136
  rb_define_method(cT, "inspect", robject_inspect, 0);
5337
2137
  rb_define_method(cT, "each", robject_each, 0);
5338
2138
  rb_define_method(cT, "map", robject_map, 0);
5339
2139
  rb_define_method(cT, "each_with_index", robject_each_with_index, 0);
5340
2140
  rb_define_method(cT, "map_with_index", robject_map_with_index, 0);
5341
2141
  rb_define_method(cT, "abs", robject_abs, 0);
2142
+ /**
2143
+ * Binary add.
2144
+ * @overload + other
2145
+ * @param [Numo::NArray,Numeric] other
2146
+ * @return [Numo::NArray] self + other
2147
+ */
5342
2148
  rb_define_method(cT, "+", robject_add, 1);
2149
+ /**
2150
+ * Binary sub.
2151
+ * @overload - other
2152
+ * @param [Numo::NArray,Numeric] other
2153
+ * @return [Numo::NArray] self - other
2154
+ */
5343
2155
  rb_define_method(cT, "-", robject_sub, 1);
2156
+ /**
2157
+ * Binary mul.
2158
+ * @overload * other
2159
+ * @param [Numo::NArray,Numeric] other
2160
+ * @return [Numo::NArray] self * other
2161
+ */
5344
2162
  rb_define_method(cT, "*", robject_mul, 1);
2163
+ /**
2164
+ * Binary div.
2165
+ * @overload / other
2166
+ * @param [Numo::NArray,Numeric] other
2167
+ * @return [Numo::NArray] self / other
2168
+ */
5345
2169
  rb_define_method(cT, "/", robject_div, 1);
2170
+ /**
2171
+ * Binary mod.
2172
+ * @overload % other
2173
+ * @param [Numo::NArray,Numeric] other
2174
+ * @return [Numo::NArray] self % other
2175
+ */
5346
2176
  rb_define_method(cT, "%", robject_mod, 1);
2177
+ /**
2178
+ * Binary divmod.
2179
+ * @overload divmod other
2180
+ * @param [Numo::NArray,Numeric] other
2181
+ * @return [Numo::NArray] divmod of self and other.
2182
+ */
5347
2183
  rb_define_method(cT, "divmod", robject_divmod, 1);
5348
2184
  rb_define_method(cT, "**", robject_pow, 1);
5349
2185
  rb_define_alias(cT, "pow", "**");
@@ -5354,35 +2190,207 @@ void Init_numo_robject(void) {
5354
2190
  rb_define_alias(cT, "conj", "view");
5355
2191
  rb_define_alias(cT, "im", "view");
5356
2192
  rb_define_alias(cT, "conjugate", "conj");
2193
+ /**
2194
+ * Comparison eq other.
2195
+ * @overload eq other
2196
+ * @param [Numo::NArray,Numeric] other
2197
+ * @return [Numo::Bit] result of self eq other.
2198
+ */
5357
2199
  rb_define_method(cT, "eq", robject_eq, 1);
2200
+ /**
2201
+ * Comparison ne other.
2202
+ * @overload ne other
2203
+ * @param [Numo::NArray,Numeric] other
2204
+ * @return [Numo::Bit] result of self ne other.
2205
+ */
5358
2206
  rb_define_method(cT, "ne", robject_ne, 1);
2207
+ /**
2208
+ * Comparison nearly_eq other.
2209
+ * @overload nearly_eq other
2210
+ * @param [Numo::NArray,Numeric] other
2211
+ * @return [Numo::Bit] result of self nearly_eq other.
2212
+ */
5359
2213
  rb_define_method(cT, "nearly_eq", robject_nearly_eq, 1);
5360
2214
  rb_define_alias(cT, "close_to", "nearly_eq");
2215
+ /**
2216
+ * Binary bit_and.
2217
+ * @overload & other
2218
+ * @param [Numo::NArray,Numeric] other
2219
+ * @return [Numo::NArray] self & other
2220
+ */
5361
2221
  rb_define_method(cT, "&", robject_bit_and, 1);
2222
+ /**
2223
+ * Binary bit_or.
2224
+ * @overload | other
2225
+ * @param [Numo::NArray,Numeric] other
2226
+ * @return [Numo::NArray] self | other
2227
+ */
5362
2228
  rb_define_method(cT, "|", robject_bit_or, 1);
2229
+ /**
2230
+ * Binary bit_xor.
2231
+ * @overload ^ other
2232
+ * @param [Numo::NArray,Numeric] other
2233
+ * @return [Numo::NArray] self ^ other
2234
+ */
5363
2235
  rb_define_method(cT, "^", robject_bit_xor, 1);
2236
+ /**
2237
+ * Unary bit_not.
2238
+ * @overload ~
2239
+ * @return [Numo::RObject] bit_not of self.
2240
+ */
5364
2241
  rb_define_method(cT, "~", robject_bit_not, 0);
2242
+ /**
2243
+ * Binary left_shift.
2244
+ * @overload << other
2245
+ * @param [Numo::NArray,Numeric] other
2246
+ * @return [Numo::NArray] self << other
2247
+ */
5365
2248
  rb_define_method(cT, "<<", robject_left_shift, 1);
2249
+ /**
2250
+ * Binary right_shift.
2251
+ * @overload >> other
2252
+ * @param [Numo::NArray,Numeric] other
2253
+ * @return [Numo::NArray] self >> other
2254
+ */
5366
2255
  rb_define_method(cT, ">>", robject_right_shift, 1);
2256
+ /**
2257
+ * Unary floor.
2258
+ * @overload floor
2259
+ * @return [Numo::RObject] floor of self.
2260
+ */
5367
2261
  rb_define_method(cT, "floor", robject_floor, 0);
2262
+ /**
2263
+ * Unary round.
2264
+ * @overload round
2265
+ * @return [Numo::RObject] round of self.
2266
+ */
5368
2267
  rb_define_method(cT, "round", robject_round, 0);
2268
+ /**
2269
+ * Unary ceil.
2270
+ * @overload ceil
2271
+ * @return [Numo::RObject] ceil of self.
2272
+ */
5369
2273
  rb_define_method(cT, "ceil", robject_ceil, 0);
2274
+ /**
2275
+ * Unary trunc.
2276
+ * @overload trunc
2277
+ * @return [Numo::RObject] trunc of self.
2278
+ */
5370
2279
  rb_define_method(cT, "trunc", robject_trunc, 0);
2280
+ /**
2281
+ * Comparison gt other.
2282
+ * @overload gt other
2283
+ * @param [Numo::NArray,Numeric] other
2284
+ * @return [Numo::Bit] result of self gt other.
2285
+ */
5371
2286
  rb_define_method(cT, "gt", robject_gt, 1);
2287
+ /**
2288
+ * Comparison ge other.
2289
+ * @overload ge other
2290
+ * @param [Numo::NArray,Numeric] other
2291
+ * @return [Numo::Bit] result of self ge other.
2292
+ */
5372
2293
  rb_define_method(cT, "ge", robject_ge, 1);
2294
+ /**
2295
+ * Comparison lt other.
2296
+ * @overload lt other
2297
+ * @param [Numo::NArray,Numeric] other
2298
+ * @return [Numo::Bit] result of self lt other.
2299
+ */
5373
2300
  rb_define_method(cT, "lt", robject_lt, 1);
2301
+ /**
2302
+ * Comparison le other.
2303
+ * @overload le other
2304
+ * @param [Numo::NArray,Numeric] other
2305
+ * @return [Numo::Bit] result of self le other.
2306
+ */
5374
2307
  rb_define_method(cT, "le", robject_le, 1);
5375
2308
  rb_define_alias(cT, ">", "gt");
5376
2309
  rb_define_alias(cT, ">=", "ge");
5377
2310
  rb_define_alias(cT, "<", "lt");
5378
2311
  rb_define_alias(cT, "<=", "le");
2312
+ /**
2313
+ * Clip array elements by [min,max].
2314
+ * If either of min or max is nil, one side is clipped.
2315
+ * @overload clip(min,max)
2316
+ * @param [Numo::NArray,Numeric] min
2317
+ * @param [Numo::NArray,Numeric] max
2318
+ * @return [Numo::NArray] result of clip.
2319
+ *
2320
+ * @example
2321
+ * a = Numo::Int32.new(10).seq
2322
+ * # => Numo::Int32#shape=[10]
2323
+ * # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
2324
+ *
2325
+ * a.clip(1,8)
2326
+ * # => Numo::Int32#shape=[10]
2327
+ * # [1, 1, 2, 3, 4, 5, 6, 7, 8, 8]
2328
+ *
2329
+ * a.inplace.clip(3,6)
2330
+ * a
2331
+ * # => Numo::Int32#shape=[10]
2332
+ * # [3, 3, 3, 3, 4, 5, 6, 6, 6, 6]
2333
+ *
2334
+ * b = Numo::Int32.new(10).seq
2335
+ * # => Numo::Int32#shape=[10]
2336
+ * # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
2337
+ *
2338
+ * b.clip([3,4,1,1,1,4,4,4,4,4], 8)
2339
+ * # => Numo::Int32#shape=[10]
2340
+ * # [3, 4, 2, 3, 4, 5, 6, 7, 8, 8]
2341
+ */
5379
2342
  rb_define_method(cT, "clip", robject_clip, 2);
2343
+ /**
2344
+ * Condition of isnan.
2345
+ * @overload isnan
2346
+ * @return [Numo::Bit] Condition of isnan.
2347
+ */
5380
2348
  rb_define_method(cT, "isnan", robject_isnan, 0);
2349
+ /**
2350
+ * Condition of isinf.
2351
+ * @overload isinf
2352
+ * @return [Numo::Bit] Condition of isinf.
2353
+ */
5381
2354
  rb_define_method(cT, "isinf", robject_isinf, 0);
2355
+ /**
2356
+ * Condition of isposinf.
2357
+ * @overload isposinf
2358
+ * @return [Numo::Bit] Condition of isposinf.
2359
+ */
5382
2360
  rb_define_method(cT, "isposinf", robject_isposinf, 0);
2361
+ /**
2362
+ * Condition of isneginf.
2363
+ * @overload isneginf
2364
+ * @return [Numo::Bit] Condition of isneginf.
2365
+ */
5383
2366
  rb_define_method(cT, "isneginf", robject_isneginf, 0);
2367
+ /**
2368
+ * Condition of isfinite.
2369
+ * @overload isfinite
2370
+ * @return [Numo::Bit] Condition of isfinite.
2371
+ */
5384
2372
  rb_define_method(cT, "isfinite", robject_isfinite, 0);
2373
+ /**
2374
+ * sum of self.
2375
+ * @overload sum(axis:nil, keepdims:false, nan:false)
2376
+ * @param [TrueClass] nan If true, apply NaN-aware algorithm
2377
+ * (avoid NaN for sum/mean etc, or, return NaN for min/max etc).
2378
+ * @param [Numeric,Array,Range] axis Performs sum along the axis.
2379
+ * @param [TrueClass] keepdims If true, the reduced axes are left in the result array as
2380
+ * dimensions with size one.
2381
+ * @return [Numo::RObject] returns result of sum.
2382
+ */
5385
2383
  rb_define_method(cT, "sum", robject_sum, -1);
2384
+ /**
2385
+ * prod of self.
2386
+ * @overload prod(axis:nil, keepdims:false, nan:false)
2387
+ * @param [TrueClass] nan If true, apply NaN-aware algorithm
2388
+ * (avoid NaN for sum/mean etc, or, return NaN for min/max etc).
2389
+ * @param [Numeric,Array,Range] axis Performs prod along the axis.
2390
+ * @param [TrueClass] keepdims If true, the reduced axes are left in the result array as
2391
+ * dimensions with size one.
2392
+ * @return [Numo::RObject] returns result of prod.
2393
+ */
5386
2394
  rb_define_method(cT, "prod", robject_prod, -1);
5387
2395
  /**
5388
2396
  * mean of self.
@@ -5428,13 +2436,135 @@ void Init_numo_robject(void) {
5428
2436
  * @return [Numo::RObject] returns result of rms.
5429
2437
  */
5430
2438
  rb_define_method(cT, "rms", robject_rms, -1);
2439
+ /**
2440
+ * min of self.
2441
+ * @overload min(axis:nil, keepdims:false, nan:false)
2442
+ * @param [TrueClass] nan If true, apply NaN-aware algorithm
2443
+ * (avoid NaN for sum/mean etc, or, return NaN for min/max etc).
2444
+ * @param [Numeric,Array,Range] axis Performs min along the axis.
2445
+ * @param [TrueClass] keepdims If true, the reduced axes are left in the result array as
2446
+ * dimensions with size one.
2447
+ * @return [Numo::RObject] returns result of min.
2448
+ */
5431
2449
  rb_define_method(cT, "min", robject_min, -1);
2450
+ /**
2451
+ * max of self.
2452
+ * @overload max(axis:nil, keepdims:false, nan:false)
2453
+ * @param [TrueClass] nan If true, apply NaN-aware algorithm
2454
+ * (avoid NaN for sum/mean etc, or, return NaN for min/max etc).
2455
+ * @param [Numeric,Array,Range] axis Performs max along the axis.
2456
+ * @param [TrueClass] keepdims If true, the reduced axes are left in the result array as
2457
+ * dimensions with size one.
2458
+ * @return [Numo::RObject] returns result of max.
2459
+ */
5432
2460
  rb_define_method(cT, "max", robject_max, -1);
2461
+ /**
2462
+ * ptp of self.
2463
+ * @overload ptp(axis:nil, keepdims:false, nan:false)
2464
+ * @param [TrueClass] nan If true, apply NaN-aware algorithm
2465
+ * (avoid NaN for sum/mean etc, or, return NaN for min/max etc).
2466
+ * @param [Numeric,Array,Range] axis Performs ptp along the axis.
2467
+ * @param [TrueClass] keepdims If true, the reduced axes are left in the result array as
2468
+ * dimensions with size one.
2469
+ * @return [Numo::RObject] returns result of ptp.
2470
+ */
5433
2471
  rb_define_method(cT, "ptp", robject_ptp, -1);
2472
+ /**
2473
+ * Index of the maximum value.
2474
+ * @overload max_index(axis:nil, nan:false)
2475
+ * @param [TrueClass] nan If true, apply NaN-aware algorithm (return
2476
+ * NaN posision if exist).
2477
+ * @param [Numeric,Array,Range] axis Finds maximum values along the axis and
2478
+ * returns **flat 1-d indices**.
2479
+ * @return [Integer,Numo::Int] returns result indices.
2480
+ * @see #argmax
2481
+ * @see #max
2482
+ *
2483
+ * @example
2484
+ * a = Numo::NArray[3,4,1,2]
2485
+ * a.max_index #=> 1
2486
+ *
2487
+ * b = Numo::NArray[[3,4,1],[2,0,5]]
2488
+ * b.max_index #=> 5
2489
+ * b.max_index(axis:1) #=> [1, 5]
2490
+ * b.max_index(axis:0) #=> [0, 1, 5]
2491
+ * b[b.max_index(axis:0)] #=> [3, 4, 5]
2492
+ */
5434
2493
  rb_define_method(cT, "max_index", robject_max_index, -1);
2494
+ /**
2495
+ * Index of the minimum value.
2496
+ * @overload min_index(axis:nil, nan:false)
2497
+ * @param [TrueClass] nan If true, apply NaN-aware algorithm (returnNaN posision if exist).
2498
+ * @param [Numeric,Array,Range] axis Finds minimum values along the axis and
2499
+ * returns **flat 1-d indices**.
2500
+ * @return [Integer,Numo::Int] returns result indices.
2501
+ * @see #argmin
2502
+ * @see #min
2503
+ *
2504
+ * @example
2505
+ * a = Numo::NArray[3,4,1,2]
2506
+ * a.min_index #=> 2
2507
+ *
2508
+ * b = Numo::NArray[[3,4,1],[2,0,5]]
2509
+ * b.min_index #=> 4
2510
+ * b.min_index(axis:1) #=> [2, 4]
2511
+ * b.min_index(axis:0) #=> [3, 4, 2]
2512
+ * b[b.min_index(axis:0)] #=> [2, 0, 1]
2513
+ */
5435
2514
  rb_define_method(cT, "min_index", robject_min_index, -1);
2515
+ /**
2516
+ * Index of the maximum value.
2517
+ * @overload argmax(axis:nil, nan:false)
2518
+ * @param [TrueClass] nan If true, apply NaN-aware algorithm (return NaN posision
2519
+ * if exist).
2520
+ * @param [Numeric,Array,Range] axis Finds maximum values along the axis and
2521
+ * returns **indices along the axis**.
2522
+ * @return [Integer,Numo::Int] returns the result indices.
2523
+ * @see #max_index
2524
+ * @see #max
2525
+ *
2526
+ * @example
2527
+ * a = Numo::NArray[3,4,1,2]
2528
+ * a.argmax #=> 1
2529
+ *
2530
+ * b = Numo::NArray[[3,4,1],[2,0,5]]
2531
+ * b.argmax #=> 5
2532
+ * b.argmax(axis:1) #=> [1, 2]
2533
+ * b.argmax(axis:0) #=> [0, 0, 1]
2534
+ * b.at(b.argmax(axis:0), 0..-1) #=> [3, 4, 5]
2535
+ */
5436
2536
  rb_define_method(cT, "argmax", robject_argmax, -1);
2537
+ /**
2538
+ * Index of the minimum value.
2539
+ * @overload argmin(axis:nil, nan:false)
2540
+ * @param [TrueClass] nan If true, apply NaN-aware algorithm (return
2541
+ * NaN posision if exist).
2542
+ * @param [Numeric,Array,Range] axis Finds minimum values along the axis and
2543
+ * returns **indices along the axis**.
2544
+ * @return [Integer,Numo::Int] returns the result indices.
2545
+ * @see #min_index
2546
+ * @see #min
2547
+ *
2548
+ * @example
2549
+ * a = Numo::NArray[3,4,1,2]
2550
+ * a.argmin #=> 2
2551
+ *
2552
+ * b = Numo::NArray[[3,4,1],[2,0,5]]
2553
+ * b.argmin #=> 4
2554
+ * b.argmin(axis:1) #=> [2, 1]
2555
+ * b.argmin(axis:0) #=> [1, 1, 0]
2556
+ * b.at(b.argmin(axis:0), 0..-1) #=> [2, 0, 1]
2557
+ */
5437
2558
  rb_define_method(cT, "argmin", robject_argmin, -1);
2559
+ /**
2560
+ * minmax of self.
2561
+ * @overload minmax(axis:nil, keepdims:false, nan:false)
2562
+ * @param [TrueClass] nan If true, apply NaN-aware algorithm (return NaN if exist).
2563
+ * @param [Numeric,Array,Range] axis Finds min-max along the axis.
2564
+ * @param [TrueClass] keepdims (keyword) If true, the reduced axes are left in
2565
+ * the result array as dimensions with size one.
2566
+ * @return [Numo::RObject,Numo::RObject] min and max of self.
2567
+ */
5438
2568
  rb_define_method(cT, "minmax", robject_minmax, -1);
5439
2569
  /**
5440
2570
  * Element-wise maximum of two arrays.
@@ -5454,13 +2584,106 @@ void Init_numo_robject(void) {
5454
2584
  * @return [Numo::RObject]
5455
2585
  */
5456
2586
  rb_define_module_function(cT, "minimum", robject_s_minimum, -1);
2587
+ /**
2588
+ * cumsum of self.
2589
+ * @overload cumsum(axis:nil, nan:false)
2590
+ * @param [Numeric,Array,Range] axis Performs cumsum along the axis.
2591
+ * @param [TrueClass] nan If true, apply NaN-aware algorithm (avoid NaN if exists).
2592
+ * @return [Numo::RObject] cumsum of self.
2593
+ */
5457
2594
  rb_define_method(cT, "cumsum", robject_cumsum, -1);
2595
+ /**
2596
+ * cumprod of self.
2597
+ * @overload cumprod(axis:nil, nan:false)
2598
+ * @param [Numeric,Array,Range] axis Performs cumprod along the axis.
2599
+ * @param [TrueClass] nan If true, apply NaN-aware algorithm (avoid NaN if exists).
2600
+ * @return [Numo::RObject] cumprod of self.
2601
+ */
5458
2602
  rb_define_method(cT, "cumprod", robject_cumprod, -1);
2603
+ /**
2604
+ * Binary mulsum.
2605
+ *
2606
+ * @overload mulsum(other, axis:nil, keepdims:false, nan:false)
2607
+ * @param [Numo::NArray,Numeric] other
2608
+ * @param [Numeric,Array,Range] axis Performs mulsum along the axis.
2609
+ * @param [TrueClass] keepdims (keyword) If true, the reduced axes are left in
2610
+ * the result array as dimensions with size one.
2611
+ * @param [TrueClass] nan (keyword) If true, apply NaN-aware algorithm
2612
+ * (avoid NaN if exists).
2613
+ * @return [Numo::NArray] mulsum of self and other.
2614
+ */
5459
2615
  rb_define_method(cT, "mulsum", robject_mulsum, -1);
2616
+ /**
2617
+ * Set linear sequence of numbers to self. The sequence is obtained from
2618
+ * beg+i*step
2619
+ * where i is 1-dimensional index.
2620
+ * @overload seq([beg,[step]])
2621
+ * @param [Numeric] beg beginning of sequence. (default=0)
2622
+ * @param [Numeric] step step of sequence. (default=1)
2623
+ * @return [Numo::RObject] self.
2624
+ * @example
2625
+ * Numo::DFloat.new(6).seq(1,-0.2)
2626
+ * # => Numo::DFloat#shape=[6]
2627
+ * # [1, 0.8, 0.6, 0.4, 0.2, 0]
2628
+ *
2629
+ * Numo::DComplex.new(6).seq(1,-0.2+0.2i)
2630
+ * # => Numo::DComplex#shape=[6]
2631
+ * # [1+0i, 0.8+0.2i, 0.6+0.4i, 0.4+0.6i, 0.2+0.8i, 0+1i]
2632
+ */
5460
2633
  rb_define_method(cT, "seq", robject_seq, -1);
2634
+ /**
2635
+ * Set logarithmic sequence of numbers to self. The sequence is obtained from
2636
+ * `base**(beg+i*step)`
2637
+ * where i is 1-dimensional index.
2638
+ * Applicable classes: DFloat, SFloat, DComplex, SCopmplex.
2639
+ *
2640
+ * @overload logseq(beg,step,[base])
2641
+ * @param [Numeric] beg The beginning of sequence.
2642
+ * @param [Numeric] step The step of sequence.
2643
+ * @param [Numeric] base The base of log space. (default=10)
2644
+ * @return [Numo::RObject] self.
2645
+ *
2646
+ * @example
2647
+ * Numo::DFloat.new(5).logseq(4,-1,2)
2648
+ * # => Numo::DFloat#shape=[5]
2649
+ * # [16, 8, 4, 2, 1]
2650
+ *
2651
+ * Numo::DComplex.new(5).logseq(0,1i*Math::PI/3,Math::E)
2652
+ * # => Numo::DComplex#shape=[5]
2653
+ * # [1+7.26156e-310i, 0.5+0.866025i, -0.5+0.866025i, -1+1.22465e-16i, ...]
2654
+ */
5461
2655
  rb_define_method(cT, "logseq", robject_logseq, -1);
2656
+ /**
2657
+ * Eye: Set a value to diagonal components, set 0 to non-diagonal components.
2658
+ * @overload eye([element,offset])
2659
+ * @param [Numeric] element Diagonal element to be stored. Default is 1.
2660
+ * @param [Integer] offset Diagonal offset from the main diagonal. The
2661
+ * default is 0. k>0 for diagonals above the main diagonal, and k<0
2662
+ * for diagonals below the main diagonal.
2663
+ * @return [Numo::RObject] eye of self.
2664
+ */
5462
2665
  rb_define_method(cT, "eye", robject_eye, -1);
5463
2666
  rb_define_alias(cT, "indgen", "seq");
2667
+ /**
2668
+ * Generate uniformly distributed random numbers on self narray.
2669
+ * @overload rand([[low],high])
2670
+ * @param [Numeric] low lower inclusive boundary of random numbers. (default=0)
2671
+ * @param [Numeric] high upper exclusive boundary of random numbers.
2672
+ * (default=1 or 1+1i for complex types)
2673
+ * @return [Numo::RObject] self.
2674
+ * @example
2675
+ * Numo::DFloat.new(6).rand
2676
+ * # => Numo::DFloat#shape=[6]
2677
+ * # [0.0617545, 0.373067, 0.794815, 0.201042, 0.116041, 0.344032]
2678
+ *
2679
+ * Numo::DComplex.new(6).rand(5+5i)
2680
+ * # => Numo::DComplex#shape=[6]
2681
+ * # [2.69974+3.68908i, 0.825443+0.254414i, 0.540323+0.34354i, 4.52061+2.39322i, ...]
2682
+ *
2683
+ * Numo::Int32.new(6).rand(2,5)
2684
+ * # => Numo::Int32#shape=[6]
2685
+ * # [4, 3, 3, 2, 4, 2]
2686
+ */
5464
2687
  rb_define_method(cT, "rand", robject_rand, -1);
5465
2688
  rb_define_method(cT, "poly", robject_poly, -2);
5466
2689
  rb_define_singleton_method(cT, "[]", robject_s_cast, -2);