Sys::Syslog patch to allow unix domain sockets
[p5sagit/p5-mst-13.2.git] / lib / Math / Complex.pm
index 5ec4a56..7a4617c 100644 (file)
@@ -1,34 +1,63 @@
 # $RCSFile$
 #
 # Complex numbers and associated mathematical functions
-# -- Raphael Manfredi, Sept 1996
+# -- Raphael Manfredi, September 1996
+# -- Jarkko Hietaniemi, March-April 1997
 
 require Exporter;
-package Math::Complex; @ISA = qw(Exporter);
-
-@EXPORT = qw(
-       pi i Re Im arg
-       log10 logn cbrt root
-       tan cotan asin acos atan acotan
-       sinh cosh tanh cotanh asinh acosh atanh acotanh
-       cplx cplxe
+package Math::Complex;
+
+use strict;
+
+use vars qw($VERSION @ISA
+           @EXPORT %EXPORT_TAGS
+           $package $display
+           $i $logn %logn);
+
+@ISA = qw(Exporter);
+
+$VERSION = 1.01;
+
+my @trig = qw(
+             pi
+             sin cos tan
+             csc cosec sec cot cotan
+             asin acos atan
+             acsc acosec asec acot acotan
+             sinh cosh tanh
+             csch cosech sech coth cotanh
+             asinh acosh atanh
+             acsch acosech asech acoth acotanh
+            );
+
+@EXPORT = (qw(
+            i Re Im arg
+            sqrt exp log ln
+            log10 logn cbrt root
+            cplx cplxe
+            ),
+          @trig);
+
+%EXPORT_TAGS = (
+    'trig' => [@trig],
 );
 
 use overload
-       '+'             => \&plus,
-       '-'             => \&minus,
-       '*'             => \&multiply,
-       '/'             => \&divide,
+       '+'     => \&plus,
+       '-'     => \&minus,
+       '*'     => \&multiply,
+       '/'     => \&divide,
        '**'    => \&power,
        '<=>'   => \&spaceship,
        'neg'   => \&negate,
-       '~'             => \&conjugate,
+       '~'     => \&conjugate,
        'abs'   => \&abs,
        'sqrt'  => \&sqrt,
        'exp'   => \&exp,
        'log'   => \&log,
        'sin'   => \&sin,
        'cos'   => \&cos,
+       'tan'   => \&tan,
        'atan2' => \&atan2,
        qw("" stringify);
 
@@ -56,7 +85,7 @@ $display = 'cartesian';                       # Default display format
 sub make {
        my $self = bless {}, shift;
        my ($re, $im) = @_;
-       $self->{cartesian} = [$re, $im];
+       $self->{'cartesian'} = [$re, $im];
        $self->{c_dirty} = 0;
        $self->{p_dirty} = 1;
        return $self;
@@ -71,7 +100,7 @@ sub emake {
        my $self = bless {}, shift;
        my ($rho, $theta) = @_;
        $theta += pi() if $rho < 0;
-       $self->{polar} = [abs($rho), $theta];
+       $self->{'polar'} = [abs($rho), $theta];
        $self->{p_dirty} = 0;
        $self->{c_dirty} = 1;
        return $self;
@@ -87,7 +116,7 @@ sub new { &make }            # For backward compatibility only.
 #
 sub cplx {
        my ($re, $im) = @_;
-       return $package->make($re, $im);
+       return $package->make($re, defined $im ? $im : 0);
 }
 
 #
@@ -98,7 +127,7 @@ sub cplx {
 #
 sub cplxe {
        my ($rho, $theta) = @_;
-       return $package->emake($rho, $theta);
+       return $package->emake($rho, defined $theta ? $theta : 0);
 }
 
 #
@@ -106,10 +135,16 @@ sub cplxe {
 #
 # The number defined as 2 * pi = 360 degrees
 #
-sub pi () {
-       $pi = 4 * atan2(1, 1) unless $pi;
-       return $pi;
-}
+
+use constant pi => 4 * atan2(1, 1);
+
+#
+# log2inv
+#
+# Used in log10().
+#
+
+use constant log10inv => 1 / log(10);
 
 #
 # i
@@ -117,9 +152,10 @@ sub pi () {
 # The number defined as i*i = -1;
 #
 sub i () {
-       $i = bless {} unless $i;                # There can be only one i
-       $i->{cartesian} = [0, 1];
-       $i->{polar} = [1, pi/2];
+        return $i if ($i);
+       $i = bless {};
+       $i->{'cartesian'} = [0, 1];
+       $i->{'polar'}     = [1, pi/2];
        $i->{c_dirty} = 0;
        $i->{p_dirty} = 0;
        return $i;
@@ -129,11 +165,13 @@ sub i () {
 # Attribute access/set routines
 #
 
-sub cartesian {$_[0]->{c_dirty} ? $_[0]->update_cartesian : $_[0]->{cartesian}}
-sub polar     {$_[0]->{p_dirty} ? $_[0]->update_polar : $_[0]->{polar}}
+sub cartesian {$_[0]->{c_dirty} ?
+                  $_[0]->update_cartesian : $_[0]->{'cartesian'}}
+sub polar     {$_[0]->{p_dirty} ?
+                  $_[0]->update_polar : $_[0]->{'polar'}}
 
-sub set_cartesian { $_[0]->{p_dirty}++; $_[0]->{cartesian} = $_[1] }
-sub set_polar     { $_[0]->{c_dirty}++; $_[0]->{polar} = $_[1] }
+sub set_cartesian { $_[0]->{p_dirty}++; $_[0]->{'cartesian'} = $_[1] }
+sub set_polar     { $_[0]->{c_dirty}++; $_[0]->{'polar'} = $_[1] }
 
 #
 # ->update_cartesian
@@ -142,9 +180,9 @@ sub set_polar     { $_[0]->{c_dirty}++; $_[0]->{polar} = $_[1] }
 #
 sub update_cartesian {
        my $self = shift;
-       my ($r, $t) = @{$self->{polar}};
+       my ($r, $t) = @{$self->{'polar'}};
        $self->{c_dirty} = 0;
-       return $self->{cartesian} = [$r * cos $t, $r * sin $t];
+       return $self->{'cartesian'} = [$r * cos $t, $r * sin $t];
 }
 
 #
@@ -155,10 +193,10 @@ sub update_cartesian {
 #
 sub update_polar {
        my $self = shift;
-       my ($x, $y) = @{$self->{cartesian}};
+       my ($x, $y) = @{$self->{'cartesian'}};
        $self->{p_dirty} = 0;
-       return $self->{polar} = [0, 0] if $x == 0 && $y == 0;
-       return $self->{polar} = [sqrt($x*$x + $y*$y), atan2($y, $x)];
+       return $self->{'polar'} = [0, 0] if $x == 0 && $y == 0;
+       return $self->{'polar'} = [sqrt($x*$x + $y*$y), atan2($y, $x)];
 }
 
 #
@@ -169,7 +207,8 @@ sub update_polar {
 sub plus {
        my ($z1, $z2, $regular) = @_;
        my ($re1, $im1) = @{$z1->cartesian};
-       my ($re2, $im2) = ref $z2 ? @{$z2->cartesian} : ($z2);
+       $z2 = cplx($z2) unless ref $z2;
+       my ($re2, $im2) = ref $z2 ? @{$z2->cartesian} : ($z2, 0);
        unless (defined $regular) {
                $z1->set_cartesian([$re1 + $re2, $im1 + $im2]);
                return $z1;
@@ -185,7 +224,8 @@ sub plus {
 sub minus {
        my ($z1, $z2, $inverted) = @_;
        my ($re1, $im1) = @{$z1->cartesian};
-       my ($re2, $im2) = ref $z2 ? @{$z2->cartesian} : ($z2);
+       $z2 = cplx($z2) unless ref $z2;
+       my ($re2, $im2) = @{$z2->cartesian};
        unless (defined $inverted) {
                $z1->set_cartesian([$re1 - $re2, $im1 - $im2]);
                return $z1;
@@ -193,6 +233,7 @@ sub minus {
        return $inverted ?
                (ref $z1)->make($re2 - $re1, $im2 - $im1) :
                (ref $z1)->make($re1 - $re2, $im1 - $im2);
+
 }
 
 #
@@ -203,7 +244,8 @@ sub minus {
 sub multiply {
        my ($z1, $z2, $regular) = @_;
        my ($r1, $t1) = @{$z1->polar};
-       my ($r2, $t2) = ref $z2 ? @{$z2->polar} : (abs($z2), $z2 >= 0 ? 0 : pi);
+       $z2 = cplxe(abs($z2), $z2 >= 0 ? 0 : pi) unless ref $z2;
+       my ($r2, $t2) = @{$z2->polar};
        unless (defined $regular) {
                $z1->set_polar([$r1 * $r2, $t1 + $t2]);
                return $z1;
@@ -212,6 +254,27 @@ sub multiply {
 }
 
 #
+# _divbyzero
+#
+# Die on division by zero.
+#
+sub _divbyzero {
+    my $mess = "$_[0]: Division by zero.\n";
+
+    if (defined $_[1]) {
+       $mess .= "(Because in the definition of $_[0], the divisor ";
+       $mess .= "$_[1] " unless ($_[1] eq '0');
+       $mess .= "is 0)\n";
+    }
+
+    my @up = caller(1);
+    
+    $mess .= "Died at $up[1] line $up[2].\n";
+
+    die $mess;
+}
+
+#
 # (divide)
 #
 # Computes z1/z2.
@@ -219,14 +282,35 @@ sub multiply {
 sub divide {
        my ($z1, $z2, $inverted) = @_;
        my ($r1, $t1) = @{$z1->polar};
-       my ($r2, $t2) = ref $z2 ? @{$z2->polar} : (abs($z2), $z2 >= 0 ? 0 : pi);
+       $z2 = cplxe(abs($z2), $z2 >= 0 ? 0 : pi) unless ref $z2;
+       my ($r2, $t2) = @{$z2->polar};
        unless (defined $inverted) {
+               _divbyzero "$z1/0" if ($r2 == 0);
                $z1->set_polar([$r1 / $r2, $t1 - $t2]);
                return $z1;
        }
-       return $inverted ?
-               (ref $z1)->emake($r2 / $r1, $t2 - $t1) :
-               (ref $z1)->emake($r1 / $r2, $t1 - $t2);
+       if ($inverted) {
+               _divbyzero "$z2/0" if ($r1 == 0);
+               return (ref $z1)->emake($r2 / $r1, $t2 - $t1);
+       } else {
+               _divbyzero "$z1/0" if ($r2 == 0);
+               return (ref $z1)->emake($r1 / $r2, $t1 - $t2);
+       }
+}
+
+#
+# _zerotozero
+#
+# Die on zero raised to the zeroth.
+#
+sub _zerotozero {
+    my $mess = "The zero raised to the zeroth power is not defined.\n";
+
+    my @up = caller(1);
+    
+    $mess .= "Died at $up[1] line $up[2].\n";
+
+    die $mess;
 }
 
 #
@@ -236,8 +320,24 @@ sub divide {
 #
 sub power {
        my ($z1, $z2, $inverted) = @_;
-       return exp($z1 * log $z2) if defined $inverted && $inverted;
-       return exp($z2 * log $z1);
+       my $z1z = $z1 == 0;
+       my $z2z = $z2 == 0;
+       _zerotozero if ($z1z and $z2z);
+       if ($inverted) {
+           return 0 if ($z2z);
+           return 1 if ($z1z or $z2 == 1);
+       } else {
+           return 0 if ($z1z);
+           return 1 if ($z2z or $z1 == 1);
+       }
+       $z2 = cplx($z2) unless ref $z2;
+       unless (defined $inverted) {
+               my $z3 = exp($z2 * log $z1);
+               $z1->set_cartesian([@{$z3->cartesian}]);
+               return $z1;
+       }
+       return exp($z2 * log $z1) unless $inverted;
+       return exp($z1 * log $z2);
 }
 
 #
@@ -248,8 +348,8 @@ sub power {
 #
 sub spaceship {
        my ($z1, $z2, $inverted) = @_;
-       my ($re1, $im1) = @{$z1->cartesian};
-       my ($re2, $im2) = ref $z2 ? @{$z2->cartesian} : ($z2);
+       my ($re1, $im1) = ref $z1 ? @{$z1->cartesian} : ($z1, 0);
+       my ($re2, $im2) = ref $z2 ? @{$z2->cartesian} : ($z2, 0);
        my $sgn = $inverted ? -1 : 1;
        return $sgn * ($re1 <=> $re2) if $re1 != $re2;
        return $sgn * ($im1 <=> $im2);
@@ -292,6 +392,7 @@ sub conjugate {
 #
 sub abs {
        my ($z) = @_;
+       return abs($z) unless ref $z;
        my ($r, $t) = @{$z->polar};
        return abs($r);
 }
@@ -303,7 +404,7 @@ sub abs {
 #
 sub arg {
        my ($z) = @_;
-       return 0 unless ref $z;
+       return ($z < 0 ? pi : 0) unless ref $z;
        my ($r, $t) = @{$z->polar};
        return $t;
 }
@@ -311,10 +412,11 @@ sub arg {
 #
 # (sqrt)
 #
-# Compute sqrt(z) (positive only).
+# Compute sqrt(z).
 #
 sub sqrt {
        my ($z) = @_;
+       $z = cplx($z, 0) unless ref $z;
        my ($r, $t) = @{$z->polar};
        return (ref $z)->emake(sqrt($r), $t/2);
 }
@@ -322,16 +424,31 @@ sub sqrt {
 #
 # cbrt
 #
-# Compute cbrt(z) (cubic root, primary only).
+# Compute cbrt(z) (cubic root).
 #
 sub cbrt {
        my ($z) = @_;
-       return $z ** (1/3) unless ref $z;
+       return cplx($z, 0) ** (1/3) unless ref $z;
        my ($r, $t) = @{$z->polar};
        return (ref $z)->emake($r**(1/3), $t/3);
 }
 
 #
+# _rootbad
+#
+# Die on bad root.
+#
+sub _rootbad {
+    my $mess = "Root $_[0] not defined, root must be positive integer.\n";
+
+    my @up = caller(1);
+    
+    $mess .= "Died at $up[1] line $up[2].\n";
+
+    die $mess;
+}
+
+#
 # root
 #
 # Computes all nth root for z, returning an array whose size is n.
@@ -343,8 +460,7 @@ sub cbrt {
 #
 sub root {
        my ($z, $n) = @_;
-       $n = int($n + 0.5);
-       return undef unless $n > 0;
+       _rootbad($n) if ($n < 1 or int($n) != $n);
        my ($r, $t) = ref $z ? @{$z->polar} : (abs($z), $z >= 0 ? 0 : pi);
        my @root;
        my $k;
@@ -389,6 +505,7 @@ sub Im {
 #
 sub exp {
        my ($z) = @_;
+       $z = cplx($z, 0) unless ref $z;
        my ($x, $y) = @{$z->cartesian};
        return (ref $z)->emake(exp($x), $y);
 }
@@ -400,21 +517,33 @@ sub exp {
 #
 sub log {
        my ($z) = @_;
+       $z = cplx($z, 0) unless ref $z;
+       my ($x, $y) = @{$z->cartesian};
        my ($r, $t) = @{$z->polar};
+       $t -= 2 * pi if ($t >  pi() and $x < 0);
+       $t += 2 * pi if ($t < -pi() and $x < 0);
        return (ref $z)->make(log($r), $t);
 }
 
 #
+# ln
+#
+# Alias for log().
+#
+sub ln { Math::Complex::log(@_) }
+
+#
 # log10
 #
 # Compute log10(z).
 #
+
 sub log10 {
        my ($z) = @_;
-       $log10 = log(10) unless defined $log10;
-       return log($z) / $log10 unless ref $z;
+
+       return log(cplx($z, 0)) * log10inv unless ref $z;
        my ($r, $t) = @{$z->polar};
-       return (ref $z)->make(log($r) / $log10, $t / $log10);
+       return (ref $z)->make(log($r) * log10inv, $t * log10inv);
 }
 
 #
@@ -424,9 +553,10 @@ sub log10 {
 #
 sub logn {
        my ($z, $n) = @_;
+       $z = cplx($z, 0) unless ref $z;
        my $logn = $logn{$n};
        $logn = $logn{$n} = log($n) unless defined $logn;       # Cache log(n)
-       return log($z) / log($n);
+       return log($z) / $logn;
 }
 
 #
@@ -436,10 +566,12 @@ sub logn {
 #
 sub cos {
        my ($z) = @_;
+       $z = cplx($z, 0) unless ref $z;
        my ($x, $y) = @{$z->cartesian};
        my $ey = exp($y);
        my $ey_1 = 1 / $ey;
-       return (ref $z)->make(cos($x) * ($ey + $ey_1)/2, sin($x) * ($ey_1 - $ey)/2);
+       return (ref $z)->make(cos($x) * ($ey + $ey_1)/2,
+                             sin($x) * ($ey_1 - $ey)/2);
 }
 
 #
@@ -449,10 +581,12 @@ sub cos {
 #
 sub sin {
        my ($z) = @_;
+       $z = cplx($z, 0) unless ref $z;
        my ($x, $y) = @{$z->cartesian};
        my $ey = exp($y);
        my $ey_1 = 1 / $ey;
-       return (ref $z)->make(sin($x) * ($ey + $ey_1)/2, cos($x) * ($ey - $ey_1)/2);
+       return (ref $z)->make(sin($x) * ($ey + $ey_1)/2,
+                             cos($x) * ($ey - $ey_1)/2);
 }
 
 #
@@ -462,29 +596,70 @@ sub sin {
 #
 sub tan {
        my ($z) = @_;
-       return sin($z) / cos($z);
+       my $cz = cos($z);
+       _divbyzero "tan($z)", "cos($z)" if ($cz == 0);
+       return sin($z) / $cz;
 }
 
 #
-# cotan
+# sec
+#
+# Computes the secant sec(z) = 1 / cos(z).
+#
+sub sec {
+       my ($z) = @_;
+       my $cz = cos($z);
+       _divbyzero "sec($z)", "cos($z)" if ($cz == 0);
+       return 1 / $cz;
+}
+
+#
+# csc
+#
+# Computes the cosecant csc(z) = 1 / sin(z).
+#
+sub csc {
+       my ($z) = @_;
+       my $sz = sin($z);
+       _divbyzero "csc($z)", "sin($z)" if ($sz == 0);
+       return 1 / $sz;
+}
+
+#
+# cosec
+#
+# Alias for csc().
+#
+sub cosec { Math::Complex::csc(@_) }
+
+#
+# cot
 #
-# Computes cotan(z) = 1 / tan(z).
+# Computes cot(z) = 1 / tan(z).
 #
-sub cotan {
+sub cot {
        my ($z) = @_;
-       return cos($z) / sin($z);
+       my $sz = sin($z);
+       _divbyzero "cot($z)", "sin($z)" if ($sz == 0);
+       return cos($z) / $sz;
 }
 
 #
+# cotan
+#
+# Alias for cot().
+#
+sub cotan { Math::Complex::cot(@_) }
+
+#
 # acos
 #
 # Computes the arc cosine acos(z) = -i log(z + sqrt(z*z-1)).
 #
 sub acos {
        my ($z) = @_;
-       my $cz = $z*$z - 1;
-       $cz = cplx($cz, 0) if !ref $cz && $cz < 0;      # Force complex if <0
-       return ~i * log($z + sqrt $cz);                         # ~i is -i
+       $z = cplx($z, 0) unless ref $z;
+       return ~i * log($z + (Re($z) * Im($z) > 0 ? 1 : -1) * sqrt($z*$z - 1));
 }
 
 #
@@ -494,43 +669,88 @@ sub acos {
 #
 sub asin {
        my ($z) = @_;
-       my $cz = 1 - $z*$z;
-       $cz = cplx($cz, 0) if !ref $cz && $cz < 0;      # Force complex if <0
-       return ~i * log(i * $z + sqrt $cz);                     # ~i is -i
+       $z = cplx($z, 0) unless ref $z;
+       return ~i * log(i * $z + sqrt(1 - $z*$z));
 }
 
 #
 # atan
 #
-# Computes the arc tagent atan(z) = i/2 log((i+z) / (i-z)).
+# Computes the arc tangent atan(z) = i/2 log((i+z) / (i-z)).
 #
 sub atan {
        my ($z) = @_;
-       return i/2 * log((i + $z) / (i - $z));
+       $z = cplx($z, 0) unless ref $z;
+       _divbyzero "atan($z)", "i - $z" if ($z == i);
+       return i/2*log((i + $z) / (i - $z));
 }
 
 #
-# acotan
+# asec
+#
+# Computes the arc secant asec(z) = acos(1 / z).
+#
+sub asec {
+       my ($z) = @_;
+       _divbyzero "asec($z)", $z if ($z == 0);
+       return acos(1 / $z);
+}
+
+#
+# acsc
 #
-# Computes the arc cotangent acotan(z) = -i/2 log((i+z) / (z-i))
+# Computes the arc cosecant sec(z) = asin(1 / z).
 #
-sub acotan {
+sub acsc {
        my ($z) = @_;
+       _divbyzero "acsc($z)", $z if ($z == 0);
+       return asin(1 / $z);
+}
+
+#
+# acosec
+#
+# Alias for acsc().
+#
+sub acosec { Math::Complex::acsc(@_) }
+
+#
+# acot
+#
+# Computes the arc cotangent acot(z) = -i/2 log((i+z) / (z-i))
+#
+sub acot {
+       my ($z) = @_;
+       $z = cplx($z, 0) unless ref $z;
+       _divbyzero "acot($z)", "$z - i" if ($z == i);
        return i/-2 * log((i + $z) / ($z - i));
 }
 
 #
+# acotan
+#
+# Alias for acot().
+#
+sub acotan { Math::Complex::acot(@_) }
+
+#
 # cosh
 #
 # Computes the hyperbolic cosine cosh(z) = (exp(z) + exp(-z))/2.
 #
 sub cosh {
        my ($z) = @_;
-       my ($x, $y) = ref $z ? @{$z->cartesian} : ($z);
+       my $real;
+       unless (ref $z) {
+           $z = cplx($z, 0);
+           $real = 1;
+       }
+       my ($x, $y) = @{$z->cartesian};
        my $ex = exp($x);
        my $ex_1 = 1 / $ex;
-       return ($ex + $ex_1)/2 unless ref $z;
-       return (ref $z)->make(cos($y) * ($ex + $ex_1)/2, sin($y) * ($ex - $ex_1)/2);
+       return cplx(0.5 * ($ex + $ex_1), 0) if $real;
+       return (ref $z)->make(cos($y) * ($ex + $ex_1)/2,
+                             sin($y) * ($ex - $ex_1)/2);
 }
 
 #
@@ -540,11 +760,17 @@ sub cosh {
 #
 sub sinh {
        my ($z) = @_;
-       my ($x, $y) = ref $z ? @{$z->cartesian} : ($z);
+       my $real;
+       unless (ref $z) {
+           $z = cplx($z, 0);
+           $real = 1;
+       }
+       my ($x, $y) = @{$z->cartesian};
        my $ex = exp($x);
        my $ex_1 = 1 / $ex;
-       return ($ex - $ex_1)/2 unless ref $z;
-       return (ref $z)->make(cos($y) * ($ex - $ex_1)/2, sin($y) * ($ex + $ex_1)/2);
+       return cplx(0.5 * ($ex - $ex_1), 0) if $real;
+       return (ref $z)->make(cos($y) * ($ex - $ex_1)/2,
+                             sin($y) * ($ex + $ex_1)/2);
 }
 
 #
@@ -554,29 +780,70 @@ sub sinh {
 #
 sub tanh {
        my ($z) = @_;
-       return sinh($z) / cosh($z);
+       my $cz = cosh($z);
+       _divbyzero "tanh($z)", "cosh($z)" if ($cz == 0);
+       return sinh($z) / $cz;
 }
 
 #
-# cotanh
+# sech
+#
+# Computes the hyperbolic secant sech(z) = 1 / cosh(z).
+#
+sub sech {
+       my ($z) = @_;
+       my $cz = cosh($z);
+       _divbyzero "sech($z)", "cosh($z)" if ($cz == 0);
+       return 1 / $cz;
+}
+
+#
+# csch
 #
-# Comptutes the hyperbolic cotangent cotanh(z) = cosh(z) / sinh(z).
+# Computes the hyperbolic cosecant csch(z) = 1 / sinh(z).
 #
-sub cotanh {
+sub csch {
        my ($z) = @_;
-       return cosh($z) / sinh($z);
+       my $sz = sinh($z);
+       _divbyzero "csch($z)", "sinh($z)" if ($sz == 0);
+       return 1 / $sz;
 }
 
 #
+# cosech
+#
+# Alias for csch().
+#
+sub cosech { Math::Complex::csch(@_) }
+
+#
+# coth
+#
+# Computes the hyperbolic cotangent coth(z) = cosh(z) / sinh(z).
+#
+sub coth {
+       my ($z) = @_;
+       my $sz = sinh($z);
+       _divbyzero "coth($z)", "sinh($z)" if ($sz == 0);
+       return cosh($z) / $sz;
+}
+
+#
+# cotanh
+#
+# Alias for coth().
+#
+sub cotanh { Math::Complex::coth(@_) }
+
+#
 # acosh
 #
 # Computes the arc hyperbolic cosine acosh(z) = log(z + sqrt(z*z-1)).
 #
 sub acosh {
        my ($z) = @_;
-       my $cz = $z*$z - 1;
-       $cz = cplx($cz, 0) if !ref $cz && $cz < 0;      # Force complex if <0
-       return log($z + sqrt $cz);
+       $z = cplx($z, 0) unless ref $z;
+       return log($z + sqrt($z*$z - 1));
 }
 
 #
@@ -586,8 +853,8 @@ sub acosh {
 #
 sub asinh {
        my ($z) = @_;
-       my $cz = $z*$z + 1;                                                     # Already complex if <0
-       return log($z + sqrt $cz);
+       $z = cplx($z, 0) unless ref $z;
+       return log($z + sqrt($z*$z + 1));
 }
 
 #
@@ -597,32 +864,70 @@ sub asinh {
 #
 sub atanh {
        my ($z) = @_;
+       _divbyzero 'atanh(1)', "1 - $z" if ($z == 1);
+       $z = cplx($z, 0) unless ref $z;
        my $cz = (1 + $z) / (1 - $z);
-       $cz = cplx($cz, 0) if !ref $cz && $cz < 0;      # Force complex if <0
        return log($cz) / 2;
 }
 
 #
-# acotanh
+# asech
+#
+# Computes the hyperbolic arc secant asech(z) = acosh(1 / z).
+#
+sub asech {
+       my ($z) = @_;
+       _divbyzero 'asech(0)', $z if ($z == 0);
+       return acosh(1 / $z);
+}
+
+#
+# acsch
+#
+# Computes the hyperbolic arc cosecant acsch(z) = asinh(1 / z).
+#
+sub acsch {
+       my ($z) = @_;
+       _divbyzero 'acsch(0)', $z if ($z == 0);
+       return asinh(1 / $z);
+}
+
+#
+# acosech
+#
+# Alias for acosh().
+#
+sub acosech { Math::Complex::acsch(@_) }
+
 #
-# Computes the arc hyperbolic cotangent acotanh(z) = 1/2 log((1+z) / (z-1)).
+# acoth
 #
-sub acotanh {
+# Computes the arc hyperbolic cotangent acoth(z) = 1/2 log((1+z) / (z-1)).
+#
+sub acoth {
        my ($z) = @_;
+       _divbyzero 'acoth(1)', "$z - 1" if ($z == 1);
+       $z = cplx($z, 0) unless ref $z;
        my $cz = (1 + $z) / ($z - 1);
-       $cz = cplx($cz, 0) if !ref $cz && $cz < 0;      # Force complex if <0
        return log($cz) / 2;
 }
 
 #
+# acotanh
+#
+# Alias for acot().
+#
+sub acotanh { Math::Complex::acoth(@_) }
+
+#
 # (atan2)
 #
 # Compute atan(z1/z2).
 #
 sub atan2 {
        my ($z1, $z2, $inverted) = @_;
-       my ($re1, $im1) = @{$z1->cartesian};
-       my ($re2, $im2) = ref $z2 ? @{$z2->cartesian} : ($z2);
+       my ($re1, $im1) = ref $z1 ? @{$z1->cartesian} : ($z1, 0);
+       my ($re2, $im2) = ref $z2 ? @{$z2->cartesian} : ($z2, 0);
        my $tan;
        if (defined $inverted && $inverted) {   # atan(z2/z1)
                return pi * ($re2 > 0 ? 1 : -1) if $re1 == 0 && $im1 == 0;
@@ -653,7 +958,7 @@ sub display_format {
 
        if (ref $self) {                        # Called as a method
                $format = shift;
-       } else {                                        # Regular procedure call
+       } else {                                # Regular procedure call
                $format = $self;
                undef $self;
        }
@@ -699,12 +1004,17 @@ sub stringify_cartesian {
        my ($x, $y) = @{$z->cartesian};
        my ($re, $im);
 
+       $x = int($x + ($x < 0 ? -1 : 1) * 1e-14)
+               if int(abs($x)) != int(abs($x) + 1e-14);
+       $y = int($y + ($y < 0 ? -1 : 1) * 1e-14)
+               if int(abs($y)) != int(abs($y) + 1e-14);
+
        $re = "$x" if abs($x) >= 1e-14;
        if ($y == 1)                            { $im = 'i' }
        elsif ($y == -1)                        { $im = '-i' }
-       elsif (abs($y) >= 1e-14)        { $im = "${y}i" }
+       elsif (abs($y) >= 1e-14)        { $im = $y . "i" }
 
-       my $str;
+       my $str = '';
        $str = $re if defined $re;
        $str .= "+$im" if defined $im;
        $str =~ s/\+-/-/;
@@ -723,18 +1033,26 @@ sub stringify_polar {
        my $z  = shift;
        my ($r, $t) = @{$z->polar};
        my $theta;
+       my $eps = 1e-14;
 
-       return '[0,0]' if $r <= 1e-14;
+       return '[0,0]' if $r <= $eps;
 
        my $tpi = 2 * pi;
        my $nt = $t / $tpi;
        $nt = ($nt - int($nt)) * $tpi;
        $nt += $tpi if $nt < 0;                 # Range [0, 2pi]
 
-       if (abs($nt) <= 1e-14)                  { $theta = 0 }
-       elsif (abs(pi-$nt) <= 1e-14)    { $theta = 'pi' }
+       if (abs($nt) <= $eps)           { $theta = 0 }
+       elsif (abs(pi-$nt) <= $eps)     { $theta = 'pi' }
 
-       return "\[$r,$theta\]" if defined $theta;
+       if (defined $theta) {
+               $r = int($r + ($r < 0 ? -1 : 1) * $eps)
+                       if int(abs($r)) != int(abs($r) + $eps);
+               $theta = int($theta + ($theta < 0 ? -1 : 1) * $eps)
+                       if ($theta ne 'pi' and
+                           int(abs($theta)) != int(abs($theta) + $eps));
+               return "\[$r,$theta\]";
+       }
 
        #
        # Okay, number is not a real. Try to identify pi/n and friends...
@@ -745,14 +1063,21 @@ sub stringify_polar {
        
        for ($k = 1, $kpi = pi; $k < 10; $k++, $kpi += pi) {
                $n = int($kpi / $nt + ($nt > 0 ? 1 : -1) * 0.5);
-               if (abs($kpi/$n - $nt) <= 1e-14) {
-                       $theta = ($nt < 0 ? '-':'').($k == 1 ? 'pi':"${k}pi").'/'.abs($n);
+               if (abs($kpi/$n - $nt) <= $eps) {
+                       $theta = ($nt < 0 ? '-':'').
+                                ($k == 1 ? 'pi':"${k}pi").'/'.abs($n);
                        last;
                }
        }
 
        $theta = $nt unless defined $theta;
 
+       $r = int($r + ($r < 0 ? -1 : 1) * $eps)
+               if int(abs($r)) != int(abs($r) + $eps);
+       $theta = int($theta + ($theta < 0 ? -1 : 1) * $eps)
+               if ($theta !~ m(^-?\d*pi/\d+$) and
+                   int(abs($theta)) != int(abs($theta) + $eps));
+
        return "\[$r,$theta\]";
 }
 
@@ -766,6 +1091,7 @@ Math::Complex - complex numbers and associated mathematical functions
 =head1 SYNOPSIS
 
        use Math::Complex;
+       
        $z = Math::Complex->make(5, 6);
        $t = 4 - 3*i + $z;
        $j = cplxe(1, 2*pi/3);
@@ -958,24 +1284,41 @@ numbers:
        logn(z, n) = log(z) / log(n)
 
        tan(z) = sin(z) / cos(z)
-       cotan(z) = 1 / tan(z)
+
+       csc(z) = 1 / sin(z)
+       sec(z) = 1 / cos(z)
+       cot(z) = 1 / tan(z)
 
        asin(z) = -i * log(i*z + sqrt(1-z*z))
        acos(z) = -i * log(z + sqrt(z*z-1))
        atan(z) = i/2 * log((i+z) / (i-z))
-       acotan(z) = -i/2 * log((i+z) / (z-i))
+
+       acsc(z) = asin(1 / z)
+       asec(z) = acos(1 / z)
+       acot(z) = -i/2 * log((i+z) / (z-i))
 
        sinh(z) = 1/2 (exp(z) - exp(-z))
        cosh(z) = 1/2 (exp(z) + exp(-z))
-       tanh(z) = sinh(z) / cosh(z)
-       cotanh(z) = 1 / tanh(z)
+       tanh(z) = sinh(z) / cosh(z) = (exp(z) - exp(-z)) / (exp(z) + exp(-z))
+
+       csch(z) = 1 / sinh(z)
+       sech(z) = 1 / cosh(z)
+       coth(z) = 1 / tanh(z)
        
        asinh(z) = log(z + sqrt(z*z+1))
        acosh(z) = log(z + sqrt(z*z-1))
        atanh(z) = 1/2 * log((1+z) / (1-z))
-       acotanh(z) = 1/2 * log((1+z) / (z-1))
 
-The I<root> function is available to compute all the I<n>th
+       acsch(z) = asinh(1 / z)
+       asech(z) = acosh(1 / z)
+       acoth(z) = atanh(1 / z) = 1/2 * log((1+z) / (z-1))
+
+I<log>, I<csc>, I<cot>, I<acsc>, I<acot>, I<csch>, I<coth>,
+I<acosech>, I<acotanh>, have aliases I<ln>, I<cosec>, I<cotan>,
+I<acosec>, I<acotan>, I<cosech>, I<cotanh>, I<acosech>, I<acotanh>,
+respectively.
+
+The I<root> function is available to compute all the I<n>
 roots of some complex, where I<n> is a strictly positive integer.
 There are exactly I<n> such roots, returned as a list. Getting the
 number mathematicians call C<j> such that:
@@ -990,10 +1333,11 @@ The I<k>th root for C<z = [r,t]> is given by:
 
        (root(z, n))[k] = r**(1/n) * exp(i * (t + 2*k*pi)/n)
 
-The I<spaceshift> operation is also defined. In order to ensure its
-restriction to real numbers is conform to what you would expect, the
-comparison is run on the real part of the complex number first,
-and imaginary parts are compared only when the real parts match. 
+The I<spaceship> comparison operator, E<lt>=E<gt>, is also defined. In
+order to ensure its restriction to real numbers is conform to what you
+would expect, the comparison is run on the real part of the complex
+number first, and imaginary parts are compared only when the real
+parts match.
 
 =head1 CREATION
 
@@ -1011,9 +1355,9 @@ if you like. To create a number using the trigonometric form, use either:
        $z = Math::Complex->emake(5, pi/3);
        $x = cplxe(5, pi/3);
 
-instead. The first argument is the modulus, the second is the angle (in radians).
-(Mnmemonic: C<e> is used as a notation for complex numbers in the trigonometric
-form).
+instead. The first argument is the modulus, the second is the angle
+(in radians, the full circle is 2*pi).  (Mnmemonic: C<e> is used as a
+notation for complex numbers in the trigonometric form).
 
 It is possible to write:
 
@@ -1071,10 +1415,47 @@ Here are some examples:
        $k = exp(i * 2*pi/3);
        print "$j - $k = ", $j - $k, "\n";
 
+=head1 ERRORS DUE TO DIVISION BY ZERO
+
+The division (/) and the following functions
+
+       tan
+       sec
+       csc
+       cot
+       asec
+       acsc
+       atan
+       acot
+       tanh
+       sech
+       csch
+       coth
+       atanh
+       asech
+       acsch
+       acoth
+
+cannot be computed for all arguments because that would mean dividing
+by zero. These situations cause fatal runtime errors looking like this
+
+       cot(0): Division by zero.
+       (Because in the definition of cot(0), the divisor sin(0) is 0)
+       Died at ...
+
+For the C<csc>, C<cot>, C<asec>, C<acsc>, C<csch>, C<coth>, C<asech>,
+C<acsch>, the argument cannot be C<0> (zero). For the C<atanh>,
+C<acoth>, the argument cannot be C<1> (one). For the C<atan>, C<acot>,
+the argument cannot be C<i> (the imaginary unit).  For the C<tan>,
+C<sec>, C<tanh>, C<sech>, the argument cannot be I<pi/2 + k * pi>, where
+I<k> is any integer.
+
 =head1 BUGS
 
-Saying C<use Math::Complex;> exports many mathematical routines in the caller
-environment.  This is construed as a feature by the Author, actually... ;-)
+Saying C<use Math::Complex;> exports many mathematical routines in the
+caller environment and even overrides some (C<sin>, C<cos>, C<sqrt>,
+C<log>, C<exp>).  This is construed as a feature by the Authors,
+actually... ;-)
 
 The code is not optimized for speed, although we try to use the cartesian
 form for addition-like operators and the trigonometric form for all
@@ -1088,6 +1469,11 @@ All routines expect to be given real or complex numbers. Don't attempt to
 use BigFloat, since Perl has currently no rule to disambiguate a '+'
 operation (for instance) between two overloaded entities.
 
-=head1 AUTHOR
+=head1 AUTHORS
+
+Raphael Manfredi <F<Raphael_Manfredi@grenoble.hp.com>> and
+Jarkko Hietaniemi <F<jhi@iki.fi>>.
+
+=cut
 
-Raphael Manfredi <F<Raphael_Manfredi@grenoble.hp.com>>
+# eof