Example usage for java.lang Double compare

List of usage examples for java.lang Double compare

Introduction

In this page you can find the example usage for java.lang Double compare.

Prototype

public static int compare(double d1, double d2) 

Source Link

Document

Compares the two specified double values.

Usage

From source file:edu.cmu.tetrad.search.Test.java

private double logOfSum(List<Double> logs) {

    Collections.sort(logs, new Comparator<Double>() {
        @Override//from   ww  w. j  ava 2  s.c o  m
        public int compare(Double o1, Double o2) {
            return -Double.compare(o1, o2);
        }
    });

    double sum = 0.0;
    int N = logs.size() - 1;
    double loga0 = logs.get(0);

    for (int i = 1; i <= N; i++) {
        sum += Math.exp(logs.get(i) - loga0);
    }

    sum += 1;

    return loga0 + Math.log(sum);
}

From source file:org.orekit.utils.ElevationMask.java

/** Checking and ordering the azimuth-elevation tabulation.
 * @param azimelev azimuth-elevation tabulation to be checked and ordered
 * @return ordered azimuth-elevation tabulation ordered
 *//*from   ww  w .ja  v a 2  s  .com*/
private static double[][] checkMask(final double[][] azimelev) {

    // Copy of the given mask
    final double[][] mask = new double[azimelev.length + 2][azimelev[0].length];
    for (int i = 0; i < azimelev.length; i++) {
        System.arraycopy(azimelev[i], 0, mask[i + 1], 0, azimelev[i].length);
        // Reducing azimuth between 0 and 2*Pi
        mask[i + 1][0] = MathUtils.normalizeAngle(mask[i + 1][0], FastMath.PI);
    }

    // Sorting the mask with respect to azimuth
    Arrays.sort(mask, 1, mask.length - 1, new Comparator<double[]>() {
        public int compare(final double[] d1, final double[] d2) {
            return Double.compare(d1[0], d2[0]);
        }
    });

    // Extending the mask in order to cover [0, 2PI] in azimuth
    mask[0][0] = mask[mask.length - 2][0] - MathUtils.TWO_PI;
    mask[0][1] = mask[mask.length - 2][1];
    mask[mask.length - 1][0] = mask[1][0] + MathUtils.TWO_PI;
    mask[mask.length - 1][1] = mask[1][1];

    // Checking the sorted mask: same azimuth modulo 2PI must have same elevation
    for (int i = 1; i < mask.length; i++) {
        if (Double.compare(mask[i - 1][0], mask[i][0]) == 0) {
            if (Double.compare(mask[i - 1][1], mask[i][1]) != 0) {
                throw new OrekitIllegalArgumentException(
                        OrekitMessages.UNEXPECTED_TWO_ELEVATION_VALUES_FOR_ONE_AZIMUTH, mask[i - 1][1],
                        mask[i][1], mask[i][0]);
            }
        }
    }

    return mask;
}

From source file:com.anhth12.word2vec.VocabWord.java

@Override
public int compareTo(VocabWord o) {
    return Double.compare(wordFrequency.get(), o.getWordFrequency());
}

From source file:lda.inference.internal.Topics.java

List<Pair<String, Double>> getVocabsSortedByPhi(int topicID, Vocabularies vocabs, final double beta) {
    if (topicID < 0 || topics.size() <= topicID || vocabs == null || beta <= 0.0) {
        throw new IllegalArgumentException();
    }//w  w  w. j av  a2s. com

    Topic topic = topics.get(topicID);
    List<Pair<String, Double>> vocabProbPairs = vocabs.getVocabularyList().stream()
            .map(v -> new ImmutablePair<String, Double>(v.toString(), topic.getPhi(v.id(), beta)))
            .sorted((p1, p2) -> Double.compare(p2.getRight(), p1.getRight())).collect(Collectors.toList());
    return Collections.unmodifiableList(vocabProbPairs);
}

From source file:com.playtech.portal.platform.common.util.Validator.java

/**
 * Returns <code>true</code> if the doubles are equal.
 *
 * @param  double1 the first double//  w  w  w . j  av  a2s . c o m
 * @param  double2 the second double
 * @return <code>true</code> if the doubles are equal; <code>false</code>
 *         otherwise
 */
public static boolean equals(double double1, double double2) {
    if (Double.compare(double1, double2) == 0) {
        return true;
    } else {
        return false;
    }
}

From source file:facebook4j.GeoLocation.java

@Override
public boolean equals(Object o) {
    if (this == o)
        return true;
    if (!(o instanceof GeoLocation))
        return false;

    GeoLocation that = (GeoLocation) o;//from w ww . jav a  2s .c  om

    if (Double.compare(that.getLatitude(), latitude) != 0)
        return false;
    if (Double.compare(that.getLongitude(), longitude) != 0)
        return false;

    return true;
}

From source file:it.units.malelab.sse.MyGeneticAlgorithm.java

@Override
public Population evolve(Population initial, StoppingCondition condition) {
    Population current = initial;/*from w  w w . j av  a2  s.  c  o  m*/
    generationsEvolved = 0;
    while (!condition.isSatisfied(current)) {
        current = nextGeneration(current);
        generationsEvolved++;
        //obtain stats
        List<EnumMap<Evaluator.ResultType, Double>> statsList = new ArrayList<>(current.getPopulationSize());
        Iterator<Chromosome> iterator = current.iterator();
        while (iterator.hasNext()) {
            OperationsChromosome chromosome = (OperationsChromosome) iterator.next();
            EnumMap<Evaluator.ResultType, Double> stats = chromosome.getStats();
            if (stats.containsKey(Evaluator.ResultType.OVERLAPNESS)) {
                statsList.add(stats);
            }
        }
        Collections.sort(statsList, new Comparator<EnumMap<Evaluator.ResultType, Double>>() {
            @Override
            public int compare(EnumMap<Evaluator.ResultType, Double> stats1,
                    EnumMap<Evaluator.ResultType, Double> stats2) {
                return Double.compare(stats1.get(Evaluator.ResultType.OVERLAPNESS),
                        stats2.get(Evaluator.ResultType.OVERLAPNESS));
            }
        });
        EnumMap<Evaluator.ResultType, Double> bestStats = statsList.get(0);
        EnumMap<Evaluator.ResultType, Double> top10Stats = mean(statsList.subList(0, 10));
        EnumMap<Evaluator.ResultType, Double> allStats = mean(statsList);
        System.out.printf("ovp=%5.3f/%5.3f/%5.3f   ", bestStats.get(Evaluator.ResultType.OVERLAPNESS),
                top10Stats.get(Evaluator.ResultType.OVERLAPNESS),
                allStats.get(Evaluator.ResultType.OVERLAPNESS));
        System.out.printf("ops=%4.0f/%4.0f/%4.0f   ", bestStats.get(Evaluator.ResultType.AVG_OPS),
                top10Stats.get(Evaluator.ResultType.AVG_OPS), allStats.get(Evaluator.ResultType.AVG_OPS));
        System.out.printf("mfp=%4.0f/%4.0f/%4.0f   ", bestStats.get(Evaluator.ResultType.AVG_FOOTPRINT),
                top10Stats.get(Evaluator.ResultType.AVG_FOOTPRINT),
                allStats.get(Evaluator.ResultType.AVG_FOOTPRINT));
        System.out.printf("err=%5.3f/%5.3f/%5.3f   ", bestStats.get(Evaluator.ResultType.ERROR_RATIO),
                top10Stats.get(Evaluator.ResultType.ERROR_RATIO),
                allStats.get(Evaluator.ResultType.ERROR_RATIO));
        System.out.printf("size=%3.0f/%3.0f/%3.0f   ", bestStats.get(Evaluator.ResultType.SIZE),
                top10Stats.get(Evaluator.ResultType.SIZE), allStats.get(Evaluator.ResultType.SIZE));
        System.out.printf("evals=%8d\n", evaluator.getEvaluatedCount());
        //System.out.println(evaluator.getErrorCodes());
    }
    return current;
}

From source file:com.mauersu.util.redis.DefaultTypedTuple.java

public int compareTo(Double o) {

    double thisScore = (score == null ? 0.0 : score);
    double otherScore = (o == null ? 0.0 : o);

    return Double.compare(thisScore, otherScore);
}

From source file:io.yields.math.framework.property.SortedProperty.java

private int compareDouble(Object tx, Object ty) {
    double x = (double) tx;
    double y = (double) ty;
    if (Math.abs(x - y) < EPS) {
        return 0;
    } else {//from   ww  w .  ja va  2  s . co  m
        return Double.compare(x, y);
    }
}

From source file:com.joptimizer.solvers.BasicKKTSolver.java

/**
 * Returns the two vectors v and w.//www .  j ava  2s .com
 */
@Override
public double[][] solve() throws Exception {

    RealVector v = null;// dim equals cols of A
    RealVector w = null;// dim equals rank of A

    if (Log.isLoggable(MainActivity.JOPTIMIZER_LOGTAG, Log.DEBUG)) {
        Log.d(MainActivity.JOPTIMIZER_LOGTAG, "H: " + ArrayUtils.toString(H.getData()));
        Log.d(MainActivity.JOPTIMIZER_LOGTAG, "g: " + ArrayUtils.toString(g.toArray()));
    }
    RealMatrix HInv;
    try {
        HInv = Utils.squareMatrixInverse(H);
    } catch (SingularMatrixException e) {
        HInv = null;
    }

    if (HInv != null) {
        // Solving KKT system via elimination
        if (A != null) {
            if (Log.isLoggable(MainActivity.JOPTIMIZER_LOGTAG, Log.DEBUG)) {
                Log.d(MainActivity.JOPTIMIZER_LOGTAG, "A: " + ArrayUtils.toString(A.getData()));
                if (h != null) {
                    Log.d(MainActivity.JOPTIMIZER_LOGTAG, "h: " + ArrayUtils.toString(h.toArray()));
                }
            }
            RealMatrix AHInv = A.multiply(HInv);
            RealMatrix MenoS = AHInv.multiply(AT);
            RealMatrix MenoSInv = Utils.squareMatrixInverse(MenoS);
            if (h == null || Double.compare(h.getNorm(), 0.) == 0) {
                w = MenoSInv.operate(AHInv.operate(g)).mapMultiply(-1.);
            } else {
                w = MenoSInv.operate(h.subtract(AHInv.operate(g)));
            }
            v = HInv.operate(g.add(AT.operate(w)).mapMultiply(-1.));
        } else {
            w = null;
            v = HInv.operate(g).mapMultiply(-1.);
        }
    } else {
        // Solving the full KKT system
        if (A != null) {
            KKTSolver kktSolver = new BasicKKTSolver();
            kktSolver.setCheckKKTSolutionAccuracy(false);
            double[][] fullSol = this.solveFullKKT(new BasicKKTSolver());
            v = new ArrayRealVector(fullSol[0]);
            w = new ArrayRealVector(fullSol[1]);
        } else {
            //@TODO: try with rescaled H
            throw new Exception("KKT solution failed");
        }
    }

    // solution checking
    if (this.checkKKTSolutionAccuracy && !this.checkKKTSolutionAccuracy(v, w)) {
        Log.e(MainActivity.JOPTIMIZER_LOGTAG, "KKT solution failed");
        throw new Exception("KKT solution failed");
    }

    double[][] ret = new double[2][];
    ret[0] = v.toArray();
    ret[1] = (w != null) ? w.toArray() : null;
    return ret;
}