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