Update to 2.0.0 tree from current Fremantle build
[opencv] / 3rdparty / lapack / dlals0.c
1 #include "clapack.h"
2
3 /* Table of constant values */
4
5 static doublereal c_b5 = -1.;
6 static integer c__1 = 1;
7 static doublereal c_b11 = 1.;
8 static doublereal c_b13 = 0.;
9 static integer c__0 = 0;
10
11 /* Subroutine */ int dlals0_(integer *icompq, integer *nl, integer *nr, 
12         integer *sqre, integer *nrhs, doublereal *b, integer *ldb, doublereal 
13         *bx, integer *ldbx, integer *perm, integer *givptr, integer *givcol, 
14         integer *ldgcol, doublereal *givnum, integer *ldgnum, doublereal *
15         poles, doublereal *difl, doublereal *difr, doublereal *z__, integer *
16         k, doublereal *c__, doublereal *s, doublereal *work, integer *info)
17 {
18     /* System generated locals */
19     integer givcol_dim1, givcol_offset, b_dim1, b_offset, bx_dim1, bx_offset, 
20             difr_dim1, difr_offset, givnum_dim1, givnum_offset, poles_dim1, 
21             poles_offset, i__1, i__2;
22     doublereal d__1;
23
24     /* Local variables */
25     integer i__, j, m, n;
26     doublereal dj;
27     integer nlp1;
28     doublereal temp;
29     extern /* Subroutine */ int drot_(integer *, doublereal *, integer *, 
30             doublereal *, integer *, doublereal *, doublereal *);
31     extern doublereal dnrm2_(integer *, doublereal *, integer *);
32     extern /* Subroutine */ int dscal_(integer *, doublereal *, doublereal *, 
33             integer *);
34     doublereal diflj, difrj, dsigj;
35     extern /* Subroutine */ int dgemv_(char *, integer *, integer *, 
36             doublereal *, doublereal *, integer *, doublereal *, integer *, 
37             doublereal *, doublereal *, integer *), dcopy_(integer *, 
38             doublereal *, integer *, doublereal *, integer *);
39     extern doublereal dlamc3_(doublereal *, doublereal *);
40     extern /* Subroutine */ int dlascl_(char *, integer *, integer *, 
41             doublereal *, doublereal *, integer *, integer *, doublereal *, 
42             integer *, integer *), dlacpy_(char *, integer *, integer 
43             *, doublereal *, integer *, doublereal *, integer *), 
44             xerbla_(char *, integer *);
45     doublereal dsigjp;
46
47
48 /*  -- LAPACK routine (version 3.1) -- */
49 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
50 /*     November 2006 */
51
52 /*     .. Scalar Arguments .. */
53 /*     .. */
54 /*     .. Array Arguments .. */
55 /*     .. */
56
57 /*  Purpose */
58 /*  ======= */
59
60 /*  DLALS0 applies back the multiplying factors of either the left or the */
61 /*  right singular vector matrix of a diagonal matrix appended by a row */
62 /*  to the right hand side matrix B in solving the least squares problem */
63 /*  using the divide-and-conquer SVD approach. */
64
65 /*  For the left singular vector matrix, three types of orthogonal */
66 /*  matrices are involved: */
67
68 /*  (1L) Givens rotations: the number of such rotations is GIVPTR; the */
69 /*       pairs of columns/rows they were applied to are stored in GIVCOL; */
70 /*       and the C- and S-values of these rotations are stored in GIVNUM. */
71
72 /*  (2L) Permutation. The (NL+1)-st row of B is to be moved to the first */
73 /*       row, and for J=2:N, PERM(J)-th row of B is to be moved to the */
74 /*       J-th row. */
75
76 /*  (3L) The left singular vector matrix of the remaining matrix. */
77
78 /*  For the right singular vector matrix, four types of orthogonal */
79 /*  matrices are involved: */
80
81 /*  (1R) The right singular vector matrix of the remaining matrix. */
82
83 /*  (2R) If SQRE = 1, one extra Givens rotation to generate the right */
84 /*       null space. */
85
86 /*  (3R) The inverse transformation of (2L). */
87
88 /*  (4R) The inverse transformation of (1L). */
89
90 /*  Arguments */
91 /*  ========= */
92
93 /*  ICOMPQ (input) INTEGER */
94 /*         Specifies whether singular vectors are to be computed in */
95 /*         factored form: */
96 /*         = 0: Left singular vector matrix. */
97 /*         = 1: Right singular vector matrix. */
98
99 /*  NL     (input) INTEGER */
100 /*         The row dimension of the upper block. NL >= 1. */
101
102 /*  NR     (input) INTEGER */
103 /*         The row dimension of the lower block. NR >= 1. */
104
105 /*  SQRE   (input) INTEGER */
106 /*         = 0: the lower block is an NR-by-NR square matrix. */
107 /*         = 1: the lower block is an NR-by-(NR+1) rectangular matrix. */
108
109 /*         The bidiagonal matrix has row dimension N = NL + NR + 1, */
110 /*         and column dimension M = N + SQRE. */
111
112 /*  NRHS   (input) INTEGER */
113 /*         The number of columns of B and BX. NRHS must be at least 1. */
114
115 /*  B      (input/output) DOUBLE PRECISION array, dimension ( LDB, NRHS ) */
116 /*         On input, B contains the right hand sides of the least */
117 /*         squares problem in rows 1 through M. On output, B contains */
118 /*         the solution X in rows 1 through N. */
119
120 /*  LDB    (input) INTEGER */
121 /*         The leading dimension of B. LDB must be at least */
122 /*         max(1,MAX( M, N ) ). */
123
124 /*  BX     (workspace) DOUBLE PRECISION array, dimension ( LDBX, NRHS ) */
125
126 /*  LDBX   (input) INTEGER */
127 /*         The leading dimension of BX. */
128
129 /*  PERM   (input) INTEGER array, dimension ( N ) */
130 /*         The permutations (from deflation and sorting) applied */
131 /*         to the two blocks. */
132
133 /*  GIVPTR (input) INTEGER */
134 /*         The number of Givens rotations which took place in this */
135 /*         subproblem. */
136
137 /*  GIVCOL (input) INTEGER array, dimension ( LDGCOL, 2 ) */
138 /*         Each pair of numbers indicates a pair of rows/columns */
139 /*         involved in a Givens rotation. */
140
141 /*  LDGCOL (input) INTEGER */
142 /*         The leading dimension of GIVCOL, must be at least N. */
143
144 /*  GIVNUM (input) DOUBLE PRECISION array, dimension ( LDGNUM, 2 ) */
145 /*         Each number indicates the C or S value used in the */
146 /*         corresponding Givens rotation. */
147
148 /*  LDGNUM (input) INTEGER */
149 /*         The leading dimension of arrays DIFR, POLES and */
150 /*         GIVNUM, must be at least K. */
151
152 /*  POLES  (input) DOUBLE PRECISION array, dimension ( LDGNUM, 2 ) */
153 /*         On entry, POLES(1:K, 1) contains the new singular */
154 /*         values obtained from solving the secular equation, and */
155 /*         POLES(1:K, 2) is an array containing the poles in the secular */
156 /*         equation. */
157
158 /*  DIFL   (input) DOUBLE PRECISION array, dimension ( K ). */
159 /*         On entry, DIFL(I) is the distance between I-th updated */
160 /*         (undeflated) singular value and the I-th (undeflated) old */
161 /*         singular value. */
162
163 /*  DIFR   (input) DOUBLE PRECISION array, dimension ( LDGNUM, 2 ). */
164 /*         On entry, DIFR(I, 1) contains the distances between I-th */
165 /*         updated (undeflated) singular value and the I+1-th */
166 /*         (undeflated) old singular value. And DIFR(I, 2) is the */
167 /*         normalizing factor for the I-th right singular vector. */
168
169 /*  Z      (input) DOUBLE PRECISION array, dimension ( K ) */
170 /*         Contain the components of the deflation-adjusted updating row */
171 /*         vector. */
172
173 /*  K      (input) INTEGER */
174 /*         Contains the dimension of the non-deflated matrix, */
175 /*         This is the order of the related secular equation. 1 <= K <=N. */
176
177 /*  C      (input) DOUBLE PRECISION */
178 /*         C contains garbage if SQRE =0 and the C-value of a Givens */
179 /*         rotation related to the right null space if SQRE = 1. */
180
181 /*  S      (input) DOUBLE PRECISION */
182 /*         S contains garbage if SQRE =0 and the S-value of a Givens */
183 /*         rotation related to the right null space if SQRE = 1. */
184
185 /*  WORK   (workspace) DOUBLE PRECISION array, dimension ( K ) */
186
187 /*  INFO   (output) INTEGER */
188 /*          = 0:  successful exit. */
189 /*          < 0:  if INFO = -i, the i-th argument had an illegal value. */
190
191 /*  Further Details */
192 /*  =============== */
193
194 /*  Based on contributions by */
195 /*     Ming Gu and Ren-Cang Li, Computer Science Division, University of */
196 /*       California at Berkeley, USA */
197 /*     Osni Marques, LBNL/NERSC, USA */
198
199 /*  ===================================================================== */
200
201 /*     .. Parameters .. */
202 /*     .. */
203 /*     .. Local Scalars .. */
204 /*     .. */
205 /*     .. External Subroutines .. */
206 /*     .. */
207 /*     .. External Functions .. */
208 /*     .. */
209 /*     .. Intrinsic Functions .. */
210 /*     .. */
211 /*     .. Executable Statements .. */
212
213 /*     Test the input parameters. */
214
215     /* Parameter adjustments */
216     b_dim1 = *ldb;
217     b_offset = 1 + b_dim1;
218     b -= b_offset;
219     bx_dim1 = *ldbx;
220     bx_offset = 1 + bx_dim1;
221     bx -= bx_offset;
222     --perm;
223     givcol_dim1 = *ldgcol;
224     givcol_offset = 1 + givcol_dim1;
225     givcol -= givcol_offset;
226     difr_dim1 = *ldgnum;
227     difr_offset = 1 + difr_dim1;
228     difr -= difr_offset;
229     poles_dim1 = *ldgnum;
230     poles_offset = 1 + poles_dim1;
231     poles -= poles_offset;
232     givnum_dim1 = *ldgnum;
233     givnum_offset = 1 + givnum_dim1;
234     givnum -= givnum_offset;
235     --difl;
236     --z__;
237     --work;
238
239     /* Function Body */
240     *info = 0;
241
242     if (*icompq < 0 || *icompq > 1) {
243         *info = -1;
244     } else if (*nl < 1) {
245         *info = -2;
246     } else if (*nr < 1) {
247         *info = -3;
248     } else if (*sqre < 0 || *sqre > 1) {
249         *info = -4;
250     }
251
252     n = *nl + *nr + 1;
253
254     if (*nrhs < 1) {
255         *info = -5;
256     } else if (*ldb < n) {
257         *info = -7;
258     } else if (*ldbx < n) {
259         *info = -9;
260     } else if (*givptr < 0) {
261         *info = -11;
262     } else if (*ldgcol < n) {
263         *info = -13;
264     } else if (*ldgnum < n) {
265         *info = -15;
266     } else if (*k < 1) {
267         *info = -20;
268     }
269     if (*info != 0) {
270         i__1 = -(*info);
271         xerbla_("DLALS0", &i__1);
272         return 0;
273     }
274
275     m = n + *sqre;
276     nlp1 = *nl + 1;
277
278     if (*icompq == 0) {
279
280 /*        Apply back orthogonal transformations from the left. */
281
282 /*        Step (1L): apply back the Givens rotations performed. */
283
284         i__1 = *givptr;
285         for (i__ = 1; i__ <= i__1; ++i__) {
286             drot_(nrhs, &b[givcol[i__ + (givcol_dim1 << 1)] + b_dim1], ldb, &
287                     b[givcol[i__ + givcol_dim1] + b_dim1], ldb, &givnum[i__ + 
288                     (givnum_dim1 << 1)], &givnum[i__ + givnum_dim1]);
289 /* L10: */
290         }
291
292 /*        Step (2L): permute rows of B. */
293
294         dcopy_(nrhs, &b[nlp1 + b_dim1], ldb, &bx[bx_dim1 + 1], ldbx);
295         i__1 = n;
296         for (i__ = 2; i__ <= i__1; ++i__) {
297             dcopy_(nrhs, &b[perm[i__] + b_dim1], ldb, &bx[i__ + bx_dim1], 
298                     ldbx);
299 /* L20: */
300         }
301
302 /*        Step (3L): apply the inverse of the left singular vector */
303 /*        matrix to BX. */
304
305         if (*k == 1) {
306             dcopy_(nrhs, &bx[bx_offset], ldbx, &b[b_offset], ldb);
307             if (z__[1] < 0.) {
308                 dscal_(nrhs, &c_b5, &b[b_offset], ldb);
309             }
310         } else {
311             i__1 = *k;
312             for (j = 1; j <= i__1; ++j) {
313                 diflj = difl[j];
314                 dj = poles[j + poles_dim1];
315                 dsigj = -poles[j + (poles_dim1 << 1)];
316                 if (j < *k) {
317                     difrj = -difr[j + difr_dim1];
318                     dsigjp = -poles[j + 1 + (poles_dim1 << 1)];
319                 }
320                 if (z__[j] == 0. || poles[j + (poles_dim1 << 1)] == 0.) {
321                     work[j] = 0.;
322                 } else {
323                     work[j] = -poles[j + (poles_dim1 << 1)] * z__[j] / diflj /
324                              (poles[j + (poles_dim1 << 1)] + dj);
325                 }
326                 i__2 = j - 1;
327                 for (i__ = 1; i__ <= i__2; ++i__) {
328                     if (z__[i__] == 0. || poles[i__ + (poles_dim1 << 1)] == 
329                             0.) {
330                         work[i__] = 0.;
331                     } else {
332                         work[i__] = poles[i__ + (poles_dim1 << 1)] * z__[i__] 
333                                 / (dlamc3_(&poles[i__ + (poles_dim1 << 1)], &
334                                 dsigj) - diflj) / (poles[i__ + (poles_dim1 << 
335                                 1)] + dj);
336                     }
337 /* L30: */
338                 }
339                 i__2 = *k;
340                 for (i__ = j + 1; i__ <= i__2; ++i__) {
341                     if (z__[i__] == 0. || poles[i__ + (poles_dim1 << 1)] == 
342                             0.) {
343                         work[i__] = 0.;
344                     } else {
345                         work[i__] = poles[i__ + (poles_dim1 << 1)] * z__[i__] 
346                                 / (dlamc3_(&poles[i__ + (poles_dim1 << 1)], &
347                                 dsigjp) + difrj) / (poles[i__ + (poles_dim1 <<
348                                  1)] + dj);
349                     }
350 /* L40: */
351                 }
352                 work[1] = -1.;
353                 temp = dnrm2_(k, &work[1], &c__1);
354                 dgemv_("T", k, nrhs, &c_b11, &bx[bx_offset], ldbx, &work[1], &
355                         c__1, &c_b13, &b[j + b_dim1], ldb);
356                 dlascl_("G", &c__0, &c__0, &temp, &c_b11, &c__1, nrhs, &b[j + 
357                         b_dim1], ldb, info);
358 /* L50: */
359             }
360         }
361
362 /*        Move the deflated rows of BX to B also. */
363
364         if (*k < max(m,n)) {
365             i__1 = n - *k;
366             dlacpy_("A", &i__1, nrhs, &bx[*k + 1 + bx_dim1], ldbx, &b[*k + 1 
367                     + b_dim1], ldb);
368         }
369     } else {
370
371 /*        Apply back the right orthogonal transformations. */
372
373 /*        Step (1R): apply back the new right singular vector matrix */
374 /*        to B. */
375
376         if (*k == 1) {
377             dcopy_(nrhs, &b[b_offset], ldb, &bx[bx_offset], ldbx);
378         } else {
379             i__1 = *k;
380             for (j = 1; j <= i__1; ++j) {
381                 dsigj = poles[j + (poles_dim1 << 1)];
382                 if (z__[j] == 0.) {
383                     work[j] = 0.;
384                 } else {
385                     work[j] = -z__[j] / difl[j] / (dsigj + poles[j + 
386                             poles_dim1]) / difr[j + (difr_dim1 << 1)];
387                 }
388                 i__2 = j - 1;
389                 for (i__ = 1; i__ <= i__2; ++i__) {
390                     if (z__[j] == 0.) {
391                         work[i__] = 0.;
392                     } else {
393                         d__1 = -poles[i__ + 1 + (poles_dim1 << 1)];
394                         work[i__] = z__[j] / (dlamc3_(&dsigj, &d__1) - difr[
395                                 i__ + difr_dim1]) / (dsigj + poles[i__ + 
396                                 poles_dim1]) / difr[i__ + (difr_dim1 << 1)];
397                     }
398 /* L60: */
399                 }
400                 i__2 = *k;
401                 for (i__ = j + 1; i__ <= i__2; ++i__) {
402                     if (z__[j] == 0.) {
403                         work[i__] = 0.;
404                     } else {
405                         d__1 = -poles[i__ + (poles_dim1 << 1)];
406                         work[i__] = z__[j] / (dlamc3_(&dsigj, &d__1) - difl[
407                                 i__]) / (dsigj + poles[i__ + poles_dim1]) / 
408                                 difr[i__ + (difr_dim1 << 1)];
409                     }
410 /* L70: */
411                 }
412                 dgemv_("T", k, nrhs, &c_b11, &b[b_offset], ldb, &work[1], &
413                         c__1, &c_b13, &bx[j + bx_dim1], ldbx);
414 /* L80: */
415             }
416         }
417
418 /*        Step (2R): if SQRE = 1, apply back the rotation that is */
419 /*        related to the right null space of the subproblem. */
420
421         if (*sqre == 1) {
422             dcopy_(nrhs, &b[m + b_dim1], ldb, &bx[m + bx_dim1], ldbx);
423             drot_(nrhs, &bx[bx_dim1 + 1], ldbx, &bx[m + bx_dim1], ldbx, c__, 
424                     s);
425         }
426         if (*k < max(m,n)) {
427             i__1 = n - *k;
428             dlacpy_("A", &i__1, nrhs, &b[*k + 1 + b_dim1], ldb, &bx[*k + 1 + 
429                     bx_dim1], ldbx);
430         }
431
432 /*        Step (3R): permute rows of B. */
433
434         dcopy_(nrhs, &bx[bx_dim1 + 1], ldbx, &b[nlp1 + b_dim1], ldb);
435         if (*sqre == 1) {
436             dcopy_(nrhs, &bx[m + bx_dim1], ldbx, &b[m + b_dim1], ldb);
437         }
438         i__1 = n;
439         for (i__ = 2; i__ <= i__1; ++i__) {
440             dcopy_(nrhs, &bx[i__ + bx_dim1], ldbx, &b[perm[i__] + b_dim1], 
441                     ldb);
442 /* L90: */
443         }
444
445 /*        Step (4R): apply back the Givens rotations performed. */
446
447         for (i__ = *givptr; i__ >= 1; --i__) {
448             d__1 = -givnum[i__ + givnum_dim1];
449             drot_(nrhs, &b[givcol[i__ + (givcol_dim1 << 1)] + b_dim1], ldb, &
450                     b[givcol[i__ + givcol_dim1] + b_dim1], ldb, &givnum[i__ + 
451                     (givnum_dim1 << 1)], &d__1);
452 /* L100: */
453         }
454     }
455
456     return 0;
457
458 /*     End of DLALS0 */
459
460 } /* dlals0_ */