nmatrix 0.0.9 → 0.1.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (101) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +1 -0
  3. data/History.txt +95 -1
  4. data/LICENSE.txt +2 -2
  5. data/README.rdoc +24 -26
  6. data/Rakefile +32 -16
  7. data/ext/nmatrix/data/complex.h +2 -2
  8. data/ext/nmatrix/data/data.cpp +27 -51
  9. data/ext/nmatrix/data/data.h +92 -4
  10. data/ext/nmatrix/data/meta.h +2 -2
  11. data/ext/nmatrix/data/rational.h +2 -2
  12. data/ext/nmatrix/data/ruby_object.h +2 -2
  13. data/ext/nmatrix/extconf.rb +87 -86
  14. data/ext/nmatrix/math.cpp +45 -40
  15. data/ext/nmatrix/math/asum.h +3 -3
  16. data/ext/nmatrix/math/geev.h +2 -2
  17. data/ext/nmatrix/math/gemm.h +6 -2
  18. data/ext/nmatrix/math/gemv.h +6 -2
  19. data/ext/nmatrix/math/ger.h +2 -2
  20. data/ext/nmatrix/math/gesdd.h +2 -2
  21. data/ext/nmatrix/math/gesvd.h +2 -2
  22. data/ext/nmatrix/math/getf2.h +2 -2
  23. data/ext/nmatrix/math/getrf.h +2 -2
  24. data/ext/nmatrix/math/getri.h +2 -2
  25. data/ext/nmatrix/math/getrs.h +7 -3
  26. data/ext/nmatrix/math/idamax.h +2 -2
  27. data/ext/nmatrix/math/inc.h +12 -6
  28. data/ext/nmatrix/math/laswp.h +2 -2
  29. data/ext/nmatrix/math/long_dtype.h +2 -2
  30. data/ext/nmatrix/math/math.h +16 -10
  31. data/ext/nmatrix/math/nrm2.h +3 -3
  32. data/ext/nmatrix/math/potrs.h +7 -3
  33. data/ext/nmatrix/math/rot.h +2 -2
  34. data/ext/nmatrix/math/rotg.h +2 -2
  35. data/ext/nmatrix/math/scal.h +2 -2
  36. data/ext/nmatrix/math/swap.h +2 -2
  37. data/ext/nmatrix/math/trsm.h +7 -3
  38. data/ext/nmatrix/nm_memory.h +60 -0
  39. data/ext/nmatrix/nmatrix.cpp +13 -47
  40. data/ext/nmatrix/nmatrix.h +37 -12
  41. data/ext/nmatrix/ruby_constants.cpp +4 -2
  42. data/ext/nmatrix/ruby_constants.h +4 -2
  43. data/ext/nmatrix/ruby_nmatrix.c +937 -170
  44. data/ext/nmatrix/storage/common.cpp +2 -2
  45. data/ext/nmatrix/storage/common.h +2 -2
  46. data/ext/nmatrix/storage/{dense.cpp → dense/dense.cpp} +253 -100
  47. data/ext/nmatrix/storage/{dense.h → dense/dense.h} +6 -5
  48. data/ext/nmatrix/storage/{list.cpp → list/list.cpp} +517 -98
  49. data/ext/nmatrix/storage/{list.h → list/list.h} +13 -6
  50. data/ext/nmatrix/storage/storage.cpp +48 -19
  51. data/ext/nmatrix/storage/storage.h +4 -4
  52. data/ext/nmatrix/storage/yale/class.h +112 -43
  53. data/ext/nmatrix/storage/yale/iterators/base.h +2 -2
  54. data/ext/nmatrix/storage/yale/iterators/iterator.h +2 -2
  55. data/ext/nmatrix/storage/yale/iterators/row.h +2 -2
  56. data/ext/nmatrix/storage/yale/iterators/row_stored.h +2 -2
  57. data/ext/nmatrix/storage/yale/iterators/row_stored_nd.h +4 -3
  58. data/ext/nmatrix/storage/yale/iterators/stored_diagonal.h +2 -2
  59. data/ext/nmatrix/storage/yale/math/transpose.h +2 -2
  60. data/ext/nmatrix/storage/yale/yale.cpp +343 -52
  61. data/ext/nmatrix/storage/yale/yale.h +7 -3
  62. data/ext/nmatrix/types.h +2 -2
  63. data/ext/nmatrix/util/io.cpp +5 -5
  64. data/ext/nmatrix/util/io.h +2 -2
  65. data/ext/nmatrix/util/sl_list.cpp +40 -27
  66. data/ext/nmatrix/util/sl_list.h +3 -3
  67. data/ext/nmatrix/util/util.h +2 -2
  68. data/lib/nmatrix.rb +2 -2
  69. data/lib/nmatrix/blas.rb +2 -2
  70. data/lib/nmatrix/enumerate.rb +17 -6
  71. data/lib/nmatrix/io/market.rb +2 -3
  72. data/lib/nmatrix/io/mat5_reader.rb +2 -2
  73. data/lib/nmatrix/io/mat_reader.rb +2 -2
  74. data/lib/nmatrix/lapack.rb +46 -46
  75. data/lib/nmatrix/math.rb +213 -20
  76. data/lib/nmatrix/monkeys.rb +24 -2
  77. data/lib/nmatrix/nmatrix.rb +394 -9
  78. data/lib/nmatrix/nvector.rb +2 -64
  79. data/lib/nmatrix/rspec.rb +2 -2
  80. data/lib/nmatrix/shortcuts.rb +14 -61
  81. data/lib/nmatrix/version.rb +11 -3
  82. data/lib/nmatrix/yale_functions.rb +4 -4
  83. data/nmatrix.gemspec +2 -7
  84. data/scripts/mac-brew-gcc.sh +11 -8
  85. data/scripts/mac-mavericks-brew-gcc.sh +22 -0
  86. data/spec/00_nmatrix_spec.rb +116 -7
  87. data/spec/01_enum_spec.rb +17 -3
  88. data/spec/02_slice_spec.rb +11 -3
  89. data/spec/blas_spec.rb +5 -2
  90. data/spec/elementwise_spec.rb +5 -2
  91. data/spec/io_spec.rb +27 -17
  92. data/spec/lapack_spec.rb +157 -9
  93. data/spec/math_spec.rb +95 -4
  94. data/spec/nmatrix_yale_spec.rb +21 -26
  95. data/spec/rspec_monkeys.rb +27 -0
  96. data/spec/rspec_spec.rb +2 -2
  97. data/spec/shortcuts_spec.rb +5 -10
  98. data/spec/slice_set_spec.rb +6 -2
  99. data/spec/spec_helper.rb +3 -2
  100. data/spec/stat_spec.rb +174 -158
  101. metadata +15 -15
data/spec/01_enum_spec.rb CHANGED
@@ -8,8 +8,8 @@
8
8
  #
9
9
  # == Copyright Information
10
10
  #
11
- # SciRuby is Copyright (c) 2010 - 2012, Ruby Science Foundation
12
- # NMatrix is Copyright (c) 2012, Ruby Science Foundation
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
13
  #
14
14
  # Please see LICENSE.txt for additional copyright notices.
15
15
  #
@@ -37,6 +37,10 @@ describe "NMatrix enumeration for" do
37
37
  @m = @n[1..4,1..3]
38
38
  end
39
39
 
40
+ #after :each do
41
+ # GC.start
42
+ #end
43
+
40
44
  if stype == :yale
41
45
  it "should iterate properly along each row of a slice" do
42
46
  vv = []
@@ -178,7 +182,17 @@ describe "NMatrix enumeration for" do
178
182
 
179
183
  end
180
184
 
185
+ if stype == :list or stype == :dense then
186
+ it "should correctly map to a matrix with a single element" do
187
+ nm = N.new([1], [2.0], stype: stype)
188
+ nm.map { |e| e**2 }.should eq N.new([1], [4.0], stype: stype)
189
+ end
181
190
 
191
+ it "should correctly map to a matrix with multiple elements" do
192
+ nm = N.new([2], [2.0, 2.0], stype: stype)
193
+ nm.map { |e| e**2 }.should eq N.new([2], [4.0, 4.0], stype: stype)
194
+ end
195
+ end
182
196
  end
183
197
  end
184
- end
198
+ end
@@ -8,8 +8,8 @@
8
8
  #
9
9
  # == Copyright Information
10
10
  #
11
- # SciRuby is Copyright (c) 2010 - 2012, Ruby Science Foundation
12
- # NMatrix is Copyright (c) 2012, Ruby Science Foundation
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
13
  #
14
14
  # Please see LICENSE.txt for additional copyright notices.
15
15
  #
@@ -34,10 +34,14 @@ describe "Slice operation" do
34
34
  [:dense, :list, :yale].each do |stype|
35
35
  context "for #{stype}" do
36
36
  before :each do
37
- GC.start # don't have to do this, but it helps to make sure we've cleaned up our pointers properly.
37
+ #GC.start # don't have to do this, but it helps to make sure we've cleaned up our pointers properly.
38
38
  @m = create_matrix(stype)
39
39
  end
40
40
 
41
+ #after :each do
42
+ # GC.start
43
+ #end
44
+
41
45
  it "should correctly return a row of a reference-slice" do
42
46
  @n = create_rectangular_matrix(stype)
43
47
  @m = @n[1..4,1..3]
@@ -256,6 +260,10 @@ describe "Slice operation" do
256
260
  @m[0..2,0..2].should == @m[0...3,0...3]
257
261
  end
258
262
 
263
+ it 'should correctly handle :* slice notation' do
264
+ @m[:*,0].should eq @m[0...@m.shape[0], 0]
265
+ end
266
+
259
267
  if stype == :dense
260
268
  [:byte,:int8,:int16,:int32,:int64,:float32,:float64,:rational64,:rational128].each do |left_dtype|
261
269
  [:byte,:int8,:int16,:int32,:int64,:float32,:float64,:rational64,:rational128].each do |right_dtype|
data/spec/blas_spec.rb CHANGED
@@ -8,8 +8,8 @@
8
8
  #
9
9
  # == Copyright Information
10
10
  #
11
- # SciRuby is Copyright (c) 2010 - 2012, Ruby Science Foundation
12
- # NMatrix is Copyright (c) 2012, Ruby Science Foundation
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
13
  #
14
14
  # Please see LICENSE.txt for additional copyright notices.
15
15
  #
@@ -29,6 +29,9 @@
29
29
  require File.join(File.dirname(__FILE__), "spec_helper.rb")
30
30
 
31
31
  describe NMatrix::BLAS do
32
+ #after :each do
33
+ # GC.start
34
+ #end
32
35
 
33
36
  [:rational32, :rational64, :rational128, :float32, :float64, :complex64, :complex128].each do |dtype|
34
37
  context dtype do
@@ -8,8 +8,8 @@
8
8
  #
9
9
  # == Copyright Information
10
10
  #
11
- # SciRuby is Copyright (c) 2010 - 2012, Ruby Science Foundation
12
- # NMatrix is Copyright (c) 2012, Ruby Science Foundation
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
13
  #
14
14
  # Please see LICENSE.txt for additional copyright notices.
15
15
  #
@@ -29,6 +29,9 @@
29
29
  require File.join(File.dirname(__FILE__), "spec_helper.rb")
30
30
 
31
31
  describe NMatrix do
32
+ #after :each do
33
+ # GC.start
34
+ #end
32
35
 
33
36
  context "yale" do
34
37
  before :each do
data/spec/io_spec.rb CHANGED
@@ -8,8 +8,8 @@
8
8
  #
9
9
  # == Copyright Information
10
10
  #
11
- # SciRuby is Copyright (c) 2010 - 2012, Ruby Science Foundation
12
- # NMatrix is Copyright (c) 2012, Ruby Science Foundation
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
13
  #
14
14
  # Please see LICENSE.txt for additional copyright notices.
15
15
  #
@@ -24,9 +24,21 @@
24
24
  #
25
25
  # Basic tests for NMatrix::IO.
26
26
  #
27
+ require "tmpdir" # Used to avoid cluttering the repository.
28
+
27
29
  require "./lib/nmatrix"
28
30
 
29
31
  describe NMatrix::IO do
32
+ before :each do
33
+ @tmp_dir = Dir.mktmpdir
34
+ @test_out = File.join(@tmp_dir, "test-out")
35
+ end
36
+
37
+ after :each do
38
+ File.delete(@test_out) if File.file?(@test_out)
39
+ Dir.rmdir(@tmp_dir)
40
+ end
41
+
30
42
  it "repacks a string" do
31
43
  NMatrix::IO::Matlab.repack("hello", :miUINT8, :byte).should == "hello"
32
44
  end
@@ -83,57 +95,55 @@ describe NMatrix::IO do
83
95
 
84
96
  it "reads and writes NMatrix dense" do
85
97
  n = NMatrix.new(:dense, [4,3], [0,1,2,3,4,5,6,7,8,9,10,11], :int32)
86
- n.write("test-out")
98
+ n.write(@test_out)
87
99
 
88
- m = NMatrix.read("test-out")
100
+ m = NMatrix.read(@test_out)
89
101
  n.should == m
90
102
  end
91
103
 
92
104
  it "reads and writes NMatrix dense as symmetric" do
93
105
  n = NMatrix.new(:dense, 3, [0,1,2,1,3,4,2,4,5], :int16)
94
- n.write("test-out", :symmetric)
106
+ n.write(@test_out, :symmetric)
95
107
 
96
- m = NMatrix.read("test-out")
108
+ m = NMatrix.read(@test_out)
97
109
  n.should == m
98
110
  end
99
111
 
100
112
  it "reads and writes NMatrix dense as skew" do
101
113
  n = NMatrix.new(:dense, 3, [0,1,2,-1,3,4,-2,-4,5], :float64)
102
- n.write("test-out", :skew)
114
+ n.write(@test_out, :skew)
103
115
 
104
- m = NMatrix.read("test-out")
116
+ m = NMatrix.read(@test_out)
105
117
  n.should == m
106
118
  end
107
119
 
108
120
  it "reads and writes NMatrix dense as hermitian" do
109
121
  n = NMatrix.new(:dense, 3, [0,1,2,1,3,4,2,4,5], :complex64)
110
- n.write("test-out", :hermitian)
122
+ n.write(@test_out, :hermitian)
111
123
 
112
- m = NMatrix.read("test-out")
124
+ m = NMatrix.read(@test_out)
113
125
  n.should == m
114
126
  end
115
127
 
116
128
  it "reads and writes NMatrix dense as upper" do
117
129
  n = NMatrix.new(:dense, 3, [-1,1,2,3,4,5,6,7,8], :int32)
118
- n.write("test-out", :upper)
130
+ n.write(@test_out, :upper)
119
131
 
120
132
  m = NMatrix.new(:dense, 3, [-1,1,2,0,4,5,0,0,8], :int32) # lower version of the same
121
133
 
122
- o = NMatrix.read("test-out")
134
+ o = NMatrix.read(@test_out)
123
135
  o.should == m
124
136
  o.should_not == n
125
137
  end
126
138
 
127
139
  it "reads and writes NMatrix dense as lower" do
128
140
  n = NMatrix.new(:dense, 3, [-1,1,2,3,4,5,6,7,8], :int32)
129
- n.write("test-out", :lower)
141
+ n.write(@test_out, :lower)
130
142
 
131
143
  m = NMatrix.new(:dense, 3, [-1,0,0,3,4,0,6,7,8], :int32) # lower version of the same
132
144
 
133
- o = NMatrix.read("test-out")
145
+ o = NMatrix.read(@test_out)
134
146
  o.should == m
135
147
  o.should_not == n
136
148
  end
137
-
138
-
139
- end
149
+ end
data/spec/lapack_spec.rb CHANGED
@@ -8,8 +8,8 @@
8
8
  #
9
9
  # == Copyright Information
10
10
  #
11
- # SciRuby is Copyright (c) 2010 - 2012, Ruby Science Foundation
12
- # NMatrix is Copyright (c) 2012, Ruby Science Foundation
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
13
  #
14
14
  # Please see LICENSE.txt for additional copyright notices.
15
15
  #
@@ -29,6 +29,10 @@
29
29
  require File.join(File.dirname(__FILE__), "spec_helper.rb")
30
30
 
31
31
  describe NMatrix::LAPACK do
32
+ #after :each do
33
+ # GC.start
34
+ #end
35
+
32
36
  # where integer math is allowed
33
37
  [:byte, :int8, :int16, :int32, :int64, :rational32, :rational64, :rational128, :float32, :float64, :complex64, :complex128].each do |dtype|
34
38
  context dtype do
@@ -55,7 +59,23 @@ describe NMatrix::LAPACK do
55
59
  # where integer math is not allowed
56
60
  [:rational32, :rational64, :rational128, :float32, :float64, :complex64, :complex128].each do |dtype|
57
61
  context dtype do
58
- it "exposes clapack getrf" do
62
+
63
+ it "exposes clapack_gesv" do
64
+ a = NMatrix[[1.quo(1), 2, 3], [0,1.quo(2),4],[3,3,9]].cast(dtype: dtype)
65
+ b = NMatrix[[1.quo(1)],[2],[3]].cast(dtype: dtype)
66
+ err = case dtype
67
+ when :float32, :complex64
68
+ 1e-6
69
+ when :float64, :complex128
70
+ 1e-8
71
+ else
72
+ 1e-64
73
+ end
74
+ NMatrix::LAPACK::clapack_gesv(:row,a.shape[0],b.shape[1],a,a.shape[0],b,b.shape[0]).should be_within(err).of(NMatrix[[-1.quo(2)], [0], [1.quo(2)]].cast(dtype: dtype))
75
+ end
76
+
77
+
78
+ it "exposes clapack_getrf" do
59
79
  a = NMatrix.new(3, [4,9,2,3,5,7,8,1,6], dtype: dtype)
60
80
  NMatrix::LAPACK::clapack_getrf(:row, 3, 3, a, 3)
61
81
 
@@ -80,7 +100,7 @@ describe NMatrix::LAPACK do
80
100
  a[2,2].should be_within(err).of(360.quo(53))
81
101
  end
82
102
 
83
- it "exposes clapack potrf" do
103
+ it "exposes clapack_potrf" do
84
104
  # first do upper
85
105
  begin
86
106
  a = NMatrix.new(:dense, 3, [25,15,-5, 0,18,0, 0,0,11], dtype)
@@ -99,7 +119,7 @@ describe NMatrix::LAPACK do
99
119
  end
100
120
 
101
121
  # Together, these calls are basically xGESV from LAPACK: http://www.netlib.org/lapack/double/dgesv.f
102
- it "exposes clapack getrs" do
122
+ it "exposes clapack_getrs" do
103
123
  a = NMatrix.new(3, [-2,4,-3,3,-2,1,0,-4,3], dtype: dtype)
104
124
  ipiv = NMatrix::LAPACK::clapack_getrf(:row, 3, 3, a, 3)
105
125
  b = NMatrix.new([3,1], [-1, 17, -9], dtype: dtype)
@@ -111,7 +131,7 @@ describe NMatrix::LAPACK do
111
131
  b[2].should == -13
112
132
  end
113
133
 
114
- it "exposes clapack getri" do
134
+ it "exposes clapack_getri" do
115
135
  a = NMatrix.new(:dense, 3, [1,0,4,1,1,6,-3,0,-10], dtype)
116
136
  ipiv = NMatrix::LAPACK::clapack_getrf(:row, 3, 3, a, 3) # get pivot from getrf, use for getri
117
137
 
@@ -125,7 +145,7 @@ describe NMatrix::LAPACK do
125
145
  end
126
146
  end
127
147
 
128
- it "exposes lapack gesdd" do
148
+ it "exposes lapack_gesdd" do
129
149
  if [:float32, :float64].include? dtype
130
150
  a = NMatrix.new([5,6], %w|8.79 9.93 9.83 5.45 3.16
131
151
  6.11 6.91 5.04 -0.27 7.98
@@ -160,6 +180,7 @@ describe NMatrix::LAPACK do
160
180
  #http://software.intel.com/sites/products/documentation/doclib/mkl_sa/11/mkl_lapack_examples/cgesvd_ex.c.htm
161
181
  pending "Example may be wrong"
162
182
  else
183
+ pending "Not implemented for non-LAPACK dtypes"
163
184
  a = NMatrix.new([4,3], dtype: dtype)
164
185
  end
165
186
  err = case dtype
@@ -173,7 +194,7 @@ describe NMatrix::LAPACK do
173
194
  err = err *5e1
174
195
  begin
175
196
 
176
- info = NMatrix::LAPACK::lapack_gesvd(:a, :a, a.shape[0], a.shape[1], a, a.shape[0], s, u, ldu, vt, ldvt, 500)
197
+ info = NMatrix::LAPACK::lapack_gesdd(:a, a.shape[0], a.shape[1], a, a.shape[0], s, u, ldu, vt, ldvt, 500)
177
198
 
178
199
  rescue NotImplementedError => e
179
200
  pending e.to_s
@@ -186,7 +207,7 @@ describe NMatrix::LAPACK do
186
207
  end
187
208
 
188
209
 
189
- it "exposes lapack gesvd" do
210
+ it "exposes lapack_gesvd" do
190
211
  # http://software.intel.com/sites/products/documentation/doclib/mkl_sa/11/mkl_lapack_examples/dgesvd_ex.c.htm
191
212
  if [:float32, :float64].include? dtype
192
213
  a = NMatrix.new([5,6], %w|8.79 9.93 9.83 5.45 3.16
@@ -257,6 +278,133 @@ describe NMatrix::LAPACK do
257
278
  s.transpose.should be_within(err).of(s_true.row(0))
258
279
 
259
280
  end
281
+
282
+ it "exposes the convenience gesvd method" do
283
+ # http://software.intel.com/sites/products/documentation/doclib/mkl_sa/11/mkl_lapack_examples/dgesvd_ex.c.htm
284
+ if [:float32, :float64].include? dtype
285
+ a = NMatrix.new([5,6], %w|8.79 9.93 9.83 5.45 3.16
286
+ 6.11 6.91 5.04 -0.27 7.98
287
+ -9.15 -7.93 4.86 4.85 3.01
288
+ 9.57 1.64 8.83 0.74 5.80
289
+ -3.49 4.02 9.80 10.00 4.27
290
+ 9.84 0.15 -8.99 -6.02 -5.31|.map(&:to_f), dtype: dtype)
291
+ s_true = NMatrix.new([1,5], [27.468732418221848, 22.643185009774697, 8.558388228482576, 5.985723201512133, 2.014899658715756], dtype: dtype)
292
+ right_true = NMatrix.new([5,6], [0.5911423764124365, 0.2631678147140568, 0.35543017386282716, 0.3142643627269275, 0.2299383153647484, 0.0, 0.39756679420242547, 0.24379902792633046, -0.22239000068544604, -0.7534661509534584, -0.36358968669749664, 0.0, 0.03347896906244727, -0.6002725806935828, -0.45083926892230763, 0.23344965724471425, -0.3054757327479317, 0.0, 0.4297069031370182, 0.23616680628112555, -0.6858628638738117, 0.3318600182003095, 0.1649276348845103, 0.0, 0.4697479215666587, -0.350891398883702, 0.38744460309967327, 0.15873555958215635, -0.5182574373535355, 0.0, -0.29335875846440357, 0.57626211913389, -0.020852917980871258, 0.3790776670601607, -0.6525516005923976, 0.0], dtype: dtype)
293
+ #right_true = NMatrix.new([5,6],
294
+ # %w|-0.59 0.26 0.36 0.31 0.23
295
+ # -0.40 0.24 -0.22 -0.75 -0.36
296
+ # -0.03 -0.60 -0.45 0.23 -0.31
297
+ # -0.43 0.24 -0.69 0.33 0.16
298
+ # -0.47 -0.35 0.39 0.16 -0.52
299
+ # 0.29 0.58 -0.02 0.38 -0.65|.map(&:to_f),
300
+ # dtype)
301
+ left_true = NMatrix.new([5,5], [0.25138279272049635, 0.3968455517769292, 0.6921510074703637, 0.3661704447722309, 0.4076352386533525, 0.814836686086339, 0.3586615001880027, -0.24888801115928438, -0.3685935379446176, -0.09796256926688672, -0.2606185055842211, 0.7007682094072526, -0.22081144672043734, 0.38593848318854174, -0.49325014285102375, 0.3967237771305971, -0.4507112412166429, 0.2513211496937535, 0.4342486014366711, -0.6226840720358049, -0.21802776368654594, 0.14020994987112056, 0.5891194492399431, -0.6265282503648172, -0.4395516923423326], dtype: dtype)
302
+ #left_true = NMatrix.new([5,5],
303
+ # %w|-0.25 -0.40 -0.69 -0.37 -0.41
304
+ # 0.81 0.36 -0.25 -0.37 -0.10
305
+ # -0.26 0.70 -0.22 0.39 -0.49
306
+ # 0.40 -0.45 0.25 0.43 -0.62
307
+ # -0.22 0.14 0.59 -0.63 -0.44|.map(&:to_f),
308
+ # dtype)
309
+ s = NMatrix.new([5,1], 0, dtype: dtype)
310
+ u = NMatrix.new([5,5], 0, dtype: dtype)
311
+ ldu = 5
312
+ vt = NMatrix.new([6,6], 0, dtype: dtype)
313
+ ldvt= 6
314
+ elsif [:complex64, :complex128].include? dtype
315
+ #http://software.intel.com/sites/products/documentation/doclib/mkl_sa/11/mkl_lapack_examples/cgesvd_ex.c.htm
316
+ pending "Example may be wrong"
317
+ a = NMatrix.new([4,3], [[ 5.91, -5.69], [ 7.09, 2.72], [ 7.78, -4.06], [ -0.79, -7.21], [ -3.15, -4.08], [ -1.89, 3.27], [ 4.57, -2.07], [ -3.88, -3.30], [ -4.89, 4.20], [ 4.10, -6.70], [ 3.28, -3.84], [ 3.84, 1.19]].map {|e| Complex(*e) } , dtype: dtype)
318
+ s_true = NMatrix.new([3,1], [17.63, 11.61, 6.78], dtype: dtype)
319
+ left_true = NMatrix.new([4,4], [[-0.86, 0.0], [0.4, 0.0], [0.32, 0.0], [-0.35, 0.13], [-0.24, -0.21], [-0.63, 0.6], [0.15, 0.32], [0.61, 0.61], [-0.36, 0.1]].map {|e| Complex(*e)}, dtype: dtype)
320
+ right_true = NMatrix.new([4,3], [[ -0.22, 0.51], [ -0.37, -0.32], [ -0.53, 0.11], [ 0.15, 0.38], [ 0.31, 0.31], [ 0.09, -0.57], [ 0.18, -0.39], [ 0.38, -0.39], [ 0.53, 0.24], [ 0.49, 0.28], [ -0.47, -0.25], [ -0.15, 0.19]].map {|e| Complex *e} , dtype: dtype)
321
+
322
+ s = NMatrix.new([3,1], 0, dtype: dtype)
323
+ u = NMatrix.new([4,4], 0, dtype: dtype)
324
+ ldu = 4
325
+ vt = NMatrix.new([3,3], 0, dtype: dtype)
326
+ ldvt= 3
327
+ else
328
+ a = NMatrix.new([4,3], dtype: dtype)
329
+ end
330
+ err = case dtype
331
+ when :float32, :complex64
332
+ 1e-6
333
+ when :float64, :complex128
334
+ 1e-15
335
+ else
336
+ 1e-64 # FIXME: should be 0, but be_within(0) does not work.
337
+ end
338
+ err = err *5e1
339
+ begin
340
+ u, s, vt = a.gesvd
341
+ rescue NotImplementedError => e
342
+ pending e.to_s
343
+ end
344
+ u.should be_within(err).of(left_true)
345
+ #FIXME: Is the next line correct?
346
+ vt[0...right_true.shape[0], 0...right_true.shape[1]-1].should be_within(err).of(right_true[0...right_true.shape[0],0...right_true.shape[1]-1])
347
+ s.transpose.should be_within(err).of(s_true.row(0))
348
+
349
+ end
350
+ it "exposes the convenience gesdd method" do
351
+ # http://software.intel.com/sites/products/documentation/doclib/mkl_sa/11/mkl_lapack_examples/dgesvd_ex.c.htm
352
+ if [:float32, :float64].include? dtype
353
+ a = NMatrix.new([5,6], %w|8.79 9.93 9.83 5.45 3.16
354
+ 6.11 6.91 5.04 -0.27 7.98
355
+ -9.15 -7.93 4.86 4.85 3.01
356
+ 9.57 1.64 8.83 0.74 5.80
357
+ -3.49 4.02 9.80 10.00 4.27
358
+ 9.84 0.15 -8.99 -6.02 -5.31|.map(&:to_f), dtype: dtype)
359
+ s_true = NMatrix.new([1,5], [27.468732418221848, 22.643185009774697, 8.558388228482576, 5.985723201512133, 2.014899658715756], dtype: dtype)
360
+ right_true = NMatrix.new([5,6], [0.5911423764124365, 0.2631678147140568, 0.35543017386282716, 0.3142643627269275, 0.2299383153647484, 0.0, 0.39756679420242547, 0.24379902792633046, -0.22239000068544604, -0.7534661509534584, -0.36358968669749664, 0.0, 0.03347896906244727, -0.6002725806935828, -0.45083926892230763, 0.23344965724471425, -0.3054757327479317, 0.0, 0.4297069031370182, 0.23616680628112555, -0.6858628638738117, 0.3318600182003095, 0.1649276348845103, 0.0, 0.4697479215666587, -0.350891398883702, 0.38744460309967327, 0.15873555958215635, -0.5182574373535355, 0.0, -0.29335875846440357, 0.57626211913389, -0.020852917980871258, 0.3790776670601607, -0.6525516005923976, 0.0], dtype: dtype)
361
+ left_true = NMatrix.new([5,5], [0.25138279272049635, 0.3968455517769292, 0.6921510074703637, 0.3661704447722309, 0.4076352386533525, 0.814836686086339, 0.3586615001880027, -0.24888801115928438, -0.3685935379446176, -0.09796256926688672, -0.2606185055842211, 0.7007682094072526, -0.22081144672043734, 0.38593848318854174, -0.49325014285102375, 0.3967237771305971, -0.4507112412166429, 0.2513211496937535, 0.4342486014366711, -0.6226840720358049, -0.21802776368654594, 0.14020994987112056, 0.5891194492399431, -0.6265282503648172, -0.4395516923423326], dtype: dtype)
362
+ u = NMatrix.new([5,5], 0, dtype: dtype)
363
+ ldu = 5
364
+ vt = NMatrix.new([6,6], 0, dtype: dtype)
365
+ ldvt= 6
366
+ elsif [:complex64, :complex128].include? dtype
367
+ #http://software.intel.com/sites/products/documentation/doclib/mkl_sa/11/mkl_lapack_examples/cgesvd_ex.c.htm
368
+ pending "Example may be wrong"
369
+ a = NMatrix.new([4,3], [[ 5.91, -5.69], [ 7.09, 2.72], [ 7.78, -4.06], [ -0.79, -7.21], [ -3.15, -4.08], [ -1.89, 3.27], [ 4.57, -2.07], [ -3.88, -3.30], [ -4.89, 4.20], [ 4.10, -6.70], [ 3.28, -3.84], [ 3.84, 1.19]].map {|e| Complex(*e) } , dtype: dtype)
370
+ s_true = NMatrix.new([3,1], [17.63, 11.61, 6.78], dtype: dtype)
371
+ left_true = NMatrix.new([4,4], [[-0.86, 0.0], [0.4, 0.0], [0.32, 0.0], [-0.35, 0.13], [-0.24, -0.21], [-0.63, 0.6], [0.15, 0.32], [0.61, 0.61], [-0.36, 0.1]].map {|e| Complex(*e)}, dtype: dtype)
372
+ right_true = NMatrix.new([4,3], [[ -0.22, 0.51], [ -0.37, -0.32], [ -0.53, 0.11], [ 0.15, 0.38], [ 0.31, 0.31], [ 0.09, -0.57], [ 0.18, -0.39], [ 0.38, -0.39], [ 0.53, 0.24], [ 0.49, 0.28], [ -0.47, -0.25], [ -0.15, 0.19]].map {|e| Complex *e} , dtype: dtype)
373
+
374
+ s = NMatrix.new([3,1], 0, dtype: dtype)
375
+ u = NMatrix.new([4,4], 0, dtype: dtype)
376
+ ldu = 4
377
+ vt = NMatrix.new([3,3], 0, dtype: dtype)
378
+ ldvt= 3
379
+ else
380
+ a = NMatrix.new([4,3], dtype: dtype)
381
+ end
382
+ s = NMatrix.new([5,1], 0, dtype: dtype)
383
+ u = NMatrix.new([5,5], 0, dtype: dtype)
384
+ ldu = 5
385
+ vt = NMatrix.new([6,6], 0, dtype: dtype)
386
+ ldvt= 6
387
+ err = case dtype
388
+ when :float32, :complex64
389
+ 1e-6
390
+ when :float64, :complex128
391
+ 1e-15
392
+ else
393
+ 1e-64 # FIXME: should be 0, but be_within(0) does not work.
394
+ end
395
+ err = err *5e1
396
+ begin
397
+
398
+ u, s, vt = a.gesdd(500)
399
+
400
+ rescue NotImplementedError => e
401
+ pending e.to_s
402
+ end
403
+ u.should be_within(err).of(left_true)
404
+ #FIXME: Is the next line correct?
405
+ vt[0...right_true.shape[0], 0...right_true.shape[1]-1].should be_within(err).of(right_true[0...right_true.shape[0],0...right_true.shape[1]-1])
406
+ s.transpose.should be_within(err).of(s_true.row(0))
407
+ end
260
408
 
261
409
 
262
410
  it "exposes geev" do