Example usage for org.apache.commons.math3.linear FieldMatrix power

List of usage examples for org.apache.commons.math3.linear FieldMatrix power

Introduction

In this page you can find the example usage for org.apache.commons.math3.linear FieldMatrix power.

Prototype

FieldMatrix<T> power(final int p) throws NonSquareMatrixException, NotPositiveException;

Source Link

Document

Returns the result multiplying this with itself p times.

Usage

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)./*from   ww w .j ava 2s  .co  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();
}