Leaked source code of windows server 2003
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.

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