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 double dfloat; // Type aliases for shorter notation
80
100
  // following the codebase naming convention.
101
+ DEF_NARRAY_CLIP_METHOD_FUNC(dfloat, numo_cDFloat)
102
+ DEF_NARRAY_FLT_SUM_METHOD_FUNC(dfloat, numo_cDFloat)
103
+ DEF_NARRAY_FLT_PROD_METHOD_FUNC(dfloat, numo_cDFloat)
81
104
  DEF_NARRAY_FLT_MEAN_METHOD_FUNC(dfloat, numo_cDFloat, double, numo_cDFloat)
82
105
  DEF_NARRAY_FLT_VAR_METHOD_FUNC(dfloat, numo_cDFloat, double, numo_cDFloat)
83
106
  DEF_NARRAY_FLT_STDDEV_METHOD_FUNC(dfloat, numo_cDFloat, double, numo_cDFloat)
84
107
  DEF_NARRAY_FLT_RMS_METHOD_FUNC(dfloat, numo_cDFloat, double, numo_cDFloat)
108
+ DEF_NARRAY_FLT_MIN_METHOD_FUNC(dfloat, numo_cDFloat)
109
+ DEF_NARRAY_FLT_MAX_METHOD_FUNC(dfloat, numo_cDFloat)
110
+ DEF_NARRAY_FLT_PTP_METHOD_FUNC(dfloat, numo_cDFloat)
111
+ DEF_NARRAY_FLT_MAX_INDEX_METHOD_FUNC(dfloat)
112
+ DEF_NARRAY_FLT_MIN_INDEX_METHOD_FUNC(dfloat)
113
+ DEF_NARRAY_FLT_ARGMAX_METHOD_FUNC(dfloat)
114
+ DEF_NARRAY_FLT_ARGMIN_METHOD_FUNC(dfloat)
115
+ DEF_NARRAY_FLT_MAXIMUM_METHOD_FUNC(dfloat, numo_cDFloat)
116
+ DEF_NARRAY_FLT_MINIMUM_METHOD_FUNC(dfloat, numo_cDFloat)
117
+ DEF_NARRAY_FLT_MINMAX_METHOD_FUNC(dfloat, numo_cDFloat)
118
+ DEF_NARRAY_FLT_CUMSUM_METHOD_FUNC(dfloat, numo_cDFloat)
119
+ DEF_NARRAY_FLT_CUMPROD_METHOD_FUNC(dfloat, numo_cDFloat)
120
+ DEF_NARRAY_FLT_MULSUM_METHOD_FUNC(dfloat, numo_cDFloat)
121
+ DEF_NARRAY_FLT_SEQ_METHOD_FUNC(dfloat)
122
+ DEF_NARRAY_FLT_LOGSEQ_METHOD_FUNC(dfloat)
123
+ DEF_NARRAY_EYE_METHOD_FUNC(dfloat)
124
+ DEF_NARRAY_FLT_RAND_METHOD_FUNC(dfloat)
85
125
  #ifdef __SSE2__
86
126
  DEF_NARRAY_FLT_SQRT_SSE2_DBL_METHOD_FUNC(dfloat, numo_cDFloat)
87
127
  #else
@@ -3916,93 +3956,6 @@ static VALUE dfloat_le(VALUE self, VALUE other) {
3916
3956
  }
3917
3957
  }
3918
3958
 
3919
- static void iter_dfloat_clip(na_loop_t* const lp) {
3920
- size_t i;
3921
- char *p1, *p2, *p3, *p4;
3922
- ssize_t s1, s2, s3, s4;
3923
- dtype x, min, max;
3924
- INIT_COUNTER(lp, i);
3925
- INIT_PTR(lp, 0, p1, s1);
3926
- INIT_PTR(lp, 1, p2, s2);
3927
- INIT_PTR(lp, 2, p3, s3);
3928
- INIT_PTR(lp, 3, p4, s4);
3929
- for (; i--;) {
3930
- GET_DATA_STRIDE(p1, s1, dtype, x);
3931
- GET_DATA_STRIDE(p2, s2, dtype, min);
3932
- GET_DATA_STRIDE(p3, s3, dtype, max);
3933
- if (m_gt(min, max)) {
3934
- rb_raise(nary_eOperationError, "min is greater than max");
3935
- }
3936
- if (m_lt(x, min)) {
3937
- x = min;
3938
- }
3939
- if (m_gt(x, max)) {
3940
- x = max;
3941
- }
3942
- SET_DATA_STRIDE(p4, s4, dtype, x);
3943
- }
3944
- }
3945
-
3946
- static void iter_dfloat_clip_min(na_loop_t* const lp) {
3947
- size_t i;
3948
- char *p1, *p2, *p3;
3949
- ssize_t s1, s2, s3;
3950
- dtype x, min;
3951
- INIT_COUNTER(lp, i);
3952
- INIT_PTR(lp, 0, p1, s1);
3953
- INIT_PTR(lp, 1, p2, s2);
3954
- INIT_PTR(lp, 2, p3, s3);
3955
- for (; i--;) {
3956
- GET_DATA_STRIDE(p1, s1, dtype, x);
3957
- GET_DATA_STRIDE(p2, s2, dtype, min);
3958
- if (m_lt(x, min)) {
3959
- x = min;
3960
- }
3961
- SET_DATA_STRIDE(p3, s3, dtype, x);
3962
- }
3963
- }
3964
-
3965
- static void iter_dfloat_clip_max(na_loop_t* const lp) {
3966
- size_t i;
3967
- char *p1, *p2, *p3;
3968
- ssize_t s1, s2, s3;
3969
- dtype x, max;
3970
- INIT_COUNTER(lp, i);
3971
- INIT_PTR(lp, 0, p1, s1);
3972
- INIT_PTR(lp, 1, p2, s2);
3973
- INIT_PTR(lp, 2, p3, s3);
3974
- for (; i--;) {
3975
- GET_DATA_STRIDE(p1, s1, dtype, x);
3976
- GET_DATA_STRIDE(p2, s2, dtype, max);
3977
- if (m_gt(x, max)) {
3978
- x = max;
3979
- }
3980
- SET_DATA_STRIDE(p3, s3, dtype, x);
3981
- }
3982
- }
3983
-
3984
- static VALUE dfloat_clip(VALUE self, VALUE min, VALUE max) {
3985
- ndfunc_arg_in_t ain[3] = { { Qnil, 0 }, { cT, 0 }, { cT, 0 } };
3986
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
3987
- ndfunc_t ndf_min = { iter_dfloat_clip_min, STRIDE_LOOP, 2, 1, ain, aout };
3988
- ndfunc_t ndf_max = { iter_dfloat_clip_max, STRIDE_LOOP, 2, 1, ain, aout };
3989
- ndfunc_t ndf_both = { iter_dfloat_clip, STRIDE_LOOP, 3, 1, ain, aout };
3990
-
3991
- if (RTEST(min)) {
3992
- if (RTEST(max)) {
3993
- return na_ndloop(&ndf_both, 3, self, min, max);
3994
- } else {
3995
- return na_ndloop(&ndf_min, 2, self, min);
3996
- }
3997
- } else {
3998
- if (RTEST(max)) {
3999
- return na_ndloop(&ndf_max, 2, self, max);
4000
- }
4001
- }
4002
- rb_raise(rb_eArgError, "min and max are not given");
4003
- return Qnil;
4004
- }
4005
-
4006
3959
  static void iter_dfloat_isnan(na_loop_t* const lp) {
4007
3960
  size_t i;
4008
3961
  char* p1;
@@ -4188,78 +4141,6 @@ static VALUE dfloat_isfinite(VALUE self) {
4188
4141
  return na_ndloop(&ndf, 1, self);
4189
4142
  }
4190
4143
 
4191
- static void iter_dfloat_sum(na_loop_t* const lp) {
4192
- size_t n;
4193
- char *p1, *p2;
4194
- ssize_t s1;
4195
-
4196
- INIT_COUNTER(lp, n);
4197
- INIT_PTR(lp, 0, p1, s1);
4198
- p2 = lp->args[1].ptr + lp->args[1].iter[0].pos;
4199
-
4200
- *(dtype*)p2 = f_sum(n, p1, s1);
4201
- }
4202
- static void iter_dfloat_sum_nan(na_loop_t* const lp) {
4203
- size_t n;
4204
- char *p1, *p2;
4205
- ssize_t s1;
4206
-
4207
- INIT_COUNTER(lp, n);
4208
- INIT_PTR(lp, 0, p1, s1);
4209
- p2 = lp->args[1].ptr + lp->args[1].iter[0].pos;
4210
-
4211
- *(dtype*)p2 = f_sum_nan(n, p1, s1);
4212
- }
4213
-
4214
- static VALUE dfloat_sum(int argc, VALUE* argv, VALUE self) {
4215
- VALUE v, reduce;
4216
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { sym_reduce, 0 } };
4217
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
4218
- ndfunc_t ndf = { iter_dfloat_sum, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE, 2, 1, ain, aout };
4219
-
4220
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, iter_dfloat_sum_nan);
4221
-
4222
- v = na_ndloop(&ndf, 2, self, reduce);
4223
-
4224
- return dfloat_extract(v);
4225
- }
4226
-
4227
- static void iter_dfloat_prod(na_loop_t* const lp) {
4228
- size_t n;
4229
- char *p1, *p2;
4230
- ssize_t s1;
4231
-
4232
- INIT_COUNTER(lp, n);
4233
- INIT_PTR(lp, 0, p1, s1);
4234
- p2 = lp->args[1].ptr + lp->args[1].iter[0].pos;
4235
-
4236
- *(dtype*)p2 = f_prod(n, p1, s1);
4237
- }
4238
- static void iter_dfloat_prod_nan(na_loop_t* const lp) {
4239
- size_t n;
4240
- char *p1, *p2;
4241
- ssize_t s1;
4242
-
4243
- INIT_COUNTER(lp, n);
4244
- INIT_PTR(lp, 0, p1, s1);
4245
- p2 = lp->args[1].ptr + lp->args[1].iter[0].pos;
4246
-
4247
- *(dtype*)p2 = f_prod_nan(n, p1, s1);
4248
- }
4249
-
4250
- static VALUE dfloat_prod(int argc, VALUE* argv, VALUE self) {
4251
- VALUE v, reduce;
4252
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { sym_reduce, 0 } };
4253
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
4254
- ndfunc_t ndf = { iter_dfloat_prod, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE, 2, 1, ain, aout };
4255
-
4256
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, iter_dfloat_prod_nan);
4257
-
4258
- v = na_ndloop(&ndf, 2, self, reduce);
4259
-
4260
- return dfloat_extract(v);
4261
- }
4262
-
4263
4144
  static void iter_dfloat_kahan_sum(na_loop_t* const lp) {
4264
4145
  size_t n;
4265
4146
  char *p1, *p2;
@@ -4296,1135 +4177,6 @@ static VALUE dfloat_kahan_sum(int argc, VALUE* argv, VALUE self) {
4296
4177
  return dfloat_extract(v);
4297
4178
  }
4298
4179
 
4299
- static void iter_dfloat_min(na_loop_t* const lp) {
4300
- size_t n;
4301
- char *p1, *p2;
4302
- ssize_t s1;
4303
-
4304
- INIT_COUNTER(lp, n);
4305
- INIT_PTR(lp, 0, p1, s1);
4306
- p2 = lp->args[1].ptr + lp->args[1].iter[0].pos;
4307
-
4308
- *(dtype*)p2 = f_min(n, p1, s1);
4309
- }
4310
- static void iter_dfloat_min_nan(na_loop_t* const lp) {
4311
- size_t n;
4312
- char *p1, *p2;
4313
- ssize_t s1;
4314
-
4315
- INIT_COUNTER(lp, n);
4316
- INIT_PTR(lp, 0, p1, s1);
4317
- p2 = lp->args[1].ptr + lp->args[1].iter[0].pos;
4318
-
4319
- *(dtype*)p2 = f_min_nan(n, p1, s1);
4320
- }
4321
-
4322
- static VALUE dfloat_min(int argc, VALUE* argv, VALUE self) {
4323
- VALUE v, reduce;
4324
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { sym_reduce, 0 } };
4325
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
4326
- ndfunc_t ndf = { iter_dfloat_min, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE, 2, 1, ain, aout };
4327
-
4328
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, iter_dfloat_min_nan);
4329
-
4330
- v = na_ndloop(&ndf, 2, self, reduce);
4331
-
4332
- return dfloat_extract(v);
4333
- }
4334
-
4335
- static void iter_dfloat_max(na_loop_t* const lp) {
4336
- size_t n;
4337
- char *p1, *p2;
4338
- ssize_t s1;
4339
-
4340
- INIT_COUNTER(lp, n);
4341
- INIT_PTR(lp, 0, p1, s1);
4342
- p2 = lp->args[1].ptr + lp->args[1].iter[0].pos;
4343
-
4344
- *(dtype*)p2 = f_max(n, p1, s1);
4345
- }
4346
- static void iter_dfloat_max_nan(na_loop_t* const lp) {
4347
- size_t n;
4348
- char *p1, *p2;
4349
- ssize_t s1;
4350
-
4351
- INIT_COUNTER(lp, n);
4352
- INIT_PTR(lp, 0, p1, s1);
4353
- p2 = lp->args[1].ptr + lp->args[1].iter[0].pos;
4354
-
4355
- *(dtype*)p2 = f_max_nan(n, p1, s1);
4356
- }
4357
-
4358
- static VALUE dfloat_max(int argc, VALUE* argv, VALUE self) {
4359
- VALUE v, reduce;
4360
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { sym_reduce, 0 } };
4361
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
4362
- ndfunc_t ndf = { iter_dfloat_max, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE, 2, 1, ain, aout };
4363
-
4364
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, iter_dfloat_max_nan);
4365
-
4366
- v = na_ndloop(&ndf, 2, self, reduce);
4367
-
4368
- return dfloat_extract(v);
4369
- }
4370
-
4371
- static void iter_dfloat_ptp(na_loop_t* const lp) {
4372
- size_t n;
4373
- char *p1, *p2;
4374
- ssize_t s1;
4375
-
4376
- INIT_COUNTER(lp, n);
4377
- INIT_PTR(lp, 0, p1, s1);
4378
- p2 = lp->args[1].ptr + lp->args[1].iter[0].pos;
4379
-
4380
- *(dtype*)p2 = f_ptp(n, p1, s1);
4381
- }
4382
- static void iter_dfloat_ptp_nan(na_loop_t* const lp) {
4383
- size_t n;
4384
- char *p1, *p2;
4385
- ssize_t s1;
4386
-
4387
- INIT_COUNTER(lp, n);
4388
- INIT_PTR(lp, 0, p1, s1);
4389
- p2 = lp->args[1].ptr + lp->args[1].iter[0].pos;
4390
-
4391
- *(dtype*)p2 = f_ptp_nan(n, p1, s1);
4392
- }
4393
-
4394
- static VALUE dfloat_ptp(int argc, VALUE* argv, VALUE self) {
4395
- VALUE v, reduce;
4396
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { sym_reduce, 0 } };
4397
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
4398
- ndfunc_t ndf = { iter_dfloat_ptp, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE, 2, 1, ain, aout };
4399
-
4400
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, iter_dfloat_ptp_nan);
4401
-
4402
- v = na_ndloop(&ndf, 2, self, reduce);
4403
-
4404
- return dfloat_extract(v);
4405
- }
4406
-
4407
- #define idx_t int64_t
4408
- static void iter_dfloat_max_index_index64(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(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_dfloat_max_index_index32(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(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
- #define idx_t int64_t
4442
- static void iter_dfloat_max_index_index64_nan(na_loop_t* const lp) {
4443
- size_t n, idx;
4444
- char *d_ptr, *i_ptr, *o_ptr;
4445
- ssize_t d_step, i_step;
4446
-
4447
- INIT_COUNTER(lp, n);
4448
- INIT_PTR(lp, 0, d_ptr, d_step);
4449
-
4450
- idx = f_max_index_nan(n, d_ptr, d_step);
4451
-
4452
- INIT_PTR(lp, 1, i_ptr, i_step);
4453
- o_ptr = NDL_PTR(lp, 2);
4454
- *(idx_t*)o_ptr = *(idx_t*)(i_ptr + i_step * idx);
4455
- }
4456
- #undef idx_t
4457
-
4458
- #define idx_t int32_t
4459
- static void iter_dfloat_max_index_index32_nan(na_loop_t* const lp) {
4460
- size_t n, idx;
4461
- char *d_ptr, *i_ptr, *o_ptr;
4462
- ssize_t d_step, i_step;
4463
-
4464
- INIT_COUNTER(lp, n);
4465
- INIT_PTR(lp, 0, d_ptr, d_step);
4466
-
4467
- idx = f_max_index_nan(n, d_ptr, d_step);
4468
-
4469
- INIT_PTR(lp, 1, i_ptr, i_step);
4470
- o_ptr = NDL_PTR(lp, 2);
4471
- *(idx_t*)o_ptr = *(idx_t*)(i_ptr + i_step * idx);
4472
- }
4473
- #undef idx_t
4474
-
4475
- static VALUE dfloat_max_index(int argc, VALUE* argv, VALUE self) {
4476
- narray_t* na;
4477
- VALUE idx, reduce;
4478
- ndfunc_arg_in_t ain[3] = { { Qnil, 0 }, { Qnil, 0 }, { sym_reduce, 0 } };
4479
- ndfunc_arg_out_t aout[1] = { { 0, 0, 0 } };
4480
- ndfunc_t ndf = { 0, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE | NDF_EXTRACT, 3, 1, ain, aout };
4481
-
4482
- GetNArray(self, na);
4483
- if (na->ndim == 0) {
4484
- return INT2FIX(0);
4485
- }
4486
- if (na->size > (~(u_int32_t)0)) {
4487
- aout[0].type = numo_cInt64;
4488
- idx = nary_new(numo_cInt64, na->ndim, na->shape);
4489
- ndf.func = iter_dfloat_max_index_index64;
4490
-
4491
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, iter_dfloat_max_index_index64_nan);
4492
-
4493
- } else {
4494
- aout[0].type = numo_cInt32;
4495
- idx = nary_new(numo_cInt32, na->ndim, na->shape);
4496
- ndf.func = iter_dfloat_max_index_index32;
4497
-
4498
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, iter_dfloat_max_index_index32_nan);
4499
- }
4500
- rb_funcall(idx, rb_intern("seq"), 0);
4501
-
4502
- return na_ndloop(&ndf, 3, self, idx, reduce);
4503
- }
4504
-
4505
- #define idx_t int64_t
4506
- static void iter_dfloat_min_index_index64(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(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_dfloat_min_index_index32(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(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
- #define idx_t int64_t
4540
- static void iter_dfloat_min_index_index64_nan(na_loop_t* const lp) {
4541
- size_t n, idx;
4542
- char *d_ptr, *i_ptr, *o_ptr;
4543
- ssize_t d_step, i_step;
4544
-
4545
- INIT_COUNTER(lp, n);
4546
- INIT_PTR(lp, 0, d_ptr, d_step);
4547
-
4548
- idx = f_min_index_nan(n, d_ptr, d_step);
4549
-
4550
- INIT_PTR(lp, 1, i_ptr, i_step);
4551
- o_ptr = NDL_PTR(lp, 2);
4552
- *(idx_t*)o_ptr = *(idx_t*)(i_ptr + i_step * idx);
4553
- }
4554
- #undef idx_t
4555
-
4556
- #define idx_t int32_t
4557
- static void iter_dfloat_min_index_index32_nan(na_loop_t* const lp) {
4558
- size_t n, idx;
4559
- char *d_ptr, *i_ptr, *o_ptr;
4560
- ssize_t d_step, i_step;
4561
-
4562
- INIT_COUNTER(lp, n);
4563
- INIT_PTR(lp, 0, d_ptr, d_step);
4564
-
4565
- idx = f_min_index_nan(n, d_ptr, d_step);
4566
-
4567
- INIT_PTR(lp, 1, i_ptr, i_step);
4568
- o_ptr = NDL_PTR(lp, 2);
4569
- *(idx_t*)o_ptr = *(idx_t*)(i_ptr + i_step * idx);
4570
- }
4571
- #undef idx_t
4572
-
4573
- static VALUE dfloat_min_index(int argc, VALUE* argv, VALUE self) {
4574
- narray_t* na;
4575
- VALUE idx, reduce;
4576
- ndfunc_arg_in_t ain[3] = { { Qnil, 0 }, { Qnil, 0 }, { sym_reduce, 0 } };
4577
- ndfunc_arg_out_t aout[1] = { { 0, 0, 0 } };
4578
- ndfunc_t ndf = { 0, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE | NDF_EXTRACT, 3, 1, ain, aout };
4579
-
4580
- GetNArray(self, na);
4581
- if (na->ndim == 0) {
4582
- return INT2FIX(0);
4583
- }
4584
- if (na->size > (~(u_int32_t)0)) {
4585
- aout[0].type = numo_cInt64;
4586
- idx = nary_new(numo_cInt64, na->ndim, na->shape);
4587
- ndf.func = iter_dfloat_min_index_index64;
4588
-
4589
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, iter_dfloat_min_index_index64_nan);
4590
-
4591
- } else {
4592
- aout[0].type = numo_cInt32;
4593
- idx = nary_new(numo_cInt32, na->ndim, na->shape);
4594
- ndf.func = iter_dfloat_min_index_index32;
4595
-
4596
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, iter_dfloat_min_index_index32_nan);
4597
- }
4598
- rb_funcall(idx, rb_intern("seq"), 0);
4599
-
4600
- return na_ndloop(&ndf, 3, self, idx, reduce);
4601
- }
4602
-
4603
- #define idx_t int64_t
4604
- static void iter_dfloat_argmax_arg64(na_loop_t* const lp) {
4605
- size_t n, idx;
4606
- char *d_ptr, *o_ptr;
4607
- ssize_t d_step;
4608
-
4609
- INIT_COUNTER(lp, n);
4610
- INIT_PTR(lp, 0, d_ptr, d_step);
4611
-
4612
- idx = f_max_index(n, d_ptr, d_step);
4613
-
4614
- o_ptr = NDL_PTR(lp, 1);
4615
- *(idx_t*)o_ptr = (idx_t)idx;
4616
- }
4617
- #undef idx_t
4618
-
4619
- #define idx_t int32_t
4620
- static void iter_dfloat_argmax_arg32(na_loop_t* const lp) {
4621
- size_t n, idx;
4622
- char *d_ptr, *o_ptr;
4623
- ssize_t d_step;
4624
-
4625
- INIT_COUNTER(lp, n);
4626
- INIT_PTR(lp, 0, d_ptr, d_step);
4627
-
4628
- idx = f_max_index(n, d_ptr, d_step);
4629
-
4630
- o_ptr = NDL_PTR(lp, 1);
4631
- *(idx_t*)o_ptr = (idx_t)idx;
4632
- }
4633
- #undef idx_t
4634
-
4635
- #define idx_t int64_t
4636
- static void iter_dfloat_argmax_arg64_nan(na_loop_t* const lp) {
4637
- size_t n, idx;
4638
- char *d_ptr, *o_ptr;
4639
- ssize_t d_step;
4640
-
4641
- INIT_COUNTER(lp, n);
4642
- INIT_PTR(lp, 0, d_ptr, d_step);
4643
-
4644
- idx = f_max_index_nan(n, d_ptr, d_step);
4645
-
4646
- o_ptr = NDL_PTR(lp, 1);
4647
- *(idx_t*)o_ptr = (idx_t)idx;
4648
- }
4649
- #undef idx_t
4650
-
4651
- #define idx_t int32_t
4652
- static void iter_dfloat_argmax_arg32_nan(na_loop_t* const lp) {
4653
- size_t n, idx;
4654
- char *d_ptr, *o_ptr;
4655
- ssize_t d_step;
4656
-
4657
- INIT_COUNTER(lp, n);
4658
- INIT_PTR(lp, 0, d_ptr, d_step);
4659
-
4660
- idx = f_max_index_nan(n, d_ptr, d_step);
4661
-
4662
- o_ptr = NDL_PTR(lp, 1);
4663
- *(idx_t*)o_ptr = (idx_t)idx;
4664
- }
4665
- #undef idx_t
4666
-
4667
- static VALUE dfloat_argmax(int argc, VALUE* argv, VALUE self) {
4668
- narray_t* na;
4669
- VALUE reduce;
4670
- ndfunc_arg_in_t ain[2] = { { Qnil, 0 }, { sym_reduce, 0 } };
4671
- ndfunc_arg_out_t aout[1] = { { 0, 0, 0 } };
4672
- ndfunc_t ndf = { 0, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE | NDF_EXTRACT, 2, 1, ain, aout };
4673
-
4674
- GetNArray(self, na);
4675
- if (na->ndim == 0) {
4676
- return INT2FIX(0);
4677
- }
4678
- if (na->size > (~(u_int32_t)0)) {
4679
- aout[0].type = numo_cInt64;
4680
- ndf.func = iter_dfloat_argmax_arg64;
4681
-
4682
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, iter_dfloat_argmax_arg64_nan);
4683
-
4684
- } else {
4685
- aout[0].type = numo_cInt32;
4686
- ndf.func = iter_dfloat_argmax_arg32;
4687
-
4688
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, iter_dfloat_argmax_arg32_nan);
4689
- }
4690
-
4691
- return na_ndloop(&ndf, 2, self, reduce);
4692
- }
4693
-
4694
- #define idx_t int64_t
4695
- static void iter_dfloat_argmin_arg64(na_loop_t* const lp) {
4696
- size_t n, idx;
4697
- char *d_ptr, *o_ptr;
4698
- ssize_t d_step;
4699
-
4700
- INIT_COUNTER(lp, n);
4701
- INIT_PTR(lp, 0, d_ptr, d_step);
4702
-
4703
- idx = f_min_index(n, d_ptr, d_step);
4704
-
4705
- o_ptr = NDL_PTR(lp, 1);
4706
- *(idx_t*)o_ptr = (idx_t)idx;
4707
- }
4708
- #undef idx_t
4709
-
4710
- #define idx_t int32_t
4711
- static void iter_dfloat_argmin_arg32(na_loop_t* const lp) {
4712
- size_t n, idx;
4713
- char *d_ptr, *o_ptr;
4714
- ssize_t d_step;
4715
-
4716
- INIT_COUNTER(lp, n);
4717
- INIT_PTR(lp, 0, d_ptr, d_step);
4718
-
4719
- idx = f_min_index(n, d_ptr, d_step);
4720
-
4721
- o_ptr = NDL_PTR(lp, 1);
4722
- *(idx_t*)o_ptr = (idx_t)idx;
4723
- }
4724
- #undef idx_t
4725
-
4726
- #define idx_t int64_t
4727
- static void iter_dfloat_argmin_arg64_nan(na_loop_t* const lp) {
4728
- size_t n, idx;
4729
- char *d_ptr, *o_ptr;
4730
- ssize_t d_step;
4731
-
4732
- INIT_COUNTER(lp, n);
4733
- INIT_PTR(lp, 0, d_ptr, d_step);
4734
-
4735
- idx = f_min_index_nan(n, d_ptr, d_step);
4736
-
4737
- o_ptr = NDL_PTR(lp, 1);
4738
- *(idx_t*)o_ptr = (idx_t)idx;
4739
- }
4740
- #undef idx_t
4741
-
4742
- #define idx_t int32_t
4743
- static void iter_dfloat_argmin_arg32_nan(na_loop_t* const lp) {
4744
- size_t n, idx;
4745
- char *d_ptr, *o_ptr;
4746
- ssize_t d_step;
4747
-
4748
- INIT_COUNTER(lp, n);
4749
- INIT_PTR(lp, 0, d_ptr, d_step);
4750
-
4751
- idx = f_min_index_nan(n, d_ptr, d_step);
4752
-
4753
- o_ptr = NDL_PTR(lp, 1);
4754
- *(idx_t*)o_ptr = (idx_t)idx;
4755
- }
4756
- #undef idx_t
4757
-
4758
- static VALUE dfloat_argmin(int argc, VALUE* argv, VALUE self) {
4759
- narray_t* na;
4760
- VALUE reduce;
4761
- ndfunc_arg_in_t ain[2] = { { Qnil, 0 }, { sym_reduce, 0 } };
4762
- ndfunc_arg_out_t aout[1] = { { 0, 0, 0 } };
4763
- ndfunc_t ndf = { 0, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE | NDF_EXTRACT, 2, 1, ain, aout };
4764
-
4765
- GetNArray(self, na);
4766
- if (na->ndim == 0) {
4767
- return INT2FIX(0);
4768
- }
4769
- if (na->size > (~(u_int32_t)0)) {
4770
- aout[0].type = numo_cInt64;
4771
- ndf.func = iter_dfloat_argmin_arg64;
4772
-
4773
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, iter_dfloat_argmin_arg64_nan);
4774
-
4775
- } else {
4776
- aout[0].type = numo_cInt32;
4777
- ndf.func = iter_dfloat_argmin_arg32;
4778
-
4779
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, iter_dfloat_argmin_arg32_nan);
4780
- }
4781
-
4782
- return na_ndloop(&ndf, 2, self, reduce);
4783
- }
4784
-
4785
- static void iter_dfloat_minmax(na_loop_t* const lp) {
4786
- size_t n;
4787
- char* p1;
4788
- ssize_t s1;
4789
- dtype xmin, xmax;
4790
-
4791
- INIT_COUNTER(lp, n);
4792
- INIT_PTR(lp, 0, p1, s1);
4793
-
4794
- f_minmax(n, p1, s1, &xmin, &xmax);
4795
-
4796
- *(dtype*)(lp->args[1].ptr + lp->args[1].iter[0].pos) = xmin;
4797
- *(dtype*)(lp->args[2].ptr + lp->args[2].iter[0].pos) = xmax;
4798
- }
4799
- static void iter_dfloat_minmax_nan(na_loop_t* const lp) {
4800
- size_t n;
4801
- char* p1;
4802
- ssize_t s1;
4803
- dtype xmin, xmax;
4804
-
4805
- INIT_COUNTER(lp, n);
4806
- INIT_PTR(lp, 0, p1, s1);
4807
-
4808
- f_minmax_nan(n, p1, s1, &xmin, &xmax);
4809
-
4810
- *(dtype*)(lp->args[1].ptr + lp->args[1].iter[0].pos) = xmin;
4811
- *(dtype*)(lp->args[2].ptr + lp->args[2].iter[0].pos) = xmax;
4812
- }
4813
-
4814
- static VALUE dfloat_minmax(int argc, VALUE* argv, VALUE self) {
4815
- VALUE reduce;
4816
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { sym_reduce, 0 } };
4817
- ndfunc_arg_out_t aout[2] = { { cT, 0 }, { cT, 0 } };
4818
- ndfunc_t ndf = {
4819
- iter_dfloat_minmax, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE | NDF_EXTRACT, 2, 2, ain, aout
4820
- };
4821
-
4822
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, iter_dfloat_minmax_nan);
4823
-
4824
- return na_ndloop(&ndf, 2, self, reduce);
4825
- }
4826
-
4827
- static void iter_dfloat_s_maximum(na_loop_t* const lp) {
4828
- size_t i, n;
4829
- char *p1, *p2, *p3;
4830
- ssize_t s1, s2, s3;
4831
-
4832
- INIT_COUNTER(lp, n);
4833
- INIT_PTR(lp, 0, p1, s1);
4834
- INIT_PTR(lp, 1, p2, s2);
4835
- INIT_PTR(lp, 2, p3, s3);
4836
-
4837
- for (i = 0; i < n; i++) {
4838
- dtype x, y, z;
4839
- GET_DATA_STRIDE(p1, s1, dtype, x);
4840
- GET_DATA_STRIDE(p2, s2, dtype, y);
4841
- GET_DATA(p3, dtype, z);
4842
- z = f_maximum(x, y);
4843
- SET_DATA_STRIDE(p3, s3, dtype, z);
4844
- }
4845
- }
4846
- static void iter_dfloat_s_maximum_nan(na_loop_t* const lp) {
4847
- size_t i, n;
4848
- char *p1, *p2, *p3;
4849
- ssize_t s1, s2, s3;
4850
-
4851
- INIT_COUNTER(lp, n);
4852
- INIT_PTR(lp, 0, p1, s1);
4853
- INIT_PTR(lp, 1, p2, s2);
4854
- INIT_PTR(lp, 2, p3, s3);
4855
-
4856
- for (i = 0; i < n; i++) {
4857
- dtype x, y, z;
4858
- GET_DATA_STRIDE(p1, s1, dtype, x);
4859
- GET_DATA_STRIDE(p2, s2, dtype, y);
4860
- GET_DATA(p3, dtype, z);
4861
- z = f_maximum_nan(x, y);
4862
- SET_DATA_STRIDE(p3, s3, dtype, z);
4863
- }
4864
- }
4865
-
4866
- static VALUE dfloat_s_maximum(int argc, VALUE* argv, VALUE mod) {
4867
- VALUE a1 = Qnil;
4868
- VALUE a2 = Qnil;
4869
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
4870
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
4871
- ndfunc_t ndf = { iter_dfloat_s_maximum, STRIDE_LOOP_NIP, 2, 1, ain, aout };
4872
-
4873
- VALUE kw_hash = Qnil;
4874
- ID kw_table[1] = { id_nan };
4875
- VALUE opts[1] = { Qundef };
4876
-
4877
- rb_scan_args(argc, argv, "20:", &a1, &a2, &kw_hash);
4878
- rb_get_kwargs(kw_hash, kw_table, 0, 1, opts);
4879
- if (opts[0] != Qundef) {
4880
- ndf.func = iter_dfloat_s_maximum_nan;
4881
- }
4882
-
4883
- return na_ndloop(&ndf, 2, a1, a2);
4884
- }
4885
-
4886
- static void iter_dfloat_s_minimum(na_loop_t* const lp) {
4887
- size_t i, n;
4888
- char *p1, *p2, *p3;
4889
- ssize_t s1, s2, s3;
4890
-
4891
- INIT_COUNTER(lp, n);
4892
- INIT_PTR(lp, 0, p1, s1);
4893
- INIT_PTR(lp, 1, p2, s2);
4894
- INIT_PTR(lp, 2, p3, s3);
4895
-
4896
- for (i = 0; i < n; i++) {
4897
- dtype x, y, z;
4898
- GET_DATA_STRIDE(p1, s1, dtype, x);
4899
- GET_DATA_STRIDE(p2, s2, dtype, y);
4900
- GET_DATA(p3, dtype, z);
4901
- z = f_minimum(x, y);
4902
- SET_DATA_STRIDE(p3, s3, dtype, z);
4903
- }
4904
- }
4905
- static void iter_dfloat_s_minimum_nan(na_loop_t* const lp) {
4906
- size_t i, n;
4907
- char *p1, *p2, *p3;
4908
- ssize_t s1, s2, s3;
4909
-
4910
- INIT_COUNTER(lp, n);
4911
- INIT_PTR(lp, 0, p1, s1);
4912
- INIT_PTR(lp, 1, p2, s2);
4913
- INIT_PTR(lp, 2, p3, s3);
4914
-
4915
- for (i = 0; i < n; i++) {
4916
- dtype x, y, z;
4917
- GET_DATA_STRIDE(p1, s1, dtype, x);
4918
- GET_DATA_STRIDE(p2, s2, dtype, y);
4919
- GET_DATA(p3, dtype, z);
4920
- z = f_minimum_nan(x, y);
4921
- SET_DATA_STRIDE(p3, s3, dtype, z);
4922
- }
4923
- }
4924
-
4925
- static VALUE dfloat_s_minimum(int argc, VALUE* argv, VALUE mod) {
4926
- VALUE a1 = Qnil;
4927
- VALUE a2 = Qnil;
4928
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
4929
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
4930
- ndfunc_t ndf = { iter_dfloat_s_minimum, STRIDE_LOOP_NIP, 2, 1, ain, aout };
4931
-
4932
- VALUE kw_hash = Qnil;
4933
- ID kw_table[1] = { id_nan };
4934
- VALUE opts[1] = { Qundef };
4935
-
4936
- rb_scan_args(argc, argv, "20:", &a1, &a2, &kw_hash);
4937
- rb_get_kwargs(kw_hash, kw_table, 0, 1, opts);
4938
- if (opts[0] != Qundef) {
4939
- ndf.func = iter_dfloat_s_minimum_nan;
4940
- }
4941
-
4942
- return na_ndloop(&ndf, 2, a1, a2);
4943
- }
4944
-
4945
- static void iter_dfloat_cumsum(na_loop_t* const lp) {
4946
- size_t i;
4947
- char *p1, *p2;
4948
- ssize_t s1, s2;
4949
- dtype x, y;
4950
-
4951
- INIT_COUNTER(lp, i);
4952
- INIT_PTR(lp, 0, p1, s1);
4953
- INIT_PTR(lp, 1, p2, s2);
4954
-
4955
- GET_DATA_STRIDE(p1, s1, dtype, x);
4956
- SET_DATA_STRIDE(p2, s2, dtype, x);
4957
- for (i--; i--;) {
4958
- GET_DATA_STRIDE(p1, s1, dtype, y);
4959
- m_cumsum(x, y);
4960
- SET_DATA_STRIDE(p2, s2, dtype, x);
4961
- }
4962
- }
4963
- static void iter_dfloat_cumsum_nan(na_loop_t* const lp) {
4964
- size_t i;
4965
- char *p1, *p2;
4966
- ssize_t s1, s2;
4967
- dtype x, y;
4968
-
4969
- INIT_COUNTER(lp, i);
4970
- INIT_PTR(lp, 0, p1, s1);
4971
- INIT_PTR(lp, 1, p2, s2);
4972
-
4973
- GET_DATA_STRIDE(p1, s1, dtype, x);
4974
- SET_DATA_STRIDE(p2, s2, dtype, x);
4975
- for (i--; i--;) {
4976
- GET_DATA_STRIDE(p1, s1, dtype, y);
4977
- m_cumsum_nan(x, y);
4978
- SET_DATA_STRIDE(p2, s2, dtype, x);
4979
- }
4980
- }
4981
-
4982
- static VALUE dfloat_cumsum(int argc, VALUE* argv, VALUE self) {
4983
- VALUE reduce;
4984
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { sym_reduce, 0 } };
4985
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
4986
- ndfunc_t ndf = {
4987
- iter_dfloat_cumsum, STRIDE_LOOP | NDF_FLAT_REDUCE | NDF_CUM, 2, 1, ain, aout
4988
- };
4989
-
4990
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, iter_dfloat_cumsum_nan);
4991
-
4992
- return na_ndloop(&ndf, 2, self, reduce);
4993
- }
4994
-
4995
- static void iter_dfloat_cumprod(na_loop_t* const lp) {
4996
- size_t i;
4997
- char *p1, *p2;
4998
- ssize_t s1, s2;
4999
- dtype x, y;
5000
-
5001
- INIT_COUNTER(lp, i);
5002
- INIT_PTR(lp, 0, p1, s1);
5003
- INIT_PTR(lp, 1, p2, s2);
5004
-
5005
- GET_DATA_STRIDE(p1, s1, dtype, x);
5006
- SET_DATA_STRIDE(p2, s2, dtype, x);
5007
- for (i--; i--;) {
5008
- GET_DATA_STRIDE(p1, s1, dtype, y);
5009
- m_cumprod(x, y);
5010
- SET_DATA_STRIDE(p2, s2, dtype, x);
5011
- }
5012
- }
5013
- static void iter_dfloat_cumprod_nan(na_loop_t* const lp) {
5014
- size_t i;
5015
- char *p1, *p2;
5016
- ssize_t s1, s2;
5017
- dtype x, y;
5018
-
5019
- INIT_COUNTER(lp, i);
5020
- INIT_PTR(lp, 0, p1, s1);
5021
- INIT_PTR(lp, 1, p2, s2);
5022
-
5023
- GET_DATA_STRIDE(p1, s1, dtype, x);
5024
- SET_DATA_STRIDE(p2, s2, dtype, x);
5025
- for (i--; i--;) {
5026
- GET_DATA_STRIDE(p1, s1, dtype, y);
5027
- m_cumprod_nan(x, y);
5028
- SET_DATA_STRIDE(p2, s2, dtype, x);
5029
- }
5030
- }
5031
-
5032
- static VALUE dfloat_cumprod(int argc, VALUE* argv, VALUE self) {
5033
- VALUE reduce;
5034
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { sym_reduce, 0 } };
5035
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
5036
- ndfunc_t ndf = {
5037
- iter_dfloat_cumprod, STRIDE_LOOP | NDF_FLAT_REDUCE | NDF_CUM, 2, 1, ain, aout
5038
- };
5039
-
5040
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, iter_dfloat_cumprod_nan);
5041
-
5042
- return na_ndloop(&ndf, 2, self, reduce);
5043
- }
5044
-
5045
- //
5046
- static void iter_dfloat_mulsum(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(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(x, y, z);
5075
- SET_DATA_STRIDE(p3, s3, dtype, z);
5076
- }
5077
- }
5078
- }
5079
- //
5080
- static void iter_dfloat_mulsum_nan(na_loop_t* const lp) {
5081
- size_t i, n;
5082
- char *p1, *p2, *p3;
5083
- ssize_t s1, s2, s3;
5084
-
5085
- INIT_COUNTER(lp, n);
5086
- INIT_PTR(lp, 0, p1, s1);
5087
- INIT_PTR(lp, 1, p2, s2);
5088
- INIT_PTR(lp, 2, p3, s3);
5089
-
5090
- if (s3 == 0) {
5091
- dtype z;
5092
- // Reduce loop
5093
- GET_DATA(p3, dtype, z);
5094
- for (i = 0; i < n; i++) {
5095
- dtype x, y;
5096
- GET_DATA_STRIDE(p1, s1, dtype, x);
5097
- GET_DATA_STRIDE(p2, s2, dtype, y);
5098
- m_mulsum_nan(x, y, z);
5099
- }
5100
- SET_DATA(p3, dtype, z);
5101
- return;
5102
- } else {
5103
- for (i = 0; i < n; i++) {
5104
- dtype x, y, z;
5105
- GET_DATA_STRIDE(p1, s1, dtype, x);
5106
- GET_DATA_STRIDE(p2, s2, dtype, y);
5107
- GET_DATA(p3, dtype, z);
5108
- m_mulsum_nan(x, y, z);
5109
- SET_DATA_STRIDE(p3, s3, dtype, z);
5110
- }
5111
- }
5112
- }
5113
- //
5114
-
5115
- static VALUE dfloat_mulsum_self(int argc, VALUE* argv, VALUE self) {
5116
- VALUE v, reduce;
5117
- VALUE naryv[2];
5118
- ndfunc_arg_in_t ain[4] = { { cT, 0 }, { cT, 0 }, { sym_reduce, 0 }, { sym_init, 0 } };
5119
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
5120
- ndfunc_t ndf = { iter_dfloat_mulsum, STRIDE_LOOP_NIP, 4, 1, ain, aout };
5121
-
5122
- if (argc < 1) {
5123
- rb_raise(rb_eArgError, "wrong number of arguments (%d for >=1)", argc);
5124
- }
5125
- // should fix below: [self.ndim,other.ndim].max or?
5126
- naryv[0] = self;
5127
- naryv[1] = argv[0];
5128
- //
5129
- reduce = na_reduce_dimension(argc - 1, argv + 1, 2, naryv, &ndf, iter_dfloat_mulsum_nan);
5130
- //
5131
-
5132
- v = na_ndloop(&ndf, 4, self, argv[0], reduce, m_mulsum_init);
5133
- return dfloat_extract(v);
5134
- }
5135
-
5136
- static VALUE dfloat_mulsum(int argc, VALUE* argv, VALUE self) {
5137
- //
5138
- VALUE klass, v;
5139
- //
5140
- if (argc < 1) {
5141
- rb_raise(rb_eArgError, "wrong number of arguments (%d for >=1)", argc);
5142
- }
5143
- //
5144
- klass = na_upcast(rb_obj_class(self), rb_obj_class(argv[0]));
5145
- if (klass == cT) {
5146
- return dfloat_mulsum_self(argc, argv, self);
5147
- } else {
5148
- v = rb_funcall(klass, id_cast, 1, self);
5149
- //
5150
- return rb_funcallv_kw(v, rb_intern("mulsum"), argc, argv, RB_PASS_CALLED_KEYWORDS);
5151
- //
5152
- }
5153
- //
5154
- }
5155
-
5156
- typedef dtype seq_data_t;
5157
-
5158
- typedef double seq_count_t;
5159
-
5160
- typedef struct {
5161
- seq_data_t beg;
5162
- seq_data_t step;
5163
- seq_count_t count;
5164
- } seq_opt_t;
5165
-
5166
- static void iter_dfloat_seq(na_loop_t* const lp) {
5167
- size_t i;
5168
- char* p1;
5169
- ssize_t s1;
5170
- size_t* idx1;
5171
- dtype x;
5172
- seq_data_t beg, step;
5173
- seq_count_t c;
5174
- seq_opt_t* g;
5175
-
5176
- INIT_COUNTER(lp, i);
5177
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
5178
- g = (seq_opt_t*)(lp->opt_ptr);
5179
- beg = g->beg;
5180
- step = g->step;
5181
- c = g->count;
5182
- if (idx1) {
5183
- for (; i--;) {
5184
- x = f_seq(beg, step, c++);
5185
- *(dtype*)(p1 + *idx1) = x;
5186
- idx1++;
5187
- }
5188
- } else {
5189
- for (; i--;) {
5190
- x = f_seq(beg, step, c++);
5191
- *(dtype*)(p1) = x;
5192
- p1 += s1;
5193
- }
5194
- }
5195
- g->count = c;
5196
- }
5197
-
5198
- static VALUE dfloat_seq(int argc, VALUE* args, VALUE self) {
5199
- seq_opt_t* g;
5200
- VALUE vbeg = Qnil, vstep = Qnil;
5201
- ndfunc_arg_in_t ain[1] = { { OVERWRITE, 0 } };
5202
- ndfunc_t ndf = { iter_dfloat_seq, FULL_LOOP, 1, 0, ain, 0 };
5203
-
5204
- g = ALLOCA_N(seq_opt_t, 1);
5205
- g->beg = m_zero;
5206
- g->step = m_one;
5207
- g->count = 0;
5208
- rb_scan_args(argc, args, "02", &vbeg, &vstep);
5209
- if (vbeg != Qnil) {
5210
- g->beg = m_num_to_data(vbeg);
5211
- }
5212
- if (vstep != Qnil) {
5213
- g->step = m_num_to_data(vstep);
5214
- }
5215
-
5216
- na_ndloop3(&ndf, g, 1, self);
5217
- return self;
5218
- }
5219
-
5220
- typedef struct {
5221
- seq_data_t beg;
5222
- seq_data_t step;
5223
- seq_data_t base;
5224
- seq_count_t count;
5225
- } logseq_opt_t;
5226
-
5227
- static void iter_dfloat_logseq(na_loop_t* const lp) {
5228
- size_t i;
5229
- char* p1;
5230
- ssize_t s1;
5231
- size_t* idx1;
5232
- dtype x;
5233
- seq_data_t beg, step, base;
5234
- seq_count_t c;
5235
- logseq_opt_t* g;
5236
-
5237
- INIT_COUNTER(lp, i);
5238
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
5239
- g = (logseq_opt_t*)(lp->opt_ptr);
5240
- beg = g->beg;
5241
- step = g->step;
5242
- base = g->base;
5243
- c = g->count;
5244
- if (idx1) {
5245
- for (; i--;) {
5246
- x = f_seq(beg, step, c++);
5247
- *(dtype*)(p1 + *idx1) = m_pow(base, x);
5248
- idx1++;
5249
- }
5250
- } else {
5251
- for (; i--;) {
5252
- x = f_seq(beg, step, c++);
5253
- *(dtype*)(p1) = m_pow(base, x);
5254
- p1 += s1;
5255
- }
5256
- }
5257
- g->count = c;
5258
- }
5259
-
5260
- static VALUE dfloat_logseq(int argc, VALUE* args, VALUE self) {
5261
- logseq_opt_t* g;
5262
- VALUE vbeg, vstep, vbase;
5263
- ndfunc_arg_in_t ain[1] = { { OVERWRITE, 0 } };
5264
- ndfunc_t ndf = { iter_dfloat_logseq, FULL_LOOP, 1, 0, ain, 0 };
5265
-
5266
- g = ALLOCA_N(logseq_opt_t, 1);
5267
- rb_scan_args(argc, args, "21", &vbeg, &vstep, &vbase);
5268
- g->beg = m_num_to_data(vbeg);
5269
- g->step = m_num_to_data(vstep);
5270
- if (vbase == Qnil) {
5271
- g->base = m_from_real(10);
5272
- } else {
5273
- g->base = m_num_to_data(vbase);
5274
- }
5275
- na_ndloop3(&ndf, g, 1, self);
5276
- return self;
5277
- }
5278
-
5279
- static void iter_dfloat_eye(na_loop_t* const lp) {
5280
- size_t n0, n1;
5281
- size_t i0, i1;
5282
- ssize_t s0, s1;
5283
- char *p0, *p1;
5284
- char* g;
5285
- ssize_t kofs;
5286
- dtype data;
5287
-
5288
- g = (char*)(lp->opt_ptr);
5289
- kofs = *(ssize_t*)g;
5290
- data = *(dtype*)(g + sizeof(ssize_t));
5291
-
5292
- n0 = lp->args[0].shape[0];
5293
- n1 = lp->args[0].shape[1];
5294
- s0 = lp->args[0].iter[0].step;
5295
- s1 = lp->args[0].iter[1].step;
5296
- p0 = NDL_PTR(lp, 0);
5297
-
5298
- for (i0 = 0; i0 < n0; i0++) {
5299
- p1 = p0;
5300
- for (i1 = 0; i1 < n1; i1++) {
5301
- *(dtype*)p1 = (i0 + kofs == i1) ? data : m_zero;
5302
- p1 += s1;
5303
- }
5304
- p0 += s0;
5305
- }
5306
- }
5307
-
5308
- static VALUE dfloat_eye(int argc, VALUE* argv, VALUE self) {
5309
- ndfunc_arg_in_t ain[1] = { { OVERWRITE, 2 } };
5310
- ndfunc_t ndf = { iter_dfloat_eye, NO_LOOP, 1, 0, ain, 0 };
5311
- ssize_t kofs;
5312
- dtype data;
5313
- char* g;
5314
- int nd;
5315
- narray_t* na;
5316
-
5317
- // check arguments
5318
- if (argc > 2) {
5319
- rb_raise(rb_eArgError, "too many arguments (%d for 0..2)", argc);
5320
- } else if (argc == 2) {
5321
- data = m_num_to_data(argv[0]);
5322
- kofs = NUM2SSIZET(argv[1]);
5323
- } else if (argc == 1) {
5324
- data = m_num_to_data(argv[0]);
5325
- kofs = 0;
5326
- } else {
5327
- data = m_one;
5328
- kofs = 0;
5329
- }
5330
-
5331
- GetNArray(self, na);
5332
- nd = na->ndim;
5333
- if (nd < 2) {
5334
- rb_raise(nary_eDimensionError, "less than 2-d array");
5335
- }
5336
-
5337
- // Diagonal offset from the main diagonal.
5338
- if (kofs >= 0) {
5339
- if ((size_t)(kofs) >= na->shape[nd - 1]) {
5340
- rb_raise(
5341
- rb_eArgError,
5342
- "invalid diagonal offset(%" SZF "d) for "
5343
- "last dimension size(%" SZF "d)",
5344
- kofs, na->shape[nd - 1]
5345
- );
5346
- }
5347
- } else {
5348
- if ((size_t)(-kofs) >= na->shape[nd - 2]) {
5349
- rb_raise(
5350
- rb_eArgError,
5351
- "invalid diagonal offset(%" SZF "d) for "
5352
- "last-1 dimension size(%" SZF "d)",
5353
- kofs, na->shape[nd - 2]
5354
- );
5355
- }
5356
- }
5357
-
5358
- g = ALLOCA_N(char, sizeof(ssize_t) + sizeof(dtype));
5359
- *(ssize_t*)g = kofs;
5360
- *(dtype*)(g + sizeof(ssize_t)) = data;
5361
-
5362
- na_ndloop3(&ndf, g, 1, self);
5363
- return self;
5364
- }
5365
-
5366
- typedef struct {
5367
- dtype low;
5368
- dtype max;
5369
- } rand_opt_t;
5370
-
5371
- static void iter_dfloat_rand(na_loop_t* const lp) {
5372
- size_t i;
5373
- char* p1;
5374
- ssize_t s1;
5375
- size_t* idx1;
5376
- dtype x;
5377
- rand_opt_t* g;
5378
- dtype low;
5379
- dtype max;
5380
-
5381
- INIT_COUNTER(lp, i);
5382
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
5383
- g = (rand_opt_t*)(lp->opt_ptr);
5384
- low = g->low;
5385
- max = g->max;
5386
-
5387
- if (idx1) {
5388
- for (; i--;) {
5389
- x = m_add(m_rand(max), low);
5390
- SET_DATA_INDEX(p1, idx1, dtype, x);
5391
- }
5392
- } else {
5393
- for (; i--;) {
5394
- x = m_add(m_rand(max), low);
5395
- SET_DATA_STRIDE(p1, s1, dtype, x);
5396
- }
5397
- }
5398
- }
5399
-
5400
- static VALUE dfloat_rand(int argc, VALUE* args, VALUE self) {
5401
- rand_opt_t g;
5402
- VALUE v1 = Qnil, v2 = Qnil;
5403
- dtype high;
5404
- ndfunc_arg_in_t ain[1] = { { OVERWRITE, 0 } };
5405
- ndfunc_t ndf = { iter_dfloat_rand, FULL_LOOP, 1, 0, ain, 0 };
5406
-
5407
- rb_scan_args(argc, args, "02", &v1, &v2);
5408
- if (v2 == Qnil) {
5409
- g.low = m_zero;
5410
- if (v1 == Qnil) {
5411
-
5412
- g.max = high = m_one;
5413
-
5414
- } else {
5415
- g.max = high = m_num_to_data(v1);
5416
- }
5417
-
5418
- } else {
5419
- g.low = m_num_to_data(v1);
5420
- high = m_num_to_data(v2);
5421
- g.max = m_sub(high, g.low);
5422
- }
5423
-
5424
- na_ndloop3(&ndf, &g, 1, self);
5425
- return self;
5426
- }
5427
-
5428
4180
  typedef struct {
5429
4181
  dtype mu;
5430
4182
  rtype sigma;