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,1328 @@
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 "bidiagonal.h"
41
+
42
+ /*************************************************************************
43
+ Reduction of a rectangular matrix to bidiagonal form
44
+
45
+ The algorithm reduces the rectangular matrix A to bidiagonal form by
46
+ orthogonal transformations P and Q: A = Q*B*P.
47
+
48
+ Input parameters:
49
+ A - source matrix. array[0..M-1, 0..N-1]
50
+ M - number of rows in matrix A.
51
+ N - number of columns in matrix A.
52
+
53
+ Output parameters:
54
+ A - matrices Q, B, P in compact form (see below).
55
+ TauQ - scalar factors which are used to form matrix Q.
56
+ TauP - scalar factors which are used to form matrix P.
57
+
58
+ The main diagonal and one of the secondary diagonals of matrix A are
59
+ replaced with bidiagonal matrix B. Other elements contain elementary
60
+ reflections which form MxM matrix Q and NxN matrix P, respectively.
61
+
62
+ If M>=N, B is the upper bidiagonal MxN matrix and is stored in the
63
+ corresponding elements of matrix A. Matrix Q is represented as a
64
+ product of elementary reflections Q = H(0)*H(1)*...*H(n-1), where
65
+ H(i) = 1-tau*v*v'. Here tau is a scalar which is stored in TauQ[i], and
66
+ vector v has the following structure: v(0:i-1)=0, v(i)=1, v(i+1:m-1) is
67
+ stored in elements A(i+1:m-1,i). Matrix P is as follows: P =
68
+ G(0)*G(1)*...*G(n-2), where G(i) = 1 - tau*u*u'. Tau is stored in TauP[i],
69
+ u(0:i)=0, u(i+1)=1, u(i+2:n-1) is stored in elements A(i,i+2:n-1).
70
+
71
+ If M<N, B is the lower bidiagonal MxN matrix and is stored in the
72
+ corresponding elements of matrix A. Q = H(0)*H(1)*...*H(m-2), where
73
+ H(i) = 1 - tau*v*v', tau is stored in TauQ, v(0:i)=0, v(i+1)=1, v(i+2:m-1)
74
+ is stored in elements A(i+2:m-1,i). P = G(0)*G(1)*...*G(m-1),
75
+ G(i) = 1-tau*u*u', tau is stored in TauP, u(0:i-1)=0, u(i)=1, u(i+1:n-1)
76
+ is stored in A(i,i+1:n-1).
77
+
78
+ EXAMPLE:
79
+
80
+ m=6, n=5 (m > n): m=5, n=6 (m < n):
81
+
82
+ ( d e u1 u1 u1 ) ( d u1 u1 u1 u1 u1 )
83
+ ( v1 d e u2 u2 ) ( e d u2 u2 u2 u2 )
84
+ ( v1 v2 d e u3 ) ( v1 e d u3 u3 u3 )
85
+ ( v1 v2 v3 d e ) ( v1 v2 e d u4 u4 )
86
+ ( v1 v2 v3 v4 d ) ( v1 v2 v3 e d u5 )
87
+ ( v1 v2 v3 v4 v5 )
88
+
89
+ Here vi and ui are vectors which form H(i) and G(i), and d and e -
90
+ are the diagonal and off-diagonal elements of matrix B.
91
+ *************************************************************************/
92
+ void rmatrixbd(ap::real_2d_array& a,
93
+ int m,
94
+ int n,
95
+ ap::real_1d_array& tauq,
96
+ ap::real_1d_array& taup)
97
+ {
98
+ ap::real_1d_array work;
99
+ ap::real_1d_array t;
100
+ int minmn;
101
+ int maxmn;
102
+ int i;
103
+ int j;
104
+ double ltau;
105
+
106
+
107
+ //
108
+ // Prepare
109
+ //
110
+ if( n<=0||m<=0 )
111
+ {
112
+ return;
113
+ }
114
+ minmn = ap::minint(m, n);
115
+ maxmn = ap::maxint(m, n);
116
+ work.setbounds(0, maxmn);
117
+ t.setbounds(0, maxmn);
118
+ if( m>=n )
119
+ {
120
+ tauq.setbounds(0, n-1);
121
+ taup.setbounds(0, n-1);
122
+ }
123
+ else
124
+ {
125
+ tauq.setbounds(0, m-1);
126
+ taup.setbounds(0, m-1);
127
+ }
128
+ if( m>=n )
129
+ {
130
+
131
+ //
132
+ // Reduce to upper bidiagonal form
133
+ //
134
+ for(i = 0; i <= n-1; i++)
135
+ {
136
+
137
+ //
138
+ // Generate elementary reflector H(i) to annihilate A(i+1:m-1,i)
139
+ //
140
+ ap::vmove(t.getvector(1, m-i), a.getcolumn(i, i, m-1));
141
+ generatereflection(t, m-i, ltau);
142
+ tauq(i) = ltau;
143
+ ap::vmove(a.getcolumn(i, i, m-1), t.getvector(1, m-i));
144
+ t(1) = 1;
145
+
146
+ //
147
+ // Apply H(i) to A(i:m-1,i+1:n-1) from the left
148
+ //
149
+ applyreflectionfromtheleft(a, ltau, t, i, m-1, i+1, n-1, work);
150
+ if( i<n-1 )
151
+ {
152
+
153
+ //
154
+ // Generate elementary reflector G(i) to annihilate
155
+ // A(i,i+2:n-1)
156
+ //
157
+ ap::vmove(&t(1), &a(i, i+1), ap::vlen(1,n-i-1));
158
+ generatereflection(t, n-1-i, ltau);
159
+ taup(i) = ltau;
160
+ ap::vmove(&a(i, i+1), &t(1), ap::vlen(i+1,n-1));
161
+ t(1) = 1;
162
+
163
+ //
164
+ // Apply G(i) to A(i+1:m-1,i+1:n-1) from the right
165
+ //
166
+ applyreflectionfromtheright(a, ltau, t, i+1, m-1, i+1, n-1, work);
167
+ }
168
+ else
169
+ {
170
+ taup(i) = 0;
171
+ }
172
+ }
173
+ }
174
+ else
175
+ {
176
+
177
+ //
178
+ // Reduce to lower bidiagonal form
179
+ //
180
+ for(i = 0; i <= m-1; i++)
181
+ {
182
+
183
+ //
184
+ // Generate elementary reflector G(i) to annihilate A(i,i+1:n-1)
185
+ //
186
+ ap::vmove(&t(1), &a(i, i), ap::vlen(1,n-i));
187
+ generatereflection(t, n-i, ltau);
188
+ taup(i) = ltau;
189
+ ap::vmove(&a(i, i), &t(1), ap::vlen(i,n-1));
190
+ t(1) = 1;
191
+
192
+ //
193
+ // Apply G(i) to A(i+1:m-1,i:n-1) from the right
194
+ //
195
+ applyreflectionfromtheright(a, ltau, t, i+1, m-1, i, n-1, work);
196
+ if( i<m-1 )
197
+ {
198
+
199
+ //
200
+ // Generate elementary reflector H(i) to annihilate
201
+ // A(i+2:m-1,i)
202
+ //
203
+ ap::vmove(t.getvector(1, m-1-i), a.getcolumn(i, i+1, m-1));
204
+ generatereflection(t, m-1-i, ltau);
205
+ tauq(i) = ltau;
206
+ ap::vmove(a.getcolumn(i, i+1, m-1), t.getvector(1, m-1-i));
207
+ t(1) = 1;
208
+
209
+ //
210
+ // Apply H(i) to A(i+1:m-1,i+1:n-1) from the left
211
+ //
212
+ applyreflectionfromtheleft(a, ltau, t, i+1, m-1, i+1, n-1, work);
213
+ }
214
+ else
215
+ {
216
+ tauq(i) = 0;
217
+ }
218
+ }
219
+ }
220
+ }
221
+
222
+
223
+ /*************************************************************************
224
+ Unpacking matrix Q which reduces a matrix to bidiagonal form.
225
+
226
+ Input parameters:
227
+ QP - matrices Q and P in compact form.
228
+ Output of ToBidiagonal subroutine.
229
+ M - number of rows in matrix A.
230
+ N - number of columns in matrix A.
231
+ TAUQ - scalar factors which are used to form Q.
232
+ Output of ToBidiagonal subroutine.
233
+ QColumns - required number of columns in matrix Q.
234
+ M>=QColumns>=0.
235
+
236
+ Output parameters:
237
+ Q - first QColumns columns of matrix Q.
238
+ Array[0..M-1, 0..QColumns-1]
239
+ If QColumns=0, the array is not modified.
240
+
241
+ -- ALGLIB --
242
+ Copyright 2005 by Bochkanov Sergey
243
+ *************************************************************************/
244
+ void rmatrixbdunpackq(const ap::real_2d_array& qp,
245
+ int m,
246
+ int n,
247
+ const ap::real_1d_array& tauq,
248
+ int qcolumns,
249
+ ap::real_2d_array& q)
250
+ {
251
+ int i;
252
+ int j;
253
+
254
+ ap::ap_error::make_assertion(qcolumns<=m, "RMatrixBDUnpackQ: QColumns>M!");
255
+ ap::ap_error::make_assertion(qcolumns>=0, "RMatrixBDUnpackQ: QColumns<0!");
256
+ if( m==0||n==0||qcolumns==0 )
257
+ {
258
+ return;
259
+ }
260
+
261
+ //
262
+ // prepare Q
263
+ //
264
+ q.setbounds(0, m-1, 0, qcolumns-1);
265
+ for(i = 0; i <= m-1; i++)
266
+ {
267
+ for(j = 0; j <= qcolumns-1; j++)
268
+ {
269
+ if( i==j )
270
+ {
271
+ q(i,j) = 1;
272
+ }
273
+ else
274
+ {
275
+ q(i,j) = 0;
276
+ }
277
+ }
278
+ }
279
+
280
+ //
281
+ // Calculate
282
+ //
283
+ rmatrixbdmultiplybyq(qp, m, n, tauq, q, m, qcolumns, false, false);
284
+ }
285
+
286
+
287
+ /*************************************************************************
288
+ Multiplication by matrix Q which reduces matrix A to bidiagonal form.
289
+
290
+ The algorithm allows pre- or post-multiply by Q or Q'.
291
+
292
+ Input parameters:
293
+ QP - matrices Q and P in compact form.
294
+ Output of ToBidiagonal subroutine.
295
+ M - number of rows in matrix A.
296
+ N - number of columns in matrix A.
297
+ TAUQ - scalar factors which are used to form Q.
298
+ Output of ToBidiagonal subroutine.
299
+ Z - multiplied matrix.
300
+ array[0..ZRows-1,0..ZColumns-1]
301
+ ZRows - number of rows in matrix Z. If FromTheRight=False,
302
+ ZRows=M, otherwise ZRows can be arbitrary.
303
+ ZColumns - number of columns in matrix Z. If FromTheRight=True,
304
+ ZColumns=M, otherwise ZColumns can be arbitrary.
305
+ FromTheRight - pre- or post-multiply.
306
+ DoTranspose - multiply by Q or Q'.
307
+
308
+ Output parameters:
309
+ Z - product of Z and Q.
310
+ Array[0..ZRows-1,0..ZColumns-1]
311
+ If ZRows=0 or ZColumns=0, the array is not modified.
312
+
313
+ -- ALGLIB --
314
+ Copyright 2005 by Bochkanov Sergey
315
+ *************************************************************************/
316
+ void rmatrixbdmultiplybyq(const ap::real_2d_array& qp,
317
+ int m,
318
+ int n,
319
+ const ap::real_1d_array& tauq,
320
+ ap::real_2d_array& z,
321
+ int zrows,
322
+ int zcolumns,
323
+ bool fromtheright,
324
+ bool dotranspose)
325
+ {
326
+ int i;
327
+ int i1;
328
+ int i2;
329
+ int istep;
330
+ ap::real_1d_array v;
331
+ ap::real_1d_array work;
332
+ int mx;
333
+
334
+ if( m<=0||n<=0||zrows<=0||zcolumns<=0 )
335
+ {
336
+ return;
337
+ }
338
+ ap::ap_error::make_assertion(fromtheright&&zcolumns==m||!fromtheright&&zrows==m, "RMatrixBDMultiplyByQ: incorrect Z size!");
339
+
340
+ //
341
+ // init
342
+ //
343
+ mx = ap::maxint(m, n);
344
+ mx = ap::maxint(mx, zrows);
345
+ mx = ap::maxint(mx, zcolumns);
346
+ v.setbounds(0, mx);
347
+ work.setbounds(0, mx);
348
+ if( m>=n )
349
+ {
350
+
351
+ //
352
+ // setup
353
+ //
354
+ if( fromtheright )
355
+ {
356
+ i1 = 0;
357
+ i2 = n-1;
358
+ istep = +1;
359
+ }
360
+ else
361
+ {
362
+ i1 = n-1;
363
+ i2 = 0;
364
+ istep = -1;
365
+ }
366
+ if( dotranspose )
367
+ {
368
+ i = i1;
369
+ i1 = i2;
370
+ i2 = i;
371
+ istep = -istep;
372
+ }
373
+
374
+ //
375
+ // Process
376
+ //
377
+ i = i1;
378
+ do
379
+ {
380
+ ap::vmove(v.getvector(1, m-i), qp.getcolumn(i, i, m-1));
381
+ v(1) = 1;
382
+ if( fromtheright )
383
+ {
384
+ applyreflectionfromtheright(z, tauq(i), v, 0, zrows-1, i, m-1, work);
385
+ }
386
+ else
387
+ {
388
+ applyreflectionfromtheleft(z, tauq(i), v, i, m-1, 0, zcolumns-1, work);
389
+ }
390
+ i = i+istep;
391
+ }
392
+ while(i!=i2+istep);
393
+ }
394
+ else
395
+ {
396
+
397
+ //
398
+ // setup
399
+ //
400
+ if( fromtheright )
401
+ {
402
+ i1 = 0;
403
+ i2 = m-2;
404
+ istep = +1;
405
+ }
406
+ else
407
+ {
408
+ i1 = m-2;
409
+ i2 = 0;
410
+ istep = -1;
411
+ }
412
+ if( dotranspose )
413
+ {
414
+ i = i1;
415
+ i1 = i2;
416
+ i2 = i;
417
+ istep = -istep;
418
+ }
419
+
420
+ //
421
+ // Process
422
+ //
423
+ if( m-1>0 )
424
+ {
425
+ i = i1;
426
+ do
427
+ {
428
+ ap::vmove(v.getvector(1, m-i-1), qp.getcolumn(i, i+1, m-1));
429
+ v(1) = 1;
430
+ if( fromtheright )
431
+ {
432
+ applyreflectionfromtheright(z, tauq(i), v, 0, zrows-1, i+1, m-1, work);
433
+ }
434
+ else
435
+ {
436
+ applyreflectionfromtheleft(z, tauq(i), v, i+1, m-1, 0, zcolumns-1, work);
437
+ }
438
+ i = i+istep;
439
+ }
440
+ while(i!=i2+istep);
441
+ }
442
+ }
443
+ }
444
+
445
+
446
+ /*************************************************************************
447
+ Unpacking matrix P which reduces matrix A to bidiagonal form.
448
+ The subroutine returns transposed matrix P.
449
+
450
+ Input parameters:
451
+ QP - matrices Q and P in compact form.
452
+ Output of ToBidiagonal subroutine.
453
+ M - number of rows in matrix A.
454
+ N - number of columns in matrix A.
455
+ TAUP - scalar factors which are used to form P.
456
+ Output of ToBidiagonal subroutine.
457
+ PTRows - required number of rows of matrix P^T. N >= PTRows >= 0.
458
+
459
+ Output parameters:
460
+ PT - first PTRows columns of matrix P^T
461
+ Array[0..PTRows-1, 0..N-1]
462
+ If PTRows=0, the array is not modified.
463
+
464
+ -- ALGLIB --
465
+ Copyright 2005-2007 by Bochkanov Sergey
466
+ *************************************************************************/
467
+ void rmatrixbdunpackpt(const ap::real_2d_array& qp,
468
+ int m,
469
+ int n,
470
+ const ap::real_1d_array& taup,
471
+ int ptrows,
472
+ ap::real_2d_array& pt)
473
+ {
474
+ int i;
475
+ int j;
476
+
477
+ ap::ap_error::make_assertion(ptrows<=n, "RMatrixBDUnpackPT: PTRows>N!");
478
+ ap::ap_error::make_assertion(ptrows>=0, "RMatrixBDUnpackPT: PTRows<0!");
479
+ if( m==0||n==0||ptrows==0 )
480
+ {
481
+ return;
482
+ }
483
+
484
+ //
485
+ // prepare PT
486
+ //
487
+ pt.setbounds(0, ptrows-1, 0, n-1);
488
+ for(i = 0; i <= ptrows-1; i++)
489
+ {
490
+ for(j = 0; j <= n-1; j++)
491
+ {
492
+ if( i==j )
493
+ {
494
+ pt(i,j) = 1;
495
+ }
496
+ else
497
+ {
498
+ pt(i,j) = 0;
499
+ }
500
+ }
501
+ }
502
+
503
+ //
504
+ // Calculate
505
+ //
506
+ rmatrixbdmultiplybyp(qp, m, n, taup, pt, ptrows, n, true, true);
507
+ }
508
+
509
+
510
+ /*************************************************************************
511
+ Multiplication by matrix P which reduces matrix A to bidiagonal form.
512
+
513
+ The algorithm allows pre- or post-multiply by P or P'.
514
+
515
+ Input parameters:
516
+ QP - matrices Q and P in compact form.
517
+ Output of RMatrixBD subroutine.
518
+ M - number of rows in matrix A.
519
+ N - number of columns in matrix A.
520
+ TAUP - scalar factors which are used to form P.
521
+ Output of RMatrixBD subroutine.
522
+ Z - multiplied matrix.
523
+ Array whose indexes range within [0..ZRows-1,0..ZColumns-1].
524
+ ZRows - number of rows in matrix Z. If FromTheRight=False,
525
+ ZRows=N, otherwise ZRows can be arbitrary.
526
+ ZColumns - number of columns in matrix Z. If FromTheRight=True,
527
+ ZColumns=N, otherwise ZColumns can be arbitrary.
528
+ FromTheRight - pre- or post-multiply.
529
+ DoTranspose - multiply by P or P'.
530
+
531
+ Output parameters:
532
+ Z - product of Z and P.
533
+ Array whose indexes range within [0..ZRows-1,0..ZColumns-1].
534
+ If ZRows=0 or ZColumns=0, the array is not modified.
535
+
536
+ -- ALGLIB --
537
+ Copyright 2005-2007 by Bochkanov Sergey
538
+ *************************************************************************/
539
+ void rmatrixbdmultiplybyp(const ap::real_2d_array& qp,
540
+ int m,
541
+ int n,
542
+ const ap::real_1d_array& taup,
543
+ ap::real_2d_array& z,
544
+ int zrows,
545
+ int zcolumns,
546
+ bool fromtheright,
547
+ bool dotranspose)
548
+ {
549
+ int i;
550
+ ap::real_1d_array v;
551
+ ap::real_1d_array work;
552
+ int mx;
553
+ int i1;
554
+ int i2;
555
+ int istep;
556
+
557
+ if( m<=0||n<=0||zrows<=0||zcolumns<=0 )
558
+ {
559
+ return;
560
+ }
561
+ ap::ap_error::make_assertion(fromtheright&&zcolumns==n||!fromtheright&&zrows==n, "RMatrixBDMultiplyByP: incorrect Z size!");
562
+
563
+ //
564
+ // init
565
+ //
566
+ mx = ap::maxint(m, n);
567
+ mx = ap::maxint(mx, zrows);
568
+ mx = ap::maxint(mx, zcolumns);
569
+ v.setbounds(0, mx);
570
+ work.setbounds(0, mx);
571
+ v.setbounds(0, mx);
572
+ work.setbounds(0, mx);
573
+ if( m>=n )
574
+ {
575
+
576
+ //
577
+ // setup
578
+ //
579
+ if( fromtheright )
580
+ {
581
+ i1 = n-2;
582
+ i2 = 0;
583
+ istep = -1;
584
+ }
585
+ else
586
+ {
587
+ i1 = 0;
588
+ i2 = n-2;
589
+ istep = +1;
590
+ }
591
+ if( !dotranspose )
592
+ {
593
+ i = i1;
594
+ i1 = i2;
595
+ i2 = i;
596
+ istep = -istep;
597
+ }
598
+
599
+ //
600
+ // Process
601
+ //
602
+ if( n-1>0 )
603
+ {
604
+ i = i1;
605
+ do
606
+ {
607
+ ap::vmove(&v(1), &qp(i, i+1), ap::vlen(1,n-1-i));
608
+ v(1) = 1;
609
+ if( fromtheright )
610
+ {
611
+ applyreflectionfromtheright(z, taup(i), v, 0, zrows-1, i+1, n-1, work);
612
+ }
613
+ else
614
+ {
615
+ applyreflectionfromtheleft(z, taup(i), v, i+1, n-1, 0, zcolumns-1, work);
616
+ }
617
+ i = i+istep;
618
+ }
619
+ while(i!=i2+istep);
620
+ }
621
+ }
622
+ else
623
+ {
624
+
625
+ //
626
+ // setup
627
+ //
628
+ if( fromtheright )
629
+ {
630
+ i1 = m-1;
631
+ i2 = 0;
632
+ istep = -1;
633
+ }
634
+ else
635
+ {
636
+ i1 = 0;
637
+ i2 = m-1;
638
+ istep = +1;
639
+ }
640
+ if( !dotranspose )
641
+ {
642
+ i = i1;
643
+ i1 = i2;
644
+ i2 = i;
645
+ istep = -istep;
646
+ }
647
+
648
+ //
649
+ // Process
650
+ //
651
+ i = i1;
652
+ do
653
+ {
654
+ ap::vmove(&v(1), &qp(i, i), ap::vlen(1,n-i));
655
+ v(1) = 1;
656
+ if( fromtheright )
657
+ {
658
+ applyreflectionfromtheright(z, taup(i), v, 0, zrows-1, i, n-1, work);
659
+ }
660
+ else
661
+ {
662
+ applyreflectionfromtheleft(z, taup(i), v, i, n-1, 0, zcolumns-1, work);
663
+ }
664
+ i = i+istep;
665
+ }
666
+ while(i!=i2+istep);
667
+ }
668
+ }
669
+
670
+
671
+ /*************************************************************************
672
+ Unpacking of the main and secondary diagonals of bidiagonal decomposition
673
+ of matrix A.
674
+
675
+ Input parameters:
676
+ B - output of RMatrixBD subroutine.
677
+ M - number of rows in matrix B.
678
+ N - number of columns in matrix B.
679
+
680
+ Output parameters:
681
+ IsUpper - True, if the matrix is upper bidiagonal.
682
+ otherwise IsUpper is False.
683
+ D - the main diagonal.
684
+ Array whose index ranges within [0..Min(M,N)-1].
685
+ E - the secondary diagonal (upper or lower, depending on
686
+ the value of IsUpper).
687
+ Array index ranges within [0..Min(M,N)-1], the last
688
+ element is not used.
689
+
690
+ -- ALGLIB --
691
+ Copyright 2005-2007 by Bochkanov Sergey
692
+ *************************************************************************/
693
+ void rmatrixbdunpackdiagonals(const ap::real_2d_array& b,
694
+ int m,
695
+ int n,
696
+ bool& isupper,
697
+ ap::real_1d_array& d,
698
+ ap::real_1d_array& e)
699
+ {
700
+ int i;
701
+
702
+ isupper = m>=n;
703
+ if( m<=0||n<=0 )
704
+ {
705
+ return;
706
+ }
707
+ if( isupper )
708
+ {
709
+ d.setbounds(0, n-1);
710
+ e.setbounds(0, n-1);
711
+ for(i = 0; i <= n-2; i++)
712
+ {
713
+ d(i) = b(i,i);
714
+ e(i) = b(i,i+1);
715
+ }
716
+ d(n-1) = b(n-1,n-1);
717
+ }
718
+ else
719
+ {
720
+ d.setbounds(0, m-1);
721
+ e.setbounds(0, m-1);
722
+ for(i = 0; i <= m-2; i++)
723
+ {
724
+ d(i) = b(i,i);
725
+ e(i) = b(i+1,i);
726
+ }
727
+ d(m-1) = b(m-1,m-1);
728
+ }
729
+ }
730
+
731
+
732
+ /*************************************************************************
733
+ Obsolete 1-based subroutine.
734
+ See RMatrixBD for 0-based replacement.
735
+ *************************************************************************/
736
+ void tobidiagonal(ap::real_2d_array& a,
737
+ int m,
738
+ int n,
739
+ ap::real_1d_array& tauq,
740
+ ap::real_1d_array& taup)
741
+ {
742
+ ap::real_1d_array work;
743
+ ap::real_1d_array t;
744
+ int minmn;
745
+ int maxmn;
746
+ int i;
747
+ double ltau;
748
+ int mmip1;
749
+ int nmi;
750
+ int ip1;
751
+ int nmip1;
752
+ int mmi;
753
+
754
+ minmn = ap::minint(m, n);
755
+ maxmn = ap::maxint(m, n);
756
+ work.setbounds(1, maxmn);
757
+ t.setbounds(1, maxmn);
758
+ taup.setbounds(1, minmn);
759
+ tauq.setbounds(1, minmn);
760
+ if( m>=n )
761
+ {
762
+
763
+ //
764
+ // Reduce to upper bidiagonal form
765
+ //
766
+ for(i = 1; i <= n; i++)
767
+ {
768
+
769
+ //
770
+ // Generate elementary reflector H(i) to annihilate A(i+1:m,i)
771
+ //
772
+ mmip1 = m-i+1;
773
+ ap::vmove(t.getvector(1, mmip1), a.getcolumn(i, i, m));
774
+ generatereflection(t, mmip1, ltau);
775
+ tauq(i) = ltau;
776
+ ap::vmove(a.getcolumn(i, i, m), t.getvector(1, mmip1));
777
+ t(1) = 1;
778
+
779
+ //
780
+ // Apply H(i) to A(i:m,i+1:n) from the left
781
+ //
782
+ applyreflectionfromtheleft(a, ltau, t, i, m, i+1, n, work);
783
+ if( i<n )
784
+ {
785
+
786
+ //
787
+ // Generate elementary reflector G(i) to annihilate
788
+ // A(i,i+2:n)
789
+ //
790
+ nmi = n-i;
791
+ ip1 = i+1;
792
+ ap::vmove(&t(1), &a(i, ip1), ap::vlen(1,nmi));
793
+ generatereflection(t, nmi, ltau);
794
+ taup(i) = ltau;
795
+ ap::vmove(&a(i, ip1), &t(1), ap::vlen(ip1,n));
796
+ t(1) = 1;
797
+
798
+ //
799
+ // Apply G(i) to A(i+1:m,i+1:n) from the right
800
+ //
801
+ applyreflectionfromtheright(a, ltau, t, i+1, m, i+1, n, work);
802
+ }
803
+ else
804
+ {
805
+ taup(i) = 0;
806
+ }
807
+ }
808
+ }
809
+ else
810
+ {
811
+
812
+ //
813
+ // Reduce to lower bidiagonal form
814
+ //
815
+ for(i = 1; i <= m; i++)
816
+ {
817
+
818
+ //
819
+ // Generate elementary reflector G(i) to annihilate A(i,i+1:n)
820
+ //
821
+ nmip1 = n-i+1;
822
+ ap::vmove(&t(1), &a(i, i), ap::vlen(1,nmip1));
823
+ generatereflection(t, nmip1, ltau);
824
+ taup(i) = ltau;
825
+ ap::vmove(&a(i, i), &t(1), ap::vlen(i,n));
826
+ t(1) = 1;
827
+
828
+ //
829
+ // Apply G(i) to A(i+1:m,i:n) from the right
830
+ //
831
+ applyreflectionfromtheright(a, ltau, t, i+1, m, i, n, work);
832
+ if( i<m )
833
+ {
834
+
835
+ //
836
+ // Generate elementary reflector H(i) to annihilate
837
+ // A(i+2:m,i)
838
+ //
839
+ mmi = m-i;
840
+ ip1 = i+1;
841
+ ap::vmove(t.getvector(1, mmi), a.getcolumn(i, ip1, m));
842
+ generatereflection(t, mmi, ltau);
843
+ tauq(i) = ltau;
844
+ ap::vmove(a.getcolumn(i, ip1, m), t.getvector(1, mmi));
845
+ t(1) = 1;
846
+
847
+ //
848
+ // Apply H(i) to A(i+1:m,i+1:n) from the left
849
+ //
850
+ applyreflectionfromtheleft(a, ltau, t, i+1, m, i+1, n, work);
851
+ }
852
+ else
853
+ {
854
+ tauq(i) = 0;
855
+ }
856
+ }
857
+ }
858
+ }
859
+
860
+
861
+ /*************************************************************************
862
+ Obsolete 1-based subroutine.
863
+ See RMatrixBDUnpackQ for 0-based replacement.
864
+ *************************************************************************/
865
+ void unpackqfrombidiagonal(const ap::real_2d_array& qp,
866
+ int m,
867
+ int n,
868
+ const ap::real_1d_array& tauq,
869
+ int qcolumns,
870
+ ap::real_2d_array& q)
871
+ {
872
+ int i;
873
+ int j;
874
+ int ip1;
875
+ ap::real_1d_array v;
876
+ ap::real_1d_array work;
877
+ int vm;
878
+
879
+ ap::ap_error::make_assertion(qcolumns<=m, "UnpackQFromBidiagonal: QColumns>M!");
880
+ if( m==0||n==0||qcolumns==0 )
881
+ {
882
+ return;
883
+ }
884
+
885
+ //
886
+ // init
887
+ //
888
+ q.setbounds(1, m, 1, qcolumns);
889
+ v.setbounds(1, m);
890
+ work.setbounds(1, qcolumns);
891
+
892
+ //
893
+ // prepare Q
894
+ //
895
+ for(i = 1; i <= m; i++)
896
+ {
897
+ for(j = 1; j <= qcolumns; j++)
898
+ {
899
+ if( i==j )
900
+ {
901
+ q(i,j) = 1;
902
+ }
903
+ else
904
+ {
905
+ q(i,j) = 0;
906
+ }
907
+ }
908
+ }
909
+ if( m>=n )
910
+ {
911
+ for(i = ap::minint(n, qcolumns); i >= 1; i--)
912
+ {
913
+ vm = m-i+1;
914
+ ap::vmove(v.getvector(1, vm), qp.getcolumn(i, i, m));
915
+ v(1) = 1;
916
+ applyreflectionfromtheleft(q, tauq(i), v, i, m, 1, qcolumns, work);
917
+ }
918
+ }
919
+ else
920
+ {
921
+ for(i = ap::minint(m-1, qcolumns-1); i >= 1; i--)
922
+ {
923
+ vm = m-i;
924
+ ip1 = i+1;
925
+ ap::vmove(v.getvector(1, vm), qp.getcolumn(i, ip1, m));
926
+ v(1) = 1;
927
+ applyreflectionfromtheleft(q, tauq(i), v, i+1, m, 1, qcolumns, work);
928
+ }
929
+ }
930
+ }
931
+
932
+
933
+ /*************************************************************************
934
+ Obsolete 1-based subroutine.
935
+ See RMatrixBDMultiplyByQ for 0-based replacement.
936
+ *************************************************************************/
937
+ void multiplybyqfrombidiagonal(const ap::real_2d_array& qp,
938
+ int m,
939
+ int n,
940
+ const ap::real_1d_array& tauq,
941
+ ap::real_2d_array& z,
942
+ int zrows,
943
+ int zcolumns,
944
+ bool fromtheright,
945
+ bool dotranspose)
946
+ {
947
+ int i;
948
+ int ip1;
949
+ int i1;
950
+ int i2;
951
+ int istep;
952
+ ap::real_1d_array v;
953
+ ap::real_1d_array work;
954
+ int vm;
955
+ int mx;
956
+
957
+ if( m<=0||n<=0||zrows<=0||zcolumns<=0 )
958
+ {
959
+ return;
960
+ }
961
+ ap::ap_error::make_assertion(fromtheright&&zcolumns==m||!fromtheright&&zrows==m, "MultiplyByQFromBidiagonal: incorrect Z size!");
962
+
963
+ //
964
+ // init
965
+ //
966
+ mx = ap::maxint(m, n);
967
+ mx = ap::maxint(mx, zrows);
968
+ mx = ap::maxint(mx, zcolumns);
969
+ v.setbounds(1, mx);
970
+ work.setbounds(1, mx);
971
+ if( m>=n )
972
+ {
973
+
974
+ //
975
+ // setup
976
+ //
977
+ if( fromtheright )
978
+ {
979
+ i1 = 1;
980
+ i2 = n;
981
+ istep = +1;
982
+ }
983
+ else
984
+ {
985
+ i1 = n;
986
+ i2 = 1;
987
+ istep = -1;
988
+ }
989
+ if( dotranspose )
990
+ {
991
+ i = i1;
992
+ i1 = i2;
993
+ i2 = i;
994
+ istep = -istep;
995
+ }
996
+
997
+ //
998
+ // Process
999
+ //
1000
+ i = i1;
1001
+ do
1002
+ {
1003
+ vm = m-i+1;
1004
+ ap::vmove(v.getvector(1, vm), qp.getcolumn(i, i, m));
1005
+ v(1) = 1;
1006
+ if( fromtheright )
1007
+ {
1008
+ applyreflectionfromtheright(z, tauq(i), v, 1, zrows, i, m, work);
1009
+ }
1010
+ else
1011
+ {
1012
+ applyreflectionfromtheleft(z, tauq(i), v, i, m, 1, zcolumns, work);
1013
+ }
1014
+ i = i+istep;
1015
+ }
1016
+ while(i!=i2+istep);
1017
+ }
1018
+ else
1019
+ {
1020
+
1021
+ //
1022
+ // setup
1023
+ //
1024
+ if( fromtheright )
1025
+ {
1026
+ i1 = 1;
1027
+ i2 = m-1;
1028
+ istep = +1;
1029
+ }
1030
+ else
1031
+ {
1032
+ i1 = m-1;
1033
+ i2 = 1;
1034
+ istep = -1;
1035
+ }
1036
+ if( dotranspose )
1037
+ {
1038
+ i = i1;
1039
+ i1 = i2;
1040
+ i2 = i;
1041
+ istep = -istep;
1042
+ }
1043
+
1044
+ //
1045
+ // Process
1046
+ //
1047
+ if( m-1>0 )
1048
+ {
1049
+ i = i1;
1050
+ do
1051
+ {
1052
+ vm = m-i;
1053
+ ip1 = i+1;
1054
+ ap::vmove(v.getvector(1, vm), qp.getcolumn(i, ip1, m));
1055
+ v(1) = 1;
1056
+ if( fromtheright )
1057
+ {
1058
+ applyreflectionfromtheright(z, tauq(i), v, 1, zrows, i+1, m, work);
1059
+ }
1060
+ else
1061
+ {
1062
+ applyreflectionfromtheleft(z, tauq(i), v, i+1, m, 1, zcolumns, work);
1063
+ }
1064
+ i = i+istep;
1065
+ }
1066
+ while(i!=i2+istep);
1067
+ }
1068
+ }
1069
+ }
1070
+
1071
+
1072
+ /*************************************************************************
1073
+ Obsolete 1-based subroutine.
1074
+ See RMatrixBDUnpackPT for 0-based replacement.
1075
+ *************************************************************************/
1076
+ void unpackptfrombidiagonal(const ap::real_2d_array& qp,
1077
+ int m,
1078
+ int n,
1079
+ const ap::real_1d_array& taup,
1080
+ int ptrows,
1081
+ ap::real_2d_array& pt)
1082
+ {
1083
+ int i;
1084
+ int j;
1085
+ int ip1;
1086
+ ap::real_1d_array v;
1087
+ ap::real_1d_array work;
1088
+ int vm;
1089
+
1090
+ ap::ap_error::make_assertion(ptrows<=n, "UnpackPTFromBidiagonal: PTRows>N!");
1091
+ if( m==0||n==0||ptrows==0 )
1092
+ {
1093
+ return;
1094
+ }
1095
+
1096
+ //
1097
+ // init
1098
+ //
1099
+ pt.setbounds(1, ptrows, 1, n);
1100
+ v.setbounds(1, n);
1101
+ work.setbounds(1, ptrows);
1102
+
1103
+ //
1104
+ // prepare PT
1105
+ //
1106
+ for(i = 1; i <= ptrows; i++)
1107
+ {
1108
+ for(j = 1; j <= n; j++)
1109
+ {
1110
+ if( i==j )
1111
+ {
1112
+ pt(i,j) = 1;
1113
+ }
1114
+ else
1115
+ {
1116
+ pt(i,j) = 0;
1117
+ }
1118
+ }
1119
+ }
1120
+ if( m>=n )
1121
+ {
1122
+ for(i = ap::minint(n-1, ptrows-1); i >= 1; i--)
1123
+ {
1124
+ vm = n-i;
1125
+ ip1 = i+1;
1126
+ ap::vmove(&v(1), &qp(i, ip1), ap::vlen(1,vm));
1127
+ v(1) = 1;
1128
+ applyreflectionfromtheright(pt, taup(i), v, 1, ptrows, i+1, n, work);
1129
+ }
1130
+ }
1131
+ else
1132
+ {
1133
+ for(i = ap::minint(m, ptrows); i >= 1; i--)
1134
+ {
1135
+ vm = n-i+1;
1136
+ ap::vmove(&v(1), &qp(i, i), ap::vlen(1,vm));
1137
+ v(1) = 1;
1138
+ applyreflectionfromtheright(pt, taup(i), v, 1, ptrows, i, n, work);
1139
+ }
1140
+ }
1141
+ }
1142
+
1143
+
1144
+ /*************************************************************************
1145
+ Obsolete 1-based subroutine.
1146
+ See RMatrixBDMultiplyByP for 0-based replacement.
1147
+ *************************************************************************/
1148
+ void multiplybypfrombidiagonal(const ap::real_2d_array& qp,
1149
+ int m,
1150
+ int n,
1151
+ const ap::real_1d_array& taup,
1152
+ ap::real_2d_array& z,
1153
+ int zrows,
1154
+ int zcolumns,
1155
+ bool fromtheright,
1156
+ bool dotranspose)
1157
+ {
1158
+ int i;
1159
+ int ip1;
1160
+ ap::real_1d_array v;
1161
+ ap::real_1d_array work;
1162
+ int vm;
1163
+ int mx;
1164
+ int i1;
1165
+ int i2;
1166
+ int istep;
1167
+
1168
+ if( m<=0||n<=0||zrows<=0||zcolumns<=0 )
1169
+ {
1170
+ return;
1171
+ }
1172
+ ap::ap_error::make_assertion(fromtheright&&zcolumns==n||!fromtheright&&zrows==n, "MultiplyByQFromBidiagonal: incorrect Z size!");
1173
+
1174
+ //
1175
+ // init
1176
+ //
1177
+ mx = ap::maxint(m, n);
1178
+ mx = ap::maxint(mx, zrows);
1179
+ mx = ap::maxint(mx, zcolumns);
1180
+ v.setbounds(1, mx);
1181
+ work.setbounds(1, mx);
1182
+ v.setbounds(1, mx);
1183
+ work.setbounds(1, mx);
1184
+ if( m>=n )
1185
+ {
1186
+
1187
+ //
1188
+ // setup
1189
+ //
1190
+ if( fromtheright )
1191
+ {
1192
+ i1 = n-1;
1193
+ i2 = 1;
1194
+ istep = -1;
1195
+ }
1196
+ else
1197
+ {
1198
+ i1 = 1;
1199
+ i2 = n-1;
1200
+ istep = +1;
1201
+ }
1202
+ if( !dotranspose )
1203
+ {
1204
+ i = i1;
1205
+ i1 = i2;
1206
+ i2 = i;
1207
+ istep = -istep;
1208
+ }
1209
+
1210
+ //
1211
+ // Process
1212
+ //
1213
+ if( n-1>0 )
1214
+ {
1215
+ i = i1;
1216
+ do
1217
+ {
1218
+ vm = n-i;
1219
+ ip1 = i+1;
1220
+ ap::vmove(&v(1), &qp(i, ip1), ap::vlen(1,vm));
1221
+ v(1) = 1;
1222
+ if( fromtheright )
1223
+ {
1224
+ applyreflectionfromtheright(z, taup(i), v, 1, zrows, i+1, n, work);
1225
+ }
1226
+ else
1227
+ {
1228
+ applyreflectionfromtheleft(z, taup(i), v, i+1, n, 1, zcolumns, work);
1229
+ }
1230
+ i = i+istep;
1231
+ }
1232
+ while(i!=i2+istep);
1233
+ }
1234
+ }
1235
+ else
1236
+ {
1237
+
1238
+ //
1239
+ // setup
1240
+ //
1241
+ if( fromtheright )
1242
+ {
1243
+ i1 = m;
1244
+ i2 = 1;
1245
+ istep = -1;
1246
+ }
1247
+ else
1248
+ {
1249
+ i1 = 1;
1250
+ i2 = m;
1251
+ istep = +1;
1252
+ }
1253
+ if( !dotranspose )
1254
+ {
1255
+ i = i1;
1256
+ i1 = i2;
1257
+ i2 = i;
1258
+ istep = -istep;
1259
+ }
1260
+
1261
+ //
1262
+ // Process
1263
+ //
1264
+ i = i1;
1265
+ do
1266
+ {
1267
+ vm = n-i+1;
1268
+ ap::vmove(&v(1), &qp(i, i), ap::vlen(1,vm));
1269
+ v(1) = 1;
1270
+ if( fromtheright )
1271
+ {
1272
+ applyreflectionfromtheright(z, taup(i), v, 1, zrows, i, n, work);
1273
+ }
1274
+ else
1275
+ {
1276
+ applyreflectionfromtheleft(z, taup(i), v, i, n, 1, zcolumns, work);
1277
+ }
1278
+ i = i+istep;
1279
+ }
1280
+ while(i!=i2+istep);
1281
+ }
1282
+ }
1283
+
1284
+
1285
+ /*************************************************************************
1286
+ Obsolete 1-based subroutine.
1287
+ See RMatrixBDUnpackDiagonals for 0-based replacement.
1288
+ *************************************************************************/
1289
+ void unpackdiagonalsfrombidiagonal(const ap::real_2d_array& b,
1290
+ int m,
1291
+ int n,
1292
+ bool& isupper,
1293
+ ap::real_1d_array& d,
1294
+ ap::real_1d_array& e)
1295
+ {
1296
+ int i;
1297
+
1298
+ isupper = m>=n;
1299
+ if( m==0||n==0 )
1300
+ {
1301
+ return;
1302
+ }
1303
+ if( isupper )
1304
+ {
1305
+ d.setbounds(1, n);
1306
+ e.setbounds(1, n);
1307
+ for(i = 1; i <= n-1; i++)
1308
+ {
1309
+ d(i) = b(i,i);
1310
+ e(i) = b(i,i+1);
1311
+ }
1312
+ d(n) = b(n,n);
1313
+ }
1314
+ else
1315
+ {
1316
+ d.setbounds(1, m);
1317
+ e.setbounds(1, m);
1318
+ for(i = 1; i <= m-1; i++)
1319
+ {
1320
+ d(i) = b(i,i);
1321
+ e(i) = b(i+1,i);
1322
+ }
1323
+ d(m) = b(m,m);
1324
+ }
1325
+ }
1326
+
1327
+
1328
+