List of usage examples for java.lang Double isInfinite
public static boolean isInfinite(double v)
From source file:com.servoy.j2db.util.Utils.java
public static Object mapToNullIfUnmanageble(Object value) { if (value instanceof Undefined || value == Scriptable.NOT_FOUND) { return null; }//from w w w . j a va 2 s. c o m if (value instanceof Number) { if (Double.isNaN(((Number) value).doubleValue())) { return null; } if (Double.isInfinite(((Number) value).doubleValue())) { return null; } } return value; }
From source file:org.eclipse.january.dataset.DatasetUtils.java
/** * Removes NaNs and infinities from floating point datasets. * All other dataset types are ignored./*from w ww. j a v a2 s . com*/ * * @param a dataset * @param value replacement value */ public static void removeNansAndInfinities(Dataset a, final Number value) { if (a instanceof DoubleDataset) { final double dvalue = DTypeUtils.toReal(value); final DoubleDataset set = (DoubleDataset) a; final IndexIterator it = set.getIterator(); final double[] data = set.getData(); while (it.hasNext()) { double x = data[it.index]; if (Double.isNaN(x) || Double.isInfinite(x)) data[it.index] = dvalue; } } else if (a instanceof FloatDataset) { final float fvalue = (float) DTypeUtils.toReal(value); final FloatDataset set = (FloatDataset) a; final IndexIterator it = set.getIterator(); final float[] data = set.getData(); while (it.hasNext()) { float x = data[it.index]; if (Float.isNaN(x) || Float.isInfinite(x)) data[it.index] = fvalue; } } else if (a instanceof CompoundDoubleDataset) { final double dvalue = DTypeUtils.toReal(value); final CompoundDoubleDataset set = (CompoundDoubleDataset) a; final int is = set.getElementsPerItem(); final IndexIterator it = set.getIterator(); final double[] data = set.getData(); while (it.hasNext()) { for (int j = 0; j < is; j++) { double x = data[it.index + j]; if (Double.isNaN(x) || Double.isInfinite(x)) data[it.index + j] = dvalue; } } } else if (a instanceof CompoundFloatDataset) { final float fvalue = (float) DTypeUtils.toReal(value); final CompoundFloatDataset set = (CompoundFloatDataset) a; final int is = set.getElementsPerItem(); final IndexIterator it = set.getIterator(); final float[] data = set.getData(); while (it.hasNext()) { for (int j = 0; j < is; j++) { float x = data[it.index + j]; if (Float.isNaN(x) || Float.isInfinite(x)) data[it.index + j] = fvalue; } } } }
From source file:org.eclipse.dataset.CompoundLongDataset.java
@Override public CompoundLongDataset ipower(final Object b) { Dataset bds = b instanceof Dataset ? (Dataset) b : DatasetFactory.createFromObject(b); final int is = bds.getElementsPerItem(); if (bds.getSize() == 1) { final double vr = bds.getElementDoubleAbs(0); final IndexIterator it = getIterator(); if (bds.isComplex()) { final double vi = bds.getElementDoubleAbs(1); if (vi == 0) { while (it.hasNext()) { for (int i = 0; i < isize; i++) { final double v = Math.pow(data[it.index + i], vr); if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE data[it.index + i] = 0; // INT_USE } else { // INT_USE data[it.index + i] = (long) v; // PRIM_TYPE_LONG // ADD_CAST } // INT_USE }/*from w w w . j a v a2 s. c o m*/ } } else { final Complex zv = new Complex(vr, vi); while (it.hasNext()) { for (int i = 0; i < isize; i++) { Complex zd = new Complex(data[it.index + i], 0); final double v = zd.pow(zv).getReal(); if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE data[it.index + i] = 0; // INT_USE } else { // INT_USE data[it.index + i] = (long) v; // PRIM_TYPE_LONG // ADD_CAST } // INT_USE } } } } else if (is == 1) { while (it.hasNext()) { for (int i = 0; i < isize; i++) { final double v = Math.pow(data[it.index + i], vr); if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE data[it.index + i] = 0; // INT_USE } else { // INT_USE data[it.index + i] = (long) v; // PRIM_TYPE_LONG // ADD_CAST } // INT_USE } } } else if (is == isize) { while (it.hasNext()) { for (int i = 0; i < isize; i++) { final double v = Math.pow(data[it.index + i], bds.getElementDoubleAbs(i)); if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE data[it.index + i] = 0; // INT_USE } else { // INT_USE data[it.index + i] = (long) v; // PRIM_TYPE_LONG // ADD_CAST } // INT_USE } } } } else { final BroadcastIterator it = BroadcastIterator.createIterator(this, bds); it.setOutputDouble(true); if (bds.isComplex()) { while (it.hasNext()) { final Complex zv = new Complex(it.bDouble, bds.getElementDoubleAbs(it.bIndex + 1)); double v = new Complex(it.aDouble, 0).pow(zv).getReal(); if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE data[it.aIndex] = 0; // INT_USE } else { // INT_USE data[it.aIndex] = (long) v; // PRIM_TYPE_LONG // ADD_CAST } // INT_USE for (int i = 1; i < isize; i++) { v = new Complex(data[it.aIndex + i], 0).pow(zv).getReal(); if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE data[it.aIndex + i] = 0; // INT_USE } else { // INT_USE data[it.aIndex + i] = (long) v; // PRIM_TYPE_LONG // ADD_CAST } // INT_USE } } } else { while (it.hasNext()) { double v = Math.pow(it.aDouble, it.bDouble); if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE data[it.aIndex] = 0; // INT_USE } else { // INT_USE data[it.aIndex] = (long) v; // PRIM_TYPE_LONG // ADD_CAST } // INT_USE for (int i = 1; i < isize; i++) { v = Math.pow(data[it.aIndex + i], bds.getElementDoubleAbs(it.bIndex + i)); if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE data[it.aIndex + i] = 0; // INT_USE } else { // INT_USE data[it.aIndex + i] = (long) v; // PRIM_TYPE_LONG // ADD_CAST } // INT_USE } } } } setDirty(); return this; }
From source file:org.eclipse.dataset.CompoundByteDataset.java
@Override public CompoundByteDataset ipower(final Object b) { Dataset bds = b instanceof Dataset ? (Dataset) b : DatasetFactory.createFromObject(b); final int is = bds.getElementsPerItem(); if (bds.getSize() == 1) { final double vr = bds.getElementDoubleAbs(0); final IndexIterator it = getIterator(); if (bds.isComplex()) { final double vi = bds.getElementDoubleAbs(1); if (vi == 0) { while (it.hasNext()) { for (int i = 0; i < isize; i++) { final double v = Math.pow(data[it.index + i], vr); if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE data[it.index + i] = 0; // INT_USE } else { // INT_USE data[it.index + i] = (byte) (long) v; // PRIM_TYPE_LONG // ADD_CAST } // INT_USE }/*from www . j av a 2 s . c o m*/ } } else { final Complex zv = new Complex(vr, vi); while (it.hasNext()) { for (int i = 0; i < isize; i++) { Complex zd = new Complex(data[it.index + i], 0); final double v = zd.pow(zv).getReal(); if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE data[it.index + i] = 0; // INT_USE } else { // INT_USE data[it.index + i] = (byte) (long) v; // PRIM_TYPE_LONG // ADD_CAST } // INT_USE } } } } else if (is == 1) { while (it.hasNext()) { for (int i = 0; i < isize; i++) { final double v = Math.pow(data[it.index + i], vr); if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE data[it.index + i] = 0; // INT_USE } else { // INT_USE data[it.index + i] = (byte) (long) v; // PRIM_TYPE_LONG // ADD_CAST } // INT_USE } } } else if (is == isize) { while (it.hasNext()) { for (int i = 0; i < isize; i++) { final double v = Math.pow(data[it.index + i], bds.getElementDoubleAbs(i)); if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE data[it.index + i] = 0; // INT_USE } else { // INT_USE data[it.index + i] = (byte) (long) v; // PRIM_TYPE_LONG // ADD_CAST } // INT_USE } } } } else { final BroadcastIterator it = BroadcastIterator.createIterator(this, bds); it.setOutputDouble(true); if (bds.isComplex()) { while (it.hasNext()) { final Complex zv = new Complex(it.bDouble, bds.getElementDoubleAbs(it.bIndex + 1)); double v = new Complex(it.aDouble, 0).pow(zv).getReal(); if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE data[it.aIndex] = 0; // INT_USE } else { // INT_USE data[it.aIndex] = (byte) (long) v; // PRIM_TYPE_LONG // ADD_CAST } // INT_USE for (int i = 1; i < isize; i++) { v = new Complex(data[it.aIndex + i], 0).pow(zv).getReal(); if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE data[it.aIndex + i] = 0; // INT_USE } else { // INT_USE data[it.aIndex + i] = (byte) (long) v; // PRIM_TYPE_LONG // ADD_CAST } // INT_USE } } } else { while (it.hasNext()) { double v = Math.pow(it.aDouble, it.bDouble); if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE data[it.aIndex] = 0; // INT_USE } else { // INT_USE data[it.aIndex] = (byte) (long) v; // PRIM_TYPE_LONG // ADD_CAST } // INT_USE for (int i = 1; i < isize; i++) { v = Math.pow(data[it.aIndex + i], bds.getElementDoubleAbs(it.bIndex + i)); if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE data[it.aIndex + i] = 0; // INT_USE } else { // INT_USE data[it.aIndex + i] = (byte) (long) v; // PRIM_TYPE_LONG // ADD_CAST } // INT_USE } } } } setDirty(); return this; }
From source file:org.eclipse.dataset.CompoundShortDataset.java
@Override public CompoundShortDataset ipower(final Object b) { Dataset bds = b instanceof Dataset ? (Dataset) b : DatasetFactory.createFromObject(b); final int is = bds.getElementsPerItem(); if (bds.getSize() == 1) { final double vr = bds.getElementDoubleAbs(0); final IndexIterator it = getIterator(); if (bds.isComplex()) { final double vi = bds.getElementDoubleAbs(1); if (vi == 0) { while (it.hasNext()) { for (int i = 0; i < isize; i++) { final double v = Math.pow(data[it.index + i], vr); if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE data[it.index + i] = 0; // INT_USE } else { // INT_USE data[it.index + i] = (short) (long) v; // PRIM_TYPE_LONG // ADD_CAST } // INT_USE }/*from w w w.j av a 2 s . c o m*/ } } else { final Complex zv = new Complex(vr, vi); while (it.hasNext()) { for (int i = 0; i < isize; i++) { Complex zd = new Complex(data[it.index + i], 0); final double v = zd.pow(zv).getReal(); if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE data[it.index + i] = 0; // INT_USE } else { // INT_USE data[it.index + i] = (short) (long) v; // PRIM_TYPE_LONG // ADD_CAST } // INT_USE } } } } else if (is == 1) { while (it.hasNext()) { for (int i = 0; i < isize; i++) { final double v = Math.pow(data[it.index + i], vr); if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE data[it.index + i] = 0; // INT_USE } else { // INT_USE data[it.index + i] = (short) (long) v; // PRIM_TYPE_LONG // ADD_CAST } // INT_USE } } } else if (is == isize) { while (it.hasNext()) { for (int i = 0; i < isize; i++) { final double v = Math.pow(data[it.index + i], bds.getElementDoubleAbs(i)); if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE data[it.index + i] = 0; // INT_USE } else { // INT_USE data[it.index + i] = (short) (long) v; // PRIM_TYPE_LONG // ADD_CAST } // INT_USE } } } } else { final BroadcastIterator it = BroadcastIterator.createIterator(this, bds); it.setOutputDouble(true); if (bds.isComplex()) { while (it.hasNext()) { final Complex zv = new Complex(it.bDouble, bds.getElementDoubleAbs(it.bIndex + 1)); double v = new Complex(it.aDouble, 0).pow(zv).getReal(); if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE data[it.aIndex] = 0; // INT_USE } else { // INT_USE data[it.aIndex] = (short) (long) v; // PRIM_TYPE_LONG // ADD_CAST } // INT_USE for (int i = 1; i < isize; i++) { v = new Complex(data[it.aIndex + i], 0).pow(zv).getReal(); if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE data[it.aIndex + i] = 0; // INT_USE } else { // INT_USE data[it.aIndex + i] = (short) (long) v; // PRIM_TYPE_LONG // ADD_CAST } // INT_USE } } } else { while (it.hasNext()) { double v = Math.pow(it.aDouble, it.bDouble); if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE data[it.aIndex] = 0; // INT_USE } else { // INT_USE data[it.aIndex] = (short) (long) v; // PRIM_TYPE_LONG // ADD_CAST } // INT_USE for (int i = 1; i < isize; i++) { v = Math.pow(data[it.aIndex + i], bds.getElementDoubleAbs(it.bIndex + i)); if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE data[it.aIndex + i] = 0; // INT_USE } else { // INT_USE data[it.aIndex + i] = (short) (long) v; // PRIM_TYPE_LONG // ADD_CAST } // INT_USE } } } } setDirty(); return this; }
From source file:org.eclipse.dataset.CompoundIntegerDataset.java
@Override public CompoundIntegerDataset ipower(final Object b) { Dataset bds = b instanceof Dataset ? (Dataset) b : DatasetFactory.createFromObject(b); final int is = bds.getElementsPerItem(); if (bds.getSize() == 1) { final double vr = bds.getElementDoubleAbs(0); final IndexIterator it = getIterator(); if (bds.isComplex()) { final double vi = bds.getElementDoubleAbs(1); if (vi == 0) { while (it.hasNext()) { for (int i = 0; i < isize; i++) { final double v = Math.pow(data[it.index + i], vr); if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE data[it.index + i] = 0; // INT_USE } else { // INT_USE data[it.index + i] = (int) (long) v; // PRIM_TYPE_LONG // ADD_CAST } // INT_USE }//from w ww . j a v a2 s .co m } } else { final Complex zv = new Complex(vr, vi); while (it.hasNext()) { for (int i = 0; i < isize; i++) { Complex zd = new Complex(data[it.index + i], 0); final double v = zd.pow(zv).getReal(); if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE data[it.index + i] = 0; // INT_USE } else { // INT_USE data[it.index + i] = (int) (long) v; // PRIM_TYPE_LONG // ADD_CAST } // INT_USE } } } } else if (is == 1) { while (it.hasNext()) { for (int i = 0; i < isize; i++) { final double v = Math.pow(data[it.index + i], vr); if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE data[it.index + i] = 0; // INT_USE } else { // INT_USE data[it.index + i] = (int) (long) v; // PRIM_TYPE_LONG // ADD_CAST } // INT_USE } } } else if (is == isize) { while (it.hasNext()) { for (int i = 0; i < isize; i++) { final double v = Math.pow(data[it.index + i], bds.getElementDoubleAbs(i)); if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE data[it.index + i] = 0; // INT_USE } else { // INT_USE data[it.index + i] = (int) (long) v; // PRIM_TYPE_LONG // ADD_CAST } // INT_USE } } } } else { final BroadcastIterator it = BroadcastIterator.createIterator(this, bds); it.setOutputDouble(true); if (bds.isComplex()) { while (it.hasNext()) { final Complex zv = new Complex(it.bDouble, bds.getElementDoubleAbs(it.bIndex + 1)); double v = new Complex(it.aDouble, 0).pow(zv).getReal(); if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE data[it.aIndex] = 0; // INT_USE } else { // INT_USE data[it.aIndex] = (int) (long) v; // PRIM_TYPE_LONG // ADD_CAST } // INT_USE for (int i = 1; i < isize; i++) { v = new Complex(data[it.aIndex + i], 0).pow(zv).getReal(); if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE data[it.aIndex + i] = 0; // INT_USE } else { // INT_USE data[it.aIndex + i] = (int) (long) v; // PRIM_TYPE_LONG // ADD_CAST } // INT_USE } } } else { while (it.hasNext()) { double v = Math.pow(it.aDouble, it.bDouble); if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE data[it.aIndex] = 0; // INT_USE } else { // INT_USE data[it.aIndex] = (int) (long) v; // PRIM_TYPE_LONG // ADD_CAST } // INT_USE for (int i = 1; i < isize; i++) { v = Math.pow(data[it.aIndex + i], bds.getElementDoubleAbs(it.bIndex + i)); if (Double.isInfinite(v) || Double.isNaN(v)) { // INT_USE data[it.aIndex + i] = 0; // INT_USE } else { // INT_USE data[it.aIndex + i] = (int) (long) v; // PRIM_TYPE_LONG // ADD_CAST } // INT_USE } } } } setDirty(); return this; }
From source file:org.gitools.analysis.groupcomparison.format.math33Preview.MathArrays.java
/** * This method is used// w w w .j a va 2 s .com * to verify that the begin and length parameters designate a subarray of positive length * and the weights are all non-negative, non-NaN, finite, and not all zero. * <p> * <ul> * <li>returns <code>true</code> iff the parameters designate a subarray of * non-negative length and the weights array contains legitimate values.</li> * <li>throws <code>MathIllegalArgumentException</code> if any of the following are true: * <ul><li>the values array is null</li> * <li>the weights array is null</li> * <li>the weights array does not have the same length as the values array</li> * <li>the weights array contains one or more infinite values</li> * <li>the weights array contains one or more NaN values</li> * <li>the weights array contains negative values</li> * <li>the start and length arguments do not determine a valid array</li></ul> * </li> * <li>returns <code>false</li> if the array is non-null, but * <code>length</code> is 0 unless <code>allowEmpty</code> is <code>true</code>. * </ul></p> * * @param values the input array. * @param weights the weights array. * @param begin index of the first array element to include. * @param length the number of elements to include. * @param allowEmpty if {@code true} than allow zero length arrays to pass. * @return {@code true} if the parameters are valid. * @throws NullArgumentException if either of the arrays are null * @throws MathIllegalArgumentException if the array indices are not valid, * the weights array contains NaN, infinite or negative elements, or there * are no positive weights. * @since 3.3 */ public static boolean verifyValues(final double[] values, final double[] weights, final int begin, final int length, final boolean allowEmpty) throws MathIllegalArgumentException { if (weights == null || values == null) { throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY); } if (weights.length != values.length) { throw new DimensionMismatchException(weights.length, values.length); } boolean containsPositiveWeight = false; for (int i = begin; i < begin + length; i++) { final double weight = weights[i]; if (Double.isNaN(weight)) { throw new MathIllegalArgumentException(LocalizedFormats.NAN_ELEMENT_AT_INDEX, Integer.valueOf(i)); } if (Double.isInfinite(weight)) { throw new MathIllegalArgumentException(LocalizedFormats.INFINITE_ARRAY_ELEMENT, Double.valueOf(weight), Integer.valueOf(i)); } if (weight < 0) { throw new MathIllegalArgumentException(LocalizedFormats.NEGATIVE_ELEMENT_AT_INDEX, Integer.valueOf(i), Double.valueOf(weight)); } if (!containsPositiveWeight && weight > 0.0) { containsPositiveWeight = true; } } if (!containsPositiveWeight) { throw new MathIllegalArgumentException(LocalizedFormats.WEIGHT_AT_LEAST_ONE_NON_ZERO); } return verifyValues(values, begin, length, allowEmpty); }
From source file:org.eclipse.january.dataset.DatasetUtils.java
/** * Make floating point datasets contain only finite values. Infinities and NaNs are replaced * by +/- MAX_VALUE and 0, respectively. * All other dataset types are ignored.// w ww.j a v a2 s. c o m * * @param a dataset */ public static void makeFinite(Dataset a) { if (a instanceof DoubleDataset) { final DoubleDataset set = (DoubleDataset) a; final IndexIterator it = set.getIterator(); final double[] data = set.getData(); while (it.hasNext()) { final double x = data[it.index]; if (Double.isNaN(x)) data[it.index] = 0; else if (Double.isInfinite(x)) data[it.index] = x > 0 ? Double.MAX_VALUE : -Double.MAX_VALUE; } } else if (a instanceof FloatDataset) { final FloatDataset set = (FloatDataset) a; final IndexIterator it = set.getIterator(); final float[] data = set.getData(); while (it.hasNext()) { final float x = data[it.index]; if (Float.isNaN(x)) data[it.index] = 0; else if (Float.isInfinite(x)) data[it.index] = x > 0 ? Float.MAX_VALUE : -Float.MAX_VALUE; } } else if (a instanceof CompoundDoubleDataset) { final CompoundDoubleDataset set = (CompoundDoubleDataset) a; final int is = set.getElementsPerItem(); final IndexIterator it = set.getIterator(); final double[] data = set.getData(); while (it.hasNext()) { for (int j = 0; j < is; j++) { final double x = data[it.index + j]; if (Double.isNaN(x)) data[it.index + j] = 0; else if (Double.isInfinite(x)) data[it.index + j] = x > 0 ? Double.MAX_VALUE : -Double.MAX_VALUE; } } } else if (a instanceof CompoundFloatDataset) { final CompoundFloatDataset set = (CompoundFloatDataset) a; final int is = set.getElementsPerItem(); final IndexIterator it = set.getIterator(); final float[] data = set.getData(); while (it.hasNext()) { for (int j = 0; j < is; j++) { final float x = data[it.index + j]; if (Float.isNaN(x)) data[it.index + j] = 0; else if (Float.isInfinite(x)) data[it.index + j] = x > 0 ? Float.MAX_VALUE : -Float.MAX_VALUE; } } } }
From source file:org.bigtextml.topics.ParallelTopicModel.java
public double modelLogLikelihood() { double logLikelihood = 0.0; int nonZeroTopics; // The likelihood of the model is a combination of a // Dirichlet-multinomial for the words in each topic // and a Dirichlet-multinomial for the topics in each // document./*from w w w. j a v a 2s . c om*/ // The likelihood function of a dirichlet multinomial is // Gamma( sum_i alpha_i ) prod_i Gamma( alpha_i + N_i ) // prod_i Gamma( alpha_i ) Gamma( sum_i (alpha_i + N_i) ) // So the log likelihood is // logGamma ( sum_i alpha_i ) - logGamma ( sum_i (alpha_i + N_i) ) + // sum_i [ logGamma( alpha_i + N_i) - logGamma( alpha_i ) ] // Do the documents first int[] topicCounts = new int[numTopics]; double[] topicLogGammas = new double[numTopics]; int[] docTopics; for (int topic = 0; topic < numTopics; topic++) { topicLogGammas[topic] = Dirichlet.logGammaStirling(alpha[topic]); } for (int doc = 0; doc < data.size(); doc++) { //TopicAssignment document = (TopicAssignment) data.get(doc); //BigLabelSequence topicSequence = (BigLabelSequence) document.topicSequence; //docTopics = topicSequence.getFeatures(); BigLabelSequence topicSequence = data.getTopicSequenceObj(doc); docTopics = topicSequence.getFeatures(); for (int token = 0; token < docTopics.length; token++) { topicCounts[docTopics[token]]++; } for (int topic = 0; topic < numTopics; topic++) { if (topicCounts[topic] > 0) { logLikelihood += (Dirichlet.logGammaStirling(alpha[topic] + topicCounts[topic]) - topicLogGammas[topic]); } } // subtract the (count + parameter) sum term logLikelihood -= Dirichlet.logGammaStirling(alphaSum + docTopics.length); Arrays.fill(topicCounts, 0); } // add the parameter sum term logLikelihood += data.size() * Dirichlet.logGammaStirling(alphaSum); // And the topics // Count the number of type-topic pairs that are not just (logGamma(beta) - logGamma(beta)) int nonZeroTypeTopics = 0; for (int type = 0; type < numTypes; type++) { // reuse this array as a pointer topicCounts = typeTopicCounts[type]; int index = 0; while (index < topicCounts.length && topicCounts[index] > 0) { int topic = topicCounts[index] & topicMask; int count = topicCounts[index] >> topicBits; nonZeroTypeTopics++; logLikelihood += Dirichlet.logGammaStirling(beta + count); if (Double.isNaN(logLikelihood)) { logger.warning("NaN in log likelihood calculation"); return 0; } else if (Double.isInfinite(logLikelihood)) { logger.warning("infinite log likelihood"); return 0; } index++; } } for (int topic = 0; topic < numTopics; topic++) { logLikelihood -= Dirichlet.logGammaStirling((beta * numTypes) + tokensPerTopic[topic]); if (Double.isNaN(logLikelihood)) { logger.info("NaN after topic " + topic + " " + tokensPerTopic[topic]); return 0; } else if (Double.isInfinite(logLikelihood)) { logger.info("Infinite value after topic " + topic + " " + tokensPerTopic[topic]); return 0; } } // logGamma(|V|*beta) for every topic logLikelihood += Dirichlet.logGammaStirling(beta * numTypes) * numTopics; // logGamma(beta) for all type/topic pairs with non-zero count logLikelihood -= Dirichlet.logGammaStirling(beta) * nonZeroTypeTopics; if (Double.isNaN(logLikelihood)) { logger.info("at the end"); } else if (Double.isInfinite(logLikelihood)) { logger.info("Infinite value beta " + beta + " * " + numTypes); return 0; } return logLikelihood; }
From source file:net.pms.util.Rational.java
/** * Compares this {@link Rational} by value with any class implementing * {@link Number}.// ww w .ja v a 2 s .c o m * <p> * This method is provided in preference to individual methods for each of * the six boolean comparison operators ({@literal <}, ==, {@literal >}, * {@literal >=}, !=, {@literal <=}). The suggested idiom for performing * these comparisons is: {@code (x.compareTo(y)} <<i>op</i>> * {@code 0)}, where <<i>op</i>> is one of the six comparison * operators. * <p> * <b>Note:</b> {@code NaN} can't be compared by value and is considered * greater than anything but itself as defined for {@link Double}. * * @param number the {@link Number} to which this {@link Rational}'s value * is to be compared. * @return A negative integer, zero, or a positive integer as this * {@link Rational} is numerically less than, equal to, or greater * than {@code number}. */ public int compareTo(@Nonnull Number number) { // Establish special cases boolean numberIsNaN; boolean numberIsInfinite; int numberSignum; if (number instanceof Rational) { numberIsNaN = Rational.isNaN((Rational) number); numberIsInfinite = Rational.isInfinite((Rational) number); numberSignum = ((Rational) number).numerator.signum(); } else if (number instanceof Float) { numberIsNaN = Float.isNaN(number.floatValue()); numberIsInfinite = Float.isInfinite(number.floatValue()); numberSignum = (int) Math.signum(number.floatValue()); } else if (number instanceof Double) { numberIsNaN = Double.isNaN(number.doubleValue()); numberIsInfinite = Double.isInfinite(number.doubleValue()); numberSignum = (int) Math.signum(number.doubleValue()); } else { numberIsNaN = false; numberIsInfinite = false; long l = number.longValue(); numberSignum = l == 0 ? 0 : l > 0 ? 1 : -1; } // NaN comparison is done according to the rules for Double. if (isNaN()) { return numberIsNaN ? 0 : 1; } if (numberIsNaN) { return -1; } if (isInfinite()) { if (numberIsInfinite) { return signum() - numberSignum; } return this.signum(); } if (numberIsInfinite) { return -numberSignum; } // List known integer types for faster and more accurate comparison if (number instanceof BigInteger) { if (isInteger()) { return bigIntegerValue().compareTo((BigInteger) number); } return bigDecimalValue(2, RoundingMode.HALF_EVEN).compareTo(new BigDecimal((BigInteger) number)); } if (number instanceof AtomicInteger || number instanceof AtomicLong || number instanceof Byte || number instanceof Integer || number instanceof Long || number instanceof Short) { if (isInteger()) { return bigIntegerValue().compareTo(BigInteger.valueOf(number.longValue())); } return bigDecimalValue(2, RoundingMode.HALF_EVEN).compareTo(new BigDecimal(number.longValue())); } if (number instanceof BigDecimal) { Rational other = valueOf((BigDecimal) number); return compareTo(other); } return bigDecimalValue().compareTo(new BigDecimal(number.doubleValue())); }