Java Code Examples for org.apache.commons.math3.exception.util.LocalizedFormats#PERMUTATION_SIZE

The following examples show how to use org.apache.commons.math3.exception.util.LocalizedFormats#PERMUTATION_SIZE . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RandomDataGenerator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * This method calls {@link MathArrays#shuffle(int[],RandomGenerator)
 * MathArrays.shuffle} in order to create a random shuffle of the set
 * of natural numbers {@code { 0, 1, ..., n - 1 }}.
 *
 * @throws NumberIsTooLargeException if {@code k > n}.
 * @throws NotStrictlyPositiveException if {@code k <= 0}.
 */
public int[] nextPermutation(int n, int k)
    throws NumberIsTooLargeException, NotStrictlyPositiveException {
    if (k > n) {
        throw new NumberIsTooLargeException(LocalizedFormats.PERMUTATION_EXCEEDS_N,
                                            k, n, true);
    }
    if (k <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.PERMUTATION_SIZE,
                                               k);
    }

    int[] index = MathArrays.natural(n);
    MathArrays.shuffle(index, getRandomGenerator());

    // Return a new array containing the first "k" entries of "index".
    return MathArrays.copyOf(index, k);
}
 
Example 2
Source File: RandomDataGenerator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>
 * Uses a 2-cycle permutation shuffle. The shuffling process is described <a
 * href="http://www.maths.abdn.ac.uk/~igc/tch/mx4002/notes/node83.html">
 * here</a>.
 * </p>
 * @throws NumberIsTooLargeException if {@code k > n}.
 * @throws NotStrictlyPositiveException if {@code k <= 0}.
 */
public int[] nextPermutation(int n, int k)
    throws NumberIsTooLargeException, NotStrictlyPositiveException {
    if (k > n) {
        throw new NumberIsTooLargeException(LocalizedFormats.PERMUTATION_EXCEEDS_N,
                                            k, n, true);
    }
    if (k <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.PERMUTATION_SIZE,
                                               k);
    }

    int[] index = getNatural(n);
    shuffle(index, n - k);
    int[] result = new int[k];
    for (int i = 0; i < k; i++) {
        result[i] = index[n - i - 1];
    }

    return result;
}
 
Example 3
Source File: RandomDataImpl.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>
 * Uses a 2-cycle permutation shuffle. The shuffling process is described <a
 * href="http://www.maths.abdn.ac.uk/~igc/tch/mx4002/notes/node83.html">
 * here</a>.
 * </p>
 */
public int[] nextPermutation(int n, int k) {
    if (k > n) {
        throw new NumberIsTooLargeException(LocalizedFormats.PERMUTATION_EXCEEDS_N,
                                            k, n, true);
    }
    if (k <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.PERMUTATION_SIZE,
                                               k);
    }

    int[] index = getNatural(n);
    shuffle(index, n - k);
    int[] result = new int[k];
    for (int i = 0; i < k; i++) {
        result[i] = index[n - i - 1];
    }

    return result;
}
 
Example 4
Source File: RandomDataGenerator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * This method calls {@link MathArrays#shuffle(int[],RandomGenerator)
 * MathArrays.shuffle} in order to create a random shuffle of the set
 * of natural numbers {@code { 0, 1, ..., n - 1 }}.
 *
 * @throws NumberIsTooLargeException if {@code k > n}.
 * @throws NotStrictlyPositiveException if {@code k <= 0}.
 */
public int[] nextPermutation(int n, int k)
    throws NumberIsTooLargeException, NotStrictlyPositiveException {
    if (k > n) {
        throw new NumberIsTooLargeException(LocalizedFormats.PERMUTATION_EXCEEDS_N,
                                            k, n, true);
    }
    if (k <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.PERMUTATION_SIZE,
                                               k);
    }

    int[] index = getNatural(n);
    MathArrays.shuffle(index, getRandomGenerator());

    // Return a new array containing the first "k" entries of "index".
    return MathArrays.copyOf(index, k);
}
 
Example 5
Source File: RandomDataImpl.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>
 * Uses a 2-cycle permutation shuffle. The shuffling process is described <a
 * href="http://www.maths.abdn.ac.uk/~igc/tch/mx4002/notes/node83.html">
 * here</a>.
 * </p>
 */
public int[] nextPermutation(int n, int k) {
    if (k > n) {
        throw new NumberIsTooLargeException(LocalizedFormats.PERMUTATION_EXCEEDS_N,
                                            k, n, true);
    }
    if (k <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.PERMUTATION_SIZE,
                                               k);
    }

    int[] index = getNatural(n);
    shuffle(index, n - k);
    int[] result = new int[k];
    for (int i = 0; i < k; i++) {
        result[i] = index[n - i - 1];
    }

    return result;
}
 
Example 6
Source File: RandomDataGenerator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>
 * Uses a 2-cycle permutation shuffle. The shuffling process is described <a
 * href="http://www.maths.abdn.ac.uk/~igc/tch/mx4002/notes/node83.html">
 * here</a>.
 * </p>
 * @throws NumberIsTooLargeException if {@code k > n}.
 * @throws NotStrictlyPositiveException if {@code k <= 0}.
 */
public int[] nextPermutation(int n, int k)
    throws NumberIsTooLargeException, NotStrictlyPositiveException {
    if (k > n) {
        throw new NumberIsTooLargeException(LocalizedFormats.PERMUTATION_EXCEEDS_N,
                                            k, n, true);
    }
    if (k <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.PERMUTATION_SIZE,
                                               k);
    }

    int[] index = getNatural(n);
    shuffle(index, n - k);
    int[] result = new int[k];
    for (int i = 0; i < k; i++) {
        result[i] = index[n - i - 1];
    }

    return result;
}
 
Example 7
Source File: RandomDataGenerator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>
 * Uses a 2-cycle permutation shuffle. The shuffling process is described <a
 * href="http://www.maths.abdn.ac.uk/~igc/tch/mx4002/notes/node83.html">
 * here</a>.
 * </p>
 * @throws NumberIsTooLargeException if {@code k > n}.
 * @throws NotStrictlyPositiveException if {@code k <= 0}.
 */
public int[] nextPermutation(int n, int k)
    throws NumberIsTooLargeException, NotStrictlyPositiveException {
    if (k > n) {
        throw new NumberIsTooLargeException(LocalizedFormats.PERMUTATION_EXCEEDS_N,
                                            k, n, true);
    }
    if (k <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.PERMUTATION_SIZE,
                                               k);
    }

    int[] index = getNatural(n);
    MathArrays.shuffle(index, getRandomGenerator());

    // Return a new array containing the first "k" entries of "index".
    return MathArrays.copyOf(index, k);
}
 
Example 8
Source File: RandomDataGenerator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * This method calls {@link MathArrays#shuffle(int[],RandomGenerator)
 * MathArrays.shuffle} in order to create a random shuffle of the set
 * of natural numbers {@code { 0, 1, ..., n - 1 }}.
 *
 * @throws NumberIsTooLargeException if {@code k > n}.
 * @throws NotStrictlyPositiveException if {@code k <= 0}.
 */
public int[] nextPermutation(int n, int k)
    throws NumberIsTooLargeException, NotStrictlyPositiveException {
    if (k > n) {
        throw new NumberIsTooLargeException(LocalizedFormats.PERMUTATION_EXCEEDS_N,
                                            k, n, true);
    }
    if (k <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.PERMUTATION_SIZE,
                                               k);
    }

    int[] index = MathArrays.natural(n);
    MathArrays.shuffle(index, getRandomGenerator());

    // Return a new array containing the first "k" entries of "index".
    return MathArrays.copyOf(index, k);
}