numo-narray-alt 0.9.5 → 0.9.7
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.
- checksums.yaml +4 -4
- data/Gemfile +3 -0
- data/README.md +20 -0
- data/ext/numo/narray/SFMT-params19937.h +12 -20
- data/ext/numo/narray/array.c +26 -2
- data/ext/numo/narray/data.c +70 -72
- data/ext/numo/narray/extconf.rb +0 -1
- data/ext/numo/narray/index.c +2 -2
- data/ext/numo/narray/kwargs.c +6 -6
- data/ext/numo/narray/math.c +10 -4
- data/ext/numo/narray/narray.c +80 -52
- data/ext/numo/narray/numo/narray.h +20 -20
- data/ext/numo/narray/numo/ndloop.h +1 -1
- data/ext/numo/narray/numo/template.h +80 -80
- data/ext/numo/narray/numo/types/bit.h +76 -0
- data/ext/numo/narray/numo/types/complex.h +2 -2
- data/ext/numo/narray/numo/types/complex_macro.h +27 -26
- data/ext/numo/narray/numo/types/float_macro.h +18 -17
- data/ext/numo/narray/numo/types/real_accum.h +22 -22
- data/ext/numo/narray/numo/types/robj_macro.h +15 -14
- data/ext/numo/narray/numo/types/xint_macro.h +50 -8
- data/ext/numo/narray/rand.c +7 -0
- data/ext/numo/narray/src/mh/mean.h +102 -0
- data/ext/numo/narray/src/mh/rms.h +102 -0
- data/ext/numo/narray/src/mh/stddev.h +103 -0
- data/ext/numo/narray/src/mh/var.h +102 -0
- data/ext/numo/narray/src/t_bit.c +121 -71
- data/ext/numo/narray/src/t_dcomplex.c +248 -387
- data/ext/numo/narray/src/t_dfloat.c +922 -1068
- data/ext/numo/narray/src/t_int16.c +282 -231
- data/ext/numo/narray/src/t_int32.c +282 -231
- data/ext/numo/narray/src/t_int64.c +281 -230
- data/ext/numo/narray/src/t_int8.c +282 -231
- data/ext/numo/narray/src/t_robject.c +278 -405
- data/ext/numo/narray/src/t_scomplex.c +246 -406
- data/ext/numo/narray/src/t_sfloat.c +916 -1058
- data/ext/numo/narray/src/t_uint16.c +282 -231
- data/ext/numo/narray/src/t_uint32.c +282 -231
- data/ext/numo/narray/src/t_uint64.c +282 -231
- data/ext/numo/narray/src/t_uint8.c +282 -231
- data/ext/numo/narray/struct.c +12 -7
- data/lib/numo/narray/extra.rb +85 -5
- metadata +6 -3
- data/ext/numo/narray/src/t_mean.c +0 -105
data/ext/numo/narray/struct.c
CHANGED
|
@@ -670,8 +670,8 @@ static inline VALUE nary_struct_store_array(VALUE self, VALUE obj) {
|
|
|
670
670
|
/*
|
|
671
671
|
Store elements to Numo::Struct from other.
|
|
672
672
|
@overload store(other)
|
|
673
|
-
|
|
674
|
-
|
|
673
|
+
@param [Object] other
|
|
674
|
+
@return [Numo::Struct] self
|
|
675
675
|
*/
|
|
676
676
|
static VALUE nary_struct_store(VALUE self, VALUE obj) {
|
|
677
677
|
if (TYPE(obj) == T_ARRAY) {
|
|
@@ -742,10 +742,10 @@ static VALUE nst_s_add_type(int argc, VALUE* argv, VALUE mod) {
|
|
|
742
742
|
return Qnil;
|
|
743
743
|
}
|
|
744
744
|
|
|
745
|
-
#define NST_TYPEDEF(tpname, tpclass)
|
|
746
|
-
static VALUE nst_s_##tpname(int argc, VALUE* argv, VALUE mod) {
|
|
747
|
-
nstruct_add_type(tpclass, argc, argv, mod);
|
|
748
|
-
return Qnil;
|
|
745
|
+
#define NST_TYPEDEF(tpname, tpclass) \
|
|
746
|
+
static VALUE nst_s_##tpname(int argc, VALUE* argv, VALUE mod) { \
|
|
747
|
+
nstruct_add_type(tpclass, argc, argv, mod); \
|
|
748
|
+
return Qnil; \
|
|
749
749
|
}
|
|
750
750
|
|
|
751
751
|
NST_TYPEDEF(int8, numo_cInt8)
|
|
@@ -761,10 +761,15 @@ NST_TYPEDEF(dcomplex, numo_cDComplex)
|
|
|
761
761
|
NST_TYPEDEF(sfloat, numo_cSFloat)
|
|
762
762
|
NST_TYPEDEF(scomplex, numo_cSComplex)
|
|
763
763
|
|
|
764
|
-
#define rb_define_singleton_alias(klass, name1, name2)
|
|
764
|
+
#define rb_define_singleton_alias(klass, name1, name2) \
|
|
765
765
|
rb_define_alias(rb_singleton_class(klass), name1, name2)
|
|
766
766
|
|
|
767
767
|
void Init_nary_struct(void) {
|
|
768
|
+
/**
|
|
769
|
+
* Document-class: Numo::Struct
|
|
770
|
+
*
|
|
771
|
+
* Structured array class.
|
|
772
|
+
*/
|
|
768
773
|
cT = rb_define_class_under(mNumo, "Struct", numo_cNArray);
|
|
769
774
|
// cNStMember = rb_define_class_under(cT, "Member", rb_cObject);
|
|
770
775
|
|
data/lib/numo/narray/extra.rb
CHANGED
|
@@ -114,6 +114,7 @@ module Numo
|
|
|
114
114
|
end
|
|
115
115
|
end
|
|
116
116
|
|
|
117
|
+
# Convert the argument to an narray.
|
|
117
118
|
def self.asarray(a)
|
|
118
119
|
case a
|
|
119
120
|
when NArray
|
|
@@ -710,6 +711,12 @@ module Numo
|
|
|
710
711
|
end
|
|
711
712
|
end
|
|
712
713
|
|
|
714
|
+
# Split an array into multiple sub-arrays vertically
|
|
715
|
+
def vsplit(indices_or_sections)
|
|
716
|
+
split(indices_or_sections, axis: 0)
|
|
717
|
+
end
|
|
718
|
+
|
|
719
|
+
# Split an array into multiple sub-arrays horizontally
|
|
713
720
|
# @example
|
|
714
721
|
# x = Numo::DFloat.new(4,4).seq
|
|
715
722
|
# # => Numo::DFloat#shape=[4,4]
|
|
@@ -742,15 +749,11 @@ module Numo
|
|
|
742
749
|
# # [11],
|
|
743
750
|
# # [15]],
|
|
744
751
|
# # Numo::DFloat(view)#shape=[4,0][]]
|
|
745
|
-
|
|
746
|
-
def vsplit(indices_or_sections)
|
|
747
|
-
split(indices_or_sections, axis: 0)
|
|
748
|
-
end
|
|
749
|
-
|
|
750
752
|
def hsplit(indices_or_sections)
|
|
751
753
|
split(indices_or_sections, axis: 1)
|
|
752
754
|
end
|
|
753
755
|
|
|
756
|
+
# Split an array into multiple sub-arrays along the depth
|
|
754
757
|
def dsplit(indices_or_sections)
|
|
755
758
|
split(indices_or_sections, axis: 2)
|
|
756
759
|
end
|
|
@@ -1042,6 +1045,83 @@ module Numo
|
|
|
1042
1045
|
a
|
|
1043
1046
|
end
|
|
1044
1047
|
|
|
1048
|
+
# Returns an index array of sort result.
|
|
1049
|
+
#
|
|
1050
|
+
# @example
|
|
1051
|
+
# require 'numo/narray'
|
|
1052
|
+
#
|
|
1053
|
+
# a = Numo::DFloat[[0.1, 0.7],
|
|
1054
|
+
# [0.4, 0.2],
|
|
1055
|
+
# [0.2, 0.5]]
|
|
1056
|
+
# pp a.argsort
|
|
1057
|
+
# # =>
|
|
1058
|
+
# # Numo::Int32#shape=[3,2]
|
|
1059
|
+
# # [[0, 1],
|
|
1060
|
+
# # [1, 0],
|
|
1061
|
+
# # [0, 1]]
|
|
1062
|
+
# pp a.argsort(axis: 0)
|
|
1063
|
+
# # =>
|
|
1064
|
+
# # Numo::Int32#shape=[3,2]
|
|
1065
|
+
# # [[0, 1],
|
|
1066
|
+
# # [2, 2],
|
|
1067
|
+
# # [1, 0]]
|
|
1068
|
+
# pp a.argsort(axis: 1)
|
|
1069
|
+
# # =>
|
|
1070
|
+
# # Numo::Int32#shape=[3,2]
|
|
1071
|
+
# # [[0, 1],
|
|
1072
|
+
# # [1, 0],
|
|
1073
|
+
# # [0, 1]]
|
|
1074
|
+
# pp a.argsort(axis: nil)
|
|
1075
|
+
# # =>
|
|
1076
|
+
# # Numo::Int32#shape=[6]
|
|
1077
|
+
# # [0, 3, 4, 2, 5, 1]
|
|
1078
|
+
#
|
|
1079
|
+
# @overload argsort(axis: -1)
|
|
1080
|
+
# @param axis [Integer, nil] Axis along which to sort. Default is -1 (the last axis).
|
|
1081
|
+
# If `nil` is given, the array is flattened before sorting.
|
|
1082
|
+
# @return [Numo::Int32] An array of indices that would sort the array.
|
|
1083
|
+
def argsort(axis_ = 'none', axis: -1)
|
|
1084
|
+
raise NotImplementedError, "argsort is not implemented for #{self.class}" unless respond_to?(:sort_index)
|
|
1085
|
+
|
|
1086
|
+
axis = axis_ unless axis_ == 'none'
|
|
1087
|
+
|
|
1088
|
+
return flatten.sort_index if axis.nil?
|
|
1089
|
+
|
|
1090
|
+
axis = ndim + axis if axis.negative?
|
|
1091
|
+
raise Numo::NArray::DimensionError, 'dimension is out of range' if axis.negative? || axis >= ndim
|
|
1092
|
+
|
|
1093
|
+
case ndim
|
|
1094
|
+
when 1
|
|
1095
|
+
sort_index
|
|
1096
|
+
when 2
|
|
1097
|
+
case axis
|
|
1098
|
+
when 0
|
|
1099
|
+
indices = transpose.sort_index(1)
|
|
1100
|
+
indices.transpose - indices.min(1)
|
|
1101
|
+
when 1
|
|
1102
|
+
indices = sort_index(1)
|
|
1103
|
+
indices - indices.min(1).expand_dims(1)
|
|
1104
|
+
end
|
|
1105
|
+
else
|
|
1106
|
+
res = Numo::Int32.zeros(*shape)
|
|
1107
|
+
slicer = Array.new(ndim)
|
|
1108
|
+
slicer[axis] = true
|
|
1109
|
+
other_axes = Array.new(ndim) { |i| i } - [axis]
|
|
1110
|
+
axis_ids = other_axes.map do |d|
|
|
1111
|
+
Array.new(shape[d]) { |i| i }
|
|
1112
|
+
end
|
|
1113
|
+
axis_ids.inject(:product).each do |indices|
|
|
1114
|
+
indices = indices.flatten
|
|
1115
|
+
other_axes.each_with_index do |d, i|
|
|
1116
|
+
slicer[d] = indices[i]
|
|
1117
|
+
end
|
|
1118
|
+
sorted_indices = self[*slicer].sort_index
|
|
1119
|
+
res[*slicer] = sorted_indices - sorted_indices.min
|
|
1120
|
+
end
|
|
1121
|
+
res
|
|
1122
|
+
end
|
|
1123
|
+
end
|
|
1124
|
+
|
|
1045
1125
|
# Return the sum along diagonals of the array.
|
|
1046
1126
|
#
|
|
1047
1127
|
# If 2-D array, computes the summation along its diagonal with the
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: numo-narray-alt
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.9.
|
|
4
|
+
version: 0.9.7
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- yoshoku
|
|
@@ -63,6 +63,10 @@ files:
|
|
|
63
63
|
- ext/numo/narray/numo/types/uint_macro.h
|
|
64
64
|
- ext/numo/narray/numo/types/xint_macro.h
|
|
65
65
|
- ext/numo/narray/rand.c
|
|
66
|
+
- ext/numo/narray/src/mh/mean.h
|
|
67
|
+
- ext/numo/narray/src/mh/rms.h
|
|
68
|
+
- ext/numo/narray/src/mh/stddev.h
|
|
69
|
+
- ext/numo/narray/src/mh/var.h
|
|
66
70
|
- ext/numo/narray/src/t_bit.c
|
|
67
71
|
- ext/numo/narray/src/t_dcomplex.c
|
|
68
72
|
- ext/numo/narray/src/t_dfloat.c
|
|
@@ -70,7 +74,6 @@ files:
|
|
|
70
74
|
- ext/numo/narray/src/t_int32.c
|
|
71
75
|
- ext/numo/narray/src/t_int64.c
|
|
72
76
|
- ext/numo/narray/src/t_int8.c
|
|
73
|
-
- ext/numo/narray/src/t_mean.c
|
|
74
77
|
- ext/numo/narray/src/t_robject.c
|
|
75
78
|
- ext/numo/narray/src/t_scomplex.c
|
|
76
79
|
- ext/numo/narray/src/t_sfloat.c
|
|
@@ -90,7 +93,7 @@ metadata:
|
|
|
90
93
|
homepage_uri: https://github.com/yoshoku/numo-narray-alt
|
|
91
94
|
source_code_uri: https://github.com/yoshoku/numo-narray-alt
|
|
92
95
|
changelog_uri: https://github.com/yoshoku/numo-narray-alt/blob/main/CHANGELOG.md
|
|
93
|
-
documentation_uri: https://gemdocs.org/gems/numo-narray-alt/0.9.
|
|
96
|
+
documentation_uri: https://gemdocs.org/gems/numo-narray-alt/0.9.7/
|
|
94
97
|
rubygems_mfa_required: 'true'
|
|
95
98
|
rdoc_options: []
|
|
96
99
|
require_paths:
|
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
t_mean.c
|
|
3
|
-
Numo::NArray Alternative
|
|
4
|
-
|
|
5
|
-
created on: 2025-10-08
|
|
6
|
-
Copyright (C) 2025 Atsushi Tatsuma
|
|
7
|
-
*/
|
|
8
|
-
#include <ruby.h>
|
|
9
|
-
|
|
10
|
-
#include "numo/narray.h"
|
|
11
|
-
#include "numo/template.h"
|
|
12
|
-
|
|
13
|
-
// Type aliases for shorter notation following the codebase naming convention.
|
|
14
|
-
typedef BIT_DIGIT bit;
|
|
15
|
-
typedef int8_t int8;
|
|
16
|
-
typedef int16_t int16;
|
|
17
|
-
typedef int32_t int32;
|
|
18
|
-
typedef int64_t int64;
|
|
19
|
-
typedef u_int8_t uint8;
|
|
20
|
-
typedef u_int16_t uint16;
|
|
21
|
-
typedef u_int32_t uint32;
|
|
22
|
-
typedef u_int64_t uint64;
|
|
23
|
-
|
|
24
|
-
#define DEF_INT_MEAN_FUNC(tDType, tRtDType, tNAryClass, tRtNAryClass) \
|
|
25
|
-
static void iter_##tDType##_mean(na_loop_t* const lp) { \
|
|
26
|
-
size_t n; \
|
|
27
|
-
char* p1; \
|
|
28
|
-
char* p2; \
|
|
29
|
-
ssize_t s1; \
|
|
30
|
-
\
|
|
31
|
-
INIT_COUNTER(lp, n); \
|
|
32
|
-
INIT_PTR(lp, 0, p1, s1); \
|
|
33
|
-
p2 = NDL_PTR(lp, 1); \
|
|
34
|
-
\
|
|
35
|
-
size_t count = 0; \
|
|
36
|
-
tRtDType sum = 0; \
|
|
37
|
-
for (size_t i = n; i--;) { \
|
|
38
|
-
sum += (tRtDType)(*(tDType*)p1); \
|
|
39
|
-
p1 += s1; \
|
|
40
|
-
count++; \
|
|
41
|
-
} \
|
|
42
|
-
\
|
|
43
|
-
*(tRtDType*)p2 = sum / (tRtDType)count; \
|
|
44
|
-
} \
|
|
45
|
-
\
|
|
46
|
-
static void iter_##tDType##_mean_nan(na_loop_t* const lp) { \
|
|
47
|
-
size_t n; \
|
|
48
|
-
char* p1; \
|
|
49
|
-
char* p2; \
|
|
50
|
-
ssize_t s1; \
|
|
51
|
-
\
|
|
52
|
-
INIT_COUNTER(lp, n); \
|
|
53
|
-
INIT_PTR(lp, 0, p1, s1); \
|
|
54
|
-
p2 = NDL_PTR(lp, 1); \
|
|
55
|
-
\
|
|
56
|
-
size_t count = 0; \
|
|
57
|
-
tRtDType tmp = 0; \
|
|
58
|
-
tRtDType sum = 0; \
|
|
59
|
-
for (size_t i = n; i--;) { \
|
|
60
|
-
tmp = (tRtDType)(*(tDType*)p1); \
|
|
61
|
-
p1 += s1; \
|
|
62
|
-
if (tmp == tmp) { \
|
|
63
|
-
sum += tmp; \
|
|
64
|
-
count++; \
|
|
65
|
-
} \
|
|
66
|
-
} \
|
|
67
|
-
\
|
|
68
|
-
*(tRtDType*)p2 = sum / (tRtDType)count; \
|
|
69
|
-
} \
|
|
70
|
-
\
|
|
71
|
-
static VALUE tDType##_mean(int argc, VALUE* argv, VALUE self) { \
|
|
72
|
-
ndfunc_arg_in_t ain[2] = { { tNAryClass, 0 }, { sym_reduce, 0 } }; \
|
|
73
|
-
ndfunc_arg_out_t aout[1] = { { tRtNAryClass, 0 } }; \
|
|
74
|
-
ndfunc_t ndf = { \
|
|
75
|
-
iter_##tDType##_mean, STRIDE_LOOP_NIP | NDF_FLAT_REDUCE, 2, 1, ain, aout \
|
|
76
|
-
}; \
|
|
77
|
-
VALUE reduce = na_reduce_dimension(argc, argv, 1, &self, &ndf, iter_##tDType##_mean_nan); \
|
|
78
|
-
VALUE v = na_ndloop(&ndf, 2, self, reduce); \
|
|
79
|
-
\
|
|
80
|
-
return rb_funcall(v, rb_intern("extract"), 0); \
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
DEF_INT_MEAN_FUNC(bit, double, numo_cBit, numo_cDFloat)
|
|
84
|
-
DEF_INT_MEAN_FUNC(int8, double, numo_cInt8, numo_cDFloat)
|
|
85
|
-
DEF_INT_MEAN_FUNC(int16, double, numo_cInt16, numo_cDFloat)
|
|
86
|
-
DEF_INT_MEAN_FUNC(int32, double, numo_cInt32, numo_cDFloat)
|
|
87
|
-
DEF_INT_MEAN_FUNC(int64, double, numo_cInt64, numo_cDFloat)
|
|
88
|
-
DEF_INT_MEAN_FUNC(uint8, double, numo_cUInt8, numo_cDFloat)
|
|
89
|
-
DEF_INT_MEAN_FUNC(uint16, double, numo_cUInt16, numo_cDFloat)
|
|
90
|
-
DEF_INT_MEAN_FUNC(uint32, double, numo_cUInt32, numo_cDFloat)
|
|
91
|
-
DEF_INT_MEAN_FUNC(uint64, double, numo_cUInt64, numo_cDFloat)
|
|
92
|
-
|
|
93
|
-
#undef DEF_INT_MEAN_FUNC
|
|
94
|
-
|
|
95
|
-
void Init_nary_mean(void) {
|
|
96
|
-
rb_define_method(numo_cBit, "mean", bit_mean, -1);
|
|
97
|
-
rb_define_method(numo_cInt8, "mean", int8_mean, -1);
|
|
98
|
-
rb_define_method(numo_cInt16, "mean", int16_mean, -1);
|
|
99
|
-
rb_define_method(numo_cInt32, "mean", int32_mean, -1);
|
|
100
|
-
rb_define_method(numo_cInt64, "mean", int64_mean, -1);
|
|
101
|
-
rb_define_method(numo_cUInt8, "mean", uint8_mean, -1);
|
|
102
|
-
rb_define_method(numo_cUInt16, "mean", uint16_mean, -1);
|
|
103
|
-
rb_define_method(numo_cUInt32, "mean", uint32_mean, -1);
|
|
104
|
-
rb_define_method(numo_cUInt64, "mean", uint64_mean, -1);
|
|
105
|
-
}
|