org.apache.commons.math3.exception.ZeroException Java Examples

The following examples show how to use org.apache.commons.math3.exception.ZeroException. 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: QuaternionTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public final void testGetInverse() {
    final Quaternion q = new Quaternion(1.5, 4, 2, -2.5);

    final Quaternion inverseQ = q.getInverse();
    Assert.assertEquals(1.5 / 28.5, inverseQ.getQ0(), 0);
    Assert.assertEquals(-4.0 / 28.5, inverseQ.getQ1(), 0);
    Assert.assertEquals(-2.0 / 28.5, inverseQ.getQ2(), 0);
    Assert.assertEquals(2.5 / 28.5, inverseQ.getQ3(), 0);

    final Quaternion product = Quaternion.multiply(inverseQ, q);
    Assert.assertEquals(1, product.getQ0(), EPS);
    Assert.assertEquals(0, product.getQ1(), EPS);
    Assert.assertEquals(0, product.getQ2(), EPS);
    Assert.assertEquals(0, product.getQ3(), EPS);

    final Quaternion qNul = new Quaternion(0, 0, 0, 0);
    try {
        final Quaternion inverseQNul = qNul.getInverse();
        Assert.fail("expecting ZeroException but got : " + inverseQNul);
    } catch (ZeroException ex) {
        // expected
    }
}
 
Example #2
Source File: GaussianFitter.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Interpolates using the specified points to determine X at the
 * specified Y.
 *
 * @param points Points to use for interpolation.
 * @param startIdx Index within points from which to start the search for
 * interpolation bounds points.
 * @param idxStep Index step for searching interpolation bounds points.
 * @param y Y value for which X should be determined.
 * @return the value of X for the specified Y.
 * @throws ZeroException if {@code idxStep} is 0.
 * @throws OutOfRangeException if specified {@code y} is not within the
 * range of the specified {@code points}.
 */
private double interpolateXAtY(WeightedObservedPoint[] points,
                               int startIdx,
                               int idxStep,
                               double y)
    throws OutOfRangeException {
    if (idxStep == 0) {
        throw new ZeroException();
    }
    final WeightedObservedPoint[] twoPoints
        = getInterpolationPointsForY(points, startIdx, idxStep, y);
    final WeightedObservedPoint p1 = twoPoints[0];
    final WeightedObservedPoint p2 = twoPoints[1];
    if (p1.getY() == y) {
        return p1.getX();
    }
    if (p2.getY() == y) {
        return p2.getX();
    }
    return p1.getX() + (((y - p1.getY()) * (p2.getX() - p1.getX())) /
                        (p2.getY() - p1.getY()));
}
 
Example #3
Source File: GaussianCurveFitter.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Interpolates using the specified points to determine X at the
 * specified Y.
 *
 * @param points Points to use for interpolation.
 * @param startIdx Index within points from which to start the search for
 * interpolation bounds points.
 * @param idxStep Index step for searching interpolation bounds points.
 * @param y Y value for which X should be determined.
 * @return the value of X for the specified Y.
 * @throws ZeroException if {@code idxStep} is 0.
 * @throws OutOfRangeException if specified {@code y} is not within the
 * range of the specified {@code points}.
 */
private double interpolateXAtY(WeightedObservedPoint[] points,
                               int startIdx,
                               int idxStep,
                               double y)
    throws OutOfRangeException {
    if (idxStep == 0) {
        throw new ZeroException();
    }
    final WeightedObservedPoint[] twoPoints
        = getInterpolationPointsForY(points, startIdx, idxStep, y);
    final WeightedObservedPoint p1 = twoPoints[0];
    final WeightedObservedPoint p2 = twoPoints[1];
    if (p1.getY() == y) {
        return p1.getX();
    }
    if (p2.getY() == y) {
        return p2.getX();
    }
    return p1.getX() + (((y - p1.getY()) * (p2.getX() - p1.getX())) /
                        (p2.getY() - p1.getY()));
}
 
Example #4
Source File: QuaternionTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public final void testGetInverse() {
    final Quaternion q = new Quaternion(1.5, 4, 2, -2.5);

    final Quaternion inverseQ = q.getInverse();
    Assert.assertEquals(1.5 / 28.5, inverseQ.getQ0(), 0);
    Assert.assertEquals(-4.0 / 28.5, inverseQ.getQ1(), 0);
    Assert.assertEquals(-2.0 / 28.5, inverseQ.getQ2(), 0);
    Assert.assertEquals(2.5 / 28.5, inverseQ.getQ3(), 0);

    final Quaternion product = Quaternion.multiply(inverseQ, q);
    Assert.assertEquals(1, product.getQ0(), EPS);
    Assert.assertEquals(0, product.getQ1(), EPS);
    Assert.assertEquals(0, product.getQ2(), EPS);
    Assert.assertEquals(0, product.getQ3(), EPS);

    final Quaternion qNul = new Quaternion(0, 0, 0, 0);
    try {
        final Quaternion inverseQNul = qNul.getInverse();
        Assert.fail("expecting ZeroException but got : " + inverseQNul);
    } catch (ZeroException ex) {
        // expected
    }
}
 
Example #5
Source File: AbstractSimplex.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * The start configuration for simplex is built from a box parallel to
 * the canonical axes of the space. The simplex is the subset of vertices
 * of a box parallel to the canonical axes. It is built as the path followed
 * while traveling from one vertex of the box to the diagonally opposite
 * vertex moving only along the box edges. The first vertex of the box will
 * be located at the start point of the optimization.
 * As an example, in dimension 3 a simplex has 4 vertices. Setting the
 * steps to (1, 10, 2) and the start point to (1, 1, 1) would imply the
 * start simplex would be: { (1, 1, 1), (2, 1, 1), (2, 11, 1), (2, 11, 3) }.
 * The first vertex would be set to the start point at (1, 1, 1) and the
 * last vertex would be set to the diagonally opposite vertex at (2, 11, 3).
 *
 * @param steps Steps along the canonical axes representing box edges. They
 * may be negative but not zero.
 * @throws NullArgumentException if {@code steps} is {@code null}.
 * @throws ZeroException if one of the steps is zero.
 */
protected AbstractSimplex(final double[] steps) {
    if (steps == null) {
        throw new NullArgumentException();
    }
    if (steps.length == 0) {
        throw new ZeroException();
    }
    dimension = steps.length;

    // Only the relative position of the n final vertices with respect
    // to the first one are stored.
    startConfiguration = new double[dimension][dimension];
    for (int i = 0; i < dimension; i++) {
        final double[] vertexI = startConfiguration[i];
        for (int j = 0; j < i + 1; j++) {
            if (steps[j] == 0) {
                throw new ZeroException(LocalizedFormats.EQUAL_VERTICES_IN_SIMPLEX);
            }
            System.arraycopy(steps, 0, vertexI, 0, j + 1);
        }
    }
}
 
Example #6
Source File: AbstractSimplex.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * The start configuration for simplex is built from a box parallel to
 * the canonical axes of the space. The simplex is the subset of vertices
 * of a box parallel to the canonical axes. It is built as the path followed
 * while traveling from one vertex of the box to the diagonally opposite
 * vertex moving only along the box edges. The first vertex of the box will
 * be located at the start point of the optimization.
 * As an example, in dimension 3 a simplex has 4 vertices. Setting the
 * steps to (1, 10, 2) and the start point to (1, 1, 1) would imply the
 * start simplex would be: { (1, 1, 1), (2, 1, 1), (2, 11, 1), (2, 11, 3) }.
 * The first vertex would be set to the start point at (1, 1, 1) and the
 * last vertex would be set to the diagonally opposite vertex at (2, 11, 3).
 *
 * @param steps Steps along the canonical axes representing box edges. They
 * may be negative but not zero.
 * @throws NullArgumentException if {@code steps} is {@code null}.
 * @throws ZeroException if one of the steps is zero.
 */
protected AbstractSimplex(final double[] steps) {
    if (steps == null) {
        throw new NullArgumentException();
    }
    if (steps.length == 0) {
        throw new ZeroException();
    }
    dimension = steps.length;

    // Only the relative position of the n final vertices with respect
    // to the first one are stored.
    startConfiguration = new double[dimension][dimension];
    for (int i = 0; i < dimension; i++) {
        final double[] vertexI = startConfiguration[i];
        for (int j = 0; j < i + 1; j++) {
            if (steps[j] == 0) {
                throw new ZeroException(LocalizedFormats.EQUAL_VERTICES_IN_SIMPLEX);
            }
            System.arraycopy(steps, 0, vertexI, 0, j + 1);
        }
    }
}
 
Example #7
Source File: GaussianFitter.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Interpolates using the specified points to determine X at the
 * specified Y.
 *
 * @param points Points to use for interpolation.
 * @param startIdx Index within points from which to start the search for
 * interpolation bounds points.
 * @param idxStep Index step for searching interpolation bounds points.
 * @param y Y value for which X should be determined.
 * @return the value of X for the specified Y.
 * @throws ZeroException if {@code idxStep} is 0.
 * @throws OutOfRangeException if specified {@code y} is not within the
 * range of the specified {@code points}.
 */
private double interpolateXAtY(WeightedObservedPoint[] points,
                               int startIdx,
                               int idxStep,
                               double y)
    throws OutOfRangeException {
    if (idxStep == 0) {
        throw new ZeroException();
    }
    final WeightedObservedPoint[] twoPoints
        = getInterpolationPointsForY(points, startIdx, idxStep, y);
    final WeightedObservedPoint p1 = twoPoints[0];
    final WeightedObservedPoint p2 = twoPoints[1];
    if (p1.getY() == y) {
        return p1.getX();
    }
    if (p2.getY() == y) {
        return p2.getX();
    }
    return p1.getX() + (((y - p1.getY()) * (p2.getX() - p1.getX())) /
                        (p2.getY() - p1.getY()));
}
 
Example #8
Source File: AbstractSimplex.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * The start configuration for simplex is built from a box parallel to
 * the canonical axes of the space. The simplex is the subset of vertices
 * of a box parallel to the canonical axes. It is built as the path followed
 * while traveling from one vertex of the box to the diagonally opposite
 * vertex moving only along the box edges. The first vertex of the box will
 * be located at the start point of the optimization.
 * As an example, in dimension 3 a simplex has 4 vertices. Setting the
 * steps to (1, 10, 2) and the start point to (1, 1, 1) would imply the
 * start simplex would be: { (1, 1, 1), (2, 1, 1), (2, 11, 1), (2, 11, 3) }.
 * The first vertex would be set to the start point at (1, 1, 1) and the
 * last vertex would be set to the diagonally opposite vertex at (2, 11, 3).
 *
 * @param steps Steps along the canonical axes representing box edges. They
 * may be negative but not zero.
 * @throws NullArgumentException if {@code steps} is {@code null}.
 * @throws ZeroException if one of the steps is zero.
 */
protected AbstractSimplex(final double[] steps) {
    if (steps == null) {
        throw new NullArgumentException();
    }
    if (steps.length == 0) {
        throw new ZeroException();
    }
    dimension = steps.length;

    // Only the relative position of the n final vertices with respect
    // to the first one are stored.
    startConfiguration = new double[dimension][dimension];
    for (int i = 0; i < dimension; i++) {
        final double[] vertexI = startConfiguration[i];
        for (int j = 0; j < i + 1; j++) {
            if (steps[j] == 0) {
                throw new ZeroException(LocalizedFormats.EQUAL_VERTICES_IN_SIMPLEX);
            }
            System.arraycopy(steps, 0, vertexI, 0, j + 1);
        }
    }
}
 
Example #9
Source File: TestUtils.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @see org.apache.commons.math3.stat.inference.GTest#gTestDataSetsComparison(long[],long[],double)
 * @since 3.1
 */
public static boolean gTestDataSetsComparison(final long[] observed1,
                                              final long[] observed2,
                                              final double alpha)
    throws DimensionMismatchException, NotPositiveException,
    ZeroException, OutOfRangeException, MaxCountExceededException {
    return G_TEST.gTestDataSetsComparison(observed1, observed2, alpha);
}
 
Example #10
Source File: RootsOfUnity.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>
 * Computes the {@code n}-th roots of unity. The roots are stored in
 * {@code omega[]}, such that {@code omega[k] = w ^ k}, where
 * {@code k = 0, ..., n - 1}, {@code w = exp(2 * pi * i / n)} and
 * {@code i = sqrt(-1)}.
 * </p>
 * <p>
 * Note that {@code n} can be positive of negative
 * </p>
 * <ul>
 * <li>{@code abs(n)} is always the number of roots of unity.</li>
 * <li>If {@code n > 0}, then the roots are stored in counter-clockwise order.</li>
 * <li>If {@code n < 0}, then the roots are stored in clockwise order.</p>
 * </ul>
 *
 * @param n the (signed) number of roots of unity to be computed
 * @throws ZeroException if {@code n = 0}
 */
public synchronized void computeRoots(int n) throws ZeroException {

    if (n == 0) {
        throw new ZeroException(
                LocalizedFormats.CANNOT_COMPUTE_0TH_ROOT_OF_UNITY);
    }

    isCounterClockWise = n > 0;

    // avoid repetitive calculations
    final int absN = FastMath.abs(n);

    if (absN == omegaCount) {
        return;
    }

    // calculate everything from scratch
    final double t = 2.0 * FastMath.PI / absN;
    final double cosT = FastMath.cos(t);
    final double sinT = FastMath.sin(t);
    omegaReal = new double[absN];
    omegaImaginaryCounterClockwise = new double[absN];
    omegaImaginaryClockwise = new double[absN];
    omegaReal[0] = 1.0;
    omegaImaginaryCounterClockwise[0] = 0.0;
    omegaImaginaryClockwise[0] = 0.0;
    for (int i = 1; i < absN; i++) {
        omegaReal[i] = omegaReal[i - 1] * cosT -
                omegaImaginaryCounterClockwise[i - 1] * sinT;
        omegaImaginaryCounterClockwise[i] = omegaReal[i - 1] * sinT +
                omegaImaginaryCounterClockwise[i - 1] * cosT;
        omegaImaginaryClockwise[i] = -omegaImaginaryCounterClockwise[i];
    }
    omegaCount = absN;
}
 
Example #11
Source File: ValueServerTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testEmptyDigestFile() throws Exception {
    try {
        URL url = getClass().getResource("emptyFile.txt");
        vs.setMode(ValueServer.DIGEST_MODE);
        vs.setValuesFileURL(url);
        vs.computeDistribution();
        Assert.fail("an exception should have been thrown");
    } catch (ZeroException ze) {
        // expected behavior
    }
}
 
Example #12
Source File: BigFractionTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testReciprocal() {
    BigFraction f = null;

    f = new BigFraction(50, 75);
    f = f.reciprocal();
    Assert.assertEquals(3, f.getNumeratorAsInt());
    Assert.assertEquals(2, f.getDenominatorAsInt());

    f = new BigFraction(4, 3);
    f = f.reciprocal();
    Assert.assertEquals(3, f.getNumeratorAsInt());
    Assert.assertEquals(4, f.getDenominatorAsInt());

    f = new BigFraction(-15, 47);
    f = f.reciprocal();
    Assert.assertEquals(-47, f.getNumeratorAsInt());
    Assert.assertEquals(15, f.getDenominatorAsInt());

    f = new BigFraction(0, 3);
    try {
        f = f.reciprocal();
        Assert.fail("expecting ZeroException");
    } catch (ZeroException ex) {
    }

    // large values
    f = new BigFraction(Integer.MAX_VALUE, 1);
    f = f.reciprocal();
    Assert.assertEquals(1, f.getNumeratorAsInt());
    Assert.assertEquals(Integer.MAX_VALUE, f.getDenominatorAsInt());
}
 
Example #13
Source File: TestUtils.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @see org.apache.commons.math3.stat.inference.GTest#gTestDataSetsComparison(long[],long[],double)
 * @since 3.1
 */
public static boolean gTestDataSetsComparison(final long[] observed1,
                                              final long[] observed2,
                                              final double alpha)
    throws DimensionMismatchException, NotPositiveException,
    ZeroException, OutOfRangeException, MaxCountExceededException {
    return G_TEST.gTestDataSetsComparison(observed1, observed2, alpha);
}
 
Example #14
Source File: BigFractionTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testReciprocal() {
    BigFraction f = null;

    f = new BigFraction(50, 75);
    f = f.reciprocal();
    Assert.assertEquals(3, f.getNumeratorAsInt());
    Assert.assertEquals(2, f.getDenominatorAsInt());

    f = new BigFraction(4, 3);
    f = f.reciprocal();
    Assert.assertEquals(3, f.getNumeratorAsInt());
    Assert.assertEquals(4, f.getDenominatorAsInt());

    f = new BigFraction(-15, 47);
    f = f.reciprocal();
    Assert.assertEquals(-47, f.getNumeratorAsInt());
    Assert.assertEquals(15, f.getDenominatorAsInt());

    f = new BigFraction(0, 3);
    try {
        f = f.reciprocal();
        Assert.fail("expecting ZeroException");
    } catch (ZeroException ex) {
    }

    // large values
    f = new BigFraction(Integer.MAX_VALUE, 1);
    f = f.reciprocal();
    Assert.assertEquals(1, f.getNumeratorAsInt());
    Assert.assertEquals(Integer.MAX_VALUE, f.getDenominatorAsInt());
}
 
Example #15
Source File: TestUtils.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @see org.apache.commons.math3.stat.inference.ChiSquareTest#chiSquareTestDataSetsComparison(long[], long[])
 *
 * @since 1.2
 */
public static double chiSquareTestDataSetsComparison(final long[] observed1,
                                                     final long[] observed2)
    throws DimensionMismatchException, NotPositiveException, ZeroException,
    MaxCountExceededException {
    return CHI_SQUARE_TEST.chiSquareTestDataSetsComparison(observed1, observed2);
}
 
Example #16
Source File: Quaternion.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Computes the normalized quaternion (the versor of the instance).
 * The norm of the quaternion must not be zero.
 *
 * @return a normalized quaternion.
 * @throws ZeroException if the norm of the quaternion is zero.
 */
public Quaternion normalize() {
    final double norm = getNorm();

    if (norm < Precision.SAFE_MIN) {
        throw new ZeroException(LocalizedFormats.NORM, norm);
    }

    return new Quaternion(q0 / norm,
                          q1 / norm,
                          q2 / norm,
                          q3 / norm);
}
 
Example #17
Source File: GaussianFitter.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Gets the two bounding interpolation points from the specified points
 * suitable for determining X at the specified Y.
 *
 * @param points Points to use for interpolation.
 * @param startIdx Index within points from which to start search for
 * interpolation bounds points.
 * @param idxStep Index step for search for interpolation bounds points.
 * @param y Y value for which X should be determined.
 * @return the array containing two points suitable for determining X at
 * the specified Y.
 * @throws ZeroException if {@code idxStep} is 0.
 * @throws OutOfRangeException if specified {@code y} is not within the
 * range of the specified {@code points}.
 */
private WeightedObservedPoint[] getInterpolationPointsForY(WeightedObservedPoint[] points,
                                                           int startIdx,
                                                           int idxStep,
                                                           double y)
    throws OutOfRangeException {
    if (idxStep == 0) {
        throw new ZeroException();
    }
    for (int i = startIdx;
         idxStep < 0 ? i + idxStep >= 0 : i + idxStep < points.length;
         i += idxStep) {
        final WeightedObservedPoint p1 = points[i];
        final WeightedObservedPoint p2 = points[i + idxStep];
        if (isBetween(y, p1.getY(), p2.getY())) {
            if (idxStep < 0) {
                return new WeightedObservedPoint[] { p2, p1 };
            } else {
                return new WeightedObservedPoint[] { p1, p2 };
            }
        }
    }

    // Boundaries are replaced by dummy values because the raised
    // exception is caught and the message never displayed.
    // TODO: Exceptions should not be used for flow control.
    throw new OutOfRangeException(y,
                                  Double.NEGATIVE_INFINITY,
                                  Double.POSITIVE_INFINITY);
}
 
Example #18
Source File: BigFraction_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Create a {@link BigFraction} given the numerator and denominator as
 * {@code BigInteger}. The {@link BigFraction} is reduced to lowest terms.
 *
 * @param num the numerator, must not be {@code null}.
 * @param den the denominator, must not be {@code null}.
 * @throws ZeroException if the denominator is zero.
 * @throws NullArgumentException if either of the arguments is null
 */
public BigFraction(BigInteger num, BigInteger den) {
    MathUtils.checkNotNull(num, LocalizedFormats.NUMERATOR);
    MathUtils.checkNotNull(den, LocalizedFormats.DENOMINATOR);
    if (BigInteger.ZERO.equals(den)) {
        throw new ZeroException(LocalizedFormats.ZERO_DENOMINATOR);
    }
    if (BigInteger.ZERO.equals(num)) {
        numerator   = BigInteger.ZERO;
        denominator = BigInteger.ONE;
    } else {

        // reduce numerator and denominator by greatest common denominator
        final BigInteger gcd = num.gcd(den);
        if (BigInteger.ONE.compareTo(gcd) < 0) {
            num = num.divide(gcd);
            den = den.divide(gcd);
        }

        // move sign to numerator
        if (BigInteger.ZERO.compareTo(den) > 0) {
            num = num.negate();
            den = den.negate();
        }

        // store the values in the final fields
        numerator   = num;
        denominator = den;

    }
}
 
Example #19
Source File: TestUtils.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @see org.apache.commons.math3.stat.inference.ChiSquareTest#chiSquareTestDataSetsComparison(long[], long[], double)
 *
 * @since 1.2
 */
public static boolean chiSquareTestDataSetsComparison(final long[] observed1,
                                                      final long[] observed2,
                                                      final double alpha)
    throws DimensionMismatchException, NotPositiveException,
    ZeroException, OutOfRangeException, MaxCountExceededException {
    return CHI_SQUARE_TEST.chiSquareTestDataSetsComparison(observed1, observed2, alpha);
}
 
Example #20
Source File: ValueServerTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testEmptyDigestFile() throws Exception {
    try {
        URL url = getClass().getResource("emptyFile.txt");
        vs.setMode(ValueServer.DIGEST_MODE);
        vs.setValuesFileURL(url);
        vs.computeDistribution();
        Assert.fail("an exception should have been thrown");
    } catch (ZeroException ze) {
        // expected behavior
    }
}
 
Example #21
Source File: GaussianCurveFitter.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Gets the two bounding interpolation points from the specified points
 * suitable for determining X at the specified Y.
 *
 * @param points Points to use for interpolation.
 * @param startIdx Index within points from which to start search for
 * interpolation bounds points.
 * @param idxStep Index step for search for interpolation bounds points.
 * @param y Y value for which X should be determined.
 * @return the array containing two points suitable for determining X at
 * the specified Y.
 * @throws ZeroException if {@code idxStep} is 0.
 * @throws OutOfRangeException if specified {@code y} is not within the
 * range of the specified {@code points}.
 */
private WeightedObservedPoint[] getInterpolationPointsForY(WeightedObservedPoint[] points,
                                                           int startIdx,
                                                           int idxStep,
                                                           double y)
    throws OutOfRangeException {
    if (idxStep == 0) {
        throw new ZeroException();
    }
    for (int i = startIdx;
         idxStep < 0 ? i + idxStep >= 0 : i + idxStep < points.length;
         i += idxStep) {
        final WeightedObservedPoint p1 = points[i];
        final WeightedObservedPoint p2 = points[i + idxStep];
        if (isBetween(y, p1.getY(), p2.getY())) {
            if (idxStep < 0) {
                return new WeightedObservedPoint[] { p2, p1 };
            } else {
                return new WeightedObservedPoint[] { p1, p2 };
            }
        }
    }

    // Boundaries are replaced by dummy values because the raised
    // exception is caught and the message never displayed.
    // TODO: Exceptions should not be used for flow control.
    throw new OutOfRangeException(y,
                                  Double.NEGATIVE_INFINITY,
                                  Double.POSITIVE_INFINITY);
}
 
Example #22
Source File: TestUtils.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @see org.apache.commons.math3.stat.inference.ChiSquareTest#chiSquareTestDataSetsComparison(long[], long[], double)
 *
 * @since 1.2
 */
public static boolean chiSquareTestDataSetsComparison(final long[] observed1,
                                                      final long[] observed2,
                                                      final double alpha)
    throws DimensionMismatchException, NotPositiveException,
    ZeroException, OutOfRangeException, MaxCountExceededException {
    return CHI_SQUARE_TEST.chiSquareTestDataSetsComparison(observed1, observed2, alpha);
}
 
Example #23
Source File: RootsOfUnity.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>
 * Computes the {@code n}-th roots of unity. The roots are stored in
 * {@code omega[]}, such that {@code omega[k] = w ^ k}, where
 * {@code k = 0, ..., n - 1}, {@code w = exp(2 * pi * i / n)} and
 * {@code i = sqrt(-1)}.
 * </p>
 * <p>
 * Note that {@code n} can be positive of negative
 * </p>
 * <ul>
 * <li>{@code abs(n)} is always the number of roots of unity.</li>
 * <li>If {@code n > 0}, then the roots are stored in counter-clockwise order.</li>
 * <li>If {@code n < 0}, then the roots are stored in clockwise order.</p>
 * </ul>
 *
 * @param n the (signed) number of roots of unity to be computed
 * @throws ZeroException if {@code n = 0}
 */
public synchronized void computeRoots(int n) throws ZeroException {

    if (n == 0) {
        throw new ZeroException(
                LocalizedFormats.CANNOT_COMPUTE_0TH_ROOT_OF_UNITY);
    }

    isCounterClockWise = n > 0;

    // avoid repetitive calculations
    final int absN = FastMath.abs(n);

    if (absN == omegaCount) {
        return;
    }

    // calculate everything from scratch
    final double t = 2.0 * FastMath.PI / absN;
    final double cosT = FastMath.cos(t);
    final double sinT = FastMath.sin(t);
    omegaReal = new double[absN];
    omegaImaginaryCounterClockwise = new double[absN];
    omegaImaginaryClockwise = new double[absN];
    omegaReal[0] = 1.0;
    omegaImaginaryCounterClockwise[0] = 0.0;
    omegaImaginaryClockwise[0] = 0.0;
    for (int i = 1; i < absN; i++) {
        omegaReal[i] = omegaReal[i - 1] * cosT -
                omegaImaginaryCounterClockwise[i - 1] * sinT;
        omegaImaginaryCounterClockwise[i] = omegaReal[i - 1] * sinT +
                omegaImaginaryCounterClockwise[i - 1] * cosT;
        omegaImaginaryClockwise[i] = -omegaImaginaryCounterClockwise[i];
    }
    omegaCount = absN;
}
 
Example #24
Source File: TestUtils.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @see org.apache.commons.math3.stat.inference.GTest#gTestDataSetsComparison(long[],long[],double)
 * @since 3.1
 */
public static boolean gTestDataSetsComparison(final long[] observed1,
                                              final long[] observed2,
                                              final double alpha)
    throws DimensionMismatchException, NotPositiveException,
    ZeroException, OutOfRangeException, MaxCountExceededException {
    return G_TEST.gTestDataSetsComparison(observed1, observed2, alpha);
}
 
Example #25
Source File: ArrayFieldVector.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Construct a vector by appending one vector to another vector.
 *
 * @param field Field to which the elements belong.
 * @param v1 First vector (will be put in front of the new vector).
 * @param v2 Second vector (will be put at back of the new vector).
 * @throws NullArgumentException if {@code v1} or {@code v2} is
 * {@code null}.
 * @throws ZeroException if both arrays are empty.
 * @see #ArrayFieldVector(FieldElement[], FieldElement[])
 */
public ArrayFieldVector(Field<T> field, T[] v1, T[] v2)
        throws NullArgumentException, ZeroException {
    if (v1 == null || v2 == null) {
        throw new NullArgumentException();
    }
    if (v1.length + v2.length == 0) {
        throw new ZeroException(LocalizedFormats.VECTOR_MUST_HAVE_AT_LEAST_ONE_ELEMENT);
    }
    data = buildArray(v1.length + v2.length);
    System.arraycopy(v1, 0, data, 0, v1.length);
    System.arraycopy(v2, 0, data, v1.length, v2.length);
    this.field = field;
}
 
Example #26
Source File: RootsOfUnity.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>
 * Computes the {@code n}-th roots of unity. The roots are stored in
 * {@code omega[]}, such that {@code omega[k] = w ^ k}, where
 * {@code k = 0, ..., n - 1}, {@code w = exp(2 * pi * i / n)} and
 * {@code i = sqrt(-1)}.
 * </p>
 * <p>
 * Note that {@code n} can be positive of negative
 * </p>
 * <ul>
 * <li>{@code abs(n)} is always the number of roots of unity.</li>
 * <li>If {@code n > 0}, then the roots are stored in counter-clockwise order.</li>
 * <li>If {@code n < 0}, then the roots are stored in clockwise order.</p>
 * </ul>
 *
 * @param n the (signed) number of roots of unity to be computed
 * @throws ZeroException if {@code n = 0}
 */
public synchronized void computeRoots(int n) throws ZeroException {

    if (n == 0) {
        throw new ZeroException(
                LocalizedFormats.CANNOT_COMPUTE_0TH_ROOT_OF_UNITY);
    }

    isCounterClockWise = n > 0;

    // avoid repetitive calculations
    final int absN = FastMath.abs(n);

    if (absN == omegaCount) {
        return;
    }

    // calculate everything from scratch
    final double t = 2.0 * FastMath.PI / absN;
    final double cosT = FastMath.cos(t);
    final double sinT = FastMath.sin(t);
    omegaReal = new double[absN];
    omegaImaginaryCounterClockwise = new double[absN];
    omegaImaginaryClockwise = new double[absN];
    omegaReal[0] = 1.0;
    omegaImaginaryCounterClockwise[0] = 0.0;
    omegaImaginaryClockwise[0] = 0.0;
    for (int i = 1; i < absN; i++) {
        omegaReal[i] = omegaReal[i - 1] * cosT -
                omegaImaginaryCounterClockwise[i - 1] * sinT;
        omegaImaginaryCounterClockwise[i] = omegaReal[i - 1] * sinT +
                omegaImaginaryCounterClockwise[i - 1] * cosT;
        omegaImaginaryClockwise[i] = -omegaImaginaryCounterClockwise[i];
    }
    omegaCount = absN;
}
 
Example #27
Source File: BigFractionTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testGetReducedFraction() {
    BigFraction threeFourths = new BigFraction(3, 4);
    Assert.assertTrue(threeFourths.equals(BigFraction.getReducedFraction(6, 8)));
    Assert.assertTrue(BigFraction.ZERO.equals(BigFraction.getReducedFraction(0, -1)));
    try {
        BigFraction.getReducedFraction(1, 0);
        Assert.fail("expecting ZeroException");
    } catch (ZeroException ex) {
        // expected
    }
    Assert.assertEquals(BigFraction.getReducedFraction(2, Integer.MIN_VALUE).getNumeratorAsInt(), -1);
    Assert.assertEquals(BigFraction.getReducedFraction(1, -1).getNumeratorAsInt(), -1);
}
 
Example #28
Source File: TestUtils.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @see org.apache.commons.math3.stat.inference.ChiSquareTest#chiSquareTestDataSetsComparison(long[], long[], double)
 *
 * @since 1.2
 */
public static boolean chiSquareTestDataSetsComparison(final long[] observed1,
                                                      final long[] observed2,
                                                      final double alpha)
    throws DimensionMismatchException, NotPositiveException,
    ZeroException, OutOfRangeException, MaxCountExceededException {
    return CHI_SQUARE_TEST.chiSquareTestDataSetsComparison(observed1, observed2, alpha);
}
 
Example #29
Source File: BigFractionTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testGetReducedFraction() {
    BigFraction threeFourths = new BigFraction(3, 4);
    Assert.assertTrue(threeFourths.equals(BigFraction.getReducedFraction(6, 8)));
    Assert.assertTrue(BigFraction.ZERO.equals(BigFraction.getReducedFraction(0, -1)));
    try {
        BigFraction.getReducedFraction(1, 0);
        Assert.fail("expecting ZeroException");
    } catch (ZeroException ex) {
        // expected
    }
    Assert.assertEquals(BigFraction.getReducedFraction(2, Integer.MIN_VALUE).getNumeratorAsInt(), -1);
    Assert.assertEquals(BigFraction.getReducedFraction(1, -1).getNumeratorAsInt(), -1);
}
 
Example #30
Source File: ValueServerTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testEmptyDigestFile() throws Exception {
    try {
        URL url = getClass().getResource("emptyFile.txt");
        vs.setMode(ValueServer.DIGEST_MODE);
        vs.setValuesFileURL(url);
        vs.computeDistribution();
        Assert.fail("an exception should have been thrown");
    } catch (ZeroException ze) {
        // expected behavior
    }
}