001:       SUBROUTINE CLATDF( 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:       COMPLEX            RHS( * ), Z( LDZ, * )
015: *     ..
016: *
017: *  Purpose
018: *  =======
019: *
020: *  CLATDF computes the contribution to the reciprocal Dif-estimate
021: *  by solving for x in Z * x = b, where b is chosen such that the norm
022: *  of x is as large as possible. It is assumed that LU decomposition
023: *  of Z has been computed by CGETC2. On entry RHS = f holds the
024: *  contribution from earlier solved sub-systems, and on return RHS = x.
025: *
026: *  The factorization of Z returned by CGETC2 has the form
027: *  Z = P * L * U * Q, where P and Q are permutation matrices. L is lower
028: *  triangular with 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 CGECON, e is normalized and solve for
036: *              Zx = +-e - f with the sign giving the greater value of
037: *              2-norm(x).  About 5 times as expensive as Default.
038: *          IJOB .ne. 2: Local look ahead strategy where
039: *              all entries of the r.h.s. b is choosen as either +1 or
040: *              -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 CGETC2:  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 according 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 CTGSYL, 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 CTGSY2 is called by CTGSYL.
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 CTGSY2 is called by
072: *          CTGSYL.
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 UMINF-95.05, Department of
101: *         Computing Science, Umea University, S-901 87 Umea, Sweden,
102: *         1995.
103: *
104: *  =====================================================================
105: *
106: *     .. Parameters ..
107:       INTEGER            MAXDIM
108:       PARAMETER          ( MAXDIM = 2 )
109:       REAL               ZERO, ONE
110:       PARAMETER          ( ZERO = 0.0E+0, ONE = 1.0E+0 )
111:       COMPLEX            CONE
112:       PARAMETER          ( CONE = ( 1.0E+0, 0.0E+0 ) )
113: *     ..
114: *     .. Local Scalars ..
115:       INTEGER            I, INFO, J, K
116:       REAL               RTEMP, SCALE, SMINU, SPLUS
117:       COMPLEX            BM, BP, PMONE, TEMP
118: *     ..
119: *     .. Local Arrays ..
120:       REAL               RWORK( MAXDIM )
121:       COMPLEX            WORK( 4*MAXDIM ), XM( MAXDIM ), XP( MAXDIM )
122: *     ..
123: *     .. External Subroutines ..
124:       EXTERNAL           CAXPY, CCOPY, CGECON, CGESC2, CLASSQ, CLASWP,
125:      $                   CSCAL
126: *     ..
127: *     .. External Functions ..
128:       REAL               SCASUM
129:       COMPLEX            CDOTC
130:       EXTERNAL           SCASUM, CDOTC
131: *     ..
132: *     .. Intrinsic Functions ..
133:       INTRINSIC          ABS, REAL, SQRT
134: *     ..
135: *     .. Executable Statements ..
136: *
137:       IF( IJOB.NE.2 ) THEN
138: *
139: *        Apply permutations IPIV to RHS
140: *
141:          CALL CLASWP( 1, RHS, LDZ, 1, N-1, IPIV, 1 )
142: *
143: *        Solve for L-part choosing RHS either to +1 or -1.
144: *
145:          PMONE = -CONE
146:          DO 10 J = 1, N - 1
147:             BP = RHS( J ) + CONE
148:             BM = RHS( J ) - CONE
149:             SPLUS = ONE
150: *
151: *           Lockahead for L- part RHS(1:N-1) = +-1
152: *           SPLUS and SMIN computed more efficiently than in BSOLVE[1].
153: *
154:             SPLUS = SPLUS + REAL( CDOTC( N-J, Z( J+1, J ), 1, Z( J+1,
155:      $              J ), 1 ) )
156:             SMINU = REAL( CDOTC( N-J, Z( J+1, J ), 1, RHS( J+1 ), 1 ) )
157:             SPLUS = SPLUS*REAL( RHS( J ) )
158:             IF( SPLUS.GT.SMINU ) THEN
159:                RHS( J ) = BP
160:             ELSE IF( SMINU.GT.SPLUS ) THEN
161:                RHS( J ) = BM
162:             ELSE
163: *
164: *              In this case the updating sums are equal and we can
165: *              choose RHS(J) +1 or -1. The first time this happens we
166: *              choose -1, thereafter +1. This is a simple way to get
167: *              good estimates of matrices like Byers well-known example
168: *              (see [1]). (Not done in BSOLVE.)
169: *
170:                RHS( J ) = RHS( J ) + PMONE
171:                PMONE = CONE
172:             END IF
173: *
174: *           Compute the remaining r.h.s.
175: *
176:             TEMP = -RHS( J )
177:             CALL CAXPY( N-J, TEMP, Z( J+1, J ), 1, RHS( J+1 ), 1 )
178:    10    CONTINUE
179: *
180: *        Solve for U- part, lockahead for RHS(N) = +-1. This is not done
181: *        In BSOLVE and will hopefully give us a better estimate because
182: *        any ill-conditioning of the original matrix is transfered to U
183: *        and not to L. U(N, N) is an approximation to sigma_min(LU).
184: *
185:          CALL CCOPY( N-1, RHS, 1, WORK, 1 )
186:          WORK( N ) = RHS( N ) + CONE
187:          RHS( N ) = RHS( N ) - CONE
188:          SPLUS = ZERO
189:          SMINU = ZERO
190:          DO 30 I = N, 1, -1
191:             TEMP = CONE / Z( I, I )
192:             WORK( I ) = WORK( I )*TEMP
193:             RHS( I ) = RHS( I )*TEMP
194:             DO 20 K = I + 1, N
195:                WORK( I ) = WORK( I ) - WORK( K )*( Z( I, K )*TEMP )
196:                RHS( I ) = RHS( I ) - RHS( K )*( Z( I, K )*TEMP )
197:    20       CONTINUE
198:             SPLUS = SPLUS + ABS( WORK( I ) )
199:             SMINU = SMINU + ABS( RHS( I ) )
200:    30    CONTINUE
201:          IF( SPLUS.GT.SMINU )
202:      $      CALL CCOPY( N, WORK, 1, RHS, 1 )
203: *
204: *        Apply the permutations JPIV to the computed solution (RHS)
205: *
206:          CALL CLASWP( 1, RHS, LDZ, 1, N-1, JPIV, -1 )
207: *
208: *        Compute the sum of squares
209: *
210:          CALL CLASSQ( N, RHS, 1, RDSCAL, RDSUM )
211:          RETURN
212:       END IF
213: *
214: *     ENTRY IJOB = 2
215: *
216: *     Compute approximate nullvector XM of Z
217: *
218:       CALL CGECON( 'I', N, Z, LDZ, ONE, RTEMP, WORK, RWORK, INFO )
219:       CALL CCOPY( N, WORK( N+1 ), 1, XM, 1 )
220: *
221: *     Compute RHS
222: *
223:       CALL CLASWP( 1, XM, LDZ, 1, N-1, IPIV, -1 )
224:       TEMP = CONE / SQRT( CDOTC( N, XM, 1, XM, 1 ) )
225:       CALL CSCAL( N, TEMP, XM, 1 )
226:       CALL CCOPY( N, XM, 1, XP, 1 )
227:       CALL CAXPY( N, CONE, RHS, 1, XP, 1 )
228:       CALL CAXPY( N, -CONE, XM, 1, RHS, 1 )
229:       CALL CGESC2( N, Z, LDZ, RHS, IPIV, JPIV, SCALE )
230:       CALL CGESC2( N, Z, LDZ, XP, IPIV, JPIV, SCALE )
231:       IF( SCASUM( N, XP, 1 ).GT.SCASUM( N, RHS, 1 ) )
232:      $   CALL CCOPY( N, XP, 1, RHS, 1 )
233: *
234: *     Compute the sum of squares
235: *
236:       CALL CLASSQ( N, RHS, 1, RDSCAL, RDSUM )
237:       RETURN
238: *
239: *     End of CLATDF
240: *
241:       END
242: