All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.geographiclib.GeoMath Maven / Gradle / Ivy

Go to download

This is a Java implementation of the geodesic algorithms from GeographicLib. This is a self-contained library which makes it easy to do geodesic computations for an ellipsoid of revolution in a Java program. It requires Java version 1.1 or later.

The newest version!
/**
 * Implementation of the net.sf.geographiclib.GeoMath class
 *
 * Copyright (c) Charles Karney (2013-2020)  and licensed
 * under the MIT/X11 License.  For more information, see
 * https://geographiclib.sourceforge.io/
 **********************************************************************/
package net.sf.geographiclib;

/**
 * Mathematical functions needed by GeographicLib.
 * 

* Define mathematical functions and constants so that any version of Java * can be used. **********************************************************************/ public class GeoMath { /** * The number of binary digits in the fraction of a double precision * number (equivalent to C++'s {@code numeric_limits::digits}). **********************************************************************/ public static final int digits = 53; /** * Square a number. *

* @param x the argument. * @return x2. **********************************************************************/ public static double sq(double x) { return x * x; } /** * The inverse hyperbolic tangent function. This is defined in terms of * Math.log1p(x) in order to maintain accuracy near x = 0. * In addition, the odd parity of the function is enforced. *

* @param x the argument. * @return atanh(x). **********************************************************************/ public static double atanh(double x) { double y = Math.abs(x); // Enforce odd parity y = Math.log1p(2 * y/(1 - y))/2; return x > 0 ? y : (x < 0 ? -y : x); } /** * Normalize a sine cosine pair. *

* @param p return parameter for normalized quantities with sinx2 * + cosx2 = 1. * @param sinx the sine. * @param cosx the cosine. **********************************************************************/ public static void norm(Pair p, double sinx, double cosx) { double r = Math.hypot(sinx, cosx); p.first = sinx/r; p.second = cosx/r; } /** * The error-free sum of two numbers. *

* @param u the first number in the sum. * @param v the second number in the sum. * @param p output Pair(s, t) with s = round(u + * v) and t = u + v - s. *

* See D. E. Knuth, TAOCP, Vol 2, 4.2.2, Theorem B. **********************************************************************/ public static void sum(Pair p, double u, double v) { double s = u + v; double up = s - v; double vpp = s - up; up -= u; vpp -= v; double t = s != 0 ? 0.0 - (up + vpp) : s; // u + v = s + t // = round(u + v) + t p.first = s; p.second = t; } /** * Evaluate a polynomial. *

* @param N the order of the polynomial. * @param p the coefficient array (of size N + s + 1 or more). * @param s starting index for the array. * @param x the variable. * @return the value of the polynomial. *

* Evaluate y = ∑n=0..N * ps+n * xNn. Return 0 if N < 0. * Return ps, if N = 0 (even if x is * infinite or a nan). The evaluation uses Horner's method. **********************************************************************/ public static double polyval(int N, double p[], int s, double x) { double y = N < 0 ? 0 : p[s++]; while (--N >= 0) y = y * x + p[s++]; return y; } /** * Coarsen a value close to zero. *

* @param x the argument * @return the coarsened value. *

* This makes the smallest gap in x = 1/16 − nextafter(1/16, 0) * = 1/257 for reals = 0.7 pm on the earth if x is an angle * in degrees. (This is about 1000 times more resolution than we get with * angles around 90 degrees.) We use this to avoid having to deal with near * singular cases when x is non-zero but tiny (e.g., * 10−200). This converts −0 to +0; however tiny * negative numbers get converted to −0. **********************************************************************/ public static double AngRound(double x) { final double z = 1/16.0; double y = Math.abs(x); // The compiler mustn't "simplify" z - (z - y) to y y = y < z ? z - (z - y) : y; return Math.copySign(y, x); } /** * Normalize an angle. *

* @param x the angle in degrees. * @return the angle reduced to the range [−180°, 180°). *

* The range of x is unrestricted. **********************************************************************/ public static double AngNormalize(double x) { double y = Math.IEEEremainder(x, 360.0); return Math.abs(y) == 180 ? Math.copySign(180.0, x) : y; } /** * Normalize a latitude. *

* @param x the angle in degrees. * @return x if it is in the range [−90°, 90°], otherwise * return NaN. **********************************************************************/ public static double LatFix(double x) { return Math.abs(x) > 90 ? Double.NaN : x; } /** * The exact difference of two angles reduced to [−180°, 180°]. *

* @param x the first angle in degrees. * @param y the second angle in degrees. * @param p output Pair(d, e) with d being the rounded * difference and e being the error. *

* This computes z = yx exactly, reduced to * [−180°, 180°]; and then sets z = d + e * where d is the nearest representable number to z and * e is the truncation error. If z = ±0° or * ±180°, then the sign of d is given by the sign of * yx. The maximum absolute value of e is * 2−26 (for doubles). **********************************************************************/ public static void AngDiff(Pair p, double x, double y) { sum(p, Math.IEEEremainder(-x, 360.0), Math.IEEEremainder(y, 360.0)); sum(p, Math.IEEEremainder(p.first, 360.0), p.second); if (p.first == 0 || Math.abs(p.first) == 180) // p = [d, e]... // If e == 0, take sign from y - x // else (e != 0, implies d = +/-180), d and e must have opposite signs p.first = Math.copySign(p.first, p.second == 0 ? y - x : -p.second); } /** * Evaluate the sine and cosine function with the argument in degrees * * @param p return Pair(s, t) with s = sin(x) and * c = cos(x). * @param x in degrees. *

* The results obey exactly the elementary properties of the trigonometric * functions, e.g., sin 9° = cos 81° = − sin 123456789°. **********************************************************************/ public static void sincosd(Pair p, double x) { // In order to minimize round-off errors, this function exactly reduces // the argument to the range [-45, 45] before converting it to radians. double r; int q; r = x % 360.0; q = (int)Math.round(r / 90); // If r is NaN this returns 0 r -= 90 * q; // now abs(r) <= 45 r = Math.toRadians(r); // Possibly could call the gnu extension sincos double s = Math.sin(r), c = Math.cos(r); double sinx, cosx; switch (q & 3) { case 0: sinx = s; cosx = c; break; case 1: sinx = c; cosx = -s; break; case 2: sinx = -s; cosx = -c; break; default: sinx = -c; cosx = s; break; // case 3 } if (sinx == 0) sinx = Math.copySign(sinx, x); p.first = sinx; p.second = 0.0 + cosx; } /** * Evaluate the sine and cosine function with reduced argument plus correction * * @param p return Pair(s, t) with s = * sin(x + t) and c = cos(x + t). * @param x reduced angle in degrees. * @param t correction in degrees. *

* This is a variant of GeoMath.sincosd allowing a correction to the angle to * be supplied. x x must be in [−180°, 180°] and * t is assumed to be a small correction. GeoMath.AngRound is * applied to the reduced angle to prevent problems with x + t * being extremely close but not exactly equal to one of the four cardinal * directions. **********************************************************************/ public static void sincosde(Pair p, double x, double t) { // In order to minimize round-off errors, this function exactly reduces // the argument to the range [-45, 45] before converting it to radians. double r; int q; q = (int)Math.round(x / 90); // If r is NaN this returns 0 r = x - 90 * q; // now abs(r) <= 45 r = Math.toRadians(GeoMath.AngRound(r + t)); // Possibly could call the gnu extension sincos double s = Math.sin(r), c = Math.cos(r); double sinx, cosx; switch (q & 3) { case 0: sinx = s; cosx = c; break; case 1: sinx = c; cosx = -s; break; case 2: sinx = -s; cosx = -c; break; default: sinx = -c; cosx = s; break; // case 3 } if (sinx == 0) sinx = Math.copySign(sinx, x); p.first = sinx; p.second = 0.0 + cosx; } /** * Evaluate the atan2 function with the result in degrees * * @param y the sine of the angle * @param x the cosine of the angle * @return atan2(y, x) in degrees. *

* The result is in the range [−180° 180°]. N.B., * atan2d(±0, −1) = ±180°. **********************************************************************/ public static double atan2d(double y, double x) { // In order to minimize round-off errors, this function rearranges the // arguments so that result of atan2 is in the range [-pi/4, pi/4] before // converting it to degrees and mapping the result to the correct // quadrant. int q = 0; if (Math.abs(y) > Math.abs(x)) { double t; t = x; x = y; y = t; q = 2; } if (x < 0) { x = -x; ++q; } // here x >= 0 and x >= abs(y), so angle is in [-pi/4, pi/4] double ang = Math.toDegrees(Math.atan2(y, x)); switch (q) { // Note that atan2d(-0.0, 1.0) will return -0. However, we expect that // atan2d will not be called with y = -0. If need be, include // // case 0: ang = 0 + ang; break; // // and handle mpfr as in AngRound. case 1: ang = Math.copySign(180.0, y) - ang; break; case 2: ang = 90 - ang; break; case 3: ang = -90 + ang; break; default: break; } return ang; } private GeoMath() {} }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy