Update to 2.0.0 tree from current Fremantle build
[opencv] / 3rdparty / lapack / slasd6.c
1 #include "clapack.h"
2
3 /* Table of constant values */
4
5 static integer c__0 = 0;
6 static real c_b7 = 1.f;
7 static integer c__1 = 1;
8 static integer c_n1 = -1;
9
10 /* Subroutine */ int slasd6_(integer *icompq, integer *nl, integer *nr, 
11         integer *sqre, real *d__, real *vf, real *vl, real *alpha, real *beta, 
12          integer *idxq, integer *perm, integer *givptr, integer *givcol, 
13         integer *ldgcol, real *givnum, integer *ldgnum, real *poles, real *
14         difl, real *difr, real *z__, integer *k, real *c__, real *s, real *
15         work, integer *iwork, integer *info)
16 {
17     /* System generated locals */
18     integer givcol_dim1, givcol_offset, givnum_dim1, givnum_offset, 
19             poles_dim1, poles_offset, i__1;
20     real r__1, r__2;
21
22     /* Local variables */
23     integer i__, m, n, n1, n2, iw, idx, idxc, idxp, ivfw, ivlw;
24     extern /* Subroutine */ int scopy_(integer *, real *, integer *, real *, 
25             integer *), slasd7_(integer *, integer *, integer *, integer *, 
26             integer *, real *, real *, real *, real *, real *, real *, real *, 
27              real *, real *, real *, integer *, integer *, integer *, integer 
28             *, integer *, integer *, integer *, real *, integer *, real *, 
29             real *, integer *), slasd8_(integer *, integer *, real *, real *, 
30             real *, real *, real *, real *, integer *, real *, real *, 
31             integer *);
32     integer isigma;
33     extern /* Subroutine */ int xerbla_(char *, integer *), slascl_(
34             char *, integer *, integer *, real *, real *, integer *, integer *
35 , real *, integer *, integer *), slamrg_(integer *, 
36             integer *, real *, integer *, integer *, integer *);
37     real orgnrm;
38
39
40 /*  -- LAPACK auxiliary routine (version 3.1) -- */
41 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
42 /*     November 2006 */
43
44 /*     .. Scalar Arguments .. */
45 /*     .. */
46 /*     .. Array Arguments .. */
47 /*     .. */
48
49 /*  Purpose */
50 /*  ======= */
51
52 /*  SLASD6 computes the SVD of an updated upper bidiagonal matrix B */
53 /*  obtained by merging two smaller ones by appending a row. This */
54 /*  routine is used only for the problem which requires all singular */
55 /*  values and optionally singular vector matrices in factored form. */
56 /*  B is an N-by-M matrix with N = NL + NR + 1 and M = N + SQRE. */
57 /*  A related subroutine, SLASD1, handles the case in which all singular */
58 /*  values and singular vectors of the bidiagonal matrix are desired. */
59
60 /*  SLASD6 computes the SVD as follows: */
61
62 /*                ( D1(in)  0    0     0 ) */
63 /*    B = U(in) * (   Z1'   a   Z2'    b ) * VT(in) */
64 /*                (   0     0   D2(in) 0 ) */
65
66 /*      = U(out) * ( D(out) 0) * VT(out) */
67
68 /*  where Z' = (Z1' a Z2' b) = u' VT', and u is a vector of dimension M */
69 /*  with ALPHA and BETA in the NL+1 and NL+2 th entries and zeros */
70 /*  elsewhere; and the entry b is empty if SQRE = 0. */
71
72 /*  The singular values of B can be computed using D1, D2, the first */
73 /*  components of all the right singular vectors of the lower block, and */
74 /*  the last components of all the right singular vectors of the upper */
75 /*  block. These components are stored and updated in VF and VL, */
76 /*  respectively, in SLASD6. Hence U and VT are not explicitly */
77 /*  referenced. */
78
79 /*  The singular values are stored in D. The algorithm consists of two */
80 /*  stages: */
81
82 /*        The first stage consists of deflating the size of the problem */
83 /*        when there are multiple singular values or if there is a zero */
84 /*        in the Z vector. For each such occurence the dimension of the */
85 /*        secular equation problem is reduced by one. This stage is */
86 /*        performed by the routine SLASD7. */
87
88 /*        The second stage consists of calculating the updated */
89 /*        singular values. This is done by finding the roots of the */
90 /*        secular equation via the routine SLASD4 (as called by SLASD8). */
91 /*        This routine also updates VF and VL and computes the distances */
92 /*        between the updated singular values and the old singular */
93 /*        values. */
94
95 /*  SLASD6 is called from SLASDA. */
96
97 /*  Arguments */
98 /*  ========= */
99
100 /*  ICOMPQ (input) INTEGER */
101 /*         Specifies whether singular vectors are to be computed in */
102 /*         factored form: */
103 /*         = 0: Compute singular values only. */
104 /*         = 1: Compute singular vectors in factored form as well. */
105
106 /*  NL     (input) INTEGER */
107 /*         The row dimension of the upper block.  NL >= 1. */
108
109 /*  NR     (input) INTEGER */
110 /*         The row dimension of the lower block.  NR >= 1. */
111
112 /*  SQRE   (input) INTEGER */
113 /*         = 0: the lower block is an NR-by-NR square matrix. */
114 /*         = 1: the lower block is an NR-by-(NR+1) rectangular matrix. */
115
116 /*         The bidiagonal matrix has row dimension N = NL + NR + 1, */
117 /*         and column dimension M = N + SQRE. */
118
119 /*  D      (input/output) REAL array, dimension (NL+NR+1). */
120 /*         On entry D(1:NL,1:NL) contains the singular values of the */
121 /*         upper block, and D(NL+2:N) contains the singular values */
122 /*         of the lower block. On exit D(1:N) contains the singular */
123 /*         values of the modified matrix. */
124
125 /*  VF     (input/output) REAL array, dimension (M) */
126 /*         On entry, VF(1:NL+1) contains the first components of all */
127 /*         right singular vectors of the upper block; and VF(NL+2:M) */
128 /*         contains the first components of all right singular vectors */
129 /*         of the lower block. On exit, VF contains the first components */
130 /*         of all right singular vectors of the bidiagonal matrix. */
131
132 /*  VL     (input/output) REAL array, dimension (M) */
133 /*         On entry, VL(1:NL+1) contains the  last components of all */
134 /*         right singular vectors of the upper block; and VL(NL+2:M) */
135 /*         contains the last components of all right singular vectors of */
136 /*         the lower block. On exit, VL contains the last components of */
137 /*         all right singular vectors of the bidiagonal matrix. */
138
139 /*  ALPHA  (input/output) REAL */
140 /*         Contains the diagonal element associated with the added row. */
141
142 /*  BETA   (input/output) REAL */
143 /*         Contains the off-diagonal element associated with the added */
144 /*         row. */
145
146 /*  IDXQ   (output) INTEGER array, dimension (N) */
147 /*         This contains the permutation which will reintegrate the */
148 /*         subproblem just solved back into sorted order, i.e. */
149 /*         D( IDXQ( I = 1, N ) ) will be in ascending order. */
150
151 /*  PERM   (output) INTEGER array, dimension ( N ) */
152 /*         The permutations (from deflation and sorting) to be applied */
153 /*         to each block. Not referenced if ICOMPQ = 0. */
154
155 /*  GIVPTR (output) INTEGER */
156 /*         The number of Givens rotations which took place in this */
157 /*         subproblem. Not referenced if ICOMPQ = 0. */
158
159 /*  GIVCOL (output) INTEGER array, dimension ( LDGCOL, 2 ) */
160 /*         Each pair of numbers indicates a pair of columns to take place */
161 /*         in a Givens rotation. Not referenced if ICOMPQ = 0. */
162
163 /*  LDGCOL (input) INTEGER */
164 /*         leading dimension of GIVCOL, must be at least N. */
165
166 /*  GIVNUM (output) REAL array, dimension ( LDGNUM, 2 ) */
167 /*         Each number indicates the C or S value to be used in the */
168 /*         corresponding Givens rotation. Not referenced if ICOMPQ = 0. */
169
170 /*  LDGNUM (input) INTEGER */
171 /*         The leading dimension of GIVNUM and POLES, must be at least N. */
172
173 /*  POLES  (output) REAL array, dimension ( LDGNUM, 2 ) */
174 /*         On exit, POLES(1,*) is an array containing the new singular */
175 /*         values obtained from solving the secular equation, and */
176 /*         POLES(2,*) is an array containing the poles in the secular */
177 /*         equation. Not referenced if ICOMPQ = 0. */
178
179 /*  DIFL   (output) REAL array, dimension ( N ) */
180 /*         On exit, DIFL(I) is the distance between I-th updated */
181 /*         (undeflated) singular value and the I-th (undeflated) old */
182 /*         singular value. */
183
184 /*  DIFR   (output) REAL array, */
185 /*                  dimension ( LDGNUM, 2 ) if ICOMPQ = 1 and */
186 /*                  dimension ( N ) if ICOMPQ = 0. */
187 /*         On exit, DIFR(I, 1) is the distance between I-th updated */
188 /*         (undeflated) singular value and the I+1-th (undeflated) old */
189 /*         singular value. */
190
191 /*         If ICOMPQ = 1, DIFR(1:K,2) is an array containing the */
192 /*         normalizing factors for the right singular vector matrix. */
193
194 /*         See SLASD8 for details on DIFL and DIFR. */
195
196 /*  Z      (output) REAL array, dimension ( M ) */
197 /*         The first elements of this array contain the components */
198 /*         of the deflation-adjusted updating row vector. */
199
200 /*  K      (output) INTEGER */
201 /*         Contains the dimension of the non-deflated matrix, */
202 /*         This is the order of the related secular equation. 1 <= K <=N. */
203
204 /*  C      (output) REAL */
205 /*         C contains garbage if SQRE =0 and the C-value of a Givens */
206 /*         rotation related to the right null space if SQRE = 1. */
207
208 /*  S      (output) REAL */
209 /*         S contains garbage if SQRE =0 and the S-value of a Givens */
210 /*         rotation related to the right null space if SQRE = 1. */
211
212 /*  WORK   (workspace) REAL array, dimension ( 4 * M ) */
213
214 /*  IWORK  (workspace) INTEGER array, dimension ( 3 * N ) */
215
216 /*  INFO   (output) INTEGER */
217 /*          = 0:  successful exit. */
218 /*          < 0:  if INFO = -i, the i-th argument had an illegal value. */
219 /*          > 0:  if INFO = 1, an singular value did not converge */
220
221 /*  Further Details */
222 /*  =============== */
223
224 /*  Based on contributions by */
225 /*     Ming Gu and Huan Ren, Computer Science Division, University of */
226 /*     California at Berkeley, USA */
227
228 /*  ===================================================================== */
229
230 /*     .. Parameters .. */
231 /*     .. */
232 /*     .. Local Scalars .. */
233 /*     .. */
234 /*     .. External Subroutines .. */
235 /*     .. */
236 /*     .. Intrinsic Functions .. */
237 /*     .. */
238 /*     .. Executable Statements .. */
239
240 /*     Test the input parameters. */
241
242     /* Parameter adjustments */
243     --d__;
244     --vf;
245     --vl;
246     --idxq;
247     --perm;
248     givcol_dim1 = *ldgcol;
249     givcol_offset = 1 + givcol_dim1;
250     givcol -= givcol_offset;
251     poles_dim1 = *ldgnum;
252     poles_offset = 1 + poles_dim1;
253     poles -= poles_offset;
254     givnum_dim1 = *ldgnum;
255     givnum_offset = 1 + givnum_dim1;
256     givnum -= givnum_offset;
257     --difl;
258     --difr;
259     --z__;
260     --work;
261     --iwork;
262
263     /* Function Body */
264     *info = 0;
265     n = *nl + *nr + 1;
266     m = n + *sqre;
267
268     if (*icompq < 0 || *icompq > 1) {
269         *info = -1;
270     } else if (*nl < 1) {
271         *info = -2;
272     } else if (*nr < 1) {
273         *info = -3;
274     } else if (*sqre < 0 || *sqre > 1) {
275         *info = -4;
276     } else if (*ldgcol < n) {
277         *info = -14;
278     } else if (*ldgnum < n) {
279         *info = -16;
280     }
281     if (*info != 0) {
282         i__1 = -(*info);
283         xerbla_("SLASD6", &i__1);
284         return 0;
285     }
286
287 /*     The following values are for bookkeeping purposes only.  They are */
288 /*     integer pointers which indicate the portion of the workspace */
289 /*     used by a particular array in SLASD7 and SLASD8. */
290
291     isigma = 1;
292     iw = isigma + n;
293     ivfw = iw + m;
294     ivlw = ivfw + m;
295
296     idx = 1;
297     idxc = idx + n;
298     idxp = idxc + n;
299
300 /*     Scale. */
301
302 /* Computing MAX */
303     r__1 = dabs(*alpha), r__2 = dabs(*beta);
304     orgnrm = dmax(r__1,r__2);
305     d__[*nl + 1] = 0.f;
306     i__1 = n;
307     for (i__ = 1; i__ <= i__1; ++i__) {
308         if ((r__1 = d__[i__], dabs(r__1)) > orgnrm) {
309             orgnrm = (r__1 = d__[i__], dabs(r__1));
310         }
311 /* L10: */
312     }
313     slascl_("G", &c__0, &c__0, &orgnrm, &c_b7, &n, &c__1, &d__[1], &n, info);
314     *alpha /= orgnrm;
315     *beta /= orgnrm;
316
317 /*     Sort and Deflate singular values. */
318
319     slasd7_(icompq, nl, nr, sqre, k, &d__[1], &z__[1], &work[iw], &vf[1], &
320             work[ivfw], &vl[1], &work[ivlw], alpha, beta, &work[isigma], &
321             iwork[idx], &iwork[idxp], &idxq[1], &perm[1], givptr, &givcol[
322             givcol_offset], ldgcol, &givnum[givnum_offset], ldgnum, c__, s, 
323             info);
324
325 /*     Solve Secular Equation, compute DIFL, DIFR, and update VF, VL. */
326
327     slasd8_(icompq, k, &d__[1], &z__[1], &vf[1], &vl[1], &difl[1], &difr[1], 
328             ldgnum, &work[isigma], &work[iw], info);
329
330 /*     Save the poles if ICOMPQ = 1. */
331
332     if (*icompq == 1) {
333         scopy_(k, &d__[1], &c__1, &poles[poles_dim1 + 1], &c__1);
334         scopy_(k, &work[isigma], &c__1, &poles[(poles_dim1 << 1) + 1], &c__1);
335     }
336
337 /*     Unscale. */
338
339     slascl_("G", &c__0, &c__0, &c_b7, &orgnrm, &n, &c__1, &d__[1], &n, info);
340
341 /*     Prepare the IDXQ sorting permutation. */
342
343     n1 = *k;
344     n2 = n - *k;
345     slamrg_(&n1, &n2, &d__[1], &c__1, &c_n1, &idxq[1]);
346
347     return 0;
348
349 /*     End of SLASD6 */
350
351 } /* slasd6_ */