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 u_int8_t uint8; // Type aliases for shorter notation
52
71
  // following the codebase naming convention.
72
+ DEF_NARRAY_CLIP_METHOD_FUNC(uint8, numo_cUInt8)
73
+ DEF_NARRAY_INT_SUM_METHOD_FUNC(uint8, numo_cUInt8, u_int64_t, numo_cUInt64)
74
+ DEF_NARRAY_INT_PROD_METHOD_FUNC(uint8, numo_cUInt8, u_int64_t, numo_cUInt64)
75
+ DEF_NARRAY_INT_MIN_METHOD_FUNC(uint8, numo_cUInt8)
76
+ DEF_NARRAY_INT_MAX_METHOD_FUNC(uint8, numo_cUInt8)
77
+ DEF_NARRAY_INT_PTP_METHOD_FUNC(uint8, numo_cUInt8)
78
+ DEF_NARRAY_INT_MAX_INDEX_METHOD_FUNC(uint8)
79
+ DEF_NARRAY_INT_MIN_INDEX_METHOD_FUNC(uint8)
80
+ DEF_NARRAY_INT_ARGMAX_METHOD_FUNC(uint8)
81
+ DEF_NARRAY_INT_ARGMIN_METHOD_FUNC(uint8)
82
+ DEF_NARRAY_INT_MAXIMUM_METHOD_FUNC(uint8, numo_cUInt8)
83
+ DEF_NARRAY_INT_MINIMUM_METHOD_FUNC(uint8, numo_cUInt8)
84
+ DEF_NARRAY_INT_MINMAX_METHOD_FUNC(uint8, numo_cUInt8)
85
+ DEF_NARRAY_INT_CUMSUM_METHOD_FUNC(uint8, numo_cUInt8)
86
+ DEF_NARRAY_INT_CUMPROD_METHOD_FUNC(uint8, numo_cUInt8)
87
+ DEF_NARRAY_INT_MULSUM_METHOD_FUNC(uint8, numo_cUInt8)
88
+ DEF_NARRAY_INT_SEQ_METHOD_FUNC(uint8)
89
+ DEF_NARRAY_EYE_METHOD_FUNC(uint8)
90
+ DEF_NARRAY_INT_RAND_METHOD_FUNC(uint8)
53
91
  DEF_NARRAY_INT_MEAN_METHOD_FUNC(uint8, numo_cUInt8)
54
92
  DEF_NARRAY_INT_VAR_METHOD_FUNC(uint8, numo_cUInt8)
55
93
  DEF_NARRAY_INT_STDDEV_METHOD_FUNC(uint8, numo_cUInt8)
@@ -3276,710 +3314,6 @@ static VALUE uint8_le(VALUE self, VALUE other) {
3276
3314
  }
3277
3315
  }
3278
3316
 
3279
- static void iter_uint8_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_uint8_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_uint8_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 uint8_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_uint8_clip_min, STRIDE_LOOP, 2, 1, ain, aout };
3378
- ndfunc_t ndf_max = { iter_uint8_clip_max, STRIDE_LOOP, 2, 1, ain, aout };
3379
- ndfunc_t ndf_both = { iter_uint8_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_uint8_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
- *(u_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::UInt8] returns result of sum.
3415
- */
3416
- static VALUE uint8_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_cUInt64, 0 } };
3420
- ndfunc_t ndf = { iter_uint8_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_uint8_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
- *(u_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::UInt8] returns result of prod.
3448
- */
3449
- static VALUE uint8_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_cUInt64, 0 } };
3453
- ndfunc_t ndf = { iter_uint8_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_uint8_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::UInt8] returns result of min.
3481
- */
3482
- static VALUE uint8_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_uint8_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 uint8_extract(v);
3493
- }
3494
-
3495
- static void iter_uint8_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::UInt8] returns result of max.
3514
- */
3515
- static VALUE uint8_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_uint8_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 uint8_extract(v);
3526
- }
3527
-
3528
- static void iter_uint8_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::UInt8] returns result of ptp.
3547
- */
3548
- static VALUE uint8_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_uint8_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 uint8_extract(v);
3559
- }
3560
-
3561
- #define idx_t int64_t
3562
- static void iter_uint8_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_uint8_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 uint8_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_uint8_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_uint8_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_uint8_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_uint8_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 uint8_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_uint8_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_uint8_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_uint8_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_uint8_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 uint8_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_uint8_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_uint8_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_uint8_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_uint8_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 uint8_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_uint8_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_uint8_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_uint8_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::UInt8,Numo::UInt8] min and max of self.
3905
- */
3906
- static VALUE uint8_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_uint8_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_uint8_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 uint8_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_uint8_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_uint8_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 uint8_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_uint8_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_uint8_bincount_32(na_loop_t* const lp) {
@@ -4209,463 +3543,6 @@ static VALUE uint8_bincount(int argc, VALUE* argv, VALUE self) {
4209
3543
  }
4210
3544
  }
4211
3545
 
4212
- static void iter_uint8_cumsum(na_loop_t* const lp) {
4213
- size_t i;
4214
- char *p1, *p2;
4215
- ssize_t s1, s2;
4216
- dtype x, y;
4217
-
4218
- INIT_COUNTER(lp, i);
4219
- INIT_PTR(lp, 0, p1, s1);
4220
- INIT_PTR(lp, 1, p2, s2);
4221
-
4222
- GET_DATA_STRIDE(p1, s1, dtype, x);
4223
- SET_DATA_STRIDE(p2, s2, dtype, x);
4224
- for (i--; i--;) {
4225
- GET_DATA_STRIDE(p1, s1, dtype, y);
4226
- m_cumsum(x, y);
4227
- SET_DATA_STRIDE(p2, s2, dtype, x);
4228
- }
4229
- }
4230
-
4231
- /*
4232
- cumsum of self.
4233
- @overload cumsum(axis:nil, nan:false)
4234
- @param [Numeric,Array,Range] axis Performs cumsum along the axis.
4235
- @param [TrueClass] nan If true, apply NaN-aware algorithm (avoid NaN if exists).
4236
- @return [Numo::UInt8] cumsum of self.
4237
- */
4238
- static VALUE uint8_cumsum(int argc, VALUE* argv, VALUE self) {
4239
- VALUE reduce;
4240
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { sym_reduce, 0 } };
4241
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
4242
- ndfunc_t ndf = {
4243
- iter_uint8_cumsum, STRIDE_LOOP | NDF_FLAT_REDUCE | NDF_CUM, 2, 1, ain, aout
4244
- };
4245
-
4246
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
4247
-
4248
- return na_ndloop(&ndf, 2, self, reduce);
4249
- }
4250
-
4251
- static void iter_uint8_cumprod(na_loop_t* const lp) {
4252
- size_t i;
4253
- char *p1, *p2;
4254
- ssize_t s1, s2;
4255
- dtype x, y;
4256
-
4257
- INIT_COUNTER(lp, i);
4258
- INIT_PTR(lp, 0, p1, s1);
4259
- INIT_PTR(lp, 1, p2, s2);
4260
-
4261
- GET_DATA_STRIDE(p1, s1, dtype, x);
4262
- SET_DATA_STRIDE(p2, s2, dtype, x);
4263
- for (i--; i--;) {
4264
- GET_DATA_STRIDE(p1, s1, dtype, y);
4265
- m_cumprod(x, y);
4266
- SET_DATA_STRIDE(p2, s2, dtype, x);
4267
- }
4268
- }
4269
-
4270
- /*
4271
- cumprod of self.
4272
- @overload cumprod(axis:nil, nan:false)
4273
- @param [Numeric,Array,Range] axis Performs cumprod along the axis.
4274
- @param [TrueClass] nan If true, apply NaN-aware algorithm (avoid NaN if exists).
4275
- @return [Numo::UInt8] cumprod of self.
4276
- */
4277
- static VALUE uint8_cumprod(int argc, VALUE* argv, VALUE self) {
4278
- VALUE reduce;
4279
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { sym_reduce, 0 } };
4280
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
4281
- ndfunc_t ndf = {
4282
- iter_uint8_cumprod, STRIDE_LOOP | NDF_FLAT_REDUCE | NDF_CUM, 2, 1, ain, aout
4283
- };
4284
-
4285
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
4286
-
4287
- return na_ndloop(&ndf, 2, self, reduce);
4288
- }
4289
-
4290
- //
4291
- static void iter_uint8_mulsum(na_loop_t* const lp) {
4292
- size_t i, n;
4293
- char *p1, *p2, *p3;
4294
- ssize_t s1, s2, s3;
4295
-
4296
- INIT_COUNTER(lp, n);
4297
- INIT_PTR(lp, 0, p1, s1);
4298
- INIT_PTR(lp, 1, p2, s2);
4299
- INIT_PTR(lp, 2, p3, s3);
4300
-
4301
- if (s3 == 0) {
4302
- dtype z;
4303
- // Reduce loop
4304
- GET_DATA(p3, dtype, z);
4305
- for (i = 0; i < n; i++) {
4306
- dtype x, y;
4307
- GET_DATA_STRIDE(p1, s1, dtype, x);
4308
- GET_DATA_STRIDE(p2, s2, dtype, y);
4309
- m_mulsum(x, y, z);
4310
- }
4311
- SET_DATA(p3, dtype, z);
4312
- return;
4313
- } else {
4314
- for (i = 0; i < n; i++) {
4315
- dtype x, y, z;
4316
- GET_DATA_STRIDE(p1, s1, dtype, x);
4317
- GET_DATA_STRIDE(p2, s2, dtype, y);
4318
- GET_DATA(p3, dtype, z);
4319
- m_mulsum(x, y, z);
4320
- SET_DATA_STRIDE(p3, s3, dtype, z);
4321
- }
4322
- }
4323
- }
4324
- //
4325
-
4326
- static VALUE uint8_mulsum_self(int argc, VALUE* argv, VALUE self) {
4327
- VALUE v, reduce;
4328
- VALUE naryv[2];
4329
- ndfunc_arg_in_t ain[4] = { { cT, 0 }, { cT, 0 }, { sym_reduce, 0 }, { sym_init, 0 } };
4330
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
4331
- ndfunc_t ndf = { iter_uint8_mulsum, STRIDE_LOOP_NIP, 4, 1, ain, aout };
4332
-
4333
- if (argc < 1) {
4334
- rb_raise(rb_eArgError, "wrong number of arguments (%d for >=1)", argc);
4335
- }
4336
- // should fix below: [self.ndim,other.ndim].max or?
4337
- naryv[0] = self;
4338
- naryv[1] = argv[0];
4339
- //
4340
- reduce = na_reduce_dimension(argc - 1, argv + 1, 2, naryv, &ndf, 0);
4341
- //
4342
-
4343
- v = na_ndloop(&ndf, 4, self, argv[0], reduce, m_mulsum_init);
4344
- return uint8_extract(v);
4345
- }
4346
-
4347
- /*
4348
- Binary mulsum.
4349
-
4350
- @overload mulsum(other, axis:nil, keepdims:false)
4351
- @param [Numo::NArray,Numeric] other
4352
- @param [Numeric,Array,Range] axis Performs mulsum along the axis.
4353
- @param [TrueClass] keepdims (keyword) If true, the reduced axes are left in the result array
4354
- as dimensions with size one.
4355
- @return [Numo::NArray] mulsum of self and other.
4356
- */
4357
- static VALUE uint8_mulsum(int argc, VALUE* argv, VALUE self) {
4358
- //
4359
- VALUE klass, v;
4360
- //
4361
- if (argc < 1) {
4362
- rb_raise(rb_eArgError, "wrong number of arguments (%d for >=1)", argc);
4363
- }
4364
- //
4365
- klass = na_upcast(rb_obj_class(self), rb_obj_class(argv[0]));
4366
- if (klass == cT) {
4367
- return uint8_mulsum_self(argc, argv, self);
4368
- } else {
4369
- v = rb_funcall(klass, id_cast, 1, self);
4370
- //
4371
- return rb_funcallv_kw(v, rb_intern("mulsum"), argc, argv, RB_PASS_CALLED_KEYWORDS);
4372
- //
4373
- }
4374
- //
4375
- }
4376
-
4377
- typedef double seq_data_t;
4378
-
4379
- typedef double seq_count_t;
4380
-
4381
- typedef struct {
4382
- seq_data_t beg;
4383
- seq_data_t step;
4384
- seq_count_t count;
4385
- } seq_opt_t;
4386
-
4387
- static void iter_uint8_seq(na_loop_t* const lp) {
4388
- size_t i;
4389
- char* p1;
4390
- ssize_t s1;
4391
- size_t* idx1;
4392
- dtype x;
4393
- seq_data_t beg, step;
4394
- seq_count_t c;
4395
- seq_opt_t* g;
4396
-
4397
- INIT_COUNTER(lp, i);
4398
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
4399
- g = (seq_opt_t*)(lp->opt_ptr);
4400
- beg = g->beg;
4401
- step = g->step;
4402
- c = g->count;
4403
- if (idx1) {
4404
- for (; i--;) {
4405
- x = f_seq(beg, step, c++);
4406
- *(dtype*)(p1 + *idx1) = x;
4407
- idx1++;
4408
- }
4409
- } else {
4410
- for (; i--;) {
4411
- x = f_seq(beg, step, c++);
4412
- *(dtype*)(p1) = x;
4413
- p1 += s1;
4414
- }
4415
- }
4416
- g->count = c;
4417
- }
4418
-
4419
- /*
4420
- Set linear sequence of numbers to self. The sequence is obtained from
4421
- beg+i*step
4422
- where i is 1-dimensional index.
4423
- @overload seq([beg,[step]])
4424
- @param [Numeric] beg beginning of sequence. (default=0)
4425
- @param [Numeric] step step of sequence. (default=1)
4426
- @return [Numo::UInt8] self.
4427
- @example
4428
- Numo::DFloat.new(6).seq(1,-0.2)
4429
- # => Numo::DFloat#shape=[6]
4430
- # [1, 0.8, 0.6, 0.4, 0.2, 0]
4431
-
4432
- Numo::DComplex.new(6).seq(1,-0.2+0.2i)
4433
- # => Numo::DComplex#shape=[6]
4434
- # [1+0i, 0.8+0.2i, 0.6+0.4i, 0.4+0.6i, 0.2+0.8i, 0+1i]
4435
- */
4436
- static VALUE uint8_seq(int argc, VALUE* argv, VALUE self) {
4437
- seq_opt_t* g;
4438
- VALUE vbeg = Qnil, vstep = Qnil;
4439
- ndfunc_arg_in_t ain[1] = { { OVERWRITE, 0 } };
4440
- ndfunc_t ndf = { iter_uint8_seq, FULL_LOOP, 1, 0, ain, 0 };
4441
-
4442
- g = ALLOCA_N(seq_opt_t, 1);
4443
- g->beg = m_zero;
4444
- g->step = m_one;
4445
- g->count = 0;
4446
- rb_scan_args(argc, argv, "02", &vbeg, &vstep);
4447
- if (vbeg != Qnil) {
4448
- g->beg = NUM2DBL(vbeg);
4449
- }
4450
- if (vstep != Qnil) {
4451
- g->step = NUM2DBL(vstep);
4452
- }
4453
-
4454
- na_ndloop3(&ndf, g, 1, self);
4455
- return self;
4456
- }
4457
-
4458
- static void iter_uint8_eye(na_loop_t* const lp) {
4459
- size_t n0, n1;
4460
- size_t i0, i1;
4461
- ssize_t s0, s1;
4462
- char *p0, *p1;
4463
- char* g;
4464
- ssize_t kofs;
4465
- dtype data;
4466
-
4467
- g = (char*)(lp->opt_ptr);
4468
- kofs = *(ssize_t*)g;
4469
- data = *(dtype*)(g + sizeof(ssize_t));
4470
-
4471
- n0 = lp->args[0].shape[0];
4472
- n1 = lp->args[0].shape[1];
4473
- s0 = lp->args[0].iter[0].step;
4474
- s1 = lp->args[0].iter[1].step;
4475
- p0 = NDL_PTR(lp, 0);
4476
-
4477
- for (i0 = 0; i0 < n0; i0++) {
4478
- p1 = p0;
4479
- for (i1 = 0; i1 < n1; i1++) {
4480
- *(dtype*)p1 = (i0 + kofs == i1) ? data : m_zero;
4481
- p1 += s1;
4482
- }
4483
- p0 += s0;
4484
- }
4485
- }
4486
-
4487
- /*
4488
- Eye: Set a value to diagonal components, set 0 to non-diagonal components.
4489
- @overload eye([element,offset])
4490
- @param [Numeric] element Diagonal element to be stored. Default is 1.
4491
- @param [Integer] offset Diagonal offset from the main diagonal. The
4492
- default is 0. k>0 for diagonals above the main diagonal, and k<0
4493
- for diagonals below the main diagonal.
4494
- @return [Numo::UInt8] eye of self.
4495
- */
4496
- static VALUE uint8_eye(int argc, VALUE* argv, VALUE self) {
4497
- ndfunc_arg_in_t ain[1] = { { OVERWRITE, 2 } };
4498
- ndfunc_t ndf = { iter_uint8_eye, NO_LOOP, 1, 0, ain, 0 };
4499
- ssize_t kofs;
4500
- dtype data;
4501
- char* g;
4502
- int nd;
4503
- narray_t* na;
4504
-
4505
- // check arguments
4506
- if (argc > 2) {
4507
- rb_raise(rb_eArgError, "too many arguments (%d for 0..2)", argc);
4508
- } else if (argc == 2) {
4509
- data = m_num_to_data(argv[0]);
4510
- kofs = NUM2SSIZET(argv[1]);
4511
- } else if (argc == 1) {
4512
- data = m_num_to_data(argv[0]);
4513
- kofs = 0;
4514
- } else {
4515
- data = m_one;
4516
- kofs = 0;
4517
- }
4518
-
4519
- GetNArray(self, na);
4520
- nd = na->ndim;
4521
- if (nd < 2) {
4522
- rb_raise(nary_eDimensionError, "less than 2-d array");
4523
- }
4524
-
4525
- // Diagonal offset from the main diagonal.
4526
- if (kofs >= 0) {
4527
- if ((size_t)(kofs) >= na->shape[nd - 1]) {
4528
- rb_raise(
4529
- rb_eArgError,
4530
- "invalid diagonal offset(%" SZF "d) for "
4531
- "last dimension size(%" SZF "d)",
4532
- kofs, na->shape[nd - 1]
4533
- );
4534
- }
4535
- } else {
4536
- if ((size_t)(-kofs) >= na->shape[nd - 2]) {
4537
- rb_raise(
4538
- rb_eArgError,
4539
- "invalid diagonal offset(%" SZF "d) for "
4540
- "last-1 dimension size(%" SZF "d)",
4541
- kofs, na->shape[nd - 2]
4542
- );
4543
- }
4544
- }
4545
-
4546
- g = ALLOCA_N(char, sizeof(ssize_t) + sizeof(dtype));
4547
- *(ssize_t*)g = kofs;
4548
- *(dtype*)(g + sizeof(ssize_t)) = data;
4549
-
4550
- na_ndloop3(&ndf, g, 1, self);
4551
- return self;
4552
- }
4553
-
4554
- #define HWID (sizeof(dtype) * 4)
4555
-
4556
- static int msb_pos(uint32_t a) {
4557
- int width = HWID;
4558
- int pos = 0;
4559
- uint32_t mask = (((dtype)1 << HWID) - 1) << HWID;
4560
-
4561
- if (a == 0) {
4562
- return -1;
4563
- }
4564
-
4565
- while (width) {
4566
- if (a & mask) {
4567
- pos += width;
4568
- } else {
4569
- mask >>= width;
4570
- }
4571
- width >>= 1;
4572
- mask &= mask << width;
4573
- }
4574
- return pos;
4575
- }
4576
-
4577
- /* generates a random number on [0,max) */
4578
- inline static dtype m_rand(uint32_t max, int shift) {
4579
- uint32_t x;
4580
- do {
4581
- x = gen_rand32();
4582
- x >>= shift;
4583
- } while (x >= max);
4584
- return x;
4585
- }
4586
-
4587
- typedef struct {
4588
- dtype low;
4589
- uint32_t max;
4590
- } rand_opt_t;
4591
-
4592
- static void iter_uint8_rand(na_loop_t* const lp) {
4593
- size_t i;
4594
- char* p1;
4595
- ssize_t s1;
4596
- size_t* idx1;
4597
- dtype x;
4598
- rand_opt_t* g;
4599
- dtype low;
4600
- uint32_t max;
4601
- int shift;
4602
-
4603
- INIT_COUNTER(lp, i);
4604
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
4605
- g = (rand_opt_t*)(lp->opt_ptr);
4606
- low = g->low;
4607
- max = g->max;
4608
- shift = 31 - msb_pos(max);
4609
-
4610
- if (idx1) {
4611
- for (; i--;) {
4612
- x = m_add(m_rand(max, shift), low);
4613
- SET_DATA_INDEX(p1, idx1, dtype, x);
4614
- }
4615
- } else {
4616
- for (; i--;) {
4617
- x = m_add(m_rand(max, shift), low);
4618
- SET_DATA_STRIDE(p1, s1, dtype, x);
4619
- }
4620
- }
4621
- }
4622
-
4623
- /*
4624
- Generate uniformly distributed random numbers on self narray.
4625
- @overload rand([[low],high])
4626
- @param [Numeric] low lower inclusive boundary of random numbers. (default=0)
4627
- @param [Numeric] high upper exclusive boundary of random numbers. (default=1 or 1+1i for
4628
- complex types)
4629
- @return [Numo::UInt8] self.
4630
- @example
4631
- Numo::DFloat.new(6).rand
4632
- # => Numo::DFloat#shape=[6]
4633
- # [0.0617545, 0.373067, 0.794815, 0.201042, 0.116041, 0.344032]
4634
-
4635
- Numo::DComplex.new(6).rand(5+5i)
4636
- # => Numo::DComplex#shape=[6]
4637
- # [2.69974+3.68908i, 0.825443+0.254414i, 0.540323+0.34354i, 4.52061+2.39322i, ...]
4638
-
4639
- Numo::Int32.new(6).rand(2,5)
4640
- # => Numo::Int32#shape=[6]
4641
- # [4, 3, 3, 2, 4, 2]
4642
- */
4643
- static VALUE uint8_rand(int argc, VALUE* argv, VALUE self) {
4644
- rand_opt_t g;
4645
- VALUE v1 = Qnil, v2 = Qnil;
4646
- dtype high;
4647
- ndfunc_arg_in_t ain[1] = { { OVERWRITE, 0 } };
4648
- ndfunc_t ndf = { iter_uint8_rand, FULL_LOOP, 1, 0, ain, 0 };
4649
-
4650
- rb_scan_args(argc, argv, "11", &v1, &v2);
4651
- if (v2 == Qnil) {
4652
- g.low = m_zero;
4653
- g.max = high = m_num_to_data(v1);
4654
-
4655
- } else {
4656
- g.low = m_num_to_data(v1);
4657
- high = m_num_to_data(v2);
4658
- g.max = m_sub(high, g.low);
4659
- }
4660
-
4661
- if (high <= g.low) {
4662
- rb_raise(rb_eArgError, "high must be larger than low");
4663
- }
4664
-
4665
- na_ndloop3(&ndf, &g, 1, self);
4666
- return self;
4667
- }
4668
-
4669
3546
  static void iter_uint8_poly(na_loop_t* const lp) {
4670
3547
  size_t i;
4671
3548
  dtype x, y, a;
@@ -5338,16 +4215,170 @@ void Init_numo_uint8(void) {
5338
4215
  rb_define_alias(cT, ">=", "ge");
5339
4216
  rb_define_alias(cT, "<", "lt");
5340
4217
  rb_define_alias(cT, "<=", "le");
4218
+ /**
4219
+ * Clip array elements by [min,max].
4220
+ * If either of min or max is nil, one side is clipped.
4221
+ * @overload clip(min,max)
4222
+ * @param [Numo::NArray,Numeric] min
4223
+ * @param [Numo::NArray,Numeric] max
4224
+ * @return [Numo::NArray] result of clip.
4225
+ *
4226
+ * @example
4227
+ * a = Numo::Int32.new(10).seq
4228
+ * # => Numo::Int32#shape=[10]
4229
+ * # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
4230
+ *
4231
+ * a.clip(1,8)
4232
+ * # => Numo::Int32#shape=[10]
4233
+ * # [1, 1, 2, 3, 4, 5, 6, 7, 8, 8]
4234
+ *
4235
+ * a.inplace.clip(3,6)
4236
+ * a
4237
+ * # => Numo::Int32#shape=[10]
4238
+ * # [3, 3, 3, 3, 4, 5, 6, 6, 6, 6]
4239
+ *
4240
+ * b = Numo::Int32.new(10).seq
4241
+ * # => Numo::Int32#shape=[10]
4242
+ * # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
4243
+ *
4244
+ * b.clip([3,4,1,1,1,4,4,4,4,4], 8)
4245
+ * # => Numo::Int32#shape=[10]
4246
+ * # [3, 4, 2, 3, 4, 5, 6, 7, 8, 8]
4247
+ */
5341
4248
  rb_define_method(cT, "clip", uint8_clip, 2);
4249
+ /**
4250
+ * sum of self.
4251
+ * @overload sum(axis:nil, keepdims:false)
4252
+ * @param [Numeric,Array,Range] axis Performs sum along the axis.
4253
+ * @param [TrueClass] keepdims If true, the reduced axes are left in the result array as
4254
+ * dimensions with size one.
4255
+ * @return [Numo::UInt64] returns result of sum.
4256
+ */
5342
4257
  rb_define_method(cT, "sum", uint8_sum, -1);
4258
+ /**
4259
+ * prod of self.
4260
+ * @overload prod(axis:nil, keepdims:false)
4261
+ * @param [Numeric,Array,Range] axis Performs prod along the axis.
4262
+ * @param [TrueClass] keepdims If true, the reduced axes are left in the result array as
4263
+ * dimensions with size one.
4264
+ * @return [Numo::UInt64] returns result of prod.
4265
+ */
5343
4266
  rb_define_method(cT, "prod", uint8_prod, -1);
4267
+ /**
4268
+ * min of self.
4269
+ * @overload min(axis:nil, keepdims:false)
4270
+ * @param [Numeric,Array,Range] axis Performs min along the axis.
4271
+ * @param [TrueClass] keepdims If true, the reduced axes are left in the result array as
4272
+ * dimensions with size one.
4273
+ * @return [Numo::UInt8] returns result of min.
4274
+ */
5344
4275
  rb_define_method(cT, "min", uint8_min, -1);
4276
+ /**
4277
+ * max of self.
4278
+ * @overload max(axis:nil, keepdims:false)
4279
+ * @param [Numeric,Array,Range] axis Performs max along the axis.
4280
+ * @param [TrueClass] keepdims If true, the reduced axes are left in the result array as
4281
+ * dimensions with size one.
4282
+ * @return [Numo::UInt8] returns result of max.
4283
+ */
5345
4284
  rb_define_method(cT, "max", uint8_max, -1);
4285
+ /**
4286
+ * ptp of self.
4287
+ * @overload ptp(axis:nil, keepdims:false)
4288
+ * @param [Numeric,Array,Range] axis Performs ptp along the axis.
4289
+ * @param [TrueClass] keepdims If true, the reduced axes are left in the result array as
4290
+ * dimensions with size one.
4291
+ * @return [Numo::UInt8] returns result of ptp.
4292
+ */
5346
4293
  rb_define_method(cT, "ptp", uint8_ptp, -1);
4294
+ /**
4295
+ * Index of the maximum value.
4296
+ * @overload max_index(axis:nil)
4297
+ * @param [Numeric,Array,Range] axis Finds maximum values along the axis and
4298
+ * returns **flat 1-d indices**.
4299
+ * @return [Integer,Numo::Int] returns result indices.
4300
+ * @see #argmax
4301
+ * @see #max
4302
+ *
4303
+ * @example
4304
+ * a = Numo::NArray[3,4,1,2]
4305
+ * a.max_index #=> 1
4306
+ *
4307
+ * b = Numo::NArray[[3,4,1],[2,0,5]]
4308
+ * b.max_index #=> 5
4309
+ * b.max_index(axis:1) #=> [1, 5]
4310
+ * b.max_index(axis:0) #=> [0, 1, 5]
4311
+ * b[b.max_index(axis:0)] #=> [3, 4, 5]
4312
+ */
5347
4313
  rb_define_method(cT, "max_index", uint8_max_index, -1);
4314
+ /**
4315
+ * Index of the minimum value.
4316
+ * @overload min_index(axis:nil)
4317
+ * @param [Numeric,Array,Range] axis Finds minimum values along the axis and
4318
+ * returns **flat 1-d indices**.
4319
+ * @return [Integer,Numo::Int] returns result indices.
4320
+ * @see #argmin
4321
+ * @see #min
4322
+ *
4323
+ * @example
4324
+ * a = Numo::NArray[3,4,1,2]
4325
+ * a.min_index #=> 2
4326
+ *
4327
+ * b = Numo::NArray[[3,4,1],[2,0,5]]
4328
+ * b.min_index #=> 4
4329
+ * b.min_index(axis:1) #=> [2, 4]
4330
+ * b.min_index(axis:0) #=> [3, 4, 2]
4331
+ * b[b.min_index(axis:0)] #=> [2, 0, 1]
4332
+ */
5348
4333
  rb_define_method(cT, "min_index", uint8_min_index, -1);
4334
+ /**
4335
+ * Index of the maximum value.
4336
+ * @overload argmax(axis:nil)
4337
+ * @param [Numeric,Array,Range] axis Finds maximum values along the axis and
4338
+ * returns **indices along the axis**.
4339
+ * @return [Integer,Numo::Int] returns the result indices.
4340
+ * @see #max_index
4341
+ * @see #max
4342
+ *
4343
+ * @example
4344
+ * a = Numo::NArray[3,4,1,2]
4345
+ * a.argmax #=> 1
4346
+ *
4347
+ * b = Numo::NArray[[3,4,1],[2,0,5]]
4348
+ * b.argmax #=> 5
4349
+ * b.argmax(axis:1) #=> [1, 2]
4350
+ * b.argmax(axis:0) #=> [0, 0, 1]
4351
+ * b.at(b.argmax(axis:0), 0..-1) #=> [3, 4, 5]
4352
+ */
5349
4353
  rb_define_method(cT, "argmax", uint8_argmax, -1);
4354
+ /**
4355
+ * Index of the minimum value.
4356
+ * @overload argmin(axis:nil)
4357
+ * @param [Numeric,Array,Range] axis Finds minimum values along the axis and
4358
+ * returns **indices along the axis**.
4359
+ * @return [Integer,Numo::Int] returns the result indices.
4360
+ * @see #min_index
4361
+ * @see #min
4362
+ *
4363
+ * @example
4364
+ * a = Numo::NArray[3,4,1,2]
4365
+ * a.argmin #=> 2
4366
+ *
4367
+ * b = Numo::NArray[[3,4,1],[2,0,5]]
4368
+ * b.argmin #=> 4
4369
+ * b.argmin(axis:1) #=> [2, 1]
4370
+ * b.argmin(axis:0) #=> [1, 1, 0]
4371
+ * b.at(b.argmin(axis:0), 0..-1) #=> [2, 0, 1]
4372
+ */
5350
4373
  rb_define_method(cT, "argmin", uint8_argmin, -1);
4374
+ /**
4375
+ * minmax of self.
4376
+ * @overload minmax(axis:nil, keepdims:false)
4377
+ * @param [Numeric,Array,Range] axis Finds min-max along the axis.
4378
+ * @param [TrueClass] keepdims (keyword) If true, the reduced axes are left in
4379
+ * the result array as dimensions with size one.
4380
+ * @return [Numo::UInt8,Numo::UInt8] min and max of self.
4381
+ */
5351
4382
  rb_define_method(cT, "minmax", uint8_minmax, -1);
5352
4383
  /**
5353
4384
  * Element-wise maximum of two arrays.
@@ -5364,12 +4395,82 @@ void Init_numo_uint8(void) {
5364
4395
  */
5365
4396
  rb_define_module_function(cT, "minimum", uint8_s_minimum, -1);
5366
4397
  rb_define_method(cT, "bincount", uint8_bincount, -1);
4398
+ /**
4399
+ * cumsum of self.
4400
+ * @overload cumsum(axis:nil, nan:false)
4401
+ * @param [Numeric,Array,Range] axis Performs cumsum along the axis.
4402
+ * @param [TrueClass] nan If true, apply NaN-aware algorithm (avoid NaN if exists).
4403
+ * @return [Numo::UInt8] cumsum of self.
4404
+ */
5367
4405
  rb_define_method(cT, "cumsum", uint8_cumsum, -1);
4406
+ /**
4407
+ * cumprod of self.
4408
+ * @overload cumprod(axis:nil, nan:false)
4409
+ * @param [Numeric,Array,Range] axis Performs cumprod along the axis.
4410
+ * @param [TrueClass] nan If true, apply NaN-aware algorithm (avoid NaN if exists).
4411
+ * @return [Numo::UInt8] cumprod of self.
4412
+ */
5368
4413
  rb_define_method(cT, "cumprod", uint8_cumprod, -1);
4414
+ /**
4415
+ * Binary mulsum.
4416
+ *
4417
+ * @overload mulsum(other, axis:nil, keepdims:false)
4418
+ * @param [Numo::NArray,Numeric] other
4419
+ * @param [Numeric,Array,Range] axis Performs mulsum along the axis.
4420
+ * @param [TrueClass] keepdims (keyword) If true, the reduced axes are left in
4421
+ * the result array as dimensions with size one.
4422
+ * @return [Numo::NArray] mulsum of self and other.
4423
+ */
5369
4424
  rb_define_method(cT, "mulsum", uint8_mulsum, -1);
4425
+ /**
4426
+ * Set linear sequence of numbers to self. The sequence is obtained from
4427
+ * beg+i*step
4428
+ * where i is 1-dimensional index.
4429
+ * @overload seq([beg,[step]])
4430
+ * @param [Numeric] beg beginning of sequence. (default=0)
4431
+ * @param [Numeric] step step of sequence. (default=1)
4432
+ * @return [Numo::UInt8] self.
4433
+ * @example
4434
+ * Numo::DFloat.new(6).seq(1,-0.2)
4435
+ * # => Numo::DFloat#shape=[6]
4436
+ * # [1, 0.8, 0.6, 0.4, 0.2, 0]
4437
+ *
4438
+ * Numo::DComplex.new(6).seq(1,-0.2+0.2i)
4439
+ * # => Numo::DComplex#shape=[6]
4440
+ * # [1+0i, 0.8+0.2i, 0.6+0.4i, 0.4+0.6i, 0.2+0.8i, 0+1i]
4441
+ */
5370
4442
  rb_define_method(cT, "seq", uint8_seq, -1);
4443
+ /**
4444
+ * Eye: Set a value to diagonal components, set 0 to non-diagonal components.
4445
+ * @overload eye([element,offset])
4446
+ * @param [Numeric] element Diagonal element to be stored. Default is 1.
4447
+ * @param [Integer] offset Diagonal offset from the main diagonal. The
4448
+ * default is 0. k>0 for diagonals above the main diagonal, and k<0
4449
+ * for diagonals below the main diagonal.
4450
+ * @return [Numo::UInt8] eye of self.
4451
+ */
5371
4452
  rb_define_method(cT, "eye", uint8_eye, -1);
5372
4453
  rb_define_alias(cT, "indgen", "seq");
4454
+ /**
4455
+ * Generate uniformly distributed random numbers on self narray.
4456
+ * @overload rand([[low],high])
4457
+ * @param [Numeric] low lower inclusive boundary of random numbers. (default=0)
4458
+ * @param [Numeric] high upper exclusive boundary of random numbers.
4459
+ * (default=1 or 1+1i for complex types)
4460
+ * @return [Numo::UInt8] self.
4461
+ * @example
4462
+ * Numo::DFloat.new(6).rand
4463
+ * # => Numo::DFloat#shape=[6]
4464
+ * # [0.0617545, 0.373067, 0.794815, 0.201042, 0.116041, 0.344032]
4465
+ *
4466
+ * Numo::DComplex.new(6).rand(5+5i)
4467
+ * # => Numo::DComplex#shape=[6]
4468
+ * # [2.69974+3.68908i, 0.825443+0.254414i, 0.540323+0.34354i, 4.52061+2.39322i, ...]
4469
+ *
4470
+ * Numo::Int32.new(6).rand(2,5)
4471
+ * # => Numo::Int32#shape=[6]
4472
+ * # [4, 3, 3, 2, 4, 2]
4473
+ */
5373
4474
  rb_define_method(cT, "rand", uint8_rand, -1);
5374
4475
  rb_define_method(cT, "poly", uint8_poly, -2);
5375
4476