numo-narray 0.9.0.1-x86-mingw32

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 (124) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +4 -0
  3. data/README.md +47 -0
  4. data/Rakefile +41 -0
  5. data/ext/numo/narray/SFMT-params.h +97 -0
  6. data/ext/numo/narray/SFMT-params19937.h +46 -0
  7. data/ext/numo/narray/SFMT.c +620 -0
  8. data/ext/numo/narray/SFMT.h +157 -0
  9. data/ext/numo/narray/array.c +525 -0
  10. data/ext/numo/narray/data.c +901 -0
  11. data/ext/numo/narray/depend.erb +33 -0
  12. data/ext/numo/narray/extconf.rb +117 -0
  13. data/ext/numo/narray/gen/bit.erb.c +811 -0
  14. data/ext/numo/narray/gen/cogen.rb +18 -0
  15. data/ext/numo/narray/gen/def/dcomplex.rb +32 -0
  16. data/ext/numo/narray/gen/def/dfloat.rb +30 -0
  17. data/ext/numo/narray/gen/def/int16.rb +29 -0
  18. data/ext/numo/narray/gen/def/int32.rb +29 -0
  19. data/ext/numo/narray/gen/def/int64.rb +29 -0
  20. data/ext/numo/narray/gen/def/int8.rb +29 -0
  21. data/ext/numo/narray/gen/def/robject.rb +30 -0
  22. data/ext/numo/narray/gen/def/scomplex.rb +32 -0
  23. data/ext/numo/narray/gen/def/sfloat.rb +30 -0
  24. data/ext/numo/narray/gen/def/uint16.rb +29 -0
  25. data/ext/numo/narray/gen/def/uint32.rb +29 -0
  26. data/ext/numo/narray/gen/def/uint64.rb +29 -0
  27. data/ext/numo/narray/gen/def/uint8.rb +29 -0
  28. data/ext/numo/narray/gen/dtype.erb.c +328 -0
  29. data/ext/numo/narray/gen/tmpl/accum.c +36 -0
  30. data/ext/numo/narray/gen/tmpl/accum_binary.c +75 -0
  31. data/ext/numo/narray/gen/tmpl/accum_index.c +58 -0
  32. data/ext/numo/narray/gen/tmpl/allocate.c +35 -0
  33. data/ext/numo/narray/gen/tmpl/aref.c +51 -0
  34. data/ext/numo/narray/gen/tmpl/aset.c +61 -0
  35. data/ext/numo/narray/gen/tmpl/binary.c +53 -0
  36. data/ext/numo/narray/gen/tmpl/binary2.c +55 -0
  37. data/ext/numo/narray/gen/tmpl/binary_s.c +34 -0
  38. data/ext/numo/narray/gen/tmpl/bit_binary.c +94 -0
  39. data/ext/numo/narray/gen/tmpl/bit_count.c +82 -0
  40. data/ext/numo/narray/gen/tmpl/bit_unary.c +77 -0
  41. data/ext/numo/narray/gen/tmpl/cast.c +37 -0
  42. data/ext/numo/narray/gen/tmpl/cast_array.c +79 -0
  43. data/ext/numo/narray/gen/tmpl/cast_numeric.c +22 -0
  44. data/ext/numo/narray/gen/tmpl/coerce_cast.c +8 -0
  45. data/ext/numo/narray/gen/tmpl/cond_binary.c +51 -0
  46. data/ext/numo/narray/gen/tmpl/cond_unary.c +45 -0
  47. data/ext/numo/narray/gen/tmpl/cum.c +42 -0
  48. data/ext/numo/narray/gen/tmpl/each.c +43 -0
  49. data/ext/numo/narray/gen/tmpl/each_with_index.c +64 -0
  50. data/ext/numo/narray/gen/tmpl/extract.c +23 -0
  51. data/ext/numo/narray/gen/tmpl/eye.c +91 -0
  52. data/ext/numo/narray/gen/tmpl/fill.c +38 -0
  53. data/ext/numo/narray/gen/tmpl/format.c +60 -0
  54. data/ext/numo/narray/gen/tmpl/format_to_a.c +47 -0
  55. data/ext/numo/narray/gen/tmpl/head.c +25 -0
  56. data/ext/numo/narray/gen/tmpl/inspect.c +16 -0
  57. data/ext/numo/narray/gen/tmpl/map_with_index.c +94 -0
  58. data/ext/numo/narray/gen/tmpl/median.c +44 -0
  59. data/ext/numo/narray/gen/tmpl/minmax.c +47 -0
  60. data/ext/numo/narray/gen/tmpl/poly.c +49 -0
  61. data/ext/numo/narray/gen/tmpl/pow.c +74 -0
  62. data/ext/numo/narray/gen/tmpl/powint.c +17 -0
  63. data/ext/numo/narray/gen/tmpl/qsort.c +149 -0
  64. data/ext/numo/narray/gen/tmpl/rand.c +33 -0
  65. data/ext/numo/narray/gen/tmpl/rand_norm.c +46 -0
  66. data/ext/numo/narray/gen/tmpl/robj_allocate.c +32 -0
  67. data/ext/numo/narray/gen/tmpl/seq.c +61 -0
  68. data/ext/numo/narray/gen/tmpl/set2.c +56 -0
  69. data/ext/numo/narray/gen/tmpl/sort.c +36 -0
  70. data/ext/numo/narray/gen/tmpl/sort_index.c +86 -0
  71. data/ext/numo/narray/gen/tmpl/store.c +31 -0
  72. data/ext/numo/narray/gen/tmpl/store_array.c +5 -0
  73. data/ext/numo/narray/gen/tmpl/store_from.c +53 -0
  74. data/ext/numo/narray/gen/tmpl/store_numeric.c +22 -0
  75. data/ext/numo/narray/gen/tmpl/to_a.c +41 -0
  76. data/ext/numo/narray/gen/tmpl/unary.c +58 -0
  77. data/ext/numo/narray/gen/tmpl/unary2.c +58 -0
  78. data/ext/numo/narray/gen/tmpl/unary_s.c +57 -0
  79. data/ext/numo/narray/index.c +822 -0
  80. data/ext/numo/narray/kwarg.c +79 -0
  81. data/ext/numo/narray/math.c +140 -0
  82. data/ext/numo/narray/narray.c +1539 -0
  83. data/ext/numo/narray/ndloop.c +1928 -0
  84. data/ext/numo/narray/numo/compat.h +23 -0
  85. data/ext/numo/narray/numo/intern.h +112 -0
  86. data/ext/numo/narray/numo/narray.h +411 -0
  87. data/ext/numo/narray/numo/ndloop.h +99 -0
  88. data/ext/numo/narray/numo/template.h +140 -0
  89. data/ext/numo/narray/numo/types/bit.h +19 -0
  90. data/ext/numo/narray/numo/types/complex.h +410 -0
  91. data/ext/numo/narray/numo/types/complex_macro.h +205 -0
  92. data/ext/numo/narray/numo/types/dcomplex.h +11 -0
  93. data/ext/numo/narray/numo/types/dfloat.h +12 -0
  94. data/ext/numo/narray/numo/types/float_def.h +34 -0
  95. data/ext/numo/narray/numo/types/float_macro.h +277 -0
  96. data/ext/numo/narray/numo/types/int16.h +12 -0
  97. data/ext/numo/narray/numo/types/int32.h +12 -0
  98. data/ext/numo/narray/numo/types/int64.h +12 -0
  99. data/ext/numo/narray/numo/types/int8.h +12 -0
  100. data/ext/numo/narray/numo/types/int_macro.h +34 -0
  101. data/ext/numo/narray/numo/types/robj_macro.h +218 -0
  102. data/ext/numo/narray/numo/types/robject.h +21 -0
  103. data/ext/numo/narray/numo/types/scomplex.h +11 -0
  104. data/ext/numo/narray/numo/types/sfloat.h +13 -0
  105. data/ext/numo/narray/numo/types/uint16.h +12 -0
  106. data/ext/numo/narray/numo/types/uint32.h +12 -0
  107. data/ext/numo/narray/numo/types/uint64.h +12 -0
  108. data/ext/numo/narray/numo/types/uint8.h +12 -0
  109. data/ext/numo/narray/numo/types/uint_macro.h +31 -0
  110. data/ext/numo/narray/numo/types/xint_macro.h +133 -0
  111. data/ext/numo/narray/rand.c +87 -0
  112. data/ext/numo/narray/step.c +506 -0
  113. data/ext/numo/narray/struct.c +872 -0
  114. data/lib/2.1/numo/narray.so +0 -0
  115. data/lib/2.2/numo/narray.so +0 -0
  116. data/lib/2.3/numo/narray.so +0 -0
  117. data/lib/erbpp/line_number.rb +126 -0
  118. data/lib/erbpp/narray_def.rb +338 -0
  119. data/lib/erbpp.rb +286 -0
  120. data/lib/numo/narray.rb +6 -0
  121. data/numo-narray.gemspec +35 -0
  122. data/spec/bit_spec.rb +93 -0
  123. data/spec/narray_spec.rb +249 -0
  124. metadata +238 -0
@@ -0,0 +1,218 @@
1
+ #define m_zero INT2FIX(0)
2
+ #define m_one INT2FIX(1)
3
+
4
+ #define m_num_to_data(x) (x)
5
+ #define m_data_to_num(x) (x)
6
+
7
+ #define m_from_double(x) rb_float_new(x)
8
+ #define m_from_real(x) rb_float_new(x)
9
+
10
+ #define m_add(x,y) rb_funcall(x,'+',1,y)
11
+ #define m_sub(x,y) rb_funcall(x,'-',1,y)
12
+ #define m_mul(x,y) rb_funcall(x,'*',1,y)
13
+ #define m_div(x,y) rb_funcall(x,'/',1,y)
14
+ #define m_mod(x,y) rb_funcall(x,'%',1,y)
15
+ #define m_divmod(x,y,a,b) \
16
+ {x = rb_funcall(x,id_divmod,1,y); \
17
+ a = RARRAY_PTR(x)[0]; b = RARRAY_PTR(x)[0];}
18
+ #define m_pow(x,y) rb_funcall(x,id_pow,1,y)
19
+ #define m_pow_int(x,y) rb_funcall(x,id_pow,1,y)
20
+
21
+ #define m_abs(x) rb_funcall(x,id_abs,0)
22
+ #define m_minus(x) rb_funcall(x,id_minus,0)
23
+ #define m_inverse(x) rb_funcall(x,id_inverse,0)
24
+ #define m_square(x) rb_funcall(x,'*',1,x)
25
+ #define m_floor(x) rb_funcall(x,id_floor,0)
26
+ #define m_round(x) rb_funcall(x,id_round,0)
27
+ #define m_ceil(x) rb_funcall(x,id_ceil,0)
28
+
29
+ #define m_eq(x,y) RTEST(rb_funcall(x,id_eq,1,y))
30
+ #define m_ne(x,y) RTEST(rb_funcall(x,id_ne,1,y))
31
+ #define m_gt(x,y) RTEST(rb_funcall(x,id_op_gt,1,y))
32
+ #define m_ge(x,y) RTEST(rb_funcall(x,id_op_ge,1,y))
33
+ #define m_lt(x,y) RTEST(rb_funcall(x,id_op_lt,1,y))
34
+ #define m_le(x,y) RTEST(rb_funcall(x,id_op_le,1,y))
35
+
36
+ #define m_bit_and(x,y) rb_funcall(x,id_bit_and,1,y)
37
+ #define m_bit_or(x,y) rb_funcall(x,id_bit_or, 1,y)
38
+ #define m_bit_xor(x,y) rb_funcall(x,id_bit_xor,1,y)
39
+ #define m_bit_not(x) rb_funcall(x,id_bit_not,0)
40
+
41
+ #define m_isnan(x) ((rb_respond_to(x,id_nan_p)) ? RTEST(rb_funcall(x,id_nan_p,0)) : 0)
42
+ #define m_isinf(x) ((rb_respond_to(x,id_infinite_p)) ? RTEST(rb_funcall(x,id_infinite_p,0)) : 0)
43
+ #define m_isfinite(x) ((rb_respond_to(x,id_finite_p)) ? RTEST(rb_funcall(x,id_finite_p,0)) : 0)
44
+
45
+ #define m_mulsum(x,y,z) {z = m_add(m_mul(x,y),z);}
46
+ #define m_mulsum_init INT2FIX(0)
47
+
48
+ #define m_rand to_res53(gen_rand64())
49
+ #define m_rand_norm(a) rand_norm(a)
50
+
51
+ #define m_sprintf(s,x) robj_sprintf(s,x)
52
+
53
+ static inline int robj_sprintf(char *s, VALUE x) {
54
+ VALUE v = rb_funcall(x,rb_intern("to_s"),0);
55
+ return sprintf(s,"%s",StringValuePtr(v));
56
+ }
57
+
58
+ static inline dtype f_sum(size_t n, char *p, ssize_t stride)
59
+ {
60
+ size_t i=n;
61
+ dtype x,y=INT2FIX(0);
62
+
63
+ for (; i--;) {
64
+ x = *(dtype*)p;
65
+ if (!m_isnan(x)) {
66
+ y = m_add(x,y);
67
+ }
68
+ p += stride;
69
+ }
70
+ return y;
71
+ }
72
+
73
+ static inline dtype f_prod(size_t n, char *p, ssize_t stride)
74
+ {
75
+ size_t i=n;
76
+ dtype x,y=INT2FIX(1);
77
+
78
+ for (; i--;) {
79
+ x = *(dtype*)p;
80
+ if (!m_isnan(x)) {
81
+ y = m_mul(x,y);
82
+ }
83
+ p += stride;
84
+ }
85
+ return y;
86
+ }
87
+
88
+ static inline dtype f_mean(size_t n, char *p, ssize_t stride)
89
+ {
90
+ size_t i=n;
91
+ size_t count=0;
92
+ dtype x,y=INT2FIX(0);
93
+
94
+ for (; i--;) {
95
+ x = *(dtype*)p;
96
+ if (!m_isnan(x)) {
97
+ y = m_add(x,y);
98
+ count++;
99
+ }
100
+ p += stride;
101
+ }
102
+ return m_div(y,DBL2NUM(count));
103
+ }
104
+
105
+ static inline dtype f_var(size_t n, char *p, ssize_t stride)
106
+ {
107
+ size_t i=n;
108
+ size_t count=0;
109
+ dtype x,m;
110
+ dtype y=INT2FIX(0);
111
+
112
+ m = f_mean(n,p,stride);
113
+
114
+ for (; i--;) {
115
+ x = *(dtype*)p;
116
+ if (!m_isnan(x)) {
117
+ y = m_add(y,m_square(m_abs(m_sub(x,m))));
118
+ count++;
119
+ }
120
+ p += stride;
121
+ }
122
+ return m_div(y,DBL2NUM(count-1));
123
+ }
124
+
125
+ static inline dtype f_stddev(size_t n, char *p, ssize_t stride)
126
+ {
127
+ VALUE m = rb_const_get(rb_mKernel,rb_intern("Math"));
128
+ return rb_funcall(m,rb_intern("sqrt"),1,f_var(n,p,stride));
129
+ }
130
+
131
+ static inline dtype f_rms(size_t n, char *p, ssize_t stride)
132
+ {
133
+ size_t i=n;
134
+ size_t count=0;
135
+ dtype x;
136
+ dtype y=INT2FIX(0);
137
+ VALUE m;
138
+
139
+ for (; i--;) {
140
+ x = *(dtype*)p;
141
+ if (!m_isnan(x)) {
142
+ y = m_add(y,m_square(m_abs(x)));
143
+ count++;
144
+ }
145
+ p += stride;
146
+ }
147
+ y = m_div(y,DBL2NUM(count));
148
+ m = rb_const_get(rb_mKernel,rb_intern("Math"));
149
+ return rb_funcall(m,rb_intern("sqrt"),1,y);
150
+ }
151
+
152
+ static inline dtype f_min(size_t n, char *p, ssize_t stride)
153
+ {
154
+ dtype x,y;
155
+ size_t i=n;
156
+
157
+ y = *(dtype*)p;
158
+ p += stride;
159
+ i--;
160
+ for (; i--;) {
161
+ x = *(dtype*)p;
162
+ if (!m_isnan(x) && (m_isnan(y) || m_lt(x,y))) {
163
+ y = x;
164
+ }
165
+ p += stride;
166
+ }
167
+ return y;
168
+ }
169
+
170
+ static inline dtype f_max(size_t n, char *p, ssize_t stride)
171
+ {
172
+ dtype x,y;
173
+ size_t i=n;
174
+
175
+ y = *(dtype*)p;
176
+ p += stride;
177
+ i--;
178
+ for (; i--;) {
179
+ x = *(dtype*)p;
180
+ if (!m_isnan(x) && (m_isnan(y) || m_gt(x,y))) {
181
+ y = x;
182
+ }
183
+ p += stride;
184
+ }
185
+ return y;
186
+ }
187
+
188
+ static inline size_t f_min_index(size_t n, char *p, ssize_t stride)
189
+ {
190
+ dtype x, y;
191
+ size_t i, j=0;
192
+
193
+ y = *(dtype*)p;
194
+ for (i=1; i<n; i++) {
195
+ x = *(dtype*)(p+i*stride);
196
+ if (!m_isnan(x) && (m_isnan(y) || m_lt(x,y))) {
197
+ y = x;
198
+ j = i;
199
+ }
200
+ }
201
+ return j;
202
+ }
203
+
204
+ static inline size_t f_max_index(size_t n, char *p, ssize_t stride)
205
+ {
206
+ dtype x, y;
207
+ size_t i, j=0;
208
+
209
+ y = *(dtype*)p;
210
+ for (i=1; i<n; i++) {
211
+ x = *(dtype*)(p+i*stride);
212
+ if (!m_isnan(x) && (m_isnan(y) || m_gt(x,y))) {
213
+ y = x;
214
+ j = i;
215
+ }
216
+ }
217
+ return j;
218
+ }
@@ -0,0 +1,21 @@
1
+ typedef VALUE dtype;
2
+ typedef VALUE rtype;
3
+ #define cT numo_cRObject
4
+ #define cRT cT
5
+ //#define mTM mRObjectMath
6
+
7
+ #include "float_def.h"
8
+ #include "robj_macro.h"
9
+
10
+ #define m_min_init (0.0/0.0)
11
+ #define m_max_init (0.0/0.0)
12
+ #define m_extract(x) (*(VALUE*)x)
13
+ #define m_nearly_eq(x,y) robj_nearly_eq(x,y)
14
+
15
+ inline static int robj_nearly_eq(VALUE vx, VALUE vy)
16
+ {
17
+ double x, y;
18
+ x = NUM2DBL(vx);
19
+ y = NUM2DBL(vy);
20
+ return (fabs(x-y)<=(fabs(x)+fabs(y))*DBL_EPSILON*2);
21
+ }
@@ -0,0 +1,11 @@
1
+ typedef scomplex dtype;
2
+ typedef float rtype;
3
+ #define cT numo_cSComplex
4
+ #define cRT numo_cSFloat
5
+ #define mTM numo_mSComplexMath
6
+
7
+ #include "complex_macro.h"
8
+
9
+ static inline bool c_nearly_eq(dtype x, dtype y) {
10
+ return c_abs(c_sub(x,y)) <= (c_abs(x)+c_abs(y))*FLT_EPSILON*2;
11
+ }
@@ -0,0 +1,13 @@
1
+ typedef float dtype;
2
+ typedef float rtype;
3
+ #define cT numo_cSFloat
4
+ #define cRT cT
5
+ #define mTM numo_mSFloatMath
6
+
7
+ #include "float_macro.h"
8
+
9
+ #define m_min_init numo_sfloat_new_dim0(0.0/0.0)
10
+ #define m_max_init numo_sfloat_new_dim0(0.0/0.0)
11
+
12
+ #define m_extract(x) rb_float_new(*(float*)x)
13
+ #define m_nearly_eq(x,y) (fabs(x-y)<=(fabs(x)+fabs(y))*FLT_EPSILON*2)
@@ -0,0 +1,12 @@
1
+ typedef u_int16_t dtype;
2
+ typedef u_int16_t rtype;
3
+ #define cT numo_cUInt16
4
+ #define cRT cT
5
+
6
+ #define m_num_to_data(x) ((dtype)NUM2UINT(x))
7
+ #define m_data_to_num(x) UINT2NUM((unsigned int)(x))
8
+ #define m_extract(x) UINT2NUM((unsigned int)*(dtype*)(x))
9
+ #define m_sprintf(s,x) sprintf(s,"%u",(unsigned int)(x))
10
+ #define m_rand ((dtype)gen_rand32())
11
+
12
+ #include "uint_macro.h"
@@ -0,0 +1,12 @@
1
+ typedef u_int32_t dtype;
2
+ typedef u_int32_t rtype;
3
+ #define cT numo_cUInt32
4
+ #define cRT cT
5
+
6
+ #define m_num_to_data(x) ((dtype)NUM2UINT32(x))
7
+ #define m_data_to_num(x) UINT322NUM((u_int32_t)(x))
8
+ #define m_extract(x) UINT322NUM((u_int32_t)*(dtype*)(x))
9
+ #define m_sprintf(s,x) sprintf(s,"%"PRIu32,(u_int32_t)(x))
10
+ #define m_rand ((dtype)gen_rand32())
11
+
12
+ #include "uint_macro.h"
@@ -0,0 +1,12 @@
1
+ typedef u_int64_t dtype;
2
+ typedef u_int64_t rtype;
3
+ #define cT numo_cUInt64
4
+ #define cRT cT
5
+
6
+ #define m_num_to_data(x) ((dtype)NUM2UINT64(x))
7
+ #define m_data_to_num(x) UINT642NUM((u_int64_t)(x))
8
+ #define m_extract(x) UINT642NUM((u_int64_t)*(dtype*)(x))
9
+ #define m_sprintf(s,x) sprintf(s,"%"PRIu64,(u_int64_t)(x))
10
+ #define m_rand ((dtype)gen_rand64())
11
+
12
+ #include "uint_macro.h"
@@ -0,0 +1,12 @@
1
+ typedef u_int8_t dtype;
2
+ typedef u_int8_t rtype;
3
+ #define cT numo_cUInt8
4
+ #define cRT cT
5
+
6
+ #define m_num_to_data(x) ((dtype)NUM2UINT(x))
7
+ #define m_data_to_num(x) UINT2NUM((unsigned int)(x))
8
+ #define m_extract(x) UINT2NUM((unsigned int)*(dtype*)(x))
9
+ #define m_sprintf(s,x) sprintf(s,"%u",(unsigned int)(x))
10
+ #define m_rand ((dtype)gen_rand32())
11
+
12
+ #include "uint_macro.h"
@@ -0,0 +1,31 @@
1
+ #include "xint_macro.h"
2
+
3
+ #define m_abs(x) (x)
4
+
5
+ static inline dtype int_inverse(dtype x) {
6
+ switch (x) {
7
+ case 1:
8
+ return 1;
9
+ case 0:
10
+ rb_raise(rb_eZeroDivError, "divided by 0");
11
+ default:
12
+ return 0;
13
+ }
14
+ }
15
+
16
+ static dtype pow_int(dtype x, int p)
17
+ {
18
+ dtype r = m_one;
19
+ switch(p) {
20
+ case 0: return 1;
21
+ case 1: return x;
22
+ case 2: return x*x;
23
+ case 3: return x*x*x;
24
+ }
25
+ while (p) {
26
+ if (p&1) r *= x;
27
+ x *= x;
28
+ p >>= 1;
29
+ }
30
+ return r;
31
+ }
@@ -0,0 +1,133 @@
1
+ #define m_zero 0
2
+ #define m_one 1
3
+
4
+ #define m_from_double(x) (x)
5
+ #define m_from_real(x) (x)
6
+
7
+ #define m_add(x,y) ((x)+(y))
8
+ #define m_sub(x,y) ((x)-(y))
9
+ #define m_mul(x,y) ((x)*(y))
10
+ #define m_div(x,y) ((x)/(y))
11
+ #define m_mod(x,y) ((x)%(y))
12
+ #define m_divmod(x,y,a,b) {a=(x)/(y); b=m_mod(x,y);}
13
+ #define m_pow(x,y) pow_int(x,y)
14
+ #define m_pow_int(x,y) pow_int(x,y)
15
+
16
+ #define m_bit_and(x,y) ((x)&(y))
17
+ #define m_bit_or(x,y) ((x)|(y))
18
+ #define m_bit_xor(x,y) ((x)^(y))
19
+ #define m_bit_not(x) (~(x))
20
+
21
+ #define m_minus(x) (-(x))
22
+ #define m_inverse(x) int_inverse(x)
23
+ #define m_square(x) ((x)*(x))
24
+
25
+ #define m_eq(x,y) ((x)==(y))
26
+ #define m_ne(x,y) ((x)!=(y))
27
+ #define m_gt(x,y) ((x)>(y))
28
+ #define m_ge(x,y) ((x)>=(y))
29
+ #define m_lt(x,y) ((x)<(y))
30
+ #define m_le(x,y) ((x)<=(y))
31
+ #define m_isnan(x) 0
32
+
33
+ #define m_mulsum(x,y,z) {z += x*y;}
34
+ #define m_mulsum_init INT2FIX(0)
35
+
36
+ #define cmp(a,b) \
37
+ ((qsort_cast(a)==qsort_cast(b)) ? 0 : \
38
+ (qsort_cast(a) > qsort_cast(b)) ? 1 : -1)
39
+ #define cmpgt(a,b) \
40
+ (qsort_cast(a) > qsort_cast(b))
41
+
42
+
43
+ static inline dtype f_sum(size_t n, char *p, ssize_t stride)
44
+ {
45
+ dtype x,y=0;
46
+ size_t i=n;
47
+ for (; i--;) {
48
+ x = *(dtype*)p;
49
+ y += x;
50
+ p += stride;
51
+ }
52
+ return y;
53
+ }
54
+
55
+ static inline dtype f_prod(size_t n, char *p, ssize_t stride)
56
+ {
57
+ dtype x,y=1;
58
+ size_t i=n;
59
+ for (; i--;) {
60
+ x = *(dtype*)p;
61
+ y *= x;
62
+ p += stride;
63
+ }
64
+ return y;
65
+ }
66
+
67
+ static inline dtype f_min(size_t n, char *p, ssize_t stride)
68
+ {
69
+ dtype x,y;
70
+ size_t i=n;
71
+
72
+ y = *(dtype*)p;
73
+ p += stride;
74
+ i--;
75
+ for (; i--;) {
76
+ x = *(dtype*)p;
77
+ if (x < y) {
78
+ y = x;
79
+ }
80
+ p += stride;
81
+ }
82
+ return y;
83
+ }
84
+
85
+ static inline dtype f_max(size_t n, char *p, ssize_t stride)
86
+ {
87
+ dtype x,y;
88
+ size_t i=n;
89
+
90
+ y = *(dtype*)p;
91
+ p += stride;
92
+ i--;
93
+ for (; i--;) {
94
+ x = *(dtype*)p;
95
+ if (x > y) {
96
+ y = x;
97
+ }
98
+ p += stride;
99
+ }
100
+ return y;
101
+ }
102
+
103
+ static inline size_t f_min_index(size_t n, char *p, ssize_t stride)
104
+ {
105
+ dtype x, y;
106
+ size_t i, j=0;
107
+
108
+ y = *(dtype*)p;
109
+ for (i=1; i<n; i++) {
110
+ x = *(dtype*)(p+i*stride);
111
+ if (x < y) {
112
+ y = x;
113
+ j = i;
114
+ }
115
+ }
116
+ return j;
117
+ }
118
+
119
+ static inline size_t f_max_index(size_t n, char *p, ssize_t stride)
120
+ {
121
+ dtype x, y;
122
+ size_t i, j=0;
123
+
124
+ y = *(dtype*)p;
125
+ for (i=1; i<n; i++) {
126
+ x = *(dtype*)(p+i*stride);
127
+ if (x > y) {
128
+ y = x;
129
+ j = i;
130
+ }
131
+ }
132
+ return j;
133
+ }
@@ -0,0 +1,87 @@
1
+ #include "ruby.h"
2
+ #include "numo/narray.h"
3
+ #include "SFMT.h"
4
+
5
+ #ifdef HAVE_UNISTD_H
6
+ #include <unistd.h>
7
+ #endif
8
+ #include <time.h>
9
+ #ifdef HAVE_SYS_TIME_H
10
+ #include <sys/time.h>
11
+ #endif
12
+
13
+ int n_bits(u_int64_t a)
14
+ {
15
+ int i, x, /*xu,*/ xl, n=5;
16
+ u_int64_t m;
17
+
18
+ if (a==0) return 0;
19
+ //if (a<0) a=-a;
20
+
21
+ x = 1<<n;
22
+ //xu = 1<<(n+1);
23
+ xl = 0;
24
+ //printf("%3i, [%3i, %3i], %i\n", i, xu, xl, x);
25
+
26
+ for (i=n; i>=0; i--) {
27
+ m = ~((1<<(x-1))-1);
28
+ if (m & a) {
29
+ xl = x;
30
+ x += 1<<(i-1);
31
+ } else {
32
+ //xu = x;
33
+ x -= 1<<(i-1);
34
+ }
35
+ //printf("%3i, [%3i, %3i], %i, 0x%lx, 0x%lx\n", i, xu, xl, x, m, m&a);
36
+ }
37
+ return xl;
38
+ }
39
+
40
+ void rand_norm(double *a)
41
+ {
42
+ double x1, x2, w;
43
+ do {
44
+ x1 = to_res53(gen_rand64());
45
+ x1 = x1*2-1;
46
+ x2 = to_res53(gen_rand64());
47
+ x2 = x2*2-1;
48
+ w = x1 * x1 + x2 * x2;
49
+ } while (w>=1);
50
+ w = sqrt( (-2*log(w)) / w );
51
+ a[0] = x1*w;
52
+ a[1] = x2*w;
53
+ }
54
+
55
+ static u_int64_t
56
+ random_seed()
57
+ {
58
+ static int n = 0;
59
+ struct timeval tv;
60
+
61
+ gettimeofday(&tv, 0);
62
+ return tv.tv_sec ^ tv.tv_usec ^ getpid() ^ n++;
63
+ }
64
+
65
+ static VALUE
66
+ nary_s_srand(int argc, VALUE *argv, VALUE obj)
67
+ {
68
+ VALUE vseed;
69
+ u_int64_t seed;
70
+
71
+ //rb_secure(4);
72
+ if (rb_scan_args(argc, argv, "01", &vseed) == 0) {
73
+ seed = random_seed();
74
+ }
75
+ else {
76
+ seed = NUM2UINT64(vseed);
77
+ }
78
+ init_gen_rand(seed);
79
+
80
+ return Qnil;
81
+ }
82
+
83
+ void
84
+ Init_nary_rand() {
85
+ rb_define_singleton_method(cNArray, "srand", nary_s_srand, -1);
86
+ init_gen_rand(0);
87
+ }