numo-narray 0.9.0.5 → 0.9.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ee92d774c7adbc21131b5d25b83f2cd83ffb2870
4
- data.tar.gz: 185833b9b03560dfd494a000268f6d920883b343
3
+ metadata.gz: fc2454fb169ad87ab4cb44c775c52906cb7636e8
4
+ data.tar.gz: f81cd5ccc97b7fec259329021fce9ed896871670
5
5
  SHA512:
6
- metadata.gz: 9c4de1ae4febaa26a8b7da95f2e3e97278e0e3ee208194926cc0ecfdc5bcf799dcac372a2b970c0d89102e6210bd239d2ca357a5c7598e7c54719ccc63d5ae35
7
- data.tar.gz: e474764872f99bb8cab763d268b971674831ddfaafadb457749cc90c54440ff74f014a77b85617946c36ddd17e96f59906112103c3305bdb9d38b78781826b34
6
+ metadata.gz: c0e17e85b64041604643c9dfec270466dcf298cb794059f62f203eb82eb0b001d7a60a1b992c4bba7e0c286b03f508aaab0181187c9dff983f78b1c1f860c2f5
7
+ data.tar.gz: 3a37fe344a33dd64effe30eb531300ef0144d250ad9223198bb4cbc016cbff7a312f3f39fdc4ad2bc83314dfc2de708d842c64740494690f79c62acfd58ef277
data/Rakefile CHANGED
@@ -1,5 +1,15 @@
1
1
  require "bundler/gem_tasks"
2
2
  begin
3
+
4
+ task :doc do
5
+ dir = "ext/numo/narray"
6
+ src = %w[array.c data.c index.c math.c narray.c rand.c struct.c].
7
+ map{|s| File.join(dir,s)} +
8
+ [File.join(dir,"types/*.c"), "lib/numo/narray/extra.rb"]
9
+ sh "cd ext/numo/narray; ruby extconf.rb; make src"
10
+ sh "rm -rf yard .yardoc; yard doc -o yard -r README.md #{src.join(' ')}"
11
+ end
12
+
3
13
  require "rake/extensiontask"
4
14
  require "rake_compiler_dock"
5
15
  require "shellwords"
@@ -52,14 +62,5 @@ namespace :release do
52
62
  end
53
63
  end
54
64
 
55
- task :doc do
56
- dir = "ext/numo/narray"
57
- src = %w[array.c data.c index.c math.c narray.c rand.c struct.c].
58
- map{|s| File.join(dir,s)} +
59
- [File.join(dir,"types/*.c"), "lib/numo/narray/extra.rb"]
60
- sh "cd ext/numo/narray; ruby extconf.rb; make src"
61
- sh "rm -rf yard .yardoc; yard doc -o yard -r README.md #{src.join(' ')}"
62
- end
63
-
64
65
  rescue LoadError
65
66
  end
@@ -32,147 +32,104 @@ static ID id_cast;
32
32
  static ID id_le;
33
33
  static ID id_Complex;
34
34
 
35
- typedef struct {
36
- int ndim;
37
- size_t *shape;
38
- VALUE dtype;
39
- } na_compose_t;
40
-
41
- static size_t
42
- na_compose_memsize(const void *ptr)
43
- {
44
- const na_compose_t *nc = (const na_compose_t*)ptr;
45
-
46
- return sizeof(na_compose_t) + nc->ndim * sizeof(size_t);
47
- }
48
-
49
- static void
50
- na_compose_free(void *ptr)
51
- {
52
- na_compose_t *nc = (na_compose_t*)ptr;
53
-
54
- if (nc->shape)
55
- xfree(nc->shape);
56
- xfree(nc);
57
- }
58
-
59
- static void
60
- na_compose_gc_mark(void* nc)
61
- {
62
- rb_gc_mark(((na_compose_t*)nc)->dtype);
63
- }
64
-
65
- static const rb_data_type_t compose_data_type = {
66
- "Numo::NArray/compose",
67
- {na_compose_gc_mark, na_compose_free, na_compose_memsize,},
68
- 0, 0, RUBY_TYPED_FREE_IMMEDIATELY|RUBY_TYPED_WB_PROTECTED
69
- };
70
-
71
- #define WrapCompose(p) TypedData_Wrap_Struct(rb_cData, &compose_data_type, (void*)(p));
72
- #define GetCompose(v,p) TypedData_Get_Struct(v, na_compose_t, &compose_data_type, p)
73
35
 
74
36
  static VALUE
75
37
  na_object_type(int type, VALUE v)
76
38
  {
77
39
  static VALUE int32_max = Qnil;
78
40
  if (NIL_P(int32_max))
79
- int32_max = ULONG2NUM(2147483647);
41
+ int32_max = ULONG2NUM(2147483647);
80
42
 
81
43
  switch(TYPE(v)) {
82
44
 
83
45
  case T_TRUE:
84
46
  case T_FALSE:
85
- if (type<NA_BIT)
86
- return NA_BIT;
87
- return type;
47
+ if (type<NA_BIT)
48
+ return NA_BIT;
49
+ return type;
88
50
 
89
51
  #if SIZEOF_LONG == 4
90
52
  case T_FIXNUM:
91
- if (type<NA_INT32)
92
- return NA_INT32;
93
- return type;
53
+ if (type<NA_INT32)
54
+ return NA_INT32;
55
+ return type;
94
56
  case T_BIGNUM:
95
- if (type<NA_INT64) {
96
- v = rb_funcall(v,id_abs,0);
97
- if (RTEST(rb_funcall(v,id_le,1,int32_max))) {
98
- if (type<NA_INT32)
99
- return NA_INT32;
100
- } else {
101
- return NA_INT64;
102
- }
103
- }
104
- return type;
57
+ if (type<NA_INT64) {
58
+ v = rb_funcall(v,id_abs,0);
59
+ if (RTEST(rb_funcall(v,id_le,1,int32_max))) {
60
+ if (type<NA_INT32)
61
+ return NA_INT32;
62
+ } else {
63
+ return NA_INT64;
64
+ }
65
+ }
66
+ return type;
105
67
 
106
68
  #elif SIZEOF_LONG == 8
107
69
  case T_FIXNUM:
108
- if (type<NA_INT64) {
109
- long x = NUM2LONG(v);
110
- if (x<0) x=-x;
111
- if (x<=2147483647) {
112
- if (type<NA_INT32)
113
- return NA_INT32;
114
- } else {
115
- return NA_INT64;
116
- }
117
- }
118
- return type;
70
+ if (type<NA_INT64) {
71
+ long x = NUM2LONG(v);
72
+ if (x<0) x=-x;
73
+ if (x<=2147483647) {
74
+ if (type<NA_INT32)
75
+ return NA_INT32;
76
+ } else {
77
+ return NA_INT64;
78
+ }
79
+ }
80
+ return type;
119
81
  case T_BIGNUM:
120
- if (type<NA_INT64)
121
- return NA_INT64;
122
- return type;
82
+ if (type<NA_INT64)
83
+ return NA_INT64;
84
+ return type;
123
85
  #else
124
86
  case T_FIXNUM:
125
87
  case T_BIGNUM:
126
- if (type<NA_INT64) {
127
- v = rb_funcall(v,id_abs,0);
128
- if (RTEST(rb_funcall(v,id_le,1,int32_max))) {
129
- if (type<NA_INT32)
130
- return NA_INT32;
131
- } else {
132
- return NA_INT64;
133
- }
134
- }
135
- return type;
88
+ if (type<NA_INT64) {
89
+ v = rb_funcall(v,id_abs,0);
90
+ if (RTEST(rb_funcall(v,id_le,1,int32_max))) {
91
+ if (type<NA_INT32)
92
+ return NA_INT32;
93
+ } else {
94
+ return NA_INT64;
95
+ }
96
+ }
97
+ return type;
136
98
  #endif
137
99
 
138
100
  case T_FLOAT:
139
- if (type<NA_DFLOAT)
140
- return NA_DFLOAT;
141
- return type;
101
+ if (type<NA_DFLOAT)
102
+ return NA_DFLOAT;
103
+ return type;
142
104
 
143
105
  case T_NIL:
144
- return type;
106
+ return type;
145
107
 
146
108
  default:
147
- if (CLASS_OF(v) == rb_const_get( rb_cObject, id_Complex )) {
148
- return NA_DCOMPLEX;
149
- }
109
+ if (CLASS_OF(v) == rb_const_get( rb_cObject, id_Complex )) {
110
+ return NA_DCOMPLEX;
111
+ }
150
112
  }
151
113
  return NA_ROBJ;
152
114
  }
153
115
 
154
116
 
155
- #define MDAI_ATTR_TYPE(tp,v,attr) \
156
- {tp = na_object_type(tp,rb_funcall(v,id_##attr,0));}
117
+ #define MDAI_ATTR_TYPE(tp,v,attr) \
118
+ {tp = na_object_type(tp,rb_funcall(v,id_##attr,0));}
157
119
 
158
- void na_mdai_object_type(na_mdai_t *mdai, VALUE v)
120
+ static int na_mdai_object_type(int type, VALUE v)
159
121
  {
160
- if (IsNArray(v)) {
161
- if (NIL_P(mdai->na_type)) {
162
- mdai->na_type = CLASS_OF(v);
163
- } else {
164
- mdai->na_type = na_upcast(CLASS_OF(v), mdai->na_type);
165
- }
166
- } else if (rb_obj_is_kind_of(v, rb_cRange)) {
167
- MDAI_ATTR_TYPE(mdai->type,v,begin);
168
- MDAI_ATTR_TYPE(mdai->type,v,end);
122
+ if (rb_obj_is_kind_of(v, rb_cRange)) {
123
+ MDAI_ATTR_TYPE(type,v,begin);
124
+ MDAI_ATTR_TYPE(type,v,end);
169
125
  } else if (rb_obj_is_kind_of(v, na_cStep)) {
170
- MDAI_ATTR_TYPE(mdai->type,v,begin);
171
- MDAI_ATTR_TYPE(mdai->type,v,end);
172
- MDAI_ATTR_TYPE(mdai->type,v,step);
126
+ MDAI_ATTR_TYPE(type,v,begin);
127
+ MDAI_ATTR_TYPE(type,v,end);
128
+ MDAI_ATTR_TYPE(type,v,step);
173
129
  } else {
174
- mdai->type = na_object_type(mdai->type,v);
130
+ type = na_object_type(type,v);
175
131
  }
132
+ return type;
176
133
  }
177
134
 
178
135
 
@@ -186,8 +143,8 @@ na_mdai_alloc(VALUE ary)
186
143
  mdai->capa = n;
187
144
  mdai->item = ALLOC_N( na_mdai_item_t, n );
188
145
  for (i=0; i<n; i++) {
189
- mdai->item[i].shape = 0;
190
- mdai->item[i].val = Qnil;
146
+ mdai->item[i].shape = 0;
147
+ mdai->item[i].val = Qnil;
191
148
  }
192
149
  mdai->item[0].val = ary;
193
150
  mdai->type = NA_NONE;
@@ -206,8 +163,8 @@ na_mdai_realloc(na_mdai_t *mdai, int n_extra)
206
163
  n = mdai->capa;
207
164
  REALLOC_N( mdai->item, na_mdai_item_t, n );
208
165
  for (; i<n; i++) {
209
- mdai->item[i].shape = 0;
210
- mdai->item[i].val = Qnil;
166
+ mdai->item[i].shape = 0;
167
+ mdai->item[i].val = Qnil;
211
168
  }
212
169
  }
213
170
 
@@ -237,108 +194,149 @@ na_mdai_investigate(na_mdai_t *mdai, int ndim)
237
194
  for (i=0; i < RARRAY_LEN(val); i++) {
238
195
  v = RARRAY_AREF(val,i);
239
196
 
240
- if (TYPE(v) == T_ARRAY) {
241
- /* check recursive array */
242
- for (j=0; j<ndim; j++) {
243
- if (mdai->item[j].val == v)
244
- rb_raise(rb_eStandardError,
245
- "cannot convert from a recursive Array to NArray");
246
- }
247
- if ( ndim >= mdai->capa ) {
248
- na_mdai_realloc(mdai,4);
249
- }
250
- mdai->item[ndim].val = v;
251
- if ( na_mdai_investigate(mdai,ndim+1) ) {
252
- len--; /* Array is empty */
253
- }
254
- }
255
- else
197
+ if (TYPE(v) == T_ARRAY) {
198
+ /* check recursive array */
199
+ for (j=0; j<ndim; j++) {
200
+ if (mdai->item[j].val == v)
201
+ rb_raise(rb_eStandardError,
202
+ "cannot convert from a recursive Array to NArray");
203
+ }
204
+ if ( ndim >= mdai->capa ) {
205
+ na_mdai_realloc(mdai,4);
206
+ }
207
+ mdai->item[ndim].val = v;
208
+ if ( na_mdai_investigate(mdai,ndim+1) ) {
209
+ len--; /* Array is empty */
210
+ }
211
+ }
212
+ else
256
213
  if (rb_obj_is_kind_of(v, rb_cRange) || rb_obj_is_kind_of(v, na_cStep)) {
257
- nary_step_sequence(v,&length,&dbeg,&dstep);
258
- len += length-1;
259
- na_mdai_object_type(mdai,v);
260
- }
261
- else {
262
- na_mdai_object_type(mdai,v);
263
-
264
- if (IsNArray(v)) {
265
- int r;
266
- narray_t *na;
267
- GetNArray(v,na);
268
- if ( na->ndim == 0 ) {
269
- len--; /* NArray is empty */
270
- } else {
271
- if ( ndim+na->ndim > mdai->capa ) {
272
- na_mdai_realloc(mdai,((na->ndim-1)/4+1)*4);
273
- }
274
- for ( j=0,r=ndim; j < na->ndim ; j++,r++ ) {
275
- if ( mdai->item[r].shape < na->shape[j] )
276
- mdai->item[r].shape = na->shape[j];
277
- }
278
- }
279
- }
280
- }
214
+ nary_step_sequence(v,&length,&dbeg,&dstep);
215
+ len += length-1;
216
+ mdai->type = na_mdai_object_type(mdai->type, v);
217
+ }
218
+ else if (IsNArray(v)) {
219
+ int r;
220
+ narray_t *na;
221
+ GetNArray(v,na);
222
+ if ( na->ndim == 0 ) {
223
+ len--; /* NArray is empty */
224
+ } else {
225
+ if ( ndim+na->ndim > mdai->capa ) {
226
+ na_mdai_realloc(mdai,((na->ndim-1)/4+1)*4);
227
+ }
228
+ for ( j=0,r=ndim; j < na->ndim ; j++,r++ ) {
229
+ if ( mdai->item[r].shape < na->shape[j] )
230
+ mdai->item[r].shape = na->shape[j];
231
+ }
232
+ }
233
+ // type
234
+ if (NIL_P(mdai->na_type)) {
235
+ mdai->na_type = CLASS_OF(v);
236
+ } else {
237
+ mdai->na_type = na_upcast(CLASS_OF(v), mdai->na_type);
238
+ }
239
+ } else {
240
+ mdai->type = na_mdai_object_type(mdai->type, v);
241
+ }
281
242
  }
282
243
 
283
244
  if (len==0) return 1; /* this array is empty */
284
245
  if (mdai->item[ndim-1].shape < len) {
285
- mdai->item[ndim-1].shape = len;
246
+ mdai->item[ndim-1].shape = len;
286
247
  }
287
248
  return 0;
288
249
  }
289
250
 
290
- static void
291
- na_mdai_result(na_mdai_t *mdai, na_compose_t *nc)
292
- {
293
- int i, ndim;
294
- VALUE tp;
295
- size_t *shape;
296
251
 
252
+ static inline int
253
+ na_mdai_ndim(na_mdai_t *mdai)
254
+ {
255
+ int i;
297
256
  // Dimension
298
257
  for (i=0; i < mdai->capa && mdai->item[i].shape > 0; i++) ;
299
- nc->ndim = ndim = i;
300
- nc->shape = NULL;
301
- nc->dtype = Qnil;
302
-
303
- if (ndim>0) {
304
- // Shape
305
- nc->shape = shape = ALLOC_N(size_t,ndim);
306
- for (i=0; i<ndim; i++) {
307
- shape[i] = mdai->item[i].shape;
258
+ return i;
259
+ }
260
+
261
+ static inline void
262
+ na_mdai_shape(na_mdai_t *mdai, int ndim, size_t *shape)
263
+ {
264
+ int i;
265
+ for (i=0; i<ndim; i++) {
266
+ shape[i] = mdai->item[i].shape;
267
+ }
268
+ }
269
+
270
+ static VALUE
271
+ na_mdai_dtype_numeric(int type)
272
+ {
273
+ VALUE tp;
274
+ // DataType
275
+ switch(type) {
276
+ case NA_BIT:
277
+ tp = numo_cBit;
278
+ break;
279
+ case NA_INT32:
280
+ tp = numo_cInt32;
281
+ break;
282
+ case NA_INT64:
283
+ tp = numo_cInt64;
284
+ break;
285
+ case NA_DFLOAT:
286
+ tp = numo_cDFloat;
287
+ break;
288
+ case NA_DCOMPLEX:
289
+ tp = numo_cDComplex;
290
+ break;
291
+ case NA_ROBJ:
292
+ tp = numo_cRObject;
293
+ break;
294
+ default:
295
+ tp = Qnil;
296
+ }
297
+ return tp;
298
+ }
299
+
300
+ static VALUE
301
+ na_mdai_dtype(na_mdai_t *mdai)
302
+ {
303
+ VALUE tp;
304
+
305
+ tp = na_mdai_dtype_numeric(mdai->type);
306
+
307
+ if (!NIL_P(mdai->na_type)) {
308
+ if (NIL_P(tp)) {
309
+ tp = mdai->na_type;
310
+ } else {
311
+ tp = na_upcast(mdai->na_type,tp);
308
312
  }
313
+ }
314
+ return tp;
315
+ }
316
+
309
317
 
310
- // DataType
311
- switch(mdai->type) {
312
- case NA_BIT:
313
- tp = numo_cBit;
314
- break;
315
- case NA_INT32:
316
- tp = numo_cInt32;
317
- break;
318
- case NA_INT64:
319
- tp = numo_cInt64;
320
- break;
321
- case NA_DFLOAT:
322
- tp = numo_cDFloat;
323
- break;
324
- case NA_DCOMPLEX:
325
- tp = numo_cDComplex;
326
- break;
327
- case NA_ROBJ:
328
- tp = numo_cRObject;
329
- break;
330
- default:
331
- tp = Qnil;
332
- }
333
- if (!NIL_P(mdai->na_type)) {
334
- if (NIL_P(tp)) {
335
- tp = mdai->na_type;
336
- } else {
337
- tp = na_upcast(mdai->na_type,tp);
338
- }
339
- }
340
- nc->dtype = tp;
318
+ static inline VALUE
319
+ update_type(VALUE *ptype, VALUE dtype)
320
+ {
321
+ if (ptype) {
322
+ if (*ptype == cNArray || !RTEST(*ptype)) {
323
+ *ptype = dtype;
324
+ } else {
325
+ dtype = *ptype;
326
+ }
341
327
  }
328
+ return dtype;
329
+ }
330
+
331
+ static inline void
332
+ check_subclass_of_narray(VALUE dtype)
333
+ {
334
+ if (RTEST(rb_obj_is_kind_of(dtype, rb_cClass))) {
335
+ if (RTEST(rb_funcall(dtype, id_le, 1, cNArray))) {
336
+ return;
337
+ }
338
+ }
339
+ rb_raise(nary_eCastError, "cannot convert to NArray");
342
340
  }
343
341
 
344
342
 
@@ -356,87 +354,105 @@ static const rb_data_type_t mdai_data_type = {
356
354
  0, 0, RUBY_TYPED_FREE_IMMEDIATELY|RUBY_TYPED_WB_PROTECTED
357
355
  };
358
356
 
359
- VALUE
360
- na_ary_composition(VALUE ary)
357
+
358
+ static void
359
+ na_composition3_ary(VALUE ary, VALUE *ptype, VALUE *pshape, VALUE *pnary)
361
360
  {
362
- volatile VALUE vmdai, vnc;
361
+ VALUE vmdai;
363
362
  na_mdai_t *mdai;
364
- na_compose_t *nc;
365
- int j;
363
+ int i, ndim;
364
+ size_t *shape;
365
+ VALUE dtype, dshape;
366
366
 
367
- nc = ALLOC(na_compose_t);
368
- vnc = WrapCompose(nc);
369
- if (TYPE(ary) == T_ARRAY) {
370
- mdai = na_mdai_alloc(ary);
371
- vmdai = TypedData_Wrap_Struct(rb_cData, &mdai_data_type, (void*)mdai);
372
- if ( na_mdai_investigate(mdai, 1) ) {
373
- // empty
374
- nc->ndim = 1;
375
- nc->shape = ALLOC_N(size_t, 1);
376
- nc->shape[0] = 0;
377
- nc->dtype = Qnil;
378
- } else {
379
- na_mdai_result(mdai, nc);
367
+ mdai = na_mdai_alloc(ary);
368
+ vmdai = TypedData_Wrap_Struct(rb_cData, &mdai_data_type, (void*)mdai);
369
+ if ( na_mdai_investigate(mdai, 1) ) {
370
+ // empty
371
+ dtype = update_type(ptype, numo_cInt32);
372
+ if (pshape) {
373
+ *pshape = rb_ary_new3(1, INT2FIX(0));
380
374
  }
381
- rb_gc_force_recycle(vmdai);
382
- } else if (IsNArray(ary)) {
383
- narray_t *na;
384
- GetNArray(ary,na);
385
- nc->ndim = na->ndim;
386
- nc->shape = ALLOC_N(size_t, na->ndim);
387
- for (j=0; j<na->ndim; j++) {
388
- nc->shape[j] = na->shape[j];
375
+ if (pnary) {
376
+ check_subclass_of_narray(dtype);
377
+ shape = ALLOCA_N(size_t, 1);
378
+ shape[0] = 0;
379
+ *pnary = nary_new(dtype, 1, shape);
389
380
  }
390
- nc->dtype = CLASS_OF(ary);
391
381
  } else {
392
- rb_bug("invalid type for md-array: %s", rb_class2name(CLASS_OF(ary)));
382
+ ndim = na_mdai_ndim(mdai);
383
+ shape = ALLOCA_N(size_t, ndim);
384
+ na_mdai_shape(mdai, ndim, shape);
385
+ dtype = update_type(ptype, na_mdai_dtype(mdai));
386
+ if (pshape) {
387
+ dshape = rb_ary_new2(ndim);
388
+ for (i=0; i<ndim; i++) {
389
+ rb_ary_push(dshape, SIZET2NUM(shape[i]));
390
+ }
391
+ *pshape = dshape;
392
+ }
393
+ if (pnary) {
394
+ check_subclass_of_narray(dtype);
395
+ *pnary = nary_new(dtype, ndim, shape);
396
+ }
393
397
  }
394
- return vnc;
398
+ RB_GC_GUARD(vmdai);
395
399
  }
396
400
 
397
401
 
398
402
  static void
399
- na_ary_composition2(VALUE ary, VALUE *type, VALUE *shape)
403
+ na_composition3(VALUE obj, VALUE *ptype, VALUE *pshape, VALUE *pnary)
400
404
  {
401
- VALUE vnc, dshape;
402
- na_compose_t *nc;
403
- int i;
405
+ VALUE dtype, dshape;
404
406
 
405
- // investigate MD-Array
406
- vnc = na_ary_composition(ary);
407
- GetCompose(vnc,nc);
408
- dshape = rb_ary_new2(nc->ndim);
409
- for (i=0; i<nc->ndim; i++) {
410
- rb_ary_push(dshape, SIZET2NUM(nc->shape[i]));
407
+ if (TYPE(obj) == T_ARRAY) {
408
+ na_composition3_ary(obj, ptype, pshape, pnary);
409
+ }
410
+ else if (RTEST(rb_obj_is_kind_of(obj,rb_cNumeric))) {
411
+ dtype = na_mdai_dtype_numeric(na_mdai_object_type(NA_NONE, obj));
412
+ dtype = update_type(ptype, dtype);
413
+ if (pshape) {
414
+ *pshape = rb_ary_new();
415
+ }
416
+ if (pnary) {
417
+ check_subclass_of_narray(dtype);
418
+ *pnary = nary_new(dtype, 0, 0);
419
+ }
420
+ }
421
+ else if (IsNArray(obj)) {
422
+ int i, ndim;
423
+ narray_t *na;
424
+ GetNArray(obj,na);
425
+ ndim = na->ndim;
426
+ dtype = update_type(ptype, CLASS_OF(obj));
427
+ if (pshape) {
428
+ dshape = rb_ary_new2(ndim);
429
+ for (i=0; i<ndim; i++) {
430
+ rb_ary_push(dshape, SIZET2NUM(na->shape[i]));
431
+ }
432
+ *pshape = dshape;
433
+ }
434
+ if (pnary) {
435
+ *pnary = nary_new(dtype, ndim, na->shape);
436
+ }
437
+ } else {
438
+ rb_bug("invalid type for md-array: %s", rb_class2name(CLASS_OF(obj)));
411
439
  }
412
- if (shape) {*shape = dshape;}
413
- if (type) {*type = nc->dtype;}
414
- RB_GC_GUARD(vnc);
415
440
  }
416
441
 
442
+
417
443
  static VALUE
418
444
  na_s_array_shape(VALUE mod, VALUE ary)
419
445
  {
420
446
  VALUE shape;
421
447
 
422
- if (TYPE(ary)!=T_ARRAY) {
423
- // 0-dimension
424
- return rb_ary_new();
448
+ if (TYPE(ary) != T_ARRAY) {
449
+ // 0-dimension
450
+ return rb_ary_new();
425
451
  }
426
- na_ary_composition2(ary, 0, &shape);
452
+ na_composition3(ary, 0, &shape, 0);
427
453
  return shape;
428
454
  }
429
455
 
430
- static inline void
431
- check_subclass_of_narray(VALUE dtype) {
432
- if (RTEST(rb_obj_is_kind_of(dtype, rb_cClass))) {
433
- if (RTEST(rb_funcall(dtype, id_le, 1, cNArray))) {
434
- return;
435
- }
436
- }
437
- rb_raise(nary_eCastError, "cannot convert to NArray");
438
- }
439
-
440
456
 
441
457
  /*
442
458
  Generate new unallocated NArray instance with shape and type defined from obj.
@@ -457,29 +473,9 @@ check_subclass_of_narray(VALUE dtype) {
457
473
  VALUE
458
474
  na_s_new_like(VALUE type, VALUE obj)
459
475
  {
460
- VALUE vnc, newary;
461
- na_compose_t *nc;
476
+ VALUE newary;
462
477
 
463
- if (RTEST(rb_obj_is_kind_of(obj,rb_cNumeric))) {
464
- // investigate type
465
- if (type == cNArray) {
466
- vnc = na_ary_composition(rb_ary_new3(1,obj));
467
- GetCompose(vnc,nc);
468
- type = nc->dtype;
469
- }
470
- check_subclass_of_narray(type);
471
- newary = nary_new(type, 0, 0);
472
- } else {
473
- // investigate MD-Array
474
- vnc = na_ary_composition(obj);
475
- GetCompose(vnc,nc);
476
- if (type == cNArray) {
477
- type = nc->dtype;
478
- }
479
- check_subclass_of_narray(type);
480
- newary = nary_new(type, nc->ndim, nc->shape);
481
- }
482
- RB_GC_GUARD(vnc);
478
+ na_composition3(obj, &type, 0, &newary);
483
479
  return newary;
484
480
  }
485
481
 
@@ -487,16 +483,10 @@ na_s_new_like(VALUE type, VALUE obj)
487
483
  VALUE
488
484
  na_ary_composition_dtype(VALUE ary)
489
485
  {
490
- volatile VALUE vnc;
491
- na_compose_t *nc;
486
+ VALUE type = Qnil;
492
487
 
493
- switch(TYPE(ary)) {
494
- case T_ARRAY:
495
- vnc = na_ary_composition(ary);
496
- GetCompose(vnc,nc);
497
- return nc->dtype;
498
- }
499
- return CLASS_OF(ary);
488
+ na_composition3(ary, &type, 0, 0);
489
+ return type;
500
490
  }
501
491
 
502
492
  static VALUE
@@ -506,8 +496,6 @@ na_s_array_type(VALUE mod, VALUE ary)
506
496
  }
507
497
 
508
498
 
509
-
510
-
511
499
  /*
512
500
  Generate NArray object. NArray datatype is automatically selected.
513
501
  @overload [](elements)
@@ -528,11 +516,12 @@ nary_s_bracket(VALUE klass, VALUE ary)
528
516
  }
529
517
 
530
518
 
531
- VALUE
532
- nst_check_compatibility(VALUE self, VALUE ary);
519
+ //VALUE
520
+ //nst_check_compatibility(VALUE self, VALUE ary);
533
521
 
534
522
 
535
523
  /* investigate ndim, shape, type of Array */
524
+ /*
536
525
  static int
537
526
  na_mdai_for_struct(na_mdai_t *mdai, int ndim)
538
527
  {
@@ -563,7 +552,7 @@ na_mdai_for_struct(na_mdai_t *mdai, int ndim)
563
552
  }
564
553
 
565
554
  if (TYPE(val) == T_ARRAY) {
566
- /* check recursive array */
555
+ // check recursive array
567
556
  for (j=0; j<ndim-1; j++) {
568
557
  if (mdai->item[j].val == val)
569
558
  rb_raise(rb_eStandardError,
@@ -608,8 +597,10 @@ na_mdai_for_struct(na_mdai_t *mdai, int ndim)
608
597
  //fprintf(stderr,"invalid for struct:"); rb_p(val); abort();
609
598
  return 0;
610
599
  }
600
+ */
611
601
 
612
602
 
603
+ /*
613
604
  VALUE
614
605
  na_ary_composition_for_struct(VALUE nstruct, VALUE ary)
615
606
  {
@@ -621,20 +612,20 @@ na_ary_composition_for_struct(VALUE nstruct, VALUE ary)
621
612
  mdai->na_type = nstruct;
622
613
  vmdai = TypedData_Wrap_Struct(rb_cData, &mdai_data_type, (void*)mdai);
623
614
  na_mdai_for_struct(mdai, 0);
624
- nc = ALLOC(na_compose_t);
615
+ nc = na_compose_alloc();
625
616
  vnc = WrapCompose(nc);
626
617
  na_mdai_result(mdai, nc);
627
618
  //fprintf(stderr,"nc->ndim=%d\n",nc->ndim);
628
619
  rb_gc_force_recycle(vmdai);
629
620
  return vnc;
630
621
  }
622
+ */
631
623
 
632
624
 
633
625
 
634
626
  void
635
627
  Init_nary_array()
636
628
  {
637
- //rb_define_singleton_method(cNArray, "mdai", na_mdai, 1);
638
629
  rb_define_singleton_method(cNArray, "array_shape", na_s_array_shape, 1);
639
630
  rb_define_singleton_method(cNArray, "array_type", na_s_array_type, 1);
640
631
  rb_define_singleton_method(cNArray, "new_like", na_s_new_like, 1);