Update to 2.0.0 tree from current Fremantle build
[opencv] / 3rdparty / lapack / slasdq.c
1 #include "clapack.h"
2
3 /* Table of constant values */
4
5 static integer c__1 = 1;
6
7 /* Subroutine */ int slasdq_(char *uplo, integer *sqre, integer *n, integer *
8         ncvt, integer *nru, integer *ncc, real *d__, real *e, real *vt, 
9         integer *ldvt, real *u, integer *ldu, real *c__, integer *ldc, real *
10         work, integer *info)
11 {
12     /* System generated locals */
13     integer c_dim1, c_offset, u_dim1, u_offset, vt_dim1, vt_offset, i__1, 
14             i__2;
15
16     /* Local variables */
17     integer i__, j;
18     real r__, cs, sn;
19     integer np1, isub;
20     real smin;
21     integer sqre1;
22     extern logical lsame_(char *, char *);
23     extern /* Subroutine */ int slasr_(char *, char *, char *, integer *, 
24             integer *, real *, real *, real *, integer *);
25     integer iuplo;
26     extern /* Subroutine */ int sswap_(integer *, real *, integer *, real *, 
27             integer *), xerbla_(char *, integer *), slartg_(real *, 
28             real *, real *, real *, real *);
29     logical rotate;
30     extern /* Subroutine */ int sbdsqr_(char *, integer *, integer *, integer 
31             *, integer *, real *, real *, real *, integer *, real *, integer *
32 , real *, integer *, real *, integer *);
33
34
35 /*  -- LAPACK auxiliary routine (version 3.1) -- */
36 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
37 /*     November 2006 */
38
39 /*     .. Scalar Arguments .. */
40 /*     .. */
41 /*     .. Array Arguments .. */
42 /*     .. */
43
44 /*  Purpose */
45 /*  ======= */
46
47 /*  SLASDQ computes the singular value decomposition (SVD) of a real */
48 /*  (upper or lower) bidiagonal matrix with diagonal D and offdiagonal */
49 /*  E, accumulating the transformations if desired. Letting B denote */
50 /*  the input bidiagonal matrix, the algorithm computes orthogonal */
51 /*  matrices Q and P such that B = Q * S * P' (P' denotes the transpose */
52 /*  of P). The singular values S are overwritten on D. */
53
54 /*  The input matrix U  is changed to U  * Q  if desired. */
55 /*  The input matrix VT is changed to P' * VT if desired. */
56 /*  The input matrix C  is changed to Q' * C  if desired. */
57
58 /*  See "Computing  Small Singular Values of Bidiagonal Matrices With */
59 /*  Guaranteed High Relative Accuracy," by J. Demmel and W. Kahan, */
60 /*  LAPACK Working Note #3, for a detailed description of the algorithm. */
61
62 /*  Arguments */
63 /*  ========= */
64
65 /*  UPLO  (input) CHARACTER*1 */
66 /*        On entry, UPLO specifies whether the input bidiagonal matrix */
67 /*        is upper or lower bidiagonal, and wether it is square are */
68 /*        not. */
69 /*           UPLO = 'U' or 'u'   B is upper bidiagonal. */
70 /*           UPLO = 'L' or 'l'   B is lower bidiagonal. */
71
72 /*  SQRE  (input) INTEGER */
73 /*        = 0: then the input matrix is N-by-N. */
74 /*        = 1: then the input matrix is N-by-(N+1) if UPLU = 'U' and */
75 /*             (N+1)-by-N if UPLU = 'L'. */
76
77 /*        The bidiagonal matrix has */
78 /*        N = NL + NR + 1 rows and */
79 /*        M = N + SQRE >= N columns. */
80
81 /*  N     (input) INTEGER */
82 /*        On entry, N specifies the number of rows and columns */
83 /*        in the matrix. N must be at least 0. */
84
85 /*  NCVT  (input) INTEGER */
86 /*        On entry, NCVT specifies the number of columns of */
87 /*        the matrix VT. NCVT must be at least 0. */
88
89 /*  NRU   (input) INTEGER */
90 /*        On entry, NRU specifies the number of rows of */
91 /*        the matrix U. NRU must be at least 0. */
92
93 /*  NCC   (input) INTEGER */
94 /*        On entry, NCC specifies the number of columns of */
95 /*        the matrix C. NCC must be at least 0. */
96
97 /*  D     (input/output) REAL array, dimension (N) */
98 /*        On entry, D contains the diagonal entries of the */
99 /*        bidiagonal matrix whose SVD is desired. On normal exit, */
100 /*        D contains the singular values in ascending order. */
101
102 /*  E     (input/output) REAL array. */
103 /*        dimension is (N-1) if SQRE = 0 and N if SQRE = 1. */
104 /*        On entry, the entries of E contain the offdiagonal entries */
105 /*        of the bidiagonal matrix whose SVD is desired. On normal */
106 /*        exit, E will contain 0. If the algorithm does not converge, */
107 /*        D and E will contain the diagonal and superdiagonal entries */
108 /*        of a bidiagonal matrix orthogonally equivalent to the one */
109 /*        given as input. */
110
111 /*  VT    (input/output) REAL array, dimension (LDVT, NCVT) */
112 /*        On entry, contains a matrix which on exit has been */
113 /*        premultiplied by P', dimension N-by-NCVT if SQRE = 0 */
114 /*        and (N+1)-by-NCVT if SQRE = 1 (not referenced if NCVT=0). */
115
116 /*  LDVT  (input) INTEGER */
117 /*        On entry, LDVT specifies the leading dimension of VT as */
118 /*        declared in the calling (sub) program. LDVT must be at */
119 /*        least 1. If NCVT is nonzero LDVT must also be at least N. */
120
121 /*  U     (input/output) REAL array, dimension (LDU, N) */
122 /*        On entry, contains a  matrix which on exit has been */
123 /*        postmultiplied by Q, dimension NRU-by-N if SQRE = 0 */
124 /*        and NRU-by-(N+1) if SQRE = 1 (not referenced if NRU=0). */
125
126 /*  LDU   (input) INTEGER */
127 /*        On entry, LDU  specifies the leading dimension of U as */
128 /*        declared in the calling (sub) program. LDU must be at */
129 /*        least max( 1, NRU ) . */
130
131 /*  C     (input/output) REAL array, dimension (LDC, NCC) */
132 /*        On entry, contains an N-by-NCC matrix which on exit */
133 /*        has been premultiplied by Q'  dimension N-by-NCC if SQRE = 0 */
134 /*        and (N+1)-by-NCC if SQRE = 1 (not referenced if NCC=0). */
135
136 /*  LDC   (input) INTEGER */
137 /*        On entry, LDC  specifies the leading dimension of C as */
138 /*        declared in the calling (sub) program. LDC must be at */
139 /*        least 1. If NCC is nonzero, LDC must also be at least N. */
140
141 /*  WORK  (workspace) REAL array, dimension (4*N) */
142 /*        Workspace. Only referenced if one of NCVT, NRU, or NCC is */
143 /*        nonzero, and if N is at least 2. */
144
145 /*  INFO  (output) INTEGER */
146 /*        On exit, a value of 0 indicates a successful exit. */
147 /*        If INFO < 0, argument number -INFO is illegal. */
148 /*        If INFO > 0, the algorithm did not converge, and INFO */
149 /*        specifies how many superdiagonals did not converge. */
150
151 /*  Further Details */
152 /*  =============== */
153
154 /*  Based on contributions by */
155 /*     Ming Gu and Huan Ren, Computer Science Division, University of */
156 /*     California at Berkeley, USA */
157
158 /*  ===================================================================== */
159
160 /*     .. Parameters .. */
161 /*     .. */
162 /*     .. Local Scalars .. */
163 /*     .. */
164 /*     .. External Subroutines .. */
165 /*     .. */
166 /*     .. External Functions .. */
167 /*     .. */
168 /*     .. Intrinsic Functions .. */
169 /*     .. */
170 /*     .. Executable Statements .. */
171
172 /*     Test the input parameters. */
173
174     /* Parameter adjustments */
175     --d__;
176     --e;
177     vt_dim1 = *ldvt;
178     vt_offset = 1 + vt_dim1;
179     vt -= vt_offset;
180     u_dim1 = *ldu;
181     u_offset = 1 + u_dim1;
182     u -= u_offset;
183     c_dim1 = *ldc;
184     c_offset = 1 + c_dim1;
185     c__ -= c_offset;
186     --work;
187
188     /* Function Body */
189     *info = 0;
190     iuplo = 0;
191     if (lsame_(uplo, "U")) {
192         iuplo = 1;
193     }
194     if (lsame_(uplo, "L")) {
195         iuplo = 2;
196     }
197     if (iuplo == 0) {
198         *info = -1;
199     } else if (*sqre < 0 || *sqre > 1) {
200         *info = -2;
201     } else if (*n < 0) {
202         *info = -3;
203     } else if (*ncvt < 0) {
204         *info = -4;
205     } else if (*nru < 0) {
206         *info = -5;
207     } else if (*ncc < 0) {
208         *info = -6;
209     } else if (*ncvt == 0 && *ldvt < 1 || *ncvt > 0 && *ldvt < max(1,*n)) {
210         *info = -10;
211     } else if (*ldu < max(1,*nru)) {
212         *info = -12;
213     } else if (*ncc == 0 && *ldc < 1 || *ncc > 0 && *ldc < max(1,*n)) {
214         *info = -14;
215     }
216     if (*info != 0) {
217         i__1 = -(*info);
218         xerbla_("SLASDQ", &i__1);
219         return 0;
220     }
221     if (*n == 0) {
222         return 0;
223     }
224
225 /*     ROTATE is true if any singular vectors desired, false otherwise */
226
227     rotate = *ncvt > 0 || *nru > 0 || *ncc > 0;
228     np1 = *n + 1;
229     sqre1 = *sqre;
230
231 /*     If matrix non-square upper bidiagonal, rotate to be lower */
232 /*     bidiagonal.  The rotations are on the right. */
233
234     if (iuplo == 1 && sqre1 == 1) {
235         i__1 = *n - 1;
236         for (i__ = 1; i__ <= i__1; ++i__) {
237             slartg_(&d__[i__], &e[i__], &cs, &sn, &r__);
238             d__[i__] = r__;
239             e[i__] = sn * d__[i__ + 1];
240             d__[i__ + 1] = cs * d__[i__ + 1];
241             if (rotate) {
242                 work[i__] = cs;
243                 work[*n + i__] = sn;
244             }
245 /* L10: */
246         }
247         slartg_(&d__[*n], &e[*n], &cs, &sn, &r__);
248         d__[*n] = r__;
249         e[*n] = 0.f;
250         if (rotate) {
251             work[*n] = cs;
252             work[*n + *n] = sn;
253         }
254         iuplo = 2;
255         sqre1 = 0;
256
257 /*        Update singular vectors if desired. */
258
259         if (*ncvt > 0) {
260             slasr_("L", "V", "F", &np1, ncvt, &work[1], &work[np1], &vt[
261                     vt_offset], ldvt);
262         }
263     }
264
265 /*     If matrix lower bidiagonal, rotate to be upper bidiagonal */
266 /*     by applying Givens rotations on the left. */
267
268     if (iuplo == 2) {
269         i__1 = *n - 1;
270         for (i__ = 1; i__ <= i__1; ++i__) {
271             slartg_(&d__[i__], &e[i__], &cs, &sn, &r__);
272             d__[i__] = r__;
273             e[i__] = sn * d__[i__ + 1];
274             d__[i__ + 1] = cs * d__[i__ + 1];
275             if (rotate) {
276                 work[i__] = cs;
277                 work[*n + i__] = sn;
278             }
279 /* L20: */
280         }
281
282 /*        If matrix (N+1)-by-N lower bidiagonal, one additional */
283 /*        rotation is needed. */
284
285         if (sqre1 == 1) {
286             slartg_(&d__[*n], &e[*n], &cs, &sn, &r__);
287             d__[*n] = r__;
288             if (rotate) {
289                 work[*n] = cs;
290                 work[*n + *n] = sn;
291             }
292         }
293
294 /*        Update singular vectors if desired. */
295
296         if (*nru > 0) {
297             if (sqre1 == 0) {
298                 slasr_("R", "V", "F", nru, n, &work[1], &work[np1], &u[
299                         u_offset], ldu);
300             } else {
301                 slasr_("R", "V", "F", nru, &np1, &work[1], &work[np1], &u[
302                         u_offset], ldu);
303             }
304         }
305         if (*ncc > 0) {
306             if (sqre1 == 0) {
307                 slasr_("L", "V", "F", n, ncc, &work[1], &work[np1], &c__[
308                         c_offset], ldc);
309             } else {
310                 slasr_("L", "V", "F", &np1, ncc, &work[1], &work[np1], &c__[
311                         c_offset], ldc);
312             }
313         }
314     }
315
316 /*     Call SBDSQR to compute the SVD of the reduced real */
317 /*     N-by-N upper bidiagonal matrix. */
318
319     sbdsqr_("U", n, ncvt, nru, ncc, &d__[1], &e[1], &vt[vt_offset], ldvt, &u[
320             u_offset], ldu, &c__[c_offset], ldc, &work[1], info);
321
322 /*     Sort the singular values into ascending order (insertion sort on */
323 /*     singular values, but only one transposition per singular vector) */
324
325     i__1 = *n;
326     for (i__ = 1; i__ <= i__1; ++i__) {
327
328 /*        Scan for smallest D(I). */
329
330         isub = i__;
331         smin = d__[i__];
332         i__2 = *n;
333         for (j = i__ + 1; j <= i__2; ++j) {
334             if (d__[j] < smin) {
335                 isub = j;
336                 smin = d__[j];
337             }
338 /* L30: */
339         }
340         if (isub != i__) {
341
342 /*           Swap singular values and vectors. */
343
344             d__[isub] = d__[i__];
345             d__[i__] = smin;
346             if (*ncvt > 0) {
347                 sswap_(ncvt, &vt[isub + vt_dim1], ldvt, &vt[i__ + vt_dim1], 
348                         ldvt);
349             }
350             if (*nru > 0) {
351                 sswap_(nru, &u[isub * u_dim1 + 1], &c__1, &u[i__ * u_dim1 + 1]
352 , &c__1);
353             }
354             if (*ncc > 0) {
355                 sswap_(ncc, &c__[isub + c_dim1], ldc, &c__[i__ + c_dim1], ldc)
356                         ;
357             }
358         }
359 /* L40: */
360     }
361
362     return 0;
363
364 /*     End of SLASDQ */
365
366 } /* slasdq_ */