dtbrfs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. /* dtbrfs.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. http://www.netlib.org/f2c/libf2c.zip
  10. */
  11. #include "f2c.h"
  12. #include "blaswrap.h"
  13. /* Table of constant values */
  14. static integer c__1 = 1;
  15. static doublereal c_b19 = -1.;
  16. /* Subroutine */ int dtbrfs_(char *uplo, char *trans, char *diag, integer *n,
  17. integer *kd, integer *nrhs, doublereal *ab, integer *ldab, doublereal
  18. *b, integer *ldb, doublereal *x, integer *ldx, doublereal *ferr,
  19. doublereal *berr, doublereal *work, integer *iwork, integer *info)
  20. {
  21. /* System generated locals */
  22. integer ab_dim1, ab_offset, b_dim1, b_offset, x_dim1, x_offset, i__1,
  23. i__2, i__3, i__4, i__5;
  24. doublereal d__1, d__2, d__3;
  25. /* Local variables */
  26. integer i__, j, k;
  27. doublereal s, xk;
  28. integer nz;
  29. doublereal eps;
  30. integer kase;
  31. doublereal safe1, safe2;
  32. extern logical lsame_(char *, char *);
  33. integer isave[3];
  34. extern /* Subroutine */ int dtbmv_(char *, char *, char *, integer *,
  35. integer *, doublereal *, integer *, doublereal *, integer *), dcopy_(integer *, doublereal *, integer *
  36. , doublereal *, integer *), dtbsv_(char *, char *, char *,
  37. integer *, integer *, doublereal *, integer *, doublereal *,
  38. integer *), daxpy_(integer *, doublereal *
  39. , doublereal *, integer *, doublereal *, integer *);
  40. logical upper;
  41. extern /* Subroutine */ int dlacn2_(integer *, doublereal *, doublereal *,
  42. integer *, doublereal *, integer *, integer *);
  43. extern doublereal dlamch_(char *);
  44. doublereal safmin;
  45. extern /* Subroutine */ int xerbla_(char *, integer *);
  46. logical notran;
  47. char transt[1];
  48. logical nounit;
  49. doublereal lstres;
  50. /* -- LAPACK routine (version 3.2) -- */
  51. /* Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
  52. /* November 2006 */
  53. /* Modified to call DLACN2 in place of DLACON, 5 Feb 03, SJH. */
  54. /* .. Scalar Arguments .. */
  55. /* .. */
  56. /* .. Array Arguments .. */
  57. /* .. */
  58. /* Purpose */
  59. /* ======= */
  60. /* DTBRFS provides error bounds and backward error estimates for the */
  61. /* solution to a system of linear equations with a triangular band */
  62. /* coefficient matrix. */
  63. /* The solution matrix X must be computed by DTBTRS or some other */
  64. /* means before entering this routine. DTBRFS does not do iterative */
  65. /* refinement because doing so cannot improve the backward error. */
  66. /* Arguments */
  67. /* ========= */
  68. /* UPLO (input) CHARACTER*1 */
  69. /* = 'U': A is upper triangular; */
  70. /* = 'L': A is lower triangular. */
  71. /* TRANS (input) CHARACTER*1 */
  72. /* Specifies the form of the system of equations: */
  73. /* = 'N': A * X = B (No transpose) */
  74. /* = 'T': A**T * X = B (Transpose) */
  75. /* = 'C': A**H * X = B (Conjugate transpose = Transpose) */
  76. /* DIAG (input) CHARACTER*1 */
  77. /* = 'N': A is non-unit triangular; */
  78. /* = 'U': A is unit triangular. */
  79. /* N (input) INTEGER */
  80. /* The order of the matrix A. N >= 0. */
  81. /* KD (input) INTEGER */
  82. /* The number of superdiagonals or subdiagonals of the */
  83. /* triangular band matrix A. KD >= 0. */
  84. /* NRHS (input) INTEGER */
  85. /* The number of right hand sides, i.e., the number of columns */
  86. /* of the matrices B and X. NRHS >= 0. */
  87. /* AB (input) DOUBLE PRECISION array, dimension (LDAB,N) */
  88. /* The upper or lower triangular band matrix A, stored in the */
  89. /* first kd+1 rows of the array. The j-th column of A is stored */
  90. /* in the j-th column of the array AB as follows: */
  91. /* if UPLO = 'U', AB(kd+1+i-j,j) = A(i,j) for max(1,j-kd)<=i<=j; */
  92. /* if UPLO = 'L', AB(1+i-j,j) = A(i,j) for j<=i<=min(n,j+kd). */
  93. /* If DIAG = 'U', the diagonal elements of A are not referenced */
  94. /* and are assumed to be 1. */
  95. /* LDAB (input) INTEGER */
  96. /* The leading dimension of the array AB. LDAB >= KD+1. */
  97. /* B (input) DOUBLE PRECISION array, dimension (LDB,NRHS) */
  98. /* The right hand side matrix B. */
  99. /* LDB (input) INTEGER */
  100. /* The leading dimension of the array B. LDB >= max(1,N). */
  101. /* X (input) DOUBLE PRECISION array, dimension (LDX,NRHS) */
  102. /* The solution matrix X. */
  103. /* LDX (input) INTEGER */
  104. /* The leading dimension of the array X. LDX >= max(1,N). */
  105. /* FERR (output) DOUBLE PRECISION array, dimension (NRHS) */
  106. /* The estimated forward error bound for each solution vector */
  107. /* X(j) (the j-th column of the solution matrix X). */
  108. /* If XTRUE is the true solution corresponding to X(j), FERR(j) */
  109. /* is an estimated upper bound for the magnitude of the largest */
  110. /* element in (X(j) - XTRUE) divided by the magnitude of the */
  111. /* largest element in X(j). The estimate is as reliable as */
  112. /* the estimate for RCOND, and is almost always a slight */
  113. /* overestimate of the true error. */
  114. /* BERR (output) DOUBLE PRECISION array, dimension (NRHS) */
  115. /* The componentwise relative backward error of each solution */
  116. /* vector X(j) (i.e., the smallest relative change in */
  117. /* any element of A or B that makes X(j) an exact solution). */
  118. /* WORK (workspace) DOUBLE PRECISION array, dimension (3*N) */
  119. /* IWORK (workspace) INTEGER array, dimension (N) */
  120. /* INFO (output) INTEGER */
  121. /* = 0: successful exit */
  122. /* < 0: if INFO = -i, the i-th argument had an illegal value */
  123. /* ===================================================================== */
  124. /* .. Parameters .. */
  125. /* .. */
  126. /* .. Local Scalars .. */
  127. /* .. */
  128. /* .. Local Arrays .. */
  129. /* .. */
  130. /* .. External Subroutines .. */
  131. /* .. */
  132. /* .. Intrinsic Functions .. */
  133. /* .. */
  134. /* .. External Functions .. */
  135. /* .. */
  136. /* .. Executable Statements .. */
  137. /* Test the input parameters. */
  138. /* Parameter adjustments */
  139. ab_dim1 = *ldab;
  140. ab_offset = 1 + ab_dim1;
  141. ab -= ab_offset;
  142. b_dim1 = *ldb;
  143. b_offset = 1 + b_dim1;
  144. b -= b_offset;
  145. x_dim1 = *ldx;
  146. x_offset = 1 + x_dim1;
  147. x -= x_offset;
  148. --ferr;
  149. --berr;
  150. --work;
  151. --iwork;
  152. /* Function Body */
  153. *info = 0;
  154. upper = lsame_(uplo, "U");
  155. notran = lsame_(trans, "N");
  156. nounit = lsame_(diag, "N");
  157. if (! upper && ! lsame_(uplo, "L")) {
  158. *info = -1;
  159. } else if (! notran && ! lsame_(trans, "T") && !
  160. lsame_(trans, "C")) {
  161. *info = -2;
  162. } else if (! nounit && ! lsame_(diag, "U")) {
  163. *info = -3;
  164. } else if (*n < 0) {
  165. *info = -4;
  166. } else if (*kd < 0) {
  167. *info = -5;
  168. } else if (*nrhs < 0) {
  169. *info = -6;
  170. } else if (*ldab < *kd + 1) {
  171. *info = -8;
  172. } else if (*ldb < max(1,*n)) {
  173. *info = -10;
  174. } else if (*ldx < max(1,*n)) {
  175. *info = -12;
  176. }
  177. if (*info != 0) {
  178. i__1 = -(*info);
  179. xerbla_("DTBRFS", &i__1);
  180. return 0;
  181. }
  182. /* Quick return if possible */
  183. if (*n == 0 || *nrhs == 0) {
  184. i__1 = *nrhs;
  185. for (j = 1; j <= i__1; ++j) {
  186. ferr[j] = 0.;
  187. berr[j] = 0.;
  188. /* L10: */
  189. }
  190. return 0;
  191. }
  192. if (notran) {
  193. *(unsigned char *)transt = 'T';
  194. } else {
  195. *(unsigned char *)transt = 'N';
  196. }
  197. /* NZ = maximum number of nonzero elements in each row of A, plus 1 */
  198. nz = *kd + 2;
  199. eps = dlamch_("Epsilon");
  200. safmin = dlamch_("Safe minimum");
  201. safe1 = nz * safmin;
  202. safe2 = safe1 / eps;
  203. /* Do for each right hand side */
  204. i__1 = *nrhs;
  205. for (j = 1; j <= i__1; ++j) {
  206. /* Compute residual R = B - op(A) * X, */
  207. /* where op(A) = A or A', depending on TRANS. */
  208. dcopy_(n, &x[j * x_dim1 + 1], &c__1, &work[*n + 1], &c__1);
  209. dtbmv_(uplo, trans, diag, n, kd, &ab[ab_offset], ldab, &work[*n + 1],
  210. &c__1);
  211. daxpy_(n, &c_b19, &b[j * b_dim1 + 1], &c__1, &work[*n + 1], &c__1);
  212. /* Compute componentwise relative backward error from formula */
  213. /* max(i) ( abs(R(i)) / ( abs(op(A))*abs(X) + abs(B) )(i) ) */
  214. /* where abs(Z) is the componentwise absolute value of the matrix */
  215. /* or vector Z. If the i-th component of the denominator is less */
  216. /* than SAFE2, then SAFE1 is added to the i-th components of the */
  217. /* numerator and denominator before dividing. */
  218. i__2 = *n;
  219. for (i__ = 1; i__ <= i__2; ++i__) {
  220. work[i__] = (d__1 = b[i__ + j * b_dim1], abs(d__1));
  221. /* L20: */
  222. }
  223. if (notran) {
  224. /* Compute abs(A)*abs(X) + abs(B). */
  225. if (upper) {
  226. if (nounit) {
  227. i__2 = *n;
  228. for (k = 1; k <= i__2; ++k) {
  229. xk = (d__1 = x[k + j * x_dim1], abs(d__1));
  230. /* Computing MAX */
  231. i__3 = 1, i__4 = k - *kd;
  232. i__5 = k;
  233. for (i__ = max(i__3,i__4); i__ <= i__5; ++i__) {
  234. work[i__] += (d__1 = ab[*kd + 1 + i__ - k + k *
  235. ab_dim1], abs(d__1)) * xk;
  236. /* L30: */
  237. }
  238. /* L40: */
  239. }
  240. } else {
  241. i__2 = *n;
  242. for (k = 1; k <= i__2; ++k) {
  243. xk = (d__1 = x[k + j * x_dim1], abs(d__1));
  244. /* Computing MAX */
  245. i__5 = 1, i__3 = k - *kd;
  246. i__4 = k - 1;
  247. for (i__ = max(i__5,i__3); i__ <= i__4; ++i__) {
  248. work[i__] += (d__1 = ab[*kd + 1 + i__ - k + k *
  249. ab_dim1], abs(d__1)) * xk;
  250. /* L50: */
  251. }
  252. work[k] += xk;
  253. /* L60: */
  254. }
  255. }
  256. } else {
  257. if (nounit) {
  258. i__2 = *n;
  259. for (k = 1; k <= i__2; ++k) {
  260. xk = (d__1 = x[k + j * x_dim1], abs(d__1));
  261. /* Computing MIN */
  262. i__5 = *n, i__3 = k + *kd;
  263. i__4 = min(i__5,i__3);
  264. for (i__ = k; i__ <= i__4; ++i__) {
  265. work[i__] += (d__1 = ab[i__ + 1 - k + k * ab_dim1]
  266. , abs(d__1)) * xk;
  267. /* L70: */
  268. }
  269. /* L80: */
  270. }
  271. } else {
  272. i__2 = *n;
  273. for (k = 1; k <= i__2; ++k) {
  274. xk = (d__1 = x[k + j * x_dim1], abs(d__1));
  275. /* Computing MIN */
  276. i__5 = *n, i__3 = k + *kd;
  277. i__4 = min(i__5,i__3);
  278. for (i__ = k + 1; i__ <= i__4; ++i__) {
  279. work[i__] += (d__1 = ab[i__ + 1 - k + k * ab_dim1]
  280. , abs(d__1)) * xk;
  281. /* L90: */
  282. }
  283. work[k] += xk;
  284. /* L100: */
  285. }
  286. }
  287. }
  288. } else {
  289. /* Compute abs(A')*abs(X) + abs(B). */
  290. if (upper) {
  291. if (nounit) {
  292. i__2 = *n;
  293. for (k = 1; k <= i__2; ++k) {
  294. s = 0.;
  295. /* Computing MAX */
  296. i__4 = 1, i__5 = k - *kd;
  297. i__3 = k;
  298. for (i__ = max(i__4,i__5); i__ <= i__3; ++i__) {
  299. s += (d__1 = ab[*kd + 1 + i__ - k + k * ab_dim1],
  300. abs(d__1)) * (d__2 = x[i__ + j * x_dim1],
  301. abs(d__2));
  302. /* L110: */
  303. }
  304. work[k] += s;
  305. /* L120: */
  306. }
  307. } else {
  308. i__2 = *n;
  309. for (k = 1; k <= i__2; ++k) {
  310. s = (d__1 = x[k + j * x_dim1], abs(d__1));
  311. /* Computing MAX */
  312. i__3 = 1, i__4 = k - *kd;
  313. i__5 = k - 1;
  314. for (i__ = max(i__3,i__4); i__ <= i__5; ++i__) {
  315. s += (d__1 = ab[*kd + 1 + i__ - k + k * ab_dim1],
  316. abs(d__1)) * (d__2 = x[i__ + j * x_dim1],
  317. abs(d__2));
  318. /* L130: */
  319. }
  320. work[k] += s;
  321. /* L140: */
  322. }
  323. }
  324. } else {
  325. if (nounit) {
  326. i__2 = *n;
  327. for (k = 1; k <= i__2; ++k) {
  328. s = 0.;
  329. /* Computing MIN */
  330. i__3 = *n, i__4 = k + *kd;
  331. i__5 = min(i__3,i__4);
  332. for (i__ = k; i__ <= i__5; ++i__) {
  333. s += (d__1 = ab[i__ + 1 - k + k * ab_dim1], abs(
  334. d__1)) * (d__2 = x[i__ + j * x_dim1], abs(
  335. d__2));
  336. /* L150: */
  337. }
  338. work[k] += s;
  339. /* L160: */
  340. }
  341. } else {
  342. i__2 = *n;
  343. for (k = 1; k <= i__2; ++k) {
  344. s = (d__1 = x[k + j * x_dim1], abs(d__1));
  345. /* Computing MIN */
  346. i__3 = *n, i__4 = k + *kd;
  347. i__5 = min(i__3,i__4);
  348. for (i__ = k + 1; i__ <= i__5; ++i__) {
  349. s += (d__1 = ab[i__ + 1 - k + k * ab_dim1], abs(
  350. d__1)) * (d__2 = x[i__ + j * x_dim1], abs(
  351. d__2));
  352. /* L170: */
  353. }
  354. work[k] += s;
  355. /* L180: */
  356. }
  357. }
  358. }
  359. }
  360. s = 0.;
  361. i__2 = *n;
  362. for (i__ = 1; i__ <= i__2; ++i__) {
  363. if (work[i__] > safe2) {
  364. /* Computing MAX */
  365. d__2 = s, d__3 = (d__1 = work[*n + i__], abs(d__1)) / work[
  366. i__];
  367. s = max(d__2,d__3);
  368. } else {
  369. /* Computing MAX */
  370. d__2 = s, d__3 = ((d__1 = work[*n + i__], abs(d__1)) + safe1)
  371. / (work[i__] + safe1);
  372. s = max(d__2,d__3);
  373. }
  374. /* L190: */
  375. }
  376. berr[j] = s;
  377. /* Bound error from formula */
  378. /* norm(X - XTRUE) / norm(X) .le. FERR = */
  379. /* norm( abs(inv(op(A)))* */
  380. /* ( abs(R) + NZ*EPS*( abs(op(A))*abs(X)+abs(B) ))) / norm(X) */
  381. /* where */
  382. /* norm(Z) is the magnitude of the largest component of Z */
  383. /* inv(op(A)) is the inverse of op(A) */
  384. /* abs(Z) is the componentwise absolute value of the matrix or */
  385. /* vector Z */
  386. /* NZ is the maximum number of nonzeros in any row of A, plus 1 */
  387. /* EPS is machine epsilon */
  388. /* The i-th component of abs(R)+NZ*EPS*(abs(op(A))*abs(X)+abs(B)) */
  389. /* is incremented by SAFE1 if the i-th component of */
  390. /* abs(op(A))*abs(X) + abs(B) is less than SAFE2. */
  391. /* Use DLACN2 to estimate the infinity-norm of the matrix */
  392. /* inv(op(A)) * diag(W), */
  393. /* where W = abs(R) + NZ*EPS*( abs(op(A))*abs(X)+abs(B) ))) */
  394. i__2 = *n;
  395. for (i__ = 1; i__ <= i__2; ++i__) {
  396. if (work[i__] > safe2) {
  397. work[i__] = (d__1 = work[*n + i__], abs(d__1)) + nz * eps *
  398. work[i__];
  399. } else {
  400. work[i__] = (d__1 = work[*n + i__], abs(d__1)) + nz * eps *
  401. work[i__] + safe1;
  402. }
  403. /* L200: */
  404. }
  405. kase = 0;
  406. L210:
  407. dlacn2_(n, &work[(*n << 1) + 1], &work[*n + 1], &iwork[1], &ferr[j], &
  408. kase, isave);
  409. if (kase != 0) {
  410. if (kase == 1) {
  411. /* Multiply by diag(W)*inv(op(A)'). */
  412. dtbsv_(uplo, transt, diag, n, kd, &ab[ab_offset], ldab, &work[
  413. *n + 1], &c__1);
  414. i__2 = *n;
  415. for (i__ = 1; i__ <= i__2; ++i__) {
  416. work[*n + i__] = work[i__] * work[*n + i__];
  417. /* L220: */
  418. }
  419. } else {
  420. /* Multiply by inv(op(A))*diag(W). */
  421. i__2 = *n;
  422. for (i__ = 1; i__ <= i__2; ++i__) {
  423. work[*n + i__] = work[i__] * work[*n + i__];
  424. /* L230: */
  425. }
  426. dtbsv_(uplo, trans, diag, n, kd, &ab[ab_offset], ldab, &work[*
  427. n + 1], &c__1);
  428. }
  429. goto L210;
  430. }
  431. /* Normalize error. */
  432. lstres = 0.;
  433. i__2 = *n;
  434. for (i__ = 1; i__ <= i__2; ++i__) {
  435. /* Computing MAX */
  436. d__2 = lstres, d__3 = (d__1 = x[i__ + j * x_dim1], abs(d__1));
  437. lstres = max(d__2,d__3);
  438. /* L240: */
  439. }
  440. if (lstres != 0.) {
  441. ferr[j] /= lstres;
  442. }
  443. /* L250: */
  444. }
  445. return 0;
  446. /* End of DTBRFS */
  447. } /* dtbrfs_ */