Update to 2.0.0 tree from current Fremantle build
[opencv] / 3rdparty / lapack / dlasda.c
1 #include "clapack.h"
2
3 /* Table of constant values */
4
5 static integer c__0 = 0;
6 static doublereal c_b11 = 0.;
7 static doublereal c_b12 = 1.;
8 static integer c__1 = 1;
9 static integer c__2 = 2;
10
11 /* Subroutine */ int dlasda_(integer *icompq, integer *smlsiz, integer *n, 
12         integer *sqre, doublereal *d__, doublereal *e, doublereal *u, integer 
13         *ldu, doublereal *vt, integer *k, doublereal *difl, doublereal *difr, 
14         doublereal *z__, doublereal *poles, integer *givptr, integer *givcol, 
15         integer *ldgcol, integer *perm, doublereal *givnum, doublereal *c__, 
16         doublereal *s, doublereal *work, integer *iwork, integer *info)
17 {
18     /* System generated locals */
19     integer givcol_dim1, givcol_offset, perm_dim1, perm_offset, difl_dim1, 
20             difl_offset, difr_dim1, difr_offset, givnum_dim1, givnum_offset, 
21             poles_dim1, poles_offset, u_dim1, u_offset, vt_dim1, vt_offset, 
22             z_dim1, z_offset, i__1, i__2;
23
24     /* Builtin functions */
25     integer pow_ii(integer *, integer *);
26
27     /* Local variables */
28     integer i__, j, m, i1, ic, lf, nd, ll, nl, vf, nr, vl, im1, ncc, nlf, nrf,
29              vfi, iwk, vli, lvl, nru, ndb1, nlp1, lvl2, nrp1;
30     doublereal beta;
31     integer idxq, nlvl;
32     doublereal alpha;
33     integer inode, ndiml, ndimr, idxqi, itemp;
34     extern /* Subroutine */ int dcopy_(integer *, doublereal *, integer *, 
35             doublereal *, integer *);
36     integer sqrei;
37     extern /* Subroutine */ int dlasd6_(integer *, integer *, integer *, 
38             integer *, doublereal *, doublereal *, doublereal *, doublereal *, 
39              doublereal *, integer *, integer *, integer *, integer *, 
40             integer *, doublereal *, integer *, doublereal *, doublereal *, 
41             doublereal *, doublereal *, integer *, doublereal *, doublereal *, 
42              doublereal *, integer *, integer *);
43     integer nwork1, nwork2;
44     extern /* Subroutine */ int dlasdq_(char *, integer *, integer *, integer 
45             *, integer *, integer *, doublereal *, doublereal *, doublereal *, 
46              integer *, doublereal *, integer *, doublereal *, integer *, 
47             doublereal *, integer *), dlasdt_(integer *, integer *, 
48             integer *, integer *, integer *, integer *, integer *), dlaset_(
49             char *, integer *, integer *, doublereal *, doublereal *, 
50             doublereal *, integer *), xerbla_(char *, integer *);
51     integer smlszp;
52
53
54 /*  -- LAPACK auxiliary routine (version 3.1) -- */
55 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
56 /*     November 2006 */
57
58 /*     .. Scalar Arguments .. */
59 /*     .. */
60 /*     .. Array Arguments .. */
61 /*     .. */
62
63 /*  Purpose */
64 /*  ======= */
65
66 /*  Using a divide and conquer approach, DLASDA computes the singular */
67 /*  value decomposition (SVD) of a real upper bidiagonal N-by-M matrix */
68 /*  B with diagonal D and offdiagonal E, where M = N + SQRE. The */
69 /*  algorithm computes the singular values in the SVD B = U * S * VT. */
70 /*  The orthogonal matrices U and VT are optionally computed in */
71 /*  compact form. */
72
73 /*  A related subroutine, DLASD0, computes the singular values and */
74 /*  the singular vectors in explicit form. */
75
76 /*  Arguments */
77 /*  ========= */
78
79 /*  ICOMPQ (input) INTEGER */
80 /*         Specifies whether singular vectors are to be computed */
81 /*         in compact form, as follows */
82 /*         = 0: Compute singular values only. */
83 /*         = 1: Compute singular vectors of upper bidiagonal */
84 /*              matrix in compact form. */
85
86 /*  SMLSIZ (input) INTEGER */
87 /*         The maximum size of the subproblems at the bottom of the */
88 /*         computation tree. */
89
90 /*  N      (input) INTEGER */
91 /*         The row dimension of the upper bidiagonal matrix. This is */
92 /*         also the dimension of the main diagonal array D. */
93
94 /*  SQRE   (input) INTEGER */
95 /*         Specifies the column dimension of the bidiagonal matrix. */
96 /*         = 0: The bidiagonal matrix has column dimension M = N; */
97 /*         = 1: The bidiagonal matrix has column dimension M = N + 1. */
98
99 /*  D      (input/output) DOUBLE PRECISION array, dimension ( N ) */
100 /*         On entry D contains the main diagonal of the bidiagonal */
101 /*         matrix. On exit D, if INFO = 0, contains its singular values. */
102
103 /*  E      (input) DOUBLE PRECISION array, dimension ( M-1 ) */
104 /*         Contains the subdiagonal entries of the bidiagonal matrix. */
105 /*         On exit, E has been destroyed. */
106
107 /*  U      (output) DOUBLE PRECISION array, */
108 /*         dimension ( LDU, SMLSIZ ) if ICOMPQ = 1, and not referenced */
109 /*         if ICOMPQ = 0. If ICOMPQ = 1, on exit, U contains the left */
110 /*         singular vector matrices of all subproblems at the bottom */
111 /*         level. */
112
113 /*  LDU    (input) INTEGER, LDU = > N. */
114 /*         The leading dimension of arrays U, VT, DIFL, DIFR, POLES, */
115 /*         GIVNUM, and Z. */
116
117 /*  VT     (output) DOUBLE PRECISION array, */
118 /*         dimension ( LDU, SMLSIZ+1 ) if ICOMPQ = 1, and not referenced */
119 /*         if ICOMPQ = 0. If ICOMPQ = 1, on exit, VT' contains the right */
120 /*         singular vector matrices of all subproblems at the bottom */
121 /*         level. */
122
123 /*  K      (output) INTEGER array, */
124 /*         dimension ( N ) if ICOMPQ = 1 and dimension 1 if ICOMPQ = 0. */
125 /*         If ICOMPQ = 1, on exit, K(I) is the dimension of the I-th */
126 /*         secular equation on the computation tree. */
127
128 /*  DIFL   (output) DOUBLE PRECISION array, dimension ( LDU, NLVL ), */
129 /*         where NLVL = floor(log_2 (N/SMLSIZ))). */
130
131 /*  DIFR   (output) DOUBLE PRECISION array, */
132 /*                  dimension ( LDU, 2 * NLVL ) if ICOMPQ = 1 and */
133 /*                  dimension ( N ) if ICOMPQ = 0. */
134 /*         If ICOMPQ = 1, on exit, DIFL(1:N, I) and DIFR(1:N, 2 * I - 1) */
135 /*         record distances between singular values on the I-th */
136 /*         level and singular values on the (I -1)-th level, and */
137 /*         DIFR(1:N, 2 * I ) contains the normalizing factors for */
138 /*         the right singular vector matrix. See DLASD8 for details. */
139
140 /*  Z      (output) DOUBLE PRECISION array, */
141 /*                  dimension ( LDU, NLVL ) if ICOMPQ = 1 and */
142 /*                  dimension ( N ) if ICOMPQ = 0. */
143 /*         The first K elements of Z(1, I) contain the components of */
144 /*         the deflation-adjusted updating row vector for subproblems */
145 /*         on the I-th level. */
146
147 /*  POLES  (output) DOUBLE PRECISION array, */
148 /*         dimension ( LDU, 2 * NLVL ) if ICOMPQ = 1, and not referenced */
149 /*         if ICOMPQ = 0. If ICOMPQ = 1, on exit, POLES(1, 2*I - 1) and */
150 /*         POLES(1, 2*I) contain  the new and old singular values */
151 /*         involved in the secular equations on the I-th level. */
152
153 /*  GIVPTR (output) INTEGER array, */
154 /*         dimension ( N ) if ICOMPQ = 1, and not referenced if */
155 /*         ICOMPQ = 0. If ICOMPQ = 1, on exit, GIVPTR( I ) records */
156 /*         the number of Givens rotations performed on the I-th */
157 /*         problem on the computation tree. */
158
159 /*  GIVCOL (output) INTEGER array, */
160 /*         dimension ( LDGCOL, 2 * NLVL ) if ICOMPQ = 1, and not */
161 /*         referenced if ICOMPQ = 0. If ICOMPQ = 1, on exit, for each I, */
162 /*         GIVCOL(1, 2 *I - 1) and GIVCOL(1, 2 *I) record the locations */
163 /*         of Givens rotations performed on the I-th level on the */
164 /*         computation tree. */
165
166 /*  LDGCOL (input) INTEGER, LDGCOL = > N. */
167 /*         The leading dimension of arrays GIVCOL and PERM. */
168
169 /*  PERM   (output) INTEGER array, */
170 /*         dimension ( LDGCOL, NLVL ) if ICOMPQ = 1, and not referenced */
171 /*         if ICOMPQ = 0. If ICOMPQ = 1, on exit, PERM(1, I) records */
172 /*         permutations done on the I-th level of the computation tree. */
173
174 /*  GIVNUM (output) DOUBLE PRECISION array, */
175 /*         dimension ( LDU,  2 * NLVL ) if ICOMPQ = 1, and not */
176 /*         referenced if ICOMPQ = 0. If ICOMPQ = 1, on exit, for each I, */
177 /*         GIVNUM(1, 2 *I - 1) and GIVNUM(1, 2 *I) record the C- and S- */
178 /*         values of Givens rotations performed on the I-th level on */
179 /*         the computation tree. */
180
181 /*  C      (output) DOUBLE PRECISION array, */
182 /*         dimension ( N ) if ICOMPQ = 1, and dimension 1 if ICOMPQ = 0. */
183 /*         If ICOMPQ = 1 and the I-th subproblem is not square, on exit, */
184 /*         C( I ) contains the C-value of a Givens rotation related to */
185 /*         the right null space of the I-th subproblem. */
186
187 /*  S      (output) DOUBLE PRECISION array, dimension ( N ) if */
188 /*         ICOMPQ = 1, and dimension 1 if ICOMPQ = 0. If ICOMPQ = 1 */
189 /*         and the I-th subproblem is not square, on exit, S( I ) */
190 /*         contains the S-value of a Givens rotation related to */
191 /*         the right null space of the I-th subproblem. */
192
193 /*  WORK   (workspace) DOUBLE PRECISION array, dimension */
194 /*         (6 * N + (SMLSIZ + 1)*(SMLSIZ + 1)). */
195
196 /*  IWORK  (workspace) INTEGER array. */
197 /*         Dimension must be at least (7 * N). */
198
199 /*  INFO   (output) INTEGER */
200 /*          = 0:  successful exit. */
201 /*          < 0:  if INFO = -i, the i-th argument had an illegal value. */
202 /*          > 0:  if INFO = 1, an singular value did not converge */
203
204 /*  Further Details */
205 /*  =============== */
206
207 /*  Based on contributions by */
208 /*     Ming Gu and Huan Ren, Computer Science Division, University of */
209 /*     California at Berkeley, USA */
210
211 /*  ===================================================================== */
212
213 /*     .. Parameters .. */
214 /*     .. */
215 /*     .. Local Scalars .. */
216 /*     .. */
217 /*     .. External Subroutines .. */
218 /*     .. */
219 /*     .. Executable Statements .. */
220
221 /*     Test the input parameters. */
222
223     /* Parameter adjustments */
224     --d__;
225     --e;
226     givnum_dim1 = *ldu;
227     givnum_offset = 1 + givnum_dim1;
228     givnum -= givnum_offset;
229     poles_dim1 = *ldu;
230     poles_offset = 1 + poles_dim1;
231     poles -= poles_offset;
232     z_dim1 = *ldu;
233     z_offset = 1 + z_dim1;
234     z__ -= z_offset;
235     difr_dim1 = *ldu;
236     difr_offset = 1 + difr_dim1;
237     difr -= difr_offset;
238     difl_dim1 = *ldu;
239     difl_offset = 1 + difl_dim1;
240     difl -= difl_offset;
241     vt_dim1 = *ldu;
242     vt_offset = 1 + vt_dim1;
243     vt -= vt_offset;
244     u_dim1 = *ldu;
245     u_offset = 1 + u_dim1;
246     u -= u_offset;
247     --k;
248     --givptr;
249     perm_dim1 = *ldgcol;
250     perm_offset = 1 + perm_dim1;
251     perm -= perm_offset;
252     givcol_dim1 = *ldgcol;
253     givcol_offset = 1 + givcol_dim1;
254     givcol -= givcol_offset;
255     --c__;
256     --s;
257     --work;
258     --iwork;
259
260     /* Function Body */
261     *info = 0;
262
263     if (*icompq < 0 || *icompq > 1) {
264         *info = -1;
265     } else if (*smlsiz < 3) {
266         *info = -2;
267     } else if (*n < 0) {
268         *info = -3;
269     } else if (*sqre < 0 || *sqre > 1) {
270         *info = -4;
271     } else if (*ldu < *n + *sqre) {
272         *info = -8;
273     } else if (*ldgcol < *n) {
274         *info = -17;
275     }
276     if (*info != 0) {
277         i__1 = -(*info);
278         xerbla_("DLASDA", &i__1);
279         return 0;
280     }
281
282     m = *n + *sqre;
283
284 /*     If the input matrix is too small, call DLASDQ to find the SVD. */
285
286     if (*n <= *smlsiz) {
287         if (*icompq == 0) {
288             dlasdq_("U", sqre, n, &c__0, &c__0, &c__0, &d__[1], &e[1], &vt[
289                     vt_offset], ldu, &u[u_offset], ldu, &u[u_offset], ldu, &
290                     work[1], info);
291         } else {
292             dlasdq_("U", sqre, n, &m, n, &c__0, &d__[1], &e[1], &vt[vt_offset]
293 , ldu, &u[u_offset], ldu, &u[u_offset], ldu, &work[1], 
294                     info);
295         }
296         return 0;
297     }
298
299 /*     Book-keeping and  set up the computation tree. */
300
301     inode = 1;
302     ndiml = inode + *n;
303     ndimr = ndiml + *n;
304     idxq = ndimr + *n;
305     iwk = idxq + *n;
306
307     ncc = 0;
308     nru = 0;
309
310     smlszp = *smlsiz + 1;
311     vf = 1;
312     vl = vf + m;
313     nwork1 = vl + m;
314     nwork2 = nwork1 + smlszp * smlszp;
315
316     dlasdt_(n, &nlvl, &nd, &iwork[inode], &iwork[ndiml], &iwork[ndimr], 
317             smlsiz);
318
319 /*     for the nodes on bottom level of the tree, solve */
320 /*     their subproblems by DLASDQ. */
321
322     ndb1 = (nd + 1) / 2;
323     i__1 = nd;
324     for (i__ = ndb1; i__ <= i__1; ++i__) {
325
326 /*        IC : center row of each node */
327 /*        NL : number of rows of left  subproblem */
328 /*        NR : number of rows of right subproblem */
329 /*        NLF: starting row of the left   subproblem */
330 /*        NRF: starting row of the right  subproblem */
331
332         i1 = i__ - 1;
333         ic = iwork[inode + i1];
334         nl = iwork[ndiml + i1];
335         nlp1 = nl + 1;
336         nr = iwork[ndimr + i1];
337         nlf = ic - nl;
338         nrf = ic + 1;
339         idxqi = idxq + nlf - 2;
340         vfi = vf + nlf - 1;
341         vli = vl + nlf - 1;
342         sqrei = 1;
343         if (*icompq == 0) {
344             dlaset_("A", &nlp1, &nlp1, &c_b11, &c_b12, &work[nwork1], &smlszp);
345             dlasdq_("U", &sqrei, &nl, &nlp1, &nru, &ncc, &d__[nlf], &e[nlf], &
346                     work[nwork1], &smlszp, &work[nwork2], &nl, &work[nwork2], 
347                     &nl, &work[nwork2], info);
348             itemp = nwork1 + nl * smlszp;
349             dcopy_(&nlp1, &work[nwork1], &c__1, &work[vfi], &c__1);
350             dcopy_(&nlp1, &work[itemp], &c__1, &work[vli], &c__1);
351         } else {
352             dlaset_("A", &nl, &nl, &c_b11, &c_b12, &u[nlf + u_dim1], ldu);
353             dlaset_("A", &nlp1, &nlp1, &c_b11, &c_b12, &vt[nlf + vt_dim1], 
354                     ldu);
355             dlasdq_("U", &sqrei, &nl, &nlp1, &nl, &ncc, &d__[nlf], &e[nlf], &
356                     vt[nlf + vt_dim1], ldu, &u[nlf + u_dim1], ldu, &u[nlf + 
357                     u_dim1], ldu, &work[nwork1], info);
358             dcopy_(&nlp1, &vt[nlf + vt_dim1], &c__1, &work[vfi], &c__1);
359             dcopy_(&nlp1, &vt[nlf + nlp1 * vt_dim1], &c__1, &work[vli], &c__1)
360                     ;
361         }
362         if (*info != 0) {
363             return 0;
364         }
365         i__2 = nl;
366         for (j = 1; j <= i__2; ++j) {
367             iwork[idxqi + j] = j;
368 /* L10: */
369         }
370         if (i__ == nd && *sqre == 0) {
371             sqrei = 0;
372         } else {
373             sqrei = 1;
374         }
375         idxqi += nlp1;
376         vfi += nlp1;
377         vli += nlp1;
378         nrp1 = nr + sqrei;
379         if (*icompq == 0) {
380             dlaset_("A", &nrp1, &nrp1, &c_b11, &c_b12, &work[nwork1], &smlszp);
381             dlasdq_("U", &sqrei, &nr, &nrp1, &nru, &ncc, &d__[nrf], &e[nrf], &
382                     work[nwork1], &smlszp, &work[nwork2], &nr, &work[nwork2], 
383                     &nr, &work[nwork2], info);
384             itemp = nwork1 + (nrp1 - 1) * smlszp;
385             dcopy_(&nrp1, &work[nwork1], &c__1, &work[vfi], &c__1);
386             dcopy_(&nrp1, &work[itemp], &c__1, &work[vli], &c__1);
387         } else {
388             dlaset_("A", &nr, &nr, &c_b11, &c_b12, &u[nrf + u_dim1], ldu);
389             dlaset_("A", &nrp1, &nrp1, &c_b11, &c_b12, &vt[nrf + vt_dim1], 
390                     ldu);
391             dlasdq_("U", &sqrei, &nr, &nrp1, &nr, &ncc, &d__[nrf], &e[nrf], &
392                     vt[nrf + vt_dim1], ldu, &u[nrf + u_dim1], ldu, &u[nrf + 
393                     u_dim1], ldu, &work[nwork1], info);
394             dcopy_(&nrp1, &vt[nrf + vt_dim1], &c__1, &work[vfi], &c__1);
395             dcopy_(&nrp1, &vt[nrf + nrp1 * vt_dim1], &c__1, &work[vli], &c__1)
396                     ;
397         }
398         if (*info != 0) {
399             return 0;
400         }
401         i__2 = nr;
402         for (j = 1; j <= i__2; ++j) {
403             iwork[idxqi + j] = j;
404 /* L20: */
405         }
406 /* L30: */
407     }
408
409 /*     Now conquer each subproblem bottom-up. */
410
411     j = pow_ii(&c__2, &nlvl);
412     for (lvl = nlvl; lvl >= 1; --lvl) {
413         lvl2 = (lvl << 1) - 1;
414
415 /*        Find the first node LF and last node LL on */
416 /*        the current level LVL. */
417
418         if (lvl == 1) {
419             lf = 1;
420             ll = 1;
421         } else {
422             i__1 = lvl - 1;
423             lf = pow_ii(&c__2, &i__1);
424             ll = (lf << 1) - 1;
425         }
426         i__1 = ll;
427         for (i__ = lf; i__ <= i__1; ++i__) {
428             im1 = i__ - 1;
429             ic = iwork[inode + im1];
430             nl = iwork[ndiml + im1];
431             nr = iwork[ndimr + im1];
432             nlf = ic - nl;
433             nrf = ic + 1;
434             if (i__ == ll) {
435                 sqrei = *sqre;
436             } else {
437                 sqrei = 1;
438             }
439             vfi = vf + nlf - 1;
440             vli = vl + nlf - 1;
441             idxqi = idxq + nlf - 1;
442             alpha = d__[ic];
443             beta = e[ic];
444             if (*icompq == 0) {
445                 dlasd6_(icompq, &nl, &nr, &sqrei, &d__[nlf], &work[vfi], &
446                         work[vli], &alpha, &beta, &iwork[idxqi], &perm[
447                         perm_offset], &givptr[1], &givcol[givcol_offset], 
448                         ldgcol, &givnum[givnum_offset], ldu, &poles[
449                         poles_offset], &difl[difl_offset], &difr[difr_offset], 
450                          &z__[z_offset], &k[1], &c__[1], &s[1], &work[nwork1], 
451                          &iwork[iwk], info);
452             } else {
453                 --j;
454                 dlasd6_(icompq, &nl, &nr, &sqrei, &d__[nlf], &work[vfi], &
455                         work[vli], &alpha, &beta, &iwork[idxqi], &perm[nlf + 
456                         lvl * perm_dim1], &givptr[j], &givcol[nlf + lvl2 * 
457                         givcol_dim1], ldgcol, &givnum[nlf + lvl2 * 
458                         givnum_dim1], ldu, &poles[nlf + lvl2 * poles_dim1], &
459                         difl[nlf + lvl * difl_dim1], &difr[nlf + lvl2 * 
460                         difr_dim1], &z__[nlf + lvl * z_dim1], &k[j], &c__[j], 
461                         &s[j], &work[nwork1], &iwork[iwk], info);
462             }
463             if (*info != 0) {
464                 return 0;
465             }
466 /* L40: */
467         }
468 /* L50: */
469     }
470
471     return 0;
472
473 /*     End of DLASDA */
474
475 } /* dlasda_ */