<?xml version="1.0" encoding="windows-1252"?>
<node id="1286" title="perlman:Math::Trig" created="1999-12-22 20:30:56" updated="2005-08-13 01:21:40">
<type id="119">
perlfunc</type>
<author id="113">
root</author>
<data>
<field name="doctext">
</field>
<field name="name">

&lt;P&gt;
Math::Trig - trigonometric functions

&lt;P&gt;
&lt;HR&gt;
</field>
<field name="synopsis">

&lt;P&gt;
&lt;PRE&gt;        use Math::Trig;
        
        $x = tan(0.9);
        $y = acos(3.7);
        $z = asin(2.4);
        
        $halfpi = pi/2;
&lt;/PRE&gt;
&lt;P&gt;
&lt;PRE&gt;        $rad = deg2rad(120);
&lt;/PRE&gt;
&lt;P&gt;
&lt;HR&gt;
</field>
<field name="description">

&lt;P&gt;
&lt;CODE&gt;Math::Trig&lt;/CODE&gt; defines many trigonometric functions not defined by the core Perl which
defines only the [perlfunc:sin|sin] and [perlfunc:cos|cos]. The constant
&lt;STRONG&gt;pi&lt;/STRONG&gt; is also defined as are a few convenience functions for angle conversions.

&lt;P&gt;
&lt;HR&gt;
&lt;H1&gt;&lt;A NAME="TRIGONOMETRIC_FUNCTIONS"&gt;TRIGONOMETRIC FUNCTIONS&lt;/A&gt;&lt;/H1&gt;
&lt;P&gt;
The tangent

&lt;DL&gt;
&lt;DT&gt;&lt;STRONG&gt;&lt;A NAME="item_tan"&gt;tan&lt;/A&gt;&lt;/STRONG&gt;&lt;DD&gt;
&lt;/DL&gt;
&lt;P&gt;
The cofunctions of the sine, cosine, and tangent (cosec/csc and cotan/cot
are aliases)

&lt;P&gt;
&lt;STRONG&gt;csc&lt;/STRONG&gt;, &lt;STRONG&gt;cosec&lt;/STRONG&gt;, &lt;STRONG&gt;sec&lt;/STRONG&gt;, &lt;STRONG&gt;sec&lt;/STRONG&gt;, &lt;STRONG&gt;cot&lt;/STRONG&gt;, &lt;STRONG&gt;cotan&lt;/STRONG&gt;

&lt;P&gt;
The arcus (also known as the inverse) functions of the sine, cosine, and
tangent

&lt;P&gt;
&lt;STRONG&gt;asin&lt;/STRONG&gt;, &lt;STRONG&gt;acos&lt;/STRONG&gt;, &lt;STRONG&gt;atan&lt;/STRONG&gt;

&lt;P&gt;
The principal value of the arc tangent of y/x

&lt;P&gt;
&lt;STRONG&gt;atan2&lt;/STRONG&gt;(y, x)

&lt;P&gt;
The arcus cofunctions of the sine, cosine, and tangent (acosec/acsc and
acotan/acot are aliases)

&lt;P&gt;
&lt;STRONG&gt;acsc&lt;/STRONG&gt;, &lt;STRONG&gt;acosec&lt;/STRONG&gt;, &lt;STRONG&gt;asec&lt;/STRONG&gt;, &lt;STRONG&gt;acot&lt;/STRONG&gt;, &lt;STRONG&gt;acotan&lt;/STRONG&gt;

&lt;P&gt;
The hyperbolic sine, cosine, and tangent

&lt;P&gt;
&lt;STRONG&gt;sinh&lt;/STRONG&gt;, &lt;STRONG&gt;cosh&lt;/STRONG&gt;, &lt;STRONG&gt;tanh&lt;/STRONG&gt;

&lt;P&gt;
The cofunctions of the hyperbolic sine, cosine, and tangent (cosech/csch
and cotanh/coth are aliases)

&lt;P&gt;
&lt;STRONG&gt;csch&lt;/STRONG&gt;, &lt;STRONG&gt;cosech&lt;/STRONG&gt;, &lt;STRONG&gt;sech&lt;/STRONG&gt;, &lt;STRONG&gt;coth&lt;/STRONG&gt;, &lt;STRONG&gt;cotanh&lt;/STRONG&gt;

&lt;P&gt;
The arcus (also known as the inverse) functions of the hyperbolic sine,
cosine, and tangent

&lt;P&gt;
&lt;STRONG&gt;asinh&lt;/STRONG&gt;, &lt;STRONG&gt;acosh&lt;/STRONG&gt;, &lt;STRONG&gt;atanh&lt;/STRONG&gt;

&lt;P&gt;
The arcus cofunctions of the hyperbolic sine, cosine, and tangent
(acsch/acosech and acoth/acotanh are aliases)

&lt;P&gt;
&lt;STRONG&gt;acsch&lt;/STRONG&gt;, &lt;STRONG&gt;acosech&lt;/STRONG&gt;, &lt;STRONG&gt;asech&lt;/STRONG&gt;, &lt;STRONG&gt;acoth&lt;/STRONG&gt;, &lt;STRONG&gt;acotanh&lt;/STRONG&gt;

&lt;P&gt;
The trigonometric constant &lt;STRONG&gt;pi&lt;/STRONG&gt; is also defined.

&lt;P&gt;
&lt;CODE&gt;$pi2&lt;/CODE&gt; = 2 * &lt;STRONG&gt;pi&lt;/STRONG&gt;;

&lt;P&gt;
&lt;HR&gt;
&lt;H2&gt;&lt;A NAME="ERRORS_DUE_TO_DIVISION_BY_ZERO"&gt;ERRORS DUE TO DIVISION BY ZERO&lt;/A&gt;&lt;/H2&gt;
&lt;P&gt;
The following functions

&lt;P&gt;
&lt;PRE&gt;        acoth
        acsc
        acsch
        asec
        asech
        atanh
        cot
        coth
        csc
        csch
        sec
        sech
        tan
        tanh
&lt;/PRE&gt;
&lt;P&gt;
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

&lt;P&gt;
&lt;PRE&gt;        cot(0): Division by zero.
        (Because in the definition of cot(0), the divisor sin(0) is 0)
        Died at ...
&lt;/PRE&gt;
&lt;P&gt;
or

&lt;P&gt;
&lt;PRE&gt;        atanh(-1): Logarithm of zero.
        Died at...
&lt;/PRE&gt;
&lt;P&gt;
For the &lt;CODE&gt;csc&lt;/CODE&gt;, &lt;CODE&gt;cot&lt;/CODE&gt;, &lt;CODE&gt;asec&lt;/CODE&gt;, &lt;CODE&gt;acsc&lt;/CODE&gt;, &lt;CODE&gt;acot&lt;/CODE&gt;, &lt;CODE&gt;csch&lt;/CODE&gt;, &lt;CODE&gt;coth&lt;/CODE&gt;,
&lt;CODE&gt;asech&lt;/CODE&gt;, &lt;CODE&gt;acsch&lt;/CODE&gt;, the argument cannot be &lt;CODE&gt;0&lt;/CODE&gt; (zero). For the
&lt;CODE&gt;atanh&lt;/CODE&gt;, &lt;CODE&gt;acoth&lt;/CODE&gt;, the argument cannot be &lt;CODE&gt;1&lt;/CODE&gt; (one). For the
&lt;CODE&gt;atanh&lt;/CODE&gt;, &lt;CODE&gt;acoth&lt;/CODE&gt;, the argument cannot be &lt;CODE&gt;-1&lt;/CODE&gt; (minus one). For the
&lt;CODE&gt;tan&lt;/CODE&gt;, &lt;CODE&gt;sec&lt;/CODE&gt;, &lt;CODE&gt;tanh&lt;/CODE&gt;, &lt;CODE&gt;sech&lt;/CODE&gt;, the argument cannot be &lt;EM&gt;pi/2 + k *
pi&lt;/EM&gt;, where &lt;EM&gt;k&lt;/EM&gt; is any integer.

&lt;P&gt;
&lt;HR&gt;
&lt;H2&gt;&lt;A NAME="SIMPLE_REAL_ARGUMENTS_COMPLEX"&gt;SIMPLE (REAL) ARGUMENTS, COMPLEX RESULTS&lt;/A&gt;&lt;/H2&gt;
&lt;P&gt;
Please note that some of the trigonometric functions can break out from the &lt;STRONG&gt;real axis&lt;/STRONG&gt; into the &lt;STRONG&gt;complex plane&lt;/STRONG&gt;. For example
&lt;CODE&gt;asin(2)&lt;/CODE&gt; has no definition for plain real numbers but it has definition for complex
numbers.

&lt;P&gt;
In Perl terms this means that supplying the usual Perl numbers (also known
as scalars, please see [perlman:perldata|perldata]) as input for the trigonometric functions might produce as output results
that no more are simple real numbers: instead they are complex numbers.

&lt;P&gt;
The &lt;CODE&gt;Math::Trig&lt;/CODE&gt; handles this by using the &lt;CODE&gt;Math::Complex&lt;/CODE&gt; package which knows how to handle complex numbers, please see [perlman:Math::Complex|Complex]
for more information. In practice you need not to worry about getting
complex numbers as results because the &lt;CODE&gt;Math::Complex&lt;/CODE&gt; takes care of details like for example how to display complex numbers. For
example:

&lt;P&gt;
&lt;PRE&gt;        print asin(2), &amp;quot;\n&amp;quot;;
    
should produce something like this (take or leave few last decimals):
&lt;/PRE&gt;
&lt;P&gt;
&lt;PRE&gt;        1.5707963267949-1.31695789692482i
&lt;/PRE&gt;
&lt;P&gt;
That is, a complex number with the real part of approximately &lt;CODE&gt;1.571&lt;/CODE&gt;
and the imaginary part of approximately &lt;CODE&gt;-1.317&lt;/CODE&gt;.

&lt;P&gt;
&lt;HR&gt;
&lt;H1&gt;&lt;A NAME="PLANE_ANGLE_CONVERSIONS"&gt;PLANE ANGLE CONVERSIONS&lt;/A&gt;&lt;/H1&gt;
&lt;P&gt;
(Plane, 2-dimensional) angles may be converted with the following
functions.

&lt;P&gt;
&lt;PRE&gt;        $radians  = deg2rad($degrees);
        $radians  = grad2rad($gradians);
        
        $degrees  = rad2deg($radians);
        $degrees  = grad2deg($gradians);
        
        $gradians = deg2grad($degrees);
        $gradians = rad2grad($radians);
&lt;/PRE&gt;
&lt;P&gt;
The full circle is 2 &lt;EM&gt;pi&lt;/EM&gt; radians or &lt;EM&gt;360&lt;/EM&gt; degrees or &lt;EM&gt;400&lt;/EM&gt; gradians.

&lt;P&gt;
&lt;HR&gt;
&lt;H1&gt;&lt;A NAME="RADIAL_COORDINATE_CONVERSIONS"&gt;RADIAL COORDINATE CONVERSIONS&lt;/A&gt;&lt;/H1&gt;
&lt;P&gt;
&lt;STRONG&gt;Radial coordinate systems&lt;/STRONG&gt; are the &lt;STRONG&gt;spherical&lt;/STRONG&gt; and the &lt;STRONG&gt;cylindrical&lt;/STRONG&gt;
systems, explained shortly in more detail.

&lt;P&gt;
You can import radial coordinate conversion functions by using the
&lt;CODE&gt;:radial&lt;/CODE&gt; tag:

&lt;P&gt;
&lt;PRE&gt;    use Math::Trig ':radial';
&lt;/PRE&gt;
&lt;P&gt;
&lt;PRE&gt;    ($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);
&lt;/PRE&gt;
&lt;P&gt;
&lt;STRONG&gt;All angles are in radians&lt;/STRONG&gt;.

&lt;P&gt;
&lt;HR&gt;
&lt;H2&gt;&lt;A NAME="COORDINATE_SYSTEMS"&gt;COORDINATE SYSTEMS&lt;/A&gt;&lt;/H2&gt;
&lt;P&gt;
&lt;STRONG&gt;Cartesian&lt;/STRONG&gt; coordinates are the usual rectangular &lt;EM&gt;(x, y,
z)&lt;/EM&gt;-coordinates.

&lt;P&gt;
Spherical coordinates, &lt;EM&gt;(rho, theta, pi)&lt;/EM&gt;, 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 &lt;STRONG&gt;rho&lt;/STRONG&gt;, also known as the &lt;EM&gt;radial&lt;/EM&gt; coordinate. The angle in the &lt;EM&gt;xy&lt;/EM&gt;-plane (around the &lt;EM&gt;z&lt;/EM&gt;-axis) is &lt;STRONG&gt;theta&lt;/STRONG&gt;, also known as the &lt;EM&gt;azimuthal&lt;/EM&gt;
coordinate. The angle from the &lt;EM&gt;z&lt;/EM&gt;-axis is &lt;STRONG&gt;phi&lt;/STRONG&gt;, also known as the
&lt;EM&gt;polar&lt;/EM&gt; coordinate. The `North Pole' is therefore &lt;EM&gt;0, 0, rho&lt;/EM&gt;, and the `Bay of Guinea' (think of the missing big chunk of Africa) &lt;EM&gt;0,
pi/2, rho&lt;/EM&gt;.

&lt;P&gt;
&lt;STRONG&gt;Beware&lt;/STRONG&gt;: some texts define &lt;EM&gt;theta&lt;/EM&gt; and &lt;EM&gt;phi&lt;/EM&gt; the other way round, some texts define the &lt;EM&gt;phi&lt;/EM&gt; to start from the horizontal plane, some texts use &lt;EM&gt;r&lt;/EM&gt; in place of &lt;EM&gt;rho&lt;/EM&gt;.

&lt;P&gt;
Cylindrical coordinates, &lt;EM&gt;(rho, theta, z)&lt;/EM&gt;, 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 &lt;STRONG&gt;rho&lt;/STRONG&gt;, also known as the &lt;EM&gt;radial&lt;/EM&gt; coordinate. The angle in the &lt;EM&gt;xy&lt;/EM&gt;-plane (around the &lt;EM&gt;z&lt;/EM&gt;-axis) is &lt;STRONG&gt;theta&lt;/STRONG&gt;, also known as the &lt;EM&gt;azimuthal&lt;/EM&gt;
coordinate. The third coordinate is the &lt;EM&gt;z&lt;/EM&gt;, pointing up from the
&lt;STRONG&gt;theta&lt;/STRONG&gt;-plane.

&lt;P&gt;
&lt;HR&gt;
&lt;H2&gt;&lt;A NAME="3_D_ANGLE_CONVERSIONS"&gt;3-D ANGLE CONVERSIONS&lt;/A&gt;&lt;/H2&gt;
&lt;P&gt;
Conversions to and from spherical and cylindrical coordinates are
available. Please notice that the conversions are not necessarily
reversible because of the equalities like &lt;EM&gt;pi&lt;/EM&gt; angles being equal to
&lt;EM&gt;-pi&lt;/EM&gt; angles.

&lt;DL&gt;
&lt;DT&gt;&lt;STRONG&gt;&lt;A NAME="item_cartesian_to_cylindrical"&gt;cartesian_to_cylindrical&lt;/A&gt;&lt;/STRONG&gt;&lt;P&gt;
&lt;DD&gt;
&lt;PRE&gt;        ($rho, $theta, $z) = cartesian_to_cylindrical($x, $y, $z);
&lt;/PRE&gt;
&lt;DT&gt;&lt;STRONG&gt;&lt;A NAME="item_cartesian_to_spherical"&gt;cartesian_to_spherical&lt;/A&gt;&lt;/STRONG&gt;&lt;P&gt;
&lt;DD&gt;
&lt;PRE&gt;        ($rho, $theta, $phi) = cartesian_to_spherical($x, $y, $z);
&lt;/PRE&gt;
&lt;DT&gt;&lt;STRONG&gt;&lt;A NAME="item_cylindrical_to_cartesian"&gt;cylindrical_to_cartesian&lt;/A&gt;&lt;/STRONG&gt;&lt;P&gt;
&lt;DD&gt;
&lt;PRE&gt;        ($x, $y, $z) = cylindrical_to_cartesian($rho, $theta, $z);
&lt;/PRE&gt;
&lt;DT&gt;&lt;STRONG&gt;&lt;A NAME="item_cylindrical_to_spherical"&gt;cylindrical_to_spherical&lt;/A&gt;&lt;/STRONG&gt;&lt;P&gt;
&lt;DD&gt;
&lt;PRE&gt;        ($rho_s, $theta, $phi) = cylindrical_to_spherical($rho_c, $theta, $z);
&lt;/PRE&gt;
&lt;P&gt;
Notice that when &lt;CODE&gt;$z&lt;/CODE&gt; is not 0 &lt;CODE&gt;$rho_s&lt;/CODE&gt; is not equal to &lt;CODE&gt;$rho_c&lt;/CODE&gt;.

&lt;P&gt;&lt;DT&gt;&lt;STRONG&gt;&lt;A NAME="item_spherical_to_cartesian"&gt;spherical_to_cartesian&lt;/A&gt;&lt;/STRONG&gt;&lt;P&gt;
&lt;DD&gt;
&lt;PRE&gt;        ($x, $y, $z) = spherical_to_cartesian($rho, $theta, $phi);
&lt;/PRE&gt;
&lt;DT&gt;&lt;STRONG&gt;&lt;A NAME="item_spherical_to_cylindrical"&gt;spherical_to_cylindrical&lt;/A&gt;&lt;/STRONG&gt;&lt;P&gt;
&lt;DD&gt;
&lt;PRE&gt;        ($rho_c, $theta, $z) = spherical_to_cylindrical($rho_s, $theta, $phi);
&lt;/PRE&gt;
&lt;P&gt;
Notice that when &lt;CODE&gt;$z&lt;/CODE&gt; is not 0 &lt;CODE&gt;$rho_c&lt;/CODE&gt; is not equal to &lt;CODE&gt;$rho_s&lt;/CODE&gt;.

&lt;/DL&gt;
&lt;P&gt;
&lt;HR&gt;
&lt;H1&gt;&lt;A NAME="GREAT_CIRCLE_DISTANCES"&gt;GREAT CIRCLE DISTANCES&lt;/A&gt;&lt;/H1&gt;
&lt;P&gt;
You can compute spherical distances, called &lt;STRONG&gt;great circle distances&lt;/STRONG&gt;, by importing the &lt;CODE&gt;great_circle_distance&lt;/CODE&gt; function:

&lt;P&gt;
&lt;PRE&gt;        use Math::Trig 'great_circle_distance'
&lt;/PRE&gt;
&lt;P&gt;
&lt;PRE&gt;    $distance = great_circle_distance($theta0, $phi0, $theta1, $phi, &amp;#091;, $rho&amp;#093;);
&lt;/PRE&gt;
&lt;P&gt;
The &lt;EM&gt;great circle distance&lt;/EM&gt; is the shortest distance between two points on a sphere. The distance is in &lt;CODE&gt;$rho&lt;/CODE&gt; units. The &lt;CODE&gt;$rho&lt;/CODE&gt; is optional, it defaults to 1 (the unit sphere), therefore the distance
defaults to radians.

&lt;P&gt;
&lt;HR&gt;
&lt;H1&gt;&lt;A NAME="EXAMPLES"&gt;EXAMPLES&lt;/A&gt;&lt;/H1&gt;
&lt;P&gt;
To calculate the distance between London 
&lt;FONT SIZE=-1&gt;(51.3N&lt;/FONT&gt; 
&lt;FONT SIZE=-1&gt;0.5W)&lt;/FONT&gt; and Tokyo 
&lt;FONT SIZE=-1&gt;(35.7N&lt;/FONT&gt; 
&lt;FONT SIZE=-1&gt;139.8E)&lt;/FONT&gt; in kilometers:

&lt;P&gt;
&lt;PRE&gt;        use Math::Trig qw(great_circle_distance deg2rad);
&lt;/PRE&gt;
&lt;P&gt;
&lt;PRE&gt;        # 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));
&lt;/PRE&gt;
&lt;P&gt;
&lt;PRE&gt;        $km = great_circle_distance(@L, @T, 6378);
&lt;/PRE&gt;
&lt;P&gt;
The answer may be off by up to 0.3% because of the irregular (slightly
aspherical) form of the Earth.

&lt;P&gt;
&lt;HR&gt;
&lt;H1&gt;&lt;A NAME="BUGS"&gt;BUGS&lt;/A&gt;&lt;/H1&gt;
&lt;P&gt;
Saying &lt;CODE&gt;use Math::Trig;&lt;/CODE&gt; exports many mathematical routines in the caller environment and even
overrides some ([perlfunc:sin|sin], [perlfunc:cos|cos]). This is construed as a feature by the Authors, actually... ;-)

&lt;P&gt;
The code is not optimized for speed, especially because we use
&lt;CODE&gt;Math::Complex&lt;/CODE&gt; 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 &lt;CODE&gt;asin(2)&lt;/CODE&gt; to give an answer instead of giving a fatal runtime error.

&lt;P&gt;
&lt;HR&gt;
&lt;H1&gt;&lt;A NAME="AUTHORS"&gt;AUTHORS&lt;/A&gt;&lt;/H1&gt;
&lt;P&gt;
Jarkko Hietaniemi &amp;lt;&lt;EM&gt;jhi@iki.fi&lt;/EM&gt;&amp;gt; and Raphael Manfredi &amp;lt;&lt;EM&gt;Raphael_Manfredi@grenoble.hp.com&lt;/EM&gt;&amp;gt;.

&lt;HR&gt;
</field>
</data>
</node>
