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
@@ -43,6 +43,25 @@ static ID id_to_a;
43
43
  VALUE cT;
44
44
  extern VALUE cRT;
45
45
 
46
+ #include "mh/clip.h"
47
+ #include "mh/sum.h"
48
+ #include "mh/prod.h"
49
+ #include "mh/min.h"
50
+ #include "mh/max.h"
51
+ #include "mh/ptp.h"
52
+ #include "mh/max_index.h"
53
+ #include "mh/min_index.h"
54
+ #include "mh/argmax.h"
55
+ #include "mh/argmin.h"
56
+ #include "mh/maximum.h"
57
+ #include "mh/minimum.h"
58
+ #include "mh/minmax.h"
59
+ #include "mh/cumsum.h"
60
+ #include "mh/cumprod.h"
61
+ #include "mh/mulsum.h"
62
+ #include "mh/seq.h"
63
+ #include "mh/eye.h"
64
+ #include "mh/rand.h"
46
65
  #include "mh/mean.h"
47
66
  #include "mh/var.h"
48
67
  #include "mh/stddev.h"
@@ -50,6 +69,25 @@ extern VALUE cRT;
50
69
 
51
70
  typedef int8_t int8; // Type aliases for shorter notation
52
71
  // following the codebase naming convention.
72
+ DEF_NARRAY_CLIP_METHOD_FUNC(int8, numo_cInt8)
73
+ DEF_NARRAY_INT_SUM_METHOD_FUNC(int8, numo_cInt8, int64_t, numo_cInt64)
74
+ DEF_NARRAY_INT_PROD_METHOD_FUNC(int8, numo_cInt8, int64_t, numo_cInt64)
75
+ DEF_NARRAY_INT_MAX_METHOD_FUNC(int8, numo_cInt8)
76
+ DEF_NARRAY_INT_MIN_METHOD_FUNC(int8, numo_cInt8)
77
+ DEF_NARRAY_INT_PTP_METHOD_FUNC(int8, numo_cInt8)
78
+ DEF_NARRAY_INT_MAX_INDEX_METHOD_FUNC(int8)
79
+ DEF_NARRAY_INT_MIN_INDEX_METHOD_FUNC(int8)
80
+ DEF_NARRAY_INT_ARGMAX_METHOD_FUNC(int8)
81
+ DEF_NARRAY_INT_ARGMIN_METHOD_FUNC(int8)
82
+ DEF_NARRAY_INT_MAXIMUM_METHOD_FUNC(int8, numo_cInt8)
83
+ DEF_NARRAY_INT_MINIMUM_METHOD_FUNC(int8, numo_cInt8)
84
+ DEF_NARRAY_INT_MINMAX_METHOD_FUNC(int8, numo_cInt8)
85
+ DEF_NARRAY_INT_CUMSUM_METHOD_FUNC(int8, numo_cInt8)
86
+ DEF_NARRAY_INT_CUMPROD_METHOD_FUNC(int8, numo_cInt8)
87
+ DEF_NARRAY_INT_MULSUM_METHOD_FUNC(int8, numo_cInt8)
88
+ DEF_NARRAY_INT_SEQ_METHOD_FUNC(int8)
89
+ DEF_NARRAY_EYE_METHOD_FUNC(int8)
90
+ DEF_NARRAY_INT_RAND_METHOD_FUNC(int8)
53
91
  DEF_NARRAY_INT_MEAN_METHOD_FUNC(int8, numo_cInt8)
54
92
  DEF_NARRAY_INT_VAR_METHOD_FUNC(int8, numo_cInt8)
55
93
  DEF_NARRAY_INT_STDDEV_METHOD_FUNC(int8, numo_cInt8)
@@ -3276,710 +3314,6 @@ static VALUE int8_le(VALUE self, VALUE other) {
3276
3314
  }
3277
3315
  }
3278
3316
 
3279
- static void iter_int8_clip(na_loop_t* const lp) {
3280
- size_t i;
3281
- char *p1, *p2, *p3, *p4;
3282
- ssize_t s1, s2, s3, s4;
3283
- dtype x, min, max;
3284
- INIT_COUNTER(lp, i);
3285
- INIT_PTR(lp, 0, p1, s1);
3286
- INIT_PTR(lp, 1, p2, s2);
3287
- INIT_PTR(lp, 2, p3, s3);
3288
- INIT_PTR(lp, 3, p4, s4);
3289
- for (; i--;) {
3290
- GET_DATA_STRIDE(p1, s1, dtype, x);
3291
- GET_DATA_STRIDE(p2, s2, dtype, min);
3292
- GET_DATA_STRIDE(p3, s3, dtype, max);
3293
- if (m_gt(min, max)) {
3294
- rb_raise(nary_eOperationError, "min is greater than max");
3295
- }
3296
- if (m_lt(x, min)) {
3297
- x = min;
3298
- }
3299
- if (m_gt(x, max)) {
3300
- x = max;
3301
- }
3302
- SET_DATA_STRIDE(p4, s4, dtype, x);
3303
- }
3304
- }
3305
-
3306
- static void iter_int8_clip_min(na_loop_t* const lp) {
3307
- size_t i;
3308
- char *p1, *p2, *p3;
3309
- ssize_t s1, s2, s3;
3310
- dtype x, min;
3311
- INIT_COUNTER(lp, i);
3312
- INIT_PTR(lp, 0, p1, s1);
3313
- INIT_PTR(lp, 1, p2, s2);
3314
- INIT_PTR(lp, 2, p3, s3);
3315
- for (; i--;) {
3316
- GET_DATA_STRIDE(p1, s1, dtype, x);
3317
- GET_DATA_STRIDE(p2, s2, dtype, min);
3318
- if (m_lt(x, min)) {
3319
- x = min;
3320
- }
3321
- SET_DATA_STRIDE(p3, s3, dtype, x);
3322
- }
3323
- }
3324
-
3325
- static void iter_int8_clip_max(na_loop_t* const lp) {
3326
- size_t i;
3327
- char *p1, *p2, *p3;
3328
- ssize_t s1, s2, s3;
3329
- dtype x, max;
3330
- INIT_COUNTER(lp, i);
3331
- INIT_PTR(lp, 0, p1, s1);
3332
- INIT_PTR(lp, 1, p2, s2);
3333
- INIT_PTR(lp, 2, p3, s3);
3334
- for (; i--;) {
3335
- GET_DATA_STRIDE(p1, s1, dtype, x);
3336
- GET_DATA_STRIDE(p2, s2, dtype, max);
3337
- if (m_gt(x, max)) {
3338
- x = max;
3339
- }
3340
- SET_DATA_STRIDE(p3, s3, dtype, x);
3341
- }
3342
- }
3343
-
3344
- /*
3345
- Clip array elements by [min,max].
3346
- If either of min or max is nil, one side is clipped.
3347
- @overload clip(min,max)
3348
- @param [Numo::NArray,Numeric] min
3349
- @param [Numo::NArray,Numeric] max
3350
- @return [Numo::NArray] result of clip.
3351
-
3352
- @example
3353
- a = Numo::Int32.new(10).seq
3354
- # => Numo::Int32#shape=[10]
3355
- # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
3356
-
3357
- a.clip(1,8)
3358
- # => Numo::Int32#shape=[10]
3359
- # [1, 1, 2, 3, 4, 5, 6, 7, 8, 8]
3360
-
3361
- a.inplace.clip(3,6)
3362
- a
3363
- # => Numo::Int32#shape=[10]
3364
- # [3, 3, 3, 3, 4, 5, 6, 6, 6, 6]
3365
-
3366
- b = Numo::Int32.new(10).seq
3367
- # => Numo::Int32#shape=[10]
3368
- # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
3369
-
3370
- b.clip([3,4,1,1,1,4,4,4,4,4], 8)
3371
- # => Numo::Int32#shape=[10]
3372
- # [3, 4, 2, 3, 4, 5, 6, 7, 8, 8]
3373
- */
3374
- static VALUE int8_clip(VALUE self, VALUE min, VALUE max) {
3375
- ndfunc_arg_in_t ain[3] = { { Qnil, 0 }, { cT, 0 }, { cT, 0 } };
3376
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
3377
- ndfunc_t ndf_min = { iter_int8_clip_min, STRIDE_LOOP, 2, 1, ain, aout };
3378
- ndfunc_t ndf_max = { iter_int8_clip_max, STRIDE_LOOP, 2, 1, ain, aout };
3379
- ndfunc_t ndf_both = { iter_int8_clip, STRIDE_LOOP, 3, 1, ain, aout };
3380
-
3381
- if (RTEST(min)) {
3382
- if (RTEST(max)) {
3383
- return na_ndloop(&ndf_both, 3, self, min, max);
3384
- } else {
3385
- return na_ndloop(&ndf_min, 2, self, min);
3386
- }
3387
- } else {
3388
- if (RTEST(max)) {
3389
- return na_ndloop(&ndf_max, 2, self, max);
3390
- }
3391
- }
3392
- rb_raise(rb_eArgError, "min and max are not given");
3393
- return Qnil;
3394
- }
3395
-
3396
- static void iter_int8_sum(na_loop_t* const lp) {
3397
- size_t n;
3398
- char *p1, *p2;
3399
- ssize_t s1;
3400
-
3401
- INIT_COUNTER(lp, n);
3402
- INIT_PTR(lp, 0, p1, s1);
3403
- p2 = lp->args[1].ptr + lp->args[1].iter[0].pos;
3404
-
3405
- *(int64_t*)p2 = f_sum(n, p1, s1);
3406
- }
3407
-
3408
- /*
3409
- sum of self.
3410
- @overload sum(axis:nil, keepdims:false)
3411
- @param [Numeric,Array,Range] axis Performs sum along the axis.
3412
- @param [TrueClass] keepdims If true, the reduced axes are left in the result array as
3413
- dimensions with size one.
3414
- @return [Numo::Int8] returns result of sum.
3415
- */
3416
- static VALUE int8_sum(int argc, VALUE* argv, VALUE self) {
3417
- VALUE v, reduce;
3418
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { sym_reduce, 0 } };
3419
- ndfunc_arg_out_t aout[1] = { { numo_cInt64, 0 } };
3420
- ndfunc_t ndf = { iter_int8_sum, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE, 2, 1, ain, aout };
3421
-
3422
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
3423
-
3424
- v = na_ndloop(&ndf, 2, self, reduce);
3425
-
3426
- return rb_funcall(v, rb_intern("extract"), 0);
3427
- }
3428
-
3429
- static void iter_int8_prod(na_loop_t* const lp) {
3430
- size_t n;
3431
- char *p1, *p2;
3432
- ssize_t s1;
3433
-
3434
- INIT_COUNTER(lp, n);
3435
- INIT_PTR(lp, 0, p1, s1);
3436
- p2 = lp->args[1].ptr + lp->args[1].iter[0].pos;
3437
-
3438
- *(int64_t*)p2 = f_prod(n, p1, s1);
3439
- }
3440
-
3441
- /*
3442
- prod of self.
3443
- @overload prod(axis:nil, keepdims:false)
3444
- @param [Numeric,Array,Range] axis Performs prod along the axis.
3445
- @param [TrueClass] keepdims If true, the reduced axes are left in the result array as
3446
- dimensions with size one.
3447
- @return [Numo::Int8] returns result of prod.
3448
- */
3449
- static VALUE int8_prod(int argc, VALUE* argv, VALUE self) {
3450
- VALUE v, reduce;
3451
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { sym_reduce, 0 } };
3452
- ndfunc_arg_out_t aout[1] = { { numo_cInt64, 0 } };
3453
- ndfunc_t ndf = { iter_int8_prod, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE, 2, 1, ain, aout };
3454
-
3455
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
3456
-
3457
- v = na_ndloop(&ndf, 2, self, reduce);
3458
-
3459
- return rb_funcall(v, rb_intern("extract"), 0);
3460
- }
3461
-
3462
- static void iter_int8_min(na_loop_t* const lp) {
3463
- size_t n;
3464
- char *p1, *p2;
3465
- ssize_t s1;
3466
-
3467
- INIT_COUNTER(lp, n);
3468
- INIT_PTR(lp, 0, p1, s1);
3469
- p2 = lp->args[1].ptr + lp->args[1].iter[0].pos;
3470
-
3471
- *(dtype*)p2 = f_min(n, p1, s1);
3472
- }
3473
-
3474
- /*
3475
- min of self.
3476
- @overload min(axis:nil, keepdims:false)
3477
- @param [Numeric,Array,Range] axis Performs min along the axis.
3478
- @param [TrueClass] keepdims If true, the reduced axes are left in the result array as
3479
- dimensions with size one.
3480
- @return [Numo::Int8] returns result of min.
3481
- */
3482
- static VALUE int8_min(int argc, VALUE* argv, VALUE self) {
3483
- VALUE v, reduce;
3484
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { sym_reduce, 0 } };
3485
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
3486
- ndfunc_t ndf = { iter_int8_min, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE, 2, 1, ain, aout };
3487
-
3488
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
3489
-
3490
- v = na_ndloop(&ndf, 2, self, reduce);
3491
-
3492
- return int8_extract(v);
3493
- }
3494
-
3495
- static void iter_int8_max(na_loop_t* const lp) {
3496
- size_t n;
3497
- char *p1, *p2;
3498
- ssize_t s1;
3499
-
3500
- INIT_COUNTER(lp, n);
3501
- INIT_PTR(lp, 0, p1, s1);
3502
- p2 = lp->args[1].ptr + lp->args[1].iter[0].pos;
3503
-
3504
- *(dtype*)p2 = f_max(n, p1, s1);
3505
- }
3506
-
3507
- /*
3508
- max of self.
3509
- @overload max(axis:nil, keepdims:false)
3510
- @param [Numeric,Array,Range] axis Performs max along the axis.
3511
- @param [TrueClass] keepdims If true, the reduced axes are left in the result array as
3512
- dimensions with size one.
3513
- @return [Numo::Int8] returns result of max.
3514
- */
3515
- static VALUE int8_max(int argc, VALUE* argv, VALUE self) {
3516
- VALUE v, reduce;
3517
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { sym_reduce, 0 } };
3518
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
3519
- ndfunc_t ndf = { iter_int8_max, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE, 2, 1, ain, aout };
3520
-
3521
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
3522
-
3523
- v = na_ndloop(&ndf, 2, self, reduce);
3524
-
3525
- return int8_extract(v);
3526
- }
3527
-
3528
- static void iter_int8_ptp(na_loop_t* const lp) {
3529
- size_t n;
3530
- char *p1, *p2;
3531
- ssize_t s1;
3532
-
3533
- INIT_COUNTER(lp, n);
3534
- INIT_PTR(lp, 0, p1, s1);
3535
- p2 = lp->args[1].ptr + lp->args[1].iter[0].pos;
3536
-
3537
- *(dtype*)p2 = f_ptp(n, p1, s1);
3538
- }
3539
-
3540
- /*
3541
- ptp of self.
3542
- @overload ptp(axis:nil, keepdims:false)
3543
- @param [Numeric,Array,Range] axis Performs ptp along the axis.
3544
- @param [TrueClass] keepdims If true, the reduced axes are left in the result array as
3545
- dimensions with size one.
3546
- @return [Numo::Int8] returns result of ptp.
3547
- */
3548
- static VALUE int8_ptp(int argc, VALUE* argv, VALUE self) {
3549
- VALUE v, reduce;
3550
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { sym_reduce, 0 } };
3551
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
3552
- ndfunc_t ndf = { iter_int8_ptp, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE, 2, 1, ain, aout };
3553
-
3554
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
3555
-
3556
- v = na_ndloop(&ndf, 2, self, reduce);
3557
-
3558
- return int8_extract(v);
3559
- }
3560
-
3561
- #define idx_t int64_t
3562
- static void iter_int8_max_index_index64(na_loop_t* const lp) {
3563
- size_t n, idx;
3564
- char *d_ptr, *i_ptr, *o_ptr;
3565
- ssize_t d_step, i_step;
3566
-
3567
- INIT_COUNTER(lp, n);
3568
- INIT_PTR(lp, 0, d_ptr, d_step);
3569
-
3570
- idx = f_max_index(n, d_ptr, d_step);
3571
-
3572
- INIT_PTR(lp, 1, i_ptr, i_step);
3573
- o_ptr = NDL_PTR(lp, 2);
3574
- *(idx_t*)o_ptr = *(idx_t*)(i_ptr + i_step * idx);
3575
- }
3576
- #undef idx_t
3577
-
3578
- #define idx_t int32_t
3579
- static void iter_int8_max_index_index32(na_loop_t* const lp) {
3580
- size_t n, idx;
3581
- char *d_ptr, *i_ptr, *o_ptr;
3582
- ssize_t d_step, i_step;
3583
-
3584
- INIT_COUNTER(lp, n);
3585
- INIT_PTR(lp, 0, d_ptr, d_step);
3586
-
3587
- idx = f_max_index(n, d_ptr, d_step);
3588
-
3589
- INIT_PTR(lp, 1, i_ptr, i_step);
3590
- o_ptr = NDL_PTR(lp, 2);
3591
- *(idx_t*)o_ptr = *(idx_t*)(i_ptr + i_step * idx);
3592
- }
3593
- #undef idx_t
3594
-
3595
- /*
3596
- Index of the maximum value.
3597
- @overload max_index(axis:nil)
3598
- @param [Numeric,Array,Range] axis Finds maximum values along the axis and returns **flat
3599
- 1-d indices**.
3600
- @return [Integer,Numo::Int] returns result indices.
3601
- @see #argmax
3602
- @see #max
3603
-
3604
- @example
3605
- a = Numo::NArray[3,4,1,2]
3606
- a.max_index #=> 1
3607
-
3608
- b = Numo::NArray[[3,4,1],[2,0,5]]
3609
- b.max_index #=> 5
3610
- b.max_index(axis:1) #=> [1, 5]
3611
- b.max_index(axis:0) #=> [0, 1, 5]
3612
- b[b.max_index(axis:0)] #=> [3, 4, 5]
3613
- */
3614
- static VALUE int8_max_index(int argc, VALUE* argv, VALUE self) {
3615
- narray_t* na;
3616
- VALUE idx, reduce;
3617
- ndfunc_arg_in_t ain[3] = { { Qnil, 0 }, { Qnil, 0 }, { sym_reduce, 0 } };
3618
- ndfunc_arg_out_t aout[1] = { { 0, 0, 0 } };
3619
- ndfunc_t ndf = { 0, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE | NDF_EXTRACT, 3, 1, ain, aout };
3620
-
3621
- GetNArray(self, na);
3622
- if (na->ndim == 0) {
3623
- return INT2FIX(0);
3624
- }
3625
- if (na->size > (~(u_int32_t)0)) {
3626
- aout[0].type = numo_cInt64;
3627
- idx = nary_new(numo_cInt64, na->ndim, na->shape);
3628
- ndf.func = iter_int8_max_index_index64;
3629
-
3630
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
3631
-
3632
- } else {
3633
- aout[0].type = numo_cInt32;
3634
- idx = nary_new(numo_cInt32, na->ndim, na->shape);
3635
- ndf.func = iter_int8_max_index_index32;
3636
-
3637
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
3638
- }
3639
- rb_funcall(idx, rb_intern("seq"), 0);
3640
-
3641
- return na_ndloop(&ndf, 3, self, idx, reduce);
3642
- }
3643
-
3644
- #define idx_t int64_t
3645
- static void iter_int8_min_index_index64(na_loop_t* const lp) {
3646
- size_t n, idx;
3647
- char *d_ptr, *i_ptr, *o_ptr;
3648
- ssize_t d_step, i_step;
3649
-
3650
- INIT_COUNTER(lp, n);
3651
- INIT_PTR(lp, 0, d_ptr, d_step);
3652
-
3653
- idx = f_min_index(n, d_ptr, d_step);
3654
-
3655
- INIT_PTR(lp, 1, i_ptr, i_step);
3656
- o_ptr = NDL_PTR(lp, 2);
3657
- *(idx_t*)o_ptr = *(idx_t*)(i_ptr + i_step * idx);
3658
- }
3659
- #undef idx_t
3660
-
3661
- #define idx_t int32_t
3662
- static void iter_int8_min_index_index32(na_loop_t* const lp) {
3663
- size_t n, idx;
3664
- char *d_ptr, *i_ptr, *o_ptr;
3665
- ssize_t d_step, i_step;
3666
-
3667
- INIT_COUNTER(lp, n);
3668
- INIT_PTR(lp, 0, d_ptr, d_step);
3669
-
3670
- idx = f_min_index(n, d_ptr, d_step);
3671
-
3672
- INIT_PTR(lp, 1, i_ptr, i_step);
3673
- o_ptr = NDL_PTR(lp, 2);
3674
- *(idx_t*)o_ptr = *(idx_t*)(i_ptr + i_step * idx);
3675
- }
3676
- #undef idx_t
3677
-
3678
- /*
3679
- Index of the minimum value.
3680
- @overload min_index(axis:nil)
3681
- @param [Numeric,Array,Range] axis Finds minimum values along the axis and returns **flat
3682
- 1-d indices**.
3683
- @return [Integer,Numo::Int] returns result indices.
3684
- @see #argmin
3685
- @see #min
3686
-
3687
- @example
3688
- a = Numo::NArray[3,4,1,2]
3689
- a.min_index #=> 2
3690
-
3691
- b = Numo::NArray[[3,4,1],[2,0,5]]
3692
- b.min_index #=> 4
3693
- b.min_index(axis:1) #=> [2, 4]
3694
- b.min_index(axis:0) #=> [3, 4, 2]
3695
- b[b.min_index(axis:0)] #=> [2, 0, 1]
3696
- */
3697
- static VALUE int8_min_index(int argc, VALUE* argv, VALUE self) {
3698
- narray_t* na;
3699
- VALUE idx, reduce;
3700
- ndfunc_arg_in_t ain[3] = { { Qnil, 0 }, { Qnil, 0 }, { sym_reduce, 0 } };
3701
- ndfunc_arg_out_t aout[1] = { { 0, 0, 0 } };
3702
- ndfunc_t ndf = { 0, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE | NDF_EXTRACT, 3, 1, ain, aout };
3703
-
3704
- GetNArray(self, na);
3705
- if (na->ndim == 0) {
3706
- return INT2FIX(0);
3707
- }
3708
- if (na->size > (~(u_int32_t)0)) {
3709
- aout[0].type = numo_cInt64;
3710
- idx = nary_new(numo_cInt64, na->ndim, na->shape);
3711
- ndf.func = iter_int8_min_index_index64;
3712
-
3713
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
3714
-
3715
- } else {
3716
- aout[0].type = numo_cInt32;
3717
- idx = nary_new(numo_cInt32, na->ndim, na->shape);
3718
- ndf.func = iter_int8_min_index_index32;
3719
-
3720
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
3721
- }
3722
- rb_funcall(idx, rb_intern("seq"), 0);
3723
-
3724
- return na_ndloop(&ndf, 3, self, idx, reduce);
3725
- }
3726
-
3727
- #define idx_t int64_t
3728
- static void iter_int8_argmax_arg64(na_loop_t* const lp) {
3729
- size_t n, idx;
3730
- char *d_ptr, *o_ptr;
3731
- ssize_t d_step;
3732
-
3733
- INIT_COUNTER(lp, n);
3734
- INIT_PTR(lp, 0, d_ptr, d_step);
3735
-
3736
- idx = f_max_index(n, d_ptr, d_step);
3737
-
3738
- o_ptr = NDL_PTR(lp, 1);
3739
- *(idx_t*)o_ptr = (idx_t)idx;
3740
- }
3741
- #undef idx_t
3742
-
3743
- #define idx_t int32_t
3744
- static void iter_int8_argmax_arg32(na_loop_t* const lp) {
3745
- size_t n, idx;
3746
- char *d_ptr, *o_ptr;
3747
- ssize_t d_step;
3748
-
3749
- INIT_COUNTER(lp, n);
3750
- INIT_PTR(lp, 0, d_ptr, d_step);
3751
-
3752
- idx = f_max_index(n, d_ptr, d_step);
3753
-
3754
- o_ptr = NDL_PTR(lp, 1);
3755
- *(idx_t*)o_ptr = (idx_t)idx;
3756
- }
3757
- #undef idx_t
3758
-
3759
- /*
3760
- Index of the maximum value.
3761
- @overload argmax(axis:nil)
3762
- @param [Numeric,Array,Range] axis Finds maximum values along the axis and returns **indices
3763
- along the axis**.
3764
- @return [Integer,Numo::Int] returns the result indices.
3765
- @see #max_index
3766
- @see #max
3767
-
3768
- @example
3769
- a = Numo::NArray[3,4,1,2]
3770
- a.argmax #=> 1
3771
-
3772
- b = Numo::NArray[[3,4,1],[2,0,5]]
3773
- b.argmax #=> 5
3774
- b.argmax(axis:1) #=> [1, 2]
3775
- b.argmax(axis:0) #=> [0, 0, 1]
3776
- b.at(b.argmax(axis:0), 0..-1) #=> [3, 4, 5]
3777
- */
3778
- static VALUE int8_argmax(int argc, VALUE* argv, VALUE self) {
3779
- narray_t* na;
3780
- VALUE reduce;
3781
- ndfunc_arg_in_t ain[2] = { { Qnil, 0 }, { sym_reduce, 0 } };
3782
- ndfunc_arg_out_t aout[1] = { { 0, 0, 0 } };
3783
- ndfunc_t ndf = { 0, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE | NDF_EXTRACT, 2, 1, ain, aout };
3784
-
3785
- GetNArray(self, na);
3786
- if (na->ndim == 0) {
3787
- return INT2FIX(0);
3788
- }
3789
- if (na->size > (~(u_int32_t)0)) {
3790
- aout[0].type = numo_cInt64;
3791
- ndf.func = iter_int8_argmax_arg64;
3792
-
3793
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
3794
-
3795
- } else {
3796
- aout[0].type = numo_cInt32;
3797
- ndf.func = iter_int8_argmax_arg32;
3798
-
3799
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
3800
- }
3801
-
3802
- return na_ndloop(&ndf, 2, self, reduce);
3803
- }
3804
-
3805
- #define idx_t int64_t
3806
- static void iter_int8_argmin_arg64(na_loop_t* const lp) {
3807
- size_t n, idx;
3808
- char *d_ptr, *o_ptr;
3809
- ssize_t d_step;
3810
-
3811
- INIT_COUNTER(lp, n);
3812
- INIT_PTR(lp, 0, d_ptr, d_step);
3813
-
3814
- idx = f_min_index(n, d_ptr, d_step);
3815
-
3816
- o_ptr = NDL_PTR(lp, 1);
3817
- *(idx_t*)o_ptr = (idx_t)idx;
3818
- }
3819
- #undef idx_t
3820
-
3821
- #define idx_t int32_t
3822
- static void iter_int8_argmin_arg32(na_loop_t* const lp) {
3823
- size_t n, idx;
3824
- char *d_ptr, *o_ptr;
3825
- ssize_t d_step;
3826
-
3827
- INIT_COUNTER(lp, n);
3828
- INIT_PTR(lp, 0, d_ptr, d_step);
3829
-
3830
- idx = f_min_index(n, d_ptr, d_step);
3831
-
3832
- o_ptr = NDL_PTR(lp, 1);
3833
- *(idx_t*)o_ptr = (idx_t)idx;
3834
- }
3835
- #undef idx_t
3836
-
3837
- /*
3838
- Index of the minimum value.
3839
- @overload argmin(axis:nil)
3840
- @param [Numeric,Array,Range] axis Finds minimum values along the axis and returns **indices
3841
- along the axis**.
3842
- @return [Integer,Numo::Int] returns the result indices.
3843
- @see #min_index
3844
- @see #min
3845
-
3846
- @example
3847
- a = Numo::NArray[3,4,1,2]
3848
- a.argmin #=> 2
3849
-
3850
- b = Numo::NArray[[3,4,1],[2,0,5]]
3851
- b.argmin #=> 4
3852
- b.argmin(axis:1) #=> [2, 1]
3853
- b.argmin(axis:0) #=> [1, 1, 0]
3854
- b.at(b.argmin(axis:0), 0..-1) #=> [2, 0, 1]
3855
- */
3856
- static VALUE int8_argmin(int argc, VALUE* argv, VALUE self) {
3857
- narray_t* na;
3858
- VALUE reduce;
3859
- ndfunc_arg_in_t ain[2] = { { Qnil, 0 }, { sym_reduce, 0 } };
3860
- ndfunc_arg_out_t aout[1] = { { 0, 0, 0 } };
3861
- ndfunc_t ndf = { 0, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE | NDF_EXTRACT, 2, 1, ain, aout };
3862
-
3863
- GetNArray(self, na);
3864
- if (na->ndim == 0) {
3865
- return INT2FIX(0);
3866
- }
3867
- if (na->size > (~(u_int32_t)0)) {
3868
- aout[0].type = numo_cInt64;
3869
- ndf.func = iter_int8_argmin_arg64;
3870
-
3871
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
3872
-
3873
- } else {
3874
- aout[0].type = numo_cInt32;
3875
- ndf.func = iter_int8_argmin_arg32;
3876
-
3877
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
3878
- }
3879
-
3880
- return na_ndloop(&ndf, 2, self, reduce);
3881
- }
3882
-
3883
- static void iter_int8_minmax(na_loop_t* const lp) {
3884
- size_t n;
3885
- char* p1;
3886
- ssize_t s1;
3887
- dtype xmin, xmax;
3888
-
3889
- INIT_COUNTER(lp, n);
3890
- INIT_PTR(lp, 0, p1, s1);
3891
-
3892
- f_minmax(n, p1, s1, &xmin, &xmax);
3893
-
3894
- *(dtype*)(lp->args[1].ptr + lp->args[1].iter[0].pos) = xmin;
3895
- *(dtype*)(lp->args[2].ptr + lp->args[2].iter[0].pos) = xmax;
3896
- }
3897
-
3898
- /*
3899
- minmax of self.
3900
- @overload minmax(axis:nil, keepdims:false)
3901
- @param [Numeric,Array,Range] axis Finds min-max along the axis.
3902
- @param [TrueClass] keepdims (keyword) If true, the reduced axes are left in the result array
3903
- as dimensions with size one.
3904
- @return [Numo::Int8,Numo::Int8] min and max of self.
3905
- */
3906
- static VALUE int8_minmax(int argc, VALUE* argv, VALUE self) {
3907
- VALUE reduce;
3908
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { sym_reduce, 0 } };
3909
- ndfunc_arg_out_t aout[2] = { { cT, 0 }, { cT, 0 } };
3910
- ndfunc_t ndf = {
3911
- iter_int8_minmax, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE | NDF_EXTRACT, 2, 2, ain, aout
3912
- };
3913
-
3914
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
3915
-
3916
- return na_ndloop(&ndf, 2, self, reduce);
3917
- }
3918
-
3919
- static void iter_int8_s_maximum(na_loop_t* const lp) {
3920
- size_t i, n;
3921
- char *p1, *p2, *p3;
3922
- ssize_t s1, s2, s3;
3923
-
3924
- INIT_COUNTER(lp, n);
3925
- INIT_PTR(lp, 0, p1, s1);
3926
- INIT_PTR(lp, 1, p2, s2);
3927
- INIT_PTR(lp, 2, p3, s3);
3928
-
3929
- for (i = 0; i < n; i++) {
3930
- dtype x, y, z;
3931
- GET_DATA_STRIDE(p1, s1, dtype, x);
3932
- GET_DATA_STRIDE(p2, s2, dtype, y);
3933
- GET_DATA(p3, dtype, z);
3934
- z = f_maximum(x, y);
3935
- SET_DATA_STRIDE(p3, s3, dtype, z);
3936
- }
3937
- }
3938
-
3939
- static VALUE int8_s_maximum(int argc, VALUE* argv, VALUE mod) {
3940
- VALUE a1 = Qnil;
3941
- VALUE a2 = Qnil;
3942
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
3943
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
3944
- ndfunc_t ndf = { iter_int8_s_maximum, STRIDE_LOOP_NIP, 2, 1, ain, aout };
3945
-
3946
- rb_scan_args(argc, argv, "20", &a1, &a2);
3947
-
3948
- return na_ndloop(&ndf, 2, a1, a2);
3949
- }
3950
-
3951
- static void iter_int8_s_minimum(na_loop_t* const lp) {
3952
- size_t i, n;
3953
- char *p1, *p2, *p3;
3954
- ssize_t s1, s2, s3;
3955
-
3956
- INIT_COUNTER(lp, n);
3957
- INIT_PTR(lp, 0, p1, s1);
3958
- INIT_PTR(lp, 1, p2, s2);
3959
- INIT_PTR(lp, 2, p3, s3);
3960
-
3961
- for (i = 0; i < n; i++) {
3962
- dtype x, y, z;
3963
- GET_DATA_STRIDE(p1, s1, dtype, x);
3964
- GET_DATA_STRIDE(p2, s2, dtype, y);
3965
- GET_DATA(p3, dtype, z);
3966
- z = f_minimum(x, y);
3967
- SET_DATA_STRIDE(p3, s3, dtype, z);
3968
- }
3969
- }
3970
-
3971
- static VALUE int8_s_minimum(int argc, VALUE* argv, VALUE mod) {
3972
- VALUE a1 = Qnil;
3973
- VALUE a2 = Qnil;
3974
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
3975
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
3976
- ndfunc_t ndf = { iter_int8_s_minimum, STRIDE_LOOP_NIP, 2, 1, ain, aout };
3977
-
3978
- rb_scan_args(argc, argv, "20", &a1, &a2);
3979
-
3980
- return na_ndloop(&ndf, 2, a1, a2);
3981
- }
3982
-
3983
3317
  // ------- Integer count without weights -------
3984
3318
 
3985
3319
  static void iter_int8_bincount_32(na_loop_t* const lp) {
@@ -4213,461 +3547,6 @@ static VALUE int8_bincount(int argc, VALUE* argv, VALUE self) {
4213
3547
  }
4214
3548
  }
4215
3549
 
4216
- static void iter_int8_cumsum(na_loop_t* const lp) {
4217
- size_t i;
4218
- char *p1, *p2;
4219
- ssize_t s1, s2;
4220
- dtype x, y;
4221
-
4222
- INIT_COUNTER(lp, i);
4223
- INIT_PTR(lp, 0, p1, s1);
4224
- INIT_PTR(lp, 1, p2, s2);
4225
-
4226
- GET_DATA_STRIDE(p1, s1, dtype, x);
4227
- SET_DATA_STRIDE(p2, s2, dtype, x);
4228
- for (i--; i--;) {
4229
- GET_DATA_STRIDE(p1, s1, dtype, y);
4230
- m_cumsum(x, y);
4231
- SET_DATA_STRIDE(p2, s2, dtype, x);
4232
- }
4233
- }
4234
-
4235
- /*
4236
- cumsum of self.
4237
- @overload cumsum(axis:nil, nan:false)
4238
- @param [Numeric,Array,Range] axis Performs cumsum along the axis.
4239
- @param [TrueClass] nan If true, apply NaN-aware algorithm (avoid NaN if exists).
4240
- @return [Numo::Int8] cumsum of self.
4241
- */
4242
- static VALUE int8_cumsum(int argc, VALUE* argv, VALUE self) {
4243
- VALUE reduce;
4244
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { sym_reduce, 0 } };
4245
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
4246
- ndfunc_t ndf = { iter_int8_cumsum, STRIDE_LOOP | NDF_FLAT_REDUCE | NDF_CUM, 2, 1, ain, aout };
4247
-
4248
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
4249
-
4250
- return na_ndloop(&ndf, 2, self, reduce);
4251
- }
4252
-
4253
- static void iter_int8_cumprod(na_loop_t* const lp) {
4254
- size_t i;
4255
- char *p1, *p2;
4256
- ssize_t s1, s2;
4257
- dtype x, y;
4258
-
4259
- INIT_COUNTER(lp, i);
4260
- INIT_PTR(lp, 0, p1, s1);
4261
- INIT_PTR(lp, 1, p2, s2);
4262
-
4263
- GET_DATA_STRIDE(p1, s1, dtype, x);
4264
- SET_DATA_STRIDE(p2, s2, dtype, x);
4265
- for (i--; i--;) {
4266
- GET_DATA_STRIDE(p1, s1, dtype, y);
4267
- m_cumprod(x, y);
4268
- SET_DATA_STRIDE(p2, s2, dtype, x);
4269
- }
4270
- }
4271
-
4272
- /*
4273
- cumprod of self.
4274
- @overload cumprod(axis:nil, nan:false)
4275
- @param [Numeric,Array,Range] axis Performs cumprod along the axis.
4276
- @param [TrueClass] nan If true, apply NaN-aware algorithm (avoid NaN if exists).
4277
- @return [Numo::Int8] cumprod of self.
4278
- */
4279
- static VALUE int8_cumprod(int argc, VALUE* argv, VALUE self) {
4280
- VALUE reduce;
4281
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { sym_reduce, 0 } };
4282
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
4283
- ndfunc_t ndf = {
4284
- iter_int8_cumprod, STRIDE_LOOP | NDF_FLAT_REDUCE | NDF_CUM, 2, 1, ain, aout
4285
- };
4286
-
4287
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
4288
-
4289
- return na_ndloop(&ndf, 2, self, reduce);
4290
- }
4291
-
4292
- //
4293
- static void iter_int8_mulsum(na_loop_t* const lp) {
4294
- size_t i, n;
4295
- char *p1, *p2, *p3;
4296
- ssize_t s1, s2, s3;
4297
-
4298
- INIT_COUNTER(lp, n);
4299
- INIT_PTR(lp, 0, p1, s1);
4300
- INIT_PTR(lp, 1, p2, s2);
4301
- INIT_PTR(lp, 2, p3, s3);
4302
-
4303
- if (s3 == 0) {
4304
- dtype z;
4305
- // Reduce loop
4306
- GET_DATA(p3, dtype, z);
4307
- for (i = 0; i < n; i++) {
4308
- dtype x, y;
4309
- GET_DATA_STRIDE(p1, s1, dtype, x);
4310
- GET_DATA_STRIDE(p2, s2, dtype, y);
4311
- m_mulsum(x, y, z);
4312
- }
4313
- SET_DATA(p3, dtype, z);
4314
- return;
4315
- } else {
4316
- for (i = 0; i < n; i++) {
4317
- dtype x, y, z;
4318
- GET_DATA_STRIDE(p1, s1, dtype, x);
4319
- GET_DATA_STRIDE(p2, s2, dtype, y);
4320
- GET_DATA(p3, dtype, z);
4321
- m_mulsum(x, y, z);
4322
- SET_DATA_STRIDE(p3, s3, dtype, z);
4323
- }
4324
- }
4325
- }
4326
- //
4327
-
4328
- static VALUE int8_mulsum_self(int argc, VALUE* argv, VALUE self) {
4329
- VALUE v, reduce;
4330
- VALUE naryv[2];
4331
- ndfunc_arg_in_t ain[4] = { { cT, 0 }, { cT, 0 }, { sym_reduce, 0 }, { sym_init, 0 } };
4332
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
4333
- ndfunc_t ndf = { iter_int8_mulsum, STRIDE_LOOP_NIP, 4, 1, ain, aout };
4334
-
4335
- if (argc < 1) {
4336
- rb_raise(rb_eArgError, "wrong number of arguments (%d for >=1)", argc);
4337
- }
4338
- // should fix below: [self.ndim,other.ndim].max or?
4339
- naryv[0] = self;
4340
- naryv[1] = argv[0];
4341
- //
4342
- reduce = na_reduce_dimension(argc - 1, argv + 1, 2, naryv, &ndf, 0);
4343
- //
4344
-
4345
- v = na_ndloop(&ndf, 4, self, argv[0], reduce, m_mulsum_init);
4346
- return int8_extract(v);
4347
- }
4348
-
4349
- /*
4350
- Binary mulsum.
4351
-
4352
- @overload mulsum(other, axis:nil, keepdims:false)
4353
- @param [Numo::NArray,Numeric] other
4354
- @param [Numeric,Array,Range] axis Performs mulsum along the axis.
4355
- @param [TrueClass] keepdims (keyword) If true, the reduced axes are left in the result array
4356
- as dimensions with size one.
4357
- @return [Numo::NArray] mulsum of self and other.
4358
- */
4359
- static VALUE int8_mulsum(int argc, VALUE* argv, VALUE self) {
4360
- //
4361
- VALUE klass, v;
4362
- //
4363
- if (argc < 1) {
4364
- rb_raise(rb_eArgError, "wrong number of arguments (%d for >=1)", argc);
4365
- }
4366
- //
4367
- klass = na_upcast(rb_obj_class(self), rb_obj_class(argv[0]));
4368
- if (klass == cT) {
4369
- return int8_mulsum_self(argc, argv, self);
4370
- } else {
4371
- v = rb_funcall(klass, id_cast, 1, self);
4372
- //
4373
- return rb_funcallv_kw(v, rb_intern("mulsum"), argc, argv, RB_PASS_CALLED_KEYWORDS);
4374
- //
4375
- }
4376
- //
4377
- }
4378
-
4379
- typedef double seq_data_t;
4380
-
4381
- typedef double seq_count_t;
4382
-
4383
- typedef struct {
4384
- seq_data_t beg;
4385
- seq_data_t step;
4386
- seq_count_t count;
4387
- } seq_opt_t;
4388
-
4389
- static void iter_int8_seq(na_loop_t* const lp) {
4390
- size_t i;
4391
- char* p1;
4392
- ssize_t s1;
4393
- size_t* idx1;
4394
- dtype x;
4395
- seq_data_t beg, step;
4396
- seq_count_t c;
4397
- seq_opt_t* g;
4398
-
4399
- INIT_COUNTER(lp, i);
4400
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
4401
- g = (seq_opt_t*)(lp->opt_ptr);
4402
- beg = g->beg;
4403
- step = g->step;
4404
- c = g->count;
4405
- if (idx1) {
4406
- for (; i--;) {
4407
- x = f_seq(beg, step, c++);
4408
- *(dtype*)(p1 + *idx1) = x;
4409
- idx1++;
4410
- }
4411
- } else {
4412
- for (; i--;) {
4413
- x = f_seq(beg, step, c++);
4414
- *(dtype*)(p1) = x;
4415
- p1 += s1;
4416
- }
4417
- }
4418
- g->count = c;
4419
- }
4420
-
4421
- /*
4422
- Set linear sequence of numbers to self. The sequence is obtained from
4423
- beg+i*step
4424
- where i is 1-dimensional index.
4425
- @overload seq([beg,[step]])
4426
- @param [Numeric] beg beginning of sequence. (default=0)
4427
- @param [Numeric] step step of sequence. (default=1)
4428
- @return [Numo::Int8] self.
4429
- @example
4430
- Numo::DFloat.new(6).seq(1,-0.2)
4431
- # => Numo::DFloat#shape=[6]
4432
- # [1, 0.8, 0.6, 0.4, 0.2, 0]
4433
-
4434
- Numo::DComplex.new(6).seq(1,-0.2+0.2i)
4435
- # => Numo::DComplex#shape=[6]
4436
- # [1+0i, 0.8+0.2i, 0.6+0.4i, 0.4+0.6i, 0.2+0.8i, 0+1i]
4437
- */
4438
- static VALUE int8_seq(int argc, VALUE* argv, VALUE self) {
4439
- seq_opt_t* g;
4440
- VALUE vbeg = Qnil, vstep = Qnil;
4441
- ndfunc_arg_in_t ain[1] = { { OVERWRITE, 0 } };
4442
- ndfunc_t ndf = { iter_int8_seq, FULL_LOOP, 1, 0, ain, 0 };
4443
-
4444
- g = ALLOCA_N(seq_opt_t, 1);
4445
- g->beg = m_zero;
4446
- g->step = m_one;
4447
- g->count = 0;
4448
- rb_scan_args(argc, argv, "02", &vbeg, &vstep);
4449
- if (vbeg != Qnil) {
4450
- g->beg = NUM2DBL(vbeg);
4451
- }
4452
- if (vstep != Qnil) {
4453
- g->step = NUM2DBL(vstep);
4454
- }
4455
-
4456
- na_ndloop3(&ndf, g, 1, self);
4457
- return self;
4458
- }
4459
-
4460
- static void iter_int8_eye(na_loop_t* const lp) {
4461
- size_t n0, n1;
4462
- size_t i0, i1;
4463
- ssize_t s0, s1;
4464
- char *p0, *p1;
4465
- char* g;
4466
- ssize_t kofs;
4467
- dtype data;
4468
-
4469
- g = (char*)(lp->opt_ptr);
4470
- kofs = *(ssize_t*)g;
4471
- data = *(dtype*)(g + sizeof(ssize_t));
4472
-
4473
- n0 = lp->args[0].shape[0];
4474
- n1 = lp->args[0].shape[1];
4475
- s0 = lp->args[0].iter[0].step;
4476
- s1 = lp->args[0].iter[1].step;
4477
- p0 = NDL_PTR(lp, 0);
4478
-
4479
- for (i0 = 0; i0 < n0; i0++) {
4480
- p1 = p0;
4481
- for (i1 = 0; i1 < n1; i1++) {
4482
- *(dtype*)p1 = (i0 + kofs == i1) ? data : m_zero;
4483
- p1 += s1;
4484
- }
4485
- p0 += s0;
4486
- }
4487
- }
4488
-
4489
- /*
4490
- Eye: Set a value to diagonal components, set 0 to non-diagonal components.
4491
- @overload eye([element,offset])
4492
- @param [Numeric] element Diagonal element to be stored. Default is 1.
4493
- @param [Integer] offset Diagonal offset from the main diagonal. The
4494
- default is 0. k>0 for diagonals above the main diagonal, and k<0
4495
- for diagonals below the main diagonal.
4496
- @return [Numo::Int8] eye of self.
4497
- */
4498
- static VALUE int8_eye(int argc, VALUE* argv, VALUE self) {
4499
- ndfunc_arg_in_t ain[1] = { { OVERWRITE, 2 } };
4500
- ndfunc_t ndf = { iter_int8_eye, NO_LOOP, 1, 0, ain, 0 };
4501
- ssize_t kofs;
4502
- dtype data;
4503
- char* g;
4504
- int nd;
4505
- narray_t* na;
4506
-
4507
- // check arguments
4508
- if (argc > 2) {
4509
- rb_raise(rb_eArgError, "too many arguments (%d for 0..2)", argc);
4510
- } else if (argc == 2) {
4511
- data = m_num_to_data(argv[0]);
4512
- kofs = NUM2SSIZET(argv[1]);
4513
- } else if (argc == 1) {
4514
- data = m_num_to_data(argv[0]);
4515
- kofs = 0;
4516
- } else {
4517
- data = m_one;
4518
- kofs = 0;
4519
- }
4520
-
4521
- GetNArray(self, na);
4522
- nd = na->ndim;
4523
- if (nd < 2) {
4524
- rb_raise(nary_eDimensionError, "less than 2-d array");
4525
- }
4526
-
4527
- // Diagonal offset from the main diagonal.
4528
- if (kofs >= 0) {
4529
- if ((size_t)(kofs) >= na->shape[nd - 1]) {
4530
- rb_raise(
4531
- rb_eArgError,
4532
- "invalid diagonal offset(%" SZF "d) for "
4533
- "last dimension size(%" SZF "d)",
4534
- kofs, na->shape[nd - 1]
4535
- );
4536
- }
4537
- } else {
4538
- if ((size_t)(-kofs) >= na->shape[nd - 2]) {
4539
- rb_raise(
4540
- rb_eArgError,
4541
- "invalid diagonal offset(%" SZF "d) for "
4542
- "last-1 dimension size(%" SZF "d)",
4543
- kofs, na->shape[nd - 2]
4544
- );
4545
- }
4546
- }
4547
-
4548
- g = ALLOCA_N(char, sizeof(ssize_t) + sizeof(dtype));
4549
- *(ssize_t*)g = kofs;
4550
- *(dtype*)(g + sizeof(ssize_t)) = data;
4551
-
4552
- na_ndloop3(&ndf, g, 1, self);
4553
- return self;
4554
- }
4555
-
4556
- #define HWID (sizeof(dtype) * 4)
4557
-
4558
- static int msb_pos(uint32_t a) {
4559
- int width = HWID;
4560
- int pos = 0;
4561
- uint32_t mask = (((dtype)1 << HWID) - 1) << HWID;
4562
-
4563
- if (a == 0) {
4564
- return -1;
4565
- }
4566
-
4567
- while (width) {
4568
- if (a & mask) {
4569
- pos += width;
4570
- } else {
4571
- mask >>= width;
4572
- }
4573
- width >>= 1;
4574
- mask &= mask << width;
4575
- }
4576
- return pos;
4577
- }
4578
-
4579
- /* generates a random number on [0,max) */
4580
- inline static dtype m_rand(uint32_t max, int shift) {
4581
- uint32_t x;
4582
- do {
4583
- x = gen_rand32();
4584
- x >>= shift;
4585
- } while (x >= max);
4586
- return x;
4587
- }
4588
-
4589
- typedef struct {
4590
- dtype low;
4591
- uint32_t max;
4592
- } rand_opt_t;
4593
-
4594
- static void iter_int8_rand(na_loop_t* const lp) {
4595
- size_t i;
4596
- char* p1;
4597
- ssize_t s1;
4598
- size_t* idx1;
4599
- dtype x;
4600
- rand_opt_t* g;
4601
- dtype low;
4602
- uint32_t max;
4603
- int shift;
4604
-
4605
- INIT_COUNTER(lp, i);
4606
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
4607
- g = (rand_opt_t*)(lp->opt_ptr);
4608
- low = g->low;
4609
- max = g->max;
4610
- shift = 31 - msb_pos(max);
4611
-
4612
- if (idx1) {
4613
- for (; i--;) {
4614
- x = m_add(m_rand(max, shift), low);
4615
- SET_DATA_INDEX(p1, idx1, dtype, x);
4616
- }
4617
- } else {
4618
- for (; i--;) {
4619
- x = m_add(m_rand(max, shift), low);
4620
- SET_DATA_STRIDE(p1, s1, dtype, x);
4621
- }
4622
- }
4623
- }
4624
-
4625
- /*
4626
- Generate uniformly distributed random numbers on self narray.
4627
- @overload rand([[low],high])
4628
- @param [Numeric] low lower inclusive boundary of random numbers. (default=0)
4629
- @param [Numeric] high upper exclusive boundary of random numbers. (default=1 or 1+1i for
4630
- complex types)
4631
- @return [Numo::Int8] self.
4632
- @example
4633
- Numo::DFloat.new(6).rand
4634
- # => Numo::DFloat#shape=[6]
4635
- # [0.0617545, 0.373067, 0.794815, 0.201042, 0.116041, 0.344032]
4636
-
4637
- Numo::DComplex.new(6).rand(5+5i)
4638
- # => Numo::DComplex#shape=[6]
4639
- # [2.69974+3.68908i, 0.825443+0.254414i, 0.540323+0.34354i, 4.52061+2.39322i, ...]
4640
-
4641
- Numo::Int32.new(6).rand(2,5)
4642
- # => Numo::Int32#shape=[6]
4643
- # [4, 3, 3, 2, 4, 2]
4644
- */
4645
- static VALUE int8_rand(int argc, VALUE* argv, VALUE self) {
4646
- rand_opt_t g;
4647
- VALUE v1 = Qnil, v2 = Qnil;
4648
- dtype high;
4649
- ndfunc_arg_in_t ain[1] = { { OVERWRITE, 0 } };
4650
- ndfunc_t ndf = { iter_int8_rand, FULL_LOOP, 1, 0, ain, 0 };
4651
-
4652
- rb_scan_args(argc, argv, "11", &v1, &v2);
4653
- if (v2 == Qnil) {
4654
- g.low = m_zero;
4655
- g.max = high = m_num_to_data(v1);
4656
-
4657
- } else {
4658
- g.low = m_num_to_data(v1);
4659
- high = m_num_to_data(v2);
4660
- g.max = m_sub(high, g.low);
4661
- }
4662
-
4663
- if (high <= g.low) {
4664
- rb_raise(rb_eArgError, "high must be larger than low");
4665
- }
4666
-
4667
- na_ndloop3(&ndf, &g, 1, self);
4668
- return self;
4669
- }
4670
-
4671
3550
  static void iter_int8_poly(na_loop_t* const lp) {
4672
3551
  size_t i;
4673
3552
  dtype x, y, a;
@@ -5340,16 +4219,170 @@ void Init_numo_int8(void) {
5340
4219
  rb_define_alias(cT, ">=", "ge");
5341
4220
  rb_define_alias(cT, "<", "lt");
5342
4221
  rb_define_alias(cT, "<=", "le");
4222
+ /**
4223
+ * Clip array elements by [min,max].
4224
+ * If either of min or max is nil, one side is clipped.
4225
+ * @overload clip(min,max)
4226
+ * @param [Numo::NArray,Numeric] min
4227
+ * @param [Numo::NArray,Numeric] max
4228
+ * @return [Numo::NArray] result of clip.
4229
+ *
4230
+ * @example
4231
+ * a = Numo::Int32.new(10).seq
4232
+ * # => Numo::Int32#shape=[10]
4233
+ * # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
4234
+ *
4235
+ * a.clip(1,8)
4236
+ * # => Numo::Int32#shape=[10]
4237
+ * # [1, 1, 2, 3, 4, 5, 6, 7, 8, 8]
4238
+ *
4239
+ * a.inplace.clip(3,6)
4240
+ * a
4241
+ * # => Numo::Int32#shape=[10]
4242
+ * # [3, 3, 3, 3, 4, 5, 6, 6, 6, 6]
4243
+ *
4244
+ * b = Numo::Int32.new(10).seq
4245
+ * # => Numo::Int32#shape=[10]
4246
+ * # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
4247
+ *
4248
+ * b.clip([3,4,1,1,1,4,4,4,4,4], 8)
4249
+ * # => Numo::Int32#shape=[10]
4250
+ * # [3, 4, 2, 3, 4, 5, 6, 7, 8, 8]
4251
+ */
5343
4252
  rb_define_method(cT, "clip", int8_clip, 2);
4253
+ /**
4254
+ * sum of self.
4255
+ * @overload sum(axis:nil, keepdims:false)
4256
+ * @param [Numeric,Array,Range] axis Performs sum along the axis.
4257
+ * @param [TrueClass] keepdims If true, the reduced axes are left in the result array as
4258
+ * dimensions with size one.
4259
+ * @return [Numo::Int8] returns result of sum.
4260
+ */
5344
4261
  rb_define_method(cT, "sum", int8_sum, -1);
4262
+ /**
4263
+ * prod of self.
4264
+ * @overload prod(axis:nil, keepdims:false)
4265
+ * @param [Numeric,Array,Range] axis Performs prod along the axis.
4266
+ * @param [TrueClass] keepdims If true, the reduced axes are left in the result array as
4267
+ * dimensions with size one.
4268
+ * @return [Numo::Int64] returns result of prod.
4269
+ */
5345
4270
  rb_define_method(cT, "prod", int8_prod, -1);
4271
+ /**
4272
+ * min of self.
4273
+ * @overload min(axis:nil, keepdims:false)
4274
+ * @param [Numeric,Array,Range] axis Performs min along the axis.
4275
+ * @param [TrueClass] keepdims If true, the reduced axes are left in the result array as
4276
+ * dimensions with size one.
4277
+ * @return [Numo::Int8] returns result of min.
4278
+ */
5346
4279
  rb_define_method(cT, "min", int8_min, -1);
4280
+ /**
4281
+ * max of self.
4282
+ * @overload max(axis:nil, keepdims:false)
4283
+ * @param [Numeric,Array,Range] axis Performs max along the axis.
4284
+ * @param [TrueClass] keepdims If true, the reduced axes are left in the result array as
4285
+ * dimensions with size one.
4286
+ * @return [Numo::Int8] returns result of max.
4287
+ */
5347
4288
  rb_define_method(cT, "max", int8_max, -1);
4289
+ /**
4290
+ * ptp of self.
4291
+ * @overload ptp(axis:nil, keepdims:false)
4292
+ * @param [Numeric,Array,Range] axis Performs ptp along the axis.
4293
+ * @param [TrueClass] keepdims If true, the reduced axes are left in the result array as
4294
+ * dimensions with size one.
4295
+ * @return [Numo::Int8] returns result of ptp.
4296
+ */
5348
4297
  rb_define_method(cT, "ptp", int8_ptp, -1);
4298
+ /**
4299
+ * Index of the maximum value.
4300
+ * @overload max_index(axis:nil)
4301
+ * @param [Numeric,Array,Range] axis Finds maximum values along the axis and
4302
+ * returns **flat 1-d indices**.
4303
+ * @return [Integer,Numo::Int] returns result indices.
4304
+ * @see #argmax
4305
+ * @see #max
4306
+ *
4307
+ * @example
4308
+ * a = Numo::NArray[3,4,1,2]
4309
+ * a.max_index #=> 1
4310
+ *
4311
+ * b = Numo::NArray[[3,4,1],[2,0,5]]
4312
+ * b.max_index #=> 5
4313
+ * b.max_index(axis:1) #=> [1, 5]
4314
+ * b.max_index(axis:0) #=> [0, 1, 5]
4315
+ * b[b.max_index(axis:0)] #=> [3, 4, 5]
4316
+ */
5349
4317
  rb_define_method(cT, "max_index", int8_max_index, -1);
4318
+ /**
4319
+ * Index of the minimum value.
4320
+ * @overload min_index(axis:nil)
4321
+ * @param [Numeric,Array,Range] axis Finds minimum values along the axis and
4322
+ * returns **flat 1-d indices**.
4323
+ * @return [Integer,Numo::Int] returns result indices.
4324
+ * @see #argmin
4325
+ * @see #min
4326
+ *
4327
+ * @example
4328
+ * a = Numo::NArray[3,4,1,2]
4329
+ * a.min_index #=> 2
4330
+ *
4331
+ * b = Numo::NArray[[3,4,1],[2,0,5]]
4332
+ * b.min_index #=> 4
4333
+ * b.min_index(axis:1) #=> [2, 4]
4334
+ * b.min_index(axis:0) #=> [3, 4, 2]
4335
+ * b[b.min_index(axis:0)] #=> [2, 0, 1]
4336
+ */
5350
4337
  rb_define_method(cT, "min_index", int8_min_index, -1);
4338
+ /**
4339
+ * Index of the maximum value.
4340
+ * @overload argmax(axis:nil)
4341
+ * @param [Numeric,Array,Range] axis Finds maximum values along the axis and
4342
+ * returns **indices along the axis**.
4343
+ * @return [Integer,Numo::Int] returns the result indices.
4344
+ * @see #max_index
4345
+ * @see #max
4346
+ *
4347
+ * @example
4348
+ * a = Numo::NArray[3,4,1,2]
4349
+ * a.argmax #=> 1
4350
+ *
4351
+ * b = Numo::NArray[[3,4,1],[2,0,5]]
4352
+ * b.argmax #=> 5
4353
+ * b.argmax(axis:1) #=> [1, 2]
4354
+ * b.argmax(axis:0) #=> [0, 0, 1]
4355
+ * b.at(b.argmax(axis:0), 0..-1) #=> [3, 4, 5]
4356
+ */
5351
4357
  rb_define_method(cT, "argmax", int8_argmax, -1);
4358
+ /**
4359
+ * Index of the minimum value.
4360
+ * @overload argmin(axis:nil)
4361
+ * @param [Numeric,Array,Range] axis Finds minimum values along the axis and
4362
+ * returns **indices along the axis**.
4363
+ * @return [Integer,Numo::Int] returns the result indices.
4364
+ * @see #min_index
4365
+ * @see #min
4366
+ *
4367
+ * @example
4368
+ * a = Numo::NArray[3,4,1,2]
4369
+ * a.argmin #=> 2
4370
+ *
4371
+ * b = Numo::NArray[[3,4,1],[2,0,5]]
4372
+ * b.argmin #=> 4
4373
+ * b.argmin(axis:1) #=> [2, 1]
4374
+ * b.argmin(axis:0) #=> [1, 1, 0]
4375
+ * b.at(b.argmin(axis:0), 0..-1) #=> [2, 0, 1]
4376
+ */
5352
4377
  rb_define_method(cT, "argmin", int8_argmin, -1);
4378
+ /**
4379
+ * minmax of self.
4380
+ * @overload minmax(axis:nil, keepdims:false)
4381
+ * @param [Numeric,Array,Range] axis Finds min-max along the axis.
4382
+ * @param [TrueClass] keepdims (keyword) If true, the reduced axes are left in
4383
+ * the result array as dimensions with size one.
4384
+ * @return [Numo::Int8,Numo::Int8] min and max of self.
4385
+ */
5353
4386
  rb_define_method(cT, "minmax", int8_minmax, -1);
5354
4387
  /**
5355
4388
  * Element-wise maximum of two arrays.
@@ -5366,12 +4399,82 @@ void Init_numo_int8(void) {
5366
4399
  */
5367
4400
  rb_define_module_function(cT, "minimum", int8_s_minimum, -1);
5368
4401
  rb_define_method(cT, "bincount", int8_bincount, -1);
4402
+ /**
4403
+ * cumsum of self.
4404
+ * @overload cumsum(axis:nil, nan:false)
4405
+ * @param [Numeric,Array,Range] axis Performs cumsum along the axis.
4406
+ * @param [TrueClass] nan If true, apply NaN-aware algorithm (avoid NaN if exists).
4407
+ * @return [Numo::Int8] cumsum of self.
4408
+ */
5369
4409
  rb_define_method(cT, "cumsum", int8_cumsum, -1);
4410
+ /**
4411
+ * cumprod of self.
4412
+ * @overload cumprod(axis:nil, nan:false)
4413
+ * @param [Numeric,Array,Range] axis Performs cumprod along the axis.
4414
+ * @param [TrueClass] nan If true, apply NaN-aware algorithm (avoid NaN if exists).
4415
+ * @return [Numo::Int8] cumprod of self.
4416
+ */
5370
4417
  rb_define_method(cT, "cumprod", int8_cumprod, -1);
4418
+ /**
4419
+ * Binary mulsum.
4420
+ *
4421
+ * @overload mulsum(other, axis:nil, keepdims:false)
4422
+ * @param [Numo::NArray,Numeric] other
4423
+ * @param [Numeric,Array,Range] axis Performs mulsum along the axis.
4424
+ * @param [TrueClass] keepdims (keyword) If true, the reduced axes are left in
4425
+ * the result array as dimensions with size one.
4426
+ * @return [Numo::NArray] mulsum of self and other.
4427
+ */
5371
4428
  rb_define_method(cT, "mulsum", int8_mulsum, -1);
4429
+ /**
4430
+ * Set linear sequence of numbers to self. The sequence is obtained from
4431
+ * beg+i*step
4432
+ * where i is 1-dimensional index.
4433
+ * @overload seq([beg,[step]])
4434
+ * @param [Numeric] beg beginning of sequence. (default=0)
4435
+ * @param [Numeric] step step of sequence. (default=1)
4436
+ * @return [Numo::Int8] self.
4437
+ * @example
4438
+ * Numo::DFloat.new(6).seq(1,-0.2)
4439
+ * # => Numo::DFloat#shape=[6]
4440
+ * # [1, 0.8, 0.6, 0.4, 0.2, 0]
4441
+ *
4442
+ * Numo::DComplex.new(6).seq(1,-0.2+0.2i)
4443
+ * # => Numo::DComplex#shape=[6]
4444
+ * # [1+0i, 0.8+0.2i, 0.6+0.4i, 0.4+0.6i, 0.2+0.8i, 0+1i]
4445
+ */
5372
4446
  rb_define_method(cT, "seq", int8_seq, -1);
4447
+ /**
4448
+ * Eye: Set a value to diagonal components, set 0 to non-diagonal components.
4449
+ * @overload eye([element,offset])
4450
+ * @param [Numeric] element Diagonal element to be stored. Default is 1.
4451
+ * @param [Integer] offset Diagonal offset from the main diagonal. The
4452
+ * default is 0. k>0 for diagonals above the main diagonal, and k<0
4453
+ * for diagonals below the main diagonal.
4454
+ * @return [Numo::Int8] eye of self.
4455
+ */
5373
4456
  rb_define_method(cT, "eye", int8_eye, -1);
5374
4457
  rb_define_alias(cT, "indgen", "seq");
4458
+ /**
4459
+ * Generate uniformly distributed random numbers on self narray.
4460
+ * @overload rand([[low],high])
4461
+ * @param [Numeric] low lower inclusive boundary of random numbers. (default=0)
4462
+ * @param [Numeric] high upper exclusive boundary of random numbers.
4463
+ * (default=1 or 1+1i for complex types)
4464
+ * @return [Numo::Int8] self.
4465
+ * @example
4466
+ * Numo::DFloat.new(6).rand
4467
+ * # => Numo::DFloat#shape=[6]
4468
+ * # [0.0617545, 0.373067, 0.794815, 0.201042, 0.116041, 0.344032]
4469
+ *
4470
+ * Numo::DComplex.new(6).rand(5+5i)
4471
+ * # => Numo::DComplex#shape=[6]
4472
+ * # [2.69974+3.68908i, 0.825443+0.254414i, 0.540323+0.34354i, 4.52061+2.39322i, ...]
4473
+ *
4474
+ * Numo::Int32.new(6).rand(2,5)
4475
+ * # => Numo::Int32#shape=[6]
4476
+ * # [4, 3, 3, 2, 4, 2]
4477
+ */
5375
4478
  rb_define_method(cT, "rand", int8_rand, -1);
5376
4479
  rb_define_method(cT, "poly", int8_poly, -2);
5377
4480