Update to 2.0.0 tree from current Fremantle build
[opencv] / 3rdparty / lapack / slasd3.c
1 #include "clapack.h"
2
3 /* Table of constant values */
4
5 static integer c__1 = 1;
6 static integer c__0 = 0;
7 static real c_b13 = 1.f;
8 static real c_b26 = 0.f;
9
10 /* Subroutine */ int slasd3_(integer *nl, integer *nr, integer *sqre, integer 
11         *k, real *d__, real *q, integer *ldq, real *dsigma, real *u, integer *
12         ldu, real *u2, integer *ldu2, real *vt, integer *ldvt, real *vt2, 
13         integer *ldvt2, integer *idxc, integer *ctot, real *z__, integer *
14         info)
15 {
16     /* System generated locals */
17     integer q_dim1, q_offset, u_dim1, u_offset, u2_dim1, u2_offset, vt_dim1, 
18             vt_offset, vt2_dim1, vt2_offset, i__1, i__2;
19     real r__1, r__2;
20
21     /* Builtin functions */
22     double sqrt(doublereal), r_sign(real *, real *);
23
24     /* Local variables */
25     integer i__, j, m, n, jc;
26     real rho;
27     integer nlp1, nlp2, nrp1;
28     real temp;
29     extern doublereal snrm2_(integer *, real *, integer *);
30     integer ctemp;
31     extern /* Subroutine */ int sgemm_(char *, char *, integer *, integer *, 
32             integer *, real *, real *, integer *, real *, integer *, real *, 
33             real *, integer *);
34     integer ktemp;
35     extern /* Subroutine */ int scopy_(integer *, real *, integer *, real *, 
36             integer *);
37     extern doublereal slamc3_(real *, real *);
38     extern /* Subroutine */ int slasd4_(integer *, integer *, real *, real *, 
39             real *, real *, real *, real *, integer *), xerbla_(char *, 
40             integer *), slascl_(char *, integer *, integer *, real *, 
41             real *, integer *, integer *, real *, integer *, integer *), slacpy_(char *, integer *, integer *, real *, integer *, 
42             real *, integer *);
43
44
45 /*  -- LAPACK auxiliary routine (version 3.1) -- */
46 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
47 /*     November 2006 */
48
49 /*     .. Scalar Arguments .. */
50 /*     .. */
51 /*     .. Array Arguments .. */
52 /*     .. */
53
54 /*  Purpose */
55 /*  ======= */
56
57 /*  SLASD3 finds all the square roots of the roots of the secular */
58 /*  equation, as defined by the values in D and Z.  It makes the */
59 /*  appropriate calls to SLASD4 and then updates the singular */
60 /*  vectors by matrix multiplication. */
61
62 /*  This code makes very mild assumptions about floating point */
63 /*  arithmetic. It will work on machines with a guard digit in */
64 /*  add/subtract, or on those binary machines without guard digits */
65 /*  which subtract like the Cray XMP, Cray YMP, Cray C 90, or Cray 2. */
66 /*  It could conceivably fail on hexadecimal or decimal machines */
67 /*  without guard digits, but we know of none. */
68
69 /*  SLASD3 is called from SLASD1. */
70
71 /*  Arguments */
72 /*  ========= */
73
74 /*  NL     (input) INTEGER */
75 /*         The row dimension of the upper block.  NL >= 1. */
76
77 /*  NR     (input) INTEGER */
78 /*         The row dimension of the lower block.  NR >= 1. */
79
80 /*  SQRE   (input) INTEGER */
81 /*         = 0: the lower block is an NR-by-NR square matrix. */
82 /*         = 1: the lower block is an NR-by-(NR+1) rectangular matrix. */
83
84 /*         The bidiagonal matrix has N = NL + NR + 1 rows and */
85 /*         M = N + SQRE >= N columns. */
86
87 /*  K      (input) INTEGER */
88 /*         The size of the secular equation, 1 =< K = < N. */
89
90 /*  D      (output) REAL array, dimension(K) */
91 /*         On exit the square roots of the roots of the secular equation, */
92 /*         in ascending order. */
93
94 /*  Q      (workspace) REAL array, */
95 /*                     dimension at least (LDQ,K). */
96
97 /*  LDQ    (input) INTEGER */
98 /*         The leading dimension of the array Q.  LDQ >= K. */
99
100 /*  DSIGMA (input/output) REAL array, dimension(K) */
101 /*         The first K elements of this array contain the old roots */
102 /*         of the deflated updating problem.  These are the poles */
103 /*         of the secular equation. */
104
105 /*  U      (output) REAL array, dimension (LDU, N) */
106 /*         The last N - K columns of this matrix contain the deflated */
107 /*         left singular vectors. */
108
109 /*  LDU    (input) INTEGER */
110 /*         The leading dimension of the array U.  LDU >= N. */
111
112 /*  U2     (input) REAL array, dimension (LDU2, N) */
113 /*         The first K columns of this matrix contain the non-deflated */
114 /*         left singular vectors for the split problem. */
115
116 /*  LDU2   (input) INTEGER */
117 /*         The leading dimension of the array U2.  LDU2 >= N. */
118
119 /*  VT     (output) REAL array, dimension (LDVT, M) */
120 /*         The last M - K columns of VT' contain the deflated */
121 /*         right singular vectors. */
122
123 /*  LDVT   (input) INTEGER */
124 /*         The leading dimension of the array VT.  LDVT >= N. */
125
126 /*  VT2    (input/output) REAL array, dimension (LDVT2, N) */
127 /*         The first K columns of VT2' contain the non-deflated */
128 /*         right singular vectors for the split problem. */
129
130 /*  LDVT2  (input) INTEGER */
131 /*         The leading dimension of the array VT2.  LDVT2 >= N. */
132
133 /*  IDXC   (input) INTEGER array, dimension (N) */
134 /*         The permutation used to arrange the columns of U (and rows of */
135 /*         VT) into three groups:  the first group contains non-zero */
136 /*         entries only at and above (or before) NL +1; the second */
137 /*         contains non-zero entries only at and below (or after) NL+2; */
138 /*         and the third is dense. The first column of U and the row of */
139 /*         VT are treated separately, however. */
140
141 /*         The rows of the singular vectors found by SLASD4 */
142 /*         must be likewise permuted before the matrix multiplies can */
143 /*         take place. */
144
145 /*  CTOT   (input) INTEGER array, dimension (4) */
146 /*         A count of the total number of the various types of columns */
147 /*         in U (or rows in VT), as described in IDXC. The fourth column */
148 /*         type is any column which has been deflated. */
149
150 /*  Z      (input/output) REAL array, dimension (K) */
151 /*         The first K elements of this array contain the components */
152 /*         of the deflation-adjusted updating row vector. */
153
154 /*  INFO   (output) INTEGER */
155 /*         = 0:  successful exit. */
156 /*         < 0:  if INFO = -i, the i-th argument had an illegal value. */
157 /*         > 0:  if INFO = 1, an singular value did not converge */
158
159 /*  Further Details */
160 /*  =============== */
161
162 /*  Based on contributions by */
163 /*     Ming Gu and Huan Ren, Computer Science Division, University of */
164 /*     California at Berkeley, USA */
165
166 /*  ===================================================================== */
167
168 /*     .. Parameters .. */
169 /*     .. */
170 /*     .. Local Scalars .. */
171 /*     .. */
172 /*     .. External Functions .. */
173 /*     .. */
174 /*     .. External Subroutines .. */
175 /*     .. */
176 /*     .. Intrinsic Functions .. */
177 /*     .. */
178 /*     .. Executable Statements .. */
179
180 /*     Test the input parameters. */
181
182     /* Parameter adjustments */
183     --d__;
184     q_dim1 = *ldq;
185     q_offset = 1 + q_dim1;
186     q -= q_offset;
187     --dsigma;
188     u_dim1 = *ldu;
189     u_offset = 1 + u_dim1;
190     u -= u_offset;
191     u2_dim1 = *ldu2;
192     u2_offset = 1 + u2_dim1;
193     u2 -= u2_offset;
194     vt_dim1 = *ldvt;
195     vt_offset = 1 + vt_dim1;
196     vt -= vt_offset;
197     vt2_dim1 = *ldvt2;
198     vt2_offset = 1 + vt2_dim1;
199     vt2 -= vt2_offset;
200     --idxc;
201     --ctot;
202     --z__;
203
204     /* Function Body */
205     *info = 0;
206
207     if (*nl < 1) {
208         *info = -1;
209     } else if (*nr < 1) {
210         *info = -2;
211     } else if (*sqre != 1 && *sqre != 0) {
212         *info = -3;
213     }
214
215     n = *nl + *nr + 1;
216     m = n + *sqre;
217     nlp1 = *nl + 1;
218     nlp2 = *nl + 2;
219
220     if (*k < 1 || *k > n) {
221         *info = -4;
222     } else if (*ldq < *k) {
223         *info = -7;
224     } else if (*ldu < n) {
225         *info = -10;
226     } else if (*ldu2 < n) {
227         *info = -12;
228     } else if (*ldvt < m) {
229         *info = -14;
230     } else if (*ldvt2 < m) {
231         *info = -16;
232     }
233     if (*info != 0) {
234         i__1 = -(*info);
235         xerbla_("SLASD3", &i__1);
236         return 0;
237     }
238
239 /*     Quick return if possible */
240
241     if (*k == 1) {
242         d__[1] = dabs(z__[1]);
243         scopy_(&m, &vt2[vt2_dim1 + 1], ldvt2, &vt[vt_dim1 + 1], ldvt);
244         if (z__[1] > 0.f) {
245             scopy_(&n, &u2[u2_dim1 + 1], &c__1, &u[u_dim1 + 1], &c__1);
246         } else {
247             i__1 = n;
248             for (i__ = 1; i__ <= i__1; ++i__) {
249                 u[i__ + u_dim1] = -u2[i__ + u2_dim1];
250 /* L10: */
251             }
252         }
253         return 0;
254     }
255
256 /*     Modify values DSIGMA(i) to make sure all DSIGMA(i)-DSIGMA(j) can */
257 /*     be computed with high relative accuracy (barring over/underflow). */
258 /*     This is a problem on machines without a guard digit in */
259 /*     add/subtract (Cray XMP, Cray YMP, Cray C 90 and Cray 2). */
260 /*     The following code replaces DSIGMA(I) by 2*DSIGMA(I)-DSIGMA(I), */
261 /*     which on any of these machines zeros out the bottommost */
262 /*     bit of DSIGMA(I) if it is 1; this makes the subsequent */
263 /*     subtractions DSIGMA(I)-DSIGMA(J) unproblematic when cancellation */
264 /*     occurs. On binary machines with a guard digit (almost all */
265 /*     machines) it does not change DSIGMA(I) at all. On hexadecimal */
266 /*     and decimal machines with a guard digit, it slightly */
267 /*     changes the bottommost bits of DSIGMA(I). It does not account */
268 /*     for hexadecimal or decimal machines without guard digits */
269 /*     (we know of none). We use a subroutine call to compute */
270 /*     2*DSIGMA(I) to prevent optimizing compilers from eliminating */
271 /*     this code. */
272
273     i__1 = *k;
274     for (i__ = 1; i__ <= i__1; ++i__) {
275         dsigma[i__] = slamc3_(&dsigma[i__], &dsigma[i__]) - dsigma[i__];
276 /* L20: */
277     }
278
279 /*     Keep a copy of Z. */
280
281     scopy_(k, &z__[1], &c__1, &q[q_offset], &c__1);
282
283 /*     Normalize Z. */
284
285     rho = snrm2_(k, &z__[1], &c__1);
286     slascl_("G", &c__0, &c__0, &rho, &c_b13, k, &c__1, &z__[1], k, info);
287     rho *= rho;
288
289 /*     Find the new singular values. */
290
291     i__1 = *k;
292     for (j = 1; j <= i__1; ++j) {
293         slasd4_(k, &j, &dsigma[1], &z__[1], &u[j * u_dim1 + 1], &rho, &d__[j], 
294                  &vt[j * vt_dim1 + 1], info);
295
296 /*        If the zero finder fails, the computation is terminated. */
297
298         if (*info != 0) {
299             return 0;
300         }
301 /* L30: */
302     }
303
304 /*     Compute updated Z. */
305
306     i__1 = *k;
307     for (i__ = 1; i__ <= i__1; ++i__) {
308         z__[i__] = u[i__ + *k * u_dim1] * vt[i__ + *k * vt_dim1];
309         i__2 = i__ - 1;
310         for (j = 1; j <= i__2; ++j) {
311             z__[i__] *= u[i__ + j * u_dim1] * vt[i__ + j * vt_dim1] / (dsigma[
312                     i__] - dsigma[j]) / (dsigma[i__] + dsigma[j]);
313 /* L40: */
314         }
315         i__2 = *k - 1;
316         for (j = i__; j <= i__2; ++j) {
317             z__[i__] *= u[i__ + j * u_dim1] * vt[i__ + j * vt_dim1] / (dsigma[
318                     i__] - dsigma[j + 1]) / (dsigma[i__] + dsigma[j + 1]);
319 /* L50: */
320         }
321         r__2 = sqrt((r__1 = z__[i__], dabs(r__1)));
322         z__[i__] = r_sign(&r__2, &q[i__ + q_dim1]);
323 /* L60: */
324     }
325
326 /*     Compute left singular vectors of the modified diagonal matrix, */
327 /*     and store related information for the right singular vectors. */
328
329     i__1 = *k;
330     for (i__ = 1; i__ <= i__1; ++i__) {
331         vt[i__ * vt_dim1 + 1] = z__[1] / u[i__ * u_dim1 + 1] / vt[i__ * 
332                 vt_dim1 + 1];
333         u[i__ * u_dim1 + 1] = -1.f;
334         i__2 = *k;
335         for (j = 2; j <= i__2; ++j) {
336             vt[j + i__ * vt_dim1] = z__[j] / u[j + i__ * u_dim1] / vt[j + i__ 
337                     * vt_dim1];
338             u[j + i__ * u_dim1] = dsigma[j] * vt[j + i__ * vt_dim1];
339 /* L70: */
340         }
341         temp = snrm2_(k, &u[i__ * u_dim1 + 1], &c__1);
342         q[i__ * q_dim1 + 1] = u[i__ * u_dim1 + 1] / temp;
343         i__2 = *k;
344         for (j = 2; j <= i__2; ++j) {
345             jc = idxc[j];
346             q[j + i__ * q_dim1] = u[jc + i__ * u_dim1] / temp;
347 /* L80: */
348         }
349 /* L90: */
350     }
351
352 /*     Update the left singular vector matrix. */
353
354     if (*k == 2) {
355         sgemm_("N", "N", &n, k, k, &c_b13, &u2[u2_offset], ldu2, &q[q_offset], 
356                  ldq, &c_b26, &u[u_offset], ldu);
357         goto L100;
358     }
359     if (ctot[1] > 0) {
360         sgemm_("N", "N", nl, k, &ctot[1], &c_b13, &u2[(u2_dim1 << 1) + 1], 
361                 ldu2, &q[q_dim1 + 2], ldq, &c_b26, &u[u_dim1 + 1], ldu);
362         if (ctot[3] > 0) {
363             ktemp = ctot[1] + 2 + ctot[2];
364             sgemm_("N", "N", nl, k, &ctot[3], &c_b13, &u2[ktemp * u2_dim1 + 1]
365 , ldu2, &q[ktemp + q_dim1], ldq, &c_b13, &u[u_dim1 + 1], 
366                     ldu);
367         }
368     } else if (ctot[3] > 0) {
369         ktemp = ctot[1] + 2 + ctot[2];
370         sgemm_("N", "N", nl, k, &ctot[3], &c_b13, &u2[ktemp * u2_dim1 + 1], 
371                 ldu2, &q[ktemp + q_dim1], ldq, &c_b26, &u[u_dim1 + 1], ldu);
372     } else {
373         slacpy_("F", nl, k, &u2[u2_offset], ldu2, &u[u_offset], ldu);
374     }
375     scopy_(k, &q[q_dim1 + 1], ldq, &u[nlp1 + u_dim1], ldu);
376     ktemp = ctot[1] + 2;
377     ctemp = ctot[2] + ctot[3];
378     sgemm_("N", "N", nr, k, &ctemp, &c_b13, &u2[nlp2 + ktemp * u2_dim1], ldu2, 
379              &q[ktemp + q_dim1], ldq, &c_b26, &u[nlp2 + u_dim1], ldu);
380
381 /*     Generate the right singular vectors. */
382
383 L100:
384     i__1 = *k;
385     for (i__ = 1; i__ <= i__1; ++i__) {
386         temp = snrm2_(k, &vt[i__ * vt_dim1 + 1], &c__1);
387         q[i__ + q_dim1] = vt[i__ * vt_dim1 + 1] / temp;
388         i__2 = *k;
389         for (j = 2; j <= i__2; ++j) {
390             jc = idxc[j];
391             q[i__ + j * q_dim1] = vt[jc + i__ * vt_dim1] / temp;
392 /* L110: */
393         }
394 /* L120: */
395     }
396
397 /*     Update the right singular vector matrix. */
398
399     if (*k == 2) {
400         sgemm_("N", "N", k, &m, k, &c_b13, &q[q_offset], ldq, &vt2[vt2_offset]
401 , ldvt2, &c_b26, &vt[vt_offset], ldvt);
402         return 0;
403     }
404     ktemp = ctot[1] + 1;
405     sgemm_("N", "N", k, &nlp1, &ktemp, &c_b13, &q[q_dim1 + 1], ldq, &vt2[
406             vt2_dim1 + 1], ldvt2, &c_b26, &vt[vt_dim1 + 1], ldvt);
407     ktemp = ctot[1] + 2 + ctot[2];
408     if (ktemp <= *ldvt2) {
409         sgemm_("N", "N", k, &nlp1, &ctot[3], &c_b13, &q[ktemp * q_dim1 + 1], 
410                 ldq, &vt2[ktemp + vt2_dim1], ldvt2, &c_b13, &vt[vt_dim1 + 1], 
411                 ldvt);
412     }
413
414     ktemp = ctot[1] + 1;
415     nrp1 = *nr + *sqre;
416     if (ktemp > 1) {
417         i__1 = *k;
418         for (i__ = 1; i__ <= i__1; ++i__) {
419             q[i__ + ktemp * q_dim1] = q[i__ + q_dim1];
420 /* L130: */
421         }
422         i__1 = m;
423         for (i__ = nlp2; i__ <= i__1; ++i__) {
424             vt2[ktemp + i__ * vt2_dim1] = vt2[i__ * vt2_dim1 + 1];
425 /* L140: */
426         }
427     }
428     ctemp = ctot[2] + 1 + ctot[3];
429     sgemm_("N", "N", k, &nrp1, &ctemp, &c_b13, &q[ktemp * q_dim1 + 1], ldq, &
430             vt2[ktemp + nlp2 * vt2_dim1], ldvt2, &c_b26, &vt[nlp2 * vt_dim1 + 
431             1], ldvt);
432
433     return 0;
434
435 /*     End of SLASD3 */
436
437 } /* slasd3_ */