|P is for Practical|
perlman:Math::Trigby root (Scribe)
|on Dec 23, 1999 at 01:30 UTC||Need Help??|
See the current Perl documentation for Math::Trig.
Here is our local, out-dated (pre-5.6) version:
Math::Trig - trigonometric functions
use Math::Trig; $x = tan(0.9); $y = acos(3.7); $z = asin(2.4); $halfpi = pi/2;
$rad = deg2rad(120);
The cofunctions of the sine, cosine, and tangent (cosec/csc and cotan/cot are aliases)
csc, cosec, sec, sec, cot, cotan
The arcus (also known as the inverse) functions of the sine, cosine, and tangent
asin, acos, atan
The principal value of the arc tangent of y/x
The arcus cofunctions of the sine, cosine, and tangent (acosec/acsc and acotan/acot are aliases)
acsc, acosec, asec, acot, acotan
The hyperbolic sine, cosine, and tangent
sinh, cosh, tanh
The cofunctions of the hyperbolic sine, cosine, and tangent (cosech/csch and cotanh/coth are aliases)
csch, cosech, sech, coth, cotanh
The arcus (also known as the inverse) functions of the hyperbolic sine, cosine, and tangent
asinh, acosh, atanh
The arcus cofunctions of the hyperbolic sine, cosine, and tangent (acsch/acosech and acoth/acotanh are aliases)
acsch, acosech, asech, acoth, acotanh
The trigonometric constant pi is also defined.
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 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 ...
atanh(-1): Logarithm of zero. Died at...
Please note that some of the trigonometric functions can break out from the real axis into the complex plane. For example
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.
print asin(2), "\n"; should produce something like this (take or leave few last decimals):
That is, a complex number with the real part of approximately
(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 2 pi radians or 360 degrees or 400 gradians.
Radial coordinate systems are the spherical and the cylindrical systems, explained shortly in more detail.
You can import radial coordinate conversion functions by using the
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.
Cartesian coordinates 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 is rho, also known as the radial coordinate. The angle in the xy-plane (around the z-axis) is theta, also known as the azimuthal coordinate. The angle from the z-axis is phi, also known as the polar coordinate. The `North Pole' is therefore 0, 0, rho, and the `Bay of Guinea' (think of the missing big chunk of Africa) 0, pi/2, rho.
Beware: 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 coordinates which define a point in three-dimensional space. They are based on a cylinder surface. The radius of the cylinder is rho, also known as the radial coordinate. The angle in the xy-plane (around the z-axis) is theta, also known as the azimuthal coordinate. The third coordinate is the z, pointing up from the theta-plane.
Conversions to and from spherical and cylindrical coordinates are available. Please notice that the conversions are not necessarily reversible because of the equalities like pi angles being equal to -pi angles.
You can compute spherical distances, called great circle distances, by importing the
use Math::Trig 'great_circle_distance'
$distance = great_circle_distance($theta0, $phi0, $theta1, $phi, [, $rho]);
The great circle distance is the shortest distance between two points on a sphere. The distance is in
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.
The code is not optimized for speed, especially because we use
Jarkko Hietaniemi <firstname.lastname@example.org> and Raphael Manfredi <Raphael_Manfredi@grenoble.hp.com>.