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