nmatrix 0.0.1

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 (43) hide show
  1. data/.autotest +23 -0
  2. data/.gemtest +0 -0
  3. data/Gemfile +7 -0
  4. data/History.txt +6 -0
  5. data/LICENSE.txt +21 -0
  6. data/Manifest.txt +51 -0
  7. data/README.rdoc +63 -0
  8. data/Rakefile +154 -0
  9. data/ext/nmatrix/cblas.c +150 -0
  10. data/ext/nmatrix/dense.c +307 -0
  11. data/ext/nmatrix/dense/blas_header.template.c +52 -0
  12. data/ext/nmatrix/dense/elementwise.template.c +107 -0
  13. data/ext/nmatrix/dense/gemm.template.c +159 -0
  14. data/ext/nmatrix/dense/gemv.template.c +130 -0
  15. data/ext/nmatrix/dense/rationalmath.template.c +68 -0
  16. data/ext/nmatrix/depend +18 -0
  17. data/ext/nmatrix/extconf.rb +143 -0
  18. data/ext/nmatrix/generator.rb +594 -0
  19. data/ext/nmatrix/generator/syntax_tree.rb +481 -0
  20. data/ext/nmatrix/list.c +774 -0
  21. data/ext/nmatrix/nmatrix.c +1977 -0
  22. data/ext/nmatrix/nmatrix.h +912 -0
  23. data/ext/nmatrix/rational.c +98 -0
  24. data/ext/nmatrix/yale.c +726 -0
  25. data/ext/nmatrix/yale/complexmath.template.c +71 -0
  26. data/ext/nmatrix/yale/elementwise.template.c +46 -0
  27. data/ext/nmatrix/yale/elementwise_op.template.c +73 -0
  28. data/ext/nmatrix/yale/numbmm.template.c +94 -0
  29. data/ext/nmatrix/yale/smmp1.template.c +21 -0
  30. data/ext/nmatrix/yale/smmp1_header.template.c +38 -0
  31. data/ext/nmatrix/yale/smmp2.template.c +43 -0
  32. data/ext/nmatrix/yale/smmp2_header.template.c +46 -0
  33. data/ext/nmatrix/yale/sort_columns.template.c +56 -0
  34. data/ext/nmatrix/yale/symbmm.template.c +54 -0
  35. data/ext/nmatrix/yale/transp.template.c +68 -0
  36. data/lib/array.rb +67 -0
  37. data/lib/nmatrix.rb +263 -0
  38. data/lib/string.rb +65 -0
  39. data/spec/nmatrix_spec.rb +395 -0
  40. data/spec/nmatrix_yale_spec.rb +239 -0
  41. data/spec/nvector_spec.rb +43 -0
  42. data/spec/syntax_tree_spec.rb +46 -0
  43. metadata +150 -0
@@ -0,0 +1,239 @@
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
+ # == nmatrix_yale_spec.rb
24
+ #
25
+ # Basic tests for NMatrix's Yale storage type.
26
+ #
27
+ require "./lib/nmatrix"
28
+
29
+ describe NMatrix do
30
+ it "correctly compares two empty yale matrices" do
31
+ n = NMatrix.new(:yale, [4,4], :float64)
32
+ m = NMatrix.new(:yale, [4,4], :float64)
33
+ n.should.eql? m
34
+ end
35
+
36
+ it "correctly compares two yale matrices following basic assignments" do
37
+ n = NMatrix.new(:yale, [2,2], :float64)
38
+ m = NMatrix.new(:yale, [2,2], :float64)
39
+ m[0,0] = 1
40
+ m[0,1] = 1
41
+ n.should_not.eql? m
42
+ n[0,0] = 1
43
+ n.should_not.eql? m
44
+ n[0,1] = 1
45
+ n.should.eql? m
46
+ end
47
+
48
+ it "correctly compares two yale matrices following elementwise operations" do
49
+ n = NMatrix.new(:yale, [2,2], :float64)
50
+ n[0,1] = 1
51
+ m = NMatrix.new(:yale, [2,2], :float64)
52
+ m[0,1] = -1
53
+ r = NMatrix.new(:yale, [2,2], :float64)
54
+ r[0,1] = 0
55
+ (n+m).should.eql? r
56
+ end
57
+
58
+ it "correctly sets diagonal values in yale" do
59
+ n = NMatrix.new(:yale, [2,3], :float64)
60
+ n[1,1] = 0.1
61
+ n[0,0] = 0.2
62
+ n.__yale_d__.should == [0.2, 0.1]
63
+ end
64
+
65
+ it "does not resize yale until necessary" do
66
+ n = NMatrix.new(:yale, [2,3], :float64)
67
+ n.__yale_size__.should == 3
68
+ n.capacity.should == 5
69
+ n[0,0] = 0.1
70
+ n[0,1] = 0.2
71
+ n[1,0] = 0.3
72
+ n.__yale_size__.should == 5
73
+ n.capacity.should == 5
74
+ end
75
+
76
+
77
+ it "correctly sets when not resizing (yale)" do
78
+ n = NMatrix.new(:yale, [2,3], :float64)
79
+ n[0,0] = 0.1
80
+ n[0,1] = 0.2
81
+ n[1,0] = 0.3
82
+ n.__yale_a__ == [0.1, 0.0, 0.0, 0.2, 0.3]
83
+ n.__yale_ija__ == [3,4,5,1,0]
84
+ end
85
+
86
+ it "correctly sets when resizing (yale)" do
87
+ n = NMatrix.new(:yale, [2,3], :float64)
88
+ n[0,0] = 0.01
89
+ n[1,1] = 0.1
90
+ n[0,1] = 0.2
91
+ n[1,0] = 0.3
92
+ n[1,2] = 0.4
93
+ n.__yale_d__.should == [0.01, 0.1]
94
+ n.__yale_ia__.should == [3,4,6]
95
+ n.__yale_ja__.should == [1,0,2,nil]
96
+ n.__yale_lu__.should == [0.2, 0.3, 0.4, nil]
97
+ end
98
+
99
+ it "correctly sets values within rows of yale" do
100
+ n = NMatrix.new(:yale, [3,20], :float64)
101
+ n[2,1] = 1.0
102
+ n[2,0] = 1.5
103
+ n[2,15] = 2.0
104
+ n.__yale_lu__.should == [1.5, 1.0, 2.0]
105
+ n.__yale_ja__.should == [0, 1, 15]
106
+ end
107
+
108
+ it "correctly gets values within rows of yale" do
109
+ n = NMatrix.new(:yale, [3,20], :float64)
110
+ n[2,1] = 1.0
111
+ n[2,0] = 1.5
112
+ n[2,15] = 2.0
113
+ n[2,1].should == 1.0
114
+ n[2,0].should == 1.5
115
+ n[2,15].should == 2.0
116
+ end
117
+
118
+ it "correctly sets values within large rows of yale" do
119
+ n = NMatrix.new(:yale, [10,300], :float64)
120
+ n[5,1] = 1.0
121
+ n[5,0] = 1.5
122
+ n[5,15] = 2.0
123
+ n[5,291] = 3.0
124
+ n[5,292] = 4.0
125
+ n[5,289] = 5.0
126
+ n[5,290] = 6.0
127
+ n[5,293] = 2.0
128
+ n[5,299] = 7.0
129
+ n[5,100] = 8.0
130
+ n.__yale_lu__.should == [1.5, 1.0, 2.0, 8.0, 5.0, 6.0, 3.0, 4.0, 2.0, 7.0]
131
+ n.__yale_ja__.should == [0, 1, 15, 100, 289, 290, 291, 292, 293, 299]
132
+ end
133
+
134
+ it "correctly gets values within large rows of yale" do
135
+ n = NMatrix.new(:yale, [10,300], :float64)
136
+ n[5,1] = 1.0
137
+ n[5,0] = 1.5
138
+ n[5,15] = 2.0
139
+ n[5,291] = 3.0
140
+ n[5,292] = 4.0
141
+ n[5,289] = 5.0
142
+ n[5,290] = 6.0
143
+ n[5,293] = 2.0
144
+ n[5,299] = 7.0
145
+ n[5,100] = 8.0
146
+
147
+ n.__yale_ja__.each_index do |idx|
148
+ j = n.__yale_ja__[idx]
149
+ n[5,j].should == n.__yale_lu__[idx]
150
+ end
151
+ end
152
+
153
+ it "correctly dots two identical yale matrices" do
154
+ a = NMatrix.new(:yale, 4, :float64)
155
+ a[0,1] = 4.0
156
+ a[1,2] = 1.0
157
+ a[1,3] = 1.0
158
+ a[3,1] = 2.0
159
+
160
+ b = a.dup
161
+ c = a.dot b
162
+
163
+ c[0,0].should == 0.0
164
+ c[0,1].should == 0.0
165
+ c[0,2].should == 4.0
166
+ c[0,3].should == 4.0
167
+ c[1,0].should == 0.0
168
+ c[1,1].should == 2.0
169
+ c[1,2].should == 0.0
170
+ c[1,3].should == 0.0
171
+ c[2,0].should == 0.0
172
+ c[2,1].should == 0.0
173
+ c[2,2].should == 0.0
174
+ c[2,3].should == 0.0
175
+ c[3,0].should == 0.0
176
+ c[3,1].should == 0.0
177
+ c[3,2].should == 2.0
178
+ c[3,3].should == 2.0
179
+ end
180
+
181
+ it "correctly dots two identical yale matrices where a positive and negative partial sum cancel on the diagonal" do
182
+ a = NMatrix.new(:yale, 4, :float64)
183
+ a[0,0] = 1.0
184
+ a[0,1] = 4.0
185
+ a[1,2] = 2.0
186
+ a[1,3] = -4.0
187
+ a[3,1] = 4.0
188
+ a[3,3] = 4.0
189
+
190
+ b = a.dup
191
+ c = a.dot b
192
+
193
+ #c[0,0].should == 1.0
194
+ #c[0,1].should == 4.0
195
+ #c[0,2].should == 8.0
196
+ #c[0,3].should == -16.0
197
+ #c[1,0].should == 0.0
198
+ #c[1,1].should == -16.0
199
+ #c[1,2].should == 0.0
200
+ #c[1,3].should == -16.0
201
+ #c[2,0].should == 0.0
202
+ #c[2,1].should == 0.0
203
+ #c[2,2].should == 0.0
204
+ #c[2,3].should == 0.0
205
+ #c[3,0].should == 0.0
206
+ #c[3,1].should == 0.0
207
+ #c[3,2].should == 8.0
208
+ #c[3,3].should == 0.0 # this is the positive and negative partial sum cancel
209
+
210
+ c.__yale_ija__.reject { |i| i.nil? }.should == [5,8,9,9,11,1,2,3,3,1,2]
211
+ c.__yale_a__.reject { |i| i.nil? }.should == [1.0, -16.0, 0.0, 0.0, 0.0, 4.0, 8.0, -16.0, -16.0, 16.0, 8.0]
212
+
213
+ end
214
+
215
+ it "correctly transposes yale" do
216
+ a = NMatrix.new(:yale, 4, :float64)
217
+ a[0,0] = 1.0
218
+ a[0,1] = 4.0
219
+ a[1,2] = 2.0
220
+ a[1,3] = -4.0
221
+ a[3,1] = 5.0
222
+ a[3,3] = 6.0
223
+ b = a.transpose
224
+
225
+ b[0,0].should == 1.0
226
+ b[1,0].should == 4.0
227
+ b[2,0].should == 0.0
228
+ b[3,0].should == 0.0
229
+ b[0,1].should == 0.0
230
+ b[1,1].should == 0.0
231
+ b[2,1].should == 2.0
232
+ b[3,1].should == -4.0
233
+ b[0,3].should == 0.0
234
+ b[1,3].should == 5.0
235
+ b[2,3].should == 0.0
236
+ b[3,3].should == 6.0
237
+ end
238
+
239
+ end
@@ -0,0 +1,43 @@
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
+ # == nvector_spec.rb
24
+ #
25
+ # Basic tests for NVector.
26
+ #
27
+
28
+ require "./lib/nmatrix"
29
+
30
+ describe NVector do
31
+ it "correctly initializes" do
32
+ v = NVector.new 5, :float64
33
+ v.shape[0].should == 5
34
+ v.shape[1].should == 1
35
+ end
36
+
37
+ it "permits setting and getting contents" do
38
+ v = NVector.new 5, :float64
39
+ v[0] = 1.555
40
+ v[0].should == 1.555
41
+ end
42
+
43
+ end
@@ -0,0 +1,46 @@
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
+ # == syntax_tree_spec.rb
24
+ #
25
+ # Tests for the source code generator (syntax_tree.rb in the
26
+ # ext/nmatrix/generator dir).
27
+ #
28
+ require "./ext/nmatrix/generator/syntax_tree.rb"
29
+
30
+ describe SyntaxTree do
31
+ #it "correctly handles rational == 0" do
32
+ # SyntaxTree.parse("x == 0").operate(:rational,:r128).should == "x.n == 0"
33
+ #end
34
+ #
35
+ #it "correctly handles rational = 0" do
36
+ # SyntaxTree.parse("x == 0").operate(:rational,:r128).split("\n").should == ["x.n = 0", "x.d = 1;"]
37
+ #end
38
+ #
39
+ #it "correctly handles complex == 0" do
40
+ # SyntaxTree.parse("x == 0").operate(:complex,:c128).should == "x.r == 0 && x.i == 0"
41
+ #end
42
+ #
43
+ #it "correctly handles complex = 0" do
44
+ # SyntaxTree.parse("x = 0").operate(:complex,:c128).split("\n").should == ["x.r = 0", "x.i = 1;"]
45
+ #end
46
+ end
metadata ADDED
@@ -0,0 +1,150 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nmatrix
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - John Woods
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rdoc
16
+ requirement: &81217480 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.10'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *81217480
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake-compiler
27
+ requirement: &81217250 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '0.7'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *81217250
36
+ - !ruby/object:Gem::Dependency
37
+ name: hoe
38
+ requirement: &81217030 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: '3.0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *81217030
47
+ description: ! 'NMatrix is an experimental linear algebra library for Ruby, written
48
+ mostly in C. It can be used with or without SciRuby, but is part of the SciRuby
49
+ project.
50
+
51
+
52
+ NMatrix was inspired by and based heavily upon {NArray}[http://narray.rubyforge.org],
53
+ by Masahiro Tanaka.
54
+
55
+
56
+ {<img src=https://www.pledgie.com/campaigns/15783.png?skin_name=chrome>}[http://www.pledgie.com/campaigns/15783]'
57
+ email:
58
+ - john.o.woods@gmail.com
59
+ executables: []
60
+ extensions:
61
+ - ext/nmatrix/extconf.rb
62
+ extra_rdoc_files:
63
+ - History.txt
64
+ - Manifest.txt
65
+ - README.rdoc
66
+ - LICENSE.txt
67
+ files:
68
+ - .autotest
69
+ - History.txt
70
+ - Manifest.txt
71
+ - README.rdoc
72
+ - LICENSE.txt
73
+ - Rakefile
74
+ - Gemfile
75
+ - spec/nmatrix_spec.rb
76
+ - spec/nmatrix_yale_spec.rb
77
+ - spec/nvector_spec.rb
78
+ - spec/syntax_tree_spec.rb
79
+ - lib/array.rb
80
+ - lib/nmatrix.rb
81
+ - lib/string.rb
82
+ - ext/nmatrix/cblas.c
83
+ - ext/nmatrix/dense.c
84
+ - ext/nmatrix/depend
85
+ - ext/nmatrix/extconf.rb
86
+ - ext/nmatrix/generator.rb
87
+ - ext/nmatrix/list.c
88
+ - ext/nmatrix/nmatrix.c
89
+ - ext/nmatrix/nmatrix.h
90
+ - ext/nmatrix/rational.c
91
+ - ext/nmatrix/yale.c
92
+ - ext/nmatrix/generator/syntax_tree.rb
93
+ - ext/nmatrix/dense/blas_header.template.c
94
+ - ext/nmatrix/dense/elementwise.template.c
95
+ - ext/nmatrix/dense/gemm.template.c
96
+ - ext/nmatrix/dense/gemv.template.c
97
+ - ext/nmatrix/dense/rationalmath.template.c
98
+ - ext/nmatrix/yale/smmp1.template.c
99
+ - ext/nmatrix/yale/smmp2.template.c
100
+ - ext/nmatrix/yale/smmp1_header.template.c
101
+ - ext/nmatrix/yale/smmp2_header.template.c
102
+ - ext/nmatrix/yale/sort_columns.template.c
103
+ - ext/nmatrix/yale/symbmm.template.c
104
+ - ext/nmatrix/yale/numbmm.template.c
105
+ - ext/nmatrix/yale/transp.template.c
106
+ - ext/nmatrix/yale/complexmath.template.c
107
+ - ext/nmatrix/yale/elementwise.template.c
108
+ - ext/nmatrix/yale/elementwise_op.template.c
109
+ - .gemtest
110
+ homepage: http://sciruby.com
111
+ licenses: []
112
+ post_install_message: ! "***********************************************************\nWelcome
113
+ to SciRuby: Tools for Scientific Computing in Ruby!\n\n ***
114
+ WARNING ***\nPlease be aware that NMatrix is in ALPHA status. If you're\nthinking
115
+ of using NMatrix to write mission critical code,\nsuch as for driving a car or flying
116
+ a space shuttle, you\nmay wish to choose other software (for now).\n\nNMatrix requires
117
+ a C compiler, and has been tested only\nwith GCC 4.6.1. We are happy to accept contributions\nwhich
118
+ improve the portability of this project.\n\nAlso required is ATLAS. Most Linux distributions
119
+ and Mac\nversions include ATLAS, but you may wish to compile it\nyourself.\n\nMore
120
+ explicit instructions for NMatrix and SciRuby should\nbe available on the SciRuby
121
+ website, sciruby.com, or\nthrough our mailing list (which can be found on our web-\nsite).\n\nThanks
122
+ for trying out NMatrix! Happy coding!\n\n***********************************************************\n"
123
+ rdoc_options:
124
+ - --main
125
+ - README.rdoc
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '1.9'
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ none: false
136
+ requirements:
137
+ - - ! '>='
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ segments:
141
+ - 0
142
+ hash: -102996319
143
+ requirements: []
144
+ rubyforge_project: nmatrix
145
+ rubygems_version: 1.8.10
146
+ signing_key:
147
+ specification_version: 3
148
+ summary: NMatrix is an experimental linear algebra library for Ruby, written mostly
149
+ in C
150
+ test_files: []