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
@@ -11,7 +11,7 @@ if is_int && !is_object
11
11
  rand_type = "uint#{rand_bit}_t"
12
12
  %>
13
13
 
14
- #define HWID (sizeof(dtype)>>1)
14
+ #define HWID (sizeof(dtype)*4)
15
15
 
16
16
  static int msb_pos(<%=rand_type%> a)
17
17
  {
@@ -109,7 +109,7 @@ static void
109
109
  Generate uniformly distributed random numbers on self narray.
110
110
  @overload rand([[low],high])
111
111
  @param [Numeric] low lower inclusive boundary of random numbers. (default=0)
112
- @param [Numeric] high upper exclusive boundary of random numbers. (default=1)
112
+ @param [Numeric] high upper exclusive boundary of random numbers. (default=1 or 1+1i for complex types)
113
113
  @return [Numo::<%=class_name%>] self.
114
114
  @example
115
115
  Numo::DFloat.new(6).rand
@@ -131,14 +131,25 @@ static VALUE
131
131
  ndfunc_arg_in_t ain[1] = {{OVERWRITE,0}};
132
132
  ndfunc_t ndf = {<%=c_iter%>, FULL_LOOP, 1,0, ain,0};
133
133
 
134
+ <% if is_int && !is_object %>
135
+ rb_scan_args(argc, args, "11", &v1, &v2);
136
+ if (v2==Qnil) {
137
+ g.low = m_zero;
138
+ g.max = high = m_num_to_data(v1);
139
+ <% else %>
134
140
  rb_scan_args(argc, args, "02", &v1, &v2);
135
141
  if (v2==Qnil) {
136
142
  g.low = m_zero;
137
143
  if (v1==Qnil) {
144
+ <% if is_complex %>
145
+ g.max = high = c_new(1,1);
146
+ <% else %>
138
147
  g.max = high = m_one;
148
+ <% end %>
139
149
  } else {
140
150
  g.max = high = m_num_to_data(v1);
141
151
  }
152
+ <% end %>
142
153
  } else {
143
154
  g.low = m_num_to_data(v1);
144
155
  high = m_num_to_data(v2);
@@ -1,3 +1,8 @@
1
+ typedef struct {
2
+ dtype mu;
3
+ rtype sigma;
4
+ } randn_opt_t;
5
+
1
6
  static void
2
7
  <%=c_iter%>(na_loop_t *const lp)
3
8
  {
@@ -5,42 +10,110 @@ static void
5
10
  char *p1;
6
11
  ssize_t s1;
7
12
  size_t *idx1;
8
- dtype y;
9
- dtype a[2];
13
+ <% if is_complex %>
14
+ dtype *a0;
15
+ <% else %>
16
+ dtype *a0, *a1;
17
+ <% end %>
18
+ dtype mu;
19
+ rtype sigma;
20
+ randn_opt_t *g;
10
21
 
11
22
  INIT_COUNTER(lp, i);
12
23
  INIT_PTR_IDX(lp, 0, p1, s1, idx1);
24
+ g = (randn_opt_t*)(lp->opt_ptr);
25
+ mu = g->mu;
26
+ sigma = g->sigma;
27
+
13
28
  if (idx1) {
29
+ <% if is_complex %>
30
+ for (; i--;) {
31
+ a0 = (dtype*)(p1+*idx1);
32
+ m_rand_norm(mu,sigma,a0);
33
+ idx1 += 1;
34
+ }
35
+ <% else %>
14
36
  for (; i>1; i-=2) {
15
- m_rand_norm(a);
16
- *(dtype*)(p1+*idx1) = a[0];
17
- *(dtype*)(p1+*(idx1+1)) = a[1];
37
+ a0 = (dtype*)(p1+*idx1);
38
+ a1 = (dtype*)(p1+*(idx1+1));
39
+ m_rand_norm(mu,sigma,a0,a1);
18
40
  idx1 += 2;
19
41
  }
20
42
  if (i>0) {
21
- m_rand_norm(a);
22
- *(dtype*)(p1+*idx1) = a[0];
43
+ a0 = (dtype*)(p1+*idx1);
44
+ m_rand_norm(mu,sigma,a0,0);
23
45
  }
46
+ <% end %>
24
47
  } else {
48
+ <% if is_complex %>
49
+ for (; i--;) {
50
+ a0 = (dtype*)(p1);
51
+ m_rand_norm(mu,sigma,a0);
52
+ p1 += s1;
53
+ }
54
+ <% else %>
25
55
  for (; i>1; i-=2) {
26
- m_rand_norm(a);
27
- *(dtype*)(p1) = a[0];
28
- *(dtype*)(p1+s1) = a[1];
56
+ a0 = (dtype*)(p1);
57
+ a1 = (dtype*)(p1+s1);
58
+ m_rand_norm(mu,sigma,a0,a1);
29
59
  p1 += s1*2;
30
60
  }
31
61
  if (i>0) {
32
- m_rand_norm(a);
33
- *(dtype*)(p1) = a[0];
62
+ a0 = (dtype*)(p1);
63
+ m_rand_norm(mu,sigma,a0,0);
34
64
  }
65
+ <% end %>
35
66
  }
36
67
  }
37
68
 
69
+ /*
70
+ Generates random numbers from the normal distribution on self narray
71
+ using Box-Muller Transformation.
72
+ @overload rand_norm([mu,[sigma]])
73
+ @param [Numeric] mu mean of normal distribution. (default=0)
74
+ @param [Numeric] sigma standard deviation of normal distribution. (default=1)
75
+ @return [Numo::<%=class_name%>] self.
76
+ @example
77
+ Numo::DFloat.new(5,5).rand_norm
78
+ => Numo::DFloat#shape=[5,5]
79
+ [[-0.581255, -0.168354, 0.586895, -0.595142, -0.802802],
80
+ [-0.326106, 0.282922, 1.68427, 0.918499, -0.0485384],
81
+ [-0.464453, -0.992194, 0.413794, -0.60717, -0.699695],
82
+ [-1.64168, 0.48676, -0.875871, -1.43275, 0.812172],
83
+ [-0.209975, -0.103612, -0.878617, -1.42495, 1.0968]]
84
+ Numo::DFloat.new(5,5).rand_norm(10,0.1)
85
+ => Numo::DFloat#shape=[5,5]
86
+ [[9.9019, 9.90339, 10.0826, 9.98384, 9.72861],
87
+ [9.81507, 10.0272, 9.91445, 10.0568, 9.88923],
88
+ [10.0234, 9.97874, 9.96011, 9.9006, 9.99964],
89
+ [10.0186, 9.94598, 9.92236, 9.99811, 9.97003],
90
+ [9.79266, 9.95044, 9.95212, 9.93692, 10.2027]]
91
+ Numo::DComplex.new(3,3).rand_norm(5+5i)
92
+ => Numo::DComplex#shape=[3,3]
93
+ [[5.84303+4.40052i, 4.00984+6.08982i, 5.10979+5.13215i],
94
+ [4.26477+3.99655i, 4.90052+5.00763i, 4.46607+2.3444i],
95
+ [4.5528+7.11003i, 5.62117+6.69094i, 5.05443+5.35133i]]
96
+ */
38
97
  static VALUE
39
- <%=c_func%>(VALUE self)
98
+ <%=c_func%>(int argc, VALUE *args, VALUE self)
40
99
  {
100
+ int n;
101
+ randn_opt_t g;
102
+ VALUE v1=Qnil, v2=Qnil;
41
103
  ndfunc_arg_in_t ain[1] = {{OVERWRITE,0}};
42
- ndfunc_t ndf = { <%=c_iter%>, FULL_LOOP, 1, 0, ain, 0 };
104
+ ndfunc_t ndf = {<%=c_iter%>, FULL_LOOP, 1,0, ain,0};
43
105
 
44
- na_ndloop(&ndf, 1, self);
45
- return self
106
+ n = rb_scan_args(argc, args, "02", &v1, &v2);
107
+ if (n == 0) {
108
+ g.mu = m_zero;
109
+ } else {
110
+ g.mu = m_num_to_data(v1);
111
+ }
112
+ if (n == 2) {
113
+ g.sigma = NUM2DBL(v2);
114
+ } else {
115
+ g.sigma = 1;
116
+ }
117
+ na_ndloop3(&ndf, &g, 1, self);
118
+ return self;
46
119
  }
@@ -24,8 +24,12 @@ static VALUE
24
24
  }
25
25
  }
26
26
 
27
+ <% if is_object %>
28
+ numo_robject_store_numeric(self,obj);
29
+ <% else %>
27
30
  rb_raise(nary_eCastError, "unknown conversion from %s to %s",
28
31
  rb_class2name(CLASS_OF(obj)),
29
32
  rb_class2name(CLASS_OF(self)));
33
+ <% end %>
30
34
  return self;
31
35
  }
@@ -1,5 +1,102 @@
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 *p1;
8
+ size_t s1, *idx1;
9
+ VALUE x;
10
+ double y;
11
+ dtype z;
12
+ size_t len, c;
13
+ double beg, step;
14
+
15
+ INIT_COUNTER(lp, n);
16
+ INIT_PTR_IDX(lp, 0, p1, s1, idx1);
17
+ v1 = lp->args[1].value;
18
+ i = 0;
19
+
20
+ if (lp->args[1].ptr) {
21
+ if (v1 == Qtrue) {
22
+ iter_<%=tp%>_store_<%=tp%>(lp);
23
+ i = lp->args[1].shape[0];
24
+ if (idx1) {
25
+ idx1 += i;
26
+ } else {
27
+ p1 += s1 * i;
28
+ }
29
+ }
30
+ goto loop_end;
31
+ }
32
+
33
+ ptr = &v1;
34
+
35
+ switch(TYPE(v1)) {
36
+ case T_ARRAY:
37
+ n1 = RARRAY_LEN(v1);
38
+ ptr = RARRAY_PTR(v1);
39
+ break;
40
+ case T_NIL:
41
+ n1 = 0;
42
+ break;
43
+ default:
44
+ n1 = 1;
45
+ }
46
+
47
+ if (idx1) {
48
+ for (i=i1=0; i1<n1 && i<n; i++,i1++) {
49
+ x = ptr[i1];
50
+ if (rb_obj_is_kind_of(x, rb_cRange) || rb_obj_is_kind_of(x, na_cStep)) {
51
+ nary_step_sequence(x,&len,&beg,&step);
52
+ for (c=0; c<len && i<n; c++,i++) {
53
+ y = beg + step * c;
54
+ z = m_from_double(y);
55
+ SET_DATA_INDEX(p1, idx1, dtype, z);
56
+ }
57
+ }
58
+ else if (TYPE(x) != T_ARRAY) {
59
+ z = m_num_to_data(x);
60
+ SET_DATA_INDEX(p1, idx1, dtype, z);
61
+ }
62
+ }
63
+ } else {
64
+ for (i=i1=0; i1<n1 && i<n; i++,i1++) {
65
+ x = ptr[i1];
66
+ if (rb_obj_is_kind_of(x, rb_cRange) || rb_obj_is_kind_of(x, na_cStep)) {
67
+ nary_step_sequence(x,&len,&beg,&step);
68
+ for (c=0; c<len && i<n; c++,i++) {
69
+ y = beg + step * c;
70
+ z = m_from_double(y);
71
+ SET_DATA_STRIDE(p1, s1, dtype, z);
72
+ }
73
+ }
74
+ else if (TYPE(x) != T_ARRAY) {
75
+ z = m_num_to_data(x);
76
+ SET_DATA_STRIDE(p1, s1, dtype, z);
77
+ }
78
+ }
79
+ }
80
+
81
+ loop_end:
82
+ z = m_zero;
83
+ if (idx1) {
84
+ for (; i<n; i++) {
85
+ SET_DATA_INDEX(p1, idx1, dtype, z);
86
+ }
87
+ } else {
88
+ for (; i<n; i++) {
89
+ SET_DATA_STRIDE(p1, s1, dtype, z);
90
+ }
91
+ }
92
+ }
93
+
1
94
  static VALUE
2
- <%=c_func%>(VALUE self, VALUE obj)
95
+ <%=c_func%>(VALUE self, VALUE rary)
3
96
  {
4
- return <%=find_tmpl("store").c_func%>(self,<%=find_tmpl("cast_array").c_func%>(obj));
97
+ ndfunc_arg_in_t ain[2] = {{OVERWRITE,0},{rb_cArray,0}};
98
+ ndfunc_t ndf = {<%=c_iter%>, FULL_LOOP, 2, 0, ain, 0};
99
+
100
+ na_ndloop_store_rarray(&ndf, self, rary);
101
+ return self;
5
102
  }
@@ -10,7 +10,7 @@ static VALUE
10
10
  case NARRAY_DATA_T:
11
11
  ptr = NA_DATA_PTR(na);
12
12
  if (na->size > 0 && ptr == NULL) {
13
- ptr = xmalloc(((na->size-1)/sizeof(BIT_DIGIT)+1)*sizeof(BIT_DIGIT)/8);
13
+ ptr = xmalloc(((na->size-1)/8/sizeof(BIT_DIGIT)+1)*sizeof(BIT_DIGIT));
14
14
  NA_DATA_PTR(na) = ptr;
15
15
  }
16
16
  break;
@@ -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,13 +39,14 @@
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
  dtype x;
44
46
 
45
- pos = na_get_scalar_position(self, argc, argv, 1);
46
- if (pos == -1) {
47
- return na_aref_main(argc, argv, self, 0);
47
+ nd = na_get_result_dimension(self, argc, argv, 1, &pos);
48
+ if (nd) {
49
+ return na_aref_main(argc, argv, self, 0, nd);
48
50
  } else {
49
51
  ptr = na_get_pointer_for_read(self);
50
52
  LOAD_BIT(ptr,pos,x);
@@ -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
  dtype x;
@@ -48,9 +50,9 @@ static VALUE
48
50
  if (argc==0) {
49
51
  <%=c_func.sub(/_aset/,"_store")%>(self, argv[argc]);
50
52
  } else {
51
- pos = na_get_scalar_position(self, argc, argv, sizeof(dtype));
52
- if (pos == -1) {
53
- a = na_aref_main(argc, argv, self, 0);
53
+ nd = na_get_result_dimension(self, argc, argv, 1, &pos);
54
+ if (nd) {
55
+ a = na_aref_main(argc, argv, self, 0, nd);
54
56
  <%=c_func.sub(/_aset/,"_store")%>(a, argv[argc]);
55
57
  } else {
56
58
  ptr = na_get_pointer_for_read_write(self);
@@ -1,80 +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
- BIT_DIGIT *a2;
8
- size_t p2;
9
- size_t s2, *idx2;
10
- VALUE x;
11
- double y;
12
- BIT_DIGIT z;
13
- size_t len, c;
14
- double beg, step;
15
-
16
- v1 = lp->args[0].value;
17
- ptr = &v1;
18
- INIT_COUNTER(lp, n);
19
- INIT_PTR_BIT_IDX(lp, 1, a2, p2, s2, idx2);
20
-
21
- switch(TYPE(v1)) {
22
- case T_ARRAY:
23
- n1 = RARRAY_LEN(v1);
24
- ptr = RARRAY_PTR(v1);
25
- break;
26
- case T_NIL:
27
- n1 = 0;
28
- break;
29
- default:
30
- n1 = 1;
31
- }
32
- if (idx2) {
33
- <% ["STORE_BIT(a2, p2+*idx2, z); idx2++;",
34
- "STORE_BIT(a2, p2, z); p2+=s2;"].each_with_index do |x,i| %>
35
- for (i=i1=0; i1<n1 && i<n; i++,i1++) {
36
- x = ptr[i1];
37
- if (rb_obj_is_kind_of(x, rb_cRange) || rb_obj_is_kind_of(x, na_cStep)) {
38
- nary_step_sequence(x,&len,&beg,&step);
39
- for (c=0; c<len && i<n; c++,i++) {
40
- y = beg + step * c;
41
- z = m_from_double(y);
42
- <%= x %>
43
- }
44
- }
45
- if (TYPE(x) != T_ARRAY) {
46
- if (x == Qnil) x = INT2FIX(0);
47
- z = m_num_to_data(x);
48
- <%= x %>
49
- }
50
- }
51
- z = m_zero;
52
- for (; i<n; i++) {
53
- <%= x %>
54
- }
55
- <% if i<1 %>
56
- } else {
57
- <% end
58
- end %>
59
- }
60
- }
61
-
62
1
  static VALUE
63
2
  <%=c_func%>(VALUE rary)
64
3
  {
65
- volatile VALUE vnc, nary;
4
+ VALUE vnc, nary;
66
5
  narray_t *na;
67
6
  na_compose_t *nc;
68
- ndfunc_arg_in_t ain[2] = {{rb_cArray,0},{Qnil,0}};
69
- ndfunc_t ndf = {<%=c_iter%>, FULL_LOOP, 2,0, ain,0};
70
7
 
71
8
  vnc = na_ary_composition(rary);
72
9
  Data_Get_Struct(vnc, na_compose_t, nc);
73
10
  nary = rb_narray_new(cT, nc->ndim, nc->shape);
11
+ RB_GC_GUARD(vnc);
74
12
  GetNArray(nary,na);
75
13
  if (na->size > 0) {
76
14
  <%=find_tmpl("allocate").c_func%>(nary);
77
- na_ndloop_cast_rarray_to_narray(&ndf, rary, nary);
15
+ <%=find_tmpl("store_array").c_func%>(nary, rary);
78
16
  }
79
17
  return nary;
80
18
  }