Math::Trig(3) Perl Programmers Reference Guide Math::Trig(3) 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); DESCRIPTION Math::Trig defines many trigonometric functions not defined by the core Perl which defines only the sin() and cos(). The constantpiis also defined as are a few convenience functions for angle conversions. TRIGONOMETRIC FUNCTIONS The tangenttanThe cofunctions of the sine, cosine, and tangent (cosec/csc and cotan/cot are aliases)csc,cosec,sec,sec,cot,cotanThe arcus (also known as the inverse) functions of the sine, cosine, and tangentasin,acos,atanThe principal value of the arc tangent of y/xatan2(y, x) The arcus cofunctions of the sine, cosine, and tangent (acosec/acsc and acotan/acot are aliases)acsc,acosec,asec,acot,acotanThe hyperbolic sine, cosine, and tangentsinh,cosh,tanhThe cofunctions of the hyperbolic sine, cosine, and tangent (cosech/csch and cotanh/coth are aliases)csch,cosech,sech,coth,cotanhThe arcus (also known as the inverse) functions of the hyperbolic sine, cosine, and tangentasinh,acosh,atanhThe arcus cofunctions of the hyperbolic sine, cosine, and tangent (acsch/acosech and acoth/acotanh are aliases)acsch,acosech,asech,acoth,acotanhThe trigonometric constantpiis also defined. $pi2 = 2 *pi;ERRORS DUE TO DIVISION BY ZEROThe 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 dividing by zero or taking logarithm of zero. These situations cause fatal runtime errors looking like this cot(0): Division by zero. (Because in the definition of cot(0), the divisor sin(0) is 0) Died at ... or atanh(-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 argument cannot be -1 (minus one). For the tan, sec, tanh, sech, the argument cannot bepi/2 + k *pi, wherekis any integer.SIMPLE (REAL) ARGUMENTS, COMPLEX RESULTSPlease note that some of the trigonometric functions can break out from thereal axisinto thecomplex plane. For example asin(2) has no definition for plain real numbers but it has definition for complex numbers. In Perl terms this means that supplying the usual Perl numbers (also known as scalars, please see theperldatamanpage) 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 theMath::Complexmanpage for more information. In practice you need not to worry about getting complex 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 and the imaginary part of approximately -1.317. PLANE ANGLE CONVERSIONS (Plane, 2-dimensional) angles may be converted with the following functions. $radians = deg2rad($degrees); $radians = grad2rad($gradians); $degrees = rad2deg($radians); $degrees = grad2deg($gradians); $gradians = deg2grad($degrees); $gradians = rad2grad($radians); The full circle is 2piradians or360degrees or400gradians. RADIAL COORDINATE CONVERSIONSRadial coordinate systemsare thesphericaland thecylindricalsystems, explained shortly in more detail. You can import radial coordinate conversion functions by using the :radial tag: use Math::Trig ':radial'; ($rho, $theta, $z) = cartesian_to_cylindrical($x, $y, $z); ($rho, $theta, $phi) = cartesian_to_spherical($x, $y, $z); ($x, $y, $z) = cylindrical_to_cartesian($rho, $theta, $z); ($rho_s, $theta, $phi) = cylindrical_to_spherical($rho_c, $theta, $z); ($x, $y, $z) = spherical_to_cartesian($rho, $theta, $phi); ($rho_c, $theta, $z) = spherical_to_cylindrical($rho_s, $theta, $phi);All angles are in radians.COORDINATE SYSTEMSCartesiancoordinates are the usual rectangular(x, y, z)-coordinates. Spherical coordinates,(rho, theta, pi), are three-dimensional coordinates which define a point in three-dimensional space. They are based on a sphere surface. The radius of the sphere isrho, also known as theradialcoordinate. The angle in thexy-plane (around thez-axis) istheta, also known as theazimuthalcoordinate. The angle from thez-axis isphi, also known as thepolarcoordinate. The `North Pole' is therefore0, 0, rho, and the `Bay of Guinea' (think of the missing big chunk of Africa)0, pi/2, rho.Beware: some texts definethetaandphithe other way round, some texts define thephito start from the horizontal plane, some texts userin place ofrho. Cylindrical coordinates,(rho, theta, z), are three-dimensional coordinates which define a point in three-dimensional space. They are based on a cylinder surface. The radius of the cylinder isrho, also known as theradialcoordinate. The angle in thexy-plane (around thez-axis) istheta, also known as theazimuthalcoordinate. The third coordinate is thez, pointing up from thetheta-plane.3-D ANGLE CONVERSIONSConversions to and from spherical and cylindrical coordinates are available. Please notice that the conversions are not necessarily reversible because of the equalities likepiangles being equal to-piangles. cartesian_to_cylindrical ($rho, $theta, $z) = cartesian_to_cylindrical($x, $y, $z); cartesian_to_spherical ($rho, $theta, $phi) = cartesian_to_spherical($x, $y, $z); cylindrical_to_cartesian ($x, $y, $z) = cylindrical_to_cartesian($rho, $theta, $z); cylindrical_to_spherical ($rho_s, $theta, $phi) = cylindrical_to_spherical($rho_c, $theta, $z); Notice that when $z is not 0 $rho_s is not equal to $rho_c. spherical_to_cartesian ($x, $y, $z) = spherical_to_cartesian($rho, $theta, $phi); spherical_to_cylindrical ($rho_c, $theta, $z) = spherical_to_cylindrical($rho_s, $theta, $phi); Notice that when $z is not 0 $rho_c is not equal to $rho_s. GREAT CIRCLE DISTANCES You can compute spherical distances, calledgreat circle distances, by importing the great_circle_distance function: use Math::Trig 'great_circle_distance' $distance = great_circle_distance($theta0, $phi0, $theta1, $phi, [, $rho]); Thegreat circle distanceis the shortest distance between two points on 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. EXAMPLES To calculate the distance between London (51.3N 0.5W) and Tokyo (35.7N 139.8E) in kilometers: use Math::Trig qw(great_circle_distance deg2rad); # Notice the 90 - latitude: phi zero is at the North Pole. @L = (deg2rad(-0.5), deg2rad(90 - 51.3)); @T = (deg2rad(139.8),deg2rad(90 - 35.7)); $km = great_circle_distance(@L, @T, 6378); The answer may be off by up to 0.3% because of the irregular (slightly aspherical) form of the Earth. BUGS Saying use Math::Trig; exports many mathematical routines in the caller environment and even overrides some (sin, cos). This is construed 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. AUTHORS Jarkko Hietaniemi <jhi@iki.fi> and Raphael Manfredi <Raphael_Manfredi@grenoble.hp.com>. 3rd Berkeley Distribution perl 5.005, patch 02 Math::Trig(3)