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
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  ndloop.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
 
7
7
  #include <ruby.h>
@@ -70,10 +70,13 @@ typedef struct NA_MD_LOOP {
70
70
  #define NDL_WRITE 2
71
71
  #define NDL_READ_WRITE (NDL_READ|NDL_WRITE)
72
72
 
73
+ static ID id_cast;
74
+ static ID id_extract;
75
+
73
76
  static inline VALUE
74
77
  nary_type_s_cast(VALUE type, VALUE obj)
75
78
  {
76
- return rb_funcall(type,rb_intern("cast"),1,obj);
79
+ return rb_funcall(type,id_cast,1,obj);
77
80
  }
78
81
 
79
82
  static void
@@ -470,24 +473,6 @@ ndloop_release(VALUE vlp)
470
473
  }
471
474
 
472
475
 
473
- /*
474
- static void
475
- ndloop_free(na_md_loop_t* lp)
476
- {
477
- int j;
478
- VALUE v;
479
-
480
- for (j=0; j<lp->narg; j++) {
481
- v = LARG(lp,j).value;
482
- if (IsNArray(v)) {
483
- na_release_lock(v);
484
- }
485
- }
486
- xfree(lp);
487
- }
488
- */
489
-
490
-
491
476
  /*
492
477
  set lp->n[i] (shape of n-d iteration) here
493
478
  */
@@ -529,7 +514,7 @@ ndloop_set_stepidx(na_md_loop_t *lp, int j, VALUE vna, int *dim_map, int rwflag)
529
514
  narray_t *na;
530
515
 
531
516
  LARG(lp,j).value = vna;
532
- LARG(lp,j).elmsz = na_get_elmsz(vna);
517
+ LARG(lp,j).elmsz = nary_element_stride(vna);
533
518
  if (rwflag == NDL_READ) {
534
519
  LARG(lp,j).ptr = na_get_pointer_for_read(vna);
535
520
  } else
@@ -796,7 +781,7 @@ ndloop_set_output_narray(ndfunc_t *nf, na_md_loop_t *lp, int k,
796
781
  }
797
782
  if (!RTEST(v)) {
798
783
  // new object
799
- v = rb_narray_new(type, na_ndim, na_shape);
784
+ v = nary_new(type, na_ndim, na_shape);
800
785
  flag = NDL_WRITE;
801
786
  }
802
787
 
@@ -1226,7 +1211,7 @@ ndloop_extract(VALUE results, ndfunc_t *nf)
1226
1211
  narray_t *na;
1227
1212
 
1228
1213
  if (id_extract==0) {
1229
- id_extract = rb_intern("extract");
1214
+ id_extract = id_extract;
1230
1215
  }
1231
1216
 
1232
1217
  // extract result objects
@@ -1662,7 +1647,7 @@ loop_store_subnarray(ndfunc_t *nf, na_md_loop_t *lp, int i0, size_t *c, VALUE a)
1662
1647
 
1663
1648
  a_type = CLASS_OF(LARG(lp,0).value);
1664
1649
  if (CLASS_OF(a) != a_type) {
1665
- a = rb_funcall(a_type, rb_intern("cast"), 1, a);
1650
+ a = rb_funcall(a_type, id_cast, 1, a);
1666
1651
  }
1667
1652
  GetNArray(a,na);
1668
1653
  if (na->ndim != nd-i0+1) {
@@ -1971,3 +1956,11 @@ na_ndloop(nf, argc, va_alist)
1971
1956
 
1972
1957
  return rb_ensure(ndloop_run, (VALUE)&lp, ndloop_release, (VALUE)&lp);
1973
1958
  }
1959
+
1960
+
1961
+ void
1962
+ Init_nary_ndloop()
1963
+ {
1964
+ id_cast = rb_intern("cast");
1965
+ id_extract = rb_intern("extract");
1966
+ }
@@ -1,65 +1,80 @@
1
1
  /*
2
2
  intern.h
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
  #ifndef INTERN_H
7
7
  #define INTERN_H
8
8
 
9
- VALUE rb_narray_new(VALUE elem, int ndim, size_t *shape);
10
- VALUE rb_narray_view_new(VALUE elem, int ndim, size_t *shape);
11
- VALUE rb_narray_debug_info(VALUE);
9
+ #define rb_narray_new nary_new
10
+ VALUE nary_new(VALUE elem, int ndim, size_t *shape);
11
+ #define rb_narray_view_new nary_view_new
12
+ VALUE nary_view_new(VALUE elem, int ndim, size_t *shape);
13
+ #define rb_narray_debug_info nary_debug_info
14
+ VALUE nary_debug_info(VALUE);
12
15
 
13
- VALUE na_original_data(VALUE self);
14
- VALUE na_make_view(VALUE self);
15
- VALUE na_make_view_struct(VALUE self, VALUE dtype, VALUE offset);
16
- void na_array_to_internal_shape(VALUE self, VALUE ary, size_t *shape);
17
- VALUE na_expand_dims(VALUE self, VALUE vdim);
16
+ #define na_make_view nary_make_view
17
+ VALUE nary_make_view(VALUE self);
18
18
 
19
- VALUE na_reduce_dimension(int argc, VALUE *argv, int naryc, VALUE *naryv);
19
+ #define na_s_allocate nary_s_allocate
20
+ VALUE nary_s_allocate(VALUE klass);
21
+ #define na_s_allocate_view nary_s_allocate_view
22
+ VALUE nary_s_allocate_view(VALUE klass);
23
+ #define na_s_new_like nary_s_new_like
24
+ VALUE nary_s_new_like(VALUE type, VALUE obj);
20
25
 
21
- VALUE na_check_ladder(VALUE self, int start_dim);
22
- VALUE na_check_contiguous(VALUE self);
26
+ void na_alloc_shape(narray_t *na, int ndim);
27
+ void na_array_to_internal_shape(VALUE self, VALUE ary, size_t *shape);
28
+ void na_index_arg_to_internal_order(int argc, VALUE *argv, VALUE self);
23
29
  void na_setup_shape(narray_t *na, int ndim, size_t *shape);
24
30
 
25
- VALUE na_transpose_map(VALUE self, int *map);
26
- VALUE na_flatten_dim(VALUE self, int sd);
27
- VALUE na_flatten_by_reduce(int argc, VALUE *argv, VALUE self);
31
+ #define na_get_elmsz nary_element_stride
32
+ //#define na_element_stride nary_element_stride
33
+ unsigned int nary_element_stride(VALUE nary);
34
+ #define na_dtype_elmsz nary_dtype_element_stride
35
+ size_t nary_dtype_element_stride(VALUE klass);
28
36
 
29
- VALUE na_sort_main(int argc, VALUE *argv, volatile VALUE self, na_iter_func_t iter_func);
30
- VALUE na_median_main(int argc, VALUE *argv, volatile VALUE self, na_iter_func_t iter_func);
37
+ #define na_get_pointer nary_get_pointer
38
+ char *nary_get_pointer(VALUE);
39
+ #define na_get_pointer_for_write nary_get_pointer_for_write
40
+ char *nary_get_pointer_for_write(VALUE);
41
+ #define na_get_pointer_for_read nary_get_pointer_for_read
42
+ char *nary_get_pointer_for_read(VALUE);
43
+ #define na_get_pointer_for_read_write nary_get_pointer_for_read_write
44
+ char *nary_get_pointer_for_read_write(VALUE);
45
+ #define na_get_offset nary_get_offset
46
+ size_t nary_get_offset(VALUE self);
31
47
 
32
- VALUE na_sort_index_main(int argc, VALUE *argv, VALUE self, void (*func_qsort)());
48
+ #define na_copy_flags nary_copy_flags
49
+ void nary_copy_flags(VALUE src, VALUE dst);
33
50
 
34
- char *na_get_pointer(VALUE);
35
- char *na_get_pointer_for_write(VALUE);
36
- char *na_get_pointer_for_read(VALUE);
37
- char *na_get_pointer_for_read_write(VALUE);
51
+ #define na_check_ladder nary_check_ladder
52
+ VALUE nary_check_ladder(VALUE self, int start_dim);
53
+ #define na_check_contiguous nary_check_contiguous
54
+ VALUE nary_check_contiguous(VALUE self);
38
55
 
39
- size_t na_get_offset(VALUE self);
56
+ #define na_flatten_dim nary_flatten_dim
57
+ VALUE nary_flatten_dim(VALUE self, int sd);
40
58
 
41
- VALUE na_s_allocate(VALUE klass);
42
- VALUE na_s_allocate_view(VALUE klass);
59
+ #define na_flatten nary_flatten
60
+ VALUE nary_flatten(VALUE);
43
61
 
44
- void na_alloc_shape(narray_t *na, int ndim);
45
- void na_alloc_index(narray_t *na);
62
+ #define na_copy nary_dup
63
+ VALUE nary_dup(VALUE);
46
64
 
47
- void na_copy_flags(VALUE src, VALUE dst);
65
+ #define na_store nary_store
66
+ VALUE nary_store(VALUE self, VALUE src);
48
67
 
49
- VALUE na_flatten(VALUE);
50
- VALUE na_copy(VALUE);
51
- VALUE na_upcast(VALUE type1, VALUE type2);
68
+ #define na_upcast numo_na_upcast
69
+ VALUE numo_na_upcast(VALUE type1, VALUE type2);
52
70
 
53
- stridx_t *na_get_stride(VALUE v);
71
+ void na_release_lock(VALUE); // currently do nothing
54
72
 
55
- void na_release_lock(VALUE);
56
-
57
- void na_index_arg_to_internal_order(int argc, VALUE *argv, VALUE self);
58
-
59
- //ndfunc_t *ndfunc_alloc(na_iter_func_t func, int has_loop, int narg, int nres, ...);
60
- //ndfunc_t *ndfunc_alloc2(na_iter_func_t func, unsigned int flag, int narg, int nres, VALUE *etypes);
61
- //void ndfunc_free(ndfunc_t* nf);
73
+ // used in reduce methods
74
+ #define na_reduce_dimension nary_reduce_dimension
75
+ VALUE nary_reduce_dimension(int argc, VALUE *argv, int naryc, VALUE *naryv, int *ignore_nan);
62
76
 
77
+ // ndloop
63
78
  VALUE na_ndloop(ndfunc_t *nf, int argc, ...);
64
79
  VALUE na_ndloop2(ndfunc_t *nf, VALUE args);
65
80
  VALUE na_ndloop3(ndfunc_t *nf, void *ptr, int argc, ...);
@@ -68,41 +83,22 @@ VALUE na_ndloop4(ndfunc_t *nf, void *ptr, VALUE args);
68
83
  VALUE na_ndloop_cast_narray_to_rarray(ndfunc_t *nf, VALUE nary, VALUE fmt);
69
84
  VALUE na_ndloop_store_rarray(ndfunc_t *nf, VALUE nary, VALUE rary);
70
85
  VALUE na_ndloop_store_rarray2(ndfunc_t *nf, VALUE nary, VALUE rary, VALUE opt);
71
-
72
86
  VALUE na_ndloop_inspect(VALUE nary, na_text_func_t func, VALUE opt);
73
-
74
87
  VALUE na_ndloop_with_index(ndfunc_t *nf, int argc, ...);
75
88
 
89
+ #define na_info_str nary_info_str
90
+ VALUE nary_info_str(VALUE);
76
91
 
77
- VALUE na_info_str(VALUE);
78
-
79
- size_t na_get_elmsz(VALUE nary);
80
- size_t na_dtype_elmsz(VALUE klass);
81
-
82
- bool na_test_reduce(VALUE reduce, int dim);
83
-
84
- //size_t *na_mdarray_investigate(VALUE ary, int *ndim, VALUE *type);
85
- VALUE na_ary_composition(VALUE ary);
86
-
87
- //void na_copy_bytes(na_loop_t *const itr);
88
-
89
- VALUE na_debug_set(VALUE mod, VALUE flag);
92
+ #define na_test_reduce nary_test_reduce
93
+ bool nary_test_reduce(VALUE reduce, int dim);
90
94
 
91
95
  void nary_step_array_index(VALUE self, size_t ary_size, size_t *plen, ssize_t *pbeg, ssize_t *pstep);
92
-
93
- VALUE na_store(VALUE self, VALUE src);
94
-
95
96
  void nary_step_sequence(VALUE self, size_t *plen, double *pbeg, double *pstep);
96
97
 
97
- VALUE nary_init_accum_aref0(VALUE self, VALUE reduce);
98
-
99
- VALUE nary_mathcast(int argc, VALUE *argv);
100
-
101
-
102
- int
103
- na_get_result_dimension(VALUE self, int argc, VALUE *argv, ssize_t stride, size_t *pos_idx);
104
- VALUE
105
- na_aref_main(int nidx, VALUE *idx, VALUE self, int keep_dim, int nd);
106
-
98
+ // used in aref, aset
99
+ #define na_get_result_dimension nary_get_result_dimension
100
+ int nary_get_result_dimension(VALUE self, int argc, VALUE *argv, ssize_t stride, size_t *pos_idx);
101
+ #define na_aref_main nary_aref_main
102
+ VALUE nary_aref_main(int nidx, VALUE *idx, VALUE self, int keep_dim, int nd);
107
103
 
108
104
  #endif /* ifndef INTERN_H */
@@ -1,13 +1,20 @@
1
1
  /*
2
2
  narray.h
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
  #ifndef NARRAY_H
7
7
  #define NARRAY_H
8
8
 
9
- #define NARRAY_VERSION "0.9.0.4"
10
- #define NARRAY_VERSION_CODE 904
9
+ #if defined(__cplusplus)
10
+ extern "C" {
11
+ #if 0
12
+ } /* satisfy cc-mode */
13
+ #endif
14
+ #endif
15
+
16
+ #define NARRAY_VERSION "0.9.0.5"
17
+ #define NARRAY_VERSION_CODE 905
11
18
 
12
19
  #include <math.h>
13
20
  #include "numo/compat.h"
@@ -126,13 +133,14 @@ extern VALUE nary_eCastError;
126
133
  extern VALUE nary_eShapeError;
127
134
  extern VALUE nary_eOperationError;
128
135
  extern VALUE nary_eDimensionError;
136
+ extern const rb_data_type_t na_data_type;
129
137
 
130
138
  //EXTERN const int na_sizeof[NA_NTYPES+1];
131
139
  #endif
132
140
 
133
141
  #define cNArray numo_cNArray
134
142
  #define mNumo rb_mNumo
135
- #define na_upcast(x,y) numo_na_upcast(x,y)
143
+ //#define na_upcast(x,y) numo_na_upcast(x,y)
136
144
 
137
145
  /* global variables within this module */
138
146
  extern VALUE numo_cBit;
@@ -167,6 +175,7 @@ typedef struct RNArray {
167
175
  unsigned char ndim; // # of dimensions
168
176
  unsigned char type;
169
177
  unsigned char flag[2]; // flags
178
+ unsigned short elmsz; // element size
170
179
  size_t size; // # of total elements
171
180
  size_t *shape; // # of elements for each dimension
172
181
  VALUE reduce;
@@ -210,11 +219,12 @@ typedef struct RNArrayFileMap {
210
219
  } narray_filemap_t;
211
220
 
212
221
 
222
+ // this will be revised in future.
213
223
  typedef struct {
214
- int ndim;
215
- size_t *shape;
216
- VALUE dtype;
217
- } na_compose_t;
224
+ unsigned int element_bits;
225
+ unsigned int element_bytes;
226
+ unsigned int element_stride;
227
+ } narray_type_info_t;
218
228
 
219
229
 
220
230
  static inline narray_t *
@@ -222,7 +232,7 @@ na_get_narray_t(VALUE obj)
222
232
  {
223
233
  narray_t *na;
224
234
 
225
- Check_Type(obj, T_DATA);
235
+ Check_TypedStruct(obj,&na_data_type);
226
236
  na = (narray_t*)DATA_PTR(obj);
227
237
  return na;
228
238
  }
@@ -232,7 +242,7 @@ _na_get_narray_t(VALUE obj, unsigned char na_type)
232
242
  {
233
243
  narray_t *na;
234
244
 
235
- Check_Type(obj, T_DATA);
245
+ Check_TypedStruct(obj,&na_data_type);
236
246
  na = (narray_t*)DATA_PTR(obj);
237
247
  if (na->type != na_type) {
238
248
  rb_bug("unknown type 0x%x (0x%x given)", na_type, na->type);
@@ -244,9 +254,9 @@ _na_get_narray_t(VALUE obj, unsigned char na_type)
244
254
  #define na_get_narray_view_t(obj) (narray_view_t*)_na_get_narray_t(obj,NARRAY_VIEW_T)
245
255
  #define na_get_narray_filemap_t(obj) (narray_filemap_t*)_na_get_narray_t(obj,NARRAY_FILEMAP_T)
246
256
 
247
- #define GetNArray(obj,var) Data_Get_Struct(obj, narray_t, var)
248
- #define GetNArrayView(obj,var) Data_Get_Struct(obj, narray_view_t, var)
249
- #define GetNArrayData(obj,var) Data_Get_Struct(obj, narray_data_t, var)
257
+ #define GetNArray(obj,var) TypedData_Get_Struct(obj, narray_t, &na_data_type, var)
258
+ #define GetNArrayView(obj,var) TypedData_Get_Struct(obj, narray_view_t, &na_data_type, var)
259
+ #define GetNArrayData(obj,var) TypedData_Get_Struct(obj, narray_data_t, &na_data_type, var)
250
260
 
251
261
  #define SDX_IS_STRIDE(x) ((x).stride&0x1)
252
262
  #define SDX_IS_INDEX(x) (!SDX_IS_STRIDE(x))
@@ -387,6 +397,7 @@ _na_get_narray_t(VALUE obj, unsigned char na_type)
387
397
  #define NUM2IMAG(v) NUM2DBL( rb_funcall((v),na_id_imag,0) )
388
398
 
389
399
  #define NA_MAX_DIMENSION (int)(sizeof(VALUE)*8-2)
400
+ #define NA_MAX_ELMSZ 65535
390
401
 
391
402
  typedef unsigned int BIT_DIGIT;
392
403
  //#define BYTE_BIT_DIGIT sizeof(BIT_DIGIT)
@@ -398,7 +409,21 @@ typedef unsigned int BIT_DIGIT;
398
409
  #define ELEMENT_BYTE_SIZE "ELEMENT_BYTE_SIZE"
399
410
  #define CONTIGUOUS_STRIDE "CONTIGUOUS_STRIDE"
400
411
 
412
+
413
+ #ifdef RUBY_INTEGER_UNIFICATION
414
+ #define IS_INTEGER_CLASS(c) ((c)==rb_cInteger)
415
+ #else
416
+ #define IS_INTEGER_CLASS(c) ((c)==rb_cFixnum||(c)==rb_cBignum)
417
+ #endif
418
+
401
419
  #include "numo/ndloop.h"
402
420
  #include "numo/intern.h"
403
421
 
422
+ #if defined(__cplusplus)
423
+ #if 0
424
+ { /* satisfy cc-mode */
425
+ #endif
426
+ } /* extern "C" { */
427
+ #endif
428
+
404
429
  #endif /* ifndef NARRAY_H */
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  ndloop.h
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
  #ifndef NDLOOP_H
7
7
  #define NDLOOP_H
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  template.h
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
  #ifndef TEMPLATE_H
7
7
  #define TEMPLATE_H
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  complex.h
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
 
7
7
 
@@ -39,9 +39,13 @@ static inline dtype NUM2COMP(VALUE v) {
39
39
  #define c_is_zero(x) (REAL(x)==0 && IMAG(x)==0)
40
40
  #define c_eq(x,y) (REAL(x)==REAL(y) && IMAG(x)==IMAG(y))
41
41
  #define c_ne(x,y) (REAL(x)!=REAL(y) || IMAG(x)!=IMAG(y))
42
- #define c_isnan(x) (isnan(REAL(x))||isnan(IMAG(x)))
43
- #define c_isinf(x) (isinf(REAL(x))||isinf(IMAG(x)))
44
- #define c_isfinite(x) (isfinite(REAL(x))&&isfinite(IMAG(x)))
42
+ #define c_isnan(x) (isnan(REAL(x)) || isnan(IMAG(x)))
43
+ #define c_isinf(x) (isinf(REAL(x)) || isinf(IMAG(x)))
44
+ #define c_isposinf(x) ((isinf(REAL(x)) && signbit(REAL(x))==0) || \
45
+ (isinf(IMAG(x)) && signbit(IMAG(x))==0))
46
+ #define c_isneginf(x) ((isinf(REAL(x)) && signbit(REAL(x))) || \
47
+ (isinf(IMAG(x)) && signbit(IMAG(x))))
48
+ #define c_isfinite(x) (isfinite(REAL(x)) && isfinite(IMAG(x)))
45
49
 
46
50
  static inline dtype c_zero() {
47
51
  dtype z;
@@ -85,13 +85,20 @@ static inline dtype c_from_dcomplex(dcomplex x) {
85
85
 
86
86
  #define m_isnan(x) c_isnan(x)
87
87
  #define m_isinf(x) c_isinf(x)
88
+ #define m_isposinf(x) c_isposinf(x)
89
+ #define m_isneginf(x) c_isneginf(x)
88
90
  #define m_isfinite(x) c_isfinite(x)
89
91
 
90
92
  #define m_sum(x,y) {if (!c_isnan(x)) {y=c_add(x,y);}}
91
93
  #define m_sum_init INT2FIX(0)
92
94
 
93
- #define m_mulsum(x,y,z) {z = c_add(c_mul(x,y),z);}
94
95
  #define m_mulsum_init INT2FIX(0)
96
+ #define m_mulsum(x,y,z) {z = c_add(c_mul(x,y),z);}
97
+ #define m_mulsum_nan(x,y,z) {if(!m_isnan(x) && !m_isnan(y)){z = c_add(c_mul(x,y),z);}}
98
+ #define m_cumsum(x,y) {(x)=c_add(x,y);}
99
+ #define m_cumsum_nan(x,y) {if (c_isnan(x)) {(x)=(y);} else if (!c_isnan(y)) {(x)=c_add(x,y);}}
100
+ #define m_cumprod(x,y) {(x)=c_mul(x,y);}
101
+ #define m_cumprod_nan(x,y) {if (c_isnan(x)) {(x)=(y);} else if (!c_isnan(y)) {(x)=c_mul(x,y);}}
95
102
 
96
103
  #define m_sprintf(s,x) sprintf(s,"%g%+gi",REAL(x),IMAG(x))
97
104
 
@@ -116,12 +123,27 @@ static inline dtype c_from_dcomplex(dcomplex x) {
116
123
  #define m_acosh(x) c_acosh(x)
117
124
  #define m_atanh(x) c_atanh(x)
118
125
  #define m_hypot(x,y) c_hypot(x,y)
126
+ #define m_sinc(x) c_div(c_sin(x),x)
119
127
 
120
128
  static inline dtype f_sum(size_t n, char *p, ssize_t stride)
121
129
  {
122
130
  size_t i=n;
123
131
  dtype x,y;
124
132
 
133
+ y = c_zero();
134
+ for (; i--;) {
135
+ x = *(dtype*)p;
136
+ y = c_add(x,y);
137
+ p += stride;
138
+ }
139
+ return y;
140
+ }
141
+
142
+ static inline dtype f_sum_nan(size_t n, char *p, ssize_t stride)
143
+ {
144
+ size_t i=n;
145
+ dtype x,y;
146
+
125
147
  y = c_zero();
126
148
  for (; i--;) {
127
149
  x = *(dtype*)p;
@@ -139,6 +161,32 @@ static inline dtype f_kahan_sum(size_t n, char *p, ssize_t stride)
139
161
  dtype x;
140
162
  volatile dtype y,t,r;
141
163
 
164
+ y = c_zero();
165
+ r = c_zero();
166
+ for (; i--;) {
167
+ x = *(dtype*)p;
168
+ if (fabs(REAL(x)) > fabs(REAL(y))) {
169
+ double z=REAL(x); REAL(x)=REAL(y); REAL(y)=z;
170
+ }
171
+ if (fabs(IMAG(x)) > fabs(IMAG(y))) {
172
+ double z=IMAG(x); IMAG(x)=IMAG(y); IMAG(y)=z;
173
+ }
174
+ r = c_add(x, r);
175
+ t = y;
176
+ y = c_add(r, y);
177
+ t = c_sub(y, t);
178
+ r = c_sub(r, t);
179
+ p += stride;
180
+ }
181
+ return y;
182
+ }
183
+
184
+ static inline dtype f_kahan_sum_nan(size_t n, char *p, ssize_t stride)
185
+ {
186
+ size_t i=n;
187
+ dtype x;
188
+ volatile dtype y,t,r;
189
+
142
190
  y = c_zero();
143
191
  r = c_zero();
144
192
  for (; i--;) {
@@ -166,6 +214,20 @@ static inline dtype f_prod(size_t n, char *p, ssize_t stride)
166
214
  size_t i=n;
167
215
  dtype x,y;
168
216
 
217
+ y = c_one();
218
+ for (; i--;) {
219
+ x = *(dtype*)p;
220
+ y = c_mul(x,y);
221
+ p += stride;
222
+ }
223
+ return y;
224
+ }
225
+
226
+ static inline dtype f_prod_nan(size_t n, char *p, ssize_t stride)
227
+ {
228
+ size_t i=n;
229
+ dtype x,y;
230
+
169
231
  y = c_one();
170
232
  for (; i--;) {
171
233
  x = *(dtype*)p;
@@ -183,6 +245,22 @@ static inline dtype f_mean(size_t n, char *p, ssize_t stride)
183
245
  size_t count=0;
184
246
  dtype x,y;
185
247
 
248
+ y = c_zero();
249
+ for (; i--;) {
250
+ x = *(dtype*)p;
251
+ y = c_add(x,y);
252
+ count++;
253
+ p += stride;
254
+ }
255
+ return c_div_r(y,count);
256
+ }
257
+
258
+ static inline dtype f_mean_nan(size_t n, char *p, ssize_t stride)
259
+ {
260
+ size_t i=n;
261
+ size_t count=0;
262
+ dtype x,y;
263
+
186
264
  y = c_zero();
187
265
  for (; i--;) {
188
266
  x = *(dtype*)p;
@@ -204,6 +282,24 @@ static inline rtype f_var(size_t n, char *p, ssize_t stride)
204
282
 
205
283
  m = f_mean(n,p,stride);
206
284
 
285
+ for (; i--;) {
286
+ x = *(dtype*)p;
287
+ y += c_abs_square(c_sub(x,m));
288
+ count++;
289
+ p += stride;
290
+ }
291
+ return y/(count-1);
292
+ }
293
+
294
+ static inline rtype f_var_nan(size_t n, char *p, ssize_t stride)
295
+ {
296
+ size_t i=n;
297
+ size_t count=0;
298
+ dtype x,m;
299
+ rtype y=0;
300
+
301
+ m = f_mean_nan(n,p,stride);
302
+
207
303
  for (; i--;) {
208
304
  x = *(dtype*)p;
209
305
  if (!c_isnan(x)) {
@@ -220,6 +316,11 @@ static inline rtype f_stddev(size_t n, char *p, ssize_t stride)
220
316
  return r_sqrt(f_var(n,p,stride));
221
317
  }
222
318
 
319
+ static inline rtype f_stddev_nan(size_t n, char *p, ssize_t stride)
320
+ {
321
+ return r_sqrt(f_var_nan(n,p,stride));
322
+ }
323
+
223
324
  static inline rtype f_rms(size_t n, char *p, ssize_t stride)
224
325
  {
225
326
  size_t i=n;
@@ -227,6 +328,22 @@ static inline rtype f_rms(size_t n, char *p, ssize_t stride)
227
328
  dtype x;
228
329
  rtype y=0;
229
330
 
331
+ for (; i--;) {
332
+ x = *(dtype*)p;
333
+ y += c_abs_square(x);
334
+ count++;
335
+ p += stride;
336
+ }
337
+ return r_sqrt(y/count);
338
+ }
339
+
340
+ static inline rtype f_rms_nan(size_t n, char *p, ssize_t stride)
341
+ {
342
+ size_t i=n;
343
+ size_t count=0;
344
+ dtype x;
345
+ rtype y=0;
346
+
230
347
  for (; i--;) {
231
348
  x = *(dtype*)p;
232
349
  if (!c_isnan(x)) {