001:       SUBROUTINE ZLARGV( N, X, INCX, Y, INCY, C, INCC )
002: *
003: *  -- LAPACK auxiliary routine (version 3.2) --
004: *     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd..
005: *     November 2006
006: *
007: *     .. Scalar Arguments ..
008:       INTEGER            INCC, INCX, INCY, N
009: *     ..
010: *     .. Array Arguments ..
011:       DOUBLE PRECISION   C( * )
012:       COMPLEX*16         X( * ), Y( * )
013: *     ..
014: *
015: *  Purpose
016: *  =======
017: *
018: *  ZLARGV generates a vector of complex plane rotations with real
019: *  cosines, determined by elements of the complex vectors x and y.
020: *  For i = 1,2,...,n
021: *
022: *     (        c(i)   s(i) ) ( x(i) ) = ( r(i) )
023: *     ( -conjg(s(i))  c(i) ) ( y(i) ) = (   0  )
024: *
025: *     where c(i)**2 + ABS(s(i))**2 = 1
026: *
027: *  The following conventions are used (these are the same as in ZLARTG,
028: *  but differ from the BLAS1 routine ZROTG):
029: *     If y(i)=0, then c(i)=1 and s(i)=0.
030: *     If x(i)=0, then c(i)=0 and s(i) is chosen so that r(i) is real.
031: *
032: *  Arguments
033: *  =========
034: *
035: *  N       (input) INTEGER
036: *          The number of plane rotations to be generated.
037: *
038: *  X       (input/output) COMPLEX*16 array, dimension (1+(N-1)*INCX)
039: *          On entry, the vector x.
040: *          On exit, x(i) is overwritten by r(i), for i = 1,...,n.
041: *
042: *  INCX    (input) INTEGER
043: *          The increment between elements of X. INCX > 0.
044: *
045: *  Y       (input/output) COMPLEX*16 array, dimension (1+(N-1)*INCY)
046: *          On entry, the vector y.
047: *          On exit, the sines of the plane rotations.
048: *
049: *  INCY    (input) INTEGER
050: *          The increment between elements of Y. INCY > 0.
051: *
052: *  C       (output) DOUBLE PRECISION array, dimension (1+(N-1)*INCC)
053: *          The cosines of the plane rotations.
054: *
055: *  INCC    (input) INTEGER
056: *          The increment between elements of C. INCC > 0.
057: *
058: *  Further Details
059: *  ======= =======
060: *
061: *  6-6-96 - Modified with a new algorithm by W. Kahan and J. Demmel
062: *
063: *  This version has a few statements commented out for thread safety
064: *  (machine parameters are computed on each entry). 10 feb 03, SJH.
065: *
066: *  =====================================================================
067: *
068: *     .. Parameters ..
069:       DOUBLE PRECISION   TWO, ONE, ZERO
070:       PARAMETER          ( TWO = 2.0D+0, ONE = 1.0D+0, ZERO = 0.0D+0 )
071:       COMPLEX*16         CZERO
072:       PARAMETER          ( CZERO = ( 0.0D+0, 0.0D+0 ) )
073: *     ..
074: *     .. Local Scalars ..
075: *     LOGICAL            FIRST
076: 
077:       INTEGER            COUNT, I, IC, IX, IY, J
078:       DOUBLE PRECISION   CS, D, DI, DR, EPS, F2, F2S, G2, G2S, SAFMIN,
079:      $                   SAFMN2, SAFMX2, SCALE
080:       COMPLEX*16         F, FF, FS, G, GS, R, SN
081: *     ..
082: *     .. External Functions ..
083:       DOUBLE PRECISION   DLAMCH, DLAPY2
084:       EXTERNAL           DLAMCH, DLAPY2
085: *     ..
086: *     .. Intrinsic Functions ..
087:       INTRINSIC          ABS, DBLE, DCMPLX, DCONJG, DIMAG, INT, LOG,
088:      $                   MAX, SQRT
089: *     ..
090: *     .. Statement Functions ..
091:       DOUBLE PRECISION   ABS1, ABSSQ
092: *     ..
093: *     .. Save statement ..
094: *     SAVE               FIRST, SAFMX2, SAFMIN, SAFMN2
095: *     ..
096: *     .. Data statements ..
097: *     DATA               FIRST / .TRUE. /
098: *     ..
099: *     .. Statement Function definitions ..
100:       ABS1( FF ) = MAX( ABS( DBLE( FF ) ), ABS( DIMAG( FF ) ) )
101:       ABSSQ( FF ) = DBLE( FF )**2 + DIMAG( FF )**2
102: *     ..
103: *     .. Executable Statements ..
104: *
105: *     IF( FIRST ) THEN
106: *        FIRST = .FALSE.
107:          SAFMIN = DLAMCH( 'S' )
108:          EPS = DLAMCH( 'E' )
109:          SAFMN2 = DLAMCH( 'B' )**INT( LOG( SAFMIN / EPS ) /
110:      $            LOG( DLAMCH( 'B' ) ) / TWO )
111:          SAFMX2 = ONE / SAFMN2
112: *     END IF
113:       IX = 1
114:       IY = 1
115:       IC = 1
116:       DO 60 I = 1, N
117:          F = X( IX )
118:          G = Y( IY )
119: *
120: *        Use identical algorithm as in ZLARTG
121: *
122:          SCALE = MAX( ABS1( F ), ABS1( G ) )
123:          FS = F
124:          GS = G
125:          COUNT = 0
126:          IF( SCALE.GE.SAFMX2 ) THEN
127:    10       CONTINUE
128:             COUNT = COUNT + 1
129:             FS = FS*SAFMN2
130:             GS = GS*SAFMN2
131:             SCALE = SCALE*SAFMN2
132:             IF( SCALE.GE.SAFMX2 )
133:      $         GO TO 10
134:          ELSE IF( SCALE.LE.SAFMN2 ) THEN
135:             IF( G.EQ.CZERO ) THEN
136:                CS = ONE
137:                SN = CZERO
138:                R = F
139:                GO TO 50
140:             END IF
141:    20       CONTINUE
142:             COUNT = COUNT - 1
143:             FS = FS*SAFMX2
144:             GS = GS*SAFMX2
145:             SCALE = SCALE*SAFMX2
146:             IF( SCALE.LE.SAFMN2 )
147:      $         GO TO 20
148:          END IF
149:          F2 = ABSSQ( FS )
150:          G2 = ABSSQ( GS )
151:          IF( F2.LE.MAX( G2, ONE )*SAFMIN ) THEN
152: *
153: *           This is a rare case: F is very small.
154: *
155:             IF( F.EQ.CZERO ) THEN
156:                CS = ZERO
157:                R = DLAPY2( DBLE( G ), DIMAG( G ) )
158: *              Do complex/real division explicitly with two real
159: *              divisions
160:                D = DLAPY2( DBLE( GS ), DIMAG( GS ) )
161:                SN = DCMPLX( DBLE( GS ) / D, -DIMAG( GS ) / D )
162:                GO TO 50
163:             END IF
164:             F2S = DLAPY2( DBLE( FS ), DIMAG( FS ) )
165: *           G2 and G2S are accurate
166: *           G2 is at least SAFMIN, and G2S is at least SAFMN2
167:             G2S = SQRT( G2 )
168: *           Error in CS from underflow in F2S is at most
169: *           UNFL / SAFMN2 .lt. sqrt(UNFL*EPS) .lt. EPS
170: *           If MAX(G2,ONE)=G2, then F2 .lt. G2*SAFMIN,
171: *           and so CS .lt. sqrt(SAFMIN)
172: *           If MAX(G2,ONE)=ONE, then F2 .lt. SAFMIN
173: *           and so CS .lt. sqrt(SAFMIN)/SAFMN2 = sqrt(EPS)
174: *           Therefore, CS = F2S/G2S / sqrt( 1 + (F2S/G2S)**2 ) = F2S/G2S
175:             CS = F2S / G2S
176: *           Make sure abs(FF) = 1
177: *           Do complex/real division explicitly with 2 real divisions
178:             IF( ABS1( F ).GT.ONE ) THEN
179:                D = DLAPY2( DBLE( F ), DIMAG( F ) )
180:                FF = DCMPLX( DBLE( F ) / D, DIMAG( F ) / D )
181:             ELSE
182:                DR = SAFMX2*DBLE( F )
183:                DI = SAFMX2*DIMAG( F )
184:                D = DLAPY2( DR, DI )
185:                FF = DCMPLX( DR / D, DI / D )
186:             END IF
187:             SN = FF*DCMPLX( DBLE( GS ) / G2S, -DIMAG( GS ) / G2S )
188:             R = CS*F + SN*G
189:          ELSE
190: *
191: *           This is the most common case.
192: *           Neither F2 nor F2/G2 are less than SAFMIN
193: *           F2S cannot overflow, and it is accurate
194: *
195:             F2S = SQRT( ONE+G2 / F2 )
196: *           Do the F2S(real)*FS(complex) multiply with two real
197: *           multiplies
198:             R = DCMPLX( F2S*DBLE( FS ), F2S*DIMAG( FS ) )
199:             CS = ONE / F2S
200:             D = F2 + G2
201: *           Do complex/real division explicitly with two real divisions
202:             SN = DCMPLX( DBLE( R ) / D, DIMAG( R ) / D )
203:             SN = SN*DCONJG( GS )
204:             IF( COUNT.NE.0 ) THEN
205:                IF( COUNT.GT.0 ) THEN
206:                   DO 30 J = 1, COUNT
207:                      R = R*SAFMX2
208:    30             CONTINUE
209:                ELSE
210:                   DO 40 J = 1, -COUNT
211:                      R = R*SAFMN2
212:    40             CONTINUE
213:                END IF
214:             END IF
215:          END IF
216:    50    CONTINUE
217:          C( IC ) = CS
218:          Y( IY ) = SN
219:          X( IX ) = R
220:          IC = IC + INCC
221:          IY = IY + INCY
222:          IX = IX + INCX
223:    60 CONTINUE
224:       RETURN
225: *
226: *     End of ZLARGV
227: *
228:       END
229: