# $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,
- '/' => \÷,
+ '+' => \&plus,
+ '-' => \&minus,
+ '*' => \&multiply,
+ '/' => \÷,
'**' => \&power,
'<=>' => \&spaceship,
'neg' => \&negate,
- '~' => \&conjugate,
+ '~' => \&conjugate,
'abs' => \&abs,
'sqrt' => \&sqrt,
'exp' => \&exp,
'log' => \&log,
'sin' => \&sin,
'cos' => \&cos,
+ 'tan' => \&tan,
'atan2' => \&atan2,
qw("" stringify);
#
sub cplx {
my ($re, $im) = @_;
- return $package->make($re, $im);
+ return $package->make($re, defined $im ? $im : 0);
}
#
#
sub cplxe {
my ($rho, $theta) = @_;
- return $package->emake($rho, $theta);
+ return $package->emake($rho, defined $theta ? $theta : 0);
}
#
#
# 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
# The number defined as i*i = -1;
#
sub i () {
- $i = bless {} unless $i; # There can be only one i
+ return $i if ($i);
+ $i = bless {};
$i->{'cartesian'} = [0, 1];
- $i->{'polar'} = [1, pi/2];
+ $i->{'polar'} = [1, pi/2];
$i->{c_dirty} = 0;
$i->{p_dirty} = 0;
return $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 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;
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;
return $inverted ?
(ref $z1)->make($re2 - $re1, $im2 - $im1) :
(ref $z1)->make($re1 - $re2, $im1 - $im2);
+
}
#
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;
}
#
+# _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.
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;
}
#
#
sub power {
my ($z1, $z2, $inverted) = @_;
- return exp($z1 * log $z2) if defined $inverted && $inverted;
- return exp($z2 * log $z1);
+ _zerotozero if ($z1 == 0 and $z2 == 0);
+ return 1 if ($z1 == 1);
+ return 0 if ($z2 == 0);
+ $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);
}
#
#
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);
#
sub abs {
my ($z) = @_;
+ return abs($z) unless ref $z;
my ($r, $t) = @{$z->polar};
return abs($r);
}
#
sub arg {
my ($z) = @_;
- return 0 unless ref $z;
+ return ($z < 0 ? pi : 0) unless ref $z;
my ($r, $t) = @{$z->polar};
return $t;
}
#
# (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);
}
#
# 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.
#
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;
#
sub exp {
my ($z) = @_;
+ $z = cplx($z, 0) unless ref $z;
my ($x, $y) = @{$z->cartesian};
return (ref $z)->emake(exp($x), $y);
}
#
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);
}
#
#
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;
}
#
#
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);
}
#
#
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);
}
#
#
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 cotan(z) = 1 / tan(z).
+# Computes the secant sec(z) = 1 / cos(z).
#
-sub cotan {
+sub sec {
my ($z) = @_;
- return cos($z) / sin($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 cot(z) = 1 / tan(z).
+#
+sub cot {
+ my ($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));
}
#
#
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 ($ex + $ex_1)/2 if $real;
+ return (ref $z)->make(cos($y) * ($ex + $ex_1)/2,
+ sin($y) * ($ex - $ex_1)/2);
}
#
#
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 ($ex - $ex_1)/2 if $real;
+ return (ref $z)->make(cos($y) * ($ex - $ex_1)/2,
+ sin($y) * ($ex + $ex_1)/2);
}
#
#
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
+#
+# Computes the hyperbolic cosecant csch(z) = 1 / sinh(z).
+#
+sub csch {
+ my ($z) = @_;
+ my $sz = sinh($z);
+ _divbyzero "csch($z)", "sinh($z)" if ($sz == 0);
+ return 1 / $sz;
+}
+
#
-# Comptutes the hyperbolic cotangent cotanh(z) = cosh(z) / sinh(z).
+# cosech
#
-sub cotanh {
+# Alias for csch().
+#
+sub cosech { Math::Complex::csch(@_) }
+
+#
+# coth
+#
+# Computes the hyperbolic cotangent coth(z) = cosh(z) / sinh(z).
+#
+sub coth {
my ($z) = @_;
- return cosh($z) / sinh($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));
}
#
#
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));
}
#
#
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().
#
-# Computes the arc hyperbolic cotangent acotanh(z) = 1/2 log((1+z) / (z-1)).
+sub acosech { Math::Complex::acsch(@_) }
+
+#
+# acoth
+#
+# Computes the arc hyperbolic cotangent acoth(z) = 1/2 log((1+z) / (z-1)).
#
-sub acotanh {
+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;
if (ref $self) { # Called as a method
$format = shift;
- } else { # Regular procedure call
+ } else { # Regular procedure call
$format = $self;
undef $self;
}
elsif ($y == -1) { $im = '-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/\+-/-/;
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' }
if (defined $theta) {
- $r = int($r + ($r < 0 ? -1 : 1) * 1e-14)
- if int(abs($r)) != int(abs($r) + 1e-14);
- $theta = int($theta + ($theta < 0 ? -1 : 1) * 1e-14)
- if int(abs($theta)) != int(abs($theta) + 1e-14);
+ $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\]";
}
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) * 1e-14)
- if int(abs($r)) != int(abs($r) + 1e-14);
- $theta = int($theta + ($theta < 0 ? -1 : 1) * 1e-14)
- if int(abs($theta)) != int(abs($theta) + 1e-14);
+ $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\]";
}
=head1 SYNOPSIS
use Math::Complex;
+
$z = Math::Complex->make(5, 6);
$t = 4 - 3*i + $z;
$j = cplxe(1, 2*pi/3);
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:
(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 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
$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:
$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
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>>
+ Jarkko Hietaniemi <F<jhi@iki.fi>>
+
+=cut
-Raphael Manfredi <F<Raphael_Manfredi@grenoble.hp.com>>
+# eof