numo-narray 0.9.0.3 → 0.9.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +12 -6
  3. data/Rakefile +2 -10
  4. data/ext/numo/narray/array.c +1 -6
  5. data/ext/numo/narray/data.c +3 -9
  6. data/ext/numo/narray/depend.erb +1 -1
  7. data/ext/numo/narray/extconf.rb +0 -1
  8. data/ext/numo/narray/gen/def/bit.rb +2 -0
  9. data/ext/numo/narray/gen/def/dcomplex.rb +2 -0
  10. data/ext/numo/narray/gen/def/dfloat.rb +2 -0
  11. data/ext/numo/narray/gen/def/int16.rb +2 -0
  12. data/ext/numo/narray/gen/def/int32.rb +2 -0
  13. data/ext/numo/narray/gen/def/int64.rb +2 -0
  14. data/ext/numo/narray/gen/def/int8.rb +2 -0
  15. data/ext/numo/narray/gen/def/robject.rb +2 -0
  16. data/ext/numo/narray/gen/def/scomplex.rb +2 -0
  17. data/ext/numo/narray/gen/def/sfloat.rb +2 -0
  18. data/ext/numo/narray/gen/def/uint16.rb +2 -0
  19. data/ext/numo/narray/gen/def/uint32.rb +2 -0
  20. data/ext/numo/narray/gen/def/uint64.rb +2 -0
  21. data/ext/numo/narray/gen/def/uint8.rb +2 -0
  22. data/ext/numo/narray/gen/dtype.erb.c +7 -7
  23. data/ext/numo/narray/gen/spec.rb +24 -2
  24. data/ext/numo/narray/gen/tmpl/accum_binary.c +6 -0
  25. data/ext/numo/narray/gen/tmpl/aref.c +6 -4
  26. data/ext/numo/narray/gen/tmpl/aset.c +6 -4
  27. data/ext/numo/narray/gen/tmpl/binary.c +4 -0
  28. data/ext/numo/narray/gen/tmpl/binary2.c +4 -0
  29. data/ext/numo/narray/gen/tmpl/bincount.c +180 -0
  30. data/ext/numo/narray/gen/tmpl/cast.c +4 -0
  31. data/ext/numo/narray/gen/tmpl/cast_array.c +3 -64
  32. data/ext/numo/narray/gen/tmpl/cond_binary.c +4 -0
  33. data/ext/numo/narray/gen/tmpl/inspect.c +4 -0
  34. data/ext/numo/narray/gen/tmpl/pow.c +4 -0
  35. data/ext/numo/narray/gen/tmpl/qsort.c +1 -7
  36. data/ext/numo/narray/gen/tmpl/rand.c +13 -2
  37. data/ext/numo/narray/gen/tmpl/rand_norm.c +89 -16
  38. data/ext/numo/narray/gen/tmpl/store.c +4 -0
  39. data/ext/numo/narray/gen/tmpl/store_array.c +99 -2
  40. data/ext/numo/narray/gen/tmpl_bit/allocate.c +1 -1
  41. data/ext/numo/narray/gen/tmpl_bit/aref.c +6 -4
  42. data/ext/numo/narray/gen/tmpl_bit/aset.c +6 -4
  43. data/ext/numo/narray/gen/tmpl_bit/cast_array.c +3 -65
  44. data/ext/numo/narray/gen/tmpl_bit/mask.c +16 -1
  45. data/ext/numo/narray/gen/tmpl_bit/store_array.c +101 -2
  46. data/ext/numo/narray/gen/tmpl_bit/where.c +7 -23
  47. data/ext/numo/narray/gen/tmpl_bit/where2.c +58 -4
  48. data/ext/numo/narray/index.c +168 -166
  49. data/ext/numo/narray/kwarg.c +1 -6
  50. data/ext/numo/narray/math.c +8 -12
  51. data/ext/numo/narray/narray.c +231 -71
  52. data/ext/numo/narray/ndloop.c +86 -26
  53. data/ext/numo/narray/numo/intern.h +6 -10
  54. data/ext/numo/narray/numo/narray.h +83 -54
  55. data/ext/numo/narray/numo/ndloop.h +0 -5
  56. data/ext/numo/narray/numo/template.h +0 -5
  57. data/ext/numo/narray/numo/types/complex.h +1 -6
  58. data/ext/numo/narray/numo/types/complex_macro.h +30 -3
  59. data/ext/numo/narray/numo/types/dcomplex.h +18 -0
  60. data/ext/numo/narray/numo/types/dfloat.h +18 -0
  61. data/ext/numo/narray/numo/types/float_macro.h +25 -2
  62. data/ext/numo/narray/numo/types/robj_macro.h +2 -4
  63. data/ext/numo/narray/numo/types/scomplex.h +18 -0
  64. data/ext/numo/narray/numo/types/sfloat.h +18 -0
  65. data/ext/numo/narray/rand.c +0 -15
  66. data/ext/numo/narray/step.c +0 -5
  67. data/ext/numo/narray/struct.c +7 -12
  68. data/lib/erbpp/line_number.rb +4 -4
  69. data/lib/erbpp/narray_def.rb +16 -7
  70. data/lib/numo/narray.rb +2 -0
  71. data/lib/numo/narray/extra.rb +465 -0
  72. data/numo-narray.gemspec +2 -2
  73. data/spec/narray_spec.rb +4 -3
  74. metadata +13 -7
  75. data/ext/numo/narray/gen/tmpl/head.c +0 -25
@@ -61,10 +61,15 @@ static VALUE
61
61
  static VALUE
62
62
  <%=c_func%>(int argc, VALUE *argv, VALUE self)
63
63
  {
64
+ <% if !is_object %>
64
65
  VALUE klass, v;
66
+ <% end %>
65
67
  if (argc < 1) {
66
68
  rb_raise(rb_eArgError,"wrong number of arguments (%d for >=1)",argc);
67
69
  }
70
+ <% if is_object %>
71
+ return <%=c_func%>_self(argc, argv, self);
72
+ <% else %>
68
73
  klass = na_upcast(CLASS_OF(self),CLASS_OF(argv[0]));
69
74
  if (klass==cT) {
70
75
  return <%=c_func%>_self(argc, argv, self);
@@ -72,4 +77,5 @@ static VALUE
72
77
  v = rb_funcall(klass, id_cast, 1, self);
73
78
  return rb_funcall2(v, rb_intern("<%=method%>"), argc, argv);
74
79
  }
80
+ <% end %>
75
81
  }
@@ -9,6 +9,7 @@
9
9
  +dim0+, +dim1+, ... includes other than Numeric index, e.g., Range
10
10
  or Array or true.
11
11
 
12
+ @example
12
13
  a = Numo::DFloat.new(4,5).seq
13
14
  => Numo::DFloat#shape=[4,5]
14
15
  [[0, 1, 2, 3, 4],
@@ -38,12 +39,13 @@
38
39
  static VALUE
39
40
  <%=c_func%>(int argc, VALUE *argv, VALUE self)
40
41
  {
41
- ssize_t pos;
42
+ int nd;
43
+ size_t pos;
42
44
  char *ptr;
43
45
 
44
- pos = na_get_scalar_position(self, argc, argv, sizeof(dtype));
45
- if (pos == -1) {
46
- return na_aref_main(argc, argv, self, 0);
46
+ nd = na_get_result_dimension(self, argc, argv, sizeof(dtype), &pos);
47
+ if (nd) {
48
+ return na_aref_main(argc, argv, self, 0, nd);
47
49
  } else {
48
50
  ptr = na_get_pointer_for_read(self) + pos;
49
51
  return m_extract(ptr);
@@ -8,6 +8,7 @@
8
8
  --- Replace element(s) at +dim0+, +dim1+, ... (index/range/array/true
9
9
  for each dimention). Broadcasting mechanism is applied.
10
10
 
11
+ @example
11
12
  a = Numo::DFloat.new(3,4).seq
12
13
  => Numo::DFloat#shape=[3,4]
13
14
  [[0, 1, 2, 3],
@@ -39,7 +40,8 @@
39
40
  static VALUE
40
41
  <%=c_func%>(int argc, VALUE *argv, VALUE self)
41
42
  {
42
- ssize_t pos;
43
+ int nd;
44
+ size_t pos;
43
45
  char *ptr;
44
46
  VALUE a;
45
47
 
@@ -47,9 +49,9 @@ static VALUE
47
49
  if (argc==0) {
48
50
  <%=c_func.sub(/_aset/,"_store")%>(self, argv[argc]);
49
51
  } else {
50
- pos = na_get_scalar_position(self, argc, argv, sizeof(dtype));
51
- if (pos == -1) {
52
- a = na_aref_main(argc, argv, self, 0);
52
+ nd = na_get_result_dimension(self, argc, argv, sizeof(dtype), &pos);
53
+ if (nd) {
54
+ a = na_aref_main(argc, argv, self, 0, nd);
53
55
  <%=c_func.sub(/_aset/,"_store")%>(a, argv[argc]);
54
56
  } else {
55
57
  ptr = na_get_pointer_for_read_write(self) + pos;
@@ -42,6 +42,9 @@ static VALUE
42
42
  static VALUE
43
43
  <%=c_func%>(VALUE self, VALUE other)
44
44
  {
45
+ <% if is_object %>
46
+ return <%=c_func%>_self(self, other);
47
+ <% else %>
45
48
  VALUE klass, v;
46
49
  klass = na_upcast(CLASS_OF(self),CLASS_OF(other));
47
50
  if (klass==cT) {
@@ -50,4 +53,5 @@ static VALUE
50
53
  v = rb_funcall(klass, id_cast, 1, self);
51
54
  return rb_funcall(v, <%=id_op%>, 1, other);
52
55
  }
56
+ <% end %>
53
57
  }
@@ -44,6 +44,9 @@ static VALUE
44
44
  static VALUE
45
45
  <%=c_func%>(VALUE self, VALUE other)
46
46
  {
47
+ <% if is_object %>
48
+ return <%=c_func%>_self(self, other);
49
+ <% else %>
47
50
  VALUE klass, v;
48
51
  klass = na_upcast(CLASS_OF(self),CLASS_OF(other));
49
52
  if (klass==cT) {
@@ -52,4 +55,5 @@ static VALUE
52
55
  v = rb_funcall(klass, id_cast, 1, self);
53
56
  return rb_funcall(v, <%=id_op%>, 1, other);
54
57
  }
58
+ <% end %>
55
59
  }
@@ -0,0 +1,180 @@
1
+ // ------- Integer count without weights -------
2
+ <%
3
+ [32,64].each do |bits|
4
+ cnt_cT = "numo_cUInt#{bits}"
5
+ cnt_type = "u_int#{bits}_t"
6
+ %>
7
+ static void
8
+ <%=c_iter%>_<%=bits%>(na_loop_t *const lp)
9
+ {
10
+ size_t i, x, n;
11
+ char *p1, *p2;
12
+ ssize_t s1, s2;
13
+ size_t *idx1;
14
+
15
+ INIT_PTR_IDX(lp, 0, p1, s1, idx1);
16
+ INIT_PTR(lp, 1, p2, s2);
17
+ i = lp->args[0].shape[0];
18
+ n = lp->args[1].shape[0];
19
+
20
+ // initialize
21
+ for (x=0; x < n; x++) {
22
+ *(<%=cnt_type%>*)(p2 + s2*x) = 0;
23
+ }
24
+
25
+ if (idx1) {
26
+ for (; i--;) {
27
+ GET_DATA_INDEX(p1,idx1,dtype,x);
28
+ (*(<%=cnt_type%>*)(p2 + s2*x))++;
29
+ }
30
+ } else {
31
+ for (; i--;) {
32
+ GET_DATA_STRIDE(p1,s1,dtype,x);
33
+ (*(<%=cnt_type%>*)(p2 + s2*x))++;
34
+ }
35
+ }
36
+ }
37
+
38
+ static VALUE
39
+ <%=c_func%>_<%=bits%>(VALUE self, size_t length)
40
+ {
41
+ size_t shape_out[1] = {length};
42
+ ndfunc_arg_in_t ain[1] = {{cT,1}};
43
+ ndfunc_arg_out_t aout[1] = {{<%=cnt_cT%>,1,shape_out}};
44
+ ndfunc_t ndf = {<%=c_iter%>_<%=bits%>, NO_LOOP|NDF_STRIDE_LOOP|NDF_INDEX_LOOP,
45
+ 1, 1, ain, aout};
46
+
47
+ return na_ndloop(&ndf, 1, self);
48
+ }
49
+ <% end %>
50
+ // ------- end of Integer count without weights -------
51
+
52
+ // ------- Float count with weights -------
53
+ <%
54
+ [["SF","float"],
55
+ ["DF","double"]].each do |fn,cnt_type|
56
+ cnt_cT = "numo_c#{fn}loat"
57
+ fn = fn.downcase
58
+ %>
59
+ static void
60
+ <%=c_iter%>_<%=fn%>(na_loop_t *const lp)
61
+ {
62
+ <%=cnt_type%> w;
63
+ size_t i, x, n, m;
64
+ char *p1, *p2, *p3;
65
+ ssize_t s1, s2, s3;
66
+
67
+ INIT_PTR(lp, 0, p1, s1);
68
+ INIT_PTR(lp, 1, p2, s2);
69
+ INIT_PTR(lp, 2, p3, s3);
70
+ i = lp->args[0].shape[0];
71
+ m = lp->args[1].shape[0];
72
+ n = lp->args[2].shape[0];
73
+
74
+ if (i != m) {
75
+ rb_raise(nary_eShapeError,
76
+ "size mismatch along last axis between self and weight");
77
+ }
78
+
79
+ // initialize
80
+ for (x=0; x < n; x++) {
81
+ *(<%=cnt_type%>*)(p3 + s3*x) = 0;
82
+ }
83
+ for (; i--;) {
84
+ GET_DATA_STRIDE(p1,s1,dtype,x);
85
+ GET_DATA_STRIDE(p2,s2,<%=cnt_type%>,w);
86
+ (*(<%=cnt_type%>*)(p3 + s3*x)) += w;
87
+ }
88
+ }
89
+
90
+ static VALUE
91
+ <%=c_func%>_<%=fn%>(VALUE self, VALUE weight, size_t length)
92
+ {
93
+ size_t shape_out[1] = {length};
94
+ ndfunc_arg_in_t ain[2] = {{cT,1},{<%=cnt_cT%>,1}};
95
+ ndfunc_arg_out_t aout[1] = {{<%=cnt_cT%>,1,shape_out}};
96
+ ndfunc_t ndf = {<%=c_iter%>_<%=fn%>, NO_LOOP|NDF_STRIDE_LOOP,
97
+ 2, 1, ain, aout};
98
+
99
+ return na_ndloop(&ndf, 2, self, weight);
100
+ }
101
+ <% end %>
102
+ // ------- end of Float count with weights -------
103
+
104
+ /*
105
+ Count the number of occurrences of each non-negative integer value.
106
+ Only Integer-types has this method.
107
+
108
+ @overload <%=method%>([weight], minlength:nil)
109
+ @param [SFloat or DFloat or Array] weight (optional) Array of
110
+ float values. Its size along last axis should be same as that of self.
111
+ @param [Integer] minlength (keyword, optional) Minimum size along
112
+ last axis for the output array.
113
+ @return [UInt32 or UInt64 or SFloat or DFloat]
114
+ Returns Float NArray if weight array is supplied,
115
+ otherwise returns UInt32 or UInt64 depending on the size along last axis.
116
+ @example
117
+ Numo::Int32[0..4].bincount
118
+ => Numo::UInt32#shape=[5]
119
+ [1, 1, 1, 1, 1]
120
+
121
+ Numo::Int32[0, 1, 1, 3, 2, 1, 7].bincount
122
+ => Numo::UInt32#shape=[8]
123
+ [1, 3, 1, 1, 0, 0, 0, 1]
124
+
125
+ x = Numo::Int32[0, 1, 1, 3, 2, 1, 7, 23]
126
+ x.bincount.size == x.max+1
127
+ => true
128
+
129
+ w = Numo::DFloat[0.3, 0.5, 0.2, 0.7, 1.0, -0.6]
130
+ x = Numo::Int32[0, 1, 1, 2, 2, 2]
131
+ x.bincount(w)
132
+ => Numo::DFloat#shape=[3]
133
+ [0.3, 0.7, 1.1]
134
+
135
+ */
136
+ static VALUE
137
+ <%=c_func%>(int argc, VALUE *argv, VALUE self)
138
+ {
139
+ VALUE weight=Qnil, kw=Qnil;
140
+ VALUE opts[1] = {Qundef};
141
+ VALUE v, wclass;
142
+ ID table[1] = {id_minlength};
143
+ size_t length, minlength;
144
+
145
+ rb_scan_args(argc, argv, "01:", &weight, &kw);
146
+ rb_get_kwargs(kw, table, 0, 1, opts);
147
+
148
+ <% if is_unsigned %>
149
+ v = numo_<%=type_name%>_max(0,0,self);
150
+ <% else %>
151
+ v = numo_<%=type_name%>_minmax(0,0,self);
152
+ if (m_num_to_data(RARRAY_AREF(v,0)) < 0) {
153
+ rb_raise(rb_eArgError,"array items must be non-netagive");
154
+ }
155
+ v = RARRAY_AREF(v,1);
156
+ <% end %>
157
+ length = NUM2SIZET(v) + 1;
158
+
159
+ if (opts[0] != Qundef) {
160
+ minlength = NUM2SIZET(opts[0]);
161
+ if (minlength > length) {
162
+ length = minlength;
163
+ }
164
+ }
165
+
166
+ if (NIL_P(weight)) {
167
+ if (length > 4294967295ul) {
168
+ return <%=c_func%>_64(self, length);
169
+ } else {
170
+ return <%=c_func%>_32(self, length);
171
+ }
172
+ } else {
173
+ wclass = CLASS_OF(weight);
174
+ if (wclass == numo_cSFloat) {
175
+ return <%=c_func%>_sf(self, weight, length);
176
+ } else {
177
+ return <%=c_func%>_df(self, weight, length);
178
+ }
179
+ }
180
+ }
@@ -32,6 +32,10 @@ static VALUE
32
32
  }
33
33
  return v;
34
34
  }
35
+ <% if is_object %>
36
+ return numo_robject_new_dim0(obj);
37
+ <% else %>
35
38
  rb_raise(nary_eCastError,"cannot cast to %s",rb_class2name(type));
36
39
  return Qnil;
40
+ <% end %>
37
41
  }
@@ -1,79 +1,18 @@
1
- static void
2
- <%=c_iter%>(na_loop_t *const lp)
3
- {
4
- size_t i, n;
5
- size_t i1, n1;
6
- VALUE v1, *ptr;
7
- char *p2;
8
- size_t s2, *idx2;
9
- VALUE x;
10
- double y;
11
- dtype z;
12
- size_t len, c;
13
- double beg, step;
14
-
15
- v1 = lp->args[0].value;
16
- ptr = &v1;
17
- INIT_COUNTER(lp, n);
18
- INIT_PTR_IDX(lp, 1, p2, s2, idx2);
19
-
20
- switch(TYPE(v1)) {
21
- case T_ARRAY:
22
- n1 = RARRAY_LEN(v1);
23
- ptr = RARRAY_PTR(v1);
24
- break;
25
- case T_NIL:
26
- n1 = 0;
27
- break;
28
- default:
29
- n1 = 1;
30
- }
31
- if (idx2) {
32
- <% ["SET_DATA_INDEX(p2, idx2, dtype, z)",
33
- "SET_DATA_STRIDE(p2, s2, dtype, z)"].each_with_index do |x,i| %>
34
- for (i=i1=0; i1<n1 && i<n; i++,i1++) {
35
- x = ptr[i1];
36
- if (rb_obj_is_kind_of(x, rb_cRange) || rb_obj_is_kind_of(x, na_cStep)) {
37
- nary_step_sequence(x,&len,&beg,&step);
38
- for (c=0; c<len && i<n; c++,i++) {
39
- y = beg + step * c;
40
- z = m_from_double(y);
41
- <%= x %>
42
- }
43
- }
44
- else if (TYPE(x) != T_ARRAY) {
45
- if (x == Qnil) x = INT2FIX(0);
46
- z = m_num_to_data(x);
47
- <%= x %>
48
- }
49
- }
50
- z = m_zero;
51
- for (; i<n; i++) {
52
- <%= x %>
53
- }
54
- <% if i<1 %>
55
- } else {
56
- <% end
57
- end %>
58
- }
59
- }
60
-
61
1
  static VALUE
62
2
  <%=c_func%>(VALUE rary)
63
3
  {
64
- volatile VALUE vnc, nary;
4
+ VALUE vnc, nary;
65
5
  narray_t *na;
66
6
  na_compose_t *nc;
67
- ndfunc_arg_in_t ain[2] = {{rb_cArray,0},{Qnil,0}};
68
- ndfunc_t ndf = { <%=c_iter%>, FULL_LOOP, 2, 0, ain, 0 };
69
7
 
70
8
  vnc = na_ary_composition(rary);
71
9
  Data_Get_Struct(vnc, na_compose_t, nc);
72
10
  nary = rb_narray_new(cT, nc->ndim, nc->shape);
11
+ RB_GC_GUARD(vnc);
73
12
  GetNArray(nary,na);
74
13
  if (na->size > 0) {
75
14
  <%=find_tmpl("allocate").c_func%>(nary);
76
- na_ndloop_cast_rarray_to_narray(&ndf, rary, nary);
15
+ <%=find_tmpl("store_array").c_func%>(nary,rary);
77
16
  }
78
17
  return nary;
79
18
  }
@@ -40,6 +40,9 @@ static VALUE
40
40
  static VALUE
41
41
  <%=c_func%>(VALUE self, VALUE other)
42
42
  {
43
+ <% if is_object %>
44
+ return <%=c_func%>_self(self, other);
45
+ <% else %>
43
46
  VALUE klass, v;
44
47
  klass = na_upcast(CLASS_OF(self),CLASS_OF(other));
45
48
  if (klass==cT) {
@@ -48,4 +51,5 @@ static VALUE
48
51
  v = rb_funcall(klass, id_cast, 1, self);
49
52
  return rb_funcall(v, id_<%=method%>, 1, other);
50
53
  }
54
+ <% end %>
51
55
  }
@@ -1,7 +1,11 @@
1
1
  static VALUE
2
2
  <%=c_iter%>(char *ptr, size_t pos, VALUE fmt)
3
3
  {
4
+ <% if is_object %>
5
+ return rb_inspect(*(VALUE*)(ptr+pos));
6
+ <% else %>
4
7
  return format_<%=tp%>(fmt, (dtype*)(ptr+pos));
8
+ <% end %>
5
9
  }
6
10
 
7
11
  /*
@@ -63,6 +63,9 @@ static VALUE
63
63
  static VALUE
64
64
  <%=c_func%>(VALUE self, VALUE other)
65
65
  {
66
+ <% if is_object %>
67
+ return <%=c_func%>_self(self,other);
68
+ <% else %>
66
69
  VALUE klass, v;
67
70
  klass = na_upcast(CLASS_OF(self),CLASS_OF(other));
68
71
  if (klass==cT) {
@@ -71,4 +74,5 @@ static VALUE
71
74
  v = rb_funcall(klass, id_cast, 1, self);
72
75
  return rb_funcall(v, id_pow, 1, other);
73
76
  }
77
+ <% end %>
74
78
  }
@@ -1,12 +1,7 @@
1
1
  /*
2
2
  qsort.c
3
3
  Numerical Array Extension for Ruby
4
- (C) Copyright 2007-2011 by Masahiro TANAKA
5
-
6
- This program is free software.
7
- You can distribute/modify this program
8
- under the same terms as Ruby itself.
9
- NO WARRANTY.
4
+ (C) Copyright 2007-2016 by Masahiro TANAKA
10
5
  */
11
6
 
12
7
  /*
@@ -146,4 +141,3 @@ void
146
141
  goto loop;
147
142
  }
148
143
  }
149
-