.\" Automatically generated by Pod::Man v1.34, Pod::Parser v1.13 .\" .\" Standard preamble: .\" ======================================================================== .de Sh \" Subsection heading .br .if t .Sp .ne 5 .PP \fB\\$1\fR .PP .. .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. | will give a .\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to .\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C' .\" expand to `' in nroff, nothing in troff, for use with C<>. .tr \(*W-|\(bv\*(Tr .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' 'br\} .\" .\" If the F register is turned on, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . nr % 0 . rr F .\} .\" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .hy 0 .if n .na .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "Math::Trig 3" .TH Math::Trig 3 "2002-06-01" "perl v5.8.0" "Perl Programmers Reference Guide" .SH "NAME" Math::Trig \- trigonometric functions .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use Math::Trig; .Ve .PP .Vb 3 \& $x = tan(0.9); \& $y = acos(3.7); \& $z = asin(2.4); .Ve .PP .Vb 1 \& $halfpi = pi/2; .Ve .PP .Vb 1 \& $rad = deg2rad(120); .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" \&\f(CW\*(C`Math::Trig\*(C'\fR defines many trigonometric functions not defined by the core Perl which defines only the \f(CW\*(C`sin()\*(C'\fR and \f(CW\*(C`cos()\*(C'\fR. The constant \&\fBpi\fR is also defined as are a few convenience functions for angle conversions. .SH "TRIGONOMETRIC FUNCTIONS" .IX Header "TRIGONOMETRIC FUNCTIONS" The tangent .IP "\fBtan\fR" 4 .IX Item "tan" .PP The cofunctions of the sine, cosine, and tangent (cosec/csc and cotan/cot are aliases) .PP \&\fBcsc\fR, \fBcosec\fR, \fBsec\fR, \fBsec\fR, \fBcot\fR, \fBcotan\fR .PP The arcus (also known as the inverse) functions of the sine, cosine, and tangent .PP \&\fBasin\fR, \fBacos\fR, \fBatan\fR .PP The principal value of the arc tangent of y/x .PP \&\fBatan2\fR(y, x) .PP The arcus cofunctions of the sine, cosine, and tangent (acosec/acsc and acotan/acot are aliases) .PP \&\fBacsc\fR, \fBacosec\fR, \fBasec\fR, \fBacot\fR, \fBacotan\fR .PP The hyperbolic sine, cosine, and tangent .PP \&\fBsinh\fR, \fBcosh\fR, \fBtanh\fR .PP The cofunctions of the hyperbolic sine, cosine, and tangent (cosech/csch and cotanh/coth are aliases) .PP \&\fBcsch\fR, \fBcosech\fR, \fBsech\fR, \fBcoth\fR, \fBcotanh\fR .PP The arcus (also known as the inverse) functions of the hyperbolic sine, cosine, and tangent .PP \&\fBasinh\fR, \fBacosh\fR, \fBatanh\fR .PP The arcus cofunctions of the hyperbolic sine, cosine, and tangent (acsch/acosech and acoth/acotanh are aliases) .PP \&\fBacsch\fR, \fBacosech\fR, \fBasech\fR, \fBacoth\fR, \fBacotanh\fR .PP The trigonometric constant \fBpi\fR is also defined. .PP $pi2 = 2 * \fBpi\fR; .Sh "\s-1ERRORS\s0 \s-1DUE\s0 \s-1TO\s0 \s-1DIVISION\s0 \s-1BY\s0 \s-1ZERO\s0" .IX Subsection "ERRORS DUE TO DIVISION BY ZERO" The following functions .PP .Vb 14 \& acoth \& acsc \& acsch \& asec \& asech \& atanh \& cot \& coth \& csc \& csch \& sec \& sech \& tan \& tanh .Ve .PP 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 .PP .Vb 3 \& cot(0): Division by zero. \& (Because in the definition of cot(0), the divisor sin(0) is 0) \& Died at ... .Ve .PP or .PP .Vb 2 \& atanh(-1): Logarithm of zero. \& Died at... .Ve .PP For the \f(CW\*(C`csc\*(C'\fR, \f(CW\*(C`cot\*(C'\fR, \f(CW\*(C`asec\*(C'\fR, \f(CW\*(C`acsc\*(C'\fR, \f(CW\*(C`acot\*(C'\fR, \f(CW\*(C`csch\*(C'\fR, \f(CW\*(C`coth\*(C'\fR, \&\f(CW\*(C`asech\*(C'\fR, \f(CW\*(C`acsch\*(C'\fR, the argument cannot be \f(CW0\fR (zero). For the \&\f(CW\*(C`atanh\*(C'\fR, \f(CW\*(C`acoth\*(C'\fR, the argument cannot be \f(CW1\fR (one). For the \&\f(CW\*(C`atanh\*(C'\fR, \f(CW\*(C`acoth\*(C'\fR, the argument cannot be \f(CW\*(C`\-1\*(C'\fR (minus one). For the \&\f(CW\*(C`tan\*(C'\fR, \f(CW\*(C`sec\*(C'\fR, \f(CW\*(C`tanh\*(C'\fR, \f(CW\*(C`sech\*(C'\fR, the argument cannot be \fIpi/2 + k * pi\fR, where \fIk\fR is any integer. .Sh "\s-1SIMPLE\s0 (\s-1REAL\s0) \s-1ARGUMENTS\s0, \s-1COMPLEX\s0 \s-1RESULTS\s0" .IX Subsection "SIMPLE (REAL) ARGUMENTS, COMPLEX RESULTS" Please note that some of the trigonometric functions can break out from the \fBreal axis\fR into the \fBcomplex plane\fR. For example \&\f(CWasin(2)\fR has no definition for plain real numbers but it has definition for complex numbers. .PP 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. .PP The \f(CW\*(C`Math::Trig\*(C'\fR handles this by using the \f(CW\*(C`Math::Complex\*(C'\fR package which knows how to handle complex numbers, please see Math::Complex for more information. In practice you need not to worry about getting complex numbers as results because the \f(CW\*(C`Math::Complex\*(C'\fR takes care of details like for example how to display complex numbers. For example: .PP .Vb 1 \& print asin(2), "\en"; .Ve .PP should produce something like this (take or leave few last decimals): .PP .Vb 1 \& 1.5707963267949-1.31695789692482i .Ve .PP That is, a complex number with the real part of approximately \f(CW1.571\fR and the imaginary part of approximately \f(CW\*(C`\-1.317\*(C'\fR. .SH "PLANE ANGLE CONVERSIONS" .IX Header "PLANE ANGLE CONVERSIONS" (Plane, 2\-dimensional) angles may be converted with the following functions. .PP .Vb 2 \& $radians = deg2rad($degrees); \& $radians = grad2rad($gradians); .Ve .PP .Vb 2 \& $degrees = rad2deg($radians); \& $degrees = grad2deg($gradians); .Ve .PP .Vb 2 \& $gradians = deg2grad($degrees); \& $gradians = rad2grad($radians); .Ve .PP The full circle is 2 \fIpi\fR radians or \fI360\fR degrees or \fI400\fR 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: .PP .Vb 2 \& $zillions_of_radians = deg2rad($zillions_of_degrees, 1); \& $negative_degrees = rad2deg($negative_radians, 1); .Ve .PP You can also do the wrapping explicitly by \fIrad2rad()\fR, \fIdeg2deg()\fR, and \&\fIgrad2grad()\fR. .SH "RADIAL COORDINATE CONVERSIONS" .IX Header "RADIAL COORDINATE CONVERSIONS" \&\fBRadial coordinate systems\fR are the \fBspherical\fR and the \fBcylindrical\fR systems, explained shortly in more detail. .PP You can import radial coordinate conversion functions by using the \&\f(CW\*(C`:radial\*(C'\fR tag: .PP .Vb 1 \& use Math::Trig ':radial'; .Ve .PP .Vb 6 \& ($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); .Ve .PP \&\fBAll angles are in radians\fR. .Sh "\s-1COORDINATE\s0 \s-1SYSTEMS\s0" .IX Subsection "COORDINATE SYSTEMS" \&\fBCartesian\fR coordinates are the usual rectangular \fI(x, y, z)\fR\-coordinates. .PP Spherical coordinates, \fI(rho, theta, pi)\fR, 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 \fBrho\fR, also known as the \fIradial\fR coordinate. The angle in the \fIxy\fR\-plane (around the \fIz\fR\-axis) is \fBtheta\fR, also known as the \fIazimuthal\fR coordinate. The angle from the \fIz\fR\-axis is \fBphi\fR, also known as the \&\fIpolar\fR coordinate. The `North Pole' is therefore \fI0, 0, rho\fR, and the `Bay of Guinea' (think of the missing big chunk of Africa) \fI0, pi/2, rho\fR. In geographical terms \fIphi\fR is latitude (northward positive, southward negative) and \fItheta\fR is longitude (eastward positive, westward negative). .PP \&\fB\s-1BEWARE\s0\fR: some texts define \fItheta\fR and \fIphi\fR the other way round, some texts define the \fIphi\fR to start from the horizontal plane, some texts use \fIr\fR in place of \fIrho\fR. .PP Cylindrical coordinates, \fI(rho, theta, z)\fR, 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 \fBrho\fR, also known as the \fIradial\fR coordinate. The angle in the \fIxy\fR\-plane (around the \fIz\fR\-axis) is \fBtheta\fR, also known as the \fIazimuthal\fR coordinate. The third coordinate is the \fIz\fR, pointing up from the \&\fBtheta\fR\-plane. .Sh "3\-D \s-1ANGLE\s0 \s-1CONVERSIONS\s0" .IX Subsection "3-D ANGLE CONVERSIONS" Conversions to and from spherical and cylindrical coordinates are available. Please notice that the conversions are not necessarily reversible because of the equalities like \fIpi\fR angles being equal to \&\fI\-pi\fR angles. .IP "cartesian_to_cylindrical" 4 .IX Item "cartesian_to_cylindrical" .Vb 1 \& ($rho, $theta, $z) = cartesian_to_cylindrical($x, $y, $z); .Ve .IP "cartesian_to_spherical" 4 .IX Item "cartesian_to_spherical" .Vb 1 \& ($rho, $theta, $phi) = cartesian_to_spherical($x, $y, $z); .Ve .IP "cylindrical_to_cartesian" 4 .IX Item "cylindrical_to_cartesian" .Vb 1 \& ($x, $y, $z) = cylindrical_to_cartesian($rho, $theta, $z); .Ve .IP "cylindrical_to_spherical" 4 .IX Item "cylindrical_to_spherical" .Vb 1 \& ($rho_s, $theta, $phi) = cylindrical_to_spherical($rho_c, $theta, $z); .Ve .Sp Notice that when \f(CW$z\fR is not 0 \f(CW$rho_s\fR is not equal to \f(CW$rho_c\fR. .IP "spherical_to_cartesian" 4 .IX Item "spherical_to_cartesian" .Vb 1 \& ($x, $y, $z) = spherical_to_cartesian($rho, $theta, $phi); .Ve .IP "spherical_to_cylindrical" 4 .IX Item "spherical_to_cylindrical" .Vb 1 \& ($rho_c, $theta, $z) = spherical_to_cylindrical($rho_s, $theta, $phi); .Ve .Sp Notice that when \f(CW$z\fR is not 0 \f(CW$rho_c\fR is not equal to \f(CW$rho_s\fR. .SH "GREAT CIRCLE DISTANCES AND DIRECTIONS" .IX Header "GREAT CIRCLE DISTANCES AND DIRECTIONS" You can compute spherical distances, called \fBgreat circle distances\fR, by importing the \fIgreat_circle_distance()\fR function: .PP .Vb 1 \& use Math::Trig 'great_circle_distance'; .Ve .PP .Vb 1 \& $distance = great_circle_distance($theta0, $phi0, $theta1, $phi1, [, $rho]); .Ve .PP The \fIgreat circle distance\fR is the shortest distance between two points on a sphere. The distance is in \f(CW$rho\fR units. The \f(CW$rho\fR is optional, it defaults to 1 (the unit sphere), therefore the distance defaults to radians. .PP If you think geographically the \fItheta\fR are longitudes: zero at the Greenwhich meridian, eastward positive, westward negative\*(--and the \&\fIphi\fR are latitudes: zero at the North Pole, northward positive, southward negative. \fB\s-1NOTE\s0\fR: this formula thinks in mathematics, not geographically: the \fIphi\fR 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 \fIpi/2\fR (also known as 90 degrees). .PP .Vb 2 \& $distance = great_circle_distance($lon0, pi/2 - $lat0, \& $lon1, pi/2 - $lat1, $rho); .Ve .PP The direction you must follow the great circle can be computed by the \&\fIgreat_circle_direction()\fR function: .PP .Vb 1 \& use Math::Trig 'great_circle_direction'; .Ve .PP .Vb 1 \& $direction = great_circle_direction($theta0, $phi0, $theta1, $phi1); .Ve .PP The result is in radians, zero indicating straight north, pi or \-pi straight south, pi/2 straight west, and \-pi/2 straight east. .PP 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. .SH "EXAMPLES" .IX Header "EXAMPLES" To calculate the distance between London (51.3N 0.5W) and Tokyo (35.7N 139.8E) in kilometers: .PP .Vb 1 \& use Math::Trig qw(great_circle_distance deg2rad); .Ve .PP .Vb 3 \& # 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)); .Ve .PP .Vb 1 \& $km = great_circle_distance(@L, @T, 6378); .Ve .PP The direction you would have to go from London to Tokyo .PP .Vb 1 \& use Math::Trig qw(great_circle_direction); .Ve .PP .Vb 1 \& $rad = great_circle_direction(@L, @T); .Ve .Sh "\s-1CAVEAT\s0 \s-1FOR\s0 \s-1GREAT\s0 \s-1CIRCLE\s0 \s-1FORMULAS\s0" .IX Subsection "CAVEAT FOR GREAT CIRCLE FORMULAS" The answers may be off by few percentages because of the irregular (slightly aspherical) form of the Earth. The formula used for grear circle distances .PP .Vb 4 \& lat0 = 90 degrees - phi0 \& lat1 = 90 degrees - phi1 \& d = R * arccos(cos(lat0) * cos(lat1) * cos(lon1 - lon01) + \& sin(lat0) * sin(lat1)) .Ve .PP is also somewhat unreliable for small distances (for locations separated less than about five degrees) because it uses arc cosine which is rather ill-conditioned for values close to zero. .SH "BUGS" .IX Header "BUGS" Saying \f(CW\*(C`use Math::Trig;\*(C'\fR exports many mathematical routines in the caller environment and even overrides some (\f(CW\*(C`sin\*(C'\fR, \f(CW\*(C`cos\*(C'\fR). This is construed as a feature by the Authors, actually... ;\-) .PP The code is not optimized for speed, especially because we use \&\f(CW\*(C`Math::Complex\*(C'\fR 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 \f(CWasin(2)\fR to give an answer instead of giving a fatal runtime error. .SH "AUTHORS" .IX Header "AUTHORS" Jarkko Hietaniemi <\fIjhi@iki.fi\fR> and Raphael Manfredi <\fIRaphael_Manfredi@pobox.com\fR>.