List of usage examples for org.apache.commons.math3.fraction BigFraction multiply
public BigFraction multiply(final BigFraction fraction)
Multiplies the value of this fraction by another, returning the result in reduced form.
From source file:gedi.util.math.stat.distributions.OccupancyNumberDistribution.java
private static final BigFraction rationalv(int a, int b, int k, int n) { BigFraction re = BigFraction.ONE; for (int i = n; i > n - a * b; i--) re = re.multiply(new BigFraction(i, 1)); BigFraction ba = new BigFraction(1, 1); for (int i = 1; i <= b; i++) ba = ba.multiply(new BigFraction(i, 1)); re = re.divide(a == 0 ? BigFraction.ONE : ba.pow(a)); BigFraction f1 = a * b == 0 ? BigFraction.ONE : new BigFraction(1, k).pow(a * b); BigFraction f2 = n - a * b == 0 ? BigFraction.ONE : new BigFraction(k - a, k).pow(n - a * b); return re.multiply(f1).multiply(f2); }
From source file:com.netflix.imfutility.util.ConversionHelper.java
/** * Transforms edit units to a timecode according to the given edit unit rate. * <ul>/* w w w . ja v a 2s. c om*/ * <li>An example of edit units is a frame.</li> * <li>The output timecode has the following format 'hh:mm:ss.xxx', where xxx is milliseconds.</li> * </ul> * * @param eu edit units to be transformed * @param unitsInSec edit unit rate * @return timecode as a string in "hh:mm:ss.mss" format. */ public static String editUnitToTimecode(BigInteger eu, BigFraction unitsInSec) { BigFraction editUnits = new BigFraction(eu); BigFraction unitsInMin = unitsInSec.multiply(new BigFraction(60)); BigFraction unitsInHour = unitsInSec.multiply(new BigFraction(60 * 60)); int hours = editUnits.divide(unitsInHour).intValue(); int minutes = editUnits.subtract(unitsInHour.multiply(hours)).divide(unitsInMin).intValue(); int seconds = editUnits.subtract(unitsInHour.multiply(hours)).subtract(unitsInMin.multiply(minutes)) .divide(unitsInSec).intValue(); BigFraction units = editUnits.subtract(unitsInHour.multiply(hours)).subtract(unitsInMin.multiply(minutes)) .subtract(unitsInSec.multiply(seconds)); int milliseconds = new BigFraction(1000).divide(unitsInSec).multiply(units).intValue(); return String.format("%02d:%02d:%02d.%03d", hours, minutes, seconds, milliseconds); }
From source file:com.netflix.imfutility.util.ConversionHelper.java
/** * Transforms milliseconds to an SMPTE timecode according to the given edit unit rate. * <ul>/* w w w. j a v a 2s . c o m*/ * <li>An example of edit units is a frame.</li> * <li>The output timecode has the following format 'hh:mm:ss:ff'.</li> * </ul> * * @param milliseconds milliseconds to be transformed * @param unitsInSec edit unit rate * @return timecode as a string in "hh:mm:ss:ff" format. */ public static String msToSmpteTimecode(long milliseconds, BigFraction unitsInSec) { BigFraction ms = new BigFraction(milliseconds); BigFraction msInMin = new BigFraction(60 * 1000); BigFraction msInHour = new BigFraction(60 * 60 * 1000); BigFraction msInSec = new BigFraction(1000); BigFraction unitsInMs = unitsInSec.divide(msInSec); int hours = ms.divide(msInHour).intValue(); int minutes = ms.subtract(msInHour.multiply(hours)).divide(msInMin).intValue(); int seconds = ms.subtract(msInHour.multiply(hours)).subtract(msInMin.multiply(minutes)).divide(msInSec) .intValue(); int units = ms.subtract(msInHour.multiply(hours)).subtract(msInMin.multiply(minutes)) .subtract(msInSec.multiply(seconds)).multiply(unitsInMs).intValue(); return String.format("%02d:%02d:%02d:%02d", hours, minutes, seconds, units); }
From source file:cc.redberry.core.number.Rational.java
@Override public Rational multiply(BigFraction fraction) { NumberUtils.checkNotNull(fraction);/*from www . j a va 2 s. c o m*/ return new Rational(fraction.multiply(fraction)); }
From source file:com.netflix.imfutility.itunes.ITunesFormatBuilder.java
@Override protected void doBuildDestContext() { DestTemplateParameterContext destContext = contextProvider.getDestContext(); // set interlaced to false if not specified String interlaced = destContext.getParameterValue(INTERLACED); destContext.addParameter(INTERLACED, interlaced == null ? Boolean.FALSE.toString() : interlaced); // define is dar provided destContext.addParameter(DEST_PARAM_VIDEO_IS_DAR_SPECIFIED, Boolean.toString(destContext.getParameterValue(DAR) != null)); // set frame rate for interlaced scan // for ffmpeg iFrameRate=frameRate*2 // for prenc iFrameRate=frameRate BigFraction iFrameRate = ConversionHelper.parseEditRate(destContext.getParameterValue(FRAME_RATE)); if (!SystemUtils.IS_OS_MAC_OSX) { iFrameRate = iFrameRate.multiply(2); }// ww w . j a v a 2 s. c o m destContext.addParameter(DEST_PARAM_VIDEO_IFRAME_RATE, ConversionHelper.toREditRate(iFrameRate)); }
From source file:cz.cuni.mff.spl.evaluator.statistics.KolmogorovSmirnovTestFlag.java
/** * Calculates the exact value of {@code P(D_n < d)} using the method described in [1] (reference * in class javadoc above) and {@link org.apache.commons.math3.fraction.BigFraction} (see * above).// w w w. j av a2 s.c o m * * @param d statistic * @param n sample size * @return the two-sided probability of \(P(D_n < d)\) * @throws MathArithmeticException if algorithm fails to convert {@code h} to a * {@link org.apache.commons.math3.fraction.BigFraction} in expressing {@code d} as \((k * - h) / m\) for integer {@code k, m} and \(0 \le h < 1\). */ private double exactK(double d, int n) throws MathArithmeticException { final int k = (int) Math.ceil(n * d); final FieldMatrix<BigFraction> H = this.createExactH(d, n); final FieldMatrix<BigFraction> Hpower = H.power(n); BigFraction pFrac = Hpower.getEntry(k - 1, k - 1); for (int i = 1; i <= n; ++i) { pFrac = pFrac.multiply(i).divide(n); } /* * BigFraction.doubleValue converts numerator to double and the denominator to double and * divides afterwards. That gives NaN quite easy. This does not (scale is the number of * digits): */ return pFrac.bigDecimalValue(20, BigDecimal.ROUND_HALF_UP).doubleValue(); }
From source file:cz.cuni.mff.spl.evaluator.statistics.KolmogorovSmirnovTestFlag.java
/*** * Creates {@code H} of size {@code m x m} as described in [1] (see above). * * @param d statistic/*from w w w . j a v a2 s . c o m*/ * @param n sample size * @return H matrix * @throws NumberIsTooLargeException if fractional part is greater than 1 * @throws FractionConversionException if algorithm fails to convert {@code h} to a * {@link org.apache.commons.math3.fraction.BigFraction} in expressing {@code d} as \((k * - h) / m\) for integer {@code k, m} and \(0 <= h < 1\). */ private FieldMatrix<BigFraction> createExactH(double d, int n) throws NumberIsTooLargeException, FractionConversionException { final int k = (int) Math.ceil(n * d); final int m = 2 * k - 1; final double hDouble = k - n * d; if (hDouble >= 1) { throw new NumberIsTooLargeException(hDouble, 1.0, false); } BigFraction h = null; try { h = new BigFraction(hDouble, 1.0e-20, 10000); } catch (final FractionConversionException e1) { try { h = new BigFraction(hDouble, 1.0e-10, 10000); } catch (final FractionConversionException e2) { h = new BigFraction(hDouble, 1.0e-5, 10000); } } final BigFraction[][] Hdata = new BigFraction[m][m]; /* * Start by filling everything with either 0 or 1. */ for (int i = 0; i < m; ++i) { for (int j = 0; j < m; ++j) { if (i - j + 1 < 0) { Hdata[i][j] = BigFraction.ZERO; } else { Hdata[i][j] = BigFraction.ONE; } } } /* * Setting up power-array to avoid calculating the same value twice: hPowers[0] = h^1 ... * hPowers[m-1] = h^m */ final BigFraction[] hPowers = new BigFraction[m]; hPowers[0] = h; for (int i = 1; i < m; ++i) { hPowers[i] = h.multiply(hPowers[i - 1]); } /* * First column and last row has special values (each other reversed). */ for (int i = 0; i < m; ++i) { Hdata[i][0] = Hdata[i][0].subtract(hPowers[i]); Hdata[m - 1][i] = Hdata[m - 1][i].subtract(hPowers[m - i - 1]); } /* * [1] states: "For 1/2 < h < 1 the bottom left element of the matrix should be (1 - 2*h^m + * (2h - 1)^m )/m!" Since 0 <= h < 1, then if h > 1/2 is sufficient to check: */ if (h.compareTo(BigFraction.ONE_HALF) == 1) { Hdata[m - 1][0] = Hdata[m - 1][0].add(h.multiply(2).subtract(1).pow(m)); } /* * Aside from the first column and last row, the (i, j)-th element is 1/(i - j + 1)! if i - * j + 1 >= 0, else 0. 1's and 0's are already put, so only division with (i - j + 1)! is * needed in the elements that have 1's. There is no need to calculate (i - j + 1)! and then * divide - small steps avoid overflows. Note that i - j + 1 > 0 <=> i + 1 > j instead of * j'ing all the way to m. Also note that it is started at g = 2 because dividing by 1 isn't * really necessary. */ for (int i = 0; i < m; ++i) { for (int j = 0; j < i + 1; ++j) { if (i - j + 1 > 0) { for (int g = 2; g <= i - j + 1; ++g) { Hdata[i][j] = Hdata[i][j].divide(g); } } } } return new Array2DRowFieldMatrix<BigFraction>(BigFractionField.getInstance(), Hdata); }