List of usage examples for java.lang Double compare
public static int compare(double d1, double d2)
From source file:com.grantingersoll.intell.clustering.KMeansClusteringEngine.java
public static String getTopFeatures(Vector vector, String[] dictionary, int numTerms) { List<TermIndexWeight> vectorTerms = new ArrayList<TermIndexWeight>(); Iterator<Vector.Element> iter = vector.iterateNonZero(); while (iter.hasNext()) { Vector.Element elt = iter.next(); vectorTerms.add(new TermIndexWeight(elt.index(), elt.get())); }/*from w ww . ja va 2s.c om*/ // Sort results in reverse order (ie weight in descending order) Collections.sort(vectorTerms, new Comparator<TermIndexWeight>() { public int compare(TermIndexWeight one, TermIndexWeight two) { return Double.compare(two.weight, one.weight); } }); Collection<Pair<String, Double>> topTerms = new LinkedList<Pair<String, Double>>(); for (int i = 0; (i < vectorTerms.size()) && (i < numTerms); i++) { int index = vectorTerms.get(i).index; String dictTerm = dictionary[index]; if (dictTerm == null) { log.error("Dictionary entry missing for {}", index); continue; } topTerms.add(new Pair<String, Double>(dictTerm, vectorTerms.get(i).weight)); } StringBuilder sb = new StringBuilder(100); for (Pair<String, Double> item : topTerms) { String term = item.getFirst(); sb.append("\n\t\t"); sb.append(StringUtils.rightPad(term, 40)); sb.append("=>"); sb.append(StringUtils.leftPad(item.getSecond().toString(), 20)); } return sb.toString(); }
From source file:it.polimi.geinterface.GroupEntityManager.java
/** * Method used to store last detected beacons. The maximum number of beacons that can be stored is 3: the * nearest two, and the furthest one./* ww w . ja v a 2 s . co m*/ * @param visibleBeacons - List of {@link Beacon} detected in the last BLE scan. */ protected void saveLastSeenBeacons(final ArrayList<Beacon> visibleBeacons) { if (visibleBeacons.size() == 0) { synchronized (lastSeenBeacons) { lastSeenBeacons.clear(); } return; } scheduler.schedule(new Runnable() { @Override public void run() { if (visibleBeacons.size() < 4) synchronized (lastSeenBeacons) { lastSeenBeacons.clear(); for (Beacon e : visibleBeacons) { Entity be = new Entity.Builder(e.getId1() + ":" + e.getId2() + ":" + e.getId3(), Type.BLE_BEACON) .setDistance( GroupEntityManager.getDistanceRangeFromBeacon(e.getDistance())) .build(); lastSeenBeacons.add(be); } } else { //sort by distance Collections.sort(visibleBeacons, new Comparator<Beacon>() { @Override public int compare(Beacon lhs, Beacon rhs) { return Double.compare(lhs.getDistance(), rhs.getDistance()); } }); Beacon b1 = visibleBeacons.get(0); Beacon b2 = visibleBeacons.get(1); Beacon b3 = visibleBeacons.get(visibleBeacons.size() - 1); Entity e1 = new Entity.Builder(b1.getId1() + ":" + b1.getId2() + ":" + b1.getId3(), Type.BLE_BEACON) .setDistance(GroupEntityManager.getDistanceRangeFromBeacon(b1.getDistance())) .build(); Entity e2 = new Entity.Builder(b2.getId1() + ":" + b2.getId2() + ":" + b2.getId3(), Type.BLE_BEACON) .setDistance(GroupEntityManager.getDistanceRangeFromBeacon(b2.getDistance())) .build(); Entity e3 = new Entity.Builder(b3.getId1() + ":" + b3.getId2() + ":" + b3.getId3(), Type.BLE_BEACON) .setDistance(GroupEntityManager.getDistanceRangeFromBeacon(b3.getDistance())) .build(); synchronized (lastSeenBeacons) { lastSeenBeacons.add(e1); lastSeenBeacons.add(e2); lastSeenBeacons.add(e3); } } } }); }
From source file:org.apache.carbondata.processing.merger.CarbonDataMergerUtil.java
/** * Sorting of the segments.//from www . j a va 2 s . com * @param segments */ public static void sortSegments(List segments) { // sort the segment details. Collections.sort(segments, new Comparator<LoadMetadataDetails>() { @Override public int compare(LoadMetadataDetails seg1, LoadMetadataDetails seg2) { double seg1Id = Double.parseDouble(seg1.getLoadName()); double seg2Id = Double.parseDouble(seg2.getLoadName()); return Double.compare(seg1Id, seg2Id); } }); }
From source file:ml.shifu.shifu.core.binning.UpdateBinningInfoMapper.java
private void populateStats(String[] units, String tag, Double weight, int columnIndex, int newCCIndex) { ColumnConfig columnConfig = this.columnConfigList.get(columnIndex); CountAndFrequentItems countAndFrequentItems = this.variableCountMap.get(newCCIndex); if (countAndFrequentItems == null) { countAndFrequentItems = new CountAndFrequentItems(); this.variableCountMap.put(newCCIndex, countAndFrequentItems); }/* w ww . java2s . co m*/ countAndFrequentItems.offer(this.missingOrInvalidValues, units[columnIndex]); boolean isMissingValue = false; boolean isInvalidValue = false; BinningInfoWritable binningInfoWritable = this.columnBinningInfo.get(newCCIndex); if (binningInfoWritable == null) { return; } binningInfoWritable.setTotalCount(binningInfoWritable.getTotalCount() + 1L); if (columnConfig.isHybrid()) { int binNum = 0; if (units[columnIndex] == null || missingOrInvalidValues.contains(units[columnIndex].toLowerCase())) { isMissingValue = true; } String str = units[columnIndex]; double douVal = BinUtils.parseNumber(str); Double hybridThreshold = columnConfig.getHybridThreshold(); if (hybridThreshold == null) { hybridThreshold = Double.NEGATIVE_INFINITY; } // douVal < hybridThreshould which will also be set to category boolean isCategory = Double.isNaN(douVal) || douVal < hybridThreshold; boolean isNumber = !Double.isNaN(douVal); if (isMissingValue) { binningInfoWritable.setMissingCount(binningInfoWritable.getMissingCount() + 1L); binNum = binningInfoWritable.getBinCategories().size() + binningInfoWritable.getBinBoundaries().size(); } else if (isCategory) { // get categorical bin number in category list binNum = quickLocateCategoricalBin(this.categoricalBinMap.get(newCCIndex), str); if (binNum < 0) { isInvalidValue = true; } if (isInvalidValue) { // the same as missing count binningInfoWritable.setMissingCount(binningInfoWritable.getMissingCount() + 1L); binNum = binningInfoWritable.getBinCategories().size() + binningInfoWritable.getBinBoundaries().size(); } else { // if real category value, binNum should + binBoundaries.size binNum += binningInfoWritable.getBinBoundaries().size(); ; } } else if (isNumber) { binNum = getBinNum(binningInfoWritable.getBinBoundaries(), douVal); if (binNum == -1) { throw new RuntimeException("binNum should not be -1 to this step."); } // other stats are treated as numerical features binningInfoWritable.setSum(binningInfoWritable.getSum() + douVal); double squaredVal = douVal * douVal; binningInfoWritable.setSquaredSum(binningInfoWritable.getSquaredSum() + squaredVal); binningInfoWritable.setTripleSum(binningInfoWritable.getTripleSum() + squaredVal * douVal); binningInfoWritable.setQuarticSum(binningInfoWritable.getQuarticSum() + squaredVal * squaredVal); if (Double.compare(binningInfoWritable.getMax(), douVal) < 0) { binningInfoWritable.setMax(douVal); } if (Double.compare(binningInfoWritable.getMin(), douVal) > 0) { binningInfoWritable.setMin(douVal); } } if (posTags.contains(tag)) { binningInfoWritable.getBinCountPos()[binNum] += 1L; binningInfoWritable.getBinWeightPos()[binNum] += weight; } else if (negTags.contains(tag)) { binningInfoWritable.getBinCountNeg()[binNum] += 1L; binningInfoWritable.getBinWeightNeg()[binNum] += weight; } } else if (columnConfig.isCategorical()) { int lastBinIndex = binningInfoWritable.getBinCategories().size(); int binNum = 0; if (units[columnIndex] == null || missingOrInvalidValues.contains(units[columnIndex].toLowerCase())) { isMissingValue = true; } else { String str = units[columnIndex]; binNum = quickLocateCategoricalBin(this.categoricalBinMap.get(newCCIndex), str); if (binNum < 0) { isInvalidValue = true; } } if (isInvalidValue || isMissingValue) { binningInfoWritable.setMissingCount(binningInfoWritable.getMissingCount() + 1L); binNum = lastBinIndex; } if (modelConfig.isRegression()) { if (posTags.contains(tag)) { binningInfoWritable.getBinCountPos()[binNum] += 1L; binningInfoWritable.getBinWeightPos()[binNum] += weight; } else if (negTags.contains(tag)) { binningInfoWritable.getBinCountNeg()[binNum] += 1L; binningInfoWritable.getBinWeightNeg()[binNum] += weight; } } else { // for multiple classification, set bin count to BinCountPos and leave BinCountNeg empty binningInfoWritable.getBinCountPos()[binNum] += 1L; binningInfoWritable.getBinWeightPos()[binNum] += weight; } } else if (columnConfig.isNumerical()) { int lastBinIndex = binningInfoWritable.getBinBoundaries().size(); double douVal = 0.0; if (units[columnIndex] == null || units[columnIndex].length() == 0 || missingOrInvalidValues.contains(units[columnIndex].toLowerCase())) { isMissingValue = true; } else { try { douVal = Double.parseDouble(units[columnIndex].trim()); } catch (Exception e) { isInvalidValue = true; } } // add logic the same as CalculateNewStatsUDF if (Double.compare(douVal, modelConfig.getNumericalValueThreshold()) > 0) { isInvalidValue = true; } if (isInvalidValue || isMissingValue) { binningInfoWritable.setMissingCount(binningInfoWritable.getMissingCount() + 1L); if (modelConfig.isRegression()) { if (posTags.contains(tag)) { binningInfoWritable.getBinCountPos()[lastBinIndex] += 1L; binningInfoWritable.getBinWeightPos()[lastBinIndex] += weight; } else if (negTags.contains(tag)) { binningInfoWritable.getBinCountNeg()[lastBinIndex] += 1L; binningInfoWritable.getBinWeightNeg()[lastBinIndex] += weight; } } } else { // For invalid or missing values, no need update sum, squaredSum, max, min ... int binNum = getBinNum(binningInfoWritable.getBinBoundaries(), units[columnIndex]); if (binNum == -1) { throw new RuntimeException("binNum should not be -1 to this step."); } if (modelConfig.isRegression()) { if (posTags.contains(tag)) { binningInfoWritable.getBinCountPos()[binNum] += 1L; binningInfoWritable.getBinWeightPos()[binNum] += weight; } else if (negTags.contains(tag)) { binningInfoWritable.getBinCountNeg()[binNum] += 1L; binningInfoWritable.getBinWeightNeg()[binNum] += weight; } } binningInfoWritable.setSum(binningInfoWritable.getSum() + douVal); double squaredVal = douVal * douVal; binningInfoWritable.setSquaredSum(binningInfoWritable.getSquaredSum() + squaredVal); binningInfoWritable.setTripleSum(binningInfoWritable.getTripleSum() + squaredVal * douVal); binningInfoWritable.setQuarticSum(binningInfoWritable.getQuarticSum() + squaredVal * squaredVal); if (Double.compare(binningInfoWritable.getMax(), douVal) < 0) { binningInfoWritable.setMax(douVal); } if (Double.compare(binningInfoWritable.getMin(), douVal) > 0) { binningInfoWritable.setMin(douVal); } } } }
From source file:com.rapidminer.tools.Tools.java
/** * Returns <code>false</code> if either d1 or d2 is Double.NaN. Otherwis returns * <code>true</code> if the d1 is greater than d2. *//*from w w w .j a v a 2 s.co m*/ public static boolean isGreater(double d1, double d2) { if (Double.isNaN(d1) || Double.isNaN(d2)) { return false; } return Double.compare(d1, d2) > 0; }
From source file:blusunrize.immersiveengineering.client.render.TileRenderAutoWorkbench.java
public static BlueprintLines getBlueprintDrawable(ItemStack stack, World world) { if (stack.isEmpty()) return null; EntityPlayer player = ClientUtils.mc().player; ArrayList<BufferedImage> images = new ArrayList<>(); try {//from w w w. ja v a 2 s.c om IBakedModel ibakedmodel = ClientUtils.mc().getRenderItem().getItemModelWithOverrides(stack, world, player); HashSet<String> textures = new HashSet(); Collection<BakedQuad> quads = ibakedmodel.getQuads(null, null, 0); for (BakedQuad quad : quads) if (quad != null && quad.getSprite() != null) textures.add(quad.getSprite().getIconName()); for (String s : textures) { ResourceLocation rl = new ResourceLocation(s); rl = new ResourceLocation(rl.getNamespace(), String.format("%s/%s%s", "textures", rl.getPath(), ".png")); IResource resource = ClientUtils.mc().getResourceManager().getResource(rl); BufferedImage bufferedImage = TextureUtil.readBufferedImage(resource.getInputStream()); if (bufferedImage != null) images.add(bufferedImage); } } catch (Exception e) { } if (images.isEmpty()) return null; ArrayList<Pair<TexturePoint, TexturePoint>> lines = new ArrayList(); HashSet testSet = new HashSet(); HashMultimap<Integer, TexturePoint> area = HashMultimap.create(); int wMax = 0; for (BufferedImage bufferedImage : images) { Set<Pair<TexturePoint, TexturePoint>> temp_lines = new HashSet<>(); int w = bufferedImage.getWidth(); int h = bufferedImage.getHeight(); if (h > w) h = w; if (w > wMax) wMax = w; for (int hh = 0; hh < h; hh++) for (int ww = 0; ww < w; ww++) { int argb = bufferedImage.getRGB(ww, hh); float r = (argb >> 16 & 255) / 255f; float g = (argb >> 8 & 255) / 255f; float b = (argb & 255) / 255f; float intesity = (r + b + g) / 3f; int alpha = (argb >> 24) & 255; if (alpha > 0) { boolean added = false; //Check colour sets for similar colour to shade it later TexturePoint tp = new TexturePoint(ww, hh, w); if (!testSet.contains(tp)) { for (Integer key : area.keySet()) { for (TexturePoint p : area.get(key)) { float mod = w / (float) p.scale; int pColour = bufferedImage.getRGB((int) (p.x * mod), (int) (p.y * mod)); float dR = (r - (pColour >> 16 & 255) / 255f); float dG = (g - (pColour >> 8 & 255) / 255f); float dB = (b - (pColour & 255) / 255f); double delta = Math.sqrt(dR * dR + dG * dG + dB * dB); if (delta < .25) { area.put(key, tp); added = true; break; } } if (added) break; } if (!added) area.put(argb, tp); testSet.add(tp); } //Compare to direct neighbour for (int i = 0; i < 4; i++) { int xx = (i == 0 ? -1 : i == 1 ? 1 : 0); int yy = (i == 2 ? -1 : i == 3 ? 1 : 0); int u = ww + xx; int v = hh + yy; int neighbour = 0; float delta = 1; boolean notTransparent = false; if (u >= 0 && u < w && v >= 0 && v < h) { neighbour = bufferedImage.getRGB(u, v); notTransparent = ((neighbour >> 24) & 255) > 0; if (notTransparent) { float neighbourIntesity = ((neighbour >> 16 & 255) + (neighbour >> 8 & 255) + (neighbour & 255)) / 765f; float intesityDelta = Math.max(0, Math.min(1, Math.abs(intesity - neighbourIntesity))); float rDelta = Math.max(0, Math.min(1, Math.abs(r - (neighbour >> 16 & 255) / 255f))); float gDelta = Math.max(0, Math.min(1, Math.abs(g - (neighbour >> 8 & 255) / 255f))); float bDelta = Math.max(0, Math.min(1, Math.abs(b - (neighbour & 255) / 255f))); delta = Math.max(intesityDelta, Math.max(rDelta, Math.max(gDelta, bDelta))); delta = delta < .25 ? 0 : delta > .4 ? 1 : delta; } } if (delta > 0) { Pair<TexturePoint, TexturePoint> l = Pair.of( new TexturePoint(ww + (i == 0 ? 0 : i == 1 ? 1 : 0), hh + (i == 2 ? 0 : i == 3 ? 1 : 0), w), new TexturePoint(ww + (i == 0 ? 0 : i == 1 ? 1 : 1), hh + (i == 2 ? 0 : i == 3 ? 1 : 1), w)); temp_lines.add(l); } } } } lines.addAll(temp_lines); } ArrayList<Integer> lumiSort = new ArrayList<>(area.keySet()); Collections.sort(lumiSort, (rgb1, rgb2) -> Double.compare(getLuminance(rgb1), getLuminance(rgb2))); HashMultimap<ShadeStyle, Point> complete_areaMap = HashMultimap.create(); int lineNumber = 2; int lineStyle = 0; for (Integer i : lumiSort) { complete_areaMap.putAll(new ShadeStyle(lineNumber, lineStyle), area.get(i)); ++lineStyle; lineStyle %= 3; if (lineStyle == 0) lineNumber += 1; } Set<Pair<Point, Point>> complete_lines = new HashSet<>(); for (Pair<TexturePoint, TexturePoint> line : lines) { TexturePoint p1 = line.getKey(); TexturePoint p2 = line.getValue(); complete_lines.add(Pair.of( new Point((int) (p1.x / (float) p1.scale * wMax), (int) (p1.y / (float) p1.scale * wMax)), new Point((int) (p2.x / (float) p2.scale * wMax), (int) (p2.y / (float) p2.scale * wMax)))); } return new BlueprintLines(wMax, complete_lines, complete_areaMap); }
From source file:cz.cuni.mff.spl.evaluator.statistics.KolmogorovSmirnovTestFlag.java
/** * Computes the two-sample Kolmogorov-Smirnov test statistic, \(D_{n,m}=\sup_x |F_n(x)-F_m(x)|\) * where \(n\) is the length of {@code x}, \(m\) is the length of {@code y}, \(F_n\) is the * empirical distribution that puts mass \(1/n\) at each of the values in {@code x} and \(F_m\) * is the empirical distribution of the {@code y} values. Finally \(n m D_{n,m}\) is returned * as long value. /*w w w . j ava 2 s.c o m*/ * * Implementation added to indicate whether the supremum was gained from a positive or negative * (negFlag = 0 or 1 respectively) difference * * @param x first sample * @param y second sample * @return test statistic \(n m D_{n,m}\) used to evaluate the null hypothesis that {@code x} and * {@code y} represent samples from the same underlying distribution * @throws InsufficientDataException if either {@code x} or {@code y} does not have length at * least 2 * @throws NullArgumentException if either {@code x} or {@code y} is null */ private long integralKolmogorovSmirnovStatisticFlag(double[] x, double[] y) { checkArray(x); checkArray(y); // Copy and sort the sample arrays final double[] sx = MathArrays.copyOf(x); final double[] sy = MathArrays.copyOf(y); Arrays.sort(sx); Arrays.sort(sy); final int n = sx.length; final int m = sy.length; int rankX = 0; int rankY = 0; long curD = 0l; negFlag = 0; // Find the max difference between cdf_x and cdf_y long supD = 0l; do { double z = Double.compare(sx[rankX], sy[rankY]) <= 0 ? sx[rankX] : sy[rankY]; while (rankX < n && Double.compare(sx[rankX], z) == 0) { rankX += 1; curD += m; } while (rankY < m && Double.compare(sy[rankY], z) == 0) { rankY += 1; curD -= n; } if (curD > supD) { negFlag = 0; supD = curD; } //the current largest difference is -ve and hence y lies left of x else if (-curD > supD) { negFlag = 1; supD = -curD; } } while (rankX < n && rankY < m); return supD; }
From source file:net.certiv.authmgr.task.section.core.classifier.BayesPartitionClassifier.java
public static double normaliseSignificanceX(double sig) { if (Double.compare(IClassifier.UPPER_BOUND, sig) < 0) { return IClassifier.UPPER_BOUND; } else if (Double.compare(IClassifier.LOWER_BOUND, sig) > 0) { return IClassifier.LOWER_BOUND; } else {//ww w . ja v a 2 s .c o m return sig; } }
From source file:com.mesosphere.dcos.cassandra.common.config.CassandraConfig.java
@Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof CassandraConfig)) return false; CassandraConfig that = (CassandraConfig) o; return Double.compare(that.getCpus(), getCpus()) == 0 && getMemoryMb() == that.getMemoryMb() && getDiskMb() == that.getDiskMb() && getDiskType() == that.getDiskType() && getJmxPort() == that.getJmxPort() && getPublishDiscoveryInfo() == that.getPublishDiscoveryInfo() && Objects.equals(getRollingRestartName(), that.getRollingRestartName()) && Objects.equals(getVersion(), that.getVersion()) && Objects.equals(getReplaceIp(), that.getReplaceIp()) && Objects.equals(getHeap(), that.getHeap()) && Objects.equals(getLocation(), that.getLocation()) && Objects.equals(getApplication(), that.getApplication()); }
From source file:ml.shifu.shifu.core.binning.UpdateBinningInfoReducer.java
private double[] computePosRate(long[] binCountPos, long[] binCountNeg) { assert binCountPos != null && binCountNeg != null && binCountPos.length == binCountNeg.length; double[] posRate = new double[binCountPos.length]; for (int i = 0; i < posRate.length; i++) { if (Double.compare(binCountPos[i] + binCountNeg[i], 0d) != 0) { // only compute effective pos rate, if /0, don't do it posRate[i] = binCountPos[i] * 1.0d / (binCountPos[i] + binCountNeg[i]); }//from w w w . jav a2 s.co m } return posRate; }