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