```001:       SUBROUTINE SSYTD2( UPLO, N, A, LDA, 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, LDA, N
010: *     ..
011: *     .. Array Arguments ..
012:       REAL               A( LDA, * ), D( * ), E( * ), TAU( * )
013: *     ..
014: *
015: *  Purpose
016: *  =======
017: *
018: *  SSYTD2 reduces a real symmetric matrix A to symmetric tridiagonal
019: *  form T by an orthogonal similarity transformation: Q' * A * Q = T.
020: *
021: *  Arguments
022: *  =========
023: *
024: *  UPLO    (input) CHARACTER*1
025: *          Specifies whether the upper or lower triangular part of the
026: *          symmetric matrix A is stored:
027: *          = 'U':  Upper triangular
028: *          = 'L':  Lower triangular
029: *
030: *  N       (input) INTEGER
031: *          The order of the matrix A.  N >= 0.
032: *
033: *  A       (input/output) REAL array, dimension (LDA,N)
034: *          On entry, the symmetric matrix A.  If UPLO = 'U', the leading
035: *          n-by-n upper triangular part of A contains the upper
036: *          triangular part of the matrix A, and the strictly lower
037: *          triangular part of A is not referenced.  If UPLO = 'L', the
038: *          leading n-by-n lower triangular part of A contains the lower
039: *          triangular part of the matrix A, and the strictly upper
040: *          triangular part of A is not referenced.
041: *          On exit, if UPLO = 'U', the diagonal and first superdiagonal
042: *          of A are overwritten by the corresponding elements of the
043: *          tridiagonal matrix T, and the elements above the first
044: *          superdiagonal, with the array TAU, represent the orthogonal
045: *          matrix Q as a product of elementary reflectors; if UPLO
046: *          = 'L', the diagonal and first subdiagonal of A are over-
047: *          written by the corresponding elements of the tridiagonal
048: *          matrix T, and the elements below the first subdiagonal, with
049: *          the array TAU, represent the orthogonal matrix Q as a product
050: *          of elementary reflectors. See Further Details.
051: *
052: *  LDA     (input) INTEGER
053: *          The leading dimension of the array A.  LDA >= max(1,N).
054: *
055: *  D       (output) REAL array, dimension (N)
056: *          The diagonal elements of the tridiagonal matrix T:
057: *          D(i) = A(i,i).
058: *
059: *  E       (output) REAL array, dimension (N-1)
060: *          The off-diagonal elements of the tridiagonal matrix T:
061: *          E(i) = A(i,i+1) if UPLO = 'U', E(i) = A(i+1,i) if UPLO = 'L'.
062: *
063: *  TAU     (output) REAL array, dimension (N-1)
064: *          The scalar factors of the elementary reflectors (see Further
065: *          Details).
066: *
067: *  INFO    (output) INTEGER
068: *          = 0:  successful exit
069: *          < 0:  if INFO = -i, the i-th argument had an illegal value.
070: *
071: *  Further Details
072: *  ===============
073: *
074: *  If UPLO = 'U', the matrix Q is represented as a product of elementary
075: *  reflectors
076: *
077: *     Q = H(n-1) . . . H(2) H(1).
078: *
079: *  Each H(i) has the form
080: *
081: *     H(i) = I - tau * v * v'
082: *
083: *  where tau is a real scalar, and v is a real vector with
084: *  v(i+1:n) = 0 and v(i) = 1; v(1:i-1) is stored on exit in
085: *  A(1:i-1,i+1), and tau in TAU(i).
086: *
087: *  If UPLO = 'L', the matrix Q is represented as a product of elementary
088: *  reflectors
089: *
090: *     Q = H(1) H(2) . . . H(n-1).
091: *
092: *  Each H(i) has the form
093: *
094: *     H(i) = I - tau * v * v'
095: *
096: *  where tau is a real scalar, and v is a real vector with
097: *  v(1:i) = 0 and v(i+1) = 1; v(i+2:n) is stored on exit in A(i+2:n,i),
098: *  and tau in TAU(i).
099: *
100: *  The contents of A on exit are illustrated by the following examples
101: *  with n = 5:
102: *
103: *  if UPLO = 'U':                       if UPLO = 'L':
104: *
105: *    (  d   e   v2  v3  v4 )              (  d                  )
106: *    (      d   e   v3  v4 )              (  e   d              )
107: *    (          d   e   v4 )              (  v1  e   d          )
108: *    (              d   e  )              (  v1  v2  e   d      )
109: *    (                  d  )              (  v1  v2  v3  e   d  )
110: *
111: *  where d and e denote diagonal and off-diagonal elements of T, and vi
112: *  denotes an element of the vector defining H(i).
113: *
114: *  =====================================================================
115: *
116: *     .. Parameters ..
117:       REAL               ONE, ZERO, HALF
118:       PARAMETER          ( ONE = 1.0, ZERO = 0.0, HALF = 1.0 / 2.0 )
119: *     ..
120: *     .. Local Scalars ..
121:       LOGICAL            UPPER
122:       INTEGER            I
123:       REAL               ALPHA, TAUI
124: *     ..
125: *     .. External Subroutines ..
126:       EXTERNAL           SAXPY, SLARFG, SSYMV, SSYR2, XERBLA
127: *     ..
128: *     .. External Functions ..
129:       LOGICAL            LSAME
130:       REAL               SDOT
131:       EXTERNAL           LSAME, SDOT
132: *     ..
133: *     .. Intrinsic Functions ..
134:       INTRINSIC          MAX, MIN
135: *     ..
136: *     .. Executable Statements ..
137: *
138: *     Test the input parameters
139: *
140:       INFO = 0
141:       UPPER = LSAME( UPLO, 'U' )
142:       IF( .NOT.UPPER .AND. .NOT.LSAME( UPLO, 'L' ) ) THEN
143:          INFO = -1
144:       ELSE IF( N.LT.0 ) THEN
145:          INFO = -2
146:       ELSE IF( LDA.LT.MAX( 1, N ) ) THEN
147:          INFO = -4
148:       END IF
149:       IF( INFO.NE.0 ) THEN
150:          CALL XERBLA( 'SSYTD2', -INFO )
151:          RETURN
152:       END IF
153: *
154: *     Quick return if possible
155: *
156:       IF( N.LE.0 )
157:      \$   RETURN
158: *
159:       IF( UPPER ) THEN
160: *
161: *        Reduce the upper triangle of A
162: *
163:          DO 10 I = N - 1, 1, -1
164: *
165: *           Generate elementary reflector H(i) = I - tau * v * v'
166: *           to annihilate A(1:i-1,i+1)
167: *
168:             CALL SLARFG( I, A( I, I+1 ), A( 1, I+1 ), 1, TAUI )
169:             E( I ) = A( I, I+1 )
170: *
171:             IF( TAUI.NE.ZERO ) THEN
172: *
173: *              Apply H(i) from both sides to A(1:i,1:i)
174: *
175:                A( I, I+1 ) = ONE
176: *
177: *              Compute  x := tau * A * v  storing x in TAU(1:i)
178: *
179:                CALL SSYMV( UPLO, I, TAUI, A, LDA, A( 1, I+1 ), 1, ZERO,
180:      \$                     TAU, 1 )
181: *
182: *              Compute  w := x - 1/2 * tau * (x'*v) * v
183: *
184:                ALPHA = -HALF*TAUI*SDOT( I, TAU, 1, A( 1, I+1 ), 1 )
185:                CALL SAXPY( I, ALPHA, A( 1, I+1 ), 1, TAU, 1 )
186: *
187: *              Apply the transformation as a rank-2 update:
188: *                 A := A - v * w' - w * v'
189: *
190:                CALL SSYR2( UPLO, I, -ONE, A( 1, I+1 ), 1, TAU, 1, A,
191:      \$                     LDA )
192: *
193:                A( I, I+1 ) = E( I )
194:             END IF
195:             D( I+1 ) = A( I+1, I+1 )
196:             TAU( I ) = TAUI
197:    10    CONTINUE
198:          D( 1 ) = A( 1, 1 )
199:       ELSE
200: *
201: *        Reduce the lower triangle of A
202: *
203:          DO 20 I = 1, N - 1
204: *
205: *           Generate elementary reflector H(i) = I - tau * v * v'
206: *           to annihilate A(i+2:n,i)
207: *
208:             CALL SLARFG( N-I, A( I+1, I ), A( MIN( I+2, N ), I ), 1,
209:      \$                   TAUI )
210:             E( I ) = A( I+1, I )
211: *
212:             IF( TAUI.NE.ZERO ) THEN
213: *
214: *              Apply H(i) from both sides to A(i+1:n,i+1:n)
215: *
216:                A( I+1, I ) = ONE
217: *
218: *              Compute  x := tau * A * v  storing y in TAU(i:n-1)
219: *
220:                CALL SSYMV( UPLO, N-I, TAUI, A( I+1, I+1 ), LDA,
221:      \$                     A( I+1, I ), 1, ZERO, TAU( I ), 1 )
222: *
223: *              Compute  w := x - 1/2 * tau * (x'*v) * v
224: *
225:                ALPHA = -HALF*TAUI*SDOT( N-I, TAU( I ), 1, A( I+1, I ),
226:      \$                 1 )
227:                CALL SAXPY( N-I, ALPHA, A( I+1, I ), 1, TAU( I ), 1 )
228: *
229: *              Apply the transformation as a rank-2 update:
230: *                 A := A - v * w' - w * v'
231: *
232:                CALL SSYR2( UPLO, N-I, -ONE, A( I+1, I ), 1, TAU( I ), 1,
233:      \$                     A( I+1, I+1 ), LDA )
234: *
235:                A( I+1, I ) = E( I )
236:             END IF
237:             D( I ) = A( I, I )
238:             TAU( I ) = TAUI
239:    20    CONTINUE
240:          D( N ) = A( N, N )
241:       END IF
242: *
243:       RETURN
244: *
245: *     End of SSYTD2
246: *
247:       END
248: ```