00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include "f2c.h"
00014 #include "blaswrap.h"
00015
00016
00017
00018 static integer c__1 = 1;
00019 static integer c_n1 = -1;
00020 static integer c__0 = 0;
00021 static real c_b31 = 0.f;
00022 static integer c__2 = 2;
00023 static real c_b54 = 1.f;
00024
00025 int sgelsy_(integer *m, integer *n, integer *nrhs, real *a,
00026 integer *lda, real *b, integer *ldb, integer *jpvt, real *rcond,
00027 integer *rank, real *work, integer *lwork, integer *info)
00028 {
00029
00030 integer a_dim1, a_offset, b_dim1, b_offset, i__1, i__2;
00031 real r__1, r__2;
00032
00033
00034 integer i__, j;
00035 real c1, c2, s1, s2;
00036 integer nb, mn, nb1, nb2, nb3, nb4;
00037 real anrm, bnrm, smin, smax;
00038 integer iascl, ibscl, ismin, ismax;
00039 extern int scopy_(integer *, real *, integer *, real *,
00040 integer *);
00041 real wsize;
00042 extern int strsm_(char *, char *, char *, char *,
00043 integer *, integer *, real *, real *, integer *, real *, integer *
00044 ), slaic1_(integer *, integer *,
00045 real *, real *, real *, real *, real *, real *, real *), sgeqp3_(
00046 integer *, integer *, real *, integer *, integer *, real *, real *
00047 , integer *, integer *), slabad_(real *, real *);
00048 extern doublereal slamch_(char *), slange_(char *, integer *,
00049 integer *, real *, integer *, real *);
00050 extern int xerbla_(char *, integer *);
00051 extern integer ilaenv_(integer *, char *, char *, integer *, integer *,
00052 integer *, integer *);
00053 real bignum;
00054 extern int slascl_(char *, integer *, integer *, real *,
00055 real *, integer *, integer *, real *, integer *, integer *), slaset_(char *, integer *, integer *, real *, real *,
00056 real *, integer *);
00057 integer lwkmin;
00058 real sminpr, smaxpr, smlnum;
00059 integer lwkopt;
00060 logical lquery;
00061 extern int sormqr_(char *, char *, integer *, integer *,
00062 integer *, real *, integer *, real *, real *, integer *, real *,
00063 integer *, integer *), sormrz_(char *, char *,
00064 integer *, integer *, integer *, integer *, real *, integer *,
00065 real *, real *, integer *, real *, integer *, integer *), stzrzf_(integer *, integer *, real *, integer *, real *,
00066 real *, integer *, integer *);
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209 a_dim1 = *lda;
00210 a_offset = 1 + a_dim1;
00211 a -= a_offset;
00212 b_dim1 = *ldb;
00213 b_offset = 1 + b_dim1;
00214 b -= b_offset;
00215 --jpvt;
00216 --work;
00217
00218
00219 mn = min(*m,*n);
00220 ismin = mn + 1;
00221 ismax = (mn << 1) + 1;
00222
00223
00224
00225 *info = 0;
00226 lquery = *lwork == -1;
00227 if (*m < 0) {
00228 *info = -1;
00229 } else if (*n < 0) {
00230 *info = -2;
00231 } else if (*nrhs < 0) {
00232 *info = -3;
00233 } else if (*lda < max(1,*m)) {
00234 *info = -5;
00235 } else {
00236
00237 i__1 = max(1,*m);
00238 if (*ldb < max(i__1,*n)) {
00239 *info = -7;
00240 }
00241 }
00242
00243
00244
00245 if (*info == 0) {
00246 if (mn == 0 || *nrhs == 0) {
00247 lwkmin = 1;
00248 lwkopt = 1;
00249 } else {
00250 nb1 = ilaenv_(&c__1, "SGEQRF", " ", m, n, &c_n1, &c_n1);
00251 nb2 = ilaenv_(&c__1, "SGERQF", " ", m, n, &c_n1, &c_n1);
00252 nb3 = ilaenv_(&c__1, "SORMQR", " ", m, n, nrhs, &c_n1);
00253 nb4 = ilaenv_(&c__1, "SORMRQ", " ", m, n, nrhs, &c_n1);
00254
00255 i__1 = max(nb1,nb2), i__1 = max(i__1,nb3);
00256 nb = max(i__1,nb4);
00257
00258 i__1 = mn << 1, i__2 = *n + 1, i__1 = max(i__1,i__2), i__2 = mn +
00259 *nrhs;
00260 lwkmin = mn + max(i__1,i__2);
00261
00262 i__1 = lwkmin, i__2 = mn + (*n << 1) + nb * (*n + 1), i__1 = max(
00263 i__1,i__2), i__2 = (mn << 1) + nb * *nrhs;
00264 lwkopt = max(i__1,i__2);
00265 }
00266 work[1] = (real) lwkopt;
00267
00268 if (*lwork < lwkmin && ! lquery) {
00269 *info = -12;
00270 }
00271 }
00272
00273 if (*info != 0) {
00274 i__1 = -(*info);
00275 xerbla_("SGELSY", &i__1);
00276 return 0;
00277 } else if (lquery) {
00278 return 0;
00279 }
00280
00281
00282
00283 if (mn == 0 || *nrhs == 0) {
00284 *rank = 0;
00285 return 0;
00286 }
00287
00288
00289
00290 smlnum = slamch_("S") / slamch_("P");
00291 bignum = 1.f / smlnum;
00292 slabad_(&smlnum, &bignum);
00293
00294
00295
00296 anrm = slange_("M", m, n, &a[a_offset], lda, &work[1]);
00297 iascl = 0;
00298 if (anrm > 0.f && anrm < smlnum) {
00299
00300
00301
00302 slascl_("G", &c__0, &c__0, &anrm, &smlnum, m, n, &a[a_offset], lda,
00303 info);
00304 iascl = 1;
00305 } else if (anrm > bignum) {
00306
00307
00308
00309 slascl_("G", &c__0, &c__0, &anrm, &bignum, m, n, &a[a_offset], lda,
00310 info);
00311 iascl = 2;
00312 } else if (anrm == 0.f) {
00313
00314
00315
00316 i__1 = max(*m,*n);
00317 slaset_("F", &i__1, nrhs, &c_b31, &c_b31, &b[b_offset], ldb);
00318 *rank = 0;
00319 goto L70;
00320 }
00321
00322 bnrm = slange_("M", m, nrhs, &b[b_offset], ldb, &work[1]);
00323 ibscl = 0;
00324 if (bnrm > 0.f && bnrm < smlnum) {
00325
00326
00327
00328 slascl_("G", &c__0, &c__0, &bnrm, &smlnum, m, nrhs, &b[b_offset], ldb,
00329 info);
00330 ibscl = 1;
00331 } else if (bnrm > bignum) {
00332
00333
00334
00335 slascl_("G", &c__0, &c__0, &bnrm, &bignum, m, nrhs, &b[b_offset], ldb,
00336 info);
00337 ibscl = 2;
00338 }
00339
00340
00341
00342
00343 i__1 = *lwork - mn;
00344 sgeqp3_(m, n, &a[a_offset], lda, &jpvt[1], &work[1], &work[mn + 1], &i__1,
00345 info);
00346 wsize = mn + work[mn + 1];
00347
00348
00349
00350
00351
00352
00353 work[ismin] = 1.f;
00354 work[ismax] = 1.f;
00355 smax = (r__1 = a[a_dim1 + 1], dabs(r__1));
00356 smin = smax;
00357 if ((r__1 = a[a_dim1 + 1], dabs(r__1)) == 0.f) {
00358 *rank = 0;
00359 i__1 = max(*m,*n);
00360 slaset_("F", &i__1, nrhs, &c_b31, &c_b31, &b[b_offset], ldb);
00361 goto L70;
00362 } else {
00363 *rank = 1;
00364 }
00365
00366 L10:
00367 if (*rank < mn) {
00368 i__ = *rank + 1;
00369 slaic1_(&c__2, rank, &work[ismin], &smin, &a[i__ * a_dim1 + 1], &a[
00370 i__ + i__ * a_dim1], &sminpr, &s1, &c1);
00371 slaic1_(&c__1, rank, &work[ismax], &smax, &a[i__ * a_dim1 + 1], &a[
00372 i__ + i__ * a_dim1], &smaxpr, &s2, &c2);
00373
00374 if (smaxpr * *rcond <= sminpr) {
00375 i__1 = *rank;
00376 for (i__ = 1; i__ <= i__1; ++i__) {
00377 work[ismin + i__ - 1] = s1 * work[ismin + i__ - 1];
00378 work[ismax + i__ - 1] = s2 * work[ismax + i__ - 1];
00379
00380 }
00381 work[ismin + *rank] = c1;
00382 work[ismax + *rank] = c2;
00383 smin = sminpr;
00384 smax = smaxpr;
00385 ++(*rank);
00386 goto L10;
00387 }
00388 }
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398 if (*rank < *n) {
00399 i__1 = *lwork - (mn << 1);
00400 stzrzf_(rank, n, &a[a_offset], lda, &work[mn + 1], &work[(mn << 1) +
00401 1], &i__1, info);
00402 }
00403
00404
00405
00406
00407
00408
00409 i__1 = *lwork - (mn << 1);
00410 sormqr_("Left", "Transpose", m, nrhs, &mn, &a[a_offset], lda, &work[1], &
00411 b[b_offset], ldb, &work[(mn << 1) + 1], &i__1, info);
00412
00413 r__1 = wsize, r__2 = (mn << 1) + work[(mn << 1) + 1];
00414 wsize = dmax(r__1,r__2);
00415
00416
00417
00418
00419
00420 strsm_("Left", "Upper", "No transpose", "Non-unit", rank, nrhs, &c_b54, &
00421 a[a_offset], lda, &b[b_offset], ldb);
00422
00423 i__1 = *nrhs;
00424 for (j = 1; j <= i__1; ++j) {
00425 i__2 = *n;
00426 for (i__ = *rank + 1; i__ <= i__2; ++i__) {
00427 b[i__ + j * b_dim1] = 0.f;
00428
00429 }
00430
00431 }
00432
00433
00434
00435 if (*rank < *n) {
00436 i__1 = *n - *rank;
00437 i__2 = *lwork - (mn << 1);
00438 sormrz_("Left", "Transpose", n, nrhs, rank, &i__1, &a[a_offset], lda,
00439 &work[mn + 1], &b[b_offset], ldb, &work[(mn << 1) + 1], &i__2,
00440 info);
00441 }
00442
00443
00444
00445
00446
00447 i__1 = *nrhs;
00448 for (j = 1; j <= i__1; ++j) {
00449 i__2 = *n;
00450 for (i__ = 1; i__ <= i__2; ++i__) {
00451 work[jpvt[i__]] = b[i__ + j * b_dim1];
00452
00453 }
00454 scopy_(n, &work[1], &c__1, &b[j * b_dim1 + 1], &c__1);
00455
00456 }
00457
00458
00459
00460
00461
00462 if (iascl == 1) {
00463 slascl_("G", &c__0, &c__0, &anrm, &smlnum, n, nrhs, &b[b_offset], ldb,
00464 info);
00465 slascl_("U", &c__0, &c__0, &smlnum, &anrm, rank, rank, &a[a_offset],
00466 lda, info);
00467 } else if (iascl == 2) {
00468 slascl_("G", &c__0, &c__0, &anrm, &bignum, n, nrhs, &b[b_offset], ldb,
00469 info);
00470 slascl_("U", &c__0, &c__0, &bignum, &anrm, rank, rank, &a[a_offset],
00471 lda, info);
00472 }
00473 if (ibscl == 1) {
00474 slascl_("G", &c__0, &c__0, &smlnum, &bnrm, n, nrhs, &b[b_offset], ldb,
00475 info);
00476 } else if (ibscl == 2) {
00477 slascl_("G", &c__0, &c__0, &bignum, &bnrm, n, nrhs, &b[b_offset], ldb,
00478 info);
00479 }
00480
00481 L70:
00482 work[1] = (real) lwkopt;
00483
00484 return 0;
00485
00486
00487
00488 }