[perl #40272] subroutine call with & in perlop example
[p5sagit/p5-mst-13.2.git] / lib / Math / Complex.pm
CommitLineData
66730be0 1#
2# Complex numbers and associated mathematical functions
b42d0ec9 3# -- Raphael Manfredi Since Sep 1996
4# -- Jarkko Hietaniemi Since Mar 1997
5# -- Daniel S. Lewart Since Sep 1997
fb73857a 6#
a0d0e21e 7
5aabfad6 8package Math::Complex;
a0d0e21e 9
bf5f1b4c 10use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $Inf);
9fbe1b12 11
affad850 12$VERSION = 1.36;
476757f7 13
9fbe1b12 14BEGIN {
ffb4440d 15 unless ($^O eq 'unicosmk') {
16 my $e = $!;
830ec763 17 # We do want an arithmetic overflow, Inf INF inf Infinity:.
18 undef $Inf unless eval <<'EOE' and $Inf =~ /^inf(?:inity)?$/i;
19 local $SIG{FPE} = sub {die};
20 my $t = CORE::exp 30;
21 $Inf = CORE::exp $t;
22EOE
23 if (!defined $Inf) { # Try a different method
24 undef $Inf unless eval <<'EOE' and $Inf =~ /^inf(?:inity)?$/i;
25 local $SIG{FPE} = sub {die};
26 my $t = 1;
27 $Inf = $t + "1e99999999999999999999999999999999";
28EOE
29 }
ffb4440d 30 $! = $e; # Clear ERANGE.
ffb4440d 31 }
5240e574 32 $Inf = "Inf" if !defined $Inf || !($Inf > 0); # Desperation.
9fbe1b12 33}
fb73857a 34
9fbe1b12 35use strict;
fb73857a 36
9fbe1b12 37my $i;
38my %LOGN;
0c721ce2 39
91cb744f 40# Regular expression for floating point numbers.
bf5f1b4c 41# These days we could use Scalar::Util::lln(), I guess.
42my $gre = qr'\s*([\+\-]?(?:(?:(?:\d+(?:_\d+)*(?:\.\d*(?:_\d+)*)?|\.\d+(?:_\d+)*)(?:[eE][\+\-]?\d+(?:_\d+)*)?))|inf)'i;
91cb744f 43
9fbe1b12 44require Exporter;
0c721ce2 45
5aabfad6 46@ISA = qw(Exporter);
47
5aabfad6 48my @trig = qw(
49 pi
fb73857a 50 tan
5aabfad6 51 csc cosec sec cot cotan
52 asin acos atan
53 acsc acosec asec acot acotan
54 sinh cosh tanh
55 csch cosech sech coth cotanh
56 asinh acosh atanh
57 acsch acosech asech acoth acotanh
58 );
59
60@EXPORT = (qw(
b42d0ec9 61 i Re Im rho theta arg
fb73857a 62 sqrt log ln
5aabfad6 63 log10 logn cbrt root
64 cplx cplxe
bf5f1b4c 65 atan2
5aabfad6 66 ),
67 @trig);
68
affad850 69my @pi = qw(pi pi2 pi4 pip2 pip4);
70
71@EXPORT_OK = @pi;
bf5f1b4c 72
5aabfad6 73%EXPORT_TAGS = (
74 'trig' => [@trig],
affad850 75 'pi' => [@pi],
66730be0 76);
a0d0e21e 77
a5f75d66 78use overload
affad850 79 '+' => \&_plus,
80 '-' => \&_minus,
81 '*' => \&_multiply,
82 '/' => \&_divide,
83 '**' => \&_power,
84 '==' => \&_numeq,
85 '<=>' => \&_spaceship,
86 'neg' => \&_negate,
87 '~' => \&_conjugate,
66730be0 88 'abs' => \&abs,
89 'sqrt' => \&sqrt,
90 'exp' => \&exp,
91 'log' => \&log,
92 'sin' => \&sin,
93 'cos' => \&cos,
0c721ce2 94 'tan' => \&tan,
66730be0 95 'atan2' => \&atan2,
affad850 96 '""' => \&_stringify;
66730be0 97
98#
b42d0ec9 99# Package "privates"
66730be0 100#
101
16357284 102my %DISPLAY_FORMAT = ('style' => 'cartesian',
103 'polar_pretty_print' => 1);
104my $eps = 1e-14; # Epsilon
66730be0 105
106#
107# Object attributes (internal):
108# cartesian [real, imaginary] -- cartesian form
109# polar [rho, theta] -- polar form
110# c_dirty cartesian form not up-to-date
111# p_dirty polar form not up-to-date
112# display display format (package's global when not set)
affad850 113# bn_cartesian
114# bnc_dirty
66730be0 115#
116
b42d0ec9 117# Die on bad *make() arguments.
118
119sub _cannot_make {
bf5f1b4c 120 die "@{[(caller(1))[3]]}: Cannot take $_[0] of '$_[1]'.\n";
b42d0ec9 121}
122
bf5f1b4c 123sub _make {
91cb744f 124 my $arg = shift;
bf5f1b4c 125 my ($p, $q);
91cb744f 126
bf5f1b4c 127 if ($arg =~ /^$gre$/) {
128 ($p, $q) = ($1, 0);
129 } elsif ($arg =~ /^(?:$gre)?$gre\s*i\s*$/) {
91cb744f 130 ($p, $q) = ($1 || 0, $2);
bf5f1b4c 131 } elsif ($arg =~ /^\s*\(\s*$gre\s*(?:,\s*$gre\s*)?\)\s*$/) {
91cb744f 132 ($p, $q) = ($1, $2 || 0);
91cb744f 133 }
134
bf5f1b4c 135 if (defined $p) {
91cb744f 136 $p =~ s/^\+//;
bf5f1b4c 137 $p =~ s/^(-?)inf$/"${1}9**9**9"/e;
91cb744f 138 $q =~ s/^\+//;
bf5f1b4c 139 $q =~ s/^(-?)inf$/"${1}9**9**9"/e;
91cb744f 140 }
141
bf5f1b4c 142 return ($p, $q);
143}
144
145sub _emake {
146 my $arg = shift;
147 my ($p, $q);
148
149 if ($arg =~ /^\s*\[\s*$gre\s*(?:,\s*$gre\s*)?\]\s*$/) {
150 ($p, $q) = ($1, $2 || 0);
151 } elsif ($arg =~ m!^\s*\[\s*$gre\s*(?:,\s*([-+]?\d*\s*)?pi(?:/\s*(\d+))?\s*)?\]\s*$!) {
152 ($p, $q) = ($1, ($2 eq '-' ? -1 : ($2 || 1)) * pi() / ($3 || 1));
153 } elsif ($arg =~ /^\s*\[\s*$gre\s*\]\s*$/) {
154 ($p, $q) = ($1, 0);
155 } elsif ($arg =~ /^\s*$gre\s*$/) {
156 ($p, $q) = ($1, 0);
157 }
158
159 if (defined $p) {
160 $p =~ s/^\+//;
161 $q =~ s/^\+//;
162 $p =~ s/^(-?)inf$/"${1}9**9**9"/e;
163 $q =~ s/^(-?)inf$/"${1}9**9**9"/e;
164 }
165
166 return ($p, $q);
91cb744f 167}
168
66730be0 169#
170# ->make
171#
172# Create a new complex number (cartesian form)
173#
174sub make {
bf5f1b4c 175 my $self = bless {}, shift;
176 my ($re, $im);
177 if (@_ == 0) {
178 ($re, $im) = (0, 0);
179 } elsif (@_ == 1) {
180 return (ref $self)->emake($_[0])
181 if ($_[0] =~ /^\s*\[/);
182 ($re, $im) = _make($_[0]);
183 } elsif (@_ == 2) {
184 ($re, $im) = @_;
185 }
186 if (defined $re) {
91cb744f 187 _cannot_make("real part", $re) unless $re =~ /^$gre$/;
bf5f1b4c 188 }
189 $im ||= 0;
190 _cannot_make("imaginary part", $im) unless $im =~ /^$gre$/;
affad850 191 $self->_set_cartesian([$re, $im ]);
bf5f1b4c 192 $self->display_format('cartesian');
193
194 return $self;
66730be0 195}
196
197#
198# ->emake
199#
200# Create a new complex number (exponential form)
201#
202sub emake {
bf5f1b4c 203 my $self = bless {}, shift;
204 my ($rho, $theta);
205 if (@_ == 0) {
206 ($rho, $theta) = (0, 0);
207 } elsif (@_ == 1) {
208 return (ref $self)->make($_[0])
209 if ($_[0] =~ /^\s*\(/ || $_[0] =~ /i\s*$/);
210 ($rho, $theta) = _emake($_[0]);
211 } elsif (@_ == 2) {
212 ($rho, $theta) = @_;
213 }
214 if (defined $rho && defined $theta) {
fb73857a 215 if ($rho < 0) {
216 $rho = -$rho;
217 $theta = ($theta <= 0) ? $theta + pi() : $theta - pi();
218 }
bf5f1b4c 219 }
220 if (defined $rho) {
91cb744f 221 _cannot_make("rho", $rho) unless $rho =~ /^$gre$/;
bf5f1b4c 222 }
223 $theta ||= 0;
224 _cannot_make("theta", $theta) unless $theta =~ /^$gre$/;
affad850 225 $self->_set_polar([$rho, $theta]);
bf5f1b4c 226 $self->display_format('polar');
227
228 return $self;
66730be0 229}
230
231sub new { &make } # For backward compatibility only.
232
233#
234# cplx
235#
236# Creates a complex number from a (re, im) tuple.
237# This avoids the burden of writing Math::Complex->make(re, im).
238#
239sub cplx {
91cb744f 240 return __PACKAGE__->make(@_);
66730be0 241}
242
243#
244# cplxe
245#
246# Creates a complex number from a (rho, theta) tuple.
247# This avoids the burden of writing Math::Complex->emake(rho, theta).
248#
249sub cplxe {
91cb744f 250 return __PACKAGE__->emake(@_);
66730be0 251}
252
253#
254# pi
255#
fb73857a 256# The number defined as pi = 180 degrees
66730be0 257#
6570f784 258sub pi () { 4 * CORE::atan2(1, 1) }
5cd24f17 259
260#
affad850 261# pi2
5cd24f17 262#
fb73857a 263# The full circle
264#
affad850 265sub pi2 () { 2 * pi }
266
267#
268# pi4
269#
270# The full circle twice.
271#
272sub pi4 () { 4 * pi }
fb73857a 273
5cd24f17 274#
fb73857a 275# pip2
276#
277# The quarter circle
278#
6570f784 279sub pip2 () { pi / 2 }
5cd24f17 280
fb73857a 281#
affad850 282# pip4
d09ae4e6 283#
affad850 284# The eighth circle.
d09ae4e6 285#
affad850 286sub pip4 () { pi / 4 }
d09ae4e6 287
288#
affad850 289# _uplog10
fb73857a 290#
291# Used in log10().
292#
affad850 293sub _uplog10 () { 1 / CORE::log(10) }
66730be0 294
295#
296# i
297#
298# The number defined as i*i = -1;
299#
300sub i () {
5cd24f17 301 return $i if ($i);
302 $i = bless {};
40da2db3 303 $i->{'cartesian'} = [0, 1];
fb73857a 304 $i->{'polar'} = [1, pip2];
66730be0 305 $i->{c_dirty} = 0;
306 $i->{p_dirty} = 0;
307 return $i;
308}
309
310#
affad850 311# _ip2
1fa12f56 312#
313# Half of i.
314#
affad850 315sub _ip2 () { i / 2 }
1fa12f56 316
317#
66730be0 318# Attribute access/set routines
319#
320
affad850 321sub _cartesian {$_[0]->{c_dirty} ?
322 $_[0]->_update_cartesian : $_[0]->{'cartesian'}}
323sub _polar {$_[0]->{p_dirty} ?
324 $_[0]->_update_polar : $_[0]->{'polar'}}
66730be0 325
affad850 326sub _set_cartesian { $_[0]->{p_dirty}++; $_[0]->{c_dirty} = 0;
327 $_[0]->{'cartesian'} = $_[1] }
328sub _set_polar { $_[0]->{c_dirty}++; $_[0]->{p_dirty} = 0;
329 $_[0]->{'polar'} = $_[1] }
66730be0 330
331#
affad850 332# ->_update_cartesian
66730be0 333#
334# Recompute and return the cartesian form, given accurate polar form.
335#
affad850 336sub _update_cartesian {
66730be0 337 my $self = shift;
40da2db3 338 my ($r, $t) = @{$self->{'polar'}};
66730be0 339 $self->{c_dirty} = 0;
a8693bd3 340 return $self->{'cartesian'} = [$r * CORE::cos($t), $r * CORE::sin($t)];
66730be0 341}
342
343#
344#
affad850 345# ->_update_polar
66730be0 346#
347# Recompute and return the polar form, given accurate cartesian form.
348#
affad850 349sub _update_polar {
66730be0 350 my $self = shift;
40da2db3 351 my ($x, $y) = @{$self->{'cartesian'}};
66730be0 352 $self->{p_dirty} = 0;
40da2db3 353 return $self->{'polar'} = [0, 0] if $x == 0 && $y == 0;
1fa12f56 354 return $self->{'polar'} = [CORE::sqrt($x*$x + $y*$y),
355 CORE::atan2($y, $x)];
66730be0 356}
357
358#
affad850 359# (_plus)
66730be0 360#
361# Computes z1+z2.
362#
affad850 363sub _plus {
66730be0 364 my ($z1, $z2, $regular) = @_;
affad850 365 my ($re1, $im1) = @{$z1->_cartesian};
0e505df1 366 $z2 = cplx($z2) unless ref $z2;
affad850 367 my ($re2, $im2) = ref $z2 ? @{$z2->_cartesian} : ($z2, 0);
66730be0 368 unless (defined $regular) {
affad850 369 $z1->_set_cartesian([$re1 + $re2, $im1 + $im2]);
66730be0 370 return $z1;
371 }
372 return (ref $z1)->make($re1 + $re2, $im1 + $im2);
373}
374
375#
affad850 376# (_minus)
66730be0 377#
378# Computes z1-z2.
379#
affad850 380sub _minus {
66730be0 381 my ($z1, $z2, $inverted) = @_;
affad850 382 my ($re1, $im1) = @{$z1->_cartesian};
0e505df1 383 $z2 = cplx($z2) unless ref $z2;
affad850 384 my ($re2, $im2) = @{$z2->_cartesian};
66730be0 385 unless (defined $inverted) {
affad850 386 $z1->_set_cartesian([$re1 - $re2, $im1 - $im2]);
66730be0 387 return $z1;
388 }
389 return $inverted ?
390 (ref $z1)->make($re2 - $re1, $im2 - $im1) :
391 (ref $z1)->make($re1 - $re2, $im1 - $im2);
0e505df1 392
66730be0 393}
394
395#
affad850 396# (_multiply)
66730be0 397#
398# Computes z1*z2.
399#
affad850 400sub _multiply {
fb73857a 401 my ($z1, $z2, $regular) = @_;
402 if ($z1->{p_dirty} == 0 and ref $z2 and $z2->{p_dirty} == 0) {
403 # if both polar better use polar to avoid rounding errors
affad850 404 my ($r1, $t1) = @{$z1->_polar};
405 my ($r2, $t2) = @{$z2->_polar};
fb73857a 406 my $t = $t1 + $t2;
affad850 407 if ($t > pi()) { $t -= pi2 }
408 elsif ($t <= -pi()) { $t += pi2 }
fb73857a 409 unless (defined $regular) {
affad850 410 $z1->_set_polar([$r1 * $r2, $t]);
66730be0 411 return $z1;
fb73857a 412 }
413 return (ref $z1)->emake($r1 * $r2, $t);
414 } else {
affad850 415 my ($x1, $y1) = @{$z1->_cartesian};
fb73857a 416 if (ref $z2) {
affad850 417 my ($x2, $y2) = @{$z2->_cartesian};
fb73857a 418 return (ref $z1)->make($x1*$x2-$y1*$y2, $x1*$y2+$y1*$x2);
419 } else {
420 return (ref $z1)->make($x1*$z2, $y1*$z2);
421 }
66730be0 422 }
66730be0 423}
424
425#
0e505df1 426# _divbyzero
0c721ce2 427#
428# Die on division by zero.
429#
0e505df1 430sub _divbyzero {
5cd24f17 431 my $mess = "$_[0]: Division by zero.\n";
432
433 if (defined $_[1]) {
434 $mess .= "(Because in the definition of $_[0], the divisor ";
1fa12f56 435 $mess .= "$_[1] " unless ("$_[1]" eq '0');
5cd24f17 436 $mess .= "is 0)\n";
437 }
438
0c721ce2 439 my @up = caller(1);
fb73857a 440
5cd24f17 441 $mess .= "Died at $up[1] line $up[2].\n";
442
443 die $mess;
0c721ce2 444}
445
446#
affad850 447# (_divide)
66730be0 448#
449# Computes z1/z2.
450#
affad850 451sub _divide {
66730be0 452 my ($z1, $z2, $inverted) = @_;
fb73857a 453 if ($z1->{p_dirty} == 0 and ref $z2 and $z2->{p_dirty} == 0) {
454 # if both polar better use polar to avoid rounding errors
affad850 455 my ($r1, $t1) = @{$z1->_polar};
456 my ($r2, $t2) = @{$z2->_polar};
fb73857a 457 my $t;
458 if ($inverted) {
0e505df1 459 _divbyzero "$z2/0" if ($r1 == 0);
fb73857a 460 $t = $t2 - $t1;
affad850 461 if ($t > pi()) { $t -= pi2 }
462 elsif ($t <= -pi()) { $t += pi2 }
fb73857a 463 return (ref $z1)->emake($r2 / $r1, $t);
464 } else {
0e505df1 465 _divbyzero "$z1/0" if ($r2 == 0);
fb73857a 466 $t = $t1 - $t2;
affad850 467 if ($t > pi()) { $t -= pi2 }
468 elsif ($t <= -pi()) { $t += pi2 }
fb73857a 469 return (ref $z1)->emake($r1 / $r2, $t);
470 }
471 } else {
472 my ($d, $x2, $y2);
473 if ($inverted) {
affad850 474 ($x2, $y2) = @{$z1->_cartesian};
fb73857a 475 $d = $x2*$x2 + $y2*$y2;
476 _divbyzero "$z2/0" if $d == 0;
477 return (ref $z1)->make(($x2*$z2)/$d, -($y2*$z2)/$d);
478 } else {
affad850 479 my ($x1, $y1) = @{$z1->_cartesian};
fb73857a 480 if (ref $z2) {
affad850 481 ($x2, $y2) = @{$z2->_cartesian};
fb73857a 482 $d = $x2*$x2 + $y2*$y2;
483 _divbyzero "$z1/0" if $d == 0;
484 my $u = ($x1*$x2 + $y1*$y2)/$d;
485 my $v = ($y1*$x2 - $x1*$y2)/$d;
486 return (ref $z1)->make($u, $v);
487 } else {
488 _divbyzero "$z1/0" if $z2 == 0;
489 return (ref $z1)->make($x1/$z2, $y1/$z2);
490 }
491 }
0c721ce2 492 }
66730be0 493}
494
495#
affad850 496# (_power)
66730be0 497#
498# Computes z1**z2 = exp(z2 * log z1)).
499#
affad850 500sub _power {
66730be0 501 my ($z1, $z2, $inverted) = @_;
ace5de91 502 if ($inverted) {
2820d885 503 return 1 if $z1 == 0 || $z2 == 1;
504 return 0 if $z2 == 0 && Re($z1) > 0;
ace5de91 505 } else {
2820d885 506 return 1 if $z2 == 0 || $z1 == 1;
507 return 0 if $z1 == 0 && Re($z2) > 0;
ace5de91 508 }
1fa12f56 509 my $w = $inverted ? &exp($z1 * &log($z2))
510 : &exp($z2 * &log($z1));
d09ae4e6 511 # If both arguments cartesian, return cartesian, else polar.
512 return $z1->{c_dirty} == 0 &&
513 (not ref $z2 or $z2->{c_dirty} == 0) ?
affad850 514 cplx(@{$w->_cartesian}) : $w;
66730be0 515}
516
517#
affad850 518# (_spaceship)
66730be0 519#
520# Computes z1 <=> z2.
2820d885 521# Sorts on the real part first, then on the imaginary part. Thus 2-4i < 3+8i.
66730be0 522#
affad850 523sub _spaceship {
66730be0 524 my ($z1, $z2, $inverted) = @_;
affad850 525 my ($re1, $im1) = ref $z1 ? @{$z1->_cartesian} : ($z1, 0);
526 my ($re2, $im2) = ref $z2 ? @{$z2->_cartesian} : ($z2, 0);
66730be0 527 my $sgn = $inverted ? -1 : 1;
528 return $sgn * ($re1 <=> $re2) if $re1 != $re2;
529 return $sgn * ($im1 <=> $im2);
530}
531
532#
affad850 533# (_numeq)
1fa12f56 534#
535# Computes z1 == z2.
536#
affad850 537# (Required in addition to _spaceship() because of NaNs.)
538sub _numeq {
1fa12f56 539 my ($z1, $z2, $inverted) = @_;
affad850 540 my ($re1, $im1) = ref $z1 ? @{$z1->_cartesian} : ($z1, 0);
541 my ($re2, $im2) = ref $z2 ? @{$z2->_cartesian} : ($z2, 0);
1fa12f56 542 return $re1 == $re2 && $im1 == $im2 ? 1 : 0;
543}
544
545#
affad850 546# (_negate)
66730be0 547#
548# Computes -z.
549#
affad850 550sub _negate {
66730be0 551 my ($z) = @_;
552 if ($z->{c_dirty}) {
affad850 553 my ($r, $t) = @{$z->_polar};
fb73857a 554 $t = ($t <= 0) ? $t + pi : $t - pi;
555 return (ref $z)->emake($r, $t);
66730be0 556 }
affad850 557 my ($re, $im) = @{$z->_cartesian};
66730be0 558 return (ref $z)->make(-$re, -$im);
559}
560
561#
affad850 562# (_conjugate)
66730be0 563#
affad850 564# Compute complex's _conjugate.
66730be0 565#
affad850 566sub _conjugate {
66730be0 567 my ($z) = @_;
568 if ($z->{c_dirty}) {
affad850 569 my ($r, $t) = @{$z->_polar};
66730be0 570 return (ref $z)->emake($r, -$t);
571 }
affad850 572 my ($re, $im) = @{$z->_cartesian};
66730be0 573 return (ref $z)->make($re, -$im);
574}
575
576#
577# (abs)
578#
b42d0ec9 579# Compute or set complex's norm (rho).
66730be0 580#
581sub abs {
b42d0ec9 582 my ($z, $rho) = @_;
1fa12f56 583 unless (ref $z) {
584 if (@_ == 2) {
585 $_[0] = $_[1];
586 } else {
587 return CORE::abs($z);
588 }
589 }
b42d0ec9 590 if (defined $rho) {
affad850 591 $z->{'polar'} = [ $rho, ${$z->_polar}[1] ];
b42d0ec9 592 $z->{p_dirty} = 0;
593 $z->{c_dirty} = 1;
594 return $rho;
595 } else {
affad850 596 return ${$z->_polar}[0];
b42d0ec9 597 }
598}
599
600sub _theta {
601 my $theta = $_[0];
602
affad850 603 if ($$theta > pi()) { $$theta -= pi2 }
604 elsif ($$theta <= -pi()) { $$theta += pi2 }
66730be0 605}
606
607#
608# arg
609#
b42d0ec9 610# Compute or set complex's argument (theta).
66730be0 611#
612sub arg {
b42d0ec9 613 my ($z, $theta) = @_;
614 return $z unless ref $z;
615 if (defined $theta) {
616 _theta(\$theta);
affad850 617 $z->{'polar'} = [ ${$z->_polar}[0], $theta ];
b42d0ec9 618 $z->{p_dirty} = 0;
619 $z->{c_dirty} = 1;
620 } else {
affad850 621 $theta = ${$z->_polar}[1];
b42d0ec9 622 _theta(\$theta);
623 }
624 return $theta;
66730be0 625}
626
627#
628# (sqrt)
629#
0c721ce2 630# Compute sqrt(z).
66730be0 631#
b42d0ec9 632# It is quite tempting to use wantarray here so that in list context
633# sqrt() would return the two solutions. This, however, would
634# break things like
635#
636# print "sqrt(z) = ", sqrt($z), "\n";
637#
638# The two values would be printed side by side without no intervening
639# whitespace, quite confusing.
640# Therefore if you want the two solutions use the root().
641#
66730be0 642sub sqrt {
643 my ($z) = @_;
affad850 644 my ($re, $im) = ref $z ? @{$z->_cartesian} : ($z, 0);
1fa12f56 645 return $re < 0 ? cplx(0, CORE::sqrt(-$re)) : CORE::sqrt($re)
646 if $im == 0;
affad850 647 my ($r, $t) = @{$z->_polar};
a8693bd3 648 return (ref $z)->emake(CORE::sqrt($r), $t/2);
66730be0 649}
650
651#
652# cbrt
653#
0c721ce2 654# Compute cbrt(z) (cubic root).
66730be0 655#
b42d0ec9 656# Why are we not returning three values? The same answer as for sqrt().
657#
66730be0 658sub cbrt {
659 my ($z) = @_;
1fa12f56 660 return $z < 0 ?
661 -CORE::exp(CORE::log(-$z)/3) :
662 ($z > 0 ? CORE::exp(CORE::log($z)/3): 0)
fb73857a 663 unless ref $z;
affad850 664 my ($r, $t) = @{$z->_polar};
1fa12f56 665 return 0 if $r == 0;
a8693bd3 666 return (ref $z)->emake(CORE::exp(CORE::log($r)/3), $t/3);
66730be0 667}
668
669#
0e505df1 670# _rootbad
671#
672# Die on bad root.
673#
674sub _rootbad {
bf5f1b4c 675 my $mess = "Root '$_[0]' illegal, root rank must be positive integer.\n";
0e505df1 676
677 my @up = caller(1);
fb73857a 678
0e505df1 679 $mess .= "Died at $up[1] line $up[2].\n";
680
681 die $mess;
682}
683
684#
66730be0 685# root
686#
687# Computes all nth root for z, returning an array whose size is n.
688# `n' must be a positive integer.
689#
690# The roots are given by (for k = 0..n-1):
691#
692# z^(1/n) = r^(1/n) (cos ((t+2 k pi)/n) + i sin ((t+2 k pi)/n))
693#
694sub root {
bf5f1b4c 695 my ($z, $n, $k) = @_;
0e505df1 696 _rootbad($n) if ($n < 1 or int($n) != $n);
1fa12f56 697 my ($r, $t) = ref $z ?
affad850 698 @{$z->_polar} : (CORE::abs($z), $z >= 0 ? 0 : pi);
699 my $theta_inc = pi2 / $n;
66730be0 700 my $rho = $r ** (1/$n);
d09ae4e6 701 my $cartesian = ref $z && $z->{c_dirty} == 0;
bf5f1b4c 702 if (@_ == 2) {
703 my @root;
704 for (my $i = 0, my $theta = $t / $n;
705 $i < $n;
706 $i++, $theta += $theta_inc) {
707 my $w = cplxe($rho, $theta);
708 # Yes, $cartesian is loop invariant.
affad850 709 push @root, $cartesian ? cplx(@{$w->_cartesian}) : $w;
bf5f1b4c 710 }
711 return @root;
712 } elsif (@_ == 3) {
713 my $w = cplxe($rho, $t / $n + $k * $theta_inc);
affad850 714 return $cartesian ? cplx(@{$w->_cartesian}) : $w;
a0d0e21e 715 }
a0d0e21e 716}
717
66730be0 718#
719# Re
720#
b42d0ec9 721# Return or set Re(z).
66730be0 722#
a0d0e21e 723sub Re {
b42d0ec9 724 my ($z, $Re) = @_;
66730be0 725 return $z unless ref $z;
b42d0ec9 726 if (defined $Re) {
affad850 727 $z->{'cartesian'} = [ $Re, ${$z->_cartesian}[1] ];
b42d0ec9 728 $z->{c_dirty} = 0;
729 $z->{p_dirty} = 1;
730 } else {
affad850 731 return ${$z->_cartesian}[0];
b42d0ec9 732 }
a0d0e21e 733}
734
66730be0 735#
736# Im
737#
b42d0ec9 738# Return or set Im(z).
66730be0 739#
a0d0e21e 740sub Im {
b42d0ec9 741 my ($z, $Im) = @_;
178326e7 742 return 0 unless ref $z;
b42d0ec9 743 if (defined $Im) {
affad850 744 $z->{'cartesian'} = [ ${$z->_cartesian}[0], $Im ];
b42d0ec9 745 $z->{c_dirty} = 0;
746 $z->{p_dirty} = 1;
747 } else {
affad850 748 return ${$z->_cartesian}[1];
b42d0ec9 749 }
750}
751
752#
753# rho
754#
755# Return or set rho(w).
756#
757sub rho {
758 Math::Complex::abs(@_);
759}
760
761#
762# theta
763#
764# Return or set theta(w).
765#
766sub theta {
767 Math::Complex::arg(@_);
a0d0e21e 768}
769
66730be0 770#
771# (exp)
772#
773# Computes exp(z).
774#
775sub exp {
776 my ($z) = @_;
affad850 777 my ($x, $y) = @{$z->_cartesian};
a8693bd3 778 return (ref $z)->emake(CORE::exp($x), $y);
66730be0 779}
780
781#
8c03c583 782# _logofzero
783#
fb73857a 784# Die on logarithm of zero.
8c03c583 785#
786sub _logofzero {
787 my $mess = "$_[0]: Logarithm of zero.\n";
788
789 if (defined $_[1]) {
790 $mess .= "(Because in the definition of $_[0], the argument ";
791 $mess .= "$_[1] " unless ($_[1] eq '0');
792 $mess .= "is 0)\n";
793 }
794
795 my @up = caller(1);
fb73857a 796
8c03c583 797 $mess .= "Died at $up[1] line $up[2].\n";
798
799 die $mess;
800}
801
802#
66730be0 803# (log)
804#
805# Compute log(z).
806#
807sub log {
808 my ($z) = @_;
fb73857a 809 unless (ref $z) {
810 _logofzero("log") if $z == 0;
a8693bd3 811 return $z > 0 ? CORE::log($z) : cplx(CORE::log(-$z), pi);
fb73857a 812 }
affad850 813 my ($r, $t) = @{$z->_polar};
fb73857a 814 _logofzero("log") if $r == 0;
affad850 815 if ($t > pi()) { $t -= pi2 }
816 elsif ($t <= -pi()) { $t += pi2 }
a8693bd3 817 return (ref $z)->make(CORE::log($r), $t);
66730be0 818}
819
820#
0c721ce2 821# ln
822#
823# Alias for log().
824#
825sub ln { Math::Complex::log(@_) }
826
827#
66730be0 828# log10
829#
830# Compute log10(z).
831#
5cd24f17 832
66730be0 833sub log10 {
affad850 834 return Math::Complex::log($_[0]) * _uplog10;
66730be0 835}
836
837#
838# logn
839#
840# Compute logn(z,n) = log(z) / log(n)
841#
842sub logn {
843 my ($z, $n) = @_;
0c721ce2 844 $z = cplx($z, 0) unless ref $z;
9fbe1b12 845 my $logn = $LOGN{$n};
846 $logn = $LOGN{$n} = CORE::log($n) unless defined $logn; # Cache log(n)
1fa12f56 847 return &log($z) / $logn;
66730be0 848}
849
850#
851# (cos)
852#
853# Compute cos(z) = (exp(iz) + exp(-iz))/2.
854#
855sub cos {
856 my ($z) = @_;
1fa12f56 857 return CORE::cos($z) unless ref $z;
affad850 858 my ($x, $y) = @{$z->_cartesian};
a8693bd3 859 my $ey = CORE::exp($y);
1fa12f56 860 my $sx = CORE::sin($x);
861 my $cx = CORE::cos($x);
862 my $ey_1 = $ey ? 1 / $ey : $Inf;
863 return (ref $z)->make($cx * ($ey + $ey_1)/2,
864 $sx * ($ey_1 - $ey)/2);
66730be0 865}
866
867#
868# (sin)
869#
870# Compute sin(z) = (exp(iz) - exp(-iz))/2.
871#
872sub sin {
873 my ($z) = @_;
1fa12f56 874 return CORE::sin($z) unless ref $z;
affad850 875 my ($x, $y) = @{$z->_cartesian};
a8693bd3 876 my $ey = CORE::exp($y);
1fa12f56 877 my $sx = CORE::sin($x);
878 my $cx = CORE::cos($x);
879 my $ey_1 = $ey ? 1 / $ey : $Inf;
880 return (ref $z)->make($sx * ($ey + $ey_1)/2,
881 $cx * ($ey - $ey_1)/2);
66730be0 882}
883
884#
885# tan
886#
887# Compute tan(z) = sin(z) / cos(z).
888#
889sub tan {
890 my ($z) = @_;
1fa12f56 891 my $cz = &cos($z);
892 _divbyzero "tan($z)", "cos($z)" if $cz == 0;
893 return &sin($z) / $cz;
66730be0 894}
895
896#
0c721ce2 897# sec
898#
899# Computes the secant sec(z) = 1 / cos(z).
900#
901sub sec {
902 my ($z) = @_;
1fa12f56 903 my $cz = &cos($z);
0e505df1 904 _divbyzero "sec($z)", "cos($z)" if ($cz == 0);
0c721ce2 905 return 1 / $cz;
906}
907
908#
909# csc
910#
911# Computes the cosecant csc(z) = 1 / sin(z).
912#
913sub csc {
914 my ($z) = @_;
1fa12f56 915 my $sz = &sin($z);
0e505df1 916 _divbyzero "csc($z)", "sin($z)" if ($sz == 0);
0c721ce2 917 return 1 / $sz;
918}
919
66730be0 920#
0c721ce2 921# cosec
66730be0 922#
0c721ce2 923# Alias for csc().
924#
925sub cosec { Math::Complex::csc(@_) }
926
927#
928# cot
929#
fb73857a 930# Computes cot(z) = cos(z) / sin(z).
0c721ce2 931#
932sub cot {
66730be0 933 my ($z) = @_;
1fa12f56 934 my $sz = &sin($z);
0e505df1 935 _divbyzero "cot($z)", "sin($z)" if ($sz == 0);
1fa12f56 936 return &cos($z) / $sz;
66730be0 937}
938
939#
0c721ce2 940# cotan
941#
942# Alias for cot().
943#
944sub cotan { Math::Complex::cot(@_) }
945
946#
66730be0 947# acos
948#
949# Computes the arc cosine acos(z) = -i log(z + sqrt(z*z-1)).
950#
951sub acos {
fb73857a 952 my $z = $_[0];
1fa12f56 953 return CORE::atan2(CORE::sqrt(1-$z*$z), $z)
954 if (! ref $z) && CORE::abs($z) <= 1;
40b904b7 955 $z = cplx($z, 0) unless ref $z;
affad850 956 my ($x, $y) = @{$z->_cartesian};
1fa12f56 957 return 0 if $x == 1 && $y == 0;
a8693bd3 958 my $t1 = CORE::sqrt(($x+1)*($x+1) + $y*$y);
959 my $t2 = CORE::sqrt(($x-1)*($x-1) + $y*$y);
fb73857a 960 my $alpha = ($t1 + $t2)/2;
961 my $beta = ($t1 - $t2)/2;
962 $alpha = 1 if $alpha < 1;
963 if ($beta > 1) { $beta = 1 }
964 elsif ($beta < -1) { $beta = -1 }
a8693bd3 965 my $u = CORE::atan2(CORE::sqrt(1-$beta*$beta), $beta);
966 my $v = CORE::log($alpha + CORE::sqrt($alpha*$alpha-1));
fb73857a 967 $v = -$v if $y > 0 || ($y == 0 && $x < -1);
40b904b7 968 return (ref $z)->make($u, $v);
66730be0 969}
970
971#
972# asin
973#
974# Computes the arc sine asin(z) = -i log(iz + sqrt(1-z*z)).
975#
976sub asin {
fb73857a 977 my $z = $_[0];
1fa12f56 978 return CORE::atan2($z, CORE::sqrt(1-$z*$z))
979 if (! ref $z) && CORE::abs($z) <= 1;
40b904b7 980 $z = cplx($z, 0) unless ref $z;
affad850 981 my ($x, $y) = @{$z->_cartesian};
1fa12f56 982 return 0 if $x == 0 && $y == 0;
a8693bd3 983 my $t1 = CORE::sqrt(($x+1)*($x+1) + $y*$y);
984 my $t2 = CORE::sqrt(($x-1)*($x-1) + $y*$y);
fb73857a 985 my $alpha = ($t1 + $t2)/2;
986 my $beta = ($t1 - $t2)/2;
987 $alpha = 1 if $alpha < 1;
988 if ($beta > 1) { $beta = 1 }
989 elsif ($beta < -1) { $beta = -1 }
a8693bd3 990 my $u = CORE::atan2($beta, CORE::sqrt(1-$beta*$beta));
991 my $v = -CORE::log($alpha + CORE::sqrt($alpha*$alpha-1));
fb73857a 992 $v = -$v if $y > 0 || ($y == 0 && $x < -1);
40b904b7 993 return (ref $z)->make($u, $v);
66730be0 994}
995
996#
997# atan
998#
0c721ce2 999# Computes the arc tangent atan(z) = i/2 log((i+z) / (i-z)).
66730be0 1000#
1001sub atan {
1002 my ($z) = @_;
a8693bd3 1003 return CORE::atan2($z, 1) unless ref $z;
affad850 1004 my ($x, $y) = ref $z ? @{$z->_cartesian} : ($z, 0);
1fa12f56 1005 return 0 if $x == 0 && $y == 0;
8c03c583 1006 _divbyzero "atan(i)" if ( $z == i);
1fa12f56 1007 _logofzero "atan(-i)" if (-$z == i); # -i is a bad file test...
1008 my $log = &log((i + $z) / (i - $z));
affad850 1009 return _ip2 * $log;
a0d0e21e 1010}
1011
66730be0 1012#
0c721ce2 1013# asec
1014#
1015# Computes the arc secant asec(z) = acos(1 / z).
1016#
1017sub asec {
1018 my ($z) = @_;
0e505df1 1019 _divbyzero "asec($z)", $z if ($z == 0);
fb73857a 1020 return acos(1 / $z);
0c721ce2 1021}
1022
1023#
5cd24f17 1024# acsc
0c721ce2 1025#
8c03c583 1026# Computes the arc cosecant acsc(z) = asin(1 / z).
0c721ce2 1027#
5cd24f17 1028sub acsc {
0c721ce2 1029 my ($z) = @_;
0e505df1 1030 _divbyzero "acsc($z)", $z if ($z == 0);
fb73857a 1031 return asin(1 / $z);
0c721ce2 1032}
1033
1034#
5cd24f17 1035# acosec
66730be0 1036#
5cd24f17 1037# Alias for acsc().
0c721ce2 1038#
5cd24f17 1039sub acosec { Math::Complex::acsc(@_) }
0c721ce2 1040
66730be0 1041#
0c721ce2 1042# acot
1043#
8c03c583 1044# Computes the arc cotangent acot(z) = atan(1 / z)
0c721ce2 1045#
1046sub acot {
66730be0 1047 my ($z) = @_;
1fa12f56 1048 _divbyzero "acot(0)" if $z == 0;
1049 return ($z >= 0) ? CORE::atan2(1, $z) : CORE::atan2(-1, -$z)
1050 unless ref $z;
1051 _divbyzero "acot(i)" if ($z - i == 0);
1052 _logofzero "acot(-i)" if ($z + i == 0);
8c03c583 1053 return atan(1 / $z);
66730be0 1054}
1055
1056#
0c721ce2 1057# acotan
1058#
1059# Alias for acot().
1060#
1061sub acotan { Math::Complex::acot(@_) }
1062
1063#
66730be0 1064# cosh
1065#
1066# Computes the hyperbolic cosine cosh(z) = (exp(z) + exp(-z))/2.
1067#
1068sub cosh {
1069 my ($z) = @_;
fb73857a 1070 my $ex;
0e505df1 1071 unless (ref $z) {
a8693bd3 1072 $ex = CORE::exp($z);
1fa12f56 1073 return $ex ? ($ex + 1/$ex)/2 : $Inf;
0e505df1 1074 }
affad850 1075 my ($x, $y) = @{$z->_cartesian};
a8693bd3 1076 $ex = CORE::exp($x);
1fa12f56 1077 my $ex_1 = $ex ? 1 / $ex : $Inf;
a8693bd3 1078 return (ref $z)->make(CORE::cos($y) * ($ex + $ex_1)/2,
1079 CORE::sin($y) * ($ex - $ex_1)/2);
66730be0 1080}
1081
1082#
1083# sinh
1084#
1085# Computes the hyperbolic sine sinh(z) = (exp(z) - exp(-z))/2.
1086#
1087sub sinh {
1088 my ($z) = @_;
fb73857a 1089 my $ex;
0e505df1 1090 unless (ref $z) {
1fa12f56 1091 return 0 if $z == 0;
a8693bd3 1092 $ex = CORE::exp($z);
1fa12f56 1093 return $ex ? ($ex - 1/$ex)/2 : "-$Inf";
0e505df1 1094 }
affad850 1095 my ($x, $y) = @{$z->_cartesian};
1fa12f56 1096 my $cy = CORE::cos($y);
1097 my $sy = CORE::sin($y);
a8693bd3 1098 $ex = CORE::exp($x);
1fa12f56 1099 my $ex_1 = $ex ? 1 / $ex : $Inf;
5240e574 1100 return (ref $z)->make(CORE::cos($y) * ($ex - $ex_1)/2,
1101 CORE::sin($y) * ($ex + $ex_1)/2);
66730be0 1102}
1103
1104#
1105# tanh
1106#
1107# Computes the hyperbolic tangent tanh(z) = sinh(z) / cosh(z).
1108#
1109sub tanh {
1110 my ($z) = @_;
0c721ce2 1111 my $cz = cosh($z);
0e505df1 1112 _divbyzero "tanh($z)", "cosh($z)" if ($cz == 0);
0c721ce2 1113 return sinh($z) / $cz;
66730be0 1114}
1115
1116#
0c721ce2 1117# sech
1118#
1119# Computes the hyperbolic secant sech(z) = 1 / cosh(z).
1120#
1121sub sech {
1122 my ($z) = @_;
1123 my $cz = cosh($z);
0e505df1 1124 _divbyzero "sech($z)", "cosh($z)" if ($cz == 0);
0c721ce2 1125 return 1 / $cz;
1126}
1127
1128#
1129# csch
1130#
1131# Computes the hyperbolic cosecant csch(z) = 1 / sinh(z).
66730be0 1132#
0c721ce2 1133sub csch {
1134 my ($z) = @_;
1135 my $sz = sinh($z);
0e505df1 1136 _divbyzero "csch($z)", "sinh($z)" if ($sz == 0);
0c721ce2 1137 return 1 / $sz;
1138}
1139
1140#
1141# cosech
1142#
1143# Alias for csch().
1144#
1145sub cosech { Math::Complex::csch(@_) }
1146
66730be0 1147#
0c721ce2 1148# coth
1149#
1150# Computes the hyperbolic cotangent coth(z) = cosh(z) / sinh(z).
1151#
1152sub coth {
66730be0 1153 my ($z) = @_;
0c721ce2 1154 my $sz = sinh($z);
1fa12f56 1155 _divbyzero "coth($z)", "sinh($z)" if $sz == 0;
0c721ce2 1156 return cosh($z) / $sz;
66730be0 1157}
1158
1159#
0c721ce2 1160# cotanh
1161#
1162# Alias for coth().
1163#
1164sub cotanh { Math::Complex::coth(@_) }
1165
1166#
66730be0 1167# acosh
1168#
fb73857a 1169# Computes the arc hyperbolic cosine acosh(z) = log(z + sqrt(z*z-1)).
66730be0 1170#
1171sub acosh {
1172 my ($z) = @_;
fb73857a 1173 unless (ref $z) {
fb73857a 1174 $z = cplx($z, 0);
1175 }
affad850 1176 my ($re, $im) = @{$z->_cartesian};
fb73857a 1177 if ($im == 0) {
1fa12f56 1178 return CORE::log($re + CORE::sqrt($re*$re - 1))
1179 if $re >= 1;
1180 return cplx(0, CORE::atan2(CORE::sqrt(1 - $re*$re), $re))
1181 if CORE::abs($re) < 1;
fb73857a 1182 }
9bc5fa8d 1183 my $t = &sqrt($z * $z - 1) + $z;
40b904b7 1184 # Try Taylor if looking bad (this usually means that
1185 # $z was large negative, therefore the sqrt is really
1186 # close to abs(z), summing that with z...)
9bc5fa8d 1187 $t = 1/(2 * $z) - 1/(8 * $z**3) + 1/(16 * $z**5) - 5/(128 * $z**7)
1188 if $t == 0;
1189 my $u = &log($t);
40b904b7 1190 $u->Im(-$u->Im) if $re < 0 && $im == 0;
9bc5fa8d 1191 return $re < 0 ? -$u : $u;
66730be0 1192}
1193
1194#
1195# asinh
1196#
1fa12f56 1197# Computes the arc hyperbolic sine asinh(z) = log(z + sqrt(z*z+1))
66730be0 1198#
1199sub asinh {
1200 my ($z) = @_;
1fa12f56 1201 unless (ref $z) {
1202 my $t = $z + CORE::sqrt($z*$z + 1);
1203 return CORE::log($t) if $t;
1204 }
9bc5fa8d 1205 my $t = &sqrt($z * $z + 1) + $z;
40b904b7 1206 # Try Taylor if looking bad (this usually means that
1207 # $z was large negative, therefore the sqrt is really
1208 # close to abs(z), summing that with z...)
9bc5fa8d 1209 $t = 1/(2 * $z) - 1/(8 * $z**3) + 1/(16 * $z**5) - 5/(128 * $z**7)
1210 if $t == 0;
1fa12f56 1211 return &log($t);
66730be0 1212}
1213
1214#
1215# atanh
1216#
1217# Computes the arc hyperbolic tangent atanh(z) = 1/2 log((1+z) / (1-z)).
1218#
1219sub atanh {
1220 my ($z) = @_;
fb73857a 1221 unless (ref $z) {
a8693bd3 1222 return CORE::log((1 + $z)/(1 - $z))/2 if CORE::abs($z) < 1;
fb73857a 1223 $z = cplx($z, 0);
1224 }
1fa12f56 1225 _divbyzero 'atanh(1)', "1 - $z" if (1 - $z == 0);
1226 _logofzero 'atanh(-1)' if (1 + $z == 0);
1227 return 0.5 * &log((1 + $z) / (1 - $z));
66730be0 1228}
1229
1230#
0c721ce2 1231# asech
1232#
1233# Computes the hyperbolic arc secant asech(z) = acosh(1 / z).
1234#
1235sub asech {
1236 my ($z) = @_;
1fa12f56 1237 _divbyzero 'asech(0)', "$z" if ($z == 0);
0c721ce2 1238 return acosh(1 / $z);
1239}
1240
1241#
1242# acsch
66730be0 1243#
0c721ce2 1244# Computes the hyperbolic arc cosecant acsch(z) = asinh(1 / z).
66730be0 1245#
0c721ce2 1246sub acsch {
66730be0 1247 my ($z) = @_;
0e505df1 1248 _divbyzero 'acsch(0)', $z if ($z == 0);
0c721ce2 1249 return asinh(1 / $z);
1250}
1251
1252#
1253# acosech
1254#
1255# Alias for acosh().
1256#
1257sub acosech { Math::Complex::acsch(@_) }
1258
1259#
1260# acoth
1261#
1262# Computes the arc hyperbolic cotangent acoth(z) = 1/2 log((1+z) / (z-1)).
1263#
1264sub acoth {
1265 my ($z) = @_;
1fa12f56 1266 _divbyzero 'acoth(0)' if ($z == 0);
fb73857a 1267 unless (ref $z) {
a8693bd3 1268 return CORE::log(($z + 1)/($z - 1))/2 if CORE::abs($z) > 1;
fb73857a 1269 $z = cplx($z, 0);
1270 }
1fa12f56 1271 _divbyzero 'acoth(1)', "$z - 1" if ($z - 1 == 0);
1272 _logofzero 'acoth(-1)', "1 + $z" if (1 + $z == 0);
1273 return &log((1 + $z) / ($z - 1)) / 2;
66730be0 1274}
1275
1276#
0c721ce2 1277# acotanh
1278#
1279# Alias for acot().
1280#
1281sub acotanh { Math::Complex::acoth(@_) }
1282
1283#
66730be0 1284# (atan2)
1285#
bf5f1b4c 1286# Compute atan(z1/z2), minding the right quadrant.
66730be0 1287#
1288sub atan2 {
1289 my ($z1, $z2, $inverted) = @_;
fb73857a 1290 my ($re1, $im1, $re2, $im2);
1291 if ($inverted) {
affad850 1292 ($re1, $im1) = ref $z2 ? @{$z2->_cartesian} : ($z2, 0);
1293 ($re2, $im2) = ref $z1 ? @{$z1->_cartesian} : ($z1, 0);
66730be0 1294 } else {
affad850 1295 ($re1, $im1) = ref $z1 ? @{$z1->_cartesian} : ($z1, 0);
1296 ($re2, $im2) = ref $z2 ? @{$z2->_cartesian} : ($z2, 0);
fb73857a 1297 }
bf5f1b4c 1298 if ($im1 || $im2) {
1299 # In MATLAB the imaginary parts are ignored.
1300 # warn "atan2: Imaginary parts ignored";
1301 # http://documents.wolfram.com/mathematica/functions/ArcTan
1302 # NOTE: Mathematica ArcTan[x,y] while atan2(y,x)
1303 my $s = $z1 * $z1 + $z2 * $z2;
1304 _divbyzero("atan2") if $s == 0;
1305 my $i = &i;
1306 my $r = $z2 + $z1 * $i;
1307 return -$i * &log($r / &sqrt( $s ));
66730be0 1308 }
bf5f1b4c 1309 return CORE::atan2($re1, $re2);
66730be0 1310}
1311
1312#
1313# display_format
1314# ->display_format
1315#
16357284 1316# Set (get if no argument) the display format for all complex numbers that
fb73857a 1317# don't happen to have overridden it via ->display_format
66730be0 1318#
16357284 1319# When called as an object method, this actually sets the display format for
66730be0 1320# the current object.
1321#
1322# Valid object formats are 'c' and 'p' for cartesian and polar. The first
1323# letter is used actually, so the type can be fully spelled out for clarity.
1324#
1325sub display_format {
16357284 1326 my $self = shift;
1327 my %display_format = %DISPLAY_FORMAT;
66730be0 1328
16357284 1329 if (ref $self) { # Called as an object method
1330 if (exists $self->{display_format}) {
1331 my %obj = %{$self->{display_format}};
1332 @display_format{keys %obj} = values %obj;
1333 }
476757f7 1334 }
1335 if (@_ == 1) {
1336 $display_format{style} = shift;
1337 } else {
1338 my %new = @_;
1339 @display_format{keys %new} = values %new;
66730be0 1340 }
1341
476757f7 1342 if (ref $self) { # Called as an object method
16357284 1343 $self->{display_format} = { %display_format };
1344 return
1345 wantarray ?
1346 %{$self->{display_format}} :
1347 $self->{display_format}->{style};
66730be0 1348 }
1349
476757f7 1350 # Called as a class method
16357284 1351 %DISPLAY_FORMAT = %display_format;
1352 return
1353 wantarray ?
1354 %DISPLAY_FORMAT :
1355 $DISPLAY_FORMAT{style};
66730be0 1356}
1357
1358#
affad850 1359# (_stringify)
66730be0 1360#
1361# Show nicely formatted complex number under its cartesian or polar form,
1362# depending on the current display format:
1363#
1364# . If a specific display format has been recorded for this object, use it.
1365# . Otherwise, use the generic current default for all complex numbers,
1366# which is a package global variable.
1367#
affad850 1368sub _stringify {
66730be0 1369 my ($z) = shift;
66730be0 1370
16357284 1371 my $style = $z->display_format;
1372
1373 $style = $DISPLAY_FORMAT{style} unless defined $style;
66730be0 1374
affad850 1375 return $z->_stringify_polar if $style =~ /^p/i;
1376 return $z->_stringify_cartesian;
66730be0 1377}
1378
1379#
affad850 1380# ->_stringify_cartesian
66730be0 1381#
1382# Stringify as a cartesian representation 'a+bi'.
1383#
affad850 1384sub _stringify_cartesian {
66730be0 1385 my $z = shift;
affad850 1386 my ($x, $y) = @{$z->_cartesian};
66730be0 1387 my ($re, $im);
1388
16357284 1389 my %format = $z->display_format;
1390 my $format = $format{format};
1391
1fa12f56 1392 if ($x) {
1393 if ($x =~ /^NaN[QS]?$/i) {
1394 $re = $x;
1395 } else {
1396 if ($x =~ /^-?$Inf$/oi) {
1397 $re = $x;
1398 } else {
1399 $re = defined $format ? sprintf($format, $x) : $x;
1400 }
1401 }
1402 } else {
1403 undef $re;
1404 }
1405
1406 if ($y) {
40b904b7 1407 if ($y =~ /^(NaN[QS]?)$/i) {
1fa12f56 1408 $im = $y;
1409 } else {
1410 if ($y =~ /^-?$Inf$/oi) {
1411 $im = $y;
1412 } else {
40b904b7 1413 $im =
1414 defined $format ?
1415 sprintf($format, $y) :
1416 ($y == 1 ? "" : ($y == -1 ? "-" : $y));
1fa12f56 1417 }
1418 }
1419 $im .= "i";
1420 } else {
1421 undef $im;
16357284 1422 }
66730be0 1423
1fa12f56 1424 my $str = $re;
1425
16357284 1426 if (defined $im) {
1427 if ($y < 0) {
1428 $str .= $im;
1fa12f56 1429 } elsif ($y > 0 || $im =~ /^NaN[QS]?i$/i) {
16357284 1430 $str .= "+" if defined $re;
1431 $str .= $im;
1432 }
1fa12f56 1433 } elsif (!defined $re) {
1434 $str = "0";
16357284 1435 }
66730be0 1436
1437 return $str;
1438}
1439
d09ae4e6 1440
66730be0 1441#
affad850 1442# ->_stringify_polar
66730be0 1443#
1444# Stringify as a polar representation '[r,t]'.
1445#
affad850 1446sub _stringify_polar {
66730be0 1447 my $z = shift;
affad850 1448 my ($r, $t) = @{$z->_polar};
66730be0 1449 my $theta;
1450
16357284 1451 my %format = $z->display_format;
1fa12f56 1452 my $format = $format{format};
16357284 1453
1fa12f56 1454 if ($t =~ /^NaN[QS]?$/i || $t =~ /^-?$Inf$/oi) {
1455 $theta = $t;
1456 } elsif ($t == pi) {
1457 $theta = "pi";
1458 } elsif ($r == 0 || $t == 0) {
1459 $theta = defined $format ? sprintf($format, $t) : $t;
55497cff 1460 }
66730be0 1461
1fa12f56 1462 return "[$r,$theta]" if defined $theta;
1463
66730be0 1464 #
1fa12f56 1465 # Try to identify pi/n and friends.
66730be0 1466 #
1467
affad850 1468 $t -= int(CORE::abs($t) / pi2) * pi2;
1fa12f56 1469
e97e26fa 1470 if ($format{polar_pretty_print} && $t) {
1fa12f56 1471 my ($a, $b);
9bc5fa8d 1472 for $a (2..9) {
1fa12f56 1473 $b = $t * $a / pi;
e97e26fa 1474 if ($b =~ /^-?\d+$/) {
1fa12f56 1475 $b = $b < 0 ? "-" : "" if CORE::abs($b) == 1;
1476 $theta = "${b}pi/$a";
d09ae4e6 1477 last;
66730be0 1478 }
d09ae4e6 1479 }
66730be0 1480 }
1481
16357284 1482 if (defined $format) {
1483 $r = sprintf($format, $r);
1fa12f56 1484 $theta = sprintf($format, $theta) unless defined $theta;
1485 } else {
1486 $theta = $t unless defined $theta;
16357284 1487 }
1488
1fa12f56 1489 return "[$r,$theta]";
a0d0e21e 1490}
a5f75d66 1491
14921;
1493__END__
1494
1cf6bcb8 1495=pod
1496
a5f75d66 1497=head1 NAME
1498
66730be0 1499Math::Complex - complex numbers and associated mathematical functions
a5f75d66 1500
1501=head1 SYNOPSIS
1502
66730be0 1503 use Math::Complex;
fb73857a 1504
66730be0 1505 $z = Math::Complex->make(5, 6);
1506 $t = 4 - 3*i + $z;
1507 $j = cplxe(1, 2*pi/3);
a5f75d66 1508
1509=head1 DESCRIPTION
1510
66730be0 1511This package lets you create and manipulate complex numbers. By default,
1512I<Perl> limits itself to real numbers, but an extra C<use> statement brings
1513full complex support, along with a full set of mathematical functions
1514typically associated with and/or extended to complex numbers.
1515
1516If you wonder what complex numbers are, they were invented to be able to solve
1517the following equation:
1518
1519 x*x = -1
1520
1521and by definition, the solution is noted I<i> (engineers use I<j> instead since
1522I<i> usually denotes an intensity, but the name does not matter). The number
1523I<i> is a pure I<imaginary> number.
1524
1525The arithmetics with pure imaginary numbers works just like you would expect
1526it with real numbers... you just have to remember that
1527
1528 i*i = -1
1529
1530so you have:
1531
1532 5i + 7i = i * (5 + 7) = 12i
1533 4i - 3i = i * (4 - 3) = i
1534 4i * 2i = -8
1535 6i / 2i = 3
1536 1 / i = -i
1537
1538Complex numbers are numbers that have both a real part and an imaginary
1539part, and are usually noted:
1540
1541 a + bi
1542
1543where C<a> is the I<real> part and C<b> is the I<imaginary> part. The
1544arithmetic with complex numbers is straightforward. You have to
1545keep track of the real and the imaginary parts, but otherwise the
1546rules used for real numbers just apply:
1547
1548 (4 + 3i) + (5 - 2i) = (4 + 5) + i(3 - 2) = 9 + i
1549 (2 + i) * (4 - i) = 2*4 + 4i -2i -i*i = 8 + 2i + 1 = 9 + 2i
1550
1551A graphical representation of complex numbers is possible in a plane
1552(also called the I<complex plane>, but it's really a 2D plane).
1553The number
1554
1555 z = a + bi
1556
1557is the point whose coordinates are (a, b). Actually, it would
1558be the vector originating from (0, 0) to (a, b). It follows that the addition
1559of two complex numbers is a vectorial addition.
1560
1561Since there is a bijection between a point in the 2D plane and a complex
1562number (i.e. the mapping is unique and reciprocal), a complex number
1563can also be uniquely identified with polar coordinates:
1564
1565 [rho, theta]
1566
1567where C<rho> is the distance to the origin, and C<theta> the angle between
1568the vector and the I<x> axis. There is a notation for this using the
1569exponential form, which is:
1570
1571 rho * exp(i * theta)
1572
1573where I<i> is the famous imaginary number introduced above. Conversion
1574between this form and the cartesian form C<a + bi> is immediate:
1575
1576 a = rho * cos(theta)
1577 b = rho * sin(theta)
1578
1579which is also expressed by this formula:
1580
fb73857a 1581 z = rho * exp(i * theta) = rho * (cos theta + i * sin theta)
66730be0 1582
1583In other words, it's the projection of the vector onto the I<x> and I<y>
1584axes. Mathematicians call I<rho> the I<norm> or I<modulus> and I<theta>
affad850 1585the I<argument> of the complex number. The I<norm> of C<z> is
1586marked here as C<abs(z)>.
66730be0 1587
affad850 1588The polar notation (also known as the trigonometric representation) is
1589much more handy for performing multiplications and divisions of
1590complex numbers, whilst the cartesian notation is better suited for
1591additions and subtractions. Real numbers are on the I<x> axis, and
1592therefore I<y> or I<theta> is zero or I<pi>.
66730be0 1593
1594All the common operations that can be performed on a real number have
1595been defined to work on complex numbers as well, and are merely
1596I<extensions> of the operations defined on real numbers. This means
1597they keep their natural meaning when there is no imaginary part, provided
1598the number is within their definition set.
1599
1600For instance, the C<sqrt> routine which computes the square root of
fb73857a 1601its argument is only defined for non-negative real numbers and yields a
1602non-negative real number (it is an application from B<R+> to B<R+>).
66730be0 1603If we allow it to return a complex number, then it can be extended to
1604negative real numbers to become an application from B<R> to B<C> (the
1605set of complex numbers):
1606
1607 sqrt(x) = x >= 0 ? sqrt(x) : sqrt(-x)*i
1608
1609It can also be extended to be an application from B<C> to B<C>,
1610whilst its restriction to B<R> behaves as defined above by using
1611the following definition:
1612
1613 sqrt(z = [r,t]) = sqrt(r) * exp(i * t/2)
1614
fb73857a 1615Indeed, a negative real number can be noted C<[x,pi]> (the modulus
1616I<x> is always non-negative, so C<[x,pi]> is really C<-x>, a negative
1617number) and the above definition states that
66730be0 1618
1619 sqrt([x,pi]) = sqrt(x) * exp(i*pi/2) = [sqrt(x),pi/2] = sqrt(x)*i
1620
1621which is exactly what we had defined for negative real numbers above.
b42d0ec9 1622The C<sqrt> returns only one of the solutions: if you want the both,
1623use the C<root> function.
a5f75d66 1624
66730be0 1625All the common mathematical functions defined on real numbers that
1626are extended to complex numbers share that same property of working
1627I<as usual> when the imaginary part is zero (otherwise, it would not
1628be called an extension, would it?).
a5f75d66 1629
66730be0 1630A I<new> operation possible on a complex number that is
1631the identity for real numbers is called the I<conjugate>, and is noted
d1be9408 1632with a horizontal bar above the number, or C<~z> here.
a5f75d66 1633
66730be0 1634 z = a + bi
1635 ~z = a - bi
a5f75d66 1636
66730be0 1637Simple... Now look:
a5f75d66 1638
66730be0 1639 z * ~z = (a + bi) * (a - bi) = a*a + b*b
a5f75d66 1640
66730be0 1641We saw that the norm of C<z> was noted C<abs(z)> and was defined as the
1642distance to the origin, also known as:
a5f75d66 1643
66730be0 1644 rho = abs(z) = sqrt(a*a + b*b)
a5f75d66 1645
66730be0 1646so
1647
1648 z * ~z = abs(z) ** 2
1649
1650If z is a pure real number (i.e. C<b == 0>), then the above yields:
1651
1652 a * a = abs(a) ** 2
1653
1654which is true (C<abs> has the regular meaning for real number, i.e. stands
1655for the absolute value). This example explains why the norm of C<z> is
1656noted C<abs(z)>: it extends the C<abs> function to complex numbers, yet
1657is the regular C<abs> we know when the complex number actually has no
1658imaginary part... This justifies I<a posteriori> our use of the C<abs>
1659notation for the norm.
1660
1661=head1 OPERATIONS
1662
1663Given the following notations:
1664
1665 z1 = a + bi = r1 * exp(i * t1)
1666 z2 = c + di = r2 * exp(i * t2)
1667 z = <any complex or real number>
1668
1669the following (overloaded) operations are supported on complex numbers:
1670
1671 z1 + z2 = (a + c) + i(b + d)
1672 z1 - z2 = (a - c) + i(b - d)
1673 z1 * z2 = (r1 * r2) * exp(i * (t1 + t2))
1674 z1 / z2 = (r1 / r2) * exp(i * (t1 - t2))
1675 z1 ** z2 = exp(z2 * log z1)
b42d0ec9 1676 ~z = a - bi
1677 abs(z) = r1 = sqrt(a*a + b*b)
1678 sqrt(z) = sqrt(r1) * exp(i * t/2)
1679 exp(z) = exp(a) * exp(i * b)
1680 log(z) = log(r1) + i*t
1681 sin(z) = 1/2i (exp(i * z1) - exp(-i * z))
1682 cos(z) = 1/2 (exp(i * z1) + exp(-i * z))
bf5f1b4c 1683 atan2(y, x) = atan(y / x) # Minding the right quadrant, note the order.
1684
1685The definition used for complex arguments of atan2() is
1686
1687 -i log((x + iy)/sqrt(x*x+y*y))
66730be0 1688
affad850 1689Note that atan2(0, 0) is not well-defined.
1690
66730be0 1691The following extra operations are supported on both real and complex
1692numbers:
1693
1694 Re(z) = a
1695 Im(z) = b
1696 arg(z) = t
b42d0ec9 1697 abs(z) = r
66730be0 1698
1699 cbrt(z) = z ** (1/3)
1700 log10(z) = log(z) / log(10)
1701 logn(z, n) = log(z) / log(n)
1702
1703 tan(z) = sin(z) / cos(z)
0c721ce2 1704
5aabfad6 1705 csc(z) = 1 / sin(z)
1706 sec(z) = 1 / cos(z)
0c721ce2 1707 cot(z) = 1 / tan(z)
66730be0 1708
1709 asin(z) = -i * log(i*z + sqrt(1-z*z))
fb73857a 1710 acos(z) = -i * log(z + i*sqrt(1-z*z))
66730be0 1711 atan(z) = i/2 * log((i+z) / (i-z))
0c721ce2 1712
5aabfad6 1713 acsc(z) = asin(1 / z)
1714 asec(z) = acos(1 / z)
8c03c583 1715 acot(z) = atan(1 / z) = -i/2 * log((i+z) / (z-i))
66730be0 1716
1717 sinh(z) = 1/2 (exp(z) - exp(-z))
1718 cosh(z) = 1/2 (exp(z) + exp(-z))
0c721ce2 1719 tanh(z) = sinh(z) / cosh(z) = (exp(z) - exp(-z)) / (exp(z) + exp(-z))
1720
5aabfad6 1721 csch(z) = 1 / sinh(z)
1722 sech(z) = 1 / cosh(z)
0c721ce2 1723 coth(z) = 1 / tanh(z)
fb73857a 1724
66730be0 1725 asinh(z) = log(z + sqrt(z*z+1))
1726 acosh(z) = log(z + sqrt(z*z-1))
1727 atanh(z) = 1/2 * log((1+z) / (1-z))
66730be0 1728
5aabfad6 1729 acsch(z) = asinh(1 / z)
1730 asech(z) = acosh(1 / z)
0c721ce2 1731 acoth(z) = atanh(1 / z) = 1/2 * log((1+z) / (z-1))
1732
b42d0ec9 1733I<arg>, I<abs>, I<log>, I<csc>, I<cot>, I<acsc>, I<acot>, I<csch>,
1734I<coth>, I<acosech>, I<acotanh>, have aliases I<rho>, I<theta>, I<ln>,
1735I<cosec>, I<cotan>, I<acosec>, I<acotan>, I<cosech>, I<cotanh>,
1736I<acosech>, I<acotanh>, respectively. C<Re>, C<Im>, C<arg>, C<abs>,
d1be9408 1737C<rho>, and C<theta> can be used also as mutators. The C<cbrt>
b42d0ec9 1738returns only one of the solutions: if you want all three, use the
1739C<root> function.
0c721ce2 1740
1741The I<root> function is available to compute all the I<n>
66730be0 1742roots of some complex, where I<n> is a strictly positive integer.
1743There are exactly I<n> such roots, returned as a list. Getting the
1744number mathematicians call C<j> such that:
1745
1746 1 + j + j*j = 0;
1747
1748is a simple matter of writing:
1749
1750 $j = ((root(1, 3))[1];
1751
1752The I<k>th root for C<z = [r,t]> is given by:
1753
1754 (root(z, n))[k] = r**(1/n) * exp(i * (t + 2*k*pi)/n)
1755
bf5f1b4c 1756You can return the I<k>th root directly by C<root(z, n, k)>,
1757indexing starting from I<zero> and ending at I<n - 1>.
1758
f4837644 1759The I<spaceship> comparison operator, E<lt>=E<gt>, is also defined. In
1760order to ensure its restriction to real numbers is conform to what you
1761would expect, the comparison is run on the real part of the complex
1762number first, and imaginary parts are compared only when the real
1763parts match.
66730be0 1764
1765=head1 CREATION
1766
1767To create a complex number, use either:
1768
1769 $z = Math::Complex->make(3, 4);
1770 $z = cplx(3, 4);
1771
1772if you know the cartesian form of the number, or
1773
1774 $z = 3 + 4*i;
1775
fb73857a 1776if you like. To create a number using the polar form, use either:
66730be0 1777
1778 $z = Math::Complex->emake(5, pi/3);
1779 $x = cplxe(5, pi/3);
1780
0c721ce2 1781instead. The first argument is the modulus, the second is the angle
fb73857a 1782(in radians, the full circle is 2*pi). (Mnemonic: C<e> is used as a
1783notation for complex numbers in the polar form).
66730be0 1784
1785It is possible to write:
1786
1787 $x = cplxe(-3, pi/4);
1788
16357284 1789but that will be silently converted into C<[3,-3pi/4]>, since the
1790modulus must be non-negative (it represents the distance to the origin
1791in the complex plane).
66730be0 1792
91cb744f 1793It is also possible to have a complex number as either argument of the
1794C<make>, C<emake>, C<cplx>, and C<cplxe>: the appropriate component of
b42d0ec9 1795the argument will be used.
1796
1797 $z1 = cplx(-2, 1);
1798 $z2 = cplx($z1, 4);
1799
91cb744f 1800The C<new>, C<make>, C<emake>, C<cplx>, and C<cplxe> will also
1801understand a single (string) argument of the forms
1802
1803 2-3i
1804 -3i
1805 [2,3]
bf5f1b4c 1806 [2,-3pi/4]
91cb744f 1807 [2]
1808
1809in which case the appropriate cartesian and exponential components
1810will be parsed from the string and used to create new complex numbers.
1811The imaginary component and the theta, respectively, will default to zero.
1812
bf5f1b4c 1813The C<new>, C<make>, C<emake>, C<cplx>, and C<cplxe> will also
1814understand the case of no arguments: this means plain zero or (0, 0).
1815
1816=head1 DISPLAYING
66730be0 1817
1818When printed, a complex number is usually shown under its cartesian
16357284 1819style I<a+bi>, but there are legitimate cases where the polar style
bf5f1b4c 1820I<[r,t]> is more appropriate. The process of converting the complex
1821number into a string that can be displayed is known as I<stringification>.
66730be0 1822
16357284 1823By calling the class method C<Math::Complex::display_format> and
1824supplying either C<"polar"> or C<"cartesian"> as an argument, you
5287f86b 1825override the default display style, which is C<"cartesian">. Not
16357284 1826supplying any argument returns the current settings.
66730be0 1827
1828This default can be overridden on a per-number basis by calling the
1829C<display_format> method instead. As before, not supplying any argument
5287f86b 1830returns the current display style for this number. Otherwise whatever you
1831specify will be the new display style for I<this> particular number.
66730be0 1832
1833For instance:
1834
1835 use Math::Complex;
1836
1837 Math::Complex::display_format('polar');
16357284 1838 $j = (root(1, 3))[1];
1839 print "j = $j\n"; # Prints "j = [1,2pi/3]"
66730be0 1840 $j->display_format('cartesian');
1841 print "j = $j\n"; # Prints "j = -0.5+0.866025403784439i"
1842
5287f86b 1843The polar style attempts to emphasize arguments like I<k*pi/n>
9bc5fa8d 1844(where I<n> is a positive integer and I<k> an integer within [-9, +9]),
5287f86b 1845this is called I<polar pretty-printing>.
66730be0 1846
bf5f1b4c 1847For the reverse of stringifying, see the C<make> and C<emake>.
1848
16357284 1849=head2 CHANGED IN PERL 5.6
1850
1851The C<display_format> class method and the corresponding
1852C<display_format> object method can now be called using
1853a parameter hash instead of just a one parameter.
1854
1855The old display format style, which can have values C<"cartesian"> or
40b904b7 1856C<"polar">, can be changed using the C<"style"> parameter.
1857
1858 $j->display_format(style => "polar");
1859
1860The one parameter calling convention also still works.
1861
1862 $j->display_format("polar");
16357284 1863
1864There are two new display parameters.
1865
40b904b7 1866The first one is C<"format">, which is a sprintf()-style format string
1867to be used for both numeric parts of the complex number(s). The is
1868somewhat system-dependent but most often it corresponds to C<"%.15g">.
1869You can revert to the default by setting the C<format> to C<undef>.
16357284 1870
1871 # the $j from the above example
1872
1873 $j->display_format('format' => '%.5f');
1874 print "j = $j\n"; # Prints "j = -0.50000+0.86603i"
40b904b7 1875 $j->display_format('format' => undef);
16357284 1876 print "j = $j\n"; # Prints "j = -0.5+0.86603i"
1877
1878Notice that this affects also the return values of the
1879C<display_format> methods: in list context the whole parameter hash
40b904b7 1880will be returned, as opposed to only the style parameter value.
1881This is a potential incompatibility with earlier versions if you
1882have been calling the C<display_format> method in list context.
16357284 1883
5287f86b 1884The second new display parameter is C<"polar_pretty_print">, which can
1885be set to true or false, the default being true. See the previous
1886section for what this means.
16357284 1887
66730be0 1888=head1 USAGE
1889
1890Thanks to overloading, the handling of arithmetics with complex numbers
1891is simple and almost transparent.
1892
1893Here are some examples:
1894
1895 use Math::Complex;
1896
1897 $j = cplxe(1, 2*pi/3); # $j ** 3 == 1
1898 print "j = $j, j**3 = ", $j ** 3, "\n";
1899 print "1 + j + j**2 = ", 1 + $j + $j**2, "\n";
1900
1901 $z = -16 + 0*i; # Force it to be a complex
1902 print "sqrt($z) = ", sqrt($z), "\n";
1903
1904 $k = exp(i * 2*pi/3);
1905 print "$j - $k = ", $j - $k, "\n";
a5f75d66 1906
b42d0ec9 1907 $z->Re(3); # Re, Im, arg, abs,
1908 $j->arg(2); # (the last two aka rho, theta)
1909 # can be used also as mutators.
1910
affad850 1911=head2 PI
1912
1913The constant C<pi> and some handy multiples of it (pi2, pi4,
1914and pip2 (pi/2) and pip4 (pi/4)) are also available if separately
1915exported:
1916
1917 use Math::Complex ':pi';
1918 $third_of_circle = pi2 / 3;
1919
b42d0ec9 1920=head1 ERRORS DUE TO DIVISION BY ZERO OR LOGARITHM OF ZERO
5aabfad6 1921
1922The division (/) and the following functions
1923
b42d0ec9 1924 log ln log10 logn
2820d885 1925 tan sec csc cot
b42d0ec9 1926 atan asec acsc acot
1927 tanh sech csch coth
1928 atanh asech acsch acoth
5aabfad6 1929
1930cannot be computed for all arguments because that would mean dividing
8c03c583 1931by zero or taking logarithm of zero. These situations cause fatal
1932runtime errors looking like this
5aabfad6 1933
1934 cot(0): Division by zero.
5cd24f17 1935 (Because in the definition of cot(0), the divisor sin(0) is 0)
5aabfad6 1936 Died at ...
1937
8c03c583 1938or
1939
1940 atanh(-1): Logarithm of zero.
1941 Died at...
1942
1943For the C<csc>, C<cot>, C<asec>, C<acsc>, C<acot>, C<csch>, C<coth>,
d1be9408 1944C<asech>, C<acsch>, the argument cannot be C<0> (zero). For the
b42d0ec9 1945logarithmic functions and the C<atanh>, C<acoth>, the argument cannot
1946be C<1> (one). For the C<atanh>, C<acoth>, the argument cannot be
1947C<-1> (minus one). For the C<atan>, C<acot>, the argument cannot be
1948C<i> (the imaginary unit). For the C<atan>, C<acoth>, the argument
1949cannot be C<-i> (the negative imaginary unit). For the C<tan>,
1950C<sec>, C<tanh>, the argument cannot be I<pi/2 + k * pi>, where I<k>
bf5f1b4c 1951is any integer. atan2(0, 0) is undefined, and if the complex arguments
1952are used for atan2(), a division by zero will happen if z1**2+z2**2 == 0.
b42d0ec9 1953
1954Note that because we are operating on approximations of real numbers,
1955these errors can happen when merely `too close' to the singularities
40b904b7 1956listed above.
b42d0ec9 1957
1958=head1 ERRORS DUE TO INDIGESTIBLE ARGUMENTS
1959
1960The C<make> and C<emake> accept both real and complex arguments.
1961When they cannot recognize the arguments they will die with error
1962messages like the following
1963
1964 Math::Complex::make: Cannot take real part of ...
1965 Math::Complex::make: Cannot take real part of ...
1966 Math::Complex::emake: Cannot take rho of ...
1967 Math::Complex::emake: Cannot take theta of ...
5cd24f17 1968
a5f75d66 1969=head1 BUGS
1970
5cd24f17 1971Saying C<use Math::Complex;> exports many mathematical routines in the
bf5f1b4c 1972caller environment and even overrides some (C<sqrt>, C<log>, C<atan2>).
fb73857a 1973This is construed as a feature by the Authors, actually... ;-)
a5f75d66 1974
66730be0 1975All routines expect to be given real or complex numbers. Don't attempt to
1976use BigFloat, since Perl has currently no rule to disambiguate a '+'
1977operation (for instance) between two overloaded entities.
a5f75d66 1978
d09ae4e6 1979In Cray UNICOS there is some strange numerical instability that results
1980in root(), cos(), sin(), cosh(), sinh(), losing accuracy fast. Beware.
1981The bug may be in UNICOS math libs, in UNICOS C compiler, in Math::Complex.
1982Whatever it is, it does not manifest itself anywhere else where Perl runs.
1983
0c721ce2 1984=head1 AUTHORS
a5f75d66 1985
affad850 1986Daniel S. Lewart <F<lewart!at!uiuc.edu>>
1987Jarkko Hietaniemi <F<jhi!at!iki.fi>>
1988Raphael Manfredi <F<Raphael_Manfredi!at!pobox.com>>
fb73857a 1989
5cd24f17 1990=cut
1991
b42d0ec9 19921;
1993
5cd24f17 1994# eof