Skip to content

Commit ac11f62

Browse files
Several changes to reduce the computation error
1 parent b89b15b commit ac11f62

File tree

1 file changed

+42
-34
lines changed

1 file changed

+42
-34
lines changed

SRC/clartg.f90

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ subroutine CLARTG( f, g, c, s, r )
129129
complex(wp) f, g, r, s
130130
! ..
131131
! .. Local Scalars ..
132-
real(wp) :: d, f1, f2, g1, g2, h2, p, u, uu, v, vv, w
132+
real(wp) :: d, f1, f2, g1, g2, h2, u, v, w
133133
complex(wp) :: fs, gs, t
134134
! ..
135135
! .. Intrinsic Functions ..
@@ -154,19 +154,16 @@ subroutine CLARTG( f, g, c, s, r )
154154
!
155155
! Use unscaled algorithm
156156
!
157-
g2 = ABSSQ( g )
158-
d = sqrt( g2 )
157+
d = abs( g )
159158
s = conjg( g ) / d
160159
r = d
161160
else
162161
!
163162
! Use scaled algorithm
164163
!
165164
u = min( safmax, max( safmin, g1 ) )
166-
uu = one / u
167-
gs = g*uu
168-
g2 = ABSSQ( gs )
169-
d = sqrt( g2 )
165+
gs = g / u
166+
d = abs( gs )
170167
s = conjg( gs ) / d
171168
r = d*u
172169
end if
@@ -181,60 +178,71 @@ subroutine CLARTG( f, g, c, s, r )
181178
f2 = ABSSQ( f )
182179
g2 = ABSSQ( g )
183180
h2 = f2 + g2
184-
if( f2 > rtmin .and. h2 < rtmax ) then
185-
d = sqrt( f2*h2 )
186-
else
187-
d = sqrt( f2 )*sqrt( h2 )
188-
end if
189-
p = 1 / d
190181
if( f2 > safmin * g2 ) then
191-
c = 1 / sqrt( one + g2/f2 )
182+
d = sqrt( one + g2/f2 )
183+
c = one / d
184+
if( f2 > rtmin .and. h2 < rtmax ) then
185+
s = conjg( g )*( f / sqrt( f2*h2 ) )
186+
else
187+
s = conjg( g )*( f /( f2*d ) )
188+
end if
189+
r = f * d
192190
else
193-
c = f2*p
191+
if( f2 > rtmin .and. h2 < rtmax ) then
192+
d = sqrt( f2*h2 )
193+
else
194+
d = sqrt( f2 )*sqrt( h2 )
195+
end if
196+
c = f2 / d
197+
s = conjg( g )*( f / d )
198+
r = f*( h2 / d )
194199
end if
195-
s = conjg( g )*( f*p )
196-
r = f*( h2*p )
197200
else
198201
!
199202
! Use scaled algorithm
200203
!
201204
u = min( safmax, max( safmin, f1, g1 ) )
202-
uu = one / u
203-
gs = g*uu
205+
gs = g / u
204206
g2 = ABSSQ( gs )
205-
if( f1*uu < rtmin ) then
207+
if( f1 < rtmin * u ) then
206208
!
207209
! f is not well-scaled when scaled by g1.
208210
! Use a different scaling for f.
209211
!
210212
v = min( safmax, max( safmin, f1 ) )
211-
vv = one / v
212-
w = v * uu
213-
fs = f*vv
213+
w = v / u
214+
fs = f / v
214215
f2 = ABSSQ( fs )
215216
h2 = f2*w**2 + g2
216217
else
217218
!
218219
! Otherwise use the same scaling for f and g.
219220
!
220221
w = one
221-
fs = f*uu
222+
fs = f / u
222223
f2 = ABSSQ( fs )
223224
h2 = f2 + g2
224225
end if
225-
if( f2 > rtmin .and. h2 < rtmax ) then
226-
d = sqrt( f2*h2 )
227-
else
228-
d = sqrt( f2 )*sqrt( h2 )
229-
end if
230-
p = 1 / d
231226
if( f2 > safmin * g2 ) then
232-
c = (1 / sqrt( one + g2/f2 )) * w
227+
! Use a precise algorithm
228+
d = sqrt( w**2 + g2/f2 )
229+
c = w / d
230+
if( f2 > rtmin .and. h2 < rtmax ) then
231+
s = conjg( gs )*( fs / sqrt( f2*h2 ) )
232+
else
233+
s = conjg( gs )*( fs / ( f2*d ) )
234+
end if
235+
r = ( fs * d ) * u
233236
else
234-
c = ( f2*p )*w
237+
if( f2 > rtmin .and. h2 < rtmax ) then
238+
d = sqrt( f2*h2 )
239+
else
240+
d = sqrt( f2 )*sqrt( h2 )
241+
end if
242+
c = ( f2 / d )*w
243+
s = conjg( gs )*( fs / d )
244+
r = ( fs*( h2 / d ) )*u
235245
end if
236-
s = conjg( gs )*( fs*p )
237-
r = ( fs*( h2*p ) )*u
238246
end if
239247
end if
240248
return

0 commit comments

Comments
 (0)