nmatrix 0.1.0.rc3 → 0.1.0.rc4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +4 -4
  2. data/CONTRIBUTING.md +22 -21
  3. data/History.txt +13 -0
  4. data/Manifest.txt +1 -2
  5. data/README.rdoc +8 -8
  6. data/ext/nmatrix/binary_format.txt +1 -1
  7. data/ext/nmatrix/data/complex.h +21 -21
  8. data/ext/nmatrix/data/data.cpp +9 -2
  9. data/ext/nmatrix/data/data.h +4 -2
  10. data/ext/nmatrix/math.cpp +69 -31
  11. data/ext/nmatrix/math/getf2.h +2 -2
  12. data/ext/nmatrix/math/getrf.h +2 -2
  13. data/ext/nmatrix/math/imax.h +101 -0
  14. data/ext/nmatrix/math/scal.h +30 -10
  15. data/ext/nmatrix/math/swap.h +1 -22
  16. data/ext/nmatrix/nm_memory.h +1 -1
  17. data/ext/nmatrix/nmatrix.h +2 -2
  18. data/ext/nmatrix/ruby_constants.cpp +1 -2
  19. data/ext/nmatrix/ruby_constants.h +6 -7
  20. data/ext/nmatrix/ruby_nmatrix.c +23 -18
  21. data/ext/nmatrix/storage/list/list.cpp +48 -47
  22. data/ext/nmatrix/util/io.cpp +2 -2
  23. data/lib/nmatrix.rb +0 -1
  24. data/lib/nmatrix/enumerate.rb +1 -1
  25. data/lib/nmatrix/io/market.rb +1 -1
  26. data/lib/nmatrix/io/mat_reader.rb +41 -41
  27. data/lib/nmatrix/lapack.rb +0 -1
  28. data/lib/nmatrix/math.rb +43 -0
  29. data/lib/nmatrix/nmatrix.rb +5 -1
  30. data/lib/nmatrix/version.rb +1 -1
  31. data/nmatrix.gemspec +3 -4
  32. data/spec/00_nmatrix_spec.rb +13 -6
  33. data/spec/01_enum_spec.rb +17 -25
  34. data/spec/02_slice_spec.rb +74 -82
  35. data/spec/blas_spec.rb +21 -6
  36. data/spec/elementwise_spec.rb +1 -6
  37. data/spec/io_spec.rb +15 -22
  38. data/spec/lapack_spec.rb +1 -6
  39. data/spec/leakcheck.rb +1 -1
  40. data/spec/math_spec.rb +43 -4
  41. data/spec/nmatrix_yale_spec.rb +1 -4
  42. data/spec/rspec_spec.rb +1 -1
  43. data/spec/shortcuts_spec.rb +1 -6
  44. data/spec/slice_set_spec.rb +1 -5
  45. data/spec/stat_spec.rb +46 -51
  46. metadata +32 -22
  47. data/Guardfile +0 -6
  48. data/ext/nmatrix/math/idamax.h +0 -86
  49. data/lib/nmatrix/nvector.rb +0 -184
@@ -352,6 +352,10 @@ class NMatrix
352
352
  #
353
353
  # See @row (dimension = 0), @column (dimension = 1)
354
354
  def rank(shape_idx, rank_idx, meth = :copy)
355
+
356
+ if shape_idx > (self.dim-1)
357
+ raise(RangeError, "#rank call was out of bounds")
358
+ end
355
359
 
356
360
  params = Array.new(self.dim)
357
361
  params.each.with_index do |v,d|
@@ -562,7 +566,7 @@ class NMatrix
562
566
  new_cap = matrices.inject(self.capacity - self.shape[0]) do |total,m|
563
567
  total + m.capacity - m.shape[0]
564
568
  end - self.shape[0] + new_shape[0]
565
- opts = {capacity: self.new_cap}.merge(opts)
569
+ opts = {capacity: new_cap}.merge(opts)
566
570
  end
567
571
 
568
572
  # Do the actual construction.
@@ -31,7 +31,7 @@ class NMatrix
31
31
  MAJOR = 0
32
32
  MINOR = 1
33
33
  TINY = 0
34
- PRE = "rc3"
34
+ PRE = "rc4"
35
35
 
36
36
  STRING = [MAJOR, MINOR, TINY, PRE].compact.join(".")
37
37
  end
data/nmatrix.gemspec CHANGED
@@ -22,7 +22,7 @@ which improve the portability of this project.
22
22
 
23
23
  Also required is ATLAS. Most Linux distributions and Mac
24
24
  versions include ATLAS, but you may wish to compile it
25
- yourself. The Ubuntu/Debian apt package for ATLAS WILL
25
+ yourself. The Ubuntu/Debian apt package for ATLAS WILL
26
26
  NOT WORK with NMatrix if LAPACK is also installed.
27
27
 
28
28
  More explicit instructions for NMatrix and SciRuby should
@@ -44,11 +44,10 @@ EOF
44
44
  gem.required_ruby_version = '>= 1.9'
45
45
 
46
46
  gem.add_dependency 'rdoc', '>=4.0.1'
47
- #gem.add_dependency 'yard'
48
47
  gem.add_development_dependency 'rake'
49
48
  gem.add_development_dependency 'bundler'
50
- gem.add_development_dependency 'rspec'
51
- gem.add_development_dependency 'rspec-longrun'
49
+ gem.add_development_dependency 'rspec', '~>2.14.1'
50
+ gem.add_development_dependency 'rspec-longrun', '~>1.0.1'
52
51
  gem.add_development_dependency 'pry'
53
52
  gem.add_development_dependency 'rake-compiler', '~>0.8.1'
54
53
  end
@@ -25,14 +25,9 @@
25
25
  # Basic tests for NMatrix. These should load first, as they're
26
26
  # essential to NMatrix operation.
27
27
  #
28
-
29
- require File.dirname(__FILE__) + "/spec_helper.rb"
28
+ require 'spec_helper'
30
29
 
31
30
  describe NMatrix do
32
- #after :each do
33
- # GC.start
34
- #end
35
-
36
31
  it "creates a matrix with the new constructor" do
37
32
  n = NMatrix.new([2,2], [0,1,2,3], dtype: :int64)
38
33
  expect(n.shape).to eq([2,2])
@@ -396,6 +391,18 @@ describe 'NMatrix' do
396
391
  end
397
392
  end
398
393
 
394
+ context "#rank" do
395
+ it "should get the rank of a 2-dimensional matrix" do
396
+ n = NMatrix.seq([2,3])
397
+ expect(n.rank(0, 0)).to eq(N[[0,1,2]])
398
+ end
399
+
400
+ it "should raise an error when the rank is out of bounds" do
401
+ n = NMatrix.seq([2,3])
402
+ expect { n.rank(2, 0) }.to raise_error(RangeError)
403
+ end
404
+ end
405
+
399
406
  context "#reshape" do
400
407
  it "should change the shape of a matrix without the contents changing" do
401
408
  n = NMatrix.seq(4)+1
data/spec/01_enum_spec.rb CHANGED
@@ -25,29 +25,21 @@
25
25
  # Enumerator tests for NMatrix. These should load early, as they
26
26
  # test functionality essential to matrix printing.
27
27
  #
28
-
29
- require File.dirname(__FILE__) + "/spec_helper.rb"
28
+ require 'spec_helper'
30
29
 
31
30
  describe "NMatrix enumeration for" do
32
31
  [:dense, :yale, :list].each do |stype|
33
32
  context stype do
34
-
35
- before :each do
36
- @n = create_rectangular_matrix(stype)
37
- @m = @n[1..4,1..3]
38
- end
39
-
40
- #after :each do
41
- # GC.start
42
- #end
33
+ let(:n) { create_rectangular_matrix(stype) }
34
+ let(:m) { n[1..4,1..3] }
43
35
 
44
36
  if stype == :yale
45
37
  it "should iterate properly along each row of a slice" do
46
38
  vv = []
47
39
  ii = []
48
40
  jj = []
49
- @m.extend NMatrix::YaleFunctions
50
- @m.each_row do |row|
41
+ m.extend NMatrix::YaleFunctions
42
+ m.each_row do |row|
51
43
  row.each_with_indices do |v,i,j|
52
44
  vv << v
53
45
  ii << i
@@ -64,7 +56,7 @@ describe "NMatrix enumeration for" do
64
56
  vv = []
65
57
  ii = []
66
58
  jj = []
67
- @n.send :__yale_stored_diagonal_each_with_indices__ do |v,i,j|
59
+ n.send :__yale_stored_diagonal_each_with_indices__ do |v,i,j|
68
60
  vv << v
69
61
  ii << i
70
62
  jj << j
@@ -78,7 +70,7 @@ describe "NMatrix enumeration for" do
78
70
  vv = []
79
71
  ii = []
80
72
  jj = []
81
- @n.send :__yale_stored_nondiagonal_each_with_indices__ do |v,i,j|
73
+ n.send :__yale_stored_nondiagonal_each_with_indices__ do |v,i,j|
82
74
  vv << v
83
75
  ii << i
84
76
  jj << j
@@ -90,11 +82,11 @@ describe "NMatrix enumeration for" do
90
82
  end
91
83
 
92
84
  it "should iterate along a sliced diagonal portion of an A array" do
93
- @m = @n[0..3,1..3]
85
+ m = n[0..3,1..3]
94
86
  vv = []
95
87
  ii = []
96
88
  jj = []
97
- @m.send :__yale_stored_diagonal_each_with_indices__ do |v,i,j|
89
+ m.send :__yale_stored_diagonal_each_with_indices__ do |v,i,j|
98
90
  vv << v
99
91
  ii << i
100
92
  jj << j
@@ -108,9 +100,9 @@ describe "NMatrix enumeration for" do
108
100
  vv = []
109
101
  ii = []
110
102
  jj = []
111
- @n.extend NMatrix::YaleFunctions
112
- @m.extend NMatrix::YaleFunctions
113
- @m.send :__yale_stored_nondiagonal_each_with_indices__ do |v,i,j|
103
+ n.extend NMatrix::YaleFunctions
104
+ m.extend NMatrix::YaleFunctions
105
+ m.send :__yale_stored_nondiagonal_each_with_indices__ do |v,i,j|
114
106
  vv << v
115
107
  ii << i
116
108
  jj << j
@@ -125,7 +117,7 @@ describe "NMatrix enumeration for" do
125
117
  vv = []
126
118
  ii = []
127
119
  jj = []
128
- @n.each_ordered_stored_with_indices do |v,i,j|
120
+ n.each_ordered_stored_with_indices do |v,i,j|
129
121
  vv << v
130
122
  ii << i
131
123
  jj << j
@@ -141,7 +133,7 @@ describe "NMatrix enumeration for" do
141
133
  vv = []
142
134
  ii = []
143
135
  jj = []
144
- @m.each_ordered_stored_with_indices do |v,i,j|
136
+ m.each_ordered_stored_with_indices do |v,i,j|
145
137
  vv << v
146
138
  ii << i
147
139
  jj << j
@@ -156,7 +148,7 @@ describe "NMatrix enumeration for" do
156
148
  vv = []
157
149
  ii = []
158
150
  jj = []
159
- @n.each_with_indices do |v,i,j|
151
+ n.each_with_indices do |v,i,j|
160
152
  vv << v
161
153
  ii << i
162
154
  jj << j
@@ -171,7 +163,7 @@ describe "NMatrix enumeration for" do
171
163
  vv = []
172
164
  ii = []
173
165
  jj = []
174
- @m.each_with_indices do |v,i,j|
166
+ m.each_with_indices do |v,i,j|
175
167
  vv << v
176
168
  ii << i
177
169
  jj << j
@@ -183,7 +175,7 @@ describe "NMatrix enumeration for" do
183
175
  end
184
176
 
185
177
  if stype == :list or stype == :dense then
186
- it "should correctly map to a matrix with a single element" do
178
+ it "should correctly map to a matrix with a single element" do
187
179
  nm = N.new([1], [2.0], stype: stype)
188
180
  expect(nm.map { |e| e**2 }).to eq N.new([1], [4.0], stype: stype)
189
181
  end
@@ -25,28 +25,21 @@
25
25
  # Test of slice operations. High priority tests since reference
26
26
  # slicing is needed for pretty_print.
27
27
  #
28
- require 'pry'
29
- require File.dirname(__FILE__) + "/spec_helper.rb"
28
+ require 'spec_helper'
30
29
 
31
30
  describe "Slice operation" do
32
31
  include RSpec::Longrun::DSL
33
32
 
34
33
  [:dense, :list, :yale].each do |stype|
35
34
  context "for #{stype}" do
36
- before :each do
37
35
  #GC.start # don't have to do this, but it helps to make sure we've cleaned up our pointers properly.
38
- @m = create_matrix(stype)
39
- end
40
-
41
- #after :each do
42
- # GC.start
43
- #end
36
+ let(:stype_matrix) { create_matrix(stype) }
44
37
 
45
38
  it "should correctly return a row of a reference-slice" do
46
- @n = create_rectangular_matrix(stype)
47
- @m = @n[1..4,1..3]
48
- expect(@m.row(1, :copy)).to eq(@m.row(1, :reference))
49
- expect(@m.row(1, :copy).to_flat_array).to eq([12,13,0])
39
+ n = create_rectangular_matrix(stype)
40
+ stype_matrix = n[1..4,1..3]
41
+ expect(stype_matrix.row(1, :copy)).to eq(stype_matrix.row(1, :reference))
42
+ expect(stype_matrix.row(1, :copy).to_flat_array).to eq([12,13,0])
50
43
  end
51
44
 
52
45
  if stype == :yale
@@ -80,7 +73,7 @@ describe "Slice operation" do
80
73
  js = []
81
74
 
82
75
  STDERR.puts("now") if stype == :yale
83
- @m[2,1..2].each_stored_with_indices do |v,i,j|
76
+ stype_matrix[2,1..2].each_stored_with_indices do |v,i,j|
84
77
  vs << v
85
78
  is << i
86
79
  js << j
@@ -98,7 +91,7 @@ describe "Slice operation" do
98
91
  vs = []
99
92
  is = []
100
93
  js = []
101
- @m[2,0..2].each_stored_with_indices do |v,i,j|
94
+ stype_matrix[2,0..2].each_stored_with_indices do |v,i,j|
102
95
  vs << v
103
96
  is << i
104
97
  js << j
@@ -112,7 +105,7 @@ describe "Slice operation" do
112
105
  vs = []
113
106
  is = []
114
107
  js = []
115
- @m[0..1,1..2].each_stored_with_indices do |v,i,j|
108
+ stype_matrix[0..1,1..2].each_stored_with_indices do |v,i,j|
116
109
  vs << v
117
110
  is << i
118
111
  js << j
@@ -132,77 +125,77 @@ describe "Slice operation" do
132
125
  end
133
126
 
134
127
  it "should have #is_ref? method" do
135
- a = @m[0..1, 0..1]
136
- b = @m.slice(0..1, 0..1)
137
- expect(@m.is_ref?).to be_false
128
+ a = stype_matrix[0..1, 0..1]
129
+ b = stype_matrix.slice(0..1, 0..1)
130
+ expect(stype_matrix.is_ref?).to be_false
138
131
  expect(a.is_ref?).to be_true
139
132
  expect(b.is_ref?).to be_false
140
133
  end
141
134
 
142
135
  it "reference should compare with non-reference" do
143
- expect(@m.slice(1..2,0..1)).to eq(@m[1..2, 0..1])
144
- expect(@m[1..2,0..1]).to eq(@m.slice(1..2, 0..1))
145
- expect(@m[1..2,0..1]).to eq(@m[1..2, 0..1])
136
+ expect(stype_matrix.slice(1..2,0..1)).to eq(stype_matrix[1..2, 0..1])
137
+ expect(stype_matrix[1..2,0..1]).to eq(stype_matrix.slice(1..2, 0..1))
138
+ expect(stype_matrix[1..2,0..1]).to eq(stype_matrix[1..2, 0..1])
146
139
  end
147
140
 
148
141
  context "with copying" do
149
142
  it 'should return an NMatrix' do
150
- n = @m.slice(0..1,0..1)
143
+ n = stype_matrix.slice(0..1,0..1)
151
144
  expect(nm_eql(n, NMatrix.new([2,2], [0,1,3,4], dtype: :int32))).to be_true
152
145
  end
153
146
 
154
147
  it 'should return a copy of 2x2 matrix to self elements' do
155
- n = @m.slice(1..2,0..1)
148
+ n = stype_matrix.slice(1..2,0..1)
156
149
  expect(n.shape).to eql([2,2])
157
150
 
158
- expect(n[1,1]).to eq(@m[2,1])
151
+ expect(n[1,1]).to eq(stype_matrix[2,1])
159
152
  n[1,1] = -9
160
- expect(@m[2,1]).to eql(7)
153
+ expect(stype_matrix[2,1]).to eql(7)
161
154
  end
162
155
 
163
156
  it 'should return a 1x2 matrix without refs to self elements' do
164
- n = @m.slice(0,1..2)
157
+ n = stype_matrix.slice(0,1..2)
165
158
  expect(n.shape).to eql([1,2])
166
159
 
167
- expect(n[0]).to eq(@m[0,1])
168
- expect(n[1]).to eq(@m[0,2])
160
+ expect(n[0]).to eq(stype_matrix[0,1])
161
+ expect(n[1]).to eq(stype_matrix[0,2])
169
162
  n[0] = -9
170
- expect(@m[0,1]).to eql(1)
171
- expect(@m[0,2]).to eql(2)
163
+ expect(stype_matrix[0,1]).to eql(1)
164
+ expect(stype_matrix[0,2]).to eql(2)
172
165
  end
173
166
 
174
167
  it 'should return a 2x1 matrix without refs to self elements' do
175
- @m.extend NMatrix::YaleFunctions
168
+ stype_matrix.extend NMatrix::YaleFunctions
176
169
 
177
- n = @m.slice(0..1,1)
170
+ n = stype_matrix.slice(0..1,1)
178
171
  expect(n.shape).to eql([2,1])
179
172
 
180
- expect(n[0]).to eq(@m[0,1])
181
- expect(n[1]).to eq(@m[1,1])
173
+ expect(n[0]).to eq(stype_matrix[0,1])
174
+ expect(n[1]).to eq(stype_matrix[1,1])
182
175
  n[0] = -9
183
- expect(@m[0,1]).to eql(1)
184
- expect(@m[1,1]).to eql(4)
176
+ expect(stype_matrix[0,1]).to eql(1)
177
+ expect(stype_matrix[1,1]).to eql(4)
185
178
  end
186
179
 
187
180
  it 'should be correct slice for range 0..2 and 0...3' do
188
- expect(@m.slice(0..2,0..2)).to eq(@m.slice(0...3,0...3))
181
+ expect(stype_matrix.slice(0..2,0..2)).to eq(stype_matrix.slice(0...3,0...3))
189
182
  end
190
183
 
191
184
  [:dense, :list, :yale].each do |cast_type|
192
185
  it "should cast copied slice from #{stype.upcase} to #{cast_type.upcase}" do
193
- expect(nm_eql(@m.slice(1..2, 1..2).cast(cast_type, :int32), @m.slice(1..2,1..2))).to be_true
194
- expect(nm_eql(@m.slice(0..1, 1..2).cast(cast_type, :int32), @m.slice(0..1,1..2))).to be_true
195
- expect(nm_eql(@m.slice(1..2, 0..1).cast(cast_type, :int32), @m.slice(1..2,0..1))).to be_true
196
- expect(nm_eql(@m.slice(0..1, 0..1).cast(cast_type, :int32), @m.slice(0..1,0..1))).to be_true
186
+ expect(nm_eql(stype_matrix.slice(1..2, 1..2).cast(cast_type, :int32), stype_matrix.slice(1..2,1..2))).to be_true
187
+ expect(nm_eql(stype_matrix.slice(0..1, 1..2).cast(cast_type, :int32), stype_matrix.slice(0..1,1..2))).to be_true
188
+ expect(nm_eql(stype_matrix.slice(1..2, 0..1).cast(cast_type, :int32), stype_matrix.slice(1..2,0..1))).to be_true
189
+ expect(nm_eql(stype_matrix.slice(0..1, 0..1).cast(cast_type, :int32), stype_matrix.slice(0..1,0..1))).to be_true
197
190
 
198
191
  # Non square
199
- expect(nm_eql(@m.slice(0..2, 1..2).cast(cast_type, :int32), @m.slice(0..2,1..2))).to be_true
192
+ expect(nm_eql(stype_matrix.slice(0..2, 1..2).cast(cast_type, :int32), stype_matrix.slice(0..2,1..2))).to be_true
200
193
  #require 'pry'
201
194
  #binding.pry if cast_type == :yale
202
- expect(nm_eql(@m.slice(1..2, 0..2).cast(cast_type, :int32), @m.slice(1..2,0..2))).to be_true
195
+ expect(nm_eql(stype_matrix.slice(1..2, 0..2).cast(cast_type, :int32), stype_matrix.slice(1..2,0..2))).to be_true
203
196
 
204
197
  # Full
205
- expect(nm_eql(@m.slice(0..2, 0..2).cast(cast_type, :int32), @m)).to be_true
198
+ expect(nm_eql(stype_matrix.slice(0..2, 0..2).cast(cast_type, :int32), stype_matrix)).to be_true
206
199
  end
207
200
  end
208
201
  end
@@ -210,8 +203,8 @@ describe "Slice operation" do
210
203
  # Yale:
211
204
  #context "by copy" do
212
205
  #it "should correctly preserve zeros" do
213
- # @m = NMatrix.new(:yale, 3, :int64)
214
- # column_slice = @m.column(2, :copy)
206
+ # stype_matrix = NMatrix.new(:yale, 3, :int64)
207
+ # column_slice = stype_matrix.column(2, :copy)
215
208
  # column_slice[0].should == 0
216
209
  # column_slice[1].should == 0
217
210
  # column_slice[2].should == 0
@@ -220,48 +213,48 @@ describe "Slice operation" do
220
213
 
221
214
  context "by reference" do
222
215
  it 'should return an NMatrix' do
223
- n = @m[0..1,0..1]
216
+ n = stype_matrix[0..1,0..1]
224
217
  expect(nm_eql(n, NMatrix.new([2,2], [0,1,3,4], dtype: :int32))).to be_true
225
218
  end
226
219
 
227
220
  it 'should return a 2x2 matrix with refs to self elements' do
228
- n = @m[1..2,0..1]
221
+ n = stype_matrix[1..2,0..1]
229
222
  expect(n.shape).to eql([2,2])
230
223
 
231
- expect(n[0,0]).to eq(@m[1,0])
224
+ expect(n[0,0]).to eq(stype_matrix[1,0])
232
225
  n[0,0] = -9
233
- expect(@m[1,0]).to eql(-9)
226
+ expect(stype_matrix[1,0]).to eql(-9)
234
227
  end
235
228
 
236
229
  it 'should return a 1x2 vector with refs to self elements' do
237
- n = @m[0,1..2]
230
+ n = stype_matrix[0,1..2]
238
231
  expect(n.shape).to eql([1,2])
239
232
 
240
- expect(n[0]).to eq(@m[0,1])
233
+ expect(n[0]).to eq(stype_matrix[0,1])
241
234
  n[0] = -9
242
- expect(@m[0,1]).to eql(-9)
235
+ expect(stype_matrix[0,1]).to eql(-9)
243
236
  end
244
237
 
245
238
  it 'should return a 2x1 vector with refs to self elements' do
246
- n = @m[0..1,1]
239
+ n = stype_matrix[0..1,1]
247
240
  expect(n.shape).to eql([2,1])
248
241
 
249
- expect(n[0]).to eq(@m[0,1])
242
+ expect(n[0]).to eq(stype_matrix[0,1])
250
243
  n[0] = -9
251
- expect(@m[0,1]).to eql(-9)
244
+ expect(stype_matrix[0,1]).to eql(-9)
252
245
  end
253
246
 
254
247
  it 'should slice again' do
255
- n = @m[1..2, 1..2]
248
+ n = stype_matrix[1..2, 1..2]
256
249
  expect(nm_eql(n[1,0..1], NVector.new(2, [7,8], dtype: :int32).transpose)).to be_true
257
250
  end
258
251
 
259
252
  it 'should be correct slice for range 0..2 and 0...3' do
260
- expect(@m[0..2,0..2]).to eq(@m[0...3,0...3])
253
+ expect(stype_matrix[0..2,0..2]).to eq(stype_matrix[0...3,0...3])
261
254
  end
262
255
 
263
256
  it 'should correctly handle :* slice notation' do
264
- expect(@m[:*,0]).to eq @m[0...@m.shape[0], 0]
257
+ expect(stype_matrix[:*,0]).to eq stype_matrix[0...stype_matrix.shape[0], 0]
265
258
  end
266
259
 
267
260
  if stype == :dense
@@ -313,30 +306,30 @@ describe "Slice operation" do
313
306
  context "operations" do
314
307
 
315
308
  it "correctly transposes slices" do
316
- expect(@m[0...3,0].transpose).to eq NMatrix[[0, 3, 6]]
317
- expect(@m[0...3,1].transpose).to eq NMatrix[[1, 4, 7]]
318
- expect(@m[0...3,2].transpose).to eq NMatrix[[2, 5, 8]]
319
- expect(@m[0,0...3].transpose).to eq NMatrix[[0], [1], [2]]
320
- expect(@m[1,0...3].transpose).to eq NMatrix[[3], [4], [5]]
321
- expect(@m[2,0...3].transpose).to eq NMatrix[[6], [7], [8]]
322
- expect(@m[1..2,1..2].transpose).to eq NMatrix[[4, 7], [5, 8]]
309
+ expect(stype_matrix[0...3,0].transpose).to eq NMatrix[[0, 3, 6]]
310
+ expect(stype_matrix[0...3,1].transpose).to eq NMatrix[[1, 4, 7]]
311
+ expect(stype_matrix[0...3,2].transpose).to eq NMatrix[[2, 5, 8]]
312
+ expect(stype_matrix[0,0...3].transpose).to eq NMatrix[[0], [1], [2]]
313
+ expect(stype_matrix[1,0...3].transpose).to eq NMatrix[[3], [4], [5]]
314
+ expect(stype_matrix[2,0...3].transpose).to eq NMatrix[[6], [7], [8]]
315
+ expect(stype_matrix[1..2,1..2].transpose).to eq NMatrix[[4, 7], [5, 8]]
323
316
  end
324
317
 
325
318
  it "adds slices" do
326
- expect(NMatrix[[0,0,0]] + @m[1,0..2]).to eq NMatrix[[3, 4, 5]]
319
+ expect(NMatrix[[0,0,0]] + stype_matrix[1,0..2]).to eq NMatrix[[3, 4, 5]]
327
320
  end
328
321
 
329
322
  it "scalar adds to slices" do
330
- expect(@m[1,0..2]+1).to eq NMatrix[[4, 5, 6]]
323
+ expect(stype_matrix[1,0..2]+1).to eq NMatrix[[4, 5, 6]]
331
324
  end
332
325
 
333
326
  it "compares slices to scalars" do
334
- (@m[1, 0..2] > 2).each { |e| expect(e != 0).to be_true }
327
+ (stype_matrix[1, 0..2] > 2).each { |e| expect(e != 0).to be_true }
335
328
  end
336
329
 
337
330
  it "iterates only over elements in the slice" do
338
331
  els = []
339
- @m[1, 0..2].each { |e| els << e }
332
+ stype_matrix[1, 0..2].each { |e| els << e }
340
333
  expect(els.size).to eq 3
341
334
  expect(els[0]).to eq 3
342
335
  expect(els[1]).to eq 4
@@ -345,7 +338,7 @@ describe "Slice operation" do
345
338
 
346
339
  it "iterates with index only over elements in the slice" do
347
340
  els = []
348
- @m[1, 0..2].each_stored_with_indices { |a| els << a }
341
+ stype_matrix[1, 0..2].each_stored_with_indices { |a| els << a }
349
342
  expect(els.size).to eq 3
350
343
  expect(els[0]).to eq [3, 0, 0]
351
344
  expect(els[1]).to eq [4, 0, 1]
@@ -359,13 +352,13 @@ describe "Slice operation" do
359
352
  example 'should be cleaned up by garbage collector without errors' do
360
353
  step "reference slice" do
361
354
  1.times do
362
- n = @m[1..2,0..1]
355
+ n = stype_matrix[1..2,0..1]
363
356
  end
364
357
  GC.start
365
358
  end
366
359
 
367
360
  step "reference slice of casted-copy" do
368
- expect(@m).to eq(NMatrix.new([3,3], (0..9).to_a, dtype: :int32).cast(stype, :int32))
361
+ expect(stype_matrix).to eq(NMatrix.new([3,3], (0..9).to_a, dtype: :int32).cast(stype, :int32))
369
362
  n = nil
370
363
  1.times do
371
364
  m = NMatrix.new([2,2], [1,2,3,4]).cast(stype, :int32)
@@ -378,24 +371,23 @@ describe "Slice operation" do
378
371
 
379
372
  [:dense, :list, :yale].each do |cast_type|
380
373
  it "should cast a square reference-slice from #{stype.upcase} to #{cast_type.upcase}" do
381
- expect(nm_eql(@m[1..2, 1..2].cast(cast_type), @m[1..2,1..2])).to be_true
382
- expect(nm_eql(@m[0..1, 1..2].cast(cast_type), @m[0..1,1..2])).to be_true
383
- expect(nm_eql(@m[1..2, 0..1].cast(cast_type), @m[1..2,0..1])).to be_true
384
- expect(nm_eql(@m[0..1, 0..1].cast(cast_type), @m[0..1,0..1])).to be_true
374
+ expect(nm_eql(stype_matrix[1..2, 1..2].cast(cast_type), stype_matrix[1..2,1..2])).to be_true
375
+ expect(nm_eql(stype_matrix[0..1, 1..2].cast(cast_type), stype_matrix[0..1,1..2])).to be_true
376
+ expect(nm_eql(stype_matrix[1..2, 0..1].cast(cast_type), stype_matrix[1..2,0..1])).to be_true
377
+ expect(nm_eql(stype_matrix[0..1, 0..1].cast(cast_type), stype_matrix[0..1,0..1])).to be_true
385
378
  end
386
379
 
387
380
  it "should cast a rectangular reference-slice from #{stype.upcase} to #{cast_type.upcase}" do
388
381
  # Non square
389
- expect(nm_eql(@m[0..2, 1..2].cast(cast_type), @m[0..2,1..2])).to be_true # FIXME: memory problem.
390
- expect(nm_eql(@m[1..2, 0..2].cast(cast_type), @m[1..2,0..2])).to be_true # this one is fine
382
+ expect(nm_eql(stype_matrix[0..2, 1..2].cast(cast_type), stype_matrix[0..2,1..2])).to be_true # FIXME: memory problem.
383
+ expect(nm_eql(stype_matrix[1..2, 0..2].cast(cast_type), stype_matrix[1..2,0..2])).to be_true # this one is fine
391
384
  end
392
385
 
393
386
  it "should cast a square full-matrix reference-slice from #{stype.upcase} to #{cast_type.upcase}" do
394
- expect(nm_eql(@m[0..2, 0..2].cast(cast_type), @m)).to be_true
387
+ expect(nm_eql(stype_matrix[0..2, 0..2].cast(cast_type), stype_matrix)).to be_true
395
388
  end
396
389
  end
397
390
  end
398
-
399
391
  end
400
392
  end
401
393
  end