Example usage for org.apache.commons.math3.util Precision equals

List of usage examples for org.apache.commons.math3.util Precision equals

Introduction

In this page you can find the example usage for org.apache.commons.math3.util Precision equals.

Prototype

public static boolean equals(double x, double y) 

Source Link

Document

Returns true iff they are equal as defined by #equals(double,double,int) equals(x, y, 1) .

Usage

From source file:jsat.distributions.SingleValueDistribution.java

@Override
public double pdf(double x) {
    if (Precision.equals(x, value)) {
        return 1;
    } else {//from  w  w  w . j  ava  2  s .c o m
        return 0;
    }
}

From source file:com.inform.jamps.solver.gurobi.GurobiExpression.java

@Override
public Expression addTerm(final double coefficient, final Variable variable) {
    if (Precision.equals(coefficient, ZERO_COEFFICIENT)) {
        return this;
    }//from w ww  . j  ava  2s .c om

    if (!(variable instanceof GurobiVariable)) {
        throw new IllegalArgumentException(
                "Adding variable " + variable.getName() + " of type not equal GurobiVariable is not supported");
    }

    final GurobiProgram varProgram = ((GurobiVariable) variable).getProgram();
    final GurobiProgram ownProgram = (objective == null) ? constraint.getProgram() : objective.getProgram();
    if (varProgram == null || !varProgram.equals(ownProgram)) {
        throw new IllegalArgumentException(
                "Adding variable " + variable.getName() + " from a different program is not supported");
    }

    final GurobiLinearTerm term = linearTerms.get(variable);

    if (term == null) {
        final GurobiLinearTerm newTerm = new GurobiLinearTerm(coefficient, (GurobiVariable) variable);
        linearTerms.put((GurobiVariable) variable, newTerm);
    } else {
        term.addCoefficient(coefficient);
    }

    return this;
}

From source file:jsat.distributions.empirical.KernelDensityEstimatorButla.java

public KernelDensityEstimatorButla(Vec dataPoints, KDEFormelVariant formelVariant, double bandwidth,
        double minSearchStep, double minSearchAccuracy) {

    // TODO check

    this.dataPoints = dataPoints.sortedCopy();
    this.X = dataPoints.arrayCopy();
    this.minSearchStep = minSearchStep;
    this.minSearchAccuracy = minSearchAccuracy;
    this.kdeFormelVariant = formelVariant;

    if (Precision.equals(bandwidth, 0)) {
        bandwidth = MyKernelDensityEstimator.BandwithGuassEstimate(dataPoints);
        this.minSearchStep = bandwidth / 4.0;
    }/*from   w  ww.j  a v  a2s  .com*/

    if (this.minSearchStep < 0.0001) {
        this.minSearchStep = 0.25d;
    }

    if (formelVariant == KDEFormelVariant.OriginalKDE) {

        final MyKernelDensityEstimator kernelDensity = new MyKernelDensityEstimator(dataPoints,
                GaussKF.getInstance(), bandwidth);
        kernelPdfFunction = ContinuousDistribution.getFunctionPDF(kernelDensity);
        kernelDerivationFunction = ContinuousDistribution.getFunctionPDF(
                new MyKernelDensityEstimator(dataPoints, GaussKFDerivation.getInstance(), bandwidth));

        startX = kernelDensity.min() + bandwidth;
        endX = kernelDensity.max() - bandwidth;

    } else if (formelVariant == KDEFormelVariant.OriginalButlaVariableBandwidth) {

        kernelPdfFunction = new Function() {

            private static final long serialVersionUID = 337703545623146489L;

            @Override
            public double f(Vec x) {
                return f(new double[] { x.get(0) });
            }

            @Override
            public double f(double... x) {

                final double t = x[0];
                double sum = 0.0d;

                final double maxH = Math.pow(X[X.length - 1] * 0.05, 2);
                int from = Arrays.binarySearch(X, t - maxH * 13);
                int to = Arrays.binarySearch(X, t + maxH * 13);
                from = from < 0 ? -from - 1 : from;
                to = to < 0 ? -to - 1 : to;

                for (int i = Math.max(0, from); i < Math.min(X.length, to + 1); i++) {
                    final double ti = dataPoints.get(i);
                    if (!Precision.equals(ti, 0)) {
                        sum += Math.exp(-Math.pow(t - ti, 2) / (2 * 0.05 * ti))
                                / (Math.sqrt(2.0 * Math.PI) * 0.05 * ti);
                    }
                }

                return sum / dataPoints.length();
            }
        };

        kernelDerivationFunction = new Function() {
            private static final long serialVersionUID = 1896912471233540595L;

            @Override
            public double f(Vec x) {
                return f(new double[] { x.get(0) });
            }

            @Override
            public double f(double... x) {

                final double t = x[0];
                double sum = 0.0d;

                final double maxH = Math.pow(X[X.length - 1] * 0.05, 2);
                int from = Arrays.binarySearch(X, t - maxH * 13);
                int to = Arrays.binarySearch(X, t + maxH * 13);
                from = from < 0 ? -from - 1 : from;
                to = to < 0 ? -to - 1 : to;

                for (int i = Math.max(0, from); i < Math.min(X.length, to + 1); i++) {
                    final double ti = dataPoints.get(i);
                    if (!Precision.equals(ti, 0)) {
                        sum += (-79.7885 * Math.exp(-10 * Math.pow(t - ti, 2) / ti)
                                * (Math.pow(ti, 2) + 0.1 * ti - Math.pow(t, 2))) / Math.pow(ti, 3);
                    }
                }

                return sum / dataPoints.length();
            }
        };

        startX = Math.max(dataPoints.get(0), 1.0);
        endX = dataPoints.get(dataPoints.length() - 1);

    } else if (formelVariant == KDEFormelVariant.ButlaBandwidthNotSquared) {

        kernelPdfFunction = new Function() {
            private static final long serialVersionUID = -8200289641116502672L;

            @Override
            public double f(Vec x) {
                return f(new double[] { x.get(0) });
            }

            @Override
            public double f(double... x) {

                final double t = x[0];
                double sum = 0.0d;

                final double maxH = Math.pow(X[X.length - 1] * 0.05, 2);
                int from = Arrays.binarySearch(X, t - maxH * 13);
                int to = Arrays.binarySearch(X, t + maxH * 13);
                from = from < 0 ? -from - 1 : from;
                to = to < 0 ? -to - 1 : to;

                for (int i = Math.max(0, from); i < Math.min(X.length, to + 1); i++) {
                    final double ti = X[i];
                    if (!Precision.equals(ti, 0)) {
                        sum += Math.exp(-Math.pow(t - ti, 2) / (2 * Math.pow(0.05 * ti, 2)))
                                / (Math.sqrt(2.0 * Math.PI) * 0.05 * ti);
                    }
                }

                return sum / dataPoints.length();
            }
        };

        kernelDerivationFunction = new Function() {
            private static final long serialVersionUID = -2561020473687438986L;

            @Override
            public double f(Vec x) {
                return f(new double[] { x.get(0) });
            }

            @Override
            public double f(double... x) {

                final double t = x[0];
                double sum = 0.0d;

                final double maxH = Math.pow(X[X.length - 1] * 0.05, 2);
                int from = Arrays.binarySearch(X, t - maxH * 13);
                int to = Arrays.binarySearch(X, t + maxH * 13);
                from = from < 0 ? -from - 1 : from;
                to = to < 0 ? -to - 1 : to;

                for (int i = Math.max(0, from); i < Math.min(X.length, to + 1); i++) {
                    final double ti = dataPoints.get(i);
                    if (!Precision.equals(ti, 0)) {
                        sum += ((-7.97885 * Math.pow(ti, 2) - 3191.54 * ti * t + 3191.54 * Math.pow(t, 2))
                                * Math.exp(-200 * Math.pow(t - ti, 2) / Math.pow(ti, 2))) / Math.pow(ti, 4);
                    }
                }

                return sum / dataPoints.length();
            }
        };

        startX = Math.max(dataPoints.get(0), 1.0);
        endX = dataPoints.get(dataPoints.length() - 1);

    } else if (formelVariant == KDEFormelVariant.ButlaBandwidthSquared) {

        kernelPdfFunction = new Function() {
            private static final long serialVersionUID = 6749547413109881687L;

            @Override
            public double f(Vec x) {
                return f(new double[] { x.get(0) });
            }

            @Override
            public double f(double... x) {

                final double t = x[0];
                double sum = 0.0d;

                final double maxH = X[X.length - 1] * 0.05;
                int from = Arrays.binarySearch(X, t - maxH * 13);
                int to = Arrays.binarySearch(X, t + maxH * 13);
                from = from < 0 ? -from - 1 : from;
                to = to < 0 ? -to - 1 : to;

                for (int i = Math.max(0, from); i < Math.min(X.length, to + 1); i++) {
                    final double ti = dataPoints.get(i);
                    if (!Precision.equals(ti, 0)) {
                        sum += Math.exp(-Math.pow(t - ti, 2) / (2 * 0.05 * ti))
                                / (Math.sqrt(2.0 * Math.PI * 0.05 * ti));
                    }
                }

                return sum / dataPoints.length();
            }
        };

        kernelDerivationFunction = new Function() {
            private static final long serialVersionUID = 3612595828189571262L;

            @Override
            public double f(Vec x) {
                return f(new double[] { x.get(0) });
            }

            @Override
            public double f(double... x) {

                final double t = x[0];
                double sum = 0.0d;

                final double maxH = X[X.length - 1] * 0.05;
                int from = Arrays.binarySearch(X, t - maxH * 13);
                int to = Arrays.binarySearch(X, t + maxH * 13);
                from = from < 0 ? -from - 1 : from;
                to = to < 0 ? -to - 1 : to;

                for (int i = Math.max(0, from); i < Math.min(X.length, to + 1); i++) {
                    final double ti = dataPoints.get(i);
                    if (!Precision.equals(ti, 0)) {
                        sum += (Math.exp(-10 * Math.pow(t - ti, 2) / ti)
                                * (-17.8412 * Math.pow(ti, 2) - 0.892062 * ti + 17.8412 * Math.pow(t, 2)))
                                / Math.sqrt(Math.pow(ti, 5));
                    }
                }

                return sum / dataPoints.length();
            }
        };

        startX = Math.max(dataPoints.get(0), 1.0);
        endX = dataPoints.get(dataPoints.length() - 1);
    }
}

From source file:com.inform.jamps.solver.gurobi.GurobiExpression.java

@Override
public Expression addTerm(final double constant) {
    if (Precision.equals(constant, ZERO_COEFFICIENT)) {
        return this;
    }/*from  ww  w.  ja  v a2  s  .  c om*/

    this.constant += constant;
    return this;
}

From source file:de.upb.timok.models.PDTTA.java

private boolean checkProbability(int state) {
    final List<Transition> outgoingTransitions = getTransitions(state, true);
    final double sum = outgoingTransitions.stream().mapToDouble(t -> t.getProbability()).sum();
    return Precision.equals(sum, 1);
}

From source file:jsat.distributions.MyDistributionSearch.java

/**
 * True iff there are only identical values in the vector
 * @param v/*from   w  w w.  j  a v  a  2  s .  co  m*/
 */
public static Pair<Boolean, Double> checkForDifferentValues(Vec v) {
    final double value = v.get(0);
    for (int i = 1; i < v.length(); i++) {
        if (!Precision.equals(v.get(i), value)) {
            return new Pair<>(false, -1.0);
        }
    }
    return new Pair<>(true, value);
}

From source file:com.clust4j.utils.VectorTests.java

@Test
public void testCosSim() {
    final double[] a = new double[] { 1, 1, 1, 1 };
    final double[] b = new double[] { 1, 2, 3, 4 };
    final double cosSim1 = VecUtils.cosSim(a, b);

    assertTrue(Precision.equals(cosSim1, 0.9128709291752769));
}

From source file:com.inform.jamps.solver.gurobi.GurobiVariable.java

@Override
public String toString() {
    final StringBuilder sb = new StringBuilder(200);
    switch (type) {
    case BINARY:/*w w  w  .j  av a  2 s.co m*/
        sb.append("Binary ");
        break;
    case CONTINUOUS:
        sb.append("Continuous ");
        break;
    case INTEGER:
        sb.append("Integer ");
        break;
    case SEMI_CONTINUOUS:
        sb.append("Semi-Continuous ");
        break;
    case SEMI_INTEGER:
        sb.append("Semi-Integer ");
        break;
    default:
        break;
    }
    sb.append(name);
    if (lowerBound > Double.NEGATIVE_INFINITY || upperBound < Double.POSITIVE_INFINITY) {
        if (Precision.equals(lowerBound, Double.NEGATIVE_INFINITY)) {
            sb.append(" (,");
        } else {
            sb.append(" [");
            sb.append(lowerBound);
            sb.append(',');
        }
        if (Precision.equals(upperBound, Double.POSITIVE_INFINITY)) {
            sb.append(')');
        } else {
            sb.append(' ');
            sb.append(upperBound);
            sb.append(']');
        }
    } else {
        sb.append(" (unbounded)");
    }
    return sb.toString();
}

From source file:com.inform.jamps.solver.gurobi.GurobiExpression.java

@Override
public int compareTo(final Expression expr) {
    if (!(expr instanceof GurobiExpression)) {
        return -1;
    }//w w w  . j  ava2 s.co m
    if (equals(expr)) {
        return 0;
    }

    final GurobiExpression grbExpr = ((GurobiExpression) expr);
    final int exprLength1 = linearTerms.size() + (Precision.equals(constant, ZERO_COEFFICIENT) ? 0 : 1);
    final int exprLength2 = grbExpr.linearTerms.size()
            + (Precision.equals(grbExpr.constant, ZERO_COEFFICIENT) ? 0 : 1);

    final int result = Integer.valueOf(exprLength1).compareTo(exprLength2);
    if (result != 0) {
        return result;
    }

    return Double.compare(constant, grbExpr.constant);
}

From source file:de.upb.timok.models.PDTTA.java

boolean fixProbability(int state) {
    // TODO use Fraction or BigFraction here!
    final List<Transition> outgoingTransitions = getTransitions(state, true);
    final double sum = outgoingTransitions.stream().mapToDouble(t -> t.getProbability()).sum();
    logger.info("Sum of transition probabilities is {}", sum);
    // divide every probability by the sum of probabilities s.t. they sum up to 1
    outgoingTransitions.forEach(t -> t.setProbability(t.getProbability() / sum));
    final double newSum = outgoingTransitions.stream().mapToDouble(t -> t.getProbability()).sum();
    logger.info("Corrected sum of transition probabilities is {}", newSum);
    if (!Precision.equals(newSum, 1.0)) {
        throw new IllegalStateException();
    }//from w  ww . j av a  2  s  .c  om
    return true;
}