numo-narray-alt 0.9.4 → 0.9.5
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/README.md +9 -3
- data/Rakefile +8 -0
- data/ext/numo/narray/SFMT-params19937.h +16 -12
- data/ext/numo/narray/SFMT.c +12 -5
- data/ext/numo/narray/array.c +25 -19
- data/ext/numo/narray/data.c +74 -70
- data/ext/numo/narray/extconf.rb +1 -0
- data/ext/numo/narray/index.c +49 -26
- data/ext/numo/narray/kwargs.c +11 -9
- data/ext/numo/narray/math.c +4 -2
- data/ext/numo/narray/narray.c +17 -10
- data/ext/numo/narray/ndloop.c +52 -63
- data/ext/numo/narray/numo/intern.h +9 -3
- 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 +85 -81
- data/ext/numo/narray/numo/types/complex.h +7 -3
- data/ext/numo/narray/numo/types/complex_macro.h +27 -25
- data/ext/numo/narray/numo/types/float_macro.h +20 -17
- data/ext/numo/narray/numo/types/real_accum.h +22 -22
- data/ext/numo/narray/numo/types/robj_macro.h +19 -12
- data/ext/numo/narray/numo/types/xint_macro.h +9 -8
- data/ext/numo/narray/src/t_bit.c +97 -88
- data/ext/numo/narray/src/t_dcomplex.c +336 -307
- data/ext/numo/narray/src/t_dfloat.c +522 -456
- data/ext/numo/narray/src/t_int16.c +351 -308
- data/ext/numo/narray/src/t_int32.c +351 -308
- data/ext/numo/narray/src/t_int64.c +351 -308
- data/ext/numo/narray/src/t_int8.c +309 -288
- data/ext/numo/narray/src/t_mean.c +105 -0
- data/ext/numo/narray/src/t_robject.c +323 -296
- data/ext/numo/narray/src/t_scomplex.c +327 -302
- data/ext/numo/narray/src/t_sfloat.c +515 -451
- data/ext/numo/narray/src/t_uint16.c +351 -308
- data/ext/numo/narray/src/t_uint32.c +351 -308
- data/ext/numo/narray/src/t_uint64.c +351 -308
- data/ext/numo/narray/src/t_uint8.c +311 -288
- data/ext/numo/narray/step.c +7 -2
- data/ext/numo/narray/struct.c +24 -22
- data/lib/numo/narray/extra.rb +66 -25
- data/numo-narray-alt.gemspec +38 -0
- metadata +7 -1
data/ext/numo/narray/step.c
CHANGED
@@ -36,7 +36,9 @@ static ID id_beg, id_end, id_len, id_step;
|
|
36
36
|
* <i>array_sizse</i> is given, negative array index is considered.
|
37
37
|
*/
|
38
38
|
|
39
|
-
void nary_step_array_index(
|
39
|
+
void nary_step_array_index(
|
40
|
+
VALUE obj, size_t ary_size, size_t* plen, ssize_t* pbeg, ssize_t* pstep
|
41
|
+
) {
|
40
42
|
size_t len;
|
41
43
|
ssize_t beg = 0, step = 1;
|
42
44
|
VALUE vbeg, vend, vstep, vlen;
|
@@ -178,7 +180,10 @@ void nary_step_array_index(VALUE obj, size_t ary_size, size_t* plen, ssize_t* pb
|
|
178
180
|
// puts("pass 2");
|
179
181
|
|
180
182
|
if (beg < 0 || beg >= (ssize_t)ary_size || end < 0 || end >= (ssize_t)ary_size) {
|
181
|
-
rb_raise(
|
183
|
+
rb_raise(
|
184
|
+
rb_eRangeError, "beg=%" SZF "d,end=%" SZF "d is out of array size (%" SZF "u)", beg, end,
|
185
|
+
ary_size
|
186
|
+
);
|
182
187
|
}
|
183
188
|
if (plen) *plen = len;
|
184
189
|
if (pbeg) *pbeg = beg;
|
data/ext/numo/narray/struct.c
CHANGED
@@ -167,7 +167,10 @@ static VALUE nst_field_view(VALUE self, VALUE idx) {
|
|
167
167
|
def = nst_definition(self, idx);
|
168
168
|
if (!RTEST(def)) {
|
169
169
|
idx = rb_funcall(idx, rb_intern("to_s"), 0);
|
170
|
-
rb_raise(
|
170
|
+
rb_raise(
|
171
|
+
rb_eTypeError, "Invalid field: '%s' for struct %s", StringValuePtr(idx),
|
172
|
+
rb_class2name(rb_obj_class(self))
|
173
|
+
);
|
171
174
|
}
|
172
175
|
type = RARRAY_AREF(def, 1);
|
173
176
|
ofs = RARRAY_AREF(def, 2);
|
@@ -268,7 +271,6 @@ static VALUE nst_s_new(int argc, VALUE* argv, VALUE klass) {
|
|
268
271
|
|
269
272
|
size = rb_iv_get(st, "__offset__");
|
270
273
|
members = rb_iv_get(st, "__members__");
|
271
|
-
// printf("size=%d\n",NUM2INT(size));
|
272
274
|
rb_define_const(st, CONTIGUOUS_STRIDE, size);
|
273
275
|
rb_define_const(st, ELEMENT_BYTE_SIZE, size);
|
274
276
|
rb_define_const(st, ELEMENT_BIT_SIZE, rb_funcall(size, '*', 1, INT2FIX(8)));
|
@@ -344,7 +346,9 @@ static VALUE nstruct_add_type(VALUE type, int argc, VALUE* argv, VALUE nst) {
|
|
344
346
|
|
345
347
|
size = rb_funcall(type, rb_intern("byte_size"), 0);
|
346
348
|
rb_iv_set(nst, "__offset__", rb_funcall(ofs, '+', 1, size));
|
347
|
-
rb_ary_push(
|
349
|
+
rb_ary_push(
|
350
|
+
rb_iv_get(nst, "__members__"), rb_ary_new3(4, name, type, ofs, size)
|
351
|
+
); // <- field definition
|
348
352
|
return Qnil;
|
349
353
|
}
|
350
354
|
|
@@ -421,9 +425,9 @@ static VALUE nst_create_member_views(VALUE self) {
|
|
421
425
|
|
422
426
|
static VALUE nary_struct_to_a(VALUE self) {
|
423
427
|
volatile VALUE opt;
|
424
|
-
ndfunc_arg_in_t ain[3] = {{Qnil, 0}, {sym_loop_opt}, {sym_option}};
|
425
|
-
ndfunc_arg_out_t aout[1] = {{rb_cArray, 0}}; // dummy?
|
426
|
-
ndfunc_t ndf = {iter_nstruct_to_a, NO_LOOP, 3, 1, ain, aout};
|
428
|
+
ndfunc_arg_in_t ain[3] = { { Qnil, 0 }, { sym_loop_opt }, { sym_option } };
|
429
|
+
ndfunc_arg_out_t aout[1] = { { rb_cArray, 0 } }; // dummy?
|
430
|
+
ndfunc_t ndf = { iter_nstruct_to_a, NO_LOOP, 3, 1, ain, aout };
|
427
431
|
|
428
432
|
opt = nst_create_member_views(self);
|
429
433
|
return na_ndloop_cast_narray_to_rarray(&ndf, self, opt);
|
@@ -581,19 +585,14 @@ static VALUE nary_struct_cast_array(VALUE klass, VALUE rary) {
|
|
581
585
|
narray_t* na;
|
582
586
|
// na_compose_t *nc;
|
583
587
|
VALUE opt;
|
584
|
-
ndfunc_arg_in_t ain[3] = {{OVERWRITE, 0}, {rb_cArray, 0}, {sym_option}};
|
585
|
-
ndfunc_t ndf = {iter_nstruct_from_a, NO_LOOP, 3, 0, ain, 0};
|
586
|
-
|
587
|
-
// fprintf(stderr,"rary:");rb_p(rary);
|
588
|
-
// fprintf(stderr,"class_of(rary):");rb_p(rb_obj_class(rary));
|
588
|
+
ndfunc_arg_in_t ain[3] = { { OVERWRITE, 0 }, { rb_cArray, 0 }, { sym_option } };
|
589
|
+
ndfunc_t ndf = { iter_nstruct_from_a, NO_LOOP, 3, 0, ain, 0 };
|
589
590
|
|
590
591
|
// vnc = na_ary_composition_for_struct(klass, rary);
|
591
592
|
// Data_Get_Struct(vnc, na_compose_t, nc);
|
592
593
|
// nary = nary_new(klass, nc->ndim, nc->shape);
|
593
594
|
nary = na_s_new_like(klass, rary);
|
594
595
|
GetNArray(nary, na);
|
595
|
-
// fprintf(stderr,"na->size=%lu\n",na->size);
|
596
|
-
// fprintf(stderr,"na->ndim=%d\n",na->ndim);
|
597
596
|
if (na->size > 0) {
|
598
597
|
opt = nst_create_member_views(nary);
|
599
598
|
rb_funcall(nary, rb_intern("allocate"), 0);
|
@@ -657,8 +656,8 @@ static void iter_struct_store_struct(na_loop_t* const lp) {
|
|
657
656
|
}
|
658
657
|
|
659
658
|
static VALUE nary_struct_store_struct(VALUE self, VALUE obj) {
|
660
|
-
ndfunc_arg_in_t ain[2] = {{OVERWRITE, 0}, {Qnil, 0}};
|
661
|
-
ndfunc_t ndf = {iter_struct_store_struct, FULL_LOOP, 2, 0, ain, 0};
|
659
|
+
ndfunc_arg_in_t ain[2] = { { OVERWRITE, 0 }, { Qnil, 0 } };
|
660
|
+
ndfunc_t ndf = { iter_struct_store_struct, FULL_LOOP, 2, 0, ain, 0 };
|
662
661
|
|
663
662
|
na_ndloop(&ndf, 2, self, obj);
|
664
663
|
return self;
|
@@ -683,8 +682,10 @@ static VALUE nary_struct_store(VALUE self, VALUE obj) {
|
|
683
682
|
nary_struct_store_struct(self, obj);
|
684
683
|
return self;
|
685
684
|
}
|
686
|
-
rb_raise(
|
687
|
-
|
685
|
+
rb_raise(
|
686
|
+
nary_eCastError, "unknown conversion from %s to %s", rb_class2name(rb_obj_class(obj)),
|
687
|
+
rb_class2name(rb_obj_class(self))
|
688
|
+
);
|
688
689
|
return self;
|
689
690
|
}
|
690
691
|
|
@@ -741,10 +742,10 @@ static VALUE nst_s_add_type(int argc, VALUE* argv, VALUE mod) {
|
|
741
742
|
return Qnil;
|
742
743
|
}
|
743
744
|
|
744
|
-
#define NST_TYPEDEF(tpname, tpclass)
|
745
|
-
static VALUE nst_s_##tpname(int argc, VALUE* argv, VALUE mod) {
|
746
|
-
nstruct_add_type(tpclass, argc, argv, mod);
|
747
|
-
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; \
|
748
749
|
}
|
749
750
|
|
750
751
|
NST_TYPEDEF(int8, numo_cInt8)
|
@@ -760,7 +761,8 @@ NST_TYPEDEF(dcomplex, numo_cDComplex)
|
|
760
761
|
NST_TYPEDEF(sfloat, numo_cSFloat)
|
761
762
|
NST_TYPEDEF(scomplex, numo_cSComplex)
|
762
763
|
|
763
|
-
#define rb_define_singleton_alias(klass, name1, name2)
|
764
|
+
#define rb_define_singleton_alias(klass, name1, name2) \
|
765
|
+
rb_define_alias(rb_singleton_class(klass), name1, name2)
|
764
766
|
|
765
767
|
void Init_nary_struct(void) {
|
766
768
|
cT = rb_define_class_under(mNumo, "Struct", numo_cNArray);
|
data/lib/numo/narray/extra.rb
CHANGED
@@ -1086,7 +1086,7 @@ module Numo
|
|
1086
1086
|
if am > nx && an > nx && bm > nx && bn > nx &&
|
1087
1087
|
size > ns && b.size > ns
|
1088
1088
|
@@warn_slow_dot = true
|
1089
|
-
warn "\nwarning: Built-in matrix dot is slow. Consider installing
|
1089
|
+
warn "\nwarning: Built-in matrix dot is slow. Consider installing numo-linalg-alt gem.\n\n"
|
1090
1090
|
end
|
1091
1091
|
end
|
1092
1092
|
self[false, :new].mulsum(b[false, :new, true, true], axis: -2)
|
@@ -1209,8 +1209,37 @@ module Numo
|
|
1209
1209
|
(self[*adim] * b[*bdim]).reshape(*shpr)
|
1210
1210
|
end
|
1211
1211
|
|
1212
|
-
#
|
1213
|
-
|
1212
|
+
# Compute a covariance matrix.
|
1213
|
+
#
|
1214
|
+
# @param y [Numo::NArray] (optional) If not nil, the covariance matrix of `self` and `y` is computed.
|
1215
|
+
# @param ddof [Integer] (optional) Delta degrees of freedom. The divisor used in calculations is `N - ddof`,
|
1216
|
+
# where `N` represents the number of observations.
|
1217
|
+
# @param fweights [Numo::NArray] (optional) 1-D array of integer frequency weights.
|
1218
|
+
# @param aweights [Numo::NArray] (optional) 1-D array of observation vector weights.
|
1219
|
+
# @return [Numo::NArray] return covariance matrix
|
1220
|
+
#
|
1221
|
+
# @example
|
1222
|
+
# x = Numo::DFloat[4, 5, 6]
|
1223
|
+
# x.cov
|
1224
|
+
# # => 1.0
|
1225
|
+
#
|
1226
|
+
# x = Numo::DFloat[[4, 5, 6], [3, 2, 1]]
|
1227
|
+
# x.cov
|
1228
|
+
# # => Numo::DFloat#shape=[2,2]
|
1229
|
+
# # [[1, -1],
|
1230
|
+
# # [-1, 1]]
|
1231
|
+
#
|
1232
|
+
# y = Numo::DFloat[7, 9, 8]
|
1233
|
+
# x.cov(y)
|
1234
|
+
# # => Numo::DFloat#shape=[3,3]
|
1235
|
+
# # [[1, -1, 0.5],
|
1236
|
+
# # [-1, 1, -0.5],
|
1237
|
+
# # [0.5, -0.5, 1]]
|
1238
|
+
def cov(y = nil, ddof: 1, fweights: nil, aweights: nil) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
1239
|
+
raise Numo::NArray::ShapeError, 'ndim must be <= 2' if ndim > 2
|
1240
|
+
raise Numo::NArray::ShapeError, 'y.ndim must be <= 2' if !y.nil? && (y.ndim > 2)
|
1241
|
+
raise ArgumentError, 'ddof must be 0 or 1' unless [0, 1].include?(ddof)
|
1242
|
+
|
1214
1243
|
m = if y
|
1215
1244
|
NArray.vstack([self, y])
|
1216
1245
|
else
|
@@ -1218,36 +1247,48 @@ module Numo
|
|
1218
1247
|
end
|
1219
1248
|
w = nil
|
1220
1249
|
if fweights
|
1221
|
-
|
1222
|
-
|
1250
|
+
fweights = Numo::NArray.cast(fweights) unless fweights.is_a?(Numo::NArray)
|
1251
|
+
raise ArgumentError, 'fweights must be 1-D array' unless fweights.ndim == 1
|
1252
|
+
raise ArgumentError, 'fweights size is wrong' unless fweights.size == m.shape[1]
|
1253
|
+
raise ArgumentError, 'fweights must be non-negative' if (fweights < 0).any?
|
1254
|
+
raise ArgumentError, 'fweights must be integer' unless fweights == fweights.floor
|
1255
|
+
|
1256
|
+
w = fweights
|
1223
1257
|
end
|
1224
1258
|
if aweights
|
1225
|
-
|
1226
|
-
|
1227
|
-
|
1228
|
-
|
1229
|
-
|
1230
|
-
if
|
1231
|
-
|
1232
|
-
elsif aweights.nil?
|
1233
|
-
fact = w_sum - ddof
|
1259
|
+
aweights = Numo::NArray.cast(aweights) unless aweights.is_a?(Numo::NArray)
|
1260
|
+
raise ArgumentError, 'aweights must be 1-D array' unless aweights.ndim == 1
|
1261
|
+
raise ArgumentError, 'aweights size is wrong' unless aweights.size == m.shape[1]
|
1262
|
+
raise ArgumentError, 'aweights must be non-negative' if (aweights < 0).any?
|
1263
|
+
|
1264
|
+
if w.nil?
|
1265
|
+
w = aweights
|
1234
1266
|
else
|
1235
|
-
|
1236
|
-
fact = w_sum - (ddof * wa_sum / w_sum)
|
1267
|
+
w *= aweights
|
1237
1268
|
end
|
1238
|
-
raise StandardError, 'Degrees of freedom <= 0 for slice' if (fact <= 0).any?
|
1239
|
-
else
|
1240
|
-
fact = m.shape[-1] - ddof
|
1241
1269
|
end
|
1242
|
-
if w
|
1243
|
-
|
1244
|
-
|
1245
|
-
|
1270
|
+
fact = if w.nil?
|
1271
|
+
m.shape[-1] - ddof
|
1272
|
+
elsif ddof == 0
|
1273
|
+
w.sum
|
1274
|
+
elsif aweights.nil?
|
1275
|
+
w.sum - ddof
|
1276
|
+
else
|
1277
|
+
w_sum = w.sum
|
1278
|
+
w_sum - (ddof * (w * aweights).sum / w_sum)
|
1279
|
+
end
|
1280
|
+
if fact <= 0
|
1281
|
+
warn('Degrees of freedom <= 0 for slice')
|
1282
|
+
fact = 0.0
|
1283
|
+
end
|
1284
|
+
if w.nil?
|
1246
1285
|
m -= m.mean(axis: -1, keepdims: true)
|
1247
1286
|
mw = m
|
1287
|
+
else
|
1288
|
+
m -= (m * w).sum(axis: -1, keepdims: true) / w.sum
|
1289
|
+
mw = m * w
|
1248
1290
|
end
|
1249
|
-
|
1250
|
-
mw.dot(mt.conj) / fact
|
1291
|
+
m.dot(mw.transpose.conj) / fact
|
1251
1292
|
end
|
1252
1293
|
|
1253
1294
|
private
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'English'
|
4
|
+
|
5
|
+
lib = File.expand_path('lib', __dir__)
|
6
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
7
|
+
|
8
|
+
open('ext/numo/narray/numo/narray.h') do |f|
|
9
|
+
f.each_line do |l|
|
10
|
+
if /NARRAY_VERSION "([\d.]+)"/ =~ l
|
11
|
+
NARRAY_VERSION = Regexp.last_match(1)
|
12
|
+
break
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
Gem::Specification.new do |spec|
|
18
|
+
spec.name = 'numo-narray-alt'
|
19
|
+
spec.version = NARRAY_VERSION
|
20
|
+
spec.authors = ['yoshoku']
|
21
|
+
spec.email = ['yoshoku@outlook.com']
|
22
|
+
spec.description = 'Numo::NArray Alternative is an experimental project forked from Numo::NArray.'
|
23
|
+
spec.summary = 'Numo::NArray Alternative is an experimental project forked from Numo::NArray.'
|
24
|
+
spec.homepage = 'https://github.com/yoshoku/numo-narray-alt'
|
25
|
+
spec.license = 'BSD-3-Clause'
|
26
|
+
spec.required_ruby_version = '>= 2.2'
|
27
|
+
|
28
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
29
|
+
spec.metadata['source_code_uri'] = spec.homepage
|
30
|
+
spec.metadata['changelog_uri'] = "#{spec.homepage}/blob/main/CHANGELOG.md"
|
31
|
+
spec.metadata['documentation_uri'] = "https://gemdocs.org/gems/#{spec.name}/#{spec.version}/"
|
32
|
+
|
33
|
+
spec.files = `git ls-files Gemfile LICENSE README.md Rakefile lib ext numo-narray-alt.gemspec`.split($INPUT_RECORD_SEPARATOR) # rubocop:disable Layout/LineLength
|
34
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
35
|
+
spec.require_paths = ['lib']
|
36
|
+
spec.extensions = ['ext/numo/narray/extconf.rb']
|
37
|
+
spec.metadata['rubygems_mfa_required'] = 'true'
|
38
|
+
end
|
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.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- yoshoku
|
@@ -70,6 +70,7 @@ files:
|
|
70
70
|
- ext/numo/narray/src/t_int32.c
|
71
71
|
- ext/numo/narray/src/t_int64.c
|
72
72
|
- ext/numo/narray/src/t_int8.c
|
73
|
+
- ext/numo/narray/src/t_mean.c
|
73
74
|
- ext/numo/narray/src/t_robject.c
|
74
75
|
- ext/numo/narray/src/t_scomplex.c
|
75
76
|
- ext/numo/narray/src/t_sfloat.c
|
@@ -81,10 +82,15 @@ files:
|
|
81
82
|
- ext/numo/narray/struct.c
|
82
83
|
- lib/numo/narray.rb
|
83
84
|
- lib/numo/narray/extra.rb
|
85
|
+
- numo-narray-alt.gemspec
|
84
86
|
homepage: https://github.com/yoshoku/numo-narray-alt
|
85
87
|
licenses:
|
86
88
|
- BSD-3-Clause
|
87
89
|
metadata:
|
90
|
+
homepage_uri: https://github.com/yoshoku/numo-narray-alt
|
91
|
+
source_code_uri: https://github.com/yoshoku/numo-narray-alt
|
92
|
+
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.5/
|
88
94
|
rubygems_mfa_required: 'true'
|
89
95
|
rdoc_options: []
|
90
96
|
require_paths:
|