numo-narray-alt 0.9.3

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 (66) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +14 -0
  3. data/LICENSE +30 -0
  4. data/README.md +71 -0
  5. data/Rakefile +24 -0
  6. data/ext/numo/narray/SFMT-params.h +97 -0
  7. data/ext/numo/narray/SFMT-params19937.h +48 -0
  8. data/ext/numo/narray/SFMT.c +602 -0
  9. data/ext/numo/narray/SFMT.h +147 -0
  10. data/ext/numo/narray/array.c +575 -0
  11. data/ext/numo/narray/data.c +958 -0
  12. data/ext/numo/narray/extconf.rb +84 -0
  13. data/ext/numo/narray/index.c +1092 -0
  14. data/ext/numo/narray/kwargs.c +142 -0
  15. data/ext/numo/narray/math.c +133 -0
  16. data/ext/numo/narray/narray.c +1976 -0
  17. data/ext/numo/narray/narray.def +28 -0
  18. data/ext/numo/narray/ndloop.c +1840 -0
  19. data/ext/numo/narray/numo/compat.h +23 -0
  20. data/ext/numo/narray/numo/intern.h +115 -0
  21. data/ext/numo/narray/numo/narray.h +480 -0
  22. data/ext/numo/narray/numo/ndloop.h +93 -0
  23. data/ext/numo/narray/numo/template.h +149 -0
  24. data/ext/numo/narray/numo/types/bit.h +38 -0
  25. data/ext/numo/narray/numo/types/complex.h +404 -0
  26. data/ext/numo/narray/numo/types/complex_macro.h +384 -0
  27. data/ext/numo/narray/numo/types/dcomplex.h +42 -0
  28. data/ext/numo/narray/numo/types/dfloat.h +44 -0
  29. data/ext/numo/narray/numo/types/float_def.h +34 -0
  30. data/ext/numo/narray/numo/types/float_macro.h +202 -0
  31. data/ext/numo/narray/numo/types/int16.h +27 -0
  32. data/ext/numo/narray/numo/types/int32.h +23 -0
  33. data/ext/numo/narray/numo/types/int64.h +23 -0
  34. data/ext/numo/narray/numo/types/int8.h +23 -0
  35. data/ext/numo/narray/numo/types/int_macro.h +66 -0
  36. data/ext/numo/narray/numo/types/real_accum.h +481 -0
  37. data/ext/numo/narray/numo/types/robj_macro.h +78 -0
  38. data/ext/numo/narray/numo/types/robject.h +25 -0
  39. data/ext/numo/narray/numo/types/scomplex.h +42 -0
  40. data/ext/numo/narray/numo/types/sfloat.h +45 -0
  41. data/ext/numo/narray/numo/types/uint16.h +24 -0
  42. data/ext/numo/narray/numo/types/uint32.h +20 -0
  43. data/ext/numo/narray/numo/types/uint64.h +20 -0
  44. data/ext/numo/narray/numo/types/uint8.h +20 -0
  45. data/ext/numo/narray/numo/types/uint_macro.h +57 -0
  46. data/ext/numo/narray/numo/types/xint_macro.h +166 -0
  47. data/ext/numo/narray/rand.c +40 -0
  48. data/ext/numo/narray/src/t_bit.c +3236 -0
  49. data/ext/numo/narray/src/t_dcomplex.c +6776 -0
  50. data/ext/numo/narray/src/t_dfloat.c +9417 -0
  51. data/ext/numo/narray/src/t_int16.c +5757 -0
  52. data/ext/numo/narray/src/t_int32.c +5757 -0
  53. data/ext/numo/narray/src/t_int64.c +5759 -0
  54. data/ext/numo/narray/src/t_int8.c +5355 -0
  55. data/ext/numo/narray/src/t_robject.c +5567 -0
  56. data/ext/numo/narray/src/t_scomplex.c +6731 -0
  57. data/ext/numo/narray/src/t_sfloat.c +9374 -0
  58. data/ext/numo/narray/src/t_uint16.c +5753 -0
  59. data/ext/numo/narray/src/t_uint32.c +5753 -0
  60. data/ext/numo/narray/src/t_uint64.c +5755 -0
  61. data/ext/numo/narray/src/t_uint8.c +5351 -0
  62. data/ext/numo/narray/step.c +266 -0
  63. data/ext/numo/narray/struct.c +814 -0
  64. data/lib/numo/narray/extra.rb +1266 -0
  65. data/lib/numo/narray.rb +4 -0
  66. metadata +106 -0
@@ -0,0 +1,142 @@
1
+ /**********************************************************************
2
+
3
+ Function to extract Keyword argument for ruby-2.1.x
4
+ Copied from class.c in ruby-2.4.2
5
+
6
+ Copyright (C) 1993-2007 Yukihiro Matsumoto
7
+
8
+ **********************************************************************/
9
+ #include <ruby.h>
10
+ #define rb_hash_tbl_raw(hash) rb_hash_tbl(hash)
11
+
12
+ /* from internal.h */
13
+ struct RBasicRaw {
14
+ VALUE flags;
15
+ VALUE klass;
16
+ };
17
+
18
+ #define RBASIC_SET_CLASS(obj, cls) \
19
+ do { \
20
+ VALUE _obj_ = (obj); \
21
+ RB_OBJ_WRITE(_obj_, &((struct RBasicRaw*)(_obj_))->klass, cls); \
22
+ } while (0)
23
+
24
+ /* from class.c */
25
+ VALUE
26
+ rb_keyword_error_new(const char* error, VALUE keys) {
27
+ const char* msg = "";
28
+ VALUE error_message;
29
+
30
+ if (RARRAY_LEN(keys) == 1) {
31
+ keys = RARRAY_AREF(keys, 0);
32
+ } else {
33
+ keys = rb_ary_join(keys, rb_usascii_str_new2(", "));
34
+ msg = "s";
35
+ }
36
+
37
+ error_message = rb_sprintf("%s keyword%s: %" PRIsVALUE, error, msg, keys);
38
+
39
+ return rb_exc_new_str(rb_eArgError, error_message);
40
+ }
41
+
42
+ NORETURN(static void rb_keyword_error(const char* error, VALUE keys));
43
+ static void rb_keyword_error(const char* error, VALUE keys) {
44
+ rb_exc_raise(rb_keyword_error_new(error, keys));
45
+ }
46
+
47
+ NORETURN(static void unknown_keyword_error(VALUE hash, const ID* table, int keywords));
48
+ static void unknown_keyword_error(VALUE hash, const ID* table, int keywords) {
49
+ st_table* tbl = rb_hash_tbl_raw(hash);
50
+ VALUE keys;
51
+ int i;
52
+ for (i = 0; i < keywords; i++) {
53
+ st_data_t key = ID2SYM(table[i]);
54
+ st_delete(tbl, &key, NULL);
55
+ }
56
+ keys = rb_funcallv(hash, rb_intern("keys"), 0, 0);
57
+ if (!RB_TYPE_P(keys, T_ARRAY)) rb_raise(rb_eArgError, "unknown keyword");
58
+ rb_keyword_error("unknown", keys);
59
+ }
60
+
61
+ static int separate_symbol(st_data_t key, st_data_t value, st_data_t arg) {
62
+ VALUE* kwdhash = (VALUE*)arg;
63
+
64
+ if (!SYMBOL_P(key)) kwdhash++;
65
+ if (!*kwdhash) *kwdhash = rb_hash_new();
66
+ rb_hash_aset(*kwdhash, (VALUE)key, (VALUE)value);
67
+ return ST_CONTINUE;
68
+ }
69
+
70
+ VALUE
71
+ rb_extract_keywords(VALUE* orighash) {
72
+ VALUE parthash[2] = {0, 0};
73
+ VALUE hash = *orighash;
74
+
75
+ if (RHASH_EMPTY_P(hash)) {
76
+ *orighash = 0;
77
+ return hash;
78
+ }
79
+ st_foreach(rb_hash_tbl_raw(hash), separate_symbol, (st_data_t)&parthash);
80
+ *orighash = parthash[1];
81
+ if (parthash[1] && RBASIC_CLASS(hash) != rb_cHash) {
82
+ RBASIC_SET_CLASS(parthash[1], RBASIC_CLASS(hash));
83
+ }
84
+ return parthash[0];
85
+ }
86
+
87
+ int rb_get_kwargs(VALUE keyword_hash, const ID* table, int required, int optional, VALUE* values) {
88
+ int i = 0, j;
89
+ int rest = 0;
90
+ VALUE missing = Qnil;
91
+ st_data_t key;
92
+
93
+ #define extract_kwarg(keyword, val) \
94
+ (key = (st_data_t)(keyword), \
95
+ values ? st_delete(rb_hash_tbl_raw(keyword_hash), &key, (val)) : st_lookup(rb_hash_tbl_raw(keyword_hash), key, (val)))
96
+
97
+ if (NIL_P(keyword_hash)) keyword_hash = 0;
98
+
99
+ if (optional < 0) {
100
+ rest = 1;
101
+ optional = -1 - optional;
102
+ }
103
+ if (values) {
104
+ for (j = 0; j < required + optional; j++) {
105
+ values[j] = Qundef;
106
+ }
107
+ }
108
+ if (required) {
109
+ for (; i < required; i++) {
110
+ VALUE keyword = ID2SYM(table[i]);
111
+ if (keyword_hash) {
112
+ st_data_t val;
113
+ if (extract_kwarg(keyword, &val)) {
114
+ if (values) values[i] = (VALUE)val;
115
+ continue;
116
+ }
117
+ }
118
+ if (NIL_P(missing)) missing = rb_ary_tmp_new(1);
119
+ rb_ary_push(missing, keyword);
120
+ }
121
+ if (!NIL_P(missing)) {
122
+ rb_keyword_error("missing", missing);
123
+ }
124
+ }
125
+ j = i;
126
+ if (optional && keyword_hash) {
127
+ for (i = 0; i < optional; i++) {
128
+ st_data_t val;
129
+ if (extract_kwarg(ID2SYM(table[required + i]), &val)) {
130
+ if (values) values[required + i] = (VALUE)val;
131
+ j++;
132
+ }
133
+ }
134
+ }
135
+ if (!rest && keyword_hash) {
136
+ if (RHASH_SIZE(keyword_hash) > (unsigned int)(values ? 0 : j)) {
137
+ unknown_keyword_error(keyword_hash, table, required + optional);
138
+ }
139
+ }
140
+ return j;
141
+ #undef extract_kwarg
142
+ }
@@ -0,0 +1,133 @@
1
+ /*
2
+ math.c
3
+ Ruby/Numo::NArray - Numerical Array class for Ruby
4
+ Copyright (C) 1999-2020 Masahiro TANAKA
5
+ */
6
+ #include <ruby.h>
7
+
8
+ #include "numo/narray.h"
9
+
10
+ VALUE numo_mNMath;
11
+ extern VALUE numo_mDFloatMath, numo_mDComplexMath;
12
+ extern VALUE numo_mSFloatMath, numo_mSComplexMath;
13
+ static ID id_send;
14
+ static ID id_UPCAST;
15
+ static ID id_DISPATCH;
16
+ static ID id_extract;
17
+
18
+ static VALUE nary_type_s_upcast(VALUE type1, VALUE type2) {
19
+ VALUE upcast_hash;
20
+ VALUE result_type;
21
+
22
+ if (type1 == type2) return type1;
23
+ upcast_hash = rb_const_get(type1, id_UPCAST);
24
+ result_type = rb_hash_aref(upcast_hash, type2);
25
+ if (NIL_P(result_type)) {
26
+ if (TYPE(type2) == T_CLASS) {
27
+ if (RTEST(rb_class_inherited_p(type2, cNArray))) {
28
+ upcast_hash = rb_const_get(type2, id_UPCAST);
29
+ result_type = rb_hash_aref(upcast_hash, type1);
30
+ }
31
+ }
32
+ }
33
+ return result_type;
34
+ }
35
+
36
+ static VALUE nary_math_cast2(VALUE type1, VALUE type2) {
37
+ if (RTEST(rb_class_inherited_p(type1, cNArray))) {
38
+ return nary_type_s_upcast(type1, type2);
39
+ }
40
+ if (RTEST(rb_class_inherited_p(type2, cNArray))) {
41
+ return nary_type_s_upcast(type2, type1);
42
+ }
43
+ if (RTEST(rb_class_inherited_p(type1, rb_cNumeric)) && RTEST(rb_class_inherited_p(type2, rb_cNumeric))) {
44
+ if (RTEST(rb_class_inherited_p(type1, rb_cComplex)) || RTEST(rb_class_inherited_p(type2, rb_cComplex))) {
45
+ return rb_cComplex;
46
+ }
47
+ return rb_cFloat;
48
+ }
49
+ return type2;
50
+ }
51
+
52
+ VALUE na_ary_composition_dtype(VALUE);
53
+
54
+ static VALUE nary_mathcast(int argc, VALUE* argv) {
55
+ VALUE type, type2;
56
+ int i;
57
+
58
+ type = na_ary_composition_dtype(argv[0]);
59
+ for (i = 1; i < argc; i++) {
60
+ type2 = na_ary_composition_dtype(argv[i]);
61
+ type = nary_math_cast2(type, type2);
62
+ if (NIL_P(type)) {
63
+ rb_raise(rb_eTypeError, "includes unknown DataType for upcast");
64
+ }
65
+ }
66
+ return type;
67
+ }
68
+
69
+ /*
70
+ Dispatches method to Math module of upcasted type,
71
+ eg, Numo::DFloat::Math.
72
+ @overload method_missing(name,x,...)
73
+ @param [Symbol] name method name.
74
+ @param [NArray,Numeric] x input array.
75
+ @return [NArray] result.
76
+ */
77
+ static VALUE nary_math_method_missing(int argc, VALUE* argv, VALUE mod) {
78
+ VALUE type, ans, typemod, hash;
79
+ if (argc > 1) {
80
+ type = nary_mathcast(argc - 1, argv + 1);
81
+
82
+ hash = rb_const_get(mod, id_DISPATCH);
83
+ typemod = rb_hash_aref(hash, type);
84
+ if (NIL_P(typemod)) {
85
+ rb_raise(rb_eTypeError, "%s is unknown for Numo::NMath", rb_class2name(type));
86
+ }
87
+
88
+ ans = rb_funcall2(typemod, id_send, argc, argv);
89
+
90
+ if (!RTEST(rb_class_inherited_p(type, cNArray)) && IsNArray(ans)) {
91
+ ans = rb_funcall(ans, id_extract, 0);
92
+ }
93
+ return ans;
94
+ }
95
+ rb_raise(rb_eArgError, "argument or method missing");
96
+ return Qnil;
97
+ }
98
+
99
+ void Init_nary_math(void) {
100
+ VALUE hCast;
101
+
102
+ numo_mNMath = rb_define_module_under(mNumo, "NMath");
103
+ rb_define_singleton_method(numo_mNMath, "method_missing", nary_math_method_missing, -1);
104
+
105
+ hCast = rb_hash_new();
106
+ rb_define_const(numo_mNMath, "DISPATCH", hCast);
107
+ rb_hash_aset(hCast, numo_cInt64, numo_mDFloatMath);
108
+ rb_hash_aset(hCast, numo_cInt32, numo_mDFloatMath);
109
+ rb_hash_aset(hCast, numo_cInt16, numo_mDFloatMath);
110
+ rb_hash_aset(hCast, numo_cInt8, numo_mDFloatMath);
111
+ rb_hash_aset(hCast, numo_cUInt64, numo_mDFloatMath);
112
+ rb_hash_aset(hCast, numo_cUInt32, numo_mDFloatMath);
113
+ rb_hash_aset(hCast, numo_cUInt16, numo_mDFloatMath);
114
+ rb_hash_aset(hCast, numo_cUInt8, numo_mDFloatMath);
115
+ rb_hash_aset(hCast, numo_cDFloat, numo_mDFloatMath);
116
+ rb_hash_aset(hCast, numo_cDFloat, numo_mDFloatMath);
117
+ rb_hash_aset(hCast, numo_cDComplex, numo_mDComplexMath);
118
+ rb_hash_aset(hCast, numo_cSFloat, numo_mSFloatMath);
119
+ rb_hash_aset(hCast, numo_cSComplex, numo_mSComplexMath);
120
+ #ifdef RUBY_INTEGER_UNIFICATION
121
+ rb_hash_aset(hCast, rb_cInteger, rb_mMath);
122
+ #else
123
+ rb_hash_aset(hCast, rb_cFixnum, rb_mMath);
124
+ rb_hash_aset(hCast, rb_cBignum, rb_mMath);
125
+ #endif
126
+ rb_hash_aset(hCast, rb_cFloat, rb_mMath);
127
+ rb_hash_aset(hCast, rb_cComplex, numo_mDComplexMath);
128
+
129
+ id_send = rb_intern("send");
130
+ id_UPCAST = rb_intern("UPCAST");
131
+ id_DISPATCH = rb_intern("DISPATCH");
132
+ id_extract = rb_intern("extract");
133
+ }