C_LAPACK: Fixes to make it compile with MSVC (#3605)
[platform/upstream/openblas.git] / lapack-netlib / SRC / dorcsd2by1.c
1 #include <math.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <stdio.h>
5 #include <complex.h>
6 #ifdef complex
7 #undef complex
8 #endif
9 #ifdef I
10 #undef I
11 #endif
12
13 #if defined(_WIN64)
14 typedef long long BLASLONG;
15 typedef unsigned long long BLASULONG;
16 #else
17 typedef long BLASLONG;
18 typedef unsigned long BLASULONG;
19 #endif
20
21 #ifdef LAPACK_ILP64
22 typedef BLASLONG blasint;
23 #if defined(_WIN64)
24 #define blasabs(x) llabs(x)
25 #else
26 #define blasabs(x) labs(x)
27 #endif
28 #else
29 typedef int blasint;
30 #define blasabs(x) abs(x)
31 #endif
32
33 typedef blasint integer;
34
35 typedef unsigned int uinteger;
36 typedef char *address;
37 typedef short int shortint;
38 typedef float real;
39 typedef double doublereal;
40 typedef struct { real r, i; } complex;
41 typedef struct { doublereal r, i; } doublecomplex;
42 #ifdef _MSC_VER
43 static inline _Fcomplex Cf(complex *z) {_Fcomplex zz={z->r , z->i}; return zz;}
44 static inline _Dcomplex Cd(doublecomplex *z) {_Dcomplex zz={z->r , z->i};return zz;}
45 static inline _Fcomplex * _pCf(complex *z) {return (_Fcomplex*)z;}
46 static inline _Dcomplex * _pCd(doublecomplex *z) {return (_Dcomplex*)z;}
47 #else
48 static inline _Complex float Cf(complex *z) {return z->r + z->i*_Complex_I;}
49 static inline _Complex double Cd(doublecomplex *z) {return z->r + z->i*_Complex_I;}
50 static inline _Complex float * _pCf(complex *z) {return (_Complex float*)z;}
51 static inline _Complex double * _pCd(doublecomplex *z) {return (_Complex double*)z;}
52 #endif
53 #define pCf(z) (*_pCf(z))
54 #define pCd(z) (*_pCd(z))
55 typedef int logical;
56 typedef short int shortlogical;
57 typedef char logical1;
58 typedef char integer1;
59
60 #define TRUE_ (1)
61 #define FALSE_ (0)
62
63 /* Extern is for use with -E */
64 #ifndef Extern
65 #define Extern extern
66 #endif
67
68 /* I/O stuff */
69
70 typedef int flag;
71 typedef int ftnlen;
72 typedef int ftnint;
73
74 /*external read, write*/
75 typedef struct
76 {       flag cierr;
77         ftnint ciunit;
78         flag ciend;
79         char *cifmt;
80         ftnint cirec;
81 } cilist;
82
83 /*internal read, write*/
84 typedef struct
85 {       flag icierr;
86         char *iciunit;
87         flag iciend;
88         char *icifmt;
89         ftnint icirlen;
90         ftnint icirnum;
91 } icilist;
92
93 /*open*/
94 typedef struct
95 {       flag oerr;
96         ftnint ounit;
97         char *ofnm;
98         ftnlen ofnmlen;
99         char *osta;
100         char *oacc;
101         char *ofm;
102         ftnint orl;
103         char *oblnk;
104 } olist;
105
106 /*close*/
107 typedef struct
108 {       flag cerr;
109         ftnint cunit;
110         char *csta;
111 } cllist;
112
113 /*rewind, backspace, endfile*/
114 typedef struct
115 {       flag aerr;
116         ftnint aunit;
117 } alist;
118
119 /* inquire */
120 typedef struct
121 {       flag inerr;
122         ftnint inunit;
123         char *infile;
124         ftnlen infilen;
125         ftnint  *inex;  /*parameters in standard's order*/
126         ftnint  *inopen;
127         ftnint  *innum;
128         ftnint  *innamed;
129         char    *inname;
130         ftnlen  innamlen;
131         char    *inacc;
132         ftnlen  inacclen;
133         char    *inseq;
134         ftnlen  inseqlen;
135         char    *indir;
136         ftnlen  indirlen;
137         char    *infmt;
138         ftnlen  infmtlen;
139         char    *inform;
140         ftnint  informlen;
141         char    *inunf;
142         ftnlen  inunflen;
143         ftnint  *inrecl;
144         ftnint  *innrec;
145         char    *inblank;
146         ftnlen  inblanklen;
147 } inlist;
148
149 #define VOID void
150
151 union Multitype {       /* for multiple entry points */
152         integer1 g;
153         shortint h;
154         integer i;
155         /* longint j; */
156         real r;
157         doublereal d;
158         complex c;
159         doublecomplex z;
160         };
161
162 typedef union Multitype Multitype;
163
164 struct Vardesc {        /* for Namelist */
165         char *name;
166         char *addr;
167         ftnlen *dims;
168         int  type;
169         };
170 typedef struct Vardesc Vardesc;
171
172 struct Namelist {
173         char *name;
174         Vardesc **vars;
175         int nvars;
176         };
177 typedef struct Namelist Namelist;
178
179 #define abs(x) ((x) >= 0 ? (x) : -(x))
180 #define dabs(x) (fabs(x))
181 #define f2cmin(a,b) ((a) <= (b) ? (a) : (b))
182 #define f2cmax(a,b) ((a) >= (b) ? (a) : (b))
183 #define dmin(a,b) (f2cmin(a,b))
184 #define dmax(a,b) (f2cmax(a,b))
185 #define bit_test(a,b)   ((a) >> (b) & 1)
186 #define bit_clear(a,b)  ((a) & ~((uinteger)1 << (b)))
187 #define bit_set(a,b)    ((a) |  ((uinteger)1 << (b)))
188
189 #define abort_() { sig_die("Fortran abort routine called", 1); }
190 #define c_abs(z) (cabsf(Cf(z)))
191 #define c_cos(R,Z) { pCf(R)=ccos(Cf(Z)); }
192 #ifdef _MSC_VER
193 #define c_div(c, a, b) {Cf(c)._Val[0] = (Cf(a)._Val[0]/Cf(b)._Val[0]); Cf(c)._Val[1]=(Cf(a)._Val[1]/Cf(b)._Val[1]);}
194 #define z_div(c, a, b) {Cd(c)._Val[0] = (Cd(a)._Val[0]/Cd(b)._Val[0]); Cd(c)._Val[1]=(Cd(a)._Val[1]/df(b)._Val[1]);}
195 #else
196 #define c_div(c, a, b) {pCf(c) = Cf(a)/Cf(b);}
197 #define z_div(c, a, b) {pCd(c) = Cd(a)/Cd(b);}
198 #endif
199 #define c_exp(R, Z) {pCf(R) = cexpf(Cf(Z));}
200 #define c_log(R, Z) {pCf(R) = clogf(Cf(Z));}
201 #define c_sin(R, Z) {pCf(R) = csinf(Cf(Z));}
202 //#define c_sqrt(R, Z) {*(R) = csqrtf(Cf(Z));}
203 #define c_sqrt(R, Z) {pCf(R) = csqrtf(Cf(Z));}
204 #define d_abs(x) (fabs(*(x)))
205 #define d_acos(x) (acos(*(x)))
206 #define d_asin(x) (asin(*(x)))
207 #define d_atan(x) (atan(*(x)))
208 #define d_atn2(x, y) (atan2(*(x),*(y)))
209 #define d_cnjg(R, Z) { pCd(R) = conj(Cd(Z)); }
210 #define r_cnjg(R, Z) { pCf(R) = conjf(Cf(Z)); }
211 #define d_cos(x) (cos(*(x)))
212 #define d_cosh(x) (cosh(*(x)))
213 #define d_dim(__a, __b) ( *(__a) > *(__b) ? *(__a) - *(__b) : 0.0 )
214 #define d_exp(x) (exp(*(x)))
215 #define d_imag(z) (cimag(Cd(z)))
216 #define r_imag(z) (cimagf(Cf(z)))
217 #define d_int(__x) (*(__x)>0 ? floor(*(__x)) : -floor(- *(__x)))
218 #define r_int(__x) (*(__x)>0 ? floor(*(__x)) : -floor(- *(__x)))
219 #define d_lg10(x) ( 0.43429448190325182765 * log(*(x)) )
220 #define r_lg10(x) ( 0.43429448190325182765 * log(*(x)) )
221 #define d_log(x) (log(*(x)))
222 #define d_mod(x, y) (fmod(*(x), *(y)))
223 #define u_nint(__x) ((__x)>=0 ? floor((__x) + .5) : -floor(.5 - (__x)))
224 #define d_nint(x) u_nint(*(x))
225 #define u_sign(__a,__b) ((__b) >= 0 ? ((__a) >= 0 ? (__a) : -(__a)) : -((__a) >= 0 ? (__a) : -(__a)))
226 #define d_sign(a,b) u_sign(*(a),*(b))
227 #define r_sign(a,b) u_sign(*(a),*(b))
228 #define d_sin(x) (sin(*(x)))
229 #define d_sinh(x) (sinh(*(x)))
230 #define d_sqrt(x) (sqrt(*(x)))
231 #define d_tan(x) (tan(*(x)))
232 #define d_tanh(x) (tanh(*(x)))
233 #define i_abs(x) abs(*(x))
234 #define i_dnnt(x) ((integer)u_nint(*(x)))
235 #define i_len(s, n) (n)
236 #define i_nint(x) ((integer)u_nint(*(x)))
237 #define i_sign(a,b) ((integer)u_sign((integer)*(a),(integer)*(b)))
238 #define pow_dd(ap, bp) ( pow(*(ap), *(bp)))
239 #define pow_si(B,E) spow_ui(*(B),*(E))
240 #define pow_ri(B,E) spow_ui(*(B),*(E))
241 #define pow_di(B,E) dpow_ui(*(B),*(E))
242 #define pow_zi(p, a, b) {pCd(p) = zpow_ui(Cd(a), *(b));}
243 #define pow_ci(p, a, b) {pCf(p) = cpow_ui(Cf(a), *(b));}
244 #define pow_zz(R,A,B) {pCd(R) = cpow(Cd(A),*(B));}
245 #define s_cat(lpp, rpp, rnp, np, llp) {         ftnlen i, nc, ll; char *f__rp, *lp;     ll = (llp); lp = (lpp);         for(i=0; i < (int)*(np); ++i) {                 nc = ll;                if((rnp)[i] < nc) nc = (rnp)[i];                ll -= nc;               f__rp = (rpp)[i];               while(--nc >= 0) *lp++ = *(f__rp)++;         }  while(--ll >= 0) *lp++ = ' '; }
246 #define s_cmp(a,b,c,d) ((integer)strncmp((a),(b),f2cmin((c),(d))))
247 #define s_copy(A,B,C,D) { int __i,__m; for (__i=0, __m=f2cmin((C),(D)); __i<__m && (B)[__i] != 0; ++__i) (A)[__i] = (B)[__i]; }
248 #define sig_die(s, kill) { exit(1); }
249 #define s_stop(s, n) {exit(0);}
250 static char junk[] = "\n@(#)LIBF77 VERSION 19990503\n";
251 #define z_abs(z) (cabs(Cd(z)))
252 #define z_exp(R, Z) {pCd(R) = cexp(Cd(Z));}
253 #define z_sqrt(R, Z) {pCd(R) = csqrt(Cd(Z));}
254 #define myexit_() break;
255 #define mycycle() continue;
256 #define myceiling(w) {ceil(w)}
257 #define myhuge(w) {HUGE_VAL}
258 //#define mymaxloc_(w,s,e,n) {if (sizeof(*(w)) == sizeof(double)) dmaxloc_((w),*(s),*(e),n); else dmaxloc_((w),*(s),*(e),n);}
259 #define mymaxloc(w,s,e,n) {dmaxloc_(w,*(s),*(e),n)}
260
261 /* procedure parameter types for -A and -C++ */
262
263 #define F2C_proc_par_types 1
264 #ifdef __cplusplus
265 typedef logical (*L_fp)(...);
266 #else
267 typedef logical (*L_fp)();
268 #endif
269
270 static float spow_ui(float x, integer n) {
271         float pow=1.0; unsigned long int u;
272         if(n != 0) {
273                 if(n < 0) n = -n, x = 1/x;
274                 for(u = n; ; ) {
275                         if(u & 01) pow *= x;
276                         if(u >>= 1) x *= x;
277                         else break;
278                 }
279         }
280         return pow;
281 }
282 static double dpow_ui(double x, integer n) {
283         double pow=1.0; unsigned long int u;
284         if(n != 0) {
285                 if(n < 0) n = -n, x = 1/x;
286                 for(u = n; ; ) {
287                         if(u & 01) pow *= x;
288                         if(u >>= 1) x *= x;
289                         else break;
290                 }
291         }
292         return pow;
293 }
294 #ifdef _MSC_VER
295 static _Fcomplex cpow_ui(complex x, integer n) {
296         complex pow={1.0,0.0}; unsigned long int u;
297                 if(n != 0) {
298                 if(n < 0) n = -n, x.r = 1/x.r, x.i=1/x.i;
299                 for(u = n; ; ) {
300                         if(u & 01) pow.r *= x.r, pow.i *= x.i;
301                         if(u >>= 1) x.r *= x.r, x.i *= x.i;
302                         else break;
303                 }
304         }
305         _Fcomplex p={pow.r, pow.i};
306         return p;
307 }
308 #else
309 static _Complex float cpow_ui(_Complex float x, integer n) {
310         _Complex float pow=1.0; unsigned long int u;
311         if(n != 0) {
312                 if(n < 0) n = -n, x = 1/x;
313                 for(u = n; ; ) {
314                         if(u & 01) pow *= x;
315                         if(u >>= 1) x *= x;
316                         else break;
317                 }
318         }
319         return pow;
320 }
321 #endif
322 #ifdef _MSC_VER
323 static _Dcomplex zpow_ui(_Dcomplex x, integer n) {
324         _Dcomplex pow={1.0,0.0}; unsigned long int u;
325         if(n != 0) {
326                 if(n < 0) n = -n, x._Val[0] = 1/x._Val[0], x._Val[1] =1/x._Val[1];
327                 for(u = n; ; ) {
328                         if(u & 01) pow._Val[0] *= x._Val[0], pow._Val[1] *= x._Val[1];
329                         if(u >>= 1) x._Val[0] *= x._Val[0], x._Val[1] *= x._Val[1];
330                         else break;
331                 }
332         }
333         _Dcomplex p = {pow._Val[0], pow._Val[1]};
334         return p;
335 }
336 #else
337 static _Complex double zpow_ui(_Complex double x, integer n) {
338         _Complex double pow=1.0; unsigned long int u;
339         if(n != 0) {
340                 if(n < 0) n = -n, x = 1/x;
341                 for(u = n; ; ) {
342                         if(u & 01) pow *= x;
343                         if(u >>= 1) x *= x;
344                         else break;
345                 }
346         }
347         return pow;
348 }
349 #endif
350 static integer pow_ii(integer x, integer n) {
351         integer pow; unsigned long int u;
352         if (n <= 0) {
353                 if (n == 0 || x == 1) pow = 1;
354                 else if (x != -1) pow = x == 0 ? 1/x : 0;
355                 else n = -n;
356         }
357         if ((n > 0) || !(n == 0 || x == 1 || x != -1)) {
358                 u = n;
359                 for(pow = 1; ; ) {
360                         if(u & 01) pow *= x;
361                         if(u >>= 1) x *= x;
362                         else break;
363                 }
364         }
365         return pow;
366 }
367 static integer dmaxloc_(double *w, integer s, integer e, integer *n)
368 {
369         double m; integer i, mi;
370         for(m=w[s-1], mi=s, i=s+1; i<=e; i++)
371                 if (w[i-1]>m) mi=i ,m=w[i-1];
372         return mi-s+1;
373 }
374 static integer smaxloc_(float *w, integer s, integer e, integer *n)
375 {
376         float m; integer i, mi;
377         for(m=w[s-1], mi=s, i=s+1; i<=e; i++)
378                 if (w[i-1]>m) mi=i ,m=w[i-1];
379         return mi-s+1;
380 }
381 static inline void cdotc_(complex *z, integer *n_, complex *x, integer *incx_, complex *y, integer *incy_) {
382         integer n = *n_, incx = *incx_, incy = *incy_, i;
383 #ifdef _MSC_VER
384         _Fcomplex zdotc = {0.0, 0.0};
385         if (incx == 1 && incy == 1) {
386                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
387                         zdotc._Val[0] += conjf(Cf(&x[i]))._Val[0] * Cf(&y[i])._Val[0];
388                         zdotc._Val[1] += conjf(Cf(&x[i]))._Val[1] * Cf(&y[i])._Val[1];
389                 }
390         } else {
391                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
392                         zdotc._Val[0] += conjf(Cf(&x[i*incx]))._Val[0] * Cf(&y[i*incy])._Val[0];
393                         zdotc._Val[1] += conjf(Cf(&x[i*incx]))._Val[1] * Cf(&y[i*incy])._Val[1];
394                 }
395         }
396         pCf(z) = zdotc;
397 }
398 #else
399         _Complex float zdotc = 0.0;
400         if (incx == 1 && incy == 1) {
401                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
402                         zdotc += conjf(Cf(&x[i])) * Cf(&y[i]);
403                 }
404         } else {
405                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
406                         zdotc += conjf(Cf(&x[i*incx])) * Cf(&y[i*incy]);
407                 }
408         }
409         pCf(z) = zdotc;
410 }
411 #endif
412 static inline void zdotc_(doublecomplex *z, integer *n_, doublecomplex *x, integer *incx_, doublecomplex *y, integer *incy_) {
413         integer n = *n_, incx = *incx_, incy = *incy_, i;
414 #ifdef _MSC_VER
415         _Dcomplex zdotc = {0.0, 0.0};
416         if (incx == 1 && incy == 1) {
417                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
418                         zdotc._Val[0] += conj(Cd(&x[i]))._Val[0] * Cd(&y[i])._Val[0];
419                         zdotc._Val[1] += conj(Cd(&x[i]))._Val[1] * Cd(&y[i])._Val[1];
420                 }
421         } else {
422                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
423                         zdotc._Val[0] += conj(Cd(&x[i*incx]))._Val[0] * Cd(&y[i*incy])._Val[0];
424                         zdotc._Val[1] += conj(Cd(&x[i*incx]))._Val[1] * Cd(&y[i*incy])._Val[1];
425                 }
426         }
427         pCd(z) = zdotc;
428 }
429 #else
430         _Complex double zdotc = 0.0;
431         if (incx == 1 && incy == 1) {
432                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
433                         zdotc += conj(Cd(&x[i])) * Cd(&y[i]);
434                 }
435         } else {
436                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
437                         zdotc += conj(Cd(&x[i*incx])) * Cd(&y[i*incy]);
438                 }
439         }
440         pCd(z) = zdotc;
441 }
442 #endif  
443 static inline void cdotu_(complex *z, integer *n_, complex *x, integer *incx_, complex *y, integer *incy_) {
444         integer n = *n_, incx = *incx_, incy = *incy_, i;
445 #ifdef _MSC_VER
446         _Fcomplex zdotc = {0.0, 0.0};
447         if (incx == 1 && incy == 1) {
448                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
449                         zdotc._Val[0] += Cf(&x[i])._Val[0] * Cf(&y[i])._Val[0];
450                         zdotc._Val[1] += Cf(&x[i])._Val[1] * Cf(&y[i])._Val[1];
451                 }
452         } else {
453                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
454                         zdotc._Val[0] += Cf(&x[i*incx])._Val[0] * Cf(&y[i*incy])._Val[0];
455                         zdotc._Val[1] += Cf(&x[i*incx])._Val[1] * Cf(&y[i*incy])._Val[1];
456                 }
457         }
458         pCf(z) = zdotc;
459 }
460 #else
461         _Complex float zdotc = 0.0;
462         if (incx == 1 && incy == 1) {
463                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
464                         zdotc += Cf(&x[i]) * Cf(&y[i]);
465                 }
466         } else {
467                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
468                         zdotc += Cf(&x[i*incx]) * Cf(&y[i*incy]);
469                 }
470         }
471         pCf(z) = zdotc;
472 }
473 #endif
474 static inline void zdotu_(doublecomplex *z, integer *n_, doublecomplex *x, integer *incx_, doublecomplex *y, integer *incy_) {
475         integer n = *n_, incx = *incx_, incy = *incy_, i;
476 #ifdef _MSC_VER
477         _Dcomplex zdotc = {0.0, 0.0};
478         if (incx == 1 && incy == 1) {
479                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
480                         zdotc._Val[0] += Cd(&x[i])._Val[0] * Cd(&y[i])._Val[0];
481                         zdotc._Val[1] += Cd(&x[i])._Val[1] * Cd(&y[i])._Val[1];
482                 }
483         } else {
484                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
485                         zdotc._Val[0] += Cd(&x[i*incx])._Val[0] * Cd(&y[i*incy])._Val[0];
486                         zdotc._Val[1] += Cd(&x[i*incx])._Val[1] * Cd(&y[i*incy])._Val[1];
487                 }
488         }
489         pCd(z) = zdotc;
490 }
491 #else
492         _Complex double zdotc = 0.0;
493         if (incx == 1 && incy == 1) {
494                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
495                         zdotc += Cd(&x[i]) * Cd(&y[i]);
496                 }
497         } else {
498                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
499                         zdotc += Cd(&x[i*incx]) * Cd(&y[i*incy]);
500                 }
501         }
502         pCd(z) = zdotc;
503 }
504 #endif
505 /*  -- translated by f2c (version 20000121).
506    You must link the resulting object file with the libraries:
507         -lf2c -lm   (in that order)
508 */
509
510
511
512
513 /* Table of constant values */
514
515 static integer c_n1 = -1;
516 static integer c__1 = 1;
517 static logical c_false = FALSE_;
518
519 /* > \brief \b DORCSD2BY1 */
520
521 /*  =========== DOCUMENTATION =========== */
522
523 /* Online html documentation available at */
524 /*            http://www.netlib.org/lapack/explore-html/ */
525
526 /* > \htmlonly */
527 /* > Download DORCSD2BY1 + dependencies */
528 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/dorcsd2
529 by1.f"> */
530 /* > [TGZ]</a> */
531 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/dorcsd2
532 by1.f"> */
533 /* > [ZIP]</a> */
534 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/dorcsd2
535 by1.f"> */
536 /* > [TXT]</a> */
537 /* > \endhtmlonly */
538
539 /*  Definition: */
540 /*  =========== */
541
542 /*       SUBROUTINE DORCSD2BY1( JOBU1, JOBU2, JOBV1T, M, P, Q, X11, LDX11, */
543 /*                              X21, LDX21, THETA, U1, LDU1, U2, LDU2, V1T, */
544 /*                              LDV1T, WORK, LWORK, IWORK, INFO ) */
545
546 /*       CHARACTER          JOBU1, JOBU2, JOBV1T */
547 /*       INTEGER            INFO, LDU1, LDU2, LDV1T, LWORK, LDX11, LDX21, */
548 /*      $                   M, P, Q */
549 /*       DOUBLE PRECISION   THETA(*) */
550 /*       DOUBLE PRECISION   U1(LDU1,*), U2(LDU2,*), V1T(LDV1T,*), WORK(*), */
551 /*      $                   X11(LDX11,*), X21(LDX21,*) */
552 /*       INTEGER            IWORK(*) */
553
554
555 /* > \par Purpose: */
556 /*  ============= */
557 /* > */
558 /* >\verbatim */
559 /* > */
560 /* > DORCSD2BY1 computes the CS decomposition of an M-by-Q matrix X with */
561 /* > orthonormal columns that has been partitioned into a 2-by-1 block */
562 /* > structure: */
563 /* > */
564 /* >                                [  I1 0  0 ] */
565 /* >                                [  0  C  0 ] */
566 /* >          [ X11 ]   [ U1 |    ] [  0  0  0 ] */
567 /* >      X = [-----] = [---------] [----------] V1**T . */
568 /* >          [ X21 ]   [    | U2 ] [  0  0  0 ] */
569 /* >                                [  0  S  0 ] */
570 /* >                                [  0  0  I2] */
571 /* > */
572 /* > X11 is P-by-Q. The orthogonal matrices U1, U2, and V1 are P-by-P, */
573 /* > (M-P)-by-(M-P), and Q-by-Q, respectively. C and S are R-by-R */
574 /* > nonnegative diagonal matrices satisfying C^2 + S^2 = I, in which */
575 /* > R = MIN(P,M-P,Q,M-Q). I1 is a K1-by-K1 identity matrix and I2 is a */
576 /* > K2-by-K2 identity matrix, where K1 = MAX(Q+P-M,0), K2 = MAX(Q-P,0). */
577 /* > \endverbatim */
578
579 /*  Arguments: */
580 /*  ========== */
581
582 /* > \param[in] JOBU1 */
583 /* > \verbatim */
584 /* >          JOBU1 is CHARACTER */
585 /* >          = 'Y':      U1 is computed; */
586 /* >          otherwise:  U1 is not computed. */
587 /* > \endverbatim */
588 /* > */
589 /* > \param[in] JOBU2 */
590 /* > \verbatim */
591 /* >          JOBU2 is CHARACTER */
592 /* >          = 'Y':      U2 is computed; */
593 /* >          otherwise:  U2 is not computed. */
594 /* > \endverbatim */
595 /* > */
596 /* > \param[in] JOBV1T */
597 /* > \verbatim */
598 /* >          JOBV1T is CHARACTER */
599 /* >          = 'Y':      V1T is computed; */
600 /* >          otherwise:  V1T is not computed. */
601 /* > \endverbatim */
602 /* > */
603 /* > \param[in] M */
604 /* > \verbatim */
605 /* >          M is INTEGER */
606 /* >          The number of rows in X. */
607 /* > \endverbatim */
608 /* > */
609 /* > \param[in] P */
610 /* > \verbatim */
611 /* >          P is INTEGER */
612 /* >          The number of rows in X11. 0 <= P <= M. */
613 /* > \endverbatim */
614 /* > */
615 /* > \param[in] Q */
616 /* > \verbatim */
617 /* >          Q is INTEGER */
618 /* >          The number of columns in X11 and X21. 0 <= Q <= M. */
619 /* > \endverbatim */
620 /* > */
621 /* > \param[in,out] X11 */
622 /* > \verbatim */
623 /* >          X11 is DOUBLE PRECISION array, dimension (LDX11,Q) */
624 /* >          On entry, part of the orthogonal matrix whose CSD is desired. */
625 /* > \endverbatim */
626 /* > */
627 /* > \param[in] LDX11 */
628 /* > \verbatim */
629 /* >          LDX11 is INTEGER */
630 /* >          The leading dimension of X11. LDX11 >= MAX(1,P). */
631 /* > \endverbatim */
632 /* > */
633 /* > \param[in,out] X21 */
634 /* > \verbatim */
635 /* >          X21 is DOUBLE PRECISION array, dimension (LDX21,Q) */
636 /* >          On entry, part of the orthogonal matrix whose CSD is desired. */
637 /* > \endverbatim */
638 /* > */
639 /* > \param[in] LDX21 */
640 /* > \verbatim */
641 /* >          LDX21 is INTEGER */
642 /* >          The leading dimension of X21. LDX21 >= MAX(1,M-P). */
643 /* > \endverbatim */
644 /* > */
645 /* > \param[out] THETA */
646 /* > \verbatim */
647 /* >          THETA is DOUBLE PRECISION array, dimension (R), in which R = */
648 /* >          MIN(P,M-P,Q,M-Q). */
649 /* >          C = DIAG( COS(THETA(1)), ... , COS(THETA(R)) ) and */
650 /* >          S = DIAG( SIN(THETA(1)), ... , SIN(THETA(R)) ). */
651 /* > \endverbatim */
652 /* > */
653 /* > \param[out] U1 */
654 /* > \verbatim */
655 /* >          U1 is DOUBLE PRECISION array, dimension (P) */
656 /* >          If JOBU1 = 'Y', U1 contains the P-by-P orthogonal matrix U1. */
657 /* > \endverbatim */
658 /* > */
659 /* > \param[in] LDU1 */
660 /* > \verbatim */
661 /* >          LDU1 is INTEGER */
662 /* >          The leading dimension of U1. If JOBU1 = 'Y', LDU1 >= */
663 /* >          MAX(1,P). */
664 /* > \endverbatim */
665 /* > */
666 /* > \param[out] U2 */
667 /* > \verbatim */
668 /* >          U2 is DOUBLE PRECISION array, dimension (M-P) */
669 /* >          If JOBU2 = 'Y', U2 contains the (M-P)-by-(M-P) orthogonal */
670 /* >          matrix U2. */
671 /* > \endverbatim */
672 /* > */
673 /* > \param[in] LDU2 */
674 /* > \verbatim */
675 /* >          LDU2 is INTEGER */
676 /* >          The leading dimension of U2. If JOBU2 = 'Y', LDU2 >= */
677 /* >          MAX(1,M-P). */
678 /* > \endverbatim */
679 /* > */
680 /* > \param[out] V1T */
681 /* > \verbatim */
682 /* >          V1T is DOUBLE PRECISION array, dimension (Q) */
683 /* >          If JOBV1T = 'Y', V1T contains the Q-by-Q matrix orthogonal */
684 /* >          matrix V1**T. */
685 /* > \endverbatim */
686 /* > */
687 /* > \param[in] LDV1T */
688 /* > \verbatim */
689 /* >          LDV1T is INTEGER */
690 /* >          The leading dimension of V1T. If JOBV1T = 'Y', LDV1T >= */
691 /* >          MAX(1,Q). */
692 /* > \endverbatim */
693 /* > */
694 /* > \param[out] WORK */
695 /* > \verbatim */
696 /* >          WORK is DOUBLE PRECISION array, dimension (MAX(1,LWORK)) */
697 /* >          On exit, if INFO = 0, WORK(1) returns the optimal LWORK. */
698 /* >          If INFO > 0 on exit, WORK(2:R) contains the values PHI(1), */
699 /* >          ..., PHI(R-1) that, together with THETA(1), ..., THETA(R), */
700 /* >          define the matrix in intermediate bidiagonal-block form */
701 /* >          remaining after nonconvergence. INFO specifies the number */
702 /* >          of nonzero PHI's. */
703 /* > \endverbatim */
704 /* > */
705 /* > \param[in] LWORK */
706 /* > \verbatim */
707 /* >          LWORK is INTEGER */
708 /* >          The dimension of the array WORK. */
709 /* > */
710 /* >          If LWORK = -1, then a workspace query is assumed; the routine */
711 /* >          only calculates the optimal size of the WORK array, returns */
712 /* >          this value as the first entry of the work array, and no error */
713 /* >          message related to LWORK is issued by XERBLA. */
714 /* > \endverbatim */
715 /* > */
716 /* > \param[out] IWORK */
717 /* > \verbatim */
718 /* >          IWORK is INTEGER array, dimension (M-MIN(P,M-P,Q,M-Q)) */
719 /* > \endverbatim */
720 /* > */
721 /* > \param[out] INFO */
722 /* > \verbatim */
723 /* >          INFO is INTEGER */
724 /* >          = 0:  successful exit. */
725 /* >          < 0:  if INFO = -i, the i-th argument had an illegal value. */
726 /* >          > 0:  DBBCSD did not converge. See the description of WORK */
727 /* >                above for details. */
728 /* > \endverbatim */
729
730 /* > \par References: */
731 /*  ================ */
732 /* > */
733 /* >  [1] Brian D. Sutton. Computing the complete CS decomposition. Numer. */
734 /* >      Algorithms, 50(1):33-65, 2009. */
735
736 /*  Authors: */
737 /*  ======== */
738
739 /* > \author Univ. of Tennessee */
740 /* > \author Univ. of California Berkeley */
741 /* > \author Univ. of Colorado Denver */
742 /* > \author NAG Ltd. */
743
744 /* > \date July 2012 */
745
746 /* > \ingroup doubleOTHERcomputational */
747
748 /*  ===================================================================== */
749 /* Subroutine */ int dorcsd2by1_(char *jobu1, char *jobu2, char *jobv1t, 
750         integer *m, integer *p, integer *q, doublereal *x11, integer *ldx11, 
751         doublereal *x21, integer *ldx21, doublereal *theta, doublereal *u1, 
752         integer *ldu1, doublereal *u2, integer *ldu2, doublereal *v1t, 
753         integer *ldv1t, doublereal *work, integer *lwork, integer *iwork, 
754         integer *info)
755 {
756     /* System generated locals */
757     integer u1_dim1, u1_offset, u2_dim1, u2_offset, v1t_dim1, v1t_offset, 
758             x11_dim1, x11_offset, x21_dim1, x21_offset, i__1, i__2, i__3;
759
760     /* Local variables */
761     integer ib11d, ib11e, ib12d, ib12e, ib21d, ib21e, ib22d, ib22e, iphi, 
762             lworkmin, lworkopt, i__, j, r__;
763     extern logical lsame_(char *, char *);
764     integer childinfo;
765     extern /* Subroutine */ int dcopy_(integer *, doublereal *, integer *, 
766             doublereal *, integer *);
767     integer lorglqmin, lorgqrmin, lorglqopt, itaup1, itaup2, itauq1, 
768             lorgqropt;
769     logical wantu1, wantu2;
770     extern /* Subroutine */ int dbbcsd_(char *, char *, char *, char *, char *
771             , integer *, integer *, integer *, doublereal *, doublereal *, 
772             doublereal *, integer *, doublereal *, integer *, doublereal *, 
773             integer *, doublereal *, integer *, doublereal *, doublereal *, 
774             doublereal *, doublereal *, doublereal *, doublereal *, 
775             doublereal *, doublereal *, doublereal *, integer *, integer *);
776     integer ibbcsd, lbbcsd, iorbdb, lorbdb;
777     extern /* Subroutine */ int dlacpy_(char *, integer *, integer *, 
778             doublereal *, integer *, doublereal *, integer *), 
779             xerbla_(char *, integer *, ftnlen), dlapmr_(logical *, integer *, 
780             integer *, doublereal *, integer *, integer *), dlapmt_(logical *,
781              integer *, integer *, doublereal *, integer *, integer *), 
782             dorglq_(integer *, integer *, integer *, doublereal *, integer *, 
783             doublereal *, doublereal *, integer *, integer *);
784     integer iorglq;
785     extern /* Subroutine */ int dorgqr_(integer *, integer *, integer *, 
786             doublereal *, integer *, doublereal *, doublereal *, integer *, 
787             integer *);
788     integer lorglq, iorgqr, lorgqr;
789     extern /* Subroutine */ int dorbdb1_(integer *, integer *, integer *, 
790             doublereal *, integer *, doublereal *, integer *, doublereal *, 
791             doublereal *, doublereal *, doublereal *, doublereal *, 
792             doublereal *, integer *, integer *), dorbdb2_(integer *, integer *
793             , integer *, doublereal *, integer *, doublereal *, integer *, 
794             doublereal *, doublereal *, doublereal *, doublereal *, 
795             doublereal *, doublereal *, integer *, integer *), dorbdb3_(
796             integer *, integer *, integer *, doublereal *, integer *, 
797             doublereal *, integer *, doublereal *, doublereal *, doublereal *,
798              doublereal *, doublereal *, doublereal *, integer *, integer *), 
799             dorbdb4_(integer *, integer *, integer *, doublereal *, integer *,
800              doublereal *, integer *, doublereal *, doublereal *, doublereal *
801             , doublereal *, doublereal *, doublereal *, doublereal *, integer 
802             *, integer *);
803     logical lquery, wantv1t;
804     doublereal dum1[1], dum2[1] /* was [1][1] */;
805
806
807 /*  -- LAPACK computational routine (3.5.0) -- */
808 /*  -- LAPACK is a software package provided by Univ. of Tennessee,    -- */
809 /*  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
810 /*     July 2012 */
811
812
813 /*  ===================================================================== */
814
815
816 /*     Test input arguments */
817
818     /* Parameter adjustments */
819     x11_dim1 = *ldx11;
820     x11_offset = 1 + x11_dim1 * 1;
821     x11 -= x11_offset;
822     x21_dim1 = *ldx21;
823     x21_offset = 1 + x21_dim1 * 1;
824     x21 -= x21_offset;
825     --theta;
826     u1_dim1 = *ldu1;
827     u1_offset = 1 + u1_dim1 * 1;
828     u1 -= u1_offset;
829     u2_dim1 = *ldu2;
830     u2_offset = 1 + u2_dim1 * 1;
831     u2 -= u2_offset;
832     v1t_dim1 = *ldv1t;
833     v1t_offset = 1 + v1t_dim1 * 1;
834     v1t -= v1t_offset;
835     --work;
836     --iwork;
837
838     /* Function Body */
839     *info = 0;
840     wantu1 = lsame_(jobu1, "Y");
841     wantu2 = lsame_(jobu2, "Y");
842     wantv1t = lsame_(jobv1t, "Y");
843     lquery = *lwork == -1;
844
845     if (*m < 0) {
846         *info = -4;
847     } else if (*p < 0 || *p > *m) {
848         *info = -5;
849     } else if (*q < 0 || *q > *m) {
850         *info = -6;
851     } else if (*ldx11 < f2cmax(1,*p)) {
852         *info = -8;
853     } else /* if(complicated condition) */ {
854 /* Computing MAX */
855         i__1 = 1, i__2 = *m - *p;
856         if (*ldx21 < f2cmax(i__1,i__2)) {
857             *info = -10;
858         } else if (wantu1 && *ldu1 < f2cmax(1,*p)) {
859             *info = -13;
860         } else /* if(complicated condition) */ {
861 /* Computing MAX */
862             i__1 = 1, i__2 = *m - *p;
863             if (wantu2 && *ldu2 < f2cmax(i__1,i__2)) {
864                 *info = -15;
865             } else if (wantv1t && *ldv1t < f2cmax(1,*q)) {
866                 *info = -17;
867             }
868         }
869     }
870
871 /* Computing MIN */
872     i__1 = *p, i__2 = *m - *p, i__1 = f2cmin(i__1,i__2), i__1 = f2cmin(i__1,*q), 
873             i__2 = *m - *q;
874     r__ = f2cmin(i__1,i__2);
875
876 /*     Compute workspace */
877
878 /*       WORK layout: */
879 /*     |-------------------------------------------------------| */
880 /*     | LWORKOPT (1)                                          | */
881 /*     |-------------------------------------------------------| */
882 /*     | PHI (MAX(1,R-1))                                      | */
883 /*     |-------------------------------------------------------| */
884 /*     | TAUP1 (MAX(1,P))                        | B11D (R)    | */
885 /*     | TAUP2 (MAX(1,M-P))                      | B11E (R-1)  | */
886 /*     | TAUQ1 (MAX(1,Q))                        | B12D (R)    | */
887 /*     |-----------------------------------------| B12E (R-1)  | */
888 /*     | DORBDB WORK | DORGQR WORK | DORGLQ WORK | B21D (R)    | */
889 /*     |             |             |             | B21E (R-1)  | */
890 /*     |             |             |             | B22D (R)    | */
891 /*     |             |             |             | B22E (R-1)  | */
892 /*     |             |             |             | DBBCSD WORK | */
893 /*     |-------------------------------------------------------| */
894
895     if (*info == 0) {
896         iphi = 2;
897 /* Computing MAX */
898         i__1 = 1, i__2 = r__ - 1;
899         ib11d = iphi + f2cmax(i__1,i__2);
900         ib11e = ib11d + f2cmax(1,r__);
901 /* Computing MAX */
902         i__1 = 1, i__2 = r__ - 1;
903         ib12d = ib11e + f2cmax(i__1,i__2);
904         ib12e = ib12d + f2cmax(1,r__);
905 /* Computing MAX */
906         i__1 = 1, i__2 = r__ - 1;
907         ib21d = ib12e + f2cmax(i__1,i__2);
908         ib21e = ib21d + f2cmax(1,r__);
909 /* Computing MAX */
910         i__1 = 1, i__2 = r__ - 1;
911         ib22d = ib21e + f2cmax(i__1,i__2);
912         ib22e = ib22d + f2cmax(1,r__);
913 /* Computing MAX */
914         i__1 = 1, i__2 = r__ - 1;
915         ibbcsd = ib22e + f2cmax(i__1,i__2);
916 /* Computing MAX */
917         i__1 = 1, i__2 = r__ - 1;
918         itaup1 = iphi + f2cmax(i__1,i__2);
919         itaup2 = itaup1 + f2cmax(1,*p);
920 /* Computing MAX */
921         i__1 = 1, i__2 = *m - *p;
922         itauq1 = itaup2 + f2cmax(i__1,i__2);
923         iorbdb = itauq1 + f2cmax(1,*q);
924         iorgqr = itauq1 + f2cmax(1,*q);
925         iorglq = itauq1 + f2cmax(1,*q);
926         lorgqrmin = 1;
927         lorgqropt = 1;
928         lorglqmin = 1;
929         lorglqopt = 1;
930         if (r__ == *q) {
931             dorbdb1_(m, p, q, &x11[x11_offset], ldx11, &x21[x21_offset], 
932                     ldx21, &theta[1], dum1, dum1, dum1, dum1, &work[1], &c_n1,
933                      &childinfo);
934             lorbdb = (integer) work[1];
935             if (wantu1 && *p > 0) {
936                 dorgqr_(p, p, q, &u1[u1_offset], ldu1, dum1, &work[1], &c_n1, 
937                         &childinfo);
938                 lorgqrmin = f2cmax(lorgqrmin,*p);
939 /* Computing MAX */
940                 i__1 = lorgqropt, i__2 = (integer) work[1];
941                 lorgqropt = f2cmax(i__1,i__2);
942             }
943             if (wantu2 && *m - *p > 0) {
944                 i__1 = *m - *p;
945                 i__2 = *m - *p;
946                 dorgqr_(&i__1, &i__2, q, &u2[u2_offset], ldu2, dum1, &work[1],
947                          &c_n1, &childinfo);
948 /* Computing MAX */
949                 i__1 = lorgqrmin, i__2 = *m - *p;
950                 lorgqrmin = f2cmax(i__1,i__2);
951 /* Computing MAX */
952                 i__1 = lorgqropt, i__2 = (integer) work[1];
953                 lorgqropt = f2cmax(i__1,i__2);
954             }
955             if (wantv1t && *q > 0) {
956                 i__1 = *q - 1;
957                 i__2 = *q - 1;
958                 i__3 = *q - 1;
959                 dorglq_(&i__1, &i__2, &i__3, &v1t[v1t_offset], ldv1t, dum1, &
960                         work[1], &c_n1, &childinfo);
961 /* Computing MAX */
962                 i__1 = lorglqmin, i__2 = *q - 1;
963                 lorglqmin = f2cmax(i__1,i__2);
964 /* Computing MAX */
965                 i__1 = lorglqopt, i__2 = (integer) work[1];
966                 lorglqopt = f2cmax(i__1,i__2);
967             }
968             dbbcsd_(jobu1, jobu2, jobv1t, "N", "N", m, p, q, &theta[1], dum1, 
969                     &u1[u1_offset], ldu1, &u2[u2_offset], ldu2, &v1t[
970                     v1t_offset], ldv1t, dum2, &c__1, dum1, dum1, dum1, dum1, 
971                     dum1, dum1, dum1, dum1, &work[1], &c_n1, &childinfo);
972             lbbcsd = (integer) work[1];
973         } else if (r__ == *p) {
974             dorbdb2_(m, p, q, &x11[x11_offset], ldx11, &x21[x21_offset], 
975                     ldx21, &theta[1], dum1, dum1, dum1, dum1, &work[1], &c_n1,
976                      &childinfo);
977             lorbdb = (integer) work[1];
978             if (wantu1 && *p > 0) {
979                 i__1 = *p - 1;
980                 i__2 = *p - 1;
981                 i__3 = *p - 1;
982                 dorgqr_(&i__1, &i__2, &i__3, &u1[(u1_dim1 << 1) + 2], ldu1, 
983                         dum1, &work[1], &c_n1, &childinfo);
984 /* Computing MAX */
985                 i__1 = lorgqrmin, i__2 = *p - 1;
986                 lorgqrmin = f2cmax(i__1,i__2);
987 /* Computing MAX */
988                 i__1 = lorgqropt, i__2 = (integer) work[1];
989                 lorgqropt = f2cmax(i__1,i__2);
990             }
991             if (wantu2 && *m - *p > 0) {
992                 i__1 = *m - *p;
993                 i__2 = *m - *p;
994                 dorgqr_(&i__1, &i__2, q, &u2[u2_offset], ldu2, dum1, &work[1],
995                          &c_n1, &childinfo);
996 /* Computing MAX */
997                 i__1 = lorgqrmin, i__2 = *m - *p;
998                 lorgqrmin = f2cmax(i__1,i__2);
999 /* Computing MAX */
1000                 i__1 = lorgqropt, i__2 = (integer) work[1];
1001                 lorgqropt = f2cmax(i__1,i__2);
1002             }
1003             if (wantv1t && *q > 0) {
1004                 dorglq_(q, q, &r__, &v1t[v1t_offset], ldv1t, dum1, &work[1], &
1005                         c_n1, &childinfo);
1006                 lorglqmin = f2cmax(lorglqmin,*q);
1007 /* Computing MAX */
1008                 i__1 = lorglqopt, i__2 = (integer) work[1];
1009                 lorglqopt = f2cmax(i__1,i__2);
1010             }
1011             dbbcsd_(jobv1t, "N", jobu1, jobu2, "T", m, q, p, &theta[1], dum1, 
1012                     &v1t[v1t_offset], ldv1t, dum2, &c__1, &u1[u1_offset], 
1013                     ldu1, &u2[u2_offset], ldu2, dum1, dum1, dum1, dum1, dum1, 
1014                     dum1, dum1, dum1, &work[1], &c_n1, &childinfo);
1015             lbbcsd = (integer) work[1];
1016         } else if (r__ == *m - *p) {
1017             dorbdb3_(m, p, q, &x11[x11_offset], ldx11, &x21[x21_offset], 
1018                     ldx21, &theta[1], dum1, dum1, dum1, dum1, &work[1], &c_n1,
1019                      &childinfo);
1020             lorbdb = (integer) work[1];
1021             if (wantu1 && *p > 0) {
1022                 dorgqr_(p, p, q, &u1[u1_offset], ldu1, dum1, &work[1], &c_n1, 
1023                         &childinfo);
1024                 lorgqrmin = f2cmax(lorgqrmin,*p);
1025 /* Computing MAX */
1026                 i__1 = lorgqropt, i__2 = (integer) work[1];
1027                 lorgqropt = f2cmax(i__1,i__2);
1028             }
1029             if (wantu2 && *m - *p > 0) {
1030                 i__1 = *m - *p - 1;
1031                 i__2 = *m - *p - 1;
1032                 i__3 = *m - *p - 1;
1033                 dorgqr_(&i__1, &i__2, &i__3, &u2[(u2_dim1 << 1) + 2], ldu2, 
1034                         dum1, &work[1], &c_n1, &childinfo);
1035 /* Computing MAX */
1036                 i__1 = lorgqrmin, i__2 = *m - *p - 1;
1037                 lorgqrmin = f2cmax(i__1,i__2);
1038 /* Computing MAX */
1039                 i__1 = lorgqropt, i__2 = (integer) work[1];
1040                 lorgqropt = f2cmax(i__1,i__2);
1041             }
1042             if (wantv1t && *q > 0) {
1043                 dorglq_(q, q, &r__, &v1t[v1t_offset], ldv1t, dum1, &work[1], &
1044                         c_n1, &childinfo);
1045                 lorglqmin = f2cmax(lorglqmin,*q);
1046 /* Computing MAX */
1047                 i__1 = lorglqopt, i__2 = (integer) work[1];
1048                 lorglqopt = f2cmax(i__1,i__2);
1049             }
1050             i__1 = *m - *q;
1051             i__2 = *m - *p;
1052             dbbcsd_("N", jobv1t, jobu2, jobu1, "T", m, &i__1, &i__2, &theta[1]
1053                     , dum1, dum2, &c__1, &v1t[v1t_offset], ldv1t, &u2[
1054                     u2_offset], ldu2, &u1[u1_offset], ldu1, dum1, dum1, dum1, 
1055                     dum1, dum1, dum1, dum1, dum1, &work[1], &c_n1, &childinfo);
1056             lbbcsd = (integer) work[1];
1057         } else {
1058             dorbdb4_(m, p, q, &x11[x11_offset], ldx11, &x21[x21_offset], 
1059                     ldx21, &theta[1], dum1, dum1, dum1, dum1, dum1, &work[1], 
1060                     &c_n1, &childinfo);
1061             lorbdb = *m + (integer) work[1];
1062             if (wantu1 && *p > 0) {
1063                 i__1 = *m - *q;
1064                 dorgqr_(p, p, &i__1, &u1[u1_offset], ldu1, dum1, &work[1], &
1065                         c_n1, &childinfo);
1066                 lorgqrmin = f2cmax(lorgqrmin,*p);
1067 /* Computing MAX */
1068                 i__1 = lorgqropt, i__2 = (integer) work[1];
1069                 lorgqropt = f2cmax(i__1,i__2);
1070             }
1071             if (wantu2 && *m - *p > 0) {
1072                 i__1 = *m - *p;
1073                 i__2 = *m - *p;
1074                 i__3 = *m - *q;
1075                 dorgqr_(&i__1, &i__2, &i__3, &u2[u2_offset], ldu2, dum1, &
1076                         work[1], &c_n1, &childinfo);
1077 /* Computing MAX */
1078                 i__1 = lorgqrmin, i__2 = *m - *p;
1079                 lorgqrmin = f2cmax(i__1,i__2);
1080 /* Computing MAX */
1081                 i__1 = lorgqropt, i__2 = (integer) work[1];
1082                 lorgqropt = f2cmax(i__1,i__2);
1083             }
1084             if (wantv1t && *q > 0) {
1085                 dorglq_(q, q, q, &v1t[v1t_offset], ldv1t, dum1, &work[1], &
1086                         c_n1, &childinfo);
1087                 lorglqmin = f2cmax(lorglqmin,*q);
1088 /* Computing MAX */
1089                 i__1 = lorglqopt, i__2 = (integer) work[1];
1090                 lorglqopt = f2cmax(i__1,i__2);
1091             }
1092             i__1 = *m - *p;
1093             i__2 = *m - *q;
1094             dbbcsd_(jobu2, jobu1, "N", jobv1t, "N", m, &i__1, &i__2, &theta[1]
1095                     , dum1, &u2[u2_offset], ldu2, &u1[u1_offset], ldu1, dum2, 
1096                     &c__1, &v1t[v1t_offset], ldv1t, dum1, dum1, dum1, dum1, 
1097                     dum1, dum1, dum1, dum1, &work[1], &c_n1, &childinfo);
1098             lbbcsd = (integer) work[1];
1099         }
1100 /* Computing MAX */
1101         i__1 = iorbdb + lorbdb - 1, i__2 = iorgqr + lorgqrmin - 1, i__1 = f2cmax(
1102                 i__1,i__2), i__2 = iorglq + lorglqmin - 1, i__1 = f2cmax(i__1,
1103                 i__2), i__2 = ibbcsd + lbbcsd - 1;
1104         lworkmin = f2cmax(i__1,i__2);
1105 /* Computing MAX */
1106         i__1 = iorbdb + lorbdb - 1, i__2 = iorgqr + lorgqropt - 1, i__1 = f2cmax(
1107                 i__1,i__2), i__2 = iorglq + lorglqopt - 1, i__1 = f2cmax(i__1,
1108                 i__2), i__2 = ibbcsd + lbbcsd - 1;
1109         lworkopt = f2cmax(i__1,i__2);
1110         work[1] = (doublereal) lworkopt;
1111         if (*lwork < lworkmin && ! lquery) {
1112             *info = -19;
1113         }
1114     }
1115     if (*info != 0) {
1116         i__1 = -(*info);
1117         xerbla_("DORCSD2BY1", &i__1, (ftnlen)10);
1118         return 0;
1119     } else if (lquery) {
1120         return 0;
1121     }
1122     lorgqr = *lwork - iorgqr + 1;
1123     lorglq = *lwork - iorglq + 1;
1124
1125 /*     Handle four cases separately: R = Q, R = P, R = M-P, and R = M-Q, */
1126 /*     in which R = MIN(P,M-P,Q,M-Q) */
1127
1128     if (r__ == *q) {
1129
1130 /*        Case 1: R = Q */
1131
1132 /*        Simultaneously bidiagonalize X11 and X21 */
1133
1134         dorbdb1_(m, p, q, &x11[x11_offset], ldx11, &x21[x21_offset], ldx21, &
1135                 theta[1], &work[iphi], &work[itaup1], &work[itaup2], &work[
1136                 itauq1], &work[iorbdb], &lorbdb, &childinfo);
1137
1138 /*        Accumulate Householder reflectors */
1139
1140         if (wantu1 && *p > 0) {
1141             dlacpy_("L", p, q, &x11[x11_offset], ldx11, &u1[u1_offset], ldu1);
1142             dorgqr_(p, p, q, &u1[u1_offset], ldu1, &work[itaup1], &work[
1143                     iorgqr], &lorgqr, &childinfo);
1144         }
1145         if (wantu2 && *m - *p > 0) {
1146             i__1 = *m - *p;
1147             dlacpy_("L", &i__1, q, &x21[x21_offset], ldx21, &u2[u2_offset], 
1148                     ldu2);
1149             i__1 = *m - *p;
1150             i__2 = *m - *p;
1151             dorgqr_(&i__1, &i__2, q, &u2[u2_offset], ldu2, &work[itaup2], &
1152                     work[iorgqr], &lorgqr, &childinfo);
1153         }
1154         if (wantv1t && *q > 0) {
1155             v1t[v1t_dim1 + 1] = 1.;
1156             i__1 = *q;
1157             for (j = 2; j <= i__1; ++j) {
1158                 v1t[j * v1t_dim1 + 1] = 0.;
1159                 v1t[j + v1t_dim1] = 0.;
1160             }
1161             i__1 = *q - 1;
1162             i__2 = *q - 1;
1163             dlacpy_("U", &i__1, &i__2, &x21[(x21_dim1 << 1) + 1], ldx21, &v1t[
1164                     (v1t_dim1 << 1) + 2], ldv1t);
1165             i__1 = *q - 1;
1166             i__2 = *q - 1;
1167             i__3 = *q - 1;
1168             dorglq_(&i__1, &i__2, &i__3, &v1t[(v1t_dim1 << 1) + 2], ldv1t, &
1169                     work[itauq1], &work[iorglq], &lorglq, &childinfo);
1170         }
1171
1172 /*        Simultaneously diagonalize X11 and X21. */
1173
1174         dbbcsd_(jobu1, jobu2, jobv1t, "N", "N", m, p, q, &theta[1], &work[
1175                 iphi], &u1[u1_offset], ldu1, &u2[u2_offset], ldu2, &v1t[
1176                 v1t_offset], ldv1t, dum2, &c__1, &work[ib11d], &work[ib11e], &
1177                 work[ib12d], &work[ib12e], &work[ib21d], &work[ib21e], &work[
1178                 ib22d], &work[ib22e], &work[ibbcsd], &lbbcsd, &childinfo);
1179
1180 /*        Permute rows and columns to place zero submatrices in */
1181 /*        preferred positions */
1182
1183         if (*q > 0 && wantu2) {
1184             i__1 = *q;
1185             for (i__ = 1; i__ <= i__1; ++i__) {
1186                 iwork[i__] = *m - *p - *q + i__;
1187             }
1188             i__1 = *m - *p;
1189             for (i__ = *q + 1; i__ <= i__1; ++i__) {
1190                 iwork[i__] = i__ - *q;
1191             }
1192             i__1 = *m - *p;
1193             i__2 = *m - *p;
1194             dlapmt_(&c_false, &i__1, &i__2, &u2[u2_offset], ldu2, &iwork[1]);
1195         }
1196     } else if (r__ == *p) {
1197
1198 /*        Case 2: R = P */
1199
1200 /*        Simultaneously bidiagonalize X11 and X21 */
1201
1202         dorbdb2_(m, p, q, &x11[x11_offset], ldx11, &x21[x21_offset], ldx21, &
1203                 theta[1], &work[iphi], &work[itaup1], &work[itaup2], &work[
1204                 itauq1], &work[iorbdb], &lorbdb, &childinfo);
1205
1206 /*        Accumulate Householder reflectors */
1207
1208         if (wantu1 && *p > 0) {
1209             u1[u1_dim1 + 1] = 1.;
1210             i__1 = *p;
1211             for (j = 2; j <= i__1; ++j) {
1212                 u1[j * u1_dim1 + 1] = 0.;
1213                 u1[j + u1_dim1] = 0.;
1214             }
1215             i__1 = *p - 1;
1216             i__2 = *p - 1;
1217             dlacpy_("L", &i__1, &i__2, &x11[x11_dim1 + 2], ldx11, &u1[(
1218                     u1_dim1 << 1) + 2], ldu1);
1219             i__1 = *p - 1;
1220             i__2 = *p - 1;
1221             i__3 = *p - 1;
1222             dorgqr_(&i__1, &i__2, &i__3, &u1[(u1_dim1 << 1) + 2], ldu1, &work[
1223                     itaup1], &work[iorgqr], &lorgqr, &childinfo);
1224         }
1225         if (wantu2 && *m - *p > 0) {
1226             i__1 = *m - *p;
1227             dlacpy_("L", &i__1, q, &x21[x21_offset], ldx21, &u2[u2_offset], 
1228                     ldu2);
1229             i__1 = *m - *p;
1230             i__2 = *m - *p;
1231             dorgqr_(&i__1, &i__2, q, &u2[u2_offset], ldu2, &work[itaup2], &
1232                     work[iorgqr], &lorgqr, &childinfo);
1233         }
1234         if (wantv1t && *q > 0) {
1235             dlacpy_("U", p, q, &x11[x11_offset], ldx11, &v1t[v1t_offset], 
1236                     ldv1t);
1237             dorglq_(q, q, &r__, &v1t[v1t_offset], ldv1t, &work[itauq1], &work[
1238                     iorglq], &lorglq, &childinfo);
1239         }
1240
1241 /*        Simultaneously diagonalize X11 and X21. */
1242
1243         dbbcsd_(jobv1t, "N", jobu1, jobu2, "T", m, q, p, &theta[1], &work[
1244                 iphi], &v1t[v1t_offset], ldv1t, dum2, &c__1, &u1[u1_offset], 
1245                 ldu1, &u2[u2_offset], ldu2, &work[ib11d], &work[ib11e], &work[
1246                 ib12d], &work[ib12e], &work[ib21d], &work[ib21e], &work[ib22d]
1247                 , &work[ib22e], &work[ibbcsd], &lbbcsd, &childinfo);
1248
1249 /*        Permute rows and columns to place identity submatrices in */
1250 /*        preferred positions */
1251
1252         if (*q > 0 && wantu2) {
1253             i__1 = *q;
1254             for (i__ = 1; i__ <= i__1; ++i__) {
1255                 iwork[i__] = *m - *p - *q + i__;
1256             }
1257             i__1 = *m - *p;
1258             for (i__ = *q + 1; i__ <= i__1; ++i__) {
1259                 iwork[i__] = i__ - *q;
1260             }
1261             i__1 = *m - *p;
1262             i__2 = *m - *p;
1263             dlapmt_(&c_false, &i__1, &i__2, &u2[u2_offset], ldu2, &iwork[1]);
1264         }
1265     } else if (r__ == *m - *p) {
1266
1267 /*        Case 3: R = M-P */
1268
1269 /*        Simultaneously bidiagonalize X11 and X21 */
1270
1271         dorbdb3_(m, p, q, &x11[x11_offset], ldx11, &x21[x21_offset], ldx21, &
1272                 theta[1], &work[iphi], &work[itaup1], &work[itaup2], &work[
1273                 itauq1], &work[iorbdb], &lorbdb, &childinfo);
1274
1275 /*        Accumulate Householder reflectors */
1276
1277         if (wantu1 && *p > 0) {
1278             dlacpy_("L", p, q, &x11[x11_offset], ldx11, &u1[u1_offset], ldu1);
1279             dorgqr_(p, p, q, &u1[u1_offset], ldu1, &work[itaup1], &work[
1280                     iorgqr], &lorgqr, &childinfo);
1281         }
1282         if (wantu2 && *m - *p > 0) {
1283             u2[u2_dim1 + 1] = 1.;
1284             i__1 = *m - *p;
1285             for (j = 2; j <= i__1; ++j) {
1286                 u2[j * u2_dim1 + 1] = 0.;
1287                 u2[j + u2_dim1] = 0.;
1288             }
1289             i__1 = *m - *p - 1;
1290             i__2 = *m - *p - 1;
1291             dlacpy_("L", &i__1, &i__2, &x21[x21_dim1 + 2], ldx21, &u2[(
1292                     u2_dim1 << 1) + 2], ldu2);
1293             i__1 = *m - *p - 1;
1294             i__2 = *m - *p - 1;
1295             i__3 = *m - *p - 1;
1296             dorgqr_(&i__1, &i__2, &i__3, &u2[(u2_dim1 << 1) + 2], ldu2, &work[
1297                     itaup2], &work[iorgqr], &lorgqr, &childinfo);
1298         }
1299         if (wantv1t && *q > 0) {
1300             i__1 = *m - *p;
1301             dlacpy_("U", &i__1, q, &x21[x21_offset], ldx21, &v1t[v1t_offset], 
1302                     ldv1t);
1303             dorglq_(q, q, &r__, &v1t[v1t_offset], ldv1t, &work[itauq1], &work[
1304                     iorglq], &lorglq, &childinfo);
1305         }
1306
1307 /*        Simultaneously diagonalize X11 and X21. */
1308
1309         i__1 = *m - *q;
1310         i__2 = *m - *p;
1311         dbbcsd_("N", jobv1t, jobu2, jobu1, "T", m, &i__1, &i__2, &theta[1], &
1312                 work[iphi], dum2, &c__1, &v1t[v1t_offset], ldv1t, &u2[
1313                 u2_offset], ldu2, &u1[u1_offset], ldu1, &work[ib11d], &work[
1314                 ib11e], &work[ib12d], &work[ib12e], &work[ib21d], &work[ib21e]
1315                 , &work[ib22d], &work[ib22e], &work[ibbcsd], &lbbcsd, &
1316                 childinfo);
1317
1318 /*        Permute rows and columns to place identity submatrices in */
1319 /*        preferred positions */
1320
1321         if (*q > r__) {
1322             i__1 = r__;
1323             for (i__ = 1; i__ <= i__1; ++i__) {
1324                 iwork[i__] = *q - r__ + i__;
1325             }
1326             i__1 = *q;
1327             for (i__ = r__ + 1; i__ <= i__1; ++i__) {
1328                 iwork[i__] = i__ - r__;
1329             }
1330             if (wantu1) {
1331                 dlapmt_(&c_false, p, q, &u1[u1_offset], ldu1, &iwork[1]);
1332             }
1333             if (wantv1t) {
1334                 dlapmr_(&c_false, q, q, &v1t[v1t_offset], ldv1t, &iwork[1]);
1335             }
1336         }
1337     } else {
1338
1339 /*        Case 4: R = M-Q */
1340
1341 /*        Simultaneously bidiagonalize X11 and X21 */
1342
1343         i__1 = lorbdb - *m;
1344         dorbdb4_(m, p, q, &x11[x11_offset], ldx11, &x21[x21_offset], ldx21, &
1345                 theta[1], &work[iphi], &work[itaup1], &work[itaup2], &work[
1346                 itauq1], &work[iorbdb], &work[iorbdb + *m], &i__1, &childinfo)
1347                 ;
1348
1349 /*        Accumulate Householder reflectors */
1350
1351         if (wantu1 && *p > 0) {
1352             dcopy_(p, &work[iorbdb], &c__1, &u1[u1_offset], &c__1);
1353             i__1 = *p;
1354             for (j = 2; j <= i__1; ++j) {
1355                 u1[j * u1_dim1 + 1] = 0.;
1356             }
1357             i__1 = *p - 1;
1358             i__2 = *m - *q - 1;
1359             dlacpy_("L", &i__1, &i__2, &x11[x11_dim1 + 2], ldx11, &u1[(
1360                     u1_dim1 << 1) + 2], ldu1);
1361             i__1 = *m - *q;
1362             dorgqr_(p, p, &i__1, &u1[u1_offset], ldu1, &work[itaup1], &work[
1363                     iorgqr], &lorgqr, &childinfo);
1364         }
1365         if (wantu2 && *m - *p > 0) {
1366             i__1 = *m - *p;
1367             dcopy_(&i__1, &work[iorbdb + *p], &c__1, &u2[u2_offset], &c__1);
1368             i__1 = *m - *p;
1369             for (j = 2; j <= i__1; ++j) {
1370                 u2[j * u2_dim1 + 1] = 0.;
1371             }
1372             i__1 = *m - *p - 1;
1373             i__2 = *m - *q - 1;
1374             dlacpy_("L", &i__1, &i__2, &x21[x21_dim1 + 2], ldx21, &u2[(
1375                     u2_dim1 << 1) + 2], ldu2);
1376             i__1 = *m - *p;
1377             i__2 = *m - *p;
1378             i__3 = *m - *q;
1379             dorgqr_(&i__1, &i__2, &i__3, &u2[u2_offset], ldu2, &work[itaup2], 
1380                     &work[iorgqr], &lorgqr, &childinfo);
1381         }
1382         if (wantv1t && *q > 0) {
1383             i__1 = *m - *q;
1384             dlacpy_("U", &i__1, q, &x21[x21_offset], ldx21, &v1t[v1t_offset], 
1385                     ldv1t);
1386             i__1 = *p - (*m - *q);
1387             i__2 = *q - (*m - *q);
1388             dlacpy_("U", &i__1, &i__2, &x11[*m - *q + 1 + (*m - *q + 1) * 
1389                     x11_dim1], ldx11, &v1t[*m - *q + 1 + (*m - *q + 1) * 
1390                     v1t_dim1], ldv1t);
1391             i__1 = -(*p) + *q;
1392             i__2 = *q - *p;
1393             dlacpy_("U", &i__1, &i__2, &x21[*m - *q + 1 + (*p + 1) * x21_dim1]
1394                     , ldx21, &v1t[*p + 1 + (*p + 1) * v1t_dim1], ldv1t);
1395             dorglq_(q, q, q, &v1t[v1t_offset], ldv1t, &work[itauq1], &work[
1396                     iorglq], &lorglq, &childinfo);
1397         }
1398
1399 /*        Simultaneously diagonalize X11 and X21. */
1400
1401         i__1 = *m - *p;
1402         i__2 = *m - *q;
1403         dbbcsd_(jobu2, jobu1, "N", jobv1t, "N", m, &i__1, &i__2, &theta[1], &
1404                 work[iphi], &u2[u2_offset], ldu2, &u1[u1_offset], ldu1, dum2, 
1405                 &c__1, &v1t[v1t_offset], ldv1t, &work[ib11d], &work[ib11e], &
1406                 work[ib12d], &work[ib12e], &work[ib21d], &work[ib21e], &work[
1407                 ib22d], &work[ib22e], &work[ibbcsd], &lbbcsd, &childinfo);
1408
1409 /*        Permute rows and columns to place identity submatrices in */
1410 /*        preferred positions */
1411
1412         if (*p > r__) {
1413             i__1 = r__;
1414             for (i__ = 1; i__ <= i__1; ++i__) {
1415                 iwork[i__] = *p - r__ + i__;
1416             }
1417             i__1 = *p;
1418             for (i__ = r__ + 1; i__ <= i__1; ++i__) {
1419                 iwork[i__] = i__ - r__;
1420             }
1421             if (wantu1) {
1422                 dlapmt_(&c_false, p, p, &u1[u1_offset], ldu1, &iwork[1]);
1423             }
1424             if (wantv1t) {
1425                 dlapmr_(&c_false, p, q, &v1t[v1t_offset], ldv1t, &iwork[1]);
1426             }
1427         }
1428     }
1429
1430     return 0;
1431
1432 /*     End of DORCSD2BY1 */
1433
1434 } /* dorcsd2by1_ */
1435