This is an automated email from the ASF dual-hosted git repository. erans pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-math.git
The following commit(s) were added to refs/heads/master by this push: new cf2b9e7 MATH-1504: Removed "BigFraction" class (ported to "Commons Numbers"). cf2b9e7 is described below commit cf2b9e7479179bff9111df99b7fa9f30a1bd5a8b Author: Gilles Sadowski <gil...@harfang.homelinux.org> AuthorDate: Tue Dec 3 14:51:53 2019 +0100 MATH-1504: Removed "BigFraction" class (ported to "Commons Numbers"). Functionality unused within "Commons Math" was also removed: * Utility to build "FieldMatrix<BigFraction>" instances. * Utility to convert from "FieldMatrix<BigFraction>" to "RealMatrix". Class "BigFraction" was used for testing "Field" functionalities. Corresponding unit tests were refactored to use class "Dfp" instead. --- src/changes/changes.xml | 3 + .../apache/commons/math4/fraction/BigFraction.java | 1219 -------------------- .../commons/math4/fraction/BigFractionField.java | 85 -- .../apache/commons/math4/linear/MatrixUtils.java | 45 - .../FieldHermiteInterpolatorTest.java | 152 +-- .../math4/fraction/BigFractionFieldTest.java | 45 - .../commons/math4/fraction/BigFractionTest.java | 643 ----------- .../commons/math4/linear/MatrixUtilsTest.java | 14 - 8 files changed, 79 insertions(+), 2127 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index eea884f..d3c0a15 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -54,6 +54,9 @@ If the output is not quite correct, check for invisible trailing spaces! </release> <release version="4.0" date="XXXX-XX-XX" description=""> + <action dev="erans" type="update" issue="MATH-1504"> + Removed class "BigFraction" (ported to "Commons Numbers"). + </action> <action dev="erans" type="update" issue="MATH-1446"> Removed class "Fraction" (ported to "Commons Numbers"). </action> diff --git a/src/main/java/org/apache/commons/math4/fraction/BigFraction.java b/src/main/java/org/apache/commons/math4/fraction/BigFraction.java deleted file mode 100644 index 851c472..0000000 --- a/src/main/java/org/apache/commons/math4/fraction/BigFraction.java +++ /dev/null @@ -1,1219 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.math4.fraction; - -import java.io.Serializable; -import java.math.BigDecimal; -import java.math.BigInteger; - -import org.apache.commons.math4.FieldElement; -import org.apache.commons.math4.exception.MathArithmeticException; -import org.apache.commons.math4.exception.MathIllegalArgumentException; -import org.apache.commons.math4.exception.NullArgumentException; -import org.apache.commons.math4.exception.ZeroException; -import org.apache.commons.math4.exception.util.LocalizedFormats; -import org.apache.commons.numbers.core.ArithmeticUtils; -import org.apache.commons.math4.util.FastMath; -import org.apache.commons.math4.util.MathUtils; - -/** - * Representation of a rational number without any overflow. This class is - * immutable. - * - * @since 2.0 - */ -public class BigFraction - extends Number - implements FieldElement<BigFraction>, Comparable<BigFraction>, Serializable { - - /** A fraction representing "2 / 1". */ - public static final BigFraction TWO = new BigFraction(2); - - /** A fraction representing "1". */ - public static final BigFraction ONE = new BigFraction(1); - - /** A fraction representing "0". */ - public static final BigFraction ZERO = new BigFraction(0); - - /** A fraction representing "-1 / 1". */ - public static final BigFraction MINUS_ONE = new BigFraction(-1); - - /** A fraction representing "4/5". */ - public static final BigFraction FOUR_FIFTHS = new BigFraction(4, 5); - - /** A fraction representing "1/5". */ - public static final BigFraction ONE_FIFTH = new BigFraction(1, 5); - - /** A fraction representing "1/2". */ - public static final BigFraction ONE_HALF = new BigFraction(1, 2); - - /** A fraction representing "1/4". */ - public static final BigFraction ONE_QUARTER = new BigFraction(1, 4); - - /** A fraction representing "1/3". */ - public static final BigFraction ONE_THIRD = new BigFraction(1, 3); - - /** A fraction representing "3/5". */ - public static final BigFraction THREE_FIFTHS = new BigFraction(3, 5); - - /** A fraction representing "3/4". */ - public static final BigFraction THREE_QUARTERS = new BigFraction(3, 4); - - /** A fraction representing "2/5". */ - public static final BigFraction TWO_FIFTHS = new BigFraction(2, 5); - - /** A fraction representing "2/4". */ - public static final BigFraction TWO_QUARTERS = new BigFraction(2, 4); - - /** A fraction representing "2/3". */ - public static final BigFraction TWO_THIRDS = new BigFraction(2, 3); - - /** Serializable version identifier. */ - private static final long serialVersionUID = -5630213147331578515L; - - /** <code>BigInteger</code> representation of 100. */ - private static final BigInteger ONE_HUNDRED = BigInteger.valueOf(100); - - /** The numerator. */ - private final BigInteger numerator; - - /** The denominator. */ - private final BigInteger denominator; - - /** - * <p> - * Create a {@link BigFraction} equivalent to the passed {@code BigInteger}, ie - * "num / 1". - * </p> - * - * @param num - * the numerator. - */ - public BigFraction(final BigInteger num) { - this(num, BigInteger.ONE); - } - - /** - * 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 (den.signum() == 0) { - throw new ZeroException(LocalizedFormats.ZERO_DENOMINATOR); - } - if (num.signum() == 0) { - 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 (den.signum() == -1) { - num = num.negate(); - den = den.negate(); - } - - // store the values in the final fields - numerator = num; - denominator = den; - - } - } - - /** - * Create a fraction given the double value. - * <p> - * This constructor behaves <em>differently</em> from - * {@link #BigFraction(double, double, int)}. It converts the double value - * exactly, considering its internal bits representation. This works for all - * values except NaN and infinities and does not requires any loop or - * convergence threshold. - * </p> - * <p> - * Since this conversion is exact and since double numbers are sometimes - * approximated, the fraction created may seem strange in some cases. For example, - * calling <code>new BigFraction(1.0 / 3.0)</code> does <em>not</em> create - * the fraction 1/3, but the fraction 6004799503160661 / 18014398509481984 - * because the double number passed to the constructor is not exactly 1/3 - * (this number cannot be stored exactly in IEEE754). - * </p> - * @see #BigFraction(double, double, int) - * @param value the double value to convert to a fraction. - * @exception MathIllegalArgumentException if value is NaN or infinite - */ - public BigFraction(final double value) throws MathIllegalArgumentException { - if (Double.isNaN(value)) { - throw new MathIllegalArgumentException(LocalizedFormats.NAN_VALUE_CONVERSION); - } - if (Double.isInfinite(value)) { - throw new MathIllegalArgumentException(LocalizedFormats.INFINITE_VALUE_CONVERSION); - } - - // compute m and k such that value = m * 2^k - final long bits = Double.doubleToLongBits(value); - final long sign = bits & 0x8000000000000000L; - final long exponent = bits & 0x7ff0000000000000L; - long m = bits & 0x000fffffffffffffL; - if (exponent != 0) { - // this was a normalized number, add the implicit most significant bit - m |= 0x0010000000000000L; - } - if (sign != 0) { - m = -m; - } - int k = ((int) (exponent >> 52)) - 1075; - while (((m & 0x001ffffffffffffeL) != 0) && ((m & 0x1) == 0)) { - m >>= 1; - ++k; - } - - if (k < 0) { - numerator = BigInteger.valueOf(m); - denominator = BigInteger.ZERO.flipBit(-k); - } else { - numerator = BigInteger.valueOf(m).multiply(BigInteger.ZERO.flipBit(k)); - denominator = BigInteger.ONE; - } - - } - - /** - * Create a fraction given the double value and maximum error allowed. - * <p> - * References: - * <ul> - * <li><a href="http://mathworld.wolfram.com/ContinuedFraction.html"> - * Continued Fraction</a> equations (11) and (22)-(26)</li> - * </ul> - * - * @param value - * the double value to convert to a fraction. - * @param epsilon - * maximum error allowed. The resulting fraction is within - * <code>epsilon</code> of <code>value</code>, in absolute terms. - * @param maxIterations - * maximum number of convergents. - * @throws FractionConversionException - * if the continued fraction failed to converge. - * @see #BigFraction(double) - */ - public BigFraction(final double value, final double epsilon, - final int maxIterations) - throws FractionConversionException { - this(value, epsilon, Integer.MAX_VALUE, maxIterations); - } - - /** - * Create a fraction given the double value and either the maximum error - * allowed or the maximum number of denominator digits. - * <p> - * - * NOTE: This constructor is called with EITHER - a valid epsilon value and - * the maxDenominator set to Integer.MAX_VALUE (that way the maxDenominator - * has no effect). OR - a valid maxDenominator value and the epsilon value - * set to zero (that way epsilon only has effect if there is an exact match - * before the maxDenominator value is reached). - * </p> - * <p> - * - * It has been done this way so that the same code can be (re)used for both - * scenarios. However this could be confusing to users if it were part of - * the public API and this constructor should therefore remain PRIVATE. - * </p> - * - * See JIRA issue ticket MATH-181 for more details: - * - * https://issues.apache.org/jira/browse/MATH-181 - * - * @param value - * the double value to convert to a fraction. - * @param epsilon - * maximum error allowed. The resulting fraction is within - * <code>epsilon</code> of <code>value</code>, in absolute terms. - * @param maxDenominator - * maximum denominator value allowed. - * @param maxIterations - * maximum number of convergents. - * @throws FractionConversionException - * if the continued fraction failed to converge. - */ - private BigFraction(final double value, final double epsilon, - final int maxDenominator, int maxIterations) - throws FractionConversionException { - long overflow = Integer.MAX_VALUE; - double r0 = value; - long a0 = (long) FastMath.floor(r0); - - if (FastMath.abs(a0) > overflow) { - throw new FractionConversionException(value, a0, 1l); - } - - // check for (almost) integer arguments, which should not go - // to iterations. - if (FastMath.abs(a0 - value) < epsilon) { - numerator = BigInteger.valueOf(a0); - denominator = BigInteger.ONE; - return; - } - - long p0 = 1; - long q0 = 0; - long p1 = a0; - long q1 = 1; - - long p2 = 0; - long q2 = 1; - - int n = 0; - boolean stop = false; - do { - ++n; - final double r1 = 1.0 / (r0 - a0); - final long a1 = (long) FastMath.floor(r1); - p2 = (a1 * p1) + p0; - q2 = (a1 * q1) + q0; - if ((p2 > overflow) || (q2 > overflow)) { - // in maxDenominator mode, if the last fraction was very close to the actual value - // q2 may overflow in the next iteration; in this case return the last one. - if (epsilon == 0.0 && FastMath.abs(q1) < maxDenominator) { - break; - } - throw new FractionConversionException(value, p2, q2); - } - - final double convergent = (double) p2 / (double) q2; - if ((n < maxIterations) && - (FastMath.abs(convergent - value) > epsilon) && - (q2 < maxDenominator)) { - p0 = p1; - p1 = p2; - q0 = q1; - q1 = q2; - a0 = a1; - r0 = r1; - } else { - stop = true; - } - } while (!stop); - - if (n >= maxIterations) { - throw new FractionConversionException(value, maxIterations); - } - - if (q2 < maxDenominator) { - numerator = BigInteger.valueOf(p2); - denominator = BigInteger.valueOf(q2); - } else { - numerator = BigInteger.valueOf(p1); - denominator = BigInteger.valueOf(q1); - } - } - - /** - * Create a fraction given the double value and maximum denominator. - * <p> - * References: - * <ul> - * <li><a href="http://mathworld.wolfram.com/ContinuedFraction.html"> - * Continued Fraction</a> equations (11) and (22)-(26)</li> - * </ul> - * - * @param value - * the double value to convert to a fraction. - * @param maxDenominator - * The maximum allowed value for denominator. - * @throws FractionConversionException - * if the continued fraction failed to converge. - */ - public BigFraction(final double value, final int maxDenominator) - throws FractionConversionException { - this(value, 0, maxDenominator, 100); - } - - /** - * <p> - * Create a {@link BigFraction} equivalent to the passed {@code int}, ie - * "num / 1". - * </p> - * - * @param num - * the numerator. - */ - public BigFraction(final int num) { - this(BigInteger.valueOf(num), BigInteger.ONE); - } - - /** - * <p> - * Create a {@link BigFraction} given the numerator and denominator as simple - * {@code int}. The {@link BigFraction} is reduced to lowest terms. - * </p> - * - * @param num - * the numerator. - * @param den - * the denominator. - */ - public BigFraction(final int num, final int den) { - this(BigInteger.valueOf(num), BigInteger.valueOf(den)); - } - - /** - * <p> - * Create a {@link BigFraction} equivalent to the passed long, ie "num / 1". - * </p> - * - * @param num - * the numerator. - */ - public BigFraction(final long num) { - this(BigInteger.valueOf(num), BigInteger.ONE); - } - - /** - * <p> - * Create a {@link BigFraction} given the numerator and denominator as simple - * {@code long}. The {@link BigFraction} is reduced to lowest terms. - * </p> - * - * @param num - * the numerator. - * @param den - * the denominator. - */ - public BigFraction(final long num, final long den) { - this(BigInteger.valueOf(num), BigInteger.valueOf(den)); - } - - /** - * <p> - * Creates a <code>BigFraction</code> instance with the 2 parts of a fraction - * Y/Z. - * </p> - * - * <p> - * Any negative signs are resolved to be on the numerator. - * </p> - * - * @param numerator - * the numerator, for example the three in 'three sevenths'. - * @param denominator - * the denominator, for example the seven in 'three sevenths'. - * @return a new fraction instance, with the numerator and denominator - * reduced. - * @throws ArithmeticException - * if the denominator is <code>zero</code>. - */ - public static BigFraction getReducedFraction(final int numerator, - final int denominator) { - if (numerator == 0) { - return ZERO; // normalize zero. - } - - return new BigFraction(numerator, denominator); - } - - /** - * <p> - * Returns the absolute value of this {@link BigFraction}. - * </p> - * - * @return the absolute value as a {@link BigFraction}. - */ - public BigFraction abs() { - return (numerator.signum() == 1) ? this : negate(); - } - - /** - * <p> - * Adds the value of this fraction to the passed {@link BigInteger}, - * returning the result in reduced form. - * </p> - * - * @param bg - * the {@link BigInteger} to add, must'nt be <code>null</code>. - * @return a <code>BigFraction</code> instance with the resulting values. - * @throws NullArgumentException - * if the {@link BigInteger} is <code>null</code>. - */ - public BigFraction add(final BigInteger bg) throws NullArgumentException { - MathUtils.checkNotNull(bg); - - if (numerator.signum() == 0) { - return new BigFraction(bg); - } - if (bg.signum() == 0) { - return this; - } - - return new BigFraction(numerator.add(denominator.multiply(bg)), denominator); - } - - /** - * <p> - * Adds the value of this fraction to the passed {@code integer}, returning - * the result in reduced form. - * </p> - * - * @param i - * the {@code integer} to add. - * @return a <code>BigFraction</code> instance with the resulting values. - */ - public BigFraction add(final int i) { - return add(BigInteger.valueOf(i)); - } - - /** - * <p> - * Adds the value of this fraction to the passed {@code long}, returning - * the result in reduced form. - * </p> - * - * @param l - * the {@code long} to add. - * @return a <code>BigFraction</code> instance with the resulting values. - */ - public BigFraction add(final long l) { - return add(BigInteger.valueOf(l)); - } - - /** - * <p> - * Adds the value of this fraction to another, returning the result in - * reduced form. - * </p> - * - * @param fraction - * the {@link BigFraction} to add, must not be <code>null</code>. - * @return a {@link BigFraction} instance with the resulting values. - * @throws NullArgumentException if the {@link BigFraction} is {@code null}. - */ - @Override - public BigFraction add(final BigFraction fraction) { - MathUtils.checkNotNull(fraction, LocalizedFormats.FRACTION); - if (fraction.numerator.signum() == 0) { - return this; - } - if (numerator.signum() == 0) { - return fraction; - } - - BigInteger num = null; - BigInteger den = null; - - if (denominator.equals(fraction.denominator)) { - num = numerator.add(fraction.numerator); - den = denominator; - } else { - num = (numerator.multiply(fraction.denominator)).add((fraction.numerator).multiply(denominator)); - den = denominator.multiply(fraction.denominator); - } - - if (num.signum() == 0) { - return ZERO; - } - - return new BigFraction(num, den); - - } - - /** - * <p> - * Gets the fraction as a <code>BigDecimal</code>. This calculates the - * fraction as the numerator divided by denominator. - * </p> - * - * @return the fraction as a <code>BigDecimal</code>. - * @throws ArithmeticException - * if the exact quotient does not have a terminating decimal - * expansion. - * @see BigDecimal - */ - public BigDecimal bigDecimalValue() { - return new BigDecimal(numerator).divide(new BigDecimal(denominator)); - } - - /** - * <p> - * Gets the fraction as a <code>BigDecimal</code> following the passed - * rounding mode. This calculates the fraction as the numerator divided by - * denominator. - * </p> - * - * @param roundingMode - * rounding mode to apply. see {@link BigDecimal} constants. - * @return the fraction as a <code>BigDecimal</code>. - * @throws IllegalArgumentException - * if {@code roundingMode} does not represent a valid rounding - * mode. - * @see BigDecimal - */ - public BigDecimal bigDecimalValue(final int roundingMode) { - return new BigDecimal(numerator).divide(new BigDecimal(denominator), roundingMode); - } - - /** - * <p> - * Gets the fraction as a <code>BigDecimal</code> following the passed scale - * and rounding mode. This calculates the fraction as the numerator divided - * by denominator. - * </p> - * - * @param scale - * scale of the <code>BigDecimal</code> quotient to be returned. - * see {@link BigDecimal} for more information. - * @param roundingMode - * rounding mode to apply. see {@link BigDecimal} constants. - * @return the fraction as a <code>BigDecimal</code>. - * @see BigDecimal - */ - public BigDecimal bigDecimalValue(final int scale, final int roundingMode) { - return new BigDecimal(numerator).divide(new BigDecimal(denominator), scale, roundingMode); - } - - /** - * <p> - * Compares this object to another based on size. - * </p> - * - * @param object - * the object to compare to, must not be <code>null</code>. - * @return -1 if this is less than {@code object}, +1 if this is greater - * than {@code object}, 0 if they are equal. - * @see java.lang.Comparable#compareTo(java.lang.Object) - */ - @Override - public int compareTo(final BigFraction object) { - int lhsSigNum = numerator.signum(); - int rhsSigNum = object.numerator.signum(); - - if (lhsSigNum != rhsSigNum) { - return (lhsSigNum > rhsSigNum) ? 1 : -1; - } - if (lhsSigNum == 0) { - return 0; - } - - BigInteger nOd = numerator.multiply(object.denominator); - BigInteger dOn = denominator.multiply(object.numerator); - return nOd.compareTo(dOn); - } - - /** - * <p> - * Divide the value of this fraction by the passed {@code BigInteger}, - * ie {@code this * 1 / bg}, returning the result in reduced form. - * </p> - * - * @param bg the {@code BigInteger} to divide by, must not be {@code null} - * @return a {@link BigFraction} instance with the resulting values - * @throws NullArgumentException if the {@code BigInteger} is {@code null} - * @throws MathArithmeticException if the fraction to divide by is zero - */ - public BigFraction divide(final BigInteger bg) { - MathUtils.checkNotNull(bg); - if (bg.signum() == 0) { - throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR); - } - if (numerator.signum() == 0) { - return ZERO; - } - return new BigFraction(numerator, denominator.multiply(bg)); - } - - /** - * <p> - * Divide the value of this fraction by the passed {@code int}, ie - * {@code this * 1 / i}, returning the result in reduced form. - * </p> - * - * @param i the {@code int} to divide by - * @return a {@link BigFraction} instance with the resulting values - * @throws MathArithmeticException if the fraction to divide by is zero - */ - public BigFraction divide(final int i) { - return divide(BigInteger.valueOf(i)); - } - - /** - * <p> - * Divide the value of this fraction by the passed {@code long}, ie - * {@code this * 1 / l}, returning the result in reduced form. - * </p> - * - * @param l the {@code long} to divide by - * @return a {@link BigFraction} instance with the resulting values - * @throws MathArithmeticException if the fraction to divide by is zero - */ - public BigFraction divide(final long l) { - return divide(BigInteger.valueOf(l)); - } - - /** - * <p> - * Divide the value of this fraction by another, returning the result in - * reduced form. - * </p> - * - * @param fraction Fraction to divide by, must not be {@code null}. - * @return a {@link BigFraction} instance with the resulting values. - * @throws NullArgumentException if the {@code fraction} is {@code null}. - * @throws MathArithmeticException if the fraction to divide by is zero - */ - @Override - public BigFraction divide(final BigFraction fraction) { - MathUtils.checkNotNull(fraction, LocalizedFormats.FRACTION); - if (fraction.numerator.signum() == 0) { - throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR); - } - if (numerator.signum() == 0) { - return ZERO; - } - - return multiply(fraction.reciprocal()); - } - - /** - * <p> - * Gets the fraction as a {@code double}. This calculates the fraction as - * the numerator divided by denominator. - * </p> - * - * @return the fraction as a {@code double} - * @see java.lang.Number#doubleValue() - */ - @Override - public double doubleValue() { - double result = numerator.doubleValue() / denominator.doubleValue(); - if (Double.isNaN(result)) { - // Numerator and/or denominator must be out of range: - // Calculate how far to shift them to put them in range. - int shift = FastMath.max(numerator.bitLength(), - denominator.bitLength()) - FastMath.getExponent(Double.MAX_VALUE); - result = numerator.shiftRight(shift).doubleValue() / - denominator.shiftRight(shift).doubleValue(); - } - return result; - } - - /** - * <p> - * Test for the equality of two fractions. If the lowest term numerator and - * denominators are the same for both fractions, the two fractions are - * considered to be equal. - * </p> - * - * @param other - * fraction to test for equality to this fraction, can be - * <code>null</code>. - * @return true if two fractions are equal, false if object is - * <code>null</code>, not an instance of {@link BigFraction}, or not - * equal to this fraction instance. - * @see java.lang.Object#equals(java.lang.Object) - */ - @Override - public boolean equals(final Object other) { - boolean ret = false; - - if (this == other) { - ret = true; - } else if (other instanceof BigFraction) { - BigFraction rhs = ((BigFraction) other).reduce(); - BigFraction thisOne = this.reduce(); - ret = thisOne.numerator.equals(rhs.numerator) && thisOne.denominator.equals(rhs.denominator); - } - - return ret; - } - - /** - * <p> - * Gets the fraction as a {@code float}. This calculates the fraction as - * the numerator divided by denominator. - * </p> - * - * @return the fraction as a {@code float}. - * @see java.lang.Number#floatValue() - */ - @Override - public float floatValue() { - float result = numerator.floatValue() / denominator.floatValue(); - if (Double.isNaN(result)) { - // Numerator and/or denominator must be out of range: - // Calculate how far to shift them to put them in range. - int shift = FastMath.max(numerator.bitLength(), - denominator.bitLength()) - FastMath.getExponent(Float.MAX_VALUE); - result = numerator.shiftRight(shift).floatValue() / - denominator.shiftRight(shift).floatValue(); - } - return result; - } - - /** - * <p> - * Access the denominator as a <code>BigInteger</code>. - * </p> - * - * @return the denominator as a <code>BigInteger</code>. - */ - public BigInteger getDenominator() { - return denominator; - } - - /** - * <p> - * Access the denominator as a {@code int}. - * </p> - * - * @return the denominator as a {@code int}. - */ - public int getDenominatorAsInt() { - return denominator.intValue(); - } - - /** - * <p> - * Access the denominator as a {@code long}. - * </p> - * - * @return the denominator as a {@code long}. - */ - public long getDenominatorAsLong() { - return denominator.longValue(); - } - - /** - * <p> - * Access the numerator as a <code>BigInteger</code>. - * </p> - * - * @return the numerator as a <code>BigInteger</code>. - */ - public BigInteger getNumerator() { - return numerator; - } - - /** - * <p> - * Access the numerator as a {@code int}. - * </p> - * - * @return the numerator as a {@code int}. - */ - public int getNumeratorAsInt() { - return numerator.intValue(); - } - - /** - * <p> - * Access the numerator as a {@code long}. - * </p> - * - * @return the numerator as a {@code long}. - */ - public long getNumeratorAsLong() { - return numerator.longValue(); - } - - /** - * <p> - * Gets a hashCode for the fraction. - * </p> - * - * @return a hash code value for this object. - * @see java.lang.Object#hashCode() - */ - @Override - public int hashCode() { - return 37 * (37 * 17 + numerator.hashCode()) + denominator.hashCode(); - } - - /** - * <p> - * Gets the fraction as an {@code int}. This returns the whole number part - * of the fraction. - * </p> - * - * @return the whole number fraction part. - * @see java.lang.Number#intValue() - */ - @Override - public int intValue() { - return numerator.divide(denominator).intValue(); - } - - /** - * <p> - * Gets the fraction as a {@code long}. This returns the whole number part - * of the fraction. - * </p> - * - * @return the whole number fraction part. - * @see java.lang.Number#longValue() - */ - @Override - public long longValue() { - return numerator.divide(denominator).longValue(); - } - - /** - * <p> - * Multiplies the value of this fraction by the passed - * <code>BigInteger</code>, returning the result in reduced form. - * </p> - * - * @param bg the {@code BigInteger} to multiply by. - * @return a {@code BigFraction} instance with the resulting values. - * @throws NullArgumentException if {@code bg} is {@code null}. - */ - public BigFraction multiply(final BigInteger bg) { - MathUtils.checkNotNull(bg); - if (numerator.signum() == 0 || bg.signum() == 0) { - return ZERO; - } - return new BigFraction(bg.multiply(numerator), denominator); - } - - /** - * <p> - * Multiply the value of this fraction by the passed {@code int}, returning - * the result in reduced form. - * </p> - * - * @param i - * the {@code int} to multiply by. - * @return a {@link BigFraction} instance with the resulting values. - */ - @Override - public BigFraction multiply(final int i) { - if (i == 0 || numerator.signum() == 0) { - return ZERO; - } - - return multiply(BigInteger.valueOf(i)); - } - - /** - * <p> - * Multiply the value of this fraction by the passed {@code long}, - * returning the result in reduced form. - * </p> - * - * @param l - * the {@code long} to multiply by. - * @return a {@link BigFraction} instance with the resulting values. - */ - public BigFraction multiply(final long l) { - if (l == 0 || numerator.signum() == 0) { - return ZERO; - } - - return multiply(BigInteger.valueOf(l)); - } - - /** - * <p> - * Multiplies the value of this fraction by another, returning the result in - * reduced form. - * </p> - * - * @param fraction Fraction to multiply by, must not be {@code null}. - * @return a {@link BigFraction} instance with the resulting values. - * @throws NullArgumentException if {@code fraction} is {@code null}. - */ - @Override - public BigFraction multiply(final BigFraction fraction) { - MathUtils.checkNotNull(fraction, LocalizedFormats.FRACTION); - if (numerator.signum() == 0 || - fraction.numerator.signum() == 0) { - return ZERO; - } - return new BigFraction(numerator.multiply(fraction.numerator), - denominator.multiply(fraction.denominator)); - } - - /** - * <p> - * Return the additive inverse of this fraction, returning the result in - * reduced form. - * </p> - * - * @return the negation of this fraction. - */ - @Override - public BigFraction negate() { - return new BigFraction(numerator.negate(), denominator); - } - - /** - * <p> - * Gets the fraction percentage as a {@code double}. This calculates the - * fraction as the numerator divided by denominator multiplied by 100. - * </p> - * - * @return the fraction percentage as a {@code double}. - */ - public double percentageValue() { - return multiply(ONE_HUNDRED).doubleValue(); - } - - /** - * <p> - * Returns a {@code BigFraction} whose value is - * {@code (this<sup>exponent</sup>)}, returning the result in reduced form. - * </p> - * - * @param exponent - * exponent to which this {@code BigFraction} is to be - * raised. - * @return <tt>this<sup>exponent</sup></tt>. - */ - public BigFraction pow(final int exponent) { - if (exponent == 0) { - return ONE; - } - if (numerator.signum() == 0) { - return this; - } - - if (exponent < 0) { - return new BigFraction(denominator.pow(-exponent), numerator.pow(-exponent)); - } - return new BigFraction(numerator.pow(exponent), denominator.pow(exponent)); - } - - /** - * <p> - * Returns a <code>BigFraction</code> whose value is - * <tt>(this<sup>exponent</sup>)</tt>, returning the result in reduced form. - * </p> - * - * @param exponent - * exponent to which this <code>BigFraction</code> is to be raised. - * @return <tt>this<sup>exponent</sup></tt> as a <code>BigFraction</code>. - */ - public BigFraction pow(final long exponent) { - if (exponent == 0) { - return ONE; - } - if (numerator.signum() == 0) { - return this; - } - - if (exponent < 0) { - return new BigFraction(ArithmeticUtils.pow(denominator, -exponent), - ArithmeticUtils.pow(numerator, -exponent)); - } - return new BigFraction(ArithmeticUtils.pow(numerator, exponent), - ArithmeticUtils.pow(denominator, exponent)); - } - - /** - * <p> - * Returns a <code>BigFraction</code> whose value is - * <tt>(this<sup>exponent</sup>)</tt>, returning the result in reduced form. - * </p> - * - * @param exponent - * exponent to which this <code>BigFraction</code> is to be raised. - * @return <tt>this<sup>exponent</sup></tt> as a <code>BigFraction</code>. - */ - public BigFraction pow(final BigInteger exponent) { - if (exponent.signum() == 0) { - return ONE; - } - if (numerator.signum() == 0) { - return this; - } - - if (exponent.signum() == -1) { - final BigInteger eNeg = exponent.negate(); - return new BigFraction(ArithmeticUtils.pow(denominator, eNeg), - ArithmeticUtils.pow(numerator, eNeg)); - } - return new BigFraction(ArithmeticUtils.pow(numerator, exponent), - ArithmeticUtils.pow(denominator, exponent)); - } - - /** - * <p> - * Returns a <code>double</code> whose value is - * <tt>(this<sup>exponent</sup>)</tt>, returning the result in reduced form. - * </p> - * - * @param exponent - * exponent to which this <code>BigFraction</code> is to be raised. - * @return <tt>this<sup>exponent</sup></tt>. - */ - public double pow(final double exponent) { - return FastMath.pow(numerator.doubleValue(), exponent) / - FastMath.pow(denominator.doubleValue(), exponent); - } - - /** - * <p> - * Return the multiplicative inverse of this fraction. - * </p> - * - * @return the reciprocal fraction. - */ - @Override - public BigFraction reciprocal() { - return new BigFraction(denominator, numerator); - } - - /** - * <p> - * Reduce this <code>BigFraction</code> to its lowest terms. - * </p> - * - * @return the reduced <code>BigFraction</code>. It doesn't change anything if - * the fraction can be reduced. - */ - public BigFraction reduce() { - final BigInteger gcd = numerator.gcd(denominator); - - if (BigInteger.ONE.compareTo(gcd) < 0) { - return new BigFraction(numerator.divide(gcd), denominator.divide(gcd)); - } else { - return this; - } - } - - /** - * <p> - * Subtracts the value of an {@link BigInteger} from the value of this - * {@code BigFraction}, returning the result in reduced form. - * </p> - * - * @param bg the {@link BigInteger} to subtract, cannot be {@code null}. - * @return a {@code BigFraction} instance with the resulting values. - * @throws NullArgumentException if the {@link BigInteger} is {@code null}. - */ - public BigFraction subtract(final BigInteger bg) { - MathUtils.checkNotNull(bg); - if (bg.signum() == 0) { - return this; - } - if (numerator.signum() == 0) { - return new BigFraction(bg.negate()); - } - - return new BigFraction(numerator.subtract(denominator.multiply(bg)), denominator); - } - - /** - * <p> - * Subtracts the value of an {@code integer} from the value of this - * {@code BigFraction}, returning the result in reduced form. - * </p> - * - * @param i the {@code integer} to subtract. - * @return a {@code BigFraction} instance with the resulting values. - */ - public BigFraction subtract(final int i) { - return subtract(BigInteger.valueOf(i)); - } - - /** - * <p> - * Subtracts the value of a {@code long} from the value of this - * {@code BigFraction}, returning the result in reduced form. - * </p> - * - * @param l the {@code long} to subtract. - * @return a {@code BigFraction} instance with the resulting values. - */ - public BigFraction subtract(final long l) { - return subtract(BigInteger.valueOf(l)); - } - - /** - * <p> - * Subtracts the value of another fraction from the value of this one, - * returning the result in reduced form. - * </p> - * - * @param fraction {@link BigFraction} to subtract, must not be {@code null}. - * @return a {@link BigFraction} instance with the resulting values - * @throws NullArgumentException if the {@code fraction} is {@code null}. - */ - @Override - public BigFraction subtract(final BigFraction fraction) { - MathUtils.checkNotNull(fraction, LocalizedFormats.FRACTION); - if (fraction.numerator.signum() == 0) { - return this; - } - if (numerator.signum() == 0) { - return fraction.negate(); - } - - BigInteger num = null; - BigInteger den = null; - if (denominator.equals(fraction.denominator)) { - num = numerator.subtract(fraction.numerator); - den = denominator; - } else { - num = (numerator.multiply(fraction.denominator)).subtract((fraction.numerator).multiply(denominator)); - den = denominator.multiply(fraction.denominator); - } - return new BigFraction(num, den); - - } - - /** - * <p> - * Returns the <code>String</code> representing this fraction, ie - * "num / dem" or just "num" if the denominator is one. - * </p> - * - * @return a string representation of the fraction. - * @see java.lang.Object#toString() - */ - @Override - public String toString() { - String str = null; - if (BigInteger.ONE.equals(denominator)) { - str = numerator.toString(); - } else if (BigInteger.ZERO.equals(numerator)) { - str = "0"; - } else { - str = numerator + " / " + denominator; - } - return str; - } - - /** {@inheritDoc} */ - @Override - public BigFractionField getField() { - return BigFractionField.getInstance(); - } - -} diff --git a/src/main/java/org/apache/commons/math4/fraction/BigFractionField.java b/src/main/java/org/apache/commons/math4/fraction/BigFractionField.java deleted file mode 100644 index fd55002..0000000 --- a/src/main/java/org/apache/commons/math4/fraction/BigFractionField.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.commons.math4.fraction; - -import java.io.Serializable; - -import org.apache.commons.math4.Field; -import org.apache.commons.math4.FieldElement; - -/** - * Representation of the fractional numbers without any overflow field. - * <p> - * This class is a singleton. - * </p> - * @see Fraction - * @since 2.0 - */ -public class BigFractionField implements Field<BigFraction>, Serializable { - - /** Serializable version identifier */ - private static final long serialVersionUID = -1699294557189741703L; - - /** Private constructor for the singleton. - */ - private BigFractionField() { - } - - /** Get the unique instance. - * @return the unique instance - */ - public static BigFractionField getInstance() { - return LazyHolder.INSTANCE; - } - - /** {@inheritDoc} */ - @Override - public BigFraction getOne() { - return BigFraction.ONE; - } - - /** {@inheritDoc} */ - @Override - public BigFraction getZero() { - return BigFraction.ZERO; - } - - /** {@inheritDoc} */ - @Override - public Class<? extends FieldElement<BigFraction>> getRuntimeClass() { - return BigFraction.class; - } - // CHECKSTYLE: stop HideUtilityClassConstructor - /** Holder for the instance. - * <p>We use here the Initialization On Demand Holder Idiom.</p> - */ - private static class LazyHolder { - /** Cached field instance. */ - private static final BigFractionField INSTANCE = new BigFractionField(); - } - // CHECKSTYLE: resume HideUtilityClassConstructor - - /** Handle deserialization of the singleton. - * @return the singleton instance - */ - private Object readResolve() { - // return the singleton instance - return LazyHolder.INSTANCE; - } - -} diff --git a/src/main/java/org/apache/commons/math4/linear/MatrixUtils.java b/src/main/java/org/apache/commons/math4/linear/MatrixUtils.java index 9f88288..c47944f 100644 --- a/src/main/java/org/apache/commons/math4/linear/MatrixUtils.java +++ b/src/main/java/org/apache/commons/math4/linear/MatrixUtils.java @@ -32,7 +32,6 @@ import org.apache.commons.math4.exception.NumberIsTooSmallException; import org.apache.commons.math4.exception.OutOfRangeException; import org.apache.commons.math4.exception.ZeroException; import org.apache.commons.math4.exception.util.LocalizedFormats; -import org.apache.commons.math4.fraction.BigFraction; import org.apache.commons.math4.util.FastMath; import org.apache.commons.math4.util.MathArrays; import org.apache.commons.math4.util.MathUtils; @@ -607,50 +606,6 @@ public class MatrixUtils { left.checkMultiply(right); } - /** - * Convert a {@link FieldMatrix}/{@link BigFraction} matrix to a {@link RealMatrix}. - * - * @param m Matrix to convert. - * @return the converted matrix. - */ - public static Array2DRowRealMatrix bigFractionMatrixToRealMatrix(final FieldMatrix<BigFraction> m) { - final BigFractionMatrixConverter converter = new BigFractionMatrixConverter(); - m.walkInOptimizedOrder(converter); - return converter.getConvertedMatrix(); - } - - /** Converter for {@link FieldMatrix}/{@link BigFraction}. */ - private static class BigFractionMatrixConverter extends DefaultFieldMatrixPreservingVisitor<BigFraction> { - /** Converted array. */ - private double[][] data; - /** Simple constructor. */ - BigFractionMatrixConverter() { - super(BigFraction.ZERO); - } - - /** {@inheritDoc} */ - @Override - public void start(int rows, int columns, - int startRow, int endRow, int startColumn, int endColumn) { - data = new double[rows][columns]; - } - - /** {@inheritDoc} */ - @Override - public void visit(int row, int column, BigFraction value) { - data[row][column] = value.doubleValue(); - } - - /** - * Get the converted matrix. - * - * @return the converted matrix. - */ - Array2DRowRealMatrix getConvertedMatrix() { - return new Array2DRowRealMatrix(data, false); - } - } - /** Serialize a {@link RealVector}. * <p> * This method is intended to be called from within a private diff --git a/src/test/java/org/apache/commons/math4/analysis/interpolation/FieldHermiteInterpolatorTest.java b/src/test/java/org/apache/commons/math4/analysis/interpolation/FieldHermiteInterpolatorTest.java index 33fab5d..572435d 100644 --- a/src/test/java/org/apache/commons/math4/analysis/interpolation/FieldHermiteInterpolatorTest.java +++ b/src/test/java/org/apache/commons/math4/analysis/interpolation/FieldHermiteInterpolatorTest.java @@ -22,9 +22,9 @@ import org.apache.commons.math4.analysis.interpolation.FieldHermiteInterpolator; import org.apache.commons.math4.analysis.polynomials.PolynomialFunction; import org.apache.commons.math4.dfp.Dfp; import org.apache.commons.math4.dfp.DfpField; +import org.apache.commons.math4.linear.Dfp25; import org.apache.commons.math4.exception.MathIllegalArgumentException; import org.apache.commons.math4.exception.NoDataException; -import org.apache.commons.math4.fraction.BigFraction; import org.apache.commons.math4.util.FastMath; import org.junit.Assert; import org.junit.Test; @@ -33,61 +33,61 @@ public class FieldHermiteInterpolatorTest { @Test public void testZero() { - FieldHermiteInterpolator<BigFraction> interpolator = new FieldHermiteInterpolator<>(); - interpolator.addSamplePoint(new BigFraction(0), new BigFraction[] { new BigFraction(0) }); + FieldHermiteInterpolator<Dfp> interpolator = new FieldHermiteInterpolator<>(); + interpolator.addSamplePoint(Dfp25.of(0), new Dfp[] { Dfp25.of(0) }); for (int x = -10; x < 10; x++) { - BigFraction y = interpolator.value(new BigFraction(x))[0]; - Assert.assertEquals(BigFraction.ZERO, y); - BigFraction[][] derivatives = interpolator.derivatives(new BigFraction(x), 1); - Assert.assertEquals(BigFraction.ZERO, derivatives[0][0]); - Assert.assertEquals(BigFraction.ZERO, derivatives[1][0]); + Dfp y = interpolator.value(Dfp25.of(x))[0]; + Assert.assertEquals(Dfp25.ZERO, y); + Dfp[][] derivatives = interpolator.derivatives(Dfp25.of(x), 1); + Assert.assertEquals(Dfp25.ZERO, derivatives[0][0]); + Assert.assertEquals(Dfp25.ZERO, derivatives[1][0]); } } @Test public void testQuadratic() { - FieldHermiteInterpolator<BigFraction> interpolator = new FieldHermiteInterpolator<>(); - interpolator.addSamplePoint(new BigFraction(0), new BigFraction[] { new BigFraction(2) }); - interpolator.addSamplePoint(new BigFraction(1), new BigFraction[] { new BigFraction(0) }); - interpolator.addSamplePoint(new BigFraction(2), new BigFraction[] { new BigFraction(0) }); + FieldHermiteInterpolator<Dfp> interpolator = new FieldHermiteInterpolator<>(); + interpolator.addSamplePoint(Dfp25.of(0), new Dfp[] { Dfp25.of(2) }); + interpolator.addSamplePoint(Dfp25.of(1), new Dfp[] { Dfp25.of(0) }); + interpolator.addSamplePoint(Dfp25.of(2), new Dfp[] { Dfp25.of(0) }); for (double x = -10; x < 10; x += 1.0) { - BigFraction y = interpolator.value(new BigFraction(x))[0]; - Assert.assertEquals((x - 1) * (x - 2), y.doubleValue(), 1.0e-15); - BigFraction[][] derivatives = interpolator.derivatives(new BigFraction(x), 3); - Assert.assertEquals((x - 1) * (x - 2), derivatives[0][0].doubleValue(), 1.0e-15); - Assert.assertEquals(2 * x - 3, derivatives[1][0].doubleValue(), 1.0e-15); - Assert.assertEquals(2, derivatives[2][0].doubleValue(), 1.0e-15); - Assert.assertEquals(0, derivatives[3][0].doubleValue(), 1.0e-15); + Dfp y = interpolator.value(Dfp25.of(x))[0]; + Assert.assertEquals((x - 1) * (x - 2), y.toDouble(), 1.0e-15); + Dfp[][] derivatives = interpolator.derivatives(Dfp25.of(x), 3); + Assert.assertEquals((x - 1) * (x - 2), derivatives[0][0].toDouble(), 1.0e-15); + Assert.assertEquals(2 * x - 3, derivatives[1][0].toDouble(), 1.0e-15); + Assert.assertEquals(2, derivatives[2][0].toDouble(), 1.0e-15); + Assert.assertEquals(0, derivatives[3][0].toDouble(), 1.0e-15); } } @Test public void testMixedDerivatives() { - FieldHermiteInterpolator<BigFraction> interpolator = new FieldHermiteInterpolator<>(); - interpolator.addSamplePoint(new BigFraction(0), new BigFraction[] { new BigFraction(1) }, new BigFraction[] { new BigFraction(2) }); - interpolator.addSamplePoint(new BigFraction(1), new BigFraction[] { new BigFraction(4) }); - interpolator.addSamplePoint(new BigFraction(2), new BigFraction[] { new BigFraction(5) }, new BigFraction[] { new BigFraction(2) }); - BigFraction[][] derivatives = interpolator.derivatives(new BigFraction(0), 5); - Assert.assertEquals(new BigFraction( 1), derivatives[0][0]); - Assert.assertEquals(new BigFraction( 2), derivatives[1][0]); - Assert.assertEquals(new BigFraction( 8), derivatives[2][0]); - Assert.assertEquals(new BigFraction(-24), derivatives[3][0]); - Assert.assertEquals(new BigFraction( 24), derivatives[4][0]); - Assert.assertEquals(new BigFraction( 0), derivatives[5][0]); - derivatives = interpolator.derivatives(new BigFraction(1), 5); - Assert.assertEquals(new BigFraction( 4), derivatives[0][0]); - Assert.assertEquals(new BigFraction( 2), derivatives[1][0]); - Assert.assertEquals(new BigFraction( -4), derivatives[2][0]); - Assert.assertEquals(new BigFraction( 0), derivatives[3][0]); - Assert.assertEquals(new BigFraction( 24), derivatives[4][0]); - Assert.assertEquals(new BigFraction( 0), derivatives[5][0]); - derivatives = interpolator.derivatives(new BigFraction(2), 5); - Assert.assertEquals(new BigFraction( 5), derivatives[0][0]); - Assert.assertEquals(new BigFraction( 2), derivatives[1][0]); - Assert.assertEquals(new BigFraction( 8), derivatives[2][0]); - Assert.assertEquals(new BigFraction( 24), derivatives[3][0]); - Assert.assertEquals(new BigFraction( 24), derivatives[4][0]); - Assert.assertEquals(new BigFraction( 0), derivatives[5][0]); + FieldHermiteInterpolator<Dfp> interpolator = new FieldHermiteInterpolator<>(); + interpolator.addSamplePoint(Dfp25.of(0), new Dfp[] { Dfp25.of(1) }, new Dfp[] { Dfp25.of(2) }); + interpolator.addSamplePoint(Dfp25.of(1), new Dfp[] { Dfp25.of(4) }); + interpolator.addSamplePoint(Dfp25.of(2), new Dfp[] { Dfp25.of(5) }, new Dfp[] { Dfp25.of(2) }); + Dfp[][] derivatives = interpolator.derivatives(Dfp25.of(0), 5); + Assert.assertEquals(Dfp25.of( 1), derivatives[0][0]); + Assert.assertEquals(Dfp25.of( 2), derivatives[1][0]); + Assert.assertEquals(Dfp25.of( 8), derivatives[2][0]); + Assert.assertEquals(Dfp25.of(-24), derivatives[3][0]); + Assert.assertEquals(Dfp25.of( 24), derivatives[4][0]); + Assert.assertEquals(Dfp25.of( 0), derivatives[5][0]); + derivatives = interpolator.derivatives(Dfp25.of(1), 5); + Assert.assertEquals(Dfp25.of( 4), derivatives[0][0]); + Assert.assertEquals(Dfp25.of( 2), derivatives[1][0]); + Assert.assertEquals(Dfp25.of( -4), derivatives[2][0]); + Assert.assertEquals(Dfp25.of( 0), derivatives[3][0]); + Assert.assertEquals(Dfp25.of( 24), derivatives[4][0]); + Assert.assertEquals(Dfp25.of( 0), derivatives[5][0]); + derivatives = interpolator.derivatives(Dfp25.of(2), 5); + Assert.assertEquals(Dfp25.of( 5), derivatives[0][0]); + Assert.assertEquals(Dfp25.of( 2), derivatives[1][0]); + Assert.assertEquals(Dfp25.of( 8), derivatives[2][0]); + Assert.assertEquals(Dfp25.of( 24), derivatives[3][0]); + Assert.assertEquals(Dfp25.of( 24), derivatives[4][0]); + Assert.assertEquals(Dfp25.of( 0), derivatives[5][0]); } @Test @@ -213,38 +213,38 @@ public class FieldHermiteInterpolatorTest { public void testWikipedia() { // this test corresponds to the example from Wikipedia page: // http://en.wikipedia.org/wiki/Hermite_interpolation - FieldHermiteInterpolator<BigFraction> interpolator = new FieldHermiteInterpolator<>(); - interpolator.addSamplePoint(new BigFraction(-1), - new BigFraction[] { new BigFraction( 2) }, - new BigFraction[] { new BigFraction(-8) }, - new BigFraction[] { new BigFraction(56) }); - interpolator.addSamplePoint(new BigFraction( 0), - new BigFraction[] { new BigFraction( 1) }, - new BigFraction[] { new BigFraction( 0) }, - new BigFraction[] { new BigFraction( 0) }); - interpolator.addSamplePoint(new BigFraction( 1), - new BigFraction[] { new BigFraction( 2) }, - new BigFraction[] { new BigFraction( 8) }, - new BigFraction[] { new BigFraction(56) }); - for (BigFraction x = new BigFraction(-1); x.doubleValue() <= 1.0; x = x.add(new BigFraction(1, 8))) { - BigFraction y = interpolator.value(x)[0]; - BigFraction x2 = x.multiply(x); - BigFraction x4 = x2.multiply(x2); - BigFraction x8 = x4.multiply(x4); - Assert.assertEquals(x8.add(new BigFraction(1)), y); + FieldHermiteInterpolator<Dfp> interpolator = new FieldHermiteInterpolator<>(); + interpolator.addSamplePoint(Dfp25.of(-1), + new Dfp[] { Dfp25.of( 2) }, + new Dfp[] { Dfp25.of(-8) }, + new Dfp[] { Dfp25.of(56) }); + interpolator.addSamplePoint(Dfp25.of( 0), + new Dfp[] { Dfp25.of( 1) }, + new Dfp[] { Dfp25.of( 0) }, + new Dfp[] { Dfp25.of( 0) }); + interpolator.addSamplePoint(Dfp25.of( 1), + new Dfp[] { Dfp25.of( 2) }, + new Dfp[] { Dfp25.of( 8) }, + new Dfp[] { Dfp25.of(56) }); + for (Dfp x = Dfp25.of(-1); x.toDouble() <= 1.0; x = x.add(Dfp25.of(1, 8))) { + Dfp y = interpolator.value(x)[0]; + Dfp x2 = x.multiply(x); + Dfp x4 = x2.multiply(x2); + Dfp x8 = x4.multiply(x4); + Assert.assertEquals(x8.add(Dfp25.of(1)), y); } } @Test public void testOnePointParabola() { - FieldHermiteInterpolator<BigFraction> interpolator = new FieldHermiteInterpolator<>(); - interpolator.addSamplePoint(new BigFraction(0), - new BigFraction[] { new BigFraction(1) }, - new BigFraction[] { new BigFraction(1) }, - new BigFraction[] { new BigFraction(2) }); - for (BigFraction x = new BigFraction(-1); x.doubleValue() <= 1.0; x = x.add(new BigFraction(1, 8))) { - BigFraction y = interpolator.value(x)[0]; - Assert.assertEquals(BigFraction.ONE.add(x.multiply(BigFraction.ONE.add(x))), y); + FieldHermiteInterpolator<Dfp> interpolator = new FieldHermiteInterpolator<>(); + interpolator.addSamplePoint(Dfp25.of(0), + new Dfp[] { Dfp25.of(1) }, + new Dfp[] { Dfp25.of(1) }, + new Dfp[] { Dfp25.of(2) }); + for (Dfp x = Dfp25.of(-1); x.toDouble() <= 1.0; x = x.add(Dfp25.of(1, 8))) { + Dfp y = interpolator.value(x)[0]; + Assert.assertEquals(Dfp25.ONE.add(x.multiply(Dfp25.ONE.add(x))), y); } } @@ -258,19 +258,19 @@ public class FieldHermiteInterpolatorTest { @Test(expected=NoDataException.class) public void testEmptySampleValue() { - new FieldHermiteInterpolator<BigFraction>().value(BigFraction.ZERO); + new FieldHermiteInterpolator<Dfp>().value(Dfp25.ZERO); } @Test(expected=NoDataException.class) public void testEmptySampleDerivative() { - new FieldHermiteInterpolator<BigFraction>().derivatives(BigFraction.ZERO, 1); + new FieldHermiteInterpolator<Dfp>().derivatives(Dfp25.ZERO, 1); } @Test(expected=MathIllegalArgumentException.class) public void testDuplicatedAbscissa() { - FieldHermiteInterpolator<BigFraction> interpolator = new FieldHermiteInterpolator<>(); - interpolator.addSamplePoint(new BigFraction(1), new BigFraction[] { new BigFraction(0) }); - interpolator.addSamplePoint(new BigFraction(1), new BigFraction[] { new BigFraction(1) }); + FieldHermiteInterpolator<Dfp> interpolator = new FieldHermiteInterpolator<>(); + interpolator.addSamplePoint(Dfp25.of(1), new Dfp[] { Dfp25.of(0) }); + interpolator.addSamplePoint(Dfp25.of(1), new Dfp[] { Dfp25.of(1) }); } } diff --git a/src/test/java/org/apache/commons/math4/fraction/BigFractionFieldTest.java b/src/test/java/org/apache/commons/math4/fraction/BigFractionFieldTest.java deleted file mode 100644 index ccbd6eb..0000000 --- a/src/test/java/org/apache/commons/math4/fraction/BigFractionFieldTest.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.math4.fraction; - - -import org.apache.commons.math4.TestUtils; -import org.apache.commons.math4.fraction.BigFraction; -import org.apache.commons.math4.fraction.BigFractionField; -import org.junit.Assert; -import org.junit.Test; - -public class BigFractionFieldTest { - - @Test - public void testZero() { - Assert.assertEquals(BigFraction.ZERO, BigFractionField.getInstance().getZero()); - } - - @Test - public void testOne() { - Assert.assertEquals(BigFraction.ONE, BigFractionField.getInstance().getOne()); - } - - @Test - public void testSerial() { - // deserializing the singleton should give the singleton itself back - BigFractionField field = BigFractionField.getInstance(); - Assert.assertTrue(field == TestUtils.serializeAndRecover(field)); - } - -} diff --git a/src/test/java/org/apache/commons/math4/fraction/BigFractionTest.java b/src/test/java/org/apache/commons/math4/fraction/BigFractionTest.java deleted file mode 100644 index bba0cd4..0000000 --- a/src/test/java/org/apache/commons/math4/fraction/BigFractionTest.java +++ /dev/null @@ -1,643 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.math4.fraction; - -import java.math.BigDecimal; -import java.math.BigInteger; - -import org.apache.commons.math4.TestUtils; -import org.apache.commons.math4.exception.ConvergenceException; -import org.apache.commons.math4.exception.MathArithmeticException; -import org.apache.commons.math4.exception.MathIllegalArgumentException; -import org.apache.commons.math4.exception.NullArgumentException; -import org.apache.commons.math4.exception.ZeroException; -import org.apache.commons.math4.fraction.BigFraction; -import org.apache.commons.math4.fraction.FractionConversionException; -import org.apache.commons.math4.util.FastMath; -import org.junit.Assert; -import org.junit.Test; - - -public class BigFractionTest { - - private void assertFraction(int expectedNumerator, int expectedDenominator, BigFraction actual) { - Assert.assertEquals(expectedNumerator, actual.getNumeratorAsInt()); - Assert.assertEquals(expectedDenominator, actual.getDenominatorAsInt()); - } - - private void assertFraction(long expectedNumerator, long expectedDenominator, BigFraction actual) { - Assert.assertEquals(expectedNumerator, actual.getNumeratorAsLong()); - Assert.assertEquals(expectedDenominator, actual.getDenominatorAsLong()); - } - - @Test - public void testConstructor() { - assertFraction(0, 1, new BigFraction(0, 1)); - assertFraction(0, 1, new BigFraction(0l, 2l)); - assertFraction(0, 1, new BigFraction(0, -1)); - assertFraction(1, 2, new BigFraction(1, 2)); - assertFraction(1, 2, new BigFraction(2, 4)); - assertFraction(-1, 2, new BigFraction(-1, 2)); - assertFraction(-1, 2, new BigFraction(1, -2)); - assertFraction(-1, 2, new BigFraction(-2, 4)); - assertFraction(-1, 2, new BigFraction(2, -4)); - assertFraction(11, 1, new BigFraction(11)); - assertFraction(11, 1, new BigFraction(11l)); - assertFraction(11, 1, new BigFraction(new BigInteger("11"))); - - assertFraction(0, 1, new BigFraction(0.00000000000001, 1.0e-5, 100)); - assertFraction(2, 5, new BigFraction(0.40000000000001, 1.0e-5, 100)); - assertFraction(15, 1, new BigFraction(15.0000000000001, 1.0e-5, 100)); - - Assert.assertEquals(0.00000000000001, new BigFraction(0.00000000000001).doubleValue(), 0.0); - Assert.assertEquals(0.40000000000001, new BigFraction(0.40000000000001).doubleValue(), 0.0); - Assert.assertEquals(15.0000000000001, new BigFraction(15.0000000000001).doubleValue(), 0.0); - assertFraction(3602879701896487l, 9007199254740992l, new BigFraction(0.40000000000001)); - assertFraction(1055531162664967l, 70368744177664l, new BigFraction(15.0000000000001)); - try { - new BigFraction(null, BigInteger.ONE); - Assert.fail("Expecting NullArgumentException"); - } catch (NullArgumentException npe) { - // expected - } - try { - new BigFraction(BigInteger.ONE, null); - Assert.fail("Expecting NullArgumentException"); - } catch (NullArgumentException npe) { - // expected - } - try { - new BigFraction(BigInteger.ONE, BigInteger.ZERO); - Assert.fail("Expecting ZeroException"); - } catch (ZeroException npe) { - // expected - } - try { - new BigFraction(2.0 * Integer.MAX_VALUE, 1.0e-5, 100000); - Assert.fail("Expecting FractionConversionException"); - } catch (FractionConversionException fce) { - // expected - } - } - - @Test(expected=ConvergenceException.class) - public void testGoldenRatio() { - // the golden ratio is notoriously a difficult number for continuous fraction - new BigFraction((1 + FastMath.sqrt(5)) / 2, 1.0e-12, 25); - } - - // MATH-179 - @Test - public void testDoubleConstructor() throws ConvergenceException { - assertFraction(1, 2, new BigFraction((double) 1 / (double) 2, 1.0e-5, 100)); - assertFraction(1, 3, new BigFraction((double) 1 / (double) 3, 1.0e-5, 100)); - assertFraction(2, 3, new BigFraction((double) 2 / (double) 3, 1.0e-5, 100)); - assertFraction(1, 4, new BigFraction((double) 1 / (double) 4, 1.0e-5, 100)); - assertFraction(3, 4, new BigFraction((double) 3 / (double) 4, 1.0e-5, 100)); - assertFraction(1, 5, new BigFraction((double) 1 / (double) 5, 1.0e-5, 100)); - assertFraction(2, 5, new BigFraction((double) 2 / (double) 5, 1.0e-5, 100)); - assertFraction(3, 5, new BigFraction((double) 3 / (double) 5, 1.0e-5, 100)); - assertFraction(4, 5, new BigFraction((double) 4 / (double) 5, 1.0e-5, 100)); - assertFraction(1, 6, new BigFraction((double) 1 / (double) 6, 1.0e-5, 100)); - assertFraction(5, 6, new BigFraction((double) 5 / (double) 6, 1.0e-5, 100)); - assertFraction(1, 7, new BigFraction((double) 1 / (double) 7, 1.0e-5, 100)); - assertFraction(2, 7, new BigFraction((double) 2 / (double) 7, 1.0e-5, 100)); - assertFraction(3, 7, new BigFraction((double) 3 / (double) 7, 1.0e-5, 100)); - assertFraction(4, 7, new BigFraction((double) 4 / (double) 7, 1.0e-5, 100)); - assertFraction(5, 7, new BigFraction((double) 5 / (double) 7, 1.0e-5, 100)); - assertFraction(6, 7, new BigFraction((double) 6 / (double) 7, 1.0e-5, 100)); - assertFraction(1, 8, new BigFraction((double) 1 / (double) 8, 1.0e-5, 100)); - assertFraction(3, 8, new BigFraction((double) 3 / (double) 8, 1.0e-5, 100)); - assertFraction(5, 8, new BigFraction((double) 5 / (double) 8, 1.0e-5, 100)); - assertFraction(7, 8, new BigFraction((double) 7 / (double) 8, 1.0e-5, 100)); - assertFraction(1, 9, new BigFraction((double) 1 / (double) 9, 1.0e-5, 100)); - assertFraction(2, 9, new BigFraction((double) 2 / (double) 9, 1.0e-5, 100)); - assertFraction(4, 9, new BigFraction((double) 4 / (double) 9, 1.0e-5, 100)); - assertFraction(5, 9, new BigFraction((double) 5 / (double) 9, 1.0e-5, 100)); - assertFraction(7, 9, new BigFraction((double) 7 / (double) 9, 1.0e-5, 100)); - assertFraction(8, 9, new BigFraction((double) 8 / (double) 9, 1.0e-5, 100)); - assertFraction(1, 10, new BigFraction((double) 1 / (double) 10, 1.0e-5, 100)); - assertFraction(3, 10, new BigFraction((double) 3 / (double) 10, 1.0e-5, 100)); - assertFraction(7, 10, new BigFraction((double) 7 / (double) 10, 1.0e-5, 100)); - assertFraction(9, 10, new BigFraction((double) 9 / (double) 10, 1.0e-5, 100)); - assertFraction(1, 11, new BigFraction((double) 1 / (double) 11, 1.0e-5, 100)); - assertFraction(2, 11, new BigFraction((double) 2 / (double) 11, 1.0e-5, 100)); - assertFraction(3, 11, new BigFraction((double) 3 / (double) 11, 1.0e-5, 100)); - assertFraction(4, 11, new BigFraction((double) 4 / (double) 11, 1.0e-5, 100)); - assertFraction(5, 11, new BigFraction((double) 5 / (double) 11, 1.0e-5, 100)); - assertFraction(6, 11, new BigFraction((double) 6 / (double) 11, 1.0e-5, 100)); - assertFraction(7, 11, new BigFraction((double) 7 / (double) 11, 1.0e-5, 100)); - assertFraction(8, 11, new BigFraction((double) 8 / (double) 11, 1.0e-5, 100)); - assertFraction(9, 11, new BigFraction((double) 9 / (double) 11, 1.0e-5, 100)); - assertFraction(10, 11, new BigFraction((double) 10 / (double) 11, 1.0e-5, 100)); - } - - // MATH-181 - @Test - public void testDigitLimitConstructor() throws ConvergenceException { - assertFraction(2, 5, new BigFraction(0.4, 9)); - assertFraction(2, 5, new BigFraction(0.4, 99)); - assertFraction(2, 5, new BigFraction(0.4, 999)); - - assertFraction(3, 5, new BigFraction(0.6152, 9)); - assertFraction(8, 13, new BigFraction(0.6152, 99)); - assertFraction(510, 829, new BigFraction(0.6152, 999)); - assertFraction(769, 1250, new BigFraction(0.6152, 9999)); - - // MATH-996 - assertFraction(1, 2, new BigFraction(0.5000000001, 10)); - } - - // MATH-1029 - @Test(expected=FractionConversionException.class) - public void testPositiveValueOverflow() { - assertFraction((long) 1e10, 1, new BigFraction(1e10, 1000)); - } - - // MATH-1029 - @Test(expected=FractionConversionException.class) - public void testNegativeValueOverflow() { - assertFraction((long) -1e10, 1, new BigFraction(-1e10, 1000)); - } - - @Test - public void testEpsilonLimitConstructor() throws ConvergenceException { - assertFraction(2, 5, new BigFraction(0.4, 1.0e-5, 100)); - - assertFraction(3, 5, new BigFraction(0.6152, 0.02, 100)); - assertFraction(8, 13, new BigFraction(0.6152, 1.0e-3, 100)); - assertFraction(251, 408, new BigFraction(0.6152, 1.0e-4, 100)); - assertFraction(251, 408, new BigFraction(0.6152, 1.0e-5, 100)); - assertFraction(510, 829, new BigFraction(0.6152, 1.0e-6, 100)); - assertFraction(769, 1250, new BigFraction(0.6152, 1.0e-7, 100)); - } - - @Test - public void testCompareTo() { - BigFraction first = new BigFraction(1, 2); - BigFraction second = new BigFraction(1, 3); - BigFraction third = new BigFraction(1, 2); - - Assert.assertEquals(0, first.compareTo(first)); - Assert.assertEquals(0, first.compareTo(third)); - Assert.assertEquals(1, first.compareTo(second)); - Assert.assertEquals(-1, second.compareTo(first)); - - // these two values are different approximations of PI - // the first one is approximately PI - 3.07e-18 - // the second one is approximately PI + 1.936e-17 - BigFraction pi1 = new BigFraction(1068966896, 340262731); - BigFraction pi2 = new BigFraction( 411557987, 131002976); - Assert.assertEquals(-1, pi1.compareTo(pi2)); - Assert.assertEquals( 1, pi2.compareTo(pi1)); - Assert.assertEquals(0.0, pi1.doubleValue() - pi2.doubleValue(), 1.0e-20); - - } - - @Test - public void testDoubleValue() { - BigFraction first = new BigFraction(1, 2); - BigFraction second = new BigFraction(1, 3); - - Assert.assertEquals(0.5, first.doubleValue(), 0.0); - Assert.assertEquals(1.0 / 3.0, second.doubleValue(), 0.0); - } - - // MATH-744 - @Test - public void testDoubleValueForLargeNumeratorAndDenominator() { - final BigInteger pow400 = BigInteger.TEN.pow(400); - final BigInteger pow401 = BigInteger.TEN.pow(401); - final BigInteger two = new BigInteger("2"); - final BigFraction large = new BigFraction(pow401.add(BigInteger.ONE), - pow400.multiply(two)); - - Assert.assertEquals(5, large.doubleValue(), 1e-15); - } - - // MATH-744 - @Test - public void testFloatValueForLargeNumeratorAndDenominator() { - final BigInteger pow400 = BigInteger.TEN.pow(400); - final BigInteger pow401 = BigInteger.TEN.pow(401); - final BigInteger two = new BigInteger("2"); - final BigFraction large = new BigFraction(pow401.add(BigInteger.ONE), - pow400.multiply(two)); - - Assert.assertEquals(5, large.floatValue(), 1e-15); - } - - @Test - public void testFloatValue() { - BigFraction first = new BigFraction(1, 2); - BigFraction second = new BigFraction(1, 3); - - Assert.assertEquals(0.5f, first.floatValue(), 0.0f); - Assert.assertEquals((float) (1.0 / 3.0), second.floatValue(), 0.0f); - } - - @Test - public void testIntValue() { - BigFraction first = new BigFraction(1, 2); - BigFraction second = new BigFraction(3, 2); - - Assert.assertEquals(0, first.intValue()); - Assert.assertEquals(1, second.intValue()); - } - - @Test - public void testLongValue() { - BigFraction first = new BigFraction(1, 2); - BigFraction second = new BigFraction(3, 2); - - Assert.assertEquals(0L, first.longValue()); - Assert.assertEquals(1L, second.longValue()); - } - - @Test - public void testConstructorDouble() { - assertFraction(1, 2, new BigFraction(0.5)); - assertFraction(6004799503160661l, 18014398509481984l, new BigFraction(1.0 / 3.0)); - assertFraction(6124895493223875l, 36028797018963968l, new BigFraction(17.0 / 100.0)); - assertFraction(1784551352345559l, 562949953421312l, new BigFraction(317.0 / 100.0)); - assertFraction(-1, 2, new BigFraction(-0.5)); - assertFraction(-6004799503160661l, 18014398509481984l, new BigFraction(-1.0 / 3.0)); - assertFraction(-6124895493223875l, 36028797018963968l, new BigFraction(17.0 / -100.0)); - assertFraction(-1784551352345559l, 562949953421312l, new BigFraction(-317.0 / 100.0)); - for (double v : new double[] { Double.NaN, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY}) { - try { - new BigFraction(v); - Assert.fail("Expecting MathIllegalArgumentException"); - } catch (MathIllegalArgumentException iae) { - // expected - } - } - Assert.assertEquals(1l, new BigFraction(Double.MAX_VALUE).getDenominatorAsLong()); - Assert.assertEquals(1l, new BigFraction(Double.longBitsToDouble(0x0010000000000000L)).getNumeratorAsLong()); - Assert.assertEquals(1l, new BigFraction(Double.MIN_VALUE).getNumeratorAsLong()); - } - - @Test - public void testAbs() { - BigFraction a = new BigFraction(10, 21); - BigFraction b = new BigFraction(-10, 21); - BigFraction c = new BigFraction(10, -21); - - assertFraction(10, 21, a.abs()); - assertFraction(10, 21, b.abs()); - assertFraction(10, 21, c.abs()); - } - - @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()); - } - - @Test - public void testNegate() { - BigFraction f = null; - - f = new BigFraction(50, 75); - f = f.negate(); - Assert.assertEquals(-2, f.getNumeratorAsInt()); - Assert.assertEquals(3, f.getDenominatorAsInt()); - - f = new BigFraction(-50, 75); - f = f.negate(); - Assert.assertEquals(2, f.getNumeratorAsInt()); - Assert.assertEquals(3, f.getDenominatorAsInt()); - - // large values - f = new BigFraction(Integer.MAX_VALUE - 1, Integer.MAX_VALUE); - f = f.negate(); - Assert.assertEquals(Integer.MIN_VALUE + 2, f.getNumeratorAsInt()); - Assert.assertEquals(Integer.MAX_VALUE, f.getDenominatorAsInt()); - - } - - @Test - public void testAdd() { - BigFraction a = new BigFraction(1, 2); - BigFraction b = new BigFraction(2, 3); - - assertFraction(1, 1, a.add(a)); - assertFraction(7, 6, a.add(b)); - assertFraction(7, 6, b.add(a)); - assertFraction(4, 3, b.add(b)); - - BigFraction f1 = new BigFraction(Integer.MAX_VALUE - 1, 1); - BigFraction f2 = BigFraction.ONE; - BigFraction f = f1.add(f2); - Assert.assertEquals(Integer.MAX_VALUE, f.getNumeratorAsInt()); - Assert.assertEquals(1, f.getDenominatorAsInt()); - - f1 = new BigFraction(-1, 13 * 13 * 2 * 2); - f2 = new BigFraction(-2, 13 * 17 * 2); - f = f1.add(f2); - Assert.assertEquals(13 * 13 * 17 * 2 * 2, f.getDenominatorAsInt()); - Assert.assertEquals(-17 - 2 * 13 * 2, f.getNumeratorAsInt()); - - try { - f.add((BigFraction) null); - Assert.fail("expecting NullArgumentException"); - } catch (NullArgumentException ex) { - } - - // if this fraction is added naively, it will overflow. - // check that it doesn't. - f1 = new BigFraction(1, 32768 * 3); - f2 = new BigFraction(1, 59049); - f = f1.add(f2); - Assert.assertEquals(52451, f.getNumeratorAsInt()); - Assert.assertEquals(1934917632, f.getDenominatorAsInt()); - - f1 = new BigFraction(Integer.MIN_VALUE, 3); - f2 = new BigFraction(1, 3); - f = f1.add(f2); - Assert.assertEquals(Integer.MIN_VALUE + 1, f.getNumeratorAsInt()); - Assert.assertEquals(3, f.getDenominatorAsInt()); - - f1 = new BigFraction(Integer.MAX_VALUE - 1, 1); - f = f1.add(BigInteger.ONE); - Assert.assertEquals(Integer.MAX_VALUE, f.getNumeratorAsInt()); - Assert.assertEquals(1, f.getDenominatorAsInt()); - - f = f.add(BigInteger.ZERO); - Assert.assertEquals(Integer.MAX_VALUE, f.getNumeratorAsInt()); - Assert.assertEquals(1, f.getDenominatorAsInt()); - - f1 = new BigFraction(Integer.MAX_VALUE - 1, 1); - f = f1.add(1); - Assert.assertEquals(Integer.MAX_VALUE, f.getNumeratorAsInt()); - Assert.assertEquals(1, f.getDenominatorAsInt()); - - f = f.add(0); - Assert.assertEquals(Integer.MAX_VALUE, f.getNumeratorAsInt()); - Assert.assertEquals(1, f.getDenominatorAsInt()); - - f1 = new BigFraction(Integer.MAX_VALUE - 1, 1); - f = f1.add(1l); - Assert.assertEquals(Integer.MAX_VALUE, f.getNumeratorAsInt()); - Assert.assertEquals(1, f.getDenominatorAsInt()); - - f = f.add(0l); - Assert.assertEquals(Integer.MAX_VALUE, f.getNumeratorAsInt()); - Assert.assertEquals(1, f.getDenominatorAsInt()); - - } - - @Test - public void testDivide() { - BigFraction a = new BigFraction(1, 2); - BigFraction b = new BigFraction(2, 3); - - assertFraction(1, 1, a.divide(a)); - assertFraction(3, 4, a.divide(b)); - assertFraction(4, 3, b.divide(a)); - assertFraction(1, 1, b.divide(b)); - - BigFraction f1 = new BigFraction(3, 5); - BigFraction f2 = BigFraction.ZERO; - try { - f1.divide(f2); - Assert.fail("expecting MathArithmeticException"); - } catch (MathArithmeticException ex) { - } - - f1 = new BigFraction(0, 5); - f2 = new BigFraction(2, 7); - BigFraction f = f1.divide(f2); - Assert.assertSame(BigFraction.ZERO, f); - - f1 = new BigFraction(2, 7); - f2 = BigFraction.ONE; - f = f1.divide(f2); - Assert.assertEquals(2, f.getNumeratorAsInt()); - Assert.assertEquals(7, f.getDenominatorAsInt()); - - f1 = new BigFraction(1, Integer.MAX_VALUE); - f = f1.divide(f1); - Assert.assertEquals(1, f.getNumeratorAsInt()); - Assert.assertEquals(1, f.getDenominatorAsInt()); - - f1 = new BigFraction(Integer.MIN_VALUE, Integer.MAX_VALUE); - f2 = new BigFraction(1, Integer.MAX_VALUE); - f = f1.divide(f2); - Assert.assertEquals(Integer.MIN_VALUE, f.getNumeratorAsInt()); - Assert.assertEquals(1, f.getDenominatorAsInt()); - - try { - f.divide((BigFraction) null); - Assert.fail("expecting NullArgumentException"); - } catch (NullArgumentException ex) { - } - - f1 = new BigFraction(Integer.MIN_VALUE, Integer.MAX_VALUE); - f = f1.divide(BigInteger.valueOf(Integer.MIN_VALUE)); - Assert.assertEquals(Integer.MAX_VALUE, f.getDenominatorAsInt()); - Assert.assertEquals(1, f.getNumeratorAsInt()); - - f1 = new BigFraction(Integer.MIN_VALUE, Integer.MAX_VALUE); - f = f1.divide(Integer.MIN_VALUE); - Assert.assertEquals(Integer.MAX_VALUE, f.getDenominatorAsInt()); - Assert.assertEquals(1, f.getNumeratorAsInt()); - - f1 = new BigFraction(Integer.MIN_VALUE, Integer.MAX_VALUE); - f = f1.divide((long) Integer.MIN_VALUE); - Assert.assertEquals(Integer.MAX_VALUE, f.getDenominatorAsInt()); - Assert.assertEquals(1, f.getNumeratorAsInt()); - - } - - @Test - public void testMultiply() { - BigFraction a = new BigFraction(1, 2); - BigFraction b = new BigFraction(2, 3); - - assertFraction(1, 4, a.multiply(a)); - assertFraction(1, 3, a.multiply(b)); - assertFraction(1, 3, b.multiply(a)); - assertFraction(4, 9, b.multiply(b)); - - BigFraction f1 = new BigFraction(Integer.MAX_VALUE, 1); - BigFraction f2 = new BigFraction(Integer.MIN_VALUE, Integer.MAX_VALUE); - BigFraction f = f1.multiply(f2); - Assert.assertEquals(Integer.MIN_VALUE, f.getNumeratorAsInt()); - Assert.assertEquals(1, f.getDenominatorAsInt()); - - f = f2.multiply(Integer.MAX_VALUE); - Assert.assertEquals(Integer.MIN_VALUE, f.getNumeratorAsInt()); - Assert.assertEquals(1, f.getDenominatorAsInt()); - - f = f2.multiply((long) Integer.MAX_VALUE); - Assert.assertEquals(Integer.MIN_VALUE, f.getNumeratorAsInt()); - Assert.assertEquals(1, f.getDenominatorAsInt()); - - try { - f.multiply((BigFraction) null); - Assert.fail("expecting NullArgumentException"); - } catch (NullArgumentException ex) { - } - - } - - @Test - public void testSubtract() { - BigFraction a = new BigFraction(1, 2); - BigFraction b = new BigFraction(2, 3); - - assertFraction(0, 1, a.subtract(a)); - assertFraction(-1, 6, a.subtract(b)); - assertFraction(1, 6, b.subtract(a)); - assertFraction(0, 1, b.subtract(b)); - - BigFraction f = new BigFraction(1, 1); - try { - f.subtract((BigFraction) null); - Assert.fail("expecting NullArgumentException"); - } catch (NullArgumentException ex) { - } - - // if this fraction is subtracted naively, it will overflow. - // check that it doesn't. - BigFraction f1 = new BigFraction(1, 32768 * 3); - BigFraction f2 = new BigFraction(1, 59049); - f = f1.subtract(f2); - Assert.assertEquals(-13085, f.getNumeratorAsInt()); - Assert.assertEquals(1934917632, f.getDenominatorAsInt()); - - f1 = new BigFraction(Integer.MIN_VALUE, 3); - f2 = new BigFraction(1, 3).negate(); - f = f1.subtract(f2); - Assert.assertEquals(Integer.MIN_VALUE + 1, f.getNumeratorAsInt()); - Assert.assertEquals(3, f.getDenominatorAsInt()); - - f1 = new BigFraction(Integer.MAX_VALUE, 1); - f2 = BigFraction.ONE; - f = f1.subtract(f2); - Assert.assertEquals(Integer.MAX_VALUE - 1, f.getNumeratorAsInt()); - Assert.assertEquals(1, f.getDenominatorAsInt()); - - } - - @Test - public void testBigDecimalValue() { - Assert.assertEquals(new BigDecimal(0.5), new BigFraction(1, 2).bigDecimalValue()); - Assert.assertEquals(new BigDecimal("0.0003"), new BigFraction(3, 10000).bigDecimalValue()); - Assert.assertEquals(new BigDecimal("0"), new BigFraction(1, 3).bigDecimalValue(BigDecimal.ROUND_DOWN)); - Assert.assertEquals(new BigDecimal("0.333"), new BigFraction(1, 3).bigDecimalValue(3, BigDecimal.ROUND_DOWN)); - } - - @Test - public void testEqualsAndHashCode() { - BigFraction zero = new BigFraction(0, 1); - BigFraction nullFraction = null; - Assert.assertTrue(zero.equals(zero)); - Assert.assertFalse(zero.equals(nullFraction)); - Assert.assertFalse(zero.equals(Double.valueOf(0))); - BigFraction zero2 = new BigFraction(0, 2); - Assert.assertTrue(zero.equals(zero2)); - Assert.assertEquals(zero.hashCode(), zero2.hashCode()); - BigFraction one = new BigFraction(1, 1); - Assert.assertFalse((one.equals(zero) || zero.equals(one))); - Assert.assertTrue(one.equals(BigFraction.ONE)); - } - - @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); - } - - @Test - public void testPercentage() { - Assert.assertEquals(50.0, new BigFraction(1, 2).percentageValue(), 1.0e-15); - } - - @Test - public void testPow() { - Assert.assertEquals(new BigFraction(8192, 1594323), new BigFraction(2, 3).pow(13)); - Assert.assertEquals(new BigFraction(8192, 1594323), new BigFraction(2, 3).pow(13l)); - Assert.assertEquals(new BigFraction(8192, 1594323), new BigFraction(2, 3).pow(BigInteger.valueOf(13l))); - Assert.assertEquals(BigFraction.ONE, new BigFraction(2, 3).pow(0)); - Assert.assertEquals(BigFraction.ONE, new BigFraction(2, 3).pow(0l)); - Assert.assertEquals(BigFraction.ONE, new BigFraction(2, 3).pow(BigInteger.valueOf(0l))); - Assert.assertEquals(new BigFraction(1594323, 8192), new BigFraction(2, 3).pow(-13)); - Assert.assertEquals(new BigFraction(1594323, 8192), new BigFraction(2, 3).pow(-13l)); - Assert.assertEquals(new BigFraction(1594323, 8192), new BigFraction(2, 3).pow(BigInteger.valueOf(-13l))); - } - - @Test - public void testMath340() { - BigFraction fractionA = new BigFraction(0.00131); - BigFraction fractionB = new BigFraction(.37).reciprocal(); - BigFraction errorResult = fractionA.multiply(fractionB); - BigFraction correctResult = new BigFraction(fractionA.getNumerator().multiply(fractionB.getNumerator()), - fractionA.getDenominator().multiply(fractionB.getDenominator())); - Assert.assertEquals(correctResult, errorResult); - } - - @Test - public void testSerial() throws FractionConversionException { - BigFraction[] fractions = { - new BigFraction(3, 4), BigFraction.ONE, BigFraction.ZERO, - new BigFraction(17), new BigFraction(FastMath.PI, 1000), - new BigFraction(-5, 2) - }; - for (BigFraction fraction : fractions) { - Assert.assertEquals(fraction, TestUtils.serializeAndRecover(fraction)); - } - } - -} diff --git a/src/test/java/org/apache/commons/math4/linear/MatrixUtilsTest.java b/src/test/java/org/apache/commons/math4/linear/MatrixUtilsTest.java index d0a4931..10d49a2 100644 --- a/src/test/java/org/apache/commons/math4/linear/MatrixUtilsTest.java +++ b/src/test/java/org/apache/commons/math4/linear/MatrixUtilsTest.java @@ -22,7 +22,6 @@ import org.apache.commons.math4.TestUtils; import org.apache.commons.math4.exception.MathIllegalArgumentException; import org.apache.commons.math4.exception.NotStrictlyPositiveException; import org.apache.commons.math4.exception.NullArgumentException; -import org.apache.commons.math4.fraction.BigFraction; import org.apache.commons.math4.dfp.Dfp; import org.apache.commons.math4.dfp.DfpField; import org.apache.commons.math4.linear.Dfp25; @@ -261,19 +260,6 @@ public final class MatrixUtilsTest { } } - @Test - public void testBigDfpConverter() { - BigFraction[][] bfData = { - { new BigFraction(1), new BigFraction(2), new BigFraction(3) }, - { new BigFraction(2), new BigFraction(5), new BigFraction(3) }, - { new BigFraction(1), new BigFraction(0), new BigFraction(8) } - }; - FieldMatrix<BigFraction> m = new Array2DRowFieldMatrix<>(bfData, false); - RealMatrix converted = MatrixUtils.bigFractionMatrixToRealMatrix(m); - RealMatrix reference = new Array2DRowRealMatrix(testData, false); - Assert.assertEquals(0.0, converted.subtract(reference).getNorm(), 0.0); - } - public static final Dfp[][] asDfp(double[][] data) { Dfp d[][] = new Dfp[data.length][]; for (int i = 0; i < data.length; ++i) {