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_int64_t uint64; // Type aliases for shorter notation
52
71
  // following the codebase naming convention.
72
+ DEF_NARRAY_CLIP_METHOD_FUNC(uint64, numo_cUInt64)
73
+ DEF_NARRAY_INT_SUM_METHOD_FUNC(uint64, numo_cUInt64, u_int64_t, numo_cUInt64)
74
+ DEF_NARRAY_INT_PROD_METHOD_FUNC(uint64, numo_cUInt64, u_int64_t, numo_cUInt64)
75
+ DEF_NARRAY_INT_MIN_METHOD_FUNC(uint64, numo_cUInt64)
76
+ DEF_NARRAY_INT_MAX_METHOD_FUNC(uint64, numo_cUInt64)
77
+ DEF_NARRAY_INT_PTP_METHOD_FUNC(uint64, numo_cUInt64)
78
+ DEF_NARRAY_INT_MAX_INDEX_METHOD_FUNC(uint64)
79
+ DEF_NARRAY_INT_MIN_INDEX_METHOD_FUNC(uint64)
80
+ DEF_NARRAY_INT_ARGMAX_METHOD_FUNC(uint64)
81
+ DEF_NARRAY_INT_ARGMIN_METHOD_FUNC(uint64)
82
+ DEF_NARRAY_INT_MAXIMUM_METHOD_FUNC(uint64, numo_cUInt64)
83
+ DEF_NARRAY_INT_MINIMUM_METHOD_FUNC(uint64, numo_cUInt64)
84
+ DEF_NARRAY_INT_MINMAX_METHOD_FUNC(uint64, numo_cUInt64)
85
+ DEF_NARRAY_INT_CUMSUM_METHOD_FUNC(uint64, numo_cUInt64)
86
+ DEF_NARRAY_INT_CUMPROD_METHOD_FUNC(uint64, numo_cUInt64)
87
+ DEF_NARRAY_INT_MULSUM_METHOD_FUNC(uint64, numo_cUInt64)
88
+ DEF_NARRAY_INT_SEQ_METHOD_FUNC(uint64)
89
+ DEF_NARRAY_EYE_METHOD_FUNC(uint64)
90
+ DEF_NARRAY_INT64_RAND_METHOD_FUNC(uint64)
53
91
  DEF_NARRAY_INT_MEAN_METHOD_FUNC(uint64, numo_cUInt64)
54
92
  DEF_NARRAY_INT_VAR_METHOD_FUNC(uint64, numo_cUInt64)
55
93
  DEF_NARRAY_INT_STDDEV_METHOD_FUNC(uint64, numo_cUInt64)
@@ -3698,710 +3736,6 @@ static VALUE uint64_le(VALUE self, VALUE other) {
3698
3736
  }
3699
3737
  }
3700
3738
 
3701
- static void iter_uint64_clip(na_loop_t* const lp) {
3702
- size_t i;
3703
- char *p1, *p2, *p3, *p4;
3704
- ssize_t s1, s2, s3, s4;
3705
- dtype x, min, max;
3706
- INIT_COUNTER(lp, i);
3707
- INIT_PTR(lp, 0, p1, s1);
3708
- INIT_PTR(lp, 1, p2, s2);
3709
- INIT_PTR(lp, 2, p3, s3);
3710
- INIT_PTR(lp, 3, p4, s4);
3711
- for (; i--;) {
3712
- GET_DATA_STRIDE(p1, s1, dtype, x);
3713
- GET_DATA_STRIDE(p2, s2, dtype, min);
3714
- GET_DATA_STRIDE(p3, s3, dtype, max);
3715
- if (m_gt(min, max)) {
3716
- rb_raise(nary_eOperationError, "min is greater than max");
3717
- }
3718
- if (m_lt(x, min)) {
3719
- x = min;
3720
- }
3721
- if (m_gt(x, max)) {
3722
- x = max;
3723
- }
3724
- SET_DATA_STRIDE(p4, s4, dtype, x);
3725
- }
3726
- }
3727
-
3728
- static void iter_uint64_clip_min(na_loop_t* const lp) {
3729
- size_t i;
3730
- char *p1, *p2, *p3;
3731
- ssize_t s1, s2, s3;
3732
- dtype x, min;
3733
- INIT_COUNTER(lp, i);
3734
- INIT_PTR(lp, 0, p1, s1);
3735
- INIT_PTR(lp, 1, p2, s2);
3736
- INIT_PTR(lp, 2, p3, s3);
3737
- for (; i--;) {
3738
- GET_DATA_STRIDE(p1, s1, dtype, x);
3739
- GET_DATA_STRIDE(p2, s2, dtype, min);
3740
- if (m_lt(x, min)) {
3741
- x = min;
3742
- }
3743
- SET_DATA_STRIDE(p3, s3, dtype, x);
3744
- }
3745
- }
3746
-
3747
- static void iter_uint64_clip_max(na_loop_t* const lp) {
3748
- size_t i;
3749
- char *p1, *p2, *p3;
3750
- ssize_t s1, s2, s3;
3751
- dtype x, max;
3752
- INIT_COUNTER(lp, i);
3753
- INIT_PTR(lp, 0, p1, s1);
3754
- INIT_PTR(lp, 1, p2, s2);
3755
- INIT_PTR(lp, 2, p3, s3);
3756
- for (; i--;) {
3757
- GET_DATA_STRIDE(p1, s1, dtype, x);
3758
- GET_DATA_STRIDE(p2, s2, dtype, max);
3759
- if (m_gt(x, max)) {
3760
- x = max;
3761
- }
3762
- SET_DATA_STRIDE(p3, s3, dtype, x);
3763
- }
3764
- }
3765
-
3766
- /*
3767
- Clip array elements by [min,max].
3768
- If either of min or max is nil, one side is clipped.
3769
- @overload clip(min,max)
3770
- @param [Numo::NArray,Numeric] min
3771
- @param [Numo::NArray,Numeric] max
3772
- @return [Numo::NArray] result of clip.
3773
-
3774
- @example
3775
- a = Numo::Int32.new(10).seq
3776
- # => Numo::Int32#shape=[10]
3777
- # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
3778
-
3779
- a.clip(1,8)
3780
- # => Numo::Int32#shape=[10]
3781
- # [1, 1, 2, 3, 4, 5, 6, 7, 8, 8]
3782
-
3783
- a.inplace.clip(3,6)
3784
- a
3785
- # => Numo::Int32#shape=[10]
3786
- # [3, 3, 3, 3, 4, 5, 6, 6, 6, 6]
3787
-
3788
- b = Numo::Int32.new(10).seq
3789
- # => Numo::Int32#shape=[10]
3790
- # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
3791
-
3792
- b.clip([3,4,1,1,1,4,4,4,4,4], 8)
3793
- # => Numo::Int32#shape=[10]
3794
- # [3, 4, 2, 3, 4, 5, 6, 7, 8, 8]
3795
- */
3796
- static VALUE uint64_clip(VALUE self, VALUE min, VALUE max) {
3797
- ndfunc_arg_in_t ain[3] = { { Qnil, 0 }, { cT, 0 }, { cT, 0 } };
3798
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
3799
- ndfunc_t ndf_min = { iter_uint64_clip_min, STRIDE_LOOP, 2, 1, ain, aout };
3800
- ndfunc_t ndf_max = { iter_uint64_clip_max, STRIDE_LOOP, 2, 1, ain, aout };
3801
- ndfunc_t ndf_both = { iter_uint64_clip, STRIDE_LOOP, 3, 1, ain, aout };
3802
-
3803
- if (RTEST(min)) {
3804
- if (RTEST(max)) {
3805
- return na_ndloop(&ndf_both, 3, self, min, max);
3806
- } else {
3807
- return na_ndloop(&ndf_min, 2, self, min);
3808
- }
3809
- } else {
3810
- if (RTEST(max)) {
3811
- return na_ndloop(&ndf_max, 2, self, max);
3812
- }
3813
- }
3814
- rb_raise(rb_eArgError, "min and max are not given");
3815
- return Qnil;
3816
- }
3817
-
3818
- static void iter_uint64_sum(na_loop_t* const lp) {
3819
- size_t n;
3820
- char *p1, *p2;
3821
- ssize_t s1;
3822
-
3823
- INIT_COUNTER(lp, n);
3824
- INIT_PTR(lp, 0, p1, s1);
3825
- p2 = lp->args[1].ptr + lp->args[1].iter[0].pos;
3826
-
3827
- *(u_int64_t*)p2 = f_sum(n, p1, s1);
3828
- }
3829
-
3830
- /*
3831
- sum of self.
3832
- @overload sum(axis:nil, keepdims:false)
3833
- @param [Numeric,Array,Range] axis Performs sum along the axis.
3834
- @param [TrueClass] keepdims If true, the reduced axes are left in the result array as
3835
- dimensions with size one.
3836
- @return [Numo::UInt64] returns result of sum.
3837
- */
3838
- static VALUE uint64_sum(int argc, VALUE* argv, VALUE self) {
3839
- VALUE v, reduce;
3840
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { sym_reduce, 0 } };
3841
- ndfunc_arg_out_t aout[1] = { { numo_cUInt64, 0 } };
3842
- ndfunc_t ndf = { iter_uint64_sum, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE, 2, 1, ain, aout };
3843
-
3844
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
3845
-
3846
- v = na_ndloop(&ndf, 2, self, reduce);
3847
-
3848
- return rb_funcall(v, rb_intern("extract"), 0);
3849
- }
3850
-
3851
- static void iter_uint64_prod(na_loop_t* const lp) {
3852
- size_t n;
3853
- char *p1, *p2;
3854
- ssize_t s1;
3855
-
3856
- INIT_COUNTER(lp, n);
3857
- INIT_PTR(lp, 0, p1, s1);
3858
- p2 = lp->args[1].ptr + lp->args[1].iter[0].pos;
3859
-
3860
- *(u_int64_t*)p2 = f_prod(n, p1, s1);
3861
- }
3862
-
3863
- /*
3864
- prod of self.
3865
- @overload prod(axis:nil, keepdims:false)
3866
- @param [Numeric,Array,Range] axis Performs prod along the axis.
3867
- @param [TrueClass] keepdims If true, the reduced axes are left in the result array as
3868
- dimensions with size one.
3869
- @return [Numo::UInt64] returns result of prod.
3870
- */
3871
- static VALUE uint64_prod(int argc, VALUE* argv, VALUE self) {
3872
- VALUE v, reduce;
3873
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { sym_reduce, 0 } };
3874
- ndfunc_arg_out_t aout[1] = { { numo_cUInt64, 0 } };
3875
- ndfunc_t ndf = { iter_uint64_prod, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE, 2, 1, ain, aout };
3876
-
3877
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
3878
-
3879
- v = na_ndloop(&ndf, 2, self, reduce);
3880
-
3881
- return rb_funcall(v, rb_intern("extract"), 0);
3882
- }
3883
-
3884
- static void iter_uint64_min(na_loop_t* const lp) {
3885
- size_t n;
3886
- char *p1, *p2;
3887
- ssize_t s1;
3888
-
3889
- INIT_COUNTER(lp, n);
3890
- INIT_PTR(lp, 0, p1, s1);
3891
- p2 = lp->args[1].ptr + lp->args[1].iter[0].pos;
3892
-
3893
- *(dtype*)p2 = f_min(n, p1, s1);
3894
- }
3895
-
3896
- /*
3897
- min of self.
3898
- @overload min(axis:nil, keepdims:false)
3899
- @param [Numeric,Array,Range] axis Performs min along the axis.
3900
- @param [TrueClass] keepdims If true, the reduced axes are left in the result array as
3901
- dimensions with size one.
3902
- @return [Numo::UInt64] returns result of min.
3903
- */
3904
- static VALUE uint64_min(int argc, VALUE* argv, VALUE self) {
3905
- VALUE v, reduce;
3906
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { sym_reduce, 0 } };
3907
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
3908
- ndfunc_t ndf = { iter_uint64_min, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE, 2, 1, ain, aout };
3909
-
3910
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
3911
-
3912
- v = na_ndloop(&ndf, 2, self, reduce);
3913
-
3914
- return uint64_extract(v);
3915
- }
3916
-
3917
- static void iter_uint64_max(na_loop_t* const lp) {
3918
- size_t n;
3919
- char *p1, *p2;
3920
- ssize_t s1;
3921
-
3922
- INIT_COUNTER(lp, n);
3923
- INIT_PTR(lp, 0, p1, s1);
3924
- p2 = lp->args[1].ptr + lp->args[1].iter[0].pos;
3925
-
3926
- *(dtype*)p2 = f_max(n, p1, s1);
3927
- }
3928
-
3929
- /*
3930
- max of self.
3931
- @overload max(axis:nil, keepdims:false)
3932
- @param [Numeric,Array,Range] axis Performs max along the axis.
3933
- @param [TrueClass] keepdims If true, the reduced axes are left in the result array as
3934
- dimensions with size one.
3935
- @return [Numo::UInt64] returns result of max.
3936
- */
3937
- static VALUE uint64_max(int argc, VALUE* argv, VALUE self) {
3938
- VALUE v, reduce;
3939
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { sym_reduce, 0 } };
3940
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
3941
- ndfunc_t ndf = { iter_uint64_max, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE, 2, 1, ain, aout };
3942
-
3943
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
3944
-
3945
- v = na_ndloop(&ndf, 2, self, reduce);
3946
-
3947
- return uint64_extract(v);
3948
- }
3949
-
3950
- static void iter_uint64_ptp(na_loop_t* const lp) {
3951
- size_t n;
3952
- char *p1, *p2;
3953
- ssize_t s1;
3954
-
3955
- INIT_COUNTER(lp, n);
3956
- INIT_PTR(lp, 0, p1, s1);
3957
- p2 = lp->args[1].ptr + lp->args[1].iter[0].pos;
3958
-
3959
- *(dtype*)p2 = f_ptp(n, p1, s1);
3960
- }
3961
-
3962
- /*
3963
- ptp of self.
3964
- @overload ptp(axis:nil, keepdims:false)
3965
- @param [Numeric,Array,Range] axis Performs ptp along the axis.
3966
- @param [TrueClass] keepdims If true, the reduced axes are left in the result array as
3967
- dimensions with size one.
3968
- @return [Numo::UInt64] returns result of ptp.
3969
- */
3970
- static VALUE uint64_ptp(int argc, VALUE* argv, VALUE self) {
3971
- VALUE v, reduce;
3972
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { sym_reduce, 0 } };
3973
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
3974
- ndfunc_t ndf = { iter_uint64_ptp, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE, 2, 1, ain, aout };
3975
-
3976
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
3977
-
3978
- v = na_ndloop(&ndf, 2, self, reduce);
3979
-
3980
- return uint64_extract(v);
3981
- }
3982
-
3983
- #define idx_t int64_t
3984
- static void iter_uint64_max_index_index64(na_loop_t* const lp) {
3985
- size_t n, idx;
3986
- char *d_ptr, *i_ptr, *o_ptr;
3987
- ssize_t d_step, i_step;
3988
-
3989
- INIT_COUNTER(lp, n);
3990
- INIT_PTR(lp, 0, d_ptr, d_step);
3991
-
3992
- idx = f_max_index(n, d_ptr, d_step);
3993
-
3994
- INIT_PTR(lp, 1, i_ptr, i_step);
3995
- o_ptr = NDL_PTR(lp, 2);
3996
- *(idx_t*)o_ptr = *(idx_t*)(i_ptr + i_step * idx);
3997
- }
3998
- #undef idx_t
3999
-
4000
- #define idx_t int32_t
4001
- static void iter_uint64_max_index_index32(na_loop_t* const lp) {
4002
- size_t n, idx;
4003
- char *d_ptr, *i_ptr, *o_ptr;
4004
- ssize_t d_step, i_step;
4005
-
4006
- INIT_COUNTER(lp, n);
4007
- INIT_PTR(lp, 0, d_ptr, d_step);
4008
-
4009
- idx = f_max_index(n, d_ptr, d_step);
4010
-
4011
- INIT_PTR(lp, 1, i_ptr, i_step);
4012
- o_ptr = NDL_PTR(lp, 2);
4013
- *(idx_t*)o_ptr = *(idx_t*)(i_ptr + i_step * idx);
4014
- }
4015
- #undef idx_t
4016
-
4017
- /*
4018
- Index of the maximum value.
4019
- @overload max_index(axis:nil)
4020
- @param [Numeric,Array,Range] axis Finds maximum values along the axis and returns **flat
4021
- 1-d indices**.
4022
- @return [Integer,Numo::Int] returns result indices.
4023
- @see #argmax
4024
- @see #max
4025
-
4026
- @example
4027
- a = Numo::NArray[3,4,1,2]
4028
- a.max_index #=> 1
4029
-
4030
- b = Numo::NArray[[3,4,1],[2,0,5]]
4031
- b.max_index #=> 5
4032
- b.max_index(axis:1) #=> [1, 5]
4033
- b.max_index(axis:0) #=> [0, 1, 5]
4034
- b[b.max_index(axis:0)] #=> [3, 4, 5]
4035
- */
4036
- static VALUE uint64_max_index(int argc, VALUE* argv, VALUE self) {
4037
- narray_t* na;
4038
- VALUE idx, reduce;
4039
- ndfunc_arg_in_t ain[3] = { { Qnil, 0 }, { Qnil, 0 }, { sym_reduce, 0 } };
4040
- ndfunc_arg_out_t aout[1] = { { 0, 0, 0 } };
4041
- ndfunc_t ndf = { 0, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE | NDF_EXTRACT, 3, 1, ain, aout };
4042
-
4043
- GetNArray(self, na);
4044
- if (na->ndim == 0) {
4045
- return INT2FIX(0);
4046
- }
4047
- if (na->size > (~(u_int32_t)0)) {
4048
- aout[0].type = numo_cInt64;
4049
- idx = nary_new(numo_cInt64, na->ndim, na->shape);
4050
- ndf.func = iter_uint64_max_index_index64;
4051
-
4052
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
4053
-
4054
- } else {
4055
- aout[0].type = numo_cInt32;
4056
- idx = nary_new(numo_cInt32, na->ndim, na->shape);
4057
- ndf.func = iter_uint64_max_index_index32;
4058
-
4059
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
4060
- }
4061
- rb_funcall(idx, rb_intern("seq"), 0);
4062
-
4063
- return na_ndloop(&ndf, 3, self, idx, reduce);
4064
- }
4065
-
4066
- #define idx_t int64_t
4067
- static void iter_uint64_min_index_index64(na_loop_t* const lp) {
4068
- size_t n, idx;
4069
- char *d_ptr, *i_ptr, *o_ptr;
4070
- ssize_t d_step, i_step;
4071
-
4072
- INIT_COUNTER(lp, n);
4073
- INIT_PTR(lp, 0, d_ptr, d_step);
4074
-
4075
- idx = f_min_index(n, d_ptr, d_step);
4076
-
4077
- INIT_PTR(lp, 1, i_ptr, i_step);
4078
- o_ptr = NDL_PTR(lp, 2);
4079
- *(idx_t*)o_ptr = *(idx_t*)(i_ptr + i_step * idx);
4080
- }
4081
- #undef idx_t
4082
-
4083
- #define idx_t int32_t
4084
- static void iter_uint64_min_index_index32(na_loop_t* const lp) {
4085
- size_t n, idx;
4086
- char *d_ptr, *i_ptr, *o_ptr;
4087
- ssize_t d_step, i_step;
4088
-
4089
- INIT_COUNTER(lp, n);
4090
- INIT_PTR(lp, 0, d_ptr, d_step);
4091
-
4092
- idx = f_min_index(n, d_ptr, d_step);
4093
-
4094
- INIT_PTR(lp, 1, i_ptr, i_step);
4095
- o_ptr = NDL_PTR(lp, 2);
4096
- *(idx_t*)o_ptr = *(idx_t*)(i_ptr + i_step * idx);
4097
- }
4098
- #undef idx_t
4099
-
4100
- /*
4101
- Index of the minimum value.
4102
- @overload min_index(axis:nil)
4103
- @param [Numeric,Array,Range] axis Finds minimum values along the axis and returns **flat
4104
- 1-d indices**.
4105
- @return [Integer,Numo::Int] returns result indices.
4106
- @see #argmin
4107
- @see #min
4108
-
4109
- @example
4110
- a = Numo::NArray[3,4,1,2]
4111
- a.min_index #=> 2
4112
-
4113
- b = Numo::NArray[[3,4,1],[2,0,5]]
4114
- b.min_index #=> 4
4115
- b.min_index(axis:1) #=> [2, 4]
4116
- b.min_index(axis:0) #=> [3, 4, 2]
4117
- b[b.min_index(axis:0)] #=> [2, 0, 1]
4118
- */
4119
- static VALUE uint64_min_index(int argc, VALUE* argv, VALUE self) {
4120
- narray_t* na;
4121
- VALUE idx, reduce;
4122
- ndfunc_arg_in_t ain[3] = { { Qnil, 0 }, { Qnil, 0 }, { sym_reduce, 0 } };
4123
- ndfunc_arg_out_t aout[1] = { { 0, 0, 0 } };
4124
- ndfunc_t ndf = { 0, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE | NDF_EXTRACT, 3, 1, ain, aout };
4125
-
4126
- GetNArray(self, na);
4127
- if (na->ndim == 0) {
4128
- return INT2FIX(0);
4129
- }
4130
- if (na->size > (~(u_int32_t)0)) {
4131
- aout[0].type = numo_cInt64;
4132
- idx = nary_new(numo_cInt64, na->ndim, na->shape);
4133
- ndf.func = iter_uint64_min_index_index64;
4134
-
4135
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
4136
-
4137
- } else {
4138
- aout[0].type = numo_cInt32;
4139
- idx = nary_new(numo_cInt32, na->ndim, na->shape);
4140
- ndf.func = iter_uint64_min_index_index32;
4141
-
4142
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
4143
- }
4144
- rb_funcall(idx, rb_intern("seq"), 0);
4145
-
4146
- return na_ndloop(&ndf, 3, self, idx, reduce);
4147
- }
4148
-
4149
- #define idx_t int64_t
4150
- static void iter_uint64_argmax_arg64(na_loop_t* const lp) {
4151
- size_t n, idx;
4152
- char *d_ptr, *o_ptr;
4153
- ssize_t d_step;
4154
-
4155
- INIT_COUNTER(lp, n);
4156
- INIT_PTR(lp, 0, d_ptr, d_step);
4157
-
4158
- idx = f_max_index(n, d_ptr, d_step);
4159
-
4160
- o_ptr = NDL_PTR(lp, 1);
4161
- *(idx_t*)o_ptr = (idx_t)idx;
4162
- }
4163
- #undef idx_t
4164
-
4165
- #define idx_t int32_t
4166
- static void iter_uint64_argmax_arg32(na_loop_t* const lp) {
4167
- size_t n, idx;
4168
- char *d_ptr, *o_ptr;
4169
- ssize_t d_step;
4170
-
4171
- INIT_COUNTER(lp, n);
4172
- INIT_PTR(lp, 0, d_ptr, d_step);
4173
-
4174
- idx = f_max_index(n, d_ptr, d_step);
4175
-
4176
- o_ptr = NDL_PTR(lp, 1);
4177
- *(idx_t*)o_ptr = (idx_t)idx;
4178
- }
4179
- #undef idx_t
4180
-
4181
- /*
4182
- Index of the maximum value.
4183
- @overload argmax(axis:nil)
4184
- @param [Numeric,Array,Range] axis Finds maximum values along the axis and returns **indices
4185
- along the axis**.
4186
- @return [Integer,Numo::Int] returns the result indices.
4187
- @see #max_index
4188
- @see #max
4189
-
4190
- @example
4191
- a = Numo::NArray[3,4,1,2]
4192
- a.argmax #=> 1
4193
-
4194
- b = Numo::NArray[[3,4,1],[2,0,5]]
4195
- b.argmax #=> 5
4196
- b.argmax(axis:1) #=> [1, 2]
4197
- b.argmax(axis:0) #=> [0, 0, 1]
4198
- b.at(b.argmax(axis:0), 0..-1) #=> [3, 4, 5]
4199
- */
4200
- static VALUE uint64_argmax(int argc, VALUE* argv, VALUE self) {
4201
- narray_t* na;
4202
- VALUE reduce;
4203
- ndfunc_arg_in_t ain[2] = { { Qnil, 0 }, { sym_reduce, 0 } };
4204
- ndfunc_arg_out_t aout[1] = { { 0, 0, 0 } };
4205
- ndfunc_t ndf = { 0, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE | NDF_EXTRACT, 2, 1, ain, aout };
4206
-
4207
- GetNArray(self, na);
4208
- if (na->ndim == 0) {
4209
- return INT2FIX(0);
4210
- }
4211
- if (na->size > (~(u_int32_t)0)) {
4212
- aout[0].type = numo_cInt64;
4213
- ndf.func = iter_uint64_argmax_arg64;
4214
-
4215
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
4216
-
4217
- } else {
4218
- aout[0].type = numo_cInt32;
4219
- ndf.func = iter_uint64_argmax_arg32;
4220
-
4221
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
4222
- }
4223
-
4224
- return na_ndloop(&ndf, 2, self, reduce);
4225
- }
4226
-
4227
- #define idx_t int64_t
4228
- static void iter_uint64_argmin_arg64(na_loop_t* const lp) {
4229
- size_t n, idx;
4230
- char *d_ptr, *o_ptr;
4231
- ssize_t d_step;
4232
-
4233
- INIT_COUNTER(lp, n);
4234
- INIT_PTR(lp, 0, d_ptr, d_step);
4235
-
4236
- idx = f_min_index(n, d_ptr, d_step);
4237
-
4238
- o_ptr = NDL_PTR(lp, 1);
4239
- *(idx_t*)o_ptr = (idx_t)idx;
4240
- }
4241
- #undef idx_t
4242
-
4243
- #define idx_t int32_t
4244
- static void iter_uint64_argmin_arg32(na_loop_t* const lp) {
4245
- size_t n, idx;
4246
- char *d_ptr, *o_ptr;
4247
- ssize_t d_step;
4248
-
4249
- INIT_COUNTER(lp, n);
4250
- INIT_PTR(lp, 0, d_ptr, d_step);
4251
-
4252
- idx = f_min_index(n, d_ptr, d_step);
4253
-
4254
- o_ptr = NDL_PTR(lp, 1);
4255
- *(idx_t*)o_ptr = (idx_t)idx;
4256
- }
4257
- #undef idx_t
4258
-
4259
- /*
4260
- Index of the minimum value.
4261
- @overload argmin(axis:nil)
4262
- @param [Numeric,Array,Range] axis Finds minimum values along the axis and returns **indices
4263
- along the axis**.
4264
- @return [Integer,Numo::Int] returns the result indices.
4265
- @see #min_index
4266
- @see #min
4267
-
4268
- @example
4269
- a = Numo::NArray[3,4,1,2]
4270
- a.argmin #=> 2
4271
-
4272
- b = Numo::NArray[[3,4,1],[2,0,5]]
4273
- b.argmin #=> 4
4274
- b.argmin(axis:1) #=> [2, 1]
4275
- b.argmin(axis:0) #=> [1, 1, 0]
4276
- b.at(b.argmin(axis:0), 0..-1) #=> [2, 0, 1]
4277
- */
4278
- static VALUE uint64_argmin(int argc, VALUE* argv, VALUE self) {
4279
- narray_t* na;
4280
- VALUE reduce;
4281
- ndfunc_arg_in_t ain[2] = { { Qnil, 0 }, { sym_reduce, 0 } };
4282
- ndfunc_arg_out_t aout[1] = { { 0, 0, 0 } };
4283
- ndfunc_t ndf = { 0, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE | NDF_EXTRACT, 2, 1, ain, aout };
4284
-
4285
- GetNArray(self, na);
4286
- if (na->ndim == 0) {
4287
- return INT2FIX(0);
4288
- }
4289
- if (na->size > (~(u_int32_t)0)) {
4290
- aout[0].type = numo_cInt64;
4291
- ndf.func = iter_uint64_argmin_arg64;
4292
-
4293
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
4294
-
4295
- } else {
4296
- aout[0].type = numo_cInt32;
4297
- ndf.func = iter_uint64_argmin_arg32;
4298
-
4299
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
4300
- }
4301
-
4302
- return na_ndloop(&ndf, 2, self, reduce);
4303
- }
4304
-
4305
- static void iter_uint64_minmax(na_loop_t* const lp) {
4306
- size_t n;
4307
- char* p1;
4308
- ssize_t s1;
4309
- dtype xmin, xmax;
4310
-
4311
- INIT_COUNTER(lp, n);
4312
- INIT_PTR(lp, 0, p1, s1);
4313
-
4314
- f_minmax(n, p1, s1, &xmin, &xmax);
4315
-
4316
- *(dtype*)(lp->args[1].ptr + lp->args[1].iter[0].pos) = xmin;
4317
- *(dtype*)(lp->args[2].ptr + lp->args[2].iter[0].pos) = xmax;
4318
- }
4319
-
4320
- /*
4321
- minmax of self.
4322
- @overload minmax(axis:nil, keepdims:false)
4323
- @param [Numeric,Array,Range] axis Finds min-max along the axis.
4324
- @param [TrueClass] keepdims (keyword) If true, the reduced axes are left in the result array
4325
- as dimensions with size one.
4326
- @return [Numo::UInt64,Numo::UInt64] min and max of self.
4327
- */
4328
- static VALUE uint64_minmax(int argc, VALUE* argv, VALUE self) {
4329
- VALUE reduce;
4330
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { sym_reduce, 0 } };
4331
- ndfunc_arg_out_t aout[2] = { { cT, 0 }, { cT, 0 } };
4332
- ndfunc_t ndf = {
4333
- iter_uint64_minmax, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE | NDF_EXTRACT, 2, 2, ain, aout
4334
- };
4335
-
4336
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
4337
-
4338
- return na_ndloop(&ndf, 2, self, reduce);
4339
- }
4340
-
4341
- static void iter_uint64_s_maximum(na_loop_t* const lp) {
4342
- size_t i, n;
4343
- char *p1, *p2, *p3;
4344
- ssize_t s1, s2, s3;
4345
-
4346
- INIT_COUNTER(lp, n);
4347
- INIT_PTR(lp, 0, p1, s1);
4348
- INIT_PTR(lp, 1, p2, s2);
4349
- INIT_PTR(lp, 2, p3, s3);
4350
-
4351
- for (i = 0; i < n; i++) {
4352
- dtype x, y, z;
4353
- GET_DATA_STRIDE(p1, s1, dtype, x);
4354
- GET_DATA_STRIDE(p2, s2, dtype, y);
4355
- GET_DATA(p3, dtype, z);
4356
- z = f_maximum(x, y);
4357
- SET_DATA_STRIDE(p3, s3, dtype, z);
4358
- }
4359
- }
4360
-
4361
- static VALUE uint64_s_maximum(int argc, VALUE* argv, VALUE mod) {
4362
- VALUE a1 = Qnil;
4363
- VALUE a2 = Qnil;
4364
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
4365
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
4366
- ndfunc_t ndf = { iter_uint64_s_maximum, STRIDE_LOOP_NIP, 2, 1, ain, aout };
4367
-
4368
- rb_scan_args(argc, argv, "20", &a1, &a2);
4369
-
4370
- return na_ndloop(&ndf, 2, a1, a2);
4371
- }
4372
-
4373
- static void iter_uint64_s_minimum(na_loop_t* const lp) {
4374
- size_t i, n;
4375
- char *p1, *p2, *p3;
4376
- ssize_t s1, s2, s3;
4377
-
4378
- INIT_COUNTER(lp, n);
4379
- INIT_PTR(lp, 0, p1, s1);
4380
- INIT_PTR(lp, 1, p2, s2);
4381
- INIT_PTR(lp, 2, p3, s3);
4382
-
4383
- for (i = 0; i < n; i++) {
4384
- dtype x, y, z;
4385
- GET_DATA_STRIDE(p1, s1, dtype, x);
4386
- GET_DATA_STRIDE(p2, s2, dtype, y);
4387
- GET_DATA(p3, dtype, z);
4388
- z = f_minimum(x, y);
4389
- SET_DATA_STRIDE(p3, s3, dtype, z);
4390
- }
4391
- }
4392
-
4393
- static VALUE uint64_s_minimum(int argc, VALUE* argv, VALUE mod) {
4394
- VALUE a1 = Qnil;
4395
- VALUE a2 = Qnil;
4396
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { cT, 0 } };
4397
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
4398
- ndfunc_t ndf = { iter_uint64_s_minimum, STRIDE_LOOP_NIP, 2, 1, ain, aout };
4399
-
4400
- rb_scan_args(argc, argv, "20", &a1, &a2);
4401
-
4402
- return na_ndloop(&ndf, 2, a1, a2);
4403
- }
4404
-
4405
3739
  // ------- Integer count without weights -------
4406
3740
 
4407
3741
  static void iter_uint64_bincount_32(na_loop_t* const lp) {
@@ -4631,465 +3965,6 @@ static VALUE uint64_bincount(int argc, VALUE* argv, VALUE self) {
4631
3965
  }
4632
3966
  }
4633
3967
 
4634
- static void iter_uint64_cumsum(na_loop_t* const lp) {
4635
- size_t i;
4636
- char *p1, *p2;
4637
- ssize_t s1, s2;
4638
- dtype x, y;
4639
-
4640
- INIT_COUNTER(lp, i);
4641
- INIT_PTR(lp, 0, p1, s1);
4642
- INIT_PTR(lp, 1, p2, s2);
4643
-
4644
- GET_DATA_STRIDE(p1, s1, dtype, x);
4645
- SET_DATA_STRIDE(p2, s2, dtype, x);
4646
- for (i--; i--;) {
4647
- GET_DATA_STRIDE(p1, s1, dtype, y);
4648
- m_cumsum(x, y);
4649
- SET_DATA_STRIDE(p2, s2, dtype, x);
4650
- }
4651
- }
4652
-
4653
- /*
4654
- cumsum of self.
4655
- @overload cumsum(axis:nil, nan:false)
4656
- @param [Numeric,Array,Range] axis Performs cumsum along the axis.
4657
- @param [TrueClass] nan If true, apply NaN-aware algorithm (avoid NaN if exists).
4658
- @return [Numo::UInt64] cumsum of self.
4659
- */
4660
- static VALUE uint64_cumsum(int argc, VALUE* argv, VALUE self) {
4661
- VALUE reduce;
4662
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { sym_reduce, 0 } };
4663
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
4664
- ndfunc_t ndf = {
4665
- iter_uint64_cumsum, STRIDE_LOOP | NDF_FLAT_REDUCE | NDF_CUM, 2, 1, ain, aout
4666
- };
4667
-
4668
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
4669
-
4670
- return na_ndloop(&ndf, 2, self, reduce);
4671
- }
4672
-
4673
- static void iter_uint64_cumprod(na_loop_t* const lp) {
4674
- size_t i;
4675
- char *p1, *p2;
4676
- ssize_t s1, s2;
4677
- dtype x, y;
4678
-
4679
- INIT_COUNTER(lp, i);
4680
- INIT_PTR(lp, 0, p1, s1);
4681
- INIT_PTR(lp, 1, p2, s2);
4682
-
4683
- GET_DATA_STRIDE(p1, s1, dtype, x);
4684
- SET_DATA_STRIDE(p2, s2, dtype, x);
4685
- for (i--; i--;) {
4686
- GET_DATA_STRIDE(p1, s1, dtype, y);
4687
- m_cumprod(x, y);
4688
- SET_DATA_STRIDE(p2, s2, dtype, x);
4689
- }
4690
- }
4691
-
4692
- /*
4693
- cumprod of self.
4694
- @overload cumprod(axis:nil, nan:false)
4695
- @param [Numeric,Array,Range] axis Performs cumprod along the axis.
4696
- @param [TrueClass] nan If true, apply NaN-aware algorithm (avoid NaN if exists).
4697
- @return [Numo::UInt64] cumprod of self.
4698
- */
4699
- static VALUE uint64_cumprod(int argc, VALUE* argv, VALUE self) {
4700
- VALUE reduce;
4701
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { sym_reduce, 0 } };
4702
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
4703
- ndfunc_t ndf = {
4704
- iter_uint64_cumprod, STRIDE_LOOP | NDF_FLAT_REDUCE | NDF_CUM, 2, 1, ain, aout
4705
- };
4706
-
4707
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
4708
-
4709
- return na_ndloop(&ndf, 2, self, reduce);
4710
- }
4711
-
4712
- //
4713
- static void iter_uint64_mulsum(na_loop_t* const lp) {
4714
- size_t i, n;
4715
- char *p1, *p2, *p3;
4716
- ssize_t s1, s2, s3;
4717
-
4718
- INIT_COUNTER(lp, n);
4719
- INIT_PTR(lp, 0, p1, s1);
4720
- INIT_PTR(lp, 1, p2, s2);
4721
- INIT_PTR(lp, 2, p3, s3);
4722
-
4723
- if (s3 == 0) {
4724
- dtype z;
4725
- // Reduce loop
4726
- GET_DATA(p3, dtype, z);
4727
- for (i = 0; i < n; i++) {
4728
- dtype x, y;
4729
- GET_DATA_STRIDE(p1, s1, dtype, x);
4730
- GET_DATA_STRIDE(p2, s2, dtype, y);
4731
- m_mulsum(x, y, z);
4732
- }
4733
- SET_DATA(p3, dtype, z);
4734
- return;
4735
- } else {
4736
- for (i = 0; i < n; i++) {
4737
- dtype x, y, z;
4738
- GET_DATA_STRIDE(p1, s1, dtype, x);
4739
- GET_DATA_STRIDE(p2, s2, dtype, y);
4740
- GET_DATA(p3, dtype, z);
4741
- m_mulsum(x, y, z);
4742
- SET_DATA_STRIDE(p3, s3, dtype, z);
4743
- }
4744
- }
4745
- }
4746
- //
4747
-
4748
- static VALUE uint64_mulsum_self(int argc, VALUE* argv, VALUE self) {
4749
- VALUE v, reduce;
4750
- VALUE naryv[2];
4751
- ndfunc_arg_in_t ain[4] = { { cT, 0 }, { cT, 0 }, { sym_reduce, 0 }, { sym_init, 0 } };
4752
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
4753
- ndfunc_t ndf = { iter_uint64_mulsum, STRIDE_LOOP_NIP, 4, 1, ain, aout };
4754
-
4755
- if (argc < 1) {
4756
- rb_raise(rb_eArgError, "wrong number of arguments (%d for >=1)", argc);
4757
- }
4758
- // should fix below: [self.ndim,other.ndim].max or?
4759
- naryv[0] = self;
4760
- naryv[1] = argv[0];
4761
- //
4762
- reduce = na_reduce_dimension(argc - 1, argv + 1, 2, naryv, &ndf, 0);
4763
- //
4764
-
4765
- v = na_ndloop(&ndf, 4, self, argv[0], reduce, m_mulsum_init);
4766
- return uint64_extract(v);
4767
- }
4768
-
4769
- /*
4770
- Binary mulsum.
4771
-
4772
- @overload mulsum(other, axis:nil, keepdims:false)
4773
- @param [Numo::NArray,Numeric] other
4774
- @param [Numeric,Array,Range] axis Performs mulsum along the axis.
4775
- @param [TrueClass] keepdims (keyword) If true, the reduced axes are left in the result array
4776
- as dimensions with size one.
4777
- @return [Numo::NArray] mulsum of self and other.
4778
- */
4779
- static VALUE uint64_mulsum(int argc, VALUE* argv, VALUE self) {
4780
- //
4781
- VALUE klass, v;
4782
- //
4783
- if (argc < 1) {
4784
- rb_raise(rb_eArgError, "wrong number of arguments (%d for >=1)", argc);
4785
- }
4786
- //
4787
- klass = na_upcast(rb_obj_class(self), rb_obj_class(argv[0]));
4788
- if (klass == cT) {
4789
- return uint64_mulsum_self(argc, argv, self);
4790
- } else {
4791
- v = rb_funcall(klass, id_cast, 1, self);
4792
- //
4793
- return rb_funcallv_kw(v, rb_intern("mulsum"), argc, argv, RB_PASS_CALLED_KEYWORDS);
4794
- //
4795
- }
4796
- //
4797
- }
4798
-
4799
- typedef double seq_data_t;
4800
-
4801
- typedef double seq_count_t;
4802
-
4803
- typedef struct {
4804
- seq_data_t beg;
4805
- seq_data_t step;
4806
- seq_count_t count;
4807
- } seq_opt_t;
4808
-
4809
- static void iter_uint64_seq(na_loop_t* const lp) {
4810
- size_t i;
4811
- char* p1;
4812
- ssize_t s1;
4813
- size_t* idx1;
4814
- dtype x;
4815
- seq_data_t beg, step;
4816
- seq_count_t c;
4817
- seq_opt_t* g;
4818
-
4819
- INIT_COUNTER(lp, i);
4820
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
4821
- g = (seq_opt_t*)(lp->opt_ptr);
4822
- beg = g->beg;
4823
- step = g->step;
4824
- c = g->count;
4825
- if (idx1) {
4826
- for (; i--;) {
4827
- x = f_seq(beg, step, c++);
4828
- *(dtype*)(p1 + *idx1) = x;
4829
- idx1++;
4830
- }
4831
- } else {
4832
- for (; i--;) {
4833
- x = f_seq(beg, step, c++);
4834
- *(dtype*)(p1) = x;
4835
- p1 += s1;
4836
- }
4837
- }
4838
- g->count = c;
4839
- }
4840
-
4841
- /*
4842
- Set linear sequence of numbers to self. The sequence is obtained from
4843
- beg+i*step
4844
- where i is 1-dimensional index.
4845
- @overload seq([beg,[step]])
4846
- @param [Numeric] beg beginning of sequence. (default=0)
4847
- @param [Numeric] step step of sequence. (default=1)
4848
- @return [Numo::UInt64] self.
4849
- @example
4850
- Numo::DFloat.new(6).seq(1,-0.2)
4851
- # => Numo::DFloat#shape=[6]
4852
- # [1, 0.8, 0.6, 0.4, 0.2, 0]
4853
-
4854
- Numo::DComplex.new(6).seq(1,-0.2+0.2i)
4855
- # => Numo::DComplex#shape=[6]
4856
- # [1+0i, 0.8+0.2i, 0.6+0.4i, 0.4+0.6i, 0.2+0.8i, 0+1i]
4857
- */
4858
- static VALUE uint64_seq(int argc, VALUE* argv, VALUE self) {
4859
- seq_opt_t* g;
4860
- VALUE vbeg = Qnil, vstep = Qnil;
4861
- ndfunc_arg_in_t ain[1] = { { OVERWRITE, 0 } };
4862
- ndfunc_t ndf = { iter_uint64_seq, FULL_LOOP, 1, 0, ain, 0 };
4863
-
4864
- g = ALLOCA_N(seq_opt_t, 1);
4865
- g->beg = m_zero;
4866
- g->step = m_one;
4867
- g->count = 0;
4868
- rb_scan_args(argc, argv, "02", &vbeg, &vstep);
4869
- if (vbeg != Qnil) {
4870
- g->beg = NUM2DBL(vbeg);
4871
- }
4872
- if (vstep != Qnil) {
4873
- g->step = NUM2DBL(vstep);
4874
- }
4875
-
4876
- na_ndloop3(&ndf, g, 1, self);
4877
- return self;
4878
- }
4879
-
4880
- static void iter_uint64_eye(na_loop_t* const lp) {
4881
- size_t n0, n1;
4882
- size_t i0, i1;
4883
- ssize_t s0, s1;
4884
- char *p0, *p1;
4885
- char* g;
4886
- ssize_t kofs;
4887
- dtype data;
4888
-
4889
- g = (char*)(lp->opt_ptr);
4890
- kofs = *(ssize_t*)g;
4891
- data = *(dtype*)(g + sizeof(ssize_t));
4892
-
4893
- n0 = lp->args[0].shape[0];
4894
- n1 = lp->args[0].shape[1];
4895
- s0 = lp->args[0].iter[0].step;
4896
- s1 = lp->args[0].iter[1].step;
4897
- p0 = NDL_PTR(lp, 0);
4898
-
4899
- for (i0 = 0; i0 < n0; i0++) {
4900
- p1 = p0;
4901
- for (i1 = 0; i1 < n1; i1++) {
4902
- *(dtype*)p1 = (i0 + kofs == i1) ? data : m_zero;
4903
- p1 += s1;
4904
- }
4905
- p0 += s0;
4906
- }
4907
- }
4908
-
4909
- /*
4910
- Eye: Set a value to diagonal components, set 0 to non-diagonal components.
4911
- @overload eye([element,offset])
4912
- @param [Numeric] element Diagonal element to be stored. Default is 1.
4913
- @param [Integer] offset Diagonal offset from the main diagonal. The
4914
- default is 0. k>0 for diagonals above the main diagonal, and k<0
4915
- for diagonals below the main diagonal.
4916
- @return [Numo::UInt64] eye of self.
4917
- */
4918
- static VALUE uint64_eye(int argc, VALUE* argv, VALUE self) {
4919
- ndfunc_arg_in_t ain[1] = { { OVERWRITE, 2 } };
4920
- ndfunc_t ndf = { iter_uint64_eye, NO_LOOP, 1, 0, ain, 0 };
4921
- ssize_t kofs;
4922
- dtype data;
4923
- char* g;
4924
- int nd;
4925
- narray_t* na;
4926
-
4927
- // check arguments
4928
- if (argc > 2) {
4929
- rb_raise(rb_eArgError, "too many arguments (%d for 0..2)", argc);
4930
- } else if (argc == 2) {
4931
- data = m_num_to_data(argv[0]);
4932
- kofs = NUM2SSIZET(argv[1]);
4933
- } else if (argc == 1) {
4934
- data = m_num_to_data(argv[0]);
4935
- kofs = 0;
4936
- } else {
4937
- data = m_one;
4938
- kofs = 0;
4939
- }
4940
-
4941
- GetNArray(self, na);
4942
- nd = na->ndim;
4943
- if (nd < 2) {
4944
- rb_raise(nary_eDimensionError, "less than 2-d array");
4945
- }
4946
-
4947
- // Diagonal offset from the main diagonal.
4948
- if (kofs >= 0) {
4949
- if ((size_t)(kofs) >= na->shape[nd - 1]) {
4950
- rb_raise(
4951
- rb_eArgError,
4952
- "invalid diagonal offset(%" SZF "d) for "
4953
- "last dimension size(%" SZF "d)",
4954
- kofs, na->shape[nd - 1]
4955
- );
4956
- }
4957
- } else {
4958
- if ((size_t)(-kofs) >= na->shape[nd - 2]) {
4959
- rb_raise(
4960
- rb_eArgError,
4961
- "invalid diagonal offset(%" SZF "d) for "
4962
- "last-1 dimension size(%" SZF "d)",
4963
- kofs, na->shape[nd - 2]
4964
- );
4965
- }
4966
- }
4967
-
4968
- g = ALLOCA_N(char, sizeof(ssize_t) + sizeof(dtype));
4969
- *(ssize_t*)g = kofs;
4970
- *(dtype*)(g + sizeof(ssize_t)) = data;
4971
-
4972
- na_ndloop3(&ndf, g, 1, self);
4973
- return self;
4974
- }
4975
-
4976
- #define HWID (sizeof(dtype) * 4)
4977
-
4978
- static int msb_pos(uint64_t a) {
4979
- int width = HWID;
4980
- int pos = 0;
4981
- uint64_t mask = (((dtype)1 << HWID) - 1) << HWID;
4982
-
4983
- if (a == 0) {
4984
- return -1;
4985
- }
4986
-
4987
- while (width) {
4988
- if (a & mask) {
4989
- pos += width;
4990
- } else {
4991
- mask >>= width;
4992
- }
4993
- width >>= 1;
4994
- mask &= mask << width;
4995
- }
4996
- return pos;
4997
- }
4998
-
4999
- /* generates a random number on [0,max) */
5000
- inline static dtype m_rand(uint64_t max, int shift) {
5001
- uint64_t x;
5002
- do {
5003
- x = gen_rand32();
5004
- x <<= 32;
5005
- x |= gen_rand32();
5006
- x >>= shift;
5007
- } while (x >= max);
5008
- return x;
5009
- }
5010
-
5011
- typedef struct {
5012
- dtype low;
5013
- uint64_t max;
5014
- } rand_opt_t;
5015
-
5016
- static void iter_uint64_rand(na_loop_t* const lp) {
5017
- size_t i;
5018
- char* p1;
5019
- ssize_t s1;
5020
- size_t* idx1;
5021
- dtype x;
5022
- rand_opt_t* g;
5023
- dtype low;
5024
- uint64_t max;
5025
- int shift;
5026
-
5027
- INIT_COUNTER(lp, i);
5028
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
5029
- g = (rand_opt_t*)(lp->opt_ptr);
5030
- low = g->low;
5031
- max = g->max;
5032
- shift = 63 - msb_pos(max);
5033
-
5034
- if (idx1) {
5035
- for (; i--;) {
5036
- x = m_add(m_rand(max, shift), low);
5037
- SET_DATA_INDEX(p1, idx1, dtype, x);
5038
- }
5039
- } else {
5040
- for (; i--;) {
5041
- x = m_add(m_rand(max, shift), low);
5042
- SET_DATA_STRIDE(p1, s1, dtype, x);
5043
- }
5044
- }
5045
- }
5046
-
5047
- /*
5048
- Generate uniformly distributed random numbers on self narray.
5049
- @overload rand([[low],high])
5050
- @param [Numeric] low lower inclusive boundary of random numbers. (default=0)
5051
- @param [Numeric] high upper exclusive boundary of random numbers. (default=1 or 1+1i for
5052
- complex types)
5053
- @return [Numo::UInt64] self.
5054
- @example
5055
- Numo::DFloat.new(6).rand
5056
- # => Numo::DFloat#shape=[6]
5057
- # [0.0617545, 0.373067, 0.794815, 0.201042, 0.116041, 0.344032]
5058
-
5059
- Numo::DComplex.new(6).rand(5+5i)
5060
- # => Numo::DComplex#shape=[6]
5061
- # [2.69974+3.68908i, 0.825443+0.254414i, 0.540323+0.34354i, 4.52061+2.39322i, ...]
5062
-
5063
- Numo::Int32.new(6).rand(2,5)
5064
- # => Numo::Int32#shape=[6]
5065
- # [4, 3, 3, 2, 4, 2]
5066
- */
5067
- static VALUE uint64_rand(int argc, VALUE* argv, VALUE self) {
5068
- rand_opt_t g;
5069
- VALUE v1 = Qnil, v2 = Qnil;
5070
- dtype high;
5071
- ndfunc_arg_in_t ain[1] = { { OVERWRITE, 0 } };
5072
- ndfunc_t ndf = { iter_uint64_rand, FULL_LOOP, 1, 0, ain, 0 };
5073
-
5074
- rb_scan_args(argc, argv, "11", &v1, &v2);
5075
- if (v2 == Qnil) {
5076
- g.low = m_zero;
5077
- g.max = high = m_num_to_data(v1);
5078
-
5079
- } else {
5080
- g.low = m_num_to_data(v1);
5081
- high = m_num_to_data(v2);
5082
- g.max = m_sub(high, g.low);
5083
- }
5084
-
5085
- if (high <= g.low) {
5086
- rb_raise(rb_eArgError, "high must be larger than low");
5087
- }
5088
-
5089
- na_ndloop3(&ndf, &g, 1, self);
5090
- return self;
5091
- }
5092
-
5093
3968
  static void iter_uint64_poly(na_loop_t* const lp) {
5094
3969
  size_t i;
5095
3970
  dtype x, y, a;
@@ -5762,16 +4637,170 @@ void Init_numo_uint64(void) {
5762
4637
  rb_define_alias(cT, ">=", "ge");
5763
4638
  rb_define_alias(cT, "<", "lt");
5764
4639
  rb_define_alias(cT, "<=", "le");
4640
+ /**
4641
+ * Clip array elements by [min,max].
4642
+ * If either of min or max is nil, one side is clipped.
4643
+ * @overload clip(min,max)
4644
+ * @param [Numo::NArray,Numeric] min
4645
+ * @param [Numo::NArray,Numeric] max
4646
+ * @return [Numo::NArray] result of clip.
4647
+ *
4648
+ * @example
4649
+ * a = Numo::Int32.new(10).seq
4650
+ * # => Numo::Int32#shape=[10]
4651
+ * # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
4652
+ *
4653
+ * a.clip(1,8)
4654
+ * # => Numo::Int32#shape=[10]
4655
+ * # [1, 1, 2, 3, 4, 5, 6, 7, 8, 8]
4656
+ *
4657
+ * a.inplace.clip(3,6)
4658
+ * a
4659
+ * # => Numo::Int32#shape=[10]
4660
+ * # [3, 3, 3, 3, 4, 5, 6, 6, 6, 6]
4661
+ *
4662
+ * b = Numo::Int32.new(10).seq
4663
+ * # => Numo::Int32#shape=[10]
4664
+ * # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
4665
+ *
4666
+ * b.clip([3,4,1,1,1,4,4,4,4,4], 8)
4667
+ * # => Numo::Int32#shape=[10]
4668
+ * # [3, 4, 2, 3, 4, 5, 6, 7, 8, 8]
4669
+ */
5765
4670
  rb_define_method(cT, "clip", uint64_clip, 2);
4671
+ /**
4672
+ * sum of self.
4673
+ * @overload sum(axis:nil, keepdims:false)
4674
+ * @param [Numeric,Array,Range] axis Performs sum along the axis.
4675
+ * @param [TrueClass] keepdims If true, the reduced axes are left in the result array as
4676
+ * dimensions with size one.
4677
+ * @return [Numo::UInt64] returns result of sum.
4678
+ */
5766
4679
  rb_define_method(cT, "sum", uint64_sum, -1);
4680
+ /**
4681
+ * prod of self.
4682
+ * @overload prod(axis:nil, keepdims:false)
4683
+ * @param [Numeric,Array,Range] axis Performs prod along the axis.
4684
+ * @param [TrueClass] keepdims If true, the reduced axes are left in the result array as
4685
+ * dimensions with size one.
4686
+ * @return [Numo::UInt64] returns result of prod.
4687
+ */
5767
4688
  rb_define_method(cT, "prod", uint64_prod, -1);
4689
+ /**
4690
+ * min of self.
4691
+ * @overload min(axis:nil, keepdims:false)
4692
+ * @param [Numeric,Array,Range] axis Performs min along the axis.
4693
+ * @param [TrueClass] keepdims If true, the reduced axes are left in the result array as
4694
+ * dimensions with size one.
4695
+ * @return [Numo::UInt64] returns result of min.
4696
+ */
5768
4697
  rb_define_method(cT, "min", uint64_min, -1);
4698
+ /**
4699
+ * max of self.
4700
+ * @overload max(axis:nil, keepdims:false)
4701
+ * @param [Numeric,Array,Range] axis Performs max along the axis.
4702
+ * @param [TrueClass] keepdims If true, the reduced axes are left in the result array as
4703
+ * dimensions with size one.
4704
+ * @return [Numo::UInt64] returns result of max.
4705
+ */
5769
4706
  rb_define_method(cT, "max", uint64_max, -1);
4707
+ /**
4708
+ * ptp of self.
4709
+ * @overload ptp(axis:nil, keepdims:false)
4710
+ * @param [Numeric,Array,Range] axis Performs ptp along the axis.
4711
+ * @param [TrueClass] keepdims If true, the reduced axes are left in the result array as
4712
+ * dimensions with size one.
4713
+ * @return [Numo::UInt64] returns result of ptp.
4714
+ */
5770
4715
  rb_define_method(cT, "ptp", uint64_ptp, -1);
4716
+ /**
4717
+ * Index of the maximum value.
4718
+ * @overload max_index(axis:nil)
4719
+ * @param [Numeric,Array,Range] axis Finds maximum values along the axis and
4720
+ * returns **flat 1-d indices**.
4721
+ * @return [Integer,Numo::Int] returns result indices.
4722
+ * @see #argmax
4723
+ * @see #max
4724
+ *
4725
+ * @example
4726
+ * a = Numo::NArray[3,4,1,2]
4727
+ * a.max_index #=> 1
4728
+ *
4729
+ * b = Numo::NArray[[3,4,1],[2,0,5]]
4730
+ * b.max_index #=> 5
4731
+ * b.max_index(axis:1) #=> [1, 5]
4732
+ * b.max_index(axis:0) #=> [0, 1, 5]
4733
+ * b[b.max_index(axis:0)] #=> [3, 4, 5]
4734
+ */
5771
4735
  rb_define_method(cT, "max_index", uint64_max_index, -1);
4736
+ /**
4737
+ * Index of the minimum value.
4738
+ * @overload min_index(axis:nil)
4739
+ * @param [Numeric,Array,Range] axis Finds minimum values along the axis and
4740
+ * returns **flat 1-d indices**.
4741
+ * @return [Integer,Numo::Int] returns result indices.
4742
+ * @see #argmin
4743
+ * @see #min
4744
+ *
4745
+ * @example
4746
+ * a = Numo::NArray[3,4,1,2]
4747
+ * a.min_index #=> 2
4748
+ *
4749
+ * b = Numo::NArray[[3,4,1],[2,0,5]]
4750
+ * b.min_index #=> 4
4751
+ * b.min_index(axis:1) #=> [2, 4]
4752
+ * b.min_index(axis:0) #=> [3, 4, 2]
4753
+ * b[b.min_index(axis:0)] #=> [2, 0, 1]
4754
+ */
5772
4755
  rb_define_method(cT, "min_index", uint64_min_index, -1);
4756
+ /**
4757
+ * Index of the maximum value.
4758
+ * @overload argmax(axis:nil)
4759
+ * @param [Numeric,Array,Range] axis Finds maximum values along the axis and
4760
+ * returns **indices along the axis**.
4761
+ * @return [Integer,Numo::Int] returns the result indices.
4762
+ * @see #max_index
4763
+ * @see #max
4764
+ *
4765
+ * @example
4766
+ * a = Numo::NArray[3,4,1,2]
4767
+ * a.argmax #=> 1
4768
+ *
4769
+ * b = Numo::NArray[[3,4,1],[2,0,5]]
4770
+ * b.argmax #=> 5
4771
+ * b.argmax(axis:1) #=> [1, 2]
4772
+ * b.argmax(axis:0) #=> [0, 0, 1]
4773
+ * b.at(b.argmax(axis:0), 0..-1) #=> [3, 4, 5]
4774
+ */
5773
4775
  rb_define_method(cT, "argmax", uint64_argmax, -1);
4776
+ /**
4777
+ * Index of the minimum value.
4778
+ * @overload argmin(axis:nil)
4779
+ * @param [Numeric,Array,Range] axis Finds minimum values along the axis and
4780
+ * returns **indices along the axis**.
4781
+ * @return [Integer,Numo::Int] returns the result indices.
4782
+ * @see #min_index
4783
+ * @see #min
4784
+ *
4785
+ * @example
4786
+ * a = Numo::NArray[3,4,1,2]
4787
+ * a.argmin #=> 2
4788
+ *
4789
+ * b = Numo::NArray[[3,4,1],[2,0,5]]
4790
+ * b.argmin #=> 4
4791
+ * b.argmin(axis:1) #=> [2, 1]
4792
+ * b.argmin(axis:0) #=> [1, 1, 0]
4793
+ * b.at(b.argmin(axis:0), 0..-1) #=> [2, 0, 1]
4794
+ */
5774
4795
  rb_define_method(cT, "argmin", uint64_argmin, -1);
4796
+ /**
4797
+ * minmax of self.
4798
+ * @overload minmax(axis:nil, keepdims:false)
4799
+ * @param [Numeric,Array,Range] axis Finds min-max along the axis.
4800
+ * @param [TrueClass] keepdims (keyword) If true, the reduced axes are left in
4801
+ * the result array as dimensions with size one.
4802
+ * @return [Numo::UInt64,Numo::UInt64] min and max of self.
4803
+ */
5775
4804
  rb_define_method(cT, "minmax", uint64_minmax, -1);
5776
4805
  /**
5777
4806
  * Element-wise maximum of two arrays.
@@ -5788,12 +4817,82 @@ void Init_numo_uint64(void) {
5788
4817
  */
5789
4818
  rb_define_module_function(cT, "minimum", uint64_s_minimum, -1);
5790
4819
  rb_define_method(cT, "bincount", uint64_bincount, -1);
4820
+ /**
4821
+ * cumsum of self.
4822
+ * @overload cumsum(axis:nil, nan:false)
4823
+ * @param [Numeric,Array,Range] axis Performs cumsum along the axis.
4824
+ * @param [TrueClass] nan If true, apply NaN-aware algorithm (avoid NaN if exists).
4825
+ * @return [Numo::UInt64] cumsum of self.
4826
+ */
5791
4827
  rb_define_method(cT, "cumsum", uint64_cumsum, -1);
4828
+ /**
4829
+ * cumprod of self.
4830
+ * @overload cumprod(axis:nil, nan:false)
4831
+ * @param [Numeric,Array,Range] axis Performs cumprod along the axis.
4832
+ * @param [TrueClass] nan If true, apply NaN-aware algorithm (avoid NaN if exists).
4833
+ * @return [Numo::UInt64] cumprod of self.
4834
+ */
5792
4835
  rb_define_method(cT, "cumprod", uint64_cumprod, -1);
4836
+ /**
4837
+ * Binary mulsum.
4838
+ *
4839
+ * @overload mulsum(other, axis:nil, keepdims:false)
4840
+ * @param [Numo::NArray,Numeric] other
4841
+ * @param [Numeric,Array,Range] axis Performs mulsum along the axis.
4842
+ * @param [TrueClass] keepdims (keyword) If true, the reduced axes are left in
4843
+ * the result array as dimensions with size one.
4844
+ * @return [Numo::NArray] mulsum of self and other.
4845
+ */
5793
4846
  rb_define_method(cT, "mulsum", uint64_mulsum, -1);
4847
+ /**
4848
+ * Set linear sequence of numbers to self. The sequence is obtained from
4849
+ * beg+i*step
4850
+ * where i is 1-dimensional index.
4851
+ * @overload seq([beg,[step]])
4852
+ * @param [Numeric] beg beginning of sequence. (default=0)
4853
+ * @param [Numeric] step step of sequence. (default=1)
4854
+ * @return [Numo::UInt64] self.
4855
+ * @example
4856
+ * Numo::DFloat.new(6).seq(1,-0.2)
4857
+ * # => Numo::DFloat#shape=[6]
4858
+ * # [1, 0.8, 0.6, 0.4, 0.2, 0]
4859
+ *
4860
+ * Numo::DComplex.new(6).seq(1,-0.2+0.2i)
4861
+ * # => Numo::DComplex#shape=[6]
4862
+ * # [1+0i, 0.8+0.2i, 0.6+0.4i, 0.4+0.6i, 0.2+0.8i, 0+1i]
4863
+ */
5794
4864
  rb_define_method(cT, "seq", uint64_seq, -1);
4865
+ /**
4866
+ * Eye: Set a value to diagonal components, set 0 to non-diagonal components.
4867
+ * @overload eye([element,offset])
4868
+ * @param [Numeric] element Diagonal element to be stored. Default is 1.
4869
+ * @param [Integer] offset Diagonal offset from the main diagonal. The
4870
+ * default is 0. k>0 for diagonals above the main diagonal, and k<0
4871
+ * for diagonals below the main diagonal.
4872
+ * @return [Numo::UInt64] eye of self.
4873
+ */
5795
4874
  rb_define_method(cT, "eye", uint64_eye, -1);
5796
4875
  rb_define_alias(cT, "indgen", "seq");
4876
+ /**
4877
+ * Generate uniformly distributed random numbers on self narray.
4878
+ * @overload rand([[low],high])
4879
+ * @param [Numeric] low lower inclusive boundary of random numbers. (default=0)
4880
+ * @param [Numeric] high upper exclusive boundary of random numbers.
4881
+ * (default=1 or 1+1i for complex types)
4882
+ * @return [Numo::UInt64] self.
4883
+ * @example
4884
+ * Numo::DFloat.new(6).rand
4885
+ * # => Numo::DFloat#shape=[6]
4886
+ * # [0.0617545, 0.373067, 0.794815, 0.201042, 0.116041, 0.344032]
4887
+ *
4888
+ * Numo::DComplex.new(6).rand(5+5i)
4889
+ * # => Numo::DComplex#shape=[6]
4890
+ * # [2.69974+3.68908i, 0.825443+0.254414i, 0.540323+0.34354i, 4.52061+2.39322i, ...]
4891
+ *
4892
+ * Numo::Int32.new(6).rand(2,5)
4893
+ * # => Numo::Int32#shape=[6]
4894
+ * # [4, 3, 3, 2, 4, 2]
4895
+ */
5797
4896
  rb_define_method(cT, "rand", uint64_rand, -1);
5798
4897
  rb_define_method(cT, "poly", uint64_poly, -2);
5799
4898