a few typo fixes
[p5sagit/p5-mst-13.2.git] / lib / Math / Complex.pm
index 5149355..19d30b0 100644 (file)
@@ -5,17 +5,39 @@
 # -- Daniel S. Lewart  Since Sep 1997
 #
 
-require Exporter;
 package Math::Complex;
 
-use 5.005_64;
-use strict;
+our($VERSION, @ISA, @EXPORT, %EXPORT_TAGS, $Inf);
+
+$VERSION = 1.32;
+
+BEGIN {
+    unless ($^O eq 'unicosmk') {
+        my $e = $!;
+       # We do want an arithmetic overflow, Inf INF inf Infinity:.
+        undef $Inf unless eval <<'EOE' and $Inf =~ /^inf(?:inity)?$/i;
+         local $SIG{FPE} = sub {die};
+         my $t = CORE::exp 30;
+         $Inf = CORE::exp $t;
+EOE
+       if (!defined $Inf) {            # Try a different method
+         undef $Inf unless eval <<'EOE' and $Inf =~ /^inf(?:inity)?$/i;
+           local $SIG{FPE} = sub {die};
+           my $t = 1;
+           $Inf = $t + "1e99999999999999999999999999999999";
+EOE
+       }
+        $! = $e; # Clear ERANGE.
+    }
+    $Inf = "Inf" if !defined $Inf || !($Inf > 0); # Desperation.
+}
 
-our($VERSION, @ISA, @EXPORT, %EXPORT_TAGS);
+use strict;
 
-my ( $i, %logn );
+my $i;
+my %LOGN;
 
-$VERSION = sprintf("%s", q$Id: Complex.pm,v 1.26 1998/11/01 00:00:00 dsl Exp $ =~ /(\d+\.\d+)/);
+require Exporter;
 
 @ISA = qw(Exporter);
 
@@ -67,17 +89,10 @@ use overload
 # Package "privates"
 #
 
-my $package        = 'Math::Complex';  # Package name
 my %DISPLAY_FORMAT = ('style' => 'cartesian',
                      'polar_pretty_print' => 1);
 my $eps            = 1e-14;            # Epsilon
 
-my $Inf;
-unless ($^O eq 'unicos') { # Unicos gets a fatal runtime error
-    $Inf = CORE::exp(CORE::exp(30));
-}
-$Inf = "Inf" if !defined $Inf || !$Inf > 0;
-
 #
 # Object attributes (internal):
 #      cartesian       [real, imaginary] -- cartesian form
@@ -659,7 +674,7 @@ sub Re {
 #
 sub Im {
        my ($z, $Im) = @_;
-       return $z unless ref $z;
+       return 0 unless ref $z;
        if (defined $Im) {
            $z->{'cartesian'} = [ ${$z->cartesian}[0], $Im ];
            $z->{c_dirty} = 0;
@@ -762,8 +777,8 @@ sub log10 {
 sub logn {
        my ($z, $n) = @_;
        $z = cplx($z, 0) unless ref $z;
-       my $logn = $logn{$n};
-       $logn = $logn{$n} = CORE::log($n) unless defined $logn; # Cache log(n)
+       my $logn = $LOGN{$n};
+       $logn = $LOGN{$n} = CORE::log($n) unless defined $logn; # Cache log(n)
        return &log($z) / $logn;
 }
 
@@ -872,7 +887,8 @@ sub acos {
        my $z = $_[0];
        return CORE::atan2(CORE::sqrt(1-$z*$z), $z)
            if (! ref $z) && CORE::abs($z) <= 1;
-       my ($x, $y) = ref $z ? @{$z->cartesian} : ($z, 0);
+       $z = cplx($z, 0) unless ref $z;
+       my ($x, $y) = @{$z->cartesian};
        return 0 if $x == 1 && $y == 0;
        my $t1 = CORE::sqrt(($x+1)*($x+1) + $y*$y);
        my $t2 = CORE::sqrt(($x-1)*($x-1) + $y*$y);
@@ -884,7 +900,7 @@ sub acos {
        my $u = CORE::atan2(CORE::sqrt(1-$beta*$beta), $beta);
        my $v = CORE::log($alpha + CORE::sqrt($alpha*$alpha-1));
        $v = -$v if $y > 0 || ($y == 0 && $x < -1);
-       return __PACKAGE__->make($u, $v);
+       return (ref $z)->make($u, $v);
 }
 
 #
@@ -896,7 +912,8 @@ sub asin {
        my $z = $_[0];
        return CORE::atan2($z, CORE::sqrt(1-$z*$z))
            if (! ref $z) && CORE::abs($z) <= 1;
-       my ($x, $y) = ref $z ? @{$z->cartesian} : ($z, 0);
+       $z = cplx($z, 0) unless ref $z;
+       my ($x, $y) = @{$z->cartesian};
        return 0 if $x == 0 && $y == 0;
        my $t1 = CORE::sqrt(($x+1)*($x+1) + $y*$y);
        my $t2 = CORE::sqrt(($x-1)*($x-1) + $y*$y);
@@ -908,7 +925,7 @@ sub asin {
        my $u =  CORE::atan2($beta, CORE::sqrt(1-$beta*$beta));
        my $v = -CORE::log($alpha + CORE::sqrt($alpha*$alpha-1));
        $v = -$v if $y > 0 || ($y == 0 && $x < -1);
-       return __PACKAGE__->make($u, $v);
+       return (ref $z)->make($u, $v);
 }
 
 #
@@ -991,8 +1008,6 @@ sub cosh {
            return $ex ? ($ex + 1/$ex)/2 : $Inf;
        }
        my ($x, $y) = @{$z->cartesian};
-       my $cy = CORE::cos($y);
-       my $sy = CORE::cos($y);
        $ex = CORE::exp($x);
        my $ex_1 = $ex ? 1 / $ex : $Inf;
        return (ref $z)->make(CORE::cos($y) * ($ex + $ex_1)/2,
@@ -1017,8 +1032,8 @@ sub sinh {
        my $sy = CORE::sin($y);
        $ex = CORE::exp($x);
        my $ex_1 = $ex ? 1 / $ex : $Inf;
-       return (ref $z)->make($cy * ($ex - $ex_1)/2,
-                             $sy * ($ex + $ex_1)/2);
+       return (ref $z)->make(CORE::cos($y) * ($ex - $ex_1)/2,
+                             CORE::sin($y) * ($ex + $ex_1)/2);
 }
 
 #
@@ -1100,10 +1115,15 @@ 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 Taylor if looking bad (this usually means that
+       # $z was large negative, therefore the sqrt is really
+       # close to abs(z), summing that with z...)
+       $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 $re < 0 && $im == 0;
+       return $re < 0 ? -$u : $u;
 }
 
 #
@@ -1117,10 +1137,12 @@ 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 Taylor if looking bad (this usually means that
+       # $z was large negative, therefore the sqrt is really
+       # close to abs(z), summing that with z...)
+       $t = 1/(2 * $z) - 1/(8 * $z**3) + 1/(16 * $z**5) - 5/(128 * $z**7)
+           if $t == 0;
        return &log($t);
 }
 
@@ -1241,23 +1263,15 @@ sub display_format {
                my %obj = %{$self->{display_format}};
                @display_format{keys %obj} = values %obj;
            }
-           if (@_ == 1) {
-               $display_format{style} = shift;
-           } else {
-               my %new = @_;
-               @display_format{keys %new} = values %new;
-           }
-       } else {                                # Called as a class method
-           if (@_ = 1) {
-               $display_format{style} = $self;
-           } else {
-               my %new = @_;
-               @display_format{keys %new} = values %new;
-           }
-           undef $self;
+       }
+       if (@_ == 1) {
+           $display_format{style} = shift;
+       } else {
+           my %new = @_;
+           @display_format{keys %new} = values %new;
        }
 
-       if (defined $self) {
+       if (ref $self) { # Called as an object method
            $self->{display_format} = { %display_format };
            return
                wantarray ?
@@ -1265,6 +1279,7 @@ sub display_format {
                    $self->{display_format}->{style};
        }
 
+        # Called as a class method
        %DISPLAY_FORMAT = %display_format;
        return
            wantarray ?
@@ -1321,15 +1336,16 @@ sub stringify_cartesian {
        }
 
        if ($y) {
-           if ($y == 1)     { $im = ""  }
-           elsif ($y == -1) { $im = "-" }
-           elsif ($y =~ /^(NaN[QS]?)$/i) {
+           if ($y =~ /^(NaN[QS]?)$/i) {
                $im = $y;
            } else {
                if ($y =~ /^-?$Inf$/oi) {
                    $im = $y;
                } else {
-                   $im = defined $format ? sprintf($format, $y) : $y;
+                   $im =
+                       defined $format ?
+                           sprintf($format, $y) :
+                           ($y == 1 ? "" : ($y == -1 ? "-" : $y));
                }
            }
            $im .= "i";
@@ -1383,11 +1399,11 @@ sub stringify_polar {
 
        $t -= int(CORE::abs($t) / pit2) * pit2;
 
-       if ($format{polar_pretty_print}) {
+       if ($format{polar_pretty_print} && $t) {
            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) {
+               if ($b =~ /^-?\d+$/) {
                    $b = $b < 0 ? "-" : "" if CORE::abs($b) == 1;
                    $theta = "${b}pi/$a";
                    last;
@@ -1409,6 +1425,7 @@ sub stringify_polar {
 __END__
 
 =pod
+
 =head1 NAME
 
 Math::Complex - complex numbers and associated mathematical functions
@@ -1544,7 +1561,7 @@ be called an extension, would it?).
 
 A I<new> operation possible on a complex number that is
 the identity for real numbers is called the I<conjugate>, and is noted
-with an horizontal bar above the number, or C<~z> here.
+with a horizontal bar above the number, or C<~z> here.
 
         z = a + bi
        ~z = a - bi
@@ -1643,7 +1660,7 @@ I<arg>, I<abs>, I<log>, I<csc>, I<cot>, I<acsc>, I<acot>, I<csch>,
 I<coth>, I<acosech>, I<acotanh>, have aliases I<rho>, I<theta>, I<ln>,
 I<cosec>, I<cotan>, I<acosec>, I<acotan>, I<cosech>, I<cotanh>,
 I<acosech>, I<acotanh>, respectively.  C<Re>, C<Im>, C<arg>, C<abs>,
-C<rho>, and C<theta> can be used also also mutators.  The C<cbrt>
+C<rho>, and C<theta> can be used also as mutators.  The C<cbrt>
 returns only one of the solutions: if you want all three, use the
 C<root> function.
 
@@ -1730,7 +1747,7 @@ For instance:
        print "j = $j\n";               # Prints "j = -0.5+0.866025403784439i"
 
 The polar style attempts to emphasize arguments like I<k*pi/n>
-(where I<n> is a positive integer and I<k> an integer within [-9,+9]),
+(where I<n> is a positive integer and I<k> an integer within [-9, +9]),
 this is called I<polar pretty-printing>.
 
 =head2 CHANGED IN PERL 5.6
@@ -1740,29 +1757,33 @@ C<display_format> object method can now be called using
 a parameter hash instead of just a one parameter.
 
 The old display format style, which can have values C<"cartesian"> or
-C<"polar">, can be changed using the C<"style"> parameter.  (The one
-parameter calling convention also still works.)
+C<"polar">, can be changed using the C<"style"> parameter.
+
+       $j->display_format(style => "polar");
+
+The one parameter calling convention also still works.
+
+       $j->display_format("polar");
 
 There are two new display parameters.
 
-The first one is C<"format">, which is a sprintf()-style format
-string to be used for both parts of the complex number(s).  The
-default is C<undef>, which corresponds usually (this is somewhat
-system-dependent) to C<"%.15g">.  You can revert to the default by
-setting the format string to C<undef>.
+The first one is C<"format">, which is a sprintf()-style format string
+to be used for both numeric parts of the complex number(s).  The is
+somewhat system-dependent but most often it corresponds to C<"%.15g">.
+You can revert to the default by setting the C<format> to C<undef>.
 
        # the $j from the above example
 
        $j->display_format('format' => '%.5f');
        print "j = $j\n";               # Prints "j = -0.50000+0.86603i"
-       $j->display_format('format' => '%.6f');
+       $j->display_format('format' => undef);
        print "j = $j\n";               # Prints "j = -0.5+0.86603i"
 
 Notice that this affects also the return values of the
 C<display_format> methods: in list context the whole parameter hash
-will be returned, as opposed to only the style parameter value.  If
-you want to know the whole truth for a complex number, you must call
-both the class method and the object method:
+will be returned, as opposed to only the style parameter value.
+This is a potential incompatibility with earlier versions if you
+have been calling the C<display_format> method in list context.
 
 The second new display parameter is C<"polar_pretty_print">, which can
 be set to true or false, the default being true.  See the previous
@@ -1815,7 +1836,7 @@ or
        Died at...
 
 For the C<csc>, C<cot>, C<asec>, C<acsc>, C<acot>, C<csch>, C<coth>,
-C<asech>, C<acsch>, the argument cannot be C<0> (zero).  For the the
+C<asech>, C<acsch>, the argument cannot be C<0> (zero).  For the
 logarithmic functions and the C<atanh>, C<acoth>, the argument cannot
 be C<1> (one).  For the C<atanh>, C<acoth>, the argument cannot be
 C<-1> (minus one).  For the C<atan>, C<acot>, the argument cannot be
@@ -1826,8 +1847,7 @@ is any integer.
 
 Note that because we are operating on approximations of real numbers,
 these errors can happen when merely `too close' to the singularities
-listed above.  For example C<tan(2*atan2(1,1)+1e-15)> will die of
-division by zero.
+listed above.
 
 =head1 ERRORS DUE TO INDIGESTIBLE ARGUMENTS