numo-narray 0.9.0.1-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (124) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +4 -0
  3. data/README.md +47 -0
  4. data/Rakefile +41 -0
  5. data/ext/numo/narray/SFMT-params.h +97 -0
  6. data/ext/numo/narray/SFMT-params19937.h +46 -0
  7. data/ext/numo/narray/SFMT.c +620 -0
  8. data/ext/numo/narray/SFMT.h +157 -0
  9. data/ext/numo/narray/array.c +525 -0
  10. data/ext/numo/narray/data.c +901 -0
  11. data/ext/numo/narray/depend.erb +33 -0
  12. data/ext/numo/narray/extconf.rb +117 -0
  13. data/ext/numo/narray/gen/bit.erb.c +811 -0
  14. data/ext/numo/narray/gen/cogen.rb +18 -0
  15. data/ext/numo/narray/gen/def/dcomplex.rb +32 -0
  16. data/ext/numo/narray/gen/def/dfloat.rb +30 -0
  17. data/ext/numo/narray/gen/def/int16.rb +29 -0
  18. data/ext/numo/narray/gen/def/int32.rb +29 -0
  19. data/ext/numo/narray/gen/def/int64.rb +29 -0
  20. data/ext/numo/narray/gen/def/int8.rb +29 -0
  21. data/ext/numo/narray/gen/def/robject.rb +30 -0
  22. data/ext/numo/narray/gen/def/scomplex.rb +32 -0
  23. data/ext/numo/narray/gen/def/sfloat.rb +30 -0
  24. data/ext/numo/narray/gen/def/uint16.rb +29 -0
  25. data/ext/numo/narray/gen/def/uint32.rb +29 -0
  26. data/ext/numo/narray/gen/def/uint64.rb +29 -0
  27. data/ext/numo/narray/gen/def/uint8.rb +29 -0
  28. data/ext/numo/narray/gen/dtype.erb.c +328 -0
  29. data/ext/numo/narray/gen/tmpl/accum.c +36 -0
  30. data/ext/numo/narray/gen/tmpl/accum_binary.c +75 -0
  31. data/ext/numo/narray/gen/tmpl/accum_index.c +58 -0
  32. data/ext/numo/narray/gen/tmpl/allocate.c +35 -0
  33. data/ext/numo/narray/gen/tmpl/aref.c +51 -0
  34. data/ext/numo/narray/gen/tmpl/aset.c +61 -0
  35. data/ext/numo/narray/gen/tmpl/binary.c +53 -0
  36. data/ext/numo/narray/gen/tmpl/binary2.c +55 -0
  37. data/ext/numo/narray/gen/tmpl/binary_s.c +34 -0
  38. data/ext/numo/narray/gen/tmpl/bit_binary.c +94 -0
  39. data/ext/numo/narray/gen/tmpl/bit_count.c +82 -0
  40. data/ext/numo/narray/gen/tmpl/bit_unary.c +77 -0
  41. data/ext/numo/narray/gen/tmpl/cast.c +37 -0
  42. data/ext/numo/narray/gen/tmpl/cast_array.c +79 -0
  43. data/ext/numo/narray/gen/tmpl/cast_numeric.c +22 -0
  44. data/ext/numo/narray/gen/tmpl/coerce_cast.c +8 -0
  45. data/ext/numo/narray/gen/tmpl/cond_binary.c +51 -0
  46. data/ext/numo/narray/gen/tmpl/cond_unary.c +45 -0
  47. data/ext/numo/narray/gen/tmpl/cum.c +42 -0
  48. data/ext/numo/narray/gen/tmpl/each.c +43 -0
  49. data/ext/numo/narray/gen/tmpl/each_with_index.c +64 -0
  50. data/ext/numo/narray/gen/tmpl/extract.c +23 -0
  51. data/ext/numo/narray/gen/tmpl/eye.c +91 -0
  52. data/ext/numo/narray/gen/tmpl/fill.c +38 -0
  53. data/ext/numo/narray/gen/tmpl/format.c +60 -0
  54. data/ext/numo/narray/gen/tmpl/format_to_a.c +47 -0
  55. data/ext/numo/narray/gen/tmpl/head.c +25 -0
  56. data/ext/numo/narray/gen/tmpl/inspect.c +16 -0
  57. data/ext/numo/narray/gen/tmpl/map_with_index.c +94 -0
  58. data/ext/numo/narray/gen/tmpl/median.c +44 -0
  59. data/ext/numo/narray/gen/tmpl/minmax.c +47 -0
  60. data/ext/numo/narray/gen/tmpl/poly.c +49 -0
  61. data/ext/numo/narray/gen/tmpl/pow.c +74 -0
  62. data/ext/numo/narray/gen/tmpl/powint.c +17 -0
  63. data/ext/numo/narray/gen/tmpl/qsort.c +149 -0
  64. data/ext/numo/narray/gen/tmpl/rand.c +33 -0
  65. data/ext/numo/narray/gen/tmpl/rand_norm.c +46 -0
  66. data/ext/numo/narray/gen/tmpl/robj_allocate.c +32 -0
  67. data/ext/numo/narray/gen/tmpl/seq.c +61 -0
  68. data/ext/numo/narray/gen/tmpl/set2.c +56 -0
  69. data/ext/numo/narray/gen/tmpl/sort.c +36 -0
  70. data/ext/numo/narray/gen/tmpl/sort_index.c +86 -0
  71. data/ext/numo/narray/gen/tmpl/store.c +31 -0
  72. data/ext/numo/narray/gen/tmpl/store_array.c +5 -0
  73. data/ext/numo/narray/gen/tmpl/store_from.c +53 -0
  74. data/ext/numo/narray/gen/tmpl/store_numeric.c +22 -0
  75. data/ext/numo/narray/gen/tmpl/to_a.c +41 -0
  76. data/ext/numo/narray/gen/tmpl/unary.c +58 -0
  77. data/ext/numo/narray/gen/tmpl/unary2.c +58 -0
  78. data/ext/numo/narray/gen/tmpl/unary_s.c +57 -0
  79. data/ext/numo/narray/index.c +822 -0
  80. data/ext/numo/narray/kwarg.c +79 -0
  81. data/ext/numo/narray/math.c +140 -0
  82. data/ext/numo/narray/narray.c +1539 -0
  83. data/ext/numo/narray/ndloop.c +1928 -0
  84. data/ext/numo/narray/numo/compat.h +23 -0
  85. data/ext/numo/narray/numo/intern.h +112 -0
  86. data/ext/numo/narray/numo/narray.h +411 -0
  87. data/ext/numo/narray/numo/ndloop.h +99 -0
  88. data/ext/numo/narray/numo/template.h +140 -0
  89. data/ext/numo/narray/numo/types/bit.h +19 -0
  90. data/ext/numo/narray/numo/types/complex.h +410 -0
  91. data/ext/numo/narray/numo/types/complex_macro.h +205 -0
  92. data/ext/numo/narray/numo/types/dcomplex.h +11 -0
  93. data/ext/numo/narray/numo/types/dfloat.h +12 -0
  94. data/ext/numo/narray/numo/types/float_def.h +34 -0
  95. data/ext/numo/narray/numo/types/float_macro.h +277 -0
  96. data/ext/numo/narray/numo/types/int16.h +12 -0
  97. data/ext/numo/narray/numo/types/int32.h +12 -0
  98. data/ext/numo/narray/numo/types/int64.h +12 -0
  99. data/ext/numo/narray/numo/types/int8.h +12 -0
  100. data/ext/numo/narray/numo/types/int_macro.h +34 -0
  101. data/ext/numo/narray/numo/types/robj_macro.h +218 -0
  102. data/ext/numo/narray/numo/types/robject.h +21 -0
  103. data/ext/numo/narray/numo/types/scomplex.h +11 -0
  104. data/ext/numo/narray/numo/types/sfloat.h +13 -0
  105. data/ext/numo/narray/numo/types/uint16.h +12 -0
  106. data/ext/numo/narray/numo/types/uint32.h +12 -0
  107. data/ext/numo/narray/numo/types/uint64.h +12 -0
  108. data/ext/numo/narray/numo/types/uint8.h +12 -0
  109. data/ext/numo/narray/numo/types/uint_macro.h +31 -0
  110. data/ext/numo/narray/numo/types/xint_macro.h +133 -0
  111. data/ext/numo/narray/rand.c +87 -0
  112. data/ext/numo/narray/step.c +506 -0
  113. data/ext/numo/narray/struct.c +872 -0
  114. data/lib/2.1/numo/narray.so +0 -0
  115. data/lib/2.2/numo/narray.so +0 -0
  116. data/lib/2.3/numo/narray.so +0 -0
  117. data/lib/erbpp/line_number.rb +126 -0
  118. data/lib/erbpp/narray_def.rb +338 -0
  119. data/lib/erbpp.rb +286 -0
  120. data/lib/numo/narray.rb +6 -0
  121. data/numo-narray.gemspec +35 -0
  122. data/spec/bit_spec.rb +93 -0
  123. data/spec/narray_spec.rb +249 -0
  124. metadata +238 -0
data/lib/erbpp.rb ADDED
@@ -0,0 +1,286 @@
1
+ require "erb"
2
+
3
+ class ErbPP
4
+ ATTRS = []
5
+
6
+ class ParamNotSetError < StandardError; end
7
+
8
+ def self.define_attrs(attrs)
9
+ attrs.each do |attr|
10
+ ivar = ("@"+attr).to_sym
11
+ define_method(attr){|*a| attr_method(ivar,*a)}
12
+ end
13
+ end
14
+
15
+ def attr_method(ivar,arg=nil)
16
+ if arg.nil?
17
+ instance_variable_get(ivar)
18
+ else
19
+ instance_variable_set(ivar,arg)
20
+ end
21
+ end
22
+
23
+ def initialize(parent,erb_path,opts={})
24
+ parents.push(parent) if parent
25
+ @erb_path = erb_path
26
+ @tmpl = @erb_path
27
+
28
+ @opts = opts
29
+ if @opts.class != Hash
30
+ raise ArgumentError, "option is not Hash"
31
+ end
32
+
33
+ @opts.each do |k,v|
34
+ ivar = ("@"+k.to_s).to_sym
35
+ instance_variable_set(ivar,v)
36
+ end
37
+ end
38
+
39
+ def load_erb
40
+ safe_level = nil
41
+ trim_mode = '%<>'
42
+ @erb = ERB.new(File.read(@erb_path),safe_level,trim_mode)
43
+ @erb.filename = @erb_path
44
+ end
45
+
46
+ def parents
47
+ @parents ||= []
48
+ end
49
+
50
+ def search_method_in_parents(_meth_id)
51
+ parents.each do |x|
52
+ if x.has_attr? _meth_id
53
+ return x
54
+ end
55
+ end
56
+ parents.each do |x|
57
+ if f = x.search_method_in_parents(_meth_id)
58
+ return f
59
+ end
60
+ end
61
+ nil
62
+ end
63
+
64
+ def attrs
65
+ self.class::ATTRS
66
+ end
67
+
68
+ def has_attr?(_meth_id)
69
+ respond_to?(_meth_id) or attrs.include?(_meth_id.to_s)
70
+ end
71
+
72
+ def check_params(*params)
73
+ params.each do |x|
74
+ val = send(x)
75
+ if !val # || val.empty?
76
+ raise ParamNotSetError,"parameter #{x.to_s} is not set"
77
+ end
78
+ end
79
+ end
80
+
81
+ alias method_missing_alias method_missing
82
+
83
+ def method_missing(_meth_id, *args, &block)
84
+ ivar = "@"+_meth_id.to_s
85
+ if args.empty? and instance_variable_defined?(ivar)
86
+ parm = instance_variable_get(ivar)
87
+ if parm.nil?
88
+ raise ParamNotSetError,"parameter #{_meth_id.to_s} is not set"
89
+ end
90
+ parm
91
+ elsif args.size == 1 and attrs.include?(_meth_id.to_s)
92
+ instance_variable_set(ivar,args.first)
93
+ elsif x = search_method_in_parents(_meth_id)
94
+ x.send(_meth_id, *args, &block)
95
+ else
96
+ method_missing_alias(_meth_id, *args)
97
+ end
98
+ end
99
+
100
+ def run
101
+ load_erb unless @erb
102
+ @erb.run(binding)
103
+ end
104
+
105
+ def result
106
+ load_erb unless @erb
107
+ @erb.result(binding)
108
+ end
109
+ end
110
+
111
+ # ----------------------------------------------------------------------
112
+
113
+ class IdVar
114
+ DEFS = []
115
+
116
+ def id_decl
117
+ "static ID #{@id_var};"
118
+ end
119
+
120
+ def id_assign
121
+ "#{@id_var} = rb_intern(\"#{@name}\");"
122
+ end
123
+
124
+ def initialize(parent,name,var=nil)
125
+ @name = name
126
+ var = name if var.nil?
127
+ @id_var = "id_"+var.gsub(/\?/,"_p").gsub(/\!/,"_bang")
128
+ DEFS.push(self)
129
+ end
130
+
131
+ def self.declaration
132
+ DEFS.map do |x|
133
+ x.id_decl
134
+ end
135
+ end
136
+
137
+ def self.assignment
138
+ DEFS.map do |x|
139
+ x.id_assign
140
+ end
141
+ end
142
+ end
143
+
144
+ # ----------------------------------------------------------------------
145
+
146
+ class Function < ErbPP
147
+ DEFS = []
148
+
149
+ attrs = %w[
150
+ singleton
151
+ method
152
+ n_arg
153
+ ]
154
+ define_attrs attrs
155
+
156
+ def id_op
157
+ if op.size == 1
158
+ "'#{op}'"
159
+ else
160
+ "id_#{method}"
161
+ end
162
+ end
163
+
164
+ def initialize(parent,tmpl,**opts)
165
+ super
166
+ @aliases = opts[:aliases] || []
167
+ @erb_path = File.join(parent.tmpl_dir, tmpl+".c")
168
+ DEFS.push(self)
169
+ end
170
+
171
+ def c_method
172
+ "#{m_prefix}#{method}"
173
+ end
174
+
175
+ def c_iter
176
+ begin
177
+ t = "_"+type_name
178
+ rescue
179
+ t = ""
180
+ end
181
+ "iter#{t}_#{method}"
182
+ end
183
+ alias c_iterator c_iter
184
+
185
+ def c_func
186
+ s = singleton ? "_s" : ""
187
+ begin
188
+ t = "_"+type_name
189
+ rescue
190
+ t = ""
191
+ end
192
+ "numo#{t}#{s}_#{method}"
193
+ end
194
+ alias c_function c_func
195
+ alias c_instance_method c_func
196
+
197
+ def op_map
198
+ @op || method
199
+ end
200
+
201
+ def code
202
+ result + "\n\n"
203
+ end
204
+
205
+ def definition
206
+ s = singleton ? "_singleton" : ""
207
+ check_params(:mod_var, :op_map, :c_func, :n_arg)
208
+ m = op_map
209
+ a = ["rb_define#{s}_method(#{mod_var}, \"#{m}\", #{c_func}, #{n_arg});"]
210
+ @aliases.map{|x| a << "rb_define_alias(#{mod_var}, \"#{x}\", \"#{m}\");"}
211
+ a
212
+ end
213
+
214
+ def self.codes
215
+ a = []
216
+ DEFS.each do |i|
217
+ x = i.code
218
+ a.push(x) if x
219
+ end
220
+ a
221
+ end
222
+
223
+ def self.definitions
224
+ a = []
225
+ DEFS.each do |i|
226
+ case x = i.definition
227
+ when Array
228
+ a.concat(x)
229
+ when String
230
+ a.push(x)
231
+ else
232
+ raise "unknown definition: #{x}" if x
233
+ end
234
+ end
235
+ a
236
+ end
237
+ end
238
+
239
+ class ModuleFunction < Function
240
+ def definition
241
+ m = op_map
242
+ "rb_define_module_function(#{mod_var}, \"#{m}\", #{c_func}, #{n_arg});"
243
+ end
244
+ end
245
+
246
+ class NodefFunction < Function
247
+ def definition
248
+ nil
249
+ end
250
+ end
251
+
252
+ class Alias < ErbPP
253
+ def initialize(parent, dst, src)
254
+ super(parent,nil)
255
+ @dst = dst
256
+ @src = src
257
+ Function::DEFS.push(self)
258
+ end
259
+
260
+ def code
261
+ nil
262
+ end
263
+
264
+ def definition
265
+ "rb_define_alias(#{mod_var}, \"#{dst}\", \"#{src}\");"
266
+ end
267
+ end
268
+
269
+ class Const < ErbPP
270
+ def initialize(parent,name,value,desc)
271
+ super(parent,nil)
272
+ @name = name
273
+ @value = value
274
+ @desc = desc
275
+ Function::DEFS.push(self)
276
+ end
277
+
278
+ def code
279
+ nil
280
+ end
281
+
282
+ def definition
283
+ "/*"+desc+"*/\n "+
284
+ "rb_define_const(#{mod_var},\"#{name}\",#{value});"
285
+ end
286
+ end
@@ -0,0 +1,6 @@
1
+ begin
2
+ major, minor, _ = RUBY_VERSION.split(/\./)
3
+ require "#{major}.#{minor}/numo/narray.so"
4
+ rescue LoadError
5
+ require "numo/narray.so"
6
+ end
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ open("ext/numo/narray/numo/narray.h") do |f|
6
+ f.each_line do |l|
7
+ if /NARRAY_VERSION "([\d.]+)"/ =~ l
8
+ NARRAY_VERSION = $1
9
+ break
10
+ end
11
+ end
12
+ end
13
+
14
+ Gem::Specification.new do |spec|
15
+ spec.name = "numo-narray"
16
+ spec.version = NARRAY_VERSION
17
+ spec.authors = ["Masahiro TANAKA"]
18
+ spec.email = ["masa16.tanaka@gmail.com"]
19
+ spec.description = %q{NArray development version.}
20
+ spec.summary = %q{NArray development version}
21
+ spec.homepage = "https://github.com/ruby-numo/narray"
22
+ spec.license = "MIT"
23
+
24
+ spec.files = `git ls-files Gemfile README.md Rakefile lib ext numo-narray.gemspec spec`.split($/)
25
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
26
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
27
+ spec.require_paths = ["lib"]
28
+ spec.extensions = ["ext/numo/narray/extconf.rb"]
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.3"
31
+ spec.add_development_dependency "rake", "~> 0"
32
+ spec.add_development_dependency "rspec", "~> 3"
33
+ spec.add_development_dependency "rake-compiler", ">= 1.0.1"
34
+ spec.add_development_dependency "rake-compiler-dock"
35
+ end
data/spec/bit_spec.rb ADDED
@@ -0,0 +1,93 @@
1
+ require File.join(File.dirname(__FILE__), "../ext/numo/narray/narray")
2
+ #Numo::NArray.debug = true
3
+
4
+ RSpec.configure do |config|
5
+ config.filter_run :focus
6
+ config.run_all_when_everything_filtered = true
7
+ end
8
+ #context :focus=>true do ... end
9
+
10
+ dtype = Numo::Bit
11
+
12
+ describe dtype do
13
+ it{expect(dtype).to be < Numo::NArray}
14
+ end
15
+
16
+ procs = [
17
+ [proc{|tp,a| tp[*a] },""],
18
+ [proc{|tp,a| tp[*a][true] },"[true]"],
19
+ [proc{|tp,a| tp[*a][0..-1] },"[0..-1]"]
20
+ ]
21
+ procs.each do |init,ref|
22
+
23
+ describe dtype,"[0,1,1,0,1,0,0,1]"+ref do
24
+ before(:all) do
25
+ @src = [0,1,1,0,1,0,0,1]
26
+ @n = @src.size
27
+ @a = init.call(dtype,@src)
28
+ end
29
+
30
+ it{expect(@a).to eq @src}
31
+ it{expect(@a & 0).to eq [0]*@n}
32
+ it{expect(@a & 1).to eq @src}
33
+ it{expect(@a | 0).to eq @src}
34
+ it{expect(@a | 1).to eq [1]*@n}
35
+ it{expect(@a ^ 0).to eq @src.map{|x| x^0}}
36
+ it{expect(@a ^ 1).to eq @src.map{|x| x^1}}
37
+ it{expect(~@a).to eq @src.map{|x| 1-x}}
38
+
39
+ it{expect(@a.count_true).to eq 4}
40
+ it{expect(@a.count_false).to eq 4}
41
+ it{expect(@a.where).to eq [1,2,4,7]}
42
+ it{expect(@a.where2).to eq [[1,2,4,7],[0,3,5,6]]}
43
+ it{expect(@a.mask(Numo::DFloat[1,2,3,4,5,6,7,8])).to eq [2,3,5,8]}
44
+ it{expect(@a).not_to be_all}
45
+ it{expect(@a).to be_any}
46
+ it{expect(@a).not_to be_none}
47
+
48
+ after(:all) do
49
+ @a = nil
50
+ end
51
+ end
52
+
53
+ end
54
+
55
+ procs = [
56
+ [proc{|tp,a| tp[*a] },""],
57
+ [proc{|tp,a| tp[*a][true,0..-1] },"[true,true]"],
58
+ ]
59
+ procs.each do |init,ref|
60
+
61
+ describe dtype,"[[0,1,1,0],[1,0,0,1]]"+ref do
62
+ before(:all) do
63
+ @src = [[0,1,1,0],[1,0,0,1]]
64
+ @n = @src.size
65
+ @a = init.call(dtype,@src)
66
+ end
67
+
68
+ it{expect(@a[5]).to eq 0}
69
+ it{expect(@a[-1]).to eq 1}
70
+ it{expect(@a[1,0]).to eq @src[1][0]}
71
+ it{expect(@a[1,1]).to eq @src[1][1]}
72
+ it{expect(@a[1,2]).to eq @src[1][2]}
73
+ it{expect(@a[3..4]).to eq [0,1]}
74
+ it{expect(@a[0,1..2]).to eq [1,1]}
75
+ it{expect(@a[0,:*]).to eq @src[0]}
76
+ it{expect(@a[1,:*]).to eq @src[1]}
77
+ it{expect(@a[:*,1]).to eq [@src[0][1],@src[1][1]]}
78
+
79
+ it{expect(@a.count_true).to eq 4}
80
+ it{expect(@a.count_false).to eq 4}
81
+ it{expect(@a.where).to eq [1,2,4,7]}
82
+ it{expect(@a.where2).to eq [[1,2,4,7],[0,3,5,6]]}
83
+ it{expect(@a.mask(Numo::DFloat[[1,2,3,4],[5,6,7,8]])).to eq [2,3,5,8]}
84
+ it{expect(@a).not_to be_all}
85
+ it{expect(@a).to be_any}
86
+ it{expect(@a).not_to be_none}
87
+
88
+ after(:all) do
89
+ @a = nil
90
+ end
91
+ end
92
+
93
+ end
@@ -0,0 +1,249 @@
1
+ require File.join(File.dirname(__FILE__), "../ext/numo/narray/narray")
2
+ #Numo::NArray.debug = true
3
+
4
+ RSpec.configure do |config|
5
+ config.filter_run :focus
6
+ config.run_all_when_everything_filtered = true
7
+ end
8
+ #context :focus=>true do ... end
9
+
10
+ types = [
11
+ Numo::DFloat,
12
+ Numo::SFloat,
13
+ Numo::DComplex,
14
+ Numo::SComplex,
15
+ Numo::Int64,
16
+ Numo::Int32,
17
+ Numo::Int16,
18
+ Numo::Int8,
19
+ Numo::UInt64,
20
+ Numo::UInt32,
21
+ Numo::UInt16,
22
+ Numo::UInt8,
23
+ ]
24
+ #types = [Numo::DFloat]
25
+ float_types = [
26
+ Numo::DFloat,
27
+ Numo::DComplex,
28
+ ]
29
+
30
+ types.each do |dtype|
31
+
32
+ describe dtype do
33
+ it{expect(dtype).to be < Numo::NArray}
34
+ end
35
+
36
+ procs = [
37
+ [proc{|tp,a| tp[*a] },""],
38
+ [proc{|tp,a| tp[*a][true] },"[true]"],
39
+ [proc{|tp,a| tp[*a][0..-1] },"[0..-1]"]
40
+ ]
41
+ procs.each do |init,ref|
42
+
43
+ describe dtype,"[1,2,3,5,7,11]"+ref do
44
+ before(:all) do
45
+ @src = [1,2,3,5,7,11]
46
+ @a = init.call(dtype,@src)
47
+ end
48
+ #context :focus=>true do
49
+
50
+ it{expect(@a).to be_kind_of dtype}
51
+ it{expect(@a.size).to eq 6}
52
+ it{expect(@a.ndim).to eq 1}
53
+ it{expect(@a.shape).to eq [6]}
54
+ it{expect(@a).not_to be_inplace}
55
+ it{expect(@a).to be_row_major}
56
+ it{expect(@a).not_to be_column_major}
57
+ it{expect(@a).to be_host_order}
58
+ it{expect(@a).not_to be_byte_swapped}
59
+ it{expect(@a).to eq [1,2,3,5,7,11]}
60
+ it{expect(@a.to_a).to eq [1,2,3,5,7,11]}
61
+ it{expect(@a.to_a).to be_kind_of Array}
62
+ it{expect(@a.dup).to eq @a}
63
+ it{expect(@a.clone).to eq @a}
64
+ it{expect(@a.dup.object_id).not_to eq @a.object_id}
65
+ it{expect(@a.clone.object_id).not_to eq @a.object_id}
66
+
67
+ it{expect(@a.eq([1,1,3,3,7,7])).to eq [1,0,1,0,1,0]}
68
+ it{expect(@a[3..4]).to eq [5,7]}
69
+ it{expect(@a[5]).to eq 11}
70
+ it{expect(@a[-1]).to eq 11}
71
+ it{expect(@a[[4,3,0,1,5,2]]).to eq [7,5,1,2,11,3]}
72
+ it{expect(@a.sum).to eq 29}
73
+ if float_types.include?(dtype)
74
+ it{expect(@a.mean).to eq 29.0/6}
75
+ it{expect(@a.var).to eq 13.766666666666669}
76
+ it{expect(@a.stddev).to eq 3.710345895825168}
77
+ it{expect(@a.rms).to eq 5.901977069875258}
78
+ end
79
+ it{expect(@a.copy.fill(12)).to eq [12]*6}
80
+ it{expect((@a + 1)).to eq [2,3,4,6,8,12]}
81
+ it{expect((@a - 1)).to eq [0,1,2,4,6,10]}
82
+ it{expect((@a * 3)).to eq [3,6,9,15,21,33]}
83
+ it{expect((@a / 0.5)).to eq [2,4,6,10,14,22]}
84
+ it{expect((-@a)).to eq [-1,-2,-3,-5,-7,-11]}
85
+ it{expect((@a ** 2)).to eq [1,4,9,25,49,121]}
86
+ it{expect(@a.swap_byte.swap_byte).to eq [1,2,3,5,7,11]}
87
+ if dtype == Numo::DComplex || dtype == Numo::SComplex
88
+ it{expect(@a.real).to eq @src}
89
+ it{expect(@a.imag).to eq [0]*6}
90
+ it{expect(@a.conj).to eq @src}
91
+ it{expect(@a.angle).to eq [0]*6}
92
+ else
93
+ it{expect(@a.min).to eq 1}
94
+ it{expect(@a.max).to eq 11}
95
+ it{expect((@a >= 3)).to eq [0,0,1,1,1,1]}
96
+ it{expect((@a > 3)).to eq [0,0,0,1,1,1]}
97
+ it{expect((@a <= 3)).to eq [1,1,1,0,0,0]}
98
+ it{expect((@a < 3)).to eq [1,1,0,0,0,0]}
99
+ it{expect((@a.eq 3)).to eq [0,0,1,0,0,0]}
100
+ it{expect(@a.sort).to eq @src}
101
+ it{expect(@a.sort_index).to eq (0..5).to_a}
102
+ it{expect(@a.median).to eq 4}
103
+ end
104
+ end
105
+ end
106
+
107
+ describe dtype, '[1..4]' do
108
+ it{expect(dtype[1..4]).to eq [1,2,3,4]}
109
+ end
110
+
111
+ #describe dtype, ".seq(5)" do
112
+ # it do
113
+ # dtype.seq(5).should == [0,1,2,3,4]
114
+ # end
115
+ #end
116
+
117
+ procs2 = [
118
+ [proc{|tp,src| tp[*src] },""],
119
+ [proc{|tp,src| tp[*src][true,true] },"[true,true]"],
120
+ [proc{|tp,src| tp[*src][0..-1,0..-1] },"[0..-1,0..-1]"]
121
+ ]
122
+ procs2.each do |init,ref|
123
+
124
+ describe dtype,'[[1,2,3],[5,7,11]]'+ref do
125
+ before(:all) do
126
+ @src = [[1,2,3],[5,7,11]]
127
+ @a = init.call(dtype,@src)
128
+ end
129
+ #context :focus=>true do
130
+
131
+ it{expect(@a).to be_kind_of dtype}
132
+ it{expect(@a.size).to eq 6}
133
+ it{expect(@a.ndim).to eq 2}
134
+ it{expect(@a.shape).to eq [2,3]}
135
+ it{expect(@a).not_to be_inplace}
136
+ it{expect(@a).to be_row_major}
137
+ it{expect(@a).not_to be_column_major}
138
+ it{expect(@a).to be_host_order}
139
+ it{expect(@a).not_to be_byte_swapped}
140
+ it{expect(@a).to eq @src}
141
+ it{expect(@a.to_a).to eq @src}
142
+ it{expect(@a.to_a).to be_kind_of Array}
143
+
144
+ it{expect(@a.eq([[1,1,3],[3,7,7]])).to eq [[1,0,1],[0,1,0]]}
145
+ it{expect(@a[5]).to eq 11}
146
+ it{expect(@a[-1]).to eq 11}
147
+ it{expect(@a[1,0]).to eq @src[1][0]}
148
+ it{expect(@a[1,1]).to eq @src[1][1]}
149
+ it{expect(@a[1,2]).to eq @src[1][2]}
150
+ it{expect(@a[3..4]).to eq [5,7]}
151
+ it{expect(@a[0,1..2]).to eq [2,3]}
152
+ it{expect(@a[0,:*]).to eq @src[0]}
153
+ it{expect(@a[1,:*]).to eq @src[1]}
154
+ it{expect(@a[:*,1]).to eq [@src[0][1],@src[1][1]]}
155
+ it{expect(@a[true,[2,0,1]]).to eq [[3,1,2],[11,5,7]]}
156
+ it{expect(@a.reshape(3,2)).to eq [[1,2],[3,5],[7,11]]}
157
+ it{expect(@a.reshape(3,nil)).to eq [[1,2],[3,5],[7,11]]}
158
+ it{expect(@a.reshape(nil,2)).to eq [[1,2],[3,5],[7,11]]}
159
+ it{expect(@a.transpose).to eq [[1,5],[2,7],[3,11]]}
160
+ it{expect(@a.transpose(1,0)).to eq [[1,5],[2,7],[3,11]]}
161
+
162
+ it{expect(@a.sum).to eq 29}
163
+ it{expect(@a.sum(0)).to eq [6, 9, 14]}
164
+ it{expect(@a.sum(1)).to eq [6, 23]}
165
+ if float_types.include?(dtype)
166
+ it{expect(@a.mean).to eq 29.0/6}
167
+ it{expect(@a.mean(0)).to eq [3, 4.5, 7]}
168
+ it{expect(@a.mean(1)).to eq [2, 23.0/3]}
169
+ end
170
+ if dtype == Numo::DComplex || dtype == Numo::SComplex
171
+ it{expect(@a.real).to eq @src}
172
+ it{expect(@a.imag).to eq [[0]*3]*2}
173
+ it{expect(@a.conj).to eq @src}
174
+ it{expect(@a.angle).to eq [[0]*3]*2}
175
+ else
176
+ it{expect(@a.min).to eq 1}
177
+ it{expect(@a.max).to eq 11}
178
+ it{expect((@a >= 3)).to eq [[0,0,1],[1,1,1]]}
179
+ it{expect((@a > 3)).to eq [[0,0,0],[1,1,1]]}
180
+ it{expect((@a <= 3)).to eq [[1,1,1],[0,0,0]]}
181
+ it{expect((@a < 3)).to eq [[1,1,0],[0,0,0]]}
182
+ it{expect((@a.eq 3)).to eq [[0,0,1],[0,0,0]]}
183
+ it{expect(@a.sort).to eq @src}
184
+ it{expect(@a.sort_index).to eq [[0,1,2],[3,4,5]]}
185
+ end
186
+ it{expect(@a.copy.fill(12)).to eq [[12]*3]*2}
187
+ it{expect((@a + 1)).to eq [[2,3,4],[6,8,12]]}
188
+ it{expect((@a + [1,2,3])).to eq [[2,4,6],[6,9,14]]}
189
+ it{expect((@a - 1)).to eq [[0,1,2],[4,6,10]]}
190
+ it{expect((@a - [1,2,3])).to eq [[0,0,0],[4,5,8]]}
191
+ it{expect((@a * 3)).to eq [[3,6,9],[15,21,33]]}
192
+ it{expect((@a * [1,2,3])).to eq [[1,4,9],[5,14,33]]}
193
+ it{expect((@a / 0.5)).to eq [[2,4,6],[10,14,22]]}
194
+ it{expect((-@a)).to eq [[-1,-2,-3],[-5,-7,-11]]}
195
+ it{expect((@a ** 2)).to eq [[1,4,9],[25,49,121]]}
196
+ it{expect((dtype[[1,0],[0,1]].dot dtype[[4,1],[2,2]])).to eq [[4,1],[2,2]]}
197
+ it{expect(@a.swap_byte.swap_byte).to eq @src}
198
+ end
199
+
200
+ end
201
+
202
+ describe dtype,"[[[1,2],[3,4]],[[5,6],[7,8]]]" do
203
+ before do
204
+ @a = dtype[[[1,2],[3,4]],[[5,6],[7,8]]]
205
+ end
206
+
207
+ it{expect(@a[0, 1, 1]).to eq 4}
208
+ #it{expect(@a[:rest]).to eq @a} # note: this spec probably shows the correct behaviour
209
+ it{expect(@a[0, :rest]).to eq [[1,2],[3,4]]}
210
+ it{expect(@a[0, false]).to eq [[1,2],[3,4]]}
211
+ it{expect(@a[0, 1, :rest]).to eq [3,4]}
212
+ it{expect(@a[0, 1, false]).to eq [3,4]}
213
+ it{expect(@a[:rest, 0]).to eq [[1,3],[5,7]]}
214
+ it{expect(@a[:rest, 0, 1]).to eq [2,6]}
215
+ it{expect(@a[1, :rest, 0]).to eq [5,7]}
216
+ it{expect{@a[1, 1, :rest, 0]}.to raise_error IndexError}
217
+ it{expect{@a[1, 1, 1, 1, :rest]}.to raise_error IndexError}
218
+ it{expect{@a[1, 1, 1, :rest, 1]}.to raise_error IndexError}
219
+ end
220
+
221
+ describe dtype, "#dot" do
222
+ it "vector.dot(vector)" do
223
+ a = dtype[1..3]
224
+ b = dtype[2..4]
225
+ expect(a.dot(b)).to eq (1*2 + 2*3 + 3*4)
226
+ end
227
+ it "matrix.dot(vector)" do
228
+ a = dtype[1..6].reshape(3,2)
229
+ b = dtype[1..2]
230
+ expect(a.dot(b)).to eq [5, 11, 17]
231
+ end
232
+ it "vector.dot(matrix)" do
233
+ a = dtype[1..2]
234
+ b = dtype[1..6].reshape(2,3)
235
+ expect(a.dot(b)).to eq [9, 12, 15]
236
+ end
237
+ it "matrix.dot(matrix)" do
238
+ a = dtype[1..6].reshape(3,2)
239
+ b = dtype[1..6].reshape(2,3)
240
+ expect(a.dot(b)).to eq [[9, 12, 15], [19, 26, 33], [29, 40, 51]]
241
+ expect(b.dot(a)).to eq [[22, 28], [49, 64]]
242
+ end
243
+ it "matrix.dot(matrix) with incorrect shape" do
244
+ a = dtype[1..6].reshape(3,2)
245
+ b = dtype[1..9].reshape(3,3)
246
+ expect{a.dot(b)}.to raise_error(TypeError)
247
+ end
248
+ end
249
+ end