LAPACK  3.9.1
LAPACK: Linear Algebra PACKage
clartg.f
Go to the documentation of this file.
1 *> \brief \b CLARTG generates a plane rotation with real cosine and complex sine.
2 *
3 * =========== DOCUMENTATION ===========
4 *
5 * Online html documentation available at
6 * http://www.netlib.org/lapack/explore-html/
7 *
8 *> \htmlonly
9 *> Download CLARTG + dependencies
10 *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/clartg.f">
11 *> [TGZ]</a>
12 *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/clartg.f">
13 *> [ZIP]</a>
14 *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/clartg.f">
15 *> [TXT]</a>
16 *> \endhtmlonly
17 *
18 * Definition:
19 * ===========
20 *
21 * SUBROUTINE CLARTG( F, G, CS, SN, R )
22 *
23 * .. Scalar Arguments ..
24 * REAL CS
25 * COMPLEX F, G, R, SN
26 * ..
27 *
28 *
29 *> \par Purpose:
30 * =============
31 *>
32 *> \verbatim
33 *>
34 *> CLARTG generates a plane rotation so that
35 *>
36 *> [ CS SN ] [ F ] [ R ]
37 *> [ __ ] . [ ] = [ ] where CS**2 + |SN|**2 = 1.
38 *> [ -SN CS ] [ G ] [ 0 ]
39 *>
40 *> This is a faster version of the BLAS1 routine CROTG, except for
41 *> the following differences:
42 *> F and G are unchanged on return.
43 *> If G=0, then CS=1 and SN=0.
44 *> If F=0, then CS=0 and SN is chosen so that R is real.
45 *> \endverbatim
46 *
47 * Arguments:
48 * ==========
49 *
50 *> \param[in] F
51 *> \verbatim
52 *> F is COMPLEX
53 *> The first component of vector to be rotated.
54 *> \endverbatim
55 *>
56 *> \param[in] G
57 *> \verbatim
58 *> G is COMPLEX
59 *> The second component of vector to be rotated.
60 *> \endverbatim
61 *>
62 *> \param[out] CS
63 *> \verbatim
64 *> CS is REAL
65 *> The cosine of the rotation.
66 *> \endverbatim
67 *>
68 *> \param[out] SN
69 *> \verbatim
70 *> SN is COMPLEX
71 *> The sine of the rotation.
72 *> \endverbatim
73 *>
74 *> \param[out] R
75 *> \verbatim
76 *> R is COMPLEX
77 *> The nonzero component of the rotated vector.
78 *> \endverbatim
79 *
80 * Authors:
81 * ========
82 *
83 *> \author Univ. of Tennessee
84 *> \author Univ. of California Berkeley
85 *> \author Univ. of Colorado Denver
86 *> \author NAG Ltd.
87 *
88 *> \ingroup complexOTHERauxiliary
89 *
90 *> \par Further Details:
91 * =====================
92 *>
93 *> \verbatim
94 *>
95 *> 3-5-96 - Modified with a new algorithm by W. Kahan and J. Demmel
96 *>
97 *> This version has a few statements commented out for thread safety
98 *> (machine parameters are computed on each entry). 10 feb 03, SJH.
99 *> \endverbatim
100 *>
101 * =====================================================================
102  SUBROUTINE clartg( F, G, CS, SN, R )
103 *
104 * -- LAPACK auxiliary routine --
105 * -- LAPACK is a software package provided by Univ. of Tennessee, --
106 * -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
107 *
108 * .. Scalar Arguments ..
109  REAL CS
110  COMPLEX F, G, R, SN
111 * ..
112 *
113 * =====================================================================
114 *
115 * .. Parameters ..
116  REAL TWO, ONE, ZERO
117  parameter( two = 2.0e+0, one = 1.0e+0, zero = 0.0e+0 )
118  COMPLEX CZERO
119  parameter( czero = ( 0.0e+0, 0.0e+0 ) )
120 * ..
121 * .. Local Scalars ..
122 * LOGICAL FIRST
123  INTEGER COUNT, I
124  REAL D, DI, DR, EPS, F2, F2S, G2, G2S, SAFMIN,
125  $ SAFMN2, SAFMX2, SCALE
126  COMPLEX FF, FS, GS
127 * ..
128 * .. External Functions ..
129  REAL SLAMCH, SLAPY2
130  LOGICAL SISNAN
131  EXTERNAL slamch, slapy2, sisnan
132 * ..
133 * .. Intrinsic Functions ..
134  INTRINSIC abs, aimag, cmplx, conjg, int, log, max, real,
135  $ sqrt
136 * ..
137 * .. Statement Functions ..
138  REAL ABS1, ABSSQ
139 * ..
140 * .. Statement Function definitions ..
141  abs1( ff ) = max( abs( real( ff ) ), abs( aimag( ff ) ) )
142  abssq( ff ) = real( ff )**2 + aimag( ff )**2
143 * ..
144 * .. Executable Statements ..
145 *
146  safmin = slamch( 'S' )
147  eps = slamch( 'E' )
148  safmn2 = slamch( 'B' )**int( log( safmin / eps ) /
149  $ log( slamch( 'B' ) ) / two )
150  safmx2 = one / safmn2
151  scale = max( abs1( f ), abs1( g ) )
152  fs = f
153  gs = g
154  count = 0
155  IF( scale.GE.safmx2 ) THEN
156  10 CONTINUE
157  count = count + 1
158  fs = fs*safmn2
159  gs = gs*safmn2
160  scale = scale*safmn2
161  IF( scale.GE.safmx2 .AND. count .LT. 20)
162  $ GO TO 10
163  ELSE IF( scale.LE.safmn2 ) THEN
164  IF( g.EQ.czero.OR.sisnan( abs( g ) ) ) THEN
165  cs = one
166  sn = czero
167  r = f
168  RETURN
169  END IF
170  20 CONTINUE
171  count = count - 1
172  fs = fs*safmx2
173  gs = gs*safmx2
174  scale = scale*safmx2
175  IF( scale.LE.safmn2 )
176  $ GO TO 20
177  END IF
178  f2 = abssq( fs )
179  g2 = abssq( gs )
180  IF( f2.LE.max( g2, one )*safmin ) THEN
181 *
182 * This is a rare case: F is very small.
183 *
184  IF( f.EQ.czero ) THEN
185  cs = zero
186  r = slapy2( real( g ), aimag( g ) )
187 * Do complex/real division explicitly with two real divisions
188  d = slapy2( real( gs ), aimag( gs ) )
189  sn = cmplx( real( gs ) / d, -aimag( gs ) / d )
190  RETURN
191  END IF
192  f2s = slapy2( real( fs ), aimag( fs ) )
193 * G2 and G2S are accurate
194 * G2 is at least SAFMIN, and G2S is at least SAFMN2
195  g2s = sqrt( g2 )
196 * Error in CS from underflow in F2S is at most
197 * UNFL / SAFMN2 .lt. sqrt(UNFL*EPS) .lt. EPS
198 * If MAX(G2,ONE)=G2, then F2 .lt. G2*SAFMIN,
199 * and so CS .lt. sqrt(SAFMIN)
200 * If MAX(G2,ONE)=ONE, then F2 .lt. SAFMIN
201 * and so CS .lt. sqrt(SAFMIN)/SAFMN2 = sqrt(EPS)
202 * Therefore, CS = F2S/G2S / sqrt( 1 + (F2S/G2S)**2 ) = F2S/G2S
203  cs = f2s / g2s
204 * Make sure abs(FF) = 1
205 * Do complex/real division explicitly with 2 real divisions
206  IF( abs1( f ).GT.one ) THEN
207  d = slapy2( real( f ), aimag( f ) )
208  ff = cmplx( real( f ) / d, aimag( f ) / d )
209  ELSE
210  dr = safmx2*real( f )
211  di = safmx2*aimag( f )
212  d = slapy2( dr, di )
213  ff = cmplx( dr / d, di / d )
214  END IF
215  sn = ff*cmplx( real( gs ) / g2s, -aimag( gs ) / g2s )
216  r = cs*f + sn*g
217  ELSE
218 *
219 * This is the most common case.
220 * Neither F2 nor F2/G2 are less than SAFMIN
221 * F2S cannot overflow, and it is accurate
222 *
223  f2s = sqrt( one+g2 / f2 )
224 * Do the F2S(real)*FS(complex) multiply with two real multiplies
225  r = cmplx( f2s*real( fs ), f2s*aimag( fs ) )
226  cs = one / f2s
227  d = f2 + g2
228 * Do complex/real division explicitly with two real divisions
229  sn = cmplx( real( r ) / d, aimag( r ) / d )
230  sn = sn*conjg( gs )
231  IF( count.NE.0 ) THEN
232  IF( count.GT.0 ) THEN
233  DO 30 i = 1, count
234  r = r*safmx2
235  30 CONTINUE
236  ELSE
237  DO 40 i = 1, -count
238  r = r*safmn2
239  40 CONTINUE
240  END IF
241  END IF
242  END IF
243  RETURN
244 *
245 * End of CLARTG
246 *
247  END
subroutine clartg(F, G, CS, SN, R)
CLARTG generates a plane rotation with real cosine and complex sine.
Definition: clartg.f:103