```001:       SUBROUTINE SLAGS2( UPPER, A1, A2, A3, B1, B2, B3, CSU, SNU, CSV,
002:      \$                   SNV, CSQ, SNQ )
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:       LOGICAL            UPPER
010:       REAL               A1, A2, A3, B1, B2, B3, CSQ, CSU, CSV, SNQ,
011:      \$                   SNU, SNV
012: *     ..
013: *
014: *  Purpose
015: *  =======
016: *
017: *  SLAGS2 computes 2-by-2 orthogonal matrices U, V and Q, such
018: *  that if ( UPPER ) then
019: *
020: *            U'*A*Q = U'*( A1 A2 )*Q = ( x  0  )
021: *                        ( 0  A3 )     ( x  x  )
022: *  and
023: *            V'*B*Q = V'*( B1 B2 )*Q = ( x  0  )
024: *                        ( 0  B3 )     ( x  x  )
025: *
026: *  or if ( .NOT.UPPER ) then
027: *
028: *            U'*A*Q = U'*( A1 0  )*Q = ( x  x  )
029: *                        ( A2 A3 )     ( 0  x  )
030: *  and
031: *            V'*B*Q = V'*( B1 0  )*Q = ( x  x  )
032: *                        ( B2 B3 )     ( 0  x  )
033: *
034: *  The rows of the transformed A and B are parallel, where
035: *
036: *    U = (  CSU  SNU ), V = (  CSV SNV ), Q = (  CSQ   SNQ )
037: *        ( -SNU  CSU )      ( -SNV CSV )      ( -SNQ   CSQ )
038: *
039: *  Z' denotes the transpose of Z.
040: *
041: *
042: *  Arguments
043: *  =========
044: *
045: *  UPPER   (input) LOGICAL
046: *          = .TRUE.: the input matrices A and B are upper triangular.
047: *          = .FALSE.: the input matrices A and B are lower triangular.
048: *
049: *  A1      (input) REAL
050: *  A2      (input) REAL
051: *  A3      (input) REAL
052: *          On entry, A1, A2 and A3 are elements of the input 2-by-2
053: *          upper (lower) triangular matrix A.
054: *
055: *  B1      (input) REAL
056: *  B2      (input) REAL
057: *  B3      (input) REAL
058: *          On entry, B1, B2 and B3 are elements of the input 2-by-2
059: *          upper (lower) triangular matrix B.
060: *
061: *  CSU     (output) REAL
062: *  SNU     (output) REAL
063: *          The desired orthogonal matrix U.
064: *
065: *  CSV     (output) REAL
066: *  SNV     (output) REAL
067: *          The desired orthogonal matrix V.
068: *
069: *  CSQ     (output) REAL
070: *  SNQ     (output) REAL
071: *          The desired orthogonal matrix Q.
072: *
073: *  =====================================================================
074: *
075: *     .. Parameters ..
076:       REAL               ZERO
077:       PARAMETER          ( ZERO = 0.0E+0 )
078: *     ..
079: *     .. Local Scalars ..
080:       REAL               A, AUA11, AUA12, AUA21, AUA22, AVB11, AVB12,
081:      \$                   AVB21, AVB22, CSL, CSR, D, S1, S2, SNL,
082:      \$                   SNR, UA11R, UA22R, VB11R, VB22R, B, C, R, UA11,
083:      \$                   UA12, UA21, UA22, VB11, VB12, VB21, VB22
084: *     ..
085: *     .. External Subroutines ..
086:       EXTERNAL           SLARTG, SLASV2
087: *     ..
088: *     .. Intrinsic Functions ..
089:       INTRINSIC          ABS
090: *     ..
091: *     .. Executable Statements ..
092: *
093:       IF( UPPER ) THEN
094: *
095: *        Input matrices A and B are upper triangular matrices
096: *
097: *        Form matrix C = A*adj(B) = ( a b )
098: *                                   ( 0 d )
099: *
100:          A = A1*B3
101:          D = A3*B1
102:          B = A2*B1 - A1*B2
103: *
104: *        The SVD of real 2-by-2 triangular C
105: *
106: *         ( CSL -SNL )*( A B )*(  CSR  SNR ) = ( R 0 )
107: *         ( SNL  CSL ) ( 0 D ) ( -SNR  CSR )   ( 0 T )
108: *
109:          CALL SLASV2( A, B, D, S1, S2, SNR, CSR, SNL, CSL )
110: *
111:          IF( ABS( CSL ).GE.ABS( SNL ) .OR. ABS( CSR ).GE.ABS( SNR ) )
112:      \$        THEN
113: *
114: *           Compute the (1,1) and (1,2) elements of U'*A and V'*B,
115: *           and (1,2) element of |U|'*|A| and |V|'*|B|.
116: *
117:             UA11R = CSL*A1
118:             UA12 = CSL*A2 + SNL*A3
119: *
120:             VB11R = CSR*B1
121:             VB12 = CSR*B2 + SNR*B3
122: *
123:             AUA12 = ABS( CSL )*ABS( A2 ) + ABS( SNL )*ABS( A3 )
124:             AVB12 = ABS( CSR )*ABS( B2 ) + ABS( SNR )*ABS( B3 )
125: *
126: *           zero (1,2) elements of U'*A and V'*B
127: *
128:             IF( ( ABS( UA11R )+ABS( UA12 ) ).NE.ZERO ) THEN
129:                IF( AUA12 / ( ABS( UA11R )+ABS( UA12 ) ).LE.AVB12 /
130:      \$             ( ABS( VB11R )+ABS( VB12 ) ) ) THEN
131:                   CALL SLARTG( -UA11R, UA12, CSQ, SNQ, R )
132:                ELSE
133:                   CALL SLARTG( -VB11R, VB12, CSQ, SNQ, R )
134:                END IF
135:             ELSE
136:                CALL SLARTG( -VB11R, VB12, CSQ, SNQ, R )
137:             END IF
138: *
139:             CSU = CSL
140:             SNU = -SNL
141:             CSV = CSR
142:             SNV = -SNR
143: *
144:          ELSE
145: *
146: *           Compute the (2,1) and (2,2) elements of U'*A and V'*B,
147: *           and (2,2) element of |U|'*|A| and |V|'*|B|.
148: *
149:             UA21 = -SNL*A1
150:             UA22 = -SNL*A2 + CSL*A3
151: *
152:             VB21 = -SNR*B1
153:             VB22 = -SNR*B2 + CSR*B3
154: *
155:             AUA22 = ABS( SNL )*ABS( A2 ) + ABS( CSL )*ABS( A3 )
156:             AVB22 = ABS( SNR )*ABS( B2 ) + ABS( CSR )*ABS( B3 )
157: *
158: *           zero (2,2) elements of U'*A and V'*B, and then swap.
159: *
160:             IF( ( ABS( UA21 )+ABS( UA22 ) ).NE.ZERO ) THEN
161:                IF( AUA22 / ( ABS( UA21 )+ABS( UA22 ) ).LE.AVB22 /
162:      \$             ( ABS( VB21 )+ABS( VB22 ) ) ) THEN
163:                   CALL SLARTG( -UA21, UA22, CSQ, SNQ, R )
164:                ELSE
165:                   CALL SLARTG( -VB21, VB22, CSQ, SNQ, R )
166:                END IF
167:             ELSE
168:                CALL SLARTG( -VB21, VB22, CSQ, SNQ, R )
169:             END IF
170: *
171:             CSU = SNL
172:             SNU = CSL
173:             CSV = SNR
174:             SNV = CSR
175: *
176:          END IF
177: *
178:       ELSE
179: *
180: *        Input matrices A and B are lower triangular matrices
181: *
182: *        Form matrix C = A*adj(B) = ( a 0 )
183: *                                   ( c d )
184: *
185:          A = A1*B3
186:          D = A3*B1
187:          C = A2*B3 - A3*B2
188: *
189: *        The SVD of real 2-by-2 triangular C
190: *
191: *         ( CSL -SNL )*( A 0 )*(  CSR  SNR ) = ( R 0 )
192: *         ( SNL  CSL ) ( C D ) ( -SNR  CSR )   ( 0 T )
193: *
194:          CALL SLASV2( A, C, D, S1, S2, SNR, CSR, SNL, CSL )
195: *
196:          IF( ABS( CSR ).GE.ABS( SNR ) .OR. ABS( CSL ).GE.ABS( SNL ) )
197:      \$        THEN
198: *
199: *           Compute the (2,1) and (2,2) elements of U'*A and V'*B,
200: *           and (2,1) element of |U|'*|A| and |V|'*|B|.
201: *
202:             UA21 = -SNR*A1 + CSR*A2
203:             UA22R = CSR*A3
204: *
205:             VB21 = -SNL*B1 + CSL*B2
206:             VB22R = CSL*B3
207: *
208:             AUA21 = ABS( SNR )*ABS( A1 ) + ABS( CSR )*ABS( A2 )
209:             AVB21 = ABS( SNL )*ABS( B1 ) + ABS( CSL )*ABS( B2 )
210: *
211: *           zero (2,1) elements of U'*A and V'*B.
212: *
213:             IF( ( ABS( UA21 )+ABS( UA22R ) ).NE.ZERO ) THEN
214:                IF( AUA21 / ( ABS( UA21 )+ABS( UA22R ) ).LE.AVB21 /
215:      \$             ( ABS( VB21 )+ABS( VB22R ) ) ) THEN
216:                   CALL SLARTG( UA22R, UA21, CSQ, SNQ, R )
217:                ELSE
218:                   CALL SLARTG( VB22R, VB21, CSQ, SNQ, R )
219:                END IF
220:             ELSE
221:                CALL SLARTG( VB22R, VB21, CSQ, SNQ, R )
222:             END IF
223: *
224:             CSU = CSR
225:             SNU = -SNR
226:             CSV = CSL
227:             SNV = -SNL
228: *
229:          ELSE
230: *
231: *           Compute the (1,1) and (1,2) elements of U'*A and V'*B,
232: *           and (1,1) element of |U|'*|A| and |V|'*|B|.
233: *
234:             UA11 = CSR*A1 + SNR*A2
235:             UA12 = SNR*A3
236: *
237:             VB11 = CSL*B1 + SNL*B2
238:             VB12 = SNL*B3
239: *
240:             AUA11 = ABS( CSR )*ABS( A1 ) + ABS( SNR )*ABS( A2 )
241:             AVB11 = ABS( CSL )*ABS( B1 ) + ABS( SNL )*ABS( B2 )
242: *
243: *           zero (1,1) elements of U'*A and V'*B, and then swap.
244: *
245:             IF( ( ABS( UA11 )+ABS( UA12 ) ).NE.ZERO ) THEN
246:                IF( AUA11 / ( ABS( UA11 )+ABS( UA12 ) ).LE.AVB11 /
247:      \$             ( ABS( VB11 )+ABS( VB12 ) ) ) THEN
248:                   CALL SLARTG( UA12, UA11, CSQ, SNQ, R )
249:                ELSE
250:                   CALL SLARTG( VB12, VB11, CSQ, SNQ, R )
251:                END IF
252:             ELSE
253:                CALL SLARTG( VB12, VB11, CSQ, SNQ, R )
254:             END IF
255: *
256:             CSU = SNR
257:             SNU = CSR
258:             CSV = SNL
259:             SNV = CSL
260: *
261:          END IF
262: *
263:       END IF
264: *
265:       RETURN
266: *
267: *     End of SLAGS2
268: *
269:       END
270: ```