001:       SUBROUTINE SSPTRD( 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:       REAL               AP( * ), D( * ), E( * ), TAU( * )
013: *     ..
014: *
015: *  Purpose
016: *  =======
017: *
018: *  SSPTRD 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) REAL 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) REAL array, dimension (N)
050: *          The diagonal elements of the tridiagonal matrix T:
051: *          D(i) = A(i,i).
052: *
053: *  E       (output) REAL 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) REAL 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:       REAL               ONE, ZERO, HALF
098:       PARAMETER          ( ONE = 1.0, ZERO = 0.0, HALF = 1.0 / 2.0 )
099: *     ..
100: *     .. Local Scalars ..
101:       LOGICAL            UPPER
102:       INTEGER            I, I1, I1I1, II
103:       REAL               ALPHA, TAUI
104: *     ..
105: *     .. External Subroutines ..
106:       EXTERNAL           SAXPY, SLARFG, SSPMV, SSPR2, XERBLA
107: *     ..
108: *     .. External Functions ..
109:       LOGICAL            LSAME
110:       REAL               SDOT
111:       EXTERNAL           LSAME, SDOT
112: *     ..
113: *     .. Executable Statements ..
114: *
115: *     Test the input parameters
116: *
117:       INFO = 0
118:       UPPER = LSAME( UPLO, 'U' )
119:       IF( .NOT.UPPER .AND. .NOT.LSAME( UPLO, 'L' ) ) THEN
120:          INFO = -1
121:       ELSE IF( N.LT.0 ) THEN
122:          INFO = -2
123:       END IF
124:       IF( INFO.NE.0 ) THEN
125:          CALL XERBLA( 'SSPTRD', -INFO )
126:          RETURN
127:       END IF
128: *
129: *     Quick return if possible
130: *
131:       IF( N.LE.0 )
132:      $   RETURN
133: *
134:       IF( UPPER ) THEN
135: *
136: *        Reduce the upper triangle of A.
137: *        I1 is the index in AP of A(1,I+1).
138: *
139:          I1 = N*( N-1 ) / 2 + 1
140:          DO 10 I = N - 1, 1, -1
141: *
142: *           Generate elementary reflector H(i) = I - tau * v * v'
143: *           to annihilate A(1:i-1,i+1)
144: *
145:             CALL SLARFG( I, AP( I1+I-1 ), AP( I1 ), 1, TAUI )
146:             E( I ) = AP( I1+I-1 )
147: *
148:             IF( TAUI.NE.ZERO ) THEN
149: *
150: *              Apply H(i) from both sides to A(1:i,1:i)
151: *
152:                AP( I1+I-1 ) = ONE
153: *
154: *              Compute  y := tau * A * v  storing y in TAU(1:i)
155: *
156:                CALL SSPMV( UPLO, I, TAUI, AP, AP( I1 ), 1, ZERO, TAU,
157:      $                     1 )
158: *
159: *              Compute  w := y - 1/2 * tau * (y'*v) * v
160: *
161:                ALPHA = -HALF*TAUI*SDOT( I, TAU, 1, AP( I1 ), 1 )
162:                CALL SAXPY( I, ALPHA, AP( I1 ), 1, TAU, 1 )
163: *
164: *              Apply the transformation as a rank-2 update:
165: *                 A := A - v * w' - w * v'
166: *
167:                CALL SSPR2( UPLO, I, -ONE, AP( I1 ), 1, TAU, 1, AP )
168: *
169:                AP( I1+I-1 ) = E( I )
170:             END IF
171:             D( I+1 ) = AP( I1+I )
172:             TAU( I ) = TAUI
173:             I1 = I1 - I
174:    10    CONTINUE
175:          D( 1 ) = AP( 1 )
176:       ELSE
177: *
178: *        Reduce the lower triangle of A. II is the index in AP of
179: *        A(i,i) and I1I1 is the index of A(i+1,i+1).
180: *
181:          II = 1
182:          DO 20 I = 1, N - 1
183:             I1I1 = II + N - I + 1
184: *
185: *           Generate elementary reflector H(i) = I - tau * v * v'
186: *           to annihilate A(i+2:n,i)
187: *
188:             CALL SLARFG( N-I, AP( II+1 ), AP( II+2 ), 1, TAUI )
189:             E( I ) = AP( II+1 )
190: *
191:             IF( TAUI.NE.ZERO ) THEN
192: *
193: *              Apply H(i) from both sides to A(i+1:n,i+1:n)
194: *
195:                AP( II+1 ) = ONE
196: *
197: *              Compute  y := tau * A * v  storing y in TAU(i:n-1)
198: *
199:                CALL SSPMV( UPLO, N-I, TAUI, AP( I1I1 ), AP( II+1 ), 1,
200:      $                     ZERO, TAU( I ), 1 )
201: *
202: *              Compute  w := y - 1/2 * tau * (y'*v) * v
203: *
204:                ALPHA = -HALF*TAUI*SDOT( N-I, TAU( I ), 1, AP( II+1 ),
205:      $                 1 )
206:                CALL SAXPY( N-I, ALPHA, AP( II+1 ), 1, TAU( I ), 1 )
207: *
208: *              Apply the transformation as a rank-2 update:
209: *                 A := A - v * w' - w * v'
210: *
211:                CALL SSPR2( UPLO, N-I, -ONE, AP( II+1 ), 1, TAU( I ), 1,
212:      $                     AP( I1I1 ) )
213: *
214:                AP( II+1 ) = E( I )
215:             END IF
216:             D( I ) = AP( II )
217:             TAU( I ) = TAUI
218:             II = I1I1
219:    20    CONTINUE
220:          D( N ) = AP( II )
221:       END IF
222: *
223:       RETURN
224: *
225: *     End of SSPTRD
226: *
227:       END
228: