nmatrix 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. data/Gemfile +1 -1
  2. data/History.txt +31 -3
  3. data/Manifest.txt +5 -0
  4. data/README.rdoc +29 -27
  5. data/ext/nmatrix/binary_format.txt +53 -0
  6. data/ext/nmatrix/data/data.cpp +18 -18
  7. data/ext/nmatrix/data/data.h +38 -7
  8. data/ext/nmatrix/data/rational.h +13 -0
  9. data/ext/nmatrix/data/ruby_object.h +10 -0
  10. data/ext/nmatrix/extconf.rb +2 -0
  11. data/ext/nmatrix/nmatrix.cpp +655 -103
  12. data/ext/nmatrix/nmatrix.h +26 -14
  13. data/ext/nmatrix/ruby_constants.cpp +4 -0
  14. data/ext/nmatrix/ruby_constants.h +2 -0
  15. data/ext/nmatrix/storage/dense.cpp +99 -41
  16. data/ext/nmatrix/storage/dense.h +3 -3
  17. data/ext/nmatrix/storage/list.cpp +36 -14
  18. data/ext/nmatrix/storage/list.h +4 -4
  19. data/ext/nmatrix/storage/storage.cpp +19 -19
  20. data/ext/nmatrix/storage/storage.h +11 -11
  21. data/ext/nmatrix/storage/yale.cpp +17 -20
  22. data/ext/nmatrix/storage/yale.h +13 -11
  23. data/ext/nmatrix/util/io.cpp +25 -23
  24. data/ext/nmatrix/util/io.h +5 -5
  25. data/ext/nmatrix/util/math.cpp +634 -17
  26. data/ext/nmatrix/util/math.h +958 -9
  27. data/ext/nmatrix/util/sl_list.cpp +7 -7
  28. data/ext/nmatrix/util/sl_list.h +2 -2
  29. data/lib/nmatrix.rb +9 -0
  30. data/lib/nmatrix/blas.rb +4 -4
  31. data/lib/nmatrix/io/market.rb +227 -0
  32. data/lib/nmatrix/io/mat_reader.rb +7 -7
  33. data/lib/nmatrix/lapack.rb +80 -0
  34. data/lib/nmatrix/nmatrix.rb +78 -52
  35. data/lib/nmatrix/shortcuts.rb +486 -0
  36. data/lib/nmatrix/version.rb +1 -1
  37. data/spec/2x2_dense_double.mat +0 -0
  38. data/spec/blas_spec.rb +59 -9
  39. data/spec/elementwise_spec.rb +25 -12
  40. data/spec/io_spec.rb +69 -1
  41. data/spec/lapack_spec.rb +53 -4
  42. data/spec/math_spec.rb +9 -0
  43. data/spec/nmatrix_list_spec.rb +95 -0
  44. data/spec/nmatrix_spec.rb +10 -53
  45. data/spec/nmatrix_yale_spec.rb +17 -15
  46. data/spec/shortcuts_spec.rb +154 -0
  47. metadata +22 -15
@@ -27,19 +27,20 @@
27
27
  require "./lib/nmatrix"
28
28
 
29
29
  describe NMatrix do
30
+ context :yale do
30
31
  it "calculates itype" do
31
32
  NMatrix.itype_by_shape([4,4]).should == :uint8
32
33
  NMatrix.itype_by_shape(4).should == :uint8
33
34
  ## FIXME: Check larger shapes for correct itype
34
35
  end
35
36
 
36
- it "compares two empty yale matrices" do
37
+ it "compares two empty matrices" do
37
38
  n = NMatrix.new(:yale, [4,4], :float64)
38
39
  m = NMatrix.new(:yale, [4,4], :float64)
39
40
  n.should == m
40
41
  end
41
42
 
42
- it "compares two yale matrices following basic assignments" do
43
+ it "compares two matrices following basic assignments" do
43
44
  n = NMatrix.new(:yale, [2,2], :float64)
44
45
  m = NMatrix.new(:yale, [2,2], :float64)
45
46
  m[0,0] = 1
@@ -51,7 +52,7 @@ describe NMatrix do
51
52
  n.should == m
52
53
  end
53
54
 
54
- it "compares two yale matrices following elementwise operations" do
55
+ it "compares two matrices following elementwise operations" do
55
56
  n = NMatrix.new(:yale, [2,2], :float64)
56
57
  n[0,1] = 1
57
58
  m = NMatrix.new(:yale, [2,2], :float64)
@@ -61,7 +62,7 @@ describe NMatrix do
61
62
  (n+m).should == r
62
63
  end
63
64
 
64
- it "sets diagonal values in yale" do
65
+ it "sets diagonal values" do
65
66
  n = NMatrix.new(:yale, [2,3], :float64)
66
67
  n.extend(NMatrix::YaleFunctions)
67
68
  n[1,1] = 0.1
@@ -69,7 +70,7 @@ describe NMatrix do
69
70
  n.yale_d.should == [0.2, 0.1]
70
71
  end
71
72
 
72
- it "does not resize yale until necessary" do
73
+ it "does not resize until necessary" do
73
74
  n = NMatrix.new(:yale, [2,3], :float64)
74
75
  n.extend(NMatrix::YaleFunctions)
75
76
  n.yale_size.should == 3
@@ -82,7 +83,7 @@ describe NMatrix do
82
83
  end
83
84
 
84
85
 
85
- it "sets when not resizing (yale)" do
86
+ it "sets when not resizing" do
86
87
  n = NMatrix.new(:yale, [2,3], :float64)
87
88
  n.extend(NMatrix::YaleFunctions)
88
89
  n[0,0] = 0.1
@@ -92,7 +93,7 @@ describe NMatrix do
92
93
  n.yale_ija == [3,4,5,1,0]
93
94
  end
94
95
 
95
- it "sets when resizing (yale)" do
96
+ it "sets when resizing" do
96
97
  n = NMatrix.new(:yale, [2,3], :float64)
97
98
  n.extend(NMatrix::YaleFunctions)
98
99
  n[0,0] = 0.01
@@ -106,7 +107,7 @@ describe NMatrix do
106
107
  n.yale_lu.should == [0.2, 0.3, 0.4, nil]
107
108
  end
108
109
 
109
- it "sets values within rows of yale" do
110
+ it "sets values within rows" do
110
111
  n = NMatrix.new(:yale, [3,20], :float64)
111
112
  n.extend(NMatrix::YaleFunctions)
112
113
  n[2,1] = 1.0
@@ -116,7 +117,7 @@ describe NMatrix do
116
117
  n.yale_ja.should == [0, 1, 15]
117
118
  end
118
119
 
119
- it "gets values within rows of yale" do
120
+ it "gets values within rows" do
120
121
  n = NMatrix.new(:yale, [3,20], :float64)
121
122
  n[2,1] = 1.0
122
123
  n[2,0] = 1.5
@@ -126,7 +127,7 @@ describe NMatrix do
126
127
  n[2,15].should == 2.0
127
128
  end
128
129
 
129
- it "sets values within large rows of yale" do
130
+ it "sets values within large rows" do
130
131
  n = NMatrix.new(:yale, [10,300], :float64)
131
132
  n.extend(NMatrix::YaleFunctions)
132
133
  n[5,1] = 1.0
@@ -143,7 +144,7 @@ describe NMatrix do
143
144
  n.yale_ja.should == [0, 1, 15, 100, 289, 290, 291, 292, 293, 299]
144
145
  end
145
146
 
146
- it "gets values within large rows of yale" do
147
+ it "gets values within large rows" do
147
148
  n = NMatrix.new(:yale, [10,300], :float64)
148
149
  n.extend(NMatrix::YaleFunctions)
149
150
  n[5,1] = 1.0
@@ -163,7 +164,7 @@ describe NMatrix do
163
164
  end
164
165
  end
165
166
 
166
- it "dots two identical yale matrices" do
167
+ it "dots two identical matrices" do
167
168
  a = NMatrix.new(:yale, 4, :float64)
168
169
  a[0,1] = 4.0
169
170
  a[1,2] = 1.0
@@ -191,7 +192,7 @@ describe NMatrix do
191
192
  c[3,3].should == 2.0
192
193
  end
193
194
 
194
- it "dots two identical yale matrices where a positive and negative partial sum cancel on the diagonal" do
195
+ it "dots two identical matrices where a positive and negative partial sum cancel on the diagonal" do
195
196
  a = NMatrix.new(:yale, 4, :float64)
196
197
 
197
198
  a[0,0] = 1.0
@@ -228,7 +229,7 @@ describe NMatrix do
228
229
 
229
230
  end
230
231
 
231
- it "transposes yale" do
232
+ it "transposes" do
232
233
  a = NMatrix.new(:yale, 4, :float64)
233
234
  a[0,0] = 1.0
234
235
  a[0,1] = 4.0
@@ -252,4 +253,5 @@ describe NMatrix do
252
253
  b[3,3].should == 6.0
253
254
  end
254
255
 
255
- end
256
+ end
257
+ end
@@ -0,0 +1,154 @@
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
+ # == shortcuts_spec.rb
24
+ #
25
+ # Specs for the shortcuts used in NMatrix and in NVector.
26
+ #
27
+
28
+ # Can we use require_relative here instead?
29
+ require File.join(File.dirname(__FILE__), "spec_helper.rb")
30
+
31
+ describe NMatrix do
32
+
33
+ it "zeros() creates a matrix of zeros" do
34
+ m = NMatrix.zeros(3)
35
+ n = NMatrix.new([3, 3], 0)
36
+
37
+ m.should.eql? n
38
+ end
39
+
40
+ it "ones() creates a matrix of ones" do
41
+ m = NMatrix.ones(3)
42
+ n = NMatrix.new([3, 3], 1)
43
+
44
+ m.should.eql? n
45
+ end
46
+
47
+ it "eye() creates an identity matrix" do
48
+ m = NMatrix.eye(3)
49
+ identity3 = NMatrix.new([3, 3], [1, 0, 0, 0, 1, 0, 0, 0, 1])
50
+
51
+ m.should.eql? identity3
52
+ end
53
+
54
+ it "random() creates a matrix of random numbers" do
55
+ m = NMatrix.random(2)
56
+
57
+ m.stype.should == :dense
58
+ m.dtype.should == :float64
59
+ end
60
+
61
+ it "seq() creates a matrix of integers, sequentially" do
62
+ m = NMatrix.seq(2) # 2x2 matrix.
63
+ value = 0
64
+
65
+ 2.times do |i|
66
+ 2.times do |j|
67
+ m[i, j].should == value
68
+ value += 1
69
+ end
70
+ end
71
+ end
72
+
73
+ it "seq() only accepts an integer or a 2-element array as dimension" do
74
+ expect { NMatrix.seq([1, 2, 3]) }.to raise_error
75
+ expect { NMatrix.seq("not an array or integer") }.to raise_error
76
+ end
77
+
78
+ it "column() returns a NMatrix" do
79
+ m = NMatrix.random(3)
80
+
81
+ m.column(2).is_a?(NMatrix).should be_true
82
+ end
83
+
84
+ it "column() accepts a second parameter (only :copy or :reference)" do
85
+ m = NMatrix.random(3)
86
+
87
+ expect { m.column(1, :copy) }.to_not raise_error
88
+ expect { m.column(1, :reference) }.to_not raise_error
89
+
90
+ expect { m.column(1, :derp) }.to raise_error
91
+ end
92
+ end
93
+
94
+ describe "NVector" do
95
+
96
+ it "zeros() creates a vector of zeros" do
97
+ v = NVector.zeros(4)
98
+
99
+ 4.times do |i|
100
+ v[i].should == 0
101
+ end
102
+ end
103
+
104
+ it "ones() creates a vector of ones" do
105
+ v = NVector.ones(3)
106
+
107
+ 3.times do |i|
108
+ v[i].should == 1
109
+ end
110
+ end
111
+
112
+ it "random() creates a vector of random numbers" do
113
+ v = NVector.zeros(4)
114
+ v.dtype.should == :float64
115
+ v.stype.should == :dense
116
+ end
117
+
118
+ it "seq() creates a vector of integers, sequentially" do
119
+ v = NVector.seq(7)
120
+ i = 0
121
+
122
+ v.each do |elem|
123
+ elem.should == i
124
+ i += 1
125
+ end
126
+ end
127
+
128
+ it "seq() only accepts integers as dimension" do
129
+ expect { NVector.seq(3) }.to_not raise_error
130
+
131
+ expect { NVector.seq([1, 3]) }.to raise_error
132
+ expect { NVector.seq(:wtf) }.to raise_error
133
+ end
134
+
135
+ it "linspace() creates a vector with n values equally spaced between a and b" do
136
+ v = NVector.linspace(0, 2, 5)
137
+ i = 0
138
+
139
+ v.each do |elem|
140
+ elem.should == i * 0.5
141
+ i += 1
142
+ end
143
+ end
144
+ end
145
+
146
+ describe "Inline constructor" do
147
+
148
+ it "creates a NMatrix with the given values" do
149
+ m = NMatrix.new([2, 2], [1, 4, 6, 7])
150
+ n = N[[1, 4], [6, 7]]
151
+
152
+ m.should.eql? n
153
+ end
154
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nmatrix
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,11 +11,11 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2012-09-21 00:00:00.000000000 Z
14
+ date: 2013-01-21 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: rake
18
- requirement: &75615060 !ruby/object:Gem::Requirement
18
+ requirement: &78366730 !ruby/object:Gem::Requirement
19
19
  none: false
20
20
  requirements:
21
21
  - - ~>
@@ -23,10 +23,10 @@ dependencies:
23
23
  version: '0.9'
24
24
  type: :development
25
25
  prerelease: false
26
- version_requirements: *75615060
26
+ version_requirements: *78366730
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
- requirement: &75614870 !ruby/object:Gem::Requirement
29
+ requirement: &78366510 !ruby/object:Gem::Requirement
30
30
  none: false
31
31
  requirements:
32
32
  - - ! '>='
@@ -34,10 +34,10 @@ dependencies:
34
34
  version: '0'
35
35
  type: :development
36
36
  prerelease: false
37
- version_requirements: *75614870
37
+ version_requirements: *78366510
38
38
  - !ruby/object:Gem::Dependency
39
39
  name: rspec
40
- requirement: &75614590 !ruby/object:Gem::Requirement
40
+ requirement: &78366230 !ruby/object:Gem::Requirement
41
41
  none: false
42
42
  requirements:
43
43
  - - ~>
@@ -45,10 +45,10 @@ dependencies:
45
45
  version: 2.9.0
46
46
  type: :development
47
47
  prerelease: false
48
- version_requirements: *75614590
48
+ version_requirements: *78366230
49
49
  - !ruby/object:Gem::Dependency
50
50
  name: pry
51
- requirement: &75614320 !ruby/object:Gem::Requirement
51
+ requirement: &78365970 !ruby/object:Gem::Requirement
52
52
  none: false
53
53
  requirements:
54
54
  - - ~>
@@ -56,10 +56,10 @@ dependencies:
56
56
  version: 0.9.9
57
57
  type: :development
58
58
  prerelease: false
59
- version_requirements: *75614320
59
+ version_requirements: *78365970
60
60
  - !ruby/object:Gem::Dependency
61
61
  name: guard-rspec
62
- requirement: &75614020 !ruby/object:Gem::Requirement
62
+ requirement: &78365710 !ruby/object:Gem::Requirement
63
63
  none: false
64
64
  requirements:
65
65
  - - ~>
@@ -67,10 +67,10 @@ dependencies:
67
67
  version: 0.7.0
68
68
  type: :development
69
69
  prerelease: false
70
- version_requirements: *75614020
70
+ version_requirements: *78365710
71
71
  - !ruby/object:Gem::Dependency
72
72
  name: rake-compiler
73
- requirement: &75613790 !ruby/object:Gem::Requirement
73
+ requirement: &78365470 !ruby/object:Gem::Requirement
74
74
  none: false
75
75
  requirements:
76
76
  - - ~>
@@ -78,7 +78,7 @@ dependencies:
78
78
  version: 0.8.1
79
79
  type: :development
80
80
  prerelease: false
81
- version_requirements: *75613790
81
+ version_requirements: *78365470
82
82
  description: NMatrix is an experimental linear algebra library for Ruby, written mostly
83
83
  in C.
84
84
  email:
@@ -97,6 +97,7 @@ files:
97
97
  - Manifest.txt
98
98
  - README.rdoc
99
99
  - Rakefile
100
+ - ext/nmatrix/binary_format.txt
100
101
  - ext/nmatrix/data/complex.h
101
102
  - ext/nmatrix/data/data.cpp
102
103
  - ext/nmatrix/data/data.h
@@ -130,14 +131,18 @@ files:
130
131
  - ext/nmatrix/util/util.h
131
132
  - lib/nmatrix.rb
132
133
  - lib/nmatrix/blas.rb
134
+ - lib/nmatrix/io/market.rb
133
135
  - lib/nmatrix/io/mat5_reader.rb
134
136
  - lib/nmatrix/io/mat_reader.rb
137
+ - lib/nmatrix/lapack.rb
135
138
  - lib/nmatrix/monkeys.rb
136
139
  - lib/nmatrix/nmatrix.rb
137
140
  - lib/nmatrix/nvector.rb
141
+ - lib/nmatrix/shortcuts.rb
138
142
  - lib/nmatrix/version.rb
139
143
  - nmatrix.gemspec
140
144
  - scripts/mac-brew-gcc.sh
145
+ - spec/2x2_dense_double.mat
141
146
  - spec/4x4_sparse.mat
142
147
  - spec/4x5_dense.mat
143
148
  - spec/blas_spec.rb
@@ -145,9 +150,11 @@ files:
145
150
  - spec/io_spec.rb
146
151
  - spec/lapack_spec.rb
147
152
  - spec/math_spec.rb
153
+ - spec/nmatrix_list_spec.rb
148
154
  - spec/nmatrix_spec.rb
149
155
  - spec/nmatrix_yale_spec.rb
150
156
  - spec/nvector_spec.rb
157
+ - spec/shortcuts_spec.rb
151
158
  - spec/slice_spec.rb
152
159
  - spec/spec_helper.rb
153
160
  - spec/utm5940.mtx
@@ -181,7 +188,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
181
188
  version: '0'
182
189
  segments:
183
190
  - 0
184
- hash: -1003602253
191
+ hash: -1012108815
185
192
  requirements: []
186
193
  rubyforge_project:
187
194
  rubygems_version: 1.8.10