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,75 @@
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_reciprocal(x) rb_funcall(x,id_reciprocal,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
+ #define m_trunc(x) rb_funcall(x,id_truncate,0)
29
+ #define m_sign(x) rb_funcall(x,id_ufo,1,INT2FIX(0))
30
+
31
+ #define m_eq(x,y) RTEST(rb_funcall(x,id_eq,1,y))
32
+ #define m_ne(x,y) RTEST(rb_funcall(x,id_ne,1,y))
33
+ #define m_gt(x,y) RTEST(rb_funcall(x,id_gt,1,y))
34
+ #define m_ge(x,y) RTEST(rb_funcall(x,id_ge,1,y))
35
+ #define m_lt(x,y) RTEST(rb_funcall(x,id_lt,1,y))
36
+ #define m_le(x,y) RTEST(rb_funcall(x,id_le,1,y))
37
+
38
+ #define m_bit_and(x,y) rb_funcall(x,id_bit_and,1,y)
39
+ #define m_bit_or(x,y) rb_funcall(x,id_bit_or, 1,y)
40
+ #define m_bit_xor(x,y) rb_funcall(x,id_bit_xor,1,y)
41
+ #define m_bit_not(x) rb_funcall(x,id_bit_not,0)
42
+
43
+ #define m_left_shift(x,y) rb_funcall(x,id_left_shift,1,y)
44
+ #define m_right_shift(x,y) rb_funcall(x,id_right_shift,1,y)
45
+
46
+ #define m_isnan(x) ((rb_respond_to(x,id_nan_p)) ? RTEST(rb_funcall(x,id_nan_p,0)) : 0)
47
+ #define m_isinf(x) ((rb_respond_to(x,id_infinite_p)) ? RTEST(rb_funcall(x,id_infinite_p,0)) : 0)
48
+ #define m_isposinf(x) ((rb_respond_to(x,id_infinite_p)) ? \
49
+ ((RTEST(rb_funcall(x,id_infinite_p,0))) ? \
50
+ m_gt(x,INT2FIX(0)) : 0) : 0)
51
+ #define m_isneginf(x) ((rb_respond_to(x,id_infinite_p)) ? \
52
+ ((RTEST(rb_funcall(x,id_infinite_p,0))) ? \
53
+ m_lt(x,INT2FIX(0)) : 0) : 0)
54
+ #define m_isfinite(x) ((rb_respond_to(x,id_finite_p)) ? RTEST(rb_funcall(x,id_finite_p,0)) : 0)
55
+
56
+ #define m_mulsum_init INT2FIX(0)
57
+
58
+ #define m_sprintf(s,x) robj_sprintf(s,x)
59
+
60
+ static inline int robj_sprintf(char *s, VALUE x) {
61
+ VALUE v = rb_funcall(x,rb_intern("to_s"),0);
62
+ return sprintf(s,"%s",StringValuePtr(v));
63
+ }
64
+
65
+ #define m_sqrt(x) \
66
+ rb_funcall(rb_const_get(rb_mKernel,rb_intern("Math")), \
67
+ rb_intern("sqrt"),1,x);
68
+
69
+ static inline dtype f_seq(dtype x, dtype y, size_t c)
70
+ {
71
+ y = m_mul(y,SIZET2NUM(c));
72
+ return m_add(x,y);
73
+ }
74
+
75
+ #include "real_accum.h"
@@ -0,0 +1,27 @@
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
+ }
22
+
23
+ /* generates a random number on [0,1)-real-interval */
24
+ inline static dtype m_rand(dtype max)
25
+ {
26
+ return DBL2NUM(genrand_res53_mix() * max);
27
+ }
@@ -0,0 +1,44 @@
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
+ }
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) = to_real2(gen_rand32()) * REAL(max);
19
+ IMAG(z) = to_real2(gen_rand32()) * 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 = to_real2(gen_rand32());
31
+ x1 = x1*2-1;
32
+ x2 = to_real2(gen_rand32());
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(1.1920928955078125e-07)
43
+ #define M_MIN rb_float_new(1.1754943508222875e-38)
44
+ #define M_MAX rb_float_new(3.4028234663852886e+38)
@@ -0,0 +1,43 @@
1
+ typedef float dtype;
2
+ typedef float rtype;
3
+ #define cT numo_cSFloat
4
+ #define cRT numo_cSFloat
5
+ #define mTM numo_mSFloatMath
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 to_real2(gen_rand32()) * 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 = to_real2(gen_rand32());
24
+ x1 = x1*2-1;
25
+ x2 = to_real2(gen_rand32());
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_sfloat_new_dim0(0.0/0.0)
36
+ #define m_max_init numo_sfloat_new_dim0(0.0/0.0)
37
+
38
+ #define m_extract(x) rb_float_new(*(float*)x)
39
+ #define m_nearly_eq(x,y) (fabs(x-y)<=(fabs(x)+fabs(y))*FLT_EPSILON*2)
40
+
41
+ #define M_EPSILON rb_float_new(1.1920928955078125e-07)
42
+ #define M_MIN rb_float_new(1.1754943508222875e-38)
43
+ #define M_MAX rb_float_new(3.4028234663852886e+38)
@@ -0,0 +1,21 @@
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
+
11
+ #ifndef UINT16_MAX
12
+ #define UINT16_MAX (65535)
13
+ #endif
14
+
15
+ #define DATA_MIN UINT16_MIN
16
+ #define DATA_MAX UINT16_MAX
17
+
18
+ #define M_MIN INT2FIX(0)
19
+ #define M_MAX m_data_to_num(UINT16_MAX)
20
+
21
+ #include "uint_macro.h"
@@ -0,0 +1,21 @@
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
+
11
+ #ifndef UINT32_MAX
12
+ #define UINT32_MAX (4294967295u)
13
+ #endif
14
+
15
+ #define DATA_MIN UINT32_MIN
16
+ #define DATA_MAX UINT32_MAX
17
+
18
+ #define M_MIN INT2FIX(0)
19
+ #define M_MAX m_data_to_num(UINT32_MAX)
20
+
21
+ #include "uint_macro.h"
@@ -0,0 +1,21 @@
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
+
11
+ #ifndef UINT64_MAX
12
+ #define UINT64_MAX (18446744073709551615ul)
13
+ #endif
14
+
15
+ #define DATA_MIN UINT64_MIN
16
+ #define DATA_MAX UINT64_MAX
17
+
18
+ #define M_MIN INT2FIX(0)
19
+ #define M_MAX m_data_to_num(UINT64_MAX)
20
+
21
+ #include "uint_macro.h"
@@ -0,0 +1,21 @@
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
+
11
+ #ifndef UINT8_MAX
12
+ #define UINT8_MAX (255)
13
+ #endif
14
+
15
+ #define DATA_MIN UINT8_MIN
16
+ #define DATA_MAX UINT8_MAX
17
+
18
+ #define M_MIN INT2FIX(0)
19
+ #define M_MAX m_data_to_num(UINT8_MAX)
20
+
21
+ #include "uint_macro.h"
@@ -0,0 +1,32 @@
1
+ #include "xint_macro.h"
2
+
3
+ #define m_abs(x) (x)
4
+ #define m_sign(x) (((x)==0) ? 0:1)
5
+
6
+ static inline dtype int_reciprocal(dtype x) {
7
+ switch (x) {
8
+ case 1:
9
+ return 1;
10
+ case 0:
11
+ rb_raise(rb_eZeroDivError, "divided by 0");
12
+ default:
13
+ return 0;
14
+ }
15
+ }
16
+
17
+ static dtype pow_int(dtype x, int p)
18
+ {
19
+ dtype r = m_one;
20
+ switch(p) {
21
+ case 0: return 1;
22
+ case 1: return x;
23
+ case 2: return x*x;
24
+ case 3: return x*x*x;
25
+ }
26
+ while (p) {
27
+ if (p&1) r *= x;
28
+ x *= x;
29
+ p >>= 1;
30
+ }
31
+ return r;
32
+ }
@@ -0,0 +1,189 @@
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_reciprocal(x) int_reciprocal(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_left_shift(x,y) ((x)<<(y))
32
+ #define m_right_shift(x,y) ((x)>>(y))
33
+
34
+ #define m_isnan(x) 0
35
+
36
+ #define m_mulsum(x,y,z) {z += x*y;}
37
+ #define m_mulsum_init INT2FIX(0)
38
+ #define m_cumsum(x,y) {x += y;}
39
+ #define m_cumprod(x,y) {x *= y;}
40
+
41
+ #define cmp(a,b) \
42
+ ((qsort_cast(a)==qsort_cast(b)) ? 0 : \
43
+ (qsort_cast(a) > qsort_cast(b)) ? 1 : -1)
44
+ #define cmpgt(a,b) \
45
+ (qsort_cast(a) > qsort_cast(b))
46
+
47
+
48
+ static inline dtype f_sum(size_t n, char *p, ssize_t stride)
49
+ {
50
+ dtype x,y=0;
51
+ size_t i=n;
52
+ for (; i--;) {
53
+ x = *(dtype*)p;
54
+ y += x;
55
+ p += stride;
56
+ }
57
+ return y;
58
+ }
59
+
60
+ static inline dtype f_prod(size_t n, char *p, ssize_t stride)
61
+ {
62
+ dtype x,y=1;
63
+ size_t i=n;
64
+ for (; i--;) {
65
+ x = *(dtype*)p;
66
+ y *= x;
67
+ p += stride;
68
+ }
69
+ return y;
70
+ }
71
+
72
+ static inline dtype f_min(size_t n, char *p, ssize_t stride)
73
+ {
74
+ dtype x,y;
75
+ size_t i=n;
76
+
77
+ y = *(dtype*)p;
78
+ p += stride;
79
+ i--;
80
+ for (; i--;) {
81
+ x = *(dtype*)p;
82
+ if (x < y) {
83
+ y = x;
84
+ }
85
+ p += stride;
86
+ }
87
+ return y;
88
+ }
89
+
90
+ static inline dtype f_max(size_t n, char *p, ssize_t stride)
91
+ {
92
+ dtype x,y;
93
+ size_t i=n;
94
+
95
+ y = *(dtype*)p;
96
+ p += stride;
97
+ i--;
98
+ for (; i--;) {
99
+ x = *(dtype*)p;
100
+ if (x > y) {
101
+ y = x;
102
+ }
103
+ p += stride;
104
+ }
105
+ return y;
106
+ }
107
+
108
+ static inline size_t f_min_index(size_t n, char *p, ssize_t stride)
109
+ {
110
+ dtype x, y;
111
+ size_t i, j=0;
112
+
113
+ y = *(dtype*)p;
114
+ for (i=1; i<n; i++) {
115
+ x = *(dtype*)(p+i*stride);
116
+ if (x < y) {
117
+ y = x;
118
+ j = i;
119
+ }
120
+ }
121
+ return j;
122
+ }
123
+
124
+ static inline size_t f_max_index(size_t n, char *p, ssize_t stride)
125
+ {
126
+ dtype x, y;
127
+ size_t i, j=0;
128
+
129
+ y = *(dtype*)p;
130
+ for (i=1; i<n; i++) {
131
+ x = *(dtype*)(p+i*stride);
132
+ if (x > y) {
133
+ y = x;
134
+ j = i;
135
+ }
136
+ }
137
+ return j;
138
+ }
139
+
140
+ static inline void
141
+ f_minmax(size_t n, char *p, ssize_t stride, dtype* amin, dtype* amax)
142
+ {
143
+ dtype x,min,max;
144
+ size_t i=n;
145
+
146
+ min = max = *(dtype*)p;
147
+ p += stride;
148
+ for (i--; i--;) {
149
+ x = *(dtype*)p;
150
+ if (m_gt(x,max)) {
151
+ max = x;
152
+ }
153
+ if (m_lt(x,min)) {
154
+ min = x;
155
+ }
156
+ p += stride;
157
+ }
158
+ *amin = min;
159
+ *amax = max;
160
+ return;
161
+ }
162
+
163
+ static inline dtype f_ptp(size_t n, char *p, ssize_t stride)
164
+ {
165
+ dtype min,max;
166
+ f_minmax(n,p,stride,&min,&max);
167
+ return m_sub(max,min);
168
+ }
169
+
170
+ static inline double f_seq(double x, double y, double c)
171
+ {
172
+ return x + y * c;
173
+ }
174
+
175
+ static inline dtype f_maximum(dtype x, dtype y)
176
+ {
177
+ if (m_ge(x,y)) {
178
+ return x;
179
+ }
180
+ return y;
181
+ }
182
+
183
+ static inline dtype f_minimum(dtype x, dtype y)
184
+ {
185
+ if (m_le(x,y)) {
186
+ return x;
187
+ }
188
+ return y;
189
+ }