.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45) .\" .\" Standard preamble: .\" ======================================================================== .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 .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Math::Trig 3perl" .TH Math::Trig 3perl 2024-09-01 "perl v5.40.0" "Perl Programmers Reference Guide" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME Math::Trig \- trigonometric functions .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 1 \& use Math::Trig; \& \& $x = tan(0.9); \& $y = acos(3.7); \& $z = asin(2.4); \& \& $halfpi = pi/2; \& \& $rad = deg2rad(120); \& \& # Import constants pi2, pi4, pip2, pip4 (2*pi, 4*pi, pi/2, pi/4). \& use Math::Trig \*(Aq:pi\*(Aq; \& \& # Import the conversions between cartesian/spherical/cylindrical. \& use Math::Trig \*(Aq:radial\*(Aq; \& \& # Import the great circle formulas. \& use Math::Trig \*(Aq:great_circle\*(Aq; .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(CWsin()\fR and \f(CWcos()\fR. The constant \&\fBpi\fR is also defined as are a few convenience functions for angle conversions, and \fIgreat circle formulas\fR for spherical movement. .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). Note that atan2(0, 0) is not well-defined. .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 area (also known as the inverse) functions of the hyperbolic sine, cosine, and tangent .PP \&\fBasinh\fR, \fBacosh\fR, \fBatanh\fR .PP The area 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 and some of handy multiples of it are also defined. .PP \&\fBpi, pi2, pi4, pip2, pip4\fR .SS "ERRORS DUE TO DIVISION BY ZERO" .IX Subsection "ERRORS DUE TO DIVISION BY ZERO" The following functions .PP .Vb 10 \& 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\-1\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. .PP Note that atan2(0, 0) is not well-defined. .SS "SIMPLE (REAL) ARGUMENTS, COMPLEX RESULTS" .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\-1.317\fR. .SH "PLANE ANGLE CONVERSIONS" .IX Header "PLANE ANGLE CONVERSIONS" (Plane, 2\-dimensional) angles may be converted with the following functions. .IP deg2rad 4 .IX Item "deg2rad" .Vb 1 \& $radians = deg2rad($degrees); .Ve .IP grad2rad 4 .IX Item "grad2rad" .Vb 1 \& $radians = grad2rad($gradians); .Ve .IP rad2deg 4 .IX Item "rad2deg" .Vb 1 \& $degrees = rad2deg($radians); .Ve .IP grad2deg 4 .IX Item "grad2deg" .Vb 1 \& $degrees = grad2deg($gradians); .Ve .IP deg2grad 4 .IX Item "deg2grad" .Vb 1 \& $gradians = deg2grad($degrees); .Ve .IP rad2grad 4 .IX Item "rad2grad" .Vb 1 \& $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 \fBrad2rad()\fR, \fBdeg2deg()\fR, and \&\fBgrad2grad()\fR. .IP rad2rad 4 .IX Item "rad2rad" .Vb 1 \& $radians_wrapped_by_2pi = rad2rad($radians); .Ve .IP deg2deg 4 .IX Item "deg2deg" .Vb 1 \& $degrees_wrapped_by_360 = deg2deg($degrees); .Ve .IP grad2grad 4 .IX Item "grad2grad" .Vb 1 \& $gradians_wrapped_by_400 = grad2grad($gradians); .Ve .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 \*(Aq:radial\*(Aq; \& \& ($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. .SS "COORDINATE SYSTEMS" .IX Subsection "COORDINATE SYSTEMS" \&\fBCartesian\fR coordinates are the usual rectangular \fI(x, y, z)\fR\-coordinates. .PP Spherical coordinates, \fI(rho, theta, phi)\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 \fIrho, 0, 0\fR, and the Gulf of Guinea (think of the missing big chunk of Africa) \fIrho, 0, pi/2\fR. In geographical terms \fIphi\fR is latitude (northward positive, southward negative) and \fItheta\fR is longitude (eastward positive, westward negative). .PP \&\fBBEWARE\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. .SS "3\-D ANGLE CONVERSIONS" .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" 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. .SS great_circle_distance .IX Subsection "great_circle_distance" Returns the great circle distance between two points on a sphere. .PP .Vb 1 \& $distance = great_circle_distance($theta0, $phi0, $theta1, $phi1, [, $rho]); .Ve .PP Where ($theta0, \f(CW$phi0\fR) and ($theta1, \f(CW$phi1\fR) are the spherical coordinates of the two points, respectively. The distance is in \f(CW$rho\fR units. The \f(CW$rho\fR is optional. It defaults to 1 (the unit sphere). .PP If you are using geographic coordinates, latitude and longitude, you need to adjust for the fact that latitude is zero at the equator increasing towards the north and decreasing towards the south. Assuming ($lat0, \f(CW$lon0\fR) and ($lat1, \f(CW$lon1\fR) are the geographic coordinates in radians of the two points, the distance can be computed with .PP .Vb 2 \& $distance = great_circle_distance($lon0, pi/2 \- $lat0, \& $lon1, pi/2 \- $lat1, $rho); .Ve .SS great_circle_direction .IX Subsection "great_circle_direction" The direction you must follow the great circle (also known as \fIbearing\fR) can be computed by the \fBgreat_circle_direction()\fR function: .PP .Vb 1 \& use Math::Trig \*(Aqgreat_circle_direction\*(Aq; \& \& $direction = great_circle_direction($theta0, $phi0, $theta1, $phi1); .Ve .SS great_circle_bearing .IX Subsection "great_circle_bearing" Alias 'great_circle_bearing' for 'great_circle_direction' is also available. .PP .Vb 1 \& use Math::Trig \*(Aqgreat_circle_bearing\*(Aq; \& \& $direction = great_circle_bearing($theta0, $phi0, $theta1, $phi1); .Ve .PP 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. .SS great_circle_destination .IX Subsection "great_circle_destination" You can inversely compute the destination if you know the starting point, direction, and distance: .PP .Vb 1 \& use Math::Trig \*(Aqgreat_circle_destination\*(Aq; \& \& # $diro is the original direction, \& # for example from great_circle_bearing(). \& # $distance is the angular distance in radians, \& # for example from great_circle_distance(). \& # $thetad and $phid are the destination coordinates, \& # $dird is the final direction at the destination. \& \& ($thetad, $phid, $dird) = \& great_circle_destination($theta, $phi, $diro, $distance); .Ve .PP or the midpoint if you know the end points: .SS great_circle_midpoint .IX Subsection "great_circle_midpoint" .Vb 1 \& use Math::Trig \*(Aqgreat_circle_midpoint\*(Aq; \& \& ($thetam, $phim) = \& great_circle_midpoint($theta0, $phi0, $theta1, $phi1); .Ve .PP The \fBgreat_circle_midpoint()\fR is just a special case of .SS great_circle_waypoint .IX Subsection "great_circle_waypoint" .Vb 1 \& use Math::Trig \*(Aqgreat_circle_waypoint\*(Aq; \& \& ($thetai, $phii) = \& great_circle_waypoint($theta0, $phi0, $theta1, $phi1, $way); .Ve .PP Where \f(CW$way\fR indicates the position of the waypoint along the great circle arc through the starting point ($theta0, \f(CW$phi0\fR) and the end point ($theta1, \f(CW$phi1\fR) relative to the distance from the starting point to the end point. So \f(CW$way\fR = 0 gives the starting point, \f(CW$way\fR = 1 gives the end point, \f(CW$way\fR < 0 gives a point "behind" the starting point, and \f(CW$way\fR > 1 gives a point beyond the end point. \f(CW$way\fR defaults to 0.5 if not given. .PP Note that antipodal points (where their distance is \fIpi\fR radians) do not have unique waypoints between them, and therefore \f(CW\*(C`undef\*(C'\fR is returned in such cases. If the points are the same, so the distance between them is zero, all waypoints are identical to the starting/end point. .PP The thetas, phis, direction, and distance in the above are all in radians. .PP You can import all the great circle formulas by .PP .Vb 1 \& use Math::Trig \*(Aq:great_circle\*(Aq; .Ve .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. (The common Mercator projection does \fBnot\fR show great circles as straight lines: straight lines in the Mercator projection are lines of constant bearing.) .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); \& \& # 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. .Ve .PP The direction you would have to go from London to Tokyo (in radians, straight north being zero, straight east being pi/2). .PP .Vb 1 \& use Math::Trig qw(great_circle_direction); \& \& my $rad = great_circle_direction(@L, @T); # About 0.547 or 0.174 pi. .Ve .PP The midpoint between London and Tokyo being .PP .Vb 1 \& use Math::Trig qw(great_circle_midpoint rad2deg); \& \& my @M = great_circle_midpoint(@L, @T); \& sub SWNE { rad2deg( $_[0] ), 90 \- rad2deg( $_[1] ) } \& my @lonlat = SWNE(@M); .Ve .PP or about 69 N 89 E, on the Putorana Plateau of Siberia. .PP \&\fBNOTE\fR: you \fBcannot\fR get from A to B like this: .PP .Vb 3 \& Dist = great_circle_distance(A, B) \& Dir = great_circle_direction(A, B) \& C = great_circle_destination(A, Dist, Dir) .Ve .PP and expect C to be B, because the bearing constantly changes when going from A to B (except in some special case like the meridians or the circles of latitudes) and in \fBgreat_circle_destination()\fR one gives a \fBconstant\fR bearing to follow. .SS "CAVEAT FOR GREAT CIRCLE FORMULAS" .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 errors are at worst about 0.55%, but generally below 0.3%. .SS "Real-valued asin and acos" .IX Subsection "Real-valued asin and acos" For small inputs \fBasin()\fR and \fBacos()\fR may return complex numbers even when real numbers would be enough and correct, this happens because of floating-point inaccuracies. You can see these inaccuracies for example by trying theses: .PP .Vb 2 \& print cos(1e\-6)**2+sin(1e\-6)**2 \- 1,"\en"; \& printf "%.20f", cos(1e\-6)**2+sin(1e\-6)**2,"\en"; .Ve .PP which will print something like this .PP .Vb 2 \& \-1.11022302462516e\-16 \& 0.99999999999999988898 .Ve .PP even though the expected results are of course exactly zero and one. The formulas used to compute \fBasin()\fR and \fBacos()\fR are quite sensitive to this, and therefore they might accidentally slip into the complex plane even when they should not. To counter this there are two interfaces that are guaranteed to return a real-valued output. .IP asin_real 4 .IX Item "asin_real" .Vb 1 \& use Math::Trig qw(asin_real); \& \& $real_angle = asin_real($input_sin); .Ve .Sp Return a real-valued arcus sine if the input is between [\-1, 1], \&\fBinclusive\fR the endpoints. For inputs greater than one, pi/2 is returned. For inputs less than minus one, \-pi/2 is returned. .IP acos_real 4 .IX Item "acos_real" .Vb 1 \& use Math::Trig qw(acos_real); \& \& $real_angle = acos_real($input_cos); .Ve .Sp Return a real-valued arcus cosine if the input is between [\-1, 1], \&\fBinclusive\fR the endpoints. For inputs greater than one, zero is returned. For inputs less than minus one, pi is returned. .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. .PP Do not attempt navigation using these formulas. .SH "SEE ALSO" .IX Header "SEE ALSO" Math::Complex .SH AUTHORS .IX Header "AUTHORS" Jarkko Hietaniemi <\fIjhi!at!iki.fi\fR>, Raphael Manfredi <\fIRaphael_Manfredi!at!pobox.com\fR>, Zefram .SH LICENSE .IX Header "LICENSE" This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.