nmatrix 0.1.0.rc5 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +0 -1
  3. data/Gemfile +0 -2
  4. data/History.txt +39 -4
  5. data/LICENSE.txt +3 -1
  6. data/Manifest.txt +2 -0
  7. data/README.rdoc +6 -14
  8. data/Rakefile +4 -1
  9. data/ext/nmatrix/data/data.cpp +1 -1
  10. data/ext/nmatrix/data/data.h +2 -1
  11. data/ext/nmatrix/data/rational.h +230 -226
  12. data/ext/nmatrix/extconf.rb +7 -4
  13. data/ext/nmatrix/math.cpp +259 -172
  14. data/ext/nmatrix/math/getri.h +2 -2
  15. data/ext/nmatrix/math/math.h +1 -1
  16. data/ext/nmatrix/ruby_constants.cpp +0 -1
  17. data/ext/nmatrix/ruby_nmatrix.c +55 -32
  18. data/ext/nmatrix/storage/dense/dense.cpp +1 -0
  19. data/ext/nmatrix/storage/yale/yale.cpp +12 -14
  20. data/ext/nmatrix/ttable_helper.rb +0 -1
  21. data/lib/nmatrix.rb +5 -0
  22. data/lib/nmatrix/homogeneous.rb +98 -0
  23. data/lib/nmatrix/io/fortran_format.rb +135 -0
  24. data/lib/nmatrix/io/harwell_boeing.rb +220 -0
  25. data/lib/nmatrix/io/market.rb +18 -8
  26. data/lib/nmatrix/io/mat5_reader.rb +16 -111
  27. data/lib/nmatrix/io/mat_reader.rb +3 -5
  28. data/lib/nmatrix/io/point_cloud.rb +27 -28
  29. data/lib/nmatrix/lapack.rb +3 -1
  30. data/lib/nmatrix/math.rb +112 -43
  31. data/lib/nmatrix/monkeys.rb +67 -11
  32. data/lib/nmatrix/nmatrix.rb +56 -33
  33. data/lib/nmatrix/rspec.rb +2 -2
  34. data/lib/nmatrix/shortcuts.rb +42 -25
  35. data/lib/nmatrix/version.rb +4 -4
  36. data/nmatrix.gemspec +4 -3
  37. data/spec/03_nmatrix_monkeys_spec.rb +72 -0
  38. data/spec/blas_spec.rb +4 -0
  39. data/spec/homogeneous_spec.rb +12 -4
  40. data/spec/io/fortran_format_spec.rb +88 -0
  41. data/spec/io/harwell_boeing_spec.rb +98 -0
  42. data/spec/io/test.rua +9 -0
  43. data/spec/math_spec.rb +51 -9
  44. metadata +38 -9
@@ -151,6 +151,10 @@ describe NMatrix::BLAS do
151
151
  expect(NMatrix::BLAS.asum(x)).to eq(10.0)
152
152
  end
153
153
 
154
+ it "exposes asum for single element" do
155
+ x = NMatrix.new([1], [-1], dtype: :float64)
156
+ expect(x.asum).to eq(1.0)
157
+ end
154
158
 
155
159
  it "exposes nrm2" do
156
160
  x = NMatrix.new([4,1], [2,-4,3,5], dtype: :float64)
@@ -31,7 +31,7 @@ require "./lib/nmatrix/homogeneous.rb"
31
31
  require 'pry'
32
32
 
33
33
  describe 'NMatrix' do
34
- context "#x_rotation" do
34
+ context ".x_rotation" do
35
35
  it "should generate a matrix representing a rotation about the x axis" do
36
36
  x = NMatrix.x_rotation(Math::PI.quo(6))
37
37
  expect(x).to be_within(1e-8).of(NMatrix.new([4,4], [1.0, 0.0, 0.0, 0.0,
@@ -42,7 +42,7 @@ describe 'NMatrix' do
42
42
  end
43
43
 
44
44
 
45
- context "#y_rotation" do
45
+ context ".y_rotation" do
46
46
  it "should generate a matrix representing a rotation about the y axis" do
47
47
  y = NMatrix.y_rotation(Math::PI.quo(6))
48
48
  expect(y).to be_within(1e-8).of(NMatrix.new([4,4], [Math.cos(Math::PI.quo(6)), 0.0, 0.5, 0.0,
@@ -52,7 +52,7 @@ describe 'NMatrix' do
52
52
  end
53
53
  end
54
54
 
55
- context "#z_rotation" do
55
+ context ".z_rotation" do
56
56
  it "should generate a matrix representing a rotation about the z axis" do
57
57
  z = NMatrix.z_rotation(Math::PI.quo(6))
58
58
  expect(z).to be_within(1e-8).of(NMatrix.new([4,4], [Math.cos(Math::PI.quo(6)), -0.5, 0.0, 0.0,
@@ -62,7 +62,7 @@ describe 'NMatrix' do
62
62
  end
63
63
  end
64
64
 
65
- context "#translation" do
65
+ context ".translation" do
66
66
  it "should generate a translation matrix from an Array" do
67
67
  t = NMatrix.translation([4,5,6])
68
68
  expect(t).to be_within(1e-8).of(NMatrix.new([4,4], [1, 0, 0, 4,
@@ -88,4 +88,12 @@ describe 'NMatrix' do
88
88
  expect(t.dtype).to be(:float64)
89
89
  end
90
90
  end
91
+
92
+ context "#quaternion" do
93
+ it "should generate a singularity-free quaternion" do
94
+ transform = NMatrix.new([4,4], [-0.9995825,-0.02527934,-0.0139845,50.61761,-0.02732551,0.9844284,0.1736463,-22.95566,0.009376526,0.1739562,-0.9847089,7.1521,0,0,0,1])
95
+ q = transform.quaternion
96
+ expect(Math.sqrt(q[0]**2 + q[1]**2 + q[2]**2 + q[3]**2)).to be_within(1e-6).of(1.0)
97
+ end
98
+ end
91
99
  end
@@ -0,0 +1,88 @@
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 - 2014, Ruby Science Foundation
12
+ # NMatrix is Copyright (c) 2012 - 2014, John Woods and the 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
+ # == fortran_format_spec.rb
24
+ #
25
+ # Basic tests for NMatrix::IO::FortranFormat.
26
+ #
27
+
28
+ require './lib/nmatrix'
29
+
30
+ describe NMatrix::IO::FortranFormat do
31
+ it "parses integer FORTRAN formats" do
32
+ int_fmt = NMatrix::IO::FortranFormat::Reader.new('(16I5)').parse
33
+
34
+ expect(int_fmt[:format_code]).to eq "INT_ID"
35
+ expect(int_fmt[:repeat]) .to eq 16
36
+ expect(int_fmt[:field_width]).to eq 5
37
+
38
+ int_fmt = NMatrix::IO::FortranFormat::Reader.new('(I4)').parse
39
+
40
+ expect(int_fmt[:format_code]).to eq "INT_ID"
41
+ expect(int_fmt[:field_width]).to eq 4
42
+ end
43
+
44
+ it "parses floating point FORTRAN formats" do
45
+ fp_fmt = NMatrix::IO::FortranFormat::Reader.new('(10F7.1)').parse
46
+
47
+ expect(fp_fmt[:format_code]) .to eq "FP_ID"
48
+ expect(fp_fmt[:repeat]) .to eq 10
49
+ expect(fp_fmt[:field_width]) .to eq 7
50
+ expect(fp_fmt[:post_decimal_width]).to eq 1
51
+
52
+ fp_fmt = NMatrix::IO::FortranFormat::Reader.new('(F4.2)').parse
53
+
54
+ expect(fp_fmt[:format_code]) .to eq "FP_ID"
55
+ expect(fp_fmt[:field_width]) .to eq 4
56
+ expect(fp_fmt[:post_decimal_width]).to eq 2
57
+ end
58
+
59
+ it "parses exponential FORTRAN formats" do
60
+ exp_fmt = NMatrix::IO::FortranFormat::Reader.new('(2E8.3E3)').parse
61
+
62
+ expect(exp_fmt[:format_code]) .to eq "EXP_ID"
63
+ expect(exp_fmt[:repeat]) .to eq 2
64
+ expect(exp_fmt[:field_width]) .to eq 8
65
+ expect(exp_fmt[:post_decimal_width]).to eq 3
66
+ expect(exp_fmt[:exponent_width]) .to eq 3
67
+
68
+ exp_fmt = NMatrix::IO::FortranFormat::Reader.new('(3E3.6)').parse
69
+
70
+ expect(exp_fmt[:format_code]) .to eq "EXP_ID"
71
+ expect(exp_fmt[:repeat]) .to eq 3
72
+ expect(exp_fmt[:field_width]) .to eq 3
73
+ expect(exp_fmt[:post_decimal_width]).to eq 6
74
+
75
+ exp_fmt = NMatrix::IO::FortranFormat::Reader.new('(E4.5)').parse
76
+ expect(exp_fmt[:format_code]) .to eq "EXP_ID"
77
+ expect(exp_fmt[:field_width]) .to eq 4
78
+ expect(exp_fmt[:post_decimal_width]).to eq 5
79
+ end
80
+
81
+ ['I3', '(F4)', '(E3.', '(E4.E5)'].each do |bad_format|
82
+ it "doesn't let bad input through : #{bad_format}" do
83
+ expect {
84
+ NMatrix::IO::FortranFormat::Reader.new(bad_format).parse
85
+ }.to raise_error(IOError)
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,98 @@
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 - 2014, Ruby Science Foundation
12
+ # NMatrix is Copyright (c) 2012 - 2014, John Woods and the 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
+ # == io_spec.rb
24
+ #
25
+ # Basic tests for NMatrix::IO::HarwelBoeing.
26
+
27
+ # TODO : After the fortran format thing is done
28
+ require 'spec_helper'
29
+ require "./lib/nmatrix"
30
+
31
+ describe NMatrix::IO::HarwellBoeing do
32
+ def check_file_header header
33
+ expect(header[:title]) .to eq("Title")
34
+ expect(header[:key]) .to eq("Key")
35
+
36
+ expect(header[:totcrd]) .to eq(5)
37
+ expect(header[:ptrcrd]) .to eq(1)
38
+ expect(header[:indcrd]) .to eq(1)
39
+ expect(header[:valcrd]) .to eq(3)
40
+ expect(header[:rhscrd]) .to eq(0)
41
+
42
+ expect(header[:mxtype]) .to eq('RUA')
43
+ expect(header[:nrow]) .to eq(5)
44
+ expect(header[:ncol]) .to eq(5)
45
+ expect(header[:nnzero]) .to eq(13)
46
+ expect(header[:neltvl]) .to eq(0)
47
+
48
+ expect(header[:ptrfmt]) .to eq({
49
+ format_code: "INT_ID",
50
+ repeat: 6,
51
+ field_width: 3
52
+ })
53
+ expect(header[:indfmt]) .to eq({
54
+ format_code: "INT_ID",
55
+ repeat: 13,
56
+ field_width: 3
57
+ })
58
+ expect(header[:valfmt]) .to eq({
59
+ format_code: "EXP_ID",
60
+ repeat: 5,
61
+ field_width: 15,
62
+ post_decimal_width: 8
63
+ })
64
+ expect(header[:rhsfmt]) .to eq({
65
+ format_code: "EXP_ID",
66
+ repeat: 5,
67
+ field_width: 15,
68
+ post_decimal_width: 8
69
+ })
70
+ end
71
+
72
+ it "loads a Harwell Boeing file values and header (currently real only)" do
73
+ n, h = NMatrix::IO::HarwellBoeing.load("spec/io/test.rua")
74
+
75
+ expect(n.is_a? NMatrix).to eq(true)
76
+ expect(n.cols) .to eq(5)
77
+ expect(n.rows) .to eq(5)
78
+
79
+ expect(n[0,0]) .to eq(11)
80
+ expect(n[4,4]) .to eq(55)
81
+
82
+ expect(h.is_a? Hash).to eq(true)
83
+ check_file_header(h)
84
+ end
85
+
86
+ it "loads only the header of the file when specified" do
87
+ h = NMatrix::IO::HarwellBoeing.load("spec/io/test.rua", header: true)
88
+
89
+ expect(h.is_a? Hash).to eq(true)
90
+ check_file_header(h)
91
+ end
92
+
93
+ it "raises error for wrong Harwell Boeing file name" do
94
+ expect{
95
+ NMatrix::IO::HarwellBoeing.load("spec/io/wrong.afx")
96
+ }.to raise_error(IOError)
97
+ end
98
+ end
@@ -0,0 +1,9 @@
1
+ Title Key
2
+ 5 1 1 3 0
3
+ RUA 5 5 13 0
4
+ (6I3) (13I3) (5E15.8) (5E15.8)
5
+ 1 4 7 8 11 14
6
+ 1 3 5 2 3 5 3 1 3 4 3 4 5
7
+ 11.0 31.0 51.0 22.0 32.0
8
+ 52.0 33.0 14.0 34.0 44.0
9
+ 35.0 45.0 55.0
@@ -28,6 +28,9 @@
28
28
 
29
29
  require 'spec_helper'
30
30
 
31
+ ALL_DTYPES = [:byte,:int8,:int16,:int32,:int64, :float32,:float64, :object,
32
+ :rational32,:rational64,:rational128, :complex64, :complex128]
33
+
31
34
  describe "math" do
32
35
  context "elementwise math functions" do
33
36
 
@@ -124,7 +127,7 @@ describe "math" do
124
127
  context "Floor and ceil for #{stype}" do
125
128
 
126
129
  [:floor, :ceil].each do |meth|
127
- [:byte,:int8,:int16,:int32,:int64, :float32,:float64, :object,:rational32,:rational64,:rational128, :complex64, :complex128].each do |dtype|
130
+ ALL_DTYPES.each do |dtype|
128
131
  context dtype do
129
132
  before :each do
130
133
  @size = [2,2]
@@ -163,6 +166,30 @@ describe "math" do
163
166
  end
164
167
  end
165
168
  end
169
+
170
+ context "#round for #{stype}" do
171
+ ALL_DTYPES.each do |dtype|
172
+ context dtype do
173
+ before :each do
174
+ @size = [2,2]
175
+ @mat = NMatrix.new @size, [1.33334, 0.9998, 1.9999, -8.9999],
176
+ dtype: dtype, stype: stype
177
+ @ans = @mat.to_a.flatten
178
+ end
179
+
180
+ it "rounds #{dtype} for #{stype}" do
181
+ expect(@mat.round).to eq(N.new(@size, @ans.map { |a| a.round},
182
+ dtype: dtype, stype: stype))
183
+ end unless(/complex/ =~ dtype)
184
+
185
+ it "rounds complex dtype #{dtype} for #{stype}" do
186
+
187
+ expect(@mat.round).to eq(N.new [2,2], @ans.map {|a|
188
+ Complex(a.real.round, a.imag.round)},dtype: dtype, stype: stype)
189
+ end if(/complex/ =~ dtype)
190
+ end
191
+ end
192
+ end
166
193
 
167
194
  end
168
195
  end
@@ -184,9 +211,9 @@ describe "math" do
184
211
  end
185
212
 
186
213
  context dtype do
187
- it "should correctly invert a matrix in place" do
188
- a = NMatrix.new(:dense, 3, [1,0,4,1,1,6,-3,0,-10], dtype)
189
- b = NMatrix.new(:dense, 3, [-5,0,-2,-4,1,-1,3.quo(2),0,1.quo(2)], dtype)
214
+ it "should correctly invert a matrix in place (bang)", :focus => true do
215
+ a = NMatrix.new(:dense, 3, [1,2,3,0,1,4,5,6,0], dtype)
216
+ b = NMatrix.new(:dense, 3, [-24,18,5,20,-15,-4,-5,4,1], dtype)
190
217
  begin
191
218
  a.invert!
192
219
  rescue NotImplementedError => e
@@ -196,16 +223,31 @@ describe "math" do
196
223
  pending e.to_s
197
224
  end
198
225
  end
199
- expect(a).to eq(b)
226
+ expect(a.round).to eq(b)
200
227
  end
201
228
 
202
229
  unless NMatrix.has_clapack?
203
- it "should correctly exact-invert a matrix" do
204
- a = NMatrix.new(:dense, 3, [1,0,4,1,1,6,-3,0,-10], dtype)
205
- b = NMatrix.new(:dense, 3, [-5,0,-2,-4,1,-1,3.quo(2),0,1.quo(2)], dtype)
206
- a.invert.should == b
230
+ it "should correctly invert a matrix in place" do
231
+ a = NMatrix.new(:dense, 5, [1, 8,-9, 7, 5,
232
+ 0, 1, 0, 4, 4,
233
+ 0, 0, 1, 2, 5,
234
+ 0, 0, 0, 1,-5,
235
+ 0, 0, 0, 0, 1 ], dtype)
236
+ b = NMatrix.new(:dense, 5, [1,-8, 9, 7, 17,
237
+ 0, 1, 0,-4,-24,
238
+ 0, 0, 1,-2,-15,
239
+ 0, 0, 0, 1, 5,
240
+ 0, 0, 0, 0, 1,], dtype)
241
+ expect(a.invert).to eq(b)
207
242
  end
208
243
  end
244
+
245
+ it "should correctly invert a matrix out-of-place" do
246
+ a = NMatrix.new(:dense, 3, [1,2,3,0,1,4,5,6,0], dtype)
247
+ b = NMatrix.new(:dense, 3, [-24,18,5,20,-15,-4,-5,4,1], dtype)
248
+
249
+ expect(a.invert(3,3)).to eq(b)
250
+ end
209
251
  end
210
252
  end
211
253
 
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.1.0.rc5
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Woods
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2014-08-01 00:00:00.000000000 Z
13
+ date: 2014-12-11 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rdoc
@@ -32,6 +32,26 @@ dependencies:
32
32
  - - ">="
33
33
  - !ruby/object:Gem::Version
34
34
  version: 4.0.1
35
+ - !ruby/object:Gem::Dependency
36
+ name: packable
37
+ requirement: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '1.3'
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: 1.3.5
45
+ type: :runtime
46
+ prerelease: false
47
+ version_requirements: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - "~>"
50
+ - !ruby/object:Gem::Version
51
+ version: '1.3'
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 1.3.5
35
55
  - !ruby/object:Gem::Dependency
36
56
  name: rake
37
57
  requirement: !ruby/object:Gem::Requirement
@@ -116,8 +136,8 @@ dependencies:
116
136
  - - "~>"
117
137
  - !ruby/object:Gem::Version
118
138
  version: '0.8'
119
- description: NMatrix is an experimental linear algebra library for Ruby, written mostly
120
- in C.
139
+ description: NMatrix is a linear algebra library for Ruby, written mostly in C and
140
+ C++.
121
141
  email:
122
142
  - john.o.woods@gmail.com
123
143
  executables: []
@@ -202,6 +222,8 @@ files:
202
222
  - lib/nmatrix/blas.rb
203
223
  - lib/nmatrix/enumerate.rb
204
224
  - lib/nmatrix/homogeneous.rb
225
+ - lib/nmatrix/io/fortran_format.rb
226
+ - lib/nmatrix/io/harwell_boeing.rb
205
227
  - lib/nmatrix/io/market.rb
206
228
  - lib/nmatrix/io/mat5_reader.rb
207
229
  - lib/nmatrix/io/mat_reader.rb
@@ -220,12 +242,16 @@ files:
220
242
  - spec/00_nmatrix_spec.rb
221
243
  - spec/01_enum_spec.rb
222
244
  - spec/02_slice_spec.rb
245
+ - spec/03_nmatrix_monkeys_spec.rb
223
246
  - spec/2x2_dense_double.mat
224
247
  - spec/4x4_sparse.mat
225
248
  - spec/4x5_dense.mat
226
249
  - spec/blas_spec.rb
227
250
  - spec/elementwise_spec.rb
228
251
  - spec/homogeneous_spec.rb
252
+ - spec/io/fortran_format_spec.rb
253
+ - spec/io/harwell_boeing_spec.rb
254
+ - spec/io/test.rua
229
255
  - spec/io_spec.rb
230
256
  - spec/lapack_spec.rb
231
257
  - spec/leakcheck.rb
@@ -242,7 +268,7 @@ files:
242
268
  - spec/utm5940.mtx
243
269
  homepage: http://sciruby.com
244
270
  licenses:
245
- - BSD 2-clause
271
+ - BSD 3-clause
246
272
  metadata: {}
247
273
  post_install_message: |
248
274
  ***********************************************************
@@ -275,26 +301,29 @@ required_ruby_version: !ruby/object:Gem::Requirement
275
301
  version: '1.9'
276
302
  required_rubygems_version: !ruby/object:Gem::Requirement
277
303
  requirements:
278
- - - ">"
304
+ - - ">="
279
305
  - !ruby/object:Gem::Version
280
- version: 1.3.1
306
+ version: '0'
281
307
  requirements: []
282
308
  rubyforge_project:
283
309
  rubygems_version: 2.2.0
284
310
  signing_key:
285
311
  specification_version: 4
286
- summary: NMatrix is an experimental linear algebra library for Ruby, written mostly
287
- in C.
312
+ summary: NMatrix is a linear algebra library for Ruby
288
313
  test_files:
289
314
  - spec/00_nmatrix_spec.rb
290
315
  - spec/01_enum_spec.rb
291
316
  - spec/02_slice_spec.rb
317
+ - spec/03_nmatrix_monkeys_spec.rb
292
318
  - spec/2x2_dense_double.mat
293
319
  - spec/4x4_sparse.mat
294
320
  - spec/4x5_dense.mat
295
321
  - spec/blas_spec.rb
296
322
  - spec/elementwise_spec.rb
297
323
  - spec/homogeneous_spec.rb
324
+ - spec/io/fortran_format_spec.rb
325
+ - spec/io/harwell_boeing_spec.rb
326
+ - spec/io/test.rua
298
327
  - spec/io_spec.rb
299
328
  - spec/lapack_spec.rb
300
329
  - spec/leakcheck.rb