2 # Complex numbers and associated mathematical functions
3 # -- Raphael Manfredi Since Sep 1996
4 # -- Jarkko Hietaniemi Since Mar 1997
5 # -- Daniel S. Lewart Since Sep 1997
10 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $Inf $ExpInf);
19 4 => '1.70141183460469229e+38',
20 8 => '1.7976931348623157e+308',
21 # AFAICT the 10, 12, and 16-byte long doubles
22 # all have the same maximum.
23 10 => '1.1897314953572317650857593266280070162E+4932',
24 12 => '1.1897314953572317650857593266280070162E+4932',
25 16 => '1.1897314953572317650857593266280070162E+4932',
27 my $nvsize = $Config{nvsize} ||
28 ($Config{uselongdouble} && $Config{longdblsize}) ||
30 die "Math::Complex: Could not figure out nvsize\n"
31 unless defined $nvsize;
32 die "Math::Complex: Cannot not figure out max nv (nvsize = $nvsize)\n"
33 unless defined $DBL_MAX{$nvsize};
34 my $DBL_MAX = eval $DBL_MAX{$nvsize};
35 die "Math::Complex: Could not figure out max nv (nvsize = $nvsize)\n"
36 unless defined $DBL_MAX;
37 my $BIGGER_THAN_THIS = 1e30; # Must find something bigger than this.
38 if ($^O eq 'unicosmk') {
41 local $SIG{FPE} = { };
43 # We do want an arithmetic overflow, Inf INF inf Infinity.
45 'exp(99999)', # Enough even with 128-bit long doubles.
55 my $i = eval "$t+1.0";
56 if (defined $i && $i > $BIGGER_THAN_THIS) {
61 $Inf = $DBL_MAX unless defined $Inf; # Oh well, close enough.
62 die "Math::Complex: Could not get Infinity"
63 unless $Inf > $BIGGER_THAN_THIS;
66 # print "# On this machine, Inf = '$Inf'\n";
74 # Regular expression for floating point numbers.
75 # These days we could use Scalar::Util::lln(), I guess.
76 my $gre = qr'\s*([\+\-]?(?:(?:(?:\d+(?:_\d+)*(?:\.\d*(?:_\d+)*)?|\.\d+(?:_\d+)*)(?:[eE][\+\-]?\d+(?:_\d+)*)?))|inf)'i;
85 csc cosec sec cot cotan
87 acsc acosec asec acot acotan
89 csch cosech sech coth cotanh
91 acsch acosech asech acoth acotanh
103 my @pi = qw(pi pi2 pi4 pip2 pip4 Inf);
119 '<=>' => \&_spaceship,
130 '""' => \&_stringify;
136 my %DISPLAY_FORMAT = ('style' => 'cartesian',
137 'polar_pretty_print' => 1);
138 my $eps = 1e-14; # Epsilon
141 # Object attributes (internal):
142 # cartesian [real, imaginary] -- cartesian form
143 # polar [rho, theta] -- polar form
144 # c_dirty cartesian form not up-to-date
145 # p_dirty polar form not up-to-date
146 # display display format (package's global when not set)
149 # Die on bad *make() arguments.
152 die "@{[(caller(1))[3]]}: Cannot take $_[0] of '$_[1]'.\n";
159 if ($arg =~ /^$gre$/) {
161 } elsif ($arg =~ /^(?:$gre)?$gre\s*i\s*$/) {
162 ($p, $q) = ($1 || 0, $2);
163 } elsif ($arg =~ /^\s*\(\s*$gre\s*(?:,\s*$gre\s*)?\)\s*$/) {
164 ($p, $q) = ($1, $2 || 0);
169 $p =~ s/^(-?)inf$/"${1}9**9**9"/e;
171 $q =~ s/^(-?)inf$/"${1}9**9**9"/e;
181 if ($arg =~ /^\s*\[\s*$gre\s*(?:,\s*$gre\s*)?\]\s*$/) {
182 ($p, $q) = ($1, $2 || 0);
183 } elsif ($arg =~ m!^\s*\[\s*$gre\s*(?:,\s*([-+]?\d*\s*)?pi(?:/\s*(\d+))?\s*)?\]\s*$!) {
184 ($p, $q) = ($1, ($2 eq '-' ? -1 : ($2 || 1)) * pi() / ($3 || 1));
185 } elsif ($arg =~ /^\s*\[\s*$gre\s*\]\s*$/) {
187 } elsif ($arg =~ /^\s*$gre\s*$/) {
194 $p =~ s/^(-?)inf$/"${1}9**9**9"/e;
195 $q =~ s/^(-?)inf$/"${1}9**9**9"/e;
204 # Create a new complex number (cartesian form)
207 my $self = bless {}, shift;
212 return (ref $self)->emake($_[0])
213 if ($_[0] =~ /^\s*\[/);
214 ($re, $im) = _make($_[0]);
219 _cannot_make("real part", $re) unless $re =~ /^$gre$/;
222 _cannot_make("imaginary part", $im) unless $im =~ /^$gre$/;
223 $self->_set_cartesian([$re, $im ]);
224 $self->display_format('cartesian');
232 # Create a new complex number (exponential form)
235 my $self = bless {}, shift;
238 ($rho, $theta) = (0, 0);
240 return (ref $self)->make($_[0])
241 if ($_[0] =~ /^\s*\(/ || $_[0] =~ /i\s*$/);
242 ($rho, $theta) = _emake($_[0]);
246 if (defined $rho && defined $theta) {
249 $theta = ($theta <= 0) ? $theta + pi() : $theta - pi();
253 _cannot_make("rho", $rho) unless $rho =~ /^$gre$/;
256 _cannot_make("theta", $theta) unless $theta =~ /^$gre$/;
257 $self->_set_polar([$rho, $theta]);
258 $self->display_format('polar');
263 sub new { &make } # For backward compatibility only.
268 # Creates a complex number from a (re, im) tuple.
269 # This avoids the burden of writing Math::Complex->make(re, im).
272 return __PACKAGE__->make(@_);
278 # Creates a complex number from a (rho, theta) tuple.
279 # This avoids the burden of writing Math::Complex->emake(rho, theta).
282 return __PACKAGE__->emake(@_);
288 # The number defined as pi = 180 degrees
290 sub pi () { 4 * CORE::atan2(1, 1) }
297 sub pi2 () { 2 * pi }
302 # The full circle twice.
304 sub pi4 () { 4 * pi }
311 sub pip2 () { pi / 2 }
318 sub pip4 () { pi / 4 }
325 sub _uplog10 () { 1 / CORE::log(10) }
330 # The number defined as i*i = -1;
335 $i->{'cartesian'} = [0, 1];
336 $i->{'polar'} = [1, pip2];
347 sub _ip2 () { i / 2 }
350 # Attribute access/set routines
353 sub _cartesian {$_[0]->{c_dirty} ?
354 $_[0]->_update_cartesian : $_[0]->{'cartesian'}}
355 sub _polar {$_[0]->{p_dirty} ?
356 $_[0]->_update_polar : $_[0]->{'polar'}}
358 sub _set_cartesian { $_[0]->{p_dirty}++; $_[0]->{c_dirty} = 0;
359 $_[0]->{'cartesian'} = $_[1] }
360 sub _set_polar { $_[0]->{c_dirty}++; $_[0]->{p_dirty} = 0;
361 $_[0]->{'polar'} = $_[1] }
364 # ->_update_cartesian
366 # Recompute and return the cartesian form, given accurate polar form.
368 sub _update_cartesian {
370 my ($r, $t) = @{$self->{'polar'}};
371 $self->{c_dirty} = 0;
372 return $self->{'cartesian'} = [$r * CORE::cos($t), $r * CORE::sin($t)];
379 # Recompute and return the polar form, given accurate cartesian form.
383 my ($x, $y) = @{$self->{'cartesian'}};
384 $self->{p_dirty} = 0;
385 return $self->{'polar'} = [0, 0] if $x == 0 && $y == 0;
386 return $self->{'polar'} = [CORE::sqrt($x*$x + $y*$y),
387 CORE::atan2($y, $x)];
396 my ($z1, $z2, $regular) = @_;
397 my ($re1, $im1) = @{$z1->_cartesian};
398 $z2 = cplx($z2) unless ref $z2;
399 my ($re2, $im2) = ref $z2 ? @{$z2->_cartesian} : ($z2, 0);
400 unless (defined $regular) {
401 $z1->_set_cartesian([$re1 + $re2, $im1 + $im2]);
404 return (ref $z1)->make($re1 + $re2, $im1 + $im2);
413 my ($z1, $z2, $inverted) = @_;
414 my ($re1, $im1) = @{$z1->_cartesian};
415 $z2 = cplx($z2) unless ref $z2;
416 my ($re2, $im2) = @{$z2->_cartesian};
417 unless (defined $inverted) {
418 $z1->_set_cartesian([$re1 - $re2, $im1 - $im2]);
422 (ref $z1)->make($re2 - $re1, $im2 - $im1) :
423 (ref $z1)->make($re1 - $re2, $im1 - $im2);
433 my ($z1, $z2, $regular) = @_;
434 if ($z1->{p_dirty} == 0 and ref $z2 and $z2->{p_dirty} == 0) {
435 # if both polar better use polar to avoid rounding errors
436 my ($r1, $t1) = @{$z1->_polar};
437 my ($r2, $t2) = @{$z2->_polar};
439 if ($t > pi()) { $t -= pi2 }
440 elsif ($t <= -pi()) { $t += pi2 }
441 unless (defined $regular) {
442 $z1->_set_polar([$r1 * $r2, $t]);
445 return (ref $z1)->emake($r1 * $r2, $t);
447 my ($x1, $y1) = @{$z1->_cartesian};
449 my ($x2, $y2) = @{$z2->_cartesian};
450 return (ref $z1)->make($x1*$x2-$y1*$y2, $x1*$y2+$y1*$x2);
452 return (ref $z1)->make($x1*$z2, $y1*$z2);
460 # Die on division by zero.
463 my $mess = "$_[0]: Division by zero.\n";
466 $mess .= "(Because in the definition of $_[0], the divisor ";
467 $mess .= "$_[1] " unless ("$_[1]" eq '0');
473 $mess .= "Died at $up[1] line $up[2].\n";
484 my ($z1, $z2, $inverted) = @_;
485 if ($z1->{p_dirty} == 0 and ref $z2 and $z2->{p_dirty} == 0) {
486 # if both polar better use polar to avoid rounding errors
487 my ($r1, $t1) = @{$z1->_polar};
488 my ($r2, $t2) = @{$z2->_polar};
491 _divbyzero "$z2/0" if ($r1 == 0);
493 if ($t > pi()) { $t -= pi2 }
494 elsif ($t <= -pi()) { $t += pi2 }
495 return (ref $z1)->emake($r2 / $r1, $t);
497 _divbyzero "$z1/0" if ($r2 == 0);
499 if ($t > pi()) { $t -= pi2 }
500 elsif ($t <= -pi()) { $t += pi2 }
501 return (ref $z1)->emake($r1 / $r2, $t);
506 ($x2, $y2) = @{$z1->_cartesian};
507 $d = $x2*$x2 + $y2*$y2;
508 _divbyzero "$z2/0" if $d == 0;
509 return (ref $z1)->make(($x2*$z2)/$d, -($y2*$z2)/$d);
511 my ($x1, $y1) = @{$z1->_cartesian};
513 ($x2, $y2) = @{$z2->_cartesian};
514 $d = $x2*$x2 + $y2*$y2;
515 _divbyzero "$z1/0" if $d == 0;
516 my $u = ($x1*$x2 + $y1*$y2)/$d;
517 my $v = ($y1*$x2 - $x1*$y2)/$d;
518 return (ref $z1)->make($u, $v);
520 _divbyzero "$z1/0" if $z2 == 0;
521 return (ref $z1)->make($x1/$z2, $y1/$z2);
530 # Computes z1**z2 = exp(z2 * log z1)).
533 my ($z1, $z2, $inverted) = @_;
535 return 1 if $z1 == 0 || $z2 == 1;
536 return 0 if $z2 == 0 && Re($z1) > 0;
538 return 1 if $z2 == 0 || $z1 == 1;
539 return 0 if $z1 == 0 && Re($z2) > 0;
541 my $w = $inverted ? &exp($z1 * &log($z2))
542 : &exp($z2 * &log($z1));
543 # If both arguments cartesian, return cartesian, else polar.
544 return $z1->{c_dirty} == 0 &&
545 (not ref $z2 or $z2->{c_dirty} == 0) ?
546 cplx(@{$w->_cartesian}) : $w;
552 # Computes z1 <=> z2.
553 # Sorts on the real part first, then on the imaginary part. Thus 2-4i < 3+8i.
556 my ($z1, $z2, $inverted) = @_;
557 my ($re1, $im1) = ref $z1 ? @{$z1->_cartesian} : ($z1, 0);
558 my ($re2, $im2) = ref $z2 ? @{$z2->_cartesian} : ($z2, 0);
559 my $sgn = $inverted ? -1 : 1;
560 return $sgn * ($re1 <=> $re2) if $re1 != $re2;
561 return $sgn * ($im1 <=> $im2);
569 # (Required in addition to _spaceship() because of NaNs.)
571 my ($z1, $z2, $inverted) = @_;
572 my ($re1, $im1) = ref $z1 ? @{$z1->_cartesian} : ($z1, 0);
573 my ($re2, $im2) = ref $z2 ? @{$z2->_cartesian} : ($z2, 0);
574 return $re1 == $re2 && $im1 == $im2 ? 1 : 0;
585 my ($r, $t) = @{$z->_polar};
586 $t = ($t <= 0) ? $t + pi : $t - pi;
587 return (ref $z)->emake($r, $t);
589 my ($re, $im) = @{$z->_cartesian};
590 return (ref $z)->make(-$re, -$im);
596 # Compute complex's _conjugate.
601 my ($r, $t) = @{$z->_polar};
602 return (ref $z)->emake($r, -$t);
604 my ($re, $im) = @{$z->_cartesian};
605 return (ref $z)->make($re, -$im);
611 # Compute or set complex's norm (rho).
619 return CORE::abs($z);
623 $z->{'polar'} = [ $rho, ${$z->_polar}[1] ];
628 return ${$z->_polar}[0];
635 if ($$theta > pi()) { $$theta -= pi2 }
636 elsif ($$theta <= -pi()) { $$theta += pi2 }
642 # Compute or set complex's argument (theta).
645 my ($z, $theta) = @_;
646 return $z unless ref $z;
647 if (defined $theta) {
649 $z->{'polar'} = [ ${$z->_polar}[0], $theta ];
653 $theta = ${$z->_polar}[1];
664 # It is quite tempting to use wantarray here so that in list context
665 # sqrt() would return the two solutions. This, however, would
668 # print "sqrt(z) = ", sqrt($z), "\n";
670 # The two values would be printed side by side without no intervening
671 # whitespace, quite confusing.
672 # Therefore if you want the two solutions use the root().
676 my ($re, $im) = ref $z ? @{$z->_cartesian} : ($z, 0);
677 return $re < 0 ? cplx(0, CORE::sqrt(-$re)) : CORE::sqrt($re)
679 my ($r, $t) = @{$z->_polar};
680 return (ref $z)->emake(CORE::sqrt($r), $t/2);
686 # Compute cbrt(z) (cubic root).
688 # Why are we not returning three values? The same answer as for sqrt().
693 -CORE::exp(CORE::log(-$z)/3) :
694 ($z > 0 ? CORE::exp(CORE::log($z)/3): 0)
696 my ($r, $t) = @{$z->_polar};
698 return (ref $z)->emake(CORE::exp(CORE::log($r)/3), $t/3);
707 my $mess = "Root '$_[0]' illegal, root rank must be positive integer.\n";
711 $mess .= "Died at $up[1] line $up[2].\n";
719 # Computes all nth root for z, returning an array whose size is n.
720 # `n' must be a positive integer.
722 # The roots are given by (for k = 0..n-1):
724 # z^(1/n) = r^(1/n) (cos ((t+2 k pi)/n) + i sin ((t+2 k pi)/n))
727 my ($z, $n, $k) = @_;
728 _rootbad($n) if ($n < 1 or int($n) != $n);
729 my ($r, $t) = ref $z ?
730 @{$z->_polar} : (CORE::abs($z), $z >= 0 ? 0 : pi);
731 my $theta_inc = pi2 / $n;
732 my $rho = $r ** (1/$n);
733 my $cartesian = ref $z && $z->{c_dirty} == 0;
736 for (my $i = 0, my $theta = $t / $n;
738 $i++, $theta += $theta_inc) {
739 my $w = cplxe($rho, $theta);
740 # Yes, $cartesian is loop invariant.
741 push @root, $cartesian ? cplx(@{$w->_cartesian}) : $w;
745 my $w = cplxe($rho, $t / $n + $k * $theta_inc);
746 return $cartesian ? cplx(@{$w->_cartesian}) : $w;
753 # Return or set Re(z).
757 return $z unless ref $z;
759 $z->{'cartesian'} = [ $Re, ${$z->_cartesian}[1] ];
763 return ${$z->_cartesian}[0];
770 # Return or set Im(z).
774 return 0 unless ref $z;
776 $z->{'cartesian'} = [ ${$z->_cartesian}[0], $Im ];
780 return ${$z->_cartesian}[1];
787 # Return or set rho(w).
790 Math::Complex::abs(@_);
796 # Return or set theta(w).
799 Math::Complex::arg(@_);
809 my ($x, $y) = @{$z->_cartesian};
810 return (ref $z)->emake(CORE::exp($x), $y);
816 # Die on logarithm of zero.
819 my $mess = "$_[0]: Logarithm of zero.\n";
822 $mess .= "(Because in the definition of $_[0], the argument ";
823 $mess .= "$_[1] " unless ($_[1] eq '0');
829 $mess .= "Died at $up[1] line $up[2].\n";
842 _logofzero("log") if $z == 0;
843 return $z > 0 ? CORE::log($z) : cplx(CORE::log(-$z), pi);
845 my ($r, $t) = @{$z->_polar};
846 _logofzero("log") if $r == 0;
847 if ($t > pi()) { $t -= pi2 }
848 elsif ($t <= -pi()) { $t += pi2 }
849 return (ref $z)->make(CORE::log($r), $t);
857 sub ln { Math::Complex::log(@_) }
866 return Math::Complex::log($_[0]) * _uplog10;
872 # Compute logn(z,n) = log(z) / log(n)
876 $z = cplx($z, 0) unless ref $z;
877 my $logn = $LOGN{$n};
878 $logn = $LOGN{$n} = CORE::log($n) unless defined $logn; # Cache log(n)
879 return &log($z) / $logn;
885 # Compute cos(z) = (exp(iz) + exp(-iz))/2.
889 return CORE::cos($z) unless ref $z;
890 my ($x, $y) = @{$z->_cartesian};
891 my $ey = CORE::exp($y);
892 my $sx = CORE::sin($x);
893 my $cx = CORE::cos($x);
894 my $ey_1 = $ey ? 1 / $ey : Inf();
895 return (ref $z)->make($cx * ($ey + $ey_1)/2,
896 $sx * ($ey_1 - $ey)/2);
902 # Compute sin(z) = (exp(iz) - exp(-iz))/2.
906 return CORE::sin($z) unless ref $z;
907 my ($x, $y) = @{$z->_cartesian};
908 my $ey = CORE::exp($y);
909 my $sx = CORE::sin($x);
910 my $cx = CORE::cos($x);
911 my $ey_1 = $ey ? 1 / $ey : Inf();
912 return (ref $z)->make($sx * ($ey + $ey_1)/2,
913 $cx * ($ey - $ey_1)/2);
919 # Compute tan(z) = sin(z) / cos(z).
924 _divbyzero "tan($z)", "cos($z)" if $cz == 0;
925 return &sin($z) / $cz;
931 # Computes the secant sec(z) = 1 / cos(z).
936 _divbyzero "sec($z)", "cos($z)" if ($cz == 0);
943 # Computes the cosecant csc(z) = 1 / sin(z).
948 _divbyzero "csc($z)", "sin($z)" if ($sz == 0);
957 sub cosec { Math::Complex::csc(@_) }
962 # Computes cot(z) = cos(z) / sin(z).
967 _divbyzero "cot($z)", "sin($z)" if ($sz == 0);
968 return &cos($z) / $sz;
976 sub cotan { Math::Complex::cot(@_) }
981 # Computes the arc cosine acos(z) = -i log(z + sqrt(z*z-1)).
985 return CORE::atan2(CORE::sqrt(1-$z*$z), $z)
986 if (! ref $z) && CORE::abs($z) <= 1;
987 $z = cplx($z, 0) unless ref $z;
988 my ($x, $y) = @{$z->_cartesian};
989 return 0 if $x == 1 && $y == 0;
990 my $t1 = CORE::sqrt(($x+1)*($x+1) + $y*$y);
991 my $t2 = CORE::sqrt(($x-1)*($x-1) + $y*$y);
992 my $alpha = ($t1 + $t2)/2;
993 my $beta = ($t1 - $t2)/2;
994 $alpha = 1 if $alpha < 1;
995 if ($beta > 1) { $beta = 1 }
996 elsif ($beta < -1) { $beta = -1 }
997 my $u = CORE::atan2(CORE::sqrt(1-$beta*$beta), $beta);
998 my $v = CORE::log($alpha + CORE::sqrt($alpha*$alpha-1));
999 $v = -$v if $y > 0 || ($y == 0 && $x < -1);
1000 return (ref $z)->make($u, $v);
1006 # Computes the arc sine asin(z) = -i log(iz + sqrt(1-z*z)).
1010 return CORE::atan2($z, CORE::sqrt(1-$z*$z))
1011 if (! ref $z) && CORE::abs($z) <= 1;
1012 $z = cplx($z, 0) unless ref $z;
1013 my ($x, $y) = @{$z->_cartesian};
1014 return 0 if $x == 0 && $y == 0;
1015 my $t1 = CORE::sqrt(($x+1)*($x+1) + $y*$y);
1016 my $t2 = CORE::sqrt(($x-1)*($x-1) + $y*$y);
1017 my $alpha = ($t1 + $t2)/2;
1018 my $beta = ($t1 - $t2)/2;
1019 $alpha = 1 if $alpha < 1;
1020 if ($beta > 1) { $beta = 1 }
1021 elsif ($beta < -1) { $beta = -1 }
1022 my $u = CORE::atan2($beta, CORE::sqrt(1-$beta*$beta));
1023 my $v = -CORE::log($alpha + CORE::sqrt($alpha*$alpha-1));
1024 $v = -$v if $y > 0 || ($y == 0 && $x < -1);
1025 return (ref $z)->make($u, $v);
1031 # Computes the arc tangent atan(z) = i/2 log((i+z) / (i-z)).
1035 return CORE::atan2($z, 1) unless ref $z;
1036 my ($x, $y) = ref $z ? @{$z->_cartesian} : ($z, 0);
1037 return 0 if $x == 0 && $y == 0;
1038 _divbyzero "atan(i)" if ( $z == i);
1039 _logofzero "atan(-i)" if (-$z == i); # -i is a bad file test...
1040 my $log = &log((i + $z) / (i - $z));
1047 # Computes the arc secant asec(z) = acos(1 / z).
1051 _divbyzero "asec($z)", $z if ($z == 0);
1052 return acos(1 / $z);
1058 # Computes the arc cosecant acsc(z) = asin(1 / z).
1062 _divbyzero "acsc($z)", $z if ($z == 0);
1063 return asin(1 / $z);
1071 sub acosec { Math::Complex::acsc(@_) }
1076 # Computes the arc cotangent acot(z) = atan(1 / z)
1080 _divbyzero "acot(0)" if $z == 0;
1081 return ($z >= 0) ? CORE::atan2(1, $z) : CORE::atan2(-1, -$z)
1083 _divbyzero "acot(i)" if ($z - i == 0);
1084 _logofzero "acot(-i)" if ($z + i == 0);
1085 return atan(1 / $z);
1093 sub acotan { Math::Complex::acot(@_) }
1098 # Computes the hyperbolic cosine cosh(z) = (exp(z) + exp(-z))/2.
1104 $ex = CORE::exp($z);
1105 return $ex ? ($ex == $ExpInf ? Inf() : ($ex + 1/$ex)/2) : Inf();
1107 my ($x, $y) = @{$z->_cartesian};
1108 $ex = CORE::exp($x);
1109 my $ex_1 = $ex ? 1 / $ex : Inf();
1110 return (ref $z)->make(CORE::cos($y) * ($ex + $ex_1)/2,
1111 CORE::sin($y) * ($ex - $ex_1)/2);
1117 # Computes the hyperbolic sine sinh(z) = (exp(z) - exp(-z))/2.
1123 return 0 if $z == 0;
1124 $ex = CORE::exp($z);
1125 return $ex ? ($ex == $ExpInf ? Inf() : ($ex - 1/$ex)/2) : -Inf();
1127 my ($x, $y) = @{$z->_cartesian};
1128 my $cy = CORE::cos($y);
1129 my $sy = CORE::sin($y);
1130 $ex = CORE::exp($x);
1131 my $ex_1 = $ex ? 1 / $ex : Inf();
1132 return (ref $z)->make(CORE::cos($y) * ($ex - $ex_1)/2,
1133 CORE::sin($y) * ($ex + $ex_1)/2);
1139 # Computes the hyperbolic tangent tanh(z) = sinh(z) / cosh(z).
1144 _divbyzero "tanh($z)", "cosh($z)" if ($cz == 0);
1146 return 1 if $cz == $sz;
1147 return -1 if $cz == -$sz;
1154 # Computes the hyperbolic secant sech(z) = 1 / cosh(z).
1159 _divbyzero "sech($z)", "cosh($z)" if ($cz == 0);
1166 # Computes the hyperbolic cosecant csch(z) = 1 / sinh(z).
1171 _divbyzero "csch($z)", "sinh($z)" if ($sz == 0);
1180 sub cosech { Math::Complex::csch(@_) }
1185 # Computes the hyperbolic cotangent coth(z) = cosh(z) / sinh(z).
1190 _divbyzero "coth($z)", "sinh($z)" if $sz == 0;
1192 return 1 if $cz == $sz;
1193 return -1 if $cz == -$sz;
1202 sub cotanh { Math::Complex::coth(@_) }
1207 # Computes the area/inverse hyperbolic cosine acosh(z) = log(z + sqrt(z*z-1)).
1214 my ($re, $im) = @{$z->_cartesian};
1216 return CORE::log($re + CORE::sqrt($re*$re - 1))
1218 return cplx(0, CORE::atan2(CORE::sqrt(1 - $re*$re), $re))
1219 if CORE::abs($re) < 1;
1221 my $t = &sqrt($z * $z - 1) + $z;
1222 # Try Taylor if looking bad (this usually means that
1223 # $z was large negative, therefore the sqrt is really
1224 # close to abs(z), summing that with z...)
1225 $t = 1/(2 * $z) - 1/(8 * $z**3) + 1/(16 * $z**5) - 5/(128 * $z**7)
1228 $u->Im(-$u->Im) if $re < 0 && $im == 0;
1229 return $re < 0 ? -$u : $u;
1235 # Computes the area/inverse hyperbolic sine asinh(z) = log(z + sqrt(z*z+1))
1240 my $t = $z + CORE::sqrt($z*$z + 1);
1241 return CORE::log($t) if $t;
1243 my $t = &sqrt($z * $z + 1) + $z;
1244 # Try Taylor if looking bad (this usually means that
1245 # $z was large negative, therefore the sqrt is really
1246 # close to abs(z), summing that with z...)
1247 $t = 1/(2 * $z) - 1/(8 * $z**3) + 1/(16 * $z**5) - 5/(128 * $z**7)
1255 # Computes the area/inverse hyperbolic tangent atanh(z) = 1/2 log((1+z) / (1-z)).
1260 return CORE::log((1 + $z)/(1 - $z))/2 if CORE::abs($z) < 1;
1263 _divbyzero 'atanh(1)', "1 - $z" if (1 - $z == 0);
1264 _logofzero 'atanh(-1)' if (1 + $z == 0);
1265 return 0.5 * &log((1 + $z) / (1 - $z));
1271 # Computes the area/inverse hyperbolic secant asech(z) = acosh(1 / z).
1275 _divbyzero 'asech(0)', "$z" if ($z == 0);
1276 return acosh(1 / $z);
1282 # Computes the area/inverse hyperbolic cosecant acsch(z) = asinh(1 / z).
1286 _divbyzero 'acsch(0)', $z if ($z == 0);
1287 return asinh(1 / $z);
1293 # Alias for acosh().
1295 sub acosech { Math::Complex::acsch(@_) }
1300 # Computes the area/inverse hyperbolic cotangent acoth(z) = 1/2 log((1+z) / (z-1)).
1304 _divbyzero 'acoth(0)' if ($z == 0);
1306 return CORE::log(($z + 1)/($z - 1))/2 if CORE::abs($z) > 1;
1309 _divbyzero 'acoth(1)', "$z - 1" if ($z - 1 == 0);
1310 _logofzero 'acoth(-1)', "1 + $z" if (1 + $z == 0);
1311 return &log((1 + $z) / ($z - 1)) / 2;
1319 sub acotanh { Math::Complex::acoth(@_) }
1324 # Compute atan(z1/z2), minding the right quadrant.
1327 my ($z1, $z2, $inverted) = @_;
1328 my ($re1, $im1, $re2, $im2);
1330 ($re1, $im1) = ref $z2 ? @{$z2->_cartesian} : ($z2, 0);
1331 ($re2, $im2) = ref $z1 ? @{$z1->_cartesian} : ($z1, 0);
1333 ($re1, $im1) = ref $z1 ? @{$z1->_cartesian} : ($z1, 0);
1334 ($re2, $im2) = ref $z2 ? @{$z2->_cartesian} : ($z2, 0);
1337 # In MATLAB the imaginary parts are ignored.
1338 # warn "atan2: Imaginary parts ignored";
1339 # http://documents.wolfram.com/mathematica/functions/ArcTan
1340 # NOTE: Mathematica ArcTan[x,y] while atan2(y,x)
1341 my $s = $z1 * $z1 + $z2 * $z2;
1342 _divbyzero("atan2") if $s == 0;
1344 my $r = $z2 + $z1 * $i;
1345 return -$i * &log($r / &sqrt( $s ));
1347 return CORE::atan2($re1, $re2);
1354 # Set (get if no argument) the display format for all complex numbers that
1355 # don't happen to have overridden it via ->display_format
1357 # When called as an object method, this actually sets the display format for
1358 # the current object.
1360 # Valid object formats are 'c' and 'p' for cartesian and polar. The first
1361 # letter is used actually, so the type can be fully spelled out for clarity.
1363 sub display_format {
1365 my %display_format = %DISPLAY_FORMAT;
1367 if (ref $self) { # Called as an object method
1368 if (exists $self->{display_format}) {
1369 my %obj = %{$self->{display_format}};
1370 @display_format{keys %obj} = values %obj;
1374 $display_format{style} = shift;
1377 @display_format{keys %new} = values %new;
1380 if (ref $self) { # Called as an object method
1381 $self->{display_format} = { %display_format };
1384 %{$self->{display_format}} :
1385 $self->{display_format}->{style};
1388 # Called as a class method
1389 %DISPLAY_FORMAT = %display_format;
1393 $DISPLAY_FORMAT{style};
1399 # Show nicely formatted complex number under its cartesian or polar form,
1400 # depending on the current display format:
1402 # . If a specific display format has been recorded for this object, use it.
1403 # . Otherwise, use the generic current default for all complex numbers,
1404 # which is a package global variable.
1409 my $style = $z->display_format;
1411 $style = $DISPLAY_FORMAT{style} unless defined $style;
1413 return $z->_stringify_polar if $style =~ /^p/i;
1414 return $z->_stringify_cartesian;
1418 # ->_stringify_cartesian
1420 # Stringify as a cartesian representation 'a+bi'.
1422 sub _stringify_cartesian {
1424 my ($x, $y) = @{$z->_cartesian};
1427 my %format = $z->display_format;
1428 my $format = $format{format};
1431 if ($x =~ /^NaN[QS]?$/i) {
1434 if ($x =~ /^-?\Q$Inf\E$/oi) {
1437 $re = defined $format ? sprintf($format, $x) : $x;
1445 if ($y =~ /^(NaN[QS]?)$/i) {
1448 if ($y =~ /^-?\Q$Inf\E$/oi) {
1453 sprintf($format, $y) :
1454 ($y == 1 ? "" : ($y == -1 ? "-" : $y));
1467 } elsif ($y > 0 || $im =~ /^NaN[QS]?i$/i) {
1468 $str .= "+" if defined $re;
1471 } elsif (!defined $re) {
1480 # ->_stringify_polar
1482 # Stringify as a polar representation '[r,t]'.
1484 sub _stringify_polar {
1486 my ($r, $t) = @{$z->_polar};
1489 my %format = $z->display_format;
1490 my $format = $format{format};
1492 if ($t =~ /^NaN[QS]?$/i || $t =~ /^-?\Q$Inf\E$/oi) {
1494 } elsif ($t == pi) {
1496 } elsif ($r == 0 || $t == 0) {
1497 $theta = defined $format ? sprintf($format, $t) : $t;
1500 return "[$r,$theta]" if defined $theta;
1503 # Try to identify pi/n and friends.
1506 $t -= int(CORE::abs($t) / pi2) * pi2;
1508 if ($format{polar_pretty_print} && $t) {
1512 if ($b =~ /^-?\d+$/) {
1513 $b = $b < 0 ? "-" : "" if CORE::abs($b) == 1;
1514 $theta = "${b}pi/$a";
1520 if (defined $format) {
1521 $r = sprintf($format, $r);
1522 $theta = sprintf($format, $theta) unless defined $theta;
1524 $theta = $t unless defined $theta;
1527 return "[$r,$theta]";
1541 Math::Complex - complex numbers and associated mathematical functions
1547 $z = Math::Complex->make(5, 6);
1549 $j = cplxe(1, 2*pi/3);
1553 This package lets you create and manipulate complex numbers. By default,
1554 I<Perl> limits itself to real numbers, but an extra C<use> statement brings
1555 full complex support, along with a full set of mathematical functions
1556 typically associated with and/or extended to complex numbers.
1558 If you wonder what complex numbers are, they were invented to be able to solve
1559 the following equation:
1563 and by definition, the solution is noted I<i> (engineers use I<j> instead since
1564 I<i> usually denotes an intensity, but the name does not matter). The number
1565 I<i> is a pure I<imaginary> number.
1567 The arithmetics with pure imaginary numbers works just like you would expect
1568 it with real numbers... you just have to remember that
1574 5i + 7i = i * (5 + 7) = 12i
1575 4i - 3i = i * (4 - 3) = i
1580 Complex numbers are numbers that have both a real part and an imaginary
1581 part, and are usually noted:
1585 where C<a> is the I<real> part and C<b> is the I<imaginary> part. The
1586 arithmetic with complex numbers is straightforward. You have to
1587 keep track of the real and the imaginary parts, but otherwise the
1588 rules used for real numbers just apply:
1590 (4 + 3i) + (5 - 2i) = (4 + 5) + i(3 - 2) = 9 + i
1591 (2 + i) * (4 - i) = 2*4 + 4i -2i -i*i = 8 + 2i + 1 = 9 + 2i
1593 A graphical representation of complex numbers is possible in a plane
1594 (also called the I<complex plane>, but it's really a 2D plane).
1599 is the point whose coordinates are (a, b). Actually, it would
1600 be the vector originating from (0, 0) to (a, b). It follows that the addition
1601 of two complex numbers is a vectorial addition.
1603 Since there is a bijection between a point in the 2D plane and a complex
1604 number (i.e. the mapping is unique and reciprocal), a complex number
1605 can also be uniquely identified with polar coordinates:
1609 where C<rho> is the distance to the origin, and C<theta> the angle between
1610 the vector and the I<x> axis. There is a notation for this using the
1611 exponential form, which is:
1613 rho * exp(i * theta)
1615 where I<i> is the famous imaginary number introduced above. Conversion
1616 between this form and the cartesian form C<a + bi> is immediate:
1618 a = rho * cos(theta)
1619 b = rho * sin(theta)
1621 which is also expressed by this formula:
1623 z = rho * exp(i * theta) = rho * (cos theta + i * sin theta)
1625 In other words, it's the projection of the vector onto the I<x> and I<y>
1626 axes. Mathematicians call I<rho> the I<norm> or I<modulus> and I<theta>
1627 the I<argument> of the complex number. The I<norm> of C<z> is
1628 marked here as C<abs(z)>.
1630 The polar notation (also known as the trigonometric representation) is
1631 much more handy for performing multiplications and divisions of
1632 complex numbers, whilst the cartesian notation is better suited for
1633 additions and subtractions. Real numbers are on the I<x> axis, and
1634 therefore I<y> or I<theta> is zero or I<pi>.
1636 All the common operations that can be performed on a real number have
1637 been defined to work on complex numbers as well, and are merely
1638 I<extensions> of the operations defined on real numbers. This means
1639 they keep their natural meaning when there is no imaginary part, provided
1640 the number is within their definition set.
1642 For instance, the C<sqrt> routine which computes the square root of
1643 its argument is only defined for non-negative real numbers and yields a
1644 non-negative real number (it is an application from B<R+> to B<R+>).
1645 If we allow it to return a complex number, then it can be extended to
1646 negative real numbers to become an application from B<R> to B<C> (the
1647 set of complex numbers):
1649 sqrt(x) = x >= 0 ? sqrt(x) : sqrt(-x)*i
1651 It can also be extended to be an application from B<C> to B<C>,
1652 whilst its restriction to B<R> behaves as defined above by using
1653 the following definition:
1655 sqrt(z = [r,t]) = sqrt(r) * exp(i * t/2)
1657 Indeed, a negative real number can be noted C<[x,pi]> (the modulus
1658 I<x> is always non-negative, so C<[x,pi]> is really C<-x>, a negative
1659 number) and the above definition states that
1661 sqrt([x,pi]) = sqrt(x) * exp(i*pi/2) = [sqrt(x),pi/2] = sqrt(x)*i
1663 which is exactly what we had defined for negative real numbers above.
1664 The C<sqrt> returns only one of the solutions: if you want the both,
1665 use the C<root> function.
1667 All the common mathematical functions defined on real numbers that
1668 are extended to complex numbers share that same property of working
1669 I<as usual> when the imaginary part is zero (otherwise, it would not
1670 be called an extension, would it?).
1672 A I<new> operation possible on a complex number that is
1673 the identity for real numbers is called the I<conjugate>, and is noted
1674 with a horizontal bar above the number, or C<~z> here.
1681 z * ~z = (a + bi) * (a - bi) = a*a + b*b
1683 We saw that the norm of C<z> was noted C<abs(z)> and was defined as the
1684 distance to the origin, also known as:
1686 rho = abs(z) = sqrt(a*a + b*b)
1690 z * ~z = abs(z) ** 2
1692 If z is a pure real number (i.e. C<b == 0>), then the above yields:
1696 which is true (C<abs> has the regular meaning for real number, i.e. stands
1697 for the absolute value). This example explains why the norm of C<z> is
1698 noted C<abs(z)>: it extends the C<abs> function to complex numbers, yet
1699 is the regular C<abs> we know when the complex number actually has no
1700 imaginary part... This justifies I<a posteriori> our use of the C<abs>
1701 notation for the norm.
1705 Given the following notations:
1707 z1 = a + bi = r1 * exp(i * t1)
1708 z2 = c + di = r2 * exp(i * t2)
1709 z = <any complex or real number>
1711 the following (overloaded) operations are supported on complex numbers:
1713 z1 + z2 = (a + c) + i(b + d)
1714 z1 - z2 = (a - c) + i(b - d)
1715 z1 * z2 = (r1 * r2) * exp(i * (t1 + t2))
1716 z1 / z2 = (r1 / r2) * exp(i * (t1 - t2))
1717 z1 ** z2 = exp(z2 * log z1)
1719 abs(z) = r1 = sqrt(a*a + b*b)
1720 sqrt(z) = sqrt(r1) * exp(i * t/2)
1721 exp(z) = exp(a) * exp(i * b)
1722 log(z) = log(r1) + i*t
1723 sin(z) = 1/2i (exp(i * z1) - exp(-i * z))
1724 cos(z) = 1/2 (exp(i * z1) + exp(-i * z))
1725 atan2(y, x) = atan(y / x) # Minding the right quadrant, note the order.
1727 The definition used for complex arguments of atan2() is
1729 -i log((x + iy)/sqrt(x*x+y*y))
1731 Note that atan2(0, 0) is not well-defined.
1733 The following extra operations are supported on both real and complex
1741 cbrt(z) = z ** (1/3)
1742 log10(z) = log(z) / log(10)
1743 logn(z, n) = log(z) / log(n)
1745 tan(z) = sin(z) / cos(z)
1751 asin(z) = -i * log(i*z + sqrt(1-z*z))
1752 acos(z) = -i * log(z + i*sqrt(1-z*z))
1753 atan(z) = i/2 * log((i+z) / (i-z))
1755 acsc(z) = asin(1 / z)
1756 asec(z) = acos(1 / z)
1757 acot(z) = atan(1 / z) = -i/2 * log((i+z) / (z-i))
1759 sinh(z) = 1/2 (exp(z) - exp(-z))
1760 cosh(z) = 1/2 (exp(z) + exp(-z))
1761 tanh(z) = sinh(z) / cosh(z) = (exp(z) - exp(-z)) / (exp(z) + exp(-z))
1763 csch(z) = 1 / sinh(z)
1764 sech(z) = 1 / cosh(z)
1765 coth(z) = 1 / tanh(z)
1767 asinh(z) = log(z + sqrt(z*z+1))
1768 acosh(z) = log(z + sqrt(z*z-1))
1769 atanh(z) = 1/2 * log((1+z) / (1-z))
1771 acsch(z) = asinh(1 / z)
1772 asech(z) = acosh(1 / z)
1773 acoth(z) = atanh(1 / z) = 1/2 * log((1+z) / (z-1))
1775 I<arg>, I<abs>, I<log>, I<csc>, I<cot>, I<acsc>, I<acot>, I<csch>,
1776 I<coth>, I<acosech>, I<acotanh>, have aliases I<rho>, I<theta>, I<ln>,
1777 I<cosec>, I<cotan>, I<acosec>, I<acotan>, I<cosech>, I<cotanh>,
1778 I<acosech>, I<acotanh>, respectively. C<Re>, C<Im>, C<arg>, C<abs>,
1779 C<rho>, and C<theta> can be used also as mutators. The C<cbrt>
1780 returns only one of the solutions: if you want all three, use the
1783 The I<root> function is available to compute all the I<n>
1784 roots of some complex, where I<n> is a strictly positive integer.
1785 There are exactly I<n> such roots, returned as a list. Getting the
1786 number mathematicians call C<j> such that:
1790 is a simple matter of writing:
1792 $j = ((root(1, 3))[1];
1794 The I<k>th root for C<z = [r,t]> is given by:
1796 (root(z, n))[k] = r**(1/n) * exp(i * (t + 2*k*pi)/n)
1798 You can return the I<k>th root directly by C<root(z, n, k)>,
1799 indexing starting from I<zero> and ending at I<n - 1>.
1801 The I<spaceship> numeric comparison operator, E<lt>=E<gt>, is also
1802 defined. In order to ensure its restriction to real numbers is conform
1803 to what you would expect, the comparison is run on the real part of
1804 the complex number first, and imaginary parts are compared only when
1805 the real parts match.
1809 To create a complex number, use either:
1811 $z = Math::Complex->make(3, 4);
1814 if you know the cartesian form of the number, or
1818 if you like. To create a number using the polar form, use either:
1820 $z = Math::Complex->emake(5, pi/3);
1821 $x = cplxe(5, pi/3);
1823 instead. The first argument is the modulus, the second is the angle
1824 (in radians, the full circle is 2*pi). (Mnemonic: C<e> is used as a
1825 notation for complex numbers in the polar form).
1827 It is possible to write:
1829 $x = cplxe(-3, pi/4);
1831 but that will be silently converted into C<[3,-3pi/4]>, since the
1832 modulus must be non-negative (it represents the distance to the origin
1833 in the complex plane).
1835 It is also possible to have a complex number as either argument of the
1836 C<make>, C<emake>, C<cplx>, and C<cplxe>: the appropriate component of
1837 the argument will be used.
1842 The C<new>, C<make>, C<emake>, C<cplx>, and C<cplxe> will also
1843 understand a single (string) argument of the forms
1851 in which case the appropriate cartesian and exponential components
1852 will be parsed from the string and used to create new complex numbers.
1853 The imaginary component and the theta, respectively, will default to zero.
1855 The C<new>, C<make>, C<emake>, C<cplx>, and C<cplxe> will also
1856 understand the case of no arguments: this means plain zero or (0, 0).
1860 When printed, a complex number is usually shown under its cartesian
1861 style I<a+bi>, but there are legitimate cases where the polar style
1862 I<[r,t]> is more appropriate. The process of converting the complex
1863 number into a string that can be displayed is known as I<stringification>.
1865 By calling the class method C<Math::Complex::display_format> and
1866 supplying either C<"polar"> or C<"cartesian"> as an argument, you
1867 override the default display style, which is C<"cartesian">. Not
1868 supplying any argument returns the current settings.
1870 This default can be overridden on a per-number basis by calling the
1871 C<display_format> method instead. As before, not supplying any argument
1872 returns the current display style for this number. Otherwise whatever you
1873 specify will be the new display style for I<this> particular number.
1879 Math::Complex::display_format('polar');
1880 $j = (root(1, 3))[1];
1881 print "j = $j\n"; # Prints "j = [1,2pi/3]"
1882 $j->display_format('cartesian');
1883 print "j = $j\n"; # Prints "j = -0.5+0.866025403784439i"
1885 The polar style attempts to emphasize arguments like I<k*pi/n>
1886 (where I<n> is a positive integer and I<k> an integer within [-9, +9]),
1887 this is called I<polar pretty-printing>.
1889 For the reverse of stringifying, see the C<make> and C<emake>.
1891 =head2 CHANGED IN PERL 5.6
1893 The C<display_format> class method and the corresponding
1894 C<display_format> object method can now be called using
1895 a parameter hash instead of just a one parameter.
1897 The old display format style, which can have values C<"cartesian"> or
1898 C<"polar">, can be changed using the C<"style"> parameter.
1900 $j->display_format(style => "polar");
1902 The one parameter calling convention also still works.
1904 $j->display_format("polar");
1906 There are two new display parameters.
1908 The first one is C<"format">, which is a sprintf()-style format string
1909 to be used for both numeric parts of the complex number(s). The is
1910 somewhat system-dependent but most often it corresponds to C<"%.15g">.
1911 You can revert to the default by setting the C<format> to C<undef>.
1913 # the $j from the above example
1915 $j->display_format('format' => '%.5f');
1916 print "j = $j\n"; # Prints "j = -0.50000+0.86603i"
1917 $j->display_format('format' => undef);
1918 print "j = $j\n"; # Prints "j = -0.5+0.86603i"
1920 Notice that this affects also the return values of the
1921 C<display_format> methods: in list context the whole parameter hash
1922 will be returned, as opposed to only the style parameter value.
1923 This is a potential incompatibility with earlier versions if you
1924 have been calling the C<display_format> method in list context.
1926 The second new display parameter is C<"polar_pretty_print">, which can
1927 be set to true or false, the default being true. See the previous
1928 section for what this means.
1932 Thanks to overloading, the handling of arithmetics with complex numbers
1933 is simple and almost transparent.
1935 Here are some examples:
1939 $j = cplxe(1, 2*pi/3); # $j ** 3 == 1
1940 print "j = $j, j**3 = ", $j ** 3, "\n";
1941 print "1 + j + j**2 = ", 1 + $j + $j**2, "\n";
1943 $z = -16 + 0*i; # Force it to be a complex
1944 print "sqrt($z) = ", sqrt($z), "\n";
1946 $k = exp(i * 2*pi/3);
1947 print "$j - $k = ", $j - $k, "\n";
1949 $z->Re(3); # Re, Im, arg, abs,
1950 $j->arg(2); # (the last two aka rho, theta)
1951 # can be used also as mutators.
1957 The constant C<pi> and some handy multiples of it (pi2, pi4,
1958 and pip2 (pi/2) and pip4 (pi/4)) are also available if separately
1961 use Math::Complex ':pi';
1962 $third_of_circle = pi2 / 3;
1966 The floating point infinity can be exported as a subroutine Inf():
1968 use Math::Complex qw(Inf sinh);
1969 my $AlsoInf = Inf() + 42;
1970 my $AnotherInf = sinh(1e42);
1971 print "$AlsoInf is $AnotherInf\n" if $AlsoInf == $AnotherInf;
1973 Note that the stringified form of infinity varies between platforms:
1974 it can be for example any of
1981 or it can be something else.
1983 Also note that in some platforms trying to use the infinity in
1984 arithmetic operations may result in Perl crashing because using
1985 an infinity causes SIGFPE or its moral equivalent to be sent.
1986 The way to ignore this is
1988 local $SIG{FPE} = sub { };
1990 =head1 ERRORS DUE TO DIVISION BY ZERO OR LOGARITHM OF ZERO
1992 The division (/) and the following functions
1998 atanh asech acsch acoth
2000 cannot be computed for all arguments because that would mean dividing
2001 by zero or taking logarithm of zero. These situations cause fatal
2002 runtime errors looking like this
2004 cot(0): Division by zero.
2005 (Because in the definition of cot(0), the divisor sin(0) is 0)
2010 atanh(-1): Logarithm of zero.
2013 For the C<csc>, C<cot>, C<asec>, C<acsc>, C<acot>, C<csch>, C<coth>,
2014 C<asech>, C<acsch>, the argument cannot be C<0> (zero). For the
2015 logarithmic functions and the C<atanh>, C<acoth>, the argument cannot
2016 be C<1> (one). For the C<atanh>, C<acoth>, the argument cannot be
2017 C<-1> (minus one). For the C<atan>, C<acot>, the argument cannot be
2018 C<i> (the imaginary unit). For the C<atan>, C<acoth>, the argument
2019 cannot be C<-i> (the negative imaginary unit). For the C<tan>,
2020 C<sec>, C<tanh>, the argument cannot be I<pi/2 + k * pi>, where I<k>
2021 is any integer. atan2(0, 0) is undefined, and if the complex arguments
2022 are used for atan2(), a division by zero will happen if z1**2+z2**2 == 0.
2024 Note that because we are operating on approximations of real numbers,
2025 these errors can happen when merely `too close' to the singularities
2028 =head1 ERRORS DUE TO INDIGESTIBLE ARGUMENTS
2030 The C<make> and C<emake> accept both real and complex arguments.
2031 When they cannot recognize the arguments they will die with error
2032 messages like the following
2034 Math::Complex::make: Cannot take real part of ...
2035 Math::Complex::make: Cannot take real part of ...
2036 Math::Complex::emake: Cannot take rho of ...
2037 Math::Complex::emake: Cannot take theta of ...
2041 Saying C<use Math::Complex;> exports many mathematical routines in the
2042 caller environment and even overrides some (C<sqrt>, C<log>, C<atan2>).
2043 This is construed as a feature by the Authors, actually... ;-)
2045 All routines expect to be given real or complex numbers. Don't attempt to
2046 use BigFloat, since Perl has currently no rule to disambiguate a '+'
2047 operation (for instance) between two overloaded entities.
2049 In Cray UNICOS there is some strange numerical instability that results
2050 in root(), cos(), sin(), cosh(), sinh(), losing accuracy fast. Beware.
2051 The bug may be in UNICOS math libs, in UNICOS C compiler, in Math::Complex.
2052 Whatever it is, it does not manifest itself anywhere else where Perl runs.
2060 Daniel S. Lewart <F<lewart!at!uiuc.edu>>
2061 Jarkko Hietaniemi <F<jhi!at!iki.fi>>
2062 Raphael Manfredi <F<Raphael_Manfredi!at!pobox.com>>
2066 This library is free software; you can redistribute it and/or modify
2067 it under the same terms as Perl itself.