Example usage for org.apache.commons.math3.exception MathInternalError MathInternalError

List of usage examples for org.apache.commons.math3.exception MathInternalError MathInternalError

Introduction

In this page you can find the example usage for org.apache.commons.math3.exception MathInternalError MathInternalError.

Prototype

public MathInternalError() 

Source Link

Document

Simple constructor.

Usage

From source file:cz.cuni.mff.spl.evaluator.statistics.KolmogorovSmirnovTestFlag.java

/**
 * If there are no ties in the combined dataset formed from x and y, this
 * method is a no-op.  If there are ties, a uniform random deviate in
 * (-minDelta / 2, minDelta / 2) - {0} is added to each value in x and y, where
 * minDelta is the minimum difference between unequal values in the combined
 * sample.  A fixed seed is used to generate the jitter, so repeated activations
 * with the same input arrays result in the same values.
 *
 * NOTE: if there are ties in the data, this method overwrites the data in
 * x and y with the jittered values.//from  w ww .  j ava  2  s. c  o m
 *
 * @param x first sample
 * @param y second sample
 */
private static void fixTies(double[] x, double[] y) {
    final double[] values = MathArrays.unique(MathArrays.concatenate(x, y));
    if (values.length == x.length + y.length) {
        return; // There are no ties
    }

    // Find the smallest difference between values, or 1 if all values are the same
    double minDelta = 1;
    double prev = values[0];
    double delta = 1;
    for (int i = 1; i < values.length; i++) {
        delta = prev - values[i];
        if (delta < minDelta) {
            minDelta = delta;
        }
        prev = values[i];
    }
    minDelta /= 2;

    // Add jitter using a fixed seed (so same arguments always give same results),
    // low-initialization-overhead generator
    final RealDistribution dist = new UniformRealDistribution(new JDKRandomGenerator(100), -minDelta, minDelta);

    // It is theoretically possible that jitter does not break ties, so repeat
    // until all ties are gone.  Bound the loop and throw MIE if bound is exceeded.
    int ct = 0;
    boolean ties = true;
    do {
        jitter(x, dist);
        jitter(y, dist);
        ties = hasTies(x, y);
        ct++;
    } while (ties && ct < 1000);
    if (ties) {
        throw new MathInternalError(); // Should never happen
    }
}

From source file:org.gitools.analysis.groupcomparison.format.math33Preview.Combinations.java

/**
 * {@inheritDoc}//from   w  w w .  jav a  2 s  . c o  m
 */
public Iterator<int[]> iterator() {
    if (k == 0 || k == n) {
        return new SingletonIterator(MathArrays.natural(k));
    }

    switch (iterationOrder) {
    case LEXICOGRAPHIC:
        return new LexicographicIterator(n, k);
    default:
        throw new MathInternalError(); // Should never happen.
    }
}

From source file:org.gitools.analysis.groupcomparison.format.math33Preview.MathArrays.java

/**
 * Check that an array is monotonically increasing or decreasing.
 *
 * @param <T>    the type of the elements in the specified array
 * @param val    Values.//from   w ww .  j  a v a  2s  .c  om
 * @param dir    Ordering direction.
 * @param strict Whether the order should be strict.
 * @return {@code true} if sorted, {@code false} otherwise.
 */
public static <T extends Comparable<? super T>> boolean isMonotonic(T[] val, OrderDirection dir,
        boolean strict) {
    T previous = val[0];
    final int max = val.length;
    for (int i = 1; i < max; i++) {
        final int comp;
        switch (dir) {
        case INCREASING:
            comp = previous.compareTo(val[i]);
            if (strict) {
                if (comp >= 0) {
                    return false;
                }
            } else {
                if (comp > 0) {
                    return false;
                }
            }
            break;
        case DECREASING:
            comp = val[i].compareTo(previous);
            if (strict) {
                if (comp >= 0) {
                    return false;
                }
            } else {
                if (comp > 0) {
                    return false;
                }
            }
            break;
        default:
            // Should never happen.
            throw new MathInternalError();
        }

        previous = val[i];
    }
    return true;
}

From source file:org.gitools.analysis.groupcomparison.format.math33Preview.MathArrays.java

/**
 * Check that the given array is sorted.
 *
 * @param val    Values./*w w  w .j a va2s . c o m*/
 * @param dir    Ordering direction.
 * @param strict Whether the order should be strict.
 * @param abort  Whether to throw an exception if the check fails.
 * @return {@code true} if the array is sorted.
 * @throws NonMonotonicSequenceException if the array is not sorted
 *                                       and {@code abort} is {@code true}.
 */
public static boolean checkOrder(double[] val, OrderDirection dir, boolean strict, boolean abort)
        throws NonMonotonicSequenceException {
    double previous = val[0];
    final int max = val.length;

    int index;
    ITEM: for (index = 1; index < max; index++) {
        switch (dir) {
        case INCREASING:
            if (strict) {
                if (val[index] <= previous) {
                    break ITEM;
                }
            } else {
                if (val[index] < previous) {
                    break ITEM;
                }
            }
            break;
        case DECREASING:
            if (strict) {
                if (val[index] >= previous) {
                    break ITEM;
                }
            } else {
                if (val[index] > previous) {
                    break ITEM;
                }
            }
            break;
        default:
            // Should never happen.
            throw new MathInternalError();
        }

        previous = val[index];
    }

    if (index == max) {
        // Loop completed.
        return true;
    }

    // Loop early exit means wrong ordering.
    if (abort) {
        throw new RuntimeException("I cannot throw NonMonotonicSequenceException");
        //throw new NonMonotonicSequenceException(val[index], previous, index, dir, strict);
    } else {
        return false;
    }
}

From source file:org.gitools.analysis.groupcomparison.format.math33Preview.MathArrays.java

/**
 * Shuffle the entries of the given array, using the
 * <a href="http://en.wikipedia.org/wiki/FisherYates_shuffle#The_modern_algorithm">
 * FisherYates</a> algorithm./*from w  ww .j a v a2 s . c  o  m*/
 * The {@code start} and {@code pos} parameters select which portion
 * of the array is randomized and which is left untouched.
 *
 * @param list  Array whose entries will be shuffled (in-place).
 * @param start Index at which shuffling begins.
 * @param pos   Shuffling is performed for index positions between
 *              {@code start} and either the end (if {@link Position#TAIL})
 *              or the beginning (if {@link Position#HEAD}) of the array.
 * @param rng   Random number generator.
 */
public static void shuffle(int[] list, int start, Position pos, RandomGenerator rng) {
    switch (pos) {
    case TAIL: {
        for (int i = list.length - 1; i >= start; i--) {
            final int target;
            if (i == start) {
                target = start;
            } else {
                // NumberIsTooLargeException cannot occur.
                target = new UniformIntegerDistribution(rng, start, i).sample();
            }
            final int temp = list[target];
            list[target] = list[i];
            list[i] = temp;
        }
    }
        break;
    case HEAD: {
        for (int i = 0; i <= start; i++) {
            final int target;
            if (i == start) {
                target = start;
            } else {
                // NumberIsTooLargeException cannot occur.
                target = new UniformIntegerDistribution(rng, i, start).sample();
            }
            final int temp = list[target];
            list[target] = list[i];
            list[i] = temp;
        }
    }
        break;
    default:
        throw new MathInternalError(); // Should never happen.
    }
}