nmatrix 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -1
- data/History.txt +31 -3
- data/Manifest.txt +5 -0
- data/README.rdoc +29 -27
- data/ext/nmatrix/binary_format.txt +53 -0
- data/ext/nmatrix/data/data.cpp +18 -18
- data/ext/nmatrix/data/data.h +38 -7
- data/ext/nmatrix/data/rational.h +13 -0
- data/ext/nmatrix/data/ruby_object.h +10 -0
- data/ext/nmatrix/extconf.rb +2 -0
- data/ext/nmatrix/nmatrix.cpp +655 -103
- data/ext/nmatrix/nmatrix.h +26 -14
- data/ext/nmatrix/ruby_constants.cpp +4 -0
- data/ext/nmatrix/ruby_constants.h +2 -0
- data/ext/nmatrix/storage/dense.cpp +99 -41
- data/ext/nmatrix/storage/dense.h +3 -3
- data/ext/nmatrix/storage/list.cpp +36 -14
- data/ext/nmatrix/storage/list.h +4 -4
- data/ext/nmatrix/storage/storage.cpp +19 -19
- data/ext/nmatrix/storage/storage.h +11 -11
- data/ext/nmatrix/storage/yale.cpp +17 -20
- data/ext/nmatrix/storage/yale.h +13 -11
- data/ext/nmatrix/util/io.cpp +25 -23
- data/ext/nmatrix/util/io.h +5 -5
- data/ext/nmatrix/util/math.cpp +634 -17
- data/ext/nmatrix/util/math.h +958 -9
- data/ext/nmatrix/util/sl_list.cpp +7 -7
- data/ext/nmatrix/util/sl_list.h +2 -2
- data/lib/nmatrix.rb +9 -0
- data/lib/nmatrix/blas.rb +4 -4
- data/lib/nmatrix/io/market.rb +227 -0
- data/lib/nmatrix/io/mat_reader.rb +7 -7
- data/lib/nmatrix/lapack.rb +80 -0
- data/lib/nmatrix/nmatrix.rb +78 -52
- data/lib/nmatrix/shortcuts.rb +486 -0
- data/lib/nmatrix/version.rb +1 -1
- data/spec/2x2_dense_double.mat +0 -0
- data/spec/blas_spec.rb +59 -9
- data/spec/elementwise_spec.rb +25 -12
- data/spec/io_spec.rb +69 -1
- data/spec/lapack_spec.rb +53 -4
- data/spec/math_spec.rb +9 -0
- data/spec/nmatrix_list_spec.rb +95 -0
- data/spec/nmatrix_spec.rb +10 -53
- data/spec/nmatrix_yale_spec.rb +17 -15
- data/spec/shortcuts_spec.rb +154 -0
- metadata +22 -15
data/lib/nmatrix/version.rb
CHANGED
Binary file
|
data/spec/blas_spec.rb
CHANGED
@@ -31,17 +31,67 @@ require File.join(File.dirname(__FILE__), "spec_helper.rb")
|
|
31
31
|
describe NMatrix::BLAS do
|
32
32
|
[:rational32, :rational64, :rational128, :float32, :float64, :complex64, :complex128].each do |dtype|
|
33
33
|
context dtype do
|
34
|
-
it "exposes cblas trsm
|
35
|
-
a
|
36
|
-
b
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
34
|
+
it "exposes cblas trsm" do
|
35
|
+
a = NMatrix.new(:dense, 3, [4,-1.quo(2), -3.quo(4), -2, 2, -1.quo(4), -4, -2, -1.quo(2)], dtype)
|
36
|
+
b = NVector.new(3, [-1, 17, -9], dtype)
|
37
|
+
NMatrix::BLAS::cblas_trsm(:row, :right, :lower, :transpose, :nonunit, 1, 3, 1.0, a, 3, b, 3)
|
38
|
+
|
39
|
+
# These test results all come from actually running a matrix through BLAS. We use them to ensure that NMatrix's
|
40
|
+
# version of these functions (for rationals) give similar results.
|
41
|
+
|
42
|
+
b[0].should == -1.quo(4)
|
43
|
+
b[1].should == 33.quo(4)
|
44
|
+
b[2].should == -13
|
45
|
+
|
46
|
+
NMatrix::BLAS::cblas_trsm(:row, :right, :upper, :transpose, :unit, 1, 3, 1.0, a, 3, b, 3)
|
47
|
+
|
48
|
+
b[0].should == -15.quo(2)
|
49
|
+
b[1].should == 5
|
50
|
+
b[2].should == -13
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
[:rational32,:rational64,:rational128,:complex64,:complex128].each do |dtype|
|
56
|
+
context dtype do
|
57
|
+
it "exposes cblas rot"
|
58
|
+
end
|
59
|
+
|
60
|
+
context dtype do
|
61
|
+
it "exposes cblas rotg"
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
[:float32, :float64].each do |dtype|
|
66
|
+
context dtype do
|
67
|
+
it "exposes cblas rot" do
|
68
|
+
x = NVector.new(5, [1,2,3,4,5], dtype)
|
69
|
+
y = NVector.new(5, [-5,-4,-3,-2,-1], dtype)
|
70
|
+
NMatrix::BLAS::cblas_rot(5, x, 1, y, -1, 0.5, Math.sqrt(3)/2)
|
71
|
+
|
72
|
+
x[0].should be_within(1e-4).of(-0.3660254037844386)
|
73
|
+
x[1].should be_within(1e-4).of(-0.7320508075688772)
|
74
|
+
x[2].should be_within(1e-4).of(-1.098076211353316)
|
75
|
+
x[3].should be_within(1e-4).of(-1.4641016151377544)
|
76
|
+
x[4].should be_within(1e-4).of(-1.8301270189221928)
|
77
|
+
|
78
|
+
y[0].should be_within(1e-4).of(-6.830127018922193)
|
79
|
+
y[1].should be_within(1e-4).of(-5.464101615137754)
|
80
|
+
y[2].should be_within(1e-4).of(-4.098076211353316)
|
81
|
+
y[3].should be_within(1e-4).of(-2.732050807568877)
|
82
|
+
y[4].should be_within(1e-4).of(-1.3660254037844386)
|
83
|
+
end
|
84
|
+
|
85
|
+
# FIXME: Need to write new Rational algorithm, which doesn't choke quite so often on irrational square roots (which often eventually cancel).
|
86
|
+
it "exposes cblas rotg" do
|
87
|
+
ab = NVector.new(2, [6,-8], dtype)
|
88
|
+
c,s = NMatrix::BLAS::cblas_rotg(ab)
|
89
|
+
ab[0].should be_within(1e-6).of(-10)
|
90
|
+
ab[1].should be_within(1e-6).of(-5.quo(3))
|
91
|
+
c.should be_within(1e-6).of(-3.quo(5))
|
92
|
+
s.should be_within(1e-6).of(4.quo(5))
|
42
93
|
end
|
43
94
|
|
44
|
-
it "exposes cblas trsm, with B as a matrix"
|
45
95
|
end
|
46
96
|
end
|
47
97
|
end
|
data/spec/elementwise_spec.rb
CHANGED
@@ -35,55 +35,68 @@ describe NMatrix do
|
|
35
35
|
@n = NMatrix.new(:list, 2, 0, :int64)
|
36
36
|
@m = NMatrix.new(:list, 2, 0, :int64)
|
37
37
|
@n[0,0] = 52
|
38
|
-
@m[
|
38
|
+
@m[1,1] = -48
|
39
39
|
@n[1,1] = 40
|
40
40
|
end
|
41
41
|
|
42
|
+
it "should perform scalar math" do
|
43
|
+
x = @n * 3
|
44
|
+
x[0,0].should == 52 * 3
|
45
|
+
x[1,1].should == 40 * 3
|
46
|
+
x[0,1].should == 0
|
47
|
+
|
48
|
+
r = NMatrix.new(:list, 3, 1)
|
49
|
+
y = r + 3
|
50
|
+
y[0,0].should == 4
|
51
|
+
end
|
52
|
+
|
42
53
|
it "should perform element-wise addition" do
|
43
|
-
r = NMatrix.new(:dense, 2, [
|
54
|
+
r = NMatrix.new(:dense, 2, [52, 0, 0, -8], :int64).cast(:list, :int64)
|
44
55
|
(@n+@m).should == r
|
45
56
|
end
|
46
57
|
|
47
58
|
it "should perform element-wise subtraction" do
|
48
|
-
r = NMatrix.new(:dense, 2, [
|
59
|
+
r = NMatrix.new(:dense, 2, [52, 0, 0, -88], :int64).cast(:list, :int64)
|
49
60
|
(@n-@m).should == r
|
50
61
|
end
|
51
62
|
|
52
63
|
it "should perform element-wise multiplication" do
|
53
|
-
r = NMatrix.new(:dense, 2, [
|
54
|
-
|
64
|
+
r = NMatrix.new(:dense, 2, [52, 0, 0, -1920], :int64).cast(:list, :int64)
|
65
|
+
m = NMatrix.new(:list, 2, 1, :int64)
|
66
|
+
m[1,1] = -48
|
67
|
+
(@n*m).should == r
|
55
68
|
end
|
56
69
|
|
57
70
|
it "should perform element-wise division" do
|
58
71
|
m = NMatrix.new(:list, 2, 1, :int64)
|
59
72
|
m[1,1] = 2
|
60
73
|
r = NMatrix.new(:dense, 2, [52, 0, 0, 20], :int64).cast(:list, :int64)
|
61
|
-
(@n
|
74
|
+
(@n/m).should == r
|
62
75
|
end
|
63
76
|
|
64
77
|
it "should perform element-wise modulo"
|
65
78
|
|
66
|
-
it "
|
79
|
+
it "should handle element-wise equality (=~)" do
|
67
80
|
(@n =~ @m).cast(:dense, :byte).should == NMatrix.new(:dense, 2, [0, 1, 1, 0], :byte)
|
68
81
|
end
|
69
82
|
|
70
|
-
it "
|
83
|
+
it "should handle element-wise inequality (!~)" do
|
71
84
|
(@n !~ @m).cast(:dense, :byte).should == NMatrix.new(:dense, 2, [1, 0, 0, 1], :byte)
|
72
85
|
end
|
73
86
|
|
74
|
-
it "
|
87
|
+
it "should handle element-wise less-than (<)" do
|
75
88
|
(@n < @m).should == NMatrix.new(:list, 2, 0, :byte)
|
76
89
|
end
|
77
90
|
|
78
|
-
it "
|
91
|
+
it "should handle element-wise greater-than (>)" do
|
79
92
|
(@n > @m).should == NMatrix.new(:dense, 2, [1, 0, 0, 1], :byte).cast(:list, :byte)
|
80
93
|
end
|
81
94
|
|
82
|
-
it "
|
95
|
+
it "should handle element-wise greater-than-or-equals (>=)" do
|
83
96
|
(@n >= @m).cast(:dense, :byte).should == NMatrix.new(:dense, 2, [1, 1, 1, 1], :byte)
|
84
97
|
end
|
85
98
|
|
86
|
-
it "
|
99
|
+
it "should handle element-wise less-than-or-equals (<=)" do
|
87
100
|
(@n <= @m).cast(:dense, :byte).should == NMatrix.new(:dense, 2, [0, 1, 1, 0], :byte)
|
88
101
|
end
|
89
102
|
end
|
data/spec/io_spec.rb
CHANGED
@@ -52,9 +52,77 @@ describe NMatrix::IO do
|
|
52
52
|
n[3,3].should == 0
|
53
53
|
end
|
54
54
|
|
55
|
-
it "reads MATLAB .mat file containing a single dense matrix" do
|
55
|
+
it "reads MATLAB .mat file containing a single dense integer matrix" do
|
56
56
|
n = NMatrix::IO::Matlab.load_mat("spec/4x5_dense.mat")
|
57
57
|
m = NMatrix.new(:dense, [4,5], [16,17,18,19,20,15,14,13,12,11,6,7,8,9,10,5,4,3,2,1])
|
58
58
|
n.should == m
|
59
59
|
end
|
60
|
+
|
61
|
+
it "reads MATLAB .mat file containing a single dense double matrix" do
|
62
|
+
n = NMatrix::IO::Matlab.load_mat("spec/2x2_dense_double.mat")
|
63
|
+
m = NMatrix.new(:dense, 2, [1.1, 2.0, 3.0, 4.0], :float64)
|
64
|
+
n.should == m
|
65
|
+
end
|
66
|
+
|
67
|
+
it "loads and saves MatrixMarket .mtx file containing a single large sparse double matrix" do
|
68
|
+
n = NMatrix::IO::Market.load("spec/utm5940.mtx")
|
69
|
+
NMatrix::IO::Market.save(n, "spec/utm5940.saved.mtx")
|
70
|
+
`wc -l spec/utm5940.mtx`.split[0].should == `wc -l spec/utm5940.saved.mtx`.split[0]
|
71
|
+
end
|
72
|
+
|
73
|
+
it "reads and writes NMatrix dense" do
|
74
|
+
n = NMatrix.new(:dense, [4,3], [0,1,2,3,4,5,6,7,8,9,10,11], :int32)
|
75
|
+
n.write("test-out")
|
76
|
+
|
77
|
+
m = NMatrix.read("test-out")
|
78
|
+
n.should == m
|
79
|
+
end
|
80
|
+
|
81
|
+
it "reads and writes NMatrix dense as symmetric" do
|
82
|
+
n = NMatrix.new(:dense, 3, [0,1,2,1,3,4,2,4,5], :int16)
|
83
|
+
n.write("test-out", :symmetric)
|
84
|
+
|
85
|
+
m = NMatrix.read("test-out")
|
86
|
+
n.should == m
|
87
|
+
end
|
88
|
+
|
89
|
+
it "reads and writes NMatrix dense as skew" do
|
90
|
+
n = NMatrix.new(:dense, 3, [0,1,2,-1,3,4,-2,-4,5], :float64)
|
91
|
+
n.write("test-out", :skew)
|
92
|
+
|
93
|
+
m = NMatrix.read("test-out")
|
94
|
+
n.should == m
|
95
|
+
end
|
96
|
+
|
97
|
+
it "reads and writes NMatrix dense as hermitian" do
|
98
|
+
n = NMatrix.new(:dense, 3, [0,1,2,1,3,4,2,4,5], :complex64)
|
99
|
+
n.write("test-out", :hermitian)
|
100
|
+
|
101
|
+
m = NMatrix.read("test-out")
|
102
|
+
n.should == m
|
103
|
+
end
|
104
|
+
|
105
|
+
it "reads and writes NMatrix dense as upper" do
|
106
|
+
n = NMatrix.new(:dense, 3, [-1,1,2,3,4,5,6,7,8], :int32)
|
107
|
+
n.write("test-out", :upper)
|
108
|
+
|
109
|
+
m = NMatrix.new(:dense, 3, [-1,1,2,0,4,5,0,0,8], :int32) # lower version of the same
|
110
|
+
|
111
|
+
o = NMatrix.read("test-out")
|
112
|
+
o.should == m
|
113
|
+
o.should_not == n
|
114
|
+
end
|
115
|
+
|
116
|
+
it "reads and writes NMatrix dense as lower" do
|
117
|
+
n = NMatrix.new(:dense, 3, [-1,1,2,3,4,5,6,7,8], :int32)
|
118
|
+
n.write("test-out", :lower)
|
119
|
+
|
120
|
+
m = NMatrix.new(:dense, 3, [-1,0,0,3,4,0,6,7,8], :int32) # lower version of the same
|
121
|
+
|
122
|
+
o = NMatrix.read("test-out")
|
123
|
+
o.should == m
|
124
|
+
o.should_not == n
|
125
|
+
end
|
126
|
+
|
127
|
+
|
60
128
|
end
|
data/spec/lapack_spec.rb
CHANGED
@@ -29,6 +29,19 @@
|
|
29
29
|
require File.join(File.dirname(__FILE__), "spec_helper.rb")
|
30
30
|
|
31
31
|
describe NMatrix::LAPACK do
|
32
|
+
# where integer math is allowed
|
33
|
+
[:byte, :int8, :int16, :int32, :int64, :rational32, :rational64, :rational128, :float32, :float64, :complex64, :complex128].each do |dtype|
|
34
|
+
context dtype do
|
35
|
+
it "exposes clapack laswp" do
|
36
|
+
a = NMatrix.new(:dense, [3,4], [1,2,3,4,5,6,7,8,9,10,11,12], dtype)
|
37
|
+
NMatrix::LAPACK::clapack_laswp(3, a, 4, 0, 3, [2,1,3,0], 1)
|
38
|
+
b = NMatrix.new(:dense, [3,4], [3,2,4,1,7,6,8,5,11,10,12,9], dtype)
|
39
|
+
a.should == b
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# where integer math is not allowed
|
32
45
|
[:rational32, :rational64, :rational128, :float32, :float64, :complex64, :complex128].each do |dtype|
|
33
46
|
context dtype do
|
34
47
|
it "exposes clapack getrf" do
|
@@ -37,16 +50,52 @@ describe NMatrix::LAPACK do
|
|
37
50
|
a[0,0].should == 8
|
38
51
|
a[0,1].should == 1
|
39
52
|
a[0,2].should == 6
|
40
|
-
a[1,0].should ==
|
41
|
-
a[1,1].should ==
|
53
|
+
a[1,0].should == 1.quo(2)
|
54
|
+
a[1,1].should == 17.quo(2)
|
42
55
|
a[1,2].should == -1
|
43
|
-
a[2,0].should ==
|
56
|
+
a[2,0].should == 3.quo(8)
|
44
57
|
# FIXME: these are rounded, == won't work
|
45
58
|
#a[2,1].should == 0.544118
|
46
59
|
#a[2,2].should == 5.294118
|
47
60
|
end
|
48
61
|
|
49
|
-
it "exposes
|
62
|
+
it "exposes clapack potrf" do
|
63
|
+
# first do upper
|
64
|
+
a = NMatrix.new(:dense, 3, [25,15,-5, 0,18,0, 0,0,11], dtype)
|
65
|
+
NMatrix::LAPACK::clapack_potrf(:row, :upper, 3, a, 3)
|
66
|
+
b = NMatrix.new(:dense, 3, [5,3,-1, 0,3,1, 0,0,3], dtype)
|
67
|
+
a.should == b
|
68
|
+
|
69
|
+
# then do lower
|
70
|
+
a = NMatrix.new(:dense, 3, [25,0,0, 15,18,0,-5,0,11], dtype)
|
71
|
+
NMatrix::LAPACK::clapack_potrf(:row, :lower, 3, a, 3)
|
72
|
+
b = NMatrix.new(:dense, 3, [5,0,0, 3,3,0, -1,1,3], dtype)
|
73
|
+
a.should == b
|
74
|
+
end
|
75
|
+
|
76
|
+
# Together, these calls are basically xGESV from LAPACK: http://www.netlib.org/lapack/double/dgesv.f
|
77
|
+
it "exposes clapack getrs" do
|
78
|
+
a = NMatrix.new(:dense, 3, [-2,4,-3,3,-2,1,0,-4,3], dtype)
|
79
|
+
ipiv = NMatrix::LAPACK::clapack_getrf(:row, 3, 3, a, 3)
|
80
|
+
b = NVector.new(3, [-1, 17, -9], dtype)
|
81
|
+
|
82
|
+
NMatrix::LAPACK::clapack_getrs(:row, false, 3, 1, a, 3, ipiv, b, 3)
|
83
|
+
|
84
|
+
b[0].should == 5
|
85
|
+
b[1].should == -15.quo(2)
|
86
|
+
b[2].should == -13
|
87
|
+
end
|
88
|
+
|
89
|
+
it "exposes clapack getri" do
|
90
|
+
a = NMatrix.new(:dense, 3, [1,0,4,1,1,6,-3,0,-10], dtype)
|
91
|
+
ipiv = NMatrix::LAPACK::clapack_getrf(:row, 3, 3, a, 3) # get pivot from getrf, use for getri
|
92
|
+
NMatrix::LAPACK::clapack_getri(:row, 3, a, 3, ipiv)
|
93
|
+
|
94
|
+
b = NMatrix.new(:dense, 3, [-5,0,-2,-4,1,-1,1.5,0,0.5], dtype)
|
95
|
+
a.should == b
|
96
|
+
end
|
97
|
+
|
98
|
+
|
50
99
|
end
|
51
100
|
end
|
52
101
|
end
|
data/spec/math_spec.rb
CHANGED
@@ -45,6 +45,15 @@ describe "math" do
|
|
45
45
|
a[2,0].should == 0.375
|
46
46
|
end
|
47
47
|
end
|
48
|
+
|
49
|
+
context dtype do
|
50
|
+
it "should correctly invert a matrix" do
|
51
|
+
a = NMatrix.new(:dense, 3, [1,0,4,1,1,6,-3,0,-10], dtype)
|
52
|
+
b = NMatrix.new(:dense, 3, [-5,0,-2,-4,1,-1,1.5,0,0.5], dtype)
|
53
|
+
a.invert!
|
54
|
+
a.should == b
|
55
|
+
end
|
56
|
+
end
|
48
57
|
end
|
49
58
|
|
50
59
|
[:float32, :float64, :complex64, :complex128].each do |dtype|
|
@@ -0,0 +1,95 @@
|
|
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
|
+
# == nmatrix_list_spec.rb
|
24
|
+
#
|
25
|
+
# Basic tests for NMatrix's list-of-lists storage type.
|
26
|
+
#
|
27
|
+
require "./lib/nmatrix"
|
28
|
+
|
29
|
+
describe NMatrix do
|
30
|
+
context :list do
|
31
|
+
it "should compare with ==" do
|
32
|
+
n = NMatrix.new(:list, [3,3,3], :int64)
|
33
|
+
m = NMatrix.new(:list, [3,3,3], :int64)
|
34
|
+
n.should == m
|
35
|
+
n[0,0,0] = 5
|
36
|
+
n.should_not == m
|
37
|
+
n[0,0,1] = 52
|
38
|
+
n[1,2,1] = -4
|
39
|
+
|
40
|
+
m[0,0,0] = 5
|
41
|
+
m[0,0,1] = 52
|
42
|
+
m[1,2,1] = -4
|
43
|
+
n.should == m
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should handle missing default value" do
|
47
|
+
NMatrix.new(:list, 3, :int8)[0,0].should == 0
|
48
|
+
NMatrix.new(:list, 4, :float64)[0,0].should == 0.0
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should allow conversion to a Ruby Hash" do
|
52
|
+
n = NMatrix.new(:list, 3, 1, :int64)
|
53
|
+
n[0,1] = 50
|
54
|
+
h = n.to_h
|
55
|
+
h.size.should == 1
|
56
|
+
h[0].size.should == 1
|
57
|
+
h[0][1].should == 50
|
58
|
+
h[0][2].should == 1
|
59
|
+
h[1][0].should == 1
|
60
|
+
end
|
61
|
+
|
62
|
+
|
63
|
+
##TODO: Make this test better. It's not nearly exhaustive enough as is.
|
64
|
+
it "should handle recursive removal" do
|
65
|
+
n = NMatrix.new(:list, [3,3,3], 0)
|
66
|
+
n[0,0,0] = 2
|
67
|
+
n[1,1,1] = 1
|
68
|
+
n[1,0,0] = 3
|
69
|
+
n[0,0,1] = 4
|
70
|
+
|
71
|
+
n[0,0,0].should == 2
|
72
|
+
n[1,1,1].should == 1
|
73
|
+
n[1,0,0].should == 3
|
74
|
+
n[0,0,1].should == 4
|
75
|
+
|
76
|
+
n[1,1,1] = 0
|
77
|
+
n[0,0,0].should == 2
|
78
|
+
n[1,1,1].should == 0
|
79
|
+
n[1,0,0].should == 3
|
80
|
+
n[0,0,1].should == 4
|
81
|
+
end
|
82
|
+
|
83
|
+
it "should correctly insert a value between the middle and last entries of a three-element list" do
|
84
|
+
n = NMatrix.new(:list, 5940, 0, :float64)
|
85
|
+
n[0,0] = -7.0710685196786e-01
|
86
|
+
n[330,0] = 7.0710685196786e-01
|
87
|
+
n[1,0] = -7.0710685196786e-01
|
88
|
+
n[2,0] = -7.0710685196786e-01
|
89
|
+
n[0,0].should_not == 0
|
90
|
+
n[330,0].should_not == 0
|
91
|
+
n[2,0].should_not == 0
|
92
|
+
n[1,0].should_not == 0
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
data/spec/nmatrix_spec.rb
CHANGED
@@ -35,6 +35,11 @@ describe NMatrix do
|
|
35
35
|
x = a.det_exact
|
36
36
|
end
|
37
37
|
|
38
|
+
it "calculates determinants" do
|
39
|
+
m = NMatrix.new(:dense, 3, [-2,2,3,-1,1,3,2,0,-1])
|
40
|
+
m.det.should == 6
|
41
|
+
end
|
42
|
+
|
38
43
|
it "allows stype casting of a dim 2 matrix between dense, sparse, and list (different dtypes)" do
|
39
44
|
m = NMatrix.new(:dense, [3,3], [0,0,1,0,2,0,3,4,5], :int64).
|
40
45
|
cast(:yale, :int32).
|
@@ -51,21 +56,6 @@ describe NMatrix do
|
|
51
56
|
end
|
52
57
|
|
53
58
|
|
54
|
-
it "compares two list matrices" do
|
55
|
-
n = NMatrix.new(:list, [3,3,3], :int64)
|
56
|
-
m = NMatrix.new(:list, [3,3,3], :int64)
|
57
|
-
n.should == m
|
58
|
-
n[0,0,0] = 5
|
59
|
-
n.should_not == m
|
60
|
-
n[0,0,1] = 52
|
61
|
-
n[1,2,1] = -4
|
62
|
-
|
63
|
-
m[0,0,0] = 5
|
64
|
-
m[0,0,1] = 52
|
65
|
-
m[1,2,1] = -4
|
66
|
-
n.should == m
|
67
|
-
end
|
68
|
-
|
69
59
|
it "fills dense Ruby object matrix with nil" do
|
70
60
|
n = NMatrix.new([4,3], :object)
|
71
61
|
n[0,0].should == nil
|
@@ -134,44 +124,6 @@ describe NMatrix do
|
|
134
124
|
end
|
135
125
|
|
136
126
|
|
137
|
-
it "list handles missing initialization value" do
|
138
|
-
NMatrix.new(:list, 3, :int8)[0,0].should == 0
|
139
|
-
NMatrix.new(:list, 4, :float64)[0,0].should == 0.0
|
140
|
-
end
|
141
|
-
|
142
|
-
it "should allow conversion of list storage to a Ruby Hash" do
|
143
|
-
n = NMatrix.new(:list, 3, 1, :int64)
|
144
|
-
n[0,1] = 50
|
145
|
-
h = n.to_h
|
146
|
-
h.size.should == 1
|
147
|
-
h[0].size.should == 1
|
148
|
-
h[0][1].should == 50
|
149
|
-
h[0][2].should == 1
|
150
|
-
h[1][0].should == 1
|
151
|
-
end
|
152
|
-
|
153
|
-
|
154
|
-
##TODO: Make this test better. It's not nearly exhaustive enough as is.
|
155
|
-
it "list handles recursive removal" do
|
156
|
-
n = NMatrix.new(:list, [3,3,3], 0)
|
157
|
-
n[0,0,0] = 2
|
158
|
-
n[1,1,1] = 1
|
159
|
-
n[1,0,0] = 3
|
160
|
-
n[0,0,1] = 4
|
161
|
-
|
162
|
-
n[0,0,0].should == 2
|
163
|
-
n[1,1,1].should == 1
|
164
|
-
n[1,0,0].should == 3
|
165
|
-
n[0,0,1].should == 4
|
166
|
-
|
167
|
-
n[1,1,1] = 0
|
168
|
-
n[0,0,0].should == 2
|
169
|
-
n[1,1,1].should == 0
|
170
|
-
n[1,0,0].should == 3
|
171
|
-
n[0,0,1].should == 4
|
172
|
-
end
|
173
|
-
|
174
|
-
|
175
127
|
it "dense handles missing initialization value" do
|
176
128
|
n = NMatrix.new(3, :int8)
|
177
129
|
n.stype.should == :dense
|
@@ -336,6 +288,11 @@ describe NMatrix do
|
|
336
288
|
NMatrix.new(storage_type, [3,2,8], 0).shape.should == [3,2,8]
|
337
289
|
NMatrix.new(storage_type, [3,2,8], 0).dim.should == 3
|
338
290
|
end
|
291
|
+
|
292
|
+
it "returns number of rows and columns" do
|
293
|
+
NMatrix.new(storage_type, [7, 4], 3).rows.should == 7
|
294
|
+
NMatrix.new(storage_type, [7, 4], 3).cols.should == 4
|
295
|
+
end
|
339
296
|
end unless storage_type == :yale
|
340
297
|
end
|
341
298
|
|