/** * Returns the base 2 logarithm of a double value. * * <p>Special cases: * * <ul> * <li>If {@code x} is NaN or less than zero, the result is NaN. * <li>If {@code x} is positive infinity, the result is positive infinity. * <li>If {@code x} is positive or negative zero, the result is negative infinity. * </ul> * * <p>The computed result is within 1 ulp of the exact result. * * <p>If the result of this method will be immediately rounded to an {@code int}, {@link * #log2(double, RoundingMode)} is faster. */ public static double log2(double x) { return log(x) / LN_2; // surprisingly within 1 ulp according to tests }
/** * Log Gamma Function * * @param Z * @return */ public static double LogGamma(double Z) { double S = 1.0 + 76.18009173 / Z - 86.50532033 / (Z + 1.0) + 24.01409822 / (Z + 2.0) - 1.231739516 / (Z + 3.0) + 0.00120858003 / (Z + 4.0) - 0.00000536382 / (Z + 5.0); double LG = (Z - 0.5) * Math.log(Z + 4.5) - (Z + 4.5) + Math.log(S * 2.50662827465); return LG; }
protected void toLog() { if (start_probability == null || transition_probability == null || emission_probability == null) return; for (int i = 0; i < start_probability.length; i++) { start_probability[i] = (float) Math.log(start_probability[i]); for (int j = 0; j < start_probability.length; j++) transition_probability[i][j] = (float) Math.log(transition_probability[i][j]); for (int j = 0; j < emission_probability[0].length; j++) emission_probability[i][j] = (float) Math.log(emission_probability[i][j]); } }
/** * 初始化 * @param outcomeLabels */ public void setLabels(String[] outcomeLabels) { this.numOutcomes = outcomeLabels.length; r = Math.log(1.0 / numOutcomes); } }
// raw estimation how many output digits we will need. // This is just enough in cases like BASE-1, and up to // 30 digits (for base 2) too much for something like (1,0,0). int len = (int) (Math.log(BASE) / Math.log(radix) * digits.length)+1; int[] rDigits = new int[len]; int rIndex = len-1; int[] current = digits; int quotLen = digits.length;
public static String humanReadableByteCount(long bytes, boolean si) { int unit = si ? 1000 : 1024; if (bytes < unit) return bytes + " B"; int exp = (int) (Math.log(bytes) / Math.log(unit)); String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp-1) + (si ? "" : "i"); return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre); }
/** * Computes m (total bits of Bloom filter) which is expected to achieve, for the specified * expected insertions, the required false positive probability. * * <p>See http://en.wikipedia.org/wiki/Bloom_filter#Probability_of_false_positives for the * formula. * * @param n expected insertions (must be positive) * @param p false positive rate (must be 0 < p < 1) */ @VisibleForTesting static long optimalNumOfBits(long n, double p) { if (p == 0) { p = Double.MIN_VALUE; } return (long) (-n * Math.log(p) / (Math.log(2) * Math.log(2))); }
public ClusterConnectionStates(long reconnectBackoffMs, long reconnectBackoffMaxMs) { this.reconnectBackoffInitMs = reconnectBackoffMs; this.reconnectBackoffMaxMs = reconnectBackoffMaxMs; this.reconnectBackoffMaxExp = Math.log(this.reconnectBackoffMaxMs / (double) Math.max(reconnectBackoffMs, 1)) / Math.log(RECONNECT_BACKOFF_EXP_BASE); this.nodeState = new HashMap<>(); }
/** * Computes the optimal k (number of hashes per element inserted in Bloom filter), given the * expected insertions and total number of bits in the Bloom filter. * * <p>See http://en.wikipedia.org/wiki/File:Bloom_filter_fp_probability.svg for the formula. * * @param n expected insertions (must be positive) * @param m total number of bits in Bloom filter (must be positive) */ @VisibleForTesting static int optimalNumOfHashFunctions(long n, long m) { // (m / n) * log(2), but avoid truncation due to division! return Math.max(1, (int) Math.round((double) m / n * Math.log(2))); }
private float computeEntropy(Map<Character, int[]> storage) { float sum = 0; for (Map.Entry<Character, int[]> entry : storage.entrySet()) { float p = entry.getValue()[0] / (float) frequency; sum -= p * Math.log(p); } return sum; }
public BinarySearch (int min, int max, int fuzziness, boolean pot, boolean mod4) { if (pot) { this.min = (int)(Math.log(MathUtils.nextPowerOfTwo(min)) / Math.log(2)); this.max = (int)(Math.log(MathUtils.nextPowerOfTwo(max)) / Math.log(2)); } else if (mod4) { this.min = min % 4 == 0 ? min : min + 4 - (min % 4); this.max = max % 4 == 0 ? max : max + 4 - (max % 4); } else { this.min = min; this.max = max; } this.fuzziness = pot ? 0 : fuzziness; this.pot = pot; this.mod4 = mod4; }
public BinarySearch (int min, int max, int fuzziness, boolean pot, boolean mod4) { if (pot) { this.min = (int)(Math.log(MathUtils.nextPowerOfTwo(min)) / Math.log(2)); this.max = (int)(Math.log(MathUtils.nextPowerOfTwo(max)) / Math.log(2)); } else if (mod4) { this.min = min % 4 == 0 ? min : min + 4 - (min % 4); this.max = max % 4 == 0 ? max : max + 4 - (max % 4); } else { this.min = min; this.max = max; } this.fuzziness = pot ? 0 : fuzziness; this.pot = pot; this.mod4 = mod4; }
public double computeMutualInformation(String first, String second) { return Math.log(Math.max(Predefine.MIN_PROBABILITY, getPairFrequency(first, second) / (totalPair / 2)) / Math.max(Predefine.MIN_PROBABILITY, (getTermFrequency(first) / totalTerm * getTermFrequency(second) / totalTerm))); }
public double computeMutualInformation(PairFrequency pair) { return Math.log(Math.max(Predefine.MIN_PROBABILITY, pair.getValue() / totalPair) / Math.max(Predefine.MIN_PROBABILITY, (CoreDictionary.getTermFrequency(pair.first) / (double) CoreDictionary.totalFrequency * CoreDictionary.getTermFrequency(pair.second) / (double) CoreDictionary.totalFrequency))); }
@Description("natural logarithm") @ScalarFunction @SqlType(StandardTypes.DOUBLE) public static double ln(@SqlType(StandardTypes.DOUBLE) double num) { return Math.log(num); }
@GwtIncompatible public void testResistsHashFloodingOnContains() { CallsCounter smallCounter = new CallsCounter(); List<CountsHashCodeAndEquals> haveSameHashesSmall = createAdversarialInput(10, smallCounter); ImmutableSet<?> smallSet = ConstructionPathway.COPY_OF_LIST.create(haveSameHashesSmall); long worstCaseOpsSmall = worstCaseQueryOperations(smallSet, smallCounter); CallsCounter largeCounter = new CallsCounter(); List<CountsHashCodeAndEquals> haveSameHashesLarge = createAdversarialInput(15, largeCounter); ImmutableSet<?> largeSet = ConstructionPathway.COPY_OF_LIST.create(haveSameHashesLarge); long worstCaseOpsLarge = worstCaseQueryOperations(largeSet, largeCounter); double ratio = (double) worstCaseOpsLarge / worstCaseOpsSmall; int smallSize = haveSameHashesSmall.size(); int largeSize = haveSameHashesLarge.size(); assertThat(ratio) .named( "ratio of equals/hashCode/compareTo operations to worst-case query an ImmutableSet " + "of size %s versus size %s", haveSameHashesLarge.size(), haveSameHashesSmall.size()) .isAtMost(2 * Math.log(largeSize) / Math.log(smallSize)); // We allow up to 2x wobble in the constant factors. }
@InputFunction public static void input(@AggregationState LongAndDoubleState state, @SqlType(StandardTypes.DOUBLE) double value) { state.setLong(state.getLong() + 1); state.setDouble(state.getDouble() + Math.log(value)); }