d2806273487573e359303544df10db8b6265ec68
[profile/ivi/opencv.git] / 3rdparty / lapack / dgebrd.c
1 /* dgebrd.f -- translated by f2c (version 20061008).
2    You must link the resulting object file with libf2c:
3         on Microsoft Windows system, link with libf2c.lib;
4         on Linux or Unix systems, link with .../path/to/libf2c.a -lm
5         or, if you install libf2c.a in a standard place, with -lf2c -lm
6         -- in that order, at the end of the command line, as in
7                 cc *.o -lf2c -lm
8         Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
9
10                 http://www.netlib.org/f2c/libf2c.zip
11 */
12
13 #include "clapack.h"
14
15
16 /* Table of constant values */
17
18 static integer c__1 = 1;
19 static integer c_n1 = -1;
20 static integer c__3 = 3;
21 static integer c__2 = 2;
22 static doublereal c_b21 = -1.;
23 static doublereal c_b22 = 1.;
24
25 /* Subroutine */ int dgebrd_(integer *m, integer *n, doublereal *a, integer *
26         lda, doublereal *d__, doublereal *e, doublereal *tauq, doublereal *
27         taup, doublereal *work, integer *lwork, integer *info)
28 {
29     /* System generated locals */
30     integer a_dim1, a_offset, i__1, i__2, i__3, i__4;
31
32     /* Local variables */
33     integer i__, j, nb, nx;
34     doublereal ws;
35     extern /* Subroutine */ int dgemm_(char *, char *, integer *, integer *, 
36             integer *, doublereal *, doublereal *, integer *, doublereal *, 
37             integer *, doublereal *, doublereal *, integer *);
38     integer nbmin, iinfo, minmn;
39     extern /* Subroutine */ int dgebd2_(integer *, integer *, doublereal *, 
40             integer *, doublereal *, doublereal *, doublereal *, doublereal *, 
41              doublereal *, integer *), dlabrd_(integer *, integer *, integer *
42 , doublereal *, integer *, doublereal *, doublereal *, doublereal 
43             *, doublereal *, doublereal *, integer *, doublereal *, integer *)
44             , xerbla_(char *, integer *);
45     extern integer ilaenv_(integer *, char *, char *, integer *, integer *, 
46             integer *, integer *);
47     integer ldwrkx, ldwrky, lwkopt;
48     logical lquery;
49
50
51 /*  -- LAPACK routine (version 3.2) -- */
52 /*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
53 /*     November 2006 */
54
55 /*     .. Scalar Arguments .. */
56 /*     .. */
57 /*     .. Array Arguments .. */
58 /*     .. */
59
60 /*  Purpose */
61 /*  ======= */
62
63 /*  DGEBRD reduces a general real M-by-N matrix A to upper or lower */
64 /*  bidiagonal form B by an orthogonal transformation: Q**T * A * P = B. */
65
66 /*  If m >= n, B is upper bidiagonal; if m < n, B is lower bidiagonal. */
67
68 /*  Arguments */
69 /*  ========= */
70
71 /*  M       (input) INTEGER */
72 /*          The number of rows in the matrix A.  M >= 0. */
73
74 /*  N       (input) INTEGER */
75 /*          The number of columns in the matrix A.  N >= 0. */
76
77 /*  A       (input/output) DOUBLE PRECISION array, dimension (LDA,N) */
78 /*          On entry, the M-by-N general matrix to be reduced. */
79 /*          On exit, */
80 /*          if m >= n, the diagonal and the first superdiagonal are */
81 /*            overwritten with the upper bidiagonal matrix B; the */
82 /*            elements below the diagonal, with the array TAUQ, represent */
83 /*            the orthogonal matrix Q as a product of elementary */
84 /*            reflectors, and the elements above the first superdiagonal, */
85 /*            with the array TAUP, represent the orthogonal matrix P as */
86 /*            a product of elementary reflectors; */
87 /*          if m < n, the diagonal and the first subdiagonal are */
88 /*            overwritten with the lower bidiagonal matrix B; the */
89 /*            elements below the first subdiagonal, with the array TAUQ, */
90 /*            represent the orthogonal matrix Q as a product of */
91 /*            elementary reflectors, and the elements above the diagonal, */
92 /*            with the array TAUP, represent the orthogonal matrix P as */
93 /*            a product of elementary reflectors. */
94 /*          See Further Details. */
95
96 /*  LDA     (input) INTEGER */
97 /*          The leading dimension of the array A.  LDA >= max(1,M). */
98
99 /*  D       (output) DOUBLE PRECISION array, dimension (min(M,N)) */
100 /*          The diagonal elements of the bidiagonal matrix B: */
101 /*          D(i) = A(i,i). */
102
103 /*  E       (output) DOUBLE PRECISION array, dimension (min(M,N)-1) */
104 /*          The off-diagonal elements of the bidiagonal matrix B: */
105 /*          if m >= n, E(i) = A(i,i+1) for i = 1,2,...,n-1; */
106 /*          if m < n, E(i) = A(i+1,i) for i = 1,2,...,m-1. */
107
108 /*  TAUQ    (output) DOUBLE PRECISION array dimension (min(M,N)) */
109 /*          The scalar factors of the elementary reflectors which */
110 /*          represent the orthogonal matrix Q. See Further Details. */
111
112 /*  TAUP    (output) DOUBLE PRECISION array, dimension (min(M,N)) */
113 /*          The scalar factors of the elementary reflectors which */
114 /*          represent the orthogonal matrix P. See Further Details. */
115
116 /*  WORK    (workspace/output) DOUBLE PRECISION array, dimension (MAX(1,LWORK)) */
117 /*          On exit, if INFO = 0, WORK(1) returns the optimal LWORK. */
118
119 /*  LWORK   (input) INTEGER */
120 /*          The length of the array WORK.  LWORK >= max(1,M,N). */
121 /*          For optimum performance LWORK >= (M+N)*NB, where NB */
122 /*          is the optimal blocksize. */
123
124 /*          If LWORK = -1, then a workspace query is assumed; the routine */
125 /*          only calculates the optimal size of the WORK array, returns */
126 /*          this value as the first entry of the WORK array, and no error */
127 /*          message related to LWORK is issued by XERBLA. */
128
129 /*  INFO    (output) INTEGER */
130 /*          = 0:  successful exit */
131 /*          < 0:  if INFO = -i, the i-th argument had an illegal value. */
132
133 /*  Further Details */
134 /*  =============== */
135
136 /*  The matrices Q and P are represented as products of elementary */
137 /*  reflectors: */
138
139 /*  If m >= n, */
140
141 /*     Q = H(1) H(2) . . . H(n)  and  P = G(1) G(2) . . . G(n-1) */
142
143 /*  Each H(i) and G(i) has the form: */
144
145 /*     H(i) = I - tauq * v * v'  and G(i) = I - taup * u * u' */
146
147 /*  where tauq and taup are real scalars, and v and u are real vectors; */
148 /*  v(1:i-1) = 0, v(i) = 1, and v(i+1:m) is stored on exit in A(i+1:m,i); */
149 /*  u(1:i) = 0, u(i+1) = 1, and u(i+2:n) is stored on exit in A(i,i+2:n); */
150 /*  tauq is stored in TAUQ(i) and taup in TAUP(i). */
151
152 /*  If m < n, */
153
154 /*     Q = H(1) H(2) . . . H(m-1)  and  P = G(1) G(2) . . . G(m) */
155
156 /*  Each H(i) and G(i) has the form: */
157
158 /*     H(i) = I - tauq * v * v'  and G(i) = I - taup * u * u' */
159
160 /*  where tauq and taup are real scalars, and v and u are real vectors; */
161 /*  v(1:i) = 0, v(i+1) = 1, and v(i+2:m) is stored on exit in A(i+2:m,i); */
162 /*  u(1:i-1) = 0, u(i) = 1, and u(i+1:n) is stored on exit in A(i,i+1:n); */
163 /*  tauq is stored in TAUQ(i) and taup in TAUP(i). */
164
165 /*  The contents of A on exit are illustrated by the following examples: */
166
167 /*  m = 6 and n = 5 (m > n):          m = 5 and n = 6 (m < n): */
168
169 /*    (  d   e   u1  u1  u1 )           (  d   u1  u1  u1  u1  u1 ) */
170 /*    (  v1  d   e   u2  u2 )           (  e   d   u2  u2  u2  u2 ) */
171 /*    (  v1  v2  d   e   u3 )           (  v1  e   d   u3  u3  u3 ) */
172 /*    (  v1  v2  v3  d   e  )           (  v1  v2  e   d   u4  u4 ) */
173 /*    (  v1  v2  v3  v4  d  )           (  v1  v2  v3  e   d   u5 ) */
174 /*    (  v1  v2  v3  v4  v5 ) */
175
176 /*  where d and e denote diagonal and off-diagonal elements of B, vi */
177 /*  denotes an element of the vector defining H(i), and ui an element of */
178 /*  the vector defining G(i). */
179
180 /*  ===================================================================== */
181
182 /*     .. Parameters .. */
183 /*     .. */
184 /*     .. Local Scalars .. */
185 /*     .. */
186 /*     .. External Subroutines .. */
187 /*     .. */
188 /*     .. Intrinsic Functions .. */
189 /*     .. */
190 /*     .. External Functions .. */
191 /*     .. */
192 /*     .. Executable Statements .. */
193
194 /*     Test the input parameters */
195
196     /* Parameter adjustments */
197     a_dim1 = *lda;
198     a_offset = 1 + a_dim1;
199     a -= a_offset;
200     --d__;
201     --e;
202     --tauq;
203     --taup;
204     --work;
205
206     /* Function Body */
207     *info = 0;
208 /* Computing MAX */
209     i__1 = 1, i__2 = ilaenv_(&c__1, "DGEBRD", " ", m, n, &c_n1, &c_n1);
210     nb = max(i__1,i__2);
211     lwkopt = (*m + *n) * nb;
212     work[1] = (doublereal) lwkopt;
213     lquery = *lwork == -1;
214     if (*m < 0) {
215         *info = -1;
216     } else if (*n < 0) {
217         *info = -2;
218     } else if (*lda < max(1,*m)) {
219         *info = -4;
220     } else /* if(complicated condition) */ {
221 /* Computing MAX */
222         i__1 = max(1,*m);
223         if (*lwork < max(i__1,*n) && ! lquery) {
224             *info = -10;
225         }
226     }
227     if (*info < 0) {
228         i__1 = -(*info);
229         xerbla_("DGEBRD", &i__1);
230         return 0;
231     } else if (lquery) {
232         return 0;
233     }
234
235 /*     Quick return if possible */
236
237     minmn = min(*m,*n);
238     if (minmn == 0) {
239         work[1] = 1.;
240         return 0;
241     }
242
243     ws = (doublereal) max(*m,*n);
244     ldwrkx = *m;
245     ldwrky = *n;
246
247     if (nb > 1 && nb < minmn) {
248
249 /*        Set the crossover point NX. */
250
251 /* Computing MAX */
252         i__1 = nb, i__2 = ilaenv_(&c__3, "DGEBRD", " ", m, n, &c_n1, &c_n1);
253         nx = max(i__1,i__2);
254
255 /*        Determine when to switch from blocked to unblocked code. */
256
257         if (nx < minmn) {
258             ws = (doublereal) ((*m + *n) * nb);
259             if ((doublereal) (*lwork) < ws) {
260
261 /*              Not enough work space for the optimal NB, consider using */
262 /*              a smaller block size. */
263
264                 nbmin = ilaenv_(&c__2, "DGEBRD", " ", m, n, &c_n1, &c_n1);
265                 if (*lwork >= (*m + *n) * nbmin) {
266                     nb = *lwork / (*m + *n);
267                 } else {
268                     nb = 1;
269                     nx = minmn;
270                 }
271             }
272         }
273     } else {
274         nx = minmn;
275     }
276
277     i__1 = minmn - nx;
278     i__2 = nb;
279     for (i__ = 1; i__2 < 0 ? i__ >= i__1 : i__ <= i__1; i__ += i__2) {
280
281 /*        Reduce rows and columns i:i+nb-1 to bidiagonal form and return */
282 /*        the matrices X and Y which are needed to update the unreduced */
283 /*        part of the matrix */
284
285         i__3 = *m - i__ + 1;
286         i__4 = *n - i__ + 1;
287         dlabrd_(&i__3, &i__4, &nb, &a[i__ + i__ * a_dim1], lda, &d__[i__], &e[
288                 i__], &tauq[i__], &taup[i__], &work[1], &ldwrkx, &work[ldwrkx 
289                 * nb + 1], &ldwrky);
290
291 /*        Update the trailing submatrix A(i+nb:m,i+nb:n), using an update */
292 /*        of the form  A := A - V*Y' - X*U' */
293
294         i__3 = *m - i__ - nb + 1;
295         i__4 = *n - i__ - nb + 1;
296         dgemm_("No transpose", "Transpose", &i__3, &i__4, &nb, &c_b21, &a[i__ 
297                 + nb + i__ * a_dim1], lda, &work[ldwrkx * nb + nb + 1], &
298                 ldwrky, &c_b22, &a[i__ + nb + (i__ + nb) * a_dim1], lda);
299         i__3 = *m - i__ - nb + 1;
300         i__4 = *n - i__ - nb + 1;
301         dgemm_("No transpose", "No transpose", &i__3, &i__4, &nb, &c_b21, &
302                 work[nb + 1], &ldwrkx, &a[i__ + (i__ + nb) * a_dim1], lda, &
303                 c_b22, &a[i__ + nb + (i__ + nb) * a_dim1], lda);
304
305 /*        Copy diagonal and off-diagonal elements of B back into A */
306
307         if (*m >= *n) {
308             i__3 = i__ + nb - 1;
309             for (j = i__; j <= i__3; ++j) {
310                 a[j + j * a_dim1] = d__[j];
311                 a[j + (j + 1) * a_dim1] = e[j];
312 /* L10: */
313             }
314         } else {
315             i__3 = i__ + nb - 1;
316             for (j = i__; j <= i__3; ++j) {
317                 a[j + j * a_dim1] = d__[j];
318                 a[j + 1 + j * a_dim1] = e[j];
319 /* L20: */
320             }
321         }
322 /* L30: */
323     }
324
325 /*     Use unblocked code to reduce the remainder of the matrix */
326
327     i__2 = *m - i__ + 1;
328     i__1 = *n - i__ + 1;
329     dgebd2_(&i__2, &i__1, &a[i__ + i__ * a_dim1], lda, &d__[i__], &e[i__], &
330             tauq[i__], &taup[i__], &work[1], &iinfo);
331     work[1] = ws;
332     return 0;
333
334 /*     End of DGEBRD */
335
336 } /* dgebrd_ */