Edit D:\app\Administrator\product\11.2.0\dbhome_1\perl\html\lib\Math\Trig.html
<?xml version="1.0" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Math::Trig - trigonometric functions</title> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <link rev="made" href="mailto:" /> </head> <body style="background-color: white"> <table border="0" width="100%" cellspacing="0" cellpadding="3"> <tr><td class="block" style="background-color: #cccccc" valign="middle"> <big><strong><span class="block"> Math::Trig - trigonometric functions</span></strong></big> </td></tr> </table> <!-- INDEX BEGIN --> <div name="index"> <p><a name="__index__"></a></p> <ul> <li><a href="#name">NAME</a></li> <li><a href="#synopsis">SYNOPSIS</a></li> <li><a href="#description">DESCRIPTION</a></li> <li><a href="#trigonometric_functions">TRIGONOMETRIC FUNCTIONS</a></li> <ul> <li><a href="#errors_due_to_division_by_zero">ERRORS DUE TO DIVISION BY ZERO</a></li> <li><a href="#simple__real__arguments__complex_results">SIMPLE (REAL) ARGUMENTS, COMPLEX RESULTS</a></li> </ul> <li><a href="#plane_angle_conversions">PLANE ANGLE CONVERSIONS</a></li> <li><a href="#radial_coordinate_conversions">RADIAL COORDINATE CONVERSIONS</a></li> <ul> <li><a href="#coordinate_systems">COORDINATE SYSTEMS</a></li> <li><a href="#3_d_angle_conversions">3-D ANGLE CONVERSIONS</a></li> </ul> <li><a href="#great_circle_distances_and_directions">GREAT CIRCLE DISTANCES AND DIRECTIONS</a></li> <ul> <li><a href="#great_circle_distance">great_circle_distance</a></li> <li><a href="#great_circle_direction">great_circle_direction</a></li> <li><a href="#great_circle_bearing">great_circle_bearing</a></li> <li><a href="#great_circle_destination">great_circle_destination</a></li> <li><a href="#great_circle_midpoint">great_circle_midpoint</a></li> <li><a href="#great_circle_waypoint">great_circle_waypoint</a></li> </ul> <li><a href="#examples">EXAMPLES</a></li> <ul> <li><a href="#caveat_for_great_circle_formulas">CAVEAT FOR GREAT CIRCLE FORMULAS</a></li> </ul> <li><a href="#bugs">BUGS</a></li> <li><a href="#authors">AUTHORS</a></li> </ul> <hr name="index" /> </div> <!-- INDEX END --> <p> </p> <h1><a name="name">NAME</a></h1> <p>Math::Trig - trigonometric functions</p> <p> </p> <hr /> <h1><a name="synopsis">SYNOPSIS</a></h1> <pre> use Math::Trig;</pre> <pre> $x = tan(0.9); $y = acos(3.7); $z = asin(2.4);</pre> <pre> $halfpi = pi/2;</pre> <pre> $rad = deg2rad(120);</pre> <pre> # Import constants pi2, pip2, pip4 (2*pi, pi/2, pi/4). use Math::Trig ':pi';</pre> <pre> # Import the conversions between cartesian/spherical/cylindrical. use Math::Trig ':radial';</pre> <pre> # Import the great circle formulas. use Math::Trig ':great_circle';</pre> <p> </p> <hr /> <h1><a name="description">DESCRIPTION</a></h1> <p><code>Math::Trig</code> defines many trigonometric functions not defined by the core Perl which defines only the <a href="file://C|\ADE\aime_smenon_perl_090715\perl\html/pod/perlfunc.html#sin"><code>sin()</code></a> and <a href="file://C|\ADE\aime_smenon_perl_090715\perl\html/pod/perlfunc.html#cos"><code>cos()</code></a>. The constant <strong>pi</strong> is also defined as are a few convenience functions for angle conversions, and <em>great circle formulas</em> for spherical movement.</p> <p> </p> <hr /> <h1><a name="trigonometric_functions">TRIGONOMETRIC FUNCTIONS</a></h1> <p>The tangent</p> <dl> <dt><strong><a name="tan" class="item"><strong>tan</strong></a></strong> </dl> <p>The cofunctions of the sine, cosine, and tangent (cosec/csc and cotan/cot are aliases)</p> <p><strong>csc</strong>, <strong>cosec</strong>, <strong>sec</strong>, <strong>sec</strong>, <strong>cot</strong>, <strong>cotan</strong></p> <p>The arcus (also known as the inverse) functions of the sine, cosine, and tangent</p> <p><strong>asin</strong>, <strong>acos</strong>, <strong>atan</strong></p> <p>The principal value of the arc tangent of y/x</p> <p><strong>atan2</strong>(y, x)</p> <p>The arcus cofunctions of the sine, cosine, and tangent (acosec/acsc and acotan/acot are aliases). Note that atan2(0, 0) is not well-defined.</p> <p><strong>acsc</strong>, <strong>acosec</strong>, <strong>asec</strong>, <strong>acot</strong>, <strong>acotan</strong></p> <p>The hyperbolic sine, cosine, and tangent</p> <p><strong>sinh</strong>, <strong>cosh</strong>, <strong>tanh</strong></p> <p>The cofunctions of the hyperbolic sine, cosine, and tangent (cosech/csch and cotanh/coth are aliases)</p> <p><strong>csch</strong>, <strong>cosech</strong>, <strong>sech</strong>, <strong>coth</strong>, <strong>cotanh</strong></p> <p>The arcus (also known as the inverse) functions of the hyperbolic sine, cosine, and tangent</p> <p><strong>asinh</strong>, <strong>acosh</strong>, <strong>atanh</strong></p> <p>The arcus cofunctions of the hyperbolic sine, cosine, and tangent (acsch/acosech and acoth/acotanh are aliases)</p> <p><strong>acsch</strong>, <strong>acosech</strong>, <strong>asech</strong>, <strong>acoth</strong>, <strong>acotanh</strong></p> <p>The trigonometric constant <strong>pi</strong> and some of handy multiples of it are also defined.</p> <p><strong>pi, pi2, pi4, pip2, pip4</strong></p> <p> </p> <h2><a name="errors_due_to_division_by_zero">ERRORS DUE TO DIVISION BY ZERO</a></h2> <p>The following functions</p> <pre> acoth acsc acsch asec asech atanh cot coth csc csch sec sech tan tanh</pre> <p>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</p> <pre> cot(0): Division by zero. (Because in the definition of cot(0), the divisor sin(0) is 0) Died at ...</pre> <p>or</p> <pre> atanh(-1): Logarithm of zero. Died at...</pre> <p>For the <code>csc</code>, <code>cot</code>, <code>asec</code>, <code>acsc</code>, <code>acot</code>, <code>csch</code>, <code>coth</code>, <code>asech</code>, <code>acsch</code>, the argument cannot be <code>0</code> (zero). For the <code>atanh</code>, <code>acoth</code>, the argument cannot be <code>1</code> (one). For the <code>atanh</code>, <code>acoth</code>, the argument cannot be <code>-1</code> (minus one). For the <a href="#tan"><code>tan</code></a>, <code>sec</code>, <code>tanh</code>, <code>sech</code>, the argument cannot be <em>pi/2 + k * pi</em>, where <em>k</em> is any integer.</p> <p>Note that atan2(0, 0) is not well-defined.</p> <p> </p> <h2><a name="simple__real__arguments__complex_results">SIMPLE (REAL) ARGUMENTS, COMPLEX RESULTS</a></h2> <p>Please note that some of the trigonometric functions can break out from the <strong>real axis</strong> into the <strong>complex plane</strong>. For example <code>asin(2)</code> has no definition for plain real numbers but it has definition for complex numbers.</p> <p>In Perl terms this means that supplying the usual Perl numbers (also known as scalars, please see <a href="file://C|\ADE\aime_smenon_perl_090715\perl\html/pod/perldata.html">the perldata manpage</a>) as input for the trigonometric functions might produce as output results that no more are simple real numbers: instead they are complex numbers.</p> <p>The <code>Math::Trig</code> handles this by using the <code>Math::Complex</code> package which knows how to handle complex numbers, please see <a href="file://C|\ADE\aime_smenon_perl_090715\perl\html/lib/Math/Complex.html">the Math::Complex manpage</a> for more information. In practice you need not to worry about getting complex numbers as results because the <code>Math::Complex</code> takes care of details like for example how to display complex numbers. For example:</p> <pre> print asin(2), "\n";</pre> <p>should produce something like this (take or leave few last decimals):</p> <pre> 1.5707963267949-1.31695789692482i</pre> <p>That is, a complex number with the real part of approximately <code>1.571</code> and the imaginary part of approximately <code>-1.317</code>.</p> <p> </p> <hr /> <h1><a name="plane_angle_conversions">PLANE ANGLE CONVERSIONS</a></h1> <p>(Plane, 2-dimensional) angles may be converted with the following functions.</p> <dl> <dt><strong><a name="deg2rad" class="item">deg2rad</a></strong> <dd> <pre> $radians = deg2rad($degrees);</pre> </dd> <dt><strong><a name="grad2rad" class="item">grad2rad</a></strong> <dd> <pre> $radians = grad2rad($gradians);</pre> </dd> <dt><strong><a name="rad2deg" class="item">rad2deg</a></strong> <dd> <pre> $degrees = rad2deg($radians);</pre> </dd> <dt><strong><a name="grad2deg" class="item">grad2deg</a></strong> <dd> <pre> $degrees = grad2deg($gradians);</pre> </dd> <dt><strong><a name="deg2grad" class="item">deg2grad</a></strong> <dd> <pre> $gradians = deg2grad($degrees);</pre> </dd> <dt><strong><a name="rad2grad" class="item">rad2grad</a></strong> <dd> <pre> $gradians = rad2grad($radians);</pre> </dd> </dl> <p>The full circle is 2 <em>pi</em> radians or <em>360</em> degrees or <em>400</em> gradians. The result is by default wrapped to be inside the [0, {2pi,360,400}[ circle. If you don't want this, supply a true second argument:</p> <pre> $zillions_of_radians = deg2rad($zillions_of_degrees, 1); $negative_degrees = rad2deg($negative_radians, 1);</pre> <p>You can also do the wrapping explicitly by rad2rad(), deg2deg(), and grad2grad().</p> <dl> <dt><strong><a name="rad2rad" class="item">rad2rad</a></strong> <dd> <pre> $radians_wrapped_by_2pi = rad2rad($radians);</pre> </dd> <dt><strong><a name="deg2deg" class="item">deg2deg</a></strong> <dd> <pre> $degrees_wrapped_by_360 = deg2deg($degrees);</pre> </dd> <dt><strong><a name="grad2grad" class="item">grad2grad</a></strong> <dd> <pre> $gradians_wrapped_by_400 = grad2grad($gradians);</pre> </dd> </dl> <p> </p> <hr /> <h1><a name="radial_coordinate_conversions">RADIAL COORDINATE CONVERSIONS</a></h1> <p><strong>Radial coordinate systems</strong> are the <strong>spherical</strong> and the <strong>cylindrical</strong> systems, explained shortly in more detail.</p> <p>You can import radial coordinate conversion functions by using the <code>:radial</code> tag:</p> <pre> use Math::Trig ':radial';</pre> <pre> ($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);</pre> <p><strong>All angles are in radians</strong>.</p> <p> </p> <h2><a name="coordinate_systems">COORDINATE SYSTEMS</a></h2> <p><strong>Cartesian</strong> coordinates are the usual rectangular <em>(x, y, z)</em>-coordinates.</p> <p>Spherical coordinates, <em>(rho, theta, pi)</em>, 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 <strong>rho</strong>, also known as the <em>radial</em> coordinate. The angle in the <em>xy</em>-plane (around the <em>z</em>-axis) is <strong>theta</strong>, also known as the <em>azimuthal</em> coordinate. The angle from the <em>z</em>-axis is <strong>phi</strong>, also known as the <em>polar</em> coordinate. The North Pole is therefore <em>0, 0, rho</em>, and the Gulf of Guinea (think of the missing big chunk of Africa) <em>0, pi/2, rho</em>. In geographical terms <em>phi</em> is latitude (northward positive, southward negative) and <em>theta</em> is longitude (eastward positive, westward negative).</p> <p><strong>BEWARE</strong>: some texts define <em>theta</em> and <em>phi</em> the other way round, some texts define the <em>phi</em> to start from the horizontal plane, some texts use <em>r</em> in place of <em>rho</em>.</p> <p>Cylindrical coordinates, <em>(rho, theta, z)</em>, 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 <strong>rho</strong>, also known as the <em>radial</em> coordinate. The angle in the <em>xy</em>-plane (around the <em>z</em>-axis) is <strong>theta</strong>, also known as the <em>azimuthal</em> coordinate. The third coordinate is the <em>z</em>, pointing up from the <strong>theta</strong>-plane.</p> <p> </p> <h2><a name="3_d_angle_conversions">3-D ANGLE CONVERSIONS</a></h2> <p>Conversions to and from spherical and cylindrical coordinates are available. Please notice that the conversions are not necessarily reversible because of the equalities like <em>pi</em> angles being equal to <em>-pi</em> angles.</p> <dl> <dt><strong><a name="cartesian_to_cylindrical" class="item">cartesian_to_cylindrical</a></strong> <dd> <pre> ($rho, $theta, $z) = cartesian_to_cylindrical($x, $y, $z);</pre> </dd> <dt><strong><a name="cartesian_to_spherical" class="item">cartesian_to_spherical</a></strong> <dd> <pre> ($rho, $theta, $phi) = cartesian_to_spherical($x, $y, $z);</pre> </dd> <dt><strong><a name="cylindrical_to_cartesian" class="item">cylindrical_to_cartesian</a></strong> <dd> <pre> ($x, $y, $z) = cylindrical_to_cartesian($rho, $theta, $z);</pre> </dd> <dt><strong><a name="cylindrical_to_spherical" class="item">cylindrical_to_spherical</a></strong> <dd> <pre> ($rho_s, $theta, $phi) = cylindrical_to_spherical($rho_c, $theta, $z);</pre> </dd> <dd> <p>Notice that when <code>$z</code> is not 0 <code>$rho_s</code> is not equal to <code>$rho_c</code>.</p> </dd> <dt><strong><a name="spherical_to_cartesian" class="item">spherical_to_cartesian</a></strong> <dd> <pre> ($x, $y, $z) = spherical_to_cartesian($rho, $theta, $phi);</pre> </dd> <dt><strong><a name="spherical_to_cylindrical" class="item">spherical_to_cylindrical</a></strong> <dd> <pre> ($rho_c, $theta, $z) = spherical_to_cylindrical($rho_s, $theta, $phi);</pre> </dd> <dd> <p>Notice that when <code>$z</code> is not 0 <code>$rho_c</code> is not equal to <code>$rho_s</code>.</p> </dd> </dl> <p> </p> <hr /> <h1><a name="great_circle_distances_and_directions">GREAT CIRCLE DISTANCES AND DIRECTIONS</a></h1> <p>A great circle is section of a circle that contains the circle diameter: the shortest distance between two (non-antipodal) points on the spherical surface goes along the great circle connecting those two points.</p> <p> </p> <h2><a name="great_circle_distance">great_circle_distance</a></h2> <p>You can compute spherical distances, called <strong>great circle distances</strong>, by importing the <code>great_circle_distance()</code> function:</p> <pre> use Math::Trig 'great_circle_distance';</pre> <pre> $distance = great_circle_distance($theta0, $phi0, $theta1, $phi1, [, $rho]);</pre> <p>The <em>great circle distance</em> is the shortest distance between two points on a sphere. The distance is in <code>$rho</code> units. The <code>$rho</code> is optional, it defaults to 1 (the unit sphere), therefore the distance defaults to radians.</p> <p>If you think geographically the <em>theta</em> are longitudes: zero at the Greenwhich meridian, eastward positive, westward negative--and the <em>phi</em> are latitudes: zero at the North Pole, northward positive, southward negative. <strong>NOTE</strong>: this formula thinks in mathematics, not geographically: the <em>phi</em> zero is at the North Pole, not at the Equator on the west coast of Africa (Bay of Guinea). You need to subtract your geographical coordinates from <em>pi/2</em> (also known as 90 degrees).</p> <pre> $distance = great_circle_distance($lon0, pi/2 - $lat0, $lon1, pi/2 - $lat1, $rho);</pre> <p> </p> <h2><a name="great_circle_direction">great_circle_direction</a></h2> <p>The direction you must follow the great circle (also known as <em>bearing</em>) can be computed by the <code>great_circle_direction()</code> function:</p> <pre> use Math::Trig 'great_circle_direction';</pre> <pre> $direction = great_circle_direction($theta0, $phi0, $theta1, $phi1);</pre> <p> </p> <h2><a name="great_circle_bearing">great_circle_bearing</a></h2> <p>Alias 'great_circle_bearing' for 'great_circle_direction' is also available.</p> <pre> use Math::Trig 'great_circle_bearing';</pre> <pre> $direction = great_circle_bearing($theta0, $phi0, $theta1, $phi1);</pre> <p>The result of great_circle_direction is in radians, zero indicating straight north, pi or -pi straight south, pi/2 straight west, and -pi/2 straight east.</p> <p>You can inversely compute the destination if you know the starting point, direction, and distance:</p> <p> </p> <h2><a name="great_circle_destination">great_circle_destination</a></h2> <pre> use Math::Trig 'great_circle_destination';</pre> <pre> # thetad and phid are the destination coordinates, # dird is the final direction at the destination.</pre> <pre> ($thetad, $phid, $dird) = great_circle_destination($theta, $phi, $direction, $distance);</pre> <p>or the midpoint if you know the end points:</p> <p> </p> <h2><a name="great_circle_midpoint">great_circle_midpoint</a></h2> <pre> use Math::Trig 'great_circle_midpoint';</pre> <pre> ($thetam, $phim) = great_circle_midpoint($theta0, $phi0, $theta1, $phi1);</pre> <p>The <code>great_circle_midpoint()</code> is just a special case of</p> <p> </p> <h2><a name="great_circle_waypoint">great_circle_waypoint</a></h2> <pre> use Math::Trig 'great_circle_waypoint';</pre> <pre> ($thetai, $phii) = great_circle_waypoint($theta0, $phi0, $theta1, $phi1, $way);</pre> <p>Where the $way is a value from zero ($theta0, $phi0) to one ($theta1, $phi1). Note that antipodal points (where their distance is <em>pi</em> radians) do not have waypoints between them (they would have an an "equator" between them), and therefore <a href="file://C|\ADE\aime_smenon_perl_090715\perl\html/pod/perlfunc.html#undef"><code>undef</code></a> 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.</p> <p>The thetas, phis, direction, and distance in the above are all in radians.</p> <p>You can import all the great circle formulas by</p> <pre> use Math::Trig ':great_circle';</pre> <p>Notice that the resulting directions might be somewhat surprising if you are looking at a flat worldmap: in such map projections the great circles 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.</p> <p> </p> <hr /> <h1><a name="examples">EXAMPLES</a></h1> <p>To calculate the distance between London (51.3N 0.5W) and Tokyo (35.7N 139.8E) in kilometers:</p> <pre> use Math::Trig qw(great_circle_distance deg2rad);</pre> <pre> # 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 = great_circle_distance(@L, @T, 6378); # About 9600 km.</pre> <p>The direction you would have to go from London to Tokyo (in radians, straight north being zero, straight east being pi/2).</p> <pre> use Math::Trig qw(great_circle_direction);</pre> <pre> my $rad = great_circle_direction(@L, @T); # About 0.547 or 0.174 pi.</pre> <p>The midpoint between London and Tokyo being</p> <pre> use Math::Trig qw(great_circle_midpoint);</pre> <pre> my @M = great_circle_midpoint(@L, @T);</pre> <p>or about 89.16N 68.93E, practically at the North Pole.</p> <p> </p> <h2><a name="caveat_for_great_circle_formulas">CAVEAT FOR GREAT CIRCLE FORMULAS</a></h2> <p>The answers may be off by few percentages because of the irregular (slightly aspherical) form of the Earth. The errors are at worst about 0.55%, but generally below 0.3%.</p> <p> </p> <hr /> <h1><a name="bugs">BUGS</a></h1> <p>Saying <code>use Math::Trig;</code> exports many mathematical routines in the caller environment and even overrides some (<a href="file://C|\ADE\aime_smenon_perl_090715\perl\html/pod/perlfunc.html#sin"><code>sin</code></a>, <a href="file://C|\ADE\aime_smenon_perl_090715\perl\html/pod/perlfunc.html#cos"><code>cos</code></a>). This is construed as a feature by the Authors, actually... ;-)</p> <p>The code is not optimized for speed, especially because we use <code>Math::Complex</code> 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 <code>asin(2)</code> to give an answer instead of giving a fatal runtime error.</p> <p>Do not attempt navigation using these formulas.</p> <p> </p> <hr /> <h1><a name="authors">AUTHORS</a></h1> <p>Jarkko Hietaniemi <<em class="file">jhi!at!iki.fi</em>> and Raphael Manfredi <<em class="file">Raphael_Manfredi!at!pobox.com</em>>.</p> <table border="0" width="100%" cellspacing="0" cellpadding="3"> <tr><td class="block" style="background-color: #cccccc" valign="middle"> <big><strong><span class="block"> Math::Trig - trigonometric functions</span></strong></big> </td></tr> </table> </body> </html>
Ms-Dos/Windows
Unix
Write backup
jsp File Browser version 1.2 by
www.vonloesch.de