nmatrix 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +3 -8
- data/.rspec +1 -1
- data/.travis.yml +12 -0
- data/CONTRIBUTING.md +27 -12
- data/Gemfile +1 -0
- data/History.txt +38 -0
- data/Manifest.txt +15 -15
- data/README.rdoc +7 -6
- data/Rakefile +40 -5
- data/ext/nmatrix/data/data.cpp +2 -37
- data/ext/nmatrix/data/data.h +19 -121
- data/ext/nmatrix/data/meta.h +70 -0
- data/ext/nmatrix/extconf.rb +40 -12
- data/ext/nmatrix/math/math.h +13 -103
- data/ext/nmatrix/nmatrix.cpp +10 -2018
- data/ext/nmatrix/nmatrix.h +16 -13
- data/ext/nmatrix/ruby_constants.cpp +12 -1
- data/ext/nmatrix/ruby_constants.h +7 -1
- data/ext/nmatrix/ruby_nmatrix.c +2169 -0
- data/ext/nmatrix/storage/dense.cpp +123 -14
- data/ext/nmatrix/storage/dense.h +10 -4
- data/ext/nmatrix/storage/list.cpp +265 -48
- data/ext/nmatrix/storage/list.h +6 -9
- data/ext/nmatrix/storage/storage.cpp +44 -54
- data/ext/nmatrix/storage/storage.h +2 -2
- data/ext/nmatrix/storage/yale/class.h +1070 -0
- data/ext/nmatrix/storage/yale/iterators/base.h +142 -0
- data/ext/nmatrix/storage/yale/iterators/iterator.h +130 -0
- data/ext/nmatrix/storage/yale/iterators/row.h +449 -0
- data/ext/nmatrix/storage/yale/iterators/row_stored.h +139 -0
- data/ext/nmatrix/storage/yale/iterators/row_stored_nd.h +167 -0
- data/ext/nmatrix/storage/yale/iterators/stored_diagonal.h +123 -0
- data/ext/nmatrix/storage/yale/math/transpose.h +110 -0
- data/ext/nmatrix/storage/yale/yale.cpp +1785 -0
- data/ext/nmatrix/storage/{yale.h → yale/yale.h} +23 -55
- data/ext/nmatrix/types.h +2 -0
- data/ext/nmatrix/util/io.cpp +27 -45
- data/ext/nmatrix/util/io.h +0 -2
- data/ext/nmatrix/util/sl_list.cpp +169 -28
- data/ext/nmatrix/util/sl_list.h +9 -3
- data/lib/nmatrix/blas.rb +20 -20
- data/lib/nmatrix/enumerate.rb +1 -1
- data/lib/nmatrix/io/mat5_reader.rb +8 -14
- data/lib/nmatrix/lapack.rb +3 -3
- data/lib/nmatrix/math.rb +3 -3
- data/lib/nmatrix/nmatrix.rb +19 -5
- data/lib/nmatrix/nvector.rb +2 -0
- data/lib/nmatrix/shortcuts.rb +90 -125
- data/lib/nmatrix/version.rb +1 -1
- data/nmatrix.gemspec +7 -8
- data/spec/{nmatrix_spec.rb → 00_nmatrix_spec.rb} +45 -208
- data/spec/01_enum_spec.rb +184 -0
- data/spec/{slice_spec.rb → 02_slice_spec.rb} +55 -39
- data/spec/blas_spec.rb +22 -54
- data/spec/elementwise_spec.rb +9 -8
- data/spec/io_spec.rb +6 -4
- data/spec/lapack_spec.rb +26 -26
- data/spec/math_spec.rb +9 -5
- data/spec/nmatrix_yale_spec.rb +29 -61
- data/spec/shortcuts_spec.rb +34 -22
- data/spec/slice_set_spec.rb +157 -0
- data/spec/spec_helper.rb +42 -2
- data/spec/stat_spec.rb +192 -0
- metadata +52 -55
- data/ext/nmatrix/storage/yale.cpp +0 -2284
- data/spec/nmatrix_list_spec.rb +0 -113
- data/spec/nvector_spec.rb +0 -112
data/spec/math_spec.rb
CHANGED
@@ -53,7 +53,11 @@ describe "math" do
|
|
53
53
|
begin
|
54
54
|
a.invert!
|
55
55
|
rescue NotImplementedError => e
|
56
|
-
|
56
|
+
if dtype.to_s =~ /rational/
|
57
|
+
pending "getri needs rational implementation"
|
58
|
+
else
|
59
|
+
pending e.to_s
|
60
|
+
end
|
57
61
|
end
|
58
62
|
a.should == b
|
59
63
|
end
|
@@ -89,8 +93,8 @@ describe "math" do
|
|
89
93
|
MATRIX32A_ARRAY
|
90
94
|
end
|
91
95
|
|
92
|
-
n = NMatrix.new([4,3], nary, left_dtype)
|
93
|
-
m = NMatrix.new([3,2], mary, right_dtype)
|
96
|
+
n = NMatrix.new([4,3], nary, dtype: left_dtype, stype: :dense)
|
97
|
+
m = NMatrix.new([3,2], mary, dtype: right_dtype, stype: :dense)
|
94
98
|
|
95
99
|
m.shape[0].should == 3
|
96
100
|
m.shape[1].should == 2
|
@@ -127,9 +131,9 @@ describe "math" do
|
|
127
131
|
it "dense handles #{left_dtype.to_s} dot #{right_dtype.to_s} vector multiplication" do
|
128
132
|
#STDERR.puts "dtype=#{dtype.to_s}"
|
129
133
|
#STDERR.puts "2"
|
130
|
-
n = NMatrix.new([4,3], [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0], left_dtype)
|
134
|
+
n = NMatrix.new([4,3], [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0], dtype: left_dtype)
|
131
135
|
|
132
|
-
m =
|
136
|
+
m = NMatrix.new([3,1], [2.0, 1.0, 0.0], dtype: right_dtype)
|
133
137
|
|
134
138
|
m.shape[0].should == 3
|
135
139
|
m.shape[1].should == 1
|
data/spec/nmatrix_yale_spec.rb
CHANGED
@@ -28,21 +28,17 @@ require "./lib/nmatrix"
|
|
28
28
|
|
29
29
|
describe NMatrix do
|
30
30
|
context :yale do
|
31
|
-
it "calculates itype" do
|
32
|
-
NMatrix.itype_by_shape([4,4]).should == :uint8
|
33
|
-
NMatrix.itype_by_shape(4).should == :uint8
|
34
|
-
## FIXME: Check larger shapes for correct itype
|
35
|
-
end
|
36
31
|
|
37
32
|
it "compares two empty matrices" do
|
38
|
-
n = NMatrix.new(:yale,
|
39
|
-
m = NMatrix.new(:yale,
|
33
|
+
n = NMatrix.new(4, stype: :yale, dtype: :float64)
|
34
|
+
m = NMatrix.new(4, stype: :yale, dtype: :float64)
|
40
35
|
n.should == m
|
41
36
|
end
|
42
37
|
|
43
38
|
it "compares two matrices following basic assignments" do
|
44
|
-
n = NMatrix.new(:yale,
|
45
|
-
m = NMatrix.new(:yale,
|
39
|
+
n = NMatrix.new(2, stype: :yale, dtype: :float64)
|
40
|
+
m = NMatrix.new(2, stype: :yale, dtype: :float64)
|
41
|
+
|
46
42
|
m[0,0] = 1
|
47
43
|
m[0,1] = 1
|
48
44
|
n.should_not == m
|
@@ -53,15 +49,16 @@ describe NMatrix do
|
|
53
49
|
end
|
54
50
|
|
55
51
|
it "compares two matrices following elementwise operations" do
|
56
|
-
n = NMatrix.new(:yale,
|
52
|
+
n = NMatrix.new(2, stype: :yale, dtype: :float64)
|
53
|
+
m = NMatrix.new(2, stype: :yale, dtype: :float64)
|
57
54
|
n[0,1] = 1
|
58
|
-
m = NMatrix.new(:yale, [2,2], :float64)
|
59
55
|
m[0,1] = -1
|
60
|
-
|
56
|
+
x = n+m
|
57
|
+
(n+m).should == NMatrix.new(2, 0.0, stype: :yale)
|
61
58
|
end
|
62
59
|
|
63
60
|
it "sets diagonal values" do
|
64
|
-
n = NMatrix.new(
|
61
|
+
n = NMatrix.new([2,3], stype: :yale, dtype: :float64)
|
65
62
|
n.extend(NMatrix::YaleFunctions)
|
66
63
|
n[1,1] = 0.1
|
67
64
|
n[0,0] = 0.2
|
@@ -69,7 +66,7 @@ describe NMatrix do
|
|
69
66
|
end
|
70
67
|
|
71
68
|
it "gets non-diagonal rows as hashes" do
|
72
|
-
n = NMatrix.new(
|
69
|
+
n = NMatrix.new([4,6], stype: :yale, dtype: :float64)
|
73
70
|
n.extend(NMatrix::YaleFunctions)
|
74
71
|
n[0,0] = 0.1
|
75
72
|
n[0,2] = 0.2
|
@@ -80,7 +77,7 @@ describe NMatrix do
|
|
80
77
|
end
|
81
78
|
|
82
79
|
it "gets non-diagonal occupied column indices for a given row" do
|
83
|
-
n = NMatrix.new(
|
80
|
+
n = NMatrix.new([4,6], stype: :yale, dtype: :float64)
|
84
81
|
n.extend(NMatrix::YaleFunctions)
|
85
82
|
n[0,0] = 0.1
|
86
83
|
n[0,2] = 0.2
|
@@ -91,7 +88,7 @@ describe NMatrix do
|
|
91
88
|
end
|
92
89
|
|
93
90
|
it "does not resize until necessary" do
|
94
|
-
n = NMatrix.new(
|
91
|
+
n = NMatrix.new([2,3], stype: :yale, dtype: :float64)
|
95
92
|
n.extend(NMatrix::YaleFunctions)
|
96
93
|
n.yale_size.should == 3
|
97
94
|
n.capacity.should == 5
|
@@ -104,7 +101,7 @@ describe NMatrix do
|
|
104
101
|
|
105
102
|
|
106
103
|
it "sets when not resizing" do
|
107
|
-
n = NMatrix.new(
|
104
|
+
n = NMatrix.new([2,3], stype: :yale, dtype: :float64)
|
108
105
|
n.extend(NMatrix::YaleFunctions)
|
109
106
|
n[0,0] = 0.1
|
110
107
|
n[0,1] = 0.2
|
@@ -114,7 +111,7 @@ describe NMatrix do
|
|
114
111
|
end
|
115
112
|
|
116
113
|
it "sets when resizing" do
|
117
|
-
n = NMatrix.new(
|
114
|
+
n = NMatrix.new([2,3], stype: :yale, dtype: :float64)
|
118
115
|
n.extend(NMatrix::YaleFunctions)
|
119
116
|
n[0,0] = 0.01
|
120
117
|
n[1,1] = 0.1
|
@@ -132,7 +129,8 @@ describe NMatrix do
|
|
132
129
|
|
133
130
|
associations = File.open('spec/nmatrix_yale_resize_test_associations.yaml') { |y| YAML::load(y) }
|
134
131
|
|
135
|
-
n = NMatrix.new(
|
132
|
+
n = NMatrix.new([618,2801], stype: :yale, dtype: :byte, capacity: associations.size)
|
133
|
+
#n = NMatrix.new(:yale, [618, 2801], associations.size, :byte)
|
136
134
|
|
137
135
|
associations.each_pair do |j,i|
|
138
136
|
n[i,j] = 1
|
@@ -145,7 +143,7 @@ describe NMatrix do
|
|
145
143
|
end
|
146
144
|
|
147
145
|
it "sets values within rows" do
|
148
|
-
n = NMatrix.new(
|
146
|
+
n = NMatrix.new([3,20], stype: :yale, dtype: :float64)
|
149
147
|
n.extend(NMatrix::YaleFunctions)
|
150
148
|
n[2,1] = 1.0
|
151
149
|
n[2,0] = 1.5
|
@@ -155,7 +153,7 @@ describe NMatrix do
|
|
155
153
|
end
|
156
154
|
|
157
155
|
it "gets values within rows" do
|
158
|
-
n = NMatrix.new(
|
156
|
+
n = NMatrix.new([3,20], stype: :yale, dtype: :float64)
|
159
157
|
n[2,1] = 1.0
|
160
158
|
n[2,0] = 1.5
|
161
159
|
n[2,15] = 2.0
|
@@ -165,7 +163,7 @@ describe NMatrix do
|
|
165
163
|
end
|
166
164
|
|
167
165
|
it "sets values within large rows" do
|
168
|
-
n = NMatrix.new(
|
166
|
+
n = NMatrix.new([10,300], stype: :yale, dtype: :float64)
|
169
167
|
n.extend(NMatrix::YaleFunctions)
|
170
168
|
n[5,1] = 1.0
|
171
169
|
n[5,0] = 1.5
|
@@ -182,7 +180,7 @@ describe NMatrix do
|
|
182
180
|
end
|
183
181
|
|
184
182
|
it "gets values within large rows" do
|
185
|
-
n = NMatrix.new(
|
183
|
+
n = NMatrix.new([10,300], stype: :yale, dtype: :float64)
|
186
184
|
n.extend(NMatrix::YaleFunctions)
|
187
185
|
n[5,1] = 1.0
|
188
186
|
n[5,0] = 1.5
|
@@ -202,7 +200,7 @@ describe NMatrix do
|
|
202
200
|
end
|
203
201
|
|
204
202
|
it "dots two identical matrices" do
|
205
|
-
a = NMatrix.new(:yale,
|
203
|
+
a = NMatrix.new(4, stype: :yale, dtype: :float64)
|
206
204
|
a[0,1] = 4.0
|
207
205
|
a[1,2] = 1.0
|
208
206
|
a[1,3] = 1.0
|
@@ -211,26 +209,13 @@ describe NMatrix do
|
|
211
209
|
b = a.dup
|
212
210
|
c = a.dot b
|
213
211
|
|
214
|
-
|
215
|
-
|
216
|
-
c
|
217
|
-
c[0,3].should == 4.0
|
218
|
-
c[1,0].should == 0.0
|
219
|
-
c[1,1].should == 2.0
|
220
|
-
c[1,2].should == 0.0
|
221
|
-
c[1,3].should == 0.0
|
222
|
-
c[2,0].should == 0.0
|
223
|
-
c[2,1].should == 0.0
|
224
|
-
c[2,2].should == 0.0
|
225
|
-
c[2,3].should == 0.0
|
226
|
-
c[3,0].should == 0.0
|
227
|
-
c[3,1].should == 0.0
|
228
|
-
c[3,2].should == 2.0
|
229
|
-
c[3,3].should == 2.0
|
212
|
+
d = NMatrix.new(4, [0,0,4,4, 0,2,0,0, 0,0,0,0, 0,0,2,2], dtype: :float64, stype: :yale)
|
213
|
+
|
214
|
+
c.should == d
|
230
215
|
end
|
231
216
|
|
232
217
|
it "dots two identical matrices where a positive and negative partial sum cancel on the diagonal" do
|
233
|
-
a = NMatrix.new(
|
218
|
+
a = NMatrix.new(4, 0.0, stype: :yale)
|
234
219
|
|
235
220
|
a[0,0] = 1.0
|
236
221
|
a[0,1] = 4.0
|
@@ -242,23 +227,6 @@ describe NMatrix do
|
|
242
227
|
b = a.dup
|
243
228
|
c = a.dot b
|
244
229
|
|
245
|
-
#c[0,0].should == 1.0
|
246
|
-
#c[0,1].should == 4.0
|
247
|
-
#c[0,2].should == 8.0
|
248
|
-
#c[0,3].should == -16.0
|
249
|
-
#c[1,0].should == 0.0
|
250
|
-
#c[1,1].should == -16.0
|
251
|
-
#c[1,2].should == 0.0
|
252
|
-
#c[1,3].should == -16.0
|
253
|
-
#c[2,0].should == 0.0
|
254
|
-
#c[2,1].should == 0.0
|
255
|
-
#c[2,2].should == 0.0
|
256
|
-
#c[2,3].should == 0.0
|
257
|
-
#c[3,0].should == 0.0
|
258
|
-
#c[3,1].should == 0.0
|
259
|
-
#c[3,2].should == 8.0
|
260
|
-
#c[3,3].should == 0.0 # this is the positive and negative partial sum cancel
|
261
|
-
|
262
230
|
c.extend(NMatrix::YaleFunctions)
|
263
231
|
|
264
232
|
c.yale_ija.reject { |i| i.nil? }.should == [5,8,9,9,11,1,2,3,3,1,2]
|
@@ -267,8 +235,8 @@ describe NMatrix do
|
|
267
235
|
end
|
268
236
|
|
269
237
|
it "dots two vectors" do
|
270
|
-
n =
|
271
|
-
m =
|
238
|
+
n = NMatrix.new([16,1], 0, stype: :yale)
|
239
|
+
m = NMatrix.new([1,16], 0, stype: :yale)
|
272
240
|
|
273
241
|
n[0] = m[0] = 1
|
274
242
|
n[1] = m[1] = 2
|
@@ -300,7 +268,7 @@ describe NMatrix do
|
|
300
268
|
end
|
301
269
|
|
302
270
|
it "transposes" do
|
303
|
-
a = NMatrix.new(
|
271
|
+
a = NMatrix.new(4, 0.0, stype: :yale)
|
304
272
|
a[0,0] = 1.0
|
305
273
|
a[0,1] = 4.0
|
306
274
|
a[1,2] = 2.0
|
data/spec/shortcuts_spec.rb
CHANGED
@@ -27,6 +27,7 @@
|
|
27
27
|
|
28
28
|
# Can we use require_relative here instead?
|
29
29
|
require File.join(File.dirname(__FILE__), "spec_helper.rb")
|
30
|
+
require 'pry'
|
30
31
|
|
31
32
|
describe NMatrix do
|
32
33
|
|
@@ -89,26 +90,12 @@ describe NMatrix do
|
|
89
90
|
|
90
91
|
2.times do |i|
|
91
92
|
2.times do |j|
|
92
|
-
m[i,
|
93
|
+
m[i,j].should == value
|
93
94
|
value += 1
|
94
95
|
end
|
95
96
|
end
|
96
97
|
end
|
97
98
|
|
98
|
-
it "seq() only accepts an integer or a 2-element array as dimension" do
|
99
|
-
m = NMatrix.seq([2, 2])
|
100
|
-
value = 0
|
101
|
-
|
102
|
-
2.times do |i|
|
103
|
-
2.times do |j|
|
104
|
-
m[i, j].should == value
|
105
|
-
value += 1
|
106
|
-
end
|
107
|
-
end
|
108
|
-
|
109
|
-
expect { NMatrix.seq([1, 2, 3]) }.to raise_error
|
110
|
-
expect { NMatrix.seq("not an array or integer") }.to raise_error
|
111
|
-
end
|
112
99
|
|
113
100
|
it "indgen() creates a matrix of integers as well as seq()" do
|
114
101
|
m = NMatrix.indgen(2) # 2x2 matrix.
|
@@ -186,6 +173,31 @@ describe NMatrix do
|
|
186
173
|
m[3,3].should eq(arr[3])
|
187
174
|
end
|
188
175
|
|
176
|
+
context "_like constructors" do
|
177
|
+
before :each do
|
178
|
+
STDERR.puts "starting GC"
|
179
|
+
GC.start
|
180
|
+
STDERR.puts "GC finished"
|
181
|
+
@nm_1d = NMatrix[5.0,0.0,1.0,2.0,3.0]
|
182
|
+
STDERR.puts "@nm_1d"
|
183
|
+
@nm_2d = NMatrix[[0.0,1.0],[2.0,3.0]]
|
184
|
+
STDERR.puts "@nm_2d"
|
185
|
+
end
|
186
|
+
|
187
|
+
it "should create an nmatrix of ones with dimensions and type the same as its argument" do
|
188
|
+
NMatrix.ones_like(@nm_1d).should eq NMatrix[1.0, 1.0, 1.0, 1.0, 1.0]
|
189
|
+
NMatrix.ones_like(@nm_2d).should eq NMatrix[[1.0, 1.0], [1.0, 1.0]]
|
190
|
+
end
|
191
|
+
|
192
|
+
it "should create an nmatrix of zeros with dimensions and type the same as its argument" do
|
193
|
+
STDERR.puts "A"
|
194
|
+
NMatrix.zeros_like(@nm_1d).should eq NMatrix[0.0, 0.0, 0.0, 0.0, 0.0]
|
195
|
+
STDERR.puts "B"
|
196
|
+
NMatrix.zeros_like(@nm_2d).should eq NMatrix[[0.0, 0.0], [0.0, 0.0]]
|
197
|
+
STDERR.puts "C"
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
189
201
|
end
|
190
202
|
|
191
203
|
describe "NVector" do
|
@@ -214,7 +226,7 @@ describe "NVector" do
|
|
214
226
|
|
215
227
|
it "seq() creates a vector of integers, sequentially" do
|
216
228
|
v = NVector.seq(7)
|
217
|
-
v.should ==
|
229
|
+
v.should == NMatrix.new([7,1], [0, 1, 2, 3, 4, 5, 6])
|
218
230
|
end
|
219
231
|
|
220
232
|
it "seq() only accepts integers as dimension" do
|
@@ -226,32 +238,32 @@ describe "NVector" do
|
|
226
238
|
|
227
239
|
it "indgen() creates a vector of integers as well as seq()" do
|
228
240
|
v = NVector.indgen(7)
|
229
|
-
v.should ==
|
241
|
+
v.should == NMatrix.new([7,1], [0, 1, 2, 3, 4, 5, 6])
|
230
242
|
end
|
231
243
|
|
232
244
|
it "findgen creates a vector of floats, sequentially" do
|
233
245
|
v = NVector.findgen(2)
|
234
|
-
v.should ==
|
246
|
+
v.should == NMatrix.new([2,1], [0.0, 1.0])
|
235
247
|
end
|
236
248
|
|
237
249
|
it "bindgen() creates a vector of bytes, sequentially" do
|
238
250
|
v = NVector.bindgen(4)
|
239
|
-
v.should ==
|
251
|
+
v.should == NMatrix.new([4,1], [0, 1, 2, 3], dtype: :byte)
|
240
252
|
end
|
241
253
|
|
242
254
|
it "cindgen() creates a vector of complexes, sequentially" do
|
243
255
|
v = NVector.cindgen(2)
|
244
|
-
v.should ==
|
256
|
+
v.should == NMatrix.new([2,1], [Complex(0.0, 0.0), Complex(1.0, 0.0)], dtype: :complex64)
|
245
257
|
end
|
246
258
|
|
247
259
|
it "linspace() creates a vector with n values equally spaced between a and b" do
|
248
260
|
v = NVector.linspace(0, 2, 5)
|
249
|
-
v.should ==
|
261
|
+
v.should == NMatrix.new([5,1], [0.0, 0.5, 1.0, 1.5, 2.0])
|
250
262
|
end
|
251
263
|
|
252
264
|
it "logspace() creates a vector with n values logarithmically spaced between decades 10^a and 10^b" do
|
253
265
|
v = NVector.logspace(0, 3, 4)
|
254
|
-
v.should ==
|
266
|
+
v.should == NMatrix.new([4,1], [1.0, 10.0, 100.0, 1000.0])
|
255
267
|
end
|
256
268
|
end
|
257
269
|
|
@@ -0,0 +1,157 @@
|
|
1
|
+
# = NMatrix
|
2
|
+
#
|
3
|
+
# A linear algebra library for scientific computation in Ruby.
|
4
|
+
# NMatrix is part of SciRuby.
|
5
|
+
#
|
6
|
+
# NMatrix was originally inspired by and derived from NArray, by
|
7
|
+
# Masahiro Tanaka: http://narray.rubyforge.org
|
8
|
+
#
|
9
|
+
# == Copyright Information
|
10
|
+
#
|
11
|
+
# SciRuby is Copyright (c) 2010 - 2012, Ruby Science Foundation
|
12
|
+
# NMatrix is Copyright (c) 2012, Ruby Science Foundation
|
13
|
+
#
|
14
|
+
# Please see LICENSE.txt for additional copyright notices.
|
15
|
+
#
|
16
|
+
# == Contributing
|
17
|
+
#
|
18
|
+
# By contributing source code to SciRuby, you agree to be bound by
|
19
|
+
# our Contributor Agreement:
|
20
|
+
#
|
21
|
+
# * https://github.com/SciRuby/sciruby/wiki/Contributor-Agreement
|
22
|
+
#
|
23
|
+
# == slice_set_spec.rb
|
24
|
+
#
|
25
|
+
# Test of slice set operations.
|
26
|
+
|
27
|
+
require 'pry'
|
28
|
+
require File.dirname(__FILE__) + "/spec_helper.rb"
|
29
|
+
|
30
|
+
describe "Set slice operation" do
|
31
|
+
include RSpec::Longrun::DSL
|
32
|
+
|
33
|
+
[:dense, :yale, :list].each do |stype|
|
34
|
+
context "for #{stype}" do
|
35
|
+
before :each do
|
36
|
+
@m = create_matrix(stype)
|
37
|
+
end
|
38
|
+
|
39
|
+
example "set and unset a range of entries with single values" do
|
40
|
+
|
41
|
+
if stype == :yale
|
42
|
+
step "verify correct arrangement of Yale IJA and A arrays" do
|
43
|
+
@m.extend NMatrix::YaleFunctions
|
44
|
+
@m.yale_ija.should == [4,6,8,10,1,2,0,2,0,1]
|
45
|
+
@m.yale_a.should == [0,4,8,0, 1,2,3,5,6,7]
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
step "set and reset a single entry" do
|
50
|
+
n = @m.clone
|
51
|
+
old_val = @m[0,0]
|
52
|
+
@m[0,0] = 100
|
53
|
+
@m[0,0].should == 100
|
54
|
+
@m[0,0] = old_val
|
55
|
+
@m.should == n
|
56
|
+
end
|
57
|
+
|
58
|
+
if stype == :yale
|
59
|
+
n = @m.clone
|
60
|
+
step "set a row of entries" do
|
61
|
+
n[0,0..2] = 0
|
62
|
+
n[0,0..2].to_flat_array.should == [0,0,0]
|
63
|
+
n[1,0..2].to_flat_array.should == [3,4,5]
|
64
|
+
n[2,0..2].to_flat_array.should == [6,7,8]
|
65
|
+
end
|
66
|
+
|
67
|
+
step "set a second row of entries" do
|
68
|
+
n[2,0..2] = 0
|
69
|
+
n[2,0..2].to_flat_array.should == [0,0,0]
|
70
|
+
n[1,0..2].to_flat_array.should == [3,4,5]
|
71
|
+
end
|
72
|
+
|
73
|
+
step "reset both rows of entries" do
|
74
|
+
n[0,0..2] = [0,1,2]
|
75
|
+
n[2,0..2] = [6,7,8]
|
76
|
+
n.should == @m
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
slice_result_a = NMatrix.new(:dense, 2, 100, @m.dtype).cast(stype)
|
81
|
+
slice_result_b = NMatrix.new(:dense, 2, 0, @m.dtype).cast(stype)
|
82
|
+
m = @m.clone
|
83
|
+
|
84
|
+
step "set upper left-hand 2x2 corner to 100" do
|
85
|
+
m[0..1,0..1] = 100
|
86
|
+
|
87
|
+
if stype == :yale
|
88
|
+
m.yale_ija.should == [4, 6, 8, 10, 1, 2, 0, 2, 0, 1]
|
89
|
+
m.yale_a.should == [100, 100, 8, 0, 100, 2, 100, 5, 6, 7]
|
90
|
+
end
|
91
|
+
|
92
|
+
m[0..1,0..1].should == slice_result_a
|
93
|
+
m[2,0..1].should == @m[2,0..1]
|
94
|
+
m[0..1,2].should == @m[0..1,2]
|
95
|
+
end
|
96
|
+
|
97
|
+
step "set upper left-hand 2x2 corner to 0" do
|
98
|
+
m[0..1,0..1] = 0
|
99
|
+
if stype == :yale
|
100
|
+
[4,5,6,8,2,2,0,1].should == m.yale_ija
|
101
|
+
[0,0,8,0,2,5,6,7].should == m.yale_a
|
102
|
+
end
|
103
|
+
|
104
|
+
m[0..1,0..1].should == slice_result_b
|
105
|
+
end
|
106
|
+
|
107
|
+
m = @m.clone
|
108
|
+
step "set lower left-hand 2x2 corner to 100" do
|
109
|
+
m[1..2,0..1] = 100
|
110
|
+
m[1..2,0..1].should == slice_result_a
|
111
|
+
m[0,0..1].should == @m[0,0..1]
|
112
|
+
m[1..2,2].should == @m[1..2,2]
|
113
|
+
end
|
114
|
+
|
115
|
+
step "set lower left-hand 2x2 corner to 0" do
|
116
|
+
m[1..2,0..1] = 0
|
117
|
+
m[1..2,0..1].should == slice_result_b
|
118
|
+
end
|
119
|
+
|
120
|
+
m = @m.clone
|
121
|
+
step "set lower right-hand 2x2 corner to 100" do
|
122
|
+
m[1..2,1..2] = 100
|
123
|
+
m[1..2,1..2].should == slice_result_a
|
124
|
+
m[0,1..2].should == @m[0,1..2]
|
125
|
+
m[1..2,0].should == @m[1..2,0]
|
126
|
+
end
|
127
|
+
|
128
|
+
step "set lower right-hand 2x2 corner to 0" do
|
129
|
+
m[1..2,1..2] = 0
|
130
|
+
m[1..2,1..2].should == slice_result_b
|
131
|
+
end
|
132
|
+
|
133
|
+
m = @m.clone
|
134
|
+
step "set upper right-hand 2x2 corner to 100" do
|
135
|
+
m[0..1,1..2] = 100
|
136
|
+
m[0..1,1..2].should == slice_result_a
|
137
|
+
m[2,1..2].should == @m[2,1..2]
|
138
|
+
m[0..1,0].should == @m[0..1,0]
|
139
|
+
end
|
140
|
+
|
141
|
+
step "set upper right-hand 2x2 corner to 0" do
|
142
|
+
m[0..1,1..2] = 0
|
143
|
+
m[0..1,1..2].should == slice_result_b
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
example "set a range of values to a matrix's contents" do
|
148
|
+
x = NMatrix.new(4, stype: :yale, dtype: :int16)
|
149
|
+
x.extend NMatrix::YaleFunctions if stype == :yale
|
150
|
+
x[1..3,1..3] = @m
|
151
|
+
x.to_flat_array.should == [0,0,0,0, 0,0,1,2, 0,3,4,5, 0,6,7,8]
|
152
|
+
end
|
153
|
+
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
end
|