nmatrix 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/History.txt +102 -10
- data/README.rdoc +24 -32
- data/Rakefile +1 -1
- data/ext/nmatrix/data/complex.h +9 -0
- data/ext/nmatrix/data/data.cpp +78 -4
- data/ext/nmatrix/data/data.h +86 -54
- data/ext/nmatrix/data/rational.h +2 -0
- data/ext/nmatrix/data/ruby_object.h +38 -8
- data/ext/nmatrix/extconf.rb +13 -7
- data/ext/nmatrix/nmatrix.cpp +262 -139
- data/ext/nmatrix/nmatrix.h +11 -4
- data/ext/nmatrix/storage/common.cpp +20 -13
- data/ext/nmatrix/storage/common.h +18 -12
- data/ext/nmatrix/storage/dense.cpp +122 -192
- data/ext/nmatrix/storage/dense.h +4 -2
- data/ext/nmatrix/storage/list.cpp +467 -636
- data/ext/nmatrix/storage/list.h +6 -3
- data/ext/nmatrix/storage/storage.cpp +83 -46
- data/ext/nmatrix/storage/storage.h +7 -7
- data/ext/nmatrix/storage/yale.cpp +621 -361
- data/ext/nmatrix/storage/yale.h +21 -9
- data/ext/nmatrix/ttable_helper.rb +27 -31
- data/ext/nmatrix/types.h +1 -1
- data/ext/nmatrix/util/math.cpp +9 -10
- data/ext/nmatrix/util/sl_list.cpp +1 -7
- data/ext/nmatrix/util/sl_list.h +0 -118
- data/lib/nmatrix/blas.rb +59 -18
- data/lib/nmatrix/monkeys.rb +0 -52
- data/lib/nmatrix/nmatrix.rb +136 -9
- data/lib/nmatrix/nvector.rb +33 -0
- data/lib/nmatrix/shortcuts.rb +95 -16
- data/lib/nmatrix/version.rb +1 -1
- data/lib/nmatrix/yale_functions.rb +25 -19
- data/spec/blas_spec.rb +1 -19
- data/spec/elementwise_spec.rb +132 -17
- data/spec/lapack_spec.rb +0 -3
- data/spec/nmatrix_list_spec.rb +18 -0
- data/spec/nmatrix_spec.rb +44 -18
- data/spec/nmatrix_yale_spec.rb +1 -3
- data/spec/shortcuts_spec.rb +26 -36
- data/spec/slice_spec.rb +2 -4
- metadata +2 -2
data/lib/nmatrix/version.rb
CHANGED
@@ -39,33 +39,36 @@ module NMatrix::YaleFunctions
|
|
39
39
|
end
|
40
40
|
|
41
41
|
# call-seq:
|
42
|
-
#
|
42
|
+
# yale_ja_at(i) -> Array
|
43
43
|
#
|
44
44
|
# Returns the non-diagonal column indices which are stored in a given row.
|
45
|
-
def
|
46
|
-
yale_nd_row(i, :
|
45
|
+
def yale_ja_at i
|
46
|
+
yale_nd_row(i, :keys)
|
47
47
|
end
|
48
|
+
alias :yale_nd_row_as_array :yale_ja_at
|
48
49
|
|
49
50
|
# call-seq:
|
50
|
-
#
|
51
|
+
# yale_ja_set_at(i) -> Set
|
51
52
|
#
|
52
53
|
# Returns the non-diagonal column indices which are stored in a given row, as a Set.
|
53
|
-
def
|
54
|
+
def yale_ja_set_at i
|
54
55
|
require 'set'
|
55
|
-
yale_nd_row(i, :
|
56
|
+
yale_nd_row(i, :keys).to_set
|
56
57
|
end
|
58
|
+
alias :yale_nd_row_as_set :yale_ja_set_at
|
57
59
|
|
58
60
|
# call-seq:
|
59
|
-
#
|
61
|
+
# yale_ja_sorted_set_at -> SortedSet
|
60
62
|
#
|
61
63
|
# Returns the non-diagonal column indices which are stored in a given row, as a Set.
|
62
|
-
def
|
64
|
+
def yale_ja_sorted_set_at i
|
63
65
|
require 'set'
|
64
|
-
SortedSet.new(yale_nd_row(i, :
|
66
|
+
SortedSet.new(yale_nd_row(i, :keys))
|
65
67
|
end
|
68
|
+
alias :yale_nd_row_as_sorted_set :yale_ja_sorted_set_at
|
66
69
|
|
67
70
|
# call-seq:
|
68
|
-
# yale_nd_row_as_hash -> Hash
|
71
|
+
# yale_nd_row_as_hash(i) -> Hash
|
69
72
|
#
|
70
73
|
# Returns the non-diagonal column indices and entries stored in a given row.
|
71
74
|
def yale_nd_row_as_hash i
|
@@ -73,35 +76,38 @@ module NMatrix::YaleFunctions
|
|
73
76
|
end
|
74
77
|
|
75
78
|
# call-seq:
|
76
|
-
#
|
79
|
+
# yale_ja_d_keys_at(i) -> Array
|
77
80
|
#
|
78
81
|
# Returns the diagonal and non-digonal column indices stored in a given row.
|
79
|
-
def
|
80
|
-
ary = yale_nd_row(i, :
|
82
|
+
def yale_ja_d_keys_at i
|
83
|
+
ary = yale_nd_row(i, :keys)
|
81
84
|
return ary if i >= self.shape[1] || self[i,i].nil? || self[i,i] == 0
|
82
85
|
ary << i
|
83
86
|
end
|
87
|
+
alias :yale_row_as_array :yale_ja_d_keys_at
|
84
88
|
|
85
89
|
# call-seq:
|
86
|
-
#
|
90
|
+
# yale_ja_d_keys_set_at(i) -> Set
|
87
91
|
#
|
88
92
|
# Returns the diagonal and non-diagonal column indices stored in a given row.
|
89
|
-
def
|
93
|
+
def yale_ja_d_keys_set_at i
|
90
94
|
require 'set'
|
91
|
-
|
95
|
+
yale_ja_d_keys_at(i).to_set
|
92
96
|
end
|
97
|
+
alias :yale_row_as_set :yale_ja_d_keys_set_at
|
93
98
|
|
94
99
|
# call-seq:
|
95
|
-
#
|
100
|
+
# yale_ja_d_keys_sorted_set_at(i) -> SortedSet
|
96
101
|
#
|
97
102
|
# Returns the diagonal and non-diagonal column indices stored in a given row.
|
98
|
-
def
|
103
|
+
def yale_ja_d_keys_sorted_set_at i
|
99
104
|
require 'set'
|
100
105
|
SortedSet.new(yale_row_as_array(i))
|
101
106
|
end
|
107
|
+
alias :yale_row_as_sorted_set :yale_ja_d_keys_sorted_set_at
|
102
108
|
|
103
109
|
# call-seq:
|
104
|
-
# yale_row_as_hash -> Hash
|
110
|
+
# yale_row_as_hash(i) -> Hash
|
105
111
|
#
|
106
112
|
# Returns the diagonal and non-diagonal column indices and entries stored in a given row.
|
107
113
|
def yale_row_as_hash i
|
data/spec/blas_spec.rb
CHANGED
@@ -67,23 +67,6 @@ describe NMatrix::BLAS do
|
|
67
67
|
|
68
68
|
[:float32, :float64].each do |dtype|
|
69
69
|
context dtype do
|
70
|
-
it "exposes unfriendly cblas_rot" do
|
71
|
-
x = NVector.new(5, [1,2,3,4,5], dtype)
|
72
|
-
y = NVector.new(5, [-5,-4,-3,-2,-1], dtype)
|
73
|
-
NMatrix::BLAS::cblas_rot(5, x, 1, y, -1, 0.5, Math.sqrt(3)/2)
|
74
|
-
|
75
|
-
x[0].should be_within(1e-4).of(-0.3660254037844386)
|
76
|
-
x[1].should be_within(1e-4).of(-0.7320508075688772)
|
77
|
-
x[2].should be_within(1e-4).of(-1.098076211353316)
|
78
|
-
x[3].should be_within(1e-4).of(-1.4641016151377544)
|
79
|
-
x[4].should be_within(1e-4).of(-1.8301270189221928)
|
80
|
-
|
81
|
-
y[0].should be_within(1e-4).of(-6.830127018922193)
|
82
|
-
y[1].should be_within(1e-4).of(-5.464101615137754)
|
83
|
-
y[2].should be_within(1e-4).of(-4.098076211353316)
|
84
|
-
y[3].should be_within(1e-4).of(-2.732050807568877)
|
85
|
-
y[4].should be_within(1e-4).of(-1.3660254037844386)
|
86
|
-
end
|
87
70
|
|
88
71
|
it "exposes cblas rot" do
|
89
72
|
x = NVector.new(5, [1,2,3,4,5], dtype)
|
@@ -103,10 +86,9 @@ describe NMatrix::BLAS do
|
|
103
86
|
y[4].should be_within(1e-4).of(-1.3660254037844386)
|
104
87
|
end
|
105
88
|
|
106
|
-
# FIXME: Need to write new Rational algorithm, which doesn't choke quite so often on irrational square roots (which often eventually cancel).
|
107
89
|
it "exposes cblas rotg" do
|
108
90
|
ab = NVector.new(2, [6,-8], dtype)
|
109
|
-
c,s = NMatrix::BLAS::
|
91
|
+
c,s = NMatrix::BLAS::rotg(ab)
|
110
92
|
ab[0].should be_within(1e-6).of(-10)
|
111
93
|
ab[1].should be_within(1e-6).of(-5.quo(3))
|
112
94
|
c.should be_within(1e-6).of(-3.quo(5))
|
data/spec/elementwise_spec.rb
CHANGED
@@ -30,6 +30,95 @@ require File.join(File.dirname(__FILE__), "spec_helper.rb")
|
|
30
30
|
|
31
31
|
describe NMatrix do
|
32
32
|
|
33
|
+
context "yale" do
|
34
|
+
before :each do
|
35
|
+
@n = NMatrix.new(:yale, 3, :int64)
|
36
|
+
@m = NMatrix.new(:yale, 3, :int64)
|
37
|
+
@n[0,0] = 52
|
38
|
+
@n[0,2] = 5
|
39
|
+
@n[1,1] = 40
|
40
|
+
@n[0,1] = 30
|
41
|
+
@n[2,0] = 6
|
42
|
+
@m[1,1] = -48
|
43
|
+
@m[0,2] = -5
|
44
|
+
@n.extend NMatrix::YaleFunctions
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should perform scalar math" do
|
48
|
+
x = @n * 3
|
49
|
+
x[0,0].should == 52 * 3
|
50
|
+
x[0,1].should == 30 * 3
|
51
|
+
x[0,2].should == 5 * 3
|
52
|
+
x[1,1].should == 40 * 3
|
53
|
+
x[2,0].should == 6 * 3
|
54
|
+
|
55
|
+
r = NMatrix.new(:yale, 3, :int64)
|
56
|
+
y = r + 3
|
57
|
+
y[0,0].should == 3
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should refuse to perform a dot operation on a yale with non-zero default" do
|
61
|
+
r = NMatrix.new(:yale, 3, :int64)
|
62
|
+
y = r + 3
|
63
|
+
expect { y.dot(r) }.to raise_error
|
64
|
+
expect { r.dot(y) }.to raise_error
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should perform element-wise addition" do
|
68
|
+
r = NMatrix.new(:dense, 3, [52,30,0,0,-8,0,6,0,0], :int64).cast(:yale, :int64)
|
69
|
+
r[0,0] = 52
|
70
|
+
r[1,1] = -8
|
71
|
+
q = @n + @m
|
72
|
+
q.should == r
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should perform element-wise subtraction" do
|
76
|
+
(@n-@m).should == NMatrix.new(:dense, 3, [52,30,10,0,88,0,6,0,0], :int64).cast(:yale, :int64)
|
77
|
+
end
|
78
|
+
|
79
|
+
it "should perform element-wise multiplication" do
|
80
|
+
r = NMatrix.new(:dense, 3, [0,0,-25,0,-1920,0,0,0,0], :int64).cast(:yale, :int64)
|
81
|
+
m = NMatrix.new(:yale, 2, :int64)
|
82
|
+
(@n*@m).should == r
|
83
|
+
end
|
84
|
+
|
85
|
+
it "should perform element-wise division" do
|
86
|
+
r = NMatrix.new(:dense, 3, [52, 30, -2, 0, -1, 0, 6, 0, 0], :int64).cast(:yale, :int64)
|
87
|
+
(@n/(@m+1)).should == r
|
88
|
+
end
|
89
|
+
|
90
|
+
it "should perform element-wise modulo" do
|
91
|
+
m = NMatrix.new(:yale, 3, :int64) + 5
|
92
|
+
(@n % m).should == NMatrix.new(:dense, 3, [2,0,0,0,0,0,1,0,0], :int64).cast(:yale, :int64)
|
93
|
+
end
|
94
|
+
|
95
|
+
it "should handle element-wise equality (=~)" do
|
96
|
+
(@n =~ @m).should == NMatrix.new(:dense, 3, [false,false,false,true,false,true,false,true,true], :object).cast(:yale, :object, false)
|
97
|
+
end
|
98
|
+
|
99
|
+
it "should handle element-wise inequality (!~)" do
|
100
|
+
(@n !~ @m).should == NMatrix.new(:dense, 3, [true,true,true,false,true,false,true,false,false], :object).cast(:yale, :object, true)
|
101
|
+
end
|
102
|
+
|
103
|
+
it "should handle element-wise less-than (<)" do
|
104
|
+
(@m < @n).should == NMatrix.new(:dense, 3, [true,true,true,false,true,false,true,false,false], :object).cast(:yale, :object, true)
|
105
|
+
end
|
106
|
+
|
107
|
+
it "should handle element-wise greater-than (>)" do
|
108
|
+
(@n > @m).should == NMatrix.new(:dense, 3, [true,true,true,false,true,false,true,false,false], :object).cast(:yale, :object, false)
|
109
|
+
end
|
110
|
+
|
111
|
+
it "should handle element-wise greater-than-or-equals (>=)" do
|
112
|
+
(@n >= @m).should == NMatrix.new(:dense, 3, true, :object).cast(:yale,:object, true)
|
113
|
+
end
|
114
|
+
|
115
|
+
it "should handle element-wise less-than-or-equals (<=)" do
|
116
|
+
r = NMatrix.new(:dense, 3, [false,false,false,true,false,true,false,true,true], :object).cast(:yale, :object, false)
|
117
|
+
(@n <= @m).should == r
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
|
33
122
|
context "list" do
|
34
123
|
before :each do
|
35
124
|
@n = NMatrix.new(:list, 2, 0, :int64)
|
@@ -51,12 +140,15 @@ describe NMatrix do
|
|
51
140
|
end
|
52
141
|
|
53
142
|
it "should perform element-wise addition" do
|
54
|
-
r = NMatrix.new(:
|
55
|
-
|
143
|
+
r = NMatrix.new(:list, 2, 0, :int64)
|
144
|
+
r[0,0] = 52
|
145
|
+
r[1,1] = -8
|
146
|
+
q = @n + @m
|
147
|
+
q.should == r
|
56
148
|
end
|
57
149
|
|
58
150
|
it "should perform element-wise subtraction" do
|
59
|
-
r = NMatrix.new(:dense, 2, [52, 0, 0,
|
151
|
+
r = NMatrix.new(:dense, 2, [52, 0, 0, 88], :int64).cast(:list, :int64)
|
60
152
|
(@n-@m).should == r
|
61
153
|
end
|
62
154
|
|
@@ -75,31 +167,48 @@ describe NMatrix do
|
|
75
167
|
end
|
76
168
|
|
77
169
|
it "should perform element-wise modulo" do
|
78
|
-
|
170
|
+
m = NMatrix.new(:list, 2, 1, :int64)
|
171
|
+
m[0,0] = 50
|
172
|
+
m[1,1] = 40
|
173
|
+
(@n % m)
|
79
174
|
end
|
80
175
|
|
81
176
|
it "should handle element-wise equality (=~)" do
|
82
|
-
|
177
|
+
r = NMatrix.new(:list, 2, false, :object)
|
178
|
+
r[0,1] = true
|
179
|
+
r[1,0] = true
|
180
|
+
|
181
|
+
(@n =~ @m).should == r
|
83
182
|
end
|
84
183
|
|
85
184
|
it "should handle element-wise inequality (!~)" do
|
86
|
-
|
185
|
+
r = NMatrix.new(:list, 2, false, :object)
|
186
|
+
r[0,0] = true
|
187
|
+
r[1,1] = true
|
188
|
+
|
189
|
+
(@n !~ @m).should == r
|
87
190
|
end
|
88
191
|
|
89
192
|
it "should handle element-wise less-than (<)" do
|
90
|
-
(@n < @m).should == NMatrix.new(:list, 2,
|
193
|
+
(@n < @m).should == NMatrix.new(:list, 2, false, :object)
|
91
194
|
end
|
92
195
|
|
93
196
|
it "should handle element-wise greater-than (>)" do
|
94
|
-
|
197
|
+
r = NMatrix.new(:list, 2, false, :object)
|
198
|
+
r[0,0] = true
|
199
|
+
r[1,1] = true
|
200
|
+
(@n > @m).should == r
|
95
201
|
end
|
96
202
|
|
97
203
|
it "should handle element-wise greater-than-or-equals (>=)" do
|
98
|
-
(@n >= @m).
|
204
|
+
(@n >= @m).should == NMatrix.new(:list, 2, true, :object)
|
99
205
|
end
|
100
206
|
|
101
207
|
it "should handle element-wise less-than-or-equals (<=)" do
|
102
|
-
|
208
|
+
r = NMatrix.new(:list, 2, false, :object)
|
209
|
+
r[0,1] = true
|
210
|
+
r[1,0] = true
|
211
|
+
(@n <= @m).should == r
|
103
212
|
end
|
104
213
|
end
|
105
214
|
|
@@ -132,6 +241,12 @@ describe NMatrix do
|
|
132
241
|
r.should == NMatrix.new(:dense, [2,2], [-1, -2, 1, 0], :int64)
|
133
242
|
end
|
134
243
|
|
244
|
+
#it "exponentiates" do
|
245
|
+
# r = @n ** 2
|
246
|
+
# # TODO: We might have problems with the dtype.
|
247
|
+
# r.should == NMatrix.new(:dense, [2,2], [1, 4, 9, 16], :int64)
|
248
|
+
#end
|
249
|
+
|
135
250
|
it "modulo" do
|
136
251
|
pending "% operator not yet implemented"
|
137
252
|
r = @n % @m
|
@@ -147,33 +262,33 @@ describe NMatrix do
|
|
147
262
|
|
148
263
|
it "equals" do
|
149
264
|
r = @n =~ @m
|
150
|
-
r.should == NMatrix.new(:dense, [2,2], [
|
265
|
+
r.should == NMatrix.new(:dense, [2,2], [false, false, true, false], :object)
|
151
266
|
end
|
152
267
|
|
153
268
|
it "is not equal" do
|
154
269
|
r = @n !~ @m
|
155
|
-
r.should == NMatrix.new(:dense, [2,2], [
|
270
|
+
r.should == NMatrix.new(:dense, [2,2], [true, true, false, true], :object)
|
156
271
|
end
|
157
272
|
|
158
273
|
it "is less than" do
|
159
274
|
r = @n < @m
|
160
|
-
r.should == NMatrix.new(:dense, [2,2],
|
275
|
+
r.should == NMatrix.new(:dense, [2,2], false, :object)
|
161
276
|
end
|
162
277
|
|
163
278
|
it "is greater than" do
|
164
279
|
r = @n > @m
|
165
|
-
r.should == NMatrix.new(:dense, [2,2], [
|
280
|
+
r.should == NMatrix.new(:dense, [2,2], [true, true, false, true], :object)
|
166
281
|
end
|
167
282
|
|
168
283
|
it "is less than or equal to" do
|
169
284
|
r = @n <= @m
|
170
|
-
r.should == NMatrix.new(:dense, [2,2], [
|
285
|
+
r.should == NMatrix.new(:dense, [2,2], [false, false, true, false], :object)
|
171
286
|
end
|
172
287
|
|
173
288
|
it "is greater than or equal to" do
|
174
|
-
n = NMatrix.new(:dense, [2,2], [
|
289
|
+
n = NMatrix.new(:dense, [2,2], [1, 2, 2, 4], :int64)
|
175
290
|
r = n >= @m
|
176
|
-
r.should == NMatrix.new(:dense, [2,2], [
|
291
|
+
r.should == NMatrix.new(:dense, [2,2], [true, true, false, true], :object)
|
177
292
|
end
|
178
293
|
end
|
179
294
|
end
|
data/spec/lapack_spec.rb
CHANGED
@@ -67,9 +67,6 @@ describe NMatrix::LAPACK do
|
|
67
67
|
a[2,0].should == 1 # 3.quo(8)
|
68
68
|
a[2,1].should be_within(err).of(52.quo(9))
|
69
69
|
a[2,2].should be_within(err).of(360.quo(53))
|
70
|
-
# FIXME: these are rounded, == won't work
|
71
|
-
#a[2,1].should == 0.544118
|
72
|
-
#a[2,2].should == 5.294118
|
73
70
|
end
|
74
71
|
|
75
72
|
it "exposes clapack potrf" do
|
data/spec/nmatrix_list_spec.rb
CHANGED
@@ -43,6 +43,24 @@ describe NMatrix do
|
|
43
43
|
n.should == m
|
44
44
|
end
|
45
45
|
|
46
|
+
it "should compare 0-1 matrices with different default values with ==" do
|
47
|
+
n = NMatrix.new(:list, 2, 0, :object)
|
48
|
+
n[0,1] = 1
|
49
|
+
n[1,0] = 1
|
50
|
+
|
51
|
+
m = NMatrix.new(:list, 2, 1, :object)
|
52
|
+
m[0,0] = 0
|
53
|
+
m[1,1] = 0
|
54
|
+
|
55
|
+
n.should == m
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should allow creation of both true- and false-based matrix" do
|
59
|
+
r1 = NMatrix.new(:list, 2, false, :object)
|
60
|
+
r2 = NMatrix.new(:list, 2, true, :object)
|
61
|
+
r1.should_not == r2
|
62
|
+
end
|
63
|
+
|
46
64
|
it "should handle missing default value" do
|
47
65
|
NMatrix.new(:list, 3, :int8)[0,0].should == 0
|
48
66
|
NMatrix.new(:list, 4, :float64)[0,0].should == 0.0
|
data/spec/nmatrix_spec.rb
CHANGED
@@ -31,7 +31,7 @@ describe NMatrix do
|
|
31
31
|
|
32
32
|
it "calculates exact determinants on small square matrices" do
|
33
33
|
a = NMatrix.new(:dense, 2, [1,2,3,4], :int64)
|
34
|
-
|
34
|
+
a.det_exact.should == -2
|
35
35
|
end
|
36
36
|
|
37
37
|
it "calculates determinants" do
|
@@ -39,11 +39,23 @@ describe NMatrix do
|
|
39
39
|
m.det.should == 6
|
40
40
|
end
|
41
41
|
|
42
|
+
it "allows casting to Ruby objects" do
|
43
|
+
m = NMatrix.new(:dense, [3,3], [0,0,1,0,2,0,3,4,5], :int64)
|
44
|
+
n = m.cast(:dense, :object)
|
45
|
+
n.should == m
|
46
|
+
end
|
47
|
+
|
48
|
+
it "allows casting from Ruby objects" do
|
49
|
+
m = NMatrix.new(:dense, [3,3], [0,0,1,0,2,0,3,4,5], :object)
|
50
|
+
n = m.cast(:dense, :int64)
|
51
|
+
m.should == n
|
52
|
+
end
|
53
|
+
|
42
54
|
it "allows stype casting of a dim 2 matrix between dense, sparse, and list (different dtypes)" do
|
43
55
|
m = NMatrix.new(:dense, [3,3], [0,0,1,0,2,0,3,4,5], :int64).
|
44
56
|
cast(:yale, :int32).
|
45
57
|
cast(:dense, :float64).
|
46
|
-
cast(:list, :
|
58
|
+
cast(:list, :object).
|
47
59
|
cast(:dense, :int16).
|
48
60
|
cast(:list, :int32).
|
49
61
|
cast(:yale, :int64) #.
|
@@ -185,14 +197,31 @@ describe NMatrix do
|
|
185
197
|
c = dtype == :object ? "Ruby object" : "non-Ruby object"
|
186
198
|
context c do
|
187
199
|
it "allows iteration of matrices" do
|
188
|
-
|
189
|
-
|
200
|
+
n = nil
|
201
|
+
if storage_type == :dense
|
202
|
+
n = NMatrix.new(:dense, [3,3], [1,2,3,4,5,6,7,8,9], dtype)
|
203
|
+
else
|
204
|
+
n = storage_type == :yale ? NMatrix.new(storage_type, [3,4], dtype) : NMatrix.new(storage_type, [3,4], 0, dtype)
|
205
|
+
n[0,0] = 1
|
206
|
+
n[0,1] = 2
|
207
|
+
n[2,3] = 4
|
208
|
+
n[2,0] = 3
|
209
|
+
end
|
210
|
+
ary = []
|
190
211
|
n.each do |x|
|
191
|
-
|
212
|
+
ary << x
|
213
|
+
end
|
214
|
+
|
215
|
+
if storage_type == :dense
|
216
|
+
ary.should == [1,2,3,4,5,6,7,8,9]
|
217
|
+
else
|
218
|
+
ary.should == [1,2,0,0,0,0,0,0,3,0,0,4]
|
192
219
|
end
|
193
220
|
end
|
194
221
|
|
195
222
|
it "allows storage-based iteration of matrices" do
|
223
|
+
STDERR.puts storage_type.inspect
|
224
|
+
STDERR.puts dtype.inspect
|
196
225
|
n = storage_type == :yale ? NMatrix.new(storage_type, [3,3], dtype) : NMatrix.new(storage_type, [3,3], 0, dtype)
|
197
226
|
n[0,0] = 1
|
198
227
|
n[0,1] = 2
|
@@ -297,7 +326,7 @@ describe NMatrix do
|
|
297
326
|
val = (a.each { })
|
298
327
|
val.should eq a
|
299
328
|
end
|
300
|
-
|
329
|
+
|
301
330
|
it "should return the matrix being iterated over when each_stored_with_indices is called with a block" do
|
302
331
|
a = NMatrix.new(2,1)
|
303
332
|
val = (a.each_stored_with_indices { })
|
@@ -321,13 +350,13 @@ describe NMatrix do
|
|
321
350
|
|
322
351
|
end
|
323
352
|
end
|
324
|
-
|
353
|
+
|
325
354
|
it "should iterate through element 256 without a segfault" do
|
326
355
|
t = NVector.random(256)
|
327
356
|
t.each { |x| x + 0 }
|
328
357
|
end
|
329
358
|
|
330
|
-
context "mapping and reduction related functions" do
|
359
|
+
context "mapping and reduction related functions" do
|
331
360
|
|
332
361
|
before :each do
|
333
362
|
@nm_1d = N[5.0,0.0,1.0,2.0,3.0]
|
@@ -344,7 +373,7 @@ describe NMatrix do
|
|
344
373
|
@nm_2d.mean.should eq N[[1.0,2.0]]
|
345
374
|
end
|
346
375
|
|
347
|
-
it "should calculate the minimum along the specified dimension" do
|
376
|
+
it "should calculate the minimum along the specified dimension" do
|
348
377
|
@nm_1d.min.should eq 0.0
|
349
378
|
@nm_2d.min.should eq N[[0.0, 1.0]]
|
350
379
|
@nm_2d.min(1).should eq N[[0.0], [2.0]]
|
@@ -370,11 +399,11 @@ describe NMatrix do
|
|
370
399
|
@nm_2d.std(1).should eq N[[Math.sqrt(0.5)], [Math.sqrt(0.5)]]
|
371
400
|
end
|
372
401
|
|
373
|
-
it "should raise an ArgumentError when any invalid dimension is provided" do
|
402
|
+
it "should raise an ArgumentError when any invalid dimension is provided" do
|
374
403
|
expect { @nm_1d.mean(3) }.to raise_exception(ArgumentError)
|
375
404
|
end
|
376
405
|
|
377
|
-
it "should convert to float if it contains only a single element" do
|
406
|
+
it "should convert to float if it contains only a single element" do
|
378
407
|
N[4.0].to_f.should eq 4.0
|
379
408
|
N[[[[4.0]]]].to_f.should eq 4.0
|
380
409
|
end
|
@@ -383,7 +412,7 @@ describe NMatrix do
|
|
383
412
|
expect { @nm_1d.to_f }.to raise_error(IndexError)
|
384
413
|
end
|
385
414
|
|
386
|
-
it "should map a block to all elements" do
|
415
|
+
it "should map a block to all elements" do
|
387
416
|
@nm_1d.map { |e| e ** 2 }.should eq N[25.0,0.0,1.0,4.0,9.0]
|
388
417
|
@nm_2d.map { |e| e ** 2 }.should eq N[[0.0,1.0],[4.0,9.0]]
|
389
418
|
end
|
@@ -478,12 +507,12 @@ describe NMatrix do
|
|
478
507
|
|
479
508
|
it "should convert integer dtypes to float when calculating variance" do
|
480
509
|
m = N[[1,2,3], [3,4,5], :int32]
|
481
|
-
m.
|
510
|
+
m.variance(0).dtype.should eq :float64
|
482
511
|
end
|
483
512
|
|
484
513
|
it "should convert integer dtypes to float when calculating standard deviation" do
|
485
514
|
m = N[[1,2,3], [3,4,5], :int32]
|
486
|
-
m.
|
515
|
+
m.std(0).dtype.should eq :float64
|
487
516
|
end
|
488
517
|
|
489
518
|
context "_like constructors" do
|
@@ -493,13 +522,10 @@ describe NMatrix do
|
|
493
522
|
NMatrix.ones_like(@nm_2d).should eq N[[1.0, 1.0], [1.0, 1.0]]
|
494
523
|
end
|
495
524
|
|
496
|
-
it "should create an nmatrix of zeros with dimensions and type the same as its argument" do
|
525
|
+
it "should create an nmatrix of zeros with dimensions and type the same as its argument" do
|
497
526
|
NMatrix.zeros_like(@nm_1d).should eq N[0.0, 0.0, 0.0, 0.0, 0.0]
|
498
527
|
NMatrix.zeros_like(@nm_2d).should eq N[[0.0, 0.0], [0.0, 0.0]]
|
499
528
|
end
|
500
|
-
|
501
529
|
end
|
502
|
-
|
503
530
|
end
|
504
|
-
|
505
531
|
end
|