Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

433 lines
12 KiB

  1. #
  2. # Trigonometric functions, mostly inherited from Math::Complex.
  3. # -- Jarkko Hietaniemi, since April 1997
  4. # -- Raphael Manfredi, September 1996 (indirectly: because of Math::Complex)
  5. #
  6. require Exporter;
  7. package Math::Trig;
  8. use strict;
  9. use Math::Complex qw(:trig);
  10. use vars qw($VERSION $PACKAGE
  11. @ISA
  12. @EXPORT @EXPORT_OK %EXPORT_TAGS);
  13. @ISA = qw(Exporter);
  14. $VERSION = 1.00;
  15. my @angcnv = qw(rad2deg rad2grad
  16. deg2rad deg2grad
  17. grad2rad grad2deg);
  18. @EXPORT = (@{$Math::Complex::EXPORT_TAGS{'trig'}},
  19. @angcnv);
  20. my @rdlcnv = qw(cartesian_to_cylindrical
  21. cartesian_to_spherical
  22. cylindrical_to_cartesian
  23. cylindrical_to_spherical
  24. spherical_to_cartesian
  25. spherical_to_cylindrical);
  26. @EXPORT_OK = (@rdlcnv, 'great_circle_distance');
  27. %EXPORT_TAGS = ('radial' => [ @rdlcnv ]);
  28. use constant pi2 => 2 * pi;
  29. use constant pip2 => pi / 2;
  30. use constant DR => pi2/360;
  31. use constant RD => 360/pi2;
  32. use constant DG => 400/360;
  33. use constant GD => 360/400;
  34. use constant RG => 400/pi2;
  35. use constant GR => pi2/400;
  36. #
  37. # Truncating remainder.
  38. #
  39. sub remt ($$) {
  40. # Oh yes, POSIX::fmod() would be faster. Possibly. If it is available.
  41. $_[0] - $_[1] * int($_[0] / $_[1]);
  42. }
  43. #
  44. # Angle conversions.
  45. #
  46. sub rad2deg ($) { remt(RD * $_[0], 360) }
  47. sub deg2rad ($) { remt(DR * $_[0], pi2) }
  48. sub grad2deg ($) { remt(GD * $_[0], 360) }
  49. sub deg2grad ($) { remt(DG * $_[0], 400) }
  50. sub rad2grad ($) { remt(RG * $_[0], 400) }
  51. sub grad2rad ($) { remt(GR * $_[0], pi2) }
  52. sub cartesian_to_spherical {
  53. my ( $x, $y, $z ) = @_;
  54. my $rho = sqrt( $x * $x + $y * $y + $z * $z );
  55. return ( $rho,
  56. atan2( $y, $x ),
  57. $rho ? acos( $z / $rho ) : 0 );
  58. }
  59. sub spherical_to_cartesian {
  60. my ( $rho, $theta, $phi ) = @_;
  61. return ( $rho * cos( $theta ) * sin( $phi ),
  62. $rho * sin( $theta ) * sin( $phi ),
  63. $rho * cos( $phi ) );
  64. }
  65. sub spherical_to_cylindrical {
  66. my ( $x, $y, $z ) = spherical_to_cartesian( @_ );
  67. return ( sqrt( $x * $x + $y * $y ), $_[1], $z );
  68. }
  69. sub cartesian_to_cylindrical {
  70. my ( $x, $y, $z ) = @_;
  71. return ( sqrt( $x * $x + $y * $y ), atan2( $y, $x ), $z );
  72. }
  73. sub cylindrical_to_cartesian {
  74. my ( $rho, $theta, $z ) = @_;
  75. return ( $rho * cos( $theta ), $rho * sin( $theta ), $z );
  76. }
  77. sub cylindrical_to_spherical {
  78. return ( cartesian_to_spherical( cylindrical_to_cartesian( @_ ) ) );
  79. }
  80. sub great_circle_distance {
  81. my ( $theta0, $phi0, $theta1, $phi1, $rho ) = @_;
  82. $rho = 1 unless defined $rho; # Default to the unit sphere.
  83. my $lat0 = pip2 - $phi0;
  84. my $lat1 = pip2 - $phi1;
  85. return $rho *
  86. acos(cos( $lat0 ) * cos( $lat1 ) * cos( $theta0 - $theta1 ) +
  87. sin( $lat0 ) * sin( $lat1 ) );
  88. }
  89. =pod
  90. =head1 NAME
  91. Math::Trig - trigonometric functions
  92. =head1 SYNOPSIS
  93. use Math::Trig;
  94. $x = tan(0.9);
  95. $y = acos(3.7);
  96. $z = asin(2.4);
  97. $halfpi = pi/2;
  98. $rad = deg2rad(120);
  99. =head1 DESCRIPTION
  100. C<Math::Trig> defines many trigonometric functions not defined by the
  101. core Perl which defines only the C<sin()> and C<cos()>. The constant
  102. B<pi> is also defined as are a few convenience functions for angle
  103. conversions.
  104. =head1 TRIGONOMETRIC FUNCTIONS
  105. The tangent
  106. =over 4
  107. =item B<tan>
  108. =back
  109. The cofunctions of the sine, cosine, and tangent (cosec/csc and cotan/cot
  110. are aliases)
  111. B<csc>, B<cosec>, B<sec>, B<sec>, B<cot>, B<cotan>
  112. The arcus (also known as the inverse) functions of the sine, cosine,
  113. and tangent
  114. B<asin>, B<acos>, B<atan>
  115. The principal value of the arc tangent of y/x
  116. B<atan2>(y, x)
  117. The arcus cofunctions of the sine, cosine, and tangent (acosec/acsc
  118. and acotan/acot are aliases)
  119. B<acsc>, B<acosec>, B<asec>, B<acot>, B<acotan>
  120. The hyperbolic sine, cosine, and tangent
  121. B<sinh>, B<cosh>, B<tanh>
  122. The cofunctions of the hyperbolic sine, cosine, and tangent (cosech/csch
  123. and cotanh/coth are aliases)
  124. B<csch>, B<cosech>, B<sech>, B<coth>, B<cotanh>
  125. The arcus (also known as the inverse) functions of the hyperbolic
  126. sine, cosine, and tangent
  127. B<asinh>, B<acosh>, B<atanh>
  128. The arcus cofunctions of the hyperbolic sine, cosine, and tangent
  129. (acsch/acosech and acoth/acotanh are aliases)
  130. B<acsch>, B<acosech>, B<asech>, B<acoth>, B<acotanh>
  131. The trigonometric constant B<pi> is also defined.
  132. $pi2 = 2 * B<pi>;
  133. =head2 ERRORS DUE TO DIVISION BY ZERO
  134. The following functions
  135. acoth
  136. acsc
  137. acsch
  138. asec
  139. asech
  140. atanh
  141. cot
  142. coth
  143. csc
  144. csch
  145. sec
  146. sech
  147. tan
  148. tanh
  149. cannot be computed for all arguments because that would mean dividing
  150. by zero or taking logarithm of zero. These situations cause fatal
  151. runtime errors looking like this
  152. cot(0): Division by zero.
  153. (Because in the definition of cot(0), the divisor sin(0) is 0)
  154. Died at ...
  155. or
  156. atanh(-1): Logarithm of zero.
  157. Died at...
  158. For the C<csc>, C<cot>, C<asec>, C<acsc>, C<acot>, C<csch>, C<coth>,
  159. C<asech>, C<acsch>, the argument cannot be C<0> (zero). For the
  160. C<atanh>, C<acoth>, the argument cannot be C<1> (one). For the
  161. C<atanh>, C<acoth>, the argument cannot be C<-1> (minus one). For the
  162. C<tan>, C<sec>, C<tanh>, C<sech>, the argument cannot be I<pi/2 + k *
  163. pi>, where I<k> is any integer.
  164. =head2 SIMPLE (REAL) ARGUMENTS, COMPLEX RESULTS
  165. Please note that some of the trigonometric functions can break out
  166. from the B<real axis> into the B<complex plane>. For example
  167. C<asin(2)> has no definition for plain real numbers but it has
  168. definition for complex numbers.
  169. In Perl terms this means that supplying the usual Perl numbers (also
  170. known as scalars, please see L<perldata>) as input for the
  171. trigonometric functions might produce as output results that no more
  172. are simple real numbers: instead they are complex numbers.
  173. The C<Math::Trig> handles this by using the C<Math::Complex> package
  174. which knows how to handle complex numbers, please see L<Math::Complex>
  175. for more information. In practice you need not to worry about getting
  176. complex numbers as results because the C<Math::Complex> takes care of
  177. details like for example how to display complex numbers. For example:
  178. print asin(2), "\n";
  179. should produce something like this (take or leave few last decimals):
  180. 1.5707963267949-1.31695789692482i
  181. That is, a complex number with the real part of approximately C<1.571>
  182. and the imaginary part of approximately C<-1.317>.
  183. =head1 PLANE ANGLE CONVERSIONS
  184. (Plane, 2-dimensional) angles may be converted with the following functions.
  185. $radians = deg2rad($degrees);
  186. $radians = grad2rad($gradians);
  187. $degrees = rad2deg($radians);
  188. $degrees = grad2deg($gradians);
  189. $gradians = deg2grad($degrees);
  190. $gradians = rad2grad($radians);
  191. The full circle is 2 I<pi> radians or I<360> degrees or I<400> gradians.
  192. =head1 RADIAL COORDINATE CONVERSIONS
  193. B<Radial coordinate systems> are the B<spherical> and the B<cylindrical>
  194. systems, explained shortly in more detail.
  195. You can import radial coordinate conversion functions by using the
  196. C<:radial> tag:
  197. use Math::Trig ':radial';
  198. ($rho, $theta, $z) = cartesian_to_cylindrical($x, $y, $z);
  199. ($rho, $theta, $phi) = cartesian_to_spherical($x, $y, $z);
  200. ($x, $y, $z) = cylindrical_to_cartesian($rho, $theta, $z);
  201. ($rho_s, $theta, $phi) = cylindrical_to_spherical($rho_c, $theta, $z);
  202. ($x, $y, $z) = spherical_to_cartesian($rho, $theta, $phi);
  203. ($rho_c, $theta, $z) = spherical_to_cylindrical($rho_s, $theta, $phi);
  204. B<All angles are in radians>.
  205. =head2 COORDINATE SYSTEMS
  206. B<Cartesian> coordinates are the usual rectangular I<(x, y,
  207. z)>-coordinates.
  208. Spherical coordinates, I<(rho, theta, pi)>, are three-dimensional
  209. coordinates which define a point in three-dimensional space. They are
  210. based on a sphere surface. The radius of the sphere is B<rho>, also
  211. known as the I<radial> coordinate. The angle in the I<xy>-plane
  212. (around the I<z>-axis) is B<theta>, also known as the I<azimuthal>
  213. coordinate. The angle from the I<z>-axis is B<phi>, also known as the
  214. I<polar> coordinate. The `North Pole' is therefore I<0, 0, rho>, and
  215. the `Bay of Guinea' (think of the missing big chunk of Africa) I<0,
  216. pi/2, rho>. In geographical terms I<phi> is latitude (northward
  217. positive, southward negative) and I<theta> is longitude (eastward
  218. positive, westward negative).
  219. B<BEWARE>: some texts define I<theta> and I<phi> the other way round,
  220. some texts define the I<phi> to start from the horizontal plane, some
  221. texts use I<r> in place of I<rho>.
  222. Cylindrical coordinates, I<(rho, theta, z)>, are three-dimensional
  223. coordinates which define a point in three-dimensional space. They are
  224. based on a cylinder surface. The radius of the cylinder is B<rho>,
  225. also known as the I<radial> coordinate. The angle in the I<xy>-plane
  226. (around the I<z>-axis) is B<theta>, also known as the I<azimuthal>
  227. coordinate. The third coordinate is the I<z>, pointing up from the
  228. B<theta>-plane.
  229. =head2 3-D ANGLE CONVERSIONS
  230. Conversions to and from spherical and cylindrical coordinates are
  231. available. Please notice that the conversions are not necessarily
  232. reversible because of the equalities like I<pi> angles being equal to
  233. I<-pi> angles.
  234. =over 4
  235. =item cartesian_to_cylindrical
  236. ($rho, $theta, $z) = cartesian_to_cylindrical($x, $y, $z);
  237. =item cartesian_to_spherical
  238. ($rho, $theta, $phi) = cartesian_to_spherical($x, $y, $z);
  239. =item cylindrical_to_cartesian
  240. ($x, $y, $z) = cylindrical_to_cartesian($rho, $theta, $z);
  241. =item cylindrical_to_spherical
  242. ($rho_s, $theta, $phi) = cylindrical_to_spherical($rho_c, $theta, $z);
  243. Notice that when C<$z> is not 0 C<$rho_s> is not equal to C<$rho_c>.
  244. =item spherical_to_cartesian
  245. ($x, $y, $z) = spherical_to_cartesian($rho, $theta, $phi);
  246. =item spherical_to_cylindrical
  247. ($rho_c, $theta, $z) = spherical_to_cylindrical($rho_s, $theta, $phi);
  248. Notice that when C<$z> is not 0 C<$rho_c> is not equal to C<$rho_s>.
  249. =back
  250. =head1 GREAT CIRCLE DISTANCES
  251. You can compute spherical distances, called B<great circle distances>,
  252. by importing the C<great_circle_distance> function:
  253. use Math::Trig 'great_circle_distance'
  254. $distance = great_circle_distance($theta0, $phi0, $theta1, $phi1, [, $rho]);
  255. The I<great circle distance> is the shortest distance between two
  256. points on a sphere. The distance is in C<$rho> units. The C<$rho> is
  257. optional, it defaults to 1 (the unit sphere), therefore the distance
  258. defaults to radians.
  259. If you think geographically the I<theta> are longitudes: zero at the
  260. Greenwhich meridian, eastward positive, westward negative--and the
  261. I<phi> are latitudes: zero at the North Pole, northward positive,
  262. southward negative. B<NOTE>: this formula thinks in mathematics, not
  263. geographically: the I<phi> zero is at the North Pole, not at the
  264. Equator on the west coast of Africa (Bay of Guinea). You need to
  265. subtract your geographical coordinates from I<pi/2> (also known as 90
  266. degrees).
  267. $distance = great_circle_distance($lon0, pi/2 - $lat0,
  268. $lon1, pi/2 - $lat1, $rho);
  269. =head1 EXAMPLES
  270. To calculate the distance between London (51.3N 0.5W) and Tokyo (35.7N
  271. 139.8E) in kilometers:
  272. use Math::Trig qw(great_circle_distance deg2rad);
  273. # Notice the 90 - latitude: phi zero is at the North Pole.
  274. @L = (deg2rad(-0.5), deg2rad(90 - 51.3));
  275. @T = (deg2rad(139.8),deg2rad(90 - 35.7));
  276. $km = great_circle_distance(@L, @T, 6378);
  277. The answer may be off by few percentages because of the irregular
  278. (slightly aspherical) form of the Earth.
  279. =head1 BUGS
  280. Saying C<use Math::Trig;> exports many mathematical routines in the
  281. caller environment and even overrides some (C<sin>, C<cos>). This is
  282. construed as a feature by the Authors, actually... ;-)
  283. The code is not optimized for speed, especially because we use
  284. C<Math::Complex> and thus go quite near complex numbers while doing
  285. the computations even when the arguments are not. This, however,
  286. cannot be completely avoided if we want things like C<asin(2)> to give
  287. an answer instead of giving a fatal runtime error.
  288. =head1 AUTHORS
  289. Jarkko Hietaniemi <F<[email protected]>> and
  290. Raphael Manfredi <F<[email protected]>>.
  291. =cut
  292. # eof