```001:       SUBROUTINE CLARFP( 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: *  CLARFP 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, beta is real and non-negative, and
026: *  x is an (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               TWO, ONE, ZERO
064:       PARAMETER          ( TWO = 2.0E+0, 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, SLAPY2
073:       EXTERNAL           SCNRM2, SLAMCH, SLAPY3, SLAPY2, 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 ) THEN
093: *
094: *        H  =  [1-alpha/abs(alpha) 0; 0 I], sign chosen so ALPHA >= 0.
095: *
096:          IF( ALPHI.EQ.ZERO ) THEN
097:             IF( ALPHR.GE.ZERO ) THEN
098: !              When TAU.eq.ZERO, the vector is special-cased to be
099: !              all zeros in the application routines.  We do not need
100: !              to clear it.
101:                TAU = ZERO
102:             ELSE
103: !              However, the application routines rely on explicit
104: !              zero checks when TAU.ne.ZERO, and we must clear X.
105:                TAU = TWO
106:                DO J = 1, N-1
107:                   X( 1 + (J-1)*INCX ) = ZERO
108:                END DO
109:                ALPHA = -ALPHA
110:             END IF
111:          ELSE
112: !           Only "reflecting" the diagonal entry to be real and non-negative.
113:             XNORM = SLAPY2( ALPHR, ALPHI )
114:             TAU = CMPLX( ONE - ALPHR / XNORM, -ALPHI / XNORM )
115:             DO J = 1, N-1
116:                X( 1 + (J-1)*INCX ) = ZERO
117:             END DO
118:             ALPHA = XNORM
119:          END IF
120:       ELSE
121: *
122: *        general case
123: *
124:          BETA = SIGN( SLAPY3( ALPHR, ALPHI, XNORM ), ALPHR )
125:          SAFMIN = SLAMCH( 'S' ) / SLAMCH( 'E' )
126:          RSAFMN = ONE / SAFMIN
127: *
128:          KNT = 0
129:          IF( ABS( BETA ).LT.SAFMIN ) THEN
130: *
131: *           XNORM, BETA may be inaccurate; scale X and recompute them
132: *
133:    10       CONTINUE
134:             KNT = KNT + 1
135:             CALL CSSCAL( N-1, RSAFMN, X, INCX )
136:             BETA = BETA*RSAFMN
137:             ALPHI = ALPHI*RSAFMN
138:             ALPHR = ALPHR*RSAFMN
139:             IF( ABS( BETA ).LT.SAFMIN )
140:      \$         GO TO 10
141: *
142: *           New BETA is at most 1, at least SAFMIN
143: *
144:             XNORM = SCNRM2( N-1, X, INCX )
145:             ALPHA = CMPLX( ALPHR, ALPHI )
146:             BETA = SIGN( SLAPY3( ALPHR, ALPHI, XNORM ), ALPHR )
147:          END IF
148:          ALPHA = ALPHA + BETA
149:          IF( BETA.LT.ZERO ) THEN
150:             BETA = -BETA
151:             TAU = -ALPHA / BETA
152:          ELSE
153:             ALPHR = ALPHI * (ALPHI/REAL( ALPHA ))
154:             ALPHR = ALPHR + XNORM * (XNORM/REAL( ALPHA ))
155:             TAU = CMPLX( ALPHR/BETA, -ALPHI/BETA )
156:             ALPHA = CMPLX( -ALPHR, ALPHI )
157:          END IF
158:          ALPHA = CLADIV( CMPLX( ONE ), ALPHA )
159:          CALL CSCAL( N-1, ALPHA, X, INCX )
160: *
161: *        If BETA is subnormal, it may lose relative accuracy
162: *
163:          DO 20 J = 1, KNT
164:             BETA = BETA*SAFMIN
165:  20      CONTINUE
166:          ALPHA = BETA
167:       END IF
168: *
169:       RETURN
170: *
171: *     End of CLARFP
172: *
173:       END
174: ```