alglib 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (255) hide show
  1. data/History.txt +7 -0
  2. data/Manifest.txt +253 -0
  3. data/README.txt +33 -0
  4. data/Rakefile +27 -0
  5. data/ext/Rakefile +24 -0
  6. data/ext/alglib.i +24 -0
  7. data/ext/alglib/Makefile +157 -0
  8. data/ext/alglib/airyf.cpp +372 -0
  9. data/ext/alglib/airyf.h +81 -0
  10. data/ext/alglib/alglib.cpp +8558 -0
  11. data/ext/alglib/alglib_util.cpp +19 -0
  12. data/ext/alglib/alglib_util.h +14 -0
  13. data/ext/alglib/ap.cpp +877 -0
  14. data/ext/alglib/ap.english.html +364 -0
  15. data/ext/alglib/ap.h +666 -0
  16. data/ext/alglib/ap.russian.html +442 -0
  17. data/ext/alglib/apvt.h +754 -0
  18. data/ext/alglib/bdss.cpp +1500 -0
  19. data/ext/alglib/bdss.h +251 -0
  20. data/ext/alglib/bdsvd.cpp +1339 -0
  21. data/ext/alglib/bdsvd.h +164 -0
  22. data/ext/alglib/bessel.cpp +1226 -0
  23. data/ext/alglib/bessel.h +331 -0
  24. data/ext/alglib/betaf.cpp +105 -0
  25. data/ext/alglib/betaf.h +74 -0
  26. data/ext/alglib/bidiagonal.cpp +1328 -0
  27. data/ext/alglib/bidiagonal.h +350 -0
  28. data/ext/alglib/binomialdistr.cpp +247 -0
  29. data/ext/alglib/binomialdistr.h +153 -0
  30. data/ext/alglib/blas.cpp +576 -0
  31. data/ext/alglib/blas.h +132 -0
  32. data/ext/alglib/cblas.cpp +226 -0
  33. data/ext/alglib/cblas.h +57 -0
  34. data/ext/alglib/cdet.cpp +138 -0
  35. data/ext/alglib/cdet.h +92 -0
  36. data/ext/alglib/chebyshev.cpp +216 -0
  37. data/ext/alglib/chebyshev.h +76 -0
  38. data/ext/alglib/chisquaredistr.cpp +157 -0
  39. data/ext/alglib/chisquaredistr.h +144 -0
  40. data/ext/alglib/cholesky.cpp +285 -0
  41. data/ext/alglib/cholesky.h +86 -0
  42. data/ext/alglib/cinverse.cpp +298 -0
  43. data/ext/alglib/cinverse.h +111 -0
  44. data/ext/alglib/clu.cpp +337 -0
  45. data/ext/alglib/clu.h +120 -0
  46. data/ext/alglib/correlation.cpp +280 -0
  47. data/ext/alglib/correlation.h +77 -0
  48. data/ext/alglib/correlationtests.cpp +726 -0
  49. data/ext/alglib/correlationtests.h +134 -0
  50. data/ext/alglib/crcond.cpp +826 -0
  51. data/ext/alglib/crcond.h +148 -0
  52. data/ext/alglib/creflections.cpp +310 -0
  53. data/ext/alglib/creflections.h +165 -0
  54. data/ext/alglib/csolve.cpp +312 -0
  55. data/ext/alglib/csolve.h +99 -0
  56. data/ext/alglib/ctrinverse.cpp +387 -0
  57. data/ext/alglib/ctrinverse.h +98 -0
  58. data/ext/alglib/ctrlinsolve.cpp +297 -0
  59. data/ext/alglib/ctrlinsolve.h +81 -0
  60. data/ext/alglib/dawson.cpp +234 -0
  61. data/ext/alglib/dawson.h +74 -0
  62. data/ext/alglib/descriptivestatistics.cpp +436 -0
  63. data/ext/alglib/descriptivestatistics.h +112 -0
  64. data/ext/alglib/det.cpp +140 -0
  65. data/ext/alglib/det.h +94 -0
  66. data/ext/alglib/dforest.cpp +1819 -0
  67. data/ext/alglib/dforest.h +316 -0
  68. data/ext/alglib/elliptic.cpp +497 -0
  69. data/ext/alglib/elliptic.h +217 -0
  70. data/ext/alglib/estnorm.cpp +429 -0
  71. data/ext/alglib/estnorm.h +107 -0
  72. data/ext/alglib/expintegrals.cpp +422 -0
  73. data/ext/alglib/expintegrals.h +108 -0
  74. data/ext/alglib/faq.english.html +258 -0
  75. data/ext/alglib/faq.russian.html +272 -0
  76. data/ext/alglib/fdistr.cpp +202 -0
  77. data/ext/alglib/fdistr.h +163 -0
  78. data/ext/alglib/fresnel.cpp +211 -0
  79. data/ext/alglib/fresnel.h +91 -0
  80. data/ext/alglib/gammaf.cpp +338 -0
  81. data/ext/alglib/gammaf.h +104 -0
  82. data/ext/alglib/gqgengauss.cpp +235 -0
  83. data/ext/alglib/gqgengauss.h +92 -0
  84. data/ext/alglib/gqgenhermite.cpp +268 -0
  85. data/ext/alglib/gqgenhermite.h +63 -0
  86. data/ext/alglib/gqgenjacobi.cpp +297 -0
  87. data/ext/alglib/gqgenjacobi.h +72 -0
  88. data/ext/alglib/gqgenlaguerre.cpp +265 -0
  89. data/ext/alglib/gqgenlaguerre.h +69 -0
  90. data/ext/alglib/gqgenlegendre.cpp +300 -0
  91. data/ext/alglib/gqgenlegendre.h +62 -0
  92. data/ext/alglib/gqgenlobatto.cpp +305 -0
  93. data/ext/alglib/gqgenlobatto.h +97 -0
  94. data/ext/alglib/gqgenradau.cpp +232 -0
  95. data/ext/alglib/gqgenradau.h +95 -0
  96. data/ext/alglib/hbisinv.cpp +480 -0
  97. data/ext/alglib/hbisinv.h +183 -0
  98. data/ext/alglib/hblas.cpp +228 -0
  99. data/ext/alglib/hblas.h +64 -0
  100. data/ext/alglib/hcholesky.cpp +339 -0
  101. data/ext/alglib/hcholesky.h +91 -0
  102. data/ext/alglib/hermite.cpp +114 -0
  103. data/ext/alglib/hermite.h +49 -0
  104. data/ext/alglib/hessenberg.cpp +370 -0
  105. data/ext/alglib/hessenberg.h +152 -0
  106. data/ext/alglib/hevd.cpp +247 -0
  107. data/ext/alglib/hevd.h +107 -0
  108. data/ext/alglib/hsschur.cpp +1316 -0
  109. data/ext/alglib/hsschur.h +108 -0
  110. data/ext/alglib/htridiagonal.cpp +734 -0
  111. data/ext/alglib/htridiagonal.h +180 -0
  112. data/ext/alglib/ialglib.cpp +6 -0
  113. data/ext/alglib/ialglib.h +9 -0
  114. data/ext/alglib/ibetaf.cpp +960 -0
  115. data/ext/alglib/ibetaf.h +125 -0
  116. data/ext/alglib/igammaf.cpp +430 -0
  117. data/ext/alglib/igammaf.h +157 -0
  118. data/ext/alglib/inv.cpp +274 -0
  119. data/ext/alglib/inv.h +115 -0
  120. data/ext/alglib/inverseupdate.cpp +480 -0
  121. data/ext/alglib/inverseupdate.h +185 -0
  122. data/ext/alglib/jacobianelliptic.cpp +164 -0
  123. data/ext/alglib/jacobianelliptic.h +94 -0
  124. data/ext/alglib/jarquebera.cpp +2271 -0
  125. data/ext/alglib/jarquebera.h +80 -0
  126. data/ext/alglib/kmeans.cpp +356 -0
  127. data/ext/alglib/kmeans.h +76 -0
  128. data/ext/alglib/laguerre.cpp +94 -0
  129. data/ext/alglib/laguerre.h +48 -0
  130. data/ext/alglib/lbfgs.cpp +1167 -0
  131. data/ext/alglib/lbfgs.h +218 -0
  132. data/ext/alglib/lda.cpp +434 -0
  133. data/ext/alglib/lda.h +133 -0
  134. data/ext/alglib/ldlt.cpp +1130 -0
  135. data/ext/alglib/ldlt.h +124 -0
  136. data/ext/alglib/leastsquares.cpp +1252 -0
  137. data/ext/alglib/leastsquares.h +290 -0
  138. data/ext/alglib/legendre.cpp +107 -0
  139. data/ext/alglib/legendre.h +49 -0
  140. data/ext/alglib/linreg.cpp +1185 -0
  141. data/ext/alglib/linreg.h +380 -0
  142. data/ext/alglib/logit.cpp +1523 -0
  143. data/ext/alglib/logit.h +333 -0
  144. data/ext/alglib/lq.cpp +399 -0
  145. data/ext/alglib/lq.h +160 -0
  146. data/ext/alglib/lu.cpp +462 -0
  147. data/ext/alglib/lu.h +119 -0
  148. data/ext/alglib/mannwhitneyu.cpp +4490 -0
  149. data/ext/alglib/mannwhitneyu.h +115 -0
  150. data/ext/alglib/minlm.cpp +918 -0
  151. data/ext/alglib/minlm.h +312 -0
  152. data/ext/alglib/mlpbase.cpp +3375 -0
  153. data/ext/alglib/mlpbase.h +589 -0
  154. data/ext/alglib/mlpe.cpp +1369 -0
  155. data/ext/alglib/mlpe.h +552 -0
  156. data/ext/alglib/mlptrain.cpp +1056 -0
  157. data/ext/alglib/mlptrain.h +283 -0
  158. data/ext/alglib/nearunityunit.cpp +91 -0
  159. data/ext/alglib/nearunityunit.h +17 -0
  160. data/ext/alglib/normaldistr.cpp +377 -0
  161. data/ext/alglib/normaldistr.h +175 -0
  162. data/ext/alglib/nsevd.cpp +1869 -0
  163. data/ext/alglib/nsevd.h +140 -0
  164. data/ext/alglib/pca.cpp +168 -0
  165. data/ext/alglib/pca.h +87 -0
  166. data/ext/alglib/poissondistr.cpp +143 -0
  167. data/ext/alglib/poissondistr.h +130 -0
  168. data/ext/alglib/polinterpolation.cpp +685 -0
  169. data/ext/alglib/polinterpolation.h +206 -0
  170. data/ext/alglib/psif.cpp +173 -0
  171. data/ext/alglib/psif.h +88 -0
  172. data/ext/alglib/qr.cpp +414 -0
  173. data/ext/alglib/qr.h +168 -0
  174. data/ext/alglib/ratinterpolation.cpp +134 -0
  175. data/ext/alglib/ratinterpolation.h +72 -0
  176. data/ext/alglib/rcond.cpp +705 -0
  177. data/ext/alglib/rcond.h +140 -0
  178. data/ext/alglib/reflections.cpp +504 -0
  179. data/ext/alglib/reflections.h +165 -0
  180. data/ext/alglib/rotations.cpp +473 -0
  181. data/ext/alglib/rotations.h +128 -0
  182. data/ext/alglib/rsolve.cpp +221 -0
  183. data/ext/alglib/rsolve.h +99 -0
  184. data/ext/alglib/sbisinv.cpp +217 -0
  185. data/ext/alglib/sbisinv.h +171 -0
  186. data/ext/alglib/sblas.cpp +185 -0
  187. data/ext/alglib/sblas.h +64 -0
  188. data/ext/alglib/schur.cpp +156 -0
  189. data/ext/alglib/schur.h +102 -0
  190. data/ext/alglib/sdet.cpp +193 -0
  191. data/ext/alglib/sdet.h +101 -0
  192. data/ext/alglib/sevd.cpp +116 -0
  193. data/ext/alglib/sevd.h +99 -0
  194. data/ext/alglib/sinverse.cpp +672 -0
  195. data/ext/alglib/sinverse.h +138 -0
  196. data/ext/alglib/spddet.cpp +138 -0
  197. data/ext/alglib/spddet.h +96 -0
  198. data/ext/alglib/spdgevd.cpp +842 -0
  199. data/ext/alglib/spdgevd.h +200 -0
  200. data/ext/alglib/spdinverse.cpp +509 -0
  201. data/ext/alglib/spdinverse.h +122 -0
  202. data/ext/alglib/spdrcond.cpp +421 -0
  203. data/ext/alglib/spdrcond.h +118 -0
  204. data/ext/alglib/spdsolve.cpp +275 -0
  205. data/ext/alglib/spdsolve.h +105 -0
  206. data/ext/alglib/spline2d.cpp +1192 -0
  207. data/ext/alglib/spline2d.h +301 -0
  208. data/ext/alglib/spline3.cpp +1264 -0
  209. data/ext/alglib/spline3.h +290 -0
  210. data/ext/alglib/srcond.cpp +595 -0
  211. data/ext/alglib/srcond.h +127 -0
  212. data/ext/alglib/ssolve.cpp +895 -0
  213. data/ext/alglib/ssolve.h +139 -0
  214. data/ext/alglib/stdafx.h +0 -0
  215. data/ext/alglib/stest.cpp +131 -0
  216. data/ext/alglib/stest.h +94 -0
  217. data/ext/alglib/studenttdistr.cpp +222 -0
  218. data/ext/alglib/studenttdistr.h +115 -0
  219. data/ext/alglib/studentttests.cpp +377 -0
  220. data/ext/alglib/studentttests.h +178 -0
  221. data/ext/alglib/svd.cpp +620 -0
  222. data/ext/alglib/svd.h +126 -0
  223. data/ext/alglib/tdbisinv.cpp +2608 -0
  224. data/ext/alglib/tdbisinv.h +228 -0
  225. data/ext/alglib/tdevd.cpp +1229 -0
  226. data/ext/alglib/tdevd.h +115 -0
  227. data/ext/alglib/tridiagonal.cpp +594 -0
  228. data/ext/alglib/tridiagonal.h +171 -0
  229. data/ext/alglib/trigintegrals.cpp +490 -0
  230. data/ext/alglib/trigintegrals.h +131 -0
  231. data/ext/alglib/trinverse.cpp +345 -0
  232. data/ext/alglib/trinverse.h +98 -0
  233. data/ext/alglib/trlinsolve.cpp +926 -0
  234. data/ext/alglib/trlinsolve.h +73 -0
  235. data/ext/alglib/tsort.cpp +405 -0
  236. data/ext/alglib/tsort.h +54 -0
  237. data/ext/alglib/variancetests.cpp +245 -0
  238. data/ext/alglib/variancetests.h +134 -0
  239. data/ext/alglib/wsr.cpp +6285 -0
  240. data/ext/alglib/wsr.h +96 -0
  241. data/ext/ap.i +97 -0
  242. data/ext/correlation.i +24 -0
  243. data/ext/extconf.rb +6 -0
  244. data/ext/logit.i +89 -0
  245. data/lib/alglib.rb +71 -0
  246. data/lib/alglib/correlation.rb +26 -0
  247. data/lib/alglib/linearregression.rb +63 -0
  248. data/lib/alglib/logit.rb +42 -0
  249. data/test/test_alglib.rb +52 -0
  250. data/test/test_correlation.rb +44 -0
  251. data/test/test_correlationtest.rb +45 -0
  252. data/test/test_linreg.rb +35 -0
  253. data/test/test_logit.rb +43 -0
  254. data/test/test_pca.rb +27 -0
  255. metadata +326 -0
@@ -0,0 +1,247 @@
1
+ /*************************************************************************
2
+ Copyright (c) 2005-2007, Sergey Bochkanov (ALGLIB project).
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are
6
+ met:
7
+
8
+ - Redistributions of source code must retain the above copyright
9
+ notice, this list of conditions and the following disclaimer.
10
+
11
+ - Redistributions in binary form must reproduce the above copyright
12
+ notice, this list of conditions and the following disclaimer listed
13
+ in this license in the documentation and/or other materials
14
+ provided with the distribution.
15
+
16
+ - Neither the name of the copyright holders nor the names of its
17
+ contributors may be used to endorse or promote products derived from
18
+ this software without specific prior written permission.
19
+
20
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24
+ OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
+ *************************************************************************/
32
+
33
+ #include <stdafx.h>
34
+ #include "hevd.h"
35
+
36
+ /*************************************************************************
37
+ Finding the eigenvalues and eigenvectors of a Hermitian matrix
38
+
39
+ The algorithm finds eigen pairs of a Hermitian matrix by reducing it to
40
+ real tridiagonal form and using the QL/QR algorithm.
41
+
42
+ Input parameters:
43
+ A - Hermitian matrix which is given by its upper or lower
44
+ triangular part.
45
+ Array whose indexes range within [0..N-1, 0..N-1].
46
+ N - size of matrix A.
47
+ IsUpper - storage format.
48
+ ZNeeded - flag controlling whether the eigenvectors are needed or
49
+ not. If ZNeeded is equal to:
50
+ * 0, the eigenvectors are not returned;
51
+ * 1, the eigenvectors are returned.
52
+
53
+ Output parameters:
54
+ D - eigenvalues in ascending order.
55
+ Array whose index ranges within [0..N-1].
56
+ Z - if ZNeeded is equal to:
57
+ * 0, Z hasn�t changed;
58
+ * 1, Z contains the eigenvectors.
59
+ Array whose indexes range within [0..N-1, 0..N-1].
60
+ The eigenvectors are stored in the matrix columns.
61
+
62
+ Result:
63
+ True, if the algorithm has converged.
64
+ False, if the algorithm hasn't converged (rare case).
65
+
66
+ Note:
67
+ eigen vectors of Hermitian matrix are defined up to multiplication by
68
+ a complex number L, such as |L|=1.
69
+
70
+ -- ALGLIB --
71
+ Copyright 2005, 23 March 2007 by Bochkanov Sergey
72
+ *************************************************************************/
73
+ bool hmatrixevd(ap::complex_2d_array a,
74
+ int n,
75
+ int zneeded,
76
+ bool isupper,
77
+ ap::real_1d_array& d,
78
+ ap::complex_2d_array& z)
79
+ {
80
+ bool result;
81
+ ap::complex_1d_array tau;
82
+ ap::real_1d_array e;
83
+ ap::real_1d_array work;
84
+ ap::real_2d_array t;
85
+ ap::complex_2d_array q;
86
+ int i;
87
+ int k;
88
+ double v;
89
+
90
+ ap::ap_error::make_assertion(zneeded==0||zneeded==1, "HermitianEVD: incorrect ZNeeded");
91
+
92
+ //
93
+ // Reduce to tridiagonal form
94
+ //
95
+ hmatrixtd(a, n, isupper, tau, d, e);
96
+ if( zneeded==1 )
97
+ {
98
+ hmatrixtdunpackq(a, n, isupper, tau, q);
99
+ zneeded = 2;
100
+ }
101
+
102
+ //
103
+ // TDEVD
104
+ //
105
+ result = smatrixtdevd(d, e, n, zneeded, t);
106
+
107
+ //
108
+ // Eigenvectors are needed
109
+ // Calculate Z = Q*T = Re(Q)*T + i*Im(Q)*T
110
+ //
111
+ if( result&&zneeded!=0 )
112
+ {
113
+ work.setbounds(0, n-1);
114
+ z.setbounds(0, n-1, 0, n-1);
115
+ for(i = 0; i <= n-1; i++)
116
+ {
117
+
118
+ //
119
+ // Calculate real part
120
+ //
121
+ for(k = 0; k <= n-1; k++)
122
+ {
123
+ work(k) = 0;
124
+ }
125
+ for(k = 0; k <= n-1; k++)
126
+ {
127
+ v = q(i,k).x;
128
+ ap::vadd(&work(0), &t(k, 0), ap::vlen(0,n-1), v);
129
+ }
130
+ for(k = 0; k <= n-1; k++)
131
+ {
132
+ z(i,k).x = work(k);
133
+ }
134
+
135
+ //
136
+ // Calculate imaginary part
137
+ //
138
+ for(k = 0; k <= n-1; k++)
139
+ {
140
+ work(k) = 0;
141
+ }
142
+ for(k = 0; k <= n-1; k++)
143
+ {
144
+ v = q(i,k).y;
145
+ ap::vadd(&work(0), &t(k, 0), ap::vlen(0,n-1), v);
146
+ }
147
+ for(k = 0; k <= n-1; k++)
148
+ {
149
+ z(i,k).y = work(k);
150
+ }
151
+ }
152
+ }
153
+ return result;
154
+ }
155
+
156
+
157
+ /*************************************************************************
158
+ Obsolete 1-based subroutine
159
+
160
+ -- ALGLIB --
161
+ Copyright 2005, 23 March 2007 by Bochkanov Sergey
162
+ *************************************************************************/
163
+ bool hermitianevd(ap::complex_2d_array a,
164
+ int n,
165
+ int zneeded,
166
+ bool isupper,
167
+ ap::real_1d_array& d,
168
+ ap::complex_2d_array& z)
169
+ {
170
+ bool result;
171
+ ap::complex_1d_array tau;
172
+ ap::real_1d_array e;
173
+ ap::real_1d_array work;
174
+ ap::real_2d_array t;
175
+ ap::complex_2d_array q;
176
+ int i;
177
+ int k;
178
+ double v;
179
+
180
+ ap::ap_error::make_assertion(zneeded==0||zneeded==1, "HermitianEVD: incorrect ZNeeded");
181
+
182
+ //
183
+ // Reduce to tridiagonal form
184
+ //
185
+ hermitiantotridiagonal(a, n, isupper, tau, d, e);
186
+ if( zneeded==1 )
187
+ {
188
+ unpackqfromhermitiantridiagonal(a, n, isupper, tau, q);
189
+ zneeded = 2;
190
+ }
191
+
192
+ //
193
+ // TDEVD
194
+ //
195
+ result = tridiagonalevd(d, e, n, zneeded, t);
196
+
197
+ //
198
+ // Eigenvectors are needed
199
+ // Calculate Z = Q*T = Re(Q)*T + i*Im(Q)*T
200
+ //
201
+ if( result&&zneeded!=0 )
202
+ {
203
+ work.setbounds(1, n);
204
+ z.setbounds(1, n, 1, n);
205
+ for(i = 1; i <= n; i++)
206
+ {
207
+
208
+ //
209
+ // Calculate real part
210
+ //
211
+ for(k = 1; k <= n; k++)
212
+ {
213
+ work(k) = 0;
214
+ }
215
+ for(k = 1; k <= n; k++)
216
+ {
217
+ v = q(i,k).x;
218
+ ap::vadd(&work(1), &t(k, 1), ap::vlen(1,n), v);
219
+ }
220
+ for(k = 1; k <= n; k++)
221
+ {
222
+ z(i,k).x = work(k);
223
+ }
224
+
225
+ //
226
+ // Calculate imaginary part
227
+ //
228
+ for(k = 1; k <= n; k++)
229
+ {
230
+ work(k) = 0;
231
+ }
232
+ for(k = 1; k <= n; k++)
233
+ {
234
+ v = q(i,k).y;
235
+ ap::vadd(&work(1), &t(k, 1), ap::vlen(1,n), v);
236
+ }
237
+ for(k = 1; k <= n; k++)
238
+ {
239
+ z(i,k).y = work(k);
240
+ }
241
+ }
242
+ }
243
+ return result;
244
+ }
245
+
246
+
247
+
@@ -0,0 +1,107 @@
1
+ /*************************************************************************
2
+ Copyright (c) 2005-2007, Sergey Bochkanov (ALGLIB project).
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are
6
+ met:
7
+
8
+ - Redistributions of source code must retain the above copyright
9
+ notice, this list of conditions and the following disclaimer.
10
+
11
+ - Redistributions in binary form must reproduce the above copyright
12
+ notice, this list of conditions and the following disclaimer listed
13
+ in this license in the documentation and/or other materials
14
+ provided with the distribution.
15
+
16
+ - Neither the name of the copyright holders nor the names of its
17
+ contributors may be used to endorse or promote products derived from
18
+ this software without specific prior written permission.
19
+
20
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24
+ OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
+ *************************************************************************/
32
+
33
+ #ifndef _hevd_h
34
+ #define _hevd_h
35
+
36
+ #include "ap.h"
37
+ #include "ialglib.h"
38
+
39
+ #include "blas.h"
40
+ #include "rotations.h"
41
+ #include "tdevd.h"
42
+ #include "cblas.h"
43
+ #include "creflections.h"
44
+ #include "hblas.h"
45
+ #include "htridiagonal.h"
46
+
47
+
48
+ /*************************************************************************
49
+ Finding the eigenvalues and eigenvectors of a Hermitian matrix
50
+
51
+ The algorithm finds eigen pairs of a Hermitian matrix by reducing it to
52
+ real tridiagonal form and using the QL/QR algorithm.
53
+
54
+ Input parameters:
55
+ A - Hermitian matrix which is given by its upper or lower
56
+ triangular part.
57
+ Array whose indexes range within [0..N-1, 0..N-1].
58
+ N - size of matrix A.
59
+ IsUpper - storage format.
60
+ ZNeeded - flag controlling whether the eigenvectors are needed or
61
+ not. If ZNeeded is equal to:
62
+ * 0, the eigenvectors are not returned;
63
+ * 1, the eigenvectors are returned.
64
+
65
+ Output parameters:
66
+ D - eigenvalues in ascending order.
67
+ Array whose index ranges within [0..N-1].
68
+ Z - if ZNeeded is equal to:
69
+ * 0, Z hasn�t changed;
70
+ * 1, Z contains the eigenvectors.
71
+ Array whose indexes range within [0..N-1, 0..N-1].
72
+ The eigenvectors are stored in the matrix columns.
73
+
74
+ Result:
75
+ True, if the algorithm has converged.
76
+ False, if the algorithm hasn't converged (rare case).
77
+
78
+ Note:
79
+ eigen vectors of Hermitian matrix are defined up to multiplication by
80
+ a complex number L, such as |L|=1.
81
+
82
+ -- ALGLIB --
83
+ Copyright 2005, 23 March 2007 by Bochkanov Sergey
84
+ *************************************************************************/
85
+ bool hmatrixevd(ap::complex_2d_array a,
86
+ int n,
87
+ int zneeded,
88
+ bool isupper,
89
+ ap::real_1d_array& d,
90
+ ap::complex_2d_array& z);
91
+
92
+
93
+ /*************************************************************************
94
+ Obsolete 1-based subroutine
95
+
96
+ -- ALGLIB --
97
+ Copyright 2005, 23 March 2007 by Bochkanov Sergey
98
+ *************************************************************************/
99
+ bool hermitianevd(ap::complex_2d_array a,
100
+ int n,
101
+ int zneeded,
102
+ bool isupper,
103
+ ap::real_1d_array& d,
104
+ ap::complex_2d_array& z);
105
+
106
+
107
+ #endif
@@ -0,0 +1,1316 @@
1
+ /*************************************************************************
2
+ Copyright (c) 1992-2007 The University of Tennessee. All rights reserved.
3
+
4
+ Contributors:
5
+ * Sergey Bochkanov (ALGLIB project). Translation from FORTRAN to
6
+ pseudocode.
7
+
8
+ See subroutines comments for additional copyrights.
9
+
10
+ Redistribution and use in source and binary forms, with or without
11
+ modification, are permitted provided that the following conditions are
12
+ met:
13
+
14
+ - Redistributions of source code must retain the above copyright
15
+ notice, this list of conditions and the following disclaimer.
16
+
17
+ - Redistributions in binary form must reproduce the above copyright
18
+ notice, this list of conditions and the following disclaimer listed
19
+ in this license in the documentation and/or other materials
20
+ provided with the distribution.
21
+
22
+ - Neither the name of the copyright holders nor the names of its
23
+ contributors may be used to endorse or promote products derived from
24
+ this software without specific prior written permission.
25
+
26
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30
+ OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37
+ *************************************************************************/
38
+
39
+ #include <stdafx.h>
40
+ #include "hsschur.h"
41
+
42
+ static void internalauxschur(bool wantt,
43
+ bool wantz,
44
+ int n,
45
+ int ilo,
46
+ int ihi,
47
+ ap::real_2d_array& h,
48
+ ap::real_1d_array& wr,
49
+ ap::real_1d_array& wi,
50
+ int iloz,
51
+ int ihiz,
52
+ ap::real_2d_array& z,
53
+ ap::real_1d_array& work,
54
+ ap::real_1d_array& workv3,
55
+ ap::real_1d_array& workc1,
56
+ ap::real_1d_array& works1,
57
+ int& info);
58
+ static void aux2x2schur(double& a,
59
+ double& b,
60
+ double& c,
61
+ double& d,
62
+ double& rt1r,
63
+ double& rt1i,
64
+ double& rt2r,
65
+ double& rt2i,
66
+ double& cs,
67
+ double& sn);
68
+ static double extschursign(double a, double b);
69
+ static int extschursigntoone(double b);
70
+
71
+ /*************************************************************************
72
+ Subroutine performing the Schur decomposition of a matrix in upper
73
+ Hessenberg form using the QR algorithm with multiple shifts.
74
+
75
+ The source matrix H is represented as S'*H*S = T, where H - matrix in
76
+ upper Hessenberg form, S - orthogonal matrix (Schur vectors), T - upper
77
+ quasi-triangular matrix (with blocks of sizes 1x1 and 2x2 on the main
78
+ diagonal).
79
+
80
+ Input parameters:
81
+ H - matrix to be decomposed.
82
+ Array whose indexes range within [1..N, 1..N].
83
+ N - size of H, N>=0.
84
+
85
+
86
+ Output parameters:
87
+ H � contains the matrix T.
88
+ Array whose indexes range within [1..N, 1..N].
89
+ All elements below the blocks on the main diagonal are equal
90
+ to 0.
91
+ S - contains Schur vectors.
92
+ Array whose indexes range within [1..N, 1..N].
93
+
94
+ Note 1:
95
+ The block structure of matrix T could be easily recognized: since all
96
+ the elements below the blocks are zeros, the elements a[i+1,i] which
97
+ are equal to 0 show the block border.
98
+
99
+ Note 2:
100
+ the algorithm performance depends on the value of the internal
101
+ parameter NS of InternalSchurDecomposition subroutine which defines
102
+ the number of shifts in the QR algorithm (analog of the block width
103
+ in block matrix algorithms in linear algebra). If you require maximum
104
+ performance on your machine, it is recommended to adjust this
105
+ parameter manually.
106
+
107
+ Result:
108
+ True, if the algorithm has converged and the parameters H and S contain
109
+ the result.
110
+ False, if the algorithm has not converged.
111
+
112
+ Algorithm implemented on the basis of subroutine DHSEQR (LAPACK 3.0 library).
113
+ *************************************************************************/
114
+ bool upperhessenbergschurdecomposition(ap::real_2d_array& h,
115
+ int n,
116
+ ap::real_2d_array& s)
117
+ {
118
+ bool result;
119
+ ap::real_1d_array wi;
120
+ ap::real_1d_array wr;
121
+ int info;
122
+
123
+ internalschurdecomposition(h, n, 1, 2, wr, wi, s, info);
124
+ result = info==0;
125
+ return result;
126
+ }
127
+
128
+
129
+ void internalschurdecomposition(ap::real_2d_array& h,
130
+ int n,
131
+ int tneeded,
132
+ int zneeded,
133
+ ap::real_1d_array& wr,
134
+ ap::real_1d_array& wi,
135
+ ap::real_2d_array& z,
136
+ int& info)
137
+ {
138
+ ap::real_1d_array work;
139
+ int i;
140
+ int i1;
141
+ int i2;
142
+ int ierr;
143
+ int ii;
144
+ int itemp;
145
+ int itn;
146
+ int its;
147
+ int j;
148
+ int k;
149
+ int l;
150
+ int maxb;
151
+ int nr;
152
+ int ns;
153
+ int nv;
154
+ double absw;
155
+ double ovfl;
156
+ double smlnum;
157
+ double tau;
158
+ double temp;
159
+ double tst1;
160
+ double ulp;
161
+ double unfl;
162
+ ap::real_2d_array s;
163
+ ap::real_1d_array v;
164
+ ap::real_1d_array vv;
165
+ ap::real_1d_array workc1;
166
+ ap::real_1d_array works1;
167
+ ap::real_1d_array workv3;
168
+ ap::real_1d_array tmpwr;
169
+ ap::real_1d_array tmpwi;
170
+ bool initz;
171
+ bool wantt;
172
+ bool wantz;
173
+ double cnst;
174
+ bool failflag;
175
+ int p1;
176
+ int p2;
177
+ double vt;
178
+
179
+
180
+ //
181
+ // Set the order of the multi-shift QR algorithm to be used.
182
+ // If you want to tune algorithm, change this values
183
+ //
184
+ ns = 12;
185
+ maxb = 50;
186
+
187
+ //
188
+ // Now 2 < NS <= MAXB < NH.
189
+ //
190
+ maxb = ap::maxint(3, maxb);
191
+ ns = ap::minint(maxb, ns);
192
+
193
+ //
194
+ // Initialize
195
+ //
196
+ cnst = 1.5;
197
+ work.setbounds(1, ap::maxint(n, 1));
198
+ s.setbounds(1, ns, 1, ns);
199
+ v.setbounds(1, ns+1);
200
+ vv.setbounds(1, ns+1);
201
+ wr.setbounds(1, ap::maxint(n, 1));
202
+ wi.setbounds(1, ap::maxint(n, 1));
203
+ workc1.setbounds(1, 1);
204
+ works1.setbounds(1, 1);
205
+ workv3.setbounds(1, 3);
206
+ tmpwr.setbounds(1, ap::maxint(n, 1));
207
+ tmpwi.setbounds(1, ap::maxint(n, 1));
208
+ ap::ap_error::make_assertion(n>=0, "InternalSchurDecomposition: incorrect N!");
209
+ ap::ap_error::make_assertion(tneeded==0||tneeded==1, "InternalSchurDecomposition: incorrect TNeeded!");
210
+ ap::ap_error::make_assertion(zneeded==0||zneeded==1||zneeded==2, "InternalSchurDecomposition: incorrect ZNeeded!");
211
+ wantt = tneeded==1;
212
+ initz = zneeded==2;
213
+ wantz = zneeded!=0;
214
+ info = 0;
215
+
216
+ //
217
+ // Initialize Z, if necessary
218
+ //
219
+ if( initz )
220
+ {
221
+ z.setbounds(1, n, 1, n);
222
+ for(i = 1; i <= n; i++)
223
+ {
224
+ for(j = 1; j <= n; j++)
225
+ {
226
+ if( i==j )
227
+ {
228
+ z(i,j) = 1;
229
+ }
230
+ else
231
+ {
232
+ z(i,j) = 0;
233
+ }
234
+ }
235
+ }
236
+ }
237
+
238
+ //
239
+ // Quick return if possible
240
+ //
241
+ if( n==0 )
242
+ {
243
+ return;
244
+ }
245
+ if( n==1 )
246
+ {
247
+ wr(1) = h(1,1);
248
+ wi(1) = 0;
249
+ return;
250
+ }
251
+
252
+ //
253
+ // Set rows and columns 1 to N to zero below the first
254
+ // subdiagonal.
255
+ //
256
+ for(j = 1; j <= n-2; j++)
257
+ {
258
+ for(i = j+2; i <= n; i++)
259
+ {
260
+ h(i,j) = 0;
261
+ }
262
+ }
263
+
264
+ //
265
+ // Test if N is sufficiently small
266
+ //
267
+ if( ns<=2||ns>n||maxb>=n )
268
+ {
269
+
270
+ //
271
+ // Use the standard double-shift algorithm
272
+ //
273
+ internalauxschur(wantt, wantz, n, 1, n, h, wr, wi, 1, n, z, work, workv3, workc1, works1, info);
274
+
275
+ //
276
+ // fill entries under diagonal blocks of T with zeros
277
+ //
278
+ if( wantt )
279
+ {
280
+ j = 1;
281
+ while(j<=n)
282
+ {
283
+ if( wi(j)==0 )
284
+ {
285
+ for(i = j+1; i <= n; i++)
286
+ {
287
+ h(i,j) = 0;
288
+ }
289
+ j = j+1;
290
+ }
291
+ else
292
+ {
293
+ for(i = j+2; i <= n; i++)
294
+ {
295
+ h(i,j) = 0;
296
+ h(i,j+1) = 0;
297
+ }
298
+ j = j+2;
299
+ }
300
+ }
301
+ }
302
+ return;
303
+ }
304
+ unfl = ap::minrealnumber;
305
+ ovfl = 1/unfl;
306
+ ulp = 2*ap::machineepsilon;
307
+ smlnum = unfl*(n/ulp);
308
+
309
+ //
310
+ // I1 and I2 are the indices of the first row and last column of H
311
+ // to which transformations must be applied. If eigenvalues only are
312
+ // being computed, I1 and I2 are set inside the main loop.
313
+ //
314
+ if( wantt )
315
+ {
316
+ i1 = 1;
317
+ i2 = n;
318
+ }
319
+
320
+ //
321
+ // ITN is the total number of multiple-shift QR iterations allowed.
322
+ //
323
+ itn = 30*n;
324
+
325
+ //
326
+ // The main loop begins here. I is the loop index and decreases from
327
+ // IHI to ILO in steps of at most MAXB. Each iteration of the loop
328
+ // works with the active submatrix in rows and columns L to I.
329
+ // Eigenvalues I+1 to IHI have already converged. Either L = ILO or
330
+ // H(L,L-1) is negligible so that the matrix splits.
331
+ //
332
+ i = n;
333
+ while(true)
334
+ {
335
+ l = 1;
336
+ if( i<1 )
337
+ {
338
+
339
+ //
340
+ // fill entries under diagonal blocks of T with zeros
341
+ //
342
+ if( wantt )
343
+ {
344
+ j = 1;
345
+ while(j<=n)
346
+ {
347
+ if( wi(j)==0 )
348
+ {
349
+ for(i = j+1; i <= n; i++)
350
+ {
351
+ h(i,j) = 0;
352
+ }
353
+ j = j+1;
354
+ }
355
+ else
356
+ {
357
+ for(i = j+2; i <= n; i++)
358
+ {
359
+ h(i,j) = 0;
360
+ h(i,j+1) = 0;
361
+ }
362
+ j = j+2;
363
+ }
364
+ }
365
+ }
366
+
367
+ //
368
+ // Exit
369
+ //
370
+ return;
371
+ }
372
+
373
+ //
374
+ // Perform multiple-shift QR iterations on rows and columns ILO to I
375
+ // until a submatrix of order at most MAXB splits off at the bottom
376
+ // because a subdiagonal element has become negligible.
377
+ //
378
+ failflag = true;
379
+ for(its = 0; its <= itn; its++)
380
+ {
381
+
382
+ //
383
+ // Look for a single small subdiagonal element.
384
+ //
385
+ for(k = i; k >= l+1; k--)
386
+ {
387
+ tst1 = fabs(h(k-1,k-1))+fabs(h(k,k));
388
+ if( tst1==0 )
389
+ {
390
+ tst1 = upperhessenberg1norm(h, l, i, l, i, work);
391
+ }
392
+ if( fabs(h(k,k-1))<=ap::maxreal(ulp*tst1, smlnum) )
393
+ {
394
+ break;
395
+ }
396
+ }
397
+ l = k;
398
+ if( l>1 )
399
+ {
400
+
401
+ //
402
+ // H(L,L-1) is negligible.
403
+ //
404
+ h(l,l-1) = 0;
405
+ }
406
+
407
+ //
408
+ // Exit from loop if a submatrix of order <= MAXB has split off.
409
+ //
410
+ if( l>=i-maxb+1 )
411
+ {
412
+ failflag = false;
413
+ break;
414
+ }
415
+
416
+ //
417
+ // Now the active submatrix is in rows and columns L to I. If
418
+ // eigenvalues only are being computed, only the active submatrix
419
+ // need be transformed.
420
+ //
421
+ if( !wantt )
422
+ {
423
+ i1 = l;
424
+ i2 = i;
425
+ }
426
+ if( its==20||its==30 )
427
+ {
428
+
429
+ //
430
+ // Exceptional shifts.
431
+ //
432
+ for(ii = i-ns+1; ii <= i; ii++)
433
+ {
434
+ wr(ii) = cnst*(fabs(h(ii,ii-1))+fabs(h(ii,ii)));
435
+ wi(ii) = 0;
436
+ }
437
+ }
438
+ else
439
+ {
440
+
441
+ //
442
+ // Use eigenvalues of trailing submatrix of order NS as shifts.
443
+ //
444
+ copymatrix(h, i-ns+1, i, i-ns+1, i, s, 1, ns, 1, ns);
445
+ internalauxschur(false, false, ns, 1, ns, s, tmpwr, tmpwi, 1, ns, z, work, workv3, workc1, works1, ierr);
446
+ for(p1 = 1; p1 <= ns; p1++)
447
+ {
448
+ wr(i-ns+p1) = tmpwr(p1);
449
+ wi(i-ns+p1) = tmpwi(p1);
450
+ }
451
+ if( ierr>0 )
452
+ {
453
+
454
+ //
455
+ // If DLAHQR failed to compute all NS eigenvalues, use the
456
+ // unconverged diagonal elements as the remaining shifts.
457
+ //
458
+ for(ii = 1; ii <= ierr; ii++)
459
+ {
460
+ wr(i-ns+ii) = s(ii,ii);
461
+ wi(i-ns+ii) = 0;
462
+ }
463
+ }
464
+ }
465
+
466
+ //
467
+ // Form the first column of (G-w(1)) (G-w(2)) . . . (G-w(ns))
468
+ // where G is the Hessenberg submatrix H(L:I,L:I) and w is
469
+ // the vector of shifts (stored in WR and WI). The result is
470
+ // stored in the local array V.
471
+ //
472
+ v(1) = 1;
473
+ for(ii = 2; ii <= ns+1; ii++)
474
+ {
475
+ v(ii) = 0;
476
+ }
477
+ nv = 1;
478
+ for(j = i-ns+1; j <= i; j++)
479
+ {
480
+ if( wi(j)>=0 )
481
+ {
482
+ if( wi(j)==0 )
483
+ {
484
+
485
+ //
486
+ // real shift
487
+ //
488
+ p1 = nv+1;
489
+ ap::vmove(&vv(1), &v(1), ap::vlen(1,p1));
490
+ matrixvectormultiply(h, l, l+nv, l, l+nv-1, false, vv, 1, nv, 1.0, v, 1, nv+1, -wr(j));
491
+ nv = nv+1;
492
+ }
493
+ else
494
+ {
495
+ if( wi(j)>0 )
496
+ {
497
+
498
+ //
499
+ // complex conjugate pair of shifts
500
+ //
501
+ p1 = nv+1;
502
+ ap::vmove(&vv(1), &v(1), ap::vlen(1,p1));
503
+ matrixvectormultiply(h, l, l+nv, l, l+nv-1, false, v, 1, nv, 1.0, vv, 1, nv+1, -2*wr(j));
504
+ itemp = vectoridxabsmax(vv, 1, nv+1);
505
+ temp = 1/ap::maxreal(fabs(vv(itemp)), smlnum);
506
+ p1 = nv+1;
507
+ ap::vmul(&vv(1), ap::vlen(1,p1), temp);
508
+ absw = pythag2(wr(j), wi(j));
509
+ temp = temp*absw*absw;
510
+ matrixvectormultiply(h, l, l+nv+1, l, l+nv, false, vv, 1, nv+1, 1.0, v, 1, nv+2, temp);
511
+ nv = nv+2;
512
+ }
513
+ }
514
+
515
+ //
516
+ // Scale V(1:NV) so that max(abs(V(i))) = 1. If V is zero,
517
+ // reset it to the unit vector.
518
+ //
519
+ itemp = vectoridxabsmax(v, 1, nv);
520
+ temp = fabs(v(itemp));
521
+ if( temp==0 )
522
+ {
523
+ v(1) = 1;
524
+ for(ii = 2; ii <= nv; ii++)
525
+ {
526
+ v(ii) = 0;
527
+ }
528
+ }
529
+ else
530
+ {
531
+ temp = ap::maxreal(temp, smlnum);
532
+ vt = 1/temp;
533
+ ap::vmul(&v(1), ap::vlen(1,nv), vt);
534
+ }
535
+ }
536
+ }
537
+
538
+ //
539
+ // Multiple-shift QR step
540
+ //
541
+ for(k = l; k <= i-1; k++)
542
+ {
543
+
544
+ //
545
+ // The first iteration of this loop determines a reflection G
546
+ // from the vector V and applies it from left and right to H,
547
+ // thus creating a nonzero bulge below the subdiagonal.
548
+ //
549
+ // Each subsequent iteration determines a reflection G to
550
+ // restore the Hessenberg form in the (K-1)th column, and thus
551
+ // chases the bulge one step toward the bottom of the active
552
+ // submatrix. NR is the order of G.
553
+ //
554
+ nr = ap::minint(ns+1, i-k+1);
555
+ if( k>l )
556
+ {
557
+ p1 = k-1;
558
+ p2 = k+nr-1;
559
+ ap::vmove(v.getvector(1, nr), h.getcolumn(p1, k, p2));
560
+ }
561
+ generatereflection(v, nr, tau);
562
+ if( k>l )
563
+ {
564
+ h(k,k-1) = v(1);
565
+ for(ii = k+1; ii <= i; ii++)
566
+ {
567
+ h(ii,k-1) = 0;
568
+ }
569
+ }
570
+ v(1) = 1;
571
+
572
+ //
573
+ // Apply G from the left to transform the rows of the matrix in
574
+ // columns K to I2.
575
+ //
576
+ applyreflectionfromtheleft(h, tau, v, k, k+nr-1, k, i2, work);
577
+
578
+ //
579
+ // Apply G from the right to transform the columns of the
580
+ // matrix in rows I1 to min(K+NR,I).
581
+ //
582
+ applyreflectionfromtheright(h, tau, v, i1, ap::minint(k+nr, i), k, k+nr-1, work);
583
+ if( wantz )
584
+ {
585
+
586
+ //
587
+ // Accumulate transformations in the matrix Z
588
+ //
589
+ applyreflectionfromtheright(z, tau, v, 1, n, k, k+nr-1, work);
590
+ }
591
+ }
592
+ }
593
+
594
+ //
595
+ // Failure to converge in remaining number of iterations
596
+ //
597
+ if( failflag )
598
+ {
599
+ info = i;
600
+ return;
601
+ }
602
+
603
+ //
604
+ // A submatrix of order <= MAXB in rows and columns L to I has split
605
+ // off. Use the double-shift QR algorithm to handle it.
606
+ //
607
+ internalauxschur(wantt, wantz, n, l, i, h, wr, wi, 1, n, z, work, workv3, workc1, works1, info);
608
+ if( info>0 )
609
+ {
610
+ return;
611
+ }
612
+
613
+ //
614
+ // Decrement number of remaining iterations, and return to start of
615
+ // the main loop with a new value of I.
616
+ //
617
+ itn = itn-its;
618
+ i = l-1;
619
+ }
620
+ }
621
+
622
+
623
+ static void internalauxschur(bool wantt,
624
+ bool wantz,
625
+ int n,
626
+ int ilo,
627
+ int ihi,
628
+ ap::real_2d_array& h,
629
+ ap::real_1d_array& wr,
630
+ ap::real_1d_array& wi,
631
+ int iloz,
632
+ int ihiz,
633
+ ap::real_2d_array& z,
634
+ ap::real_1d_array& work,
635
+ ap::real_1d_array& workv3,
636
+ ap::real_1d_array& workc1,
637
+ ap::real_1d_array& works1,
638
+ int& info)
639
+ {
640
+ int i;
641
+ int i1;
642
+ int i2;
643
+ int itn;
644
+ int its;
645
+ int j;
646
+ int k;
647
+ int l;
648
+ int m;
649
+ int nh;
650
+ int nr;
651
+ int nz;
652
+ double ave;
653
+ double cs;
654
+ double disc;
655
+ double h00;
656
+ double h10;
657
+ double h11;
658
+ double h12;
659
+ double h21;
660
+ double h22;
661
+ double h33;
662
+ double h33s;
663
+ double h43h34;
664
+ double h44;
665
+ double h44s;
666
+ double ovfl;
667
+ double s;
668
+ double smlnum;
669
+ double sn;
670
+ double sum;
671
+ double t1;
672
+ double t2;
673
+ double t3;
674
+ double tst1;
675
+ double unfl;
676
+ double v1;
677
+ double v2;
678
+ double v3;
679
+ bool failflag;
680
+ double dat1;
681
+ double dat2;
682
+ int p1;
683
+ double him1im1;
684
+ double him1i;
685
+ double hiim1;
686
+ double hii;
687
+ double wrim1;
688
+ double wri;
689
+ double wiim1;
690
+ double wii;
691
+ double ulp;
692
+
693
+ info = 0;
694
+ dat1 = 0.75;
695
+ dat2 = -0.4375;
696
+ ulp = ap::machineepsilon;
697
+
698
+ //
699
+ // Quick return if possible
700
+ //
701
+ if( n==0 )
702
+ {
703
+ return;
704
+ }
705
+ if( ilo==ihi )
706
+ {
707
+ wr(ilo) = h(ilo,ilo);
708
+ wi(ilo) = 0;
709
+ return;
710
+ }
711
+ nh = ihi-ilo+1;
712
+ nz = ihiz-iloz+1;
713
+
714
+ //
715
+ // Set machine-dependent constants for the stopping criterion.
716
+ // If norm(H) <= sqrt(OVFL), overflow should not occur.
717
+ //
718
+ unfl = ap::minrealnumber;
719
+ ovfl = 1/unfl;
720
+ smlnum = unfl*(nh/ulp);
721
+
722
+ //
723
+ // I1 and I2 are the indices of the first row and last column of H
724
+ // to which transformations must be applied. If eigenvalues only are
725
+ // being computed, I1 and I2 are set inside the main loop.
726
+ //
727
+ if( wantt )
728
+ {
729
+ i1 = 1;
730
+ i2 = n;
731
+ }
732
+
733
+ //
734
+ // ITN is the total number of QR iterations allowed.
735
+ //
736
+ itn = 30*nh;
737
+
738
+ //
739
+ // The main loop begins here. I is the loop index and decreases from
740
+ // IHI to ILO in steps of 1 or 2. Each iteration of the loop works
741
+ // with the active submatrix in rows and columns L to I.
742
+ // Eigenvalues I+1 to IHI have already converged. Either L = ILO or
743
+ // H(L,L-1) is negligible so that the matrix splits.
744
+ //
745
+ i = ihi;
746
+ while(true)
747
+ {
748
+ l = ilo;
749
+ if( i<ilo )
750
+ {
751
+ return;
752
+ }
753
+
754
+ //
755
+ // Perform QR iterations on rows and columns ILO to I until a
756
+ // submatrix of order 1 or 2 splits off at the bottom because a
757
+ // subdiagonal element has become negligible.
758
+ //
759
+ failflag = true;
760
+ for(its = 0; its <= itn; its++)
761
+ {
762
+
763
+ //
764
+ // Look for a single small subdiagonal element.
765
+ //
766
+ for(k = i; k >= l+1; k--)
767
+ {
768
+ tst1 = fabs(h(k-1,k-1))+fabs(h(k,k));
769
+ if( tst1==0 )
770
+ {
771
+ tst1 = upperhessenberg1norm(h, l, i, l, i, work);
772
+ }
773
+ if( fabs(h(k,k-1))<=ap::maxreal(ulp*tst1, smlnum) )
774
+ {
775
+ break;
776
+ }
777
+ }
778
+ l = k;
779
+ if( l>ilo )
780
+ {
781
+
782
+ //
783
+ // H(L,L-1) is negligible
784
+ //
785
+ h(l,l-1) = 0;
786
+ }
787
+
788
+ //
789
+ // Exit from loop if a submatrix of order 1 or 2 has split off.
790
+ //
791
+ if( l>=i-1 )
792
+ {
793
+ failflag = false;
794
+ break;
795
+ }
796
+
797
+ //
798
+ // Now the active submatrix is in rows and columns L to I. If
799
+ // eigenvalues only are being computed, only the active submatrix
800
+ // need be transformed.
801
+ //
802
+ if( !wantt )
803
+ {
804
+ i1 = l;
805
+ i2 = i;
806
+ }
807
+ if( its==10||its==20 )
808
+ {
809
+
810
+ //
811
+ // Exceptional shift.
812
+ //
813
+ s = fabs(h(i,i-1))+fabs(h(i-1,i-2));
814
+ h44 = dat1*s+h(i,i);
815
+ h33 = h44;
816
+ h43h34 = dat2*s*s;
817
+ }
818
+ else
819
+ {
820
+
821
+ //
822
+ // Prepare to use Francis' double shift
823
+ // (i.e. 2nd degree generalized Rayleigh quotient)
824
+ //
825
+ h44 = h(i,i);
826
+ h33 = h(i-1,i-1);
827
+ h43h34 = h(i,i-1)*h(i-1,i);
828
+ s = h(i-1,i-2)*h(i-1,i-2);
829
+ disc = (h33-h44)*0.5;
830
+ disc = disc*disc+h43h34;
831
+ if( disc>0 )
832
+ {
833
+
834
+ //
835
+ // Real roots: use Wilkinson's shift twice
836
+ //
837
+ disc = sqrt(disc);
838
+ ave = 0.5*(h33+h44);
839
+ if( fabs(h33)-fabs(h44)>0 )
840
+ {
841
+ h33 = h33*h44-h43h34;
842
+ h44 = h33/(extschursign(disc, ave)+ave);
843
+ }
844
+ else
845
+ {
846
+ h44 = extschursign(disc, ave)+ave;
847
+ }
848
+ h33 = h44;
849
+ h43h34 = 0;
850
+ }
851
+ }
852
+
853
+ //
854
+ // Look for two consecutive small subdiagonal elements.
855
+ //
856
+ for(m = i-2; m >= l; m--)
857
+ {
858
+
859
+ //
860
+ // Determine the effect of starting the double-shift QR
861
+ // iteration at row M, and see if this would make H(M,M-1)
862
+ // negligible.
863
+ //
864
+ h11 = h(m,m);
865
+ h22 = h(m+1,m+1);
866
+ h21 = h(m+1,m);
867
+ h12 = h(m,m+1);
868
+ h44s = h44-h11;
869
+ h33s = h33-h11;
870
+ v1 = (h33s*h44s-h43h34)/h21+h12;
871
+ v2 = h22-h11-h33s-h44s;
872
+ v3 = h(m+2,m+1);
873
+ s = fabs(v1)+fabs(v2)+fabs(v3);
874
+ v1 = v1/s;
875
+ v2 = v2/s;
876
+ v3 = v3/s;
877
+ workv3(1) = v1;
878
+ workv3(2) = v2;
879
+ workv3(3) = v3;
880
+ if( m==l )
881
+ {
882
+ break;
883
+ }
884
+ h00 = h(m-1,m-1);
885
+ h10 = h(m,m-1);
886
+ tst1 = fabs(v1)*(fabs(h00)+fabs(h11)+fabs(h22));
887
+ if( fabs(h10)*(fabs(v2)+fabs(v3))<=ulp*tst1 )
888
+ {
889
+ break;
890
+ }
891
+ }
892
+
893
+ //
894
+ // Double-shift QR step
895
+ //
896
+ for(k = m; k <= i-1; k++)
897
+ {
898
+
899
+ //
900
+ // The first iteration of this loop determines a reflection G
901
+ // from the vector V and applies it from left and right to H,
902
+ // thus creating a nonzero bulge below the subdiagonal.
903
+ //
904
+ // Each subsequent iteration determines a reflection G to
905
+ // restore the Hessenberg form in the (K-1)th column, and thus
906
+ // chases the bulge one step toward the bottom of the active
907
+ // submatrix. NR is the order of G.
908
+ //
909
+ nr = ap::minint(3, i-k+1);
910
+ if( k>m )
911
+ {
912
+ for(p1 = 1; p1 <= nr; p1++)
913
+ {
914
+ workv3(p1) = h(k+p1-1,k-1);
915
+ }
916
+ }
917
+ generatereflection(workv3, nr, t1);
918
+ if( k>m )
919
+ {
920
+ h(k,k-1) = workv3(1);
921
+ h(k+1,k-1) = 0;
922
+ if( k<i-1 )
923
+ {
924
+ h(k+2,k-1) = 0;
925
+ }
926
+ }
927
+ else
928
+ {
929
+ if( m>l )
930
+ {
931
+ h(k,k-1) = -h(k,k-1);
932
+ }
933
+ }
934
+ v2 = workv3(2);
935
+ t2 = t1*v2;
936
+ if( nr==3 )
937
+ {
938
+ v3 = workv3(3);
939
+ t3 = t1*v3;
940
+
941
+ //
942
+ // Apply G from the left to transform the rows of the matrix
943
+ // in columns K to I2.
944
+ //
945
+ for(j = k; j <= i2; j++)
946
+ {
947
+ sum = h(k,j)+v2*h(k+1,j)+v3*h(k+2,j);
948
+ h(k,j) = h(k,j)-sum*t1;
949
+ h(k+1,j) = h(k+1,j)-sum*t2;
950
+ h(k+2,j) = h(k+2,j)-sum*t3;
951
+ }
952
+
953
+ //
954
+ // Apply G from the right to transform the columns of the
955
+ // matrix in rows I1 to min(K+3,I).
956
+ //
957
+ for(j = i1; j <= ap::minint(k+3, i); j++)
958
+ {
959
+ sum = h(j,k)+v2*h(j,k+1)+v3*h(j,k+2);
960
+ h(j,k) = h(j,k)-sum*t1;
961
+ h(j,k+1) = h(j,k+1)-sum*t2;
962
+ h(j,k+2) = h(j,k+2)-sum*t3;
963
+ }
964
+ if( wantz )
965
+ {
966
+
967
+ //
968
+ // Accumulate transformations in the matrix Z
969
+ //
970
+ for(j = iloz; j <= ihiz; j++)
971
+ {
972
+ sum = z(j,k)+v2*z(j,k+1)+v3*z(j,k+2);
973
+ z(j,k) = z(j,k)-sum*t1;
974
+ z(j,k+1) = z(j,k+1)-sum*t2;
975
+ z(j,k+2) = z(j,k+2)-sum*t3;
976
+ }
977
+ }
978
+ }
979
+ else
980
+ {
981
+ if( nr==2 )
982
+ {
983
+
984
+ //
985
+ // Apply G from the left to transform the rows of the matrix
986
+ // in columns K to I2.
987
+ //
988
+ for(j = k; j <= i2; j++)
989
+ {
990
+ sum = h(k,j)+v2*h(k+1,j);
991
+ h(k,j) = h(k,j)-sum*t1;
992
+ h(k+1,j) = h(k+1,j)-sum*t2;
993
+ }
994
+
995
+ //
996
+ // Apply G from the right to transform the columns of the
997
+ // matrix in rows I1 to min(K+3,I).
998
+ //
999
+ for(j = i1; j <= i; j++)
1000
+ {
1001
+ sum = h(j,k)+v2*h(j,k+1);
1002
+ h(j,k) = h(j,k)-sum*t1;
1003
+ h(j,k+1) = h(j,k+1)-sum*t2;
1004
+ }
1005
+ if( wantz )
1006
+ {
1007
+
1008
+ //
1009
+ // Accumulate transformations in the matrix Z
1010
+ //
1011
+ for(j = iloz; j <= ihiz; j++)
1012
+ {
1013
+ sum = z(j,k)+v2*z(j,k+1);
1014
+ z(j,k) = z(j,k)-sum*t1;
1015
+ z(j,k+1) = z(j,k+1)-sum*t2;
1016
+ }
1017
+ }
1018
+ }
1019
+ }
1020
+ }
1021
+ }
1022
+ if( failflag )
1023
+ {
1024
+
1025
+ //
1026
+ // Failure to converge in remaining number of iterations
1027
+ //
1028
+ info = i;
1029
+ return;
1030
+ }
1031
+ if( l==i )
1032
+ {
1033
+
1034
+ //
1035
+ // H(I,I-1) is negligible: one eigenvalue has converged.
1036
+ //
1037
+ wr(i) = h(i,i);
1038
+ wi(i) = 0;
1039
+ }
1040
+ else
1041
+ {
1042
+ if( l==i-1 )
1043
+ {
1044
+
1045
+ //
1046
+ // H(I-1,I-2) is negligible: a pair of eigenvalues have converged.
1047
+ //
1048
+ // Transform the 2-by-2 submatrix to standard Schur form,
1049
+ // and compute and store the eigenvalues.
1050
+ //
1051
+ him1im1 = h(i-1,i-1);
1052
+ him1i = h(i-1,i);
1053
+ hiim1 = h(i,i-1);
1054
+ hii = h(i,i);
1055
+ aux2x2schur(him1im1, him1i, hiim1, hii, wrim1, wiim1, wri, wii, cs, sn);
1056
+ wr(i-1) = wrim1;
1057
+ wi(i-1) = wiim1;
1058
+ wr(i) = wri;
1059
+ wi(i) = wii;
1060
+ h(i-1,i-1) = him1im1;
1061
+ h(i-1,i) = him1i;
1062
+ h(i,i-1) = hiim1;
1063
+ h(i,i) = hii;
1064
+ if( wantt )
1065
+ {
1066
+
1067
+ //
1068
+ // Apply the transformation to the rest of H.
1069
+ //
1070
+ if( i2>i )
1071
+ {
1072
+ workc1(1) = cs;
1073
+ works1(1) = sn;
1074
+ applyrotationsfromtheleft(true, i-1, i, i+1, i2, workc1, works1, h, work);
1075
+ }
1076
+ workc1(1) = cs;
1077
+ works1(1) = sn;
1078
+ applyrotationsfromtheright(true, i1, i-2, i-1, i, workc1, works1, h, work);
1079
+ }
1080
+ if( wantz )
1081
+ {
1082
+
1083
+ //
1084
+ // Apply the transformation to Z.
1085
+ //
1086
+ workc1(1) = cs;
1087
+ works1(1) = sn;
1088
+ applyrotationsfromtheright(true, iloz, iloz+nz-1, i-1, i, workc1, works1, z, work);
1089
+ }
1090
+ }
1091
+ }
1092
+
1093
+ //
1094
+ // Decrement number of remaining iterations, and return to start of
1095
+ // the main loop with new value of I.
1096
+ //
1097
+ itn = itn-its;
1098
+ i = l-1;
1099
+ }
1100
+ }
1101
+
1102
+
1103
+ static void aux2x2schur(double& a,
1104
+ double& b,
1105
+ double& c,
1106
+ double& d,
1107
+ double& rt1r,
1108
+ double& rt1i,
1109
+ double& rt2r,
1110
+ double& rt2i,
1111
+ double& cs,
1112
+ double& sn)
1113
+ {
1114
+ double multpl;
1115
+ double aa;
1116
+ double bb;
1117
+ double bcmax;
1118
+ double bcmis;
1119
+ double cc;
1120
+ double cs1;
1121
+ double dd;
1122
+ double eps;
1123
+ double p;
1124
+ double sab;
1125
+ double sac;
1126
+ double scl;
1127
+ double sigma;
1128
+ double sn1;
1129
+ double tau;
1130
+ double temp;
1131
+ double z;
1132
+
1133
+ multpl = 4.0;
1134
+ eps = ap::machineepsilon;
1135
+ if( c==0 )
1136
+ {
1137
+ cs = 1;
1138
+ sn = 0;
1139
+ }
1140
+ else
1141
+ {
1142
+ if( b==0 )
1143
+ {
1144
+
1145
+ //
1146
+ // Swap rows and columns
1147
+ //
1148
+ cs = 0;
1149
+ sn = 1;
1150
+ temp = d;
1151
+ d = a;
1152
+ a = temp;
1153
+ b = -c;
1154
+ c = 0;
1155
+ }
1156
+ else
1157
+ {
1158
+ if( a-d==0&&extschursigntoone(b)!=extschursigntoone(c) )
1159
+ {
1160
+ cs = 1;
1161
+ sn = 0;
1162
+ }
1163
+ else
1164
+ {
1165
+ temp = a-d;
1166
+ p = 0.5*temp;
1167
+ bcmax = ap::maxreal(fabs(b), fabs(c));
1168
+ bcmis = ap::minreal(fabs(b), fabs(c))*extschursigntoone(b)*extschursigntoone(c);
1169
+ scl = ap::maxreal(fabs(p), bcmax);
1170
+ z = p/scl*p+bcmax/scl*bcmis;
1171
+
1172
+ //
1173
+ // If Z is of the order of the machine accuracy, postpone the
1174
+ // decision on the nature of eigenvalues
1175
+ //
1176
+ if( z>=multpl*eps )
1177
+ {
1178
+
1179
+ //
1180
+ // Real eigenvalues. Compute A and D.
1181
+ //
1182
+ z = p+extschursign(sqrt(scl)*sqrt(z), p);
1183
+ a = d+z;
1184
+ d = d-bcmax/z*bcmis;
1185
+
1186
+ //
1187
+ // Compute B and the rotation matrix
1188
+ //
1189
+ tau = pythag2(c, z);
1190
+ cs = z/tau;
1191
+ sn = c/tau;
1192
+ b = b-c;
1193
+ c = 0;
1194
+ }
1195
+ else
1196
+ {
1197
+
1198
+ //
1199
+ // Complex eigenvalues, or real (almost) equal eigenvalues.
1200
+ // Make diagonal elements equal.
1201
+ //
1202
+ sigma = b+c;
1203
+ tau = pythag2(sigma, temp);
1204
+ cs = sqrt(0.5*(1+fabs(sigma)/tau));
1205
+ sn = -p/(tau*cs)*extschursign(double(1), sigma);
1206
+
1207
+ //
1208
+ // Compute [ AA BB ] = [ A B ] [ CS -SN ]
1209
+ // [ CC DD ] [ C D ] [ SN CS ]
1210
+ //
1211
+ aa = a*cs+b*sn;
1212
+ bb = -a*sn+b*cs;
1213
+ cc = c*cs+d*sn;
1214
+ dd = -c*sn+d*cs;
1215
+
1216
+ //
1217
+ // Compute [ A B ] = [ CS SN ] [ AA BB ]
1218
+ // [ C D ] [-SN CS ] [ CC DD ]
1219
+ //
1220
+ a = aa*cs+cc*sn;
1221
+ b = bb*cs+dd*sn;
1222
+ c = -aa*sn+cc*cs;
1223
+ d = -bb*sn+dd*cs;
1224
+ temp = 0.5*(a+d);
1225
+ a = temp;
1226
+ d = temp;
1227
+ if( c!=0 )
1228
+ {
1229
+ if( b!=0 )
1230
+ {
1231
+ if( extschursigntoone(b)==extschursigntoone(c) )
1232
+ {
1233
+
1234
+ //
1235
+ // Real eigenvalues: reduce to upper triangular form
1236
+ //
1237
+ sab = sqrt(fabs(b));
1238
+ sac = sqrt(fabs(c));
1239
+ p = extschursign(sab*sac, c);
1240
+ tau = 1/sqrt(fabs(b+c));
1241
+ a = temp+p;
1242
+ d = temp-p;
1243
+ b = b-c;
1244
+ c = 0;
1245
+ cs1 = sab*tau;
1246
+ sn1 = sac*tau;
1247
+ temp = cs*cs1-sn*sn1;
1248
+ sn = cs*sn1+sn*cs1;
1249
+ cs = temp;
1250
+ }
1251
+ }
1252
+ else
1253
+ {
1254
+ b = -c;
1255
+ c = 0;
1256
+ temp = cs;
1257
+ cs = -sn;
1258
+ sn = temp;
1259
+ }
1260
+ }
1261
+ }
1262
+ }
1263
+ }
1264
+ }
1265
+
1266
+ //
1267
+ // Store eigenvalues in (RT1R,RT1I) and (RT2R,RT2I).
1268
+ //
1269
+ rt1r = a;
1270
+ rt2r = d;
1271
+ if( c==0 )
1272
+ {
1273
+ rt1i = 0;
1274
+ rt2i = 0;
1275
+ }
1276
+ else
1277
+ {
1278
+ rt1i = sqrt(fabs(b))*sqrt(fabs(c));
1279
+ rt2i = -rt1i;
1280
+ }
1281
+ }
1282
+
1283
+
1284
+ static double extschursign(double a, double b)
1285
+ {
1286
+ double result;
1287
+
1288
+ if( b>=0 )
1289
+ {
1290
+ result = fabs(a);
1291
+ }
1292
+ else
1293
+ {
1294
+ result = -fabs(a);
1295
+ }
1296
+ return result;
1297
+ }
1298
+
1299
+
1300
+ static int extschursigntoone(double b)
1301
+ {
1302
+ int result;
1303
+
1304
+ if( b>=0 )
1305
+ {
1306
+ result = 1;
1307
+ }
1308
+ else
1309
+ {
1310
+ result = -1;
1311
+ }
1312
+ return result;
1313
+ }
1314
+
1315
+
1316
+