numo-narray 0.9.0.4 → 0.9.0.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.
Files changed (135) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +11 -8
  3. data/Rakefile +9 -0
  4. data/ext/numo/narray/array.c +178 -47
  5. data/ext/numo/narray/data.c +105 -97
  6. data/ext/numo/narray/depend.erb +6 -7
  7. data/ext/numo/narray/gen/cogen.rb +30 -7
  8. data/ext/numo/narray/gen/def/bit.rb +17 -14
  9. data/ext/numo/narray/gen/def/dcomplex.rb +19 -15
  10. data/ext/numo/narray/gen/def/dfloat.rb +17 -13
  11. data/ext/numo/narray/gen/def/int16.rb +16 -12
  12. data/ext/numo/narray/gen/def/int32.rb +16 -12
  13. data/ext/numo/narray/gen/def/int64.rb +16 -12
  14. data/ext/numo/narray/gen/def/int8.rb +16 -12
  15. data/ext/numo/narray/gen/def/robject.rb +18 -14
  16. data/ext/numo/narray/gen/def/scomplex.rb +19 -15
  17. data/ext/numo/narray/gen/def/sfloat.rb +17 -13
  18. data/ext/numo/narray/gen/def/uint16.rb +16 -12
  19. data/ext/numo/narray/gen/def/uint32.rb +16 -12
  20. data/ext/numo/narray/gen/def/uint64.rb +16 -12
  21. data/ext/numo/narray/gen/def/uint8.rb +16 -12
  22. data/ext/numo/narray/gen/erbpp2.rb +324 -0
  23. data/ext/numo/narray/gen/narray_def.rb +252 -0
  24. data/ext/numo/narray/gen/spec.rb +141 -71
  25. data/ext/numo/narray/gen/tmpl/accum.c +22 -9
  26. data/ext/numo/narray/gen/tmpl/accum_binary.c +29 -13
  27. data/ext/numo/narray/gen/tmpl/accum_index.c +38 -16
  28. data/ext/numo/narray/gen/tmpl/alloc_func.c +107 -0
  29. data/ext/numo/narray/gen/tmpl/allocate.c +1 -1
  30. data/ext/numo/narray/gen/tmpl/aref.c +1 -1
  31. data/ext/numo/narray/gen/tmpl/aset.c +4 -2
  32. data/ext/numo/narray/gen/tmpl/binary.c +4 -4
  33. data/ext/numo/narray/gen/tmpl/binary2.c +5 -5
  34. data/ext/numo/narray/gen/tmpl/binary_s.c +5 -5
  35. data/ext/numo/narray/gen/tmpl/bincount.c +4 -4
  36. data/ext/numo/narray/gen/tmpl/cast.c +9 -6
  37. data/ext/numo/narray/gen/tmpl/cast_array.c +4 -9
  38. data/ext/numo/narray/gen/tmpl/class.c +9 -0
  39. data/ext/numo/narray/gen/tmpl/clip.c +118 -0
  40. data/ext/numo/narray/gen/tmpl/coerce_cast.c +4 -2
  41. data/ext/numo/narray/gen/tmpl/cond_binary.c +5 -5
  42. data/ext/numo/narray/gen/tmpl/cond_unary.c +6 -6
  43. data/ext/numo/narray/gen/tmpl/cum.c +18 -9
  44. data/ext/numo/narray/gen/tmpl/each.c +2 -2
  45. data/ext/numo/narray/gen/tmpl/each_with_index.c +2 -2
  46. data/ext/numo/narray/gen/tmpl/extract.c +2 -2
  47. data/ext/numo/narray/gen/tmpl/extract_data.c +48 -0
  48. data/ext/numo/narray/gen/tmpl/eye.c +3 -3
  49. data/ext/numo/narray/gen/tmpl/fill.c +2 -2
  50. data/ext/numo/narray/gen/tmpl/format.c +5 -5
  51. data/ext/numo/narray/gen/tmpl/format_to_a.c +4 -4
  52. data/ext/numo/narray/gen/tmpl/frexp.c +37 -0
  53. data/ext/numo/narray/gen/tmpl/init_class.c +20 -0
  54. data/ext/numo/narray/gen/tmpl/init_module.c +12 -0
  55. data/ext/numo/narray/gen/tmpl/inspect.c +2 -2
  56. data/ext/numo/narray/gen/tmpl/lib.c +45 -0
  57. data/ext/numo/narray/gen/tmpl/logseq.c +1 -1
  58. data/ext/numo/narray/gen/tmpl/map_with_index.c +2 -2
  59. data/ext/numo/narray/gen/tmpl/median.c +31 -8
  60. data/ext/numo/narray/gen/tmpl/minmax.c +24 -24
  61. data/ext/numo/narray/gen/tmpl/module.c +9 -0
  62. data/ext/numo/narray/gen/tmpl/new_dim0.c +12 -0
  63. data/ext/numo/narray/gen/tmpl/poly.c +3 -3
  64. data/ext/numo/narray/gen/tmpl/pow.c +1 -1
  65. data/ext/numo/narray/gen/tmpl/powint.c +1 -1
  66. data/ext/numo/narray/gen/tmpl/qsort.c +10 -3
  67. data/ext/numo/narray/gen/tmpl/rand.c +1 -1
  68. data/ext/numo/narray/gen/tmpl/rand_norm.c +1 -1
  69. data/ext/numo/narray/gen/tmpl/seq.c +1 -1
  70. data/ext/numo/narray/gen/tmpl/set2.c +5 -5
  71. data/ext/numo/narray/gen/tmpl/sort.c +29 -14
  72. data/ext/numo/narray/gen/tmpl/sort_index.c +41 -20
  73. data/ext/numo/narray/gen/tmpl/store.c +11 -5
  74. data/ext/numo/narray/gen/tmpl/store_array.c +1 -1
  75. data/ext/numo/narray/gen/tmpl/store_bit.c +1 -1
  76. data/ext/numo/narray/gen/tmpl/store_from.c +1 -1
  77. data/ext/numo/narray/gen/tmpl/store_numeric.c +3 -16
  78. data/ext/numo/narray/gen/tmpl/to_a.c +2 -2
  79. data/ext/numo/narray/gen/tmpl/unary.c +7 -7
  80. data/ext/numo/narray/gen/tmpl/unary2.c +8 -8
  81. data/ext/numo/narray/gen/tmpl/unary_ret2.c +33 -0
  82. data/ext/numo/narray/gen/tmpl/unary_s.c +8 -8
  83. data/ext/numo/narray/gen/tmpl_bit/allocate.c +1 -5
  84. data/ext/numo/narray/gen/tmpl_bit/aref.c +1 -1
  85. data/ext/numo/narray/gen/tmpl_bit/aset.c +2 -2
  86. data/ext/numo/narray/gen/tmpl_bit/binary.c +8 -8
  87. data/ext/numo/narray/gen/tmpl_bit/bit_count.c +8 -8
  88. data/ext/numo/narray/gen/tmpl_bit/bit_reduce.c +6 -6
  89. data/ext/numo/narray/gen/tmpl_bit/each.c +2 -2
  90. data/ext/numo/narray/gen/tmpl_bit/each_with_index.c +2 -2
  91. data/ext/numo/narray/gen/tmpl_bit/extract.c +1 -1
  92. data/ext/numo/narray/gen/tmpl_bit/fill.c +2 -2
  93. data/ext/numo/narray/gen/tmpl_bit/format.c +5 -5
  94. data/ext/numo/narray/gen/tmpl_bit/format_to_a.c +2 -2
  95. data/ext/numo/narray/gen/tmpl_bit/inspect.c +2 -2
  96. data/ext/numo/narray/gen/tmpl_bit/mask.c +5 -5
  97. data/ext/numo/narray/gen/tmpl_bit/none_p.c +4 -4
  98. data/ext/numo/narray/gen/tmpl_bit/store_array.c +2 -2
  99. data/ext/numo/narray/gen/tmpl_bit/store_bit.c +1 -1
  100. data/ext/numo/narray/gen/tmpl_bit/store_from.c +1 -1
  101. data/ext/numo/narray/gen/tmpl_bit/to_a.c +2 -2
  102. data/ext/numo/narray/gen/tmpl_bit/unary.c +9 -9
  103. data/ext/numo/narray/gen/tmpl_bit/where.c +6 -6
  104. data/ext/numo/narray/gen/tmpl_bit/where2.c +8 -8
  105. data/ext/numo/narray/index.c +46 -30
  106. data/ext/numo/narray/math.c +12 -6
  107. data/ext/numo/narray/narray.c +242 -218
  108. data/ext/numo/narray/ndloop.c +17 -24
  109. data/ext/numo/narray/numo/intern.h +63 -67
  110. data/ext/numo/narray/numo/narray.h +38 -13
  111. data/ext/numo/narray/numo/ndloop.h +1 -1
  112. data/ext/numo/narray/numo/template.h +1 -1
  113. data/ext/numo/narray/numo/types/complex.h +8 -4
  114. data/ext/numo/narray/numo/types/complex_macro.h +118 -1
  115. data/ext/numo/narray/numo/types/float_macro.h +283 -6
  116. data/ext/numo/narray/numo/types/robj_macro.h +261 -9
  117. data/ext/numo/narray/numo/types/xint_macro.h +35 -0
  118. data/ext/numo/narray/struct.c +34 -15
  119. data/lib/erbpp.rb +5 -1
  120. data/lib/erbpp/line_number.rb +10 -3
  121. data/lib/erbpp/narray_def.rb +55 -25
  122. data/lib/numo/narray/extra.rb +638 -219
  123. data/numo-narray.gemspec +1 -0
  124. data/spec/narray_spec.rb +2 -2
  125. metadata +17 -14
  126. data/ext/numo/narray/gen/dtype.erb.c +0 -82
  127. data/ext/numo/narray/gen/tmpl/cast_numeric.c +0 -22
  128. data/ext/numo/narray/gen/tmpl/robj_allocate.c +0 -32
  129. data/ext/numo/narray/gen/tmpl_bit/cast.c +0 -37
  130. data/ext/numo/narray/gen/tmpl_bit/cast_array.c +0 -18
  131. data/ext/numo/narray/gen/tmpl_bit/cast_numeric.c +0 -22
  132. data/ext/numo/narray/gen/tmpl_bit/coerce_cast.c +0 -8
  133. data/ext/numo/narray/gen/tmpl_bit/map_with_index.c +0 -94
  134. data/ext/numo/narray/gen/tmpl_bit/store.c +0 -32
  135. data/ext/numo/narray/gen/tmpl_bit/store_numeric.c +0 -22
@@ -8,7 +8,7 @@ typedef struct {
8
8
  #define STORE_INT(ptr, esz, x) memcpy(ptr,&(x),esz)
9
9
 
10
10
  static void
11
- iter_bit_where(na_loop_t *const lp)
11
+ <%=c_iter%>(na_loop_t *const lp)
12
12
  {
13
13
  size_t i;
14
14
  BIT_DIGIT *a;
@@ -58,24 +58,24 @@ iter_bit_where(na_loop_t *const lp)
58
58
  @return [Numo::Int32,Numo::Int64]
59
59
  */
60
60
  static VALUE
61
- numo_bit_where(VALUE self)
61
+ <%=c_func(0)%>(VALUE self)
62
62
  {
63
63
  volatile VALUE idx_1;
64
64
  size_t size, n_1;
65
65
  where_opt_t *g;
66
66
 
67
67
  ndfunc_arg_in_t ain[1] = {{cT,0}};
68
- ndfunc_t ndf = { iter_bit_where, FULL_LOOP, 1, 0, ain, 0 };
68
+ ndfunc_t ndf = { <%=c_iter%>, FULL_LOOP, 1, 0, ain, 0 };
69
69
 
70
70
  size = RNARRAY_SIZE(self);
71
- n_1 = NUM2SIZET(numo_bit_count_true(0, NULL, self));
71
+ n_1 = NUM2SIZET(<%=find_tmpl("count_true").c_func%>(0, NULL, self));
72
72
  g = ALLOCA_N(where_opt_t,1);
73
73
  g->count = 0;
74
74
  if (size>4294967295ul) {
75
- idx_1 = rb_narray_new(numo_cInt64, 1, &n_1);
75
+ idx_1 = nary_new(numo_cInt64, 1, &n_1);
76
76
  g->elmsz = 8;
77
77
  } else {
78
- idx_1 = rb_narray_new(numo_cInt32, 1, &n_1);
78
+ idx_1 = nary_new(numo_cInt32, 1, &n_1);
79
79
  g->elmsz = 4;
80
80
  }
81
81
  g->idx1 = na_get_pointer_for_write(idx_1);
@@ -1,5 +1,5 @@
1
1
  static void
2
- iter_bit_where2(na_loop_t *const lp)
2
+ <%=c_iter%>(na_loop_t *const lp)
3
3
  {
4
4
  size_t i;
5
5
  BIT_DIGIT *a;
@@ -59,27 +59,27 @@ iter_bit_where2(na_loop_t *const lp)
59
59
  @return [Numo::Int32,Numo::Int64]*2
60
60
  */
61
61
  static VALUE
62
- numo_bit_where2(VALUE self)
62
+ <%=c_func(0)%>(VALUE self)
63
63
  {
64
64
  VALUE idx_1, idx_0;
65
65
  size_t size, n_1, n_0;
66
66
  where_opt_t *g;
67
67
 
68
68
  ndfunc_arg_in_t ain[1] = {{cT,0}};
69
- ndfunc_t ndf = { iter_bit_where2, FULL_LOOP, 1, 0, ain, 0 };
69
+ ndfunc_t ndf = { <%=c_iter%>, FULL_LOOP, 1, 0, ain, 0 };
70
70
 
71
71
  size = RNARRAY_SIZE(self);
72
- n_1 = NUM2SIZET(numo_bit_count_true(0, NULL, self));
72
+ n_1 = NUM2SIZET(<%=find_tmpl("count_true").c_func%>(0, NULL, self));
73
73
  n_0 = size - n_1;
74
74
  g = ALLOCA_N(where_opt_t,1);
75
75
  g->count = 0;
76
76
  if (size>4294967295ul) {
77
- idx_1 = rb_narray_new(numo_cInt64, 1, &n_1);
78
- idx_0 = rb_narray_new(numo_cInt64, 1, &n_0);
77
+ idx_1 = nary_new(numo_cInt64, 1, &n_1);
78
+ idx_0 = nary_new(numo_cInt64, 1, &n_0);
79
79
  g->elmsz = 8;
80
80
  } else {
81
- idx_1 = rb_narray_new(numo_cInt32, 1, &n_1);
82
- idx_0 = rb_narray_new(numo_cInt32, 1, &n_0);
81
+ idx_1 = nary_new(numo_cInt32, 1, &n_1);
82
+ idx_0 = nary_new(numo_cInt32, 1, &n_0);
83
83
  g->elmsz = 4;
84
84
  }
85
85
  g->idx1 = na_get_pointer_for_write(idx_1);
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  index.c
3
3
  Numerical Array Extension for Ruby
4
- (C) Copyright 1999-2016 by Masahiro TANAKA
4
+ (C) Copyright 1999-2017 by Masahiro TANAKA
5
5
  */
6
6
  //#define NARRAY_C
7
7
 
@@ -74,7 +74,12 @@ static VALUE sym_rest;
74
74
  static ID id_beg;
75
75
  static ID id_end;
76
76
  static ID id_exclude_end;
77
- static ID id_each, id_step;
77
+ static ID id_each;
78
+ static ID id_step;
79
+ static ID id_dup;
80
+ static ID id_bracket;
81
+ static ID id_shift_left;
82
+ static ID id_mask;
78
83
 
79
84
 
80
85
  void
@@ -148,7 +153,7 @@ na_parse_narray_index(VALUE a, int orig_dim, ssize_t size, na_index_arg_t *q)
148
153
  rb_raise(rb_eIndexError, "should be 1-d NArray");
149
154
  }
150
155
  n = NA_SIZE(na);
151
- idx = rb_narray_new(cIndex,1,&n);
156
+ idx = nary_new(cIndex,1,&n);
152
157
  na_store(idx,a);
153
158
 
154
159
  GetNArrayData(idx,nidx);
@@ -382,8 +387,8 @@ na_index_aref_nadata(narray_data_t *na1, narray_view_t *na2,
382
387
  na2->base.shape[j] = size = q[i].n;
383
388
 
384
389
  if (q[i].reduce != 0) {
385
- m = rb_funcall(INT2FIX(1),rb_intern("<<"),1,INT2FIX(j));
386
- na2->base.reduce = rb_funcall(m,rb_intern("|"),1,na2->base.reduce);
390
+ m = rb_funcall(INT2FIX(1),id_shift_left,1,INT2FIX(j));
391
+ na2->base.reduce = rb_funcall(m,'|',1,na2->base.reduce);
387
392
  }
388
393
 
389
394
  // array index
@@ -409,7 +414,7 @@ na_index_aref_nadata(narray_data_t *na1, narray_view_t *na2,
409
414
 
410
415
  static void
411
416
  na_index_aref_naview(narray_view_t *na1, narray_view_t *na2,
412
- na_index_arg_t *q, int ndim, int keep_dim)
417
+ na_index_arg_t *q, ssize_t elmsz, int ndim, int keep_dim)
413
418
  {
414
419
  int i, j;
415
420
  ssize_t total=1;
@@ -431,11 +436,15 @@ na_index_aref_naview(narray_view_t *na1, narray_view_t *na2,
431
436
  na2->base.shape[j] = size = q[i].n;
432
437
 
433
438
  if (q[i].reduce != 0) {
434
- VALUE m = rb_funcall(INT2FIX(1),rb_intern("<<"),1,INT2FIX(j));
435
- na2->base.reduce = rb_funcall(m,rb_intern("|"),1,na2->base.reduce);
439
+ VALUE m = rb_funcall(INT2FIX(1),id_shift_left,1,INT2FIX(j));
440
+ na2->base.reduce = rb_funcall(m,'|',1,na2->base.reduce);
436
441
  }
437
442
 
438
- if (q[i].idx != NULL && SDX_IS_INDEX(sdx1)) {
443
+ if (q[i].orig_dim >= na1->base.ndim) {
444
+ // new dimension
445
+ SDX_SET_STRIDE(na2->stridx[j], elmsz);
446
+ }
447
+ else if (q[i].idx != NULL && SDX_IS_INDEX(sdx1)) {
439
448
  // index <- index
440
449
  int k;
441
450
  size_t *index = q[i].idx;
@@ -546,6 +555,7 @@ VALUE na_aref_md_protected(VALUE data_value)
546
555
  int ndim_new;
547
556
  VALUE view;
548
557
  narray_view_t *na2;
558
+ ssize_t elmsz;
549
559
 
550
560
  na_index_parse_args(args, na1, q, ndim);
551
561
 
@@ -565,16 +575,18 @@ VALUE na_aref_md_protected(VALUE data_value)
565
575
 
566
576
  na2->stridx = ALLOC_N(stridx_t,ndim_new);
567
577
 
578
+ elmsz = nary_element_stride(self);
579
+
568
580
  switch(na1->type) {
569
581
  case NARRAY_DATA_T:
570
582
  case NARRAY_FILEMAP_T:
571
- na_index_aref_nadata((narray_data_t *)na1,na2,q,na_get_elmsz(self),ndim,keep_dim);
583
+ na_index_aref_nadata((narray_data_t *)na1,na2,q,elmsz,ndim,keep_dim);
572
584
  na2->data = self;
573
585
  break;
574
586
  case NARRAY_VIEW_T:
575
587
  na2->offset = ((narray_view_t *)na1)->offset;
576
588
  na2->data = ((narray_view_t *)na1)->data;
577
- na_index_aref_naview((narray_view_t *)na1,na2,q,ndim,keep_dim);
589
+ na_index_aref_naview((narray_view_t *)na1,na2,q,elmsz,ndim,keep_dim);
578
590
  break;
579
591
  }
580
592
  if (store) {
@@ -614,12 +626,12 @@ na_aref_md(int argc, VALUE *argv, VALUE self, int keep_dim, int result_nd)
614
626
  if (argc == 1 && result_nd == 1) {
615
627
  idx = argv[0];
616
628
  if (rb_obj_is_kind_of(idx, rb_cArray)) {
617
- idx = rb_apply(numo_cNArray,rb_intern("[]"),idx);
629
+ idx = rb_apply(numo_cNArray,id_bracket,idx);
618
630
  }
619
631
  if (rb_obj_is_kind_of(idx, numo_cNArray)) {
620
632
  GetNArray(idx,nidx);
621
633
  if (NA_NDIM(nidx)>1) {
622
- store = rb_narray_new(CLASS_OF(self),NA_NDIM(nidx),NA_SHAPE(nidx));
634
+ store = nary_new(CLASS_OF(self),NA_NDIM(nidx),NA_SHAPE(nidx));
623
635
  idx = na_flatten(idx);
624
636
  RARRAY_ASET(args,0,idx);
625
637
  }
@@ -650,11 +662,11 @@ na_aref_main(int nidx, VALUE *idx, VALUE self, int keep_dim, int nd)
650
662
  na_index_arg_to_internal_order(nidx, idx, self);
651
663
 
652
664
  if (nidx==0) {
653
- return rb_funcall(self,rb_intern("copy"),0);
665
+ return rb_funcall(self,id_dup,0);
654
666
  }
655
667
  if (nidx==1) {
656
668
  if (CLASS_OF(*idx)==numo_cBit) {
657
- return rb_funcall(*idx,rb_intern("mask"),1,self);
669
+ return rb_funcall(*idx,id_mask,1,self);
658
670
  }
659
671
  }
660
672
  return na_aref_md(nidx, idx, self, keep_dim, nd);
@@ -781,7 +793,7 @@ na_get_result_dimension(VALUE self, int argc, VALUE *argv, ssize_t stride, size_
781
793
  break;
782
794
  default:
783
795
  if (!stride) {
784
- stride = na_get_elmsz(self);
796
+ stride = nary_element_stride(self);
785
797
  }
786
798
  if (argc==1 && j==1) {
787
799
  x = na_range_check(idx[0], na->size, 0);
@@ -808,19 +820,23 @@ Init_nary_index()
808
820
  {
809
821
  rb_define_method(cNArray, "slice", na_slice, -1);
810
822
 
811
- sym_ast = ID2SYM(rb_intern("*"));
812
- sym_all = ID2SYM(rb_intern("all"));
813
- sym_minus = ID2SYM(rb_intern("-"));
814
- sym_new = ID2SYM(rb_intern("new"));
815
- sym_reverse = ID2SYM(rb_intern("reverse"));
816
- sym_plus = ID2SYM(rb_intern("+"));
817
- //sym_reduce = ID2SYM(rb_intern("reduce"));
818
- sym_sum = ID2SYM(rb_intern("sum"));
819
- sym_tilde = ID2SYM(rb_intern("~"));
820
- sym_rest = ID2SYM(rb_intern("rest"));
821
- id_beg = rb_intern("begin");
822
- id_end = rb_intern("end");
823
+ sym_ast = ID2SYM(rb_intern("*"));
824
+ sym_all = ID2SYM(rb_intern("all"));
825
+ sym_minus = ID2SYM(rb_intern("-"));
826
+ sym_new = ID2SYM(rb_intern("new"));
827
+ sym_reverse = ID2SYM(rb_intern("reverse"));
828
+ sym_plus = ID2SYM(rb_intern("+"));
829
+ //sym_reduce = ID2SYM(rb_intern("reduce"));
830
+ sym_sum = ID2SYM(rb_intern("sum"));
831
+ sym_tilde = ID2SYM(rb_intern("~"));
832
+ sym_rest = ID2SYM(rb_intern("rest"));
833
+ id_beg = rb_intern("begin");
834
+ id_end = rb_intern("end");
823
835
  id_exclude_end = rb_intern("exclude_end?");
824
- id_each = rb_intern("each");
825
- id_step = rb_intern("step");
836
+ id_each = rb_intern("each");
837
+ id_step = rb_intern("step");
838
+ id_dup = rb_intern("dup");
839
+ id_bracket = rb_intern("[]");
840
+ id_shift_left = rb_intern("<<");
841
+ id_mask = rb_intern("mask");
826
842
  }
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  math.c
3
3
  Numerical Array Extension for Ruby
4
- (C) Copyright 1999-2016 by Masahiro TANAKA
4
+ (C) Copyright 1999-2017 by Masahiro TANAKA
5
5
  */
6
6
  #include <ruby.h>
7
7
  #include "numo/narray.h"
@@ -10,6 +10,9 @@ VALUE numo_mNMath;
10
10
  EXTERN VALUE numo_mDFloatMath, numo_mDComplexMath;
11
11
  EXTERN VALUE numo_mSFloatMath, numo_mSComplexMath;
12
12
  static ID id_send;
13
+ static ID id_UPCAST;
14
+ static ID id_DISPATCH;
15
+ static ID id_extract;
13
16
 
14
17
  VALUE
15
18
  nary_type_s_upcast(VALUE type1, VALUE type2)
@@ -18,12 +21,12 @@ nary_type_s_upcast(VALUE type1, VALUE type2)
18
21
  VALUE result_type;
19
22
 
20
23
  if (type1==type2) return type1;
21
- upcast_hash = rb_const_get(type1, rb_intern("UPCAST"));
24
+ upcast_hash = rb_const_get(type1, id_UPCAST);
22
25
  result_type = rb_hash_aref(upcast_hash, type2);
23
26
  if (NIL_P(result_type)) {
24
27
  if (TYPE(type2)==T_CLASS) {
25
28
  if ( RTEST(rb_class_inherited_p(type2,cNArray)) ) {
26
- upcast_hash = rb_const_get(type2, rb_intern("UPCAST"));
29
+ upcast_hash = rb_const_get(type2, id_UPCAST);
27
30
  result_type = rb_hash_aref(upcast_hash, type1);
28
31
  }
29
32
  }
@@ -85,7 +88,7 @@ VALUE nary_math_method_missing(int argc, VALUE *argv, VALUE mod)
85
88
  if (argc>1) {
86
89
  type = nary_mathcast(argc-1,argv+1);
87
90
 
88
- hash = rb_const_get(mod, rb_intern("DISPATCH"));
91
+ hash = rb_const_get(mod, id_DISPATCH);
89
92
  typemod = rb_hash_aref( hash, type );
90
93
  if (NIL_P(typemod)) {
91
94
  rb_raise(rb_eTypeError,"%s is unknown for Numo::NMath",
@@ -96,7 +99,7 @@ VALUE nary_math_method_missing(int argc, VALUE *argv, VALUE mod)
96
99
 
97
100
  if (!RTEST(rb_class_inherited_p(type,cNArray)) &&
98
101
  IsNArray(ans) ) {
99
- ans = rb_funcall(ans,rb_intern("extract"),0);
102
+ ans = rb_funcall(ans,id_extract,0);
100
103
  }
101
104
  return ans;
102
105
  }
@@ -137,5 +140,8 @@ Init_nary_math()
137
140
  rb_hash_aset(hCast, rb_cFloat, rb_mMath);
138
141
  rb_hash_aset(hCast, rb_cComplex, numo_mDComplexMath);
139
142
 
140
- id_send = rb_intern("send");
143
+ id_send = rb_intern("send");
144
+ id_UPCAST = rb_intern("UPCAST");
145
+ id_DISPATCH = rb_intern("DISPATCH");
146
+ id_extract = rb_intern("extract");
141
147
  }
@@ -1,11 +1,10 @@
1
1
  /*
2
2
  narray.c
3
3
  Numerical Array Extension for Ruby
4
- (C) Copyright 1999-2016 by Masahiro TANAKA
4
+ (C) Copyright 1999-2017 by Masahiro TANAKA
5
5
  */
6
6
  #define NARRAY_C
7
7
  #include <ruby.h>
8
- #include "numo/narray.h"
9
8
  #include <assert.h>
10
9
 
11
10
  /* global variables within this module */
@@ -17,13 +16,25 @@ VALUE nary_eOperationError;
17
16
  VALUE nary_eDimensionError;
18
17
 
19
18
  static ID id_contiguous_stride;
20
- //static ID id_element_bit_size;
21
- //static ID id_element_byte_size;
19
+ static ID id_allocate;
20
+ static ID id_element_byte_size;
21
+ static ID id_fill;
22
+ static ID id_seq;
23
+ static ID id_logseq;
24
+ static ID id_eye;
25
+ static ID id_UPCAST;
26
+ static ID id_cast;
27
+ static ID id_dup;
28
+ static ID id_to_host;
29
+ static ID id_bracket;
30
+ static ID id_shift_left;
31
+ static ID id_eq;
32
+ static ID id_count_false;
33
+ static ID id_axis;
34
+ static ID id_nan;
22
35
 
23
36
  VALUE cPointer;
24
37
 
25
- static ID id_allocate;
26
-
27
38
  VALUE sym_reduce;
28
39
  VALUE sym_option;
29
40
  VALUE sym_loop_opt;
@@ -38,30 +49,37 @@ int numo_na_inspect_rows=20;
38
49
  int numo_na_inspect_cols=80;
39
50
 
40
51
  void Init_nary_data();
52
+ void Init_nary_ndloop();
41
53
  void Init_nary_step();
42
54
  void Init_nary_index();
43
- void Init_nary_bit();
44
- void Init_nary_int8();
45
- void Init_nary_int16();
46
- void Init_nary_int32();
47
- void Init_nary_int64();
48
- void Init_nary_uint8();
49
- void Init_nary_uint16();
50
- void Init_nary_uint32();
51
- void Init_nary_uint64();
52
- void Init_nary_sfloat();
53
- void Init_nary_scomplex();
54
- void Init_nary_dfloat();
55
- void Init_nary_dcomplex();
55
+ void Init_numo_bit();
56
+ void Init_numo_int8();
57
+ void Init_numo_int16();
58
+ void Init_numo_int32();
59
+ void Init_numo_int64();
60
+ void Init_numo_uint8();
61
+ void Init_numo_uint16();
62
+ void Init_numo_uint32();
63
+ void Init_numo_uint64();
64
+ void Init_numo_sfloat();
65
+ void Init_numo_scomplex();
66
+ void Init_numo_dfloat();
67
+ void Init_numo_dcomplex();
68
+ void Init_numo_robject();
56
69
  void Init_nary_math();
57
70
  void Init_nary_rand();
58
71
  void Init_nary_array();
59
72
  void Init_nary_struct();
60
- void Init_nary_robject();
73
+
74
+ const rb_data_type_t na_data_type = {
75
+ "Numo::NArray",
76
+ {0, 0, 0,}, 0, 0, 0,
77
+ };
78
+ #include "numo/narray.h"
61
79
 
62
80
 
63
81
  static void
64
- rb_narray_debug_info_nadata(VALUE self)
82
+ nary_debug_info_nadata(VALUE self)
65
83
  {
66
84
  narray_data_t *na;
67
85
  GetNArrayData(self,na);
@@ -71,7 +89,7 @@ rb_narray_debug_info_nadata(VALUE self)
71
89
 
72
90
 
73
91
  static VALUE
74
- rb_narray_debug_info_naview(VALUE self)
92
+ nary_debug_info_naview(VALUE self)
75
93
  {
76
94
  int i;
77
95
  narray_view_t *na;
@@ -106,7 +124,7 @@ rb_narray_debug_info_naview(VALUE self)
106
124
 
107
125
 
108
126
  VALUE
109
- rb_narray_debug_info(VALUE self)
127
+ nary_debug_info(VALUE self)
110
128
  {
111
129
  int i;
112
130
  narray_t *na;
@@ -129,40 +147,55 @@ rb_narray_debug_info(VALUE self)
129
147
  switch(na->type) {
130
148
  case NARRAY_DATA_T:
131
149
  case NARRAY_FILEMAP_T:
132
- rb_narray_debug_info_nadata(self);
150
+ nary_debug_info_nadata(self);
133
151
  break;
134
152
  case NARRAY_VIEW_T:
135
- rb_narray_debug_info_naview(self);
153
+ nary_debug_info_naview(self);
136
154
  break;
137
155
  }
138
156
  return Qnil;
139
157
  }
140
158
 
141
159
 
142
- void
143
- na_free(narray_data_t* na)
160
+ static size_t
161
+ na_view_memsize(const void* ptr)
144
162
  {
145
- assert(na->base.type==NARRAY_DATA_T);
163
+ int i;
164
+ size_t size = sizeof(narray_view_t);
165
+ const narray_view_t *na = ptr;
146
166
 
147
- if (na->ptr != NULL) {
148
- xfree(na->ptr);
149
- na->ptr = NULL;
167
+ assert(na->base.type == NARRAY_VIEW_T);
168
+
169
+ if (na->stridx != NULL) {
170
+ for (i=0; i<na->base.ndim; i++) {
171
+ if (SDX_IS_INDEX(na->stridx[i])) {
172
+ size += sizeof(size_t) * na->base.shape[i];
173
+ }
174
+ }
175
+ size += sizeof(stridx_t) * na->base.ndim;
150
176
  }
151
177
  if (na->base.size > 0) {
152
178
  if (na->base.shape != NULL && na->base.shape != &(na->base.size)) {
153
- xfree(na->base.shape);
154
- na->base.shape = NULL;
179
+ size += sizeof(size_t) * na->base.ndim;
155
180
  }
156
181
  }
157
- xfree(na);
182
+ return size;
158
183
  }
159
184
 
160
185
  static void
161
- na_free_view(narray_view_t* na)
186
+ na_view_free(void* ptr)
162
187
  {
163
- assert(na->base.type==NARRAY_VIEW_T);
188
+ int i;
189
+ narray_view_t *na = (narray_view_t*)ptr;
190
+
191
+ assert(na->base.type == NARRAY_VIEW_T);
164
192
 
165
193
  if (na->stridx != NULL) {
194
+ for (i=0; i<na->base.ndim; i++) {
195
+ if (SDX_IS_INDEX(na->stridx[i])) {
196
+ xfree(SDX_GET_INDEX(na->stridx[i]));
197
+ }
198
+ }
166
199
  xfree(na->stridx);
167
200
  na->stridx = NULL;
168
201
  }
@@ -176,27 +209,18 @@ na_free_view(narray_view_t* na)
176
209
  }
177
210
 
178
211
  static void
179
- na_gc_mark_view(narray_view_t* na)
180
- {
181
- rb_gc_mark(na->data);
182
- }
183
-
184
- VALUE
185
- na_s_allocate(VALUE klass)
212
+ na_view_gc_mark(void* na)
186
213
  {
187
- narray_data_t *na = ALLOC(narray_data_t);
188
-
189
- na->base.ndim = 0;
190
- na->base.type = NARRAY_DATA_T;
191
- na->base.flag[0] = NA_FL0_INIT;
192
- na->base.flag[1] = NA_FL1_INIT;
193
- na->base.size = 0;
194
- na->base.shape = NULL;
195
- na->base.reduce = INT2FIX(0);
196
- na->ptr = NULL;
197
- return Data_Wrap_Struct(klass, 0, na_free, na);
214
+ if (((narray_t*)na)->type == NARRAY_VIEW_T) {
215
+ rb_gc_mark(((narray_view_t*)na)->data);
216
+ }
198
217
  }
199
218
 
219
+ const rb_data_type_t na_data_type_view = {
220
+ "Numo::NArrayView",
221
+ {na_view_gc_mark, na_view_free, na_view_memsize,},
222
+ &na_data_type, 0, 0,
223
+ };
200
224
 
201
225
  VALUE
202
226
  na_s_allocate_view(VALUE klass)
@@ -213,7 +237,7 @@ na_s_allocate_view(VALUE klass)
213
237
  na->data = Qnil;
214
238
  na->offset = 0;
215
239
  na->stridx = NULL;
216
- return Data_Wrap_Struct(klass, na_gc_mark_view, na_free_view, na);
240
+ return TypedData_Wrap_Struct(klass, &na_data_type_view, (void*)na);
217
241
  }
218
242
 
219
243
 
@@ -257,18 +281,18 @@ na_alloc_shape(narray_t *na, int ndim)
257
281
  {
258
282
  na->ndim = ndim;
259
283
  na->size = 0;
260
- if (ndim == 0) {
261
- na->shape = NULL;
262
- }
263
- else if (ndim == 1) {
284
+ switch(ndim) {
285
+ case 0:
286
+ case 1:
264
287
  na->shape = &(na->size);
265
- }
266
- else if (ndim < 0) {
267
- rb_raise(nary_eDimensionError,"ndim=%d is negative", ndim);
268
- }
269
- else if (ndim > NA_MAX_DIMENSION) {
270
- rb_raise(nary_eDimensionError,"ndim=%d is too many", ndim);
271
- } else {
288
+ break;
289
+ default:
290
+ if (ndim < 0) {
291
+ rb_raise(nary_eDimensionError,"ndim=%d is negative", ndim);
292
+ }
293
+ if (ndim > NA_MAX_DIMENSION) {
294
+ rb_raise(nary_eDimensionError,"ndim=%d is too many", ndim);
295
+ }
272
296
  na->shape = ALLOC_N(size_t, ndim);
273
297
  }
274
298
  }
@@ -296,7 +320,7 @@ na_setup_shape(narray_t *na, int ndim, size_t *shape)
296
320
  }
297
321
  }
298
322
 
299
- void
323
+ static void
300
324
  na_setup(VALUE self, int ndim, size_t *shape)
301
325
  {
302
326
  narray_t *na;
@@ -306,13 +330,38 @@ na_setup(VALUE self, int ndim, size_t *shape)
306
330
 
307
331
 
308
332
  /*
309
- * call-seq:
310
- * Numo::DataType.new(shape) => narray
311
- * Numo::DataType.new(size1, size2, ...) => narray
312
- *
313
- * Constructs a narray using the given <i>DataType</i> and <i>shape</i> or
314
- * <i>sizes</i>.
315
- */
333
+ @overload initialize(shape)
334
+ @overload initialize(size0, size1, ...)
335
+ @param [Array] shape (array of sizes along each dimension)
336
+ @param [Integer] sizeN (size along Nth-dimension)
337
+ @return [Numo::NArray] unallocated narray.
338
+
339
+ Constructs an instance of NArray class using the given
340
+ and <i>shape</i> or <i>sizes</i>.
341
+ Note that NArray itself is an abstract super class and
342
+ not suitable to create instances.
343
+ Use Typed Subclasses of NArray (DFloat, Int32, etc) to create instances.
344
+ This method does not allocate memory for array data.
345
+ Memory is allocated on write method such as #fill, #store, #seq, etc.
346
+
347
+ @example
348
+ i = Numo::Int64.new([2,4,3])
349
+ #=> Numo::Int64#shape=[2,4,3](empty)
350
+
351
+ f = Numo::DFloat.new(3,4)
352
+ #=> Numo::DFloat#shape=[3,4](empty)
353
+
354
+ f.fill(2)
355
+ #=> Numo::DFloat#shape=[3,4]
356
+ # [[2, 2, 2, 2],
357
+ # [2, 2, 2, 2],
358
+ # [2, 2, 2, 2]]
359
+
360
+ x = Numo::NArray.new(5)
361
+ #=> in `new': allocator undefined for Numo::NArray (TypeError)
362
+ # from t.rb:9:in `<main>'
363
+
364
+ */
316
365
  static VALUE
317
366
  na_initialize(VALUE self, VALUE args)
318
367
  {
@@ -342,7 +391,7 @@ na_initialize(VALUE self, VALUE args)
342
391
 
343
392
 
344
393
  VALUE
345
- rb_narray_new(VALUE klass, int ndim, size_t *shape)
394
+ nary_new(VALUE klass, int ndim, size_t *shape)
346
395
  {
347
396
  volatile VALUE obj;
348
397
 
@@ -353,7 +402,7 @@ rb_narray_new(VALUE klass, int ndim, size_t *shape)
353
402
 
354
403
 
355
404
  VALUE
356
- rb_narray_view_new(VALUE klass, int ndim, size_t *shape)
405
+ nary_view_new(VALUE klass, int ndim, size_t *shape)
357
406
  {
358
407
  volatile VALUE obj;
359
408
 
@@ -403,7 +452,7 @@ na_s_zeros(int argc, VALUE *argv, VALUE klass)
403
452
  {
404
453
  VALUE obj;
405
454
  obj = rb_class_new_instance(argc, argv, klass);
406
- return rb_funcall(obj, rb_intern("fill"), 1, INT2FIX(0));
455
+ return rb_funcall(obj, id_fill, 1, INT2FIX(0));
407
456
  }
408
457
 
409
458
 
@@ -427,7 +476,7 @@ na_s_ones(int argc, VALUE *argv, VALUE klass)
427
476
  {
428
477
  VALUE obj;
429
478
  obj = rb_class_new_instance(argc, argv, klass);
430
- return rb_funcall(obj, rb_intern("fill"), 1, INT2FIX(1));
479
+ return rb_funcall(obj, id_fill, 1, INT2FIX(1));
431
480
  }
432
481
 
433
482
 
@@ -466,7 +515,7 @@ na_s_linspace(int argc, VALUE *argv, VALUE klass)
466
515
  vstep = rb_funcall(obj, '/', 1, DBL2NUM(n-1));
467
516
 
468
517
  obj = rb_class_new_instance(1, &vsize, klass);
469
- return rb_funcall(obj, rb_intern("seq"), 2, vx1, vstep);
518
+ return rb_funcall(obj, id_seq, 2, vx1, vstep);
470
519
  }
471
520
 
472
521
  /*
@@ -510,7 +559,7 @@ na_s_logspace(int argc, VALUE *argv, VALUE klass)
510
559
  vstep = rb_funcall(obj, '/', 1, DBL2NUM(n-1));
511
560
 
512
561
  obj = rb_class_new_instance(1, &vsize, klass);
513
- return rb_funcall(obj, rb_intern("logseq"), 3, vx1, vstep, vbase);
562
+ return rb_funcall(obj, id_logseq, 3, vx1, vstep, vbase);
514
563
  }
515
564
 
516
565
 
@@ -541,7 +590,7 @@ na_s_eye(int argc, VALUE *argv, VALUE klass)
541
590
  argc = 2;
542
591
  }
543
592
  obj = rb_class_new_instance(argc, argv, klass);
544
- return rb_funcall(obj, rb_intern("eye"), 0);
593
+ return rb_funcall(obj, id_eye, 0);
545
594
  }
546
595
 
547
596
 
@@ -647,47 +696,6 @@ na_release_lock(VALUE self)
647
696
  }
648
697
 
649
698
 
650
-
651
- /*
652
- stridx_t *
653
- na_get_stride(VALUE v)
654
- {
655
- int i;
656
- size_t st;
657
- stridx_t *stridx=NULL;
658
- narray_t *na;
659
- GetNArray(v,na);
660
-
661
- switch(NA_TYPE(na)) {
662
- case NARRAY_DATA_T:
663
- if (NA_DATA_PTR(na)==NULL) {
664
- rb_raise(rb_eRuntimeError,"cannot read no-data NArray");
665
- }
666
- break;
667
- case NARRAY_FILEMAP_T:
668
- break;
669
- case NARRAY_VIEW_T:
670
- stridx = NA_STRIDX(na);
671
- break;
672
- default:
673
- rb_raise(rb_eRuntimeError,"invalid narray internal type");
674
- }
675
-
676
- if (!stridx) {
677
- stridx = ALLOC_N(stridx_t, na->ndim);
678
- st = NUM2SIZET(rb_const_get(CLASS_OF(v), id_contiguous_stride));
679
- //printf("step_unit=%ld, CLASS_OF(v)=%lx\n",st, CLASS_OF(v));
680
- for (i=na->ndim; i>0;) {
681
- SDX_SET_STRIDE(stridx[--i],st);
682
- st *= na->shape[i];
683
- }
684
- }
685
-
686
- return stridx;
687
- }
688
- */
689
-
690
-
691
699
  /* method: size() -- returns the total number of typeents */
692
700
  static VALUE
693
701
  na_size(VALUE self)
@@ -750,10 +758,21 @@ static VALUE
750
758
  }
751
759
 
752
760
 
753
- size_t
754
- na_get_elmsz(VALUE vna)
761
+ unsigned int
762
+ nary_element_stride(VALUE v)
755
763
  {
756
- return NUM2SIZET(rb_const_get(CLASS_OF(vna), id_contiguous_stride));
764
+ narray_type_info_t *info;
765
+ narray_t *na;
766
+
767
+ GetNArray(v,na);
768
+ if (na->type == NARRAY_VIEW_T) {
769
+ v = NA_VIEW_DATA(na);
770
+ GetNArray(v,na);
771
+ }
772
+ assert(na->type == NARRAY_DATA_T);
773
+
774
+ info = (narray_type_info_t *)(RTYPEDDATA_TYPE(v)->data);
775
+ return info->element_stride;
757
776
  }
758
777
 
759
778
  size_t
@@ -810,21 +829,6 @@ na_copy_flags(VALUE src, VALUE dst)
810
829
  }
811
830
 
812
831
 
813
- VALUE
814
- na_original_data(VALUE self)
815
- {
816
- narray_t *na;
817
- narray_view_t *nv;
818
-
819
- GetNArray(self,na);
820
- switch(na->type) {
821
- case NARRAY_VIEW_T:
822
- GetNArrayView(self, nv);
823
- return nv->data;
824
- }
825
- return self;
826
- }
827
-
828
832
  // fix name, ex, allow_stride_for_flatten_view
829
833
  VALUE
830
834
  na_check_ladder(VALUE self, int start_dim)
@@ -881,7 +885,7 @@ na_check_contiguous(VALUE self)
881
885
  return Qtrue;
882
886
  }
883
887
  if (na_check_ladder(self,0)==Qtrue) {
884
- elmsz = na_get_elmsz(self);
888
+ elmsz = nary_element_stride(self);
885
889
  if (elmsz == NA_STRIDE_AT(na,NA_NDIM(na)-1)) {
886
890
  return Qtrue;
887
891
  }
@@ -923,7 +927,7 @@ na_make_view(VALUE self)
923
927
  switch(na->type) {
924
928
  case NARRAY_DATA_T:
925
929
  case NARRAY_FILEMAP_T:
926
- stride = na_get_elmsz(self);
930
+ stride = nary_element_stride(self);
927
931
  for (i=nd; i--;) {
928
932
  SDX_SET_STRIDE(na2->stridx[i],stride);
929
933
  stride *= na->shape[i];
@@ -1038,7 +1042,7 @@ nary_reverse(int argc, VALUE *argv, VALUE self)
1038
1042
  VALUE view;
1039
1043
  VALUE reduce;
1040
1044
 
1041
- reduce = na_reduce_dimension(argc, argv, 1, &self);
1045
+ reduce = na_reduce_dimension(argc, argv, 1, &self, 0);
1042
1046
 
1043
1047
  GetNArray(self,na);
1044
1048
  nd = na->ndim;
@@ -1054,7 +1058,7 @@ nary_reverse(int argc, VALUE *argv, VALUE self)
1054
1058
  switch(na->type) {
1055
1059
  case NARRAY_DATA_T:
1056
1060
  case NARRAY_FILEMAP_T:
1057
- stride = na_get_elmsz(self);
1061
+ stride = nary_element_stride(self);
1058
1062
  offset = 0;
1059
1063
  for (i=nd; i--;) {
1060
1064
  if (na_test_reduce(reduce,i)) {
@@ -1116,12 +1120,12 @@ numo_na_upcast(VALUE type1, VALUE type2)
1116
1120
  if (type1==type2) {
1117
1121
  return type1;
1118
1122
  }
1119
- upcast_hash = rb_const_get(type1, rb_intern("UPCAST"));
1123
+ upcast_hash = rb_const_get(type1, id_UPCAST);
1120
1124
  result_type = rb_hash_aref(upcast_hash, type2);
1121
1125
  if (NIL_P(result_type)) {
1122
1126
  if (TYPE(type2)==T_CLASS) {
1123
1127
  if (RTEST(rb_class_inherited_p(type2,cNArray))) {
1124
- upcast_hash = rb_const_get(type2, rb_intern("UPCAST"));
1128
+ upcast_hash = rb_const_get(type2, id_UPCAST);
1125
1129
  result_type = rb_hash_aref(upcast_hash, type1);
1126
1130
  }
1127
1131
  }
@@ -1144,7 +1148,7 @@ nary_coerce(VALUE x, VALUE y)
1144
1148
  VALUE type;
1145
1149
 
1146
1150
  type = numo_na_upcast(CLASS_OF(x), CLASS_OF(y));
1147
- y = rb_funcall(type,rb_intern("cast"),1,y);
1151
+ y = rb_funcall(type,id_cast,1,y);
1148
1152
  return rb_assoc_new(y , x);
1149
1153
  }
1150
1154
 
@@ -1160,7 +1164,7 @@ nary_byte_size(VALUE self)
1160
1164
  narray_t *na;
1161
1165
 
1162
1166
  GetNArray(self,na);
1163
- velmsz = rb_const_get(CLASS_OF(self), rb_intern(ELEMENT_BYTE_SIZE));
1167
+ velmsz = rb_const_get(CLASS_OF(self), id_element_byte_size);
1164
1168
  if (FIXNUM_P(velmsz)) {
1165
1169
  return SIZET2NUM(NUM2SIZET(velmsz) * na->size);
1166
1170
  }
@@ -1174,7 +1178,7 @@ nary_byte_size(VALUE self)
1174
1178
  static VALUE
1175
1179
  nary_s_byte_size(VALUE type)
1176
1180
  {
1177
- return rb_const_get(type, rb_intern(ELEMENT_BYTE_SIZE));
1181
+ return rb_const_get(type, id_element_byte_size);
1178
1182
  }
1179
1183
 
1180
1184
 
@@ -1198,7 +1202,7 @@ nary_s_from_binary(int argc, VALUE *argv, VALUE type)
1198
1202
  narg = rb_scan_args(argc,argv,"11",&vstr,&vshape);
1199
1203
  Check_Type(vstr,T_STRING);
1200
1204
  str_len = RSTRING_LEN(vstr);
1201
- velmsz = rb_const_get(type, rb_intern(ELEMENT_BYTE_SIZE));
1205
+ velmsz = rb_const_get(type, id_element_byte_size);
1202
1206
  if (narg==2) {
1203
1207
  switch(TYPE(vshape)) {
1204
1208
  case T_FIXNUM:
@@ -1244,7 +1248,7 @@ nary_s_from_binary(int argc, VALUE *argv, VALUE type)
1244
1248
  shape[0] = len;
1245
1249
  }
1246
1250
 
1247
- vna = rb_narray_new(type, nd, shape);
1251
+ vna = nary_new(type, nd, shape);
1248
1252
  ptr = na_get_pointer_for_write(vna);
1249
1253
 
1250
1254
  memcpy(ptr, RSTRING_PTR(vstr), byte_size);
@@ -1283,7 +1287,7 @@ nary_store_binary(int argc, VALUE *argv, VALUE self)
1283
1287
 
1284
1288
  GetNArray(self,na);
1285
1289
  size = NA_SIZE(na);
1286
- velmsz = rb_const_get(CLASS_OF(self), rb_intern(ELEMENT_BYTE_SIZE));
1290
+ velmsz = rb_const_get(CLASS_OF(self), id_element_byte_size);
1287
1291
  if (FIXNUM_P(velmsz)) {
1288
1292
  byte_size = size * NUM2SIZET(velmsz);
1289
1293
  } else {
@@ -1317,7 +1321,7 @@ nary_to_binary(VALUE self)
1317
1321
  if (na_check_contiguous(self)==Qtrue) {
1318
1322
  offset = NA_VIEW_OFFSET(na);
1319
1323
  } else {
1320
- self = rb_funcall(self,rb_intern("copy"),0);
1324
+ self = rb_funcall(self,id_dup,0);
1321
1325
  }
1322
1326
  }
1323
1327
  len = NUM2SIZET(nary_byte_size(self));
@@ -1350,7 +1354,7 @@ nary_marshal_dump(VALUE self)
1350
1354
  if (na_check_contiguous(self)==Qtrue) {
1351
1355
  offset = NA_VIEW_OFFSET(na);
1352
1356
  } else {
1353
- self = rb_funcall(self,rb_intern("copy"),0);
1357
+ self = rb_funcall(self,id_dup,0);
1354
1358
  }
1355
1359
  }
1356
1360
  ptr = (VALUE*)na_get_pointer_for_read(self);
@@ -1402,7 +1406,7 @@ nary_marshal_load(VALUE self, VALUE a)
1402
1406
  } else {
1403
1407
  nary_store_binary(1,&v,self);
1404
1408
  if (TEST_BYTE_SWAPPED(self)) {
1405
- rb_funcall(na_inplace(self),rb_intern("to_host"),0);
1409
+ rb_funcall(na_inplace(self),id_to_host,0);
1406
1410
  REVERSE_ENDIAN(self); // correct behavior??
1407
1411
  }
1408
1412
  }
@@ -1420,7 +1424,7 @@ nary_marshal_load(VALUE self, VALUE a)
1420
1424
  static VALUE
1421
1425
  nary_cast_to(VALUE obj, VALUE type)
1422
1426
  {
1423
- return rb_funcall(type, rb_intern("cast"), 1, obj);
1427
+ return rb_funcall(type, id_cast, 1, obj);
1424
1428
  }
1425
1429
 
1426
1430
 
@@ -1437,18 +1441,19 @@ na_test_reduce(VALUE reduce, int dim)
1437
1441
  if (m==0) return 1;
1438
1442
  return (m & (1u<<dim)) ? 1 : 0;
1439
1443
  } else {
1440
- return (rb_funcall(reduce,rb_intern("[]"),1,INT2FIX(dim))==INT2FIX(1)) ?
1444
+ return (rb_funcall(reduce,id_bracket,1,INT2FIX(dim))==INT2FIX(1)) ?
1441
1445
  1 : 0 ;
1442
1446
  }
1443
1447
  }
1444
1448
 
1445
1449
 
1446
1450
  VALUE
1447
- na_reduce_dimension(int argc, VALUE *argv, int naryc, VALUE *naryv)
1451
+ na_reduce_dimension(int argc, VALUE *argv, int naryc, VALUE *naryv, int *propagate_nan)
1448
1452
  {
1449
1453
  int ndim, ndim0;
1450
1454
  int row_major;
1451
1455
  int i, r;
1456
+ long narg;
1452
1457
  size_t j;
1453
1458
  size_t len;
1454
1459
  ssize_t beg, step;
@@ -1456,6 +1461,29 @@ na_reduce_dimension(int argc, VALUE *argv, int naryc, VALUE *naryv)
1456
1461
  narray_t *na;
1457
1462
  size_t m;
1458
1463
  VALUE reduce;
1464
+ VALUE kw_hash = Qnil;
1465
+ ID kw_table[2] = {id_axis,id_nan};
1466
+ VALUE opts[2] = {Qundef,Qundef};
1467
+ VALUE axes;
1468
+
1469
+ narg = rb_scan_args(argc, argv, "*:", &axes, &kw_hash);
1470
+ rb_get_kwargs(kw_hash, kw_table, 0, 2, opts);
1471
+
1472
+ // option: axis
1473
+ if (opts[0] != Qundef && RTEST(opts[0])) {
1474
+ if (narg > 0) {
1475
+ rb_raise(rb_eArgError,"both axis-arguments and axis-keyword are given");
1476
+ }
1477
+ if (TYPE(opts[0]) == T_ARRAY) {
1478
+ axes = opts[0];
1479
+ } else {
1480
+ axes = rb_ary_new3(1,opts[0]);
1481
+ }
1482
+ }
1483
+ // option: ignore_none
1484
+ if (propagate_nan) {
1485
+ *propagate_nan = (opts[1] != Qundef && RTEST(opts[1])) ? 1 : 0;
1486
+ }
1459
1487
 
1460
1488
  if (naryc<1) {
1461
1489
  rb_raise(rb_eRuntimeError,"must be positive: naryc=%d", naryc);
@@ -1494,8 +1522,9 @@ na_reduce_dimension(int argc, VALUE *argv, int naryc, VALUE *naryv)
1494
1522
 
1495
1523
  m = 0;
1496
1524
  reduce = Qnil;
1497
- for (i=0; i<argc; i++) {
1498
- v = argv[i];
1525
+ narg = RARRAY_LEN(axes);
1526
+ for (i=0; i<narg; i++) {
1527
+ v = RARRAY_AREF(axes,i);
1499
1528
  //printf("argv[%d]=",i);rb_p(v);
1500
1529
  if (TYPE(v)==T_FIXNUM) {
1501
1530
  beg = FIX2INT(v);
@@ -1525,44 +1554,15 @@ na_reduce_dimension(int argc, VALUE *argv, int naryc, VALUE *naryv)
1525
1554
  reduce = SIZET2NUM(m);
1526
1555
  }
1527
1556
  }
1528
- v = rb_funcall( INT2FIX(1), rb_intern("<<"), 1, INT2FIX(r) );
1529
- reduce = rb_funcall( reduce, rb_intern("|"), 1, v );
1557
+ v = rb_funcall( INT2FIX(1), id_shift_left, 1, INT2FIX(r) );
1558
+ reduce = rb_funcall( reduce, '|', 1, v );
1530
1559
  }
1531
1560
  }
1561
+ RB_GC_GUARD(axes);
1532
1562
  if (reduce==Qnil) reduce = SIZET2NUM(m);
1533
1563
  return reduce;
1534
1564
  }
1535
1565
 
1536
- //--------------------------------------
1537
-
1538
- /*
1539
- void
1540
- na_index_arg_to_internal_order( int argc, VALUE *argv, VALUE self )
1541
- {
1542
- int i,j;
1543
- VALUE tmp;
1544
-
1545
- if (TEST_COLUMN_MAJOR(self)) {
1546
- for (i=0,j=argc-1; i<argc/2; i++,j--) {
1547
- tmp = argv[i];
1548
- argv[i] = argv[j];
1549
- argv[j] = tmp;
1550
- }
1551
- }
1552
- }
1553
-
1554
- VALUE
1555
- na_index_array_to_internal_order( VALUE args, VALUE self )
1556
- {
1557
- int i,j;
1558
-
1559
- if (TEST_COLUMN_MAJOR(self)) {
1560
- return rb_funcall( args, rb_intern("reverse"), 0 );
1561
- }
1562
- return args;
1563
- }
1564
- */
1565
-
1566
1566
 
1567
1567
  /*
1568
1568
  Return true if column major.
@@ -1661,20 +1661,20 @@ VALUE na_out_of_place_bang( VALUE self )
1661
1661
 
1662
1662
  int na_debug_flag=0;
1663
1663
 
1664
- VALUE na_debug_set(VALUE mod, VALUE flag)
1664
+ static VALUE na_debug_set(VALUE mod, VALUE flag)
1665
1665
  {
1666
1666
  na_debug_flag = RTEST(flag);
1667
1667
  return Qnil;
1668
1668
  }
1669
1669
 
1670
- double na_profile_value=0;
1670
+ static double na_profile_value=0;
1671
1671
 
1672
- VALUE na_profile(VALUE mod)
1672
+ static VALUE na_profile(VALUE mod)
1673
1673
  {
1674
1674
  return rb_float_new(na_profile_value);
1675
1675
  }
1676
1676
 
1677
- VALUE na_profile_set(VALUE mod, VALUE val)
1677
+ static VALUE na_profile_set(VALUE mod, VALUE val)
1678
1678
  {
1679
1679
  na_profile_value = NUM2DBL(val);
1680
1680
  return val;
@@ -1759,7 +1759,7 @@ na_equal(VALUE self, volatile VALUE other)
1759
1759
  GetNArray(self,na1);
1760
1760
 
1761
1761
  if (!rb_obj_is_kind_of(other,cNArray)) {
1762
- other = rb_funcall(CLASS_OF(self), rb_intern("cast"), 1, other);
1762
+ other = rb_funcall(CLASS_OF(self), id_cast, 1, other);
1763
1763
  }
1764
1764
 
1765
1765
  GetNArray(other,na2);
@@ -1771,8 +1771,8 @@ na_equal(VALUE self, volatile VALUE other)
1771
1771
  return Qfalse;
1772
1772
  }
1773
1773
  }
1774
- vbool = rb_funcall(self, rb_intern("eq"), 1, other);
1775
- return (rb_funcall(vbool, rb_intern("count_false"), 0)==INT2FIX(0)) ? Qtrue : Qfalse;
1774
+ vbool = rb_funcall(self, id_eq, 1, other);
1775
+ return (rb_funcall(vbool, id_count_false, 0)==INT2FIX(0)) ? Qtrue : Qfalse;
1776
1776
  }
1777
1777
 
1778
1778
 
@@ -1783,7 +1783,14 @@ Init_narray()
1783
1783
  {
1784
1784
  mNumo = rb_define_module("Numo");
1785
1785
 
1786
- /* define NArray class */
1786
+ /*
1787
+ Document-class: Numo::NArray
1788
+
1789
+ Numo::NArray is the abstract super class for
1790
+ Numerical N-dimensional Array in the Ruby/Numo module.
1791
+ Use Typed Subclasses of NArray (Numo::DFloat, Int32, etc)
1792
+ to create data array instances.
1793
+ */
1787
1794
  cNArray = rb_define_class_under(mNumo, "NArray", rb_cObject);
1788
1795
 
1789
1796
  #ifndef HAVE_RB_CCOMPLEX
@@ -1808,7 +1815,7 @@ Init_narray()
1808
1815
  rb_define_singleton_method(cNArray, "inspect_cols=", na_inspect_cols_set, 1);
1809
1816
 
1810
1817
  /* Ruby allocation framework */
1811
- rb_define_alloc_func(cNArray, na_s_allocate);
1818
+ rb_undef_alloc_func(cNArray);
1812
1819
  rb_define_method(cNArray, "initialize", na_initialize, -2);
1813
1820
  rb_define_method(cNArray, "initialize_copy", na_initialize_copy, 1);
1814
1821
 
@@ -1826,7 +1833,7 @@ Init_narray()
1826
1833
  rb_define_alias (cNArray, "rank","ndim");
1827
1834
  rb_define_method(cNArray, "empty?", na_empty_p, 0);
1828
1835
 
1829
- rb_define_method(cNArray, "debug_info", rb_narray_debug_info, 0);
1836
+ rb_define_method(cNArray, "debug_info", nary_debug_info, 0);
1830
1837
 
1831
1838
  rb_define_method(cNArray, "contiguous?", na_check_contiguous, 0);
1832
1839
 
@@ -1867,7 +1874,22 @@ Init_narray()
1867
1874
  id_allocate = rb_intern("allocate");
1868
1875
  id_contiguous_stride = rb_intern(CONTIGUOUS_STRIDE);
1869
1876
  //id_element_bit_size = rb_intern(ELEMENT_BIT_SIZE);
1870
- //id_element_byte_size = rb_intern(ELEMENT_BYTE_SIZE);
1877
+ id_element_byte_size = rb_intern(ELEMENT_BYTE_SIZE);
1878
+
1879
+ id_fill = rb_intern("fill");
1880
+ id_seq = rb_intern("seq");
1881
+ id_logseq = rb_intern("logseq");
1882
+ id_eye = rb_intern("eye");
1883
+ id_UPCAST = rb_intern("UPCAST");
1884
+ id_cast = rb_intern("cast");
1885
+ id_dup = rb_intern("dup");
1886
+ id_to_host = rb_intern("to_host");
1887
+ id_bracket = rb_intern("[]");
1888
+ id_shift_left = rb_intern("<<");
1889
+ id_eq = rb_intern("eq");
1890
+ id_count_false = rb_intern("count_false");
1891
+ id_axis = rb_intern("axis");
1892
+ id_nan = rb_intern("nan");
1871
1893
 
1872
1894
  sym_reduce = ID2SYM(rb_intern("reduce"));
1873
1895
  sym_option = ID2SYM(rb_intern("option"));
@@ -1878,26 +1900,28 @@ Init_narray()
1878
1900
  Init_nary_index();
1879
1901
 
1880
1902
  Init_nary_data();
1903
+ Init_nary_ndloop();
1904
+
1905
+ Init_numo_dcomplex();
1906
+ Init_numo_dfloat();
1907
+ Init_numo_scomplex();
1908
+ Init_numo_sfloat();
1909
+
1910
+ Init_numo_int64();
1911
+ Init_numo_uint64();
1912
+ Init_numo_int32();
1913
+ Init_numo_uint32();
1914
+ Init_numo_int16();
1915
+ Init_numo_uint16();
1916
+ Init_numo_int8();
1917
+ Init_numo_uint8();
1881
1918
 
1882
- Init_nary_dcomplex();
1883
- Init_nary_dfloat();
1884
- Init_nary_scomplex();
1885
- Init_nary_sfloat();
1886
-
1887
- Init_nary_int64();
1888
- Init_nary_uint64();
1889
- Init_nary_int32();
1890
- Init_nary_uint32();
1891
- Init_nary_int16();
1892
- Init_nary_uint16();
1893
- Init_nary_int8();
1894
- Init_nary_uint8();
1895
- Init_nary_bit();
1919
+ Init_numo_bit();
1920
+ Init_numo_robject();
1896
1921
 
1897
1922
  Init_nary_math();
1898
1923
 
1899
1924
  Init_nary_rand();
1900
1925
  Init_nary_array();
1901
1926
  Init_nary_struct();
1902
- Init_nary_robject();
1903
1927
  }