nmatrix 0.1.0 → 0.2.0

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 (78) hide show
  1. checksums.yaml +4 -4
  2. data/ext/nmatrix/data/complex.h +20 -55
  3. data/ext/nmatrix/data/data.cpp +11 -44
  4. data/ext/nmatrix/data/data.h +174 -311
  5. data/ext/nmatrix/data/meta.h +1 -7
  6. data/ext/nmatrix/data/ruby_object.h +3 -85
  7. data/ext/nmatrix/extconf.rb +2 -73
  8. data/ext/nmatrix/math.cpp +170 -813
  9. data/ext/nmatrix/math/asum.h +2 -25
  10. data/ext/nmatrix/math/{inc.h → cblas_enums.h} +11 -22
  11. data/ext/nmatrix/math/cblas_templates_core.h +507 -0
  12. data/ext/nmatrix/math/gemm.h +2 -32
  13. data/ext/nmatrix/math/gemv.h +1 -35
  14. data/ext/nmatrix/math/getrf.h +21 -6
  15. data/ext/nmatrix/math/getrs.h +0 -8
  16. data/ext/nmatrix/math/imax.h +0 -22
  17. data/ext/nmatrix/math/long_dtype.h +0 -3
  18. data/ext/nmatrix/math/math.h +11 -337
  19. data/ext/nmatrix/math/nrm2.h +2 -23
  20. data/ext/nmatrix/math/rot.h +1 -25
  21. data/ext/nmatrix/math/rotg.h +4 -13
  22. data/ext/nmatrix/math/scal.h +0 -22
  23. data/ext/nmatrix/math/trsm.h +0 -55
  24. data/ext/nmatrix/math/util.h +148 -0
  25. data/ext/nmatrix/nmatrix.cpp +0 -14
  26. data/ext/nmatrix/nmatrix.h +92 -84
  27. data/ext/nmatrix/ruby_constants.cpp +0 -2
  28. data/ext/nmatrix/ruby_constants.h +0 -2
  29. data/ext/nmatrix/ruby_nmatrix.c +86 -45
  30. data/ext/nmatrix/storage/dense/dense.cpp +1 -7
  31. data/ext/nmatrix/storage/storage.h +0 -1
  32. data/ext/nmatrix/ttable_helper.rb +0 -6
  33. data/ext/nmatrix/util/io.cpp +1 -1
  34. data/lib/nmatrix.rb +1 -19
  35. data/lib/nmatrix/blas.rb +33 -11
  36. data/lib/nmatrix/io/market.rb +3 -3
  37. data/lib/nmatrix/lapack_core.rb +181 -0
  38. data/lib/nmatrix/lapack_plugin.rb +44 -0
  39. data/lib/nmatrix/math.rb +382 -131
  40. data/lib/nmatrix/monkeys.rb +2 -3
  41. data/lib/nmatrix/nmatrix.rb +166 -13
  42. data/lib/nmatrix/shortcuts.rb +72 -7
  43. data/lib/nmatrix/version.rb +2 -2
  44. data/spec/00_nmatrix_spec.rb +154 -5
  45. data/spec/02_slice_spec.rb +2 -6
  46. data/spec/03_nmatrix_monkeys_spec.rb +7 -1
  47. data/spec/blas_spec.rb +60 -33
  48. data/spec/homogeneous_spec.rb +10 -10
  49. data/spec/lapack_core_spec.rb +482 -0
  50. data/spec/math_spec.rb +436 -52
  51. data/spec/shortcuts_spec.rb +28 -4
  52. data/spec/spec_helper.rb +14 -2
  53. data/spec/utm5940.mtx +83844 -0
  54. metadata +49 -76
  55. data/.gitignore +0 -27
  56. data/.rspec +0 -2
  57. data/.travis.yml +0 -15
  58. data/CONTRIBUTING.md +0 -82
  59. data/Gemfile +0 -2
  60. data/History.txt +0 -677
  61. data/LICENSE.txt +0 -23
  62. data/Manifest.txt +0 -92
  63. data/README.rdoc +0 -150
  64. data/Rakefile +0 -216
  65. data/ext/nmatrix/data/rational.h +0 -440
  66. data/ext/nmatrix/math/geev.h +0 -82
  67. data/ext/nmatrix/math/ger.h +0 -96
  68. data/ext/nmatrix/math/gesdd.h +0 -80
  69. data/ext/nmatrix/math/gesvd.h +0 -78
  70. data/ext/nmatrix/math/getf2.h +0 -86
  71. data/ext/nmatrix/math/getri.h +0 -108
  72. data/ext/nmatrix/math/potrs.h +0 -129
  73. data/ext/nmatrix/math/swap.h +0 -52
  74. data/lib/nmatrix/lapack.rb +0 -240
  75. data/nmatrix.gemspec +0 -55
  76. data/scripts/mac-brew-gcc.sh +0 -50
  77. data/scripts/mac-mavericks-brew-gcc.sh +0 -22
  78. data/spec/lapack_spec.rb +0 -459
@@ -1,50 +0,0 @@
1
- #!/bin/bash
2
-
3
- # Script will not work for GCC 4.8 or 4.9. For those, please see
4
- # mac-mavericks-brew-gcc.sh
5
-
6
- VERSION="4.7.2" # Script should also work with GCC 4.7.1.
7
- PREFIX="/usr/gcc-${VERSION}"
8
- LANGUAGES="c,c++,fortran"
9
- MAKE="make -j 4"
10
-
11
- brew-path() { brew info $1 | head -n3 | tail -n1 | cut -d' ' -f1; }
12
-
13
- # Prerequisites
14
-
15
- brew install gmp mpfr libmpc
16
-
17
- # Next, download & install the latest GCC:
18
-
19
- mkdir -p $PREFIX
20
- mkdir temp-gcc
21
- cd temp-gcc
22
- wget ftp://ftp.gnu.org/gnu/gcc/gcc-$VERSION/gcc-$VERSION.tar.gz
23
- tar xfz gcc-$VERSION.tar.gz
24
- rm gcc-$VERSION.tar.gz
25
- cd gcc-$VERSION
26
-
27
- mkdir build
28
- cd build
29
-
30
- # Older versions of brew need brew-path instead of brew --prefix.
31
- ../configure \
32
- --prefix=$PREFIX \
33
- --with-gmp=$(brew --prefix gmp) \
34
- --with-mpfr=$(brew --prefix mpfr) \
35
- --with-mpc=$(brew --prefix libmpc) \
36
- --program-suffix=-$VERSION \
37
- --enable-languages=$LANGUAGES \
38
- --with-system-zlib \
39
- --enable-stage1-checking \
40
- --enable-plugin \
41
- --enable-lto \
42
- --disable-multilib
43
-
44
- $MAKE bootstrap
45
-
46
- make install
47
-
48
- # Uncomment for cleanup …
49
- # cd ../../..
50
- # rm -r temp-gcc
@@ -1,22 +0,0 @@
1
- #!/bin/bash
2
-
3
- brew-path() { brew info $1 | head -n3 | tail -n1 | cut -d' ' -f1; }
4
-
5
- # Try using the following for GCC 4.9:
6
- #
7
- # brew install gmp4 mpfr2 libmpc08 isl011 cloog018
8
- #
9
- #
10
-
11
- brew install gcc49 --enable-fortran
12
- # Source for this is: http://stackoverflow.com/questions/19535422/os-x-10-9-gcc-links-to-clang
13
-
14
-
15
- # You may wish to re-install your Ruby if you're using rbenv. To do
16
- # so, make sure you've installed openssl, readline, and libyaml.
17
- #
18
- # The commands for this are:
19
- #
20
- # CC=gcc-4.8 RUBY_CONFIGURE_OPTS="--with-openssl-dir=`brew --prefix openssl` --with-readline-dir=`brew --prefix readline` --with-gcc=gcc-4.8 --enable-shared" rbenv install --keep 2.0.0-p247
21
- #
22
- #
@@ -1,459 +0,0 @@
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
- # == lapack_spec.rb
24
- #
25
- # Tests for properly exposed LAPACK functions.
26
- #
27
-
28
- require 'spec_helper'
29
-
30
- describe NMatrix::LAPACK do
31
- # where integer math is allowed
32
- [:byte, :int8, :int16, :int32, :int64, :rational32, :rational64, :rational128, :float32, :float64, :complex64, :complex128].each do |dtype|
33
- context dtype do
34
- it "exposes clapack laswp" do
35
- a = NMatrix.new(:dense, [3,4], [1,2,3,4,5,6,7,8,9,10,11,12], dtype)
36
- NMatrix::LAPACK::clapack_laswp(3, a, 4, 0, 3, [2,1,3,0], 1)
37
- b = NMatrix.new(:dense, [3,4], [3,2,4,1,7,6,8,5,11,10,12,9], dtype)
38
- expect(a).to eq(b)
39
- end
40
-
41
- it "exposes NMatrix#permute_columns and #permute_columns! (user-friendly laswp)" do
42
- a = NMatrix.new(:dense, [3,4], [1,2,3,4,5,6,7,8,9,10,11,12], dtype)
43
- b = NMatrix.new(:dense, [3,4], [3,2,4,1,7,6,8,5,11,10,12,9], dtype)
44
- piv = [2,1,3,0]
45
- r = a.permute_columns(piv)
46
- expect(r).not_to eq(a)
47
- expect(r).to eq(b)
48
- a.permute_columns!(piv)
49
- expect(a).to eq(b)
50
- end
51
- end
52
- end
53
-
54
- # where integer math is not allowed
55
- [:rational32, :rational64, :rational128, :float32, :float64, :complex64, :complex128].each do |dtype|
56
- context dtype do
57
-
58
- it "exposes clapack_gesv" do
59
- a = NMatrix[[1.quo(1), 2, 3], [0,1.quo(2),4],[3,3,9]].cast(dtype: dtype)
60
- b = NMatrix[[1.quo(1)],[2],[3]].cast(dtype: dtype)
61
- err = case dtype
62
- when :float32, :complex64
63
- 1e-6
64
- when :float64, :complex128
65
- 1e-8
66
- else
67
- 1e-64
68
- end
69
- expect(NMatrix::LAPACK::clapack_gesv(:row,a.shape[0],b.shape[1],a,a.shape[0],b,b.shape[0])).to be_within(err).of(NMatrix[[-1.quo(2)], [0], [1.quo(2)]].cast(dtype: dtype))
70
- end
71
-
72
-
73
- it "exposes clapack_getrf" do
74
- a = NMatrix.new(3, [4,9,2,3,5,7,8,1,6], dtype: dtype)
75
- NMatrix::LAPACK::clapack_getrf(:row, 3, 3, a, 3)
76
-
77
- # delta varies for different dtypes
78
- err = case dtype
79
- when :float32, :complex64
80
- 1e-6
81
- when :float64, :complex128
82
- 1e-15
83
- else
84
- 1e-64 # FIXME: should be 0, but be_within(0) does not work.
85
- end
86
-
87
- expect(a[0,0]).to eq(9) # 8
88
- expect(a[0,1]).to be_within(err).of(2.quo(9)) # 1
89
- expect(a[0,2]).to be_within(err).of(4.quo(9)) # 6
90
- expect(a[1,0]).to eq(5) # 1.quo(2)
91
- expect(a[1,1]).to be_within(err).of(53.quo(9)) # 17.quo(2)
92
- expect(a[1,2]).to be_within(err).of(7.quo(53)) # -1
93
- expect(a[2,0]).to eq(1) # 3.quo(8)
94
- expect(a[2,1]).to be_within(err).of(52.quo(9))
95
- expect(a[2,2]).to be_within(err).of(360.quo(53))
96
- end
97
-
98
- it "exposes clapack_potrf" do
99
- # first do upper
100
- begin
101
- a = NMatrix.new(:dense, 3, [25,15,-5, 0,18,0, 0,0,11], dtype)
102
- NMatrix::LAPACK::clapack_potrf(:row, :upper, 3, a, 3)
103
- b = NMatrix.new(:dense, 3, [5,3,-1, 0,3,1, 0,0,3], dtype)
104
- expect(a).to eq(b)
105
- rescue NotImplementedError => e
106
- pending e.to_s
107
- end
108
-
109
- # then do lower
110
- a = NMatrix.new(:dense, 3, [25,0,0, 15,18,0,-5,0,11], dtype)
111
- NMatrix::LAPACK::clapack_potrf(:row, :lower, 3, a, 3)
112
- b = NMatrix.new(:dense, 3, [5,0,0, 3,3,0, -1,1,3], dtype)
113
- expect(a).to eq(b)
114
- end
115
-
116
- # Together, these calls are basically xGESV from LAPACK: http://www.netlib.org/lapack/double/dgesv.f
117
- it "exposes clapack_getrs" do
118
- a = NMatrix.new(3, [-2,4,-3,3,-2,1,0,-4,3], dtype: dtype)
119
- ipiv = NMatrix::LAPACK::clapack_getrf(:row, 3, 3, a, 3)
120
- b = NMatrix.new([3,1], [-1, 17, -9], dtype: dtype)
121
-
122
- NMatrix::LAPACK::clapack_getrs(:row, false, 3, 1, a, 3, ipiv, b, 3)
123
-
124
- expect(b[0]).to eq(5)
125
- expect(b[1]).to eq(-15.quo(2))
126
- expect(b[2]).to eq(-13)
127
- end
128
-
129
- it "exposes clapack_getri" do
130
- a = NMatrix.new(:dense, 3, [1,0,4,1,1,6,-3,0,-10], dtype)
131
- ipiv = NMatrix::LAPACK::clapack_getrf(:row, 3, 3, a, 3) # get pivot from getrf, use for getri
132
-
133
- begin
134
- NMatrix::LAPACK::clapack_getri(:row, 3, a, 3, ipiv)
135
-
136
- b = NMatrix.new(:dense, 3, [-5,0,-2,-4,1,-1,1.5,0,0.5], dtype)
137
- expect(a).to eq(b)
138
- rescue NotImplementedError => e
139
- pending e.to_s
140
- end
141
- end
142
-
143
- it "exposes lapack_gesdd" do
144
- if [:float32, :float64].include? dtype
145
- a = NMatrix.new([5,6], %w|8.79 9.93 9.83 5.45 3.16
146
- 6.11 6.91 5.04 -0.27 7.98
147
- -9.15 -7.93 4.86 4.85 3.01
148
- 9.57 1.64 8.83 0.74 5.80
149
- -3.49 4.02 9.80 10.00 4.27
150
- 9.84 0.15 -8.99 -6.02 -5.31|.map(&:to_f), dtype: dtype)
151
- s_true = NMatrix.new([1,5], [27.468732418221848, 22.643185009774697, 8.558388228482576, 5.985723201512133, 2.014899658715756], dtype: dtype)
152
- 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)
153
- #right_true = NMatrix.new([5,6],
154
- # %w|-0.59 0.26 0.36 0.31 0.23
155
- # -0.40 0.24 -0.22 -0.75 -0.36
156
- # -0.03 -0.60 -0.45 0.23 -0.31
157
- # -0.43 0.24 -0.69 0.33 0.16
158
- # -0.47 -0.35 0.39 0.16 -0.52
159
- # 0.29 0.58 -0.02 0.38 -0.65|.map(&:to_f),
160
- # dtype)
161
- 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)
162
- #left_true = NMatrix.new([5,5],
163
- # %w|-0.25 -0.40 -0.69 -0.37 -0.41
164
- # 0.81 0.36 -0.25 -0.37 -0.10
165
- # -0.26 0.70 -0.22 0.39 -0.49
166
- # 0.40 -0.45 0.25 0.43 -0.62
167
- # -0.22 0.14 0.59 -0.63 -0.44|.map(&:to_f),
168
- # dtype)
169
- s = NMatrix.new([5,1], 0, dtype: dtype)
170
- u = NMatrix.new([5,5], 0, dtype: dtype)
171
- ldu = 5
172
- vt = NMatrix.new([6,6], 0, dtype: dtype)
173
- ldvt= 6
174
- elsif [:complex64, :complex128].include? dtype
175
- #http://software.intel.com/sites/products/documentation/doclib/mkl_sa/11/mkl_lapack_examples/cgesvd_ex.c.htm
176
- pending "Example may be wrong"
177
- else
178
- pending "Not implemented for non-LAPACK dtypes"
179
- a = NMatrix.new([4,3], dtype: dtype)
180
- end
181
- err = case dtype
182
- when :float32, :complex64
183
- 1e-6
184
- when :float64, :complex128
185
- 1e-15
186
- else
187
- 1e-64 # FIXME: should be 0, but be_within(0) does not work.
188
- end
189
- err = err *5e1
190
- begin
191
-
192
- info = NMatrix::LAPACK::lapack_gesdd(:a, a.shape[0], a.shape[1], a, a.shape[0], s, u, ldu, vt, ldvt, 500)
193
-
194
- rescue NotImplementedError => e
195
- pending e.to_s
196
- end
197
-
198
- expect(u).to be_within(err).of(left_true)
199
- #FIXME: Is the next line correct?
200
- expect(vt[0...right_true.shape[0], 0...right_true.shape[1]-1]).to be_within(err).of(right_true[0...right_true.shape[0],0...right_true.shape[1]-1])
201
- expect(s.transpose).to be_within(err).of(s_true.row(0))
202
- end
203
-
204
-
205
- it "exposes lapack_gesvd" do
206
- # http://software.intel.com/sites/products/documentation/doclib/mkl_sa/11/mkl_lapack_examples/dgesvd_ex.c.htm
207
- if [:float32, :float64].include? dtype
208
- a = NMatrix.new([5,6], %w|8.79 9.93 9.83 5.45 3.16
209
- 6.11 6.91 5.04 -0.27 7.98
210
- -9.15 -7.93 4.86 4.85 3.01
211
- 9.57 1.64 8.83 0.74 5.80
212
- -3.49 4.02 9.80 10.00 4.27
213
- 9.84 0.15 -8.99 -6.02 -5.31|.map(&:to_f), dtype: dtype)
214
- s_true = NMatrix.new([1,5], [27.468732418221848, 22.643185009774697, 8.558388228482576, 5.985723201512133, 2.014899658715756], dtype: dtype)
215
- 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)
216
- #right_true = NMatrix.new([5,6],
217
- # %w|-0.59 0.26 0.36 0.31 0.23
218
- # -0.40 0.24 -0.22 -0.75 -0.36
219
- # -0.03 -0.60 -0.45 0.23 -0.31
220
- # -0.43 0.24 -0.69 0.33 0.16
221
- # -0.47 -0.35 0.39 0.16 -0.52
222
- # 0.29 0.58 -0.02 0.38 -0.65|.map(&:to_f),
223
- # dtype)
224
- 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)
225
- #left_true = NMatrix.new([5,5],
226
- # %w|-0.25 -0.40 -0.69 -0.37 -0.41
227
- # 0.81 0.36 -0.25 -0.37 -0.10
228
- # -0.26 0.70 -0.22 0.39 -0.49
229
- # 0.40 -0.45 0.25 0.43 -0.62
230
- # -0.22 0.14 0.59 -0.63 -0.44|.map(&:to_f),
231
- # dtype)
232
- s = NMatrix.new([5,1], 0, dtype: dtype)
233
- u = NMatrix.new([5,5], 0, dtype: dtype)
234
- ldu = 5
235
- vt = NMatrix.new([6,6], 0, dtype: dtype)
236
- ldvt= 6
237
- elsif [:complex64, :complex128].include? dtype
238
- #http://software.intel.com/sites/products/documentation/doclib/mkl_sa/11/mkl_lapack_examples/cgesvd_ex.c.htm
239
- pending "Example may be wrong"
240
- 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)
241
- s_true = NMatrix.new([3,1], [17.63, 11.61, 6.78], dtype: dtype)
242
- 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)
243
- 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)
244
-
245
- s = NMatrix.new([3,1], 0, dtype: dtype)
246
- u = NMatrix.new([4,4], 0, dtype: dtype)
247
- ldu = 4
248
- vt = NMatrix.new([3,3], 0, dtype: dtype)
249
- ldvt= 3
250
- else
251
- a = NMatrix.new([4,3], dtype: dtype)
252
- end
253
- err = case dtype
254
- when :float32, :complex64
255
- 1e-6
256
- when :float64, :complex128
257
- 1e-15
258
- else
259
- 1e-64 # FIXME: should be 0, but be_within(0) does not work.
260
- end
261
- err = err *5e1
262
- begin
263
-
264
- info = NMatrix::LAPACK::lapack_gesvd(:a, :a, a.shape[0], a.shape[1], a, a.shape[0], s, u, ldu, vt, ldvt, 500)
265
-
266
- rescue NotImplementedError => e
267
- pending e.to_s
268
- end
269
-
270
- expect(u).to be_within(err).of(left_true)
271
- #FIXME: Is the next line correct?
272
- expect(vt[0...right_true.shape[0], 0...right_true.shape[1]-1]).to be_within(err).of(right_true[0...right_true.shape[0],0...right_true.shape[1]-1])
273
- expect(s.transpose).to be_within(err).of(s_true.row(0))
274
-
275
- end
276
-
277
- it "exposes the convenience gesvd method" do
278
- # http://software.intel.com/sites/products/documentation/doclib/mkl_sa/11/mkl_lapack_examples/dgesvd_ex.c.htm
279
- if [:float32, :float64].include? dtype
280
- a = NMatrix.new([5,6], %w|8.79 9.93 9.83 5.45 3.16
281
- 6.11 6.91 5.04 -0.27 7.98
282
- -9.15 -7.93 4.86 4.85 3.01
283
- 9.57 1.64 8.83 0.74 5.80
284
- -3.49 4.02 9.80 10.00 4.27
285
- 9.84 0.15 -8.99 -6.02 -5.31|.map(&:to_f), dtype: dtype)
286
- s_true = NMatrix.new([1,5], [27.468732418221848, 22.643185009774697, 8.558388228482576, 5.985723201512133, 2.014899658715756], dtype: dtype)
287
- 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)
288
- #right_true = NMatrix.new([5,6],
289
- # %w|-0.59 0.26 0.36 0.31 0.23
290
- # -0.40 0.24 -0.22 -0.75 -0.36
291
- # -0.03 -0.60 -0.45 0.23 -0.31
292
- # -0.43 0.24 -0.69 0.33 0.16
293
- # -0.47 -0.35 0.39 0.16 -0.52
294
- # 0.29 0.58 -0.02 0.38 -0.65|.map(&:to_f),
295
- # dtype)
296
- 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)
297
- #left_true = NMatrix.new([5,5],
298
- # %w|-0.25 -0.40 -0.69 -0.37 -0.41
299
- # 0.81 0.36 -0.25 -0.37 -0.10
300
- # -0.26 0.70 -0.22 0.39 -0.49
301
- # 0.40 -0.45 0.25 0.43 -0.62
302
- # -0.22 0.14 0.59 -0.63 -0.44|.map(&:to_f),
303
- # dtype)
304
- s = NMatrix.new([5,1], 0, dtype: dtype)
305
- u = NMatrix.new([5,5], 0, dtype: dtype)
306
- ldu = 5
307
- vt = NMatrix.new([6,6], 0, dtype: dtype)
308
- ldvt= 6
309
- elsif [:complex64, :complex128].include? dtype
310
- #http://software.intel.com/sites/products/documentation/doclib/mkl_sa/11/mkl_lapack_examples/cgesvd_ex.c.htm
311
- pending "Example may be wrong"
312
- 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)
313
- s_true = NMatrix.new([3,1], [17.63, 11.61, 6.78], dtype: dtype)
314
- 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)
315
- 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)
316
-
317
- s = NMatrix.new([3,1], 0, dtype: dtype)
318
- u = NMatrix.new([4,4], 0, dtype: dtype)
319
- ldu = 4
320
- vt = NMatrix.new([3,3], 0, dtype: dtype)
321
- ldvt= 3
322
- else
323
- a = NMatrix.new([4,3], dtype: dtype)
324
- end
325
- err = case dtype
326
- when :float32, :complex64
327
- 1e-6
328
- when :float64, :complex128
329
- 1e-15
330
- else
331
- 1e-64 # FIXME: should be 0, but be_within(0) does not work.
332
- end
333
- err = err *5e1
334
- begin
335
- u, s, vt = a.gesvd
336
- rescue NotImplementedError => e
337
- pending e.to_s
338
- end
339
- expect(u).to be_within(err).of(left_true)
340
- #FIXME: Is the next line correct?
341
- expect(vt[0...right_true.shape[0], 0...right_true.shape[1]-1]).to be_within(err).of(right_true[0...right_true.shape[0],0...right_true.shape[1]-1])
342
- expect(s.transpose).to be_within(err).of(s_true.row(0))
343
-
344
- end
345
- it "exposes the convenience gesdd method" do
346
- # http://software.intel.com/sites/products/documentation/doclib/mkl_sa/11/mkl_lapack_examples/dgesvd_ex.c.htm
347
- if [:float32, :float64].include? dtype
348
- a = NMatrix.new([5,6], %w|8.79 9.93 9.83 5.45 3.16
349
- 6.11 6.91 5.04 -0.27 7.98
350
- -9.15 -7.93 4.86 4.85 3.01
351
- 9.57 1.64 8.83 0.74 5.80
352
- -3.49 4.02 9.80 10.00 4.27
353
- 9.84 0.15 -8.99 -6.02 -5.31|.map(&:to_f), dtype: dtype)
354
- s_true = NMatrix.new([1,5], [27.468732418221848, 22.643185009774697, 8.558388228482576, 5.985723201512133, 2.014899658715756], dtype: dtype)
355
- 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)
356
- 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)
357
- u = NMatrix.new([5,5], 0, dtype: dtype)
358
- ldu = 5
359
- vt = NMatrix.new([6,6], 0, dtype: dtype)
360
- ldvt= 6
361
- elsif [:complex64, :complex128].include? dtype
362
- #http://software.intel.com/sites/products/documentation/doclib/mkl_sa/11/mkl_lapack_examples/cgesvd_ex.c.htm
363
- pending "Example may be wrong"
364
- 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)
365
- s_true = NMatrix.new([3,1], [17.63, 11.61, 6.78], dtype: dtype)
366
- 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)
367
- 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)
368
-
369
- s = NMatrix.new([3,1], 0, dtype: dtype)
370
- u = NMatrix.new([4,4], 0, dtype: dtype)
371
- ldu = 4
372
- vt = NMatrix.new([3,3], 0, dtype: dtype)
373
- ldvt= 3
374
- else
375
- a = NMatrix.new([4,3], dtype: dtype)
376
- end
377
- s = NMatrix.new([5,1], 0, dtype: dtype)
378
- u = NMatrix.new([5,5], 0, dtype: dtype)
379
- ldu = 5
380
- vt = NMatrix.new([6,6], 0, dtype: dtype)
381
- ldvt= 6
382
- err = case dtype
383
- when :float32, :complex64
384
- 1e-6
385
- when :float64, :complex128
386
- 1e-15
387
- else
388
- 1e-64 # FIXME: should be 0, but be_within(0) does not work.
389
- end
390
- err = err *5e1
391
- begin
392
-
393
- u, s, vt = a.gesdd(500)
394
-
395
- rescue NotImplementedError => e
396
- pending e.to_s
397
- end
398
- expect(u).to be_within(err).of(left_true)
399
- #FIXME: Is the next line correct?
400
- expect(vt[0...right_true.shape[0], 0...right_true.shape[1]-1]).to be_within(err).of(right_true[0...right_true.shape[0],0...right_true.shape[1]-1])
401
- expect(s.transpose).to be_within(err).of(s_true.row(0))
402
- end
403
-
404
-
405
- it "exposes geev" do
406
- pending("needs rational implementation") if dtype.to_s =~ /rational/
407
- ary = %w|-1.01 0.86 -4.60 3.31 -4.81
408
- 3.98 0.53 -7.04 5.29 3.55
409
- 3.30 8.26 -3.89 8.20 -1.51
410
- 4.43 4.96 -7.66 -7.33 6.18
411
- 7.31 -6.43 -6.16 2.47 5.58|
412
- ary = dtype.to_s =~ /complex/ ? ary.map(&:to_c) : ary.map(&:to_f)
413
-
414
- a = NMatrix.new(:dense, 5, ary, dtype).transpose
415
- lda = 5
416
- n = 5
417
-
418
- wr = NMatrix.new(:dense, [n,1], 0, dtype)
419
- wi = dtype.to_s =~ /complex/ ? nil : NMatrix.new(:dense, [n,1], 0, dtype)
420
- vl = NMatrix.new(:dense, n, 0, dtype)
421
- vr = NMatrix.new(:dense, n, 0, dtype)
422
- ldvr = n
423
- ldvl = n
424
-
425
- info = NMatrix::LAPACK::lapack_geev(:left, :right, n, a.clone, lda, wr.clone, wi.nil? ? nil : wi.clone, vl.clone, ldvl, vr.clone, ldvr, -1)
426
- expect(info).to eq(0)
427
-
428
- info = NMatrix::LAPACK::lapack_geev(:left, :right, n, a, lda, wr, wi, vl, ldvl, vr, ldvr, 2*n)
429
-
430
- # Negate these and we get a correct result:
431
- vr = vr.transpose
432
- vl = vl.transpose
433
-
434
- pending("Need complex example") if dtype.to_s =~ /complex/
435
- vl_true = NMatrix.new(:dense, 5, [0.04, 0.29, 0.13, 0.33, -0.04,
436
- 0.62, 0.0, -0.69, 0.0, -0.56,
437
- -0.04, -0.58, 0.39, 0.07, 0.13,
438
- 0.28, 0.01, 0.02, 0.19, 0.80,
439
- -0.04, 0.34, 0.40, -0.22, -0.18 ], :float64)
440
-
441
- expect(vl.abs).to be_within(1e-2).of(vl_true.abs)
442
- # Not checking vr_true.
443
- # Example from:
444
- # http://software.intel.com/sites/products/documentation/doclib/mkl_sa/11/mkl_lapack_examples/lapacke_dgeev_row.c.htm
445
- #
446
- # This is what the result should look like:
447
- # [
448
- # [0.10806497186422348, 0.16864821314811707, 0.7322341203689575, 0.0, -0.46064677834510803]
449
- # [0.40631288290023804, -0.25900983810424805, -0.02646319754421711, -0.01694658398628235, -0.33770373463630676]
450
- # [0.10235744714736938, -0.5088024139404297, 0.19164878129959106, -0.29256555438041687, -0.3087439239025116]
451
- # [0.39863115549087524, -0.0913335531949997, -0.07901126891374588, -0.07807594537734985, 0.7438457012176514]
452
- # [ 0.5395349860191345, 0.0, -0.29160499572753906, -0.49310219287872314, -0.15852922201156616]
453
- # ]
454
- #
455
-
456
- end
457
- end
458
- end
459
- end