```001:       SUBROUTINE CLARFG( N, ALPHA, X, INCX, TAU )
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            INCX, N
010:       COMPLEX            ALPHA, TAU
011: *     ..
012: *     .. Array Arguments ..
013:       COMPLEX            X( * )
014: *     ..
015: *
016: *  Purpose
017: *  =======
018: *
019: *  CLARFG generates a complex elementary reflector H of order n, such
020: *  that
021: *
022: *        H' * ( alpha ) = ( beta ),   H' * H = I.
023: *             (   x   )   (   0  )
024: *
025: *  where alpha and beta are scalars, with beta real, and x is an
026: *  (n-1)-element complex vector. H is represented in the form
027: *
028: *        H = I - tau * ( 1 ) * ( 1 v' ) ,
029: *                      ( v )
030: *
031: *  where tau is a complex scalar and v is a complex (n-1)-element
032: *  vector. Note that H is not hermitian.
033: *
034: *  If the elements of x are all zero and alpha is real, then tau = 0
035: *  and H is taken to be the unit matrix.
036: *
037: *  Otherwise  1 <= real(tau) <= 2  and  abs(tau-1) <= 1 .
038: *
039: *  Arguments
040: *  =========
041: *
042: *  N       (input) INTEGER
043: *          The order of the elementary reflector.
044: *
045: *  ALPHA   (input/output) COMPLEX
046: *          On entry, the value alpha.
047: *          On exit, it is overwritten with the value beta.
048: *
049: *  X       (input/output) COMPLEX array, dimension
050: *                         (1+(N-2)*abs(INCX))
051: *          On entry, the vector x.
052: *          On exit, it is overwritten with the vector v.
053: *
054: *  INCX    (input) INTEGER
055: *          The increment between elements of X. INCX > 0.
056: *
057: *  TAU     (output) COMPLEX
058: *          The value tau.
059: *
060: *  =====================================================================
061: *
062: *     .. Parameters ..
063:       REAL               ONE, ZERO
064:       PARAMETER          ( ONE = 1.0E+0, ZERO = 0.0E+0 )
065: *     ..
066: *     .. Local Scalars ..
067:       INTEGER            J, KNT
068:       REAL               ALPHI, ALPHR, BETA, RSAFMN, SAFMIN, XNORM
069: *     ..
070: *     .. External Functions ..
071:       REAL               SCNRM2, SLAMCH, SLAPY3
073:       EXTERNAL           SCNRM2, SLAMCH, SLAPY3, CLADIV
074: *     ..
075: *     .. Intrinsic Functions ..
076:       INTRINSIC          ABS, AIMAG, CMPLX, REAL, SIGN
077: *     ..
078: *     .. External Subroutines ..
079:       EXTERNAL           CSCAL, CSSCAL
080: *     ..
081: *     .. Executable Statements ..
082: *
083:       IF( N.LE.0 ) THEN
084:          TAU = ZERO
085:          RETURN
086:       END IF
087: *
088:       XNORM = SCNRM2( N-1, X, INCX )
089:       ALPHR = REAL( ALPHA )
090:       ALPHI = AIMAG( ALPHA )
091: *
092:       IF( XNORM.EQ.ZERO .AND. ALPHI.EQ.ZERO ) THEN
093: *
094: *        H  =  I
095: *
096:          TAU = ZERO
097:       ELSE
098: *
099: *        general case
100: *
101:          BETA = -SIGN( SLAPY3( ALPHR, ALPHI, XNORM ), ALPHR )
102:          SAFMIN = SLAMCH( 'S' ) / SLAMCH( 'E' )
103:          RSAFMN = ONE / SAFMIN
104: *
105:          KNT = 0
106:          IF( ABS( BETA ).LT.SAFMIN ) THEN
107: *
108: *           XNORM, BETA may be inaccurate; scale X and recompute them
109: *
110:    10       CONTINUE
111:             KNT = KNT + 1
112:             CALL CSSCAL( N-1, RSAFMN, X, INCX )
113:             BETA = BETA*RSAFMN
114:             ALPHI = ALPHI*RSAFMN
115:             ALPHR = ALPHR*RSAFMN
116:             IF( ABS( BETA ).LT.SAFMIN )
117:      \$         GO TO 10
118: *
119: *           New BETA is at most 1, at least SAFMIN
120: *
121:             XNORM = SCNRM2( N-1, X, INCX )
122:             ALPHA = CMPLX( ALPHR, ALPHI )
123:             BETA = -SIGN( SLAPY3( ALPHR, ALPHI, XNORM ), ALPHR )
124:          END IF
125:          TAU = CMPLX( ( BETA-ALPHR ) / BETA, -ALPHI / BETA )
126:          ALPHA = CLADIV( CMPLX( ONE ), ALPHA-BETA )
127:          CALL CSCAL( N-1, ALPHA, X, INCX )
128: *
129: *        If ALPHA is subnormal, it may lose relative accuracy
130: *
131:          DO 20 J = 1, KNT
132:             BETA = BETA*SAFMIN
133:  20      CONTINUE
134:          ALPHA = BETA
135:       END IF
136: *
137:       RETURN
138: *
139: *     End of CLARFG
140: *
141:       END
142: ```