Example usage for org.apache.commons.math.util FastMath abs

List of usage examples for org.apache.commons.math.util FastMath abs

Introduction

In this page you can find the example usage for org.apache.commons.math.util FastMath abs.

Prototype

public static double abs(double x) 

Source Link

Document

Absolute value.

Usage

From source file:com.l2jserver.service.game.world.filter.impl.RangePointFilter.java

@Override
public boolean accept(PositionableObject other) {
    if (other == null)
        return false;
    final double dx = FastMath.abs(point.getX() - other.getPoint().getX());
    final double dy = FastMath.abs(point.getY() - other.getPoint().getY());
    final double dz = FastMath.abs(point.getZ() - other.getPoint().getZ());

    if (dx > range)
        return false;
    if (dy > range)
        return false;
    if (dz > range)
        return false;

    return true;// ww w  .  j a  v a 2  s  .c  o m
}

From source file:com.l2jserver.service.game.world.filter.impl.RangeFilter.java

@Override
public boolean accept(PositionableObject other) {
    if (other == null)
        return false;
    if (other.getPoint() == null)
        return false;

    final double dx = FastMath.abs(object.getPoint().getX() - other.getPoint().getX());
    final double dy = FastMath.abs(object.getPoint().getY() - other.getPoint().getY());
    final double dz = FastMath.abs(object.getPoint().getZ() - other.getPoint().getZ());

    if (dx > range)
        return false;
    if (dy > range)
        return false;
    if (dz > range)
        return false;

    return true;/* ww w.j a  v a 2  s  .c  o  m*/
}

From source file:net.malariagen.gatk.math.SaddlePointExpansion.java

/**
 * A part of the deviance portion of the saddle point approximation.
 * <p>/*from   w w w  .j a  v a 2  s .  com*/
 * References:
 * <ol>
 * <li>Catherine Loader (2000). "Fast and Accurate Computation of Binomial
 * Probabilities.". <a target="_blank"
 * href="http://www.herine.net/stat/papers/dbinom.pdf">
 * http://www.herine.net/stat/papers/dbinom.pdf</a></li>
 * </ol>
 * </p>
 *
 * @param x the x value.
 * @param mu the average.
 * @return a part of the deviance.
 */
static double getDeviancePart(double x, double mu) {
    double ret;
    if (FastMath.abs(x - mu) < 0.1 * (x + mu)) {
        double d = x - mu;
        double v = d / (x + mu);
        double s1 = v * d;
        double s = Double.NaN;
        double ej = 2.0 * x * v;
        v = v * v;
        int j = 1;
        while (s1 != s) {
            s = s1;
            ej *= v;
            s1 = s + ej / ((j * 2) + 1);
            ++j;
        }
        ret = s1;
    } else {
        ret = x * FastMath.log(x / mu) + mu - x;
    }
    return ret;
}

From source file:edu.berkeley.path.bots.core.Coordinate.java

/**
 * Implementation of Thaddeus Vincenty's algorithms to solve the direct and
 * inverse geodetic problems.//from ww  w .java  2  s . c  o m
 * <p/>
 * This implementation may be as good as the PostGIS provided one, the
 * errors seem to be pretty small (10 ^ -3 meters), but a thorough
 * check/analysis has not been done.
 * <p/>
 * For more information, see Vincenty's original publication on the NOAA
 * web-site: <a href="http://www.ngs.noaa.gov/PUBS_LIB/inverse.pdf">
 * http://www.ngs.noaa.gov/PUBS_LIB/inverse.pdf</a>
 * 
 * @param otherCoord
 * @return the distance calculated this way.
 * @throws ClassCastException
 *             if the SRID~s don't match or are null.
 * @throws NullPointerException
 *             if otherCoord is null.
 */
public double distanceVincentyInMeters(Coordinate otherCoord) {
    // SRID's must match and not be null.
    // This can't be null, duh, and we don't check if otherCoord is, which
    // means the NullPointerException will be thrown.
    if (null == this.srid() || null == otherCoord.srid()) {
        throw new ClassCastException("This distance function uses the spheroid distance, but you "
                + "are using Coordinates with null SRID~s (from a PostgreSQL "
                + "point?).  This doesn't really make any sense and you "
                + "probably want to use .distanceCarteasianInSRUnits( other )" + "instead.");
    } else if (!this.srid().equals(otherCoord.srid())) {
        throw new ClassCastException("The SRID of otherCoord does't match this one.");
    }
    // else we are sure they are the same, and not null.

    final double a;
    final double b;
    final double f;
    // Set a, b, and f.
    switch (this.srid()) {
    case 4326:
        a = 6378137.0;
        b = 6356752.3142;
        f = (a - b) / a;
        break;
    default:
        throw new ClassCastException("The given SRID is not entered in this function, you can"
                + " get it by running the DB query defined in a function" + " in the NETCONFIG project called "
                + "util.SpatialDatabaseQueries.getSpheroidStringCached()"
                + ".  This should return a spheroid string like:\n"
                + "\tSPHEROID(\"WGS 84\",6378137,298.257223563)\n"
                + "which is SPHEROID( desc, d1, d2 ), so then\n" + "\ta = d1\n"
                + "\tb = ( d2 * d1 - d1 ) / d2\n" + "\tf = 1 / d2\n"
                + "Then you can add the numbers to the switch statement" + " in the Java.");
    }

    // CHECKSTYLE:OFF

    // Now a, b, and f should be set.

    //
    // All constants below refer to Vincenty's publication (see above).
    //
    // get parameters as radians
    final double PiOver180 = Math.PI / 180.0;
    final double phi1 = PiOver180 * this.lat();
    final double lambda1 = PiOver180 * this.lon();
    final double phi2 = PiOver180 * otherCoord.lat();
    final double lambda2 = PiOver180 * otherCoord.lon();

    // calculations
    final double a2 = a * a;
    final double b2 = b * b;
    final double a2b2b2 = (a2 - b2) / b2;

    final double omega = lambda2 - lambda1;

    final double tanphi1 = FastMath.tan(phi1);
    final double tanU1 = (1.0 - f) * tanphi1;
    final double U1 = FastMath.atan(tanU1);
    final double sinU1 = FastMath.sin(U1);
    final double cosU1 = FastMath.cos(U1);

    final double tanphi2 = FastMath.tan(phi2);
    final double tanU2 = (1.0 - f) * tanphi2;
    final double U2 = FastMath.atan(tanU2);
    final double sinU2 = FastMath.sin(U2);
    final double cosU2 = FastMath.cos(U2);

    final double sinU1sinU2 = sinU1 * sinU2;
    final double cosU1sinU2 = cosU1 * sinU2;
    final double sinU1cosU2 = sinU1 * cosU2;
    final double cosU1cosU2 = cosU1 * cosU2;

    // Below are the re-assignable fields
    // eq. 13
    double lambda = omega;
    // intermediates we'll need to compute 's'
    double A = 0.0;
    double B = 0.0;
    double sigma = 0.0;
    double deltasigma = 0.0;
    double lambda0;

    final int num_iters = 5; // 20
    final double change_threshold = 1e-5; // 0.0000000000001;
    for (int i = 0; i < num_iters; i++) {
        lambda0 = lambda;

        final double sinlambda = FastMath.sin(lambda);
        final double coslambda = FastMath.cos(lambda);

        // eq. 14
        final double sin2sigma = (cosU2 * sinlambda * cosU2 * sinlambda)
                + (cosU1sinU2 - sinU1cosU2 * coslambda) * (cosU1sinU2 - sinU1cosU2 * coslambda);
        final double sinsigma = FastMath.sqrt(sin2sigma);

        // eq. 15
        final double cossigma = sinU1sinU2 + (cosU1cosU2 * coslambda);

        // eq. 16
        sigma = FastMath.atan2(sinsigma, cossigma);

        // eq. 17 Careful! sin2sigma might be almost 0!
        final double sinalpha = (sin2sigma == 0) ? 0.0 : cosU1cosU2 * sinlambda / sinsigma;
        final double alpha = FastMath.asin(sinalpha);
        final double cosalpha = FastMath.cos(alpha);
        final double cos2alpha = cosalpha * cosalpha;

        // eq. 18 Careful! cos2alpha might be almost 0!
        final double cos2sigmam = cos2alpha == 0.0 ? 0.0 : cossigma - 2 * sinU1sinU2 / cos2alpha;
        final double u2 = cos2alpha * a2b2b2;

        final double cos2sigmam2 = cos2sigmam * cos2sigmam;

        // eq. 3
        A = 1.0 + u2 / 16384 * (4096 + u2 * (-768 + u2 * (320 - 175 * u2)));

        // eq. 4
        B = u2 / 1024 * (256 + u2 * (-128 + u2 * (74 - 47 * u2)));

        // eq. 6
        deltasigma = B * sinsigma * (cos2sigmam + B / 4 * (cossigma * (-1 + 2 * cos2sigmam2)
                - B / 6 * cos2sigmam * (-3 + 4 * sin2sigma) * (-3 + 4 * cos2sigmam2)));

        // eq. 10
        final double C = f / 16 * cos2alpha * (4 + f * (4 - 3 * cos2alpha));

        // eq. 11 (modified)
        lambda = omega + (1 - C) * f * sinalpha
                * (sigma + C * sinsigma * (cos2sigmam + C * cossigma * (-1 + 2 * cos2sigmam2)));

        // see how much improvement we got
        final double change = FastMath.abs((lambda - lambda0) / lambda);

        if ((i > 1) && (change < change_threshold)) {
            break;
        }
    } // for

    // eq. 19
    return b * A * (sigma - deltasigma);
    // CHECKSTYLE:ON
}

From source file:Align_Projections.java

public boolean converged(final int iteration, final RealPointValuePair previous,
        final RealPointValuePair current) {
    if (apObject.getStopTuning()) {
        return true;
    }/* ww w.  jav a  2 s.  c  o m*/
    final double p = previous.getValue();
    final double c = current.getValue();
    final double difference = FastMath.abs(p - c);
    final double size = FastMath.max(FastMath.abs(p), FastMath.abs(c));
    return (difference <= (size * relativeThreshold)) || (difference <= absoluteThreshold);
}

From source file:org.onebusaway.nyc.vehicle_tracking.impl.inference.likelihood.NullStateLikelihood.java

public static NullStates getNullState(Context context) {
    final VehicleState state = context.getState();
    // final Observation obs = context.getObservation();
    final BlockStateObservation blockStateObs = state.getBlockStateObservation();
    EVehiclePhase phase = state.getJourneyState().getPhase();

    if (blockStateObs == null) {
        return NullStates.NULL_STATE;
    } else {//  www.ja  v  a  2s . co m

        final boolean hasScheduledTime = FastMath
                .abs(state.getBlockStateObservation().getScheduleDeviation()) > 0d;

        if (!hasScheduledTime) {
            return NullStates.NULL_STATE;
        }

        if (!state.getBlockStateObservation().isSnapped() && ((EVehiclePhase.DEADHEAD_AFTER == phase
                && state.getBlockStateObservation().getScheduleDeviation() == 0d)
                || EVehiclePhase.AT_BASE == phase
                || (EVehiclePhase.DEADHEAD_BEFORE == phase
                        && state.getBlockStateObservation().getScheduleDeviation() == 0d)
                || (EVehiclePhase.LAYOVER_BEFORE == phase)
                        && state.getBlockStateObservation().getScheduleDeviation() == 0d)) {
            return NullStates.NULL_STATE;
        }

        return NullStates.NON_NULL_STATE;
    }
}

From source file:org.onebusaway.nyc.vehicle_tracking.impl.inference.likelihood.ScheduleLikelihood.java

private SensorModelResult computeSchedTimeProb(VehicleState parentState, VehicleState state,
        BlockState blockState, EVehiclePhase phase, Observation obs)
        throws BadProbabilityParticleFilterException {
    final SensorModelResult result = new SensorModelResult("pSchedule", 1.0);
    if (blockState == null) {
        result.addLogResultAsAnd("null state", 0.0);

    } else {//from w ww.j a  v  a 2s.com
        final StudentTDistribution schedDist = getSchedDistForBlockState(state.getBlockStateObservation());
        final double x = state.getBlockStateObservation().getScheduleDeviation();

        if (EVehiclePhase.DEADHEAD_AFTER == phase) {

            if (FastMath.abs(x) > 0d) {
                final double pSched = getScheduleDevLogProb(x, schedDist);
                result.addLogResultAsAnd("deadhead after", pSched);
            } else {
                result.addLogResultAsAnd("deadhead after", 0.0);
            }

        } else if (EVehiclePhase.DEADHEAD_BEFORE == phase) {

            if (FastMath.abs(x) > 0d) {
                final double pSched = getScheduleDevLogProb(x, schedDist);
                result.addLogResultAsAnd("deadhead before", pSched);
            } else {
                result.addLogResultAsAnd("deadhead before", 0.0);
            }

        } else if (EVehiclePhase.LAYOVER_BEFORE == phase) {

            if (FastMath.abs(x) > 0d) {
                final double pSched = getScheduleDevLogProb(x, schedDist);
                result.addLogResultAsAnd("layover before", pSched);
            } else {
                result.addLogResultAsAnd("layover before", 0.0);
            }

        } else if (EVehiclePhase.DEADHEAD_DURING == phase) {

            final double pSched = getScheduleDevLogProb(x, schedDist);
            result.addLogResultAsAnd("deadhead during", pSched);

        } else if (EVehiclePhase.LAYOVER_DURING == phase) {

            final double pSched = getScheduleDevLogProb(x, schedDist);
            result.addLogResultAsAnd("layover during", pSched);

        } else if (EVehiclePhase.IN_PROGRESS == phase) {
            final double pSched = getScheduleDevLogProb(x, schedDist);
            result.addLogResultAsAnd("in progress", pSched);
        }
    }

    return result;
}

From source file:org.onebusaway.nyc.vehicle_tracking.impl.inference.MotionModelImpl.java

private Map.Entry<BlockSampleType, BlockStateObservation> sampleEdgeFromProposal(
        BlockStateObservation parentBlockStateObs, BlockStateObservation proposalEdge, Observation obs,
        EVehiclePhase parentPhase, boolean vehicleNotMoved) {
    Map.Entry<BlockSampleType, BlockStateObservation> newEdge;
    if (proposalEdge != null) {
        if (parentBlockStateObs != null) {
            /*//from   www .  ja va2 s  .c om
             * When a state is transitioning we need to consider whether the
             * proposal edge is snapped or not, since that leads to different
             * sampling procedures. Also, we propagate non-snapped states
             * differently.
             */
            final double currentScheduleDev = BlockStateObservation.computeScheduleDeviation(obs,
                    parentBlockStateObs.getBlockState());
            final boolean runChanged = hasRunChanged(parentBlockStateObs, proposalEdge);

            if (proposalEdge.isSnapped()) {
                newEdge = Maps.immutableEntry(BlockSampleType.NOT_SAMPLED, proposalEdge);
            } else if (runChanged) {
                newEdge = Maps.immutableEntry(BlockSampleType.SCHEDULE_STATE_SAMPLE, _blockStateSamplingStrategy
                        .samplePriorScheduleState(proposalEdge.getBlockState().getBlockInstance(), obs));
            } else {
                /*
                 * When our previous journey state was deadhead-before we don't want
                 * to "jump the gun" and expect it to be at the start of the first
                 * trip and moving along, so we wait for a snapped location to appear.
                 */
                if (EVehiclePhase.AT_BASE == parentPhase) {
                    newEdge = Maps.immutableEntry(BlockSampleType.NOT_SAMPLED, parentBlockStateObs);
                } else if (EVehiclePhase.isActiveDuringBlock(parentPhase)) {
                    newEdge = Maps.immutableEntry(BlockSampleType.EDGE_MOVEMENT_SAMPLE,
                            _blockStateSamplingStrategy.sampleTransitionDistanceState(parentBlockStateObs, obs,
                                    vehicleNotMoved, parentPhase));
                } else if (EVehiclePhase.isActiveBeforeBlock(parentPhase)
                        && FastMath.abs(currentScheduleDev) > 0.0) {
                    /*
                     * Continue sampling mid-trip joins for this block...
                     */
                    newEdge = Maps.immutableEntry(BlockSampleType.SCHEDULE_STATE_SAMPLE,
                            _blockStateSamplingStrategy.samplePriorScheduleState(
                                    parentBlockStateObs.getBlockState().getBlockInstance(), obs));
                } else {
                    newEdge = Maps.immutableEntry(BlockSampleType.NOT_SAMPLED, parentBlockStateObs);
                }
            }
        } else {
            if (proposalEdge.isSnapped()) {
                newEdge = Maps.immutableEntry(BlockSampleType.NOT_SAMPLED, proposalEdge);
            } else {
                newEdge = Maps.immutableEntry(BlockSampleType.SCHEDULE_STATE_SAMPLE, _blockStateSamplingStrategy
                        .samplePriorScheduleState(proposalEdge.getBlockState().getBlockInstance(), obs));
            }
        }
    } else {
        newEdge = Maps.immutableEntry(BlockSampleType.NOT_SAMPLED, null);
    }

    return newEdge;
}