List of usage examples for java.lang Double isInfinite
public static boolean isInfinite(double v)
From source file:egat.replicatordynamics.SymmetricConstrainedFeasibleAmoebaSearch.java
private void generateFeasibleTowards(double[] newVertex, double[] first, double[] second, double scale, PayoffMatrix pm, int[] restricted, double[] distribution) { generateTowards(newVertex, first, second, scale); double V = calculateObjective(pm, newVertex, restricted, distribution); if (Double.isInfinite(V)) { double a1 = scale; double a2 = 0.0; double a; double aV; do {//from ww w. j a v a 2 s . c o m a = (a1 + a2) * 0.5; generateTowards(newVertex, first, second, a); aV = calculateObjective(pm, newVertex, restricted, distribution); if (Double.isInfinite(aV)) { a1 = a; } else { a2 = a; } } while (a1 - a2 > 1e-5); if (Double.isInfinite(aV)) { generateTowards(newVertex, first, second, a2); } } }
From source file:edu.byu.nlp.util.Matrices.java
/** * Finds an ordering that minimizes the passed-in loss function. * Formulates the problem as an instance of the 'assignment problem' * and solves using a linear solver.//w w w . j a v a2 s.c om * * @param lossfunction takes as an argument a matrix row, and outputs the * loss associated * * @return a vectors of new row assignments. For example, [0,1,2,3] indicate the trivial re-ordering. */ public static int[] getRowReordering(double[][] mat, RowReorderingLossFunction lossFunction) { // In the assignment problem formulation, there will be one variable associated with // each possible row->dst assignment. We calculate the coefficient of each of // these using the passed-in loss function. Our coefficients are therefore a // vectorized form of the loss matrix. int n = mat.length; double[] objCoeff = new double[n * n]; for (int src = 0; src < n; src++) { for (int dst = 0; dst < n; dst++) { double loss = lossFunction.rowAssignmentLoss(mat[src], dst); objCoeff[n * src + dst] = loss; if (Double.isInfinite(loss) || Double.isNaN(loss)) { throw new IllegalArgumentException("loss function returned an invalid number (" + loss + ") " + "when asked to consider \n\trow " + DoubleArrays.toString(mat[src]) + " \n\tin position " + dst); } } } // objective function double offset = 0; LinearObjectiveFunction f = new LinearObjectiveFunction(objCoeff, offset); // constraints Collection<LinearConstraint> constraints = Lists.newArrayList(); // each src must have exactly one dst for (int src = 0; src < n; src++) { double[] constCoeff = DoubleArrays.of(0, n * n); Arrays.fill(constCoeff, n * src, n * (src + 1), 1); // single row of 1s constraints.add(new LinearConstraint(constCoeff, Relationship.EQ, 1)); } // each dst must have exactly one src for (int dst = 0; dst < n; dst++) { double[] constCoeff = DoubleArrays.of(0, n * n); for (int src = 0; src < n; src++) { constCoeff[n * src + dst] = 1; // single col of 1s } constraints.add(new LinearConstraint(constCoeff, Relationship.EQ, 1)); } // solve boolean restrictToNonNegative = true; SimplexSolver solver = new SimplexSolver(); solver.setMaxIterations(10000); PointValuePair solution = solver.optimize(f, constraints, GoalType.MINIMIZE, restrictToNonNegative); double[] assignmentMatrix = solution.getPoint(); // the assignment matrix should be very simple; each x is either 0 or 1, // and there is a single 1 per row and column // we can deterministically convert this to an int[] int[] result = new int[n]; for (int src = 0; src < n; src++) { int rowNonZeroCount = 0; for (int dst = 0; dst < n; dst++) { double val = assignmentMatrix[n * src + dst]; if (Math.abs(val - 1) < 1e-6) { result[src] = dst; rowNonZeroCount++; } } if (rowNonZeroCount != 1) { throw new IllegalStateException( "The assignment problem linear solver returned an assignment matrix with " + "invalid entries. This should never happen! Here is the matrix encoded as a vector " + "with rows of length " + n + ":\n\t" + DoubleArrays.toString(assignmentMatrix)); } } return result; }
From source file:org.drugis.addis.presentation.wizard.TreatmentCategorizationWizardPresentation.java
private void transformSubtree() { DecisionTree tree = getBean().getDecisionTree(); getModelForFlexibleDose().setValue(d_flexibleLowerNode); for (DecisionTreeEdge edge : getOutEdges(d_fixedRangeNode)) { DoseQuantityChoiceNode node = new DoseQuantityChoiceNode(FlexibleDose.class, FlexibleDose.PROPERTY_MAX_DOSE, getDoseUnit()); RangeEdge rangeEdge = (RangeEdge) edge; tree.addChild(RangeEdge.copy(rangeEdge), d_flexibleLowerNode, node); LeafNode leafChild = new LeafNode(((LeafNode) tree.getEdgeTarget(edge)).getCategory()); if (leafChild.getCategory() == null) { RangeEdge upperEdge = new RangeEdge(rangeEdge.getLowerBound(), rangeEdge.isLowerBoundOpen(), Double.POSITIVE_INFINITY, true); tree.addChild(upperEdge, node, new LeafNode()); } else {/*from ww w. j a v a 2 s. c o m*/ tree.addChild(RangeEdge.copy(rangeEdge), node, leafChild); if (!Double.isInfinite(rangeEdge.getUpperBound())) { RangeEdge upperEdge = new RangeEdge(rangeEdge.getUpperBound(), !rangeEdge.isUpperBoundOpen(), Double.POSITIVE_INFINITY, true); tree.addChild(upperEdge, node, new LeafNode()); } } } }
From source file:de.tuberlin.uebb.jbop.example.DerivativeStructure.java
/** * Returns the hypotenuse of a triangle with sides {@code x} and {@code y} - * sqrt(<i>x</i><sup>2</sup> +<i>y</i><sup>2</sup>)<br/> * avoiding intermediate overflow or underflow. * /*from w ww. j a va 2 s. co m*/ * <ul> * <li>If either argument is infinite, then the result is positive infinity.</li> * <li>else, if either argument is NaN then the result is NaN.</li> * </ul> * * @param x * a value * @param y * a value * @return sqrt(<i>x</i><sup>2</sup> +<i>y</i><sup>2</sup>) * @throws DimensionMismatchException * if number of free parameters or orders are inconsistent */ public static DerivativeStructure hypot(final DerivativeStructure x, final DerivativeStructure y) throws DimensionMismatchException { x.compiler.checkCompatibility(y.compiler); if (Double.isInfinite(x.data[0]) || Double.isInfinite(y.data[0])) { return new DerivativeStructure(x.compiler.getFreeParameters(), x.compiler.getFreeParameters(), Double.POSITIVE_INFINITY); } else if (Double.isNaN(x.data[0]) || Double.isNaN(y.data[0])) { return new DerivativeStructure(x.compiler.getFreeParameters(), x.compiler.getFreeParameters(), Double.NaN); } else { final int expX = x.getExponent(); final int expY = y.getExponent(); if (expX > (expY + 27)) { // y is neglectible with respect to x return x.abs(); } else if (expY > (expX + 27)) { // x is neglectible with respect to y return y.abs(); } else { // find an intermediate scale to avoid both overflow and underflow final int middleExp = (expX + expY) / 2; // scale parameters without losing precision final DerivativeStructure scaledX = x.scalb(-middleExp); final DerivativeStructure scaledY = y.scalb(-middleExp); // compute scaled hypotenuse final DerivativeStructure scaledH = scaledX.multiply(scaledX).add(scaledY.multiply(scaledY)).sqrt(); // remove scaling return scaledH.scalb(middleExp); } } }
From source file:org.broadinstitute.gatk.tools.walkers.variantrecalibration.VariantDataManager.java
public List<VariantDatum> selectWorstVariants() { final List<VariantDatum> trainingData = new ExpandingArrayList<>(); for (final VariantDatum datum : data) { if (datum != null && !datum.failingSTDThreshold && !Double.isInfinite(datum.lod) && datum.lod < VRAC.BAD_LOD_CUTOFF) { datum.atAntiTrainingSite = true; trainingData.add(datum);/* w ww . ja va 2 s. c o m*/ } } logger.info("Training with worst " + trainingData.size() + " scoring variants --> variants with LOD <= " + String.format("%.4f", VRAC.BAD_LOD_CUTOFF) + "."); return trainingData; }
From source file:de.tuberlin.uebb.jbop.example.DerivativeStructureOnlyCompose.java
/** * Returns the hypotenuse of a triangle with sides {@code x} and {@code y} - * sqrt(<i>x</i><sup>2</sup> +<i>y</i><sup>2</sup>)<br/> * avoiding intermediate overflow or underflow. * //from ww w .ja va 2 s. c o m * <ul> * <li>If either argument is infinite, then the result is positive infinity.</li> * <li>else, if either argument is NaN then the result is NaN.</li> * </ul> * * @param x * a value * @param y * a value * @return sqrt(<i>x</i><sup>2</sup> +<i>y</i><sup>2</sup>) * @throws DimensionMismatchException * if number of free parameters or orders are inconsistent */ public static DerivativeStructureOnlyCompose hypot(final DerivativeStructureOnlyCompose x, final DerivativeStructureOnlyCompose y) throws DimensionMismatchException { x.compiler.checkCompatibility(y.compiler); if (Double.isInfinite(x.data[0]) || Double.isInfinite(y.data[0])) { return new DerivativeStructureOnlyCompose(x.compiler.getFreeParameters(), x.compiler.getFreeParameters(), Double.POSITIVE_INFINITY); } else if (Double.isNaN(x.data[0]) || Double.isNaN(y.data[0])) { return new DerivativeStructureOnlyCompose(x.compiler.getFreeParameters(), x.compiler.getFreeParameters(), Double.NaN); } else { final int expX = x.getExponent(); final int expY = y.getExponent(); if (expX > (expY + 27)) { // y is neglectible with respect to x return x.abs(); } else if (expY > (expX + 27)) { // x is neglectible with respect to y return y.abs(); } else { // find an intermediate scale to avoid both overflow and underflow final int middleExp = (expX + expY) / 2; // scale parameters without losing precision final DerivativeStructureOnlyCompose scaledX = x.scalb(-middleExp); final DerivativeStructureOnlyCompose scaledY = y.scalb(-middleExp); // compute scaled hypotenuse final DerivativeStructureOnlyCompose scaledH = scaledX.multiply(scaledX) .add(scaledY.multiply(scaledY)).sqrt(); // remove scaling return scaledH.scalb(middleExp); } } }
From source file:net.sf.mzmine.modules.peaklistmethods.dataanalysis.heatmaps.HeatMapTask.java
private double[][] groupingDataset(UserParameter selectedParameter, String referenceGroup) { // Collect all data files Vector<RawDataFile> allDataFiles = new Vector<RawDataFile>(); DescriptiveStatistics meanControlStats = new DescriptiveStatistics(); DescriptiveStatistics meanGroupStats = new DescriptiveStatistics(); allDataFiles.addAll(Arrays.asList(peakList.getRawDataFiles())); // Determine the reference group and non reference group (the rest of // the samples) for raw data files List<RawDataFile> referenceDataFiles = new ArrayList<RawDataFile>(); List<RawDataFile> nonReferenceDataFiles = new ArrayList<RawDataFile>(); List<String> groups = new ArrayList<String>(); MZmineProject project = MZmineCore.getCurrentProject(); for (RawDataFile rawDataFile : allDataFiles) { Object paramValue = project.getParameterValue(selectedParameter, rawDataFile); if (!groups.contains(String.valueOf(paramValue))) { groups.add(String.valueOf(paramValue)); }//from w w w. j a va2 s.c o m if (String.valueOf(paramValue).equals(referenceGroup)) { referenceDataFiles.add(rawDataFile); } else { nonReferenceDataFiles.add(rawDataFile); } } int numRows = 0; for (int row = 0; row < peakList.getNumberOfRows(); row++) { if (!onlyIdentified || (onlyIdentified && peakList.getRow(row).getPeakIdentities().length > 0)) { numRows++; } } // Create a new aligned peak list with all the samples if the reference // group has to be shown or with only // the non reference group if not. double[][] dataMatrix = new double[groups.size() - 1][numRows]; pValueMatrix = new String[groups.size() - 1][numRows]; // data files that should be in the heat map List<RawDataFile> shownDataFiles = nonReferenceDataFiles; for (int row = 0, rowIndex = 0; row < peakList.getNumberOfRows(); row++) { PeakListRow rowPeak = peakList.getRow(row); if (!onlyIdentified || (onlyIdentified && rowPeak.getPeakIdentities().length > 0)) { // Average area or height of the reference group meanControlStats.clear(); for (int column = 0; column < referenceDataFiles.size(); column++) { if (rowPeak.getPeak(referenceDataFiles.get(column)) != null) { if (area) { meanControlStats.addValue(rowPeak.getPeak(referenceDataFiles.get(column)).getArea()); } else { meanControlStats.addValue(rowPeak.getPeak(referenceDataFiles.get(column)).getHeight()); } } } // Divide the area or height of each peak by the average of the // area or height of the reference peaks in each row int columnIndex = 0; for (int column = 0; column < groups.size(); column++) { String group = groups.get(column); meanGroupStats.clear(); if (!group.equals(referenceGroup)) { for (int dataColumn = 0; dataColumn < shownDataFiles.size(); dataColumn++) { Object paramValue = project.getParameterValue(selectedParameter, shownDataFiles.get(dataColumn)); if (rowPeak.getPeak(shownDataFiles.get(dataColumn)) != null && String.valueOf(paramValue).equals(group)) { Feature peak = rowPeak.getPeak(shownDataFiles.get(dataColumn)); if (!Double.isInfinite(peak.getArea()) && !Double.isNaN(peak.getArea())) { if (area) { meanGroupStats.addValue(peak.getArea()); } else { meanGroupStats.addValue(peak.getHeight()); } } } } double value = meanGroupStats.getMean() / meanControlStats.getMean(); if (meanGroupStats.getN() > 1 && meanControlStats.getN() > 1) { pValueMatrix[columnIndex][rowIndex] = this.getPvalue(meanGroupStats, meanControlStats); } else { pValueMatrix[columnIndex][rowIndex] = ""; } if (log) { value = Math.log(value); } dataMatrix[columnIndex++][rowIndex] = value; } } rowIndex++; } } // Scale the data dividing the peak area/height by the standard // deviation of each column if (scale) { scale(dataMatrix); } // Create two arrays: row and column names rowNames = new String[dataMatrix[0].length]; colNames = new String[groups.size() - 1]; int columnIndex = 0; for (String group : groups) { if (!group.equals(referenceGroup)) { colNames[columnIndex++] = group; } } for (int row = 0, rowIndex = 0; row < peakList.getNumberOfRows(); row++) { if (!onlyIdentified || (onlyIdentified && peakList.getRow(row).getPeakIdentities().length > 0)) { if (peakList.getRow(row).getPeakIdentities() != null && peakList.getRow(row).getPeakIdentities().length > 0) { rowNames[rowIndex++] = peakList.getRow(row).getPreferredPeakIdentity().getName(); } else { rowNames[rowIndex++] = "Unknown"; } } } return dataMatrix; }
From source file:org.broadinstitute.sting.utils.MannWhitneyU.java
/** * Validates that observations are in the correct format for a MWU test -- this is only called by the contracts API during testing * @param tree - the collection of labeled observations * @return true iff the tree set is valid (no INFs or NaNs, at least one data point in each set) *//* w w w . j a v a 2s. co m*/ protected static boolean validateObservations(TreeSet<Pair<Number, USet>> tree) { boolean seen1 = false; boolean seen2 = false; boolean seenInvalid = false; for (Pair<Number, USet> p : tree) { if (!seen1 && p.getSecond() == USet.SET1) { seen1 = true; } if (!seen2 && p.getSecond() == USet.SET2) { seen2 = true; } if (Double.isNaN(p.getFirst().doubleValue()) || Double.isInfinite(p.getFirst().doubleValue())) { seenInvalid = true; } } return !seenInvalid && seen1 && seen2; }
From source file:com.clust4j.utils.MatUtils.java
/** * Returns true if there are any Inf (positive or negative) values in the matrix. * @throws IllegalArgumentException if there are no rows in the data * @param mat/* w w w . j a va 2 s . c o m*/ * @return true if the matrix contains Inf */ public static boolean containsInf(final double[][] mat) { checkDimsPermitEmpty(mat); final int m = mat.length; for (int i = 0; i < m; i++) for (int j = 0; j < mat[i].length; j++) if (Double.isInfinite(mat[i][j])) return true; return false; }
From source file:org.obiba.opal.web.magma.Dtos.java
private static boolean isNumeric(double d) { return !Double.isNaN(d) && !Double.isInfinite(d); }