Update to 2.0.0 tree from current Fremantle build
[opencv] / 3rdparty / lapack / dlalsd.c
1 #include "clapack.h"
2
3 /* Table of constant values */
4
5 static integer c__1 = 1;
6 static doublereal c_b6 = 0.;
7 static integer c__0 = 0;
8 static doublereal c_b11 = 1.;
9
10 /* Subroutine */ int dlalsd_(char *uplo, integer *smlsiz, integer *n, integer 
11         *nrhs, doublereal *d__, doublereal *e, doublereal *b, integer *ldb, 
12         doublereal *rcond, integer *rank, doublereal *work, integer *iwork, 
13         integer *info)
14 {
15     /* System generated locals */
16     integer b_dim1, b_offset, i__1, i__2;
17     doublereal d__1;
18
19     /* Builtin functions */
20     double log(doublereal), d_sign(doublereal *, doublereal *);
21
22     /* Local variables */
23     integer c__, i__, j, k;
24     doublereal r__;
25     integer s, u, z__;
26     doublereal cs;
27     integer bx;
28     doublereal sn;
29     integer st, vt, nm1, st1;
30     doublereal eps;
31     integer iwk;
32     doublereal tol;
33     integer difl, difr;
34     doublereal rcnd;
35     integer perm, nsub;
36     extern /* Subroutine */ int drot_(integer *, doublereal *, integer *, 
37             doublereal *, integer *, doublereal *, doublereal *);
38     integer nlvl, sqre, bxst;
39     extern /* Subroutine */ int dgemm_(char *, char *, integer *, integer *, 
40             integer *, doublereal *, doublereal *, integer *, doublereal *, 
41             integer *, doublereal *, doublereal *, integer *),
42              dcopy_(integer *, doublereal *, integer *, doublereal *, integer 
43             *);
44     integer poles, sizei, nsize, nwork, icmpq1, icmpq2;
45     extern doublereal dlamch_(char *);
46     extern /* Subroutine */ int dlasda_(integer *, integer *, integer *, 
47             integer *, doublereal *, doublereal *, doublereal *, integer *, 
48             doublereal *, integer *, doublereal *, doublereal *, doublereal *, 
49              doublereal *, integer *, integer *, integer *, integer *, 
50             doublereal *, doublereal *, doublereal *, doublereal *, integer *, 
51              integer *), dlalsa_(integer *, integer *, integer *, integer *, 
52             doublereal *, integer *, doublereal *, integer *, doublereal *, 
53             integer *, doublereal *, integer *, doublereal *, doublereal *, 
54             doublereal *, doublereal *, integer *, integer *, integer *, 
55             integer *, doublereal *, doublereal *, doublereal *, doublereal *, 
56              integer *, integer *), dlascl_(char *, integer *, integer *, 
57             doublereal *, doublereal *, integer *, integer *, doublereal *, 
58             integer *, integer *);
59     extern integer idamax_(integer *, doublereal *, integer *);
60     extern /* Subroutine */ int dlasdq_(char *, integer *, integer *, integer 
61             *, integer *, integer *, doublereal *, doublereal *, doublereal *, 
62              integer *, doublereal *, integer *, doublereal *, integer *, 
63             doublereal *, integer *), dlacpy_(char *, integer *, 
64             integer *, doublereal *, integer *, doublereal *, integer *), dlartg_(doublereal *, doublereal *, doublereal *, 
65             doublereal *, doublereal *), dlaset_(char *, integer *, integer *, 
66              doublereal *, doublereal *, doublereal *, integer *), 
67             xerbla_(char *, integer *);
68     integer givcol;
69     extern doublereal dlanst_(char *, integer *, doublereal *, doublereal *);
70     extern /* Subroutine */ int dlasrt_(char *, integer *, doublereal *, 
71             integer *);
72     doublereal orgnrm;
73     integer givnum, givptr, smlszp;
74
75
76 /*  -- LAPACK routine (version 3.1) -- */
77 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
78 /*     November 2006 */
79
80 /*     .. Scalar Arguments .. */
81 /*     .. */
82 /*     .. Array Arguments .. */
83 /*     .. */
84
85 /*  Purpose */
86 /*  ======= */
87
88 /*  DLALSD uses the singular value decomposition of A to solve the least */
89 /*  squares problem of finding X to minimize the Euclidean norm of each */
90 /*  column of A*X-B, where A is N-by-N upper bidiagonal, and X and B */
91 /*  are N-by-NRHS. The solution X overwrites B. */
92
93 /*  The singular values of A smaller than RCOND times the largest */
94 /*  singular value are treated as zero in solving the least squares */
95 /*  problem; in this case a minimum norm solution is returned. */
96 /*  The actual singular values are returned in D in ascending order. */
97
98 /*  This code makes very mild assumptions about floating point */
99 /*  arithmetic. It will work on machines with a guard digit in */
100 /*  add/subtract, or on those binary machines without guard digits */
101 /*  which subtract like the Cray XMP, Cray YMP, Cray C 90, or Cray 2. */
102 /*  It could conceivably fail on hexadecimal or decimal machines */
103 /*  without guard digits, but we know of none. */
104
105 /*  Arguments */
106 /*  ========= */
107
108 /*  UPLO   (input) CHARACTER*1 */
109 /*         = 'U': D and E define an upper bidiagonal matrix. */
110 /*         = 'L': D and E define a  lower bidiagonal matrix. */
111
112 /*  SMLSIZ (input) INTEGER */
113 /*         The maximum size of the subproblems at the bottom of the */
114 /*         computation tree. */
115
116 /*  N      (input) INTEGER */
117 /*         The dimension of the  bidiagonal matrix.  N >= 0. */
118
119 /*  NRHS   (input) INTEGER */
120 /*         The number of columns of B. NRHS must be at least 1. */
121
122 /*  D      (input/output) DOUBLE PRECISION array, dimension (N) */
123 /*         On entry D contains the main diagonal of the bidiagonal */
124 /*         matrix. On exit, if INFO = 0, D contains its singular values. */
125
126 /*  E      (input/output) DOUBLE PRECISION array, dimension (N-1) */
127 /*         Contains the super-diagonal entries of the bidiagonal matrix. */
128 /*         On exit, E has been destroyed. */
129
130 /*  B      (input/output) DOUBLE PRECISION array, dimension (LDB,NRHS) */
131 /*         On input, B contains the right hand sides of the least */
132 /*         squares problem. On output, B contains the solution X. */
133
134 /*  LDB    (input) INTEGER */
135 /*         The leading dimension of B in the calling subprogram. */
136 /*         LDB must be at least max(1,N). */
137
138 /*  RCOND  (input) DOUBLE PRECISION */
139 /*         The singular values of A less than or equal to RCOND times */
140 /*         the largest singular value are treated as zero in solving */
141 /*         the least squares problem. If RCOND is negative, */
142 /*         machine precision is used instead. */
143 /*         For example, if diag(S)*X=B were the least squares problem, */
144 /*         where diag(S) is a diagonal matrix of singular values, the */
145 /*         solution would be X(i) = B(i) / S(i) if S(i) is greater than */
146 /*         RCOND*max(S), and X(i) = 0 if S(i) is less than or equal to */
147 /*         RCOND*max(S). */
148
149 /*  RANK   (output) INTEGER */
150 /*         The number of singular values of A greater than RCOND times */
151 /*         the largest singular value. */
152
153 /*  WORK   (workspace) DOUBLE PRECISION array, dimension at least */
154 /*         (9*N + 2*N*SMLSIZ + 8*N*NLVL + N*NRHS + (SMLSIZ+1)**2), */
155 /*         where NLVL = max(0, INT(log_2 (N/(SMLSIZ+1))) + 1). */
156
157 /*  IWORK  (workspace) INTEGER array, dimension at least */
158 /*         (3*N*NLVL + 11*N) */
159
160 /*  INFO   (output) INTEGER */
161 /*         = 0:  successful exit. */
162 /*         < 0:  if INFO = -i, the i-th argument had an illegal value. */
163 /*         > 0:  The algorithm failed to compute an singular value while */
164 /*               working on the submatrix lying in rows and columns */
165 /*               INFO/(N+1) through MOD(INFO,N+1). */
166
167 /*  Further Details */
168 /*  =============== */
169
170 /*  Based on contributions by */
171 /*     Ming Gu and Ren-Cang Li, Computer Science Division, University of */
172 /*       California at Berkeley, USA */
173 /*     Osni Marques, LBNL/NERSC, USA */
174
175 /*  ===================================================================== */
176
177 /*     .. Parameters .. */
178 /*     .. */
179 /*     .. Local Scalars .. */
180 /*     .. */
181 /*     .. External Functions .. */
182 /*     .. */
183 /*     .. External Subroutines .. */
184 /*     .. */
185 /*     .. Intrinsic Functions .. */
186 /*     .. */
187 /*     .. Executable Statements .. */
188
189 /*     Test the input parameters. */
190
191     /* Parameter adjustments */
192     --d__;
193     --e;
194     b_dim1 = *ldb;
195     b_offset = 1 + b_dim1;
196     b -= b_offset;
197     --work;
198     --iwork;
199
200     /* Function Body */
201     *info = 0;
202
203     if (*n < 0) {
204         *info = -3;
205     } else if (*nrhs < 1) {
206         *info = -4;
207     } else if (*ldb < 1 || *ldb < *n) {
208         *info = -8;
209     }
210     if (*info != 0) {
211         i__1 = -(*info);
212         xerbla_("DLALSD", &i__1);
213         return 0;
214     }
215
216     eps = dlamch_("Epsilon");
217
218 /*     Set up the tolerance. */
219
220     if (*rcond <= 0. || *rcond >= 1.) {
221         rcnd = eps;
222     } else {
223         rcnd = *rcond;
224     }
225
226     *rank = 0;
227
228 /*     Quick return if possible. */
229
230     if (*n == 0) {
231         return 0;
232     } else if (*n == 1) {
233         if (d__[1] == 0.) {
234             dlaset_("A", &c__1, nrhs, &c_b6, &c_b6, &b[b_offset], ldb);
235         } else {
236             *rank = 1;
237             dlascl_("G", &c__0, &c__0, &d__[1], &c_b11, &c__1, nrhs, &b[
238                     b_offset], ldb, info);
239             d__[1] = abs(d__[1]);
240         }
241         return 0;
242     }
243
244 /*     Rotate the matrix if it is lower bidiagonal. */
245
246     if (*(unsigned char *)uplo == 'L') {
247         i__1 = *n - 1;
248         for (i__ = 1; i__ <= i__1; ++i__) {
249             dlartg_(&d__[i__], &e[i__], &cs, &sn, &r__);
250             d__[i__] = r__;
251             e[i__] = sn * d__[i__ + 1];
252             d__[i__ + 1] = cs * d__[i__ + 1];
253             if (*nrhs == 1) {
254                 drot_(&c__1, &b[i__ + b_dim1], &c__1, &b[i__ + 1 + b_dim1], &
255                         c__1, &cs, &sn);
256             } else {
257                 work[(i__ << 1) - 1] = cs;
258                 work[i__ * 2] = sn;
259             }
260 /* L10: */
261         }
262         if (*nrhs > 1) {
263             i__1 = *nrhs;
264             for (i__ = 1; i__ <= i__1; ++i__) {
265                 i__2 = *n - 1;
266                 for (j = 1; j <= i__2; ++j) {
267                     cs = work[(j << 1) - 1];
268                     sn = work[j * 2];
269                     drot_(&c__1, &b[j + i__ * b_dim1], &c__1, &b[j + 1 + i__ *
270                              b_dim1], &c__1, &cs, &sn);
271 /* L20: */
272                 }
273 /* L30: */
274             }
275         }
276     }
277
278 /*     Scale. */
279
280     nm1 = *n - 1;
281     orgnrm = dlanst_("M", n, &d__[1], &e[1]);
282     if (orgnrm == 0.) {
283         dlaset_("A", n, nrhs, &c_b6, &c_b6, &b[b_offset], ldb);
284         return 0;
285     }
286
287     dlascl_("G", &c__0, &c__0, &orgnrm, &c_b11, n, &c__1, &d__[1], n, info);
288     dlascl_("G", &c__0, &c__0, &orgnrm, &c_b11, &nm1, &c__1, &e[1], &nm1, 
289             info);
290
291 /*     If N is smaller than the minimum divide size SMLSIZ, then solve */
292 /*     the problem with another solver. */
293
294     if (*n <= *smlsiz) {
295         nwork = *n * *n + 1;
296         dlaset_("A", n, n, &c_b6, &c_b11, &work[1], n);
297         dlasdq_("U", &c__0, n, n, &c__0, nrhs, &d__[1], &e[1], &work[1], n, &
298                 work[1], n, &b[b_offset], ldb, &work[nwork], info);
299         if (*info != 0) {
300             return 0;
301         }
302         tol = rcnd * (d__1 = d__[idamax_(n, &d__[1], &c__1)], abs(d__1));
303         i__1 = *n;
304         for (i__ = 1; i__ <= i__1; ++i__) {
305             if (d__[i__] <= tol) {
306                 dlaset_("A", &c__1, nrhs, &c_b6, &c_b6, &b[i__ + b_dim1], ldb);
307             } else {
308                 dlascl_("G", &c__0, &c__0, &d__[i__], &c_b11, &c__1, nrhs, &b[
309                         i__ + b_dim1], ldb, info);
310                 ++(*rank);
311             }
312 /* L40: */
313         }
314         dgemm_("T", "N", n, nrhs, n, &c_b11, &work[1], n, &b[b_offset], ldb, &
315                 c_b6, &work[nwork], n);
316         dlacpy_("A", n, nrhs, &work[nwork], n, &b[b_offset], ldb);
317
318 /*        Unscale. */
319
320         dlascl_("G", &c__0, &c__0, &c_b11, &orgnrm, n, &c__1, &d__[1], n, 
321                 info);
322         dlasrt_("D", n, &d__[1], info);
323         dlascl_("G", &c__0, &c__0, &orgnrm, &c_b11, n, nrhs, &b[b_offset], 
324                 ldb, info);
325
326         return 0;
327     }
328
329 /*     Book-keeping and setting up some constants. */
330
331     nlvl = (integer) (log((doublereal) (*n) / (doublereal) (*smlsiz + 1)) / 
332             log(2.)) + 1;
333
334     smlszp = *smlsiz + 1;
335
336     u = 1;
337     vt = *smlsiz * *n + 1;
338     difl = vt + smlszp * *n;
339     difr = difl + nlvl * *n;
340     z__ = difr + (nlvl * *n << 1);
341     c__ = z__ + nlvl * *n;
342     s = c__ + *n;
343     poles = s + *n;
344     givnum = poles + (nlvl << 1) * *n;
345     bx = givnum + (nlvl << 1) * *n;
346     nwork = bx + *n * *nrhs;
347
348     sizei = *n + 1;
349     k = sizei + *n;
350     givptr = k + *n;
351     perm = givptr + *n;
352     givcol = perm + nlvl * *n;
353     iwk = givcol + (nlvl * *n << 1);
354
355     st = 1;
356     sqre = 0;
357     icmpq1 = 1;
358     icmpq2 = 0;
359     nsub = 0;
360
361     i__1 = *n;
362     for (i__ = 1; i__ <= i__1; ++i__) {
363         if ((d__1 = d__[i__], abs(d__1)) < eps) {
364             d__[i__] = d_sign(&eps, &d__[i__]);
365         }
366 /* L50: */
367     }
368
369     i__1 = nm1;
370     for (i__ = 1; i__ <= i__1; ++i__) {
371         if ((d__1 = e[i__], abs(d__1)) < eps || i__ == nm1) {
372             ++nsub;
373             iwork[nsub] = st;
374
375 /*           Subproblem found. First determine its size and then */
376 /*           apply divide and conquer on it. */
377
378             if (i__ < nm1) {
379
380 /*              A subproblem with E(I) small for I < NM1. */
381
382                 nsize = i__ - st + 1;
383                 iwork[sizei + nsub - 1] = nsize;
384             } else if ((d__1 = e[i__], abs(d__1)) >= eps) {
385
386 /*              A subproblem with E(NM1) not too small but I = NM1. */
387
388                 nsize = *n - st + 1;
389                 iwork[sizei + nsub - 1] = nsize;
390             } else {
391
392 /*              A subproblem with E(NM1) small. This implies an */
393 /*              1-by-1 subproblem at D(N), which is not solved */
394 /*              explicitly. */
395
396                 nsize = i__ - st + 1;
397                 iwork[sizei + nsub - 1] = nsize;
398                 ++nsub;
399                 iwork[nsub] = *n;
400                 iwork[sizei + nsub - 1] = 1;
401                 dcopy_(nrhs, &b[*n + b_dim1], ldb, &work[bx + nm1], n);
402             }
403             st1 = st - 1;
404             if (nsize == 1) {
405
406 /*              This is a 1-by-1 subproblem and is not solved */
407 /*              explicitly. */
408
409                 dcopy_(nrhs, &b[st + b_dim1], ldb, &work[bx + st1], n);
410             } else if (nsize <= *smlsiz) {
411
412 /*              This is a small subproblem and is solved by DLASDQ. */
413
414                 dlaset_("A", &nsize, &nsize, &c_b6, &c_b11, &work[vt + st1], 
415                         n);
416                 dlasdq_("U", &c__0, &nsize, &nsize, &c__0, nrhs, &d__[st], &e[
417                         st], &work[vt + st1], n, &work[nwork], n, &b[st + 
418                         b_dim1], ldb, &work[nwork], info);
419                 if (*info != 0) {
420                     return 0;
421                 }
422                 dlacpy_("A", &nsize, nrhs, &b[st + b_dim1], ldb, &work[bx + 
423                         st1], n);
424             } else {
425
426 /*              A large problem. Solve it using divide and conquer. */
427
428                 dlasda_(&icmpq1, smlsiz, &nsize, &sqre, &d__[st], &e[st], &
429                         work[u + st1], n, &work[vt + st1], &iwork[k + st1], &
430                         work[difl + st1], &work[difr + st1], &work[z__ + st1], 
431                          &work[poles + st1], &iwork[givptr + st1], &iwork[
432                         givcol + st1], n, &iwork[perm + st1], &work[givnum + 
433                         st1], &work[c__ + st1], &work[s + st1], &work[nwork], 
434                         &iwork[iwk], info);
435                 if (*info != 0) {
436                     return 0;
437                 }
438                 bxst = bx + st1;
439                 dlalsa_(&icmpq2, smlsiz, &nsize, nrhs, &b[st + b_dim1], ldb, &
440                         work[bxst], n, &work[u + st1], n, &work[vt + st1], &
441                         iwork[k + st1], &work[difl + st1], &work[difr + st1], 
442                         &work[z__ + st1], &work[poles + st1], &iwork[givptr + 
443                         st1], &iwork[givcol + st1], n, &iwork[perm + st1], &
444                         work[givnum + st1], &work[c__ + st1], &work[s + st1], 
445                         &work[nwork], &iwork[iwk], info);
446                 if (*info != 0) {
447                     return 0;
448                 }
449             }
450             st = i__ + 1;
451         }
452 /* L60: */
453     }
454
455 /*     Apply the singular values and treat the tiny ones as zero. */
456
457     tol = rcnd * (d__1 = d__[idamax_(n, &d__[1], &c__1)], abs(d__1));
458
459     i__1 = *n;
460     for (i__ = 1; i__ <= i__1; ++i__) {
461
462 /*        Some of the elements in D can be negative because 1-by-1 */
463 /*        subproblems were not solved explicitly. */
464
465         if ((d__1 = d__[i__], abs(d__1)) <= tol) {
466             dlaset_("A", &c__1, nrhs, &c_b6, &c_b6, &work[bx + i__ - 1], n);
467         } else {
468             ++(*rank);
469             dlascl_("G", &c__0, &c__0, &d__[i__], &c_b11, &c__1, nrhs, &work[
470                     bx + i__ - 1], n, info);
471         }
472         d__[i__] = (d__1 = d__[i__], abs(d__1));
473 /* L70: */
474     }
475
476 /*     Now apply back the right singular vectors. */
477
478     icmpq2 = 1;
479     i__1 = nsub;
480     for (i__ = 1; i__ <= i__1; ++i__) {
481         st = iwork[i__];
482         st1 = st - 1;
483         nsize = iwork[sizei + i__ - 1];
484         bxst = bx + st1;
485         if (nsize == 1) {
486             dcopy_(nrhs, &work[bxst], n, &b[st + b_dim1], ldb);
487         } else if (nsize <= *smlsiz) {
488             dgemm_("T", "N", &nsize, nrhs, &nsize, &c_b11, &work[vt + st1], n, 
489                      &work[bxst], n, &c_b6, &b[st + b_dim1], ldb);
490         } else {
491             dlalsa_(&icmpq2, smlsiz, &nsize, nrhs, &work[bxst], n, &b[st + 
492                     b_dim1], ldb, &work[u + st1], n, &work[vt + st1], &iwork[
493                     k + st1], &work[difl + st1], &work[difr + st1], &work[z__ 
494                     + st1], &work[poles + st1], &iwork[givptr + st1], &iwork[
495                     givcol + st1], n, &iwork[perm + st1], &work[givnum + st1], 
496                      &work[c__ + st1], &work[s + st1], &work[nwork], &iwork[
497                     iwk], info);
498             if (*info != 0) {
499                 return 0;
500             }
501         }
502 /* L80: */
503     }
504
505 /*     Unscale and sort the singular values. */
506
507     dlascl_("G", &c__0, &c__0, &c_b11, &orgnrm, n, &c__1, &d__[1], n, info);
508     dlasrt_("D", n, &d__[1], info);
509     dlascl_("G", &c__0, &c__0, &orgnrm, &c_b11, n, nrhs, &b[b_offset], ldb, 
510             info);
511
512     return 0;
513
514 /*     End of DLALSD */
515
516 } /* dlalsd_ */