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,94 @@
1
+ /*
2
+ ndloop.h
3
+ Numerical Array Extension for Ruby
4
+ (C) Copyright 1999-2017 by Masahiro TANAKA
5
+ */
6
+ #ifndef NDLOOP_H
7
+ #define NDLOOP_H
8
+
9
+ typedef struct NA_LOOP_ITER {
10
+ ssize_t pos; // - required for each dimension.
11
+ ssize_t step;
12
+ size_t *idx;
13
+ } na_loop_iter_t;
14
+
15
+ typedef struct NA_LOOP_ARGS {
16
+ VALUE value;
17
+ ssize_t elmsz;
18
+ char *ptr;
19
+ //char *buf_ptr; //
20
+ int ndim; // required for each argument.
21
+ // ssize_t pos; - not required here.
22
+ size_t *shape;
23
+ na_loop_iter_t *iter; // moved from na_loop_t
24
+ } na_loop_args_t;
25
+
26
+ // pass this structure to user iterator
27
+ typedef struct NA_LOOP {
28
+ int narg;
29
+ int ndim; // n of user dimention - required for each iterator.
30
+ size_t *n; // n of elements for each dim (=shape)
31
+ na_loop_args_t *args; // for each arg
32
+ VALUE option;
33
+ void *opt_ptr;
34
+ VALUE err_type;
35
+ } na_loop_t;
36
+
37
+
38
+ // ------------------ ndfunc -------------------------------------------
39
+
40
+ #define NDF_HAS_LOOP (1<<0) // x[i]
41
+ #define NDF_STRIDE_LOOP (1<<1) // *(x+stride*i)
42
+ #define NDF_INDEX_LOOP (1<<2) // *(x+idx[i])
43
+ #define NDF_KEEP_DIM (1<<3)
44
+ #define NDF_INPLACE (1<<4)
45
+ #define NDF_ACCEPT_BYTESWAP (1<<5)
46
+
47
+ #define NDF_FLAT_REDUCE (1<<6)
48
+ #define NDF_EXTRACT (1<<7)
49
+ #define NDF_CUM (1<<8)
50
+
51
+ #define FULL_LOOP (NDF_HAS_LOOP|NDF_STRIDE_LOOP|NDF_INDEX_LOOP|NDF_INPLACE)
52
+ #define FULL_LOOP_NIP (NDF_HAS_LOOP|NDF_STRIDE_LOOP|NDF_INDEX_LOOP)
53
+ #define STRIDE_LOOP (NDF_HAS_LOOP|NDF_STRIDE_LOOP|NDF_INPLACE)
54
+ #define STRIDE_LOOP_NIP (NDF_HAS_LOOP|NDF_STRIDE_LOOP)
55
+ #define NO_LOOP 0
56
+
57
+ #define OVERWRITE Qtrue // used for CASTABLE(t)
58
+
59
+ #define NDF_TEST(nf,fl) ((nf)->flag & (fl))
60
+ #define NDF_SET(nf,fl) {(nf)->flag |= (fl);}
61
+
62
+ #define NDF_ARG_READ_ONLY 1
63
+ #define NDF_ARG_WRITE_ONLY 2
64
+ #define NDF_ARG_READ_WRITE 3
65
+
66
+ // type of user function
67
+ typedef void (*na_iter_func_t) _((na_loop_t *const));
68
+ typedef VALUE (*na_text_func_t) _((char *ptr, size_t pos, VALUE opt));
69
+ //typedef void (*) void (*loop_func)(ndfunc_t*, na_md_loop_t*))
70
+
71
+
72
+ typedef struct NDF_ARG_IN {
73
+ VALUE type; // argument types
74
+ int dim; // # of dimension of argument handled by user function
75
+ // if dim==-1, reduce dimension
76
+ } ndfunc_arg_in_t;
77
+
78
+ typedef struct NDF_ARG_OUT {
79
+ VALUE type; // argument types
80
+ int dim; // # of dimension of argument handled by user function
81
+ size_t *shape;
82
+ } ndfunc_arg_out_t;
83
+
84
+ // spec of user function
85
+ typedef struct NDFUNCTION {
86
+ na_iter_func_t func; // user function
87
+ unsigned int flag; // what kind of loop user function supports
88
+ int nin; // # of arguments
89
+ int nout; // # of results
90
+ ndfunc_arg_in_t *ain; // spec of input arguments
91
+ ndfunc_arg_out_t *aout; // spec of output result
92
+ } ndfunc_t;
93
+
94
+ #endif /* NDLOOP_H */
@@ -0,0 +1,149 @@
1
+ /*
2
+ template.h
3
+ Numerical Array Extension for Ruby
4
+ (C) Copyright 1999-2017 by Masahiro TANAKA
5
+ */
6
+ #ifndef TEMPLATE_H
7
+ #define TEMPLATE_H
8
+
9
+ #define INIT_COUNTER( lp, c ) \
10
+ { c = (lp)->n[0]; }
11
+
12
+ #define NDL_CNT(lp) ((lp)->n[0])
13
+ #define NDL_PTR(lp,i) ((lp)->args[i].ptr + (lp)->args[i].iter[0].pos)
14
+ #define NDL_STEP(lp,i) ((lp)->args[i].iter[0].step)
15
+ #define NDL_IDX(lp,i) ((lp)->args[i].iter[0].idx)
16
+ #define NDL_ESZ(lp,i) ((lp)->args[i].elmsz)
17
+ #define NDL_SHAPE(lp,i) ((lp)->args[i].shape)
18
+
19
+ #define INIT_PTR( lp, i, pt, st ) \
20
+ { \
21
+ pt = ((lp)->args[i]).ptr + ((lp)->args[i].iter[0]).pos; \
22
+ st = ((lp)->args[i].iter[0]).step; \
23
+ }
24
+
25
+ #define INIT_PTR_IDX( lp, i, pt, st, id ) \
26
+ { \
27
+ pt = ((lp)->args[i]).ptr + ((lp)->args[i].iter[0]).pos; \
28
+ st = ((lp)->args[i].iter[0]).step; \
29
+ id = ((lp)->args[i].iter[0]).idx; \
30
+ }
31
+
32
+ #define INIT_ELMSIZE( lp, i, es ) \
33
+ { \
34
+ es = ((lp)->args[i]).elmsz; \
35
+ }
36
+
37
+ #define INIT_PTR_BIT( lp, i, ad, ps, st ) \
38
+ { \
39
+ ps = ((lp)->args[i].iter[0]).pos; \
40
+ ad = (BIT_DIGIT*)(((lp)->args[i]).ptr) + ps/NB; \
41
+ ps %= NB; \
42
+ st = ((lp)->args[i].iter[0]).step; \
43
+ }
44
+
45
+ #define INIT_PTR_BIT_IDX( lp, i, ad, ps, st, id ) \
46
+ { \
47
+ ps = ((lp)->args[i].iter[0]).pos; \
48
+ ad = (BIT_DIGIT*)(((lp)->args[i]).ptr) + ps/NB; \
49
+ ps %= NB; \
50
+ st = ((lp)->args[i].iter[0]).step; \
51
+ id = ((lp)->args[i].iter[0]).idx; \
52
+ }
53
+
54
+ #define GET_DATA( ptr, type, val ) \
55
+ { \
56
+ val = *(type*)(ptr); \
57
+ }
58
+
59
+ #define SET_DATA( ptr, type, val ) \
60
+ { \
61
+ *(type*)(ptr) = val; \
62
+ }
63
+
64
+ #define GET_DATA_STRIDE( ptr, step, type, val ) \
65
+ { \
66
+ val = *(type*)(ptr); \
67
+ ptr += step; \
68
+ }
69
+
70
+ #define GET_DATA_INDEX( ptr, idx, type, val ) \
71
+ { \
72
+ val = *(type*)(ptr + *idx); \
73
+ idx++; \
74
+ }
75
+
76
+ #define SET_DATA_STRIDE( ptr, step, type, val ) \
77
+ { \
78
+ *(type*)(ptr) = val; \
79
+ ptr += step; \
80
+ }
81
+
82
+ #define SET_DATA_INDEX( ptr, idx, type, val ) \
83
+ { \
84
+ *(type*)(ptr + *idx) = val; \
85
+ idx++; \
86
+ }
87
+
88
+ #define LOAD_BIT( adr, pos, val ) \
89
+ { \
90
+ size_t dig = (pos) / NB; \
91
+ int bit = (pos) % NB; \
92
+ val = (((BIT_DIGIT*)(adr))[dig]>>(bit)) & 1u; \
93
+ }
94
+
95
+ #define LOAD_BIT_STEP( adr, pos, step, idx, val ) \
96
+ { \
97
+ size_t dig; int bit; \
98
+ if (idx) { \
99
+ dig = ((pos) + *(idx)) / NB; \
100
+ bit = ((pos) + *(idx)) % NB; \
101
+ idx++; \
102
+ } else { \
103
+ dig = (pos) / NB; \
104
+ bit = (pos) % NB; \
105
+ pos += step; \
106
+ } \
107
+ val = (((BIT_DIGIT*)(adr))[dig]>>bit) & 1u; \
108
+ }
109
+
110
+ #define STORE_BIT(adr,pos,val) \
111
+ { \
112
+ size_t dig = (pos) / NB; \
113
+ int bit = (pos) % NB; \
114
+ ((BIT_DIGIT*)(adr))[dig] = \
115
+ (((BIT_DIGIT*)(adr))[dig] & ~(1u<<(bit))) | ((val)<<(bit)); \
116
+ }
117
+ // val -> val&1 ??
118
+
119
+ #define STORE_BIT_STEP( adr, pos, step, idx, val )\
120
+ { \
121
+ size_t dig; int bit; \
122
+ if (idx) { \
123
+ dig = ((pos) + *(idx)) / NB; \
124
+ bit = ((pos) + *(idx)) % NB; \
125
+ idx++; \
126
+ } else { \
127
+ dig = (pos) / NB; \
128
+ bit = (pos) % NB; \
129
+ pos += step; \
130
+ } \
131
+ ((BIT_DIGIT*)(adr))[dig] = \
132
+ (((BIT_DIGIT*)(adr))[dig] & ~(1u<<(bit))) | ((val)<<(bit)); \
133
+ }
134
+ // val -> val&1 ??
135
+
136
+ static inline int
137
+ is_aligned(const void *ptr, const size_t alignment)
138
+ {
139
+ return ((size_t)(ptr) & ((alignment)-1)) == 0;
140
+ }
141
+
142
+ static inline int
143
+ is_aligned_step(const ssize_t step, const size_t alignment)
144
+ {
145
+ return ((step) & ((alignment)-1)) == 0;
146
+ }
147
+
148
+
149
+ #endif /* ifndef TEMPLATE_H */
@@ -0,0 +1,33 @@
1
+ typedef BIT_DIGIT dtype;
2
+ typedef BIT_DIGIT rtype;
3
+ #define cT numo_cBit
4
+ #define cRT cT
5
+
6
+ #define m_zero 0
7
+ #define m_one 1
8
+
9
+ #define m_abs(x) (x)
10
+ #define m_sign(x) (((x)==0) ? 0:1)
11
+
12
+ #define m_from_double(x) (((x)==0) ? 0 : 1)
13
+ #define m_from_real(x) (((x)==0) ? 0 : 1)
14
+ #define m_data_to_num(x) INT2FIX(x)
15
+ #define m_sprintf(s,x) sprintf(s,"%1d",(int)(x))
16
+
17
+ #define m_copy(x) (x)
18
+ #define m_not(x) (~(x))
19
+ #define m_and(x,y) ((x)&(y))
20
+ #define m_or(x,y) ((x)|(y))
21
+ #define m_xor(x,y) ((x)^(y))
22
+ #define m_eq(x,y) (~((x)^(y)))
23
+ #define m_count_true(x) ((x)!=0)
24
+ #define m_count_false(x) ((x)==0)
25
+
26
+ static inline BIT_DIGIT m_num_to_data(VALUE num) {
27
+ if (RTEST(num)) {
28
+ if (!RTEST(rb_equal(num,INT2FIX(0)))) {
29
+ return 1;
30
+ }
31
+ }
32
+ return 0;
33
+ }
@@ -0,0 +1,409 @@
1
+ /*
2
+ complex.h
3
+ Numerical Array Extension for Ruby
4
+ (C) Copyright 1999-2017 by Masahiro TANAKA
5
+ */
6
+
7
+
8
+ static inline dtype c_new(rtype r, rtype i) {
9
+ dtype z;
10
+ REAL(z) = r;
11
+ IMAG(z) = i;
12
+ return z;
13
+ }
14
+
15
+ static inline dtype c_set_real(dtype x, rtype r) {
16
+ REAL(x)=r;
17
+ return x;
18
+ }
19
+
20
+ static inline dtype c_set_imag(dtype x, rtype i) {
21
+ IMAG(x)=i;
22
+ return x;
23
+ }
24
+
25
+ static inline VALUE COMP2NUM(dtype x) {
26
+ VALUE v;
27
+ v = rb_funcall(rb_intern("Kernel"), rb_intern("Complex"), 2,
28
+ rb_float_new(REAL(x)), rb_float_new(IMAG(x)));
29
+ return v;
30
+ }
31
+
32
+ static inline dtype NUM2COMP(VALUE v) {
33
+ dtype z;
34
+ REAL(z) = NUM2DBL(rb_funcall(v,id_real,0));
35
+ IMAG(z) = NUM2DBL(rb_funcall(v,id_imag,0));
36
+ return z;
37
+ }
38
+
39
+ #define c_is_zero(x) (REAL(x)==0 && IMAG(x)==0)
40
+ #define c_eq(x,y) (REAL(x)==REAL(y) && IMAG(x)==IMAG(y))
41
+ #define c_ne(x,y) (REAL(x)!=REAL(y) || IMAG(x)!=IMAG(y))
42
+ #define c_isnan(x) (isnan(REAL(x)) || isnan(IMAG(x)))
43
+ #define c_isinf(x) (isinf(REAL(x)) || isinf(IMAG(x)))
44
+ #define c_isposinf(x) ((isinf(REAL(x)) && signbit(REAL(x))==0) || \
45
+ (isinf(IMAG(x)) && signbit(IMAG(x))==0))
46
+ #define c_isneginf(x) ((isinf(REAL(x)) && signbit(REAL(x))) || \
47
+ (isinf(IMAG(x)) && signbit(IMAG(x))))
48
+ #define c_isfinite(x) (isfinite(REAL(x)) && isfinite(IMAG(x)))
49
+
50
+ static inline dtype c_zero() {
51
+ dtype z;
52
+ REAL(z) = 0;
53
+ IMAG(z) = 0;
54
+ return z;
55
+ }
56
+
57
+ static inline dtype c_one() {
58
+ dtype z;
59
+ REAL(z) = 1;
60
+ IMAG(z) = 0;
61
+ return z;
62
+ }
63
+
64
+ static inline dtype c_minus(dtype x) {
65
+ dtype z;
66
+ REAL(z) = -REAL(x);
67
+ IMAG(z) = -IMAG(x);
68
+ return z;
69
+ }
70
+
71
+ static inline dtype c_im(dtype x) {
72
+ dtype z;
73
+ REAL(z) = -IMAG(x);
74
+ IMAG(z) = REAL(x);
75
+ return z;
76
+ }
77
+
78
+ static inline dtype c_add(dtype x, dtype y) {
79
+ dtype z;
80
+ REAL(z) = REAL(x)+REAL(y);
81
+ IMAG(z) = IMAG(x)+IMAG(y);
82
+ return z;
83
+ }
84
+
85
+ static inline dtype c_sub(dtype x, dtype y) {
86
+ dtype z;
87
+ REAL(z) = REAL(x)-REAL(y);
88
+ IMAG(z) = IMAG(x)-IMAG(y);
89
+ return z;
90
+ }
91
+
92
+
93
+ static inline dtype c_mul(dtype x, dtype y) {
94
+ dtype z;
95
+ REAL(z) = REAL(x)*REAL(y)-IMAG(x)*IMAG(y);
96
+ IMAG(z) = REAL(x)*IMAG(y)+IMAG(x)*REAL(y);
97
+ return z;
98
+ }
99
+
100
+ static inline dtype c_mul_r(dtype x, rtype y) {
101
+ dtype z;
102
+ REAL(z) = REAL(x)*y;
103
+ IMAG(z) = IMAG(x)*y;
104
+ return z;
105
+ }
106
+
107
+ static inline dtype c_div(dtype x, dtype y) {
108
+ dtype z;
109
+ rtype s,yr,yi;
110
+ s = r_hypot(REAL(y),IMAG(y));
111
+ yr = REAL(y)/s;
112
+ yi = IMAG(y)/s;
113
+ REAL(z) = (REAL(x)*yr+IMAG(x)*yi)/s;
114
+ IMAG(z) = (IMAG(x)*yr-REAL(x)*yi)/s;
115
+ return z;
116
+ }
117
+
118
+ static inline dtype c_div_r(dtype x, rtype y) {
119
+ dtype z;
120
+ REAL(z) = REAL(x)/y;
121
+ IMAG(z) = IMAG(x)/y;
122
+ return z;
123
+ }
124
+
125
+ static inline dtype c_reciprocal(dtype x) {
126
+ dtype z;
127
+ if ( r_abs(REAL(x)) > r_abs(IMAG(x)) ) {
128
+ IMAG(z) = IMAG(x)/REAL(x);
129
+ REAL(z) = (1+IMAG(z)*IMAG(z))*REAL(x);
130
+ IMAG(z) /= -REAL(z);
131
+ REAL(z) = 1/REAL(z);
132
+ } else {
133
+ REAL(z) = REAL(x)/IMAG(x);
134
+ IMAG(z) = (1+REAL(z)*REAL(z))*IMAG(x);
135
+ REAL(z) /= IMAG(z);
136
+ IMAG(z) = -1/IMAG(z);
137
+ }
138
+ return z;
139
+ }
140
+
141
+ static inline dtype c_square(dtype x) {
142
+ dtype z;
143
+ REAL(z) = REAL(x)*REAL(x)-IMAG(x)*IMAG(x);
144
+ IMAG(z) = 2*REAL(x)*IMAG(x);
145
+ return z;
146
+ }
147
+
148
+ static inline dtype c_sqrt(dtype x) {
149
+ dtype z;
150
+ rtype xr, xi, r;
151
+ xr = REAL(x)/2;
152
+ xi = IMAG(x)/2;
153
+ r = r_hypot(xr,xi);
154
+ if (xr>0) {
155
+ REAL(z) = sqrt(r+xr);
156
+ IMAG(z) = xi/REAL(z);
157
+ } else if ( (r-=xr)!=0 ) {
158
+ IMAG(z) = (xi>=0) ? sqrt(r):-sqrt(r);
159
+ REAL(z) = xi/IMAG(z);
160
+ } else {
161
+ REAL(z) = IMAG(z) = 0;
162
+ }
163
+ return z;
164
+ }
165
+
166
+ static inline dtype c_log(dtype x) {
167
+ dtype z;
168
+ REAL(z) = r_log(r_hypot(REAL(x),IMAG(x)));
169
+ IMAG(z) = r_atan2(IMAG(x),REAL(x));
170
+ return z;
171
+ }
172
+
173
+ static inline dtype c_log2(dtype x) {
174
+ dtype z;
175
+ z = c_log(x);
176
+ z = c_mul_r(x,M_LOG2E);
177
+ return z;
178
+ }
179
+
180
+ static inline dtype c_log10(dtype x) {
181
+ dtype z;
182
+ z = c_log(x);
183
+ z = c_mul_r(x,M_LOG10E);
184
+ return z;
185
+ }
186
+
187
+ static inline dtype c_exp(dtype x) {
188
+ dtype z;
189
+ rtype a = r_exp(REAL(x));
190
+ REAL(z) = a*r_cos(IMAG(x));
191
+ IMAG(z) = a*r_sin(IMAG(x));
192
+ return z;
193
+ }
194
+
195
+ static inline dtype c_exp2(dtype x) {
196
+ dtype z;
197
+ rtype a = r_exp(REAL(x)*M_LN2);
198
+ REAL(z) = a*r_cos(IMAG(x));
199
+ IMAG(z) = a*r_sin(IMAG(x));
200
+ return z;
201
+ }
202
+
203
+ static inline dtype c_exp10(dtype x) {
204
+ dtype z;
205
+ rtype a = r_exp(REAL(x)*M_LN10);
206
+ REAL(z) = a*r_cos(IMAG(x));
207
+ IMAG(z) = a*r_sin(IMAG(x));
208
+ return z;
209
+ }
210
+
211
+ static inline dtype c_sin(dtype x) {
212
+ dtype z;
213
+ REAL(z) = r_sin(REAL(x))*r_cosh(IMAG(x));
214
+ IMAG(z) = r_cos(REAL(x))*r_sinh(IMAG(x));
215
+ return z;
216
+ }
217
+
218
+ static inline dtype c_sinh(dtype x) {
219
+ dtype z;
220
+ REAL(z) = r_sinh(REAL(x))*r_cos(IMAG(x));
221
+ IMAG(z) = r_cosh(REAL(x))*r_sin(IMAG(x));
222
+ return z;
223
+ }
224
+
225
+ static inline dtype c_cos(dtype x) {
226
+ dtype z;
227
+ REAL(z) = r_cos(REAL(x))*r_cosh(IMAG(x));
228
+ IMAG(z) = -r_sin(REAL(x))*r_sinh(IMAG(x));
229
+ return z;
230
+ }
231
+
232
+ static inline dtype c_cosh(dtype x) {
233
+ dtype z;
234
+ REAL(z) = r_cosh(REAL(x))*r_cos(IMAG(x));
235
+ IMAG(z) = r_sinh(REAL(x))*r_sin(IMAG(x));
236
+ return z;
237
+ }
238
+
239
+ static inline dtype c_tan(dtype x) {
240
+ dtype z;
241
+ rtype c, d;
242
+ if (r_abs(IMAG(x))<1) {
243
+ c = r_cos(REAL(x));
244
+ d = r_sinh(IMAG(x));
245
+ d = c*c + d*d;
246
+ REAL(z) = 0.5*r_sin(2*REAL(x))/d;
247
+ IMAG(z) = 0.5*r_sinh(2*IMAG(x))/d;
248
+ } else {
249
+ d = r_exp(-IMAG(x));
250
+ c = 2*d/(1-d*d);
251
+ c = c*c;
252
+ d = r_cos(REAL(x));
253
+ d = 1.0 + d*d*c;
254
+ REAL(z) = 0.5*r_sin(2*REAL(x))*c/d;
255
+ IMAG(z) = 1/r_tanh(IMAG(x))/d;
256
+ }
257
+ return z;
258
+ }
259
+
260
+ static inline dtype c_tanh(dtype x) {
261
+ dtype z;
262
+ rtype c, d, s;
263
+ c = r_cos(IMAG(x));
264
+ s = r_sinh(REAL(x));
265
+ d = c*c + s*s;
266
+ if (r_abs(REAL(x))<1) {
267
+ REAL(z) = s*r_cosh(REAL(x))/d;
268
+ IMAG(z) = 0.5*r_sin(2*IMAG(x))/d;
269
+ } else {
270
+ c = c / s;
271
+ c = 1 + c*c;
272
+ REAL(z) = 1/(r_tanh(REAL(x))*c);
273
+ IMAG(z) = 0.5*r_sin(2*IMAG(x))/d;
274
+ }
275
+ return z;
276
+ }
277
+
278
+ static inline dtype c_asin(dtype x) {
279
+ dtype z, y;
280
+ y = c_square(x);
281
+ REAL(y) = 1-REAL(y);
282
+ IMAG(y) = -IMAG(y);
283
+ y = c_sqrt(y);
284
+ REAL(y) -= IMAG(x);
285
+ IMAG(y) += REAL(x);
286
+ y = c_log(y);
287
+ REAL(z) = IMAG(y);
288
+ IMAG(z) = -REAL(y);
289
+ return z;
290
+ }
291
+
292
+ static inline dtype c_asinh(dtype x) {
293
+ dtype z, y;
294
+ y = c_square(x);
295
+ REAL(y) += 1;
296
+ y = c_sqrt(y);
297
+ REAL(y) += REAL(x);
298
+ IMAG(y) += IMAG(x);
299
+ z = c_log(y);
300
+ return z;
301
+ }
302
+
303
+ static inline dtype c_acos(dtype x) {
304
+ dtype z, y;
305
+ y = c_square(x);
306
+ REAL(y) = 1-REAL(y);
307
+ IMAG(y) = -IMAG(y);
308
+ y = c_sqrt(y);
309
+ REAL(z) = REAL(x)-IMAG(y);
310
+ IMAG(z) = IMAG(x)+REAL(y);
311
+ y = c_log(z);
312
+ REAL(z) = IMAG(y);
313
+ IMAG(z) = -REAL(y);
314
+ return z;
315
+ }
316
+
317
+ static inline dtype c_acosh(dtype x) {
318
+ dtype z, y;
319
+ y = c_square(x);
320
+ REAL(y) -= 1;
321
+ y = c_sqrt(y);
322
+ REAL(y) += REAL(x);
323
+ IMAG(y) += IMAG(x);
324
+ z = c_log(y);
325
+ return z;
326
+ }
327
+
328
+ static inline dtype c_atan(dtype x) {
329
+ dtype z, y;
330
+ REAL(y) = -REAL(x);
331
+ IMAG(y) = 1-IMAG(x);
332
+ REAL(z) = REAL(x);
333
+ IMAG(z) = 1+IMAG(x);
334
+ y = c_div(z,y);
335
+ y = c_log(y);
336
+ REAL(z) = -IMAG(y)/2;
337
+ IMAG(z) = REAL(y)/2;
338
+ return z;
339
+ }
340
+
341
+ static inline dtype c_atanh(dtype x) {
342
+ dtype z, y;
343
+ REAL(y) = 1-REAL(x);
344
+ IMAG(y) = -IMAG(x);
345
+ REAL(z) = 1+REAL(x);
346
+ IMAG(z) = IMAG(x);
347
+ y = c_div(z,y);
348
+ y = c_log(y);
349
+ REAL(z) = REAL(y)/2;
350
+ IMAG(z) = IMAG(y)/2;
351
+ return z;
352
+ }
353
+
354
+ static inline dtype c_pow(dtype x, dtype y)
355
+ {
356
+ dtype z;
357
+ if (c_is_zero(y)) {
358
+ z = c_one();
359
+ } else if (c_is_zero(x) && REAL(y)>0 && IMAG(y)==0) {
360
+ z = c_zero();
361
+ } else {
362
+ z = c_log(x);
363
+ z = c_mul(y,z);
364
+ z = c_exp(z);
365
+ }
366
+ return z;
367
+ }
368
+
369
+ static inline dtype c_pow_int(dtype x, int p)
370
+ {
371
+ dtype z = c_one();
372
+ if (p<0) {
373
+ x = c_pow_int(x,-p);
374
+ return c_reciprocal(x);
375
+ }
376
+ if (p==2) {return c_square(x);}
377
+ if (p&1) {z = x;}
378
+ p >>= 1;
379
+ while (p) {
380
+ x = c_square(x);
381
+ if (p&1) z = c_mul(z,x);
382
+ p >>= 1;
383
+ }
384
+ return z;
385
+ }
386
+
387
+ static inline dtype c_cbrt(dtype x) {
388
+ dtype z;
389
+ z = c_log(x);
390
+ z = c_div_r(z,3);
391
+ z = c_exp(z);
392
+ return z;
393
+ }
394
+
395
+ static inline rtype c_abs(dtype x) {
396
+ return r_hypot(REAL(x),IMAG(x));
397
+ }
398
+
399
+ static inline rtype c_abs_square(dtype x) {
400
+ return REAL(x)*REAL(x)+IMAG(x)*IMAG(x);
401
+ }
402
+
403
+
404
+
405
+ /*
406
+ static inline rtype c_hypot(dtype x, dtype y) {
407
+ return r_hypot(c_abs(x),c_abs(y));
408
+ }
409
+ */