nmatrix 0.1.0.rc1 → 0.1.0.rc2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +4 -1
- data/Gemfile +1 -4
- data/History.txt +64 -2
- data/Manifest.txt +6 -4
- data/README.rdoc +8 -5
- data/Rakefile +0 -2
- data/ext/nmatrix/data/data.cpp +2 -1
- data/ext/nmatrix/data/data.h +3 -2
- data/ext/nmatrix/extconf.rb +4 -9
- data/ext/nmatrix/math.cpp +65 -0
- data/ext/nmatrix/math/math.h +1 -0
- data/ext/nmatrix/nmatrix.h +2 -2
- data/ext/nmatrix/ruby_constants.cpp +3 -1
- data/ext/nmatrix/ruby_constants.h +3 -1
- data/ext/nmatrix/ruby_nmatrix.c +153 -8
- data/ext/nmatrix/util/sl_list.cpp +6 -2
- data/lib/nmatrix/io/point_cloud.rb +182 -0
- data/lib/nmatrix/math.rb +35 -5
- data/lib/nmatrix/nmatrix.rb +70 -26
- data/lib/nmatrix/shortcuts.rb +18 -1
- data/lib/nmatrix/version.rb +1 -1
- data/nmatrix.gemspec +2 -2
- data/spec/00_nmatrix_spec.rb +220 -176
- data/spec/01_enum_spec.rb +29 -29
- data/spec/02_slice_spec.rb +85 -85
- data/spec/blas_spec.rb +18 -18
- data/spec/elementwise_spec.rb +44 -44
- data/spec/io_spec.rb +31 -24
- data/spec/lapack_spec.rb +34 -34
- data/spec/math_spec.rb +61 -46
- data/spec/nmatrix_yale_spec.rb +35 -35
- data/spec/rspec_spec.rb +2 -2
- data/spec/shortcuts_spec.rb +66 -48
- data/spec/slice_set_spec.rb +31 -31
- data/spec/stat_spec.rb +40 -40
- data/spec/test.pcd +20 -0
- metadata +5 -2
data/spec/rspec_spec.rb
CHANGED
@@ -28,7 +28,7 @@ require File.join(File.dirname(__FILE__), "spec_helper.rb")
|
|
28
28
|
|
29
29
|
describe "RSpec" do
|
30
30
|
it "should permit #be_within to be used on a dense NMatrix" do
|
31
|
-
(NMatrix.new(:dense, [4,1], 1.0, :complex128) / 10000.0).
|
32
|
-
(NMatrix.new(:dense, [4,1], 1.0, :complex128) / 10000.0).
|
31
|
+
expect(NMatrix.new(:dense, [4,1], 1.0, :complex128) / 10000.0).to be_within(0.00000001).of(NMatrix.new(:dense, [4,1], 0.0001, :float64))
|
32
|
+
expect(NMatrix.new(:dense, [4,1], 1.0, :complex128) / 10000.0).not_to be_within(0.00000001).of(NMatrix.new(:dense, [4,1], 1.0, :float64))
|
33
33
|
end
|
34
34
|
end
|
data/spec/shortcuts_spec.rb
CHANGED
@@ -38,53 +38,71 @@ describe NMatrix do
|
|
38
38
|
m = NMatrix.zeros(3)
|
39
39
|
n = NMatrix.new([3, 3], 0)
|
40
40
|
|
41
|
-
m.
|
41
|
+
expect(m).to eq n
|
42
42
|
end
|
43
43
|
|
44
44
|
it "ones() creates a matrix of ones" do
|
45
45
|
m = NMatrix.ones(3)
|
46
46
|
n = NMatrix.new([3, 3], 1)
|
47
47
|
|
48
|
-
m.
|
48
|
+
expect(m).to eq n
|
49
49
|
end
|
50
50
|
|
51
51
|
it "eye() creates an identity matrix" do
|
52
52
|
m = NMatrix.eye(3)
|
53
53
|
identity3 = NMatrix.new([3, 3], [1, 0, 0, 0, 1, 0, 0, 0, 1])
|
54
54
|
|
55
|
-
m.
|
55
|
+
expect(m).to eq identity3
|
56
56
|
end
|
57
57
|
|
58
58
|
it "diag() creates a matrix with pre-supplied diagonal" do
|
59
59
|
arr = [1,2,3,4]
|
60
60
|
m = NMatrix.diag(arr)
|
61
|
-
m.is_a?(NMatrix).
|
61
|
+
expect(m.is_a?(NMatrix)).to be_true
|
62
62
|
end
|
63
63
|
|
64
64
|
it "diagonals() contains the seeded values on the diagonal" do
|
65
65
|
arr = [1,2,3,4]
|
66
66
|
m = NMatrix.diagonals(arr)
|
67
|
-
m[0,0].
|
68
|
-
m[1,1].
|
69
|
-
m[2,2].
|
70
|
-
m[3,3].
|
67
|
+
expect(m[0,0]).to eq(arr[0])
|
68
|
+
expect(m[1,1]).to eq(arr[1])
|
69
|
+
expect(m[2,2]).to eq(arr[2])
|
70
|
+
expect(m[3,3]).to eq(arr[3])
|
71
71
|
end
|
72
72
|
|
73
|
-
|
74
|
-
|
73
|
+
context "::random" do
|
74
|
+
it "creates a matrix of random numbers" do
|
75
|
+
m = NMatrix.random(2)
|
75
76
|
|
76
|
-
|
77
|
-
|
78
|
-
|
77
|
+
expect(m.stype).to eq(:dense)
|
78
|
+
expect(m.dtype).to eq(:float64)
|
79
|
+
end
|
80
|
+
|
81
|
+
it "creates a complex matrix of random numbers" do
|
82
|
+
m = NMatrix.random(2, :dtype => :complex128)
|
83
|
+
end
|
84
|
+
|
85
|
+
it "forbids generation of a rational matrix" do
|
86
|
+
expect { m = NMatrix.random(2, dtype: :rational128) }.to raise_error
|
87
|
+
end
|
79
88
|
|
80
|
-
|
81
|
-
|
89
|
+
it "correctly accepts :scale parameter" do
|
90
|
+
m = NMatrix.random([2,2], dtype: :byte, scale: 255)
|
91
|
+
m.each do |v|
|
92
|
+
expect(v).to be > 0
|
93
|
+
expect(v).to be < 255
|
94
|
+
end
|
95
|
+
end
|
82
96
|
|
83
|
-
|
84
|
-
|
97
|
+
it "only accepts an integer or an array as dimension" do
|
98
|
+
m = NMatrix.random([2, 2])
|
85
99
|
|
86
|
-
|
87
|
-
|
100
|
+
expect(m.stype).to eq(:dense)
|
101
|
+
expect(m.dtype).to eq(:float64)
|
102
|
+
|
103
|
+
expect { NMatrix.random(2.0) }.to raise_error
|
104
|
+
expect { NMatrix.random("not an array or integer") }.to raise_error
|
105
|
+
end
|
88
106
|
end
|
89
107
|
|
90
108
|
it "seq() creates a matrix of integers, sequentially" do
|
@@ -93,7 +111,7 @@ describe NMatrix do
|
|
93
111
|
|
94
112
|
2.times do |i|
|
95
113
|
2.times do |j|
|
96
|
-
m[i,j].
|
114
|
+
expect(m[i,j]).to eq(value)
|
97
115
|
value += 1
|
98
116
|
end
|
99
117
|
end
|
@@ -106,7 +124,7 @@ describe NMatrix do
|
|
106
124
|
|
107
125
|
2.times do |i|
|
108
126
|
2.times do |j|
|
109
|
-
m[i, j].
|
127
|
+
expect(m[i, j]).to eq(value)
|
110
128
|
value += 1
|
111
129
|
end
|
112
130
|
end
|
@@ -118,7 +136,7 @@ describe NMatrix do
|
|
118
136
|
|
119
137
|
2.times do |i|
|
120
138
|
2.times do |j|
|
121
|
-
(m[i, j]/10).
|
139
|
+
expect(m[i, j]/10).to be_within(Float::EPSILON).of(value.to_f/10)
|
122
140
|
value += 1
|
123
141
|
end
|
124
142
|
end
|
@@ -130,7 +148,7 @@ describe NMatrix do
|
|
130
148
|
|
131
149
|
2.times do |i|
|
132
150
|
2.times do |j|
|
133
|
-
m[i, j].
|
151
|
+
expect(m[i, j]).to eq(value)
|
134
152
|
value += 1
|
135
153
|
end
|
136
154
|
end
|
@@ -142,8 +160,8 @@ describe NMatrix do
|
|
142
160
|
|
143
161
|
2.times do |i|
|
144
162
|
2.times do |j|
|
145
|
-
m[i, j].real.
|
146
|
-
m[i, j].imag.
|
163
|
+
expect(m[i, j].real).to be_within(Float::EPSILON).of(value)
|
164
|
+
expect(m[i, j].imag).to be_within(Float::EPSILON).of(0.0)
|
147
165
|
value += 1
|
148
166
|
end
|
149
167
|
end
|
@@ -152,28 +170,28 @@ describe NMatrix do
|
|
152
170
|
it "column() returns a NMatrix" do
|
153
171
|
m = NMatrix.random(3)
|
154
172
|
|
155
|
-
m.column(2).is_a?(NMatrix).
|
173
|
+
expect(m.column(2).is_a?(NMatrix)).to be_true
|
156
174
|
end
|
157
175
|
|
158
176
|
it "row() returns a NMatrix" do
|
159
177
|
m = NMatrix.random(3)
|
160
178
|
|
161
|
-
m.row(2).is_a?(NMatrix).
|
179
|
+
expect(m.row(2).is_a?(NMatrix)).to be_true
|
162
180
|
end
|
163
181
|
|
164
182
|
it "diagonals() creates an NMatrix" do
|
165
183
|
arr = [1,2,3,4]
|
166
184
|
m = NMatrix.diagonals(arr)
|
167
|
-
m.is_a?(NMatrix).
|
185
|
+
expect(m.is_a?(NMatrix)).to be_true
|
168
186
|
end
|
169
187
|
|
170
188
|
it "diagonals() contains the seeded values on the diagonal" do
|
171
189
|
arr = [1,2,3,4]
|
172
190
|
m = NMatrix.diagonals(arr)
|
173
|
-
m[0,0].
|
174
|
-
m[1,1].
|
175
|
-
m[2,2].
|
176
|
-
m[3,3].
|
191
|
+
expect(m[0,0]).to eq(arr[0])
|
192
|
+
expect(m[1,1]).to eq(arr[1])
|
193
|
+
expect(m[2,2]).to eq(arr[2])
|
194
|
+
expect(m[3,3]).to eq(arr[3])
|
177
195
|
end
|
178
196
|
|
179
197
|
context "_like constructors" do
|
@@ -183,13 +201,13 @@ describe NMatrix do
|
|
183
201
|
end
|
184
202
|
|
185
203
|
it "should create an nmatrix of ones with dimensions and type the same as its argument" do
|
186
|
-
NMatrix.ones_like(@nm_1d).
|
187
|
-
NMatrix.ones_like(@nm_2d).
|
204
|
+
expect(NMatrix.ones_like(@nm_1d)).to eq NMatrix[1.0, 1.0, 1.0, 1.0, 1.0]
|
205
|
+
expect(NMatrix.ones_like(@nm_2d)).to eq NMatrix[[1.0, 1.0], [1.0, 1.0]]
|
188
206
|
end
|
189
207
|
|
190
208
|
it "should create an nmatrix of zeros with dimensions and type the same as its argument" do
|
191
|
-
NMatrix.zeros_like(@nm_1d).
|
192
|
-
NMatrix.zeros_like(@nm_2d).
|
209
|
+
expect(NMatrix.zeros_like(@nm_1d)).to eq NMatrix[0.0, 0.0, 0.0, 0.0, 0.0]
|
210
|
+
expect(NMatrix.zeros_like(@nm_2d)).to eq NMatrix[[0.0, 0.0], [0.0, 0.0]]
|
193
211
|
end
|
194
212
|
end
|
195
213
|
|
@@ -201,7 +219,7 @@ describe "NVector" do
|
|
201
219
|
v = NVector.zeros(4)
|
202
220
|
|
203
221
|
4.times do |i|
|
204
|
-
v[i].
|
222
|
+
expect(v[i]).to eq(0)
|
205
223
|
end
|
206
224
|
end
|
207
225
|
|
@@ -209,19 +227,19 @@ describe "NVector" do
|
|
209
227
|
v = NVector.ones(3)
|
210
228
|
|
211
229
|
3.times do |i|
|
212
|
-
v[i].
|
230
|
+
expect(v[i]).to eq(1)
|
213
231
|
end
|
214
232
|
end
|
215
233
|
|
216
234
|
it "random() creates a vector of random numbers" do
|
217
235
|
v = NVector.random(4)
|
218
|
-
v.dtype.
|
219
|
-
v.stype.
|
236
|
+
expect(v.dtype).to eq(:float64)
|
237
|
+
expect(v.stype).to eq(:dense)
|
220
238
|
end
|
221
239
|
|
222
240
|
it "seq() creates a vector of integers, sequentially" do
|
223
241
|
v = NVector.seq(7)
|
224
|
-
v.
|
242
|
+
expect(v).to eq(NMatrix.new([7,1], [0, 1, 2, 3, 4, 5, 6]))
|
225
243
|
end
|
226
244
|
|
227
245
|
it "seq() only accepts integers as dimension" do
|
@@ -233,32 +251,32 @@ describe "NVector" do
|
|
233
251
|
|
234
252
|
it "indgen() creates a vector of integers as well as seq()" do
|
235
253
|
v = NVector.indgen(7)
|
236
|
-
v.
|
254
|
+
expect(v).to eq(NMatrix.new([7,1], [0, 1, 2, 3, 4, 5, 6]))
|
237
255
|
end
|
238
256
|
|
239
257
|
it "findgen creates a vector of floats, sequentially" do
|
240
258
|
v = NVector.findgen(2)
|
241
|
-
v.
|
259
|
+
expect(v).to eq(NMatrix.new([2,1], [0.0, 1.0]))
|
242
260
|
end
|
243
261
|
|
244
262
|
it "bindgen() creates a vector of bytes, sequentially" do
|
245
263
|
v = NVector.bindgen(4)
|
246
|
-
v.
|
264
|
+
expect(v).to eq(NMatrix.new([4,1], [0, 1, 2, 3], dtype: :byte))
|
247
265
|
end
|
248
266
|
|
249
267
|
it "cindgen() creates a vector of complexes, sequentially" do
|
250
268
|
v = NVector.cindgen(2)
|
251
|
-
v.
|
269
|
+
expect(v).to eq(NMatrix.new([2,1], [Complex(0.0, 0.0), Complex(1.0, 0.0)], dtype: :complex64))
|
252
270
|
end
|
253
271
|
|
254
272
|
it "linspace() creates a vector with n values equally spaced between a and b" do
|
255
273
|
v = NVector.linspace(0, 2, 5)
|
256
|
-
v.
|
274
|
+
expect(v).to eq(NMatrix.new([5,1], [0.0, 0.5, 1.0, 1.5, 2.0]))
|
257
275
|
end
|
258
276
|
|
259
277
|
it "logspace() creates a vector with n values logarithmically spaced between decades 10^a and 10^b" do
|
260
278
|
v = NVector.logspace(0, 3, 4)
|
261
|
-
v.
|
279
|
+
expect(v).to eq(NMatrix.new([4,1], [1.0, 10.0, 100.0, 1000.0]))
|
262
280
|
end
|
263
281
|
end
|
264
282
|
|
@@ -268,6 +286,6 @@ describe "Inline constructor" do
|
|
268
286
|
m = NMatrix.new([2, 2], [1, 4, 6, 7])
|
269
287
|
n = NMatrix[[1, 4], [6, 7]]
|
270
288
|
|
271
|
-
m.
|
289
|
+
expect(m).to eq n
|
272
290
|
end
|
273
291
|
end
|
data/spec/slice_set_spec.rb
CHANGED
@@ -45,8 +45,8 @@ describe "Set slice operation" do
|
|
45
45
|
if stype == :yale
|
46
46
|
step "verify correct arrangement of Yale IJA and A arrays" do
|
47
47
|
@m.extend NMatrix::YaleFunctions
|
48
|
-
@m.yale_ija.
|
49
|
-
@m.yale_a.
|
48
|
+
expect(@m.yale_ija).to eq([4,6,8,10,1,2,0,2,0,1])
|
49
|
+
expect(@m.yale_a).to eq([0,4,8,0, 1,2,3,5,6,7])
|
50
50
|
end
|
51
51
|
end
|
52
52
|
|
@@ -54,30 +54,30 @@ describe "Set slice operation" do
|
|
54
54
|
n = @m.clone
|
55
55
|
old_val = @m[0,0]
|
56
56
|
@m[0,0] = 100
|
57
|
-
@m[0,0].
|
57
|
+
expect(@m[0,0]).to eq(100)
|
58
58
|
@m[0,0] = old_val
|
59
|
-
@m.
|
59
|
+
expect(@m).to eq(n)
|
60
60
|
end
|
61
61
|
|
62
62
|
if stype == :yale
|
63
63
|
n = @m.clone
|
64
64
|
step "set a row of entries" do
|
65
65
|
n[0,0..2] = 0
|
66
|
-
n[0,0..2].to_flat_array.
|
67
|
-
n[1,0..2].to_flat_array.
|
68
|
-
n[2,0..2].to_flat_array.
|
66
|
+
expect(n[0,0..2].to_flat_array).to eq([0,0,0])
|
67
|
+
expect(n[1,0..2].to_flat_array).to eq([3,4,5])
|
68
|
+
expect(n[2,0..2].to_flat_array).to eq([6,7,8])
|
69
69
|
end
|
70
70
|
|
71
71
|
step "set a second row of entries" do
|
72
72
|
n[2,0..2] = 0
|
73
|
-
n[2,0..2].to_flat_array.
|
74
|
-
n[1,0..2].to_flat_array.
|
73
|
+
expect(n[2,0..2].to_flat_array).to eq([0,0,0])
|
74
|
+
expect(n[1,0..2].to_flat_array).to eq([3,4,5])
|
75
75
|
end
|
76
76
|
|
77
77
|
step "reset both rows of entries" do
|
78
78
|
n[0,0..2] = [0,1,2]
|
79
79
|
n[2,0..2] = [6,7,8]
|
80
|
-
n.
|
80
|
+
expect(n).to eq(@m)
|
81
81
|
end
|
82
82
|
end
|
83
83
|
|
@@ -89,62 +89,62 @@ describe "Set slice operation" do
|
|
89
89
|
m[0..1,0..1] = 100
|
90
90
|
|
91
91
|
if stype == :yale
|
92
|
-
m.yale_ija.
|
93
|
-
m.yale_a.
|
92
|
+
expect(m.yale_ija).to eq([4, 6, 8, 10, 1, 2, 0, 2, 0, 1])
|
93
|
+
expect(m.yale_a).to eq([100, 100, 8, 0, 100, 2, 100, 5, 6, 7])
|
94
94
|
end
|
95
95
|
|
96
|
-
m[0..1,0..1].
|
97
|
-
m[2,0..1].
|
98
|
-
m[0..1,2].
|
96
|
+
expect(m[0..1,0..1]).to eq(slice_result_a)
|
97
|
+
expect(m[2,0..1]).to eq(@m[2,0..1])
|
98
|
+
expect(m[0..1,2]).to eq(@m[0..1,2])
|
99
99
|
end
|
100
100
|
|
101
101
|
step "set upper left-hand 2x2 corner to 0" do
|
102
102
|
m[0..1,0..1] = 0
|
103
103
|
if stype == :yale
|
104
|
-
[4,5,6,8,2,2,0,1].
|
105
|
-
[0,0,8,0,2,5,6,7].
|
104
|
+
expect([4,5,6,8,2,2,0,1]).to eq(m.yale_ija)
|
105
|
+
expect([0,0,8,0,2,5,6,7]).to eq(m.yale_a)
|
106
106
|
end
|
107
107
|
|
108
|
-
m[0..1,0..1].
|
108
|
+
expect(m[0..1,0..1]).to eq(slice_result_b)
|
109
109
|
end
|
110
110
|
|
111
111
|
m = @m.clone
|
112
112
|
step "set lower left-hand 2x2 corner to 100" do
|
113
113
|
m[1..2,0..1] = 100
|
114
|
-
m[1..2,0..1].
|
115
|
-
m[0,0..1].
|
116
|
-
m[1..2,2].
|
114
|
+
expect(m[1..2,0..1]).to eq(slice_result_a)
|
115
|
+
expect(m[0,0..1]).to eq(@m[0,0..1])
|
116
|
+
expect(m[1..2,2]).to eq(@m[1..2,2])
|
117
117
|
end
|
118
118
|
|
119
119
|
step "set lower left-hand 2x2 corner to 0" do
|
120
120
|
m[1..2,0..1] = 0
|
121
|
-
m[1..2,0..1].
|
121
|
+
expect(m[1..2,0..1]).to eq(slice_result_b)
|
122
122
|
end
|
123
123
|
|
124
124
|
m = @m.clone
|
125
125
|
step "set lower right-hand 2x2 corner to 100" do
|
126
126
|
m[1..2,1..2] = 100
|
127
|
-
m[1..2,1..2].
|
128
|
-
m[0,1..2].
|
129
|
-
m[1..2,0].
|
127
|
+
expect(m[1..2,1..2]).to eq(slice_result_a)
|
128
|
+
expect(m[0,1..2]).to eq(@m[0,1..2])
|
129
|
+
expect(m[1..2,0]).to eq(@m[1..2,0])
|
130
130
|
end
|
131
131
|
|
132
132
|
step "set lower right-hand 2x2 corner to 0" do
|
133
133
|
m[1..2,1..2] = 0
|
134
|
-
m[1..2,1..2].
|
134
|
+
expect(m[1..2,1..2]).to eq(slice_result_b)
|
135
135
|
end
|
136
136
|
|
137
137
|
m = @m.clone
|
138
138
|
step "set upper right-hand 2x2 corner to 100" do
|
139
139
|
m[0..1,1..2] = 100
|
140
|
-
m[0..1,1..2].
|
141
|
-
m[2,1..2].
|
142
|
-
m[0..1,0].
|
140
|
+
expect(m[0..1,1..2]).to eq(slice_result_a)
|
141
|
+
expect(m[2,1..2]).to eq(@m[2,1..2])
|
142
|
+
expect(m[0..1,0]).to eq(@m[0..1,0])
|
143
143
|
end
|
144
144
|
|
145
145
|
step "set upper right-hand 2x2 corner to 0" do
|
146
146
|
m[0..1,1..2] = 0
|
147
|
-
m[0..1,1..2].
|
147
|
+
expect(m[0..1,1..2]).to eq(slice_result_b)
|
148
148
|
end
|
149
149
|
end
|
150
150
|
|
@@ -152,7 +152,7 @@ describe "Set slice operation" do
|
|
152
152
|
x = NMatrix.new(4, stype: :yale, dtype: :int16)
|
153
153
|
x.extend NMatrix::YaleFunctions if stype == :yale
|
154
154
|
x[1..3,1..3] = @m
|
155
|
-
x.to_flat_array.
|
155
|
+
expect(x.to_flat_array).to eq([0,0,0,0, 0,0,1,2, 0,3,4,5, 0,6,7,8])
|
156
156
|
end
|
157
157
|
|
158
158
|
end
|
data/spec/stat_spec.rb
CHANGED
@@ -39,43 +39,43 @@ describe "Statistical functions" do
|
|
39
39
|
end
|
40
40
|
|
41
41
|
it "behaves like Enumerable#reduce with no argument to reduce" do
|
42
|
-
@nm_1d.reduce_along_dim(0) { |acc, el| acc + el }.to_f.
|
43
|
-
@nm_2d.reduce_along_dim(1) { |acc, el| acc + el }.
|
42
|
+
expect(@nm_1d.reduce_along_dim(0) { |acc, el| acc + el }.to_f).to eq 11 unless stype == :yale
|
43
|
+
expect(@nm_2d.reduce_along_dim(1) { |acc, el| acc + el }).to eq NMatrix.new([2,1], [1.0, 5.0], stype: stype)
|
44
44
|
end
|
45
45
|
|
46
46
|
it "should calculate the mean along the specified dimension" do
|
47
47
|
unless stype == :yale then
|
48
48
|
puts @nm_1d.mean
|
49
|
-
@nm_1d.mean.
|
49
|
+
expect(@nm_1d.mean).to eq NMatrix.new([1], [2.2], stype: stype, dtype: :float64)
|
50
50
|
end
|
51
|
-
@nm_2d.mean.
|
52
|
-
@nm_2d.mean(1).
|
51
|
+
expect(@nm_2d.mean).to eq NMatrix[[1.0,2.0], stype: stype]
|
52
|
+
expect(@nm_2d.mean(1)).to eq NMatrix[[0.5], [2.5], stype: stype]
|
53
53
|
end
|
54
54
|
|
55
55
|
it "should calculate the minimum along the specified dimension" do
|
56
|
-
@nm_1d.min.
|
57
|
-
@nm_2d.min.
|
58
|
-
@nm_2d.min(1).
|
56
|
+
expect(@nm_1d.min).to eq 0.0 unless stype == :yale
|
57
|
+
expect(@nm_2d.min).to eq NMatrix[[0.0, 1.0], stype: stype]
|
58
|
+
expect(@nm_2d.min(1)).to eq NMatrix[[0.0], [2.0], stype: stype]
|
59
59
|
end
|
60
60
|
|
61
61
|
it "should calculate the maximum along the specified dimension" do
|
62
|
-
@nm_1d.max.
|
63
|
-
@nm_2d.max.
|
62
|
+
expect(@nm_1d.max).to eq 5.0 unless stype == :yale
|
63
|
+
expect(@nm_2d.max).to eq NMatrix[[2.0, 3.0], stype: stype]
|
64
64
|
end
|
65
65
|
|
66
66
|
it "should calculate the variance along the specified dimension" do
|
67
|
-
@nm_1d.variance.
|
68
|
-
@nm_2d.variance(1).
|
67
|
+
expect(@nm_1d.variance).to eq NMatrix[3.7, stype: stype] unless stype == :yale
|
68
|
+
expect(@nm_2d.variance(1)).to eq NMatrix[[0.5], [0.5], stype: stype]
|
69
69
|
end
|
70
70
|
|
71
71
|
it "should calculate the sum along the specified dimension" do
|
72
|
-
@nm_1d.sum.
|
73
|
-
@nm_2d.sum.
|
72
|
+
expect(@nm_1d.sum).to eq NMatrix[11.0, stype: stype] unless stype == :yale
|
73
|
+
expect(@nm_2d.sum).to eq NMatrix[[2.0, 4.0], stype: stype]
|
74
74
|
end
|
75
75
|
|
76
76
|
it "should calculate the standard deviation along the specified dimension" do
|
77
|
-
@nm_1d.std.
|
78
|
-
@nm_2d.std(1).
|
77
|
+
expect(@nm_1d.std).to eq NMatrix[Math.sqrt(3.7), stype: stype] unless stype == :yale
|
78
|
+
expect(@nm_2d.std(1)).to eq NMatrix[[Math.sqrt(0.5)], [Math.sqrt(0.5)], stype: stype]
|
79
79
|
end
|
80
80
|
|
81
81
|
it "should raise an ArgumentError when any invalid dimension is provided" do
|
@@ -84,9 +84,9 @@ describe "Statistical functions" do
|
|
84
84
|
end
|
85
85
|
|
86
86
|
it "should convert to float if it contains only a single element" do
|
87
|
-
NMatrix[4.0, stype: stype].to_f.
|
88
|
-
NMatrix[[[[4.0]]], stype: stype].to_f.
|
89
|
-
NMatrix[[4.0], stype: stype].to_f.
|
87
|
+
expect(NMatrix[4.0, stype: stype].to_f).to eq 4.0 unless stype == :yale
|
88
|
+
expect(NMatrix[[[[4.0]]], stype: stype].to_f).to eq 4.0 unless stype == :yale
|
89
|
+
expect(NMatrix[[4.0], stype: stype].to_f).to eq 4.0
|
90
90
|
end
|
91
91
|
|
92
92
|
it "should raise an index error if it contains more than a single element" do
|
@@ -96,8 +96,8 @@ describe "Statistical functions" do
|
|
96
96
|
|
97
97
|
it "should map a block to all elements" do
|
98
98
|
#binding.pry if stype == :list
|
99
|
-
@nm_1d.map { |e| e ** 2 }.
|
100
|
-
@nm_2d.map { |e| e ** 2 }.
|
99
|
+
expect(@nm_1d.map { |e| e ** 2 }).to eq NMatrix[25.0,0.0,1.0,4.0,9.0, stype: stype] unless stype == :yale
|
100
|
+
expect(@nm_2d.map { |e| e ** 2 }).to eq NMatrix[[0.0,1.0],[4.0,9.0], stype: stype]
|
101
101
|
end
|
102
102
|
|
103
103
|
it "should map! a block to all elements in place" do
|
@@ -105,39 +105,39 @@ describe "Statistical functions" do
|
|
105
105
|
unless stype == :yale then
|
106
106
|
expected1 = @nm_1d.map &fct
|
107
107
|
@nm_1d.map! &fct
|
108
|
-
@nm_1d.
|
108
|
+
expect(@nm_1d).to eq expected1
|
109
109
|
end
|
110
110
|
expected2 = @nm_2d.map &fct
|
111
111
|
@nm_2d.map! &fct
|
112
|
-
@nm_2d.
|
112
|
+
expect(@nm_2d).to eq expected2
|
113
113
|
end
|
114
114
|
|
115
115
|
it "should return an enumerator for map without a block" do
|
116
|
-
@nm_2d.map.
|
116
|
+
expect(@nm_2d.map).to be_a Enumerator
|
117
117
|
end
|
118
118
|
|
119
119
|
it "should return an enumerator for reduce without a block" do
|
120
|
-
@nm_2d.reduce_along_dim(0).
|
120
|
+
expect(@nm_2d.reduce_along_dim(0)).to be_a Enumerator
|
121
121
|
end
|
122
122
|
|
123
123
|
it "should return an enumerator for each_along_dim without a block" do
|
124
|
-
@nm_2d.each_along_dim(0).
|
124
|
+
expect(@nm_2d.each_along_dim(0)).to be_a Enumerator
|
125
125
|
end
|
126
126
|
|
127
127
|
it "should iterate correctly for map without a block" do
|
128
128
|
en = @nm_1d.map unless stype == :yale
|
129
|
-
en.each { |e| e**2 }.
|
129
|
+
expect(en.each { |e| e**2 }).to eq @nm_1d.map { |e| e**2 } unless stype == :yale
|
130
130
|
en = @nm_2d.map
|
131
|
-
en.each { |e| e**2 }.
|
131
|
+
expect(en.each { |e| e**2 }).to eq @nm_2d.map { |e| e**2 }
|
132
132
|
end
|
133
133
|
|
134
134
|
it "should iterate correctly for reduce without a block" do
|
135
135
|
unless stype == :yale then
|
136
136
|
en = @nm_1d.reduce_along_dim(0, 1.0)
|
137
|
-
en.each { |a, e| a+e }.to_f.
|
137
|
+
expect(en.each { |a, e| a+e }.to_f).to eq 12
|
138
138
|
end
|
139
139
|
en = @nm_2d.reduce_along_dim(1, 1.0)
|
140
|
-
en.each { |a, e| a+e }.
|
140
|
+
expect(en.each { |a, e| a+e }).to eq NMatrix[[2.0],[6.0], stype: stype]
|
141
141
|
end
|
142
142
|
|
143
143
|
it "should iterate correctly for each_along_dim without a block" do
|
@@ -145,31 +145,31 @@ describe "Statistical functions" do
|
|
145
145
|
res = NMatrix.zeros_like(@nm_1d[0...1])
|
146
146
|
en = @nm_1d.each_along_dim(0)
|
147
147
|
en.each { |e| res += e }
|
148
|
-
res.to_f.
|
148
|
+
expect(res.to_f).to eq 11
|
149
149
|
end
|
150
150
|
res = NMatrix.zeros_like (@nm_2d[0...2, 0])
|
151
151
|
en = @nm_2d.each_along_dim(1)
|
152
152
|
en.each { |e| res += e }
|
153
|
-
res.
|
153
|
+
expect(res).to eq NMatrix[[1.0], [5.0], stype: stype]
|
154
154
|
end
|
155
155
|
|
156
156
|
it "should yield matrices of matching dtype for each_along_dim" do
|
157
157
|
m = NMatrix.new([2,3], [1,2,3,3,4,5], dtype: :complex128, stype: stype)
|
158
158
|
m.each_along_dim(1) do |sub_m|
|
159
|
-
sub_m.dtype.
|
159
|
+
expect(sub_m.dtype).to eq :complex128
|
160
160
|
end
|
161
161
|
end
|
162
162
|
|
163
163
|
it "should reduce to a matrix of matching dtype for reduce_along_dim" do
|
164
164
|
m = NMatrix.new([2,3], [1,2,3,3,4,5], dtype: :complex128, stype: stype)
|
165
165
|
m.reduce_along_dim(1) do |acc, sub_m|
|
166
|
-
sub_m.dtype.
|
166
|
+
expect(sub_m.dtype).to eq :complex128
|
167
167
|
acc
|
168
168
|
end
|
169
169
|
|
170
170
|
m = NMatrix.new([2,3], [1,2,3,3,4,5], dtype: :complex128, stype: stype)
|
171
171
|
m.reduce_along_dim(1, 0.0) do |acc, sub_m|
|
172
|
-
sub_m.dtype.
|
172
|
+
expect(sub_m.dtype).to eq :complex128
|
173
173
|
acc
|
174
174
|
end
|
175
175
|
end
|
@@ -177,30 +177,30 @@ describe "Statistical functions" do
|
|
177
177
|
it "should allow overriding the dtype for reduce_along_dim" do
|
178
178
|
m = NMatrix[[1,2,3], [3,4,5], dtype: :complex128]
|
179
179
|
m.reduce_along_dim(1, 0.0, :float64) do |acc, sub_m|
|
180
|
-
acc.dtype.
|
180
|
+
expect(acc.dtype).to eq :float64
|
181
181
|
acc
|
182
182
|
end
|
183
183
|
|
184
184
|
m = NMatrix[[1,2,3], [3,4,5], dtype: :complex128, stype: stype]
|
185
185
|
m.reduce_along_dim(1, nil, :float64) do |acc, sub_m|
|
186
|
-
acc.dtype.
|
186
|
+
expect(acc.dtype).to eq :float64
|
187
187
|
acc
|
188
188
|
end
|
189
189
|
end
|
190
190
|
|
191
191
|
it "should convert integer dtypes to float when calculating mean" do
|
192
192
|
m = NMatrix[[1,2,3], [3,4,5], dtype: :int32, stype: stype]
|
193
|
-
m.mean(0).dtype.
|
193
|
+
expect(m.mean(0).dtype).to eq :float64
|
194
194
|
end
|
195
195
|
|
196
196
|
it "should convert integer dtypes to float when calculating variance" do
|
197
197
|
m = NMatrix[[1,2,3], [3,4,5], dtype: :int32, stype: stype]
|
198
|
-
m.variance(0).dtype.
|
198
|
+
expect(m.variance(0).dtype).to eq :float64
|
199
199
|
end
|
200
200
|
|
201
201
|
it "should convert integer dtypes to float when calculating standard deviation" do
|
202
202
|
m = NMatrix[[1,2,3], [3,4,5], dtype: :int32, stype: stype]
|
203
|
-
m.std(0).dtype.
|
203
|
+
expect(m.std(0).dtype).to eq :float64
|
204
204
|
end
|
205
205
|
end
|
206
206
|
end
|