```001:       SUBROUTINE SSPTRD( UPLO, N, AP, D, E, TAU, INFO )
002: *
003: *  -- LAPACK routine (version 3.2) --
004: *  -- LAPACK is a software package provided by Univ. of Tennessee,    --
005: *  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
006: *     November 2006
007: *
008: *     .. Scalar Arguments ..
009:       CHARACTER          UPLO
010:       INTEGER            INFO, N
011: *     ..
012: *     .. Array Arguments ..
013:       REAL               AP( * ), D( * ), E( * ), TAU( * )
014: *     ..
015: *
016: *  Purpose
017: *  =======
018: *
019: *  SSPTRD reduces a real symmetric matrix A stored in packed form to
020: *  symmetric tridiagonal form T by an orthogonal similarity
021: *  transformation: Q**T * A * Q = T.
022: *
023: *  Arguments
024: *  =========
025: *
026: *  UPLO    (input) CHARACTER*1
027: *          = 'U':  Upper triangle of A is stored;
028: *          = 'L':  Lower triangle of A is stored.
029: *
030: *  N       (input) INTEGER
031: *          The order of the matrix A.  N >= 0.
032: *
033: *  AP      (input/output) REAL array, dimension (N*(N+1)/2)
034: *          On entry, the upper or lower triangle of the symmetric matrix
035: *          A, packed columnwise in a linear array.  The j-th column of A
036: *          is stored in the array AP as follows:
037: *          if UPLO = 'U', AP(i + (j-1)*j/2) = A(i,j) for 1<=i<=j;
038: *          if UPLO = 'L', AP(i + (j-1)*(2*n-j)/2) = A(i,j) for j<=i<=n.
039: *          On exit, if UPLO = 'U', the diagonal and first superdiagonal
040: *          of A are overwritten by the corresponding elements of the
041: *          tridiagonal matrix T, and the elements above the first
042: *          superdiagonal, with the array TAU, represent the orthogonal
043: *          matrix Q as a product of elementary reflectors; if UPLO
044: *          = 'L', the diagonal and first subdiagonal of A are over-
045: *          written by the corresponding elements of the tridiagonal
046: *          matrix T, and the elements below the first subdiagonal, with
047: *          the array TAU, represent the orthogonal matrix Q as a product
048: *          of elementary reflectors. See Further Details.
049: *
050: *  D       (output) REAL array, dimension (N)
051: *          The diagonal elements of the tridiagonal matrix T:
052: *          D(i) = A(i,i).
053: *
054: *  E       (output) REAL array, dimension (N-1)
055: *          The off-diagonal elements of the tridiagonal matrix T:
056: *          E(i) = A(i,i+1) if UPLO = 'U', E(i) = A(i+1,i) if UPLO = 'L'.
057: *
058: *  TAU     (output) REAL array, dimension (N-1)
059: *          The scalar factors of the elementary reflectors (see Further
060: *          Details).
061: *
062: *  INFO    (output) INTEGER
063: *          = 0:  successful exit
064: *          < 0:  if INFO = -i, the i-th argument had an illegal value
065: *
066: *  Further Details
067: *  ===============
068: *
069: *  If UPLO = 'U', the matrix Q is represented as a product of elementary
070: *  reflectors
071: *
072: *     Q = H(n-1) . . . H(2) H(1).
073: *
074: *  Each H(i) has the form
075: *
076: *     H(i) = I - tau * v * v'
077: *
078: *  where tau is a real scalar, and v is a real vector with
079: *  v(i+1:n) = 0 and v(i) = 1; v(1:i-1) is stored on exit in AP,
080: *  overwriting A(1:i-1,i+1), and tau is stored in TAU(i).
081: *
082: *  If UPLO = 'L', the matrix Q is represented as a product of elementary
083: *  reflectors
084: *
085: *     Q = H(1) H(2) . . . H(n-1).
086: *
087: *  Each H(i) has the form
088: *
089: *     H(i) = I - tau * v * v'
090: *
091: *  where tau is a real scalar, and v is a real vector with
092: *  v(1:i) = 0 and v(i+1) = 1; v(i+2:n) is stored on exit in AP,
093: *  overwriting A(i+2:n,i), and tau is stored in TAU(i).
094: *
095: *  =====================================================================
096: *
097: *     .. Parameters ..
098:       REAL               ONE, ZERO, HALF
099:       PARAMETER          ( ONE = 1.0, ZERO = 0.0, HALF = 1.0 / 2.0 )
100: *     ..
101: *     .. Local Scalars ..
102:       LOGICAL            UPPER
103:       INTEGER            I, I1, I1I1, II
104:       REAL               ALPHA, TAUI
105: *     ..
106: *     .. External Subroutines ..
107:       EXTERNAL           SAXPY, SLARFG, SSPMV, SSPR2, XERBLA
108: *     ..
109: *     .. External Functions ..
110:       LOGICAL            LSAME
111:       REAL               SDOT
112:       EXTERNAL           LSAME, SDOT
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( 'SSPTRD', -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 SLARFG( 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 SSPMV( 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*SDOT( I, TAU, 1, AP( I1 ), 1 )
163:                CALL SAXPY( 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 SSPR2( 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 SLARFG( 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 SSPMV( 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*SDOT( N-I, TAU( I ), 1, AP( II+1 ),
206:      \$                 1 )
207:                CALL SAXPY( 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 SSPR2( 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 SSPTRD
227: *
228:       END
229: ```