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,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 int64_t int64; // Type aliases for shorter notation
52
71
  // following the codebase naming convention.
72
+ DEF_NARRAY_CLIP_METHOD_FUNC(int64, numo_cInt64)
73
+ DEF_NARRAY_INT_SUM_METHOD_FUNC(int64, numo_cInt64, int64_t, numo_cInt64)
74
+ DEF_NARRAY_INT_PROD_METHOD_FUNC(int64, numo_cInt64, int64_t, numo_cInt64)
75
+ DEF_NARRAY_INT_MIN_METHOD_FUNC(int64, numo_cInt64)
76
+ DEF_NARRAY_INT_MAX_METHOD_FUNC(int64, numo_cInt64)
77
+ DEF_NARRAY_INT_PTP_METHOD_FUNC(int64, numo_cInt64)
78
+ DEF_NARRAY_INT_MAX_INDEX_METHOD_FUNC(int64)
79
+ DEF_NARRAY_INT_MIN_INDEX_METHOD_FUNC(int64)
80
+ DEF_NARRAY_INT_ARGMAX_METHOD_FUNC(int64)
81
+ DEF_NARRAY_INT_ARGMIN_METHOD_FUNC(int64)
82
+ DEF_NARRAY_INT_MAXIMUM_METHOD_FUNC(int64, numo_cInt64)
83
+ DEF_NARRAY_INT_MINIMUM_METHOD_FUNC(int64, numo_cInt64)
84
+ DEF_NARRAY_INT_MINMAX_METHOD_FUNC(int64, numo_cInt64)
85
+ DEF_NARRAY_INT_CUMSUM_METHOD_FUNC(int64, numo_cInt64)
86
+ DEF_NARRAY_INT_CUMPROD_METHOD_FUNC(int64, numo_cInt64)
87
+ DEF_NARRAY_INT_MULSUM_METHOD_FUNC(int64, numo_cInt64)
88
+ DEF_NARRAY_INT_SEQ_METHOD_FUNC(int64)
89
+ DEF_NARRAY_EYE_METHOD_FUNC(int64)
90
+ DEF_NARRAY_INT64_RAND_METHOD_FUNC(int64)
53
91
  DEF_NARRAY_INT_MEAN_METHOD_FUNC(int64, numo_cInt64)
54
92
  DEF_NARRAY_INT_VAR_METHOD_FUNC(int64, numo_cInt64)
55
93
  DEF_NARRAY_INT_STDDEV_METHOD_FUNC(int64, numo_cInt64)
@@ -3698,710 +3736,6 @@ static VALUE int64_le(VALUE self, VALUE other) {
3698
3736
  }
3699
3737
  }
3700
3738
 
3701
- static void iter_int64_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_int64_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_int64_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 int64_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_int64_clip_min, STRIDE_LOOP, 2, 1, ain, aout };
3800
- ndfunc_t ndf_max = { iter_int64_clip_max, STRIDE_LOOP, 2, 1, ain, aout };
3801
- ndfunc_t ndf_both = { iter_int64_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_int64_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::Int64] returns result of sum.
3837
- */
3838
- static VALUE int64_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_int64_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_int64_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::Int64] returns result of prod.
3870
- */
3871
- static VALUE int64_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_int64_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_int64_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::Int64] returns result of min.
3903
- */
3904
- static VALUE int64_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_int64_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 int64_extract(v);
3915
- }
3916
-
3917
- static void iter_int64_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::Int64] returns result of max.
3936
- */
3937
- static VALUE int64_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_int64_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 int64_extract(v);
3948
- }
3949
-
3950
- static void iter_int64_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::Int64] returns result of ptp.
3969
- */
3970
- static VALUE int64_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_int64_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 int64_extract(v);
3981
- }
3982
-
3983
- #define idx_t int64_t
3984
- static void iter_int64_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_int64_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 int64_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_int64_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_int64_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_int64_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_int64_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 int64_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_int64_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_int64_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_int64_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_int64_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 int64_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_int64_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_int64_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_int64_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_int64_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 int64_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_int64_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_int64_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_int64_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::Int64,Numo::Int64] min and max of self.
4327
- */
4328
- static VALUE int64_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_int64_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_int64_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 int64_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_int64_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_int64_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 int64_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_int64_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_int64_bincount_32(na_loop_t* const lp) {
@@ -4635,465 +3969,6 @@ static VALUE int64_bincount(int argc, VALUE* argv, VALUE self) {
4635
3969
  }
4636
3970
  }
4637
3971
 
4638
- static void iter_int64_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::Int64] cumsum of self.
4663
- */
4664
- static VALUE int64_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_int64_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_int64_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::Int64] cumprod of self.
4702
- */
4703
- static VALUE int64_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_int64_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_int64_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 int64_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_int64_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 int64_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 int64_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 int64_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_int64_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::Int64] 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 int64_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_int64_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_int64_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::Int64] eye of self.
4921
- */
4922
- static VALUE int64_eye(int argc, VALUE* argv, VALUE self) {
4923
- ndfunc_arg_in_t ain[1] = { { OVERWRITE, 2 } };
4924
- ndfunc_t ndf = { iter_int64_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(uint64_t a) {
4983
- int width = HWID;
4984
- int pos = 0;
4985
- uint64_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(uint64_t max, int shift) {
5005
- uint64_t x;
5006
- do {
5007
- x = gen_rand32();
5008
- x <<= 32;
5009
- x |= gen_rand32();
5010
- x >>= shift;
5011
- } while (x >= max);
5012
- return x;
5013
- }
5014
-
5015
- typedef struct {
5016
- dtype low;
5017
- uint64_t max;
5018
- } rand_opt_t;
5019
-
5020
- static void iter_int64_rand(na_loop_t* const lp) {
5021
- size_t i;
5022
- char* p1;
5023
- ssize_t s1;
5024
- size_t* idx1;
5025
- dtype x;
5026
- rand_opt_t* g;
5027
- dtype low;
5028
- uint64_t max;
5029
- int shift;
5030
-
5031
- INIT_COUNTER(lp, i);
5032
- INIT_PTR_IDX(lp, 0, p1, s1, idx1);
5033
- g = (rand_opt_t*)(lp->opt_ptr);
5034
- low = g->low;
5035
- max = g->max;
5036
- shift = 63 - msb_pos(max);
5037
-
5038
- if (idx1) {
5039
- for (; i--;) {
5040
- x = m_add(m_rand(max, shift), low);
5041
- SET_DATA_INDEX(p1, idx1, dtype, x);
5042
- }
5043
- } else {
5044
- for (; i--;) {
5045
- x = m_add(m_rand(max, shift), low);
5046
- SET_DATA_STRIDE(p1, s1, dtype, x);
5047
- }
5048
- }
5049
- }
5050
-
5051
- /*
5052
- Generate uniformly distributed random numbers on self narray.
5053
- @overload rand([[low],high])
5054
- @param [Numeric] low lower inclusive boundary of random numbers. (default=0)
5055
- @param [Numeric] high upper exclusive boundary of random numbers. (default=1 or 1+1i for
5056
- complex types)
5057
- @return [Numo::Int64] self.
5058
- @example
5059
- Numo::DFloat.new(6).rand
5060
- # => Numo::DFloat#shape=[6]
5061
- # [0.0617545, 0.373067, 0.794815, 0.201042, 0.116041, 0.344032]
5062
-
5063
- Numo::DComplex.new(6).rand(5+5i)
5064
- # => Numo::DComplex#shape=[6]
5065
- # [2.69974+3.68908i, 0.825443+0.254414i, 0.540323+0.34354i, 4.52061+2.39322i, ...]
5066
-
5067
- Numo::Int32.new(6).rand(2,5)
5068
- # => Numo::Int32#shape=[6]
5069
- # [4, 3, 3, 2, 4, 2]
5070
- */
5071
- static VALUE int64_rand(int argc, VALUE* argv, VALUE self) {
5072
- rand_opt_t g;
5073
- VALUE v1 = Qnil, v2 = Qnil;
5074
- dtype high;
5075
- ndfunc_arg_in_t ain[1] = { { OVERWRITE, 0 } };
5076
- ndfunc_t ndf = { iter_int64_rand, FULL_LOOP, 1, 0, ain, 0 };
5077
-
5078
- rb_scan_args(argc, argv, "11", &v1, &v2);
5079
- if (v2 == Qnil) {
5080
- g.low = m_zero;
5081
- g.max = high = m_num_to_data(v1);
5082
-
5083
- } else {
5084
- g.low = m_num_to_data(v1);
5085
- high = m_num_to_data(v2);
5086
- g.max = m_sub(high, g.low);
5087
- }
5088
-
5089
- if (high <= g.low) {
5090
- rb_raise(rb_eArgError, "high must be larger than low");
5091
- }
5092
-
5093
- na_ndloop3(&ndf, &g, 1, self);
5094
- return self;
5095
- }
5096
-
5097
3972
  static void iter_int64_poly(na_loop_t* const lp) {
5098
3973
  size_t i;
5099
3974
  dtype x, y, a;
@@ -5766,16 +4641,170 @@ void Init_numo_int64(void) {
5766
4641
  rb_define_alias(cT, ">=", "ge");
5767
4642
  rb_define_alias(cT, "<", "lt");
5768
4643
  rb_define_alias(cT, "<=", "le");
4644
+ /**
4645
+ * Clip array elements by [min,max].
4646
+ * If either of min or max is nil, one side is clipped.
4647
+ * @overload clip(min,max)
4648
+ * @param [Numo::NArray,Numeric] min
4649
+ * @param [Numo::NArray,Numeric] max
4650
+ * @return [Numo::NArray] result of clip.
4651
+ *
4652
+ * @example
4653
+ * a = Numo::Int32.new(10).seq
4654
+ * # => Numo::Int32#shape=[10]
4655
+ * # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
4656
+ *
4657
+ * a.clip(1,8)
4658
+ * # => Numo::Int32#shape=[10]
4659
+ * # [1, 1, 2, 3, 4, 5, 6, 7, 8, 8]
4660
+ *
4661
+ * a.inplace.clip(3,6)
4662
+ * a
4663
+ * # => Numo::Int32#shape=[10]
4664
+ * # [3, 3, 3, 3, 4, 5, 6, 6, 6, 6]
4665
+ *
4666
+ * b = Numo::Int32.new(10).seq
4667
+ * # => Numo::Int32#shape=[10]
4668
+ * # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
4669
+ *
4670
+ * b.clip([3,4,1,1,1,4,4,4,4,4], 8)
4671
+ * # => Numo::Int32#shape=[10]
4672
+ * # [3, 4, 2, 3, 4, 5, 6, 7, 8, 8]
4673
+ */
5769
4674
  rb_define_method(cT, "clip", int64_clip, 2);
4675
+ /**
4676
+ * sum of self.
4677
+ * @overload sum(axis:nil, keepdims:false)
4678
+ * @param [Numeric,Array,Range] axis Performs sum along the axis.
4679
+ * @param [TrueClass] keepdims If true, the reduced axes are left in the result array as
4680
+ * dimensions with size one.
4681
+ * @return [Numo::Int64] returns result of sum.
4682
+ */
5770
4683
  rb_define_method(cT, "sum", int64_sum, -1);
4684
+ /**
4685
+ * prod of self.
4686
+ * @overload prod(axis:nil, keepdims:false)
4687
+ * @param [Numeric,Array,Range] axis Performs prod along the axis.
4688
+ * @param [TrueClass] keepdims If true, the reduced axes are left in the result array as
4689
+ * dimensions with size one.
4690
+ * @return [Numo::Int64] returns result of prod.
4691
+ */
5771
4692
  rb_define_method(cT, "prod", int64_prod, -1);
4693
+ /**
4694
+ * min of self.
4695
+ * @overload min(axis:nil, keepdims:false)
4696
+ * @param [Numeric,Array,Range] axis Performs min along the axis.
4697
+ * @param [TrueClass] keepdims If true, the reduced axes are left in the result array as
4698
+ * dimensions with size one.
4699
+ * @return [Numo::Int64] returns result of min.
4700
+ */
5772
4701
  rb_define_method(cT, "min", int64_min, -1);
4702
+ /**
4703
+ * max of self.
4704
+ * @overload max(axis:nil, keepdims:false)
4705
+ * @param [Numeric,Array,Range] axis Performs max along the axis.
4706
+ * @param [TrueClass] keepdims If true, the reduced axes are left in the result array as
4707
+ * dimensions with size one.
4708
+ * @return [Numo::Int64] returns result of max.
4709
+ */
5773
4710
  rb_define_method(cT, "max", int64_max, -1);
4711
+ /**
4712
+ * ptp of self.
4713
+ * @overload ptp(axis:nil, keepdims:false)
4714
+ * @param [Numeric,Array,Range] axis Performs ptp along the axis.
4715
+ * @param [TrueClass] keepdims If true, the reduced axes are left in the result array as
4716
+ * dimensions with size one.
4717
+ * @return [Numo::Int64] returns result of ptp.
4718
+ */
5774
4719
  rb_define_method(cT, "ptp", int64_ptp, -1);
4720
+ /**
4721
+ * Index of the maximum value.
4722
+ * @overload max_index(axis:nil)
4723
+ * @param [Numeric,Array,Range] axis Finds maximum values along the axis and
4724
+ * returns **flat 1-d indices**.
4725
+ * @return [Integer,Numo::Int] returns result indices.
4726
+ * @see #argmax
4727
+ * @see #max
4728
+ *
4729
+ * @example
4730
+ * a = Numo::NArray[3,4,1,2]
4731
+ * a.max_index #=> 1
4732
+ *
4733
+ * b = Numo::NArray[[3,4,1],[2,0,5]]
4734
+ * b.max_index #=> 5
4735
+ * b.max_index(axis:1) #=> [1, 5]
4736
+ * b.max_index(axis:0) #=> [0, 1, 5]
4737
+ * b[b.max_index(axis:0)] #=> [3, 4, 5]
4738
+ */
5775
4739
  rb_define_method(cT, "max_index", int64_max_index, -1);
4740
+ /**
4741
+ * Index of the minimum value.
4742
+ * @overload min_index(axis:nil)
4743
+ * @param [Numeric,Array,Range] axis Finds minimum values along the axis and
4744
+ * returns **flat 1-d indices**.
4745
+ * @return [Integer,Numo::Int] returns result indices.
4746
+ * @see #argmin
4747
+ * @see #min
4748
+ *
4749
+ * @example
4750
+ * a = Numo::NArray[3,4,1,2]
4751
+ * a.min_index #=> 2
4752
+ *
4753
+ * b = Numo::NArray[[3,4,1],[2,0,5]]
4754
+ * b.min_index #=> 4
4755
+ * b.min_index(axis:1) #=> [2, 4]
4756
+ * b.min_index(axis:0) #=> [3, 4, 2]
4757
+ * b[b.min_index(axis:0)] #=> [2, 0, 1]
4758
+ */
5776
4759
  rb_define_method(cT, "min_index", int64_min_index, -1);
4760
+ /**
4761
+ * Index of the maximum value.
4762
+ * @overload argmax(axis:nil)
4763
+ * @param [Numeric,Array,Range] axis Finds maximum values along the axis and
4764
+ * returns **indices along the axis**.
4765
+ * @return [Integer,Numo::Int] returns the result indices.
4766
+ * @see #max_index
4767
+ * @see #max
4768
+ *
4769
+ * @example
4770
+ * a = Numo::NArray[3,4,1,2]
4771
+ * a.argmax #=> 1
4772
+ *
4773
+ * b = Numo::NArray[[3,4,1],[2,0,5]]
4774
+ * b.argmax #=> 5
4775
+ * b.argmax(axis:1) #=> [1, 2]
4776
+ * b.argmax(axis:0) #=> [0, 0, 1]
4777
+ * b.at(b.argmax(axis:0), 0..-1) #=> [3, 4, 5]
4778
+ */
5777
4779
  rb_define_method(cT, "argmax", int64_argmax, -1);
4780
+ /**
4781
+ * Index of the minimum value.
4782
+ * @overload argmin(axis:nil)
4783
+ * @param [Numeric,Array,Range] axis Finds minimum values along the axis and
4784
+ * returns **indices along the axis**.
4785
+ * @return [Integer,Numo::Int] returns the result indices.
4786
+ * @see #min_index
4787
+ * @see #min
4788
+ *
4789
+ * @example
4790
+ * a = Numo::NArray[3,4,1,2]
4791
+ * a.argmin #=> 2
4792
+ *
4793
+ * b = Numo::NArray[[3,4,1],[2,0,5]]
4794
+ * b.argmin #=> 4
4795
+ * b.argmin(axis:1) #=> [2, 1]
4796
+ * b.argmin(axis:0) #=> [1, 1, 0]
4797
+ * b.at(b.argmin(axis:0), 0..-1) #=> [2, 0, 1]
4798
+ */
5778
4799
  rb_define_method(cT, "argmin", int64_argmin, -1);
4800
+ /**
4801
+ * minmax of self.
4802
+ * @overload minmax(axis:nil, keepdims:false)
4803
+ * @param [Numeric,Array,Range] axis Finds min-max along the axis.
4804
+ * @param [TrueClass] keepdims (keyword) If true, the reduced axes are left in
4805
+ * the result array as dimensions with size one.
4806
+ * @return [Numo::Int64,Numo::Int64] min and max of self.
4807
+ */
5779
4808
  rb_define_method(cT, "minmax", int64_minmax, -1);
5780
4809
  /**
5781
4810
  * Element-wise maximum of two arrays.
@@ -5792,12 +4821,82 @@ void Init_numo_int64(void) {
5792
4821
  */
5793
4822
  rb_define_module_function(cT, "minimum", int64_s_minimum, -1);
5794
4823
  rb_define_method(cT, "bincount", int64_bincount, -1);
4824
+ /**
4825
+ * cumsum of self.
4826
+ * @overload cumsum(axis:nil, nan:false)
4827
+ * @param [Numeric,Array,Range] axis Performs cumsum along the axis.
4828
+ * @param [TrueClass] nan If true, apply NaN-aware algorithm (avoid NaN if exists).
4829
+ * @return [Numo::Int64] cumsum of self.
4830
+ */
5795
4831
  rb_define_method(cT, "cumsum", int64_cumsum, -1);
4832
+ /**
4833
+ * cumprod of self.
4834
+ * @overload cumprod(axis:nil, nan:false)
4835
+ * @param [Numeric,Array,Range] axis Performs cumprod along the axis.
4836
+ * @param [TrueClass] nan If true, apply NaN-aware algorithm (avoid NaN if exists).
4837
+ * @return [Numo::Int64] cumprod of self.
4838
+ */
5796
4839
  rb_define_method(cT, "cumprod", int64_cumprod, -1);
4840
+ /**
4841
+ * Binary mulsum.
4842
+ *
4843
+ * @overload mulsum(other, axis:nil, keepdims:false)
4844
+ * @param [Numo::NArray,Numeric] other
4845
+ * @param [Numeric,Array,Range] axis Performs mulsum along the axis.
4846
+ * @param [TrueClass] keepdims (keyword) If true, the reduced axes are left in
4847
+ * the result array as dimensions with size one.
4848
+ * @return [Numo::NArray] mulsum of self and other.
4849
+ */
5797
4850
  rb_define_method(cT, "mulsum", int64_mulsum, -1);
4851
+ /**
4852
+ * Set linear sequence of numbers to self. The sequence is obtained from
4853
+ * beg+i*step
4854
+ * where i is 1-dimensional index.
4855
+ * @overload seq([beg,[step]])
4856
+ * @param [Numeric] beg beginning of sequence. (default=0)
4857
+ * @param [Numeric] step step of sequence. (default=1)
4858
+ * @return [Numo::Int64] self.
4859
+ * @example
4860
+ * Numo::DFloat.new(6).seq(1,-0.2)
4861
+ * # => Numo::DFloat#shape=[6]
4862
+ * # [1, 0.8, 0.6, 0.4, 0.2, 0]
4863
+ *
4864
+ * Numo::DComplex.new(6).seq(1,-0.2+0.2i)
4865
+ * # => Numo::DComplex#shape=[6]
4866
+ * # [1+0i, 0.8+0.2i, 0.6+0.4i, 0.4+0.6i, 0.2+0.8i, 0+1i]
4867
+ */
5798
4868
  rb_define_method(cT, "seq", int64_seq, -1);
4869
+ /**
4870
+ * Eye: Set a value to diagonal components, set 0 to non-diagonal components.
4871
+ * @overload eye([element,offset])
4872
+ * @param [Numeric] element Diagonal element to be stored. Default is 1.
4873
+ * @param [Integer] offset Diagonal offset from the main diagonal. The
4874
+ * default is 0. k>0 for diagonals above the main diagonal, and k<0
4875
+ * for diagonals below the main diagonal.
4876
+ * @return [Numo::Int64] eye of self.
4877
+ */
5799
4878
  rb_define_method(cT, "eye", int64_eye, -1);
5800
4879
  rb_define_alias(cT, "indgen", "seq");
4880
+ /**
4881
+ * Generate uniformly distributed random numbers on self narray.
4882
+ * @overload rand([[low],high])
4883
+ * @param [Numeric] low lower inclusive boundary of random numbers. (default=0)
4884
+ * @param [Numeric] high upper exclusive boundary of random numbers.
4885
+ * (default=1 or 1+1i for complex types)
4886
+ * @return [Numo::Int64] self.
4887
+ * @example
4888
+ * Numo::DFloat.new(6).rand
4889
+ * # => Numo::DFloat#shape=[6]
4890
+ * # [0.0617545, 0.373067, 0.794815, 0.201042, 0.116041, 0.344032]
4891
+ *
4892
+ * Numo::DComplex.new(6).rand(5+5i)
4893
+ * # => Numo::DComplex#shape=[6]
4894
+ * # [2.69974+3.68908i, 0.825443+0.254414i, 0.540323+0.34354i, 4.52061+2.39322i, ...]
4895
+ *
4896
+ * Numo::Int32.new(6).rand(2,5)
4897
+ * # => Numo::Int32#shape=[6]
4898
+ * # [4, 3, 3, 2, 4, 2]
4899
+ */
5801
4900
  rb_define_method(cT, "rand", int64_rand, -1);
5802
4901
  rb_define_method(cT, "poly", int64_poly, -2);
5803
4902