nmatrix 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -70,6 +70,28 @@ describe NMatrix do
70
70
  n.yale_d.should == [0.2, 0.1]
71
71
  end
72
72
 
73
+ it "gets non-diagonal rows as hashes" do
74
+ n = NMatrix.new(:yale, [4,6], :float64)
75
+ n.extend(NMatrix::YaleFunctions)
76
+ n[0,0] = 0.1
77
+ n[0,2] = 0.2
78
+ n[0,3] = 0.3
79
+ n[1,5] = 0.4
80
+ h = n.yale_nd_row(0, :hash)
81
+ h.should == {2 => 0.2, 3 => 0.3}
82
+ end
83
+
84
+ it "gets non-diagonal occupied column indices for a given row" do
85
+ n = NMatrix.new(:yale, [4,6], :float64)
86
+ n.extend(NMatrix::YaleFunctions)
87
+ n[0,0] = 0.1
88
+ n[0,2] = 0.2
89
+ n[0,3] = 0.3
90
+ n[1,5] = 0.4
91
+ a = n.yale_nd_row(0, :array)
92
+ a.should == [2,3]
93
+ end
94
+
73
95
  it "does not resize until necessary" do
74
96
  n = NMatrix.new(:yale, [2,3], :float64)
75
97
  n.extend(NMatrix::YaleFunctions)
@@ -246,6 +268,39 @@ describe NMatrix do
246
268
 
247
269
  end
248
270
 
271
+ it "dots two vectors" do
272
+ n = NVector.new(:yale, [16,1], :int64)
273
+ m = NVector.new(:yale, [1,16], :int64)
274
+
275
+ n[0] = m[0] = 1
276
+ n[1] = m[1] = 2
277
+ n[2] = m[2] = 3
278
+ n[3] = m[3] = 4
279
+ n[4] = m[4] = 5
280
+ n[5] = m[5] = 6
281
+ n[6] = m[6] = 7
282
+ n[7] = m[7] = 8
283
+ n[8] = m[8] = 9
284
+ n[15] = m[15] = 16
285
+
286
+ nm = n.dot(m)
287
+
288
+ # Perform the same multiplication with dense
289
+ nmr = n.cast(:dense, :int64).dot(m.cast(:dense, :int64)).cast(:yale, :int64)
290
+
291
+ nm.extend(NMatrix::YaleFunctions)
292
+ nmr.extend(NMatrix::YaleFunctions)
293
+
294
+ # We want to do a structure comparison to ensure multiplication is occurring properly, but more importantly, to
295
+ # ensure that insertion sort is occurring as it should. If the row has more than four entries, it'll run quicksort
296
+ # instead. Quicksort calls insertion sort for small rows, so we test both with this particular multiplication.
297
+ nm.yale_ija[0...107].should == nmr.yale_ija[0...107]
298
+ nm.yale_a[0...107].should == nmr.yale_a[0...107]
299
+
300
+ mn = m.dot(n)
301
+ mn[0,0].should == 541
302
+ end
303
+
249
304
  it "transposes" do
250
305
  a = NMatrix.new(:yale, 4, :float64)
251
306
  a[0,0] = 1.0
data/spec/nvector_spec.rb CHANGED
@@ -25,13 +25,13 @@
25
25
  # Basic tests for NVector.
26
26
  #
27
27
 
28
- require "./lib/nmatrix"
28
+ require File.dirname(__FILE__) + "/spec_helper.rb"
29
29
 
30
30
  describe NVector do
31
31
  it "initializes" do
32
32
  v = NVector.new(5, 0, :float64)
33
- v.shape[0].should == 5
34
- v.shape[1].should == 1
33
+ v.shape[0].should == 1
34
+ v.shape[1].should == 5
35
35
  end
36
36
 
37
37
  it "permits setting and getting contents" do
@@ -43,8 +43,8 @@ describe NVector do
43
43
  it "transpose() changes raw and column stored structure" do
44
44
  v = NVector.new 5, :float64
45
45
  v = v.transpose
46
- v.shape[0].should == 1
47
- v.shape[1].should == 5
46
+ v.shape[0].should == 5
47
+ v.shape[1].should == 1
48
48
  v[0] = 1.555
49
49
  v[0].should == 1.555
50
50
  end
@@ -52,28 +52,28 @@ describe NVector do
52
52
  it "transpose!() changes destructively its raw and column stored structure" do
53
53
  v = NVector.new 5, :float64
54
54
  v.transpose!
55
- v.shape[0].should == 1
56
- v.shape[1].should == 5
55
+ v.shape[0].should == 5
56
+ v.shape[1].should == 1
57
57
  end
58
58
 
59
- it "multiply() multiples itself by another NVector" do
60
- v1 = NVector.new 2, :float64
61
- v2 = NVector.new 2, :float64
59
+ it "dot() multiples itself by another NVector" do
60
+ v1 = NVector.new(2, :float64)
61
+ v2 = NVector.new(2, :float64).transpose
62
62
  v1[0] = 1.5
63
63
  v1[1] = 2.3
64
64
  v2[0] = 1.3
65
65
  v2[1] = 2.5
66
- v1.multiply(v2).should == 12.0
66
+ v1.dot(v2).should == 12.0
67
67
  end
68
68
 
69
- it "multiply!() multiples destructively itself by another NVector" do
69
+ it "dot!() multiples itself destructively by another NVector" do
70
70
  v1 = NVector.new 2, :float64
71
- v2 = NVector.new 2, :float64
71
+ v2 = NVector.new(2, :float64).transpose
72
72
  v1[0] = 1.5
73
73
  v1[1] = 2.3
74
74
  v2[0] = 1.3
75
75
  v2[1] = 2.5
76
- v1.multiply!(v2)
76
+ v1.dot!(v2)
77
77
  v1.should == 12.0
78
78
  end
79
79
 
@@ -96,4 +96,23 @@ describe NVector do
96
96
  $stdout = STDOUT
97
97
  out.should == "0 0 0 0 0\n"
98
98
  end
99
+
100
+ [:dense, :list, :yale].each do |storage_type|
101
+ context "for #{storage_type}" do
102
+ before :each do
103
+ @m = create_vector(storage_type)
104
+ end
105
+
106
+ it "converts to an Array" do
107
+ a = @m.to_a
108
+ a.each.with_index { |v,idx| @m[idx].should equal(v) }
109
+ end
110
+
111
+ it "shuffles" do
112
+ n = @m.shuffle
113
+ n.to_a.hash.should_not == @m.to_a.hash
114
+ n.to_a.sort.hash.should equal(@m.to_a.sort.hash)
115
+ end
116
+ end
117
+ end
99
118
  end
data/spec/slice_spec.rb CHANGED
@@ -66,12 +66,12 @@ describe "Slice operation" do
66
66
  @m[2,1].should eql(7)
67
67
  end
68
68
 
69
- it 'should return a 1x2 matrix with refs to self elements' do
69
+ it 'should return a 1x2 vector with refs to self elements' do
70
70
  n = @m.slice(0,1..2)
71
71
  n.shape.should eql([1,2])
72
72
 
73
- n[0,0].should == @m[0,1]
74
- n[0,0] = -9
73
+ n[0].should == @m[0,1]
74
+ n[0] = -9
75
75
  @m[0,1].should eql(1)
76
76
  end
77
77
 
@@ -79,8 +79,8 @@ describe "Slice operation" do
79
79
  n = @m.slice(0..1,1)
80
80
  n.shape.should eql([2,1])
81
81
 
82
- n[0,0].should == @m[0,1]
83
- n[0,0] = -9
82
+ n[0].should == @m[0,1]
83
+ n[0] = -9
84
84
  @m[0,1].should eql(1)
85
85
  end
86
86
 
@@ -118,9 +118,9 @@ describe "Slice operation" do
118
118
  it "should correctly preserve zeros" do
119
119
  @m = NMatrix.new(:yale, 3, :int64)
120
120
  column_slice = @m.column(2, :copy)
121
- column_slice[0,0].should == 0
122
- column_slice[1,0].should == 0
123
- column_slice[2,0].should == 0
121
+ column_slice[0].should == 0
122
+ column_slice[1].should == 0
123
+ column_slice[2].should == 0
124
124
  end
125
125
  end
126
126
  else
@@ -139,21 +139,21 @@ describe "Slice operation" do
139
139
  @m[1,0].should eql(-9)
140
140
  end
141
141
 
142
- it 'should return a 1x2 matrix with refs to self elements' do
142
+ it 'should return a 1x2 vector with refs to self elements' do
143
143
  n = @m[0,1..2]
144
144
  n.shape.should eql([1,2])
145
145
 
146
- n[0,0].should == @m[0,1]
147
- n[0,0] = -9
146
+ n[0].should == @m[0,1]
147
+ n[0] = -9
148
148
  @m[0,1].should eql(-9)
149
149
  end
150
150
 
151
- it 'should return a 2x1 matrix with refs to self elements' do
151
+ it 'should return a 2x1 vector with refs to self elements' do
152
152
  n = @m[0..1,1]
153
153
  n.shape.should eql([2,1])
154
154
 
155
- n[0,0].should == @m[0,1]
156
- n[0,0] = -9
155
+ n[0].should == @m[0,1]
156
+ n[0] = -9
157
157
  @m[0,1].should eql(-9)
158
158
  end
159
159
 
@@ -161,7 +161,7 @@ describe "Slice operation" do
161
161
 
162
162
  it 'should slice again' do
163
163
  n = @m[1..2, 1..2]
164
- nm_eql(n[1,0..1], NMatrix.new([1,2], [7,8])).should be_true
164
+ nm_eql(n[1,0..1], NVector.new(2, [7,8], :int32).transpose).should be_true
165
165
  end
166
166
 
167
167
  it 'should be correct slice for range 0..2 and 0...3' do
@@ -293,7 +293,16 @@ describe "Slice operation" do
293
293
  def nm_eql(n, m) #:nodoc:
294
294
  if n.shape != m.shape
295
295
  false
296
- else
296
+ elsif n.is_a?(NVector)
297
+ return false unless m.is_a?(NVector) # don't compare NV to NM
298
+ long_shape = n.shape[0] == 1 ? n.shape[1] : n.shape[0]
299
+ long_shape.times do |j|
300
+ if n[j] != m[j]
301
+ puts "n[#{j}] != m[#{j}] (#{n[j]} != #{m[j]})"
302
+ return false
303
+ end
304
+ end
305
+ else # NMatrix
297
306
  n.shape[0].times do |i|
298
307
  n.shape[1].times do |j|
299
308
  if n[i,j] != m[i,j]
@@ -302,7 +311,7 @@ describe "Slice operation" do
302
311
  end
303
312
  end
304
313
  end
305
- true
306
314
  end
315
+ true
307
316
  end
308
317
  end
data/spec/spec_helper.rb CHANGED
@@ -49,3 +49,20 @@ def create_matrix(stype) #:nodoc:
49
49
 
50
50
  m
51
51
  end
52
+
53
+ def create_vector(stype) #:nodoc:
54
+ m = stype == :yale ? NVector.new(stype, 10, :int32) : NVector.new(stype, 10, 0, :int32)
55
+
56
+ m[0] = 1
57
+ m[1] = 2
58
+ m[2] = 3
59
+ m[3] = 4
60
+ m[4] = 5
61
+ m[5] = 6
62
+ m[6] = 7
63
+ m[7] = 8
64
+ m[8] = 9
65
+ m[9] = 10
66
+
67
+ m
68
+ end
metadata CHANGED
@@ -1,8 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nmatrix
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
5
- prerelease:
4
+ version: 0.0.5
6
5
  platform: ruby
7
6
  authors:
8
7
  - John Woods
@@ -11,28 +10,25 @@ authors:
11
10
  autorequire:
12
11
  bindir: bin
13
12
  cert_chain: []
14
- date: 2013-05-17 00:00:00.000000000Z
13
+ date: 2013-07-09 00:00:00.000000000 Z
15
14
  dependencies:
16
15
  - !ruby/object:Gem::Dependency
17
16
  name: rdoc
18
17
  requirement: !ruby/object:Gem::Requirement
19
- none: false
20
18
  requirements:
21
- - - ! '>='
19
+ - - '>='
22
20
  - !ruby/object:Gem::Version
23
21
  version: 4.0.1
24
22
  type: :runtime
25
23
  prerelease: false
26
24
  version_requirements: !ruby/object:Gem::Requirement
27
- none: false
28
25
  requirements:
29
- - - ! '>='
26
+ - - '>='
30
27
  - !ruby/object:Gem::Version
31
28
  version: 4.0.1
32
29
  - !ruby/object:Gem::Dependency
33
30
  name: rake
34
31
  requirement: !ruby/object:Gem::Requirement
35
- none: false
36
32
  requirements:
37
33
  - - ~>
38
34
  - !ruby/object:Gem::Version
@@ -40,7 +36,6 @@ dependencies:
40
36
  type: :development
41
37
  prerelease: false
42
38
  version_requirements: !ruby/object:Gem::Requirement
43
- none: false
44
39
  requirements:
45
40
  - - ~>
46
41
  - !ruby/object:Gem::Version
@@ -48,23 +43,20 @@ dependencies:
48
43
  - !ruby/object:Gem::Dependency
49
44
  name: bundler
50
45
  requirement: !ruby/object:Gem::Requirement
51
- none: false
52
46
  requirements:
53
- - - ! '>='
47
+ - - '>='
54
48
  - !ruby/object:Gem::Version
55
49
  version: '0'
56
50
  type: :development
57
51
  prerelease: false
58
52
  version_requirements: !ruby/object:Gem::Requirement
59
- none: false
60
53
  requirements:
61
- - - ! '>='
54
+ - - '>='
62
55
  - !ruby/object:Gem::Version
63
56
  version: '0'
64
57
  - !ruby/object:Gem::Dependency
65
58
  name: rspec
66
59
  requirement: !ruby/object:Gem::Requirement
67
- none: false
68
60
  requirements:
69
61
  - - ~>
70
62
  - !ruby/object:Gem::Version
@@ -72,7 +64,6 @@ dependencies:
72
64
  type: :development
73
65
  prerelease: false
74
66
  version_requirements: !ruby/object:Gem::Requirement
75
- none: false
76
67
  requirements:
77
68
  - - ~>
78
69
  - !ruby/object:Gem::Version
@@ -80,7 +71,6 @@ dependencies:
80
71
  - !ruby/object:Gem::Dependency
81
72
  name: pry
82
73
  requirement: !ruby/object:Gem::Requirement
83
- none: false
84
74
  requirements:
85
75
  - - ~>
86
76
  - !ruby/object:Gem::Version
@@ -88,7 +78,6 @@ dependencies:
88
78
  type: :development
89
79
  prerelease: false
90
80
  version_requirements: !ruby/object:Gem::Requirement
91
- none: false
92
81
  requirements:
93
82
  - - ~>
94
83
  - !ruby/object:Gem::Version
@@ -96,7 +85,6 @@ dependencies:
96
85
  - !ruby/object:Gem::Dependency
97
86
  name: guard-rspec
98
87
  requirement: !ruby/object:Gem::Requirement
99
- none: false
100
88
  requirements:
101
89
  - - ~>
102
90
  - !ruby/object:Gem::Version
@@ -104,7 +92,6 @@ dependencies:
104
92
  type: :development
105
93
  prerelease: false
106
94
  version_requirements: !ruby/object:Gem::Requirement
107
- none: false
108
95
  requirements:
109
96
  - - ~>
110
97
  - !ruby/object:Gem::Version
@@ -112,7 +99,6 @@ dependencies:
112
99
  - !ruby/object:Gem::Dependency
113
100
  name: rake-compiler
114
101
  requirement: !ruby/object:Gem::Requirement
115
- none: false
116
102
  requirements:
117
103
  - - ~>
118
104
  - !ruby/object:Gem::Version
@@ -120,7 +106,6 @@ dependencies:
120
106
  type: :development
121
107
  prerelease: false
122
108
  version_requirements: !ruby/object:Gem::Requirement
123
- none: false
124
109
  requirements:
125
110
  - - ~>
126
111
  - !ruby/object:Gem::Version
@@ -151,7 +136,6 @@ files:
151
136
  - ext/nmatrix/data/rational.h
152
137
  - ext/nmatrix/data/ruby_object.h
153
138
  - ext/nmatrix/extconf.rb
154
- - ext/nmatrix/new_extconf.rb
155
139
  - ext/nmatrix/nmatrix.cpp
156
140
  - ext/nmatrix/nmatrix.h
157
141
  - ext/nmatrix/ruby_constants.cpp
@@ -187,6 +171,7 @@ files:
187
171
  - lib/nmatrix/nvector.rb
188
172
  - lib/nmatrix/shortcuts.rb
189
173
  - lib/nmatrix/version.rb
174
+ - lib/nmatrix/yale_functions.rb
190
175
  - nmatrix.gemspec
191
176
  - scripts/mac-brew-gcc.sh
192
177
  - spec/2x2_dense_double.mat
@@ -196,6 +181,7 @@ files:
196
181
  - spec/elementwise_spec.rb
197
182
  - spec/io_spec.rb
198
183
  - spec/lapack_spec.rb
184
+ - spec/leakcheck.rb
199
185
  - spec/math_spec.rb
200
186
  - spec/nmatrix_list_spec.rb
201
187
  - spec/nmatrix_spec.rb
@@ -208,42 +194,71 @@ files:
208
194
  - spec/utm5940.mtx
209
195
  homepage: http://sciruby.com
210
196
  licenses: []
211
- post_install_message: ! "***********************************************************\nWelcome
212
- to SciRuby: Tools for Scientific Computing in Ruby!\n\n ***
213
- WARNING ***\nPlease be aware that NMatrix is in ALPHA status. If you're\nthinking
214
- of using NMatrix to write mission critical code,\nsuch as for driving a car or flying
215
- a space shuttle, you\nmay wish to choose other software (for now).\n\nNMatrix requires
216
- a C compiler, and has been tested only\nwith GCC 4.6+. We are happy to accept contributions\nwhich
217
- improve the portability of this project.\n\nAlso required is ATLAS. Most Linux distributions
218
- and Mac\nversions include ATLAS, but you may wish to compile it\nyourself. Rumor
219
- has it that the Ubuntu/Debian apt package\nfor ATLAS WILL NOT WORK with NMatrix.
220
- Please install it\nmanually if you are using apt.\n\nMore explicit instructions
221
- for NMatrix and SciRuby should\nbe available on the SciRuby website, sciruby.com,
222
- or\nthrough our mailing list (which can be found on our web-\nsite).\n\nThanks for
223
- trying out NMatrix! Happy coding!\n\n***********************************************************\n"
197
+ metadata: {}
198
+ post_install_message: |
199
+ ***********************************************************
200
+ Welcome to SciRuby: Tools for Scientific Computing in Ruby!
201
+
202
+ *** WARNING ***
203
+ Please be aware that NMatrix is in ALPHA status. If you're
204
+ thinking of using NMatrix to write mission critical code,
205
+ such as for driving a car or flying a space shuttle, you
206
+ may wish to choose other software (for now).
207
+
208
+ NMatrix requires a C compiler, and has been tested only
209
+ with GCC 4.6+. We are happy to accept contributions
210
+ which improve the portability of this project.
211
+
212
+ Also required is ATLAS. Most Linux distributions and Mac
213
+ versions include ATLAS, but you may wish to compile it
214
+ yourself. Rumor has it that the Ubuntu/Debian apt package
215
+ for ATLAS WILL NOT WORK with NMatrix. Please install it
216
+ manually if you are using apt.
217
+
218
+ More explicit instructions for NMatrix and SciRuby should
219
+ be available on the SciRuby website, sciruby.com, or
220
+ through our mailing list (which can be found on our web-
221
+ site).
222
+
223
+ Thanks for trying out NMatrix! Happy coding!
224
+
225
+ ***********************************************************
224
226
  rdoc_options: []
225
227
  require_paths:
226
228
  - lib
227
229
  required_ruby_version: !ruby/object:Gem::Requirement
228
- none: false
229
230
  requirements:
230
- - - ! '>='
231
+ - - '>='
231
232
  - !ruby/object:Gem::Version
232
233
  version: 1.9.2
233
234
  required_rubygems_version: !ruby/object:Gem::Requirement
234
- none: false
235
235
  requirements:
236
- - - ! '>='
236
+ - - '>='
237
237
  - !ruby/object:Gem::Version
238
238
  version: '0'
239
- segments:
240
- - 0
241
- hash: 186187539
242
239
  requirements: []
243
240
  rubyforge_project:
244
- rubygems_version: 1.8.18
241
+ rubygems_version: 2.0.2
245
242
  signing_key:
246
- specification_version: 3
243
+ specification_version: 4
247
244
  summary: NMatrix is an experimental linear algebra library for Ruby, written mostly
248
245
  in C.
249
- test_files: []
246
+ test_files:
247
+ - spec/2x2_dense_double.mat
248
+ - spec/4x4_sparse.mat
249
+ - spec/4x5_dense.mat
250
+ - spec/blas_spec.rb
251
+ - spec/elementwise_spec.rb
252
+ - spec/io_spec.rb
253
+ - spec/lapack_spec.rb
254
+ - spec/leakcheck.rb
255
+ - spec/math_spec.rb
256
+ - spec/nmatrix_list_spec.rb
257
+ - spec/nmatrix_spec.rb
258
+ - spec/nmatrix_yale_resize_test_associations.yaml
259
+ - spec/nmatrix_yale_spec.rb
260
+ - spec/nvector_spec.rb
261
+ - spec/shortcuts_spec.rb
262
+ - spec/slice_spec.rb
263
+ - spec/spec_helper.rb
264
+ - spec/utm5940.mtx