nmatrix-atlas 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/ext/nmatrix/data/complex.h +364 -0
- data/ext/nmatrix/data/data.h +638 -0
- data/ext/nmatrix/data/meta.h +64 -0
- data/ext/nmatrix/data/ruby_object.h +389 -0
- data/ext/nmatrix/math/asum.h +120 -0
- data/ext/nmatrix/math/cblas_enums.h +36 -0
- data/ext/nmatrix/math/cblas_templates_core.h +507 -0
- data/ext/nmatrix/math/gemm.h +241 -0
- data/ext/nmatrix/math/gemv.h +178 -0
- data/ext/nmatrix/math/getrf.h +255 -0
- data/ext/nmatrix/math/getrs.h +121 -0
- data/ext/nmatrix/math/imax.h +79 -0
- data/ext/nmatrix/math/laswp.h +165 -0
- data/ext/nmatrix/math/long_dtype.h +49 -0
- data/ext/nmatrix/math/math.h +744 -0
- data/ext/nmatrix/math/nrm2.h +160 -0
- data/ext/nmatrix/math/rot.h +117 -0
- data/ext/nmatrix/math/rotg.h +106 -0
- data/ext/nmatrix/math/scal.h +71 -0
- data/ext/nmatrix/math/trsm.h +332 -0
- data/ext/nmatrix/math/util.h +148 -0
- data/ext/nmatrix/nm_memory.h +60 -0
- data/ext/nmatrix/nmatrix.h +408 -0
- data/ext/nmatrix/ruby_constants.h +106 -0
- data/ext/nmatrix/storage/common.h +176 -0
- data/ext/nmatrix/storage/dense/dense.h +128 -0
- data/ext/nmatrix/storage/list/list.h +137 -0
- data/ext/nmatrix/storage/storage.h +98 -0
- data/ext/nmatrix/storage/yale/class.h +1139 -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 +168 -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.h +202 -0
- data/ext/nmatrix/types.h +54 -0
- data/ext/nmatrix/util/io.h +115 -0
- data/ext/nmatrix/util/sl_list.h +143 -0
- data/ext/nmatrix/util/util.h +78 -0
- data/ext/nmatrix_atlas/extconf.rb +250 -0
- data/ext/nmatrix_atlas/math_atlas.cpp +1206 -0
- data/ext/nmatrix_atlas/math_atlas/cblas_templates_atlas.h +72 -0
- data/ext/nmatrix_atlas/math_atlas/clapack_templates.h +332 -0
- data/ext/nmatrix_atlas/math_atlas/geev.h +82 -0
- data/ext/nmatrix_atlas/math_atlas/gesdd.h +83 -0
- data/ext/nmatrix_atlas/math_atlas/gesvd.h +81 -0
- data/ext/nmatrix_atlas/math_atlas/inc.h +47 -0
- data/ext/nmatrix_atlas/nmatrix_atlas.cpp +44 -0
- data/lib/nmatrix/atlas.rb +213 -0
- data/lib/nmatrix/lapack_ext_common.rb +69 -0
- data/spec/00_nmatrix_spec.rb +730 -0
- data/spec/01_enum_spec.rb +190 -0
- data/spec/02_slice_spec.rb +389 -0
- data/spec/03_nmatrix_monkeys_spec.rb +78 -0
- data/spec/2x2_dense_double.mat +0 -0
- data/spec/4x4_sparse.mat +0 -0
- data/spec/4x5_dense.mat +0 -0
- data/spec/blas_spec.rb +193 -0
- data/spec/elementwise_spec.rb +303 -0
- data/spec/homogeneous_spec.rb +99 -0
- data/spec/io/fortran_format_spec.rb +88 -0
- data/spec/io/harwell_boeing_spec.rb +98 -0
- data/spec/io/test.rua +9 -0
- data/spec/io_spec.rb +149 -0
- data/spec/lapack_core_spec.rb +482 -0
- data/spec/leakcheck.rb +16 -0
- data/spec/math_spec.rb +730 -0
- data/spec/nmatrix_yale_resize_test_associations.yaml +2802 -0
- data/spec/nmatrix_yale_spec.rb +286 -0
- data/spec/plugins/atlas/atlas_spec.rb +242 -0
- data/spec/rspec_monkeys.rb +56 -0
- data/spec/rspec_spec.rb +34 -0
- data/spec/shortcuts_spec.rb +310 -0
- data/spec/slice_set_spec.rb +157 -0
- data/spec/spec_helper.rb +140 -0
- data/spec/stat_spec.rb +203 -0
- data/spec/test.pcd +20 -0
- data/spec/utm5940.mtx +83844 -0
- metadata +159 -0
@@ -0,0 +1,56 @@
|
|
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 - 2014, Ruby Science Foundation
|
12
|
+
# NMatrix is Copyright (c) 2012 - 2014, John Woods and the 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
|
+
# == rspec_monkeys.rb
|
24
|
+
#
|
25
|
+
# A set of monkey patches for RSpec allowing checks of NMatrix types
|
26
|
+
#
|
27
|
+
|
28
|
+
module RSpec::Matchers::BuiltIn
|
29
|
+
class BeWithin
|
30
|
+
|
31
|
+
def of(expected)
|
32
|
+
@expected = expected
|
33
|
+
@unit = ''
|
34
|
+
if expected.is_a?(NMatrix)
|
35
|
+
@tolerance = if @delta.is_a?(NMatrix)
|
36
|
+
@delta.clone
|
37
|
+
elsif @delta.is_a?(Array)
|
38
|
+
NMatrix.new(:dense, expected.shape, @delta, expected.dtype)
|
39
|
+
else
|
40
|
+
NMatrix.ones_like(expected) * @delta
|
41
|
+
end
|
42
|
+
else
|
43
|
+
@tolerance = @delta
|
44
|
+
end
|
45
|
+
|
46
|
+
self
|
47
|
+
end
|
48
|
+
|
49
|
+
def percent_of(expected)
|
50
|
+
@expected = expected
|
51
|
+
@unit = '%'
|
52
|
+
@tolerance = @expected.abs * @delta / 100.0 # <- only change is to reverse abs and @delta
|
53
|
+
self
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
data/spec/rspec_spec.rb
ADDED
@@ -0,0 +1,34 @@
|
|
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 - 2014, Ruby Science Foundation
|
12
|
+
# NMatrix is Copyright (c) 2012 - 2014, John Woods and the 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
|
+
# == rspec_spec.rb
|
24
|
+
#
|
25
|
+
# A spec for testing monkey patches to RSpec for NMatrix.
|
26
|
+
#
|
27
|
+
require 'spec_helper'
|
28
|
+
|
29
|
+
describe "RSpec" do
|
30
|
+
it "should permit #be_within to be used on a dense NMatrix" do
|
31
|
+
expect(NMatrix.new([4,1], 1.0, dtype: :complex128, stype: :dense) / 10000.0).to be_within(0.00000001).of(NMatrix.new([4,1], 0.0001, dtype: :float64, stype: :dense))
|
32
|
+
expect(NMatrix.new([4,1], 1.0, dtype: :complex128, stype: :dense) / 10000.0).not_to be_within(0.00000001).of(NMatrix.new([4,1], 1.0, dtype: :float64, stype: :dense))
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,310 @@
|
|
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 - 2014, Ruby Science Foundation
|
12
|
+
# NMatrix is Copyright (c) 2012 - 2014, John Woods and the 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
|
+
# == shortcuts_spec.rb
|
24
|
+
#
|
25
|
+
# Specs for the shortcuts used in NMatrix and in NVector.
|
26
|
+
#
|
27
|
+
|
28
|
+
require 'spec_helper'
|
29
|
+
require 'pry'
|
30
|
+
|
31
|
+
describe NMatrix do
|
32
|
+
it "zeros() creates a matrix of zeros" do
|
33
|
+
m = NMatrix.zeros(3)
|
34
|
+
n = NMatrix.new([3, 3], 0)
|
35
|
+
|
36
|
+
expect(m).to eq n
|
37
|
+
end
|
38
|
+
|
39
|
+
it "ones() creates a matrix of ones" do
|
40
|
+
m = NMatrix.ones(3)
|
41
|
+
n = NMatrix.new([3, 3], 1)
|
42
|
+
|
43
|
+
expect(m).to eq n
|
44
|
+
end
|
45
|
+
|
46
|
+
it "eye() creates an identity matrix" do
|
47
|
+
m = NMatrix.eye(3)
|
48
|
+
identity3 = NMatrix.new([3, 3], [1, 0, 0, 0, 1, 0, 0, 0, 1])
|
49
|
+
|
50
|
+
expect(m).to eq identity3
|
51
|
+
end
|
52
|
+
|
53
|
+
it "diag() creates a matrix with pre-supplied diagonal" do
|
54
|
+
arr = [1,2,3,4]
|
55
|
+
m = NMatrix.diag(arr)
|
56
|
+
expect(m.is_a?(NMatrix)).to be_true
|
57
|
+
end
|
58
|
+
|
59
|
+
it "diagonals() contains the seeded values on the diagonal" do
|
60
|
+
arr = [1,2,3,4]
|
61
|
+
m = NMatrix.diagonals(arr)
|
62
|
+
expect(m[0,0]).to eq(arr[0])
|
63
|
+
expect(m[1,1]).to eq(arr[1])
|
64
|
+
expect(m[2,2]).to eq(arr[2])
|
65
|
+
expect(m[3,3]).to eq(arr[3])
|
66
|
+
end
|
67
|
+
|
68
|
+
ALL_DTYPES.each do |dtype|
|
69
|
+
[:dense, :yale, :list].each do |stype|
|
70
|
+
context "#block_diagonal #{dtype} #{stype}" do
|
71
|
+
it "block_diagonal() creates a block-diagonal NMatrix" do
|
72
|
+
a = NMatrix.new([2,2], [1,2,
|
73
|
+
3,4])
|
74
|
+
b = NMatrix.new([1,1], [123.0])
|
75
|
+
c = NMatrix.new([3,3], [1,2,3,
|
76
|
+
1,2,3,
|
77
|
+
1,2,3])
|
78
|
+
d = Array[ [1,1,1], [2,2,2], [3,3,3] ]
|
79
|
+
e = 12
|
80
|
+
m = NMatrix.block_diagonal(a, b, c, d, e, dtype: dtype, stype: stype)
|
81
|
+
expect(m).to eq(NMatrix.new([10,10], [1, 2, 0, 0, 0, 0, 0, 0, 0, 0,
|
82
|
+
3, 4, 0, 0, 0, 0, 0, 0, 0, 0,
|
83
|
+
0, 0, 123, 0, 0, 0, 0, 0, 0, 0,
|
84
|
+
0, 0, 0, 1, 2, 3, 0, 0, 0, 0,
|
85
|
+
0, 0, 0, 1, 2, 3, 0, 0, 0, 0,
|
86
|
+
0, 0, 0, 1, 2, 3, 0, 0, 0, 0,
|
87
|
+
0, 0, 0, 0, 0, 0, 1, 1, 1, 0,
|
88
|
+
0, 0, 0, 0, 0, 0, 2, 2, 2, 0,
|
89
|
+
0, 0, 0, 0, 0, 0, 3, 3, 3, 0,
|
90
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 12], dtype: dtype, stype: stype))
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
context "::random" do
|
97
|
+
it "creates a matrix of random numbers" do
|
98
|
+
m = NMatrix.random(2)
|
99
|
+
|
100
|
+
expect(m.stype).to eq(:dense)
|
101
|
+
expect(m.dtype).to eq(:float64)
|
102
|
+
end
|
103
|
+
|
104
|
+
it "creates a complex matrix of random numbers" do
|
105
|
+
m = NMatrix.random(2, :dtype => :complex128)
|
106
|
+
end
|
107
|
+
|
108
|
+
it "correctly accepts :scale parameter" do
|
109
|
+
m = NMatrix.random([2,2], dtype: :byte, scale: 255)
|
110
|
+
m.each do |v|
|
111
|
+
expect(v).to be >= 0
|
112
|
+
expect(v).to be < 255
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
it "only accepts an integer or an array as dimension" do
|
117
|
+
m = NMatrix.random([2, 2])
|
118
|
+
|
119
|
+
expect(m.stype).to eq(:dense)
|
120
|
+
expect(m.dtype).to eq(:float64)
|
121
|
+
|
122
|
+
expect { NMatrix.random(2.0) }.to raise_error
|
123
|
+
expect { NMatrix.random("not an array or integer") }.to raise_error
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
it "seq() creates a matrix of integers, sequentially" do
|
128
|
+
m = NMatrix.seq(2) # 2x2 matrix.
|
129
|
+
value = 0
|
130
|
+
|
131
|
+
2.times do |i|
|
132
|
+
2.times do |j|
|
133
|
+
expect(m[i,j]).to eq(value)
|
134
|
+
value += 1
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
|
140
|
+
it "indgen() creates a matrix of integers as well as seq()" do
|
141
|
+
m = NMatrix.indgen(2) # 2x2 matrix.
|
142
|
+
value = 0
|
143
|
+
|
144
|
+
2.times do |i|
|
145
|
+
2.times do |j|
|
146
|
+
expect(m[i, j]).to eq(value)
|
147
|
+
value += 1
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
it "findgen creates a matrix of floats, sequentially" do
|
153
|
+
m = NMatrix.findgen(2) # 2x2 matrix.
|
154
|
+
value = 0
|
155
|
+
|
156
|
+
2.times do |i|
|
157
|
+
2.times do |j|
|
158
|
+
expect(m[i, j]/10).to be_within(Float::EPSILON).of(value.to_f/10)
|
159
|
+
value += 1
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
it "bindgen() creates a matrix of bytes" do
|
165
|
+
m = NMatrix.bindgen(2) # 2x2 matrix.
|
166
|
+
value = 0
|
167
|
+
|
168
|
+
2.times do |i|
|
169
|
+
2.times do |j|
|
170
|
+
expect(m[i, j]).to eq(value)
|
171
|
+
value += 1
|
172
|
+
end
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
it "cindgen() creates a matrix of complexes" do
|
177
|
+
m = NMatrix.cindgen(2) # 2x2 matrix.
|
178
|
+
value = 0
|
179
|
+
|
180
|
+
2.times do |i|
|
181
|
+
2.times do |j|
|
182
|
+
expect(m[i, j].real).to be_within(Float::EPSILON).of(value)
|
183
|
+
expect(m[i, j].imag).to be_within(Float::EPSILON).of(0.0)
|
184
|
+
value += 1
|
185
|
+
end
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
it "column() returns a NMatrix" do
|
190
|
+
m = NMatrix.random(3)
|
191
|
+
|
192
|
+
expect(m.column(2).is_a?(NMatrix)).to be_true
|
193
|
+
end
|
194
|
+
|
195
|
+
it "row() returns a NMatrix" do
|
196
|
+
m = NMatrix.random(3)
|
197
|
+
|
198
|
+
expect(m.row(2).is_a?(NMatrix)).to be_true
|
199
|
+
end
|
200
|
+
|
201
|
+
it "diagonals() creates an NMatrix" do
|
202
|
+
arr = [1,2,3,4]
|
203
|
+
m = NMatrix.diagonals(arr)
|
204
|
+
expect(m.is_a?(NMatrix)).to be_true
|
205
|
+
end
|
206
|
+
|
207
|
+
it "diagonals() contains the seeded values on the diagonal" do
|
208
|
+
arr = [1,2,3,4]
|
209
|
+
m = NMatrix.diagonals(arr)
|
210
|
+
expect(m[0,0]).to eq(arr[0])
|
211
|
+
expect(m[1,1]).to eq(arr[1])
|
212
|
+
expect(m[2,2]).to eq(arr[2])
|
213
|
+
expect(m[3,3]).to eq(arr[3])
|
214
|
+
end
|
215
|
+
|
216
|
+
context "_like constructors" do
|
217
|
+
before :each do
|
218
|
+
@nm_1d = NMatrix[5.0,0.0,1.0,2.0,3.0]
|
219
|
+
@nm_2d = NMatrix[[0.0,1.0],[2.0,3.0]]
|
220
|
+
end
|
221
|
+
|
222
|
+
it "should create an nmatrix of ones with dimensions and type the same as its argument" do
|
223
|
+
expect(NMatrix.ones_like(@nm_1d)).to eq NMatrix[1.0, 1.0, 1.0, 1.0, 1.0]
|
224
|
+
expect(NMatrix.ones_like(@nm_2d)).to eq NMatrix[[1.0, 1.0], [1.0, 1.0]]
|
225
|
+
end
|
226
|
+
|
227
|
+
it "should create an nmatrix of zeros with dimensions and type the same as its argument" do
|
228
|
+
expect(NMatrix.zeros_like(@nm_1d)).to eq NMatrix[0.0, 0.0, 0.0, 0.0, 0.0]
|
229
|
+
expect(NMatrix.zeros_like(@nm_2d)).to eq NMatrix[[0.0, 0.0], [0.0, 0.0]]
|
230
|
+
end
|
231
|
+
end
|
232
|
+
|
233
|
+
end
|
234
|
+
|
235
|
+
describe "NVector" do
|
236
|
+
|
237
|
+
it "zeros() creates a vector of zeros" do
|
238
|
+
v = NVector.zeros(4)
|
239
|
+
|
240
|
+
4.times do |i|
|
241
|
+
expect(v[i]).to eq(0)
|
242
|
+
end
|
243
|
+
end
|
244
|
+
|
245
|
+
it "ones() creates a vector of ones" do
|
246
|
+
v = NVector.ones(3)
|
247
|
+
|
248
|
+
3.times do |i|
|
249
|
+
expect(v[i]).to eq(1)
|
250
|
+
end
|
251
|
+
end
|
252
|
+
|
253
|
+
it "random() creates a vector of random numbers" do
|
254
|
+
v = NVector.random(4)
|
255
|
+
expect(v.dtype).to eq(:float64)
|
256
|
+
expect(v.stype).to eq(:dense)
|
257
|
+
end
|
258
|
+
|
259
|
+
it "seq() creates a vector of integers, sequentially" do
|
260
|
+
v = NVector.seq(7)
|
261
|
+
expect(v).to eq(NMatrix.new([7,1], [0, 1, 2, 3, 4, 5, 6]))
|
262
|
+
end
|
263
|
+
|
264
|
+
it "seq() only accepts integers as dimension" do
|
265
|
+
expect { NVector.seq(3) }.to_not raise_error
|
266
|
+
|
267
|
+
expect { NVector.seq([1, 3]) }.to raise_error
|
268
|
+
expect { NVector.seq(:wtf) }.to raise_error
|
269
|
+
end
|
270
|
+
|
271
|
+
it "indgen() creates a vector of integers as well as seq()" do
|
272
|
+
v = NVector.indgen(7)
|
273
|
+
expect(v).to eq(NMatrix.new([7,1], [0, 1, 2, 3, 4, 5, 6]))
|
274
|
+
end
|
275
|
+
|
276
|
+
it "findgen creates a vector of floats, sequentially" do
|
277
|
+
v = NVector.findgen(2)
|
278
|
+
expect(v).to eq(NMatrix.new([2,1], [0.0, 1.0]))
|
279
|
+
end
|
280
|
+
|
281
|
+
it "bindgen() creates a vector of bytes, sequentially" do
|
282
|
+
v = NVector.bindgen(4)
|
283
|
+
expect(v).to eq(NMatrix.new([4,1], [0, 1, 2, 3], dtype: :byte))
|
284
|
+
end
|
285
|
+
|
286
|
+
it "cindgen() creates a vector of complexes, sequentially" do
|
287
|
+
v = NVector.cindgen(2)
|
288
|
+
expect(v).to eq(NMatrix.new([2,1], [Complex(0.0, 0.0), Complex(1.0, 0.0)], dtype: :complex64))
|
289
|
+
end
|
290
|
+
|
291
|
+
it "linspace() creates a vector with n values equally spaced between a and b" do
|
292
|
+
v = NVector.linspace(0, 2, 5)
|
293
|
+
expect(v).to eq(NMatrix.new([5,1], [0.0, 0.5, 1.0, 1.5, 2.0]))
|
294
|
+
end
|
295
|
+
|
296
|
+
it "logspace() creates a vector with n values logarithmically spaced between decades 10^a and 10^b" do
|
297
|
+
v = NVector.logspace(0, 3, 4)
|
298
|
+
expect(v).to eq(NMatrix.new([4,1], [1.0, 10.0, 100.0, 1000.0]))
|
299
|
+
end
|
300
|
+
end
|
301
|
+
|
302
|
+
describe "Inline constructor" do
|
303
|
+
|
304
|
+
it "creates a NMatrix with the given values" do
|
305
|
+
m = NMatrix.new([2, 2], [1, 4, 6, 7])
|
306
|
+
n = NMatrix[[1, 4], [6, 7]]
|
307
|
+
|
308
|
+
expect(m).to eq n
|
309
|
+
end
|
310
|
+
end
|
@@ -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 - 2014, Ruby Science Foundation
|
12
|
+
# NMatrix is Copyright (c) 2012 - 2014, John Woods and the 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 'spec_helper'
|
28
|
+
require 'pry'
|
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
|
+
expect(@m.yale_ija).to eq([4,6,8,10,1,2,0,2,0,1])
|
45
|
+
expect(@m.yale_a).to eq([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
|
+
expect(@m[0,0]).to eq(100)
|
54
|
+
@m[0,0] = old_val
|
55
|
+
expect(@m).to eq(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
|
+
expect(n[0,0..2].to_flat_array).to eq([0,0,0])
|
63
|
+
expect(n[1,0..2].to_flat_array).to eq([3,4,5])
|
64
|
+
expect(n[2,0..2].to_flat_array).to eq([6,7,8])
|
65
|
+
end
|
66
|
+
|
67
|
+
step "set a second row of entries" do
|
68
|
+
n[2,0..2] = 0
|
69
|
+
expect(n[2,0..2].to_flat_array).to eq([0,0,0])
|
70
|
+
expect(n[1,0..2].to_flat_array).to eq([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
|
+
expect(n).to eq(@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
|
+
expect(m.yale_ija).to eq([4, 6, 8, 10, 1, 2, 0, 2, 0, 1])
|
89
|
+
expect(m.yale_a).to eq([100, 100, 8, 0, 100, 2, 100, 5, 6, 7])
|
90
|
+
end
|
91
|
+
|
92
|
+
expect(m[0..1,0..1]).to eq(slice_result_a)
|
93
|
+
expect(m[2,0..1]).to eq(@m[2,0..1])
|
94
|
+
expect(m[0..1,2]).to eq(@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
|
+
expect([4,5,6,8,2,2,0,1]).to eq(m.yale_ija)
|
101
|
+
expect([0,0,8,0,2,5,6,7]).to eq(m.yale_a)
|
102
|
+
end
|
103
|
+
|
104
|
+
expect(m[0..1,0..1]).to eq(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
|
+
expect(m[1..2,0..1]).to eq(slice_result_a)
|
111
|
+
expect(m[0,0..1]).to eq(@m[0,0..1])
|
112
|
+
expect(m[1..2,2]).to eq(@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
|
+
expect(m[1..2,0..1]).to eq(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
|
+
expect(m[1..2,1..2]).to eq(slice_result_a)
|
124
|
+
expect(m[0,1..2]).to eq(@m[0,1..2])
|
125
|
+
expect(m[1..2,0]).to eq(@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
|
+
expect(m[1..2,1..2]).to eq(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
|
+
expect(m[0..1,1..2]).to eq(slice_result_a)
|
137
|
+
expect(m[2,1..2]).to eq(@m[2,1..2])
|
138
|
+
expect(m[0..1,0]).to eq(@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
|
+
expect(m[0..1,1..2]).to eq(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
|
+
expect(x.to_flat_array).to eq([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
|