numo-narray-alt 0.9.10 → 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 +2 -3
  39. metadata +23 -3
@@ -43,6 +43,26 @@ 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"
65
+ #include "mh/cumprod.h"
46
66
  #include "mh/mean.h"
47
67
  #include "mh/var.h"
48
68
  #include "mh/stddev.h"
@@ -50,6 +70,25 @@ extern VALUE cRT;
50
70
 
51
71
  typedef int32_t int32; // Type aliases for shorter notation
52
72
  // following the codebase naming convention.
73
+ DEF_NARRAY_CLIP_METHOD_FUNC(int32, numo_cInt32)
74
+ DEF_NARRAY_INT_SUM_METHOD_FUNC(int32, numo_cInt32, int64_t, numo_cInt64)
75
+ DEF_NARRAY_INT_PROD_METHOD_FUNC(int32, numo_cInt32, int64_t, numo_cInt64)
76
+ DEF_NARRAY_INT_MIN_METHOD_FUNC(int32, numo_cInt32)
77
+ DEF_NARRAY_INT_MAX_METHOD_FUNC(int32, numo_cInt32)
78
+ DEF_NARRAY_INT_PTP_METHOD_FUNC(int32, numo_cInt32)
79
+ DEF_NARRAY_INT_MAX_INDEX_METHOD_FUNC(int32)
80
+ DEF_NARRAY_INT_MIN_INDEX_METHOD_FUNC(int32)
81
+ DEF_NARRAY_INT_ARGMAX_METHOD_FUNC(int32)
82
+ DEF_NARRAY_INT_ARGMIN_METHOD_FUNC(int32)
83
+ DEF_NARRAY_INT_MAXIMUM_METHOD_FUNC(int32, numo_cInt32)
84
+ DEF_NARRAY_INT_MINIMUM_METHOD_FUNC(int32, numo_cInt32)
85
+ DEF_NARRAY_INT_MINMAX_METHOD_FUNC(int32, numo_cInt32)
86
+ DEF_NARRAY_INT_CUMSUM_METHOD_FUNC(int32, numo_cInt32)
87
+ DEF_NARRAY_INT_CUMPROD_METHOD_FUNC(int32, numo_cInt32)
88
+ DEF_NARRAY_INT_MULSUM_METHOD_FUNC(int32, numo_cInt32)
89
+ DEF_NARRAY_INT_SEQ_METHOD_FUNC(int32)
90
+ DEF_NARRAY_EYE_METHOD_FUNC(int32)
91
+ DEF_NARRAY_INT_RAND_METHOD_FUNC(int32)
53
92
  DEF_NARRAY_INT_MEAN_METHOD_FUNC(int32, numo_cInt32)
54
93
  DEF_NARRAY_INT_VAR_METHOD_FUNC(int32, numo_cInt32)
55
94
  DEF_NARRAY_INT_STDDEV_METHOD_FUNC(int32, numo_cInt32)
@@ -3698,710 +3737,6 @@ static VALUE int32_le(VALUE self, VALUE other) {
3698
3737
  }
3699
3738
  }
3700
3739
 
3701
- static void iter_int32_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_int32_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_int32_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 int32_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_int32_clip_min, STRIDE_LOOP, 2, 1, ain, aout };
3800
- ndfunc_t ndf_max = { iter_int32_clip_max, STRIDE_LOOP, 2, 1, ain, aout };
3801
- ndfunc_t ndf_both = { iter_int32_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_int32_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
- *(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::Int32] returns result of sum.
3837
- */
3838
- static VALUE int32_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_cInt64, 0 } };
3842
- ndfunc_t ndf = { iter_int32_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_int32_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
- *(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::Int32] returns result of prod.
3870
- */
3871
- static VALUE int32_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_cInt64, 0 } };
3875
- ndfunc_t ndf = { iter_int32_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_int32_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::Int32] returns result of min.
3903
- */
3904
- static VALUE int32_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_int32_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 int32_extract(v);
3915
- }
3916
-
3917
- static void iter_int32_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::Int32] returns result of max.
3936
- */
3937
- static VALUE int32_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_int32_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 int32_extract(v);
3948
- }
3949
-
3950
- static void iter_int32_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::Int32] returns result of ptp.
3969
- */
3970
- static VALUE int32_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_int32_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 int32_extract(v);
3981
- }
3982
-
3983
- #define idx_t int64_t
3984
- static void iter_int32_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_int32_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 int32_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_int32_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_int32_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_int32_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_int32_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 int32_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_int32_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_int32_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_int32_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_int32_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 int32_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_int32_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_int32_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_int32_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_int32_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 int32_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_int32_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_int32_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_int32_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::Int32,Numo::Int32] min and max of self.
4327
- */
4328
- static VALUE int32_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_int32_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_int32_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 int32_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_int32_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_int32_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 int32_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_int32_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
3740
  // ------- Integer count without weights -------
4406
3741
 
4407
3742
  static void iter_int32_bincount_32(na_loop_t* const lp) {
@@ -4635,463 +3970,6 @@ static VALUE int32_bincount(int argc, VALUE* argv, VALUE self) {
4635
3970
  }
4636
3971
  }
4637
3972
 
4638
- static void iter_int32_cumsum(na_loop_t* const lp) {
4639
- size_t i;
4640
- char *p1, *p2;
4641
- ssize_t s1, s2;
4642
- dtype x, y;
4643
-
4644
- INIT_COUNTER(lp, i);
4645
- INIT_PTR(lp, 0, p1, s1);
4646
- INIT_PTR(lp, 1, p2, s2);
4647
-
4648
- GET_DATA_STRIDE(p1, s1, dtype, x);
4649
- SET_DATA_STRIDE(p2, s2, dtype, x);
4650
- for (i--; i--;) {
4651
- GET_DATA_STRIDE(p1, s1, dtype, y);
4652
- m_cumsum(x, y);
4653
- SET_DATA_STRIDE(p2, s2, dtype, x);
4654
- }
4655
- }
4656
-
4657
- /*
4658
- cumsum of self.
4659
- @overload cumsum(axis:nil, nan:false)
4660
- @param [Numeric,Array,Range] axis Performs cumsum along the axis.
4661
- @param [TrueClass] nan If true, apply NaN-aware algorithm (avoid NaN if exists).
4662
- @return [Numo::Int32] cumsum of self.
4663
- */
4664
- static VALUE int32_cumsum(int argc, VALUE* argv, VALUE self) {
4665
- VALUE reduce;
4666
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { sym_reduce, 0 } };
4667
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
4668
- ndfunc_t ndf = {
4669
- iter_int32_cumsum, STRIDE_LOOP | NDF_FLAT_REDUCE | NDF_CUM, 2, 1, ain, aout
4670
- };
4671
-
4672
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
4673
-
4674
- return na_ndloop(&ndf, 2, self, reduce);
4675
- }
4676
-
4677
- static void iter_int32_cumprod(na_loop_t* const lp) {
4678
- size_t i;
4679
- char *p1, *p2;
4680
- ssize_t s1, s2;
4681
- dtype x, y;
4682
-
4683
- INIT_COUNTER(lp, i);
4684
- INIT_PTR(lp, 0, p1, s1);
4685
- INIT_PTR(lp, 1, p2, s2);
4686
-
4687
- GET_DATA_STRIDE(p1, s1, dtype, x);
4688
- SET_DATA_STRIDE(p2, s2, dtype, x);
4689
- for (i--; i--;) {
4690
- GET_DATA_STRIDE(p1, s1, dtype, y);
4691
- m_cumprod(x, y);
4692
- SET_DATA_STRIDE(p2, s2, dtype, x);
4693
- }
4694
- }
4695
-
4696
- /*
4697
- cumprod of self.
4698
- @overload cumprod(axis:nil, nan:false)
4699
- @param [Numeric,Array,Range] axis Performs cumprod along the axis.
4700
- @param [TrueClass] nan If true, apply NaN-aware algorithm (avoid NaN if exists).
4701
- @return [Numo::Int32] cumprod of self.
4702
- */
4703
- static VALUE int32_cumprod(int argc, VALUE* argv, VALUE self) {
4704
- VALUE reduce;
4705
- ndfunc_arg_in_t ain[2] = { { cT, 0 }, { sym_reduce, 0 } };
4706
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
4707
- ndfunc_t ndf = {
4708
- iter_int32_cumprod, STRIDE_LOOP | NDF_FLAT_REDUCE | NDF_CUM, 2, 1, ain, aout
4709
- };
4710
-
4711
- reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, 0);
4712
-
4713
- return na_ndloop(&ndf, 2, self, reduce);
4714
- }
4715
-
4716
- //
4717
- static void iter_int32_mulsum(na_loop_t* const lp) {
4718
- size_t i, n;
4719
- char *p1, *p2, *p3;
4720
- ssize_t s1, s2, s3;
4721
-
4722
- INIT_COUNTER(lp, n);
4723
- INIT_PTR(lp, 0, p1, s1);
4724
- INIT_PTR(lp, 1, p2, s2);
4725
- INIT_PTR(lp, 2, p3, s3);
4726
-
4727
- if (s3 == 0) {
4728
- dtype z;
4729
- // Reduce loop
4730
- GET_DATA(p3, dtype, z);
4731
- for (i = 0; i < n; i++) {
4732
- dtype x, y;
4733
- GET_DATA_STRIDE(p1, s1, dtype, x);
4734
- GET_DATA_STRIDE(p2, s2, dtype, y);
4735
- m_mulsum(x, y, z);
4736
- }
4737
- SET_DATA(p3, dtype, z);
4738
- return;
4739
- } else {
4740
- for (i = 0; i < n; i++) {
4741
- dtype x, y, z;
4742
- GET_DATA_STRIDE(p1, s1, dtype, x);
4743
- GET_DATA_STRIDE(p2, s2, dtype, y);
4744
- GET_DATA(p3, dtype, z);
4745
- m_mulsum(x, y, z);
4746
- SET_DATA_STRIDE(p3, s3, dtype, z);
4747
- }
4748
- }
4749
- }
4750
- //
4751
-
4752
- static VALUE int32_mulsum_self(int argc, VALUE* argv, VALUE self) {
4753
- VALUE v, reduce;
4754
- VALUE naryv[2];
4755
- ndfunc_arg_in_t ain[4] = { { cT, 0 }, { cT, 0 }, { sym_reduce, 0 }, { sym_init, 0 } };
4756
- ndfunc_arg_out_t aout[1] = { { cT, 0 } };
4757
- ndfunc_t ndf = { iter_int32_mulsum, STRIDE_LOOP_NIP, 4, 1, ain, aout };
4758
-
4759
- if (argc < 1) {
4760
- rb_raise(rb_eArgError, "wrong number of arguments (%d for >=1)", argc);
4761
- }
4762
- // should fix below: [self.ndim,other.ndim].max or?
4763
- naryv[0] = self;
4764
- naryv[1] = argv[0];
4765
- //
4766
- reduce = na_reduce_dimension(argc - 1, argv + 1, 2, naryv, &ndf, 0);
4767
- //
4768
-
4769
- v = na_ndloop(&ndf, 4, self, argv[0], reduce, m_mulsum_init);
4770
- return int32_extract(v);
4771
- }
4772
-
4773
- /*
4774
- Binary mulsum.
4775
-
4776
- @overload mulsum(other, axis:nil, keepdims:false)
4777
- @param [Numo::NArray,Numeric] other
4778
- @param [Numeric,Array,Range] axis Performs mulsum along the axis.
4779
- @param [TrueClass] keepdims (keyword) If true, the reduced axes are left in the result array
4780
- as dimensions with size one.
4781
- @return [Numo::NArray] mulsum of self and other.
4782
- */
4783
- static VALUE int32_mulsum(int argc, VALUE* argv, VALUE self) {
4784
- //
4785
- VALUE klass, v;
4786
- //
4787
- if (argc < 1) {
4788
- rb_raise(rb_eArgError, "wrong number of arguments (%d for >=1)", argc);
4789
- }
4790
- //
4791
- klass = na_upcast(rb_obj_class(self), rb_obj_class(argv[0]));
4792
- if (klass == cT) {
4793
- return int32_mulsum_self(argc, argv, self);
4794
- } else {
4795
- v = rb_funcall(klass, id_cast, 1, self);
4796
- //
4797
- return rb_funcallv_kw(v, rb_intern("mulsum"), argc, argv, RB_PASS_CALLED_KEYWORDS);
4798
- //
4799
- }
4800
- //
4801
- }
4802
-
4803
- typedef double seq_data_t;
4804
-
4805
- typedef double seq_count_t;
4806
-
4807
- typedef struct {
4808
- seq_data_t beg;
4809
- seq_data_t step;
4810
- seq_count_t count;
4811
- } seq_opt_t;
4812
-
4813
- static void iter_int32_seq(na_loop_t* const lp) {
4814
- size_t i;
4815
- char* p1;
4816
- ssize_t s1;
4817
- size_t* idx1;
4818
- dtype x;
4819
- seq_data_t beg, step;
4820
- seq_count_t c;
4821
- seq_opt_t* g;
4822
-
4823
- INIT_COUNTER(lp, i);
4824
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
4825
- g = (seq_opt_t*)(lp->opt_ptr);
4826
- beg = g->beg;
4827
- step = g->step;
4828
- c = g->count;
4829
- if (idx1) {
4830
- for (; i--;) {
4831
- x = f_seq(beg, step, c++);
4832
- *(dtype*)(p1 + *idx1) = x;
4833
- idx1++;
4834
- }
4835
- } else {
4836
- for (; i--;) {
4837
- x = f_seq(beg, step, c++);
4838
- *(dtype*)(p1) = x;
4839
- p1 += s1;
4840
- }
4841
- }
4842
- g->count = c;
4843
- }
4844
-
4845
- /*
4846
- Set linear sequence of numbers to self. The sequence is obtained from
4847
- beg+i*step
4848
- where i is 1-dimensional index.
4849
- @overload seq([beg,[step]])
4850
- @param [Numeric] beg beginning of sequence. (default=0)
4851
- @param [Numeric] step step of sequence. (default=1)
4852
- @return [Numo::Int32] self.
4853
- @example
4854
- Numo::DFloat.new(6).seq(1,-0.2)
4855
- # => Numo::DFloat#shape=[6]
4856
- # [1, 0.8, 0.6, 0.4, 0.2, 0]
4857
-
4858
- Numo::DComplex.new(6).seq(1,-0.2+0.2i)
4859
- # => Numo::DComplex#shape=[6]
4860
- # [1+0i, 0.8+0.2i, 0.6+0.4i, 0.4+0.6i, 0.2+0.8i, 0+1i]
4861
- */
4862
- static VALUE int32_seq(int argc, VALUE* argv, VALUE self) {
4863
- seq_opt_t* g;
4864
- VALUE vbeg = Qnil, vstep = Qnil;
4865
- ndfunc_arg_in_t ain[1] = { { OVERWRITE, 0 } };
4866
- ndfunc_t ndf = { iter_int32_seq, FULL_LOOP, 1, 0, ain, 0 };
4867
-
4868
- g = ALLOCA_N(seq_opt_t, 1);
4869
- g->beg = m_zero;
4870
- g->step = m_one;
4871
- g->count = 0;
4872
- rb_scan_args(argc, argv, "02", &vbeg, &vstep);
4873
- if (vbeg != Qnil) {
4874
- g->beg = NUM2DBL(vbeg);
4875
- }
4876
- if (vstep != Qnil) {
4877
- g->step = NUM2DBL(vstep);
4878
- }
4879
-
4880
- na_ndloop3(&ndf, g, 1, self);
4881
- return self;
4882
- }
4883
-
4884
- static void iter_int32_eye(na_loop_t* const lp) {
4885
- size_t n0, n1;
4886
- size_t i0, i1;
4887
- ssize_t s0, s1;
4888
- char *p0, *p1;
4889
- char* g;
4890
- ssize_t kofs;
4891
- dtype data;
4892
-
4893
- g = (char*)(lp->opt_ptr);
4894
- kofs = *(ssize_t*)g;
4895
- data = *(dtype*)(g + sizeof(ssize_t));
4896
-
4897
- n0 = lp->args[0].shape[0];
4898
- n1 = lp->args[0].shape[1];
4899
- s0 = lp->args[0].iter[0].step;
4900
- s1 = lp->args[0].iter[1].step;
4901
- p0 = NDL_PTR(lp, 0);
4902
-
4903
- for (i0 = 0; i0 < n0; i0++) {
4904
- p1 = p0;
4905
- for (i1 = 0; i1 < n1; i1++) {
4906
- *(dtype*)p1 = (i0 + kofs == i1) ? data : m_zero;
4907
- p1 += s1;
4908
- }
4909
- p0 += s0;
4910
- }
4911
- }
4912
-
4913
- /*
4914
- Eye: Set a value to diagonal components, set 0 to non-diagonal components.
4915
- @overload eye([element,offset])
4916
- @param [Numeric] element Diagonal element to be stored. Default is 1.
4917
- @param [Integer] offset Diagonal offset from the main diagonal. The
4918
- default is 0. k>0 for diagonals above the main diagonal, and k<0
4919
- for diagonals below the main diagonal.
4920
- @return [Numo::Int32] eye of self.
4921
- */
4922
- static VALUE int32_eye(int argc, VALUE* argv, VALUE self) {
4923
- ndfunc_arg_in_t ain[1] = { { OVERWRITE, 2 } };
4924
- ndfunc_t ndf = { iter_int32_eye, NO_LOOP, 1, 0, ain, 0 };
4925
- ssize_t kofs;
4926
- dtype data;
4927
- char* g;
4928
- int nd;
4929
- narray_t* na;
4930
-
4931
- // check arguments
4932
- if (argc > 2) {
4933
- rb_raise(rb_eArgError, "too many arguments (%d for 0..2)", argc);
4934
- } else if (argc == 2) {
4935
- data = m_num_to_data(argv[0]);
4936
- kofs = NUM2SSIZET(argv[1]);
4937
- } else if (argc == 1) {
4938
- data = m_num_to_data(argv[0]);
4939
- kofs = 0;
4940
- } else {
4941
- data = m_one;
4942
- kofs = 0;
4943
- }
4944
-
4945
- GetNArray(self, na);
4946
- nd = na->ndim;
4947
- if (nd < 2) {
4948
- rb_raise(nary_eDimensionError, "less than 2-d array");
4949
- }
4950
-
4951
- // Diagonal offset from the main diagonal.
4952
- if (kofs >= 0) {
4953
- if ((size_t)(kofs) >= na->shape[nd - 1]) {
4954
- rb_raise(
4955
- rb_eArgError,
4956
- "invalid diagonal offset(%" SZF "d) for "
4957
- "last dimension size(%" SZF "d)",
4958
- kofs, na->shape[nd - 1]
4959
- );
4960
- }
4961
- } else {
4962
- if ((size_t)(-kofs) >= na->shape[nd - 2]) {
4963
- rb_raise(
4964
- rb_eArgError,
4965
- "invalid diagonal offset(%" SZF "d) for "
4966
- "last-1 dimension size(%" SZF "d)",
4967
- kofs, na->shape[nd - 2]
4968
- );
4969
- }
4970
- }
4971
-
4972
- g = ALLOCA_N(char, sizeof(ssize_t) + sizeof(dtype));
4973
- *(ssize_t*)g = kofs;
4974
- *(dtype*)(g + sizeof(ssize_t)) = data;
4975
-
4976
- na_ndloop3(&ndf, g, 1, self);
4977
- return self;
4978
- }
4979
-
4980
- #define HWID (sizeof(dtype) * 4)
4981
-
4982
- static int msb_pos(uint32_t a) {
4983
- int width = HWID;
4984
- int pos = 0;
4985
- uint32_t mask = (((dtype)1 << HWID) - 1) << HWID;
4986
-
4987
- if (a == 0) {
4988
- return -1;
4989
- }
4990
-
4991
- while (width) {
4992
- if (a & mask) {
4993
- pos += width;
4994
- } else {
4995
- mask >>= width;
4996
- }
4997
- width >>= 1;
4998
- mask &= mask << width;
4999
- }
5000
- return pos;
5001
- }
5002
-
5003
- /* generates a random number on [0,max) */
5004
- inline static dtype m_rand(uint32_t max, int shift) {
5005
- uint32_t x;
5006
- do {
5007
- x = gen_rand32();
5008
- x >>= shift;
5009
- } while (x >= max);
5010
- return x;
5011
- }
5012
-
5013
- typedef struct {
5014
- dtype low;
5015
- uint32_t max;
5016
- } rand_opt_t;
5017
-
5018
- static void iter_int32_rand(na_loop_t* const lp) {
5019
- size_t i;
5020
- char* p1;
5021
- ssize_t s1;
5022
- size_t* idx1;
5023
- dtype x;
5024
- rand_opt_t* g;
5025
- dtype low;
5026
- uint32_t max;
5027
- int shift;
5028
-
5029
- INIT_COUNTER(lp, i);
5030
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
5031
- g = (rand_opt_t*)(lp->opt_ptr);
5032
- low = g->low;
5033
- max = g->max;
5034
- shift = 31 - msb_pos(max);
5035
-
5036
- if (idx1) {
5037
- for (; i--;) {
5038
- x = m_add(m_rand(max, shift), low);
5039
- SET_DATA_INDEX(p1, idx1, dtype, x);
5040
- }
5041
- } else {
5042
- for (; i--;) {
5043
- x = m_add(m_rand(max, shift), low);
5044
- SET_DATA_STRIDE(p1, s1, dtype, x);
5045
- }
5046
- }
5047
- }
5048
-
5049
- /*
5050
- Generate uniformly distributed random numbers on self narray.
5051
- @overload rand([[low],high])
5052
- @param [Numeric] low lower inclusive boundary of random numbers. (default=0)
5053
- @param [Numeric] high upper exclusive boundary of random numbers. (default=1 or 1+1i for
5054
- complex types)
5055
- @return [Numo::Int32] self.
5056
- @example
5057
- Numo::DFloat.new(6).rand
5058
- # => Numo::DFloat#shape=[6]
5059
- # [0.0617545, 0.373067, 0.794815, 0.201042, 0.116041, 0.344032]
5060
-
5061
- Numo::DComplex.new(6).rand(5+5i)
5062
- # => Numo::DComplex#shape=[6]
5063
- # [2.69974+3.68908i, 0.825443+0.254414i, 0.540323+0.34354i, 4.52061+2.39322i, ...]
5064
-
5065
- Numo::Int32.new(6).rand(2,5)
5066
- # => Numo::Int32#shape=[6]
5067
- # [4, 3, 3, 2, 4, 2]
5068
- */
5069
- static VALUE int32_rand(int argc, VALUE* argv, VALUE self) {
5070
- rand_opt_t g;
5071
- VALUE v1 = Qnil, v2 = Qnil;
5072
- dtype high;
5073
- ndfunc_arg_in_t ain[1] = { { OVERWRITE, 0 } };
5074
- ndfunc_t ndf = { iter_int32_rand, FULL_LOOP, 1, 0, ain, 0 };
5075
-
5076
- rb_scan_args(argc, argv, "11", &v1, &v2);
5077
- if (v2 == Qnil) {
5078
- g.low = m_zero;
5079
- g.max = high = m_num_to_data(v1);
5080
-
5081
- } else {
5082
- g.low = m_num_to_data(v1);
5083
- high = m_num_to_data(v2);
5084
- g.max = m_sub(high, g.low);
5085
- }
5086
-
5087
- if (high <= g.low) {
5088
- rb_raise(rb_eArgError, "high must be larger than low");
5089
- }
5090
-
5091
- na_ndloop3(&ndf, &g, 1, self);
5092
- return self;
5093
- }
5094
-
5095
3973
  static void iter_int32_poly(na_loop_t* const lp) {
5096
3974
  size_t i;
5097
3975
  dtype x, y, a;
@@ -5764,16 +4642,170 @@ void Init_numo_int32(void) {
5764
4642
  rb_define_alias(cT, ">=", "ge");
5765
4643
  rb_define_alias(cT, "<", "lt");
5766
4644
  rb_define_alias(cT, "<=", "le");
4645
+ /**
4646
+ * Clip array elements by [min,max].
4647
+ * If either of min or max is nil, one side is clipped.
4648
+ * @overload clip(min,max)
4649
+ * @param [Numo::NArray,Numeric] min
4650
+ * @param [Numo::NArray,Numeric] max
4651
+ * @return [Numo::NArray] result of clip.
4652
+ * * * * * * * *
4653
+ * @example
4654
+ * a = Numo::Int32.new(10).seq
4655
+ * # => Numo::Int32#shape=[10]
4656
+ * # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
4657
+ *
4658
+ * a.clip(1,8)
4659
+ * # => Numo::Int32#shape=[10]
4660
+ * # [1, 1, 2, 3, 4, 5, 6, 7, 8, 8]
4661
+ *
4662
+ * a.inplace.clip(3,6)
4663
+ * a
4664
+ * # => Numo::Int32#shape=[10]
4665
+ * # [3, 3, 3, 3, 4, 5, 6, 6, 6, 6]
4666
+ *
4667
+ * b = Numo::Int32.new(10).seq
4668
+ * # => Numo::Int32#shape=[10]
4669
+ * # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
4670
+ *
4671
+ * b.clip([3,4,1,1,1,4,4,4,4,4], 8)
4672
+ * # => Numo::Int32#shape=[10]
4673
+ * # [3, 4, 2, 3, 4, 5, 6, 7, 8, 8]
4674
+ */
5767
4675
  rb_define_method(cT, "clip", int32_clip, 2);
4676
+ /**
4677
+ * sum of self.
4678
+ * @overload sum(axis:nil, keepdims:false)
4679
+ * @param [Numeric,Array,Range] axis Performs sum along the axis.
4680
+ * @param [TrueClass] keepdims If true, the reduced axes are left in the result array as
4681
+ * dimensions with size one.
4682
+ * @return [Numo::Int32] returns result of sum.
4683
+ */
5768
4684
  rb_define_method(cT, "sum", int32_sum, -1);
4685
+ /**
4686
+ * prod of self.
4687
+ * @overload prod(axis:nil, keepdims:false)
4688
+ * @param [Numeric,Array,Range] axis Performs prod along the axis.
4689
+ * @param [TrueClass] keepdims If true, the reduced axes are left in the result array as
4690
+ * dimensions with size one.
4691
+ * @return [Numo::Int64] returns result of prod.
4692
+ */
5769
4693
  rb_define_method(cT, "prod", int32_prod, -1);
4694
+ /**
4695
+ * min of self.
4696
+ * @overload min(axis:nil, keepdims:false)
4697
+ * @param [Numeric,Array,Range] axis Performs min along the axis.
4698
+ * @param [TrueClass] keepdims If true, the reduced axes are left in the result array as
4699
+ * dimensions with size one.
4700
+ * @return [Numo::Int32] returns result of min.
4701
+ */
5770
4702
  rb_define_method(cT, "min", int32_min, -1);
4703
+ /**
4704
+ * max of self.
4705
+ * @overload max(axis:nil, keepdims:false)
4706
+ * @param [Numeric,Array,Range] axis Performs max along the axis.
4707
+ * @param [TrueClass] keepdims If true, the reduced axes are left in the result array as
4708
+ * dimensions with size one.
4709
+ * @return [Numo::Int32] returns result of max.
4710
+ */
5771
4711
  rb_define_method(cT, "max", int32_max, -1);
4712
+ /**
4713
+ * ptp of self.
4714
+ * @overload ptp(axis:nil, keepdims:false)
4715
+ * @param [Numeric,Array,Range] axis Performs ptp along the axis.
4716
+ * @param [TrueClass] keepdims If true, the reduced axes are left in the result array as
4717
+ * dimensions with size one.
4718
+ * @return [Numo::Int32] returns result of ptp.
4719
+ */
5772
4720
  rb_define_method(cT, "ptp", int32_ptp, -1);
4721
+ /**
4722
+ * Index of the maximum value.
4723
+ * @overload max_index(axis:nil)
4724
+ * @param [Numeric,Array,Range] axis Finds maximum values along the axis and
4725
+ * returns **flat 1-d indices**.
4726
+ * @return [Integer,Numo::Int] returns result indices.
4727
+ * @see #argmax
4728
+ * @see #max
4729
+ *
4730
+ * @example
4731
+ * a = Numo::NArray[3,4,1,2]
4732
+ * a.max_index #=> 1
4733
+ *
4734
+ * b = Numo::NArray[[3,4,1],[2,0,5]]
4735
+ * b.max_index #=> 5
4736
+ * b.max_index(axis:1) #=> [1, 5]
4737
+ * b.max_index(axis:0) #=> [0, 1, 5]
4738
+ * b[b.max_index(axis:0)] #=> [3, 4, 5]
4739
+ */
5773
4740
  rb_define_method(cT, "max_index", int32_max_index, -1);
4741
+ /**
4742
+ * Index of the minimum value.
4743
+ * @overload min_index(axis:nil)
4744
+ * @param [Numeric,Array,Range] axis Finds minimum values along the axis and
4745
+ * returns **flat 1-d indices**.
4746
+ * @return [Integer,Numo::Int] returns result indices.
4747
+ * @see #argmin
4748
+ * @see #min
4749
+ *
4750
+ * @example
4751
+ * a = Numo::NArray[3,4,1,2]
4752
+ * a.min_index #=> 2
4753
+ *
4754
+ * b = Numo::NArray[[3,4,1],[2,0,5]]
4755
+ * b.min_index #=> 4
4756
+ * b.min_index(axis:1) #=> [2, 4]
4757
+ * b.min_index(axis:0) #=> [3, 4, 2]
4758
+ * b[b.min_index(axis:0)] #=> [2, 0, 1]
4759
+ */
5774
4760
  rb_define_method(cT, "min_index", int32_min_index, -1);
4761
+ /**
4762
+ * Index of the maximum value.
4763
+ * @overload argmax(axis:nil)
4764
+ * @param [Numeric,Array,Range] axis Finds maximum values along the axis and
4765
+ * returns **indices along the axis**.
4766
+ * @return [Integer,Numo::Int] returns the result indices.
4767
+ * @see #max_index
4768
+ * @see #max
4769
+ *
4770
+ * @example
4771
+ * a = Numo::NArray[3,4,1,2]
4772
+ * a.argmax #=> 1
4773
+ *
4774
+ * b = Numo::NArray[[3,4,1],[2,0,5]]
4775
+ * b.argmax #=> 5
4776
+ * b.argmax(axis:1) #=> [1, 2]
4777
+ * b.argmax(axis:0) #=> [0, 0, 1]
4778
+ * b.at(b.argmax(axis:0), 0..-1) #=> [3, 4, 5]
4779
+ */
5775
4780
  rb_define_method(cT, "argmax", int32_argmax, -1);
4781
+ /**
4782
+ * Index of the minimum value.
4783
+ * @overload argmin(axis:nil)
4784
+ * @param [Numeric,Array,Range] axis Finds minimum values along the axis and
4785
+ * returns **indices along the axis**.
4786
+ * @return [Integer,Numo::Int] returns the result indices.
4787
+ * @see #min_index
4788
+ * @see #min
4789
+ *
4790
+ * @example
4791
+ * a = Numo::NArray[3,4,1,2]
4792
+ * a.argmin #=> 2
4793
+ *
4794
+ * b = Numo::NArray[[3,4,1],[2,0,5]]
4795
+ * b.argmin #=> 4
4796
+ * b.argmin(axis:1) #=> [2, 1]
4797
+ * b.argmin(axis:0) #=> [1, 1, 0]
4798
+ * b.at(b.argmin(axis:0), 0..-1) #=> [2, 0, 1]
4799
+ */
5776
4800
  rb_define_method(cT, "argmin", int32_argmin, -1);
4801
+ /**
4802
+ * minmax of self.
4803
+ * @overload minmax(axis:nil, keepdims:false)
4804
+ * @param [Numeric,Array,Range] axis Finds min-max along the axis.
4805
+ * @param [TrueClass] keepdims (keyword) If true, the reduced axes are left in
4806
+ * the result array as dimensions with size one.
4807
+ * @return [Numo::Int32,Numo::Int32] min and max of self.
4808
+ */
5777
4809
  rb_define_method(cT, "minmax", int32_minmax, -1);
5778
4810
  /**
5779
4811
  * Element-wise maximum of two arrays.
@@ -5790,12 +4822,82 @@ void Init_numo_int32(void) {
5790
4822
  */
5791
4823
  rb_define_module_function(cT, "minimum", int32_s_minimum, -1);
5792
4824
  rb_define_method(cT, "bincount", int32_bincount, -1);
4825
+ /**
4826
+ * cumsum of self.
4827
+ * @overload cumsum(axis:nil, nan:false)
4828
+ * @param [Numeric,Array,Range] axis Performs cumsum along the axis.
4829
+ * @param [TrueClass] nan If true, apply NaN-aware algorithm (avoid NaN if exists).
4830
+ * @return [Numo::Int32] cumsum of self.
4831
+ */
5793
4832
  rb_define_method(cT, "cumsum", int32_cumsum, -1);
4833
+ /**
4834
+ * cumprod of self.
4835
+ * @overload cumprod(axis:nil, nan:false)
4836
+ * @param [Numeric,Array,Range] axis Performs cumprod along the axis.
4837
+ * @param [TrueClass] nan If true, apply NaN-aware algorithm (avoid NaN if exists).
4838
+ * @return [Numo::Int32] cumprod of self.
4839
+ */
5794
4840
  rb_define_method(cT, "cumprod", int32_cumprod, -1);
4841
+ /**
4842
+ * Binary mulsum.
4843
+ *
4844
+ * @overload mulsum(other, axis:nil, keepdims:false)
4845
+ * @param [Numo::NArray,Numeric] other
4846
+ * @param [Numeric,Array,Range] axis Performs mulsum along the axis.
4847
+ * @param [TrueClass] keepdims (keyword) If true, the reduced axes are left in
4848
+ * the result array as dimensions with size one.
4849
+ * @return [Numo::NArray] mulsum of self and other.
4850
+ */
5795
4851
  rb_define_method(cT, "mulsum", int32_mulsum, -1);
4852
+ /**
4853
+ * Set linear sequence of numbers to self. The sequence is obtained from
4854
+ * beg+i*step
4855
+ * where i is 1-dimensional index.
4856
+ * @overload seq([beg,[step]])
4857
+ * @param [Numeric] beg beginning of sequence. (default=0)
4858
+ * @param [Numeric] step step of sequence. (default=1)
4859
+ * @return [Numo::Int32] self.
4860
+ * @example
4861
+ * Numo::DFloat.new(6).seq(1,-0.2)
4862
+ * # => Numo::DFloat#shape=[6]
4863
+ * # [1, 0.8, 0.6, 0.4, 0.2, 0]
4864
+ *
4865
+ * Numo::DComplex.new(6).seq(1,-0.2+0.2i)
4866
+ * # => Numo::DComplex#shape=[6]
4867
+ * # [1+0i, 0.8+0.2i, 0.6+0.4i, 0.4+0.6i, 0.2+0.8i, 0+1i]
4868
+ */
5796
4869
  rb_define_method(cT, "seq", int32_seq, -1);
4870
+ /**
4871
+ * Eye: Set a value to diagonal components, set 0 to non-diagonal components.
4872
+ * @overload eye([element,offset])
4873
+ * @param [Numeric] element Diagonal element to be stored. Default is 1.
4874
+ * @param [Integer] offset Diagonal offset from the main diagonal. The
4875
+ * default is 0. k>0 for diagonals above the main diagonal, and k<0
4876
+ * for diagonals below the main diagonal.
4877
+ * @return [Numo::Int32] eye of self.
4878
+ */
5797
4879
  rb_define_method(cT, "eye", int32_eye, -1);
5798
4880
  rb_define_alias(cT, "indgen", "seq");
4881
+ /**
4882
+ * Generate uniformly distributed random numbers on self narray.
4883
+ * @overload rand([[low],high])
4884
+ * @param [Numeric] low lower inclusive boundary of random numbers. (default=0)
4885
+ * @param [Numeric] high upper exclusive boundary of random numbers.
4886
+ * (default=1 or 1+1i for complex types)
4887
+ * @return [Numo::Int32] self.
4888
+ * @example
4889
+ * Numo::DFloat.new(6).rand
4890
+ * # => Numo::DFloat#shape=[6]
4891
+ * # [0.0617545, 0.373067, 0.794815, 0.201042, 0.116041, 0.344032]
4892
+ *
4893
+ * Numo::DComplex.new(6).rand(5+5i)
4894
+ * # => Numo::DComplex#shape=[6]
4895
+ * # [2.69974+3.68908i, 0.825443+0.254414i, 0.540323+0.34354i, 4.52061+2.39322i, ...]
4896
+ *
4897
+ * Numo::Int32.new(6).rand(2,5)
4898
+ * # => Numo::Int32#shape=[6]
4899
+ * # [4, 3, 3, 2, 4, 2]
4900
+ */
5799
4901
  rb_define_method(cT, "rand", int32_rand, -1);
5800
4902
  rb_define_method(cT, "poly", int32_poly, -2);
5801
4903