#
# Complex numbers and associated mathematical functions
# -- Raphael Manfredi, September 1996
-# -- Jarkko Hietaniemi, March 1997
+# -- Jarkko Hietaniemi, March-April 1997
require Exporter;
-package Math::Complex; @ISA = qw(Exporter);
+package Math::Complex;
use strict;
-use vars qw(@EXPORT $package $display
- $pi $i $ilog10 $logn %logn);
-
-@EXPORT = qw(
- pi i Re Im arg
- sqrt exp log ln
- log10 logn cbrt root
- tan
- cosec csc sec cotan cot
- asin acos atan
- acosec acsc asec acotan acot
- sinh cosh tanh
- cosech csch sech cotanh coth
- asinh acosh atanh
- acosech acsch asech acotanh acoth
- cplx cplxe
+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
#
# 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;
#
sub plus {
my ($z1, $z2, $regular) = @_;
- $z2 = cplx($z2, 0) unless ref $z2;
my ($re1, $im1) = @{$z1->cartesian};
- my ($re2, $im2) = @{$z2->cartesian};
+ 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) = @_;
- $z2 = cplx($z2, 0) unless ref $z2;
my ($re1, $im1) = @{$z1->cartesian};
- my ($re2, $im2) = @{$z2->cartesian};
+ my ($re2, $im2) = ref $z2 ? @{$z2->cartesian} : ($z2, 0);
unless (defined $inverted) {
$z1->set_cartesian([$re1 - $re2, $im1 - $im2]);
return $z1;
# Die on division by zero.
#
sub divbyzero {
- warn $package . '::' . "$_[0]: Division by zero.\n";
- warn "(Because in the definition of $_[0], $_[1] is 0)\n"
- if (defined $_[1]);
+ 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);
- my $dmess = "Died at $up[1] line $up[2].\n";
- die $dmess;
+
+ $mess .= "Died at $up[1] line $up[2].\n";
+
+ die $mess;
}
#
#
sub spaceship {
my ($z1, $z2, $inverted) = @_;
- $z2 = cplx($z2, 0) unless ref $z2;
- my ($re1, $im1) = @{$z1->cartesian};
- my ($re2, $im2) = @{$z2->cartesian};
+ 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 log {
my ($z) = @_;
$z = cplx($z, 0) unless ref $z;
- my ($r, $t) = @{$z->polar};
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);
#
# Compute log10(z).
#
+
sub log10 {
my ($z) = @_;
- my $ilog10 = 1 / log(10) unless defined $ilog10;
- return log(cplx($z, 0)) * $ilog10 unless ref $z;
+
+ return log(cplx($z, 0)) * log10inv unless ref $z;
my ($r, $t) = @{$z->polar};
- return (ref $z)->make(log($r) * $ilog10, $t * $ilog10);
+ return (ref $z)->make(log($r) * log10inv, $t * log10inv);
}
#
#
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;
#
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;
#
sub atan {
my ($z) = @_;
+ $z = cplx($z, 0) unless ref $z;
divbyzero "atan($z)", "i - $z" if ($z == i);
return i/2*log((i + $z) / (i - $z));
}
#
sub asec {
my ($z) = @_;
+ divbyzero "asec($z)", $z if ($z == 0);
return acos(1 / $z);
}
#
-# acosec
+# acsc
#
# Computes the arc cosecant sec(z) = asin(1 / z).
#
-sub acosec {
+sub acsc {
my ($z) = @_;
+ divbyzero "acsc($z)", $z if ($z == 0);
return asin(1 / $z);
}
#
-# acsc
+# acosec
#
-# Alias for acosec().
+# Alias for acsc().
#
-sub acsc { Math::Complex::acosec(@_) }
+sub acosec { Math::Complex::acsc(@_) }
#
# acot
#
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));
}
#
sub cosh {
my ($z) = @_;
- $z = cplx($z, 0) unless ref $z;
- my ($x, $y) = @{$z->cartesian};
+ my ($x, $y) = ref $z ? @{$z->cartesian} : ($z, 0);
my $ex = exp($x);
my $ex_1 = 1 / $ex;
return ($ex + $ex_1)/2 unless ref $z;
#
sub sinh {
my ($z) = @_;
- $z = cplx($z, 0) unless ref $z;
- my ($x, $y) = @{$z->cartesian};
+ my ($x, $y) = ref $z ? @{$z->cartesian} : ($z, 0);
my $ex = exp($x);
my $ex_1 = 1 / $ex;
return ($ex - $ex_1)/2 unless ref $z;
#
sub acosh {
my ($z) = @_;
- $z = cplx($z, 0) unless ref $z; # asinh(-2)
+ $z = cplx($z, 0) unless ref $z;
return log($z + sqrt($z*$z - 1));
}
#
sub asinh {
my ($z) = @_;
- $z = cplx($z, 0) unless ref $z; # asinh(-2)
+ $z = cplx($z, 0) unless ref $z;
return log($z + sqrt($z*$z + 1));
}
#
sub atanh {
my ($z) = @_;
- $z = cplx($z, 0) unless ref $z; # atanh(-2)
divbyzero 'atanh(1)', "1 - $z" if ($z == 1);
+ $z = cplx($z, 0) unless ref $z;
my $cz = (1 + $z) / (1 - $z);
return log($cz) / 2;
}
#
sub acoth {
my ($z) = @_;
- $z = cplx($z, 0) unless ref $z; # acoth(-2)
divbyzero 'acoth(1)', "$z - 1" if ($z == 1);
+ $z = cplx($z, 0) unless ref $z;
my $cz = (1 + $z) / ($z - 1);
return log($cz) / 2;
}
#
sub atan2 {
my ($z1, $z2, $inverted) = @_;
- my ($re1, $im1) = @{$z1->cartesian};
- my ($re2, $im2) = @{$z2->cartesian};
+ 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;
=head1 SYNOPSIS
use Math::Complex;
+
$z = Math::Complex->make(5, 6);
$t = 4 - 3*i + $z;
$j = cplxe(1, 2*pi/3);
tan(z) = sin(z) / cos(z)
- csc(z) = 1 / sin(z)
- sec(z) = 1 / cos(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))
- acsc(z) = asin(1 / z)
- asec(z) = acos(1 / z)
+ 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) = (exp(z) - exp(-z)) / (exp(z) + exp(-z))
- csch(z) = 1 / sinh(z)
- sech(z) = 1 / cosh(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))
- acsch(z) = asinh(1 / z)
- asech(z) = acosh(1 / z)
+ 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>,
$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
Raphael Manfredi <F<Raphael_Manfredi@grenoble.hp.com>>
Jarkko Hietaniemi <F<jhi@iki.fi>>
+
+=cut
+
+# eof