dsytri.c 10.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/* dsytri.f -- translated by f2c (version 20061008).
   You must link the resulting object file with libf2c:
	on Microsoft Windows system, link with libf2c.lib;
	on Linux or Unix systems, link with .../path/to/libf2c.a -lm
	or, if you install libf2c.a in a standard place, with -lf2c -lm
	-- in that order, at the end of the command line, as in
		cc *.o -lf2c -lm
	Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,

		http://www.netlib.org/f2c/libf2c.zip
*/

13 14
#include "clapack.h"

15 16 17 18 19 20 21

/* Table of constant values */

static integer c__1 = 1;
static doublereal c_b11 = -1.;
static doublereal c_b13 = 0.;

22 23 24
/* Subroutine */ int dsytri_(char *uplo, integer *n, doublereal *a, integer *
	lda, integer *ipiv, doublereal *work, integer *info)
{
25 26 27
    /* System generated locals */
    integer a_dim1, a_offset, i__1;
    doublereal d__1;
28

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
    /* Local variables */
    doublereal d__;
    integer k;
    doublereal t, ak;
    integer kp;
    doublereal akp1;
    extern doublereal ddot_(integer *, doublereal *, integer *, doublereal *, 
	    integer *);
    doublereal temp, akkp1;
    extern logical lsame_(char *, char *);
    extern /* Subroutine */ int dcopy_(integer *, doublereal *, integer *, 
	    doublereal *, integer *), dswap_(integer *, doublereal *, integer 
	    *, doublereal *, integer *);
    integer kstep;
    logical upper;
    extern /* Subroutine */ int dsymv_(char *, integer *, doublereal *, 
	    doublereal *, integer *, doublereal *, integer *, doublereal *, 
	    doublereal *, integer *), xerbla_(char *, integer *);
47 48


49 50 51
/*  -- LAPACK routine (version 3.2) -- */
/*     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
/*     November 2006 */
52

53 54 55 56
/*     .. Scalar Arguments .. */
/*     .. */
/*     .. Array Arguments .. */
/*     .. */
57

58 59
/*  Purpose */
/*  ======= */
60

61 62 63
/*  DSYTRI computes the inverse of a real symmetric indefinite matrix */
/*  A using the factorization A = U*D*U**T or A = L*D*L**T computed by */
/*  DSYTRF. */
64

65 66
/*  Arguments */
/*  ========= */
67

68 69 70 71 72
/*  UPLO    (input) CHARACTER*1 */
/*          Specifies whether the details of the factorization are stored */
/*          as an upper or lower triangular matrix. */
/*          = 'U':  Upper triangular, form is A = U*D*U**T; */
/*          = 'L':  Lower triangular, form is A = L*D*L**T. */
73

74 75
/*  N       (input) INTEGER */
/*          The order of the matrix A.  N >= 0. */
76

77 78 79
/*  A       (input/output) DOUBLE PRECISION array, dimension (LDA,N) */
/*          On entry, the block diagonal matrix D and the multipliers */
/*          used to obtain the factor U or L as computed by DSYTRF. */
80

81 82 83 84 85 86
/*          On exit, if INFO = 0, the (symmetric) inverse of the original */
/*          matrix.  If UPLO = 'U', the upper triangular part of the */
/*          inverse is formed and the part of A below the diagonal is not */
/*          referenced; if UPLO = 'L' the lower triangular part of the */
/*          inverse is formed and the part of A above the diagonal is */
/*          not referenced. */
87

88 89
/*  LDA     (input) INTEGER */
/*          The leading dimension of the array A.  LDA >= max(1,N). */
90

91 92 93
/*  IPIV    (input) INTEGER array, dimension (N) */
/*          Details of the interchanges and the block structure of D */
/*          as determined by DSYTRF. */
94

95
/*  WORK    (workspace) DOUBLE PRECISION array, dimension (N) */
96

97 98 99 100 101
/*  INFO    (output) INTEGER */
/*          = 0: successful exit */
/*          < 0: if INFO = -i, the i-th argument had an illegal value */
/*          > 0: if INFO = i, D(i,i) = 0; the matrix is singular and its */
/*               inverse could not be computed. */
102

103
/*  ===================================================================== */
104

105 106 107 108 109 110 111 112 113 114 115
/*     .. Parameters .. */
/*     .. */
/*     .. Local Scalars .. */
/*     .. */
/*     .. External Functions .. */
/*     .. */
/*     .. External Subroutines .. */
/*     .. */
/*     .. Intrinsic Functions .. */
/*     .. */
/*     .. Executable Statements .. */
116

117 118 119
/*     Test the input parameters. */

    /* Parameter adjustments */
120
    a_dim1 = *lda;
121
    a_offset = 1 + a_dim1;
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
    a -= a_offset;
    --ipiv;
    --work;

    /* Function Body */
    *info = 0;
    upper = lsame_(uplo, "U");
    if (! upper && ! lsame_(uplo, "L")) {
	*info = -1;
    } else if (*n < 0) {
	*info = -2;
    } else if (*lda < max(1,*n)) {
	*info = -4;
    }
    if (*info != 0) {
	i__1 = -(*info);
	xerbla_("DSYTRI", &i__1);
	return 0;
    }

/*     Quick return if possible */

    if (*n == 0) {
	return 0;
    }

/*     Check that the diagonal matrix D is nonsingular. */

    if (upper) {

/*        Upper triangular storage: examine D from bottom to top */

	for (*info = *n; *info >= 1; --(*info)) {
155
	    if (ipiv[*info] > 0 && a[*info + *info * a_dim1] == 0.) {
156 157 158 159 160 161 162 163 164 165
		return 0;
	    }
/* L10: */
	}
    } else {

/*        Lower triangular storage: examine D from top to bottom. */

	i__1 = *n;
	for (*info = 1; *info <= i__1; ++(*info)) {
166
	    if (ipiv[*info] > 0 && a[*info + *info * a_dim1] == 0.) {
167 168 169 170 171 172 173 174 175
		return 0;
	    }
/* L20: */
	}
    }
    *info = 0;

    if (upper) {

176
/*        Compute inv(A) from the factorization A = U*D*U'. */
177

178 179
/*        K is the main loop index, increasing from 1 to N in steps of */
/*        1 or 2, depending on the size of the diagonal blocks. */
180 181 182 183 184 185 186 187 188 189 190 191

	k = 1;
L30:

/*        If K > N, exit from loop. */

	if (k > *n) {
	    goto L40;
	}

	if (ipiv[k] > 0) {

192
/*           1 x 1 diagonal block */
193

194
/*           Invert the diagonal block. */
195

196
	    a[k + k * a_dim1] = 1. / a[k + k * a_dim1];
197 198 199 200 201

/*           Compute column K of the inverse. */

	    if (k > 1) {
		i__1 = k - 1;
202
		dcopy_(&i__1, &a[k * a_dim1 + 1], &c__1, &work[1], &c__1);
203 204
		i__1 = k - 1;
		dsymv_(uplo, &i__1, &c_b11, &a[a_offset], lda, &work[1], &
205
			c__1, &c_b13, &a[k * a_dim1 + 1], &c__1);
206
		i__1 = k - 1;
207 208
		a[k + k * a_dim1] -= ddot_(&i__1, &work[1], &c__1, &a[k * 
			a_dim1 + 1], &c__1);
209 210 211 212
	    }
	    kstep = 1;
	} else {

213
/*           2 x 2 diagonal block */
214

215
/*           Invert the diagonal block. */
216

217 218 219 220
	    t = (d__1 = a[k + (k + 1) * a_dim1], abs(d__1));
	    ak = a[k + k * a_dim1] / t;
	    akp1 = a[k + 1 + (k + 1) * a_dim1] / t;
	    akkp1 = a[k + (k + 1) * a_dim1] / t;
221
	    d__ = t * (ak * akp1 - 1.);
222 223 224
	    a[k + k * a_dim1] = akp1 / d__;
	    a[k + 1 + (k + 1) * a_dim1] = ak / d__;
	    a[k + (k + 1) * a_dim1] = -akkp1 / d__;
225 226 227 228 229

/*           Compute columns K and K+1 of the inverse. */

	    if (k > 1) {
		i__1 = k - 1;
230
		dcopy_(&i__1, &a[k * a_dim1 + 1], &c__1, &work[1], &c__1);
231 232
		i__1 = k - 1;
		dsymv_(uplo, &i__1, &c_b11, &a[a_offset], lda, &work[1], &
233
			c__1, &c_b13, &a[k * a_dim1 + 1], &c__1);
234
		i__1 = k - 1;
235 236
		a[k + k * a_dim1] -= ddot_(&i__1, &work[1], &c__1, &a[k * 
			a_dim1 + 1], &c__1);
237
		i__1 = k - 1;
238 239
		a[k + (k + 1) * a_dim1] -= ddot_(&i__1, &a[k * a_dim1 + 1], &
			c__1, &a[(k + 1) * a_dim1 + 1], &c__1);
240
		i__1 = k - 1;
241 242
		dcopy_(&i__1, &a[(k + 1) * a_dim1 + 1], &c__1, &work[1], &
			c__1);
243 244
		i__1 = k - 1;
		dsymv_(uplo, &i__1, &c_b11, &a[a_offset], lda, &work[1], &
245
			c__1, &c_b13, &a[(k + 1) * a_dim1 + 1], &c__1);
246
		i__1 = k - 1;
247 248
		a[k + 1 + (k + 1) * a_dim1] -= ddot_(&i__1, &work[1], &c__1, &
			a[(k + 1) * a_dim1 + 1], &c__1);
249 250 251 252 253 254 255
	    }
	    kstep = 2;
	}

	kp = (i__1 = ipiv[k], abs(i__1));
	if (kp != k) {

256 257
/*           Interchange rows and columns K and KP in the leading */
/*           submatrix A(1:k+1,1:k+1) */
258 259

	    i__1 = kp - 1;
260 261
	    dswap_(&i__1, &a[k * a_dim1 + 1], &c__1, &a[kp * a_dim1 + 1], &
		    c__1);
262
	    i__1 = k - kp - 1;
263 264 265 266 267
	    dswap_(&i__1, &a[kp + 1 + k * a_dim1], &c__1, &a[kp + (kp + 1) * 
		    a_dim1], lda);
	    temp = a[k + k * a_dim1];
	    a[k + k * a_dim1] = a[kp + kp * a_dim1];
	    a[kp + kp * a_dim1] = temp;
268
	    if (kstep == 2) {
269 270 271
		temp = a[k + (k + 1) * a_dim1];
		a[k + (k + 1) * a_dim1] = a[kp + (k + 1) * a_dim1];
		a[kp + (k + 1) * a_dim1] = temp;
272 273 274 275 276 277 278 279 280 281
	    }
	}

	k += kstep;
	goto L30;
L40:

	;
    } else {

282
/*        Compute inv(A) from the factorization A = L*D*L'. */
283

284 285
/*        K is the main loop index, increasing from 1 to N in steps of */
/*        1 or 2, depending on the size of the diagonal blocks. */
286 287 288 289 290 291 292 293 294 295 296 297

	k = *n;
L50:

/*        If K < 1, exit from loop. */

	if (k < 1) {
	    goto L60;
	}

	if (ipiv[k] > 0) {

298
/*           1 x 1 diagonal block */
299

300
/*           Invert the diagonal block. */
301

302
	    a[k + k * a_dim1] = 1. / a[k + k * a_dim1];
303 304 305 306 307

/*           Compute column K of the inverse. */

	    if (k < *n) {
		i__1 = *n - k;
308
		dcopy_(&i__1, &a[k + 1 + k * a_dim1], &c__1, &work[1], &c__1);
309
		i__1 = *n - k;
310 311 312
		dsymv_(uplo, &i__1, &c_b11, &a[k + 1 + (k + 1) * a_dim1], lda, 
			 &work[1], &c__1, &c_b13, &a[k + 1 + k * a_dim1], &
			c__1);
313
		i__1 = *n - k;
314 315
		a[k + k * a_dim1] -= ddot_(&i__1, &work[1], &c__1, &a[k + 1 + 
			k * a_dim1], &c__1);
316 317 318 319
	    }
	    kstep = 1;
	} else {

320
/*           2 x 2 diagonal block */
321

322
/*           Invert the diagonal block. */
323

324 325 326 327
	    t = (d__1 = a[k + (k - 1) * a_dim1], abs(d__1));
	    ak = a[k - 1 + (k - 1) * a_dim1] / t;
	    akp1 = a[k + k * a_dim1] / t;
	    akkp1 = a[k + (k - 1) * a_dim1] / t;
328
	    d__ = t * (ak * akp1 - 1.);
329 330 331
	    a[k - 1 + (k - 1) * a_dim1] = akp1 / d__;
	    a[k + k * a_dim1] = ak / d__;
	    a[k + (k - 1) * a_dim1] = -akkp1 / d__;
332 333 334 335 336

/*           Compute columns K-1 and K of the inverse. */

	    if (k < *n) {
		i__1 = *n - k;
337
		dcopy_(&i__1, &a[k + 1 + k * a_dim1], &c__1, &work[1], &c__1);
338
		i__1 = *n - k;
339 340 341
		dsymv_(uplo, &i__1, &c_b11, &a[k + 1 + (k + 1) * a_dim1], lda, 
			 &work[1], &c__1, &c_b13, &a[k + 1 + k * a_dim1], &
			c__1);
342
		i__1 = *n - k;
343 344
		a[k + k * a_dim1] -= ddot_(&i__1, &work[1], &c__1, &a[k + 1 + 
			k * a_dim1], &c__1);
345
		i__1 = *n - k;
346 347
		a[k + (k - 1) * a_dim1] -= ddot_(&i__1, &a[k + 1 + k * a_dim1]
, &c__1, &a[k + 1 + (k - 1) * a_dim1], &c__1);
348
		i__1 = *n - k;
349 350
		dcopy_(&i__1, &a[k + 1 + (k - 1) * a_dim1], &c__1, &work[1], &
			c__1);
351
		i__1 = *n - k;
352 353 354
		dsymv_(uplo, &i__1, &c_b11, &a[k + 1 + (k + 1) * a_dim1], lda, 
			 &work[1], &c__1, &c_b13, &a[k + 1 + (k - 1) * a_dim1]
, &c__1);
355
		i__1 = *n - k;
356 357
		a[k - 1 + (k - 1) * a_dim1] -= ddot_(&i__1, &work[1], &c__1, &
			a[k + 1 + (k - 1) * a_dim1], &c__1);
358 359 360 361 362 363 364
	    }
	    kstep = 2;
	}

	kp = (i__1 = ipiv[k], abs(i__1));
	if (kp != k) {

365 366
/*           Interchange rows and columns K and KP in the trailing */
/*           submatrix A(k-1:n,k-1:n) */
367 368 369

	    if (kp < *n) {
		i__1 = *n - kp;
370 371
		dswap_(&i__1, &a[kp + 1 + k * a_dim1], &c__1, &a[kp + 1 + kp *
			 a_dim1], &c__1);
372 373
	    }
	    i__1 = kp - k - 1;
374 375 376 377 378
	    dswap_(&i__1, &a[k + 1 + k * a_dim1], &c__1, &a[kp + (k + 1) * 
		    a_dim1], lda);
	    temp = a[k + k * a_dim1];
	    a[k + k * a_dim1] = a[kp + kp * a_dim1];
	    a[kp + kp * a_dim1] = temp;
379
	    if (kstep == 2) {
380 381 382
		temp = a[k + (k - 1) * a_dim1];
		a[k + (k - 1) * a_dim1] = a[kp + (k - 1) * a_dim1];
		a[kp + (k - 1) * a_dim1] = temp;
383 384 385 386 387 388 389 390 391 392 393 394 395 396
	    }
	}

	k -= kstep;
	goto L50;
L60:
	;
    }

    return 0;

/*     End of DSYTRI */

} /* dsytri_ */