public static final double distance(double lat1, double lon1, double lat2, double lon2, double radius) { // http://en.wikipedia.org/wiki/Great-circle_distance lat1 = toRadians(lat1); // Theta-s lon1 = toRadians(lon1); // Lambda-s lat2 = toRadians(lat2); // Theta-f lon2 = toRadians(lon2); // Lambda-f double deltaLon = lon2 - lon1; double y = sqrt(p2(cos(lat2) * sin(deltaLon)) + p2(cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(deltaLon))); double x = sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(deltaLon); return radius * atan2(y, x); }
/** {@inheritDoc} */ public double value(double x, double y) { return FastMath.pow(x, y); } }
/** * {@inheritDoc} */ public double density(double x) { if (means != null) throw new IllegalStateException("Unable to sample from more than one mean"); final double x0 = x - mean; final double x1 = x0 / standardDeviation; return FastMath.exp(-0.5 * x1 * x1) / (standardDeviation * SQRT2PI); }
/** * Returns the index of the bin to which the given value belongs * * @param value the value whose bin we are trying to find * @return the index of the bin containing the value */ private int findBin(double value) { return FastMath.min( FastMath.max((int) FastMath.ceil((value - min) / delta) - 1, 0), binCount - 1); }
/** * Approximated, fast and under-estimated equirectangular distance between two points. * Works only for small delta lat/lon, fall-back on exact distance if not the case. * See: http://www.movable-type.co.uk/scripts/latlong.html */ public static final double fastDistance(double lat1, double lon1, double lat2, double lon2, double radius) { if (abs(lat1 - lat2) > MAX_LAT_DELTA_DEG || abs(lon1 - lon2) > MAX_LON_DELTA_DEG) { return distance(lat1, lon1, lat2, lon2, radius); } double dLat = toRadians(lat2 - lat1); double dLon = toRadians(lon2 - lon1) * cos(toRadians((lat1 + lat2) / 2)); return radius * sqrt(dLat * dLat + dLon * dLon) * MAX_ERR_INV; }
/** * @param i which eigenvalue to find the norm of * @return the norm of ith (complex) eigenvalue. */ private double eigenvalueNorm(int i) { final double re = realEigenvalues[i]; final double im = imagEigenvalues[i]; return FastMath.sqrt(re * re + im * im); }
/** Get a circle point from its phase around the circle. * @param alpha phase around the circle * @return circle point on the sphere * @see #toSpace(Point) * @see #getXAxis() * @see #getYAxis() */ public Vector3D getPointAt(final double alpha) { return new Vector3D(FastMath.cos(alpha), x, FastMath.sin(alpha), y); }
/** Compute step grow/shrink factor according to normalized error. * @param error normalized error of the current step * @return grow/shrink factor for next step */ protected double computeStepGrowShrinkFactor(final double error) { return FastMath.min(maxGrowth, FastMath.max(minReduction, safety * FastMath.pow(error, exp))); }
/** Get the angle of the rotation. * @return angle of the rotation (between 0 and π) * @see #Rotation(Vector3D, double) */ public double getAngle() { if ((q0 < -0.1) || (q0 > 0.1)) { return 2 * FastMath.asin(FastMath.sqrt(q1 * q1 + q2 * q2 + q3 * q3)); } else if (q0 < 0) { return 2 * FastMath.acos(-q0); } return 2 * FastMath.acos(q0); }
@Override public IComplexNumber log() { IComplexNumber result = dup(); double real = (double) result.realComponent(); double imaginary = (double) result.imaginaryComponent(); double modulus = FastMath.sqrt(real * real + imaginary * imaginary); double arg = FastMath.atan2(imaginary, real); return result.set(FastMath.log(modulus), arg); }
static double bearing(Coordinate c1, Coordinate c2) { double deltaLon = (c2.x - c1.x) * FastMath.PI / 180; double lat1Radians = c1.y * FastMath.PI / 180; double lat2Radians = c2.y * FastMath.PI / 180; double y = FastMath.sin(deltaLon) * FastMath.cos(lat2Radians); double x = FastMath.cos(lat1Radians)*FastMath.sin(lat2Radians) - FastMath.sin(lat1Radians)*FastMath.cos(lat2Radians)*FastMath.cos(deltaLon); return FastMath.atan2(y, x); }
/** {@inheritDoc} */ @Override public double logDensity(double x) { final double n = degreesOfFreedom; final double nPlus1Over2 = (n + 1) / 2; return factor - nPlus1Over2 * FastMath.log(1 + x * x / n); }
/** {@inheritDoc} */ @Override public double getL1Norm() { double sum = 0; for (double a : data) { sum += FastMath.abs(a); } return sum; }
/** {@inheritDoc} */ public double value(final double x) { final double scaledX = normalized ? FastMath.PI * x : x; if (FastMath.abs(scaledX) <= SHORTCUT) { // use Taylor series final double scaledX2 = scaledX * scaledX; return ((scaledX2 - 20) * scaledX2 + 120) / 120; } else { // use definition expression return FastMath.sin(scaledX) / scaledX; } }