Update to 2.0.0 tree from current Fremantle build
[opencv] / 3rdparty / lapack / slalsa.c
1 #include "clapack.h"
2
3 /* Table of constant values */
4
5 static real c_b7 = 1.f;
6 static real c_b8 = 0.f;
7 static integer c__2 = 2;
8
9 /* Subroutine */ int slalsa_(integer *icompq, integer *smlsiz, integer *n, 
10         integer *nrhs, real *b, integer *ldb, real *bx, integer *ldbx, real *
11         u, integer *ldu, real *vt, integer *k, real *difl, real *difr, real *
12         z__, real *poles, integer *givptr, integer *givcol, integer *ldgcol, 
13         integer *perm, real *givnum, real *c__, real *s, real *work, integer *
14         iwork, integer *info)
15 {
16     /* System generated locals */
17     integer givcol_dim1, givcol_offset, perm_dim1, perm_offset, b_dim1, 
18             b_offset, bx_dim1, bx_offset, difl_dim1, difl_offset, difr_dim1, 
19             difr_offset, givnum_dim1, givnum_offset, poles_dim1, poles_offset,
20              u_dim1, u_offset, vt_dim1, vt_offset, z_dim1, z_offset, i__1, 
21             i__2;
22
23     /* Builtin functions */
24     integer pow_ii(integer *, integer *);
25
26     /* Local variables */
27     integer i__, j, i1, ic, lf, nd, ll, nl, nr, im1, nlf, nrf, lvl, ndb1, 
28             nlp1, lvl2, nrp1, nlvl, sqre, inode, ndiml;
29     extern /* Subroutine */ int sgemm_(char *, char *, integer *, integer *, 
30             integer *, real *, real *, integer *, real *, integer *, real *, 
31             real *, integer *);
32     integer ndimr;
33     extern /* Subroutine */ int scopy_(integer *, real *, integer *, real *, 
34             integer *), slals0_(integer *, integer *, integer *, integer *, 
35             integer *, real *, integer *, real *, integer *, integer *, 
36             integer *, integer *, integer *, real *, integer *, real *, real *
37 , real *, real *, integer *, real *, real *, real *, integer *), 
38             xerbla_(char *, integer *), slasdt_(integer *, integer *, 
39             integer *, integer *, integer *, integer *, integer *);
40
41
42 /*  -- LAPACK routine (version 3.1) -- */
43 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
44 /*     November 2006 */
45
46 /*     .. Scalar Arguments .. */
47 /*     .. */
48 /*     .. Array Arguments .. */
49 /*     .. */
50
51 /*  Purpose */
52 /*  ======= */
53
54 /*  SLALSA is an itermediate step in solving the least squares problem */
55 /*  by computing the SVD of the coefficient matrix in compact form (The */
56 /*  singular vectors are computed as products of simple orthorgonal */
57 /*  matrices.). */
58
59 /*  If ICOMPQ = 0, SLALSA applies the inverse of the left singular vector */
60 /*  matrix of an upper bidiagonal matrix to the right hand side; and if */
61 /*  ICOMPQ = 1, SLALSA applies the right singular vector matrix to the */
62 /*  right hand side. The singular vector matrices were generated in */
63 /*  compact form by SLALSA. */
64
65 /*  Arguments */
66 /*  ========= */
67
68
69 /*  ICOMPQ (input) INTEGER */
70 /*         Specifies whether the left or the right singular vector */
71 /*         matrix is involved. */
72 /*         = 0: Left singular vector matrix */
73 /*         = 1: Right singular vector matrix */
74
75 /*  SMLSIZ (input) INTEGER */
76 /*         The maximum size of the subproblems at the bottom of the */
77 /*         computation tree. */
78
79 /*  N      (input) INTEGER */
80 /*         The row and column dimensions of the upper bidiagonal matrix. */
81
82 /*  NRHS   (input) INTEGER */
83 /*         The number of columns of B and BX. NRHS must be at least 1. */
84
85 /*  B      (input/output) REAL array, dimension ( LDB, NRHS ) */
86 /*         On input, B contains the right hand sides of the least */
87 /*         squares problem in rows 1 through M. */
88 /*         On output, B contains the solution X in rows 1 through N. */
89
90 /*  LDB    (input) INTEGER */
91 /*         The leading dimension of B in the calling subprogram. */
92 /*         LDB must be at least max(1,MAX( M, N ) ). */
93
94 /*  BX     (output) REAL array, dimension ( LDBX, NRHS ) */
95 /*         On exit, the result of applying the left or right singular */
96 /*         vector matrix to B. */
97
98 /*  LDBX   (input) INTEGER */
99 /*         The leading dimension of BX. */
100
101 /*  U      (input) REAL array, dimension ( LDU, SMLSIZ ). */
102 /*         On entry, U contains the left singular vector matrices of all */
103 /*         subproblems at the bottom level. */
104
105 /*  LDU    (input) INTEGER, LDU = > N. */
106 /*         The leading dimension of arrays U, VT, DIFL, DIFR, */
107 /*         POLES, GIVNUM, and Z. */
108
109 /*  VT     (input) REAL array, dimension ( LDU, SMLSIZ+1 ). */
110 /*         On entry, VT' contains the right singular vector matrices of */
111 /*         all subproblems at the bottom level. */
112
113 /*  K      (input) INTEGER array, dimension ( N ). */
114
115 /*  DIFL   (input) REAL array, dimension ( LDU, NLVL ). */
116 /*         where NLVL = INT(log_2 (N/(SMLSIZ+1))) + 1. */
117
118 /*  DIFR   (input) REAL array, dimension ( LDU, 2 * NLVL ). */
119 /*         On entry, DIFL(*, I) and DIFR(*, 2 * I -1) record */
120 /*         distances between singular values on the I-th level and */
121 /*         singular values on the (I -1)-th level, and DIFR(*, 2 * I) */
122 /*         record the normalizing factors of the right singular vectors */
123 /*         matrices of subproblems on I-th level. */
124
125 /*  Z      (input) REAL array, dimension ( LDU, NLVL ). */
126 /*         On entry, Z(1, I) contains the components of the deflation- */
127 /*         adjusted updating row vector for subproblems on the I-th */
128 /*         level. */
129
130 /*  POLES  (input) REAL array, dimension ( LDU, 2 * NLVL ). */
131 /*         On entry, POLES(*, 2 * I -1: 2 * I) contains the new and old */
132 /*         singular values involved in the secular equations on the I-th */
133 /*         level. */
134
135 /*  GIVPTR (input) INTEGER array, dimension ( N ). */
136 /*         On entry, GIVPTR( I ) records the number of Givens */
137 /*         rotations performed on the I-th problem on the computation */
138 /*         tree. */
139
140 /*  GIVCOL (input) INTEGER array, dimension ( LDGCOL, 2 * NLVL ). */
141 /*         On entry, for each I, GIVCOL(*, 2 * I - 1: 2 * I) records the */
142 /*         locations of Givens rotations performed on the I-th level on */
143 /*         the computation tree. */
144
145 /*  LDGCOL (input) INTEGER, LDGCOL = > N. */
146 /*         The leading dimension of arrays GIVCOL and PERM. */
147
148 /*  PERM   (input) INTEGER array, dimension ( LDGCOL, NLVL ). */
149 /*         On entry, PERM(*, I) records permutations done on the I-th */
150 /*         level of the computation tree. */
151
152 /*  GIVNUM (input) REAL array, dimension ( LDU, 2 * NLVL ). */
153 /*         On entry, GIVNUM(*, 2 *I -1 : 2 * I) records the C- and S- */
154 /*         values of Givens rotations performed on the I-th level on the */
155 /*         computation tree. */
156
157 /*  C      (input) REAL array, dimension ( N ). */
158 /*         On entry, if the I-th subproblem is not square, */
159 /*         C( I ) contains the C-value of a Givens rotation related to */
160 /*         the right null space of the I-th subproblem. */
161
162 /*  S      (input) REAL array, dimension ( N ). */
163 /*         On entry, if the I-th subproblem is not square, */
164 /*         S( I ) contains the S-value of a Givens rotation related to */
165 /*         the right null space of the I-th subproblem. */
166
167 /*  WORK   (workspace) REAL array. */
168 /*         The dimension must be at least N. */
169
170 /*  IWORK  (workspace) INTEGER array. */
171 /*         The dimension must be at least 3 * N */
172
173 /*  INFO   (output) INTEGER */
174 /*          = 0:  successful exit. */
175 /*          < 0:  if INFO = -i, the i-th argument had an illegal value. */
176
177 /*  Further Details */
178 /*  =============== */
179
180 /*  Based on contributions by */
181 /*     Ming Gu and Ren-Cang Li, Computer Science Division, University of */
182 /*       California at Berkeley, USA */
183 /*     Osni Marques, LBNL/NERSC, USA */
184
185 /*  ===================================================================== */
186
187 /*     .. Parameters .. */
188 /*     .. */
189 /*     .. Local Scalars .. */
190 /*     .. */
191 /*     .. External Subroutines .. */
192 /*     .. */
193 /*     .. Executable Statements .. */
194
195 /*     Test the input parameters. */
196
197     /* Parameter adjustments */
198     b_dim1 = *ldb;
199     b_offset = 1 + b_dim1;
200     b -= b_offset;
201     bx_dim1 = *ldbx;
202     bx_offset = 1 + bx_dim1;
203     bx -= bx_offset;
204     givnum_dim1 = *ldu;
205     givnum_offset = 1 + givnum_dim1;
206     givnum -= givnum_offset;
207     poles_dim1 = *ldu;
208     poles_offset = 1 + poles_dim1;
209     poles -= poles_offset;
210     z_dim1 = *ldu;
211     z_offset = 1 + z_dim1;
212     z__ -= z_offset;
213     difr_dim1 = *ldu;
214     difr_offset = 1 + difr_dim1;
215     difr -= difr_offset;
216     difl_dim1 = *ldu;
217     difl_offset = 1 + difl_dim1;
218     difl -= difl_offset;
219     vt_dim1 = *ldu;
220     vt_offset = 1 + vt_dim1;
221     vt -= vt_offset;
222     u_dim1 = *ldu;
223     u_offset = 1 + u_dim1;
224     u -= u_offset;
225     --k;
226     --givptr;
227     perm_dim1 = *ldgcol;
228     perm_offset = 1 + perm_dim1;
229     perm -= perm_offset;
230     givcol_dim1 = *ldgcol;
231     givcol_offset = 1 + givcol_dim1;
232     givcol -= givcol_offset;
233     --c__;
234     --s;
235     --work;
236     --iwork;
237
238     /* Function Body */
239     *info = 0;
240
241     if (*icompq < 0 || *icompq > 1) {
242         *info = -1;
243     } else if (*smlsiz < 3) {
244         *info = -2;
245     } else if (*n < *smlsiz) {
246         *info = -3;
247     } else if (*nrhs < 1) {
248         *info = -4;
249     } else if (*ldb < *n) {
250         *info = -6;
251     } else if (*ldbx < *n) {
252         *info = -8;
253     } else if (*ldu < *n) {
254         *info = -10;
255     } else if (*ldgcol < *n) {
256         *info = -19;
257     }
258     if (*info != 0) {
259         i__1 = -(*info);
260         xerbla_("SLALSA", &i__1);
261         return 0;
262     }
263
264 /*     Book-keeping and  setting up the computation tree. */
265
266     inode = 1;
267     ndiml = inode + *n;
268     ndimr = ndiml + *n;
269
270     slasdt_(n, &nlvl, &nd, &iwork[inode], &iwork[ndiml], &iwork[ndimr], 
271             smlsiz);
272
273 /*     The following code applies back the left singular vector factors. */
274 /*     For applying back the right singular vector factors, go to 50. */
275
276     if (*icompq == 1) {
277         goto L50;
278     }
279
280 /*     The nodes on the bottom level of the tree were solved */
281 /*     by SLASDQ. The corresponding left and right singular vector */
282 /*     matrices are in explicit form. First apply back the left */
283 /*     singular vector matrices. */
284
285     ndb1 = (nd + 1) / 2;
286     i__1 = nd;
287     for (i__ = ndb1; i__ <= i__1; ++i__) {
288
289 /*        IC : center row of each node */
290 /*        NL : number of rows of left  subproblem */
291 /*        NR : number of rows of right subproblem */
292 /*        NLF: starting row of the left   subproblem */
293 /*        NRF: starting row of the right  subproblem */
294
295         i1 = i__ - 1;
296         ic = iwork[inode + i1];
297         nl = iwork[ndiml + i1];
298         nr = iwork[ndimr + i1];
299         nlf = ic - nl;
300         nrf = ic + 1;
301         sgemm_("T", "N", &nl, nrhs, &nl, &c_b7, &u[nlf + u_dim1], ldu, &b[nlf 
302                 + b_dim1], ldb, &c_b8, &bx[nlf + bx_dim1], ldbx);
303         sgemm_("T", "N", &nr, nrhs, &nr, &c_b7, &u[nrf + u_dim1], ldu, &b[nrf 
304                 + b_dim1], ldb, &c_b8, &bx[nrf + bx_dim1], ldbx);
305 /* L10: */
306     }
307
308 /*     Next copy the rows of B that correspond to unchanged rows */
309 /*     in the bidiagonal matrix to BX. */
310
311     i__1 = nd;
312     for (i__ = 1; i__ <= i__1; ++i__) {
313         ic = iwork[inode + i__ - 1];
314         scopy_(nrhs, &b[ic + b_dim1], ldb, &bx[ic + bx_dim1], ldbx);
315 /* L20: */
316     }
317
318 /*     Finally go through the left singular vector matrices of all */
319 /*     the other subproblems bottom-up on the tree. */
320
321     j = pow_ii(&c__2, &nlvl);
322     sqre = 0;
323
324     for (lvl = nlvl; lvl >= 1; --lvl) {
325         lvl2 = (lvl << 1) - 1;
326
327 /*        find the first node LF and last node LL on */
328 /*        the current level LVL */
329
330         if (lvl == 1) {
331             lf = 1;
332             ll = 1;
333         } else {
334             i__1 = lvl - 1;
335             lf = pow_ii(&c__2, &i__1);
336             ll = (lf << 1) - 1;
337         }
338         i__1 = ll;
339         for (i__ = lf; i__ <= i__1; ++i__) {
340             im1 = i__ - 1;
341             ic = iwork[inode + im1];
342             nl = iwork[ndiml + im1];
343             nr = iwork[ndimr + im1];
344             nlf = ic - nl;
345             nrf = ic + 1;
346             --j;
347             slals0_(icompq, &nl, &nr, &sqre, nrhs, &bx[nlf + bx_dim1], ldbx, &
348                     b[nlf + b_dim1], ldb, &perm[nlf + lvl * perm_dim1], &
349                     givptr[j], &givcol[nlf + lvl2 * givcol_dim1], ldgcol, &
350                     givnum[nlf + lvl2 * givnum_dim1], ldu, &poles[nlf + lvl2 *
351                      poles_dim1], &difl[nlf + lvl * difl_dim1], &difr[nlf + 
352                     lvl2 * difr_dim1], &z__[nlf + lvl * z_dim1], &k[j], &c__[
353                     j], &s[j], &work[1], info);
354 /* L30: */
355         }
356 /* L40: */
357     }
358     goto L90;
359
360 /*     ICOMPQ = 1: applying back the right singular vector factors. */
361
362 L50:
363
364 /*     First now go through the right singular vector matrices of all */
365 /*     the tree nodes top-down. */
366
367     j = 0;
368     i__1 = nlvl;
369     for (lvl = 1; lvl <= i__1; ++lvl) {
370         lvl2 = (lvl << 1) - 1;
371
372 /*        Find the first node LF and last node LL on */
373 /*        the current level LVL. */
374
375         if (lvl == 1) {
376             lf = 1;
377             ll = 1;
378         } else {
379             i__2 = lvl - 1;
380             lf = pow_ii(&c__2, &i__2);
381             ll = (lf << 1) - 1;
382         }
383         i__2 = lf;
384         for (i__ = ll; i__ >= i__2; --i__) {
385             im1 = i__ - 1;
386             ic = iwork[inode + im1];
387             nl = iwork[ndiml + im1];
388             nr = iwork[ndimr + im1];
389             nlf = ic - nl;
390             nrf = ic + 1;
391             if (i__ == ll) {
392                 sqre = 0;
393             } else {
394                 sqre = 1;
395             }
396             ++j;
397             slals0_(icompq, &nl, &nr, &sqre, nrhs, &b[nlf + b_dim1], ldb, &bx[
398                     nlf + bx_dim1], ldbx, &perm[nlf + lvl * perm_dim1], &
399                     givptr[j], &givcol[nlf + lvl2 * givcol_dim1], ldgcol, &
400                     givnum[nlf + lvl2 * givnum_dim1], ldu, &poles[nlf + lvl2 *
401                      poles_dim1], &difl[nlf + lvl * difl_dim1], &difr[nlf + 
402                     lvl2 * difr_dim1], &z__[nlf + lvl * z_dim1], &k[j], &c__[
403                     j], &s[j], &work[1], info);
404 /* L60: */
405         }
406 /* L70: */
407     }
408
409 /*     The nodes on the bottom level of the tree were solved */
410 /*     by SLASDQ. The corresponding right singular vector */
411 /*     matrices are in explicit form. Apply them back. */
412
413     ndb1 = (nd + 1) / 2;
414     i__1 = nd;
415     for (i__ = ndb1; i__ <= i__1; ++i__) {
416         i1 = i__ - 1;
417         ic = iwork[inode + i1];
418         nl = iwork[ndiml + i1];
419         nr = iwork[ndimr + i1];
420         nlp1 = nl + 1;
421         if (i__ == nd) {
422             nrp1 = nr;
423         } else {
424             nrp1 = nr + 1;
425         }
426         nlf = ic - nl;
427         nrf = ic + 1;
428         sgemm_("T", "N", &nlp1, nrhs, &nlp1, &c_b7, &vt[nlf + vt_dim1], ldu, &
429                 b[nlf + b_dim1], ldb, &c_b8, &bx[nlf + bx_dim1], ldbx);
430         sgemm_("T", "N", &nrp1, nrhs, &nrp1, &c_b7, &vt[nrf + vt_dim1], ldu, &
431                 b[nrf + b_dim1], ldb, &c_b8, &bx[nrf + bx_dim1], ldbx);
432 /* L80: */
433     }
434
435 L90:
436
437     return 0;
438
439 /*     End of SLALSA */
440
441 } /* slalsa_ */