Math-Complex
view release on metacpan or search on metacpan
lib/Math/Complex.pm view on Meta::CPAN
return __PACKAGE__->make(@_);
}
#
# cplxe
#
# Creates a complex number from a (rho, theta) tuple.
# This avoids the burden of writing Math::Complex->emake(rho, theta).
#
sub cplxe {
return __PACKAGE__->emake(@_);
}
#
# pi
#
# The number defined as pi = 180 degrees
#
sub pi () { 4 * CORE::atan2(1, 1) }
#
# pi2
#
# The full circle
#
sub pi2 () { 2 * pi }
#
# pi4
#
# The full circle twice.
#
sub pi4 () { 4 * pi }
#
# pip2
#
# The quarter circle
#
sub pip2 () { pi / 2 }
#
# pip4
#
# The eighth circle.
#
sub pip4 () { pi / 4 }
#
# _uplog10
#
# Used in log10().
#
sub _uplog10 () { 1 / CORE::log(10) }
#
# i
#
# The number defined as i*i = -1;
#
sub i () {
return $i if ($i);
$i = bless {};
$i->{'cartesian'} = [0, 1];
$i->{'polar'} = [1, pip2];
$i->{c_dirty} = 0;
$i->{p_dirty} = 0;
return $i;
}
#
# _ip2
#
# Half of i.
#
sub _ip2 () { i / 2 }
#
# Attribute access/set routines
#
sub _cartesian {$_[0]->{c_dirty} ?
$_[0]->_update_cartesian : $_[0]->{'cartesian'}}
sub _polar {$_[0]->{p_dirty} ?
$_[0]->_update_polar : $_[0]->{'polar'}}
sub _set_cartesian { $_[0]->{p_dirty}++; $_[0]->{c_dirty} = 0;
$_[0]->{'cartesian'} = $_[1] }
sub _set_polar { $_[0]->{c_dirty}++; $_[0]->{p_dirty} = 0;
$_[0]->{'polar'} = $_[1] }
#
# ->_update_cartesian
#
# Recompute and return the cartesian form, given accurate polar form.
#
sub _update_cartesian {
my $self = shift;
my ($r, $t) = @{$self->{'polar'}};
$self->{c_dirty} = 0;
return $self->{'cartesian'} = [$r * CORE::cos($t), $r * CORE::sin($t)];
}
#
#
# ->_update_polar
#
# Recompute and return the polar form, given accurate cartesian form.
#
sub _update_polar {
my $self = shift;
my ($x, $y) = @{$self->{'cartesian'}};
$self->{p_dirty} = 0;
return $self->{'polar'} = [0, 0] if $x == 0 && $y == 0;
return $self->{'polar'} = [CORE::sqrt($x*$x + $y*$y),
CORE::atan2($y, $x)];
}
#
# (_plus)
#
( run in 1.400 second using v1.01-cache-2.11-cpan-524268b4103 )