numo-narray-alt 0.9.9 → 0.9.11

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 (39) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +1 -1
  3. data/ext/numo/narray/numo/narray.h +2 -2
  4. data/ext/numo/narray/numo/types/robject.h +1 -1
  5. data/ext/numo/narray/src/mh/argmax.h +154 -0
  6. data/ext/numo/narray/src/mh/argmin.h +154 -0
  7. data/ext/numo/narray/src/mh/clip.h +115 -0
  8. data/ext/numo/narray/src/mh/cumprod.h +98 -0
  9. data/ext/numo/narray/src/mh/cumsum.h +98 -0
  10. data/ext/numo/narray/src/mh/eye.h +82 -0
  11. data/ext/numo/narray/src/mh/logseq.h +69 -0
  12. data/ext/numo/narray/src/mh/max.h +69 -0
  13. data/ext/numo/narray/src/mh/max_index.h +184 -0
  14. data/ext/numo/narray/src/mh/maximum.h +116 -0
  15. data/ext/numo/narray/src/mh/min.h +69 -0
  16. data/ext/numo/narray/src/mh/min_index.h +184 -0
  17. data/ext/numo/narray/src/mh/minimum.h +116 -0
  18. data/ext/numo/narray/src/mh/minmax.h +77 -0
  19. data/ext/numo/narray/src/mh/mulsum.h +185 -0
  20. data/ext/numo/narray/src/mh/prod.h +69 -0
  21. data/ext/numo/narray/src/mh/ptp.h +69 -0
  22. data/ext/numo/narray/src/mh/rand.h +315 -0
  23. data/ext/numo/narray/src/mh/seq.h +130 -0
  24. data/ext/numo/narray/src/mh/sum.h +69 -0
  25. data/ext/numo/narray/src/t_dcomplex.c +131 -667
  26. data/ext/numo/narray/src/t_dfloat.c +40 -1288
  27. data/ext/numo/narray/src/t_int16.c +262 -1161
  28. data/ext/numo/narray/src/t_int32.c +263 -1161
  29. data/ext/numo/narray/src/t_int64.c +262 -1163
  30. data/ext/numo/narray/src/t_int8.c +262 -1159
  31. data/ext/numo/narray/src/t_robject.c +427 -1675
  32. data/ext/numo/narray/src/t_scomplex.c +131 -667
  33. data/ext/numo/narray/src/t_sfloat.c +40 -1288
  34. data/ext/numo/narray/src/t_uint16.c +262 -1161
  35. data/ext/numo/narray/src/t_uint32.c +262 -1161
  36. data/ext/numo/narray/src/t_uint64.c +262 -1163
  37. data/ext/numo/narray/src/t_uint8.c +262 -1161
  38. data/lib/numo/narray.rb +3 -1
  39. metadata +23 -3
@@ -42,10 +42,30 @@ static ID id_to_a;
42
42
  VALUE cT;
43
43
  extern VALUE cRT;
44
44
 
45
+ #include "mh/clip.h"
46
+ #include "mh/sum.h"
47
+ #include "mh/prod.h"
45
48
  #include "mh/mean.h"
46
49
  #include "mh/var.h"
47
50
  #include "mh/stddev.h"
48
51
  #include "mh/rms.h"
52
+ #include "mh/min.h"
53
+ #include "mh/max.h"
54
+ #include "mh/ptp.h"
55
+ #include "mh/max_index.h"
56
+ #include "mh/min_index.h"
57
+ #include "mh/argmax.h"
58
+ #include "mh/argmin.h"
59
+ #include "mh/maximum.h"
60
+ #include "mh/minimum.h"
61
+ #include "mh/minmax.h"
62
+ #include "mh/cumsum.h"
63
+ #include "mh/cumprod.h"
64
+ #include "mh/mulsum.h"
65
+ #include "mh/seq.h"
66
+ #include "mh/logseq.h"
67
+ #include "mh/eye.h"
68
+ #include "mh/rand.h"
49
69
  #include "mh/math/sqrt.h"
50
70
  #include "mh/math/cbrt.h"
51
71
  #include "mh/math/log.h"
@@ -78,10 +98,30 @@ extern VALUE cRT;
78
98
 
79
99
  typedef float sfloat; // Type aliases for shorter notation
80
100
  // following the codebase naming convention.
101
+ DEF_NARRAY_CLIP_METHOD_FUNC(sfloat, numo_cSFloat)
102
+ DEF_NARRAY_FLT_SUM_METHOD_FUNC(sfloat, numo_cSFloat)
103
+ DEF_NARRAY_FLT_PROD_METHOD_FUNC(sfloat, numo_cSFloat)
81
104
  DEF_NARRAY_FLT_MEAN_METHOD_FUNC(sfloat, numo_cSFloat, float, numo_cSFloat)
82
105
  DEF_NARRAY_FLT_VAR_METHOD_FUNC(sfloat, numo_cSFloat, float, numo_cSFloat)
83
106
  DEF_NARRAY_FLT_STDDEV_METHOD_FUNC(sfloat, numo_cSFloat, float, numo_cSFloat)
84
107
  DEF_NARRAY_FLT_RMS_METHOD_FUNC(sfloat, numo_cSFloat, float, numo_cSFloat)
108
+ DEF_NARRAY_FLT_MIN_METHOD_FUNC(sfloat, numo_cSFloat)
109
+ DEF_NARRAY_FLT_MAX_METHOD_FUNC(sfloat, numo_cSFloat)
110
+ DEF_NARRAY_FLT_PTP_METHOD_FUNC(sfloat, numo_cSFloat)
111
+ DEF_NARRAY_FLT_MAX_INDEX_METHOD_FUNC(sfloat)
112
+ DEF_NARRAY_FLT_MIN_INDEX_METHOD_FUNC(sfloat)
113
+ DEF_NARRAY_FLT_ARGMAX_METHOD_FUNC(sfloat)
114
+ DEF_NARRAY_FLT_ARGMIN_METHOD_FUNC(sfloat)
115
+ DEF_NARRAY_FLT_MAXIMUM_METHOD_FUNC(sfloat, numo_cSFloat)
116
+ DEF_NARRAY_FLT_MINIMUM_METHOD_FUNC(sfloat, numo_cSFloat)
117
+ DEF_NARRAY_FLT_MINMAX_METHOD_FUNC(sfloat, numo_cSFloat)
118
+ DEF_NARRAY_FLT_CUMSUM_METHOD_FUNC(sfloat, numo_cSFloat)
119
+ DEF_NARRAY_FLT_CUMPROD_METHOD_FUNC(sfloat, numo_cSFloat)
120
+ DEF_NARRAY_FLT_MULSUM_METHOD_FUNC(sfloat, numo_cSFloat)
121
+ DEF_NARRAY_FLT_SEQ_METHOD_FUNC(sfloat)
122
+ DEF_NARRAY_FLT_LOGSEQ_METHOD_FUNC(sfloat)
123
+ DEF_NARRAY_EYE_METHOD_FUNC(sfloat)
124
+ DEF_NARRAY_FLT_RAND_METHOD_FUNC(sfloat)
85
125
  #ifdef __SSE2__
86
126
  DEF_NARRAY_FLT_SQRT_SSE2_SGL_METHOD_FUNC(sfloat, numo_cSFloat)
87
127
  #else
@@ -3918,93 +3958,6 @@ static VALUE sfloat_le(VALUE self, VALUE other) {
3918
3958
  }
3919
3959
  }
3920
3960
 
3921
- static void iter_sfloat_clip(na_loop_t* const lp) {
3922
- size_t i;
3923
- char *p1, *p2, *p3, *p4;
3924
- ssize_t s1, s2, s3, s4;
3925
- dtype x, min, max;
3926
- INIT_COUNTER(lp, i);
3927
- INIT_PTR(lp, 0, p1, s1);
3928
- INIT_PTR(lp, 1, p2, s2);
3929
- INIT_PTR(lp, 2, p3, s3);
3930
- INIT_PTR(lp, 3, p4, s4);
3931
- for (; i--;) {
3932
- GET_DATA_STRIDE(p1, s1, dtype, x);
3933
- GET_DATA_STRIDE(p2, s2, dtype, min);
3934
- GET_DATA_STRIDE(p3, s3, dtype, max);
3935
- if (m_gt(min, max)) {
3936
- rb_raise(nary_eOperationError, "min is greater than max");
3937
- }
3938
- if (m_lt(x, min)) {
3939
- x = min;
3940
- }
3941
- if (m_gt(x, max)) {
3942
- x = max;
3943
- }
3944
- SET_DATA_STRIDE(p4, s4, dtype, x);
3945
- }
3946
- }
3947
-
3948
- static void iter_sfloat_clip_min(na_loop_t* const lp) {
3949
- size_t i;
3950
- char *p1, *p2, *p3;
3951
- ssize_t s1, s2, s3;
3952
- dtype x, min;
3953
- INIT_COUNTER(lp, i);
3954
- INIT_PTR(lp, 0, p1, s1);
3955
- INIT_PTR(lp, 1, p2, s2);
3956
- INIT_PTR(lp, 2, p3, s3);
3957
- for (; i--;) {
3958
- GET_DATA_STRIDE(p1, s1, dtype, x);
3959
- GET_DATA_STRIDE(p2, s2, dtype, min);
3960
- if (m_lt(x, min)) {
3961
- x = min;
3962
- }
3963
- SET_DATA_STRIDE(p3, s3, dtype, x);
3964
- }
3965
- }
3966
-
3967
- static void iter_sfloat_clip_max(na_loop_t* const lp) {
3968
- size_t i;
3969
- char *p1, *p2, *p3;
3970
- ssize_t s1, s2, s3;
3971
- dtype x, max;
3972
- INIT_COUNTER(lp, i);
3973
- INIT_PTR(lp, 0, p1, s1);
3974
- INIT_PTR(lp, 1, p2, s2);
3975
- INIT_PTR(lp, 2, p3, s3);
3976
- for (; i--;) {
3977
- GET_DATA_STRIDE(p1, s1, dtype, x);
3978
- GET_DATA_STRIDE(p2, s2, dtype, max);
3979
- if (m_gt(x, max)) {
3980
- x = max;
3981
- }
3982
- SET_DATA_STRIDE(p3, s3, dtype, x);
3983
- }
3984
- }
3985
-
3986
- static VALUE sfloat_clip(VALUE self, VALUE min, VALUE max) {
3987
- ndfunc_arg_in_t ain[3] = { { Qnil, 0 }, { cT, 0 }, { cT, 0 } };
3988
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
3989
- ndfunc_t ndf_min = { iter_sfloat_clip_min, STRIDE_LOOP, 2, 1, ain, aout };
3990
- ndfunc_t ndf_max = { iter_sfloat_clip_max, STRIDE_LOOP, 2, 1, ain, aout };
3991
- ndfunc_t ndf_both = { iter_sfloat_clip, STRIDE_LOOP, 3, 1, ain, aout };
3992
-
3993
- if (RTEST(min)) {
3994
- if (RTEST(max)) {
3995
- return na_ndloop(&ndf_both, 3, self, min, max);
3996
- } else {
3997
- return na_ndloop(&ndf_min, 2, self, min);
3998
- }
3999
- } else {
4000
- if (RTEST(max)) {
4001
- return na_ndloop(&ndf_max, 2, self, max);
4002
- }
4003
- }
4004
- rb_raise(rb_eArgError, "min and max are not given");
4005
- return Qnil;
4006
- }
4007
-
4008
3961
  static void iter_sfloat_isnan(na_loop_t* const lp) {
4009
3962
  size_t i;
4010
3963
  char* p1;
@@ -4190,1207 +4143,6 @@ static VALUE sfloat_isfinite(VALUE self) {
4190
4143
  return na_ndloop(&ndf, 1, self);
4191
4144
  }
4192
4145
 
4193
- static void iter_sfloat_sum(na_loop_t* const lp) {
4194
- size_t n;
4195
- char *p1, *p2;
4196
- ssize_t s1;
4197
-
4198
- INIT_COUNTER(lp, n);
4199
- INIT_PTR(lp, 0, p1, s1);
4200
- p2 = lp->args[1].ptr + lp->args[1].iter[0].pos;
4201
-
4202
- *(dtype*)p2 = f_sum(n, p1, s1);
4203
- }
4204
- static void iter_sfloat_sum_nan(na_loop_t* const lp) {
4205
- size_t n;
4206
- char *p1, *p2;
4207
- ssize_t s1;
4208
-
4209
- INIT_COUNTER(lp, n);
4210
- INIT_PTR(lp, 0, p1, s1);
4211
- p2 = lp->args[1].ptr + lp->args[1].iter[0].pos;
4212
-
4213
- *(dtype*)p2 = f_sum_nan(n, p1, s1);
4214
- }
4215
-
4216
- static VALUE sfloat_sum(int argc, VALUE* argv, VALUE self) {
4217
- VALUE v, reduce;
4218
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { sym_reduce, 0 } };
4219
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
4220
- ndfunc_t ndf = { iter_sfloat_sum, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE, 2, 1, ain, aout };
4221
-
4222
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, iter_sfloat_sum_nan);
4223
-
4224
- v = na_ndloop(&ndf, 2, self, reduce);
4225
-
4226
- return sfloat_extract(v);
4227
- }
4228
-
4229
- static void iter_sfloat_prod(na_loop_t* const lp) {
4230
- size_t n;
4231
- char *p1, *p2;
4232
- ssize_t s1;
4233
-
4234
- INIT_COUNTER(lp, n);
4235
- INIT_PTR(lp, 0, p1, s1);
4236
- p2 = lp->args[1].ptr + lp->args[1].iter[0].pos;
4237
-
4238
- *(dtype*)p2 = f_prod(n, p1, s1);
4239
- }
4240
- static void iter_sfloat_prod_nan(na_loop_t* const lp) {
4241
- size_t n;
4242
- char *p1, *p2;
4243
- ssize_t s1;
4244
-
4245
- INIT_COUNTER(lp, n);
4246
- INIT_PTR(lp, 0, p1, s1);
4247
- p2 = lp->args[1].ptr + lp->args[1].iter[0].pos;
4248
-
4249
- *(dtype*)p2 = f_prod_nan(n, p1, s1);
4250
- }
4251
-
4252
- static VALUE sfloat_prod(int argc, VALUE* argv, VALUE self) {
4253
- VALUE v, reduce;
4254
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { sym_reduce, 0 } };
4255
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
4256
- ndfunc_t ndf = { iter_sfloat_prod, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE, 2, 1, ain, aout };
4257
-
4258
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, iter_sfloat_prod_nan);
4259
-
4260
- v = na_ndloop(&ndf, 2, self, reduce);
4261
-
4262
- return sfloat_extract(v);
4263
- }
4264
-
4265
- static void iter_sfloat_min(na_loop_t* const lp) {
4266
- size_t n;
4267
- char *p1, *p2;
4268
- ssize_t s1;
4269
-
4270
- INIT_COUNTER(lp, n);
4271
- INIT_PTR(lp, 0, p1, s1);
4272
- p2 = lp->args[1].ptr + lp->args[1].iter[0].pos;
4273
-
4274
- *(dtype*)p2 = f_min(n, p1, s1);
4275
- }
4276
- static void iter_sfloat_min_nan(na_loop_t* const lp) {
4277
- size_t n;
4278
- char *p1, *p2;
4279
- ssize_t s1;
4280
-
4281
- INIT_COUNTER(lp, n);
4282
- INIT_PTR(lp, 0, p1, s1);
4283
- p2 = lp->args[1].ptr + lp->args[1].iter[0].pos;
4284
-
4285
- *(dtype*)p2 = f_min_nan(n, p1, s1);
4286
- }
4287
-
4288
- static VALUE sfloat_min(int argc, VALUE* argv, VALUE self) {
4289
- VALUE v, reduce;
4290
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { sym_reduce, 0 } };
4291
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
4292
- ndfunc_t ndf = { iter_sfloat_min, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE, 2, 1, ain, aout };
4293
-
4294
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, iter_sfloat_min_nan);
4295
-
4296
- v = na_ndloop(&ndf, 2, self, reduce);
4297
-
4298
- return sfloat_extract(v);
4299
- }
4300
-
4301
- static void iter_sfloat_max(na_loop_t* const lp) {
4302
- size_t n;
4303
- char *p1, *p2;
4304
- ssize_t s1;
4305
-
4306
- INIT_COUNTER(lp, n);
4307
- INIT_PTR(lp, 0, p1, s1);
4308
- p2 = lp->args[1].ptr + lp->args[1].iter[0].pos;
4309
-
4310
- *(dtype*)p2 = f_max(n, p1, s1);
4311
- }
4312
- static void iter_sfloat_max_nan(na_loop_t* const lp) {
4313
- size_t n;
4314
- char *p1, *p2;
4315
- ssize_t s1;
4316
-
4317
- INIT_COUNTER(lp, n);
4318
- INIT_PTR(lp, 0, p1, s1);
4319
- p2 = lp->args[1].ptr + lp->args[1].iter[0].pos;
4320
-
4321
- *(dtype*)p2 = f_max_nan(n, p1, s1);
4322
- }
4323
-
4324
- static VALUE sfloat_max(int argc, VALUE* argv, VALUE self) {
4325
- VALUE v, reduce;
4326
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { sym_reduce, 0 } };
4327
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
4328
- ndfunc_t ndf = { iter_sfloat_max, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE, 2, 1, ain, aout };
4329
-
4330
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, iter_sfloat_max_nan);
4331
-
4332
- v = na_ndloop(&ndf, 2, self, reduce);
4333
-
4334
- return sfloat_extract(v);
4335
- }
4336
-
4337
- static void iter_sfloat_ptp(na_loop_t* const lp) {
4338
- size_t n;
4339
- char *p1, *p2;
4340
- ssize_t s1;
4341
-
4342
- INIT_COUNTER(lp, n);
4343
- INIT_PTR(lp, 0, p1, s1);
4344
- p2 = lp->args[1].ptr + lp->args[1].iter[0].pos;
4345
-
4346
- *(dtype*)p2 = f_ptp(n, p1, s1);
4347
- }
4348
- static void iter_sfloat_ptp_nan(na_loop_t* const lp) {
4349
- size_t n;
4350
- char *p1, *p2;
4351
- ssize_t s1;
4352
-
4353
- INIT_COUNTER(lp, n);
4354
- INIT_PTR(lp, 0, p1, s1);
4355
- p2 = lp->args[1].ptr + lp->args[1].iter[0].pos;
4356
-
4357
- *(dtype*)p2 = f_ptp_nan(n, p1, s1);
4358
- }
4359
-
4360
- static VALUE sfloat_ptp(int argc, VALUE* argv, VALUE self) {
4361
- VALUE v, reduce;
4362
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { sym_reduce, 0 } };
4363
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
4364
- ndfunc_t ndf = { iter_sfloat_ptp, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE, 2, 1, ain, aout };
4365
-
4366
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, iter_sfloat_ptp_nan);
4367
-
4368
- v = na_ndloop(&ndf, 2, self, reduce);
4369
-
4370
- return sfloat_extract(v);
4371
- }
4372
-
4373
- #define idx_t int64_t
4374
- static void iter_sfloat_max_index_index64(na_loop_t* const lp) {
4375
- size_t n, idx;
4376
- char *d_ptr, *i_ptr, *o_ptr;
4377
- ssize_t d_step, i_step;
4378
-
4379
- INIT_COUNTER(lp, n);
4380
- INIT_PTR(lp, 0, d_ptr, d_step);
4381
-
4382
- idx = f_max_index(n, d_ptr, d_step);
4383
-
4384
- INIT_PTR(lp, 1, i_ptr, i_step);
4385
- o_ptr = NDL_PTR(lp, 2);
4386
- *(idx_t*)o_ptr = *(idx_t*)(i_ptr + i_step * idx);
4387
- }
4388
- #undef idx_t
4389
-
4390
- #define idx_t int32_t
4391
- static void iter_sfloat_max_index_index32(na_loop_t* const lp) {
4392
- size_t n, idx;
4393
- char *d_ptr, *i_ptr, *o_ptr;
4394
- ssize_t d_step, i_step;
4395
-
4396
- INIT_COUNTER(lp, n);
4397
- INIT_PTR(lp, 0, d_ptr, d_step);
4398
-
4399
- idx = f_max_index(n, d_ptr, d_step);
4400
-
4401
- INIT_PTR(lp, 1, i_ptr, i_step);
4402
- o_ptr = NDL_PTR(lp, 2);
4403
- *(idx_t*)o_ptr = *(idx_t*)(i_ptr + i_step * idx);
4404
- }
4405
- #undef idx_t
4406
-
4407
- #define idx_t int64_t
4408
- static void iter_sfloat_max_index_index64_nan(na_loop_t* const lp) {
4409
- size_t n, idx;
4410
- char *d_ptr, *i_ptr, *o_ptr;
4411
- ssize_t d_step, i_step;
4412
-
4413
- INIT_COUNTER(lp, n);
4414
- INIT_PTR(lp, 0, d_ptr, d_step);
4415
-
4416
- idx = f_max_index_nan(n, d_ptr, d_step);
4417
-
4418
- INIT_PTR(lp, 1, i_ptr, i_step);
4419
- o_ptr = NDL_PTR(lp, 2);
4420
- *(idx_t*)o_ptr = *(idx_t*)(i_ptr + i_step * idx);
4421
- }
4422
- #undef idx_t
4423
-
4424
- #define idx_t int32_t
4425
- static void iter_sfloat_max_index_index32_nan(na_loop_t* const lp) {
4426
- size_t n, idx;
4427
- char *d_ptr, *i_ptr, *o_ptr;
4428
- ssize_t d_step, i_step;
4429
-
4430
- INIT_COUNTER(lp, n);
4431
- INIT_PTR(lp, 0, d_ptr, d_step);
4432
-
4433
- idx = f_max_index_nan(n, d_ptr, d_step);
4434
-
4435
- INIT_PTR(lp, 1, i_ptr, i_step);
4436
- o_ptr = NDL_PTR(lp, 2);
4437
- *(idx_t*)o_ptr = *(idx_t*)(i_ptr + i_step * idx);
4438
- }
4439
- #undef idx_t
4440
-
4441
- static VALUE sfloat_max_index(int argc, VALUE* argv, VALUE self) {
4442
- narray_t* na;
4443
- VALUE idx, reduce;
4444
- ndfunc_arg_in_t ain[3] = { { Qnil, 0 }, { Qnil, 0 }, { sym_reduce, 0 } };
4445
- ndfunc_arg_out_t aout[1] = { { 0, 0, 0 } };
4446
- ndfunc_t ndf = { 0, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE | NDF_EXTRACT, 3, 1, ain, aout };
4447
-
4448
- GetNArray(self, na);
4449
- if (na->ndim == 0) {
4450
- return INT2FIX(0);
4451
- }
4452
- if (na->size > (~(u_int32_t)0)) {
4453
- aout[0].type = numo_cInt64;
4454
- idx = nary_new(numo_cInt64, na->ndim, na->shape);
4455
- ndf.func = iter_sfloat_max_index_index64;
4456
-
4457
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, iter_sfloat_max_index_index64_nan);
4458
-
4459
- } else {
4460
- aout[0].type = numo_cInt32;
4461
- idx = nary_new(numo_cInt32, na->ndim, na->shape);
4462
- ndf.func = iter_sfloat_max_index_index32;
4463
-
4464
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, iter_sfloat_max_index_index32_nan);
4465
- }
4466
- rb_funcall(idx, rb_intern("seq"), 0);
4467
-
4468
- return na_ndloop(&ndf, 3, self, idx, reduce);
4469
- }
4470
-
4471
- #define idx_t int64_t
4472
- static void iter_sfloat_min_index_index64(na_loop_t* const lp) {
4473
- size_t n, idx;
4474
- char *d_ptr, *i_ptr, *o_ptr;
4475
- ssize_t d_step, i_step;
4476
-
4477
- INIT_COUNTER(lp, n);
4478
- INIT_PTR(lp, 0, d_ptr, d_step);
4479
-
4480
- idx = f_min_index(n, d_ptr, d_step);
4481
-
4482
- INIT_PTR(lp, 1, i_ptr, i_step);
4483
- o_ptr = NDL_PTR(lp, 2);
4484
- *(idx_t*)o_ptr = *(idx_t*)(i_ptr + i_step * idx);
4485
- }
4486
- #undef idx_t
4487
-
4488
- #define idx_t int32_t
4489
- static void iter_sfloat_min_index_index32(na_loop_t* const lp) {
4490
- size_t n, idx;
4491
- char *d_ptr, *i_ptr, *o_ptr;
4492
- ssize_t d_step, i_step;
4493
-
4494
- INIT_COUNTER(lp, n);
4495
- INIT_PTR(lp, 0, d_ptr, d_step);
4496
-
4497
- idx = f_min_index(n, d_ptr, d_step);
4498
-
4499
- INIT_PTR(lp, 1, i_ptr, i_step);
4500
- o_ptr = NDL_PTR(lp, 2);
4501
- *(idx_t*)o_ptr = *(idx_t*)(i_ptr + i_step * idx);
4502
- }
4503
- #undef idx_t
4504
-
4505
- #define idx_t int64_t
4506
- static void iter_sfloat_min_index_index64_nan(na_loop_t* const lp) {
4507
- size_t n, idx;
4508
- char *d_ptr, *i_ptr, *o_ptr;
4509
- ssize_t d_step, i_step;
4510
-
4511
- INIT_COUNTER(lp, n);
4512
- INIT_PTR(lp, 0, d_ptr, d_step);
4513
-
4514
- idx = f_min_index_nan(n, d_ptr, d_step);
4515
-
4516
- INIT_PTR(lp, 1, i_ptr, i_step);
4517
- o_ptr = NDL_PTR(lp, 2);
4518
- *(idx_t*)o_ptr = *(idx_t*)(i_ptr + i_step * idx);
4519
- }
4520
- #undef idx_t
4521
-
4522
- #define idx_t int32_t
4523
- static void iter_sfloat_min_index_index32_nan(na_loop_t* const lp) {
4524
- size_t n, idx;
4525
- char *d_ptr, *i_ptr, *o_ptr;
4526
- ssize_t d_step, i_step;
4527
-
4528
- INIT_COUNTER(lp, n);
4529
- INIT_PTR(lp, 0, d_ptr, d_step);
4530
-
4531
- idx = f_min_index_nan(n, d_ptr, d_step);
4532
-
4533
- INIT_PTR(lp, 1, i_ptr, i_step);
4534
- o_ptr = NDL_PTR(lp, 2);
4535
- *(idx_t*)o_ptr = *(idx_t*)(i_ptr + i_step * idx);
4536
- }
4537
- #undef idx_t
4538
-
4539
- static VALUE sfloat_min_index(int argc, VALUE* argv, VALUE self) {
4540
- narray_t* na;
4541
- VALUE idx, reduce;
4542
- ndfunc_arg_in_t ain[3] = { { Qnil, 0 }, { Qnil, 0 }, { sym_reduce, 0 } };
4543
- ndfunc_arg_out_t aout[1] = { { 0, 0, 0 } };
4544
- ndfunc_t ndf = { 0, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE | NDF_EXTRACT, 3, 1, ain, aout };
4545
-
4546
- GetNArray(self, na);
4547
- if (na->ndim == 0) {
4548
- return INT2FIX(0);
4549
- }
4550
- if (na->size > (~(u_int32_t)0)) {
4551
- aout[0].type = numo_cInt64;
4552
- idx = nary_new(numo_cInt64, na->ndim, na->shape);
4553
- ndf.func = iter_sfloat_min_index_index64;
4554
-
4555
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, iter_sfloat_min_index_index64_nan);
4556
-
4557
- } else {
4558
- aout[0].type = numo_cInt32;
4559
- idx = nary_new(numo_cInt32, na->ndim, na->shape);
4560
- ndf.func = iter_sfloat_min_index_index32;
4561
-
4562
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, iter_sfloat_min_index_index32_nan);
4563
- }
4564
- rb_funcall(idx, rb_intern("seq"), 0);
4565
-
4566
- return na_ndloop(&ndf, 3, self, idx, reduce);
4567
- }
4568
-
4569
- #define idx_t int64_t
4570
- static void iter_sfloat_argmax_arg64(na_loop_t* const lp) {
4571
- size_t n, idx;
4572
- char *d_ptr, *o_ptr;
4573
- ssize_t d_step;
4574
-
4575
- INIT_COUNTER(lp, n);
4576
- INIT_PTR(lp, 0, d_ptr, d_step);
4577
-
4578
- idx = f_max_index(n, d_ptr, d_step);
4579
-
4580
- o_ptr = NDL_PTR(lp, 1);
4581
- *(idx_t*)o_ptr = (idx_t)idx;
4582
- }
4583
- #undef idx_t
4584
-
4585
- #define idx_t int32_t
4586
- static void iter_sfloat_argmax_arg32(na_loop_t* const lp) {
4587
- size_t n, idx;
4588
- char *d_ptr, *o_ptr;
4589
- ssize_t d_step;
4590
-
4591
- INIT_COUNTER(lp, n);
4592
- INIT_PTR(lp, 0, d_ptr, d_step);
4593
-
4594
- idx = f_max_index(n, d_ptr, d_step);
4595
-
4596
- o_ptr = NDL_PTR(lp, 1);
4597
- *(idx_t*)o_ptr = (idx_t)idx;
4598
- }
4599
- #undef idx_t
4600
-
4601
- #define idx_t int64_t
4602
- static void iter_sfloat_argmax_arg64_nan(na_loop_t* const lp) {
4603
- size_t n, idx;
4604
- char *d_ptr, *o_ptr;
4605
- ssize_t d_step;
4606
-
4607
- INIT_COUNTER(lp, n);
4608
- INIT_PTR(lp, 0, d_ptr, d_step);
4609
-
4610
- idx = f_max_index_nan(n, d_ptr, d_step);
4611
-
4612
- o_ptr = NDL_PTR(lp, 1);
4613
- *(idx_t*)o_ptr = (idx_t)idx;
4614
- }
4615
- #undef idx_t
4616
-
4617
- #define idx_t int32_t
4618
- static void iter_sfloat_argmax_arg32_nan(na_loop_t* const lp) {
4619
- size_t n, idx;
4620
- char *d_ptr, *o_ptr;
4621
- ssize_t d_step;
4622
-
4623
- INIT_COUNTER(lp, n);
4624
- INIT_PTR(lp, 0, d_ptr, d_step);
4625
-
4626
- idx = f_max_index_nan(n, d_ptr, d_step);
4627
-
4628
- o_ptr = NDL_PTR(lp, 1);
4629
- *(idx_t*)o_ptr = (idx_t)idx;
4630
- }
4631
- #undef idx_t
4632
-
4633
- static VALUE sfloat_argmax(int argc, VALUE* argv, VALUE self) {
4634
- narray_t* na;
4635
- VALUE reduce;
4636
- ndfunc_arg_in_t ain[2] = { { Qnil, 0 }, { sym_reduce, 0 } };
4637
- ndfunc_arg_out_t aout[1] = { { 0, 0, 0 } };
4638
- ndfunc_t ndf = { 0, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE | NDF_EXTRACT, 2, 1, ain, aout };
4639
-
4640
- GetNArray(self, na);
4641
- if (na->ndim == 0) {
4642
- return INT2FIX(0);
4643
- }
4644
- if (na->size > (~(u_int32_t)0)) {
4645
- aout[0].type = numo_cInt64;
4646
- ndf.func = iter_sfloat_argmax_arg64;
4647
-
4648
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, iter_sfloat_argmax_arg64_nan);
4649
-
4650
- } else {
4651
- aout[0].type = numo_cInt32;
4652
- ndf.func = iter_sfloat_argmax_arg32;
4653
-
4654
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, iter_sfloat_argmax_arg32_nan);
4655
- }
4656
-
4657
- return na_ndloop(&ndf, 2, self, reduce);
4658
- }
4659
-
4660
- #define idx_t int64_t
4661
- static void iter_sfloat_argmin_arg64(na_loop_t* const lp) {
4662
- size_t n, idx;
4663
- char *d_ptr, *o_ptr;
4664
- ssize_t d_step;
4665
-
4666
- INIT_COUNTER(lp, n);
4667
- INIT_PTR(lp, 0, d_ptr, d_step);
4668
-
4669
- idx = f_min_index(n, d_ptr, d_step);
4670
-
4671
- o_ptr = NDL_PTR(lp, 1);
4672
- *(idx_t*)o_ptr = (idx_t)idx;
4673
- }
4674
- #undef idx_t
4675
-
4676
- #define idx_t int32_t
4677
- static void iter_sfloat_argmin_arg32(na_loop_t* const lp) {
4678
- size_t n, idx;
4679
- char *d_ptr, *o_ptr;
4680
- ssize_t d_step;
4681
-
4682
- INIT_COUNTER(lp, n);
4683
- INIT_PTR(lp, 0, d_ptr, d_step);
4684
-
4685
- idx = f_min_index(n, d_ptr, d_step);
4686
-
4687
- o_ptr = NDL_PTR(lp, 1);
4688
- *(idx_t*)o_ptr = (idx_t)idx;
4689
- }
4690
- #undef idx_t
4691
-
4692
- #define idx_t int64_t
4693
- static void iter_sfloat_argmin_arg64_nan(na_loop_t* const lp) {
4694
- size_t n, idx;
4695
- char *d_ptr, *o_ptr;
4696
- ssize_t d_step;
4697
-
4698
- INIT_COUNTER(lp, n);
4699
- INIT_PTR(lp, 0, d_ptr, d_step);
4700
-
4701
- idx = f_min_index_nan(n, d_ptr, d_step);
4702
-
4703
- o_ptr = NDL_PTR(lp, 1);
4704
- *(idx_t*)o_ptr = (idx_t)idx;
4705
- }
4706
- #undef idx_t
4707
-
4708
- #define idx_t int32_t
4709
- static void iter_sfloat_argmin_arg32_nan(na_loop_t* const lp) {
4710
- size_t n, idx;
4711
- char *d_ptr, *o_ptr;
4712
- ssize_t d_step;
4713
-
4714
- INIT_COUNTER(lp, n);
4715
- INIT_PTR(lp, 0, d_ptr, d_step);
4716
-
4717
- idx = f_min_index_nan(n, d_ptr, d_step);
4718
-
4719
- o_ptr = NDL_PTR(lp, 1);
4720
- *(idx_t*)o_ptr = (idx_t)idx;
4721
- }
4722
- #undef idx_t
4723
-
4724
- static VALUE sfloat_argmin(int argc, VALUE* argv, VALUE self) {
4725
- narray_t* na;
4726
- VALUE reduce;
4727
- ndfunc_arg_in_t ain[2] = { { Qnil, 0 }, { sym_reduce, 0 } };
4728
- ndfunc_arg_out_t aout[1] = { { 0, 0, 0 } };
4729
- ndfunc_t ndf = { 0, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE | NDF_EXTRACT, 2, 1, ain, aout };
4730
-
4731
- GetNArray(self, na);
4732
- if (na->ndim == 0) {
4733
- return INT2FIX(0);
4734
- }
4735
- if (na->size > (~(u_int32_t)0)) {
4736
- aout[0].type = numo_cInt64;
4737
- ndf.func = iter_sfloat_argmin_arg64;
4738
-
4739
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, iter_sfloat_argmin_arg64_nan);
4740
-
4741
- } else {
4742
- aout[0].type = numo_cInt32;
4743
- ndf.func = iter_sfloat_argmin_arg32;
4744
-
4745
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, iter_sfloat_argmin_arg32_nan);
4746
- }
4747
-
4748
- return na_ndloop(&ndf, 2, self, reduce);
4749
- }
4750
-
4751
- static void iter_sfloat_minmax(na_loop_t* const lp) {
4752
- size_t n;
4753
- char* p1;
4754
- ssize_t s1;
4755
- dtype xmin, xmax;
4756
-
4757
- INIT_COUNTER(lp, n);
4758
- INIT_PTR(lp, 0, p1, s1);
4759
-
4760
- f_minmax(n, p1, s1, &xmin, &xmax);
4761
-
4762
- *(dtype*)(lp->args[1].ptr + lp->args[1].iter[0].pos) = xmin;
4763
- *(dtype*)(lp->args[2].ptr + lp->args[2].iter[0].pos) = xmax;
4764
- }
4765
- static void iter_sfloat_minmax_nan(na_loop_t* const lp) {
4766
- size_t n;
4767
- char* p1;
4768
- ssize_t s1;
4769
- dtype xmin, xmax;
4770
-
4771
- INIT_COUNTER(lp, n);
4772
- INIT_PTR(lp, 0, p1, s1);
4773
-
4774
- f_minmax_nan(n, p1, s1, &xmin, &xmax);
4775
-
4776
- *(dtype*)(lp->args[1].ptr + lp->args[1].iter[0].pos) = xmin;
4777
- *(dtype*)(lp->args[2].ptr + lp->args[2].iter[0].pos) = xmax;
4778
- }
4779
-
4780
- static VALUE sfloat_minmax(int argc, VALUE* argv, VALUE self) {
4781
- VALUE reduce;
4782
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { sym_reduce, 0 } };
4783
- ndfunc_arg_out_t aout[2] = { { cT, 0 }, { cT, 0 } };
4784
- ndfunc_t ndf = {
4785
- iter_sfloat_minmax, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE | NDF_EXTRACT, 2, 2, ain, aout
4786
- };
4787
-
4788
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, iter_sfloat_minmax_nan);
4789
-
4790
- return na_ndloop(&ndf, 2, self, reduce);
4791
- }
4792
-
4793
- static void iter_sfloat_s_maximum(na_loop_t* const lp) {
4794
- size_t i, n;
4795
- char *p1, *p2, *p3;
4796
- ssize_t s1, s2, s3;
4797
-
4798
- INIT_COUNTER(lp, n);
4799
- INIT_PTR(lp, 0, p1, s1);
4800
- INIT_PTR(lp, 1, p2, s2);
4801
- INIT_PTR(lp, 2, p3, s3);
4802
-
4803
- for (i = 0; i < n; i++) {
4804
- dtype x, y, z;
4805
- GET_DATA_STRIDE(p1, s1, dtype, x);
4806
- GET_DATA_STRIDE(p2, s2, dtype, y);
4807
- GET_DATA(p3, dtype, z);
4808
- z = f_maximum(x, y);
4809
- SET_DATA_STRIDE(p3, s3, dtype, z);
4810
- }
4811
- }
4812
- static void iter_sfloat_s_maximum_nan(na_loop_t* const lp) {
4813
- size_t i, n;
4814
- char *p1, *p2, *p3;
4815
- ssize_t s1, s2, s3;
4816
-
4817
- INIT_COUNTER(lp, n);
4818
- INIT_PTR(lp, 0, p1, s1);
4819
- INIT_PTR(lp, 1, p2, s2);
4820
- INIT_PTR(lp, 2, p3, s3);
4821
-
4822
- for (i = 0; i < n; i++) {
4823
- dtype x, y, z;
4824
- GET_DATA_STRIDE(p1, s1, dtype, x);
4825
- GET_DATA_STRIDE(p2, s2, dtype, y);
4826
- GET_DATA(p3, dtype, z);
4827
- z = f_maximum_nan(x, y);
4828
- SET_DATA_STRIDE(p3, s3, dtype, z);
4829
- }
4830
- }
4831
-
4832
- static VALUE sfloat_s_maximum(int argc, VALUE* argv, VALUE mod) {
4833
- VALUE a1 = Qnil;
4834
- VALUE a2 = Qnil;
4835
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
4836
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
4837
- ndfunc_t ndf = { iter_sfloat_s_maximum, STRIDE_LOOP_NIP, 2, 1, ain, aout };
4838
-
4839
- VALUE kw_hash = Qnil;
4840
- ID kw_table[1] = { id_nan };
4841
- VALUE opts[1] = { Qundef };
4842
-
4843
- rb_scan_args(argc, argv, "20:", &a1, &a2, &kw_hash);
4844
- rb_get_kwargs(kw_hash, kw_table, 0, 1, opts);
4845
- if (opts[0] != Qundef) {
4846
- ndf.func = iter_sfloat_s_maximum_nan;
4847
- }
4848
-
4849
- return na_ndloop(&ndf, 2, a1, a2);
4850
- }
4851
-
4852
- static void iter_sfloat_s_minimum(na_loop_t* const lp) {
4853
- size_t i, n;
4854
- char *p1, *p2, *p3;
4855
- ssize_t s1, s2, s3;
4856
-
4857
- INIT_COUNTER(lp, n);
4858
- INIT_PTR(lp, 0, p1, s1);
4859
- INIT_PTR(lp, 1, p2, s2);
4860
- INIT_PTR(lp, 2, p3, s3);
4861
-
4862
- for (i = 0; i < n; i++) {
4863
- dtype x, y, z;
4864
- GET_DATA_STRIDE(p1, s1, dtype, x);
4865
- GET_DATA_STRIDE(p2, s2, dtype, y);
4866
- GET_DATA(p3, dtype, z);
4867
- z = f_minimum(x, y);
4868
- SET_DATA_STRIDE(p3, s3, dtype, z);
4869
- }
4870
- }
4871
- static void iter_sfloat_s_minimum_nan(na_loop_t* const lp) {
4872
- size_t i, n;
4873
- char *p1, *p2, *p3;
4874
- ssize_t s1, s2, s3;
4875
-
4876
- INIT_COUNTER(lp, n);
4877
- INIT_PTR(lp, 0, p1, s1);
4878
- INIT_PTR(lp, 1, p2, s2);
4879
- INIT_PTR(lp, 2, p3, s3);
4880
-
4881
- for (i = 0; i < n; i++) {
4882
- dtype x, y, z;
4883
- GET_DATA_STRIDE(p1, s1, dtype, x);
4884
- GET_DATA_STRIDE(p2, s2, dtype, y);
4885
- GET_DATA(p3, dtype, z);
4886
- z = f_minimum_nan(x, y);
4887
- SET_DATA_STRIDE(p3, s3, dtype, z);
4888
- }
4889
- }
4890
-
4891
- static VALUE sfloat_s_minimum(int argc, VALUE* argv, VALUE mod) {
4892
- VALUE a1 = Qnil;
4893
- VALUE a2 = Qnil;
4894
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
4895
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
4896
- ndfunc_t ndf = { iter_sfloat_s_minimum, STRIDE_LOOP_NIP, 2, 1, ain, aout };
4897
-
4898
- VALUE kw_hash = Qnil;
4899
- ID kw_table[1] = { id_nan };
4900
- VALUE opts[1] = { Qundef };
4901
-
4902
- rb_scan_args(argc, argv, "20:", &a1, &a2, &kw_hash);
4903
- rb_get_kwargs(kw_hash, kw_table, 0, 1, opts);
4904
- if (opts[0] != Qundef) {
4905
- ndf.func = iter_sfloat_s_minimum_nan;
4906
- }
4907
-
4908
- return na_ndloop(&ndf, 2, a1, a2);
4909
- }
4910
-
4911
- static void iter_sfloat_cumsum(na_loop_t* const lp) {
4912
- size_t i;
4913
- char *p1, *p2;
4914
- ssize_t s1, s2;
4915
- dtype x, y;
4916
-
4917
- INIT_COUNTER(lp, i);
4918
- INIT_PTR(lp, 0, p1, s1);
4919
- INIT_PTR(lp, 1, p2, s2);
4920
-
4921
- GET_DATA_STRIDE(p1, s1, dtype, x);
4922
- SET_DATA_STRIDE(p2, s2, dtype, x);
4923
- for (i--; i--;) {
4924
- GET_DATA_STRIDE(p1, s1, dtype, y);
4925
- m_cumsum(x, y);
4926
- SET_DATA_STRIDE(p2, s2, dtype, x);
4927
- }
4928
- }
4929
- static void iter_sfloat_cumsum_nan(na_loop_t* const lp) {
4930
- size_t i;
4931
- char *p1, *p2;
4932
- ssize_t s1, s2;
4933
- dtype x, y;
4934
-
4935
- INIT_COUNTER(lp, i);
4936
- INIT_PTR(lp, 0, p1, s1);
4937
- INIT_PTR(lp, 1, p2, s2);
4938
-
4939
- GET_DATA_STRIDE(p1, s1, dtype, x);
4940
- SET_DATA_STRIDE(p2, s2, dtype, x);
4941
- for (i--; i--;) {
4942
- GET_DATA_STRIDE(p1, s1, dtype, y);
4943
- m_cumsum_nan(x, y);
4944
- SET_DATA_STRIDE(p2, s2, dtype, x);
4945
- }
4946
- }
4947
-
4948
- static VALUE sfloat_cumsum(int argc, VALUE* argv, VALUE self) {
4949
- VALUE reduce;
4950
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { sym_reduce, 0 } };
4951
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
4952
- ndfunc_t ndf = {
4953
- iter_sfloat_cumsum, STRIDE_LOOP | NDF_FLAT_REDUCE | NDF_CUM, 2, 1, ain, aout
4954
- };
4955
-
4956
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, iter_sfloat_cumsum_nan);
4957
-
4958
- return na_ndloop(&ndf, 2, self, reduce);
4959
- }
4960
-
4961
- static void iter_sfloat_cumprod(na_loop_t* const lp) {
4962
- size_t i;
4963
- char *p1, *p2;
4964
- ssize_t s1, s2;
4965
- dtype x, y;
4966
-
4967
- INIT_COUNTER(lp, i);
4968
- INIT_PTR(lp, 0, p1, s1);
4969
- INIT_PTR(lp, 1, p2, s2);
4970
-
4971
- GET_DATA_STRIDE(p1, s1, dtype, x);
4972
- SET_DATA_STRIDE(p2, s2, dtype, x);
4973
- for (i--; i--;) {
4974
- GET_DATA_STRIDE(p1, s1, dtype, y);
4975
- m_cumprod(x, y);
4976
- SET_DATA_STRIDE(p2, s2, dtype, x);
4977
- }
4978
- }
4979
- static void iter_sfloat_cumprod_nan(na_loop_t* const lp) {
4980
- size_t i;
4981
- char *p1, *p2;
4982
- ssize_t s1, s2;
4983
- dtype x, y;
4984
-
4985
- INIT_COUNTER(lp, i);
4986
- INIT_PTR(lp, 0, p1, s1);
4987
- INIT_PTR(lp, 1, p2, s2);
4988
-
4989
- GET_DATA_STRIDE(p1, s1, dtype, x);
4990
- SET_DATA_STRIDE(p2, s2, dtype, x);
4991
- for (i--; i--;) {
4992
- GET_DATA_STRIDE(p1, s1, dtype, y);
4993
- m_cumprod_nan(x, y);
4994
- SET_DATA_STRIDE(p2, s2, dtype, x);
4995
- }
4996
- }
4997
-
4998
- static VALUE sfloat_cumprod(int argc, VALUE* argv, VALUE self) {
4999
- VALUE reduce;
5000
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { sym_reduce, 0 } };
5001
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
5002
- ndfunc_t ndf = {
5003
- iter_sfloat_cumprod, STRIDE_LOOP | NDF_FLAT_REDUCE | NDF_CUM, 2, 1, ain, aout
5004
- };
5005
-
5006
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, iter_sfloat_cumprod_nan);
5007
-
5008
- return na_ndloop(&ndf, 2, self, reduce);
5009
- }
5010
-
5011
- //
5012
- static void iter_sfloat_mulsum(na_loop_t* const lp) {
5013
- size_t i, n;
5014
- char *p1, *p2, *p3;
5015
- ssize_t s1, s2, s3;
5016
-
5017
- INIT_COUNTER(lp, n);
5018
- INIT_PTR(lp, 0, p1, s1);
5019
- INIT_PTR(lp, 1, p2, s2);
5020
- INIT_PTR(lp, 2, p3, s3);
5021
-
5022
- if (s3 == 0) {
5023
- dtype z;
5024
- // Reduce loop
5025
- GET_DATA(p3, dtype, z);
5026
- for (i = 0; i < n; i++) {
5027
- dtype x, y;
5028
- GET_DATA_STRIDE(p1, s1, dtype, x);
5029
- GET_DATA_STRIDE(p2, s2, dtype, y);
5030
- m_mulsum(x, y, z);
5031
- }
5032
- SET_DATA(p3, dtype, z);
5033
- return;
5034
- } else {
5035
- for (i = 0; i < n; i++) {
5036
- dtype x, y, z;
5037
- GET_DATA_STRIDE(p1, s1, dtype, x);
5038
- GET_DATA_STRIDE(p2, s2, dtype, y);
5039
- GET_DATA(p3, dtype, z);
5040
- m_mulsum(x, y, z);
5041
- SET_DATA_STRIDE(p3, s3, dtype, z);
5042
- }
5043
- }
5044
- }
5045
- //
5046
- static void iter_sfloat_mulsum_nan(na_loop_t* const lp) {
5047
- size_t i, n;
5048
- char *p1, *p2, *p3;
5049
- ssize_t s1, s2, s3;
5050
-
5051
- INIT_COUNTER(lp, n);
5052
- INIT_PTR(lp, 0, p1, s1);
5053
- INIT_PTR(lp, 1, p2, s2);
5054
- INIT_PTR(lp, 2, p3, s3);
5055
-
5056
- if (s3 == 0) {
5057
- dtype z;
5058
- // Reduce loop
5059
- GET_DATA(p3, dtype, z);
5060
- for (i = 0; i < n; i++) {
5061
- dtype x, y;
5062
- GET_DATA_STRIDE(p1, s1, dtype, x);
5063
- GET_DATA_STRIDE(p2, s2, dtype, y);
5064
- m_mulsum_nan(x, y, z);
5065
- }
5066
- SET_DATA(p3, dtype, z);
5067
- return;
5068
- } else {
5069
- for (i = 0; i < n; i++) {
5070
- dtype x, y, z;
5071
- GET_DATA_STRIDE(p1, s1, dtype, x);
5072
- GET_DATA_STRIDE(p2, s2, dtype, y);
5073
- GET_DATA(p3, dtype, z);
5074
- m_mulsum_nan(x, y, z);
5075
- SET_DATA_STRIDE(p3, s3, dtype, z);
5076
- }
5077
- }
5078
- }
5079
- //
5080
-
5081
- static VALUE sfloat_mulsum_self(int argc, VALUE* argv, VALUE self) {
5082
- VALUE v, reduce;
5083
- VALUE naryv[2];
5084
- ndfunc_arg_in_t ain[4] = { { cT, 0 }, { cT, 0 }, { sym_reduce, 0 }, { sym_init, 0 } };
5085
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
5086
- ndfunc_t ndf = { iter_sfloat_mulsum, STRIDE_LOOP_NIP, 4, 1, ain, aout };
5087
-
5088
- if (argc < 1) {
5089
- rb_raise(rb_eArgError, "wrong number of arguments (%d for >=1)", argc);
5090
- }
5091
- // should fix below: [self.ndim,other.ndim].max or?
5092
- naryv[0] = self;
5093
- naryv[1] = argv[0];
5094
- //
5095
- reduce = na_reduce_dimension(argc - 1, argv + 1, 2, naryv, &ndf, iter_sfloat_mulsum_nan);
5096
- //
5097
-
5098
- v = na_ndloop(&ndf, 4, self, argv[0], reduce, m_mulsum_init);
5099
- return sfloat_extract(v);
5100
- }
5101
-
5102
- static VALUE sfloat_mulsum(int argc, VALUE* argv, VALUE self) {
5103
- //
5104
- VALUE klass, v;
5105
- //
5106
- if (argc < 1) {
5107
- rb_raise(rb_eArgError, "wrong number of arguments (%d for >=1)", argc);
5108
- }
5109
- //
5110
- klass = na_upcast(rb_obj_class(self), rb_obj_class(argv[0]));
5111
- if (klass == cT) {
5112
- return sfloat_mulsum_self(argc, argv, self);
5113
- } else {
5114
- v = rb_funcall(klass, id_cast, 1, self);
5115
- //
5116
- return rb_funcallv_kw(v, rb_intern("mulsum"), argc, argv, RB_PASS_CALLED_KEYWORDS);
5117
- //
5118
- }
5119
- //
5120
- }
5121
-
5122
- typedef dtype seq_data_t;
5123
-
5124
- typedef double seq_count_t;
5125
-
5126
- typedef struct {
5127
- seq_data_t beg;
5128
- seq_data_t step;
5129
- seq_count_t count;
5130
- } seq_opt_t;
5131
-
5132
- static void iter_sfloat_seq(na_loop_t* const lp) {
5133
- size_t i;
5134
- char* p1;
5135
- ssize_t s1;
5136
- size_t* idx1;
5137
- dtype x;
5138
- seq_data_t beg, step;
5139
- seq_count_t c;
5140
- seq_opt_t* g;
5141
-
5142
- INIT_COUNTER(lp, i);
5143
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
5144
- g = (seq_opt_t*)(lp->opt_ptr);
5145
- beg = g->beg;
5146
- step = g->step;
5147
- c = g->count;
5148
- if (idx1) {
5149
- for (; i--;) {
5150
- x = f_seq(beg, step, c++);
5151
- *(dtype*)(p1 + *idx1) = x;
5152
- idx1++;
5153
- }
5154
- } else {
5155
- for (; i--;) {
5156
- x = f_seq(beg, step, c++);
5157
- *(dtype*)(p1) = x;
5158
- p1 += s1;
5159
- }
5160
- }
5161
- g->count = c;
5162
- }
5163
-
5164
- static VALUE sfloat_seq(int argc, VALUE* args, VALUE self) {
5165
- seq_opt_t* g;
5166
- VALUE vbeg = Qnil, vstep = Qnil;
5167
- ndfunc_arg_in_t ain[1] = { { OVERWRITE, 0 } };
5168
- ndfunc_t ndf = { iter_sfloat_seq, FULL_LOOP, 1, 0, ain, 0 };
5169
-
5170
- g = ALLOCA_N(seq_opt_t, 1);
5171
- g->beg = m_zero;
5172
- g->step = m_one;
5173
- g->count = 0;
5174
- rb_scan_args(argc, args, "02", &vbeg, &vstep);
5175
- if (vbeg != Qnil) {
5176
- g->beg = m_num_to_data(vbeg);
5177
- }
5178
- if (vstep != Qnil) {
5179
- g->step = m_num_to_data(vstep);
5180
- }
5181
-
5182
- na_ndloop3(&ndf, g, 1, self);
5183
- return self;
5184
- }
5185
-
5186
- typedef struct {
5187
- seq_data_t beg;
5188
- seq_data_t step;
5189
- seq_data_t base;
5190
- seq_count_t count;
5191
- } logseq_opt_t;
5192
-
5193
- static void iter_sfloat_logseq(na_loop_t* const lp) {
5194
- size_t i;
5195
- char* p1;
5196
- ssize_t s1;
5197
- size_t* idx1;
5198
- dtype x;
5199
- seq_data_t beg, step, base;
5200
- seq_count_t c;
5201
- logseq_opt_t* g;
5202
-
5203
- INIT_COUNTER(lp, i);
5204
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
5205
- g = (logseq_opt_t*)(lp->opt_ptr);
5206
- beg = g->beg;
5207
- step = g->step;
5208
- base = g->base;
5209
- c = g->count;
5210
- if (idx1) {
5211
- for (; i--;) {
5212
- x = f_seq(beg, step, c++);
5213
- *(dtype*)(p1 + *idx1) = m_pow(base, x);
5214
- idx1++;
5215
- }
5216
- } else {
5217
- for (; i--;) {
5218
- x = f_seq(beg, step, c++);
5219
- *(dtype*)(p1) = m_pow(base, x);
5220
- p1 += s1;
5221
- }
5222
- }
5223
- g->count = c;
5224
- }
5225
-
5226
- static VALUE sfloat_logseq(int argc, VALUE* args, VALUE self) {
5227
- logseq_opt_t* g;
5228
- VALUE vbeg, vstep, vbase;
5229
- ndfunc_arg_in_t ain[1] = { { OVERWRITE, 0 } };
5230
- ndfunc_t ndf = { iter_sfloat_logseq, FULL_LOOP, 1, 0, ain, 0 };
5231
-
5232
- g = ALLOCA_N(logseq_opt_t, 1);
5233
- rb_scan_args(argc, args, "21", &vbeg, &vstep, &vbase);
5234
- g->beg = m_num_to_data(vbeg);
5235
- g->step = m_num_to_data(vstep);
5236
- if (vbase == Qnil) {
5237
- g->base = m_from_real(10);
5238
- } else {
5239
- g->base = m_num_to_data(vbase);
5240
- }
5241
- na_ndloop3(&ndf, g, 1, self);
5242
- return self;
5243
- }
5244
-
5245
- static void iter_sfloat_eye(na_loop_t* const lp) {
5246
- size_t n0, n1;
5247
- size_t i0, i1;
5248
- ssize_t s0, s1;
5249
- char *p0, *p1;
5250
- char* g;
5251
- ssize_t kofs;
5252
- dtype data;
5253
-
5254
- g = (char*)(lp->opt_ptr);
5255
- kofs = *(ssize_t*)g;
5256
- data = *(dtype*)(g + sizeof(ssize_t));
5257
-
5258
- n0 = lp->args[0].shape[0];
5259
- n1 = lp->args[0].shape[1];
5260
- s0 = lp->args[0].iter[0].step;
5261
- s1 = lp->args[0].iter[1].step;
5262
- p0 = NDL_PTR(lp, 0);
5263
-
5264
- for (i0 = 0; i0 < n0; i0++) {
5265
- p1 = p0;
5266
- for (i1 = 0; i1 < n1; i1++) {
5267
- *(dtype*)p1 = (i0 + kofs == i1) ? data : m_zero;
5268
- p1 += s1;
5269
- }
5270
- p0 += s0;
5271
- }
5272
- }
5273
-
5274
- static VALUE sfloat_eye(int argc, VALUE* argv, VALUE self) {
5275
- ndfunc_arg_in_t ain[1] = { { OVERWRITE, 2 } };
5276
- ndfunc_t ndf = { iter_sfloat_eye, NO_LOOP, 1, 0, ain, 0 };
5277
- ssize_t kofs;
5278
- dtype data;
5279
- char* g;
5280
- int nd;
5281
- narray_t* na;
5282
-
5283
- // check arguments
5284
- if (argc > 2) {
5285
- rb_raise(rb_eArgError, "too many arguments (%d for 0..2)", argc);
5286
- } else if (argc == 2) {
5287
- data = m_num_to_data(argv[0]);
5288
- kofs = NUM2SSIZET(argv[1]);
5289
- } else if (argc == 1) {
5290
- data = m_num_to_data(argv[0]);
5291
- kofs = 0;
5292
- } else {
5293
- data = m_one;
5294
- kofs = 0;
5295
- }
5296
-
5297
- GetNArray(self, na);
5298
- nd = na->ndim;
5299
- if (nd < 2) {
5300
- rb_raise(nary_eDimensionError, "less than 2-d array");
5301
- }
5302
-
5303
- // Diagonal offset from the main diagonal.
5304
- if (kofs >= 0) {
5305
- if ((size_t)(kofs) >= na->shape[nd - 1]) {
5306
- rb_raise(
5307
- rb_eArgError,
5308
- "invalid diagonal offset(%" SZF "d) for "
5309
- "last dimension size(%" SZF "d)",
5310
- kofs, na->shape[nd - 1]
5311
- );
5312
- }
5313
- } else {
5314
- if ((size_t)(-kofs) >= na->shape[nd - 2]) {
5315
- rb_raise(
5316
- rb_eArgError,
5317
- "invalid diagonal offset(%" SZF "d) for "
5318
- "last-1 dimension size(%" SZF "d)",
5319
- kofs, na->shape[nd - 2]
5320
- );
5321
- }
5322
- }
5323
-
5324
- g = ALLOCA_N(char, sizeof(ssize_t) + sizeof(dtype));
5325
- *(ssize_t*)g = kofs;
5326
- *(dtype*)(g + sizeof(ssize_t)) = data;
5327
-
5328
- na_ndloop3(&ndf, g, 1, self);
5329
- return self;
5330
- }
5331
-
5332
- typedef struct {
5333
- dtype low;
5334
- dtype max;
5335
- } rand_opt_t;
5336
-
5337
- static void iter_sfloat_rand(na_loop_t* const lp) {
5338
- size_t i;
5339
- char* p1;
5340
- ssize_t s1;
5341
- size_t* idx1;
5342
- dtype x;
5343
- rand_opt_t* g;
5344
- dtype low;
5345
- dtype max;
5346
-
5347
- INIT_COUNTER(lp, i);
5348
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
5349
- g = (rand_opt_t*)(lp->opt_ptr);
5350
- low = g->low;
5351
- max = g->max;
5352
-
5353
- if (idx1) {
5354
- for (; i--;) {
5355
- x = m_add(m_rand(max), low);
5356
- SET_DATA_INDEX(p1, idx1, dtype, x);
5357
- }
5358
- } else {
5359
- for (; i--;) {
5360
- x = m_add(m_rand(max), low);
5361
- SET_DATA_STRIDE(p1, s1, dtype, x);
5362
- }
5363
- }
5364
- }
5365
-
5366
- static VALUE sfloat_rand(int argc, VALUE* args, VALUE self) {
5367
- rand_opt_t g;
5368
- VALUE v1 = Qnil, v2 = Qnil;
5369
- dtype high;
5370
- ndfunc_arg_in_t ain[1] = { { OVERWRITE, 0 } };
5371
- ndfunc_t ndf = { iter_sfloat_rand, FULL_LOOP, 1, 0, ain, 0 };
5372
-
5373
- rb_scan_args(argc, args, "02", &v1, &v2);
5374
- if (v2 == Qnil) {
5375
- g.low = m_zero;
5376
- if (v1 == Qnil) {
5377
-
5378
- g.max = high = m_one;
5379
-
5380
- } else {
5381
- g.max = high = m_num_to_data(v1);
5382
- }
5383
-
5384
- } else {
5385
- g.low = m_num_to_data(v1);
5386
- high = m_num_to_data(v2);
5387
- g.max = m_sub(high, g.low);
5388
- }
5389
-
5390
- na_ndloop3(&ndf, &g, 1, self);
5391
- return self;
5392
- }
5393
-
5394
4146
  typedef struct {
5395
4147
  dtype mu;
5396
4148
  rtype sigma;