Cross Reference: Trig.pm
xref
: /
osnet-11
/
usr
/
src
/
cmd
/
perl
/
5.8.4
/
distrib
/
lib
/
Math
/
Trig.pm
Home
History
Annotate
Line#
Navigate
Download
Search
only in
./
1
N/A
#
1
N/A
# Trigonometric functions, mostly inherited from Math::Complex.
1
N/A
# -- Jarkko Hietaniemi, since April 1997
1
N/A
# -- Raphael Manfredi, September 1996 (indirectly: because of Math::Complex)
1
N/A
#
1
N/A
1
N/A
require
Exporter
;
1
N/A
package
Math
::
Trig
;
1
N/A
1
N/A
use
5.006
;
1
N/A
use
strict
;
1
N/A
1
N/A
use
Math
::
Complex
qw
(:
trig
);
1
N/A
1
N/A
our
($
VERSION
, $
PACKAGE
, @
ISA
, @
EXPORT
, @
EXPORT_OK
, %
EXPORT_TAGS
);
1
N/A
1
N/A
@
ISA
=
qw
(
Exporter
);
1
N/A
1
N/A
$
VERSION
=
1.02
;
1
N/A
1
N/A
my
@
angcnv
=
qw
(
rad2deg
rad2grad
1
N/A
deg2rad
deg2grad
1
N/A
grad2rad
grad2deg
);
1
N/A
1
N/A
@
EXPORT
= (@{$
Math
::
Complex
::
EXPORT_TAGS
{
'trig'
}},
1
N/A
@
angcnv
);
1
N/A
1
N/A
my
@
rdlcnv
=
qw
(
cartesian_to_cylindrical
1
N/A
cartesian_to_spherical
1
N/A
cylindrical_to_cartesian
1
N/A
cylindrical_to_spherical
1
N/A
spherical_to_cartesian
1
N/A
spherical_to_cylindrical
);
1
N/A
1
N/A
@
EXPORT_OK
= (@
rdlcnv
,
'great_circle_distance'
,
'great_circle_direction'
);
1
N/A
1
N/A
%
EXPORT_TAGS
= (
'radial'
=> [ @
rdlcnv
]);
1
N/A
1
N/A
sub
pi2
() {
2
*
pi
}
1
N/A
sub
pip2
() {
pi
/
2
}
1
N/A
1
N/A
sub
DR
() {
pi2
/
360
}
1
N/A
sub
RD
() {
360
/
pi2
}
1
N/A
sub
DG
() {
400
/
360
}
1
N/A
sub
GD
() {
360
/
400
}
1
N/A
sub
RG
() {
400
/
pi2
}
1
N/A
sub
GR
() {
pi2
/
400
}
1
N/A
1
N/A
#
1
N/A
# Truncating remainder.
1
N/A
#
1
N/A
1
N/A
sub
remt
($$) {
1
N/A
# Oh yes, POSIX::fmod() would be faster. Possibly. If it is available.
1
N/A
$_[
0
] - $_[
1
] *
int
($_[
0
] / $_[
1
]);
1
N/A
}
1
N/A
1
N/A
#
1
N/A
# Angle conversions.
1
N/A
#
1
N/A
1
N/A
sub
rad2rad
($) {
remt
($_[
0
],
pi2
) }
1
N/A
1
N/A
sub
deg2deg
($) {
remt
($_[
0
],
360
) }
1
N/A
1
N/A
sub
grad2grad
($) {
remt
($_[
0
],
400
) }
1
N/A
1
N/A
sub
rad2deg
($;$) {
my
$d =
RD
* $_[
0
]; $_[
1
] ? $d :
deg2deg
($d) }
1
N/A
1
N/A
sub
deg2rad
($;$) {
my
$d =
DR
* $_[
0
]; $_[
1
] ? $d :
rad2rad
($d) }
1
N/A
1
N/A
sub
grad2deg
($;$) {
my
$d =
GD
* $_[
0
]; $_[
1
] ? $d :
deg2deg
($d) }
1
N/A
1
N/A
sub
deg2grad
($;$) {
my
$d =
DG
* $_[
0
]; $_[
1
] ? $d :
grad2grad
($d) }
1
N/A
1
N/A
sub
rad2grad
($;$) {
my
$d =
RG
* $_[
0
]; $_[
1
] ? $d :
grad2grad
($d) }
1
N/A
1
N/A
sub
grad2rad
($;$) {
my
$d =
GR
* $_[
0
]; $_[
1
] ? $d :
rad2rad
($d) }
1
N/A
1
N/A
sub
cartesian_to_spherical
{
1
N/A
my
( $x, $y, $z ) = @_;
1
N/A
1
N/A
my
$
rho
=
sqrt
( $x * $x + $y * $y + $z * $z );
1
N/A
1
N/A
return
( $
rho
,
1
N/A
atan2
( $y, $x ),
1
N/A
$
rho
?
acos
( $z / $
rho
) :
0
);
1
N/A
}
1
N/A
1
N/A
sub
spherical_to_cartesian
{
1
N/A
my
( $
rho
, $
theta
, $
phi
) = @_;
1
N/A
1
N/A
return
( $
rho
*
cos
( $
theta
) *
sin
( $
phi
),
1
N/A
$
rho
*
sin
( $
theta
) *
sin
( $
phi
),
1
N/A
$
rho
*
cos
( $
phi
) );
1
N/A
}
1
N/A
1
N/A
sub
spherical_to_cylindrical
{
1
N/A
my
( $x, $y, $z ) =
spherical_to_cartesian
( @_ );
1
N/A
1
N/A
return
(
sqrt
( $x * $x + $y * $y ), $_[
1
], $z );
1
N/A
}
1
N/A
1
N/A
sub
cartesian_to_cylindrical
{
1
N/A
my
( $x, $y, $z ) = @_;
1
N/A
1
N/A
return
(
sqrt
( $x * $x + $y * $y ),
atan2
( $y, $x ), $z );
1
N/A
}
1
N/A
1
N/A
sub
cylindrical_to_cartesian
{
1
N/A
my
( $
rho
, $
theta
, $z ) = @_;
1
N/A
1
N/A
return
( $
rho
*
cos
( $
theta
), $
rho
*
sin
( $
theta
), $z );
1
N/A
}
1
N/A
1
N/A
sub
cylindrical_to_spherical
{
1
N/A
return
(
cartesian_to_spherical
(
cylindrical_to_cartesian
( @_ ) ) );
1
N/A
}
1
N/A
1
N/A
sub
great_circle_distance
{
1
N/A
my
( $
theta0
, $
phi0
, $
theta1
, $
phi1
, $
rho
) = @_;
1
N/A
1
N/A
$
rho
=
1
unless
defined
$
rho
;
# Default to the unit sphere.
1
N/A
1
N/A
my
$
lat0
=
pip2
- $
phi0
;
1
N/A
my
$
lat1
=
pip2
- $
phi1
;
1
N/A
1
N/A
return
$
rho
*
1
N/A
acos
(
cos
( $
lat0
) *
cos
( $
lat1
) *
cos
( $
theta0
- $
theta1
) +
1
N/A
sin
( $
lat0
) *
sin
( $
lat1
) );
1
N/A
}
1
N/A
1
N/A
sub
great_circle_direction
{
1
N/A
my
( $
theta0
, $
phi0
, $
theta1
, $
phi1
) = @_;
1
N/A
1
N/A
my
$
distance
= &
great_circle_distance
;
1
N/A
1
N/A
my
$
lat0
=
pip2
- $
phi0
;
1
N/A
my
$
lat1
=
pip2
- $
phi1
;
1
N/A
1
N/A
my
$
direction
=
1
N/A
acos
((
sin
($
lat1
) -
sin
($
lat0
) *
cos
($
distance
)) /
1
N/A
(
cos
($
lat0
) *
sin
($
distance
)));
1
N/A
1
N/A
$
direction
=
pi2
- $
direction
1
N/A
if
sin
($
theta1
- $
theta0
) <
0
;
1
N/A
1
N/A
return
rad2rad
($
direction
);
1
N/A
}
1
N/A
1
N/A
1
;
1
N/A
1
N/A
__END__
1
N/A
=pod
1
N/A
1
N/A
=head1 NAME
1
N/A
1
N/A
Math::Trig - trigonometric functions
1
N/A
1
N/A
=head1 SYNOPSIS
1
N/A
1
N/A
use Math::Trig;
1
N/A
1
N/A
$x = tan(0.9);
1
N/A
$y = acos(3.7);
1
N/A
$z = asin(2.4);
1
N/A
1
N/A
$halfpi = pi/2;
1
N/A
1
N/A
$rad = deg2rad(120);
1
N/A
1
N/A
=head1 DESCRIPTION
1
N/A
1
N/A
C<Math::Trig> defines many trigonometric functions not defined by the
1
N/A
core Perl which defines only the C<sin()> and C<cos()>. The constant
1
N/A
B<pi> is also defined as are a few convenience functions for angle
1
N/A
conversions.
1
N/A
1
N/A
=head1 TRIGONOMETRIC FUNCTIONS
1
N/A
1
N/A
The tangent
1
N/A
1
N/A
=over 4
1
N/A
1
N/A
=item B<tan>
1
N/A
1
N/A
=back
1
N/A
1
N/A
The cofunctions of the sine, cosine, and tangent (
cosec
/
csc
and
cotan
/
cot
1
N/A
are aliases)
1
N/A
1
N/A
B<csc>, B<cosec>, B<sec>, B<sec>, B<cot>, B<cotan>
1
N/A
1
N/A
The arcus (also known as the inverse) functions of the sine, cosine,
1
N/A
and tangent
1
N/A
1
N/A
B<asin>, B<acos>, B<atan>
1
N/A
1
N/A
The principal value of the arc tangent of y/x
1
N/A
1
N/A
B<atan2>(y, x)
1
N/A
1
N/A
The arcus cofunctions of the sine, cosine, and tangent (
acosec
/
acsc
1
N/A
and
acotan
/
acot
are aliases)
1
N/A
1
N/A
B<acsc>, B<acosec>, B<asec>, B<acot>, B<acotan>
1
N/A
1
N/A
The hyperbolic sine, cosine, and tangent
1
N/A
1
N/A
B<sinh>, B<cosh>, B<tanh>
1
N/A
1
N/A
The cofunctions of the hyperbolic sine, cosine, and tangent (
cosech
/
csch
1
N/A
and
cotanh
/
coth
are aliases)
1
N/A
1
N/A
B<csch>, B<cosech>, B<sech>, B<coth>, B<cotanh>
1
N/A
1
N/A
The arcus (also known as the inverse) functions of the hyperbolic
1
N/A
sine, cosine, and tangent
1
N/A
1
N/A
B<asinh>, B<acosh>, B<atanh>
1
N/A
1
N/A
The arcus cofunctions of the hyperbolic sine, cosine, and tangent
1
N/A
(
acsch
/
acosech
and
acoth
/
acotanh
are aliases)
1
N/A
1
N/A
B<acsch>, B<acosech>, B<asech>, B<acoth>, B<acotanh>
1
N/A
1
N/A
The trigonometric constant B<pi> is also defined.
1
N/A
1
N/A
$pi2 = 2 * B<pi>;
1
N/A
1
N/A
=head2 ERRORS DUE TO DIVISION BY ZERO
1
N/A
1
N/A
The following functions
1
N/A
1
N/A
acoth
1
N/A
acsc
1
N/A
acsch
1
N/A
asec
1
N/A
asech
1
N/A
atanh
1
N/A
cot
1
N/A
coth
1
N/A
csc
1
N/A
csch
1
N/A
sec
1
N/A
sech
1
N/A
tan
1
N/A
tanh
1
N/A
1
N/A
cannot be computed for all arguments because that would mean dividing
1
N/A
by zero or taking logarithm of zero. These situations cause fatal
1
N/A
runtime errors looking like this
1
N/A
1
N/A
cot(0): Division by zero.
1
N/A
(Because in the definition of cot(0), the divisor sin(0) is 0)
1
N/A
Died at ...
1
N/A
1
N/A
or
1
N/A
1
N/A
atanh(-1): Logarithm of zero.
1
N/A
Died at...
1
N/A
1
N/A
For the C<csc>, C<cot>, C<asec>, C<acsc>, C<acot>, C<csch>, C<coth>,
1
N/A
C<asech>, C<acsch>, the argument cannot be C<0> (zero). For the
1
N/A
C<atanh>, C<acoth>, the argument cannot be C<1> (one). For the
1
N/A
C<atanh>, C<acoth>, the argument cannot be C<-1> (minus one). For the
1
N/A
C<tan>, C<sec>, C<tanh>, C<sech>, the argument cannot be I<pi/2 + k *
1
N/A
pi>, where I<k> is any integer.
1
N/A
1
N/A
=head2 SIMPLE (REAL) ARGUMENTS, COMPLEX RESULTS
1
N/A
1
N/A
Please note that some of the trigonometric functions can break out
1
N/A
from the B<real axis> into the B<complex plane>. For example
1
N/A
C<asin(2)> has no definition for plain real numbers but it has
1
N/A
definition for complex numbers.
1
N/A
1
N/A
In Perl terms this means that supplying the usual Perl numbers (also
1
N/A
known as scalars, please see L<perldata>) as input for the
1
N/A
trigonometric functions might produce as output results that no more
1
N/A
are simple real numbers: instead they are complex numbers.
1
N/A
1
N/A
The C<Math::Trig> handles this by using the C<Math::Complex> package
1
N/A
which knows how to handle complex numbers, please see L<Math::Complex>
1
N/A
for more information. In practice you need not to worry about getting
1
N/A
complex numbers as results because the C<Math::Complex> takes care of
1
N/A
details like for example how to display complex numbers. For example:
1
N/A
1
N/A
print asin(2), "\n";
1
N/A
1
N/A
should produce something like this (take or leave few last decimals):
1
N/A
1
N/A
1.5707963267949-1.31695789692482i
1
N/A
1
N/A
That is, a complex number with the real part of approximately C<1.571>
1
N/A
and the imaginary part of approximately C<-1.317>.
1
N/A
1
N/A
=head1 PLANE ANGLE CONVERSIONS
1
N/A
1
N/A
(Plane, 2-dimensional) angles may be converted with the following functions.
1
N/A
1
N/A
$radians = deg2rad($degrees);
1
N/A
$radians = grad2rad($gradians);
1
N/A
1
N/A
$degrees = rad2deg($radians);
1
N/A
$degrees = grad2deg($gradians);
1
N/A
1
N/A
$gradians = deg2grad($degrees);
1
N/A
$gradians = rad2grad($radians);
1
N/A
1
N/A
The full circle is 2 I<pi> radians or I<360> degrees or I<400> gradians.
1
N/A
The result is by default wrapped to be inside the [0, {2pi,360,400}[ circle.
1
N/A
If you don't want this, supply a true second argument:
1
N/A
1
N/A
$zillions_of_radians = deg2rad($zillions_of_degrees, 1);
1
N/A
$negative_degrees = rad2deg($negative_radians, 1);
1
N/A
1
N/A
You can also do the wrapping explicitly by rad2rad(), deg2deg(), and
1
N/A
grad2grad().
1
N/A
1
N/A
=head1 RADIAL COORDINATE CONVERSIONS
1
N/A
1
N/A
B<Radial coordinate systems> are the B<spherical> and the B<cylindrical>
1
N/A
systems, explained shortly in more detail.
1
N/A
1
N/A
You can import radial coordinate conversion functions by using the
1
N/A
C<:radial> tag:
1
N/A
1
N/A
use Math::Trig ':radial';
1
N/A
1
N/A
($rho, $theta, $z) = cartesian_to_cylindrical($x, $y, $z);
1
N/A
($rho, $theta, $phi) = cartesian_to_spherical($x, $y, $z);
1
N/A
($x, $y, $z) = cylindrical_to_cartesian($rho, $theta, $z);
1
N/A
($rho_s, $theta, $phi) = cylindrical_to_spherical($rho_c, $theta, $z);
1
N/A
($x, $y, $z) = spherical_to_cartesian($rho, $theta, $phi);
1
N/A
($rho_c, $theta, $z) = spherical_to_cylindrical($rho_s, $theta, $phi);
1
N/A
1
N/A
B<All angles are in radians>.
1
N/A
1
N/A
=head2 COORDINATE SYSTEMS
1
N/A
1
N/A
B<Cartesian> coordinates are the usual rectangular I<(x, y,
1
N/A
z)>-coordinates.
1
N/A
1
N/A
Spherical coordinates, I<(rho, theta, pi)>, are three-dimensional
1
N/A
coordinates which define a point in three-dimensional space. They are
1
N/A
based on a sphere surface. The radius of the sphere is B<rho>, also
1
N/A
known as the I<radial> coordinate. The angle in the I<xy>-plane
1
N/A
(around the I<z>-axis) is B<theta>, also known as the I<azimuthal>
1
N/A
coordinate. The angle from the I<z>-axis is B<phi>, also known as the
1
N/A
I<polar> coordinate. The `North Pole' is therefore I<0, 0, rho>, and
1
N/A
the `Bay of Guinea' (think of the missing big chunk of Africa) I<0,
1
N/A
pi/2, rho>. In geographical terms I<phi> is latitude (northward
1
N/A
positive, southward negative) and I<theta> is longitude (eastward
1
N/A
positive, westward negative).
1
N/A
1
N/A
B<BEWARE>: some texts define I<theta> and I<phi> the other way round,
1
N/A
some texts define the I<phi> to start from the horizontal plane, some
1
N/A
texts use I<r> in place of I<rho>.
1
N/A
1
N/A
Cylindrical coordinates, I<(rho, theta, z)>, are three-dimensional
1
N/A
coordinates which define a point in three-dimensional space. They are
1
N/A
based on a cylinder surface. The radius of the cylinder is B<rho>,
1
N/A
also known as the I<radial> coordinate. The angle in the I<xy>-plane
1
N/A
(around the I<z>-axis) is B<theta>, also known as the I<azimuthal>
1
N/A
coordinate. The third coordinate is the I<z>, pointing up from the
1
N/A
B<theta>-plane.
1
N/A
1
N/A
=head2 3-D ANGLE CONVERSIONS
1
N/A
1
N/A
Conversions to and from spherical and cylindrical coordinates are
1
N/A
available. Please notice that the conversions are not necessarily
1
N/A
reversible because of the equalities like I<pi> angles being equal to
1
N/A
I<-pi> angles.
1
N/A
1
N/A
=over 4
1
N/A
1
N/A
=item cartesian_to_cylindrical
1
N/A
1
N/A
($rho, $theta, $z) = cartesian_to_cylindrical($x, $y, $z);
1
N/A
1
N/A
=item cartesian_to_spherical
1
N/A
1
N/A
($rho, $theta, $phi) = cartesian_to_spherical($x, $y, $z);
1
N/A
1
N/A
=item cylindrical_to_cartesian
1
N/A
1
N/A
($x, $y, $z) = cylindrical_to_cartesian($rho, $theta, $z);
1
N/A
1
N/A
=item cylindrical_to_spherical
1
N/A
1
N/A
($rho_s, $theta, $phi) = cylindrical_to_spherical($rho_c, $theta, $z);
1
N/A
1
N/A
Notice that when C<$z> is not 0 C<$rho_s> is not equal to C<$rho_c>.
1
N/A
1
N/A
=item spherical_to_cartesian
1
N/A
1
N/A
($x, $y, $z) = spherical_to_cartesian($rho, $theta, $phi);
1
N/A
1
N/A
=item spherical_to_cylindrical
1
N/A
1
N/A
($rho_c, $theta, $z) = spherical_to_cylindrical($rho_s, $theta, $phi);
1
N/A
1
N/A
Notice that when C<$z> is not 0 C<$rho_c> is not equal to C<$rho_s>.
1
N/A
1
N/A
=back
1
N/A
1
N/A
=head1 GREAT CIRCLE DISTANCES AND DIRECTIONS
1
N/A
1
N/A
You can compute spherical distances, called B<great circle distances>,
1
N/A
by importing the great_circle_distance() function:
1
N/A
1
N/A
use Math::Trig 'great_circle_distance';
1
N/A
1
N/A
$distance = great_circle_distance($theta0, $phi0, $theta1, $phi1, [, $rho]);
1
N/A
1
N/A
The I<great circle distance> is the shortest distance between two
1
N/A
points on a sphere. The distance is in C<$rho> units. The C<$rho> is
1
N/A
optional, it defaults to 1 (the unit sphere), therefore the distance
1
N/A
defaults to radians.
1
N/A
1
N/A
If you think geographically the I<theta> are longitudes: zero at the
1
N/A
Greenwhich meridian, eastward positive, westward negative--and the
1
N/A
I<phi> are latitudes: zero at the North Pole, northward positive,
1
N/A
southward negative. B<NOTE>: this formula thinks in mathematics, not
1
N/A
geographically: the I<phi> zero is at the North Pole, not at the
1
N/A
Equator on the west coast of Africa (Bay of Guinea). You need to
1
N/A
subtract your geographical coordinates from I<pi/2> (also known as 90
1
N/A
degrees).
1
N/A
1
N/A
$distance = great_circle_distance($lon0, pi/2 - $lat0,
1
N/A
$lon1, pi/2 - $lat1, $rho);
1
N/A
1
N/A
The direction you must follow the great circle can be computed by the
1
N/A
great_circle_direction() function:
1
N/A
1
N/A
use Math::Trig 'great_circle_direction';
1
N/A
1
N/A
$direction = great_circle_direction($theta0, $phi0, $theta1, $phi1);
1
N/A
1
N/A
The result is in radians, zero indicating straight north, pi or -pi
1
N/A
straight south, pi/2 straight west, and -pi/2 straight east.
1
N/A
1
N/A
Notice that the resulting directions might be somewhat surprising if
1
N/A
you are looking at a flat worldmap: in such map projections the great
1
N/A
circles quite often do not look like the shortest routes-- but for
1
N/A
example the shortest possible routes from Europe or North America to
1
N/A
Asia do often cross the polar regions.
1
N/A
1
N/A
=head1 EXAMPLES
1
N/A
1
N/A
To calculate the distance between London (51.3N 0.5W) and Tokyo
1
N/A
(35.7N 139.8E) in kilometers:
1
N/A
1
N/A
use Math::Trig qw(great_circle_distance deg2rad);
1
N/A
1
N/A
# Notice the 90 - latitude: phi zero is at the North Pole.
1
N/A
@L = (deg2rad(-0.5), deg2rad(90 - 51.3));
1
N/A
@T = (deg2rad(139.8),deg2rad(90 - 35.7));
1
N/A
1
N/A
$km = great_circle_distance(@L, @T, 6378);
1
N/A
1
N/A
The direction you would have to go from London to Tokyo
1
N/A
1
N/A
use Math::Trig qw(great_circle_direction);
1
N/A
1
N/A
$rad = great_circle_direction(@L, @T);
1
N/A
1
N/A
=head2 CAVEAT FOR GREAT CIRCLE FORMULAS
1
N/A
1
N/A
The answers may be off by few percentages because of the irregular
1
N/A
(slightly aspherical) form of the Earth. The formula used for
1
N/A
grear circle distances
1
N/A
1
N/A
lat0 = 90 degrees - phi0
1
N/A
lat1 = 90 degrees - phi1
1
N/A
d = R * arccos(cos(lat0) * cos(lat1) * cos(lon1 - lon01) +
1
N/A
sin(lat0) * sin(lat1))
1
N/A
1
N/A
is also somewhat unreliable for small distances (for locations
1
N/A
separated less than about five degrees) because it uses arc cosine
1
N/A
which is rather ill-conditioned for values close to zero.
1
N/A
1
N/A
=head1 BUGS
1
N/A
1
N/A
Saying C<use Math::Trig;> exports many mathematical routines in the
1
N/A
caller environment and even overrides some (C<sin>, C<cos>). This is
1
N/A
construed as a feature by the Authors, actually... ;-)
1
N/A
1
N/A
The code is not optimized for speed, especially because we use
1
N/A
C<Math::Complex> and thus go quite near complex numbers while doing
1
N/A
the computations even when the arguments are not. This, however,
1
N/A
cannot be completely avoided if we want things like C<asin(2)> to give
1
N/A
an answer instead of giving a fatal runtime error.
1
N/A
1
N/A
=head1 AUTHORS
1
N/A
1
N/A
Jarkko Hietaniemi <F<jhi@iki.fi>> and
1
N/A
Raphael Manfredi <F<Raphael_Manfredi@pobox.com>>.
1
N/A
1
N/A
=cut
1
N/A
1
N/A
# eof