001:       SUBROUTINE SLATDF( IJOB, N, Z, LDZ, RHS, RDSUM, RDSCAL, IPIV,
002:      $                   JPIV )
003: *
004: *  -- LAPACK auxiliary routine (version 3.2) --
005: *  -- LAPACK is a software package provided by Univ. of Tennessee,    --
006: *  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
007: *     November 2006
008: *
009: *     .. Scalar Arguments ..
010:       INTEGER            IJOB, LDZ, N
011:       REAL               RDSCAL, RDSUM
012: *     ..
013: *     .. Array Arguments ..
014:       INTEGER            IPIV( * ), JPIV( * )
015:       REAL               RHS( * ), Z( LDZ, * )
016: *     ..
017: *
018: *  Purpose
019: *  =======
020: *
021: *  SLATDF uses the LU factorization of the n-by-n matrix Z computed by
022: *  SGETC2 and computes a contribution to the reciprocal Dif-estimate
023: *  by solving Z * x = b for x, and choosing the r.h.s. b such that
024: *  the norm of x is as large as possible. On entry RHS = b holds the
025: *  contribution from earlier solved sub-systems, and on return RHS = x.
026: *
027: *  The factorization of Z returned by SGETC2 has the form Z = P*L*U*Q,
028: *  where P and Q are permutation matrices. L is lower triangular with
029: *  unit diagonal elements and U is upper triangular.
030: *
031: *  Arguments
032: *  =========
033: *
034: *  IJOB    (input) INTEGER
035: *          IJOB = 2: First compute an approximative null-vector e
036: *              of Z using SGECON, e is normalized and solve for
037: *              Zx = +-e - f with the sign giving the greater value
038: *              of 2-norm(x). About 5 times as expensive as Default.
039: *          IJOB .ne. 2: Local look ahead strategy where all entries of
040: *              the r.h.s. b is choosen as either +1 or -1 (Default).
041: *
042: *  N       (input) INTEGER
043: *          The number of columns of the matrix Z.
044: *
045: *  Z       (input) REAL array, dimension (LDZ, N)
046: *          On entry, the LU part of the factorization of the n-by-n
047: *          matrix Z computed by SGETC2:  Z = P * L * U * Q
048: *
049: *  LDZ     (input) INTEGER
050: *          The leading dimension of the array Z.  LDA >= max(1, N).
051: *
052: *  RHS     (input/output) REAL array, dimension N.
053: *          On entry, RHS contains contributions from other subsystems.
054: *          On exit, RHS contains the solution of the subsystem with
055: *          entries acoording to the value of IJOB (see above).
056: *
057: *  RDSUM   (input/output) REAL
058: *          On entry, the sum of squares of computed contributions to
059: *          the Dif-estimate under computation by STGSYL, where the
060: *          scaling factor RDSCAL (see below) has been factored out.
061: *          On exit, the corresponding sum of squares updated with the
062: *          contributions from the current sub-system.
063: *          If TRANS = 'T' RDSUM is not touched.
064: *          NOTE: RDSUM only makes sense when STGSY2 is called by STGSYL.
065: *
066: *  RDSCAL  (input/output) REAL
067: *          On entry, scaling factor used to prevent overflow in RDSUM.
068: *          On exit, RDSCAL is updated w.r.t. the current contributions
069: *          in RDSUM.
070: *          If TRANS = 'T', RDSCAL is not touched.
071: *          NOTE: RDSCAL only makes sense when STGSY2 is called by
072: *                STGSYL.
073: *
074: *  IPIV    (input) INTEGER array, dimension (N).
075: *          The pivot indices; for 1 <= i <= N, row i of the
076: *          matrix has been interchanged with row IPIV(i).
077: *
078: *  JPIV    (input) INTEGER array, dimension (N).
079: *          The pivot indices; for 1 <= j <= N, column j of the
080: *          matrix has been interchanged with column JPIV(j).
081: *
082: *  Further Details
083: *  ===============
084: *
085: *  Based on contributions by
086: *     Bo Kagstrom and Peter Poromaa, Department of Computing Science,
087: *     Umea University, S-901 87 Umea, Sweden.
088: *
089: *  This routine is a further developed implementation of algorithm
090: *  BSOLVE in [1] using complete pivoting in the LU factorization.
091: *
092: *  [1] Bo Kagstrom and Lars Westin,
093: *      Generalized Schur Methods with Condition Estimators for
094: *      Solving the Generalized Sylvester Equation, IEEE Transactions
095: *      on Automatic Control, Vol. 34, No. 7, July 1989, pp 745-751.
096: *
097: *  [2] Peter Poromaa,
098: *      On Efficient and Robust Estimators for the Separation
099: *      between two Regular Matrix Pairs with Applications in
100: *      Condition Estimation. Report IMINF-95.05, Departement of
101: *      Computing Science, Umea University, S-901 87 Umea, Sweden, 1995.
102: *
103: *  =====================================================================
104: *
105: *     .. Parameters ..
106:       INTEGER            MAXDIM
107:       PARAMETER          ( MAXDIM = 8 )
108:       REAL               ZERO, ONE
109:       PARAMETER          ( ZERO = 0.0E+0, ONE = 1.0E+0 )
110: *     ..
111: *     .. Local Scalars ..
112:       INTEGER            I, INFO, J, K
113:       REAL               BM, BP, PMONE, SMINU, SPLUS, TEMP
114: *     ..
115: *     .. Local Arrays ..
116:       INTEGER            IWORK( MAXDIM )
117:       REAL               WORK( 4*MAXDIM ), XM( MAXDIM ), XP( MAXDIM )
118: *     ..
119: *     .. External Subroutines ..
120:       EXTERNAL           SAXPY, SCOPY, SGECON, SGESC2, SLASSQ, SLASWP,
121:      $                   SSCAL
122: *     ..
123: *     .. External Functions ..
124:       REAL               SASUM, SDOT
125:       EXTERNAL           SASUM, SDOT
126: *     ..
127: *     .. Intrinsic Functions ..
128:       INTRINSIC          ABS, SQRT
129: *     ..
130: *     .. Executable Statements ..
131: *
132:       IF( IJOB.NE.2 ) THEN
133: *
134: *        Apply permutations IPIV to RHS
135: *
136:          CALL SLASWP( 1, RHS, LDZ, 1, N-1, IPIV, 1 )
137: *
138: *        Solve for L-part choosing RHS either to +1 or -1.
139: *
140:          PMONE = -ONE
141: *
142:          DO 10 J = 1, N - 1
143:             BP = RHS( J ) + ONE
144:             BM = RHS( J ) - ONE
145:             SPLUS = ONE
146: *
147: *           Look-ahead for L-part RHS(1:N-1) = + or -1, SPLUS and
148: *           SMIN computed more efficiently than in BSOLVE [1].
149: *
150:             SPLUS = SPLUS + SDOT( N-J, Z( J+1, J ), 1, Z( J+1, J ), 1 )
151:             SMINU = SDOT( N-J, Z( J+1, J ), 1, RHS( J+1 ), 1 )
152:             SPLUS = SPLUS*RHS( J )
153:             IF( SPLUS.GT.SMINU ) THEN
154:                RHS( J ) = BP
155:             ELSE IF( SMINU.GT.SPLUS ) THEN
156:                RHS( J ) = BM
157:             ELSE
158: *
159: *              In this case the updating sums are equal and we can
160: *              choose RHS(J) +1 or -1. The first time this happens
161: *              we choose -1, thereafter +1. This is a simple way to
162: *              get good estimates of matrices like Byers well-known
163: *              example (see [1]). (Not done in BSOLVE.)
164: *
165:                RHS( J ) = RHS( J ) + PMONE
166:                PMONE = ONE
167:             END IF
168: *
169: *           Compute the remaining r.h.s.
170: *
171:             TEMP = -RHS( J )
172:             CALL SAXPY( N-J, TEMP, Z( J+1, J ), 1, RHS( J+1 ), 1 )
173: *
174:    10    CONTINUE
175: *
176: *        Solve for U-part, look-ahead for RHS(N) = +-1. This is not done
177: *        in BSOLVE and will hopefully give us a better estimate because
178: *        any ill-conditioning of the original matrix is transfered to U
179: *        and not to L. U(N, N) is an approximation to sigma_min(LU).
180: *
181:          CALL SCOPY( N-1, RHS, 1, XP, 1 )
182:          XP( N ) = RHS( N ) + ONE
183:          RHS( N ) = RHS( N ) - ONE
184:          SPLUS = ZERO
185:          SMINU = ZERO
186:          DO 30 I = N, 1, -1
187:             TEMP = ONE / Z( I, I )
188:             XP( I ) = XP( I )*TEMP
189:             RHS( I ) = RHS( I )*TEMP
190:             DO 20 K = I + 1, N
191:                XP( I ) = XP( I ) - XP( K )*( Z( I, K )*TEMP )
192:                RHS( I ) = RHS( I ) - RHS( K )*( Z( I, K )*TEMP )
193:    20       CONTINUE
194:             SPLUS = SPLUS + ABS( XP( I ) )
195:             SMINU = SMINU + ABS( RHS( I ) )
196:    30    CONTINUE
197:          IF( SPLUS.GT.SMINU )
198:      $      CALL SCOPY( N, XP, 1, RHS, 1 )
199: *
200: *        Apply the permutations JPIV to the computed solution (RHS)
201: *
202:          CALL SLASWP( 1, RHS, LDZ, 1, N-1, JPIV, -1 )
203: *
204: *        Compute the sum of squares
205: *
206:          CALL SLASSQ( N, RHS, 1, RDSCAL, RDSUM )
207: *
208:       ELSE
209: *
210: *        IJOB = 2, Compute approximate nullvector XM of Z
211: *
212:          CALL SGECON( 'I', N, Z, LDZ, ONE, TEMP, WORK, IWORK, INFO )
213:          CALL SCOPY( N, WORK( N+1 ), 1, XM, 1 )
214: *
215: *        Compute RHS
216: *
217:          CALL SLASWP( 1, XM, LDZ, 1, N-1, IPIV, -1 )
218:          TEMP = ONE / SQRT( SDOT( N, XM, 1, XM, 1 ) )
219:          CALL SSCAL( N, TEMP, XM, 1 )
220:          CALL SCOPY( N, XM, 1, XP, 1 )
221:          CALL SAXPY( N, ONE, RHS, 1, XP, 1 )
222:          CALL SAXPY( N, -ONE, XM, 1, RHS, 1 )
223:          CALL SGESC2( N, Z, LDZ, RHS, IPIV, JPIV, TEMP )
224:          CALL SGESC2( N, Z, LDZ, XP, IPIV, JPIV, TEMP )
225:          IF( SASUM( N, XP, 1 ).GT.SASUM( N, RHS, 1 ) )
226:      $      CALL SCOPY( N, XP, 1, RHS, 1 )
227: *
228: *        Compute the sum of squares
229: *
230:          CALL SLASSQ( N, RHS, 1, RDSCAL, RDSUM )
231: *
232:       END IF
233: *
234:       RETURN
235: *
236: *     End of SLATDF
237: *
238:       END
239: