numo-narray 0.9.1.0 → 0.9.1.1

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