From: Jarkko Hietaniemi Date: Tue, 2 May 2000 22:21:26 +0000 (+0000) Subject: Complex tweakery. X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=9bc5fa8de0e98ff1317c3fdc7e35f6e13be0ac35;p=p5sagit%2Fp5-mst-13.2.git Complex tweakery. p4raw-id: //depot/cfgperl@6051 --- diff --git a/lib/Math/Complex.pm b/lib/Math/Complex.pm index b13191b..daf190e 100644 --- a/lib/Math/Complex.pm +++ b/lib/Math/Complex.pm @@ -1105,10 +1105,13 @@ sub acosh { return cplx(0, CORE::atan2(CORE::sqrt(1 - $re*$re), $re)) if CORE::abs($re) < 1; } - my $s = &sqrt($z*$z - 1); - my $t = $z + $s; - $t = 1/(2*$s) if $t == 0 || $t && &abs(cosh(&log($t)) - $z) > $eps; - return &log($t); + my $t = &sqrt($z * $z - 1) + $z; + # Try MacLaurin if looking bad. + $t = 1/(2 * $z) - 1/(8 * $z**3) + 1/(16 * $z**5) - 5/(128 * $z**7) + if $t == 0; + my $u = &log($t); + $u->Im(-$u->Im) if $im == 0; + return $re < 0 ? -$u : $u; } # @@ -1122,10 +1125,10 @@ sub asinh { my $t = $z + CORE::sqrt($z*$z + 1); return CORE::log($t) if $t; } - my $s = &sqrt($z*$z + 1); - my $t = $z + $s; - # Try Taylor series if looking bad. - $t = 1/(2*$s) if $t == 0 || $t && &abs(sinh(&log($t)) - $z) > $eps; + my $t = &sqrt($z * $z + 1) + $z; + # Try MacLaurin if looking bad. + $t = 1/(2 * $z) - 1/(8 * $z**3) + 1/(16 * $z**5) - 5/(128 * $z**7) + if $t == 0; return &log($t); } @@ -1390,7 +1393,7 @@ sub stringify_polar { if ($format{polar_pretty_print}) { my ($a, $b); - for $a (2, 3, 4, 6, 8, 12, 16, 24, 30, 32, 36, 48, 60, 64, 72) { + for $a (2..9) { $b = $t * $a / pi; if (int($b) == $b) { $b = $b < 0 ? "-" : "" if CORE::abs($b) == 1; @@ -1734,7 +1737,7 @@ For instance: print "j = $j\n"; # Prints "j = -0.5+0.866025403784439i" The polar style attempts to emphasize arguments like I -(where I is a positive integer and I an integer within [-9,+9]), +(where I is a positive integer and I an integer within [-9, +9]), this is called I. =head2 CHANGED IN PERL 5.6 diff --git a/t/lib/complex.t b/t/lib/complex.t index 7312c09..d4beb8b 100755 --- a/t/lib/complex.t +++ b/t/lib/complex.t @@ -892,7 +892,7 @@ __END__ ( 2,-3):( 1.96863792579310, -0.96465850440760) &acosh -(-2.0,0):( -1.31695789692482, 3.14159265358979) +(-2.0,0):( 1.31695789692482, 3.14159265358979) (-1.0,0):( 0, 3.14159265358979) (-0.5,0):( 0, 2.09439510239320) ( 0.0,0):( 0, 1.57079632679490) @@ -902,8 +902,8 @@ __END__ &acosh ( 2, 3):( 1.98338702991654, 1.00014354247380) -(-2, 3):( -1.98338702991653, -2.14144911111600) -(-2,-3):( -1.98338702991653, 2.14144911111600) +(-2, 3):( 1.98338702991653, 2.14144911111600) +(-2,-3):( 1.98338702991653, -2.14144911111600) ( 2,-3):( 1.98338702991654, -1.00014354247380) &atanh @@ -922,15 +922,15 @@ __END__ &asech (-2.0,0):( 0 , 2.09439510239320) (-1.0,0):( 0 , 3.14159265358979) -(-0.5,0):( -1.31695789692482, 3.14159265358979) +(-0.5,0):( 1.31695789692482, 3.14159265358979) ( 0.5,0):( 1.31695789692482, 0 ) ( 1.0,0):( 0 , 0 ) ( 2.0,0):( 0 , 1.04719755119660) &asech ( 2, 3):( 0.23133469857397, -1.42041072246703) -(-2, 3):( -0.23133469857397, 1.72118193112276) -(-2,-3):( -0.23133469857397, -1.72118193112276) +(-2, 3):( 0.23133469857397, -1.72118193112276) +(-2,-3):( 0.23133469857397, 1.72118193112276) ( 2,-3):( 0.23133469857397, 1.42041072246703) &acsch