ruby-dnn 0.1.0

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 (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,34 @@
1
+ #ifndef DBL_EPSILON
2
+ #define DBL_EPSILON 2.2204460492503131e-16
3
+ #endif
4
+ #ifndef FLT_EPSILON
5
+ #define FLT_EPSILON 1.1920928955078125e-07
6
+ #endif
7
+ #ifndef DBL_MAX
8
+ #define DBL_MAX 1.7976931348623157e+308
9
+ #endif
10
+ #ifndef DBL_MAX
11
+ #define DBL_MAX 1.7976931348623157e+308
12
+ #endif
13
+ #ifndef FLT_MIN
14
+ #define FLT_MIN 1.1754943508222875e-38
15
+ #endif
16
+ #ifndef FLT_MAX
17
+ #define FLT_MAX 3.4028234663852886e+38
18
+ #endif
19
+
20
+ #ifndef M_PI_2
21
+ #define M_PI_2 1.57079632679489661923 /* pi/2 */
22
+ #endif
23
+ #ifndef M_LOG2E
24
+ #define M_LOG2E 1.4426950408889634074 /* log_2 e */
25
+ #endif
26
+ #ifndef M_LOG10E
27
+ #define M_LOG10E 0.43429448190325182765 /* log_10 e */
28
+ #endif
29
+ #ifndef M_LN2
30
+ #define M_LN2 0.69314718055994530942 /* log_e 2 */
31
+ #endif
32
+ #ifndef M_LN10
33
+ #define M_LN10 2.30258509299404568402 /* log_e 10 */
34
+ #endif
@@ -0,0 +1,186 @@
1
+ #include "float_def.h"
2
+
3
+ extern double round(double);
4
+ extern double log2(double);
5
+ extern double exp2(double);
6
+ #ifdef HAVE_EXP10
7
+ extern double exp10(double);
8
+ #else
9
+ extern double pow(double, double);
10
+ #endif
11
+
12
+ #define m_zero 0.0
13
+ #define m_one 1.0
14
+
15
+ #define m_num_to_data(x) NUM2DBL(x)
16
+ #define m_data_to_num(x) rb_float_new(x)
17
+
18
+ #define m_from_double(x) (x)
19
+ #define m_from_real(x) (x)
20
+
21
+ #define m_add(x,y) ((x)+(y))
22
+ #define m_sub(x,y) ((x)-(y))
23
+ #define m_mul(x,y) ((x)*(y))
24
+ #define m_div(x,y) ((x)/(y))
25
+ #define m_div_check(x,y) ((y)==0)
26
+ #define m_mod(x,y) fmod(x,y)
27
+ #define m_divmod(x,y,a,b) {a=(x)/(y); b=m_mod(x,y);}
28
+ #define m_pow(x,y) pow(x,y)
29
+ #define m_pow_int(x,y) pow_int(x,y)
30
+
31
+ #define m_abs(x) fabs(x)
32
+ #define m_minus(x) (-(x))
33
+ #define m_reciprocal(x) (1/(x))
34
+ #define m_square(x) ((x)*(x))
35
+ #define m_floor(x) floor(x)
36
+ #define m_round(x) round(x)
37
+ #define m_ceil(x) ceil(x)
38
+ #define m_trunc(x) trunc(x)
39
+ #define m_rint(x) rint(x)
40
+ #define m_sign(x) (((x)==0) ? 0.0:(((x)>0) ? 1.0:(((x)<0) ? -1.0:(x))))
41
+ #define m_copysign(x,y) copysign(x,y)
42
+ #define m_signbit(x) signbit(x)
43
+ #define m_modf(x,y,z) {double d; y=modf(x,&d); z=d;}
44
+
45
+ #define m_eq(x,y) ((x)==(y))
46
+ #define m_ne(x,y) ((x)!=(y))
47
+ #define m_gt(x,y) ((x)>(y))
48
+ #define m_ge(x,y) ((x)>=(y))
49
+ #define m_lt(x,y) ((x)<(y))
50
+ #define m_le(x,y) ((x)<=(y))
51
+
52
+ #define m_isnan(x) isnan(x)
53
+ #define m_isinf(x) isinf(x)
54
+ #define m_isposinf(x) (isinf(x) && signbit(x)==0)
55
+ #define m_isneginf(x) (isinf(x) && signbit(x))
56
+ #define m_isfinite(x) isfinite(x)
57
+
58
+ #define m_mulsum_init INT2FIX(0)
59
+
60
+ #define m_sprintf(s,x) sprintf(s,"%g",x)
61
+
62
+ #define cmp_prnan(a,b) \
63
+ ((qsort_cast(a)==qsort_cast(b)) ? 0 : \
64
+ (qsort_cast(a) > qsort_cast(b)) ? 1 : -1)
65
+
66
+ #define cmp_ignan(a,b) \
67
+ (m_isnan(qsort_cast(a)) ? (m_isnan(qsort_cast(b)) ? 0 : 1) : \
68
+ (m_isnan(qsort_cast(b)) ? -1 : \
69
+ ((qsort_cast(a)==qsort_cast(b)) ? 0 : \
70
+ (qsort_cast(a) > qsort_cast(b)) ? 1 : -1)))
71
+
72
+ #define cmpgt_prnan(a,b) \
73
+ (qsort_cast(a) > qsort_cast(b))
74
+
75
+ #define cmpgt_ignan(a,b) \
76
+ ((m_isnan(qsort_cast(a)) && !m_isnan(qsort_cast(b))) || \
77
+ (qsort_cast(a) > qsort_cast(b)))
78
+
79
+ #define m_sqrt(x) sqrt(x)
80
+ #define m_cbrt(x) cbrt(x)
81
+ #define m_log(x) log(x)
82
+ #define m_log2(x) log2(x)
83
+ #define m_log10(x) log10(x)
84
+ #define m_exp(x) exp(x)
85
+ #define m_exp2(x) exp2(x)
86
+ #ifdef HAVE_EXP10
87
+ #define m_exp10(x) exp10(x)
88
+ #else
89
+ #define m_exp10(x) pow(10, x)
90
+ #endif
91
+ #define m_expm1(x) expm1(x)
92
+ #define m_log1p(x) log1p(x)
93
+
94
+ #define m_sin(x) sin(x)
95
+ #define m_cos(x) cos(x)
96
+ #define m_tan(x) tan(x)
97
+ #define m_asin(x) asin(x)
98
+ #define m_acos(x) acos(x)
99
+ #define m_atan(x) atan(x)
100
+ #define m_sinh(x) sinh(x)
101
+ #define m_cosh(x) cosh(x)
102
+ #define m_tanh(x) tanh(x)
103
+ #define m_asinh(x) asinh(x)
104
+ #define m_acosh(x) acosh(x)
105
+ #define m_atanh(x) atanh(x)
106
+ #define m_atan2(x,y) atan2(x,y)
107
+ #define m_hypot(x,y) hypot(x,y)
108
+ #define m_sinc(x) (sin(x)/(x))
109
+
110
+ #define m_erf(x) erf(x)
111
+ #define m_erfc(x) erfc(x)
112
+ #define m_ldexp(x,y) ldexp(x,y)
113
+ #define m_frexp(x,exp) frexp(x,exp)
114
+
115
+ static inline dtype pow_int(dtype x, int p)
116
+ {
117
+ dtype r=1;
118
+ switch(p) {
119
+ case 0: return 1;
120
+ case 1: return x;
121
+ case 2: return x*x;
122
+ case 3: return x*x*x;
123
+ case 4: x=x*x; return x*x;
124
+ }
125
+ if (p<0) return 1/pow_int(x,-p);
126
+ if (p>64) return pow(x,p);
127
+ while (p) {
128
+ if (p&1) r *= x;
129
+ x *= x;
130
+ p >>= 1;
131
+ }
132
+ return r;
133
+ }
134
+
135
+ static inline dtype f_seq(dtype x, dtype y, double c)
136
+ {
137
+ return x + y * c;
138
+ }
139
+
140
+ static inline dtype f_kahan_sum(size_t n, char *p, ssize_t stride)
141
+ {
142
+ size_t i=n;
143
+ dtype x;
144
+ volatile dtype y=0;
145
+ volatile dtype t,r=0;
146
+
147
+ for (; i--;) {
148
+ x = *(dtype*)p;
149
+ p += stride;
150
+ if (fabs(x) > fabs(y)) {
151
+ dtype z=x; x=y; y=z;
152
+ }
153
+ r += x;
154
+ t = y;
155
+ y += r;
156
+ t = y-t;
157
+ r -= t;
158
+ }
159
+ return y;
160
+ }
161
+
162
+ static inline dtype f_kahan_sum_nan(size_t n, char *p, ssize_t stride)
163
+ {
164
+ size_t i=n;
165
+ dtype x;
166
+ volatile dtype y=0;
167
+ volatile dtype t,r=0;
168
+
169
+ for (; i--;) {
170
+ x = *(dtype*)p;
171
+ p += stride;
172
+ if (!m_isnan(x)) {
173
+ if (fabs(x) > fabs(y)) {
174
+ dtype z=x; x=y; y=z;
175
+ }
176
+ r += x;
177
+ t = y;
178
+ y += r;
179
+ t = y-t;
180
+ r -= t;
181
+ }
182
+ }
183
+ return y;
184
+ }
185
+
186
+ #include "real_accum.h"
@@ -0,0 +1,24 @@
1
+ typedef int16_t dtype;
2
+ typedef int16_t rtype;
3
+ #define cT numo_cInt16
4
+ #define cRT cT
5
+
6
+ #define m_num_to_data(x) ((dtype)NUM2INT(x))
7
+ #define m_data_to_num(x) INT2NUM((int)(x))
8
+ #define m_extract(x) INT2NUM((int)*(dtype*)(x))
9
+ #define m_sprintf(s,x) sprintf(s,"%d",(int)(x))
10
+
11
+ #ifndef INT16_MIN
12
+ #define INT16_MIN (-32767-1)
13
+ #endif
14
+ #ifndef INT16_MAX
15
+ #define INT16_MAX (32767)
16
+ #endif
17
+
18
+ #define DATA_MIN INT16_MIN
19
+ #define DATA_MAX INT16_MAX
20
+
21
+ #define M_MIN m_data_to_num(INT16_MIN)
22
+ #define M_MAX m_data_to_num(INT16_MAX)
23
+
24
+ #include "int_macro.h"
@@ -0,0 +1,24 @@
1
+ typedef int32_t dtype;
2
+ typedef int32_t rtype;
3
+ #define cT numo_cInt32
4
+ #define cRT cT
5
+
6
+ #define m_num_to_data(x) ((dtype)NUM2INT32(x))
7
+ #define m_data_to_num(x) INT322NUM((int32_t)(x))
8
+ #define m_extract(x) INT322NUM((int32_t)*(dtype*)(x))
9
+ #define m_sprintf(s,x) sprintf(s,"%"PRId32,(int32_t)(x))
10
+
11
+ #ifndef INT32_MIN
12
+ #define INT32_MIN (-2147483647-1)
13
+ #endif
14
+ #ifndef INT32_MAX
15
+ #define INT32_MAX (2147483647)
16
+ #endif
17
+
18
+ #define DATA_MIN INT32_MIN
19
+ #define DATA_MAX INT32_MAX
20
+
21
+ #define M_MIN m_data_to_num(INT32_MIN)
22
+ #define M_MAX m_data_to_num(INT32_MAX)
23
+
24
+ #include "int_macro.h"
@@ -0,0 +1,24 @@
1
+ typedef int64_t dtype;
2
+ typedef int64_t rtype;
3
+ #define cT numo_cInt64
4
+ #define cRT cT
5
+
6
+ #define m_num_to_data(x) ((dtype)NUM2INT64(x))
7
+ #define m_data_to_num(x) INT642NUM((int64_t)(x))
8
+ #define m_extract(x) INT642NUM((int64_t)*(dtype*)(x))
9
+ #define m_sprintf(s,x) sprintf(s,"%"PRId64,(int64_t)(x))
10
+
11
+ #ifndef INT64_MIN
12
+ #define INT64_MIN (-9223372036854775807l-1)
13
+ #endif
14
+ #ifndef INT64_MAX
15
+ #define INT64_MAX (9223372036854775807l)
16
+ #endif
17
+
18
+ #define DATA_MIN INT64_MIN
19
+ #define DATA_MAX INT64_MAX
20
+
21
+ #define M_MIN m_data_to_num(INT64_MIN)
22
+ #define M_MAX m_data_to_num(INT64_MAX)
23
+
24
+ #include "int_macro.h"
@@ -0,0 +1,24 @@
1
+ typedef int8_t dtype;
2
+ typedef int8_t rtype;
3
+ #define cT numo_cInt8
4
+ #define cRT cT
5
+
6
+ #define m_num_to_data(x) ((dtype)NUM2INT(x))
7
+ #define m_data_to_num(x) INT2NUM((int)(x))
8
+ #define m_extract(x) INT2NUM((int)*(dtype*)(x))
9
+ #define m_sprintf(s,x) sprintf(s,"%d",(int)(x))
10
+
11
+ #ifndef INT8_MIN
12
+ #define INT8_MIN (-127-1)
13
+ #endif
14
+ #ifndef INT8_MAX
15
+ #define INT8_MAX (127)
16
+ #endif
17
+
18
+ #define DATA_MIN INT8_MIN
19
+ #define DATA_MAX INT8_MAX
20
+
21
+ #define M_MIN INT2FIX(INT8_MIN)
22
+ #define M_MAX INT2FIX(INT8_MAX)
23
+
24
+ #include "int_macro.h"
@@ -0,0 +1,41 @@
1
+ #include "xint_macro.h"
2
+
3
+ #define m_sign(x) (((x)==0) ? 0 : (((x)>0) ? 1 : -1))
4
+
5
+ static inline dtype m_abs(dtype x) {
6
+ if (x==DATA_MIN) {
7
+ rb_raise(nary_eValueError, "cannot convert the minimum integer");
8
+ }
9
+ return (x<0)?-x:x;
10
+ }
11
+
12
+ static inline dtype int_reciprocal(dtype x) {
13
+ switch (x) {
14
+ case 1:
15
+ return 1;
16
+ case -1:
17
+ return -1;
18
+ case 0:
19
+ rb_raise(rb_eZeroDivError, "divided by 0");
20
+ default:
21
+ return 0;
22
+ }
23
+ }
24
+
25
+ static dtype pow_int(dtype x, int p)
26
+ {
27
+ dtype r = m_one;
28
+ switch(p) {
29
+ case 0: return 1;
30
+ case 1: return x;
31
+ case 2: return x*x;
32
+ case 3: return x*x*x;
33
+ }
34
+ if (p<0) return 0;
35
+ while (p) {
36
+ if (p&1) r *= x;
37
+ x *= x;
38
+ p >>= 1;
39
+ }
40
+ return r;
41
+ }
@@ -0,0 +1,486 @@
1
+ #define not_nan(x) ((x)==(x))
2
+
3
+ #define m_mulsum(x,y,z) {z = m_add(m_mul(x,y),z);}
4
+ #define m_mulsum_nan(x,y,z) { \
5
+ if(not_nan(x) && not_nan(y)) { \
6
+ z = m_add(m_mul(x,y),z); \
7
+ }}
8
+
9
+ #define m_cumsum(x,y) {(x)=m_add(x,y);}
10
+ #define m_cumsum_nan(x,y) { \
11
+ if (!not_nan(x)) { \
12
+ (x) = (y); \
13
+ } else if (not_nan(y)) { \
14
+ (x) = m_add(x,y); \
15
+ }}
16
+
17
+ #define m_cumprod(x,y) {(x)=m_mul(x,y);}
18
+ #define m_cumprod_nan(x,y) { \
19
+ if (!not_nan(x)) { \
20
+ (x) = (y); \
21
+ } else if (not_nan(y)) { \
22
+ (x) = m_mul(x,y); \
23
+ }}
24
+
25
+ static inline dtype f_sum(size_t n, char *p, ssize_t stride)
26
+ {
27
+ size_t i=n;
28
+ dtype x,y=m_zero;
29
+
30
+ for (; i--;) {
31
+ x = *(dtype*)p;
32
+ y = m_add(x,y);
33
+ p += stride;
34
+ }
35
+ return y;
36
+ }
37
+
38
+ static inline dtype f_sum_nan(size_t n, char *p, ssize_t stride)
39
+ {
40
+ size_t i=n;
41
+ dtype x,y=m_zero;
42
+
43
+ for (; i--;) {
44
+ x = *(dtype*)p;
45
+ p += stride;
46
+ if (not_nan(x)) {
47
+ y = m_add(x,y);
48
+ }
49
+ }
50
+ return y;
51
+ }
52
+
53
+
54
+ static inline dtype f_prod(size_t n, char *p, ssize_t stride)
55
+ {
56
+ size_t i=n;
57
+ dtype x,y=m_one;
58
+
59
+ for (; i--;) {
60
+ x = *(dtype*)p;
61
+ p += stride;
62
+ y = m_mul(x,y);
63
+ }
64
+ return y;
65
+ }
66
+
67
+ static inline dtype f_prod_nan(size_t n, char *p, ssize_t stride)
68
+ {
69
+ size_t i=n;
70
+ dtype x,y=m_one;
71
+
72
+ for (; i--;) {
73
+ x = *(dtype*)p;
74
+ p += stride;
75
+ if (not_nan(x)) {
76
+ y = m_mul(x,y);
77
+ }
78
+ }
79
+ return y;
80
+ }
81
+
82
+ static inline dtype f_mean(size_t n, char *p, ssize_t stride)
83
+ {
84
+ size_t i=n;
85
+ size_t count=0;
86
+ dtype x,y=m_zero;
87
+
88
+ for (; i--;) {
89
+ x = *(dtype*)p;
90
+ p += stride;
91
+ y = m_add(x,y);
92
+ count++;
93
+ }
94
+ return m_div(y,m_from_real(count));
95
+ }
96
+
97
+ static inline dtype f_mean_nan(size_t n, char *p, ssize_t stride)
98
+ {
99
+ size_t i=n;
100
+ size_t count=0;
101
+ dtype x,y=m_zero;
102
+
103
+ for (; i--;) {
104
+ x = *(dtype*)p;
105
+ p += stride;
106
+ if (not_nan(x)) {
107
+ y = m_add(x,y);
108
+ count++;
109
+ }
110
+ }
111
+ return m_div(y,m_from_real(count));
112
+ }
113
+
114
+ static inline dtype f_var(size_t n, char *p, ssize_t stride)
115
+ {
116
+ size_t i=n;
117
+ size_t count=0;
118
+ dtype x,y=m_zero;
119
+ dtype a,m;
120
+
121
+ m = f_mean(n,p,stride);
122
+
123
+ for (; i--;) {
124
+ x = *(dtype*)p;
125
+ p += stride;
126
+ a = m_abs(m_sub(x,m));
127
+ y = m_add(y,m_square(a));
128
+ count++;
129
+ }
130
+ return m_div(y,m_from_real(count-1));
131
+ }
132
+
133
+ static inline dtype f_var_nan(size_t n, char *p, ssize_t stride)
134
+ {
135
+ size_t i=n;
136
+ size_t count=0;
137
+ dtype x,y=m_zero;
138
+ dtype a,m;
139
+
140
+ m = f_mean_nan(n,p,stride);
141
+
142
+ for (; i--;) {
143
+ x = *(dtype*)p;
144
+ p += stride;
145
+ if (not_nan(x)) {
146
+ a = m_abs(m_sub(x,m));
147
+ y = m_add(y,m_square(a));
148
+ count++;
149
+ }
150
+ }
151
+ return m_div(y,m_from_real(count-1));
152
+ }
153
+
154
+ static inline dtype f_stddev(size_t n, char *p, ssize_t stride)
155
+ {
156
+ return m_sqrt(f_var(n,p,stride));
157
+ }
158
+
159
+ static inline dtype f_stddev_nan(size_t n, char *p, ssize_t stride)
160
+ {
161
+ return m_sqrt(f_var_nan(n,p,stride));
162
+ }
163
+
164
+ static inline dtype f_rms(size_t n, char *p, ssize_t stride)
165
+ {
166
+ size_t i=n;
167
+ size_t count=0;
168
+ dtype x,y=m_zero;
169
+
170
+ for (; i--;) {
171
+ x = *(dtype*)p;
172
+ p += stride;
173
+ y = m_add(y,m_square(m_abs(x)));
174
+ count++;
175
+ }
176
+ return m_sqrt(m_div(y,m_from_real(count)));
177
+ }
178
+
179
+ static inline dtype f_rms_nan(size_t n, char *p, ssize_t stride)
180
+ {
181
+ size_t i=n;
182
+ size_t count=0;
183
+ dtype x,y=m_zero;
184
+
185
+ for (; i--;) {
186
+ x = *(dtype*)p;
187
+ p += stride;
188
+ if (not_nan(x)) {
189
+ y = m_add(y,m_square(m_abs(x)));
190
+ count++;
191
+ }
192
+ }
193
+ return m_sqrt(m_div(y,m_from_real(count)));
194
+ }
195
+
196
+ // ---------------------------------------------------------
197
+
198
+ static inline dtype f_min_nan(size_t n, char *p, ssize_t stride)
199
+ {
200
+ dtype x,y;
201
+ size_t i=n;
202
+
203
+ y = *(dtype*)p;
204
+ p += stride;
205
+ if (!not_nan(y)) {return y;}
206
+ for (i--; i--;) {
207
+ x = *(dtype*)p;
208
+ p += stride;
209
+ if (!not_nan(x)) {return x;}
210
+ if (m_lt(x,y)) {
211
+ y = x;
212
+ }
213
+ }
214
+ return y;
215
+ }
216
+
217
+ static inline dtype f_min(size_t n, char *p, ssize_t stride)
218
+ {
219
+ dtype x,y=m_zero;
220
+ size_t i=n;
221
+
222
+ for (; i--; ) {
223
+ y = *(dtype*)p;
224
+ p += stride;
225
+ if (not_nan(y)) {
226
+ for (; i--;) {
227
+ x = *(dtype*)p;
228
+ p += stride;
229
+ if (m_lt(x,y)) {
230
+ y = x;
231
+ }
232
+ }
233
+ break;
234
+ }
235
+ }
236
+ return y;
237
+ }
238
+
239
+ static inline dtype f_max_nan(size_t n, char *p, ssize_t stride)
240
+ {
241
+ dtype x,y;
242
+ size_t i=n;
243
+
244
+ y = *(dtype*)p;
245
+ p += stride;
246
+ if (!not_nan(y)) {return y;}
247
+ for (i--; i--;) {
248
+ x = *(dtype*)p;
249
+ p += stride;
250
+ if (!not_nan(x)) {return x;}
251
+ if (m_gt(x,y)) {
252
+ y = x;
253
+ }
254
+ }
255
+ return y;
256
+ }
257
+
258
+ static inline dtype f_max(size_t n, char *p, ssize_t stride)
259
+ {
260
+ dtype x,y=m_zero;
261
+ size_t i=n;
262
+
263
+ for (; i--; ) {
264
+ y = *(dtype*)p;
265
+ p += stride;
266
+ if (not_nan(y)) {
267
+ for (; i--;) {
268
+ x = *(dtype*)p;
269
+ p += stride;
270
+ if (m_gt(x,y)) {
271
+ y = x;
272
+ }
273
+ }
274
+ break;
275
+ }
276
+ }
277
+ return y;
278
+ }
279
+
280
+ static inline size_t f_min_index_nan(size_t n, char *p, ssize_t stride)
281
+ {
282
+ dtype x, y;
283
+ size_t i, j=0;
284
+
285
+ y = *(dtype*)p;
286
+ p += stride;
287
+ if (!not_nan(y)) {return j;}
288
+ for (i=1; i<n; i++) {
289
+ x = *(dtype*)p;
290
+ p += stride;
291
+ if (!not_nan(x)) {return i;}
292
+ if (m_lt(x,y)) {
293
+ y = x;
294
+ j = i;
295
+ }
296
+ }
297
+ return j;
298
+ }
299
+
300
+ static inline size_t f_min_index(size_t n, char *p, ssize_t stride)
301
+ {
302
+ dtype x, y;
303
+ size_t i, j=0;
304
+
305
+ for (i=0; i<n; i++) {
306
+ y = *(dtype*)p;
307
+ p += stride;
308
+ if (not_nan(y)) {
309
+ j = i; i++;
310
+ for (; i<n; i++) {
311
+ x = *(dtype*)p;
312
+ p += stride;
313
+ if (m_lt(x,y)) {
314
+ y = x;
315
+ j = i;
316
+ }
317
+ }
318
+ break;
319
+ }
320
+ }
321
+ return j;
322
+ }
323
+
324
+ static inline size_t f_max_index_nan(size_t n, char *p, ssize_t stride)
325
+ {
326
+ dtype x, y;
327
+ size_t i, j=0;
328
+
329
+ y = *(dtype*)p;
330
+ p += stride;
331
+ if (!not_nan(y)) {return j;}
332
+ for (i=1; i<n; i++) {
333
+ x = *(dtype*)p;
334
+ p += stride;
335
+ if (!not_nan(x)) {return i;}
336
+ if (m_gt(x,y)) {
337
+ y = x;
338
+ j = i;
339
+ }
340
+ }
341
+ return j;
342
+ }
343
+
344
+ static inline size_t f_max_index(size_t n, char *p, ssize_t stride)
345
+ {
346
+ dtype x, y;
347
+ size_t i, j=0;
348
+
349
+ for (i=0; i<n; i++) {
350
+ y = *(dtype*)p;
351
+ p += stride;
352
+ if (not_nan(y)) {
353
+ j = i; i++;
354
+ for (; i<n; i++) {
355
+ x = *(dtype*)p;
356
+ p += stride;
357
+ if (m_gt(x,y)) {
358
+ y = x;
359
+ j = i;
360
+ }
361
+ }
362
+ break;
363
+ }
364
+ }
365
+ return j;
366
+ }
367
+
368
+ static inline void
369
+ f_minmax_nan(size_t n, char *p, ssize_t stride, dtype *amin, dtype *amax)
370
+ {
371
+ dtype x,min,max;
372
+ size_t i=n;
373
+
374
+ min = max = *(dtype*)p;
375
+ p += stride;
376
+ if (!not_nan(min)) {
377
+ *amin = *amax = min;
378
+ return;
379
+ }
380
+ for (i--; i--;) {
381
+ x = *(dtype*)p;
382
+ p += stride;
383
+ if (!not_nan(x)) {
384
+ *amin = *amax = x;
385
+ return;
386
+ }
387
+ if (m_lt(x,min)) {
388
+ min = x;
389
+ }
390
+ if (m_gt(x,max)) {
391
+ max = x;
392
+ }
393
+ }
394
+ *amin = min;
395
+ *amax = max;
396
+ return;
397
+ }
398
+
399
+ static inline dtype f_ptp_nan(size_t n, char *p, ssize_t stride)
400
+ {
401
+ dtype min,max;
402
+ f_minmax_nan(n,p,stride,&min,&max);
403
+ return m_sub(max,min);
404
+ }
405
+
406
+ static inline void
407
+ f_minmax(size_t n, char *p, ssize_t stride, dtype *amin, dtype *amax)
408
+ {
409
+ dtype x,min,max;
410
+ size_t i=n;
411
+
412
+ min = max = m_zero;
413
+ for (; i--; ) {
414
+ min = *(dtype*)p;
415
+ p += stride;
416
+ if (not_nan(min)) {
417
+ max = min;
418
+ for (; i--;) {
419
+ x = *(dtype*)p;
420
+ p += stride;
421
+ if (m_lt(x,min)) {
422
+ min = x;
423
+ }
424
+ if (m_gt(x,max)) {
425
+ max = x;
426
+ }
427
+ }
428
+ break;
429
+ }
430
+ }
431
+ *amin = min;
432
+ *amax = max;
433
+ return;
434
+ }
435
+
436
+ static inline dtype f_ptp(size_t n, char *p, ssize_t stride)
437
+ {
438
+ dtype min,max;
439
+ f_minmax(n,p,stride,&min,&max);
440
+ return m_sub(max,min);
441
+ }
442
+
443
+
444
+ static inline dtype f_maximum(dtype x, dtype y)
445
+ {
446
+ if (m_ge(x,y)) {
447
+ return x;
448
+ }
449
+ if (not_nan(y)) {
450
+ return y;
451
+ }
452
+ return x;
453
+ }
454
+
455
+ static inline dtype f_maximum_nan(dtype x, dtype y)
456
+ {
457
+ if (m_ge(x,y)) {
458
+ return x;
459
+ }
460
+ if (!not_nan(x)) {
461
+ return x;
462
+ }
463
+ return y;
464
+ }
465
+
466
+ static inline dtype f_minimum(dtype x, dtype y)
467
+ {
468
+ if (m_le(x,y)) {
469
+ return x;
470
+ }
471
+ if (not_nan(y)) {
472
+ return y;
473
+ }
474
+ return x;
475
+ }
476
+
477
+ static inline dtype f_minimum_nan(dtype x, dtype y)
478
+ {
479
+ if (m_le(x,y)) {
480
+ return x;
481
+ }
482
+ if (!not_nan(x)) {
483
+ return x;
484
+ }
485
+ return y;
486
+ }