forked from translunar/nmatrix
-
Notifications
You must be signed in to change notification settings - Fork 133
/
shortcuts_spec.rb
266 lines (213 loc) · 6.17 KB
/
shortcuts_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# = NMatrix
#
# A linear algebra library for scientific computation in Ruby.
# NMatrix is part of SciRuby.
#
# NMatrix was originally inspired by and derived from NArray, by
# Masahiro Tanaka: http://narray.rubyforge.org
#
# == Copyright Information
#
# SciRuby is Copyright (c) 2010 - 2012, Ruby Science Foundation
# NMatrix is Copyright (c) 2012, Ruby Science Foundation
#
# Please see LICENSE.txt for additional copyright notices.
#
# == Contributing
#
# By contributing source code to SciRuby, you agree to be bound by
# our Contributor Agreement:
#
# * https://github.com/SciRuby/sciruby/wiki/Contributor-Agreement
#
# == shortcuts_spec.rb
#
# Specs for the shortcuts used in NMatrix and in NVector.
#
# Can we use require_relative here instead?
require File.join(File.dirname(__FILE__), "spec_helper.rb")
describe NMatrix do
it "zeros() creates a matrix of zeros" do
m = NMatrix.zeros(3)
n = NMatrix.new([3, 3], 0)
m.should.eql? n
end
it "ones() creates a matrix of ones" do
m = NMatrix.ones(3)
n = NMatrix.new([3, 3], 1)
m.should.eql? n
end
it "eye() creates an identity matrix" do
m = NMatrix.eye(3)
identity3 = NMatrix.new([3, 3], [1, 0, 0, 0, 1, 0, 0, 0, 1])
m.should.eql? identity3
end
it "diag() creates a matrix with pre-supplied diagonal" do
arr = [1,2,3,4]
m = NMatrix.diag(arr)
m.is_a?(NMatrix).should be_true
end
it "diagonals() contains the seeded values on the diagonal" do
arr = [1,2,3,4]
m = NMatrix.diagonals(arr)
m[0,0].should eq(arr[0])
m[1,1].should eq(arr[1])
m[2,2].should eq(arr[2])
m[3,3].should eq(arr[3])
end
it "random() creates a matrix of random numbers" do
m = NMatrix.random(2)
m.stype.should == :dense
m.dtype.should == :float64
end
it "random() only accepts an integer or an array as dimension" do
m = NMatrix.random([2, 2])
m.stype.should == :dense
m.dtype.should == :float64
expect { NMatrix.random(2.0) }.to raise_error
expect { NMatrix.random("not an array or integer") }.to raise_error
end
it "seq() creates a matrix of integers, sequentially" do
m = NMatrix.seq(2) # 2x2 matrix.
value = 0
2.times do |i|
2.times do |j|
m[i, j].should == value
value += 1
end
end
end
it "seq() only accepts an integer or a 2-element array as dimension" do
m = NMatrix.seq([2, 2])
value = 0
2.times do |i|
2.times do |j|
m[i, j].should == value
value += 1
end
end
expect { NMatrix.seq([1, 2, 3]) }.to raise_error
expect { NMatrix.seq("not an array or integer") }.to raise_error
end
it "indgen() creates a matrix of integers as well as seq()" do
m = NMatrix.indgen(2) # 2x2 matrix.
value = 0
2.times do |i|
2.times do |j|
m[i, j].should == value
value += 1
end
end
end
it "findgen creates a matrix of floats, sequentially" do
m = NMatrix.findgen(2) # 2x2 matrix.
value = 0
2.times do |i|
2.times do |j|
(m[i, j]/10).should be_within(Float::EPSILON).of(value.to_f/10)
value += 1
end
end
end
it "bindgen() creates a matrix of bytes" do
m = NMatrix.bindgen(2) # 2x2 matrix.
value = 0
2.times do |i|
2.times do |j|
m[i, j].should == value
value += 1
end
end
end
it "cindgen() creates a matrix of complexes" do
m = NMatrix.cindgen(2) # 2x2 matrix.
value = 0
2.times do |i|
2.times do |j|
m[i, j].real.should be_within(Float::EPSILON).of(value)
m[i, j].imag.should be_within(Float::EPSILON).of(0.0)
value += 1
end
end
end
it "column() returns a NMatrix" do
m = NMatrix.random(3)
m.column(2).is_a?(NMatrix).should be_true
end
it "row() returns a NMatrix" do
m = NMatrix.random(3)
m.row(2).is_a?(NMatrix).should be_true
end
it "diagonals() creates an NMatrix" do
arr = [1,2,3,4]
m = NMatrix.diagonals(arr)
m.is_a?(NMatrix).should be_true
end
it "diagonals() contains the seeded values on the diagonal" do
arr = [1,2,3,4]
m = NMatrix.diagonals(arr)
m[0,0].should eq(arr[0])
m[1,1].should eq(arr[1])
m[2,2].should eq(arr[2])
m[3,3].should eq(arr[3])
end
end
describe "NVector" do
it "zeros() creates a vector of zeros" do
v = NVector.zeros(4)
4.times do |i|
v[i].should == 0
end
end
it "ones() creates a vector of ones" do
v = NVector.ones(3)
3.times do |i|
v[i].should == 1
end
end
it "random() creates a vector of random numbers" do
v = NVector.random(4)
v.dtype.should == :float64
v.stype.should == :dense
end
it "seq() creates a vector of integers, sequentially" do
v = NVector.seq(7)
v.should == NVector.new(7, [0, 1, 2, 3, 4, 5, 6], :int64)
end
it "seq() only accepts integers as dimension" do
expect { NVector.seq(3) }.to_not raise_error
expect { NVector.seq([1, 3]) }.to raise_error
expect { NVector.seq(:wtf) }.to raise_error
end
it "indgen() creates a vector of integers as well as seq()" do
v = NVector.indgen(7)
v.should == NVector.new(7, [0, 1, 2, 3, 4, 5, 6], :int64)
end
it "findgen creates a vector of floats, sequentially" do
v = NVector.findgen(2)
v.should == NVector.new(2, [0.0, 1.0], :float32)
end
it "bindgen() creates a vector of bytes, sequentially" do
v = NVector.bindgen(4)
v.should == NVector.new(4, [0, 1, 2, 3], :byte)
end
it "cindgen() creates a vector of complexes, sequentially" do
v = NVector.cindgen(2)
v.should == NVector.new(2, [Complex(0.0, 0.0), Complex(1.0, 0.0)], :complex64)
end
it "linspace() creates a vector with n values equally spaced between a and b" do
v = NVector.linspace(0, 2, 5)
v.should == NVector.new(5, [0, 0.5, 1.0, 1.5, 2.0], :float64)
end
it "logspace() creates a vector with n values logarithmically spaced between decades 10^a and 10^b" do
v = NVector.logspace(0, 3, 4)
v.should == NVector.new(4, [1, 10, 100, 1000], :float64)
end
end
describe "Inline constructor" do
it "creates a NMatrix with the given values" do
m = NMatrix.new([2, 2], [1, 4, 6, 7])
n = NMatrix[[1, 4], [6, 7]]
m.should.eql? n
end
end