001:       SUBROUTINE DSPTRD( UPLO, N, AP, D, E, TAU, INFO )
002: *
003: *  -- LAPACK routine (version 3.2) --
004: *     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd..
005: *     November 2006
006: *
007: *     .. Scalar Arguments ..
008:       CHARACTER          UPLO
009:       INTEGER            INFO, N
010: *     ..
011: *     .. Array Arguments ..
012:       DOUBLE PRECISION   AP( * ), D( * ), E( * ), TAU( * )
013: *     ..
014: *
015: *  Purpose
016: *  =======
017: *
018: *  DSPTRD reduces a real symmetric matrix A stored in packed form to
019: *  symmetric tridiagonal form T by an orthogonal similarity
020: *  transformation: Q**T * A * Q = T.
021: *
022: *  Arguments
023: *  =========
024: *
025: *  UPLO    (input) CHARACTER*1
026: *          = 'U':  Upper triangle of A is stored;
027: *          = 'L':  Lower triangle of A is stored.
028: *
029: *  N       (input) INTEGER
030: *          The order of the matrix A.  N >= 0.
031: *
032: *  AP      (input/output) DOUBLE PRECISION array, dimension (N*(N+1)/2)
033: *          On entry, the upper or lower triangle of the symmetric matrix
034: *          A, packed columnwise in a linear array.  The j-th column of A
035: *          is stored in the array AP as follows:
036: *          if UPLO = 'U', AP(i + (j-1)*j/2) = A(i,j) for 1<=i<=j;
037: *          if UPLO = 'L', AP(i + (j-1)*(2*n-j)/2) = A(i,j) for j<=i<=n.
038: *          On exit, if UPLO = 'U', the diagonal and first superdiagonal
039: *          of A are overwritten by the corresponding elements of the
040: *          tridiagonal matrix T, and the elements above the first
041: *          superdiagonal, with the array TAU, represent the orthogonal
042: *          matrix Q as a product of elementary reflectors; if UPLO
043: *          = 'L', the diagonal and first subdiagonal of A are over-
044: *          written by the corresponding elements of the tridiagonal
045: *          matrix T, and the elements below the first subdiagonal, with
046: *          the array TAU, represent the orthogonal matrix Q as a product
047: *          of elementary reflectors. See Further Details.
048: *
049: *  D       (output) DOUBLE PRECISION array, dimension (N)
050: *          The diagonal elements of the tridiagonal matrix T:
051: *          D(i) = A(i,i).
052: *
053: *  E       (output) DOUBLE PRECISION array, dimension (N-1)
054: *          The off-diagonal elements of the tridiagonal matrix T:
055: *          E(i) = A(i,i+1) if UPLO = 'U', E(i) = A(i+1,i) if UPLO = 'L'.
056: *
057: *  TAU     (output) DOUBLE PRECISION array, dimension (N-1)
058: *          The scalar factors of the elementary reflectors (see Further
059: *          Details).
060: *
061: *  INFO    (output) INTEGER
062: *          = 0:  successful exit
063: *          < 0:  if INFO = -i, the i-th argument had an illegal value
064: *
065: *  Further Details
066: *  ===============
067: *
068: *  If UPLO = 'U', the matrix Q is represented as a product of elementary
069: *  reflectors
070: *
071: *     Q = H(n-1) . . . H(2) H(1).
072: *
073: *  Each H(i) has the form
074: *
075: *     H(i) = I - tau * v * v'
076: *
077: *  where tau is a real scalar, and v is a real vector with
078: *  v(i+1:n) = 0 and v(i) = 1; v(1:i-1) is stored on exit in AP,
079: *  overwriting A(1:i-1,i+1), and tau is stored in TAU(i).
080: *
081: *  If UPLO = 'L', the matrix Q is represented as a product of elementary
082: *  reflectors
083: *
084: *     Q = H(1) H(2) . . . H(n-1).
085: *
086: *  Each H(i) has the form
087: *
088: *     H(i) = I - tau * v * v'
089: *
090: *  where tau is a real scalar, and v is a real vector with
091: *  v(1:i) = 0 and v(i+1) = 1; v(i+2:n) is stored on exit in AP,
092: *  overwriting A(i+2:n,i), and tau is stored in TAU(i).
093: *
094: *  =====================================================================
095: *
096: *     .. Parameters ..
097:       DOUBLE PRECISION   ONE, ZERO, HALF
098:       PARAMETER          ( ONE = 1.0D0, ZERO = 0.0D0,
099:      $                   HALF = 1.0D0 / 2.0D0 )
100: *     ..
101: *     .. Local Scalars ..
102:       LOGICAL            UPPER
103:       INTEGER            I, I1, I1I1, II
104:       DOUBLE PRECISION   ALPHA, TAUI
105: *     ..
106: *     .. External Subroutines ..
107:       EXTERNAL           DAXPY, DLARFG, DSPMV, DSPR2, XERBLA
108: *     ..
109: *     .. External Functions ..
110:       LOGICAL            LSAME
111:       DOUBLE PRECISION   DDOT
112:       EXTERNAL           LSAME, DDOT
113: *     ..
114: *     .. Executable Statements ..
115: *
116: *     Test the input parameters
117: *
118:       INFO = 0
119:       UPPER = LSAME( UPLO, 'U' )
120:       IF( .NOT.UPPER .AND. .NOT.LSAME( UPLO, 'L' ) ) THEN
121:          INFO = -1
122:       ELSE IF( N.LT.0 ) THEN
123:          INFO = -2
124:       END IF
125:       IF( INFO.NE.0 ) THEN
126:          CALL XERBLA( 'DSPTRD', -INFO )
127:          RETURN
128:       END IF
129: *
130: *     Quick return if possible
131: *
132:       IF( N.LE.0 )
133:      $   RETURN
134: *
135:       IF( UPPER ) THEN
136: *
137: *        Reduce the upper triangle of A.
138: *        I1 is the index in AP of A(1,I+1).
139: *
140:          I1 = N*( N-1 ) / 2 + 1
141:          DO 10 I = N - 1, 1, -1
142: *
143: *           Generate elementary reflector H(i) = I - tau * v * v'
144: *           to annihilate A(1:i-1,i+1)
145: *
146:             CALL DLARFG( I, AP( I1+I-1 ), AP( I1 ), 1, TAUI )
147:             E( I ) = AP( I1+I-1 )
148: *
149:             IF( TAUI.NE.ZERO ) THEN
150: *
151: *              Apply H(i) from both sides to A(1:i,1:i)
152: *
153:                AP( I1+I-1 ) = ONE
154: *
155: *              Compute  y := tau * A * v  storing y in TAU(1:i)
156: *
157:                CALL DSPMV( UPLO, I, TAUI, AP, AP( I1 ), 1, ZERO, TAU,
158:      $                     1 )
159: *
160: *              Compute  w := y - 1/2 * tau * (y'*v) * v
161: *
162:                ALPHA = -HALF*TAUI*DDOT( I, TAU, 1, AP( I1 ), 1 )
163:                CALL DAXPY( I, ALPHA, AP( I1 ), 1, TAU, 1 )
164: *
165: *              Apply the transformation as a rank-2 update:
166: *                 A := A - v * w' - w * v'
167: *
168:                CALL DSPR2( UPLO, I, -ONE, AP( I1 ), 1, TAU, 1, AP )
169: *
170:                AP( I1+I-1 ) = E( I )
171:             END IF
172:             D( I+1 ) = AP( I1+I )
173:             TAU( I ) = TAUI
174:             I1 = I1 - I
175:    10    CONTINUE
176:          D( 1 ) = AP( 1 )
177:       ELSE
178: *
179: *        Reduce the lower triangle of A. II is the index in AP of
180: *        A(i,i) and I1I1 is the index of A(i+1,i+1).
181: *
182:          II = 1
183:          DO 20 I = 1, N - 1
184:             I1I1 = II + N - I + 1
185: *
186: *           Generate elementary reflector H(i) = I - tau * v * v'
187: *           to annihilate A(i+2:n,i)
188: *
189:             CALL DLARFG( N-I, AP( II+1 ), AP( II+2 ), 1, TAUI )
190:             E( I ) = AP( II+1 )
191: *
192:             IF( TAUI.NE.ZERO ) THEN
193: *
194: *              Apply H(i) from both sides to A(i+1:n,i+1:n)
195: *
196:                AP( II+1 ) = ONE
197: *
198: *              Compute  y := tau * A * v  storing y in TAU(i:n-1)
199: *
200:                CALL DSPMV( UPLO, N-I, TAUI, AP( I1I1 ), AP( II+1 ), 1,
201:      $                     ZERO, TAU( I ), 1 )
202: *
203: *              Compute  w := y - 1/2 * tau * (y'*v) * v
204: *
205:                ALPHA = -HALF*TAUI*DDOT( N-I, TAU( I ), 1, AP( II+1 ),
206:      $                 1 )
207:                CALL DAXPY( N-I, ALPHA, AP( II+1 ), 1, TAU( I ), 1 )
208: *
209: *              Apply the transformation as a rank-2 update:
210: *                 A := A - v * w' - w * v'
211: *
212:                CALL DSPR2( UPLO, N-I, -ONE, AP( II+1 ), 1, TAU( I ), 1,
213:      $                     AP( I1I1 ) )
214: *
215:                AP( II+1 ) = E( I )
216:             END IF
217:             D( I ) = AP( II )
218:             TAU( I ) = TAUI
219:             II = I1I1
220:    20    CONTINUE
221:          D( N ) = AP( II )
222:       END IF
223: *
224:       RETURN
225: *
226: *     End of DSPTRD
227: *
228:       END
229: