ruby-dnn 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (127) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +8 -0
  3. data/.travis.yml +5 -0
  4. data/CODE_OF_CONDUCT.md +74 -0
  5. data/Gemfile +6 -0
  6. data/LICENSE.txt +21 -0
  7. data/README.md +42 -0
  8. data/Rakefile +10 -0
  9. data/bin/console +14 -0
  10. data/bin/setup +8 -0
  11. data/lib/dnn.rb +14 -0
  12. data/lib/dnn/core/activations.rb +116 -0
  13. data/lib/dnn/core/error.rb +13 -0
  14. data/lib/dnn/core/initializers.rb +46 -0
  15. data/lib/dnn/core/layers.rb +366 -0
  16. data/lib/dnn/core/model.rb +158 -0
  17. data/lib/dnn/core/optimizers.rb +113 -0
  18. data/lib/dnn/core/util.rb +24 -0
  19. data/lib/dnn/core/version.rb +3 -0
  20. data/lib/dnn/ext/cifar10/Makefile +263 -0
  21. data/lib/dnn/ext/cifar10/cifar10_ext.c +52 -0
  22. data/lib/dnn/ext/cifar10/cifar10_ext.o +0 -0
  23. data/lib/dnn/ext/cifar10/cifar10_ext.so +0 -0
  24. data/lib/dnn/ext/cifar10/extconf.rb +3 -0
  25. data/lib/dnn/ext/cifar10/numo/compat.h +23 -0
  26. data/lib/dnn/ext/cifar10/numo/extconf.h +13 -0
  27. data/lib/dnn/ext/cifar10/numo/intern.h +117 -0
  28. data/lib/dnn/ext/cifar10/numo/narray.h +430 -0
  29. data/lib/dnn/ext/cifar10/numo/ndloop.h +94 -0
  30. data/lib/dnn/ext/cifar10/numo/template.h +149 -0
  31. data/lib/dnn/ext/cifar10/numo/types/bit.h +33 -0
  32. data/lib/dnn/ext/cifar10/numo/types/complex.h +409 -0
  33. data/lib/dnn/ext/cifar10/numo/types/complex_macro.h +377 -0
  34. data/lib/dnn/ext/cifar10/numo/types/dcomplex.h +44 -0
  35. data/lib/dnn/ext/cifar10/numo/types/dfloat.h +42 -0
  36. data/lib/dnn/ext/cifar10/numo/types/float_def.h +34 -0
  37. data/lib/dnn/ext/cifar10/numo/types/float_macro.h +186 -0
  38. data/lib/dnn/ext/cifar10/numo/types/int16.h +24 -0
  39. data/lib/dnn/ext/cifar10/numo/types/int32.h +24 -0
  40. data/lib/dnn/ext/cifar10/numo/types/int64.h +24 -0
  41. data/lib/dnn/ext/cifar10/numo/types/int8.h +24 -0
  42. data/lib/dnn/ext/cifar10/numo/types/int_macro.h +41 -0
  43. data/lib/dnn/ext/cifar10/numo/types/real_accum.h +486 -0
  44. data/lib/dnn/ext/cifar10/numo/types/robj_macro.h +75 -0
  45. data/lib/dnn/ext/cifar10/numo/types/robject.h +27 -0
  46. data/lib/dnn/ext/cifar10/numo/types/scomplex.h +44 -0
  47. data/lib/dnn/ext/cifar10/numo/types/sfloat.h +43 -0
  48. data/lib/dnn/ext/cifar10/numo/types/uint16.h +21 -0
  49. data/lib/dnn/ext/cifar10/numo/types/uint32.h +21 -0
  50. data/lib/dnn/ext/cifar10/numo/types/uint64.h +21 -0
  51. data/lib/dnn/ext/cifar10/numo/types/uint8.h +21 -0
  52. data/lib/dnn/ext/cifar10/numo/types/uint_macro.h +32 -0
  53. data/lib/dnn/ext/cifar10/numo/types/xint_macro.h +189 -0
  54. data/lib/dnn/ext/image_io/Makefile +263 -0
  55. data/lib/dnn/ext/image_io/extconf.rb +3 -0
  56. data/lib/dnn/ext/image_io/image_io_ext.c +89 -0
  57. data/lib/dnn/ext/image_io/image_io_ext.so +0 -0
  58. data/lib/dnn/ext/image_io/numo/compat.h +23 -0
  59. data/lib/dnn/ext/image_io/numo/extconf.h +13 -0
  60. data/lib/dnn/ext/image_io/numo/intern.h +117 -0
  61. data/lib/dnn/ext/image_io/numo/narray.h +430 -0
  62. data/lib/dnn/ext/image_io/numo/ndloop.h +94 -0
  63. data/lib/dnn/ext/image_io/numo/template.h +149 -0
  64. data/lib/dnn/ext/image_io/numo/types/bit.h +33 -0
  65. data/lib/dnn/ext/image_io/numo/types/complex.h +409 -0
  66. data/lib/dnn/ext/image_io/numo/types/complex_macro.h +377 -0
  67. data/lib/dnn/ext/image_io/numo/types/dcomplex.h +44 -0
  68. data/lib/dnn/ext/image_io/numo/types/dfloat.h +42 -0
  69. data/lib/dnn/ext/image_io/numo/types/float_def.h +34 -0
  70. data/lib/dnn/ext/image_io/numo/types/float_macro.h +186 -0
  71. data/lib/dnn/ext/image_io/numo/types/int16.h +24 -0
  72. data/lib/dnn/ext/image_io/numo/types/int32.h +24 -0
  73. data/lib/dnn/ext/image_io/numo/types/int64.h +24 -0
  74. data/lib/dnn/ext/image_io/numo/types/int8.h +24 -0
  75. data/lib/dnn/ext/image_io/numo/types/int_macro.h +41 -0
  76. data/lib/dnn/ext/image_io/numo/types/real_accum.h +486 -0
  77. data/lib/dnn/ext/image_io/numo/types/robj_macro.h +75 -0
  78. data/lib/dnn/ext/image_io/numo/types/robject.h +27 -0
  79. data/lib/dnn/ext/image_io/numo/types/scomplex.h +44 -0
  80. data/lib/dnn/ext/image_io/numo/types/sfloat.h +43 -0
  81. data/lib/dnn/ext/image_io/numo/types/uint16.h +21 -0
  82. data/lib/dnn/ext/image_io/numo/types/uint32.h +21 -0
  83. data/lib/dnn/ext/image_io/numo/types/uint64.h +21 -0
  84. data/lib/dnn/ext/image_io/numo/types/uint8.h +21 -0
  85. data/lib/dnn/ext/image_io/numo/types/uint_macro.h +32 -0
  86. data/lib/dnn/ext/image_io/numo/types/xint_macro.h +189 -0
  87. data/lib/dnn/ext/image_io/stb_image.h +7462 -0
  88. data/lib/dnn/ext/image_io/stb_image_write.h +1568 -0
  89. data/lib/dnn/ext/mnist/Makefile +263 -0
  90. data/lib/dnn/ext/mnist/extconf.rb +3 -0
  91. data/lib/dnn/ext/mnist/mnist_ext.c +49 -0
  92. data/lib/dnn/ext/mnist/mnist_ext.o +0 -0
  93. data/lib/dnn/ext/mnist/mnist_ext.so +0 -0
  94. data/lib/dnn/ext/mnist/numo/compat.h +23 -0
  95. data/lib/dnn/ext/mnist/numo/extconf.h +13 -0
  96. data/lib/dnn/ext/mnist/numo/intern.h +117 -0
  97. data/lib/dnn/ext/mnist/numo/narray.h +430 -0
  98. data/lib/dnn/ext/mnist/numo/ndloop.h +94 -0
  99. data/lib/dnn/ext/mnist/numo/template.h +149 -0
  100. data/lib/dnn/ext/mnist/numo/types/bit.h +33 -0
  101. data/lib/dnn/ext/mnist/numo/types/complex.h +409 -0
  102. data/lib/dnn/ext/mnist/numo/types/complex_macro.h +377 -0
  103. data/lib/dnn/ext/mnist/numo/types/dcomplex.h +44 -0
  104. data/lib/dnn/ext/mnist/numo/types/dfloat.h +42 -0
  105. data/lib/dnn/ext/mnist/numo/types/float_def.h +34 -0
  106. data/lib/dnn/ext/mnist/numo/types/float_macro.h +186 -0
  107. data/lib/dnn/ext/mnist/numo/types/int16.h +24 -0
  108. data/lib/dnn/ext/mnist/numo/types/int32.h +24 -0
  109. data/lib/dnn/ext/mnist/numo/types/int64.h +24 -0
  110. data/lib/dnn/ext/mnist/numo/types/int8.h +24 -0
  111. data/lib/dnn/ext/mnist/numo/types/int_macro.h +41 -0
  112. data/lib/dnn/ext/mnist/numo/types/real_accum.h +486 -0
  113. data/lib/dnn/ext/mnist/numo/types/robj_macro.h +75 -0
  114. data/lib/dnn/ext/mnist/numo/types/robject.h +27 -0
  115. data/lib/dnn/ext/mnist/numo/types/scomplex.h +44 -0
  116. data/lib/dnn/ext/mnist/numo/types/sfloat.h +43 -0
  117. data/lib/dnn/ext/mnist/numo/types/uint16.h +21 -0
  118. data/lib/dnn/ext/mnist/numo/types/uint32.h +21 -0
  119. data/lib/dnn/ext/mnist/numo/types/uint64.h +21 -0
  120. data/lib/dnn/ext/mnist/numo/types/uint8.h +21 -0
  121. data/lib/dnn/ext/mnist/numo/types/uint_macro.h +32 -0
  122. data/lib/dnn/ext/mnist/numo/types/xint_macro.h +189 -0
  123. data/lib/dnn/lib/cifar10.rb +26 -0
  124. data/lib/dnn/lib/image_io.rb +33 -0
  125. data/lib/dnn/lib/mnist.rb +61 -0
  126. data/ruby-dnn.gemspec +41 -0
  127. metadata +225 -0
@@ -0,0 +1,377 @@
1
+ #include "float_def.h"
2
+
3
+ extern double round(double);
4
+ extern double log2(double);
5
+ extern double exp2(double);
6
+ extern double exp10(double);
7
+
8
+ #define r_abs(x) fabs(x)
9
+ #define r_sqrt(x) sqrt(x)
10
+ #define r_exp(x) exp(x)
11
+ #define r_log(x) log(x)
12
+ #define r_sin(x) sin(x)
13
+ #define r_cos(x) cos(x)
14
+ #define r_sinh(x) sinh(x)
15
+ #define r_cosh(x) cosh(x)
16
+ #define r_tanh(x) tanh(x)
17
+ #define r_atan2(y,x) atan2(y,x)
18
+ #define r_hypot(x,y) hypot(x,y)
19
+
20
+ #include "complex.h"
21
+
22
+ static inline dtype c_from_scomplex(scomplex x) {
23
+ dtype z;
24
+ REAL(z) = REAL(x);
25
+ IMAG(z) = IMAG(x);
26
+ return z;
27
+ }
28
+
29
+ static inline dtype c_from_dcomplex(dcomplex x) {
30
+ dtype z;
31
+ REAL(z) = REAL(x);
32
+ IMAG(z) = IMAG(x);
33
+ return z;
34
+ }
35
+
36
+ /* --------------------------- */
37
+
38
+ #define m_zero c_zero()
39
+ #define m_one c_one()
40
+
41
+ #define m_num_to_data(x) NUM2COMP(x)
42
+ #define m_data_to_num(x) COMP2NUM(x)
43
+
44
+ #define m_from_double(x) c_new(x,0)
45
+ #define m_from_real(x) c_new(x,0)
46
+ #define m_from_scomplex(x) c_from_scomplex(x)
47
+ #define m_from_dcomplex(x) c_from_dcomplex(x)
48
+
49
+ #define m_extract(x) COMP2NUM(*(dtype*)x)
50
+
51
+ #define m_real(x) REAL(x)
52
+ #define m_imag(x) IMAG(x)
53
+ #define m_set_real(x,y) c_set_real(x,y)
54
+ #define m_set_imag(x,y) c_set_imag(x,y)
55
+
56
+ #define m_add(x,y) c_add(x,y)
57
+ #define m_sub(x,y) c_sub(x,y)
58
+ #define m_mul(x,y) c_mul(x,y)
59
+ #define m_div(x,y) c_div(x,y)
60
+ #define m_mod(x,y) c_mod(x,y)
61
+ #define m_pow(x,y) c_pow(x,y)
62
+ #define m_pow_int(x,y) c_pow_int(x,y)
63
+
64
+ #define m_abs(x) c_abs(x)
65
+ #define m_minus(x) c_minus(x)
66
+ #define m_reciprocal(x) c_reciprocal(x)
67
+ #define m_square(x) c_square(x)
68
+ #define m_floor(x) c_new(floor(REAL(x)),floor(IMAG(x)))
69
+ #define m_round(x) c_new(round(REAL(x)),round(IMAG(x)))
70
+ #define m_ceil(x) c_new(ceil(REAL(x)),ceil(IMAG(x)))
71
+ #define m_trunc(x) c_new(trunc(REAL(x)),trunc(IMAG(x)))
72
+ #define m_rint(x) c_new(rint(REAL(x)),rint(IMAG(x)))
73
+ #define m_sign(x) c_new( \
74
+ ((REAL(x)==0) ? 0.0:((REAL(x)>0) ? 1.0:((REAL(x)<0) ? -1.0:REAL(x)))), \
75
+ ((IMAG(x)==0) ? 0.0:((IMAG(x)>0) ? 1.0:((IMAG(x)<0) ? -1.0:IMAG(x)))))
76
+ #define m_copysign(x,y) c_new(copysign(REAL(x),REAL(y)),copysign(IMAG(x),IMAG(y)))
77
+
78
+ #define m_im(x) c_im(x)
79
+ #define m_conj(x) c_new(REAL(x),-IMAG(x))
80
+ #define m_arg(x) atan2(IMAG(x),REAL(x))
81
+
82
+ #define m_eq(x,y) c_eq(x,y)
83
+ #define m_ne(x,y) c_ne(x,y)
84
+ #define m_nearly_eq(x,y) c_nearly_eq(x,y)
85
+
86
+ #define m_isnan(x) c_isnan(x)
87
+ #define m_isinf(x) c_isinf(x)
88
+ #define m_isposinf(x) c_isposinf(x)
89
+ #define m_isneginf(x) c_isneginf(x)
90
+ #define m_isfinite(x) c_isfinite(x)
91
+
92
+ #define m_sprintf(s,x) sprintf(s,"%g%+gi",REAL(x),IMAG(x))
93
+
94
+ #define m_sqrt(x) c_sqrt(x)
95
+ #define m_cbrt(x) c_cbrt(x)
96
+ #define m_log(x) c_log(x)
97
+ #define m_log2(x) c_log2(x)
98
+ #define m_log10(x) c_log10(x)
99
+ #define m_exp(x) c_exp(x)
100
+ #define m_exp2(x) c_exp2(x)
101
+ #define m_exp10(x) c_exp10(x)
102
+ #define m_sin(x) c_sin(x)
103
+ #define m_cos(x) c_cos(x)
104
+ #define m_tan(x) c_tan(x)
105
+ #define m_asin(x) c_asin(x)
106
+ #define m_acos(x) c_acos(x)
107
+ #define m_atan(x) c_atan(x)
108
+ #define m_sinh(x) c_sinh(x)
109
+ #define m_cosh(x) c_cosh(x)
110
+ #define m_tanh(x) c_tanh(x)
111
+ #define m_asinh(x) c_asinh(x)
112
+ #define m_acosh(x) c_acosh(x)
113
+ #define m_atanh(x) c_atanh(x)
114
+ #define m_hypot(x,y) c_hypot(x,y)
115
+ #define m_sinc(x) c_div(c_sin(x),x)
116
+
117
+ #define m_sum_init INT2FIX(0)
118
+ #define m_mulsum_init INT2FIX(0)
119
+
120
+ #define not_nan(x) (REAL(x)==REAL(x) && IMAG(x)==IMAG(x))
121
+
122
+ #define m_mulsum(x,y,z) {z = m_add(m_mul(x,y),z);}
123
+ #define m_mulsum_nan(x,y,z) { \
124
+ if(not_nan(x) && not_nan(y)) { \
125
+ z = m_add(m_mul(x,y),z); \
126
+ }}
127
+
128
+ #define m_cumsum(x,y) {(x)=m_add(x,y);}
129
+ #define m_cumsum_nan(x,y) { \
130
+ if (!not_nan(x)) { \
131
+ (x) = (y); \
132
+ } else if (not_nan(y)) { \
133
+ (x) = m_add(x,y); \
134
+ }}
135
+
136
+ #define m_cumprod(x,y) {(x)=m_mul(x,y);}
137
+ #define m_cumprod_nan(x,y) { \
138
+ if (!not_nan(x)) { \
139
+ (x) = (y); \
140
+ } else if (not_nan(y)) { \
141
+ (x) = m_mul(x,y); \
142
+ }}
143
+
144
+ static inline dtype f_sum(size_t n, char *p, ssize_t stride)
145
+ {
146
+ size_t i=n;
147
+ dtype x,y;
148
+
149
+ y = c_zero();
150
+ for (; i--;) {
151
+ x = *(dtype*)p;
152
+ y = c_add(x,y);
153
+ p += stride;
154
+ }
155
+ return y;
156
+ }
157
+
158
+ static inline dtype f_sum_nan(size_t n, char *p, ssize_t stride)
159
+ {
160
+ size_t i=n;
161
+ dtype x,y;
162
+
163
+ y = c_zero();
164
+ for (; i--;) {
165
+ x = *(dtype*)p;
166
+ if (not_nan(x)) {
167
+ y = c_add(x,y);
168
+ }
169
+ p += stride;
170
+ }
171
+ return y;
172
+ }
173
+
174
+ static inline dtype f_kahan_sum(size_t n, char *p, ssize_t stride)
175
+ {
176
+ size_t i=n;
177
+ dtype x;
178
+ volatile dtype y,t,r;
179
+
180
+ y = c_zero();
181
+ r = c_zero();
182
+ for (; i--;) {
183
+ x = *(dtype*)p;
184
+ if (fabs(REAL(x)) > fabs(REAL(y))) {
185
+ double z=REAL(x); REAL(x)=REAL(y); REAL(y)=z;
186
+ }
187
+ if (fabs(IMAG(x)) > fabs(IMAG(y))) {
188
+ double z=IMAG(x); IMAG(x)=IMAG(y); IMAG(y)=z;
189
+ }
190
+ r = c_add(x, r);
191
+ t = y;
192
+ y = c_add(r, y);
193
+ t = c_sub(y, t);
194
+ r = c_sub(r, t);
195
+ p += stride;
196
+ }
197
+ return y;
198
+ }
199
+
200
+ static inline dtype f_kahan_sum_nan(size_t n, char *p, ssize_t stride)
201
+ {
202
+ size_t i=n;
203
+ dtype x;
204
+ volatile dtype y,t,r;
205
+
206
+ y = c_zero();
207
+ r = c_zero();
208
+ for (; i--;) {
209
+ x = *(dtype*)p;
210
+ if (not_nan(x)) {
211
+ if (fabs(REAL(x)) > fabs(REAL(y))) {
212
+ double z=REAL(x); REAL(x)=REAL(y); REAL(y)=z;
213
+ }
214
+ if (fabs(IMAG(x)) > fabs(IMAG(y))) {
215
+ double z=IMAG(x); IMAG(x)=IMAG(y); IMAG(y)=z;
216
+ }
217
+ r = c_add(x, r);
218
+ t = y;
219
+ y = c_add(r, y);
220
+ t = c_sub(y, t);
221
+ r = c_sub(r, t);
222
+ }
223
+ p += stride;
224
+ }
225
+ return y;
226
+ }
227
+
228
+ static inline dtype f_prod(size_t n, char *p, ssize_t stride)
229
+ {
230
+ size_t i=n;
231
+ dtype x,y;
232
+
233
+ y = c_one();
234
+ for (; i--;) {
235
+ x = *(dtype*)p;
236
+ y = c_mul(x,y);
237
+ p += stride;
238
+ }
239
+ return y;
240
+ }
241
+
242
+ static inline dtype f_prod_nan(size_t n, char *p, ssize_t stride)
243
+ {
244
+ size_t i=n;
245
+ dtype x,y;
246
+
247
+ y = c_one();
248
+ for (; i--;) {
249
+ x = *(dtype*)p;
250
+ if (not_nan(x)) {
251
+ y = c_mul(x,y);
252
+ }
253
+ p += stride;
254
+ }
255
+ return y;
256
+ }
257
+
258
+ static inline dtype f_mean(size_t n, char *p, ssize_t stride)
259
+ {
260
+ size_t i=n;
261
+ size_t count=0;
262
+ dtype x,y;
263
+
264
+ y = c_zero();
265
+ for (; i--;) {
266
+ x = *(dtype*)p;
267
+ y = c_add(x,y);
268
+ count++;
269
+ p += stride;
270
+ }
271
+ return c_div_r(y,count);
272
+ }
273
+
274
+ static inline dtype f_mean_nan(size_t n, char *p, ssize_t stride)
275
+ {
276
+ size_t i=n;
277
+ size_t count=0;
278
+ dtype x,y;
279
+
280
+ y = c_zero();
281
+ for (; i--;) {
282
+ x = *(dtype*)p;
283
+ if (not_nan(x)) {
284
+ y = c_add(x,y);
285
+ count++;
286
+ }
287
+ p += stride;
288
+ }
289
+ return c_div_r(y,count);
290
+ }
291
+
292
+ static inline rtype f_var(size_t n, char *p, ssize_t stride)
293
+ {
294
+ size_t i=n;
295
+ size_t count=0;
296
+ dtype x,m;
297
+ rtype y=0;
298
+
299
+ m = f_mean(n,p,stride);
300
+
301
+ for (; i--;) {
302
+ x = *(dtype*)p;
303
+ y += c_abs_square(c_sub(x,m));
304
+ count++;
305
+ p += stride;
306
+ }
307
+ return y/(count-1);
308
+ }
309
+
310
+ static inline rtype f_var_nan(size_t n, char *p, ssize_t stride)
311
+ {
312
+ size_t i=n;
313
+ size_t count=0;
314
+ dtype x,m;
315
+ rtype y=0;
316
+
317
+ m = f_mean_nan(n,p,stride);
318
+
319
+ for (; i--;) {
320
+ x = *(dtype*)p;
321
+ if (not_nan(x)) {
322
+ y += c_abs_square(c_sub(x,m));
323
+ count++;
324
+ }
325
+ p += stride;
326
+ }
327
+ return y/(count-1);
328
+ }
329
+
330
+ static inline rtype f_stddev(size_t n, char *p, ssize_t stride)
331
+ {
332
+ return r_sqrt(f_var(n,p,stride));
333
+ }
334
+
335
+ static inline rtype f_stddev_nan(size_t n, char *p, ssize_t stride)
336
+ {
337
+ return r_sqrt(f_var_nan(n,p,stride));
338
+ }
339
+
340
+ static inline rtype f_rms(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
+
347
+ for (; i--;) {
348
+ x = *(dtype*)p;
349
+ y += c_abs_square(x);
350
+ count++;
351
+ p += stride;
352
+ }
353
+ return r_sqrt(y/count);
354
+ }
355
+
356
+ static inline rtype f_rms_nan(size_t n, char *p, ssize_t stride)
357
+ {
358
+ size_t i=n;
359
+ size_t count=0;
360
+ dtype x;
361
+ rtype y=0;
362
+
363
+ for (; i--;) {
364
+ x = *(dtype*)p;
365
+ if (not_nan(x)) {
366
+ y += c_abs_square(x);
367
+ count++;
368
+ }
369
+ p += stride;
370
+ }
371
+ return r_sqrt(y/count);
372
+ }
373
+
374
+ static inline dtype f_seq(dtype x, dtype y, double c)
375
+ {
376
+ return c_add(x,c_mul_r(y,c));
377
+ }
@@ -0,0 +1,44 @@
1
+ typedef dcomplex dtype;
2
+ typedef double rtype;
3
+ #define cT numo_cDComplex
4
+ #define cRT numo_cDFloat
5
+ #define mTM numo_mDComplexMath
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))*DBL_EPSILON*2;
11
+ }
12
+
13
+ #ifdef SFMT_H
14
+ /* generates a random number on [0,1)-real-interval */
15
+ inline static dtype m_rand(dtype max)
16
+ {
17
+ dtype z;
18
+ REAL(z) = genrand_res53_mix() * REAL(max);
19
+ IMAG(z) = genrand_res53_mix() * IMAG(max);
20
+ return z;
21
+ }
22
+
23
+ /* generates random numbers from the normal distribution
24
+ using Box-Muller Transformation.
25
+ */
26
+ inline static void m_rand_norm(dtype mu, rtype sigma, dtype *a0)
27
+ {
28
+ rtype x1, x2, w;
29
+ do {
30
+ x1 = genrand_res53_mix();
31
+ x1 = x1*2-1;
32
+ x2 = genrand_res53_mix();
33
+ x2 = x2*2-1;
34
+ w = x1 * x1 + x2 * x2;
35
+ } while (w>=1);
36
+ w = sqrt( (-2*log(w)) / w );
37
+ REAL(*a0) = x1*w * sigma + REAL(mu);
38
+ IMAG(*a0) = x2*w * sigma + IMAG(mu);
39
+ }
40
+ #endif
41
+
42
+ #define M_EPSILON rb_float_new(2.2204460492503131e-16)
43
+ #define M_MIN rb_float_new(2.2250738585072014e-308)
44
+ #define M_MAX rb_float_new(1.7976931348623157e+308)
@@ -0,0 +1,42 @@
1
+ typedef double dtype;
2
+ typedef double rtype;
3
+ #define cT numo_cDFloat
4
+ #define cRT numo_cDFloat
5
+ #define mTM numo_mDFloatMath
6
+
7
+ #include "float_macro.h"
8
+
9
+ #ifdef SFMT_H
10
+ /* generates a random number on [0,1)-real-interval */
11
+ inline static dtype m_rand(dtype max)
12
+ {
13
+ return genrand_res53_mix() * max;
14
+ }
15
+
16
+ /* generates random numbers from the normal distribution
17
+ using Box-Muller Transformation.
18
+ */
19
+ inline static void m_rand_norm(dtype mu, dtype sigma, dtype *a0, dtype *a1)
20
+ {
21
+ dtype x1, x2, w;
22
+ do {
23
+ x1 = genrand_res53_mix();
24
+ x1 = x1*2-1;
25
+ x2 = genrand_res53_mix();
26
+ x2 = x2*2-1;
27
+ w = x1 * x1 + x2 * x2;
28
+ } while (w>=1);
29
+ w = sqrt( (-2*log(w)) / w );
30
+ if (a0) {*a0 = x1*w * sigma + mu;}
31
+ if (a1) {*a1 = x2*w * sigma + mu;}
32
+ }
33
+ #endif
34
+
35
+ #define m_min_init numo_dfloat_new_dim0(0.0/0.0)
36
+ #define m_max_init numo_dfloat_new_dim0(0.0/0.0)
37
+ #define m_extract(x) rb_float_new(*(double*)x)
38
+ #define m_nearly_eq(x,y) (fabs(x-y)<=(fabs(x)+fabs(y))*DBL_EPSILON*2)
39
+
40
+ #define M_EPSILON rb_float_new(2.2204460492503131e-16)
41
+ #define M_MIN rb_float_new(2.2250738585072014e-308)
42
+ #define M_MAX rb_float_new(1.7976931348623157e+308)