Update to 2.0.0 tree from current Fremantle build
[opencv] / 3rdparty / lapack / slaed3.c
1 #include "clapack.h"
2
3 /* Table of constant values */
4
5 static integer c__1 = 1;
6 static real c_b22 = 1.f;
7 static real c_b23 = 0.f;
8
9 /* Subroutine */ int slaed3_(integer *k, integer *n, integer *n1, real *d__, 
10         real *q, integer *ldq, real *rho, real *dlamda, real *q2, integer *
11         indx, integer *ctot, real *w, real *s, integer *info)
12 {
13     /* System generated locals */
14     integer q_dim1, q_offset, i__1, i__2;
15     real r__1;
16
17     /* Builtin functions */
18     double sqrt(doublereal), r_sign(real *, real *);
19
20     /* Local variables */
21     integer i__, j, n2, n12, ii, n23, iq2;
22     real temp;
23     extern doublereal snrm2_(integer *, real *, integer *);
24     extern /* Subroutine */ int sgemm_(char *, char *, integer *, integer *, 
25             integer *, real *, real *, integer *, real *, integer *, real *, 
26             real *, integer *), scopy_(integer *, real *, 
27             integer *, real *, integer *), slaed4_(integer *, integer *, real 
28             *, real *, real *, real *, real *, integer *);
29     extern doublereal slamc3_(real *, real *);
30     extern /* Subroutine */ int xerbla_(char *, integer *), slacpy_(
31             char *, integer *, integer *, real *, integer *, real *, integer *
32 ), slaset_(char *, integer *, integer *, real *, real *, 
33             real *, integer *);
34
35
36 /*  -- LAPACK routine (version 3.1) -- */
37 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
38 /*     November 2006 */
39
40 /*     .. Scalar Arguments .. */
41 /*     .. */
42 /*     .. Array Arguments .. */
43 /*     .. */
44
45 /*  Purpose */
46 /*  ======= */
47
48 /*  SLAED3 finds the roots of the secular equation, as defined by the */
49 /*  values in D, W, and RHO, between 1 and K.  It makes the */
50 /*  appropriate calls to SLAED4 and then updates the eigenvectors by */
51 /*  multiplying the matrix of eigenvectors of the pair of eigensystems */
52 /*  being combined by the matrix of eigenvectors of the K-by-K system */
53 /*  which is solved here. */
54
55 /*  This code makes very mild assumptions about floating point */
56 /*  arithmetic. It will work on machines with a guard digit in */
57 /*  add/subtract, or on those binary machines without guard digits */
58 /*  which subtract like the Cray X-MP, Cray Y-MP, Cray C-90, or Cray-2. */
59 /*  It could conceivably fail on hexadecimal or decimal machines */
60 /*  without guard digits, but we know of none. */
61
62 /*  Arguments */
63 /*  ========= */
64
65 /*  K       (input) INTEGER */
66 /*          The number of terms in the rational function to be solved by */
67 /*          SLAED4.  K >= 0. */
68
69 /*  N       (input) INTEGER */
70 /*          The number of rows and columns in the Q matrix. */
71 /*          N >= K (deflation may result in N>K). */
72
73 /*  N1      (input) INTEGER */
74 /*          The location of the last eigenvalue in the leading submatrix. */
75 /*          min(1,N) <= N1 <= N/2. */
76
77 /*  D       (output) REAL array, dimension (N) */
78 /*          D(I) contains the updated eigenvalues for */
79 /*          1 <= I <= K. */
80
81 /*  Q       (output) REAL array, dimension (LDQ,N) */
82 /*          Initially the first K columns are used as workspace. */
83 /*          On output the columns 1 to K contain */
84 /*          the updated eigenvectors. */
85
86 /*  LDQ     (input) INTEGER */
87 /*          The leading dimension of the array Q.  LDQ >= max(1,N). */
88
89 /*  RHO     (input) REAL */
90 /*          The value of the parameter in the rank one update equation. */
91 /*          RHO >= 0 required. */
92
93 /*  DLAMDA  (input/output) REAL array, dimension (K) */
94 /*          The first K elements of this array contain the old roots */
95 /*          of the deflated updating problem.  These are the poles */
96 /*          of the secular equation. May be changed on output by */
97 /*          having lowest order bit set to zero on Cray X-MP, Cray Y-MP, */
98 /*          Cray-2, or Cray C-90, as described above. */
99
100 /*  Q2      (input) REAL array, dimension (LDQ2, N) */
101 /*          The first K columns of this matrix contain the non-deflated */
102 /*          eigenvectors for the split problem. */
103
104 /*  INDX    (input) INTEGER array, dimension (N) */
105 /*          The permutation used to arrange the columns of the deflated */
106 /*          Q matrix into three groups (see SLAED2). */
107 /*          The rows of the eigenvectors found by SLAED4 must be likewise */
108 /*          permuted before the matrix multiply can take place. */
109
110 /*  CTOT    (input) INTEGER array, dimension (4) */
111 /*          A count of the total number of the various types of columns */
112 /*          in Q, as described in INDX.  The fourth column type is any */
113 /*          column which has been deflated. */
114
115 /*  W       (input/output) REAL array, dimension (K) */
116 /*          The first K elements of this array contain the components */
117 /*          of the deflation-adjusted updating vector. Destroyed on */
118 /*          output. */
119
120 /*  S       (workspace) REAL array, dimension (N1 + 1)*K */
121 /*          Will contain the eigenvectors of the repaired matrix which */
122 /*          will be multiplied by the previously accumulated eigenvectors */
123 /*          to update the system. */
124
125 /*  LDS     (input) INTEGER */
126 /*          The leading dimension of S.  LDS >= max(1,K). */
127
128 /*  INFO    (output) INTEGER */
129 /*          = 0:  successful exit. */
130 /*          < 0:  if INFO = -i, the i-th argument had an illegal value. */
131 /*          > 0:  if INFO = 1, an eigenvalue did not converge */
132
133 /*  Further Details */
134 /*  =============== */
135
136 /*  Based on contributions by */
137 /*     Jeff Rutter, Computer Science Division, University of California */
138 /*     at Berkeley, USA */
139 /*  Modified by Francoise Tisseur, University of Tennessee. */
140
141 /*  ===================================================================== */
142
143 /*     .. Parameters .. */
144 /*     .. */
145 /*     .. Local Scalars .. */
146 /*     .. */
147 /*     .. External Functions .. */
148 /*     .. */
149 /*     .. External Subroutines .. */
150 /*     .. */
151 /*     .. Intrinsic Functions .. */
152 /*     .. */
153 /*     .. Executable Statements .. */
154
155 /*     Test the input parameters. */
156
157     /* Parameter adjustments */
158     --d__;
159     q_dim1 = *ldq;
160     q_offset = 1 + q_dim1;
161     q -= q_offset;
162     --dlamda;
163     --q2;
164     --indx;
165     --ctot;
166     --w;
167     --s;
168
169     /* Function Body */
170     *info = 0;
171
172     if (*k < 0) {
173         *info = -1;
174     } else if (*n < *k) {
175         *info = -2;
176     } else if (*ldq < max(1,*n)) {
177         *info = -6;
178     }
179     if (*info != 0) {
180         i__1 = -(*info);
181         xerbla_("SLAED3", &i__1);
182         return 0;
183     }
184
185 /*     Quick return if possible */
186
187     if (*k == 0) {
188         return 0;
189     }
190
191 /*     Modify values DLAMDA(i) to make sure all DLAMDA(i)-DLAMDA(j) can */
192 /*     be computed with high relative accuracy (barring over/underflow). */
193 /*     This is a problem on machines without a guard digit in */
194 /*     add/subtract (Cray XMP, Cray YMP, Cray C 90 and Cray 2). */
195 /*     The following code replaces DLAMDA(I) by 2*DLAMDA(I)-DLAMDA(I), */
196 /*     which on any of these machines zeros out the bottommost */
197 /*     bit of DLAMDA(I) if it is 1; this makes the subsequent */
198 /*     subtractions DLAMDA(I)-DLAMDA(J) unproblematic when cancellation */
199 /*     occurs. On binary machines with a guard digit (almost all */
200 /*     machines) it does not change DLAMDA(I) at all. On hexadecimal */
201 /*     and decimal machines with a guard digit, it slightly */
202 /*     changes the bottommost bits of DLAMDA(I). It does not account */
203 /*     for hexadecimal or decimal machines without guard digits */
204 /*     (we know of none). We use a subroutine call to compute */
205 /*     2*DLAMBDA(I) to prevent optimizing compilers from eliminating */
206 /*     this code. */
207
208     i__1 = *k;
209     for (i__ = 1; i__ <= i__1; ++i__) {
210         dlamda[i__] = slamc3_(&dlamda[i__], &dlamda[i__]) - dlamda[i__];
211 /* L10: */
212     }
213
214     i__1 = *k;
215     for (j = 1; j <= i__1; ++j) {
216         slaed4_(k, &j, &dlamda[1], &w[1], &q[j * q_dim1 + 1], rho, &d__[j], 
217                 info);
218
219 /*        If the zero finder fails, the computation is terminated. */
220
221         if (*info != 0) {
222             goto L120;
223         }
224 /* L20: */
225     }
226
227     if (*k == 1) {
228         goto L110;
229     }
230     if (*k == 2) {
231         i__1 = *k;
232         for (j = 1; j <= i__1; ++j) {
233             w[1] = q[j * q_dim1 + 1];
234             w[2] = q[j * q_dim1 + 2];
235             ii = indx[1];
236             q[j * q_dim1 + 1] = w[ii];
237             ii = indx[2];
238             q[j * q_dim1 + 2] = w[ii];
239 /* L30: */
240         }
241         goto L110;
242     }
243
244 /*     Compute updated W. */
245
246     scopy_(k, &w[1], &c__1, &s[1], &c__1);
247
248 /*     Initialize W(I) = Q(I,I) */
249
250     i__1 = *ldq + 1;
251     scopy_(k, &q[q_offset], &i__1, &w[1], &c__1);
252     i__1 = *k;
253     for (j = 1; j <= i__1; ++j) {
254         i__2 = j - 1;
255         for (i__ = 1; i__ <= i__2; ++i__) {
256             w[i__] *= q[i__ + j * q_dim1] / (dlamda[i__] - dlamda[j]);
257 /* L40: */
258         }
259         i__2 = *k;
260         for (i__ = j + 1; i__ <= i__2; ++i__) {
261             w[i__] *= q[i__ + j * q_dim1] / (dlamda[i__] - dlamda[j]);
262 /* L50: */
263         }
264 /* L60: */
265     }
266     i__1 = *k;
267     for (i__ = 1; i__ <= i__1; ++i__) {
268         r__1 = sqrt(-w[i__]);
269         w[i__] = r_sign(&r__1, &s[i__]);
270 /* L70: */
271     }
272
273 /*     Compute eigenvectors of the modified rank-1 modification. */
274
275     i__1 = *k;
276     for (j = 1; j <= i__1; ++j) {
277         i__2 = *k;
278         for (i__ = 1; i__ <= i__2; ++i__) {
279             s[i__] = w[i__] / q[i__ + j * q_dim1];
280 /* L80: */
281         }
282         temp = snrm2_(k, &s[1], &c__1);
283         i__2 = *k;
284         for (i__ = 1; i__ <= i__2; ++i__) {
285             ii = indx[i__];
286             q[i__ + j * q_dim1] = s[ii] / temp;
287 /* L90: */
288         }
289 /* L100: */
290     }
291
292 /*     Compute the updated eigenvectors. */
293
294 L110:
295
296     n2 = *n - *n1;
297     n12 = ctot[1] + ctot[2];
298     n23 = ctot[2] + ctot[3];
299
300     slacpy_("A", &n23, k, &q[ctot[1] + 1 + q_dim1], ldq, &s[1], &n23);
301     iq2 = *n1 * n12 + 1;
302     if (n23 != 0) {
303         sgemm_("N", "N", &n2, k, &n23, &c_b22, &q2[iq2], &n2, &s[1], &n23, &
304                 c_b23, &q[*n1 + 1 + q_dim1], ldq);
305     } else {
306         slaset_("A", &n2, k, &c_b23, &c_b23, &q[*n1 + 1 + q_dim1], ldq);
307     }
308
309     slacpy_("A", &n12, k, &q[q_offset], ldq, &s[1], &n12);
310     if (n12 != 0) {
311         sgemm_("N", "N", n1, k, &n12, &c_b22, &q2[1], n1, &s[1], &n12, &c_b23, 
312                  &q[q_offset], ldq);
313     } else {
314         slaset_("A", n1, k, &c_b23, &c_b23, &q[q_dim1 + 1], ldq);
315     }
316
317
318 L120:
319     return 0;
320
321 /*     End of SLAED3 */
322
323 } /* slaed3_ */