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.
- checksums.yaml +7 -0
- data/Gemfile +14 -0
- data/LICENSE +30 -0
- data/README.md +71 -0
- data/Rakefile +24 -0
- data/ext/numo/narray/SFMT-params.h +97 -0
- data/ext/numo/narray/SFMT-params19937.h +48 -0
- data/ext/numo/narray/SFMT.c +602 -0
- data/ext/numo/narray/SFMT.h +147 -0
- data/ext/numo/narray/array.c +575 -0
- data/ext/numo/narray/data.c +958 -0
- data/ext/numo/narray/extconf.rb +84 -0
- data/ext/numo/narray/index.c +1092 -0
- data/ext/numo/narray/kwargs.c +142 -0
- data/ext/numo/narray/math.c +133 -0
- data/ext/numo/narray/narray.c +1976 -0
- data/ext/numo/narray/narray.def +28 -0
- data/ext/numo/narray/ndloop.c +1840 -0
- data/ext/numo/narray/numo/compat.h +23 -0
- data/ext/numo/narray/numo/intern.h +115 -0
- data/ext/numo/narray/numo/narray.h +480 -0
- data/ext/numo/narray/numo/ndloop.h +93 -0
- data/ext/numo/narray/numo/template.h +149 -0
- data/ext/numo/narray/numo/types/bit.h +38 -0
- data/ext/numo/narray/numo/types/complex.h +404 -0
- data/ext/numo/narray/numo/types/complex_macro.h +384 -0
- data/ext/numo/narray/numo/types/dcomplex.h +42 -0
- data/ext/numo/narray/numo/types/dfloat.h +44 -0
- data/ext/numo/narray/numo/types/float_def.h +34 -0
- data/ext/numo/narray/numo/types/float_macro.h +202 -0
- data/ext/numo/narray/numo/types/int16.h +27 -0
- data/ext/numo/narray/numo/types/int32.h +23 -0
- data/ext/numo/narray/numo/types/int64.h +23 -0
- data/ext/numo/narray/numo/types/int8.h +23 -0
- data/ext/numo/narray/numo/types/int_macro.h +66 -0
- data/ext/numo/narray/numo/types/real_accum.h +481 -0
- data/ext/numo/narray/numo/types/robj_macro.h +78 -0
- data/ext/numo/narray/numo/types/robject.h +25 -0
- data/ext/numo/narray/numo/types/scomplex.h +42 -0
- data/ext/numo/narray/numo/types/sfloat.h +45 -0
- data/ext/numo/narray/numo/types/uint16.h +24 -0
- data/ext/numo/narray/numo/types/uint32.h +20 -0
- data/ext/numo/narray/numo/types/uint64.h +20 -0
- data/ext/numo/narray/numo/types/uint8.h +20 -0
- data/ext/numo/narray/numo/types/uint_macro.h +57 -0
- data/ext/numo/narray/numo/types/xint_macro.h +166 -0
- data/ext/numo/narray/rand.c +40 -0
- data/ext/numo/narray/src/t_bit.c +3236 -0
- data/ext/numo/narray/src/t_dcomplex.c +6776 -0
- data/ext/numo/narray/src/t_dfloat.c +9417 -0
- data/ext/numo/narray/src/t_int16.c +5757 -0
- data/ext/numo/narray/src/t_int32.c +5757 -0
- data/ext/numo/narray/src/t_int64.c +5759 -0
- data/ext/numo/narray/src/t_int8.c +5355 -0
- data/ext/numo/narray/src/t_robject.c +5567 -0
- data/ext/numo/narray/src/t_scomplex.c +6731 -0
- data/ext/numo/narray/src/t_sfloat.c +9374 -0
- data/ext/numo/narray/src/t_uint16.c +5753 -0
- data/ext/numo/narray/src/t_uint32.c +5753 -0
- data/ext/numo/narray/src/t_uint64.c +5755 -0
- data/ext/numo/narray/src/t_uint8.c +5351 -0
- data/ext/numo/narray/step.c +266 -0
- data/ext/numo/narray/struct.c +814 -0
- data/lib/numo/narray/extra.rb +1266 -0
- data/lib/numo/narray.rb +4 -0
- metadata +106 -0
@@ -0,0 +1,147 @@
|
|
1
|
+
/**
|
2
|
+
* @file SFMT.h
|
3
|
+
*
|
4
|
+
* @brief SIMD oriented Fast Mersenne Twister(SFMT) pseudorandom
|
5
|
+
* number generator
|
6
|
+
*
|
7
|
+
* @author Mutsuo Saito (Hiroshima University)
|
8
|
+
* @author Makoto Matsumoto (Hiroshima University)
|
9
|
+
*
|
10
|
+
* Copyright (C) 2006, 2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima
|
11
|
+
* University. All rights reserved.
|
12
|
+
*
|
13
|
+
* The new BSD License is applied to this software.
|
14
|
+
* see LICENSE.txt
|
15
|
+
*
|
16
|
+
* @note We assume that your system has inttypes.h. If your system
|
17
|
+
* doesn't have inttypes.h, you have to typedef uint32_t and uint64_t,
|
18
|
+
* and you have to define PRIu64 and PRIx64 in this file as follows:
|
19
|
+
* @verbatim
|
20
|
+
typedef unsigned int uint32_t
|
21
|
+
typedef unsigned long long uint64_t
|
22
|
+
#define PRIu64 "llu"
|
23
|
+
#define PRIx64 "llx"
|
24
|
+
@endverbatim
|
25
|
+
* uint32_t must be exactly 32-bit unsigned integer type (no more, no
|
26
|
+
* less), and uint64_t must be exactly 64-bit unsigned integer type.
|
27
|
+
* PRIu64 and PRIx64 are used for printf function to print 64-bit
|
28
|
+
* unsigned int and 64-bit unsigned int in hexadecimal format.
|
29
|
+
*/
|
30
|
+
|
31
|
+
#ifndef SFMT_H
|
32
|
+
#define SFMT_H
|
33
|
+
|
34
|
+
#include <stdio.h>
|
35
|
+
|
36
|
+
#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
|
37
|
+
#include <inttypes.h>
|
38
|
+
#elif defined(_MSC_VER) || defined(__BORLANDC__)
|
39
|
+
typedef unsigned int uint32_t;
|
40
|
+
typedef unsigned __int64 uint64_t;
|
41
|
+
#define inline __inline
|
42
|
+
#else
|
43
|
+
#include <inttypes.h>
|
44
|
+
#if defined(__GNUC__)
|
45
|
+
#define inline __inline__
|
46
|
+
#endif
|
47
|
+
#endif
|
48
|
+
|
49
|
+
#ifndef PRIu64
|
50
|
+
#if defined(_MSC_VER) || defined(__BORLANDC__)
|
51
|
+
#define PRIu64 "I64u"
|
52
|
+
#define PRIx64 "I64x"
|
53
|
+
#else
|
54
|
+
#define PRIu64 "llu"
|
55
|
+
#define PRIx64 "llx"
|
56
|
+
#endif
|
57
|
+
#endif
|
58
|
+
|
59
|
+
#if defined(__GNUC__)
|
60
|
+
#define ALWAYSINLINE __attribute__((always_inline))
|
61
|
+
#else
|
62
|
+
#define ALWAYSINLINE
|
63
|
+
#endif
|
64
|
+
|
65
|
+
#if defined(_MSC_VER)
|
66
|
+
#if _MSC_VER >= 1200
|
67
|
+
#define PRE_ALWAYS __forceinline
|
68
|
+
#else
|
69
|
+
#define PRE_ALWAYS inline
|
70
|
+
#endif
|
71
|
+
#else
|
72
|
+
#define PRE_ALWAYS inline
|
73
|
+
#endif
|
74
|
+
|
75
|
+
uint32_t gen_rand32(void);
|
76
|
+
uint64_t gen_rand64(void);
|
77
|
+
void fill_array32(uint32_t* array, int size);
|
78
|
+
void fill_array64(uint64_t* array, int size);
|
79
|
+
void init_gen_rand(uint32_t seed);
|
80
|
+
void init_by_array(uint32_t* init_key, int key_length);
|
81
|
+
const char* get_idstring(void);
|
82
|
+
int get_min_array_size32(void);
|
83
|
+
int get_min_array_size64(void);
|
84
|
+
|
85
|
+
/* These real versions are due to Isaku Wada */
|
86
|
+
/** generates a random number on [0,1]-real-interval */
|
87
|
+
inline static double to_real1(uint32_t v) {
|
88
|
+
return v * (1.0 / 4294967295.0);
|
89
|
+
/* divided by 2^32-1 */
|
90
|
+
}
|
91
|
+
|
92
|
+
/** generates a random number on [0,1]-real-interval */
|
93
|
+
inline static double genrand_real1(void) {
|
94
|
+
return to_real1(gen_rand32());
|
95
|
+
}
|
96
|
+
|
97
|
+
/** generates a random number on [0,1)-real-interval */
|
98
|
+
inline static double to_real2(uint32_t v) {
|
99
|
+
return v * (1.0 / 4294967296.0);
|
100
|
+
/* divided by 2^32 */
|
101
|
+
}
|
102
|
+
|
103
|
+
/** generates a random number on [0,1)-real-interval */
|
104
|
+
inline static double genrand_real2(void) {
|
105
|
+
return to_real2(gen_rand32());
|
106
|
+
}
|
107
|
+
|
108
|
+
/** generates a random number on (0,1)-real-interval */
|
109
|
+
inline static double to_real3(uint32_t v) {
|
110
|
+
return (((double)v) + 0.5) * (1.0 / 4294967296.0);
|
111
|
+
/* divided by 2^32 */
|
112
|
+
}
|
113
|
+
|
114
|
+
/** generates a random number on (0,1)-real-interval */
|
115
|
+
inline static double genrand_real3(void) {
|
116
|
+
return to_real3(gen_rand32());
|
117
|
+
}
|
118
|
+
/** These real versions are due to Isaku Wada */
|
119
|
+
|
120
|
+
/** generates a random number on [0,1) with 53-bit resolution*/
|
121
|
+
inline static double to_res53(uint64_t v) {
|
122
|
+
return v * (1.0 / 18446744073709551616.0L);
|
123
|
+
}
|
124
|
+
|
125
|
+
/** generates a random number on [0,1) with 53-bit resolution from two
|
126
|
+
* 32 bit integers */
|
127
|
+
inline static double to_res53_mix(uint32_t x, uint32_t y) {
|
128
|
+
return to_res53(x | ((uint64_t)y << 32));
|
129
|
+
}
|
130
|
+
|
131
|
+
/** generates a random number on [0,1) with 53-bit resolution
|
132
|
+
*/
|
133
|
+
inline static double genrand_res53(void) {
|
134
|
+
return to_res53(gen_rand64());
|
135
|
+
}
|
136
|
+
|
137
|
+
/** generates a random number on [0,1) with 53-bit resolution
|
138
|
+
using 32bit integer.
|
139
|
+
*/
|
140
|
+
inline static double genrand_res53_mix(void) {
|
141
|
+
uint32_t x, y;
|
142
|
+
|
143
|
+
x = gen_rand32();
|
144
|
+
y = gen_rand32();
|
145
|
+
return to_res53_mix(x, y);
|
146
|
+
}
|
147
|
+
#endif
|
@@ -0,0 +1,575 @@
|
|
1
|
+
/*
|
2
|
+
array.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
|
+
// mdai: Multi-Dimensional Array Investigation
|
11
|
+
typedef struct {
|
12
|
+
size_t shape;
|
13
|
+
VALUE val;
|
14
|
+
} na_mdai_item_t;
|
15
|
+
|
16
|
+
typedef struct {
|
17
|
+
int capa;
|
18
|
+
na_mdai_item_t* item;
|
19
|
+
int type; // Ruby numeric type - investigated separately
|
20
|
+
VALUE na_type; // NArray type
|
21
|
+
VALUE int_max;
|
22
|
+
} na_mdai_t;
|
23
|
+
|
24
|
+
// Order of Ruby object.
|
25
|
+
enum { NA_NONE, NA_BIT, NA_INT32, NA_INT64, NA_RATIONAL, NA_DFLOAT, NA_DCOMPLEX, NA_ROBJ, NA_NTYPES };
|
26
|
+
|
27
|
+
static ID id_begin;
|
28
|
+
static ID id_end;
|
29
|
+
static ID id_step;
|
30
|
+
static ID id_abs;
|
31
|
+
static ID id_cast;
|
32
|
+
static ID id_le;
|
33
|
+
#if SIZEOF_LONG <= 4
|
34
|
+
static ID id_ge;
|
35
|
+
#endif
|
36
|
+
static ID id_Complex;
|
37
|
+
static VALUE int32_max = Qnil;
|
38
|
+
static VALUE int32_min = Qnil;
|
39
|
+
|
40
|
+
static VALUE na_object_type(int type, VALUE v) {
|
41
|
+
switch (TYPE(v)) {
|
42
|
+
|
43
|
+
case T_TRUE:
|
44
|
+
case T_FALSE:
|
45
|
+
if (type < NA_BIT) return NA_BIT;
|
46
|
+
return type;
|
47
|
+
|
48
|
+
#if SIZEOF_LONG <= 4
|
49
|
+
case T_FIXNUM:
|
50
|
+
if (type < NA_INT32) return NA_INT32;
|
51
|
+
return type;
|
52
|
+
case T_BIGNUM:
|
53
|
+
if (type < NA_INT64) {
|
54
|
+
if (RTEST(rb_funcall(v, id_le, 1, int32_max)) && RTEST(rb_funcall(v, id_ge, 1, int32_min))) {
|
55
|
+
if (type < NA_INT32) return NA_INT32;
|
56
|
+
} else {
|
57
|
+
return NA_INT64;
|
58
|
+
}
|
59
|
+
}
|
60
|
+
return type;
|
61
|
+
#else
|
62
|
+
case T_FIXNUM:
|
63
|
+
if (type < NA_INT64) {
|
64
|
+
long x = NUM2LONG(v);
|
65
|
+
if (x <= 2147483647L && x >= -2147483648L) {
|
66
|
+
if (type < NA_INT32) return NA_INT32;
|
67
|
+
} else {
|
68
|
+
return NA_INT64;
|
69
|
+
}
|
70
|
+
}
|
71
|
+
return type;
|
72
|
+
case T_BIGNUM:
|
73
|
+
if (type < NA_INT64) return NA_INT64;
|
74
|
+
return type;
|
75
|
+
#endif
|
76
|
+
|
77
|
+
case T_FLOAT:
|
78
|
+
if (type < NA_DFLOAT) return NA_DFLOAT;
|
79
|
+
return type;
|
80
|
+
|
81
|
+
case T_NIL:
|
82
|
+
return type;
|
83
|
+
|
84
|
+
default:
|
85
|
+
if (rb_obj_class(v) == rb_const_get(rb_cObject, id_Complex)) {
|
86
|
+
return NA_DCOMPLEX;
|
87
|
+
}
|
88
|
+
}
|
89
|
+
return NA_ROBJ;
|
90
|
+
}
|
91
|
+
|
92
|
+
static int na_mdai_object_type(int type, VALUE v) {
|
93
|
+
if (rb_obj_is_kind_of(v, rb_cRange)) {
|
94
|
+
type = (int)na_object_type(type, rb_funcall(v, id_begin, 0));
|
95
|
+
type = (int)na_object_type(type, rb_funcall(v, id_end, 0));
|
96
|
+
#ifdef HAVE_RB_ARITHMETIC_SEQUENCE_EXTRACT
|
97
|
+
} else if (rb_obj_is_kind_of(v, rb_cArithSeq)) {
|
98
|
+
type = (int)na_object_type(type, rb_funcall(v, id_begin, 0));
|
99
|
+
type = (int)na_object_type(type, rb_funcall(v, id_end, 0));
|
100
|
+
type = (int)na_object_type(type, rb_funcall(v, id_step, 0));
|
101
|
+
#endif
|
102
|
+
} else {
|
103
|
+
type = (int)na_object_type(type, v);
|
104
|
+
}
|
105
|
+
return type;
|
106
|
+
}
|
107
|
+
|
108
|
+
static na_mdai_t* na_mdai_alloc(VALUE ary) {
|
109
|
+
int i, n = 4;
|
110
|
+
na_mdai_t* mdai;
|
111
|
+
|
112
|
+
mdai = ALLOC(na_mdai_t);
|
113
|
+
mdai->capa = n;
|
114
|
+
mdai->item = ALLOC_N(na_mdai_item_t, n);
|
115
|
+
for (i = 0; i < n; i++) {
|
116
|
+
mdai->item[i].shape = 0;
|
117
|
+
mdai->item[i].val = Qnil;
|
118
|
+
}
|
119
|
+
mdai->item[0].val = ary;
|
120
|
+
mdai->type = NA_NONE;
|
121
|
+
mdai->na_type = Qnil;
|
122
|
+
|
123
|
+
return mdai;
|
124
|
+
}
|
125
|
+
|
126
|
+
static void na_mdai_realloc(na_mdai_t* mdai, int n_extra) {
|
127
|
+
int i, n;
|
128
|
+
|
129
|
+
i = mdai->capa;
|
130
|
+
mdai->capa += n_extra;
|
131
|
+
n = mdai->capa;
|
132
|
+
REALLOC_N(mdai->item, na_mdai_item_t, n);
|
133
|
+
for (; i < n; i++) {
|
134
|
+
mdai->item[i].shape = 0;
|
135
|
+
mdai->item[i].val = Qnil;
|
136
|
+
}
|
137
|
+
}
|
138
|
+
|
139
|
+
static void na_mdai_free(void* ptr) {
|
140
|
+
na_mdai_t* mdai = (na_mdai_t*)ptr;
|
141
|
+
xfree(mdai->item);
|
142
|
+
xfree(mdai);
|
143
|
+
}
|
144
|
+
|
145
|
+
/* investigate ndim, shape, type of Array */
|
146
|
+
static int na_mdai_investigate(na_mdai_t* mdai, int ndim) {
|
147
|
+
ssize_t i;
|
148
|
+
int j;
|
149
|
+
size_t len, length;
|
150
|
+
double dbeg, dstep;
|
151
|
+
VALUE v;
|
152
|
+
VALUE val;
|
153
|
+
|
154
|
+
val = mdai->item[ndim - 1].val;
|
155
|
+
len = RARRAY_LEN(val);
|
156
|
+
|
157
|
+
for (i = 0; i < RARRAY_LEN(val); i++) {
|
158
|
+
v = RARRAY_AREF(val, i);
|
159
|
+
|
160
|
+
if (TYPE(v) == T_ARRAY) {
|
161
|
+
/* check recursive array */
|
162
|
+
for (j = 0; j < ndim; j++) {
|
163
|
+
if (mdai->item[j].val == v) rb_raise(rb_eStandardError, "cannot convert from a recursive Array to NArray");
|
164
|
+
}
|
165
|
+
if (ndim >= mdai->capa) {
|
166
|
+
na_mdai_realloc(mdai, 4);
|
167
|
+
}
|
168
|
+
mdai->item[ndim].val = v;
|
169
|
+
if (na_mdai_investigate(mdai, ndim + 1)) {
|
170
|
+
len--; /* Array is empty */
|
171
|
+
}
|
172
|
+
} else if (rb_obj_is_kind_of(v, rb_cRange)
|
173
|
+
#ifdef HAVE_RB_ARITHMETIC_SEQUENCE_EXTRACT
|
174
|
+
|| rb_obj_is_kind_of(v, rb_cArithSeq)
|
175
|
+
#else
|
176
|
+
|| rb_obj_is_kind_of(v, rb_cEnumerator)
|
177
|
+
#endif
|
178
|
+
) {
|
179
|
+
nary_step_sequence(v, &length, &dbeg, &dstep);
|
180
|
+
len += length - 1;
|
181
|
+
mdai->type = na_mdai_object_type(mdai->type, v);
|
182
|
+
} else if (IsNArray(v)) {
|
183
|
+
int r;
|
184
|
+
narray_t* na;
|
185
|
+
GetNArray(v, na);
|
186
|
+
if (na->ndim == 0) {
|
187
|
+
len--; /* NArray is empty */
|
188
|
+
} else {
|
189
|
+
if (ndim + na->ndim > mdai->capa) {
|
190
|
+
na_mdai_realloc(mdai, ((na->ndim - 1) / 4 + 1) * 4);
|
191
|
+
}
|
192
|
+
for (j = 0, r = ndim; j < na->ndim; j++, r++) {
|
193
|
+
if (mdai->item[r].shape < na->shape[j]) mdai->item[r].shape = na->shape[j];
|
194
|
+
}
|
195
|
+
}
|
196
|
+
// type
|
197
|
+
if (NIL_P(mdai->na_type)) {
|
198
|
+
mdai->na_type = rb_obj_class(v);
|
199
|
+
} else {
|
200
|
+
mdai->na_type = na_upcast(rb_obj_class(v), mdai->na_type);
|
201
|
+
}
|
202
|
+
} else {
|
203
|
+
mdai->type = na_mdai_object_type(mdai->type, v);
|
204
|
+
}
|
205
|
+
}
|
206
|
+
|
207
|
+
if (len == 0) return 1; /* this array is empty */
|
208
|
+
if (mdai->item[ndim - 1].shape < len) {
|
209
|
+
mdai->item[ndim - 1].shape = len;
|
210
|
+
}
|
211
|
+
return 0;
|
212
|
+
}
|
213
|
+
|
214
|
+
static inline int na_mdai_ndim(na_mdai_t* mdai) {
|
215
|
+
int i;
|
216
|
+
// Dimension
|
217
|
+
for (i = 0; i < mdai->capa && mdai->item[i].shape > 0; i++);
|
218
|
+
return i;
|
219
|
+
}
|
220
|
+
|
221
|
+
static inline void na_mdai_shape(na_mdai_t* mdai, int ndim, size_t* shape) {
|
222
|
+
int i;
|
223
|
+
for (i = 0; i < ndim; i++) {
|
224
|
+
shape[i] = mdai->item[i].shape;
|
225
|
+
}
|
226
|
+
}
|
227
|
+
|
228
|
+
static VALUE na_mdai_dtype_numeric(int type) {
|
229
|
+
VALUE tp;
|
230
|
+
// DataType
|
231
|
+
switch (type) {
|
232
|
+
case NA_BIT:
|
233
|
+
tp = numo_cBit;
|
234
|
+
break;
|
235
|
+
case NA_INT32:
|
236
|
+
tp = numo_cInt32;
|
237
|
+
break;
|
238
|
+
case NA_INT64:
|
239
|
+
tp = numo_cInt64;
|
240
|
+
break;
|
241
|
+
case NA_DFLOAT:
|
242
|
+
tp = numo_cDFloat;
|
243
|
+
break;
|
244
|
+
case NA_DCOMPLEX:
|
245
|
+
tp = numo_cDComplex;
|
246
|
+
break;
|
247
|
+
case NA_ROBJ:
|
248
|
+
tp = numo_cRObject;
|
249
|
+
break;
|
250
|
+
default:
|
251
|
+
tp = Qnil;
|
252
|
+
}
|
253
|
+
return tp;
|
254
|
+
}
|
255
|
+
|
256
|
+
static VALUE na_mdai_dtype(na_mdai_t* mdai) {
|
257
|
+
VALUE tp;
|
258
|
+
|
259
|
+
tp = na_mdai_dtype_numeric(mdai->type);
|
260
|
+
|
261
|
+
if (!NIL_P(mdai->na_type)) {
|
262
|
+
if (NIL_P(tp)) {
|
263
|
+
tp = mdai->na_type;
|
264
|
+
} else {
|
265
|
+
tp = na_upcast(mdai->na_type, tp);
|
266
|
+
}
|
267
|
+
}
|
268
|
+
return tp;
|
269
|
+
}
|
270
|
+
|
271
|
+
static inline VALUE update_type(VALUE* ptype, VALUE dtype) {
|
272
|
+
if (ptype) {
|
273
|
+
if (*ptype == cNArray || !RTEST(*ptype)) {
|
274
|
+
*ptype = dtype;
|
275
|
+
} else {
|
276
|
+
dtype = *ptype;
|
277
|
+
}
|
278
|
+
}
|
279
|
+
return dtype;
|
280
|
+
}
|
281
|
+
|
282
|
+
static inline void check_subclass_of_narray(VALUE dtype) {
|
283
|
+
if (RTEST(rb_obj_is_kind_of(dtype, rb_cClass))) {
|
284
|
+
if (RTEST(rb_funcall(dtype, id_le, 1, cNArray))) {
|
285
|
+
return;
|
286
|
+
}
|
287
|
+
}
|
288
|
+
rb_raise(nary_eCastError, "cannot convert to NArray");
|
289
|
+
}
|
290
|
+
|
291
|
+
static size_t na_mdai_memsize(const void* ptr) {
|
292
|
+
const na_mdai_t* mdai = (const na_mdai_t*)ptr;
|
293
|
+
|
294
|
+
return sizeof(na_mdai_t) + mdai->capa * sizeof(na_mdai_item_t);
|
295
|
+
}
|
296
|
+
|
297
|
+
static const rb_data_type_t mdai_data_type = {"Numo::NArray/mdai",
|
298
|
+
{
|
299
|
+
NULL,
|
300
|
+
na_mdai_free,
|
301
|
+
na_mdai_memsize,
|
302
|
+
},
|
303
|
+
0,
|
304
|
+
0,
|
305
|
+
RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED};
|
306
|
+
|
307
|
+
static void na_composition3_ary(VALUE ary, VALUE* ptype, VALUE* pshape, VALUE* pnary) {
|
308
|
+
VALUE vmdai;
|
309
|
+
na_mdai_t* mdai;
|
310
|
+
int i, ndim;
|
311
|
+
size_t* shape;
|
312
|
+
VALUE dtype, dshape;
|
313
|
+
|
314
|
+
mdai = na_mdai_alloc(ary);
|
315
|
+
vmdai = TypedData_Wrap_Struct(rb_cObject, &mdai_data_type, (void*)mdai);
|
316
|
+
if (na_mdai_investigate(mdai, 1)) {
|
317
|
+
// empty
|
318
|
+
dtype = update_type(ptype, numo_cInt32);
|
319
|
+
if (pshape) {
|
320
|
+
*pshape = rb_ary_new3(1, INT2FIX(0));
|
321
|
+
}
|
322
|
+
if (pnary) {
|
323
|
+
check_subclass_of_narray(dtype);
|
324
|
+
shape = ALLOCA_N(size_t, 1);
|
325
|
+
shape[0] = 0;
|
326
|
+
*pnary = nary_new(dtype, 1, shape);
|
327
|
+
}
|
328
|
+
} else {
|
329
|
+
ndim = na_mdai_ndim(mdai);
|
330
|
+
shape = ALLOCA_N(size_t, ndim);
|
331
|
+
na_mdai_shape(mdai, ndim, shape);
|
332
|
+
dtype = update_type(ptype, na_mdai_dtype(mdai));
|
333
|
+
if (pshape) {
|
334
|
+
dshape = rb_ary_new2(ndim);
|
335
|
+
for (i = 0; i < ndim; i++) {
|
336
|
+
rb_ary_push(dshape, SIZET2NUM(shape[i]));
|
337
|
+
}
|
338
|
+
*pshape = dshape;
|
339
|
+
}
|
340
|
+
if (pnary) {
|
341
|
+
check_subclass_of_narray(dtype);
|
342
|
+
*pnary = nary_new(dtype, ndim, shape);
|
343
|
+
}
|
344
|
+
}
|
345
|
+
RB_GC_GUARD(vmdai);
|
346
|
+
}
|
347
|
+
|
348
|
+
static void na_composition3(VALUE obj, VALUE* ptype, VALUE* pshape, VALUE* pnary) {
|
349
|
+
VALUE dtype, dshape;
|
350
|
+
|
351
|
+
if (TYPE(obj) == T_ARRAY) {
|
352
|
+
na_composition3_ary(obj, ptype, pshape, pnary);
|
353
|
+
} else if (RTEST(rb_obj_is_kind_of(obj, rb_cNumeric))) {
|
354
|
+
dtype = na_mdai_dtype_numeric(na_mdai_object_type(NA_NONE, obj));
|
355
|
+
dtype = update_type(ptype, dtype);
|
356
|
+
if (pshape) {
|
357
|
+
*pshape = rb_ary_new();
|
358
|
+
}
|
359
|
+
if (pnary) {
|
360
|
+
check_subclass_of_narray(dtype);
|
361
|
+
*pnary = nary_new(dtype, 0, 0);
|
362
|
+
}
|
363
|
+
} else if (IsNArray(obj)) {
|
364
|
+
int i, ndim;
|
365
|
+
narray_t* na;
|
366
|
+
GetNArray(obj, na);
|
367
|
+
ndim = na->ndim;
|
368
|
+
dtype = update_type(ptype, rb_obj_class(obj));
|
369
|
+
if (pshape) {
|
370
|
+
dshape = rb_ary_new2(ndim);
|
371
|
+
for (i = 0; i < ndim; i++) {
|
372
|
+
rb_ary_push(dshape, SIZET2NUM(na->shape[i]));
|
373
|
+
}
|
374
|
+
*pshape = dshape;
|
375
|
+
}
|
376
|
+
if (pnary) {
|
377
|
+
*pnary = nary_new(dtype, ndim, na->shape);
|
378
|
+
}
|
379
|
+
} else {
|
380
|
+
rb_raise(rb_eTypeError, "invalid type for NArray: %s", rb_class2name(rb_obj_class(obj)));
|
381
|
+
}
|
382
|
+
}
|
383
|
+
|
384
|
+
static VALUE na_s_array_shape(VALUE mod, VALUE ary) {
|
385
|
+
VALUE shape;
|
386
|
+
|
387
|
+
if (TYPE(ary) != T_ARRAY) {
|
388
|
+
// 0-dimension
|
389
|
+
return rb_ary_new();
|
390
|
+
}
|
391
|
+
na_composition3(ary, 0, &shape, 0);
|
392
|
+
return shape;
|
393
|
+
}
|
394
|
+
|
395
|
+
/*
|
396
|
+
Generate new unallocated NArray instance with shape and type defined from obj.
|
397
|
+
Numo::NArray.new_like(obj) returns instance whose type is defined from obj.
|
398
|
+
Numo::DFloat.new_like(obj) returns DFloat instance.
|
399
|
+
|
400
|
+
@overload new_like(obj)
|
401
|
+
@param [Numeric,Array,Numo::NArray] obj
|
402
|
+
@return [Numo::NArray]
|
403
|
+
@example
|
404
|
+
Numo::NArray.new_like([[1,2,3],[4,5,6]])
|
405
|
+
# => Numo::Int32#shape=[2,3](empty)
|
406
|
+
|
407
|
+
Numo::DFloat.new_like([[1,2],[3,4]])
|
408
|
+
# => Numo::DFloat#shape=[2,2](empty)
|
409
|
+
|
410
|
+
Numo::NArray.new_like([1,2i,3])
|
411
|
+
# => Numo::DComplex#shape=[3](empty)
|
412
|
+
*/
|
413
|
+
VALUE
|
414
|
+
na_s_new_like(VALUE type, VALUE obj) {
|
415
|
+
VALUE newary;
|
416
|
+
|
417
|
+
na_composition3(obj, &type, 0, &newary);
|
418
|
+
return newary;
|
419
|
+
}
|
420
|
+
|
421
|
+
VALUE
|
422
|
+
na_ary_composition_dtype(VALUE ary) {
|
423
|
+
VALUE type = Qnil;
|
424
|
+
|
425
|
+
na_composition3(ary, &type, 0, 0);
|
426
|
+
return type;
|
427
|
+
}
|
428
|
+
|
429
|
+
static VALUE na_s_array_type(VALUE mod, VALUE ary) {
|
430
|
+
return na_ary_composition_dtype(ary);
|
431
|
+
}
|
432
|
+
|
433
|
+
/*
|
434
|
+
Generate NArray object. NArray datatype is automatically selected.
|
435
|
+
@overload [](elements)
|
436
|
+
@param [Numeric,Array] elements
|
437
|
+
@return [NArray]
|
438
|
+
*/
|
439
|
+
static VALUE nary_s_bracket(VALUE klass, VALUE ary) {
|
440
|
+
VALUE dtype = Qnil;
|
441
|
+
|
442
|
+
if (TYPE(ary) != T_ARRAY) {
|
443
|
+
rb_bug("Argument is not array");
|
444
|
+
}
|
445
|
+
dtype = na_ary_composition_dtype(ary);
|
446
|
+
check_subclass_of_narray(dtype);
|
447
|
+
return rb_funcall(dtype, id_cast, 1, ary);
|
448
|
+
}
|
449
|
+
|
450
|
+
// VALUE
|
451
|
+
// nst_check_compatibility(VALUE self, VALUE ary);
|
452
|
+
|
453
|
+
/* investigate ndim, shape, type of Array */
|
454
|
+
/*
|
455
|
+
static int
|
456
|
+
na_mdai_for_struct(na_mdai_t *mdai, int ndim)
|
457
|
+
{
|
458
|
+
size_t i;
|
459
|
+
int j, r;
|
460
|
+
size_t len;
|
461
|
+
VALUE v;
|
462
|
+
VALUE val;
|
463
|
+
narray_t *na;
|
464
|
+
|
465
|
+
//fprintf(stderr,"ndim=%d\n",ndim); rb_p(mdai->na_type);
|
466
|
+
if (ndim>4) { abort(); }
|
467
|
+
val = mdai->item[ndim].val;
|
468
|
+
|
469
|
+
//fpintf(stderr,"val = "); rb_p(val);
|
470
|
+
|
471
|
+
if (rb_obj_class(val) == mdai->na_type) {
|
472
|
+
GetNArray(val,na);
|
473
|
+
if ( ndim+na->ndim > mdai->capa ) {
|
474
|
+
abort();
|
475
|
+
na_mdai_realloc(mdai,((na->ndim-1)/4+1)*4);
|
476
|
+
}
|
477
|
+
for ( j=0,r=ndim; j < na->ndim; j++,r++ ) {
|
478
|
+
if ( mdai->item[r].shape < na->shape[j] )
|
479
|
+
mdai->item[r].shape = na->shape[j];
|
480
|
+
}
|
481
|
+
return 1;
|
482
|
+
}
|
483
|
+
|
484
|
+
if (TYPE(val) == T_ARRAY) {
|
485
|
+
// check recursive array
|
486
|
+
for (j=0; j<ndim-1; j++) {
|
487
|
+
if (mdai->item[j].val == val)
|
488
|
+
rb_raise(rb_eStandardError,
|
489
|
+
"cannot convert from a recursive Array to NArray");
|
490
|
+
}
|
491
|
+
//fprintf(stderr,"check:"); rb_p(val);
|
492
|
+
// val is a Struct recort
|
493
|
+
if (RTEST( nst_check_compatibility(mdai->na_type, val) )) {
|
494
|
+
//fputs("compati\n",stderr);
|
495
|
+
return 1;
|
496
|
+
}
|
497
|
+
// otherwise, multi-dimension
|
498
|
+
if (ndim >= mdai->capa) {
|
499
|
+
//fprintf(stderr,"exeed capa\n"); abort();
|
500
|
+
na_mdai_realloc(mdai,4);
|
501
|
+
}
|
502
|
+
// finally, multidimension-check
|
503
|
+
len = RARRAY_LEN(val);
|
504
|
+
for (i=0; i < len; i++) {
|
505
|
+
v = RARRAY_AREF(val,i);
|
506
|
+
if (TYPE(v) != T_ARRAY) {
|
507
|
+
//abort();
|
508
|
+
return 0;
|
509
|
+
}
|
510
|
+
}
|
511
|
+
for (i=0; i < len; i++) {
|
512
|
+
v = RARRAY_AREF(val,i);
|
513
|
+
//fprintf(stderr,"check:"); rb_p(v);
|
514
|
+
mdai->item[ndim+1].val = v;
|
515
|
+
if ( na_mdai_for_struct( mdai, ndim+1 ) == 0 ) {
|
516
|
+
//fprintf(stderr,"not struct:"); rb_p(v);
|
517
|
+
//abort();
|
518
|
+
return 0;
|
519
|
+
}
|
520
|
+
}
|
521
|
+
if (mdai->item[ndim].shape < len) {
|
522
|
+
mdai->item[ndim].shape = len;
|
523
|
+
}
|
524
|
+
return 1;
|
525
|
+
}
|
526
|
+
|
527
|
+
//fprintf(stderr,"invalid for struct:"); rb_p(val); abort();
|
528
|
+
return 0;
|
529
|
+
}
|
530
|
+
*/
|
531
|
+
|
532
|
+
/*
|
533
|
+
VALUE
|
534
|
+
na_ary_composition_for_struct(VALUE nstruct, VALUE ary)
|
535
|
+
{
|
536
|
+
volatile VALUE vmdai, vnc;
|
537
|
+
na_mdai_t *mdai;
|
538
|
+
na_compose_t *nc;
|
539
|
+
|
540
|
+
mdai = na_mdai_alloc(ary);
|
541
|
+
mdai->na_type = nstruct;
|
542
|
+
vmdai = TypedData_Wrap_Struct(rb_cObject, &mdai_data_type, (void*)mdai);
|
543
|
+
na_mdai_for_struct(mdai, 0);
|
544
|
+
nc = na_compose_alloc();
|
545
|
+
vnc = WrapCompose(nc);
|
546
|
+
na_mdai_result(mdai, nc);
|
547
|
+
//fprintf(stderr,"nc->ndim=%d\n",nc->ndim);
|
548
|
+
rb_gc_force_recycle(vmdai);
|
549
|
+
return vnc;
|
550
|
+
}
|
551
|
+
*/
|
552
|
+
|
553
|
+
void Init_nary_array(void) {
|
554
|
+
rb_define_singleton_method(cNArray, "array_shape", na_s_array_shape, 1);
|
555
|
+
rb_define_singleton_method(cNArray, "array_type", na_s_array_type, 1);
|
556
|
+
rb_define_singleton_method(cNArray, "new_like", na_s_new_like, 1);
|
557
|
+
|
558
|
+
rb_define_singleton_method(cNArray, "[]", nary_s_bracket, -2);
|
559
|
+
|
560
|
+
id_begin = rb_intern("begin");
|
561
|
+
id_end = rb_intern("end");
|
562
|
+
id_step = rb_intern("step");
|
563
|
+
id_cast = rb_intern("cast");
|
564
|
+
id_abs = rb_intern("abs");
|
565
|
+
id_le = rb_intern("<=");
|
566
|
+
#if SIZEOF_LONG <= 4
|
567
|
+
id_ge = rb_intern(">=");
|
568
|
+
#endif
|
569
|
+
id_Complex = rb_intern("Complex");
|
570
|
+
|
571
|
+
rb_global_variable(&int32_max);
|
572
|
+
int32_max = INT2NUM(2147483647);
|
573
|
+
rb_global_variable(&int32_min);
|
574
|
+
int32_min = INT2NUM(-2147483648);
|
575
|
+
}
|