-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathnarray_spec.rb
More file actions
249 lines (230 loc) · 8.22 KB
/
narray_spec.rb
File metadata and controls
249 lines (230 loc) · 8.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
require_relative "../lib/numo/narray.so"
#Numo::NArray.debug = true
RSpec.configure do |config|
config.filter_run :focus
config.run_all_when_everything_filtered = true
end
#context :focus=>true do ... end
types = [
Numo::DFloat,
Numo::SFloat,
Numo::DComplex,
Numo::SComplex,
Numo::Int64,
Numo::Int32,
Numo::Int16,
Numo::Int8,
Numo::UInt64,
Numo::UInt32,
Numo::UInt16,
Numo::UInt8,
]
#types = [Numo::DFloat]
float_types = [
Numo::DFloat,
Numo::DComplex,
]
types.each do |dtype|
describe dtype do
it{expect(dtype).to be < Numo::NArray}
end
procs = [
[proc{|tp,a| tp[*a] },""],
[proc{|tp,a| tp[*a][true] },"[true]"],
[proc{|tp,a| tp[*a][0..-1] },"[0..-1]"]
]
procs.each do |init,ref|
describe dtype,"[1,2,3,5,7,11]"+ref do
before(:all) do
@src = [1,2,3,5,7,11]
@a = init.call(dtype,@src)
end
#context :focus=>true do
it{expect(@a).to be_kind_of dtype}
it{expect(@a.size).to eq 6}
it{expect(@a.ndim).to eq 1}
it{expect(@a.shape).to eq [6]}
it{expect(@a).not_to be_inplace}
it{expect(@a).to be_row_major}
it{expect(@a).not_to be_column_major}
it{expect(@a).to be_host_order}
it{expect(@a).not_to be_byte_swapped}
it{expect(@a).to eq [1,2,3,5,7,11]}
it{expect(@a.to_a).to eq [1,2,3,5,7,11]}
it{expect(@a.to_a).to be_kind_of Array}
it{expect(@a.dup).to eq @a}
it{expect(@a.clone).to eq @a}
it{expect(@a.dup.object_id).not_to eq @a.object_id}
it{expect(@a.clone.object_id).not_to eq @a.object_id}
it{expect(@a.eq([1,1,3,3,7,7])).to eq [1,0,1,0,1,0]}
it{expect(@a[3..4]).to eq [5,7]}
it{expect(@a[5]).to eq 11}
it{expect(@a[-1]).to eq 11}
it{expect(@a[[4,3,0,1,5,2]]).to eq [7,5,1,2,11,3]}
it{expect(@a.sum).to eq 29}
if float_types.include?(dtype)
it{expect(@a.mean).to eq 29.0/6}
it{expect(@a.var).to eq 13.766666666666669}
it{expect(@a.stddev).to eq 3.710345895825168}
it{expect(@a.rms).to eq 5.901977069875258}
end
it{expect(@a.copy.fill(12)).to eq [12]*6}
it{expect((@a + 1)).to eq [2,3,4,6,8,12]}
it{expect((@a - 1)).to eq [0,1,2,4,6,10]}
it{expect((@a * 3)).to eq [3,6,9,15,21,33]}
it{expect((@a / 0.5)).to eq [2,4,6,10,14,22]}
it{expect((-@a)).to eq [-1,-2,-3,-5,-7,-11]}
it{expect((@a ** 2)).to eq [1,4,9,25,49,121]}
it{expect(@a.swap_byte.swap_byte).to eq [1,2,3,5,7,11]}
if dtype == Numo::DComplex || dtype == Numo::SComplex
it{expect(@a.real).to eq @src}
it{expect(@a.imag).to eq [0]*6}
it{expect(@a.conj).to eq @src}
it{expect(@a.angle).to eq [0]*6}
else
it{expect(@a.min).to eq 1}
it{expect(@a.max).to eq 11}
it{expect((@a >= 3)).to eq [0,0,1,1,1,1]}
it{expect((@a > 3)).to eq [0,0,0,1,1,1]}
it{expect((@a <= 3)).to eq [1,1,1,0,0,0]}
it{expect((@a < 3)).to eq [1,1,0,0,0,0]}
it{expect((@a.eq 3)).to eq [0,0,1,0,0,0]}
it{expect(@a.sort).to eq @src}
it{expect(@a.sort_index).to eq (0..5).to_a}
it{expect(@a.median).to eq 4}
end
end
end
describe dtype, '[1..4]' do
it{expect(dtype[1..4]).to eq [1,2,3,4]}
end
#describe dtype, ".seq(5)" do
# it do
# dtype.seq(5).should == [0,1,2,3,4]
# end
#end
procs2 = [
[proc{|tp,src| tp[*src] },""],
[proc{|tp,src| tp[*src][true,true] },"[true,true]"],
[proc{|tp,src| tp[*src][0..-1,0..-1] },"[0..-1,0..-1]"]
]
procs2.each do |init,ref|
describe dtype,'[[1,2,3],[5,7,11]]'+ref do
before(:all) do
@src = [[1,2,3],[5,7,11]]
@a = init.call(dtype,@src)
end
#context :focus=>true do
it{expect(@a).to be_kind_of dtype}
it{expect(@a.size).to eq 6}
it{expect(@a.ndim).to eq 2}
it{expect(@a.shape).to eq [2,3]}
it{expect(@a).not_to be_inplace}
it{expect(@a).to be_row_major}
it{expect(@a).not_to be_column_major}
it{expect(@a).to be_host_order}
it{expect(@a).not_to be_byte_swapped}
it{expect(@a).to eq @src}
it{expect(@a.to_a).to eq @src}
it{expect(@a.to_a).to be_kind_of Array}
it{expect(@a.eq([[1,1,3],[3,7,7]])).to eq [[1,0,1],[0,1,0]]}
it{expect(@a[5]).to eq 11}
it{expect(@a[-1]).to eq 11}
it{expect(@a[1,0]).to eq @src[1][0]}
it{expect(@a[1,1]).to eq @src[1][1]}
it{expect(@a[1,2]).to eq @src[1][2]}
it{expect(@a[3..4]).to eq [5,7]}
it{expect(@a[0,1..2]).to eq [2,3]}
it{expect(@a[0,:*]).to eq @src[0]}
it{expect(@a[1,:*]).to eq @src[1]}
it{expect(@a[:*,1]).to eq [@src[0][1],@src[1][1]]}
it{expect(@a[true,[2,0,1]]).to eq [[3,1,2],[11,5,7]]}
it{expect(@a.reshape(3,2)).to eq [[1,2],[3,5],[7,11]]}
it{expect(@a.reshape(3,nil)).to eq [[1,2],[3,5],[7,11]]}
it{expect(@a.reshape(nil,2)).to eq [[1,2],[3,5],[7,11]]}
it{expect(@a.transpose).to eq [[1,5],[2,7],[3,11]]}
it{expect(@a.transpose(1,0)).to eq [[1,5],[2,7],[3,11]]}
it{expect(@a.sum).to eq 29}
it{expect(@a.sum(0)).to eq [6, 9, 14]}
it{expect(@a.sum(1)).to eq [6, 23]}
if float_types.include?(dtype)
it{expect(@a.mean).to eq 29.0/6}
it{expect(@a.mean(0)).to eq [3, 4.5, 7]}
it{expect(@a.mean(1)).to eq [2, 23.0/3]}
end
if dtype == Numo::DComplex || dtype == Numo::SComplex
it{expect(@a.real).to eq @src}
it{expect(@a.imag).to eq [[0]*3]*2}
it{expect(@a.conj).to eq @src}
it{expect(@a.angle).to eq [[0]*3]*2}
else
it{expect(@a.min).to eq 1}
it{expect(@a.max).to eq 11}
it{expect((@a >= 3)).to eq [[0,0,1],[1,1,1]]}
it{expect((@a > 3)).to eq [[0,0,0],[1,1,1]]}
it{expect((@a <= 3)).to eq [[1,1,1],[0,0,0]]}
it{expect((@a < 3)).to eq [[1,1,0],[0,0,0]]}
it{expect((@a.eq 3)).to eq [[0,0,1],[0,0,0]]}
it{expect(@a.sort).to eq @src}
it{expect(@a.sort_index).to eq [[0,1,2],[3,4,5]]}
end
it{expect(@a.copy.fill(12)).to eq [[12]*3]*2}
it{expect((@a + 1)).to eq [[2,3,4],[6,8,12]]}
it{expect((@a + [1,2,3])).to eq [[2,4,6],[6,9,14]]}
it{expect((@a - 1)).to eq [[0,1,2],[4,6,10]]}
it{expect((@a - [1,2,3])).to eq [[0,0,0],[4,5,8]]}
it{expect((@a * 3)).to eq [[3,6,9],[15,21,33]]}
it{expect((@a * [1,2,3])).to eq [[1,4,9],[5,14,33]]}
it{expect((@a / 0.5)).to eq [[2,4,6],[10,14,22]]}
it{expect((-@a)).to eq [[-1,-2,-3],[-5,-7,-11]]}
it{expect((@a ** 2)).to eq [[1,4,9],[25,49,121]]}
it{expect((dtype[[1,0],[0,1]].dot dtype[[4,1],[2,2]])).to eq [[4,1],[2,2]]}
it{expect(@a.swap_byte.swap_byte).to eq @src}
end
end
describe dtype,"[[[1,2],[3,4]],[[5,6],[7,8]]]" do
before do
@a = dtype[[[1,2],[3,4]],[[5,6],[7,8]]]
end
it{expect(@a[0, 1, 1]).to eq 4}
#it{expect(@a[:rest]).to eq @a} # note: this spec probably shows the correct behaviour
it{expect(@a[0, :rest]).to eq [[1,2],[3,4]]}
it{expect(@a[0, false]).to eq [[1,2],[3,4]]}
it{expect(@a[0, 1, :rest]).to eq [3,4]}
it{expect(@a[0, 1, false]).to eq [3,4]}
it{expect(@a[:rest, 0]).to eq [[1,3],[5,7]]}
it{expect(@a[:rest, 0, 1]).to eq [2,6]}
it{expect(@a[1, :rest, 0]).to eq [5,7]}
it{expect{@a[1, 1, :rest, 0]}.to raise_error IndexError}
it{expect{@a[1, 1, 1, 1, :rest]}.to raise_error IndexError}
it{expect{@a[1, 1, 1, :rest, 1]}.to raise_error IndexError}
end
describe dtype, "#dot" do
it "vector.dot(vector)" do
a = dtype[1..3]
b = dtype[2..4]
expect(a.dot(b)).to eq (1*2 + 2*3 + 3*4)
end
it "matrix.dot(vector)" do
a = dtype[1..6].reshape(3,2)
b = dtype[1..2]
expect(a.dot(b)).to eq [5, 11, 17]
end
it "vector.dot(matrix)" do
a = dtype[1..2]
b = dtype[1..6].reshape(2,3)
expect(a.dot(b)).to eq [9, 12, 15]
end
it "matrix.dot(matrix)" do
a = dtype[1..6].reshape(3,2)
b = dtype[1..6].reshape(2,3)
expect(a.dot(b)).to eq [[9, 12, 15], [19, 26, 33], [29, 40, 51]]
expect(b.dot(a)).to eq [[22, 28], [49, 64]]
end
it "matrix.dot(matrix) with incorrect shape" do
a = dtype[1..6].reshape(3,2)
b = dtype[1..9].reshape(3,3)
expect{a.dot(b)}.to raise_error(TypeError)
end
end
end