NAME
Math::Trig - trigonometric functions
SYNOPSIS
use Math::Trig;
$x = tan(0.9);
$y = acos(3.7);
$z = asin(2.4);
$halfpi = pi/2;
$rad = deg2rad(120);
# Import constants pi2, pip2, pip4 (2*pi, pi/2, pi/4).
use Math::Trig ':pi';
# Import the conversions between cartesian/spherical/cylindrical.
use Math::Trig ':radial';
# Import the great circle formulas.
use Math::Trig ':greatcircle';
DESCRIPTION
"Math::Trig" defines many trigonometric functions not defined by the
core Perl which defines only the "sin()" and "cos()". The constant ppiiis also defined as are a few convenience functions for angle conver-
sions, and great circle formulas for spherical movement. TTRRIIGGOONNOOMMEETTRRIICC FFUUNNCCTTIIOONNSS The tangent ttaann The cofunctions of the sine, cosine, and tangent (cosec/csc and cotan/cot are aliases) ccsscc, ccoosseecc, sseecc, sseecc, ccoott, ccoottaann The arcus (also known as the inverse) functions of the sine, cosine, and tangent aassiinn, aaccooss, aattaann The principal value of the arc tangent of y/x aattaann22(y, x) The arcus cofunctions of the sine, cosine, and tangent (acosec/acsc and acotan/acot are aliases) aaccsscc, aaccoosseecc, aasseecc, aaccoott, aaccoottaann The hyperbolic sine, cosine, and tangent ssiinnhh, ccoosshh, ttaannhh The cofunctions of the hyperbolic sine, cosine, and tangent (cosech/csch and cotanh/coth are aliases) ccsscchh, ccoosseecchh, sseecchh, ccootthh, ccoottaannhh The arcus (also known as the inverse) functions of the hyperbolic sine, cosine, and tangent aassiinnhh, aaccoosshh, aattaannhh The arcus cofunctions of the hyperbolic sine, cosine, and tangent (acsch/acosech and acoth/acotanh are aliases) aaccsscchh, aaccoosseecchh, aasseecchh, aaccootthh, aaccoottaannhh The trigonometric constant ppii is also defined.$pi2 = 2 * ppii;
EERRRROORRSS DDUUEE TTOO DDIIVVIISSIIOONN BBYY ZZEERROO The following functions acoth acsc acsch asec asech atanh cot coth csc csch sec sech tan tanh cannot be computed for all arguments because that would mean dividingby zero or taking logarithm of zero. These situations cause fatal run-
time errors looking like this cot(0): Division by zero. (Because in the definition of cot(0), the divisor sin(0) is 0) Died at ... oratanh(-1): Logarithm of zero.
Died at... For the "csc", "cot", "asec", "acsc", "acot", "csch", "coth", "asech", "acsch", the argument cannot be 0 (zero). For the "atanh", "acoth", the argument cannot be 1 (one). For the "atanh", "acoth", the argumentcannot be "-1" (minus one). For the "tan", "sec", "tanh", "sech", the
argument cannot be pi/2 + k * pi, where k is any integer. atan2(0, 0) is undefined. SSIIMMPPLLEE ((RREEAALL)) AARRGGUUMMEENNTTSS,, CCOOMMPPLLEEXX RREESSUULLTTSS Please note that some of the trigonometric functions can break out fromthe rreeaall aaxxiiss into the ccoommpplleexx ppllaannee. For example asin(2) has no defi-
nition for plain real numbers but it has definition for complex num-
bers. In Perl terms this means that supplying the usual Perl numbers (also known as scalars, please see perldata) as input for the trigonometric functions might produce as output results that no more are simple real numbers: instead they are complex numbers.The "Math::Trig" handles this by using the "Math::Complex" package
which knows how to handle complex numbers, please see Math::Complex formore information. In practice you need not to worry about getting com-
plex numbers as results because the "Math::Complex" takes care of details like for example how to display complex numbers. For example: print asin(2), "\n"; should produce something like this (take or leave few last decimals):1.5707963267949-1.31695789692482i
That is, a complex number with the real part of approximately 1.571 andthe imaginary part of approximately "-1.317".
PPLLAANNEE AANNGGLLEE CCOONNVVEERRSSIIOONNSS(Plane, 2-dimensional) angles may be converted with the following func-
tions.$radians = deg2rad($degrees);
$radians = grad2rad($gradians);
$degrees = rad2deg($radians);
$degrees = grad2deg($gradians);
$gradians = deg2grad($degrees);
$gradians = rad2grad($radians);
The full circle is 2 pi radians or 360 degrees or 400 gradians. Theresult is by default wrapped to be inside the [0, {2pi,360,400}[ cir-
cle. If you don't want this, supply a true second argument:$zillionsofradians = deg2rad($zillionsofdegrees, 1);
$negativedegrees = rad2deg($negativeradians, 1);
You can also do the wrapping explicitly by rad2rad(), deg2deg(), and grad2grad(). RRAADDIIAALL CCOOOORRDDIINNAATTEE CCOONNVVEERRSSIIOONNSSRRaaddiiaall ccoooorrddiinnaattee ssyysstteemmss are the sspphheerriiccaall and the ccyylliinnddrriiccaall sys-
tems, explained shortly in more detail. You can import radial coordinate conversion functions by using the ":radial" tag:use Math::Trig ':radial';
($rho, $theta, $z) = cartesiantocylindrical($x, $y, $z);
($rho, $theta, $phi) = cartesiantospherical($x, $y, $z);
($x, $y, $z) = cylindricaltocartesian($rho, $theta, $z);
($rhos, $theta, $phi) = cylindricaltospherical($rhoc, $theta, $z);
($x, $y, $z) = sphericaltocartesian($rho, $theta, $phi);
($rhoc, $theta, $z) = sphericaltocylindrical($rhos, $theta, $phi);
AAllll aanngglleess aarree iinn rraaddiiaannss. CCOOOORRDDIINNAATTEE SSYYSSTTEEMMSSCCaarrtteessiiaann coordinates are the usual rectangular (x, y, z)-coordinates.
Spherical coordinates, (rho, theta, pi), are three-dimensional coordi-
nates which define a point in three-dimensional space. They are based
on a sphere surface. The radius of the sphere is rrhhoo, also known asthe radial coordinate. The angle in the xy-plane (around the z-axis)
is tthheettaa, also known as the azimuthal coordinate. The angle from thez-axis is pphhii, also known as the polar coordinate. The North Pole is
therefore 0, 0, rho, and the Gulf of Guinea (think of the missing big chunk of Africa) 0, pi/2, rho. In geographical terms phi is latitude(northward positive, southward negative) and theta is longitude (east-
ward positive, westward negative). BBEEWWAARREE: some texts define theta and phi the other way round, some texts define the phi to start from the horizontal plane, some texts use r in place of rho.Cylindrical coordinates, (rho, theta, z), are three-dimensional coordi-
nates which define a point in three-dimensional space. They are based
on a cylinder surface. The radius of the cylinder is rrhhoo, also knownas the radial coordinate. The angle in the xy-plane (around the
z-axis) is tthheettaa, also known as the azimuthal coordinate. The third
coordinate is the z, pointing up from the tthheettaa-plane.
33-DD AANNGGLLEE CCOONNVVEERRSSIIOONNSS
Conversions to and from spherical and cylindrical coordinates are available. Please notice that the conversions are not necessarilyreversible because of the equalities like pi angles being equal to -pi
angles. cartesiantocylindrical($rho, $theta, $z) = cartesiantocylindrical($x, $y, $z);
cartesiantospherical($rho, $theta, $phi) = cartesiantospherical($x, $y, $z);
cylindricaltocartesian($x, $y, $z) = cylindricaltocartesian($rho, $theta, $z);
cylindricaltospherical($rhos, $theta, $phi) = cylindricaltospherical($rhoc, $theta, $z);
Notice that when $z is not 0 $rhos is not equal to $rhoc.
sphericaltocartesian($x, $y, $z) = sphericaltocartesian($rho, $theta, $phi);
sphericaltocylindrical($rhoc, $theta, $z) = sphericaltocylindrical($rhos, $theta, $phi);
Notice that when $z is not 0 $rhoc is not equal to $rhos.
GGRREEAATT CCIIRRCCLLEE DDIISSTTAANNCCEESS AANNDD DDIIRREECCTTIIOONNSS You can compute spherical distances, called ggrreeaatt cciirrccllee ddiissttaanncceess, by importing the greatcircledistance() function:use Math::Trig 'greatcircledistance';
$distance = greatcircledistance($theta0, $phi0, $theta1, $phi1, [, $rho]);
The great circle distance is the shortest distance between two pointson a sphere. The distance is in $rho units. The $rho is optional, it
defaults to 1 (the unit sphere), therefore the distance defaults to radians. If you think geographically the theta are longitudes: zero at theGreenwhich meridian, eastward positive, westward negative-and the phi
are latitudes: zero at the North Pole, northward positive, southwardnegative. NNOOTTEE: this formula thinks in mathematics, not geographi-
cally: the phi zero is at the North Pole, not at the Equator on thewest coast of Africa (Bay of Guinea). You need to subtract your geo-
graphical coordinates from pi/2 (also known as 90 degrees).$distance = greatcircledistance($lon0, pi/2 - $lat0,
$lon1, pi/2 - $lat1, $rho);
The direction you must follow the great circle (also known as bearing) can be computed by the greatcircledirection() function:use Math::Trig 'greatcircledirection';
$direction = greatcircledirection($theta0, $phi0, $theta1, $phi1);
(Alias 'greatcirclebearing' is also available.) The result is inradians, zero indicating straight north, pi or -pi straight south, pi/2
straight west, and -pi/2 straight east.
You can inversely compute the destination if you know the starting point, direction, and distance:use Math::Trig 'greatcircledestination';
# thetad and phid are the destination coordinates,
# dird is the final direction at the destination.
($thetad, $phid, $dird) =
greatcircledestination($theta, $phi, $direction, $distance);
or the midpoint if you know the end points:use Math::Trig 'greatcirclemidpoint';
($thetam, $phim) =
greatcirclemidpoint($theta0, $phi0, $theta1, $phi1);
The greatcirclemidpoint() is just a special case ofuse Math::Trig 'greatcirclewaypoint';
($thetai, $phii) =
greatcirclewaypoint($theta0, $phi0, $theta1, $phi1, $way);
Where the $way is a value from zero ($theta0, $phi0) to one ($theta1,
$phi1). Note that antipodal points (where their distance is pi radi-
ans) do not have waypoints between them (they would have an an "equa-
tor" between them), and therefore "undef" is returned for antipodal points. If the points are the same and the distance therefore zero and all waypoints therefore identical, the first point (either point) is returned.The thetas, phis, direction, and distance in the above are all in radi-
ans. You can import all the great circle formulas byuse Math::Trig ':greatcircle';
Notice that the resulting directions might be somewhat surprising if you are looking at a flat worldmap: in such map projections the greatcircles quite often do not look like the shortest routes- but for
example the shortest possible routes from Europe or North America to Asia do often cross the polar regions. EEXXAAMMPPLLEESS To calculate the distance between London (51.3N 0.5W) and Tokyo (35.7N 139.8E) in kilometers:use Math::Trig qw(greatcircledistance deg2rad);
# Notice the 90 - latitude: phi zero is at the North Pole.
sub NESW { deg2rad($[0]), deg2rad(90 - $[1]) }
my @L = NESW( -0.5, 51.3);
my @T = NESW(139.8, 35.7);my $km = greatcircledistance(@L, @T, 6378); # About 9600 km.
The direction you would have to go from London to Tokyo (in radians, straight north being zero, straight east being pi/2).use Math::Trig qw(greatcircledirection);
my $rad = greatcircledirection(@L, @T); # About 0.547 or 0.174 pi.
The midpoint between London and Tokyo beinguse Math::Trig qw(greatcirclemidpoint);
my @M = greatcirclemidpoint(@L, @T); or about 68.11N 24.74E, in the Finnish Lapland. CCAAVVEEAATT FFOORR GGRREEAATT CCIIRRCCLLEE FFOORRMMUULLAASS The answers may be off by few percentages because of the irregular (slightly aspherical) form of the Earth. The errors are at worst about0.55%, but generally below 0.3%.
BUGS
Saying "use Math::Trig;" exports many mathematical routines in the
caller environment and even overrides some ("sin", "cos"). This isconstrued as a feature by the Authors, actually... ;-)
The code is not optimized for speed, especially because we use "Math::Complex" and thus go quite near complex numbers while doing the computations even when the arguments are not. This, however, cannot be completely avoided if we want things like asin(2) to give an answer instead of giving a fatal runtime error. Do not attempt navigation using these formulas. AUTHORSJarkko Hietaniemi
and Raphael Manfredi fredi@pobox.com>. perl v5.8.8 2001-09-21 Math::Trig(3pm)