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