nmatrix 0.0.8 → 0.0.9
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.
- 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
@@ -20,23 +20,34 @@
|
|
20
20
|
#
|
21
21
|
# * https://github.com/SciRuby/sciruby/wiki/Contributor-Agreement
|
22
22
|
#
|
23
|
-
# ==
|
23
|
+
# == 02_slice_spec.rb
|
24
24
|
#
|
25
|
-
# Test of slice operations.
|
25
|
+
# Test of slice operations. High priority tests since reference
|
26
|
+
# slicing is needed for pretty_print.
|
26
27
|
#
|
28
|
+
require 'pry'
|
27
29
|
require File.dirname(__FILE__) + "/spec_helper.rb"
|
28
30
|
|
29
31
|
describe "Slice operation" do
|
32
|
+
include RSpec::Longrun::DSL
|
30
33
|
|
31
34
|
[:dense, :list, :yale].each do |stype|
|
32
35
|
context "for #{stype}" do
|
33
36
|
before :each do
|
37
|
+
GC.start # don't have to do this, but it helps to make sure we've cleaned up our pointers properly.
|
34
38
|
@m = create_matrix(stype)
|
35
39
|
end
|
36
40
|
|
41
|
+
it "should correctly return a row of a reference-slice" do
|
42
|
+
@n = create_rectangular_matrix(stype)
|
43
|
+
@m = @n[1..4,1..3]
|
44
|
+
@m.row(1, :copy).should == @m.row(1, :reference)
|
45
|
+
@m.row(1, :copy).to_flat_array.should == [12,13,0]
|
46
|
+
end
|
47
|
+
|
37
48
|
if stype == :yale
|
38
49
|
it "should binary search for the left boundary of a partial row of stored indices correctly" do
|
39
|
-
n = NMatrix.new(:yale,
|
50
|
+
n = NMatrix.new(10, stype: :yale, dtype: :int32)
|
40
51
|
n[3,0] = 1
|
41
52
|
#n[3,2] = 2
|
42
53
|
n[3,3] = 3
|
@@ -47,6 +58,7 @@ describe "Slice operation" do
|
|
47
58
|
vs = []
|
48
59
|
is = []
|
49
60
|
js = []
|
61
|
+
|
50
62
|
n[3,1..9].each_stored_with_indices do |v,i,j|
|
51
63
|
vs << v
|
52
64
|
is << i
|
@@ -108,27 +120,16 @@ describe "Slice operation" do
|
|
108
120
|
end
|
109
121
|
end
|
110
122
|
|
111
|
-
it "should return correct
|
123
|
+
it "should return correct supershape" do
|
112
124
|
x = NMatrix.random([10,12])
|
113
125
|
y = x[0...8,5...12]
|
114
126
|
y.shape.should == [8,7]
|
115
127
|
y.supershape.should == [10,12]
|
116
128
|
end
|
117
129
|
|
118
|
-
it "should return correct 1st- and 2nd-order supershape" do
|
119
|
-
pending "Not yet sure if we ever want to enable reference slices of reference slices"
|
120
|
-
x = NMatrix.random([10,12])
|
121
|
-
y = x[0...8,5...12]
|
122
|
-
z = y[0...3,0...4]
|
123
|
-
z.supershape(2).should == y.supershape(1)
|
124
|
-
z.supershape(1).should == [8,7]
|
125
|
-
end
|
126
|
-
|
127
130
|
it "should have #is_ref? method" do
|
128
131
|
a = @m[0..1, 0..1]
|
129
132
|
b = @m.slice(0..1, 0..1)
|
130
|
-
|
131
|
-
|
132
133
|
@m.is_ref?.should be_false
|
133
134
|
a.is_ref?.should be_true
|
134
135
|
b.is_ref?.should be_false
|
@@ -143,7 +144,7 @@ describe "Slice operation" do
|
|
143
144
|
context "with copying" do
|
144
145
|
it 'should return an NMatrix' do
|
145
146
|
n = @m.slice(0..1,0..1)
|
146
|
-
nm_eql(n, NMatrix.new([2,2], [0,1,3,4], :int32)).should be_true
|
147
|
+
nm_eql(n, NMatrix.new([2,2], [0,1,3,4], dtype: :int32)).should be_true
|
147
148
|
end
|
148
149
|
|
149
150
|
it 'should return a copy of 2x2 matrix to self elements' do
|
@@ -155,22 +156,28 @@ describe "Slice operation" do
|
|
155
156
|
@m[2,1].should eql(7)
|
156
157
|
end
|
157
158
|
|
158
|
-
it 'should return a 1x2 matrix
|
159
|
+
it 'should return a 1x2 matrix without refs to self elements' do
|
159
160
|
n = @m.slice(0,1..2)
|
160
161
|
n.shape.should eql([1,2])
|
161
162
|
|
162
163
|
n[0].should == @m[0,1]
|
164
|
+
n[1].should == @m[0,2]
|
163
165
|
n[0] = -9
|
164
166
|
@m[0,1].should eql(1)
|
167
|
+
@m[0,2].should eql(2)
|
165
168
|
end
|
166
169
|
|
167
|
-
it 'should return a 2x1 matrix
|
170
|
+
it 'should return a 2x1 matrix without refs to self elements' do
|
171
|
+
@m.extend NMatrix::YaleFunctions
|
172
|
+
|
168
173
|
n = @m.slice(0..1,1)
|
169
174
|
n.shape.should eql([2,1])
|
170
175
|
|
171
176
|
n[0].should == @m[0,1]
|
177
|
+
n[1].should == @m[1,1]
|
172
178
|
n[0] = -9
|
173
179
|
@m[0,1].should eql(1)
|
180
|
+
@m[1,1].should eql(4)
|
174
181
|
end
|
175
182
|
|
176
183
|
it 'should be correct slice for range 0..2 and 0...3' do
|
@@ -178,7 +185,7 @@ describe "Slice operation" do
|
|
178
185
|
end
|
179
186
|
|
180
187
|
[:dense, :list, :yale].each do |cast_type|
|
181
|
-
it "should cast from #{stype.upcase} to #{cast_type.upcase}" do
|
188
|
+
it "should cast copied slice from #{stype.upcase} to #{cast_type.upcase}" do
|
182
189
|
nm_eql(@m.slice(1..2, 1..2).cast(cast_type, :int32), @m.slice(1..2,1..2)).should be_true
|
183
190
|
nm_eql(@m.slice(0..1, 1..2).cast(cast_type, :int32), @m.slice(0..1,1..2)).should be_true
|
184
191
|
nm_eql(@m.slice(1..2, 0..1).cast(cast_type, :int32), @m.slice(1..2,0..1)).should be_true
|
@@ -186,6 +193,8 @@ describe "Slice operation" do
|
|
186
193
|
|
187
194
|
# Non square
|
188
195
|
nm_eql(@m.slice(0..2, 1..2).cast(cast_type, :int32), @m.slice(0..2,1..2)).should be_true
|
196
|
+
#require 'pry'
|
197
|
+
#binding.pry if cast_type == :yale
|
189
198
|
nm_eql(@m.slice(1..2, 0..2).cast(cast_type, :int32), @m.slice(1..2,0..2)).should be_true
|
190
199
|
|
191
200
|
# Full
|
@@ -208,7 +217,7 @@ describe "Slice operation" do
|
|
208
217
|
context "by reference" do
|
209
218
|
it 'should return an NMatrix' do
|
210
219
|
n = @m[0..1,0..1]
|
211
|
-
nm_eql(n, NMatrix.new([2,2], [0,1,3,4], :int32)).should be_true
|
220
|
+
nm_eql(n, NMatrix.new([2,2], [0,1,3,4], dtype: :int32)).should be_true
|
212
221
|
end
|
213
222
|
|
214
223
|
it 'should return a 2x2 matrix with refs to self elements' do
|
@@ -240,7 +249,7 @@ describe "Slice operation" do
|
|
240
249
|
|
241
250
|
it 'should slice again' do
|
242
251
|
n = @m[1..2, 1..2]
|
243
|
-
nm_eql(n[1,0..1], NVector.new(2, [7,8], :int32).transpose).should be_true
|
252
|
+
nm_eql(n[1,0..1], NVector.new(2, [7,8], dtype: :int32).transpose).should be_true
|
244
253
|
end
|
245
254
|
|
246
255
|
it 'should be correct slice for range 0..2 and 0...3' do
|
@@ -276,8 +285,8 @@ describe "Slice operation" do
|
|
276
285
|
MATRIX32A_ARRAY
|
277
286
|
end
|
278
287
|
|
279
|
-
n = NMatrix.new([4,3], nary, left_dtype)[1..3,1..2]
|
280
|
-
m = NMatrix.new([3,2], mary, right_dtype)[1..2,0..1]
|
288
|
+
n = NMatrix.new([4,3], nary, dtype: left_dtype)[1..3,1..2]
|
289
|
+
m = NMatrix.new([3,2], mary, dtype: right_dtype)[1..2,0..1]
|
281
290
|
|
282
291
|
r = n.dot m
|
283
292
|
r.shape.should eql([3,2])
|
@@ -333,34 +342,41 @@ describe "Slice operation" do
|
|
333
342
|
|
334
343
|
end
|
335
344
|
|
336
|
-
|
337
|
-
|
338
|
-
|
345
|
+
example 'should be cleaned up by garbage collector without errors' do
|
346
|
+
step "reference slice" do
|
347
|
+
1.times do
|
348
|
+
n = @m[1..2,0..1]
|
349
|
+
end
|
350
|
+
GC.start
|
339
351
|
end
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
352
|
+
|
353
|
+
step "reference slice of casted-copy" do
|
354
|
+
@m.should == NMatrix.new([3,3], (0..9).to_a, dtype: :int32).cast(stype, :int32)
|
355
|
+
n = nil
|
356
|
+
1.times do
|
357
|
+
m = NMatrix.new([2,2], [1,2,3,4]).cast(stype, :int32)
|
358
|
+
n = m[0..1,0..1]
|
359
|
+
end
|
360
|
+
GC.start
|
361
|
+
n.should == NMatrix.new([2,2], [1,2,3,4]).cast(stype, :int32)
|
346
362
|
end
|
347
|
-
GC.start
|
348
|
-
n.should == NMatrix.new(:dense, [2,2], [1,2,3,4]).cast(stype, :int32)
|
349
363
|
end
|
350
364
|
|
351
365
|
[:dense, :list, :yale].each do |cast_type|
|
352
|
-
it "should cast from #{stype.upcase} to #{cast_type.upcase}" do
|
353
|
-
|
366
|
+
it "should cast a square reference-slice from #{stype.upcase} to #{cast_type.upcase}" do
|
354
367
|
nm_eql(@m[1..2, 1..2].cast(cast_type), @m[1..2,1..2]).should be_true
|
355
368
|
nm_eql(@m[0..1, 1..2].cast(cast_type), @m[0..1,1..2]).should be_true
|
356
369
|
nm_eql(@m[1..2, 0..1].cast(cast_type), @m[1..2,0..1]).should be_true
|
357
370
|
nm_eql(@m[0..1, 0..1].cast(cast_type), @m[0..1,0..1]).should be_true
|
371
|
+
end
|
358
372
|
|
373
|
+
it "should cast a rectangular reference-slice from #{stype.upcase} to #{cast_type.upcase}" do
|
359
374
|
# Non square
|
360
|
-
nm_eql(@m[0..2, 1..2].cast(cast_type), @m[0..2,1..2]).should be_true
|
361
|
-
nm_eql(@m[1..2, 0..2].cast(cast_type), @m[1..2,0..2]).should be_true
|
375
|
+
nm_eql(@m[0..2, 1..2].cast(cast_type), @m[0..2,1..2]).should be_true # FIXME: memory problem.
|
376
|
+
nm_eql(@m[1..2, 0..2].cast(cast_type), @m[1..2,0..2]).should be_true # this one is fine
|
377
|
+
end
|
362
378
|
|
363
|
-
|
379
|
+
it "should cast a square full-matrix reference-slice from #{stype.upcase} to #{cast_type.upcase}" do
|
364
380
|
nm_eql(@m[0..2, 0..2].cast(cast_type), @m).should be_true
|
365
381
|
end
|
366
382
|
end
|
data/spec/blas_spec.rb
CHANGED
@@ -29,14 +29,15 @@
|
|
29
29
|
require File.join(File.dirname(__FILE__), "spec_helper.rb")
|
30
30
|
|
31
31
|
describe NMatrix::BLAS do
|
32
|
+
|
32
33
|
[:rational32, :rational64, :rational128, :float32, :float64, :complex64, :complex128].each do |dtype|
|
33
34
|
context dtype do
|
34
35
|
# This is not the same as "exposes cblas trsm", which would be for a version defined in blas.rb (which
|
35
36
|
# would greatly simplify the calling of cblas_trsm in terms of arguments, and which would be accessible
|
36
37
|
# as NMatrix::BLAS::trsm)
|
37
38
|
it "exposes unfriendly cblas_trsm" do
|
38
|
-
a = NMatrix.new(
|
39
|
-
b =
|
39
|
+
a = NMatrix.new(3, [4,-1.quo(2), -3.quo(4), -2, 2, -1.quo(4), -4, -2, -1.quo(2)], dtype: dtype)
|
40
|
+
b = NMatrix.new([3,1], [-1, 17, -9], dtype: dtype)
|
40
41
|
NMatrix::BLAS::cblas_trsm(:row, :right, :lower, :transpose, :nonunit, 1, 3, 1.0, a, 3, b, 3)
|
41
42
|
|
42
43
|
# These test results all come from actually running a matrix through BLAS. We use them to ensure that NMatrix's
|
@@ -69,21 +70,17 @@ describe NMatrix::BLAS do
|
|
69
70
|
context dtype do
|
70
71
|
|
71
72
|
it "exposes cblas rot" do
|
72
|
-
x =
|
73
|
-
y =
|
73
|
+
x = NMatrix.new([5,1], [1,2,3,4,5], dtype: dtype)
|
74
|
+
y = NMatrix.new([5,1], [-5,-4,-3,-2,-1], dtype: dtype)
|
74
75
|
x, y = NMatrix::BLAS::rot(x, y, 1.quo(2), Math.sqrt(3).quo(2), -1)
|
75
76
|
|
76
|
-
x
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
y[1].should be_within(1e-4).of(-5.464101615137754)
|
84
|
-
y[2].should be_within(1e-4).of(-4.098076211353316)
|
85
|
-
y[3].should be_within(1e-4).of(-2.732050807568877)
|
86
|
-
y[4].should be_within(1e-4).of(-1.3660254037844386)
|
77
|
+
x.should be_within(1e-4).of(
|
78
|
+
NMatrix.new([5,1], [-0.3660254037844386, -0.7320508075688772, -1.098076211353316, -1.4641016151377544, -1.8301270189221928], dtype: dtype)
|
79
|
+
)
|
80
|
+
|
81
|
+
y.should be_within(1e-4).of(
|
82
|
+
NMatrix.new([5,1], [-6.830127018922193, -5.464101615137754, -4.098076211353316, -2.732050807568877, -1.3660254037844386], dtype: dtype)
|
83
|
+
)
|
87
84
|
end
|
88
85
|
|
89
86
|
end
|
@@ -94,7 +91,8 @@ describe NMatrix::BLAS do
|
|
94
91
|
|
95
92
|
it "exposes cblas rotg" do
|
96
93
|
pending("broken for :object") if dtype == :object
|
97
|
-
|
94
|
+
|
95
|
+
ab = NMatrix.new([2,1], [6,-8], dtype: dtype)
|
98
96
|
c,s = NMatrix::BLAS::rotg(ab)
|
99
97
|
|
100
98
|
if [:float32, :float64].include?(dtype)
|
@@ -102,6 +100,7 @@ describe NMatrix::BLAS do
|
|
102
100
|
ab[1].should be_within(1e-6).of(-5.quo(3))
|
103
101
|
c.should be_within(1e-6).of(-3.quo(5))
|
104
102
|
else
|
103
|
+
pending "need correct test cases"
|
105
104
|
ab[0].should be_within(1e-6).of(10)
|
106
105
|
ab[1].should be_within(1e-6).of(5.quo(3))
|
107
106
|
c.should be_within(1e-6).of(3.quo(5))
|
@@ -111,63 +110,32 @@ describe NMatrix::BLAS do
|
|
111
110
|
|
112
111
|
# Note: this exposes gemm, not cblas_gemm (which is the unfriendly CBLAS no-error-checking version)
|
113
112
|
it "exposes gemm" do
|
114
|
-
|
115
|
-
|
116
|
-
n = NMatrix.new([4,3], dtype)
|
117
|
-
n[0,0] = 14.0
|
118
|
-
n[0,1] = 9.0
|
119
|
-
n[0,2] = 3.0
|
120
|
-
n[1,0] = 2.0
|
121
|
-
n[1,1] = 11.0
|
122
|
-
n[1,2] = 15.0
|
123
|
-
n[2,0] = 0.0
|
124
|
-
n[2,1] = 12.0
|
125
|
-
n[2,2] = 17.0
|
126
|
-
n[3,0] = 5.0
|
127
|
-
n[3,1] = 2.0
|
128
|
-
n[3,2] = 3.0
|
129
|
-
|
130
|
-
m = NMatrix.new([3,2], dtype)
|
131
|
-
|
132
|
-
m[0,0] = 12.0
|
133
|
-
m[0,1] = 25.0
|
134
|
-
m[1,0] = 9.0
|
135
|
-
m[1,1] = 10.0
|
136
|
-
m[2,0] = 8.0
|
137
|
-
m[2,1] = 5.0
|
113
|
+
n = NMatrix.new([4,3], [14.0,9.0,3.0, 2.0,11.0,15.0, 0.0,12.0,17.0, 5.0,2.0,3.0], dtype: dtype)
|
114
|
+
m = NMatrix.new([3,2], [12.0,25.0, 9.0,10.0, 8.0,5.0], dtype: dtype)
|
138
115
|
|
139
116
|
#c = NMatrix.new([4,2], dtype)
|
140
117
|
r = NMatrix::BLAS.gemm(n, m) #, c)
|
141
118
|
#c.should equal(r) # check that both are same memory address
|
142
119
|
|
143
|
-
r
|
144
|
-
r[0,1].should == 455.0
|
145
|
-
r[1,0].should == 243.0
|
146
|
-
r[1,1].should == 235.0
|
147
|
-
r[2,0].should == 244.0
|
148
|
-
r[2,1].should == 205.0
|
149
|
-
r[3,0].should == 102.0
|
150
|
-
r[3,1].should == 160.0
|
120
|
+
r.should == NMatrix.new([4,2], [273,455,243,235,244,205,102,160], dtype: dtype)
|
151
121
|
end
|
152
122
|
|
153
123
|
|
154
124
|
it "exposes gemv" do
|
155
|
-
|
156
|
-
|
157
|
-
a = 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], :float64)
|
158
|
-
x = NVector.new(3, [2.0, 1.0, 0.0], :float64)
|
125
|
+
a = 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: :float64)
|
126
|
+
x = NMatrix.new([3,1], [2.0, 1.0, 0.0], dtype: :float64)
|
159
127
|
|
160
128
|
NMatrix::BLAS.gemv(a, x)
|
161
129
|
end
|
162
130
|
|
163
131
|
it "exposes asum" do
|
164
|
-
x =
|
132
|
+
x = NMatrix.new([4,1], [1,2,3,4], dtype: :float64)
|
165
133
|
NMatrix::BLAS.asum(x).should == 10.0
|
166
134
|
end
|
167
135
|
|
168
136
|
|
169
137
|
it "exposes nrm2" do
|
170
|
-
x =
|
138
|
+
x = NMatrix.new([4,1], [2,-4,3,5], dtype: :float64)
|
171
139
|
NMatrix::BLAS.nrm2(x, 1, 3).should be_within(1e-10).of(5.385164807134504)
|
172
140
|
end
|
173
141
|
|
data/spec/elementwise_spec.rb
CHANGED
@@ -32,8 +32,9 @@ describe NMatrix do
|
|
32
32
|
|
33
33
|
context "yale" do
|
34
34
|
before :each do
|
35
|
-
@n = NMatrix.new(:yale,
|
36
|
-
@
|
35
|
+
@n = NMatrix.new(3, stype: :yale, dtype: :int64)
|
36
|
+
@n.extend NMatrix::YaleFunctions
|
37
|
+
@m = NMatrix.new(3, stype: :yale, dtype: :int64)
|
37
38
|
@n[0,0] = 52
|
38
39
|
@n[0,2] = 5
|
39
40
|
@n[1,1] = 40
|
@@ -52,13 +53,13 @@ describe NMatrix do
|
|
52
53
|
x[1,1].should == 40 * 3
|
53
54
|
x[2,0].should == 6 * 3
|
54
55
|
|
55
|
-
r = NMatrix.new(:yale,
|
56
|
+
r = NMatrix.new(3, stype: :yale, dtype: :int64)
|
56
57
|
y = r + 3
|
57
58
|
y[0,0].should == 3
|
58
59
|
end
|
59
60
|
|
60
61
|
it "should refuse to perform a dot operation on a yale with non-zero default" do
|
61
|
-
r = NMatrix.new(:yale,
|
62
|
+
r = NMatrix.new(3, stype: :yale, dtype: :int64)
|
62
63
|
y = r + 3
|
63
64
|
expect { y.dot(r) }.to raise_error
|
64
65
|
expect { r.dot(y) }.to raise_error
|
@@ -74,7 +75,7 @@ describe NMatrix do
|
|
74
75
|
|
75
76
|
it "should perform element-wise multiplication" do
|
76
77
|
r = NMatrix.new(:dense, 3, [0,0,-25,0,-1920,0,0,0,0], :int64).cast(:yale, :int64)
|
77
|
-
m = NMatrix.new(:yale,
|
78
|
+
m = NMatrix.new(2, stype: :yale, dtype: :int64)
|
78
79
|
(@n*@m).should == r
|
79
80
|
end
|
80
81
|
|
@@ -84,7 +85,7 @@ describe NMatrix do
|
|
84
85
|
end
|
85
86
|
|
86
87
|
it "should perform element-wise modulo" do
|
87
|
-
m = NMatrix.new(:yale,
|
88
|
+
m = NMatrix.new(3, stype: :yale, dtype: :int64, default: 0) + 5
|
88
89
|
(@n % m).should == NMatrix.new(:dense, 3, [2,0,0,0,0,0,1,0,0], :int64).cast(:yale, :int64)
|
89
90
|
end
|
90
91
|
|
@@ -130,13 +131,13 @@ describe NMatrix do
|
|
130
131
|
x[1,1].should == 40 * 3
|
131
132
|
x[0,1].should == 0
|
132
133
|
|
133
|
-
r = NMatrix.new(:list,
|
134
|
+
r = NMatrix.new(3, stype: :list, default: 1)
|
134
135
|
y = r + 3
|
135
136
|
y[0,0].should == 4
|
136
137
|
end
|
137
138
|
|
138
139
|
it "should perform element-wise addition" do
|
139
|
-
r = NMatrix.new(:list,
|
140
|
+
r = NMatrix.new(2, stype: :list, dtype: :int64, default: 0)
|
140
141
|
r[0,0] = 52
|
141
142
|
r[1,1] = -8
|
142
143
|
q = @n + @m
|
data/spec/io_spec.rb
CHANGED
@@ -28,11 +28,13 @@ require "./lib/nmatrix"
|
|
28
28
|
|
29
29
|
describe NMatrix::IO do
|
30
30
|
it "repacks a string" do
|
31
|
-
NMatrix::IO::Matlab.repack("hello", :miUINT8, :
|
31
|
+
NMatrix::IO::Matlab.repack("hello", :miUINT8, :byte).should == "hello"
|
32
32
|
end
|
33
33
|
|
34
34
|
it "creates yale from internal byte-string function" do
|
35
|
-
|
35
|
+
ia = NMatrix::IO::Matlab.repack("\0\1\3\3\4", :miUINT8, :itype)
|
36
|
+
ja = NMatrix::IO::Matlab.repack("\0\1\3\0\0\0\0\0\0\0\0", :miUINT8, :itype)
|
37
|
+
n = NMatrix.new(:yale, [4,4], :byte, ia, ja, "\2\3\5\4", :byte)
|
36
38
|
n[0,0].should == 2
|
37
39
|
n[1,1].should == 3
|
38
40
|
n[1,3].should == 5
|
@@ -54,13 +56,13 @@ describe NMatrix::IO do
|
|
54
56
|
|
55
57
|
it "reads MATLAB .mat file containing a single dense integer matrix" do
|
56
58
|
n = NMatrix::IO::Matlab.load_mat("spec/4x5_dense.mat")
|
57
|
-
m = NMatrix.new(
|
59
|
+
m = NMatrix.new([4,5], [16,17,18,19,20,15,14,13,12,11,6,7,8,9,10,5,4,3,2,1])
|
58
60
|
n.should == m
|
59
61
|
end
|
60
62
|
|
61
63
|
it "reads MATLAB .mat file containing a single dense double matrix" do
|
62
64
|
n = NMatrix::IO::Matlab.load_mat("spec/2x2_dense_double.mat")
|
63
|
-
m = NMatrix.new(
|
65
|
+
m = NMatrix.new(2, [1.1, 2.0, 3.0, 4.0], dtype: :float64)
|
64
66
|
n.should == m
|
65
67
|
end
|
66
68
|
|
data/spec/lapack_spec.rb
CHANGED
@@ -56,7 +56,7 @@ describe NMatrix::LAPACK do
|
|
56
56
|
[:rational32, :rational64, :rational128, :float32, :float64, :complex64, :complex128].each do |dtype|
|
57
57
|
context dtype do
|
58
58
|
it "exposes clapack getrf" do
|
59
|
-
a = NMatrix.new(
|
59
|
+
a = NMatrix.new(3, [4,9,2,3,5,7,8,1,6], dtype: dtype)
|
60
60
|
NMatrix::LAPACK::clapack_getrf(:row, 3, 3, a, 3)
|
61
61
|
|
62
62
|
# delta varies for different dtypes
|
@@ -100,9 +100,9 @@ describe NMatrix::LAPACK do
|
|
100
100
|
|
101
101
|
# Together, these calls are basically xGESV from LAPACK: http://www.netlib.org/lapack/double/dgesv.f
|
102
102
|
it "exposes clapack getrs" do
|
103
|
-
a = NMatrix.new(
|
103
|
+
a = NMatrix.new(3, [-2,4,-3,3,-2,1,0,-4,3], dtype: dtype)
|
104
104
|
ipiv = NMatrix::LAPACK::clapack_getrf(:row, 3, 3, a, 3)
|
105
|
-
b =
|
105
|
+
b = NMatrix.new([3,1], [-1, 17, -9], dtype: dtype)
|
106
106
|
|
107
107
|
NMatrix::LAPACK::clapack_getrs(:row, false, 3, 1, a, 3, ipiv, b, 3)
|
108
108
|
|
@@ -132,9 +132,9 @@ describe NMatrix::LAPACK do
|
|
132
132
|
-9.15 -7.93 4.86 4.85 3.01
|
133
133
|
9.57 1.64 8.83 0.74 5.80
|
134
134
|
-3.49 4.02 9.80 10.00 4.27
|
135
|
-
9.84 0.15 -8.99 -6.02 -5.31|.map(&:to_f), dtype)
|
136
|
-
s_true = NMatrix.new([1,5], [27.468732418221848, 22.643185009774697, 8.558388228482576, 5.985723201512133, 2.014899658715756], dtype)
|
137
|
-
right_true = NMatrix.new([5,6], [0.5911423764124365, 0.2631678147140568, 0.35543017386282716, 0.3142643627269275, 0.2299383153647484, 0.0, 0.39756679420242547, 0.24379902792633046, -0.22239000068544604, -0.7534661509534584, -0.36358968669749664, 0.0, 0.03347896906244727, -0.6002725806935828, -0.45083926892230763, 0.23344965724471425, -0.3054757327479317, 0.0, 0.4297069031370182, 0.23616680628112555, -0.6858628638738117, 0.3318600182003095, 0.1649276348845103, 0.0, 0.4697479215666587, -0.350891398883702, 0.38744460309967327, 0.15873555958215635, -0.5182574373535355, 0.0, -0.29335875846440357, 0.57626211913389, -0.020852917980871258, 0.3790776670601607, -0.6525516005923976, 0.0], dtype)
|
135
|
+
9.84 0.15 -8.99 -6.02 -5.31|.map(&:to_f), dtype: dtype)
|
136
|
+
s_true = NMatrix.new([1,5], [27.468732418221848, 22.643185009774697, 8.558388228482576, 5.985723201512133, 2.014899658715756], dtype: dtype)
|
137
|
+
right_true = NMatrix.new([5,6], [0.5911423764124365, 0.2631678147140568, 0.35543017386282716, 0.3142643627269275, 0.2299383153647484, 0.0, 0.39756679420242547, 0.24379902792633046, -0.22239000068544604, -0.7534661509534584, -0.36358968669749664, 0.0, 0.03347896906244727, -0.6002725806935828, -0.45083926892230763, 0.23344965724471425, -0.3054757327479317, 0.0, 0.4297069031370182, 0.23616680628112555, -0.6858628638738117, 0.3318600182003095, 0.1649276348845103, 0.0, 0.4697479215666587, -0.350891398883702, 0.38744460309967327, 0.15873555958215635, -0.5182574373535355, 0.0, -0.29335875846440357, 0.57626211913389, -0.020852917980871258, 0.3790776670601607, -0.6525516005923976, 0.0], dtype: dtype)
|
138
138
|
#right_true = NMatrix.new([5,6],
|
139
139
|
# %w|-0.59 0.26 0.36 0.31 0.23
|
140
140
|
# -0.40 0.24 -0.22 -0.75 -0.36
|
@@ -143,7 +143,7 @@ describe NMatrix::LAPACK do
|
|
143
143
|
# -0.47 -0.35 0.39 0.16 -0.52
|
144
144
|
# 0.29 0.58 -0.02 0.38 -0.65|.map(&:to_f),
|
145
145
|
# dtype)
|
146
|
-
left_true = NMatrix.new([5,5], [0.25138279272049635, 0.3968455517769292, 0.6921510074703637, 0.3661704447722309, 0.4076352386533525, 0.814836686086339, 0.3586615001880027, -0.24888801115928438, -0.3685935379446176, -0.09796256926688672, -0.2606185055842211, 0.7007682094072526, -0.22081144672043734, 0.38593848318854174, -0.49325014285102375, 0.3967237771305971, -0.4507112412166429, 0.2513211496937535, 0.4342486014366711, -0.6226840720358049, -0.21802776368654594, 0.14020994987112056, 0.5891194492399431, -0.6265282503648172, -0.4395516923423326], dtype)
|
146
|
+
left_true = NMatrix.new([5,5], [0.25138279272049635, 0.3968455517769292, 0.6921510074703637, 0.3661704447722309, 0.4076352386533525, 0.814836686086339, 0.3586615001880027, -0.24888801115928438, -0.3685935379446176, -0.09796256926688672, -0.2606185055842211, 0.7007682094072526, -0.22081144672043734, 0.38593848318854174, -0.49325014285102375, 0.3967237771305971, -0.4507112412166429, 0.2513211496937535, 0.4342486014366711, -0.6226840720358049, -0.21802776368654594, 0.14020994987112056, 0.5891194492399431, -0.6265282503648172, -0.4395516923423326], dtype: dtype)
|
147
147
|
#left_true = NMatrix.new([5,5],
|
148
148
|
# %w|-0.25 -0.40 -0.69 -0.37 -0.41
|
149
149
|
# 0.81 0.36 -0.25 -0.37 -0.10
|
@@ -151,16 +151,16 @@ describe NMatrix::LAPACK do
|
|
151
151
|
# 0.40 -0.45 0.25 0.43 -0.62
|
152
152
|
# -0.22 0.14 0.59 -0.63 -0.44|.map(&:to_f),
|
153
153
|
# dtype)
|
154
|
-
s = NMatrix.new([5,1], 0, dtype)
|
155
|
-
u = NMatrix.new([5,5], 0, dtype)
|
154
|
+
s = NMatrix.new([5,1], 0, dtype: dtype)
|
155
|
+
u = NMatrix.new([5,5], 0, dtype: dtype)
|
156
156
|
ldu = 5
|
157
|
-
vt = NMatrix.new([6,6], 0, dtype)
|
157
|
+
vt = NMatrix.new([6,6], 0, dtype: dtype)
|
158
158
|
ldvt= 6
|
159
159
|
elsif [:complex64, :complex128].include? dtype
|
160
160
|
#http://software.intel.com/sites/products/documentation/doclib/mkl_sa/11/mkl_lapack_examples/cgesvd_ex.c.htm
|
161
161
|
pending "Example may be wrong"
|
162
162
|
else
|
163
|
-
a = NMatrix.new([4,3], dtype)
|
163
|
+
a = NMatrix.new([4,3], dtype: dtype)
|
164
164
|
end
|
165
165
|
err = case dtype
|
166
166
|
when :float32, :complex64
|
@@ -194,9 +194,9 @@ describe NMatrix::LAPACK do
|
|
194
194
|
-9.15 -7.93 4.86 4.85 3.01
|
195
195
|
9.57 1.64 8.83 0.74 5.80
|
196
196
|
-3.49 4.02 9.80 10.00 4.27
|
197
|
-
9.84 0.15 -8.99 -6.02 -5.31|.map(&:to_f), dtype)
|
198
|
-
s_true = NMatrix.new([1,5], [27.468732418221848, 22.643185009774697, 8.558388228482576, 5.985723201512133, 2.014899658715756], dtype)
|
199
|
-
right_true = NMatrix.new([5,6], [0.5911423764124365, 0.2631678147140568, 0.35543017386282716, 0.3142643627269275, 0.2299383153647484, 0.0, 0.39756679420242547, 0.24379902792633046, -0.22239000068544604, -0.7534661509534584, -0.36358968669749664, 0.0, 0.03347896906244727, -0.6002725806935828, -0.45083926892230763, 0.23344965724471425, -0.3054757327479317, 0.0, 0.4297069031370182, 0.23616680628112555, -0.6858628638738117, 0.3318600182003095, 0.1649276348845103, 0.0, 0.4697479215666587, -0.350891398883702, 0.38744460309967327, 0.15873555958215635, -0.5182574373535355, 0.0, -0.29335875846440357, 0.57626211913389, -0.020852917980871258, 0.3790776670601607, -0.6525516005923976, 0.0], dtype)
|
197
|
+
9.84 0.15 -8.99 -6.02 -5.31|.map(&:to_f), dtype: dtype)
|
198
|
+
s_true = NMatrix.new([1,5], [27.468732418221848, 22.643185009774697, 8.558388228482576, 5.985723201512133, 2.014899658715756], dtype: dtype)
|
199
|
+
right_true = NMatrix.new([5,6], [0.5911423764124365, 0.2631678147140568, 0.35543017386282716, 0.3142643627269275, 0.2299383153647484, 0.0, 0.39756679420242547, 0.24379902792633046, -0.22239000068544604, -0.7534661509534584, -0.36358968669749664, 0.0, 0.03347896906244727, -0.6002725806935828, -0.45083926892230763, 0.23344965724471425, -0.3054757327479317, 0.0, 0.4297069031370182, 0.23616680628112555, -0.6858628638738117, 0.3318600182003095, 0.1649276348845103, 0.0, 0.4697479215666587, -0.350891398883702, 0.38744460309967327, 0.15873555958215635, -0.5182574373535355, 0.0, -0.29335875846440357, 0.57626211913389, -0.020852917980871258, 0.3790776670601607, -0.6525516005923976, 0.0], dtype: dtype)
|
200
200
|
#right_true = NMatrix.new([5,6],
|
201
201
|
# %w|-0.59 0.26 0.36 0.31 0.23
|
202
202
|
# -0.40 0.24 -0.22 -0.75 -0.36
|
@@ -205,7 +205,7 @@ describe NMatrix::LAPACK do
|
|
205
205
|
# -0.47 -0.35 0.39 0.16 -0.52
|
206
206
|
# 0.29 0.58 -0.02 0.38 -0.65|.map(&:to_f),
|
207
207
|
# dtype)
|
208
|
-
left_true = NMatrix.new([5,5], [0.25138279272049635, 0.3968455517769292, 0.6921510074703637, 0.3661704447722309, 0.4076352386533525, 0.814836686086339, 0.3586615001880027, -0.24888801115928438, -0.3685935379446176, -0.09796256926688672, -0.2606185055842211, 0.7007682094072526, -0.22081144672043734, 0.38593848318854174, -0.49325014285102375, 0.3967237771305971, -0.4507112412166429, 0.2513211496937535, 0.4342486014366711, -0.6226840720358049, -0.21802776368654594, 0.14020994987112056, 0.5891194492399431, -0.6265282503648172, -0.4395516923423326], dtype)
|
208
|
+
left_true = NMatrix.new([5,5], [0.25138279272049635, 0.3968455517769292, 0.6921510074703637, 0.3661704447722309, 0.4076352386533525, 0.814836686086339, 0.3586615001880027, -0.24888801115928438, -0.3685935379446176, -0.09796256926688672, -0.2606185055842211, 0.7007682094072526, -0.22081144672043734, 0.38593848318854174, -0.49325014285102375, 0.3967237771305971, -0.4507112412166429, 0.2513211496937535, 0.4342486014366711, -0.6226840720358049, -0.21802776368654594, 0.14020994987112056, 0.5891194492399431, -0.6265282503648172, -0.4395516923423326], dtype: dtype)
|
209
209
|
#left_true = NMatrix.new([5,5],
|
210
210
|
# %w|-0.25 -0.40 -0.69 -0.37 -0.41
|
211
211
|
# 0.81 0.36 -0.25 -0.37 -0.10
|
@@ -213,26 +213,26 @@ describe NMatrix::LAPACK do
|
|
213
213
|
# 0.40 -0.45 0.25 0.43 -0.62
|
214
214
|
# -0.22 0.14 0.59 -0.63 -0.44|.map(&:to_f),
|
215
215
|
# dtype)
|
216
|
-
s = NMatrix.new([5,1], 0, dtype)
|
217
|
-
u = NMatrix.new([5,5], 0, dtype)
|
216
|
+
s = NMatrix.new([5,1], 0, dtype: dtype)
|
217
|
+
u = NMatrix.new([5,5], 0, dtype: dtype)
|
218
218
|
ldu = 5
|
219
|
-
vt = NMatrix.new([6,6], 0, dtype)
|
219
|
+
vt = NMatrix.new([6,6], 0, dtype: dtype)
|
220
220
|
ldvt= 6
|
221
221
|
elsif [:complex64, :complex128].include? dtype
|
222
222
|
#http://software.intel.com/sites/products/documentation/doclib/mkl_sa/11/mkl_lapack_examples/cgesvd_ex.c.htm
|
223
223
|
pending "Example may be wrong"
|
224
|
-
a = NMatrix.new([4,3], [[ 5.91, -5.69], [ 7.09, 2.72], [ 7.78, -4.06], [ -0.79, -7.21], [ -3.15, -4.08], [ -1.89, 3.27], [ 4.57, -2.07], [ -3.88, -3.30], [ -4.89, 4.20], [ 4.10, -6.70], [ 3.28, -3.84], [ 3.84, 1.19]].map {|e| Complex(*e) } , dtype)
|
225
|
-
s_true = NMatrix.new([3,1], [17.63, 11.61, 6.78], dtype)
|
226
|
-
left_true = NMatrix.new([4,4], [[-0.86, 0.0], [0.4, 0.0], [0.32, 0.0], [-0.35, 0.13], [-0.24, -0.21], [-0.63, 0.6], [0.15, 0.32], [0.61, 0.61], [-0.36, 0.1]].map {|e| Complex(*e)}, dtype)
|
227
|
-
right_true = NMatrix.new([4,3], [[ -0.22, 0.51], [ -0.37, -0.32], [ -0.53, 0.11], [ 0.15, 0.38], [ 0.31, 0.31], [ 0.09, -0.57], [ 0.18, -0.39], [ 0.38, -0.39], [ 0.53, 0.24], [ 0.49, 0.28], [ -0.47, -0.25], [ -0.15, 0.19]].map {|e| Complex *e} , dtype)
|
224
|
+
a = NMatrix.new([4,3], [[ 5.91, -5.69], [ 7.09, 2.72], [ 7.78, -4.06], [ -0.79, -7.21], [ -3.15, -4.08], [ -1.89, 3.27], [ 4.57, -2.07], [ -3.88, -3.30], [ -4.89, 4.20], [ 4.10, -6.70], [ 3.28, -3.84], [ 3.84, 1.19]].map {|e| Complex(*e) } , dtype: dtype)
|
225
|
+
s_true = NMatrix.new([3,1], [17.63, 11.61, 6.78], dtype: dtype)
|
226
|
+
left_true = NMatrix.new([4,4], [[-0.86, 0.0], [0.4, 0.0], [0.32, 0.0], [-0.35, 0.13], [-0.24, -0.21], [-0.63, 0.6], [0.15, 0.32], [0.61, 0.61], [-0.36, 0.1]].map {|e| Complex(*e)}, dtype: dtype)
|
227
|
+
right_true = NMatrix.new([4,3], [[ -0.22, 0.51], [ -0.37, -0.32], [ -0.53, 0.11], [ 0.15, 0.38], [ 0.31, 0.31], [ 0.09, -0.57], [ 0.18, -0.39], [ 0.38, -0.39], [ 0.53, 0.24], [ 0.49, 0.28], [ -0.47, -0.25], [ -0.15, 0.19]].map {|e| Complex *e} , dtype: dtype)
|
228
228
|
|
229
|
-
s = NMatrix.new([3,1], 0, dtype)
|
230
|
-
u = NMatrix.new([4,4], 0, dtype)
|
229
|
+
s = NMatrix.new([3,1], 0, dtype: dtype)
|
230
|
+
u = NMatrix.new([4,4], 0, dtype: dtype)
|
231
231
|
ldu = 4
|
232
|
-
vt = NMatrix.new([3,3], 0, dtype)
|
232
|
+
vt = NMatrix.new([3,3], 0, dtype: dtype)
|
233
233
|
ldvt= 3
|
234
234
|
else
|
235
|
-
a = NMatrix.new([4,3], dtype)
|
235
|
+
a = NMatrix.new([4,3], dtype: dtype)
|
236
236
|
end
|
237
237
|
err = case dtype
|
238
238
|
when :float32, :complex64
|