numeric-linalg
Educational material on the SciPy implementation of numerical linear algebra algorithms
Name | Size | Mode | |
.. | |||
lapack/SRC/zlartg.f90 | 8051B | -rw-r--r-- |
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
!> \brief \b ZLARTG generates a plane rotation with real cosine and complex sine. ! ! =========== DOCUMENTATION =========== ! ! Online html documentation available at ! http://www.netlib.org/lapack/explore-html/ ! ! Definition: ! =========== ! ! SUBROUTINE ZLARTG( F, G, C, S, R ) ! ! .. Scalar Arguments .. ! REAL(wp) C ! COMPLEX(wp) F, G, R, S ! .. ! !> \par Purpose: ! ============= !> !> \verbatim !> !> ZLARTG generates a plane rotation so that !> !> [ C S ] . [ F ] = [ R ] !> [ -conjg(S) C ] [ G ] [ 0 ] !> !> where C is real and C**2 + |S|**2 = 1. !> !> The mathematical formulas used for C and S are !> !> sgn(x) = { x / |x|, x != 0 !> { 1, x = 0 !> !> R = sgn(F) * sqrt(|F|**2 + |G|**2) !> !> C = |F| / sqrt(|F|**2 + |G|**2) !> !> S = sgn(F) * conjg(G) / sqrt(|F|**2 + |G|**2) !> !> Special conditions: !> If G=0, then C=1 and S=0. !> If F=0, then C=0 and S is chosen so that R is real. !> !> When F and G are real, the formulas simplify to C = F/R and !> S = G/R, and the returned values of C, S, and R should be !> identical to those returned by DLARTG. !> !> The algorithm used to compute these quantities incorporates scaling !> to avoid overflow or underflow in computing the square root of the !> sum of squares. !> !> This is the same routine ZROTG fom BLAS1, except that !> F and G are unchanged on return. !> !> Below, wp=>dp stands for double precision from LA_CONSTANTS module. !> \endverbatim ! ! Arguments: ! ========== ! !> \param[in] F !> \verbatim !> F is COMPLEX(wp) !> The first component of vector to be rotated. !> \endverbatim !> !> \param[in] G !> \verbatim !> G is COMPLEX(wp) !> The second component of vector to be rotated. !> \endverbatim !> !> \param[out] C !> \verbatim !> C is REAL(wp) !> The cosine of the rotation. !> \endverbatim !> !> \param[out] S !> \verbatim !> S is COMPLEX(wp) !> The sine of the rotation. !> \endverbatim !> !> \param[out] R !> \verbatim !> R is COMPLEX(wp) !> The nonzero component of the rotated vector. !> \endverbatim ! ! Authors: ! ======== ! !> \author Weslley Pereira, University of Colorado Denver, USA ! !> \date December 2021 ! !> \ingroup lartg ! !> \par Further Details: ! ===================== !> !> \verbatim !> !> Based on the algorithm from !> !> Anderson E. (2017) !> Algorithm 978: Safe Scaling in the Level 1 BLAS !> ACM Trans Math Softw 44:1--28 !> https://doi.org/10.1145/3061665 !> !> \endverbatim ! subroutine ZLARTG( f, g, c, s, r ) use LA_CONSTANTS, & only: wp=>dp, zero=>dzero, one=>done, two=>dtwo, czero=>zzero, & safmin=>dsafmin, safmax=>dsafmax ! ! -- LAPACK auxiliary routine -- ! -- LAPACK is a software package provided by Univ. of Tennessee, -- ! -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- ! February 2021 ! ! .. Scalar Arguments .. real(wp) c complex(wp) f, g, r, s ! .. ! .. Local Scalars .. real(wp) :: d, f1, f2, g1, g2, h2, u, v, w, rtmin, rtmax complex(wp) :: fs, gs, t ! .. ! .. Intrinsic Functions .. intrinsic :: abs, aimag, conjg, max, min, real, sqrt ! .. ! .. Statement Functions .. real(wp) :: ABSSQ ! .. ! .. Statement Function definitions .. ABSSQ( t ) = real( t )**2 + aimag( t )**2 ! .. ! .. Constants .. rtmin = sqrt( safmin ) ! .. ! .. Executable Statements .. ! if( g == czero ) then c = one s = czero r = f else if( f == czero ) then c = zero if( real(g) == zero ) then r = abs(aimag(g)) s = conjg( g ) / r elseif( aimag(g) == zero ) then r = abs(real(g)) s = conjg( g ) / r else g1 = max( abs(real(g)), abs(aimag(g)) ) rtmax = sqrt( safmax/2 ) if( g1 > rtmin .and. g1 < rtmax ) then ! ! Use unscaled algorithm ! ! The following two lines can be replaced by `d = abs( g )`. ! This algorithm do not use the intrinsic complex abs. g2 = ABSSQ( g ) d = sqrt( g2 ) s = conjg( g ) / d r = d else ! ! Use scaled algorithm ! u = min( safmax, max( safmin, g1 ) ) gs = g / u ! The following two lines can be replaced by `d = abs( gs )`. ! This algorithm do not use the intrinsic complex abs. g2 = ABSSQ( gs ) d = sqrt( g2 ) s = conjg( gs ) / d r = d*u end if end if else f1 = max( abs(real(f)), abs(aimag(f)) ) g1 = max( abs(real(g)), abs(aimag(g)) ) rtmax = sqrt( safmax/4 ) if( f1 > rtmin .and. f1 < rtmax .and. & g1 > rtmin .and. g1 < rtmax ) then ! ! Use unscaled algorithm ! f2 = ABSSQ( f ) g2 = ABSSQ( g ) h2 = f2 + g2 ! safmin <= f2 <= h2 <= safmax if( f2 >= h2 * safmin ) then ! safmin <= f2/h2 <= 1, and h2/f2 is finite c = sqrt( f2 / h2 ) r = f / c rtmax = rtmax * 2 if( f2 > rtmin .and. h2 < rtmax ) then ! safmin <= sqrt( f2*h2 ) <= safmax s = conjg( g ) * ( f / sqrt( f2*h2 ) ) else s = conjg( g ) * ( r / h2 ) end if else ! f2/h2 <= safmin may be subnormal, and h2/f2 may overflow. ! Moreover, ! safmin <= f2*f2 * safmax < f2 * h2 < h2*h2 * safmin <= safmax, ! sqrt(safmin) <= sqrt(f2 * h2) <= sqrt(safmax). ! Also, ! g2 >> f2, which means that h2 = g2. d = sqrt( f2 * h2 ) c = f2 / d if( c >= safmin ) then r = f / c else ! f2 / sqrt(f2 * h2) < safmin, then ! sqrt(safmin) <= f2 * sqrt(safmax) <= h2 / sqrt(f2 * h2) <= h2 * (safmin / f2) <= h2 <= safmax r = f * ( h2 / d ) end if s = conjg( g ) * ( f / d ) end if else ! ! Use scaled algorithm ! u = min( safmax, max( safmin, f1, g1 ) ) gs = g / u g2 = ABSSQ( gs ) if( f1 / u < rtmin ) then ! ! f is not well-scaled when scaled by g1. ! Use a different scaling for f. ! v = min( safmax, max( safmin, f1 ) ) w = v / u fs = f / v f2 = ABSSQ( fs ) h2 = f2*w**2 + g2 else ! ! Otherwise use the same scaling for f and g. ! w = one fs = f / u f2 = ABSSQ( fs ) h2 = f2 + g2 end if ! safmin <= f2 <= h2 <= safmax if( f2 >= h2 * safmin ) then ! safmin <= f2/h2 <= 1, and h2/f2 is finite c = sqrt( f2 / h2 ) r = fs / c rtmax = rtmax * 2 if( f2 > rtmin .and. h2 < rtmax ) then ! safmin <= sqrt( f2*h2 ) <= safmax s = conjg( gs ) * ( fs / sqrt( f2*h2 ) ) else s = conjg( gs ) * ( r / h2 ) end if else ! f2/h2 <= safmin may be subnormal, and h2/f2 may overflow. ! Moreover, ! safmin <= f2*f2 * safmax < f2 * h2 < h2*h2 * safmin <= safmax, ! sqrt(safmin) <= sqrt(f2 * h2) <= sqrt(safmax). ! Also, ! g2 >> f2, which means that h2 = g2. d = sqrt( f2 * h2 ) c = f2 / d if( c >= safmin ) then r = fs / c else ! f2 / sqrt(f2 * h2) < safmin, then ! sqrt(safmin) <= f2 * sqrt(safmax) <= h2 / sqrt(f2 * h2) <= h2 * (safmin / f2) <= h2 <= safmax r = fs * ( h2 / d ) end if s = conjg( gs ) * ( fs / d ) end if ! Rescale c and r c = c * w r = r * u end if end if return end subroutine