numo-narray 0.9.0.4 → 0.9.0.5

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 (135) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +11 -8
  3. data/Rakefile +9 -0
  4. data/ext/numo/narray/array.c +178 -47
  5. data/ext/numo/narray/data.c +105 -97
  6. data/ext/numo/narray/depend.erb +6 -7
  7. data/ext/numo/narray/gen/cogen.rb +30 -7
  8. data/ext/numo/narray/gen/def/bit.rb +17 -14
  9. data/ext/numo/narray/gen/def/dcomplex.rb +19 -15
  10. data/ext/numo/narray/gen/def/dfloat.rb +17 -13
  11. data/ext/numo/narray/gen/def/int16.rb +16 -12
  12. data/ext/numo/narray/gen/def/int32.rb +16 -12
  13. data/ext/numo/narray/gen/def/int64.rb +16 -12
  14. data/ext/numo/narray/gen/def/int8.rb +16 -12
  15. data/ext/numo/narray/gen/def/robject.rb +18 -14
  16. data/ext/numo/narray/gen/def/scomplex.rb +19 -15
  17. data/ext/numo/narray/gen/def/sfloat.rb +17 -13
  18. data/ext/numo/narray/gen/def/uint16.rb +16 -12
  19. data/ext/numo/narray/gen/def/uint32.rb +16 -12
  20. data/ext/numo/narray/gen/def/uint64.rb +16 -12
  21. data/ext/numo/narray/gen/def/uint8.rb +16 -12
  22. data/ext/numo/narray/gen/erbpp2.rb +324 -0
  23. data/ext/numo/narray/gen/narray_def.rb +252 -0
  24. data/ext/numo/narray/gen/spec.rb +141 -71
  25. data/ext/numo/narray/gen/tmpl/accum.c +22 -9
  26. data/ext/numo/narray/gen/tmpl/accum_binary.c +29 -13
  27. data/ext/numo/narray/gen/tmpl/accum_index.c +38 -16
  28. data/ext/numo/narray/gen/tmpl/alloc_func.c +107 -0
  29. data/ext/numo/narray/gen/tmpl/allocate.c +1 -1
  30. data/ext/numo/narray/gen/tmpl/aref.c +1 -1
  31. data/ext/numo/narray/gen/tmpl/aset.c +4 -2
  32. data/ext/numo/narray/gen/tmpl/binary.c +4 -4
  33. data/ext/numo/narray/gen/tmpl/binary2.c +5 -5
  34. data/ext/numo/narray/gen/tmpl/binary_s.c +5 -5
  35. data/ext/numo/narray/gen/tmpl/bincount.c +4 -4
  36. data/ext/numo/narray/gen/tmpl/cast.c +9 -6
  37. data/ext/numo/narray/gen/tmpl/cast_array.c +4 -9
  38. data/ext/numo/narray/gen/tmpl/class.c +9 -0
  39. data/ext/numo/narray/gen/tmpl/clip.c +118 -0
  40. data/ext/numo/narray/gen/tmpl/coerce_cast.c +4 -2
  41. data/ext/numo/narray/gen/tmpl/cond_binary.c +5 -5
  42. data/ext/numo/narray/gen/tmpl/cond_unary.c +6 -6
  43. data/ext/numo/narray/gen/tmpl/cum.c +18 -9
  44. data/ext/numo/narray/gen/tmpl/each.c +2 -2
  45. data/ext/numo/narray/gen/tmpl/each_with_index.c +2 -2
  46. data/ext/numo/narray/gen/tmpl/extract.c +2 -2
  47. data/ext/numo/narray/gen/tmpl/extract_data.c +48 -0
  48. data/ext/numo/narray/gen/tmpl/eye.c +3 -3
  49. data/ext/numo/narray/gen/tmpl/fill.c +2 -2
  50. data/ext/numo/narray/gen/tmpl/format.c +5 -5
  51. data/ext/numo/narray/gen/tmpl/format_to_a.c +4 -4
  52. data/ext/numo/narray/gen/tmpl/frexp.c +37 -0
  53. data/ext/numo/narray/gen/tmpl/init_class.c +20 -0
  54. data/ext/numo/narray/gen/tmpl/init_module.c +12 -0
  55. data/ext/numo/narray/gen/tmpl/inspect.c +2 -2
  56. data/ext/numo/narray/gen/tmpl/lib.c +45 -0
  57. data/ext/numo/narray/gen/tmpl/logseq.c +1 -1
  58. data/ext/numo/narray/gen/tmpl/map_with_index.c +2 -2
  59. data/ext/numo/narray/gen/tmpl/median.c +31 -8
  60. data/ext/numo/narray/gen/tmpl/minmax.c +24 -24
  61. data/ext/numo/narray/gen/tmpl/module.c +9 -0
  62. data/ext/numo/narray/gen/tmpl/new_dim0.c +12 -0
  63. data/ext/numo/narray/gen/tmpl/poly.c +3 -3
  64. data/ext/numo/narray/gen/tmpl/pow.c +1 -1
  65. data/ext/numo/narray/gen/tmpl/powint.c +1 -1
  66. data/ext/numo/narray/gen/tmpl/qsort.c +10 -3
  67. data/ext/numo/narray/gen/tmpl/rand.c +1 -1
  68. data/ext/numo/narray/gen/tmpl/rand_norm.c +1 -1
  69. data/ext/numo/narray/gen/tmpl/seq.c +1 -1
  70. data/ext/numo/narray/gen/tmpl/set2.c +5 -5
  71. data/ext/numo/narray/gen/tmpl/sort.c +29 -14
  72. data/ext/numo/narray/gen/tmpl/sort_index.c +41 -20
  73. data/ext/numo/narray/gen/tmpl/store.c +11 -5
  74. data/ext/numo/narray/gen/tmpl/store_array.c +1 -1
  75. data/ext/numo/narray/gen/tmpl/store_bit.c +1 -1
  76. data/ext/numo/narray/gen/tmpl/store_from.c +1 -1
  77. data/ext/numo/narray/gen/tmpl/store_numeric.c +3 -16
  78. data/ext/numo/narray/gen/tmpl/to_a.c +2 -2
  79. data/ext/numo/narray/gen/tmpl/unary.c +7 -7
  80. data/ext/numo/narray/gen/tmpl/unary2.c +8 -8
  81. data/ext/numo/narray/gen/tmpl/unary_ret2.c +33 -0
  82. data/ext/numo/narray/gen/tmpl/unary_s.c +8 -8
  83. data/ext/numo/narray/gen/tmpl_bit/allocate.c +1 -5
  84. data/ext/numo/narray/gen/tmpl_bit/aref.c +1 -1
  85. data/ext/numo/narray/gen/tmpl_bit/aset.c +2 -2
  86. data/ext/numo/narray/gen/tmpl_bit/binary.c +8 -8
  87. data/ext/numo/narray/gen/tmpl_bit/bit_count.c +8 -8
  88. data/ext/numo/narray/gen/tmpl_bit/bit_reduce.c +6 -6
  89. data/ext/numo/narray/gen/tmpl_bit/each.c +2 -2
  90. data/ext/numo/narray/gen/tmpl_bit/each_with_index.c +2 -2
  91. data/ext/numo/narray/gen/tmpl_bit/extract.c +1 -1
  92. data/ext/numo/narray/gen/tmpl_bit/fill.c +2 -2
  93. data/ext/numo/narray/gen/tmpl_bit/format.c +5 -5
  94. data/ext/numo/narray/gen/tmpl_bit/format_to_a.c +2 -2
  95. data/ext/numo/narray/gen/tmpl_bit/inspect.c +2 -2
  96. data/ext/numo/narray/gen/tmpl_bit/mask.c +5 -5
  97. data/ext/numo/narray/gen/tmpl_bit/none_p.c +4 -4
  98. data/ext/numo/narray/gen/tmpl_bit/store_array.c +2 -2
  99. data/ext/numo/narray/gen/tmpl_bit/store_bit.c +1 -1
  100. data/ext/numo/narray/gen/tmpl_bit/store_from.c +1 -1
  101. data/ext/numo/narray/gen/tmpl_bit/to_a.c +2 -2
  102. data/ext/numo/narray/gen/tmpl_bit/unary.c +9 -9
  103. data/ext/numo/narray/gen/tmpl_bit/where.c +6 -6
  104. data/ext/numo/narray/gen/tmpl_bit/where2.c +8 -8
  105. data/ext/numo/narray/index.c +46 -30
  106. data/ext/numo/narray/math.c +12 -6
  107. data/ext/numo/narray/narray.c +242 -218
  108. data/ext/numo/narray/ndloop.c +17 -24
  109. data/ext/numo/narray/numo/intern.h +63 -67
  110. data/ext/numo/narray/numo/narray.h +38 -13
  111. data/ext/numo/narray/numo/ndloop.h +1 -1
  112. data/ext/numo/narray/numo/template.h +1 -1
  113. data/ext/numo/narray/numo/types/complex.h +8 -4
  114. data/ext/numo/narray/numo/types/complex_macro.h +118 -1
  115. data/ext/numo/narray/numo/types/float_macro.h +283 -6
  116. data/ext/numo/narray/numo/types/robj_macro.h +261 -9
  117. data/ext/numo/narray/numo/types/xint_macro.h +35 -0
  118. data/ext/numo/narray/struct.c +34 -15
  119. data/lib/erbpp.rb +5 -1
  120. data/lib/erbpp/line_number.rb +10 -3
  121. data/lib/erbpp/narray_def.rb +55 -25
  122. data/lib/numo/narray/extra.rb +638 -219
  123. data/numo-narray.gemspec +1 -0
  124. data/spec/narray_spec.rb +2 -2
  125. metadata +17 -14
  126. data/ext/numo/narray/gen/dtype.erb.c +0 -82
  127. data/ext/numo/narray/gen/tmpl/cast_numeric.c +0 -22
  128. data/ext/numo/narray/gen/tmpl/robj_allocate.c +0 -32
  129. data/ext/numo/narray/gen/tmpl_bit/cast.c +0 -37
  130. data/ext/numo/narray/gen/tmpl_bit/cast_array.c +0 -18
  131. data/ext/numo/narray/gen/tmpl_bit/cast_numeric.c +0 -22
  132. data/ext/numo/narray/gen/tmpl_bit/coerce_cast.c +0 -8
  133. data/ext/numo/narray/gen/tmpl_bit/map_with_index.c +0 -94
  134. data/ext/numo/narray/gen/tmpl_bit/store.c +0 -32
  135. data/ext/numo/narray/gen/tmpl_bit/store_numeric.c +0 -22
@@ -1,16 +1,20 @@
1
- class_name "UInt32"
2
- ctype "u_int32_t"
1
+ set name: "uint32"
2
+ set type_name: "uint32"
3
+ set full_class_name: "Numo::UInt32"
4
+ set class_name: "UInt32"
5
+ set class_var: "cT"
6
+ set ctype: "u_int32_t"
3
7
 
4
- has_math false
5
- is_bit false
6
- is_int true
7
- is_unsigned true
8
- is_float false
9
- is_complex false
10
- is_object false
11
- is_real true
12
- is_comparable true
13
- is_double_precision false
8
+ set has_math: false
9
+ set is_bit: false
10
+ set is_int: true
11
+ set is_unsigned: true
12
+ set is_float: false
13
+ set is_complex: false
14
+ set is_object: false
15
+ set is_real: true
16
+ set is_comparable: true
17
+ set is_double_precision: false
14
18
 
15
19
  upcast_rb "Integer"
16
20
  upcast_rb "Float", "DFloat"
@@ -1,16 +1,20 @@
1
- class_name "UInt64"
2
- ctype "u_int64_t"
1
+ set name: "uint64"
2
+ set type_name: "uint64"
3
+ set full_class_name: "Numo::UInt64"
4
+ set class_name: "UInt64"
5
+ set class_var: "cT"
6
+ set ctype: "u_int64_t"
3
7
 
4
- has_math false
5
- is_bit false
6
- is_int true
7
- is_unsigned true
8
- is_float false
9
- is_complex false
10
- is_object false
11
- is_real true
12
- is_comparable true
13
- is_double_precision false
8
+ set has_math: false
9
+ set is_bit: false
10
+ set is_int: true
11
+ set is_unsigned: true
12
+ set is_float: false
13
+ set is_complex: false
14
+ set is_object: false
15
+ set is_real: true
16
+ set is_comparable: true
17
+ set is_double_precision: false
14
18
 
15
19
  upcast_rb "Integer"
16
20
  upcast_rb "Float", "DFloat"
@@ -1,16 +1,20 @@
1
- class_name "UInt8"
2
- ctype "u_int8_t"
1
+ set name: "uint8"
2
+ set type_name: "uint8"
3
+ set full_class_name: "Numo::UInt8"
4
+ set class_name: "UInt8"
5
+ set class_var: "cT"
6
+ set ctype: "u_int8_t"
3
7
 
4
- has_math false
5
- is_bit false
6
- is_int true
7
- is_unsigned true
8
- is_float false
9
- is_complex false
10
- is_object false
11
- is_real true
12
- is_comparable true
13
- is_double_precision false
8
+ set has_math: false
9
+ set is_bit: false
10
+ set is_int: true
11
+ set is_unsigned: true
12
+ set is_float: false
13
+ set is_complex: false
14
+ set is_object: false
15
+ set is_real: true
16
+ set is_comparable: true
17
+ set is_double_precision: false
14
18
 
15
19
  upcast_rb "Integer"
16
20
  upcast_rb "Float", "DFloat"
@@ -0,0 +1,324 @@
1
+ require "erb"
2
+
3
+ class ErbPP
4
+
5
+ def initialize(parent=nil, erb_base=nil, **opts, &block)
6
+ @parent = parent
7
+ @children = []
8
+ @opts = opts
9
+ set erb_base: erb_base if erb_base
10
+ @parent.add_child(self) if @parent
11
+ instance_eval(&block) if block
12
+ end
13
+
14
+ attr_reader :children
15
+ attr_accessor :parent
16
+
17
+ def add_child(child)
18
+ @children.push(child)
19
+ end
20
+
21
+ def set(**opts)
22
+ @opts.merge!(opts)
23
+ end
24
+
25
+ def get(key, *args, &block)
26
+ if respond_to?(key)
27
+ return send(key, *args, &block)
28
+ end
29
+ if args.empty? && block.nil? && @opts.has_key?(key)
30
+ return @opts[key]
31
+ end
32
+ if @parent
33
+ return @parent.get(key, *args, &block)
34
+ end
35
+ nil
36
+ end
37
+
38
+ def description
39
+ if s = @opts[:description] || @opts[:desc]
40
+ s.gsub(/\@\{/,"[").gsub(/\@\}/,"]")
41
+ end
42
+ end
43
+
44
+ alias desc description
45
+
46
+ alias method_missing_alias method_missing
47
+
48
+ def method_missing(_meth_id, *args, &block)
49
+ if args.empty?
50
+ #$stderr.puts _meth_id.inspect
51
+ v = get(_meth_id, *args, &block)
52
+ return v if !v.nil?
53
+ end
54
+ method_missing_alias(_meth_id, *args, &block)
55
+ end
56
+
57
+ # ERB Loader
58
+
59
+ def load_erb(base_name)
60
+ safe_level = nil
61
+ trim_mode = '%<>'
62
+ file = base_name + get(:erb_suffix)
63
+ dirs = get(:erb_dir)
64
+ dirs = [dirs] if !dirs.kind_of?(Array)
65
+ dirs.each do |x|
66
+ Dir.glob(x).each do |dir|
67
+ path = File.join(dir,file)
68
+ if File.exist?(path)
69
+ erb = ERB.new(File.read(path), safe_level, trim_mode)
70
+ erb.filename = path
71
+ return erb
72
+ end
73
+ end
74
+ end
75
+ raise "file not found: #{file.inspect} in #{dirs.inspect}"
76
+ end
77
+
78
+ def run
79
+ if base = @opts[:erb_base]
80
+ load_erb(base).run(binding)
81
+ end
82
+ end
83
+
84
+ def result
85
+ if base = @opts[:erb_base]
86
+ load_erb(base).result(binding)
87
+ end
88
+ end
89
+
90
+ def write(output)
91
+ File.open(output,"wt") do |f|
92
+ f.print(result)
93
+ end
94
+ end
95
+
96
+ def init_def
97
+ end
98
+
99
+ def find_tmpl(name)
100
+ @parent.children.find{|x| x.name == name }
101
+ end
102
+
103
+ def find(name)
104
+ children.find{|x| x.name == name }
105
+ end
106
+ end
107
+
108
+
109
+ class DefLib < ErbPP
110
+ def initialize(parent=nil, **opts, &block)
111
+ opts[:erb_base] ||= 'lib'
112
+ super(parent, **opts, &block)
113
+ end
114
+ def id_assign
115
+ ids = []
116
+ @children.each{|c| a=c.get(:id_list); ids.concat(a) if a}
117
+ ids.sort.uniq.map{|x| "id_#{x[1]} = rb_intern(\"#{x[0]}\");"}
118
+ end
119
+ def id_decl
120
+ ids = []
121
+ @children.each{|c| a=c.get(:id_list); ids.concat(a) if a}
122
+ ids.sort.uniq.map{|x| "static ID id_#{x[1]};\n"}
123
+ end
124
+ def def_class(**opts, &block)
125
+ DefClass.new(self, **opts, &block)
126
+ end
127
+ def def_module(**opts, &block)
128
+ DefModule.new(self, **opts, &block)
129
+ end
130
+ end
131
+
132
+ module DeclMethod
133
+ def def_alloc_func(m, erb_path=nil, **opts, &block)
134
+ DefAllocFunc.new(self, erb_path||m, name:m, singleton:true, **opts, &block)
135
+ end
136
+ def undef_alloc_func
137
+ UndefAllocFunc.new(self)
138
+ end
139
+ def def_method(m, erb_path=nil, **opts, &block)
140
+ DefMethod.new(self, erb_path||m, name:m, **opts, &block)
141
+ end
142
+ def undef_method(m)
143
+ UndefMethod.new(self,name:m)
144
+ end
145
+ def def_singleton_method(m, erb_path=nil, **opts, &block)
146
+ DefMethod.new(self, erb_path||m, name:m, singleton:true, **opts, &block)
147
+ end
148
+ def undef_singleton_method(m)
149
+ UndefSingletonMethod.new(self,name:m)
150
+ end
151
+ def def_module_function(m, erb_path=nil, **opts, &block)
152
+ DefModuleFunction.new(self, erb_path||m, name:m, **opts, &block)
153
+ end
154
+ def def_alias(from, to)
155
+ DefAlias.new(self, from:from, to:to)
156
+ end
157
+ def def_const(m, v, **opts, &block)
158
+ DefConst.new(self, name:m, value:v, **opts, &block)
159
+ end
160
+ end
161
+
162
+ class DefModule < ErbPP
163
+ include DeclMethod
164
+ def initialize(parent, **opts, &block)
165
+ eb = opts[:erb_base] || 'module'
166
+ super(parent, erb_base:eb, **opts, &block)
167
+ end
168
+ def id_list
169
+ @id_list ||= []
170
+ end
171
+ def def_id(name,var=nil)
172
+ var = name.gsub(/\?/,"_p").gsub(/\!/,"_bang") if var.nil?
173
+ id_list << [name,var]
174
+ end
175
+ def init_def
176
+ load_erb(init_erb).result(binding)
177
+ end
178
+ def init_erb
179
+ @opts[:init_erb] || "init_module"
180
+ end
181
+ def method_code
182
+ @children.map{|c| c.result}.join("\n")
183
+ end
184
+ def _mod_var
185
+ @opts[:module_var]
186
+ end
187
+ end
188
+
189
+ class DefClass < DefModule
190
+ def initialize(parent, **opts, &block)
191
+ eb = opts[:erb_base] || 'class'
192
+ super(parent, erb_base:eb, **opts, &block)
193
+ end
194
+ def _mod_var
195
+ @opts[:class_var]
196
+ end
197
+ def init_erb
198
+ @opts[:init_erb] || "init_class"
199
+ end
200
+ def super_class
201
+ @opts[:super_class] || "rb_cObject"
202
+ end
203
+ def free_func
204
+ @opts[:free_func] || "gsl_"+get(:name)+"_free"
205
+ end
206
+ end
207
+
208
+ class DefMethod < ErbPP
209
+ include DeclMethod
210
+
211
+ def initialize(parent, erb_base, **opts, &block)
212
+ super(parent, **opts, &block)
213
+ set erb_base: erb_base
214
+ end
215
+
216
+ def id_op
217
+ if op.size == 1
218
+ "'#{op}'"
219
+ else
220
+ "id_#{c_name}"
221
+ end
222
+ end
223
+
224
+ def c_name
225
+ @opts[:name].gsub(/\?/,"_p").gsub(/\!/,"_bang")
226
+ end
227
+
228
+ def op_map
229
+ @opts[:op] || @opts[:name]
230
+ end
231
+
232
+ def c_func(n_arg=nil)
233
+ set n_arg: n_arg if n_arg
234
+ s = (singleton) ? "_s" : ""
235
+ "#{@parent.name}#{s}_#{c_name}"
236
+ end
237
+
238
+ def c_iter
239
+ "iter_#{c_func}"
240
+ end
241
+
242
+ def define_method_args
243
+ "#{_mod_var}, \"#{op_map}\", #{c_func}, #{n_arg}"
244
+ end
245
+
246
+ def init_def
247
+ return if n_arg == :nodef
248
+ s = (singleton) ? "_singleton" : ""
249
+ "rb_define#{s}_method(#{define_method_args});"
250
+ end
251
+
252
+ def singleton
253
+ @opts[:singleton]
254
+ end
255
+ end
256
+
257
+ class DefModuleFunction < DefMethod
258
+ def initialize(parent, erb_base, **opts, &block)
259
+ super(parent, erb_base, **opts, &block)
260
+ set singleton: true
261
+ end
262
+
263
+ def init_def
264
+ return if n_arg == :nodef
265
+ "rb_define_module_function(#{define_method_args});"
266
+ end
267
+ end
268
+
269
+ class DefAlias < ErbPP
270
+ def init_def
271
+ "rb_define_alias(#{_mod_var}, \"#{from}\", \"#{to}\");"
272
+ end
273
+ end
274
+
275
+ class DefAllocFunc < DefMethod
276
+ def init_def
277
+ "rb_define_alloc_func(#{_mod_var}, #{c_func});"
278
+ end
279
+ end
280
+
281
+ class UndefAllocFunc < ErbPP
282
+ def init_def
283
+ "rb_undef_alloc_func(#{_mod_var});"
284
+ end
285
+ end
286
+
287
+ class UndefMethod < ErbPP
288
+ def init_def
289
+ "rb_undef_method(#{_mod_var},\"#{name}\");"
290
+ end
291
+ end
292
+
293
+ class UndefSingletonMethod < ErbPP
294
+ def init_def
295
+ "rb_undef_method(rb_singleton_class(#{_mod_var}),\"#{name}\");"
296
+ end
297
+ end
298
+
299
+ class DefConst < ErbPP
300
+ def init_def
301
+ "/*#{desc}*/
302
+ rb_define_const(#{_mod_var},\"#{name}\",#{value});"
303
+ end
304
+ end
305
+
306
+ class DefStruct < ErbPP
307
+ def method_code
308
+ "static VALUE #{class_var};"
309
+ end
310
+ def init_def
311
+ items = members.map{|s| "\"#{s}\""}.join(",")
312
+ "/*#{description}*/
313
+ #{class_var} = rb_struct_define(\"#{class_name}\",#{items},NULL);"
314
+ end
315
+ end
316
+
317
+ class DefInclueModule < ErbPP
318
+ def initialize(parent=nil, incl_class, incl_module, **opts, &block)
319
+ super(parent,incl_class:incl_class,incl_module:incl_module,**opts,&block)
320
+ end
321
+ def init_def
322
+ "rb_include_module(#{get(:incl_class)}, #{get(:incl_module)});"
323
+ end
324
+ end
@@ -0,0 +1,252 @@
1
+ require_relative './erbpp2'
2
+
3
+ module NArrayMethod
4
+
5
+ def binary(meth, ope=nil)
6
+ ope = meth if ope.nil?
7
+ def_method(meth, "binary", op:ope)
8
+ end
9
+
10
+ def binary2(meth, ope=nil)
11
+ ope = meth if ope.nil?
12
+ def_method(meth, "binary2", op:ope)
13
+ end
14
+
15
+ def unary(meth, ope=nil)
16
+ def_method(meth, "unary", op:ope)
17
+ end
18
+
19
+ def pow
20
+ def_method("pow", "pow", op:"**")
21
+ end
22
+
23
+ def unary2(meth, dtype, tpclass)
24
+ h = {dtype:dtype, tpclass:tpclass}
25
+ def_method(meth, "unary2", **h)
26
+ end
27
+
28
+ def set2(meth, dtype, tpclass)
29
+ h = {dtype:dtype, tpclass:tpclass}
30
+ def_method(meth, "set2", h)
31
+ end
32
+
33
+ def cond_binary(meth,op=nil)
34
+ op = meth unless op
35
+ def_method(meth, "cond_binary", op:op)
36
+ end
37
+
38
+ def cond_unary(meth)
39
+ def_method(meth, "cond_unary")
40
+ end
41
+
42
+ def bit_count(meth)
43
+ def_method(meth, "bit_count")
44
+ end
45
+
46
+ def bit_reduce(meth, init_bit)
47
+ h = {init_bit:init_bit}
48
+ def_method(meth, "bit_reduce", **h)
49
+ end
50
+
51
+ def accum(meth, dtype, tpclass)
52
+ h = {dtype:dtype, tpclass:tpclass}
53
+ def_method(meth, "accum", **h)
54
+ end
55
+
56
+ def accum_index(meth)
57
+ def_method(meth, "accum_index")
58
+ end
59
+
60
+ def cum(meth, cmacro)
61
+ def_method(meth, "cum", cmacro:cmacro)
62
+ end
63
+
64
+ def accum_binary(meth, ope=nil)
65
+ ope = meth if ope.nil?
66
+ def_method(meth, "accum_binary", op:ope)
67
+ end
68
+
69
+ def qsort(type_name, dtype, dcast, suffix="")
70
+ h = {type_name:type_name, dtype:dtype, dcast:dcast, suffix:suffix}
71
+ def_method("qsort", **h)
72
+ end
73
+ end
74
+
75
+ module NMathMethod
76
+
77
+ def math(meth, n=1, tmpl=nil, **h)
78
+ if tmpl.nil?
79
+ case n
80
+ when 1
81
+ tmpl = "unary_s"
82
+ when 2
83
+ tmpl = "binary_s"
84
+ when 3
85
+ tmpl = "ternary_s"
86
+ else
87
+ raise "invalid n=#{n}"
88
+ end
89
+ end
90
+ def_module_function(meth, tmpl, **h)
91
+ end
92
+ end
93
+
94
+ # ----------------------------------------------------------------------
95
+
96
+ module NArrayType
97
+
98
+ def type_name
99
+ @opts[:type_name] ||= class_name.downcase
100
+ end
101
+ alias tp type_name
102
+
103
+ def type_var
104
+ @opts[:type_var] ||= "numo_c"+class_name
105
+ end
106
+
107
+ def math_var
108
+ @opts[:math_var] ||= "numo_m"+class_name+"Math"
109
+ end
110
+
111
+ def real_class_name(arg=nil)
112
+ if arg.nil?
113
+ @opts[:real_class_name] ||= class_name
114
+ else
115
+ @opts[:real_class_name] = arg
116
+ end
117
+ end
118
+
119
+ def real_ctype(arg=nil)
120
+ if arg.nil?
121
+ @opts[:real_ctype] ||= ctype
122
+ else
123
+ @opts[:real_ctype] = arg
124
+ end
125
+ end
126
+
127
+ def real_type_var
128
+ @opts[:real_type_var] ||= "numo_c"+real_class_name
129
+ end
130
+
131
+ def real_type_name
132
+ @opts[:real_type_name] ||= real_class_name.downcase
133
+ end
134
+
135
+ def class_alias(*args)
136
+ case a = @opts[:class_alias]
137
+ when Array
138
+ when nil
139
+ a = @opts[:class_alias] = []
140
+ else
141
+ a = @opts[:class_alias] = [a]
142
+ end
143
+ a.concat(args)
144
+ end
145
+
146
+ def upcast(c=nil,t=nil)
147
+ @opts[:upcast] ||= []
148
+ if c
149
+ if t
150
+ t = "numo_c#{t}"
151
+ else
152
+ t = "cT"
153
+ end
154
+ @opts[:upcast] << "rb_hash_aset(hCast, numo_c#{c}, #{t});"
155
+ else
156
+ @opts[:upcast]
157
+ end
158
+ end
159
+
160
+ def upcast_rb(c,t=nil)
161
+ @opts[:upcast] ||= []
162
+ if t
163
+ t = "numo_c#{t}"
164
+ else
165
+ t = "cT"
166
+ end
167
+ if c=="Integer"
168
+ @opts[:upcast] << "#ifdef RUBY_INTEGER_UNIFICATION"
169
+ @opts[:upcast] << "rb_hash_aset(hCast, rb_cInteger, #{t});"
170
+ @opts[:upcast] << "#else"
171
+ @opts[:upcast] << "rb_hash_aset(hCast, rb_cFixnum, #{t});"
172
+ @opts[:upcast] << "rb_hash_aset(hCast, rb_cBignum, #{t});"
173
+ @opts[:upcast] << "#endif"
174
+ else
175
+ @opts[:upcast] << "rb_hash_aset(hCast, rb_c#{c}, #{t});"
176
+ end
177
+ end
178
+ end
179
+
180
+ # ----------------------------------------------------------------------
181
+
182
+ module StoreFrom
183
+
184
+ def store_from(cname, dtype=nil, macro=nil)
185
+ tmpl = (cname=="Bit") ? "store_bit" : "store_from"
186
+ h = { name:cname.downcase,
187
+ type_name:cname,
188
+ type_var:"numo_c"+cname,
189
+ dtype:dtype,
190
+ macro:macro }
191
+ Store.new(self, tmpl, **h)
192
+ end
193
+
194
+ def store_numeric
195
+ StoreNum.new(self, "store_numeric", name:"numeric")
196
+ end
197
+
198
+ def store_array
199
+ StoreArray.new(self, "store_array", name:"array")
200
+ end
201
+
202
+ def definitions
203
+ a = []
204
+ @children.each do |x|
205
+ if x.condition("")
206
+ if x.get(:type_name) == parent.class_name
207
+ a.unshift(x)
208
+ else
209
+ a.push(x)
210
+ end
211
+ end
212
+ end
213
+ a
214
+ end
215
+ end
216
+
217
+ # ----------------------------------------------------------------------
218
+
219
+ class Store < DefMethod
220
+ def c_func(n=nil)
221
+ "#{parent.parent.name}_store_#{name}"
222
+ end
223
+
224
+ def condition(klass)
225
+ "#{klass}==#{type_var}"
226
+ end
227
+
228
+ def extract_data(ptr,pos,x)
229
+ case type_name
230
+ when "Bit"
231
+ "{BIT_DIGIT b; LOAD_BIT(#{ptr},#{pos},b); x = m_from_real(b);}"
232
+ when "RObject"
233
+ "#{x} = m_num_to_data(*(#{dtype}*)(#{ptr}+#{pos}))"
234
+ when /Complex/
235
+ "{#{dtype} *p = (#{dtype}*)(#{ptr}+#{pos}); #{x} = c_new(REAL(*p),IMAG(*p));}"
236
+ else
237
+ "#{x} = m_from_real(*(#{dtype}*)(#{ptr}+#{pos}))"
238
+ end
239
+ end
240
+ end
241
+
242
+ class StoreNum < Store
243
+ def condition(klass)
244
+ "IS_INTEGER_CLASS(#{klass}) || #{klass}==rb_cFloat || #{klass}==rb_cComplex"
245
+ end
246
+ end
247
+
248
+ class StoreArray < Store
249
+ def condition(klass)
250
+ "#{klass}==rb_cArray"
251
+ end
252
+ end