GeoMath.java
Go to the documentation of this file.
1 
8 package net.sf.geographiclib;
9 
16 public class GeoMath {
21  public static final int digits = 53;
26  public static final double epsilon = Math.pow(0.5, digits - 1);
31  public static final double min = Math.pow(0.5, 1022);
32 
39  public static double sq(double x) { return x * x; }
40 
49  public static double hypot(double x, double y) {
50  x = Math.abs(x); y = Math.abs(y);
51  double a = Math.max(x, y), b = Math.min(x, y) / (a != 0 ? a : 1);
52  return a * Math.sqrt(1 + b * b);
53  // For an alternative method see
54  // C. Moler and D. Morrision (1983) https://doi.org/10.1147/rd.276.0577
55  // and A. A. Dubrulle (1983) https://doi.org/10.1147/rd.276.0582
56  }
57 
71  public static double log1p(double x) {
72  double
73  y = 1 + x,
74  z = y - 1;
75  // Here's the explanation for this magic: y = 1 + z, exactly, and z
76  // approx x, thus log(y)/z (which is nearly constant near z = 0) returns
77  // a good approximation to the true log(1 + x)/x. The multiplication x *
78  // (log(y)/z) introduces little additional error.
79  return z == 0 ? x : x * Math.log(y) / z;
80  }
81 
90  public static double atanh(double x) {
91  double y = Math.abs(x); // Enforce odd parity
92  y = Math.log1p(2 * y/(1 - y))/2;
93  return x < 0 ? -y : y;
94  }
95 
104  public static double copysign(double x, double y) {
105  return Math.abs(x) * (y < 0 || (y == 0 && 1/y < 0) ? -1 : 1);
106  }
107 
115  public static double cbrt(double x) {
116  double y = Math.pow(Math.abs(x), 1/3.0); // Return the real cube root
117  return x < 0 ? -y : y;
118  }
119 
120  public static Pair norm(double sinx, double cosx) {
121  double r = hypot(sinx, cosx);
122  return new Pair(sinx/r, cosx/r);
123  }
124 
135  public static Pair sum(double u, double v) {
136  double s = u + v;
137  double up = s - v;
138  double vpp = s - up;
139  up -= u;
140  vpp -= v;
141  double t = -(up + vpp);
142  // u + v = s + t
143  // = round(u + v) + t
144  return new Pair(s, t);
145  }
146 
162  public static double polyval(int N, double p[], int s, double x) {
163  double y = N < 0 ? 0 : p[s++];
164  while (--N >= 0) y = y * x + p[s++];
165  return y;
166  }
167 
168  public static double AngRound(double x) {
169  // The makes the smallest gap in x = 1/16 - nextafter(1/16, 0) = 1/2^57
170  // for reals = 0.7 pm on the earth if x is an angle in degrees. (This
171  // is about 1000 times more resolution than we get with angles around 90
172  // degrees.) We use this to avoid having to deal with near singular
173  // cases when x is non-zero but tiny (e.g., 1.0e-200). This converts -0 to
174  // +0; however tiny negative numbers get converted to -0.
175  final double z = 1/16.0;
176  if (x == 0) return 0;
177  double y = Math.abs(x);
178  // The compiler mustn't "simplify" z - (z - y) to y
179  y = y < z ? z - (z - y) : y;
180  return x < 0 ? -y : y;
181  }
182 
191  public static double AngNormalize(double x) {
192  x = x % 360.0;
193  return x <= -180 ? x + 360 : (x <= 180 ? x : x - 360);
194  }
195 
203  public static double LatFix(double x) {
204  return Math.abs(x) > 90 ? Double.NaN : x;
205  }
206 
221  public static Pair AngDiff(double x, double y) {
222  double d, t;
223  {
224  Pair r = sum(AngNormalize(-x), AngNormalize(y));
225  d = AngNormalize(r.first); t = r.second;
226  }
227  return sum(d == 180 && t > 0 ? -180 : d, t);
228  }
229 
240  public static Pair sincosd(double x) {
241  // In order to minimize round-off errors, this function exactly reduces
242  // the argument to the range [-45, 45] before converting it to radians.
243  double r; int q;
244  r = x % 360.0;
245  q = (int)Math.floor(r / 90 + 0.5);
246  r -= 90 * q;
247  // now abs(r) <= 45
248  r = Math.toRadians(r);
249  // Possibly could call the gnu extension sincos
250  double s = Math.sin(r), c = Math.cos(r);
251  double sinx, cosx;
252  switch (q & 3) {
253  case 0: sinx = s; cosx = c; break;
254  case 1: sinx = c; cosx = -s; break;
255  case 2: sinx = -s; cosx = -c; break;
256  default: sinx = -c; cosx = s; break; // case 3
257  }
258  if (x != 0) { sinx += 0.0; cosx += 0.0; }
259  return new Pair(sinx, cosx);
260  }
261 
274  public static double atan2d(double y, double x) {
275  // In order to minimize round-off errors, this function rearranges the
276  // arguments so that result of atan2 is in the range [-pi/4, pi/4] before
277  // converting it to degrees and mapping the result to the correct
278  // quadrant.
279  int q = 0;
280  if (Math.abs(y) > Math.abs(x)) { double t; t = x; x = y; y = t; q = 2; }
281  if (x < 0) { x = -x; ++q; }
282  // here x >= 0 and x >= abs(y), so angle is in [-pi/4, pi/4]
283  double ang = Math.toDegrees(Math.atan2(y, x));
284  switch (q) {
285  // Note that atan2d(-0.0, 1.0) will return -0. However, we expect that
286  // atan2d will not be called with y = -0. If need be, include
287  //
288  // case 0: ang = 0 + ang; break;
289  //
290  // and handle mpfr as in AngRound.
291  case 1: ang = (y >= 0 ? 180 : -180) - ang; break;
292  case 2: ang = 90 - ang; break;
293  case 3: ang = -90 + ang; break;
294  }
295  return ang;
296  }
297 
304  public static boolean isfinite(double x) {
305  return Math.abs(x) <= Double.MAX_VALUE;
306  }
307 
308  private GeoMath() {}
309 }
gtsam.examples.DogLegOptimizerExample.int
int
Definition: DogLegOptimizerExample.py:111
net.sf.geographiclib.GeoMath.AngRound
static double AngRound(double x)
Definition: GeoMath.java:168
net.sf.geographiclib.Pair.second
double second
Definition: Pair.java:23
s
RealScalar s
Definition: level1_cplx_impl.h:126
d
static const double d[K][N]
Definition: igam.h:11
net.sf.geographiclib.GeoMath.LatFix
static double LatFix(double x)
Definition: GeoMath.java:203
c
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
b
Scalar * b
Definition: benchVecAdd.cpp:17
net.sf.geographiclib.GeoMath.sum
static Pair sum(double u, double v)
Definition: GeoMath.java:135
x
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
Definition: gnuplot_common_settings.hh:12
net.sf.geographiclib.GeoMath.digits
static final int digits
Definition: GeoMath.java:21
net.sf.geographiclib.GeoMath.atanh
static double atanh(double x)
Definition: GeoMath.java:90
net.sf.geographiclib.GeoMath.isfinite
static boolean isfinite(double x)
Definition: GeoMath.java:304
net.sf.geographiclib.GeoMath.AngDiff
static Pair AngDiff(double x, double y)
Definition: GeoMath.java:221
net.sf.geographiclib.GeoMath.sincosd
static Pair sincosd(double x)
Definition: GeoMath.java:240
net.sf.geographiclib.GeoMath.hypot
static double hypot(double x, double y)
Definition: GeoMath.java:49
net.sf.geographiclib.GeoMath.AngNormalize
static double AngNormalize(double x)
Definition: GeoMath.java:191
net.sf.geographiclib.GeoMath.atan2d
static double atan2d(double y, double x)
Definition: GeoMath.java:274
Eigen::numext::q
EIGEN_DEVICE_FUNC const Scalar & q
Definition: SpecialFunctionsImpl.h:1984
net.sf.geographiclib.GeoMath.polyval
static double polyval(int N, double p[], int s, double x)
Definition: GeoMath.java:162
net.sf.geographiclib.GeoMath.epsilon
static final double epsilon
Definition: GeoMath.java:26
net.sf.geographiclib.GeoMath.copysign
static double copysign(double x, double y)
Definition: GeoMath.java:104
net.sf.geographiclib.Pair.first
double first
Definition: Pair.java:19
net.sf.geographiclib.GeoMath.log1p
static double log1p(double x)
Definition: GeoMath.java:71
pybind_wrapper_test_script.z
z
Definition: pybind_wrapper_test_script.py:61
net.sf.geographiclib.Pair
Definition: Pair.java:15
net.sf.geographiclib.GeoMath.GeoMath
GeoMath()
Definition: GeoMath.java:308
net.sf.geographiclib.GeoMath.norm
static Pair norm(double sinx, double cosx)
Definition: GeoMath.java:120
y
Scalar * y
Definition: level1_cplx_impl.h:124
a
ArrayXXi a
Definition: Array_initializer_list_23_cxx11.cpp:1
net.sf.geographiclib.GeoMath
Definition: GeoMath.java:16
p
float * p
Definition: Tutorial_Map_using.cpp:9
net.sf.geographiclib.GeoMath.sq
static double sq(double x)
Definition: GeoMath.java:39
v
Array< int, Dynamic, 1 > v
Definition: Array_initializer_list_vector_cxx11.cpp:1
net.sf.geographiclib.GeoMath.cbrt
static double cbrt(double x)
Definition: GeoMath.java:115
N
#define N
Definition: igam.h:9
align_3::t
Point2 t(10, 10)
net.sf.geographiclib.GeoMath.min
static final double min
Definition: GeoMath.java:31


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:02:22