2 # Trigonometric functions, mostly inherited from Math::Complex.
3 # -- Jarkko Hietaniemi, since April 1997
4 # -- Raphael Manfredi, September 1996 (indirectly: because of Math::Complex)
12 use Math::Complex qw(:trig);
14 use vars qw($VERSION $PACKAGE
16 @EXPORT @EXPORT_OK %EXPORT_TAGS);
22 my @angcnv = qw(rad2deg rad2grad
26 @EXPORT = (@{$Math::Complex::EXPORT_TAGS{'trig'}},
29 my @rdlcnv = qw(cartesian_to_cylindrical
30 cartesian_to_spherical
31 cylindrical_to_cartesian
32 cylindrical_to_spherical
33 spherical_to_cartesian
34 spherical_to_cylindrical);
36 @EXPORT_OK = (@rdlcnv, 'great_circle_distance');
38 %EXPORT_TAGS = ('radial' => [ @rdlcnv ]);
40 use constant pi2 => 2 * pi;
41 use constant pip2 => pi / 2;
42 use constant DR => pi2/360;
43 use constant RD => 360/pi2;
44 use constant DG => 400/360;
45 use constant GD => 360/400;
46 use constant RG => 400/pi2;
47 use constant GR => pi2/400;
50 # Truncating remainder.
54 # Oh yes, POSIX::fmod() would be faster. Possibly. If it is available.
55 $_[0] - $_[1] * int($_[0] / $_[1]);
62 sub rad2deg ($) { remt(RD * $_[0], 360) }
64 sub deg2rad ($) { remt(DR * $_[0], pi2) }
66 sub grad2deg ($) { remt(GD * $_[0], 360) }
68 sub deg2grad ($) { remt(DG * $_[0], 400) }
70 sub rad2grad ($) { remt(RG * $_[0], 400) }
72 sub grad2rad ($) { remt(GR * $_[0], pi2) }
74 sub cartesian_to_spherical {
75 my ( $x, $y, $z ) = @_;
77 my $rho = sqrt( $x * $x + $y * $y + $z * $z );
81 $rho ? acos( $z / $rho ) : 0 );
84 sub spherical_to_cartesian {
85 my ( $rho, $theta, $phi ) = @_;
87 return ( $rho * cos( $theta ) * sin( $phi ),
88 $rho * sin( $theta ) * sin( $phi ),
92 sub spherical_to_cylindrical {
93 my ( $x, $y, $z ) = spherical_to_cartesian( @_ );
95 return ( sqrt( $x * $x + $y * $y ), $_[1], $z );
98 sub cartesian_to_cylindrical {
99 my ( $x, $y, $z ) = @_;
101 return ( sqrt( $x * $x + $y * $y ), atan2( $y, $x ), $z );
104 sub cylindrical_to_cartesian {
105 my ( $rho, $theta, $z ) = @_;
107 return ( $rho * cos( $theta ), $rho * sin( $theta ), $z );
110 sub cylindrical_to_spherical {
111 return ( cartesian_to_spherical( cylindrical_to_cartesian( @_ ) ) );
114 sub great_circle_distance {
115 my ( $theta0, $phi0, $theta1, $phi1, $rho ) = @_;
117 $rho = 1 unless defined $rho; # Default to the unit sphere.
119 my $lat0 = pip2 - $phi0;
120 my $lat1 = pip2 - $phi1;
123 acos(cos( $lat0 ) * cos( $lat1 ) * cos( $theta0 - $theta1 ) +
124 sin( $lat0 ) * sin( $lat1 ) );
131 Math::Trig - trigonometric functions
147 C<Math::Trig> defines many trigonometric functions not defined by the
148 core Perl which defines only the C<sin()> and C<cos()>. The constant
149 B<pi> is also defined as are a few convenience functions for angle
152 =head1 TRIGONOMETRIC FUNCTIONS
162 The cofunctions of the sine, cosine, and tangent (cosec/csc and cotan/cot
165 B<csc>, B<cosec>, B<sec>, B<sec>, B<cot>, B<cotan>
167 The arcus (also known as the inverse) functions of the sine, cosine,
170 B<asin>, B<acos>, B<atan>
172 The principal value of the arc tangent of y/x
176 The arcus cofunctions of the sine, cosine, and tangent (acosec/acsc
177 and acotan/acot are aliases)
179 B<acsc>, B<acosec>, B<asec>, B<acot>, B<acotan>
181 The hyperbolic sine, cosine, and tangent
183 B<sinh>, B<cosh>, B<tanh>
185 The cofunctions of the hyperbolic sine, cosine, and tangent (cosech/csch
186 and cotanh/coth are aliases)
188 B<csch>, B<cosech>, B<sech>, B<coth>, B<cotanh>
190 The arcus (also known as the inverse) functions of the hyperbolic
191 sine, cosine, and tangent
193 B<asinh>, B<acosh>, B<atanh>
195 The arcus cofunctions of the hyperbolic sine, cosine, and tangent
196 (acsch/acosech and acoth/acotanh are aliases)
198 B<acsch>, B<acosech>, B<asech>, B<acoth>, B<acotanh>
200 The trigonometric constant B<pi> is also defined.
204 =head2 ERRORS DUE TO DIVISION BY ZERO
206 The following functions
223 cannot be computed for all arguments because that would mean dividing
224 by zero or taking logarithm of zero. These situations cause fatal
225 runtime errors looking like this
227 cot(0): Division by zero.
228 (Because in the definition of cot(0), the divisor sin(0) is 0)
233 atanh(-1): Logarithm of zero.
236 For the C<csc>, C<cot>, C<asec>, C<acsc>, C<acot>, C<csch>, C<coth>,
237 C<asech>, C<acsch>, the argument cannot be C<0> (zero). For the
238 C<atanh>, C<acoth>, the argument cannot be C<1> (one). For the
239 C<atanh>, C<acoth>, the argument cannot be C<-1> (minus one). For the
240 C<tan>, C<sec>, C<tanh>, C<sech>, the argument cannot be I<pi/2 + k *
241 pi>, where I<k> is any integer.
243 =head2 SIMPLE (REAL) ARGUMENTS, COMPLEX RESULTS
245 Please note that some of the trigonometric functions can break out
246 from the B<real axis> into the B<complex plane>. For example
247 C<asin(2)> has no definition for plain real numbers but it has
248 definition for complex numbers.
250 In Perl terms this means that supplying the usual Perl numbers (also
251 known as scalars, please see L<perldata>) as input for the
252 trigonometric functions might produce as output results that no more
253 are simple real numbers: instead they are complex numbers.
255 The C<Math::Trig> handles this by using the C<Math::Complex> package
256 which knows how to handle complex numbers, please see L<Math::Complex>
257 for more information. In practice you need not to worry about getting
258 complex numbers as results because the C<Math::Complex> takes care of
259 details like for example how to display complex numbers. For example:
263 should produce something like this (take or leave few last decimals):
265 1.5707963267949-1.31695789692482i
267 That is, a complex number with the real part of approximately C<1.571>
268 and the imaginary part of approximately C<-1.317>.
270 =head1 PLANE ANGLE CONVERSIONS
272 (Plane, 2-dimensional) angles may be converted with the following functions.
274 $radians = deg2rad($degrees);
275 $radians = grad2rad($gradians);
277 $degrees = rad2deg($radians);
278 $degrees = grad2deg($gradians);
280 $gradians = deg2grad($degrees);
281 $gradians = rad2grad($radians);
283 The full circle is 2 I<pi> radians or I<360> degrees or I<400> gradians.
285 =head1 RADIAL COORDINATE CONVERSIONS
287 B<Radial coordinate systems> are the B<spherical> and the B<cylindrical>
288 systems, explained shortly in more detail.
290 You can import radial coordinate conversion functions by using the
293 use Math::Trig ':radial';
295 ($rho, $theta, $z) = cartesian_to_cylindrical($x, $y, $z);
296 ($rho, $theta, $phi) = cartesian_to_spherical($x, $y, $z);
297 ($x, $y, $z) = cylindrical_to_cartesian($rho, $theta, $z);
298 ($rho_s, $theta, $phi) = cylindrical_to_spherical($rho_c, $theta, $z);
299 ($x, $y, $z) = spherical_to_cartesian($rho, $theta, $phi);
300 ($rho_c, $theta, $z) = spherical_to_cylindrical($rho_s, $theta, $phi);
302 B<All angles are in radians>.
304 =head2 COORDINATE SYSTEMS
306 B<Cartesian> coordinates are the usual rectangular I<(x, y,
309 Spherical coordinates, I<(rho, theta, pi)>, are three-dimensional
310 coordinates which define a point in three-dimensional space. They are
311 based on a sphere surface. The radius of the sphere is B<rho>, also
312 known as the I<radial> coordinate. The angle in the I<xy>-plane
313 (around the I<z>-axis) is B<theta>, also known as the I<azimuthal>
314 coordinate. The angle from the I<z>-axis is B<phi>, also known as the
315 I<polar> coordinate. The `North Pole' is therefore I<0, 0, rho>, and
316 the `Bay of Guinea' (think of the missing big chunk of Africa) I<0,
317 pi/2, rho>. In geographical terms I<phi> is latitude (northward
318 positive, southward negative) and I<theta> is longitude (eastward
319 positive, westward negative).
321 B<BEWARE>: some texts define I<theta> and I<phi> the other way round,
322 some texts define the I<phi> to start from the horizontal plane, some
323 texts use I<r> in place of I<rho>.
325 Cylindrical coordinates, I<(rho, theta, z)>, are three-dimensional
326 coordinates which define a point in three-dimensional space. They are
327 based on a cylinder surface. The radius of the cylinder is B<rho>,
328 also known as the I<radial> coordinate. The angle in the I<xy>-plane
329 (around the I<z>-axis) is B<theta>, also known as the I<azimuthal>
330 coordinate. The third coordinate is the I<z>, pointing up from the
333 =head2 3-D ANGLE CONVERSIONS
335 Conversions to and from spherical and cylindrical coordinates are
336 available. Please notice that the conversions are not necessarily
337 reversible because of the equalities like I<pi> angles being equal to
342 =item cartesian_to_cylindrical
344 ($rho, $theta, $z) = cartesian_to_cylindrical($x, $y, $z);
346 =item cartesian_to_spherical
348 ($rho, $theta, $phi) = cartesian_to_spherical($x, $y, $z);
350 =item cylindrical_to_cartesian
352 ($x, $y, $z) = cylindrical_to_cartesian($rho, $theta, $z);
354 =item cylindrical_to_spherical
356 ($rho_s, $theta, $phi) = cylindrical_to_spherical($rho_c, $theta, $z);
358 Notice that when C<$z> is not 0 C<$rho_s> is not equal to C<$rho_c>.
360 =item spherical_to_cartesian
362 ($x, $y, $z) = spherical_to_cartesian($rho, $theta, $phi);
364 =item spherical_to_cylindrical
366 ($rho_c, $theta, $z) = spherical_to_cylindrical($rho_s, $theta, $phi);
368 Notice that when C<$z> is not 0 C<$rho_c> is not equal to C<$rho_s>.
372 =head1 GREAT CIRCLE DISTANCES
374 You can compute spherical distances, called B<great circle distances>,
375 by importing the C<great_circle_distance> function:
377 use Math::Trig 'great_circle_distance'
379 $distance = great_circle_distance($theta0, $phi0, $theta1, $phi1, [, $rho]);
381 The I<great circle distance> is the shortest distance between two
382 points on a sphere. The distance is in C<$rho> units. The C<$rho> is
383 optional, it defaults to 1 (the unit sphere), therefore the distance
386 If you think geographically the I<theta> are longitudes: zero at the
387 Greenwhich meridian, eastward positive, westward negative--and the
388 I<phi> are latitudes: zero at North Pole, northward positive,
389 southward negative. B<NOTE>: this formula thinks in mathematics, not
390 geographically: the I<phi> zero is at the Nort Pole, not on the
391 west coast of Africa (Bay of Guinea). You need to subtract your
392 geographical coordinates from I<pi/2> (also known as 90 degrees).
394 $distance = great_circle_distance($lon0, pi/2 - $lat0,
395 $lon1, pi/2 - $lat1, $rho);
399 To calculate the distance between London (51.3N 0.5W) and Tokyo (35.7N
400 139.8E) in kilometers:
402 use Math::Trig qw(great_circle_distance deg2rad);
404 # Notice the 90 - latitude: phi zero is at the North Pole.
405 @L = (deg2rad(-0.5), deg2rad(90 - 51.3));
406 @T = (deg2rad(139.8),deg2rad(90 - 35.7));
408 $km = great_circle_distance(@L, @T, 6378);
410 The answer may be off by few percentages because of the irregular
411 (slightly aspherical) form of the Earth.
415 Saying C<use Math::Trig;> exports many mathematical routines in the
416 caller environment and even overrides some (C<sin>, C<cos>). This is
417 construed as a feature by the Authors, actually... ;-)
419 The code is not optimized for speed, especially because we use
420 C<Math::Complex> and thus go quite near complex numbers while doing
421 the computations even when the arguments are not. This, however,
422 cannot be completely avoided if we want things like C<asin(2)> to give
423 an answer instead of giving a fatal runtime error.
427 Jarkko Hietaniemi <F<jhi@iki.fi>> and
428 Raphael Manfredi <F<Raphael_Manfredi@grenoble.hp.com>>.