List of usage examples for java.lang Math floor
public static double floor(double a)
From source file:Main.java
/** * Returns a <code>double</code> representation of the <a * href="http://mathworld.wolfram.com/BinomialCoefficient.html"> Binomial * Coefficient</a>, "<code>n choose k</code>", the number of * <code>k</code>-element subsets that can be selected from an * <code>n</code>-element set. * <p>// w w w. ja v a2 s.c om * <Strong>Preconditions</strong>: * <ul> * <li> <code>0 <= k <= n </code> (otherwise * <code>IllegalArgumentException</code> is thrown)</li> * <li> The result is small enough to fit into a <code>double</code>. The * largest value of <code>n</code> for which all coefficients are < * Double.MAX_VALUE is 1029. If the computed value exceeds Double.MAX_VALUE, * Double.POSITIVE_INFINITY is returned</li> * </ul></p> * * @param n the size of the set * @param k the size of the subsets to be counted * @return <code>n choose k</code> * @throws IllegalArgumentException if preconditions are not met. */ public static double binomialCoefficientDouble(final int n, final int k) { return Math.floor(Math.exp(binomialCoefficientLog(n, k)) + 0.5); }
From source file:Main.java
/** * Normalize an angle in a 2&pi wide interval around a center value. * This method has three main uses://from w w w.ja v a 2 s.c o m * <ul> * <li>normalize an angle between 0 and 2π:<br/> * <code>a = MathUtils.normalizeAngle(a, Math.PI);</code></li> * <li>normalize an angle between -π and +π<br/> * <code>a = MathUtils.normalizeAngle(a, 0.0);</code></li> * <li>compute the angle between two defining angular positions:<br> * <code>angle = MathUtils.normalizeAngle(end, start) - start;</code></li> * </ul> * Note that due to numerical accuracy and since π cannot be represented * exactly, the result interval is <em>closed</em>, it cannot be half-closed * as would be more satisfactory in a purely mathematical view. * @param a angle to normalize * @param center center of the desired 2π interval for the result * @return a-2kπ with integer k and center-π <= a-2kπ <= center+π * @since 1.2 */ public static double normalizeAngle(double a, double center) { return a - TWO_PI * Math.floor((a + Math.PI - center) / TWO_PI); }
From source file:Main.java
/** * Splits a collection in <tt>n</tt> new collections. The last collection may contain more elements than the others * if {@code set.size() % n != 0}./* w w w .j a va 2 s . com*/ * * @param set the input collection * @param n the number of new collections * @param <T> the element type * @return a list containing the new collections */ public static <T> List<T>[] split(Collection<T> set, int n) { if (set.size() >= n) { @SuppressWarnings("unchecked") List<T>[] arrays = new List[n]; int minSegmentSize = (int) Math.floor(set.size() / (double) n); int start = 0; int stop = minSegmentSize; Iterator<T> it = set.iterator(); for (int i = 0; i < n - 1; i++) { int segmentSize = stop - start; List<T> segment = new ArrayList<T>(segmentSize); for (int k = 0; k < segmentSize; k++) { segment.add(it.next()); } arrays[i] = segment; start = stop; stop += segmentSize; } int segmentSize = set.size() - start; List<T> segment = new ArrayList<T>(segmentSize); for (int k = 0; k < segmentSize; k++) { segment.add(it.next()); } arrays[n - 1] = segment; return arrays; } else { throw new IllegalArgumentException("n must not be smaller set size!"); } }
From source file:Main.java
/** * Returns the closest power-of-two number less than or equal to x. * /*from w w w . j a va 2 s. c o m*/ * @param x * @return the closest power-of-two number less then or equal to x */ public static int prevPow2(int x) { if (x < 1) throw new IllegalArgumentException("x must be greater or equal 1"); return (int) Math.pow(2, Math.floor(Math.log(x) / Math.log(2))); }
From source file:Rnd.java
/** * @param n//from w w w. j a v a2 s . c om * @return n */ public static int nextInt(int n) { return (int) Math.floor(rnd.nextDouble() * n); }
From source file:com.martinkampjensen.thesis.barriers.neighborhood.AbstractNeighborhood.java
public static final int[][] calculateNeighbors(List<Model> models, Neighborhood neighborhood, double maxDistance, boolean allowDebugPrints) { final int nModels = models.size(); final IntList[] lists = new ArrayIntList[nModels]; final int[][] arrays = new int[nModels][]; for (int i = 0; i < nModels; i++) { lists[i] = new ArrayIntList(); }/*from w ww . j av a 2 s . c om*/ // For status. final long calculationsTotal = (long) nModels * (nModels - 1) / 2; final boolean performStatus = (calculationsTotal >= 2500000); final long calculationsTwentieth = Math.max(1, (long) Math.floor(calculationsTotal / 20d)); long calculationsMilestone = calculationsTwentieth; long calculationsDone = 0; if (performStatus) System.err.print("Neighbors calculated: [0%"); for (int i = 0; i < nModels; i++) { final IntList list = lists[i]; final Model first = models.get(i); for (int j = i + 1; j < nModels; j++) { final Model second = models.get(j); if (neighborhood.isNeighbors(first, second, maxDistance)) { list.add(j); lists[j].add(i); } } arrays[i] = list.toArray(); lists[i] = null; // For garbage collection. // For status. if (performStatus) { calculationsDone += nModels - 1 - i; if (calculationsDone >= calculationsMilestone && i != nModels - 1) { System.err.print("..." + (int) (100 * calculationsDone / (double) calculationsTotal) + "%"); if (calculationsDone == calculationsTotal) System.err.println("]"); while (calculationsDone >= calculationsMilestone) calculationsMilestone += calculationsTwentieth; calculationsMilestone = Math.min(calculationsMilestone, calculationsTotal); } } } if (allowDebugPrints) calculateNeighborMeasures(arrays); return arrays; }
From source file:Main.java
/** * Works on arrys instead of collections * @param array/*from w w w . j a va 2s. c o m*/ * @param n * @param <T> * @return a list of arrays */ public static <T> List<T[]> split(T[] array, int n) { Class theClass = array[0].getClass(); if (array.length >= n) { @SuppressWarnings("unchecked") List<T[]> out = new ArrayList<>(); int minSegmentSize = (int) Math.floor(array.length / (double) n); int start = 0; int stop = minSegmentSize; for (int i = 0; i < n - 1; i++) { int segmentSize = stop - start; T[] segment = (T[]) Array.newInstance(theClass, segmentSize); int j = 0; for (int k = start; k < stop; k++) { segment[j] = array[k]; j++; } out.add(segment); start = stop; stop += segmentSize; } int segmentSize = array.length - start; stop = start + segmentSize; T[] segment = (T[]) Array.newInstance(theClass, segmentSize); int j = 0; for (int k = start; k < stop; k++) { segment[j] = array[k]; j++; } out.add(segment); return out; } else { throw new IllegalArgumentException("n must not be smaller set size!"); } }
From source file:edu.columbia.sel.grout.util.TileUtils.java
/** * For a description see:/*from www.jav a 2s .co m*/ * * @see http://wiki.openstreetmap.org/index.php/Slippy_map_tilenames For a * code-description see: * @see http://wiki.openstreetmap.org/index.php/Slippy_map_tilenames# * compute_bounding_box_for_tile_number * @param aLat * latitude to get the {@link OSMTileInfo} for. * @param aLon * longitude to get the {@link OSMTileInfo} for. * @return The {@link OSMTileInfo} providing 'x' 'y' and 'z'(oom) for the * coordinates passed. */ public static OSMTileInfo getMapTileFromCoordinates(final double aLat, final double aLon, final int zoom) { final int y = (int) Math .floor((1 - Math.log(Math.tan(aLat * Math.PI / 180) + 1 / Math.cos(aLat * Math.PI / 180)) / Math.PI) / 2 * (1 << zoom)); final int x = (int) Math.floor((aLon + 180) / 360 * (1 << zoom)); return new OSMTileInfo(x, y, zoom); }
From source file:Main.java
/** This method returns the largest double value that is smaller than * <code> d = x * 10<sup>exp</sup></code> where x is rounded down to * the closest integer.//w w w .j a va 2s . c o m * @param d the double value to be rounded * @param exp the exponent of 10 to which d should be rounded * @return <code> Math.floor(x) * 10<sup>exp</sup></code> */ public static double floor(double d, int exp) { double x = 1.0 * Math.pow(10.0, (double) exp); return Math.floor(d / x) * x; }
From source file:Main.java
public static int getDominantColor(Bitmap source, boolean applyThreshold) { if (source == null) return Color.argb(255, 255, 255, 255); // Keep track of how many times a hue in a given bin appears in the image. // Hue values range [0 .. 360), so dividing by 10, we get 36 bins. int[] colorBins = new int[36]; // The bin with the most colors. Initialize to -1 to prevent accidentally // thinking the first bin holds the dominant color. int maxBin = -1; // Keep track of sum hue/saturation/value per hue bin, which we'll use to // compute an average to for the dominant color. float[] sumHue = new float[36]; float[] sumSat = new float[36]; float[] sumVal = new float[36]; float[] hsv = new float[3]; int height = source.getHeight(); int width = source.getWidth(); int[] pixels = new int[width * height]; source.getPixels(pixels, 0, width, 0, 0, width, height); for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { int c = pixels[col + row * width]; // Ignore pixels with a certain transparency. if (Color.alpha(c) < 128) continue; Color.colorToHSV(c, hsv); // If a threshold is applied, ignore arbitrarily chosen values for "white" and "black". if (applyThreshold && (hsv[1] <= 0.35f || hsv[2] <= 0.35f)) continue; // We compute the dominant color by putting colors in bins based on their hue. int bin = (int) Math.floor(hsv[0] / 10.0f); // Update the sum hue/saturation/value for this bin. sumHue[bin] = sumHue[bin] + hsv[0]; sumSat[bin] = sumSat[bin] + hsv[1]; sumVal[bin] = sumVal[bin] + hsv[2]; // Increment the number of colors in this bin. colorBins[bin]++;/*ww w. j a va 2s . c om*/ // Keep track of the bin that holds the most colors. if (maxBin < 0 || colorBins[bin] > colorBins[maxBin]) maxBin = bin; } } // maxBin may never get updated if the image holds only transparent and/or black/white pixels. if (maxBin < 0) return Color.argb(255, 255, 255, 255); // Return a color with the average hue/saturation/value of the bin with the most colors. hsv[0] = sumHue[maxBin] / colorBins[maxBin]; hsv[1] = sumSat[maxBin] / colorBins[maxBin]; hsv[2] = sumVal[maxBin] / colorBins[maxBin]; return Color.HSVToColor(hsv); }