You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2019/12/03 11:58:15 UTC
[commons-math] branch master updated: MATH-1446: Removed "Fraction"
class (ported to "Commons Numbers").
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 b8d96de MATH-1446: Removed "Fraction" class (ported to "Commons Numbers").
b8d96de is described below
commit b8d96de5871d938f678b4702c2fcc306bdf1f34d
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
AuthorDate: Tue Dec 3 12:53:05 2019 +0100
MATH-1446: Removed "Fraction" class (ported to "Commons Numbers").
Functionality unused within "Commons Math" was also removed:
* Utility to build "FieldMatrix<Fraction>" instances.
* Utility to convert from "FieldMatrix<Fraction>" to "RealMatrix".
Class "Fraction" was used for testing "Field" functionalities.
Corresponding unit tests were refactored to use class "Dfp" instead.
A "dummy" class ("Dfp25") was introduced for that purpose (in "src/test").
Some tests, too tied to the field type being "Fraction", were modified.
---
src/changes/changes.xml | 3 +
.../apache/commons/math4/fraction/Fraction.java | 680 --------------------
.../commons/math4/fraction/FractionField.java | 85 ---
.../apache/commons/math4/linear/MatrixUtils.java | 47 --
.../commons/math4/fraction/FractionTest.java | 631 ------------------
.../commons/math4/linear/ArrayFieldVectorTest.java | 554 ++++++++--------
.../commons/math4/linear/BlockFieldMatrixTest.java | 712 +++++++++++----------
.../FractionFieldTest.java => linear/Dfp25.java} | 38 +-
.../math4/linear/FieldLUDecompositionTest.java | 194 +++---
.../commons/math4/linear/FieldLUSolverTest.java | 62 +-
.../commons/math4/linear/FieldMatrixImplTest.java | 501 ++++++++-------
.../commons/math4/linear/MatrixUtilsTest.java | 110 ++--
.../math4/linear/SparseFieldMatrixTest.java | 351 +++++-----
.../math4/linear/SparseFieldVectorTest.java | 399 ++++++------
.../commons/math4/util/OpenIntToFieldTest.java | 136 ++--
15 files changed, 1532 insertions(+), 2971 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 6aadf1e..eea884f 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-1446">
+ Removed class "Fraction" (ported to "Commons Numbers").
+ </action>
<action dev="erans" type="update" issue="MATH-1469">
Removed most codes in package "o.a.c.m.geometry".
"Commons Math" now depends on "Commons Geometry".
diff --git a/src/main/java/org/apache/commons/math4/fraction/Fraction.java b/src/main/java/org/apache/commons/math4/fraction/Fraction.java
deleted file mode 100644
index 4b236c1..0000000
--- a/src/main/java/org/apache/commons/math4/fraction/Fraction.java
+++ /dev/null
@@ -1,680 +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.BigInteger;
-
-import org.apache.commons.math4.FieldElement;
-import org.apache.commons.math4.exception.MathArithmeticException;
-import org.apache.commons.math4.exception.NullArgumentException;
-import org.apache.commons.math4.exception.util.LocalizedFormats;
-import org.apache.commons.numbers.core.ArithmeticUtils;
-import org.apache.commons.math4.util.FastMath;
-
-/**
- * Representation of a rational number.
- *
- * implements Serializable since 2.0
- *
- * @since 1.1
- */
-public class Fraction
- extends Number
- implements FieldElement<Fraction>, Comparable<Fraction>, Serializable {
-
- /** A fraction representing "2 / 1". */
- public static final Fraction TWO = new Fraction(2, 1);
-
- /** A fraction representing "1". */
- public static final Fraction ONE = new Fraction(1, 1);
-
- /** A fraction representing "0". */
- public static final Fraction ZERO = new Fraction(0, 1);
-
- /** A fraction representing "4/5". */
- public static final Fraction FOUR_FIFTHS = new Fraction(4, 5);
-
- /** A fraction representing "1/5". */
- public static final Fraction ONE_FIFTH = new Fraction(1, 5);
-
- /** A fraction representing "1/2". */
- public static final Fraction ONE_HALF = new Fraction(1, 2);
-
- /** A fraction representing "1/4". */
- public static final Fraction ONE_QUARTER = new Fraction(1, 4);
-
- /** A fraction representing "1/3". */
- public static final Fraction ONE_THIRD = new Fraction(1, 3);
-
- /** A fraction representing "3/5". */
- public static final Fraction THREE_FIFTHS = new Fraction(3, 5);
-
- /** A fraction representing "3/4". */
- public static final Fraction THREE_QUARTERS = new Fraction(3, 4);
-
- /** A fraction representing "2/5". */
- public static final Fraction TWO_FIFTHS = new Fraction(2, 5);
-
- /** A fraction representing "2/4". */
- public static final Fraction TWO_QUARTERS = new Fraction(2, 4);
-
- /** A fraction representing "2/3". */
- public static final Fraction TWO_THIRDS = new Fraction(2, 3);
-
- /** A fraction representing "-1 / 1". */
- public static final Fraction MINUS_ONE = new Fraction(-1, 1);
-
- /** Serializable version identifier */
- private static final long serialVersionUID = 3698073679419233275L;
-
- /** The default epsilon used for convergence. */
- private static final double DEFAULT_EPSILON = 1e-5;
-
- /** The denominator. */
- private final int denominator;
-
- /** The numerator. */
- private final int numerator;
-
- /**
- * Create a fraction given the double value.
- * @param value the double value to convert to a fraction.
- * @throws FractionConversionException if the continued fraction failed to
- * converge.
- */
- public Fraction(double value) throws FractionConversionException {
- this(value, DEFAULT_EPSILON, 100);
- }
-
- /**
- * 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} of {@code value}, in absolute terms.
- * @param maxIterations maximum number of convergents
- * @throws FractionConversionException if the continued fraction failed to
- * converge.
- */
- public Fraction(double value, double epsilon, int maxIterations)
- throws FractionConversionException
- {
- this(value, epsilon, Integer.MAX_VALUE, maxIterations);
- }
-
- /**
- * 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 Fraction(double value, int maxDenominator)
- throws FractionConversionException
- {
- this(value, 0, maxDenominator, 100);
- }
-
- /**
- * 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} of {@code value}, 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 Fraction(double value, double epsilon, 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) {
- this.numerator = (int) a0;
- this.denominator = 1;
- 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;
- double r1 = 1.0 / (r0 - a0);
- long a1 = (long)FastMath.floor(r1);
- p2 = (a1 * p1) + p0;
- q2 = (a1 * q1) + q0;
-
- if ((FastMath.abs(p2) > overflow) || (FastMath.abs(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);
- }
-
- 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) {
- this.numerator = (int) p2;
- this.denominator = (int) q2;
- } else {
- this.numerator = (int) p1;
- this.denominator = (int) q1;
- }
-
- }
-
- /**
- * Create a fraction from an int.
- * The fraction is num / 1.
- * @param num the numerator.
- */
- public Fraction(int num) {
- this(num, 1);
- }
-
- /**
- * Create a fraction given the numerator and denominator. The fraction is
- * reduced to lowest terms.
- * @param num the numerator.
- * @param den the denominator.
- * @throws MathArithmeticException if the denominator is {@code zero}
- */
- public Fraction(int num, int den) {
- if (den == 0) {
- throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR_IN_FRACTION,
- num, den);
- }
- if (den < 0) {
- if (num == Integer.MIN_VALUE ||
- den == Integer.MIN_VALUE) {
- throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_FRACTION,
- num, den);
- }
- num = -num;
- den = -den;
- }
- // reduce numerator and denominator by greatest common denominator.
- final int d = ArithmeticUtils.gcd(num, den);
- if (d > 1) {
- num /= d;
- den /= d;
- }
-
- // move sign to numerator.
- if (den < 0) {
- num = -num;
- den = -den;
- }
- this.numerator = num;
- this.denominator = den;
- }
-
- /**
- * Returns the absolute value of this fraction.
- * @return the absolute value.
- */
- public Fraction abs() {
- Fraction ret;
- if (numerator >= 0) {
- ret = this;
- } else {
- ret = negate();
- }
- return ret;
- }
-
- /**
- * Compares this object to another based on size.
- * @param object the object to compare to
- * @return -1 if this is less than {@code object}, +1 if this is greater
- * than {@code object}, 0 if they are equal.
- */
- @Override
- public int compareTo(Fraction object) {
- long nOd = ((long) numerator) * object.denominator;
- long dOn = ((long) denominator) * object.numerator;
- return (nOd < dOn) ? -1 : ((nOd > dOn) ? +1 : 0);
- }
-
- /**
- * Gets the fraction as a {@code double}. This calculates the fraction as
- * the numerator divided by denominator.
- * @return the fraction as a {@code double}
- */
- @Override
- public double doubleValue() {
- return (double)numerator / (double)denominator;
- }
-
- /**
- * 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.
- * @param other fraction to test for equality to this fraction
- * @return true if two fractions are equal, false if object is
- * {@code null}, not an instance of {@link Fraction}, or not equal
- * to this fraction instance.
- */
- @Override
- public boolean equals(Object other) {
- if (this == other) {
- return true;
- }
- if (other instanceof Fraction) {
- // since fractions are always in lowest terms, numerators and
- // denominators can be compared directly for equality.
- Fraction rhs = (Fraction)other;
- return (numerator == rhs.numerator) &&
- (denominator == rhs.denominator);
- }
- return false;
- }
-
- /**
- * Gets the fraction as a {@code float}. This calculates the fraction as
- * the numerator divided by denominator.
- * @return the fraction as a {@code float}
- */
- @Override
- public float floatValue() {
- return (float)doubleValue();
- }
-
- /**
- * Access the denominator.
- * @return the denominator.
- */
- public int getDenominator() {
- return denominator;
- }
-
- /**
- * Access the numerator.
- * @return the numerator.
- */
- public int getNumerator() {
- return numerator;
- }
-
- /**
- * Gets a hashCode for the fraction.
- * @return a hash code value for this object
- */
- @Override
- public int hashCode() {
- return 37 * (37 * 17 + numerator) + denominator;
- }
-
- /**
- * Gets the fraction as an {@code int}. This returns the whole number part
- * of the fraction.
- * @return the whole number fraction part
- */
- @Override
- public int intValue() {
- return (int)doubleValue();
- }
-
- /**
- * Gets the fraction as a {@code long}. This returns the whole number part
- * of the fraction.
- * @return the whole number fraction part
- */
- @Override
- public long longValue() {
- return (long)doubleValue();
- }
-
- /**
- * Return the additive inverse of this fraction.
- * @return the negation of this fraction.
- */
- @Override
- public Fraction negate() {
- if (numerator==Integer.MIN_VALUE) {
- throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_FRACTION, numerator, denominator);
- }
- return new Fraction(-numerator, denominator);
- }
-
- /**
- * Return the multiplicative inverse of this fraction.
- * @return the reciprocal fraction
- */
- @Override
- public Fraction reciprocal() {
- return new Fraction(denominator, numerator);
- }
-
- /**
- * <p>Adds the value of this fraction to another, returning the result in reduced form.
- * The algorithm follows Knuth, 4.5.1.</p>
- *
- * @param fraction the fraction to add, must not be {@code null}
- * @return a {@code Fraction} instance with the resulting values
- * @throws NullArgumentException if the fraction is {@code null}
- * @throws MathArithmeticException if the resulting numerator or denominator exceeds
- * {@code Integer.MAX_VALUE}
- */
- @Override
- public Fraction add(Fraction fraction) {
- return addSub(fraction, true /* add */);
- }
-
- /**
- * Add an integer to the fraction.
- * @param i the {@code integer} to add.
- * @return this + i
- */
- public Fraction add(final int i) {
- return new Fraction(numerator + i * denominator, denominator);
- }
-
- /**
- * <p>Subtracts the value of another fraction from the value of this one,
- * returning the result in reduced form.</p>
- *
- * @param fraction the fraction to subtract, must not be {@code null}
- * @return a {@code Fraction} instance with the resulting values
- * @throws NullArgumentException if the fraction is {@code null}
- * @throws MathArithmeticException if the resulting numerator or denominator
- * cannot be represented in an {@code int}.
- */
- @Override
- public Fraction subtract(Fraction fraction) {
- return addSub(fraction, false /* subtract */);
- }
-
- /**
- * Subtract an integer from the fraction.
- * @param i the {@code integer} to subtract.
- * @return this - i
- */
- public Fraction subtract(final int i) {
- return new Fraction(numerator - i * denominator, denominator);
- }
-
- /**
- * Implement add and subtract using algorithm described in Knuth 4.5.1.
- *
- * @param fraction the fraction to subtract, must not be {@code null}
- * @param isAdd true to add, false to subtract
- * @return a {@code Fraction} instance with the resulting values
- * @throws NullArgumentException if the fraction is {@code null}
- * @throws MathArithmeticException if the resulting numerator or denominator
- * cannot be represented in an {@code int}.
- */
- private Fraction addSub(Fraction fraction, boolean isAdd) {
- if (fraction == null) {
- throw new NullArgumentException(LocalizedFormats.FRACTION);
- }
- // zero is identity for addition.
- if (numerator == 0) {
- return isAdd ? fraction : fraction.negate();
- }
- if (fraction.numerator == 0) {
- return this;
- }
- // if denominators are randomly distributed, d1 will be 1 about 61%
- // of the time.
- int d1 = ArithmeticUtils.gcd(denominator, fraction.denominator);
- if (d1==1) {
- // result is ( (u*v' +/- u'v) / u'v')
- int uvp = Math.multiplyExact(numerator, fraction.denominator);
- int upv = Math.multiplyExact(fraction.numerator, denominator);
- return new Fraction
- (isAdd ? Math.addExact(uvp, upv) :
- Math.subtractExact(uvp, upv),
- Math.multiplyExact(denominator, fraction.denominator));
- }
- // the quantity 't' requires 65 bits of precision; see knuth 4.5.1
- // exercise 7. we're going to use a BigInteger.
- // t = u(v'/d1) +/- v(u'/d1)
- BigInteger uvp = BigInteger.valueOf(numerator)
- .multiply(BigInteger.valueOf(fraction.denominator/d1));
- BigInteger upv = BigInteger.valueOf(fraction.numerator)
- .multiply(BigInteger.valueOf(denominator/d1));
- BigInteger t = isAdd ? uvp.add(upv) : uvp.subtract(upv);
- // but d2 doesn't need extra precision because
- // d2 = gcd(t,d1) = gcd(t mod d1, d1)
- int tmodd1 = t.mod(BigInteger.valueOf(d1)).intValue();
- int d2 = (tmodd1==0)?d1:ArithmeticUtils.gcd(tmodd1, d1);
-
- // result is (t/d2) / (u'/d1)(v'/d2)
- BigInteger w = t.divide(BigInteger.valueOf(d2));
- if (w.bitLength() > 31) {
- throw new MathArithmeticException(LocalizedFormats.NUMERATOR_OVERFLOW_AFTER_MULTIPLY,
- w);
- }
- return new Fraction (w.intValue(),
- Math.multiplyExact(denominator/d1,
- fraction.denominator/d2));
- }
-
- /**
- * <p>Multiplies the value of this fraction by another, returning the
- * result in reduced form.</p>
- *
- * @param fraction the fraction to multiply by, must not be {@code null}
- * @return a {@code Fraction} instance with the resulting values
- * @throws NullArgumentException if the fraction is {@code null}
- * @throws MathArithmeticException if the resulting numerator or denominator exceeds
- * {@code Integer.MAX_VALUE}
- */
- @Override
- public Fraction multiply(Fraction fraction) {
- if (fraction == null) {
- throw new NullArgumentException(LocalizedFormats.FRACTION);
- }
- if (numerator == 0 || fraction.numerator == 0) {
- return ZERO;
- }
- // knuth 4.5.1
- // make sure we don't overflow unless the result *must* overflow.
- int d1 = ArithmeticUtils.gcd(numerator, fraction.denominator);
- int d2 = ArithmeticUtils.gcd(fraction.numerator, denominator);
- return getReducedFraction
- (Math.multiplyExact(numerator/d1, fraction.numerator/d2),
- Math.multiplyExact(denominator/d2, fraction.denominator/d1));
- }
-
- /**
- * Multiply the fraction by an integer.
- * @param i the {@code integer} to multiply by.
- * @return this * i
- */
- @Override
- public Fraction multiply(final int i) {
- return multiply(new Fraction(i));
- }
-
- /**
- * <p>Divide the value of this fraction by another.</p>
- *
- * @param fraction the fraction to divide by, must not be {@code null}
- * @return a {@code Fraction} instance with the resulting values
- * @throws IllegalArgumentException if the fraction is {@code null}
- * @throws MathArithmeticException if the fraction to divide by is zero
- * @throws MathArithmeticException if the resulting numerator or denominator exceeds
- * {@code Integer.MAX_VALUE}
- */
- @Override
- public Fraction divide(Fraction fraction) {
- if (fraction == null) {
- throw new NullArgumentException(LocalizedFormats.FRACTION);
- }
- if (fraction.numerator == 0) {
- throw new MathArithmeticException(LocalizedFormats.ZERO_FRACTION_TO_DIVIDE_BY,
- fraction.numerator, fraction.denominator);
- }
- return multiply(fraction.reciprocal());
- }
-
- /**
- * Divide the fraction by an integer.
- * @param i the {@code integer} to divide by.
- * @return this * i
- */
- public Fraction divide(final int i) {
- return divide(new Fraction(i));
- }
-
- /**
- * <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 100 * doubleValue();
- }
-
- /**
- * <p>Creates a {@code Fraction} 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 MathArithmeticException if the denominator is {@code zero}
- */
- public static Fraction getReducedFraction(int numerator, int denominator) {
- if (denominator == 0) {
- throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR_IN_FRACTION,
- numerator, denominator);
- }
- if (numerator==0) {
- return ZERO; // normalize zero.
- }
- // allow 2^k/-2^31 as a valid fraction (where k>0)
- if (denominator==Integer.MIN_VALUE && (numerator&1)==0) {
- numerator/=2; denominator/=2;
- }
- if (denominator < 0) {
- if (numerator==Integer.MIN_VALUE ||
- denominator==Integer.MIN_VALUE) {
- throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_FRACTION,
- numerator, denominator);
- }
- numerator = -numerator;
- denominator = -denominator;
- }
- // simplify fraction.
- int gcd = ArithmeticUtils.gcd(numerator, denominator);
- numerator /= gcd;
- denominator /= gcd;
- return new Fraction(numerator, denominator);
- }
-
- /**
- * <p>
- * Returns the {@code String} 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 (denominator == 1) {
- str = Integer.toString(numerator);
- } else if (numerator == 0) {
- str = "0";
- } else {
- str = numerator + " / " + denominator;
- }
- return str;
- }
-
- /** {@inheritDoc} */
- @Override
- public FractionField getField() {
- return FractionField.getInstance();
- }
-
-}
diff --git a/src/main/java/org/apache/commons/math4/fraction/FractionField.java b/src/main/java/org/apache/commons/math4/fraction/FractionField.java
deleted file mode 100644
index 0b513a5..0000000
--- a/src/main/java/org/apache/commons/math4/fraction/FractionField.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 field.
- * <p>
- * This class is a singleton.
- * </p>
- * @see Fraction
- * @since 2.0
- */
-public class FractionField implements Field<Fraction>, Serializable {
-
- /** Serializable version identifier */
- private static final long serialVersionUID = -1257768487499119313L;
-
- /** Private constructor for the singleton.
- */
- private FractionField() {
- }
-
- /** Get the unique instance.
- * @return the unique instance
- */
- public static FractionField getInstance() {
- return LazyHolder.INSTANCE;
- }
-
- /** {@inheritDoc} */
- @Override
- public Fraction getOne() {
- return Fraction.ONE;
- }
-
- /** {@inheritDoc} */
- @Override
- public Fraction getZero() {
- return Fraction.ZERO;
- }
-
- /** {@inheritDoc} */
- @Override
- public Class<? extends FieldElement<Fraction>> getRuntimeClass() {
- return Fraction.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 FractionField INSTANCE = new FractionField();
- }
- // 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 47d3017..9f88288 100644
--- a/src/main/java/org/apache/commons/math4/linear/MatrixUtils.java
+++ b/src/main/java/org/apache/commons/math4/linear/MatrixUtils.java
@@ -33,7 +33,6 @@ 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.fraction.Fraction;
import org.apache.commons.math4.util.FastMath;
import org.apache.commons.math4.util.MathArrays;
import org.apache.commons.math4.util.MathUtils;
@@ -532,8 +531,6 @@ public class MatrixUtils {
throw new NumberIsTooSmallException(LocalizedFormats.INITIAL_COLUMN_AFTER_FINAL_COLUMN,
endColumn, startColumn, false);
}
-
-
}
/**
@@ -611,50 +608,6 @@ public class MatrixUtils {
}
/**
- * Convert a {@link FieldMatrix}/{@link Fraction} matrix to a {@link RealMatrix}.
- * @param m Matrix to convert.
- * @return the converted matrix.
- */
- public static Array2DRowRealMatrix fractionMatrixToRealMatrix(final FieldMatrix<Fraction> m) {
- final FractionMatrixConverter converter = new FractionMatrixConverter();
- m.walkInOptimizedOrder(converter);
- return converter.getConvertedMatrix();
- }
-
- /** Converter for {@link FieldMatrix}/{@link Fraction}. */
- private static class FractionMatrixConverter extends DefaultFieldMatrixPreservingVisitor<Fraction> {
- /** Converted array. */
- private double[][] data;
- /** Simple constructor. */
- FractionMatrixConverter() {
- super(Fraction.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, Fraction value) {
- data[row][column] = value.doubleValue();
- }
-
- /**
- * Get the converted matrix.
- *
- * @return the converted matrix.
- */
- Array2DRowRealMatrix getConvertedMatrix() {
- return new Array2DRowRealMatrix(data, false);
- }
-
- }
-
- /**
* Convert a {@link FieldMatrix}/{@link BigFraction} matrix to a {@link RealMatrix}.
*
* @param m Matrix to convert.
diff --git a/src/test/java/org/apache/commons/math4/fraction/FractionTest.java b/src/test/java/org/apache/commons/math4/fraction/FractionTest.java
deleted file mode 100644
index 95a5368..0000000
--- a/src/test/java/org/apache/commons/math4/fraction/FractionTest.java
+++ /dev/null
@@ -1,631 +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.exception.ConvergenceException;
-import org.apache.commons.math4.exception.MathArithmeticException;
-import org.apache.commons.math4.exception.NullArgumentException;
-import org.apache.commons.math4.fraction.Fraction;
-import org.apache.commons.math4.fraction.FractionConversionException;
-import org.apache.commons.math4.util.FastMath;
-import org.junit.Assert;
-import org.junit.Test;
-
-
-/**
- */
-public class FractionTest {
-
- private void assertFraction(int expectedNumerator, int expectedDenominator, Fraction actual) {
- Assert.assertEquals(expectedNumerator, actual.getNumerator());
- Assert.assertEquals(expectedDenominator, actual.getDenominator());
- }
-
- @Test
- public void testConstructor() {
- assertFraction(0, 1, new Fraction(0, 1));
- assertFraction(0, 1, new Fraction(0, 2));
- assertFraction(0, 1, new Fraction(0, -1));
- assertFraction(1, 2, new Fraction(1, 2));
- assertFraction(1, 2, new Fraction(2, 4));
- assertFraction(-1, 2, new Fraction(-1, 2));
- assertFraction(-1, 2, new Fraction(1, -2));
- assertFraction(-1, 2, new Fraction(-2, 4));
- assertFraction(-1, 2, new Fraction(2, -4));
-
- // overflow
- try {
- new Fraction(Integer.MIN_VALUE, -1);
- Assert.fail();
- } catch (MathArithmeticException ex) {
- // success
- }
- try {
- new Fraction(1, Integer.MIN_VALUE);
- Assert.fail();
- } catch (MathArithmeticException ex) {
- // success
- }
-
- assertFraction(0, 1, new Fraction(0.00000000000001));
- assertFraction(2, 5, new Fraction(0.40000000000001));
- assertFraction(15, 1, new Fraction(15.0000000000001));
- }
-
- @Test(expected=ConvergenceException.class)
- public void testGoldenRatio() {
- // the golden ratio is notoriously a difficult number for continuous fraction
- new Fraction((1 + FastMath.sqrt(5)) / 2, 1.0e-12, 25);
- }
-
- // MATH-179
- @Test
- public void testDoubleConstructor() throws ConvergenceException {
- assertFraction(1, 2, new Fraction((double)1 / (double)2));
- assertFraction(1, 3, new Fraction((double)1 / (double)3));
- assertFraction(2, 3, new Fraction((double)2 / (double)3));
- assertFraction(1, 4, new Fraction((double)1 / (double)4));
- assertFraction(3, 4, new Fraction((double)3 / (double)4));
- assertFraction(1, 5, new Fraction((double)1 / (double)5));
- assertFraction(2, 5, new Fraction((double)2 / (double)5));
- assertFraction(3, 5, new Fraction((double)3 / (double)5));
- assertFraction(4, 5, new Fraction((double)4 / (double)5));
- assertFraction(1, 6, new Fraction((double)1 / (double)6));
- assertFraction(5, 6, new Fraction((double)5 / (double)6));
- assertFraction(1, 7, new Fraction((double)1 / (double)7));
- assertFraction(2, 7, new Fraction((double)2 / (double)7));
- assertFraction(3, 7, new Fraction((double)3 / (double)7));
- assertFraction(4, 7, new Fraction((double)4 / (double)7));
- assertFraction(5, 7, new Fraction((double)5 / (double)7));
- assertFraction(6, 7, new Fraction((double)6 / (double)7));
- assertFraction(1, 8, new Fraction((double)1 / (double)8));
- assertFraction(3, 8, new Fraction((double)3 / (double)8));
- assertFraction(5, 8, new Fraction((double)5 / (double)8));
- assertFraction(7, 8, new Fraction((double)7 / (double)8));
- assertFraction(1, 9, new Fraction((double)1 / (double)9));
- assertFraction(2, 9, new Fraction((double)2 / (double)9));
- assertFraction(4, 9, new Fraction((double)4 / (double)9));
- assertFraction(5, 9, new Fraction((double)5 / (double)9));
- assertFraction(7, 9, new Fraction((double)7 / (double)9));
- assertFraction(8, 9, new Fraction((double)8 / (double)9));
- assertFraction(1, 10, new Fraction((double)1 / (double)10));
- assertFraction(3, 10, new Fraction((double)3 / (double)10));
- assertFraction(7, 10, new Fraction((double)7 / (double)10));
- assertFraction(9, 10, new Fraction((double)9 / (double)10));
- assertFraction(1, 11, new Fraction((double)1 / (double)11));
- assertFraction(2, 11, new Fraction((double)2 / (double)11));
- assertFraction(3, 11, new Fraction((double)3 / (double)11));
- assertFraction(4, 11, new Fraction((double)4 / (double)11));
- assertFraction(5, 11, new Fraction((double)5 / (double)11));
- assertFraction(6, 11, new Fraction((double)6 / (double)11));
- assertFraction(7, 11, new Fraction((double)7 / (double)11));
- assertFraction(8, 11, new Fraction((double)8 / (double)11));
- assertFraction(9, 11, new Fraction((double)9 / (double)11));
- assertFraction(10, 11, new Fraction((double)10 / (double)11));
- }
-
- // MATH-181
- @Test
- public void testDigitLimitConstructor() throws ConvergenceException {
- assertFraction(2, 5, new Fraction(0.4, 9));
- assertFraction(2, 5, new Fraction(0.4, 99));
- assertFraction(2, 5, new Fraction(0.4, 999));
-
- assertFraction(3, 5, new Fraction(0.6152, 9));
- assertFraction(8, 13, new Fraction(0.6152, 99));
- assertFraction(510, 829, new Fraction(0.6152, 999));
- assertFraction(769, 1250, new Fraction(0.6152, 9999));
-
- // MATH-996
- assertFraction(1, 2, new Fraction(0.5000000001, 10));
- }
-
- @Test
- public void testIntegerOverflow() {
- checkIntegerOverflow(0.75000000001455192);
- checkIntegerOverflow(1.0e10);
- checkIntegerOverflow(-1.0e10);
- checkIntegerOverflow(-43979.60679604749);
- }
-
- private void checkIntegerOverflow(double a) {
- try {
- @SuppressWarnings("unused")
- Fraction f = new Fraction(a, 1.0e-12, 1000);
- //System.out.println(f.getNumerator() + "/" + f.getDenominator());
- Assert.fail("an exception should have been thrown");
- } catch (ConvergenceException ce) {
- // expected behavior
- }
- }
-
- @Test
- public void testEpsilonLimitConstructor() throws ConvergenceException {
- assertFraction(2, 5, new Fraction(0.4, 1.0e-5, 100));
-
- assertFraction(3, 5, new Fraction(0.6152, 0.02, 100));
- assertFraction(8, 13, new Fraction(0.6152, 1.0e-3, 100));
- assertFraction(251, 408, new Fraction(0.6152, 1.0e-4, 100));
- assertFraction(251, 408, new Fraction(0.6152, 1.0e-5, 100));
- assertFraction(510, 829, new Fraction(0.6152, 1.0e-6, 100));
- assertFraction(769, 1250, new Fraction(0.6152, 1.0e-7, 100));
- }
-
- @Test
- public void testCompareTo() {
- Fraction first = new Fraction(1, 2);
- Fraction second = new Fraction(1, 3);
- Fraction third = new Fraction(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
- Fraction pi1 = new Fraction(1068966896, 340262731);
- Fraction pi2 = new Fraction( 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() {
- Fraction first = new Fraction(1, 2);
- Fraction second = new Fraction(1, 3);
-
- Assert.assertEquals(0.5, first.doubleValue(), 0.0);
- Assert.assertEquals(1.0 / 3.0, second.doubleValue(), 0.0);
- }
-
- @Test
- public void testFloatValue() {
- Fraction first = new Fraction(1, 2);
- Fraction second = new Fraction(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() {
- Fraction first = new Fraction(1, 2);
- Fraction second = new Fraction(3, 2);
-
- Assert.assertEquals(0, first.intValue());
- Assert.assertEquals(1, second.intValue());
- }
-
- @Test
- public void testLongValue() {
- Fraction first = new Fraction(1, 2);
- Fraction second = new Fraction(3, 2);
-
- Assert.assertEquals(0L, first.longValue());
- Assert.assertEquals(1L, second.longValue());
- }
-
- @Test
- public void testConstructorDouble() {
- assertFraction(1, 2, new Fraction(0.5));
- assertFraction(1, 3, new Fraction(1.0 / 3.0));
- assertFraction(17, 100, new Fraction(17.0 / 100.0));
- assertFraction(317, 100, new Fraction(317.0 / 100.0));
- assertFraction(-1, 2, new Fraction(-0.5));
- assertFraction(-1, 3, new Fraction(-1.0 / 3.0));
- assertFraction(-17, 100, new Fraction(17.0 / -100.0));
- assertFraction(-317, 100, new Fraction(-317.0 / 100.0));
- }
-
- @Test
- public void testAbs() {
- Fraction a = new Fraction(10, 21);
- Fraction b = new Fraction(-10, 21);
- Fraction c = new Fraction(10, -21);
-
- assertFraction(10, 21, a.abs());
- assertFraction(10, 21, b.abs());
- assertFraction(10, 21, c.abs());
- }
-
- @Test
- public void testPercentage() {
- Assert.assertEquals(50.0, new Fraction(1, 2).percentageValue(), 1.0e-15);
- }
-
- @Test
- public void testMath835() {
- final int numer = Integer.MAX_VALUE / 99;
- final int denom = 1;
- final double percentage = 100 * ((double) numer) / denom;
- final Fraction frac = new Fraction(numer, denom);
- // With the implementation that preceded the fix suggested in MATH-835,
- // this test was failing, due to overflow.
- Assert.assertEquals(percentage, frac.percentageValue(), Math.ulp(percentage));
- }
-
- @Test
- public void testMath1261() {
- final Fraction a = new Fraction(Integer.MAX_VALUE, 2);
- final Fraction b = a.multiply(2);
- Assert.assertTrue(b.equals(new Fraction(Integer.MAX_VALUE)));
-
- final Fraction c = new Fraction(2, Integer.MAX_VALUE);
- final Fraction d = c.divide(2);
- Assert.assertTrue(d.equals(new Fraction(1, Integer.MAX_VALUE)));
- }
-
- @Test
- public void testReciprocal() {
- Fraction f = null;
-
- f = new Fraction(50, 75);
- f = f.reciprocal();
- Assert.assertEquals(3, f.getNumerator());
- Assert.assertEquals(2, f.getDenominator());
-
- f = new Fraction(4, 3);
- f = f.reciprocal();
- Assert.assertEquals(3, f.getNumerator());
- Assert.assertEquals(4, f.getDenominator());
-
- f = new Fraction(-15, 47);
- f = f.reciprocal();
- Assert.assertEquals(-47, f.getNumerator());
- Assert.assertEquals(15, f.getDenominator());
-
- f = new Fraction(0, 3);
- try {
- f = f.reciprocal();
- Assert.fail("expecting MathArithmeticException");
- } catch (MathArithmeticException ex) {}
-
- // large values
- f = new Fraction(Integer.MAX_VALUE, 1);
- f = f.reciprocal();
- Assert.assertEquals(1, f.getNumerator());
- Assert.assertEquals(Integer.MAX_VALUE, f.getDenominator());
- }
-
- @Test
- public void testNegate() {
- Fraction f = null;
-
- f = new Fraction(50, 75);
- f = f.negate();
- Assert.assertEquals(-2, f.getNumerator());
- Assert.assertEquals(3, f.getDenominator());
-
- f = new Fraction(-50, 75);
- f = f.negate();
- Assert.assertEquals(2, f.getNumerator());
- Assert.assertEquals(3, f.getDenominator());
-
- // large values
- f = new Fraction(Integer.MAX_VALUE-1, Integer.MAX_VALUE);
- f = f.negate();
- Assert.assertEquals(Integer.MIN_VALUE+2, f.getNumerator());
- Assert.assertEquals(Integer.MAX_VALUE, f.getDenominator());
-
- f = new Fraction(Integer.MIN_VALUE, 1);
- try {
- f = f.negate();
- Assert.fail("expecting MathArithmeticException");
- } catch (MathArithmeticException ex) {}
- }
-
- @Test
- public void testAdd() {
- Fraction a = new Fraction(1, 2);
- Fraction b = new Fraction(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));
-
- Fraction f1 = new Fraction(Integer.MAX_VALUE - 1, 1);
- Fraction f2 = Fraction.ONE;
- Fraction f = f1.add(f2);
- Assert.assertEquals(Integer.MAX_VALUE, f.getNumerator());
- Assert.assertEquals(1, f.getDenominator());
- f = f1.add(1);
- Assert.assertEquals(Integer.MAX_VALUE, f.getNumerator());
- Assert.assertEquals(1, f.getDenominator());
-
- f1 = new Fraction(-1, 13*13*2*2);
- f2 = new Fraction(-2, 13*17*2);
- f = f1.add(f2);
- Assert.assertEquals(13*13*17*2*2, f.getDenominator());
- Assert.assertEquals(-17 - 2*13*2, f.getNumerator());
-
- try {
- f.add(null);
- Assert.fail("expecting NullArgumentException");
- } catch (NullArgumentException ex) {}
-
- // if this fraction is added naively, it will overflow.
- // check that it doesn't.
- f1 = new Fraction(1,32768*3);
- f2 = new Fraction(1,59049);
- f = f1.add(f2);
- Assert.assertEquals(52451, f.getNumerator());
- Assert.assertEquals(1934917632, f.getDenominator());
-
- f1 = new Fraction(Integer.MIN_VALUE, 3);
- f2 = new Fraction(1,3);
- f = f1.add(f2);
- Assert.assertEquals(Integer.MIN_VALUE+1, f.getNumerator());
- Assert.assertEquals(3, f.getDenominator());
-
- f1 = new Fraction(Integer.MAX_VALUE - 1, 1);
- f2 = Fraction.ONE;
- f = f1.add(f2);
- Assert.assertEquals(Integer.MAX_VALUE, f.getNumerator());
- Assert.assertEquals(1, f.getDenominator());
-
- try {
- f = f.add(Fraction.ONE); // should overflow
- Assert.fail("expecting ArithmeticException but got: " + f.toString());
- } catch (ArithmeticException ex) {}
-
- // denominator should not be a multiple of 2 or 3 to trigger overflow
- f1 = new Fraction(Integer.MIN_VALUE, 5);
- f2 = new Fraction(-1,5);
- try {
- f = f1.add(f2); // should overflow
- Assert.fail("expecting MathArithmeticException but got: " + f.toString());
- } catch (MathArithmeticException ex) {}
-
- try {
- f= new Fraction(-Integer.MAX_VALUE, 1);
- f = f.add(f);
- Assert.fail("expecting ArithmeticException");
- } catch (ArithmeticException ex) {}
-
- try {
- f= new Fraction(-Integer.MAX_VALUE, 1);
- f = f.add(f);
- Assert.fail("expecting ArithmeticException");
- } catch (ArithmeticException ex) {}
-
- f1 = new Fraction(3,327680);
- f2 = new Fraction(2,59049);
- try {
- f = f1.add(f2); // should overflow
- Assert.fail("expecting ArithmeticException but got: " + f.toString());
- } catch (ArithmeticException ex) {}
- }
-
- @Test
- public void testDivide() {
- Fraction a = new Fraction(1, 2);
- Fraction b = new Fraction(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));
-
- Fraction f1 = new Fraction(3, 5);
- Fraction f2 = Fraction.ZERO;
- try {
- f1.divide(f2);
- Assert.fail("expecting MathArithmeticException");
- } catch (MathArithmeticException ex) {}
-
- f1 = new Fraction(0, 5);
- f2 = new Fraction(2, 7);
- Fraction f = f1.divide(f2);
- Assert.assertSame(Fraction.ZERO, f);
-
- f1 = new Fraction(2, 7);
- f2 = Fraction.ONE;
- f = f1.divide(f2);
- Assert.assertEquals(2, f.getNumerator());
- Assert.assertEquals(7, f.getDenominator());
-
- f1 = new Fraction(1, Integer.MAX_VALUE);
- f = f1.divide(f1);
- Assert.assertEquals(1, f.getNumerator());
- Assert.assertEquals(1, f.getDenominator());
-
- f1 = new Fraction(Integer.MIN_VALUE, Integer.MAX_VALUE);
- f2 = new Fraction(1, Integer.MAX_VALUE);
- f = f1.divide(f2);
- Assert.assertEquals(Integer.MIN_VALUE, f.getNumerator());
- Assert.assertEquals(1, f.getDenominator());
-
- try {
- f.divide(null);
- Assert.fail("NullArgumentException");
- } catch (NullArgumentException ex) {}
-
- try {
- f1 = new Fraction(1, Integer.MAX_VALUE);
- f = f1.divide(f1.reciprocal()); // should overflow
- Assert.fail("expecting ArithmeticException");
- } catch (ArithmeticException ex) {}
- try {
- f1 = new Fraction(1, -Integer.MAX_VALUE);
- f = f1.divide(f1.reciprocal()); // should overflow
- Assert.fail("expecting ArithmeticException");
- } catch (ArithmeticException ex) {}
-
- f1 = new Fraction(6, 35);
- f = f1.divide(15);
- Assert.assertEquals(2, f.getNumerator());
- Assert.assertEquals(175, f.getDenominator());
-
- }
-
- @Test
- public void testMultiply() {
- Fraction a = new Fraction(1, 2);
- Fraction b = new Fraction(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));
-
- Fraction f1 = new Fraction(Integer.MAX_VALUE, 1);
- Fraction f2 = new Fraction(Integer.MIN_VALUE, Integer.MAX_VALUE);
- Fraction f = f1.multiply(f2);
- Assert.assertEquals(Integer.MIN_VALUE, f.getNumerator());
- Assert.assertEquals(1, f.getDenominator());
-
- try {
- f.multiply(null);
- Assert.fail("expecting NullArgumentException");
- } catch (NullArgumentException ex) {}
-
- f1 = new Fraction(6, 35);
- f = f1.multiply(15);
- Assert.assertEquals(18, f.getNumerator());
- Assert.assertEquals(7, f.getDenominator());
- }
-
- @Test
- public void testSubtract() {
- Fraction a = new Fraction(1, 2);
- Fraction b = new Fraction(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));
-
- Fraction f = new Fraction(1,1);
- try {
- f.subtract(null);
- Assert.fail("expecting NullArgumentException");
- } catch (NullArgumentException ex) {}
-
- // if this fraction is subtracted naively, it will overflow.
- // check that it doesn't.
- Fraction f1 = new Fraction(1,32768*3);
- Fraction f2 = new Fraction(1,59049);
- f = f1.subtract(f2);
- Assert.assertEquals(-13085, f.getNumerator());
- Assert.assertEquals(1934917632, f.getDenominator());
-
- f1 = new Fraction(Integer.MIN_VALUE, 3);
- f2 = new Fraction(1,3).negate();
- f = f1.subtract(f2);
- Assert.assertEquals(Integer.MIN_VALUE+1, f.getNumerator());
- Assert.assertEquals(3, f.getDenominator());
-
- f1 = new Fraction(Integer.MAX_VALUE, 1);
- f2 = Fraction.ONE;
- f = f1.subtract(f2);
- Assert.assertEquals(Integer.MAX_VALUE-1, f.getNumerator());
- Assert.assertEquals(1, f.getDenominator());
- f = f1.subtract(1);
- Assert.assertEquals(Integer.MAX_VALUE-1, f.getNumerator());
- Assert.assertEquals(1, f.getDenominator());
-
- try {
- f1 = new Fraction(1, Integer.MAX_VALUE);
- f2 = new Fraction(1, Integer.MAX_VALUE - 1);
- f = f1.subtract(f2);
- Assert.fail("expecting ArithmeticException"); //should overflow
- } catch (ArithmeticException ex) {}
-
- // denominator should not be a multiple of 2 or 3 to trigger overflow
- f1 = new Fraction(Integer.MIN_VALUE, 5);
- f2 = new Fraction(1,5);
- try {
- f = f1.subtract(f2); // should overflow
- Assert.fail("expecting MathArithmeticException but got: " + f.toString());
- } catch (MathArithmeticException ex) {}
-
- try {
- f= new Fraction(Integer.MIN_VALUE, 1);
- f = f.subtract(Fraction.ONE);
- Assert.fail("expecting ArithmeticException");
- } catch (ArithmeticException ex) {}
-
- try {
- f= new Fraction(Integer.MAX_VALUE, 1);
- f = f.subtract(Fraction.ONE.negate());
- Assert.fail("expecting ArithmeticException");
- } catch (ArithmeticException ex) {}
-
- f1 = new Fraction(3,327680);
- f2 = new Fraction(2,59049);
- try {
- f = f1.subtract(f2); // should overflow
- Assert.fail("expecting ArithmeticException but got: " + f.toString());
- } catch (ArithmeticException ex) {}
- }
-
- @Test
- public void testEqualsAndHashCode() {
- Fraction zero = new Fraction(0,1);
- Fraction nullFraction = null;
- Assert.assertTrue( zero.equals(zero));
- Assert.assertFalse(zero.equals(nullFraction));
- Assert.assertFalse(zero.equals(Double.valueOf(0)));
- Fraction zero2 = new Fraction(0,2);
- Assert.assertTrue(zero.equals(zero2));
- Assert.assertEquals(zero.hashCode(), zero2.hashCode());
- Fraction one = new Fraction(1,1);
- Assert.assertFalse((one.equals(zero) ||zero.equals(one)));
- }
-
- @Test
- public void testGetReducedFraction() {
- Fraction threeFourths = new Fraction(3, 4);
- Assert.assertTrue(threeFourths.equals(Fraction.getReducedFraction(6, 8)));
- Assert.assertTrue(Fraction.ZERO.equals(Fraction.getReducedFraction(0, -1)));
- try {
- Fraction.getReducedFraction(1, 0);
- Assert.fail("expecting MathArithmeticException");
- } catch (MathArithmeticException ex) {
- // expected
- }
- Assert.assertEquals(Fraction.getReducedFraction
- (2, Integer.MIN_VALUE).getNumerator(),-1);
- Assert.assertEquals(Fraction.getReducedFraction
- (1, -1).getNumerator(), -1);
- }
-
- @Test
- public void testToString() {
- Assert.assertEquals("0", new Fraction(0, 3).toString());
- Assert.assertEquals("3", new Fraction(6, 2).toString());
- Assert.assertEquals("2 / 3", new Fraction(18, 27).toString());
- }
-
- @Test
- public void testSerial() throws FractionConversionException {
- Fraction[] fractions = {
- new Fraction(3, 4), Fraction.ONE, Fraction.ZERO,
- new Fraction(17), new Fraction(FastMath.PI, 1000),
- new Fraction(-5, 2)
- };
- for (Fraction fraction : fractions) {
- Assert.assertEquals(fraction, TestUtils.serializeAndRecover(fraction));
- }
- }
-
-}
diff --git a/src/test/java/org/apache/commons/math4/linear/ArrayFieldVectorTest.java b/src/test/java/org/apache/commons/math4/linear/ArrayFieldVectorTest.java
index 2369824..d6b3e57 100644
--- a/src/test/java/org/apache/commons/math4/linear/ArrayFieldVectorTest.java
+++ b/src/test/java/org/apache/commons/math4/linear/ArrayFieldVectorTest.java
@@ -26,8 +26,10 @@ import org.apache.commons.math4.TestUtils;
import org.apache.commons.math4.exception.MathIllegalArgumentException;
import org.apache.commons.math4.exception.NumberIsTooSmallException;
import org.apache.commons.math4.exception.OutOfRangeException;
-import org.apache.commons.math4.fraction.Fraction;
-import org.apache.commons.math4.fraction.FractionField;
+import org.apache.commons.math4.dfp.Dfp;
+import org.apache.commons.math4.dfp.DfpField;
+import org.apache.commons.math4.util.BigReal;
+import org.apache.commons.math4.util.BigRealField;
import org.apache.commons.math4.linear.ArrayFieldVector;
import org.apache.commons.math4.linear.FieldMatrix;
import org.apache.commons.math4.linear.FieldVector;
@@ -43,28 +45,28 @@ import org.junit.Test;
public class ArrayFieldVectorTest {
//
- protected Fraction[][] ma1 = {
- {new Fraction(1), new Fraction(2), new Fraction(3)},
- {new Fraction(4), new Fraction(5), new Fraction(6)},
- {new Fraction(7), new Fraction(8), new Fraction(9)}
+ protected Dfp[][] ma1 = {
+ {Dfp25.of(1), Dfp25.of(2), Dfp25.of(3)},
+ {Dfp25.of(4), Dfp25.of(5), Dfp25.of(6)},
+ {Dfp25.of(7), Dfp25.of(8), Dfp25.of(9)}
};
- protected Fraction[] vec1 = {new Fraction(1), new Fraction(2), new Fraction(3)};
- protected Fraction[] vec2 = {new Fraction(4), new Fraction(5), new Fraction(6)};
- protected Fraction[] vec3 = {new Fraction(7), new Fraction(8), new Fraction(9)};
- protected Fraction[] vec4 = { new Fraction(1), new Fraction(2), new Fraction(3),
- new Fraction(4), new Fraction(5), new Fraction(6),
- new Fraction(7), new Fraction(8), new Fraction(9)};
- protected Fraction[] vec_null = {Fraction.ZERO, Fraction.ZERO, Fraction.ZERO};
- protected Fraction[] dvec1 = {new Fraction(1), new Fraction(2), new Fraction(3),
- new Fraction(4), new Fraction(5), new Fraction(6),
- new Fraction(7), new Fraction(8), new Fraction(9)};
- protected Fraction[][] mat1 = {
- {new Fraction(1), new Fraction(2), new Fraction(3)},
- {new Fraction(4), new Fraction(5), new Fraction(6)},
- {new Fraction(7), new Fraction(8), new Fraction(9)}
+ protected Dfp[] vec1 = {Dfp25.of(1), Dfp25.of(2), Dfp25.of(3)};
+ protected Dfp[] vec2 = {Dfp25.of(4), Dfp25.of(5), Dfp25.of(6)};
+ protected Dfp[] vec3 = {Dfp25.of(7), Dfp25.of(8), Dfp25.of(9)};
+ protected Dfp[] vec4 = { Dfp25.of(1), Dfp25.of(2), Dfp25.of(3),
+ Dfp25.of(4), Dfp25.of(5), Dfp25.of(6),
+ Dfp25.of(7), Dfp25.of(8), Dfp25.of(9)};
+ protected Dfp[] vec_null = {Dfp25.ZERO, Dfp25.ZERO, Dfp25.ZERO};
+ protected Dfp[] dvec1 = {Dfp25.of(1), Dfp25.of(2), Dfp25.of(3),
+ Dfp25.of(4), Dfp25.of(5), Dfp25.of(6),
+ Dfp25.of(7), Dfp25.of(8), Dfp25.of(9)};
+ protected Dfp[][] mat1 = {
+ {Dfp25.of(1), Dfp25.of(2), Dfp25.of(3)},
+ {Dfp25.of(4), Dfp25.of(5), Dfp25.of(6)},
+ {Dfp25.of(7), Dfp25.of(8), Dfp25.of(9)}
};
- // Testclass to test the FieldVector<Fraction> interface
+ // Testclass to test the FieldVector<Dfp> interface
// only with enough content to support the test
public static class FieldVectorTestImpl<T extends FieldElement<T>>
implements FieldVector<T>, Serializable {
@@ -291,24 +293,24 @@ public class ArrayFieldVectorTest {
@Test
public void testConstructors() {
- ArrayFieldVector<Fraction> v0 = new ArrayFieldVector<>(FractionField.getInstance());
+ ArrayFieldVector<Dfp> v0 = new ArrayFieldVector<>(Dfp25.getField());
Assert.assertEquals(0, v0.getDimension());
- ArrayFieldVector<Fraction> v1 = new ArrayFieldVector<>(FractionField.getInstance(), 7);
+ ArrayFieldVector<Dfp> v1 = new ArrayFieldVector<>(Dfp25.getField(), 7);
Assert.assertEquals(7, v1.getDimension());
- Assert.assertEquals(Fraction.ZERO, v1.getEntry(6));
+ Assert.assertEquals(Dfp25.ZERO, v1.getEntry(6));
- ArrayFieldVector<Fraction> v2 = new ArrayFieldVector<>(5, new Fraction(123, 100));
+ ArrayFieldVector<Dfp> v2 = new ArrayFieldVector<>(5, Dfp25.of(123, 100));
Assert.assertEquals(5, v2.getDimension());
- Assert.assertEquals(new Fraction(123, 100), v2.getEntry(4));
+ Assert.assertEquals(Dfp25.of(123, 100), v2.getEntry(4));
- ArrayFieldVector<Fraction> v3 = new ArrayFieldVector<>(FractionField.getInstance(), vec1);
+ ArrayFieldVector<Dfp> v3 = new ArrayFieldVector<>(Dfp25.getField(), vec1);
Assert.assertEquals(3, v3.getDimension());
- Assert.assertEquals(new Fraction(2), v3.getEntry(1));
+ Assert.assertEquals(Dfp25.of(2), v3.getEntry(1));
- ArrayFieldVector<Fraction> v4 = new ArrayFieldVector<>(FractionField.getInstance(), vec4, 3, 2);
+ ArrayFieldVector<Dfp> v4 = new ArrayFieldVector<>(Dfp25.getField(), vec4, 3, 2);
Assert.assertEquals(2, v4.getDimension());
- Assert.assertEquals(new Fraction(4), v4.getEntry(0));
+ Assert.assertEquals(Dfp25.of(4), v4.getEntry(0));
try {
new ArrayFieldVector<>(vec4, 8, 3);
Assert.fail("MathIllegalArgumentException expected");
@@ -316,17 +318,17 @@ public class ArrayFieldVectorTest {
// expected behavior
}
- FieldVector<Fraction> v5_i = new ArrayFieldVector<>(dvec1);
+ FieldVector<Dfp> v5_i = new ArrayFieldVector<>(dvec1);
Assert.assertEquals(9, v5_i.getDimension());
- Assert.assertEquals(new Fraction(9), v5_i.getEntry(8));
+ Assert.assertEquals(Dfp25.of(9), v5_i.getEntry(8));
- ArrayFieldVector<Fraction> v5 = new ArrayFieldVector<>(dvec1);
+ ArrayFieldVector<Dfp> v5 = new ArrayFieldVector<>(dvec1);
Assert.assertEquals(9, v5.getDimension());
- Assert.assertEquals(new Fraction(9), v5.getEntry(8));
+ Assert.assertEquals(Dfp25.of(9), v5.getEntry(8));
- ArrayFieldVector<Fraction> v6 = new ArrayFieldVector<>(dvec1, 3, 2);
+ ArrayFieldVector<Dfp> v6 = new ArrayFieldVector<>(dvec1, 3, 2);
Assert.assertEquals(2, v6.getDimension());
- Assert.assertEquals(new Fraction(4), v6.getEntry(0));
+ Assert.assertEquals(Dfp25.of(4), v6.getEntry(0));
try {
new ArrayFieldVector<>(dvec1, 8, 3);
Assert.fail("MathIllegalArgumentException expected");
@@ -334,69 +336,69 @@ public class ArrayFieldVectorTest {
// expected behavior
}
- ArrayFieldVector<Fraction> v7 = new ArrayFieldVector<>(v1);
+ ArrayFieldVector<Dfp> v7 = new ArrayFieldVector<>(v1);
Assert.assertEquals(7, v7.getDimension());
- Assert.assertEquals(Fraction.ZERO, v7.getEntry(6));
+ Assert.assertEquals(Dfp25.ZERO, v7.getEntry(6));
- FieldVectorTestImpl<Fraction> v7_i = new FieldVectorTestImpl<>(vec1);
+ FieldVectorTestImpl<Dfp> v7_i = new FieldVectorTestImpl<>(vec1);
- ArrayFieldVector<Fraction> v7_2 = new ArrayFieldVector<>(v7_i);
+ ArrayFieldVector<Dfp> v7_2 = new ArrayFieldVector<>(v7_i);
Assert.assertEquals(3, v7_2.getDimension());
- Assert.assertEquals(new Fraction(2), v7_2.getEntry(1));
+ Assert.assertEquals(Dfp25.of(2), v7_2.getEntry(1));
- ArrayFieldVector<Fraction> v8 = new ArrayFieldVector<>(v1, true);
+ ArrayFieldVector<Dfp> v8 = new ArrayFieldVector<>(v1, true);
Assert.assertEquals(7, v8.getDimension());
- Assert.assertEquals(Fraction.ZERO, v8.getEntry(6));
+ Assert.assertEquals(Dfp25.ZERO, v8.getEntry(6));
Assert.assertNotSame("testData not same object ", v1.getDataRef(), v8.getDataRef());
- ArrayFieldVector<Fraction> v8_2 = new ArrayFieldVector<>(v1, false);
+ ArrayFieldVector<Dfp> v8_2 = new ArrayFieldVector<>(v1, false);
Assert.assertEquals(7, v8_2.getDimension());
- Assert.assertEquals(Fraction.ZERO, v8_2.getEntry(6));
+ Assert.assertEquals(Dfp25.ZERO, v8_2.getEntry(6));
Assert.assertArrayEquals(v1.getDataRef(), v8_2.getDataRef());
- ArrayFieldVector<Fraction> v9 = new ArrayFieldVector<>((FieldVector<Fraction>) v1, (FieldVector<Fraction>) v3);
+ ArrayFieldVector<Dfp> v9 = new ArrayFieldVector<>((FieldVector<Dfp>) v1, (FieldVector<Dfp>) v3);
Assert.assertEquals(10, v9.getDimension());
- Assert.assertEquals(new Fraction(1), v9.getEntry(7));
+ Assert.assertEquals(Dfp25.of(1), v9.getEntry(7));
}
@Test
public void testDataInOut() {
- ArrayFieldVector<Fraction> v1 = new ArrayFieldVector<>(vec1);
- ArrayFieldVector<Fraction> v2 = new ArrayFieldVector<>(vec2);
- ArrayFieldVector<Fraction> v4 = new ArrayFieldVector<>(vec4);
- FieldVectorTestImpl<Fraction> v2_t = new FieldVectorTestImpl<>(vec2);
+ ArrayFieldVector<Dfp> v1 = new ArrayFieldVector<>(vec1);
+ ArrayFieldVector<Dfp> v2 = new ArrayFieldVector<>(vec2);
+ ArrayFieldVector<Dfp> v4 = new ArrayFieldVector<>(vec4);
+ FieldVectorTestImpl<Dfp> v2_t = new FieldVectorTestImpl<>(vec2);
- FieldVector<Fraction> v_append_1 = v1.append(v2);
+ FieldVector<Dfp> v_append_1 = v1.append(v2);
Assert.assertEquals(6, v_append_1.getDimension());
- Assert.assertEquals(new Fraction(4), v_append_1.getEntry(3));
+ Assert.assertEquals(Dfp25.of(4), v_append_1.getEntry(3));
- FieldVector<Fraction> v_append_2 = v1.append(new Fraction(2));
+ FieldVector<Dfp> v_append_2 = v1.append(Dfp25.of(2));
Assert.assertEquals(4, v_append_2.getDimension());
- Assert.assertEquals(new Fraction(2), v_append_2.getEntry(3));
+ Assert.assertEquals(Dfp25.of(2), v_append_2.getEntry(3));
- FieldVector<Fraction> v_append_4 = v1.append(v2_t);
+ FieldVector<Dfp> v_append_4 = v1.append(v2_t);
Assert.assertEquals(6, v_append_4.getDimension());
- Assert.assertEquals(new Fraction(4), v_append_4.getEntry(3));
+ Assert.assertEquals(Dfp25.of(4), v_append_4.getEntry(3));
- FieldVector<Fraction> v_copy = v1.copy();
+ FieldVector<Dfp> v_copy = v1.copy();
Assert.assertEquals(3, v_copy.getDimension());
Assert.assertNotSame("testData not same object ", v1.getDataRef(), v_copy.toArray());
- Fraction[] a_frac = v1.toArray();
+ Dfp[] a_frac = v1.toArray();
Assert.assertEquals(3, a_frac.length);
Assert.assertNotSame("testData not same object ", v1.getDataRef(), a_frac);
-// ArrayFieldVector<Fraction> vout4 = (ArrayFieldVector<Fraction>) v1.clone();
+// ArrayFieldVector<Dfp> vout4 = (ArrayFieldVector<Dfp>) v1.clone();
// Assert.assertEquals(3, vout4.getDimension());
// Assert.assertEquals(v1.getDataRef(), vout4.getDataRef());
- FieldVector<Fraction> vout5 = v4.getSubVector(3, 3);
+ FieldVector<Dfp> vout5 = v4.getSubVector(3, 3);
Assert.assertEquals(3, vout5.getDimension());
- Assert.assertEquals(new Fraction(5), vout5.getEntry(1));
+ Assert.assertEquals(Dfp25.of(5), vout5.getEntry(1));
try {
v4.getSubVector(3, 7);
Assert.fail("OutOfRangeException expected");
@@ -404,20 +406,20 @@ public class ArrayFieldVectorTest {
// expected behavior
}
- ArrayFieldVector<Fraction> v_set1 = (ArrayFieldVector<Fraction>) v1.copy();
- v_set1.setEntry(1, new Fraction(11));
- Assert.assertEquals(new Fraction(11), v_set1.getEntry(1));
+ ArrayFieldVector<Dfp> v_set1 = (ArrayFieldVector<Dfp>) v1.copy();
+ v_set1.setEntry(1, Dfp25.of(11));
+ Assert.assertEquals(Dfp25.of(11), v_set1.getEntry(1));
try {
- v_set1.setEntry(3, new Fraction(11));
+ v_set1.setEntry(3, Dfp25.of(11));
Assert.fail("OutOfRangeException expected");
} catch (OutOfRangeException ex) {
// expected behavior
}
- ArrayFieldVector<Fraction> v_set2 = (ArrayFieldVector<Fraction>) v4.copy();
+ ArrayFieldVector<Dfp> v_set2 = (ArrayFieldVector<Dfp>) v4.copy();
v_set2.set(3, v1);
- Assert.assertEquals(new Fraction(1), v_set2.getEntry(3));
- Assert.assertEquals(new Fraction(7), v_set2.getEntry(6));
+ Assert.assertEquals(Dfp25.of(1), v_set2.getEntry(3));
+ Assert.assertEquals(Dfp25.of(7), v_set2.getEntry(6));
try {
v_set2.set(7, v1);
Assert.fail("OutOfRangeException expected");
@@ -425,9 +427,9 @@ public class ArrayFieldVectorTest {
// expected behavior
}
- ArrayFieldVector<Fraction> v_set3 = (ArrayFieldVector<Fraction>) v1.copy();
- v_set3.set(new Fraction(13));
- Assert.assertEquals(new Fraction(13), v_set3.getEntry(2));
+ ArrayFieldVector<Dfp> v_set3 = (ArrayFieldVector<Dfp>) v1.copy();
+ v_set3.set(Dfp25.of(13));
+ Assert.assertEquals(Dfp25.of(13), v_set3.getEntry(2));
try {
v_set3.getEntry(23);
@@ -436,10 +438,10 @@ public class ArrayFieldVectorTest {
// expected behavior
}
- ArrayFieldVector<Fraction> v_set4 = (ArrayFieldVector<Fraction>) v4.copy();
+ ArrayFieldVector<Dfp> v_set4 = (ArrayFieldVector<Dfp>) v4.copy();
v_set4.setSubVector(3, v2_t);
- Assert.assertEquals(new Fraction(4), v_set4.getEntry(3));
- Assert.assertEquals(new Fraction(7), v_set4.getEntry(6));
+ Assert.assertEquals(Dfp25.of(4), v_set4.getEntry(3));
+ Assert.assertEquals(Dfp25.of(7), v_set4.getEntry(6));
try {
v_set4.setSubVector(7, v2_t);
Assert.fail("OutOfRangeException expected");
@@ -448,154 +450,154 @@ public class ArrayFieldVectorTest {
}
- ArrayFieldVector<Fraction> vout10 = (ArrayFieldVector<Fraction>) v1.copy();
- ArrayFieldVector<Fraction> vout10_2 = (ArrayFieldVector<Fraction>) v1.copy();
+ ArrayFieldVector<Dfp> vout10 = (ArrayFieldVector<Dfp>) v1.copy();
+ ArrayFieldVector<Dfp> vout10_2 = (ArrayFieldVector<Dfp>) v1.copy();
Assert.assertEquals(vout10, vout10_2);
- vout10_2.setEntry(0, new Fraction(11, 10));
+ vout10_2.setEntry(0, Dfp25.of(11, 10));
Assert.assertNotSame(vout10, vout10_2);
}
@Test
public void testMapFunctions() {
- ArrayFieldVector<Fraction> v1 = new ArrayFieldVector<>(vec1);
+ ArrayFieldVector<Dfp> v1 = new ArrayFieldVector<>(vec1);
//octave = v1 .+ 2.0
- FieldVector<Fraction> v_mapAdd = v1.mapAdd(new Fraction(2));
- Fraction[] result_mapAdd = {new Fraction(3), new Fraction(4), new Fraction(5)};
+ FieldVector<Dfp> v_mapAdd = v1.mapAdd(Dfp25.of(2));
+ Dfp[] result_mapAdd = {Dfp25.of(3), Dfp25.of(4), Dfp25.of(5)};
checkArray("compare vectors" ,result_mapAdd,v_mapAdd.toArray());
//octave = v1 .+ 2.0
- FieldVector<Fraction> v_mapAddToSelf = v1.copy();
- v_mapAddToSelf.mapAddToSelf(new Fraction(2));
- Fraction[] result_mapAddToSelf = {new Fraction(3), new Fraction(4), new Fraction(5)};
+ FieldVector<Dfp> v_mapAddToSelf = v1.copy();
+ v_mapAddToSelf.mapAddToSelf(Dfp25.of(2));
+ Dfp[] result_mapAddToSelf = {Dfp25.of(3), Dfp25.of(4), Dfp25.of(5)};
checkArray("compare vectors" ,result_mapAddToSelf,v_mapAddToSelf.toArray());
//octave = v1 .- 2.0
- FieldVector<Fraction> v_mapSubtract = v1.mapSubtract(new Fraction(2));
- Fraction[] result_mapSubtract = {new Fraction(-1), Fraction.ZERO, new Fraction(1)};
+ FieldVector<Dfp> v_mapSubtract = v1.mapSubtract(Dfp25.of(2));
+ Dfp[] result_mapSubtract = {Dfp25.of(-1), Dfp25.ZERO, Dfp25.of(1)};
checkArray("compare vectors" ,result_mapSubtract,v_mapSubtract.toArray());
//octave = v1 .- 2.0
- FieldVector<Fraction> v_mapSubtractToSelf = v1.copy();
- v_mapSubtractToSelf.mapSubtractToSelf(new Fraction(2));
- Fraction[] result_mapSubtractToSelf = {new Fraction(-1), Fraction.ZERO, new Fraction(1)};
+ FieldVector<Dfp> v_mapSubtractToSelf = v1.copy();
+ v_mapSubtractToSelf.mapSubtractToSelf(Dfp25.of(2));
+ Dfp[] result_mapSubtractToSelf = {Dfp25.of(-1), Dfp25.ZERO, Dfp25.of(1)};
checkArray("compare vectors" ,result_mapSubtractToSelf,v_mapSubtractToSelf.toArray());
//octave = v1 .* 2.0
- FieldVector<Fraction> v_mapMultiply = v1.mapMultiply(new Fraction(2));
- Fraction[] result_mapMultiply = {new Fraction(2), new Fraction(4), new Fraction(6)};
+ FieldVector<Dfp> v_mapMultiply = v1.mapMultiply(Dfp25.of(2));
+ Dfp[] result_mapMultiply = {Dfp25.of(2), Dfp25.of(4), Dfp25.of(6)};
checkArray("compare vectors" ,result_mapMultiply,v_mapMultiply.toArray());
//octave = v1 .* 2.0
- FieldVector<Fraction> v_mapMultiplyToSelf = v1.copy();
- v_mapMultiplyToSelf.mapMultiplyToSelf(new Fraction(2));
- Fraction[] result_mapMultiplyToSelf = {new Fraction(2), new Fraction(4), new Fraction(6)};
+ FieldVector<Dfp> v_mapMultiplyToSelf = v1.copy();
+ v_mapMultiplyToSelf.mapMultiplyToSelf(Dfp25.of(2));
+ Dfp[] result_mapMultiplyToSelf = {Dfp25.of(2), Dfp25.of(4), Dfp25.of(6)};
checkArray("compare vectors" ,result_mapMultiplyToSelf,v_mapMultiplyToSelf.toArray());
//octave = v1 ./ 2.0
- FieldVector<Fraction> v_mapDivide = v1.mapDivide(new Fraction(2));
- Fraction[] result_mapDivide = {new Fraction(1, 2), new Fraction(1), new Fraction(3, 2)};
+ FieldVector<Dfp> v_mapDivide = v1.mapDivide(Dfp25.of(2));
+ Dfp[] result_mapDivide = {Dfp25.of(1, 2), Dfp25.of(1), Dfp25.of(3, 2)};
checkArray("compare vectors" ,result_mapDivide,v_mapDivide.toArray());
//octave = v1 ./ 2.0
- FieldVector<Fraction> v_mapDivideToSelf = v1.copy();
- v_mapDivideToSelf.mapDivideToSelf(new Fraction(2));
- Fraction[] result_mapDivideToSelf = {new Fraction(1, 2), new Fraction(1), new Fraction(3, 2)};
+ FieldVector<Dfp> v_mapDivideToSelf = v1.copy();
+ v_mapDivideToSelf.mapDivideToSelf(Dfp25.of(2));
+ Dfp[] result_mapDivideToSelf = {Dfp25.of(1, 2), Dfp25.of(1), Dfp25.of(3, 2)};
checkArray("compare vectors" ,result_mapDivideToSelf,v_mapDivideToSelf.toArray());
//octave = v1 .^-1
- FieldVector<Fraction> v_mapInv = v1.mapInv();
- Fraction[] result_mapInv = {new Fraction(1),new Fraction(1, 2),new Fraction(1, 3)};
+ FieldVector<Dfp> v_mapInv = v1.mapInv();
+ Dfp[] result_mapInv = {Dfp25.of(1),Dfp25.of(1, 2),Dfp25.of(1, 3)};
checkArray("compare vectors" ,result_mapInv,v_mapInv.toArray());
//octave = v1 .^-1
- FieldVector<Fraction> v_mapInvToSelf = v1.copy();
+ FieldVector<Dfp> v_mapInvToSelf = v1.copy();
v_mapInvToSelf.mapInvToSelf();
- Fraction[] result_mapInvToSelf = {new Fraction(1),new Fraction(1, 2),new Fraction(1, 3)};
+ Dfp[] result_mapInvToSelf = {Dfp25.of(1),Dfp25.of(1, 2),Dfp25.of(1, 3)};
checkArray("compare vectors" ,result_mapInvToSelf,v_mapInvToSelf.toArray());
}
@Test
public void testBasicFunctions() {
- ArrayFieldVector<Fraction> v1 = new ArrayFieldVector<>(vec1);
- ArrayFieldVector<Fraction> v2 = new ArrayFieldVector<>(vec2);
+ ArrayFieldVector<Dfp> v1 = new ArrayFieldVector<>(vec1);
+ ArrayFieldVector<Dfp> v2 = new ArrayFieldVector<>(vec2);
new ArrayFieldVector<>(vec_null);
- FieldVectorTestImpl<Fraction> v2_t = new FieldVectorTestImpl<>(vec2);
+ FieldVectorTestImpl<Dfp> v2_t = new FieldVectorTestImpl<>(vec2);
//octave = v1 + v2
- ArrayFieldVector<Fraction> v_add = v1.add(v2);
- Fraction[] result_add = {new Fraction(5), new Fraction(7), new Fraction(9)};
+ ArrayFieldVector<Dfp> v_add = v1.add(v2);
+ Dfp[] result_add = {Dfp25.of(5), Dfp25.of(7), Dfp25.of(9)};
checkArray("compare vect" ,v_add.toArray(),result_add);
- FieldVectorTestImpl<Fraction> vt2 = new FieldVectorTestImpl<>(vec2);
- FieldVector<Fraction> v_add_i = v1.add(vt2);
- Fraction[] result_add_i = {new Fraction(5), new Fraction(7), new Fraction(9)};
+ FieldVectorTestImpl<Dfp> vt2 = new FieldVectorTestImpl<>(vec2);
+ FieldVector<Dfp> v_add_i = v1.add(vt2);
+ Dfp[] result_add_i = {Dfp25.of(5), Dfp25.of(7), Dfp25.of(9)};
checkArray("compare vect" ,v_add_i.toArray(),result_add_i);
//octave = v1 - v2
- ArrayFieldVector<Fraction> v_subtract = v1.subtract(v2);
- Fraction[] result_subtract = {new Fraction(-3), new Fraction(-3), new Fraction(-3)};
+ ArrayFieldVector<Dfp> v_subtract = v1.subtract(v2);
+ Dfp[] result_subtract = {Dfp25.of(-3), Dfp25.of(-3), Dfp25.of(-3)};
checkArray("compare vect" ,v_subtract.toArray(),result_subtract);
- FieldVector<Fraction> v_subtract_i = v1.subtract(vt2);
- Fraction[] result_subtract_i = {new Fraction(-3), new Fraction(-3), new Fraction(-3)};
+ FieldVector<Dfp> v_subtract_i = v1.subtract(vt2);
+ Dfp[] result_subtract_i = {Dfp25.of(-3), Dfp25.of(-3), Dfp25.of(-3)};
checkArray("compare vect" ,v_subtract_i.toArray(),result_subtract_i);
// octave v1 .* v2
- ArrayFieldVector<Fraction> v_ebeMultiply = v1.ebeMultiply(v2);
- Fraction[] result_ebeMultiply = {new Fraction(4), new Fraction(10), new Fraction(18)};
+ ArrayFieldVector<Dfp> v_ebeMultiply = v1.ebeMultiply(v2);
+ Dfp[] result_ebeMultiply = {Dfp25.of(4), Dfp25.of(10), Dfp25.of(18)};
checkArray("compare vect" ,v_ebeMultiply.toArray(),result_ebeMultiply);
- FieldVector<Fraction> v_ebeMultiply_2 = v1.ebeMultiply(v2_t);
- Fraction[] result_ebeMultiply_2 = {new Fraction(4), new Fraction(10), new Fraction(18)};
+ FieldVector<Dfp> v_ebeMultiply_2 = v1.ebeMultiply(v2_t);
+ Dfp[] result_ebeMultiply_2 = {Dfp25.of(4), Dfp25.of(10), Dfp25.of(18)};
checkArray("compare vect" ,v_ebeMultiply_2.toArray(),result_ebeMultiply_2);
// octave v1 ./ v2
- ArrayFieldVector<Fraction> v_ebeDivide = v1.ebeDivide(v2);
- Fraction[] result_ebeDivide = {new Fraction(1, 4), new Fraction(2, 5), new Fraction(1, 2)};
+ ArrayFieldVector<Dfp> v_ebeDivide = v1.ebeDivide(v2);
+ Dfp[] result_ebeDivide = {Dfp25.of(1, 4), Dfp25.of(2, 5), Dfp25.of(1, 2)};
checkArray("compare vect" ,v_ebeDivide.toArray(),result_ebeDivide);
- FieldVector<Fraction> v_ebeDivide_2 = v1.ebeDivide(v2_t);
- Fraction[] result_ebeDivide_2 = {new Fraction(1, 4), new Fraction(2, 5), new Fraction(1, 2)};
+ FieldVector<Dfp> v_ebeDivide_2 = v1.ebeDivide(v2_t);
+ Dfp[] result_ebeDivide_2 = {Dfp25.of(1, 4), Dfp25.of(2, 5), Dfp25.of(1, 2)};
checkArray("compare vect" ,v_ebeDivide_2.toArray(),result_ebeDivide_2);
// octave dot(v1,v2)
- Fraction dot = v1.dotProduct(v2);
- Assert.assertEquals("compare val ",new Fraction(32), dot);
+ Dfp dot = v1.dotProduct(v2);
+ Assert.assertEquals("compare val ",Dfp25.of(32), dot);
// octave dot(v1,v2_t)
- Fraction dot_2 = v1.dotProduct(v2_t);
- Assert.assertEquals("compare val ",new Fraction(32), dot_2);
+ Dfp dot_2 = v1.dotProduct(v2_t);
+ Assert.assertEquals("compare val ",Dfp25.of(32), dot_2);
- FieldMatrix<Fraction> m_outerProduct = v1.outerProduct(v2);
- Assert.assertEquals("compare val ",new Fraction(4), m_outerProduct.getEntry(0,0));
+ FieldMatrix<Dfp> m_outerProduct = v1.outerProduct(v2);
+ Assert.assertEquals("compare val ",Dfp25.of(4), m_outerProduct.getEntry(0,0));
- FieldMatrix<Fraction> m_outerProduct_2 = v1.outerProduct(v2_t);
- Assert.assertEquals("compare val ",new Fraction(4), m_outerProduct_2.getEntry(0,0));
+ FieldMatrix<Dfp> m_outerProduct_2 = v1.outerProduct(v2_t);
+ Assert.assertEquals("compare val ",Dfp25.of(4), m_outerProduct_2.getEntry(0,0));
- ArrayFieldVector<Fraction> v_projection = v1.projection(v2);
- Fraction[] result_projection = {new Fraction(128, 77), new Fraction(160, 77), new Fraction(192, 77)};
+ ArrayFieldVector<Dfp> v_projection = v1.projection(v2);
+ Dfp[] result_projection = {Dfp25.of(128, 77), Dfp25.of(160, 77), Dfp25.of(192, 77)};
checkArray("compare vect", v_projection.toArray(), result_projection);
- FieldVector<Fraction> v_projection_2 = v1.projection(v2_t);
- Fraction[] result_projection_2 = {new Fraction(128, 77), new Fraction(160, 77), new Fraction(192, 77)};
+ FieldVector<Dfp> v_projection_2 = v1.projection(v2_t);
+ Dfp[] result_projection_2 = {Dfp25.of(128, 77), Dfp25.of(160, 77), Dfp25.of(192, 77)};
checkArray("compare vect", v_projection_2.toArray(), result_projection_2);
}
@Test
public void testMisc() {
- ArrayFieldVector<Fraction> v1 = new ArrayFieldVector<>(vec1);
- ArrayFieldVector<Fraction> v4 = new ArrayFieldVector<>(vec4);
- FieldVector<Fraction> v4_2 = new ArrayFieldVector<>(vec4);
+ ArrayFieldVector<Dfp> v1 = new ArrayFieldVector<>(vec1);
+ ArrayFieldVector<Dfp> v4 = new ArrayFieldVector<>(vec4);
+ FieldVector<Dfp> v4_2 = new ArrayFieldVector<>(vec4);
String out1 = v1.toString();
Assert.assertTrue("some output ", out1.length()!=0);
/*
- Fraction[] dout1 = v1.copyOut();
+ Dfp[] dout1 = v1.copyOut();
Assert.assertEquals(3, dout1.length);
assertNotSame("testData not same object ", v1.getDataRef(), dout1);
*/
@@ -624,8 +626,12 @@ public class ArrayFieldVectorTest {
@Test
public void testSerial() {
- ArrayFieldVector<Fraction> v = new ArrayFieldVector<>(vec1);
- Assert.assertEquals(v,TestUtils.serializeAndRecover(v));
+ final int n = 2;
+ ArrayFieldVector<BigReal> v = new ArrayFieldVector<>(BigRealField.getInstance());
+ for (int i = 0; i < n; i++) {
+ v.append(new BigReal(Math.random()));
+ }
+ Assert.assertEquals(v, TestUtils.serializeAndRecover(v));
}
@Test
@@ -633,56 +639,56 @@ public class ArrayFieldVectorTest {
// when the field is not specified, array cannot be empty
try {
- new ArrayFieldVector<>(new Fraction[0]);
+ new ArrayFieldVector<>(new Dfp[0]);
Assert.fail("MathIllegalArgumentException expected");
} catch (MathIllegalArgumentException ex) {
// expected behavior
}
try {
- new ArrayFieldVector<>(new Fraction[0], true);
+ new ArrayFieldVector<>(new Dfp[0], true);
Assert.fail("MathIllegalArgumentException expected");
} catch (MathIllegalArgumentException ex) {
// expected behavior
}
try {
- new ArrayFieldVector<>(new Fraction[0], false);
+ new ArrayFieldVector<>(new Dfp[0], false);
Assert.fail("MathIllegalArgumentException expected");
} catch (MathIllegalArgumentException ex) {
// expected behavior
}
// when the field is specified, array can be empty
- Assert.assertEquals(0, new ArrayFieldVector<>(FractionField.getInstance(), new Fraction[0]).getDimension());
- Assert.assertEquals(0, new ArrayFieldVector<>(FractionField.getInstance(), new Fraction[0], true).getDimension());
- Assert.assertEquals(0, new ArrayFieldVector<>(FractionField.getInstance(), new Fraction[0], false).getDimension());
+ Assert.assertEquals(0, new ArrayFieldVector<>(Dfp25.getField(), new Dfp[0]).getDimension());
+ Assert.assertEquals(0, new ArrayFieldVector<>(Dfp25.getField(), new Dfp[0], true).getDimension());
+ Assert.assertEquals(0, new ArrayFieldVector<>(Dfp25.getField(), new Dfp[0], false).getDimension());
}
@Test
public void testOuterProduct() {
- final ArrayFieldVector<Fraction> u
- = new ArrayFieldVector<>(FractionField.getInstance(),
- new Fraction[] {new Fraction(1),
- new Fraction(2),
- new Fraction(-3)});
- final ArrayFieldVector<Fraction> v
- = new ArrayFieldVector<>(FractionField.getInstance(),
- new Fraction[] {new Fraction(4),
- new Fraction(-2)});
-
- final FieldMatrix<Fraction> uv = u.outerProduct(v);
+ final ArrayFieldVector<Dfp> u
+ = new ArrayFieldVector<>(Dfp25.getField(),
+ new Dfp[] {Dfp25.of(1),
+ Dfp25.of(2),
+ Dfp25.of(-3)});
+ final ArrayFieldVector<Dfp> v
+ = new ArrayFieldVector<>(Dfp25.getField(),
+ new Dfp[] {Dfp25.of(4),
+ Dfp25.of(-2)});
+
+ final FieldMatrix<Dfp> uv = u.outerProduct(v);
final double tol = Math.ulp(1d);
- Assert.assertEquals(new Fraction(4).doubleValue(), uv.getEntry(0, 0).doubleValue(), tol);
- Assert.assertEquals(new Fraction(-2).doubleValue(), uv.getEntry(0, 1).doubleValue(), tol);
- Assert.assertEquals(new Fraction(8).doubleValue(), uv.getEntry(1, 0).doubleValue(), tol);
- Assert.assertEquals(new Fraction(-4).doubleValue(), uv.getEntry(1, 1).doubleValue(), tol);
- Assert.assertEquals(new Fraction(-12).doubleValue(), uv.getEntry(2, 0).doubleValue(), tol);
- Assert.assertEquals(new Fraction(6).doubleValue(), uv.getEntry(2, 1).doubleValue(), tol);
+ Assert.assertEquals(Dfp25.of(4).toDouble(), uv.getEntry(0, 0).toDouble(), tol);
+ Assert.assertEquals(Dfp25.of(-2).toDouble(), uv.getEntry(0, 1).toDouble(), tol);
+ Assert.assertEquals(Dfp25.of(8).toDouble(), uv.getEntry(1, 0).toDouble(), tol);
+ Assert.assertEquals(Dfp25.of(-4).toDouble(), uv.getEntry(1, 1).toDouble(), tol);
+ Assert.assertEquals(Dfp25.of(-12).toDouble(), uv.getEntry(2, 0).toDouble(), tol);
+ Assert.assertEquals(Dfp25.of(6).toDouble(), uv.getEntry(2, 1).toDouble(), tol);
}
/** verifies that two vectors are equals */
- protected void checkArray(String msg, Fraction[] m, Fraction[] n) {
+ protected void checkArray(String msg, Dfp[] m, Dfp[] n) {
if (m.length != n.length) {
Assert.fail("vectors have different lengths");
}
@@ -698,19 +704,19 @@ public class ArrayFieldVectorTest {
/** The whole vector is visited. */
@Test
public void testWalkInDefaultOrderPreservingVisitor1() {
- final Fraction[] data = new Fraction[] {
- Fraction.ZERO, Fraction.ONE, Fraction.ZERO,
- Fraction.ZERO, Fraction.TWO, Fraction.ZERO,
- Fraction.ZERO, Fraction.ZERO, new Fraction(3)
+ final Dfp[] data = new Dfp[] {
+ Dfp25.ZERO, Dfp25.ONE, Dfp25.ZERO,
+ Dfp25.ZERO, Dfp25.TWO, Dfp25.ZERO,
+ Dfp25.ZERO, Dfp25.ZERO, Dfp25.of(3)
};
- final ArrayFieldVector<Fraction> v = new ArrayFieldVector<>(data);
- final FieldVectorPreservingVisitor<Fraction> visitor;
- visitor = new FieldVectorPreservingVisitor<Fraction>() {
+ final ArrayFieldVector<Dfp> v = new ArrayFieldVector<>(data);
+ final FieldVectorPreservingVisitor<Dfp> visitor;
+ visitor = new FieldVectorPreservingVisitor<Dfp>() {
private int expectedIndex;
@Override
- public void visit(final int actualIndex, final Fraction actualValue) {
+ public void visit(final int actualIndex, final Dfp actualValue) {
Assert.assertEquals(expectedIndex, actualIndex);
Assert.assertEquals(Integer.toString(actualIndex),
data[actualIndex], actualValue);
@@ -727,8 +733,8 @@ public class ArrayFieldVectorTest {
}
@Override
- public Fraction end() {
- return Fraction.ZERO;
+ public Dfp end() {
+ return Dfp25.ZERO;
}
};
v.walkInDefaultOrder(visitor);
@@ -737,12 +743,12 @@ public class ArrayFieldVectorTest {
/** Visiting an invalid subvector. */
@Test
public void testWalkInDefaultOrderPreservingVisitor2() {
- final ArrayFieldVector<Fraction> v = create(5);
- final FieldVectorPreservingVisitor<Fraction> visitor;
- visitor = new FieldVectorPreservingVisitor<Fraction>() {
+ final ArrayFieldVector<Dfp> v = create(5);
+ final FieldVectorPreservingVisitor<Dfp> visitor;
+ visitor = new FieldVectorPreservingVisitor<Dfp>() {
@Override
- public void visit(int index, Fraction value) {
+ public void visit(int index, Dfp value) {
// Do nothing
}
@@ -752,8 +758,8 @@ public class ArrayFieldVectorTest {
}
@Override
- public Fraction end() {
- return Fraction.ZERO;
+ public Dfp end() {
+ return Dfp25.ZERO;
}
};
try {
@@ -791,21 +797,21 @@ public class ArrayFieldVectorTest {
/** Visiting a valid subvector. */
@Test
public void testWalkInDefaultOrderPreservingVisitor3() {
- final Fraction[] data = new Fraction[] {
- Fraction.ZERO, Fraction.ONE, Fraction.ZERO,
- Fraction.ZERO, Fraction.TWO, Fraction.ZERO,
- Fraction.ZERO, Fraction.ZERO, new Fraction(3)
+ final Dfp[] data = new Dfp[] {
+ Dfp25.ZERO, Dfp25.ONE, Dfp25.ZERO,
+ Dfp25.ZERO, Dfp25.TWO, Dfp25.ZERO,
+ Dfp25.ZERO, Dfp25.ZERO, Dfp25.of(3)
};
- final ArrayFieldVector<Fraction> v = new ArrayFieldVector<>(data);
+ final ArrayFieldVector<Dfp> v = new ArrayFieldVector<>(data);
final int expectedStart = 2;
final int expectedEnd = 7;
- final FieldVectorPreservingVisitor<Fraction> visitor;
- visitor = new FieldVectorPreservingVisitor<Fraction>() {
+ final FieldVectorPreservingVisitor<Dfp> visitor;
+ visitor = new FieldVectorPreservingVisitor<Dfp>() {
private int expectedIndex;
@Override
- public void visit(final int actualIndex, final Fraction actualValue) {
+ public void visit(final int actualIndex, final Dfp actualValue) {
Assert.assertEquals(expectedIndex, actualIndex);
Assert.assertEquals(Integer.toString(actualIndex),
data[actualIndex], actualValue);
@@ -822,8 +828,8 @@ public class ArrayFieldVectorTest {
}
@Override
- public Fraction end() {
- return Fraction.ZERO;
+ public Dfp end() {
+ return Dfp25.ZERO;
}
};
v.walkInDefaultOrder(visitor, expectedStart, expectedEnd);
@@ -832,18 +838,18 @@ public class ArrayFieldVectorTest {
/** The whole vector is visited. */
@Test
public void testWalkInOptimizedOrderPreservingVisitor1() {
- final Fraction[] data = new Fraction[] {
- Fraction.ZERO, Fraction.ONE, Fraction.ZERO,
- Fraction.ZERO, Fraction.TWO, Fraction.ZERO,
- Fraction.ZERO, Fraction.ZERO, new Fraction(3)
+ final Dfp[] data = new Dfp[] {
+ Dfp25.ZERO, Dfp25.ONE, Dfp25.ZERO,
+ Dfp25.ZERO, Dfp25.TWO, Dfp25.ZERO,
+ Dfp25.ZERO, Dfp25.ZERO, Dfp25.of(3)
};
- final ArrayFieldVector<Fraction> v = new ArrayFieldVector<>(data);
- final FieldVectorPreservingVisitor<Fraction> visitor;
- visitor = new FieldVectorPreservingVisitor<Fraction>() {
+ final ArrayFieldVector<Dfp> v = new ArrayFieldVector<>(data);
+ final FieldVectorPreservingVisitor<Dfp> visitor;
+ visitor = new FieldVectorPreservingVisitor<Dfp>() {
private final boolean[] visited = new boolean[data.length];
@Override
- public void visit(final int actualIndex, final Fraction actualValue) {
+ public void visit(final int actualIndex, final Dfp actualValue) {
visited[actualIndex] = true;
Assert.assertEquals(Integer.toString(actualIndex),
data[actualIndex], actualValue);
@@ -859,12 +865,12 @@ public class ArrayFieldVectorTest {
}
@Override
- public Fraction end() {
+ public Dfp end() {
for (int i = 0; i < data.length; i++) {
Assert.assertTrue("entry " + i + "has not been visited",
visited[i]);
}
- return Fraction.ZERO;
+ return Dfp25.ZERO;
}
};
v.walkInOptimizedOrder(visitor);
@@ -873,12 +879,12 @@ public class ArrayFieldVectorTest {
/** Visiting an invalid subvector. */
@Test
public void testWalkInOptimizedOrderPreservingVisitor2() {
- final ArrayFieldVector<Fraction> v = create(5);
- final FieldVectorPreservingVisitor<Fraction> visitor;
- visitor = new FieldVectorPreservingVisitor<Fraction>() {
+ final ArrayFieldVector<Dfp> v = create(5);
+ final FieldVectorPreservingVisitor<Dfp> visitor;
+ visitor = new FieldVectorPreservingVisitor<Dfp>() {
@Override
- public void visit(int index, Fraction value) {
+ public void visit(int index, Dfp value) {
// Do nothing
}
@@ -888,8 +894,8 @@ public class ArrayFieldVectorTest {
}
@Override
- public Fraction end() {
- return Fraction.ZERO;
+ public Dfp end() {
+ return Dfp25.ZERO;
}
};
try {
@@ -927,20 +933,20 @@ public class ArrayFieldVectorTest {
/** Visiting a valid subvector. */
@Test
public void testWalkInOptimizedOrderPreservingVisitor3() {
- final Fraction[] data = new Fraction[] {
- Fraction.ZERO, Fraction.ONE, Fraction.ZERO,
- Fraction.ZERO, Fraction.TWO, Fraction.ZERO,
- Fraction.ZERO, Fraction.ZERO, new Fraction(3)
+ final Dfp[] data = new Dfp[] {
+ Dfp25.ZERO, Dfp25.ONE, Dfp25.ZERO,
+ Dfp25.ZERO, Dfp25.TWO, Dfp25.ZERO,
+ Dfp25.ZERO, Dfp25.ZERO, Dfp25.of(3)
};
- final ArrayFieldVector<Fraction> v = new ArrayFieldVector<>(data);
+ final ArrayFieldVector<Dfp> v = new ArrayFieldVector<>(data);
final int expectedStart = 2;
final int expectedEnd = 7;
- final FieldVectorPreservingVisitor<Fraction> visitor;
- visitor = new FieldVectorPreservingVisitor<Fraction>() {
+ final FieldVectorPreservingVisitor<Dfp> visitor;
+ visitor = new FieldVectorPreservingVisitor<Dfp>() {
private final boolean[] visited = new boolean[data.length];
@Override
- public void visit(final int actualIndex, final Fraction actualValue) {
+ public void visit(final int actualIndex, final Dfp actualValue) {
Assert.assertEquals(Integer.toString(actualIndex),
data[actualIndex], actualValue);
visited[actualIndex] = true;
@@ -956,12 +962,12 @@ public class ArrayFieldVectorTest {
}
@Override
- public Fraction end() {
+ public Dfp end() {
for (int i = expectedStart; i <= expectedEnd; i++) {
Assert.assertTrue("entry " + i + "has not been visited",
visited[i]);
}
- return Fraction.ZERO;
+ return Dfp25.ZERO;
}
};
v.walkInOptimizedOrder(visitor, expectedStart, expectedEnd);
@@ -970,19 +976,19 @@ public class ArrayFieldVectorTest {
/** The whole vector is visited. */
@Test
public void testWalkInDefaultOrderChangingVisitor1() {
- final Fraction[] data = new Fraction[] {
- Fraction.ZERO, Fraction.ONE, Fraction.ZERO,
- Fraction.ZERO, Fraction.TWO, Fraction.ZERO,
- Fraction.ZERO, Fraction.ZERO, new Fraction(3)
+ final Dfp[] data = new Dfp[] {
+ Dfp25.ZERO, Dfp25.ONE, Dfp25.ZERO,
+ Dfp25.ZERO, Dfp25.TWO, Dfp25.ZERO,
+ Dfp25.ZERO, Dfp25.ZERO, Dfp25.of(3)
};
- final ArrayFieldVector<Fraction> v = new ArrayFieldVector<>(data);
- final FieldVectorChangingVisitor<Fraction> visitor;
- visitor = new FieldVectorChangingVisitor<Fraction>() {
+ final ArrayFieldVector<Dfp> v = new ArrayFieldVector<>(data);
+ final FieldVectorChangingVisitor<Dfp> visitor;
+ visitor = new FieldVectorChangingVisitor<Dfp>() {
private int expectedIndex;
@Override
- public Fraction visit(final int actualIndex, final Fraction actualValue) {
+ public Dfp visit(final int actualIndex, final Dfp actualValue) {
Assert.assertEquals(expectedIndex, actualIndex);
Assert.assertEquals(Integer.toString(actualIndex),
data[actualIndex], actualValue);
@@ -1000,8 +1006,8 @@ public class ArrayFieldVectorTest {
}
@Override
- public Fraction end() {
- return Fraction.ZERO;
+ public Dfp end() {
+ return Dfp25.ZERO;
}
};
v.walkInDefaultOrder(visitor);
@@ -1013,13 +1019,13 @@ public class ArrayFieldVectorTest {
/** Visiting an invalid subvector. */
@Test
public void testWalkInDefaultOrderChangingVisitor2() {
- final ArrayFieldVector<Fraction> v = create(5);
- final FieldVectorChangingVisitor<Fraction> visitor;
- visitor = new FieldVectorChangingVisitor<Fraction>() {
+ final ArrayFieldVector<Dfp> v = create(5);
+ final FieldVectorChangingVisitor<Dfp> visitor;
+ visitor = new FieldVectorChangingVisitor<Dfp>() {
@Override
- public Fraction visit(int index, Fraction value) {
- return Fraction.ZERO;
+ public Dfp visit(int index, Dfp value) {
+ return Dfp25.ZERO;
}
@Override
@@ -1028,8 +1034,8 @@ public class ArrayFieldVectorTest {
}
@Override
- public Fraction end() {
- return Fraction.ZERO;
+ public Dfp end() {
+ return Dfp25.ZERO;
}
};
try {
@@ -1067,21 +1073,21 @@ public class ArrayFieldVectorTest {
/** Visiting a valid subvector. */
@Test
public void testWalkInDefaultOrderChangingVisitor3() {
- final Fraction[] data = new Fraction[] {
- Fraction.ZERO, Fraction.ONE, Fraction.ZERO,
- Fraction.ZERO, Fraction.TWO, Fraction.ZERO,
- Fraction.ZERO, Fraction.ZERO, new Fraction(3)
+ final Dfp[] data = new Dfp[] {
+ Dfp25.ZERO, Dfp25.ONE, Dfp25.ZERO,
+ Dfp25.ZERO, Dfp25.TWO, Dfp25.ZERO,
+ Dfp25.ZERO, Dfp25.ZERO, Dfp25.of(3)
};
- final ArrayFieldVector<Fraction> v = new ArrayFieldVector<>(data);
+ final ArrayFieldVector<Dfp> v = new ArrayFieldVector<>(data);
final int expectedStart = 2;
final int expectedEnd = 7;
- final FieldVectorChangingVisitor<Fraction> visitor;
- visitor = new FieldVectorChangingVisitor<Fraction>() {
+ final FieldVectorChangingVisitor<Dfp> visitor;
+ visitor = new FieldVectorChangingVisitor<Dfp>() {
private int expectedIndex;
@Override
- public Fraction visit(final int actualIndex, final Fraction actualValue) {
+ public Dfp visit(final int actualIndex, final Dfp actualValue) {
Assert.assertEquals(expectedIndex, actualIndex);
Assert.assertEquals(Integer.toString(actualIndex),
data[actualIndex], actualValue);
@@ -1099,8 +1105,8 @@ public class ArrayFieldVectorTest {
}
@Override
- public Fraction end() {
- return Fraction.ZERO;
+ public Dfp end() {
+ return Dfp25.ZERO;
}
};
v.walkInDefaultOrder(visitor, expectedStart, expectedEnd);
@@ -1112,18 +1118,18 @@ public class ArrayFieldVectorTest {
/** The whole vector is visited. */
@Test
public void testWalkInOptimizedOrderChangingVisitor1() {
- final Fraction[] data = new Fraction[] {
- Fraction.ZERO, Fraction.ONE, Fraction.ZERO,
- Fraction.ZERO, Fraction.TWO, Fraction.ZERO,
- Fraction.ZERO, Fraction.ZERO, new Fraction(3)
+ final Dfp[] data = new Dfp[] {
+ Dfp25.ZERO, Dfp25.ONE, Dfp25.ZERO,
+ Dfp25.ZERO, Dfp25.TWO, Dfp25.ZERO,
+ Dfp25.ZERO, Dfp25.ZERO, Dfp25.of(3)
};
- final ArrayFieldVector<Fraction> v = new ArrayFieldVector<>(data);
- final FieldVectorChangingVisitor<Fraction> visitor;
- visitor = new FieldVectorChangingVisitor<Fraction>() {
+ final ArrayFieldVector<Dfp> v = new ArrayFieldVector<>(data);
+ final FieldVectorChangingVisitor<Dfp> visitor;
+ visitor = new FieldVectorChangingVisitor<Dfp>() {
private final boolean[] visited = new boolean[data.length];
@Override
- public Fraction visit(final int actualIndex, final Fraction actualValue) {
+ public Dfp visit(final int actualIndex, final Dfp actualValue) {
visited[actualIndex] = true;
Assert.assertEquals(Integer.toString(actualIndex),
data[actualIndex], actualValue);
@@ -1140,12 +1146,12 @@ public class ArrayFieldVectorTest {
}
@Override
- public Fraction end() {
+ public Dfp end() {
for (int i = 0; i < data.length; i++) {
Assert.assertTrue("entry " + i + "has not been visited",
visited[i]);
}
- return Fraction.ZERO;
+ return Dfp25.ZERO;
}
};
v.walkInOptimizedOrder(visitor);
@@ -1157,13 +1163,13 @@ public class ArrayFieldVectorTest {
/** Visiting an invalid subvector. */
@Test
public void testWalkInOptimizedOrderChangingVisitor2() {
- final ArrayFieldVector<Fraction> v = create(5);
- final FieldVectorChangingVisitor<Fraction> visitor;
- visitor = new FieldVectorChangingVisitor<Fraction>() {
+ final ArrayFieldVector<Dfp> v = create(5);
+ final FieldVectorChangingVisitor<Dfp> visitor;
+ visitor = new FieldVectorChangingVisitor<Dfp>() {
@Override
- public Fraction visit(int index, Fraction value) {
- return Fraction.ZERO;
+ public Dfp visit(int index, Dfp value) {
+ return Dfp25.ZERO;
}
@Override
@@ -1172,8 +1178,8 @@ public class ArrayFieldVectorTest {
}
@Override
- public Fraction end() {
- return Fraction.ZERO;
+ public Dfp end() {
+ return Dfp25.ZERO;
}
};
try {
@@ -1211,20 +1217,20 @@ public class ArrayFieldVectorTest {
/** Visiting a valid subvector. */
@Test
public void testWalkInOptimizedOrderChangingVisitor3() {
- final Fraction[] data = new Fraction[] {
- Fraction.ZERO, Fraction.ONE, Fraction.ZERO,
- Fraction.ZERO, Fraction.TWO, Fraction.ZERO,
- Fraction.ZERO, Fraction.ZERO, new Fraction(3)
+ final Dfp[] data = new Dfp[] {
+ Dfp25.ZERO, Dfp25.ONE, Dfp25.ZERO,
+ Dfp25.ZERO, Dfp25.TWO, Dfp25.ZERO,
+ Dfp25.ZERO, Dfp25.ZERO, Dfp25.of(3)
};
- final ArrayFieldVector<Fraction> v = new ArrayFieldVector<>(data);
+ final ArrayFieldVector<Dfp> v = new ArrayFieldVector<>(data);
final int expectedStart = 2;
final int expectedEnd = 7;
- final FieldVectorChangingVisitor<Fraction> visitor;
- visitor = new FieldVectorChangingVisitor<Fraction>() {
+ final FieldVectorChangingVisitor<Dfp> visitor;
+ visitor = new FieldVectorChangingVisitor<Dfp>() {
private final boolean[] visited = new boolean[data.length];
@Override
- public Fraction visit(final int actualIndex, final Fraction actualValue) {
+ public Dfp visit(final int actualIndex, final Dfp actualValue) {
Assert.assertEquals(Integer.toString(actualIndex),
data[actualIndex], actualValue);
visited[actualIndex] = true;
@@ -1241,12 +1247,12 @@ public class ArrayFieldVectorTest {
}
@Override
- public Fraction end() {
+ public Dfp end() {
for (int i = expectedStart; i <= expectedEnd; i++) {
Assert.assertTrue("entry " + i + "has not been visited",
visited[i]);
}
- return Fraction.ZERO;
+ return Dfp25.ZERO;
}
};
v.walkInOptimizedOrder(visitor, expectedStart, expectedEnd);
@@ -1255,10 +1261,10 @@ public class ArrayFieldVectorTest {
}
}
- private ArrayFieldVector<Fraction> create(int n) {
- Fraction[] t = new Fraction[n];
+ private ArrayFieldVector<Dfp> create(int n) {
+ Dfp[] t = new Dfp[n];
for (int i = 0; i < n; ++i) {
- t[i] = Fraction.ZERO;
+ t[i] = Dfp25.ZERO;
}
return new ArrayFieldVector<>(t);
}
diff --git a/src/test/java/org/apache/commons/math4/linear/BlockFieldMatrixTest.java b/src/test/java/org/apache/commons/math4/linear/BlockFieldMatrixTest.java
index 57c9cb7..a46ed46 100644
--- a/src/test/java/org/apache/commons/math4/linear/BlockFieldMatrixTest.java
+++ b/src/test/java/org/apache/commons/math4/linear/BlockFieldMatrixTest.java
@@ -28,8 +28,10 @@ import org.apache.commons.math4.exception.NotStrictlyPositiveException;
import org.apache.commons.math4.exception.NullArgumentException;
import org.apache.commons.math4.exception.NumberIsTooSmallException;
import org.apache.commons.math4.exception.OutOfRangeException;
-import org.apache.commons.math4.fraction.Fraction;
-import org.apache.commons.math4.fraction.FractionField;
+import org.apache.commons.math4.dfp.Dfp;
+import org.apache.commons.math4.dfp.DfpField;
+import org.apache.commons.math4.util.BigReal;
+import org.apache.commons.math4.util.BigRealField;
import org.apache.commons.math4.linear.ArrayFieldVector;
import org.apache.commons.math4.linear.BlockFieldMatrix;
import org.apache.commons.math4.linear.DefaultFieldMatrixChangingVisitor;
@@ -48,117 +50,117 @@ import org.apache.commons.math4.linear.NonSquareMatrixException;
public final class BlockFieldMatrixTest {
// 3 x 3 identity matrix
- protected Fraction[][] id = {
- {new Fraction(1),new Fraction(0),new Fraction(0)},
- {new Fraction(0),new Fraction(1),new Fraction(0)},
- {new Fraction(0),new Fraction(0),new Fraction(1)}
+ protected Dfp[][] id = {
+ {Dfp25.of(1),Dfp25.of(0),Dfp25.of(0)},
+ {Dfp25.of(0),Dfp25.of(1),Dfp25.of(0)},
+ {Dfp25.of(0),Dfp25.of(0),Dfp25.of(1)}
};
// Test data for group operations
- protected Fraction[][] testData = {
- {new Fraction(1),new Fraction(2),new Fraction(3)},
- {new Fraction(2),new Fraction(5),new Fraction(3)},
- {new Fraction(1),new Fraction(0),new Fraction(8)}
+ protected Dfp[][] testData = {
+ {Dfp25.of(1),Dfp25.of(2),Dfp25.of(3)},
+ {Dfp25.of(2),Dfp25.of(5),Dfp25.of(3)},
+ {Dfp25.of(1),Dfp25.of(0),Dfp25.of(8)}
};
- protected Fraction[][] testDataLU = {
- {new Fraction(2), new Fraction(5), new Fraction(3)},
- {new Fraction(1, 2), new Fraction(-5, 2), new Fraction(13, 2)},
- {new Fraction(1, 2), new Fraction(1, 5), new Fraction(1, 5)}
+ protected Dfp[][] testDataLU = {
+ {Dfp25.of(2), Dfp25.of(5), Dfp25.of(3)},
+ {Dfp25.of(1, 2), Dfp25.of(-5, 2), Dfp25.of(13, 2)},
+ {Dfp25.of(1, 2), Dfp25.of(1, 5), Dfp25.of(1, 5)}
};
- protected Fraction[][] testDataPlus2 = {
- {new Fraction(3),new Fraction(4),new Fraction(5)},
- {new Fraction(4),new Fraction(7),new Fraction(5)},
- {new Fraction(3),new Fraction(2),new Fraction(10)}
+ protected Dfp[][] testDataPlus2 = {
+ {Dfp25.of(3),Dfp25.of(4),Dfp25.of(5)},
+ {Dfp25.of(4),Dfp25.of(7),Dfp25.of(5)},
+ {Dfp25.of(3),Dfp25.of(2),Dfp25.of(10)}
};
- protected Fraction[][] testDataMinus = {
- {new Fraction(-1),new Fraction(-2),new Fraction(-3)},
- {new Fraction(-2),new Fraction(-5),new Fraction(-3)},
- {new Fraction(-1),new Fraction(0),new Fraction(-8)}
+ protected Dfp[][] testDataMinus = {
+ {Dfp25.of(-1),Dfp25.of(-2),Dfp25.of(-3)},
+ {Dfp25.of(-2),Dfp25.of(-5),Dfp25.of(-3)},
+ {Dfp25.of(-1),Dfp25.of(0),Dfp25.of(-8)}
};
- protected Fraction[] testDataRow1 = {new Fraction(1),new Fraction(2),new Fraction(3)};
- protected Fraction[] testDataCol3 = {new Fraction(3),new Fraction(3),new Fraction(8)};
- protected Fraction[][] testDataInv = {
- {new Fraction(-40),new Fraction(16),new Fraction(9)},
- {new Fraction(13),new Fraction(-5),new Fraction(-3)},
- {new Fraction(5),new Fraction(-2),new Fraction(-1)}
+ protected Dfp[] testDataRow1 = {Dfp25.of(1),Dfp25.of(2),Dfp25.of(3)};
+ protected Dfp[] testDataCol3 = {Dfp25.of(3),Dfp25.of(3),Dfp25.of(8)};
+ protected Dfp[][] testDataInv = {
+ {Dfp25.of(-40),Dfp25.of(16),Dfp25.of(9)},
+ {Dfp25.of(13),Dfp25.of(-5),Dfp25.of(-3)},
+ {Dfp25.of(5),Dfp25.of(-2),Dfp25.of(-1)}
};
- protected Fraction[] preMultTest = {new Fraction(8), new Fraction(12), new Fraction(33)};
- protected Fraction[][] testData2 = {
- {new Fraction(1),new Fraction(2),new Fraction(3)},
- {new Fraction(2),new Fraction(5),new Fraction(3)}
+ protected Dfp[] preMultTest = {Dfp25.of(8), Dfp25.of(12), Dfp25.of(33)};
+ protected Dfp[][] testData2 = {
+ {Dfp25.of(1),Dfp25.of(2),Dfp25.of(3)},
+ {Dfp25.of(2),Dfp25.of(5),Dfp25.of(3)}
};
- protected Fraction[][] testData2T = {
- {new Fraction(1),new Fraction(2)},
- {new Fraction(2),new Fraction(5)},
- {new Fraction(3),new Fraction(3)}
+ protected Dfp[][] testData2T = {
+ {Dfp25.of(1),Dfp25.of(2)},
+ {Dfp25.of(2),Dfp25.of(5)},
+ {Dfp25.of(3),Dfp25.of(3)}
};
- protected Fraction[][] testDataPlusInv = {
- {new Fraction(-39),new Fraction(18),new Fraction(12)},
- {new Fraction(15),new Fraction(0),new Fraction(0)},
- {new Fraction(6),new Fraction(-2),new Fraction(7)}
+ protected Dfp[][] testDataPlusInv = {
+ {Dfp25.of(-39),Dfp25.of(18),Dfp25.of(12)},
+ {Dfp25.of(15),Dfp25.of(0),Dfp25.of(0)},
+ {Dfp25.of(6),Dfp25.of(-2),Dfp25.of(7)}
};
// lu decomposition tests
- protected Fraction[][] luData = {
- {new Fraction(2),new Fraction(3),new Fraction(3)},
- {new Fraction(0),new Fraction(5),new Fraction(7)},
- {new Fraction(6),new Fraction(9),new Fraction(8)}
+ protected Dfp[][] luData = {
+ {Dfp25.of(2),Dfp25.of(3),Dfp25.of(3)},
+ {Dfp25.of(0),Dfp25.of(5),Dfp25.of(7)},
+ {Dfp25.of(6),Dfp25.of(9),Dfp25.of(8)}
};
- protected Fraction[][] luDataLUDecomposition = {
- {new Fraction(6),new Fraction(9),new Fraction(8)},
- {new Fraction(0),new Fraction(5),new Fraction(7)},
- {new Fraction(1, 3),new Fraction(0),new Fraction(1, 3)}
+ protected Dfp[][] luDataLUDecomposition = {
+ {Dfp25.of(6),Dfp25.of(9),Dfp25.of(8)},
+ {Dfp25.of(0),Dfp25.of(5),Dfp25.of(7)},
+ {Dfp25.of(1, 3),Dfp25.of(0),Dfp25.of(1, 3)}
};
// singular matrices
- protected Fraction[][] singular = { {new Fraction(2),new Fraction(3)}, {new Fraction(2),new Fraction(3)} };
- protected Fraction[][] bigSingular = {
- {new Fraction(1),new Fraction(2),new Fraction(3),new Fraction(4)},
- {new Fraction(2),new Fraction(5),new Fraction(3),new Fraction(4)},
- {new Fraction(7),new Fraction(3),new Fraction(256),new Fraction(1930)},
- {new Fraction(3),new Fraction(7),new Fraction(6),new Fraction(8)}
+ protected Dfp[][] singular = { {Dfp25.of(2),Dfp25.of(3)}, {Dfp25.of(2),Dfp25.of(3)} };
+ protected Dfp[][] bigSingular = {
+ {Dfp25.of(1),Dfp25.of(2),Dfp25.of(3),Dfp25.of(4)},
+ {Dfp25.of(2),Dfp25.of(5),Dfp25.of(3),Dfp25.of(4)},
+ {Dfp25.of(7),Dfp25.of(3),Dfp25.of(256),Dfp25.of(1930)},
+ {Dfp25.of(3),Dfp25.of(7),Dfp25.of(6),Dfp25.of(8)}
}; // 4th row = 1st + 2nd
- protected Fraction[][] detData = {
- {new Fraction(1),new Fraction(2),new Fraction(3)},
- {new Fraction(4),new Fraction(5),new Fraction(6)},
- {new Fraction(7),new Fraction(8),new Fraction(10)}
+ protected Dfp[][] detData = {
+ {Dfp25.of(1),Dfp25.of(2),Dfp25.of(3)},
+ {Dfp25.of(4),Dfp25.of(5),Dfp25.of(6)},
+ {Dfp25.of(7),Dfp25.of(8),Dfp25.of(10)}
};
- protected Fraction[][] detData2 = { {new Fraction(1), new Fraction(3)}, {new Fraction(2), new Fraction(4)}};
+ protected Dfp[][] detData2 = { {Dfp25.of(1), Dfp25.of(3)}, {Dfp25.of(2), Dfp25.of(4)}};
// vectors
- protected Fraction[] testVector = {new Fraction(1),new Fraction(2),new Fraction(3)};
- protected Fraction[] testVector2 = {new Fraction(1),new Fraction(2),new Fraction(3),new Fraction(4)};
+ protected Dfp[] testVector = {Dfp25.of(1),Dfp25.of(2),Dfp25.of(3)};
+ protected Dfp[] testVector2 = {Dfp25.of(1),Dfp25.of(2),Dfp25.of(3),Dfp25.of(4)};
// submatrix accessor tests
- protected Fraction[][] subTestData = {
- {new Fraction(1), new Fraction(2), new Fraction(3), new Fraction(4)},
- {new Fraction(3, 2), new Fraction(5, 2), new Fraction(7, 2), new Fraction(9, 2)},
- {new Fraction(2), new Fraction(4), new Fraction(6), new Fraction(8)},
- {new Fraction(4), new Fraction(5), new Fraction(6), new Fraction(7)}
+ protected Dfp[][] subTestData = {
+ {Dfp25.of(1), Dfp25.of(2), Dfp25.of(3), Dfp25.of(4)},
+ {Dfp25.of(3, 2), Dfp25.of(5, 2), Dfp25.of(7, 2), Dfp25.of(9, 2)},
+ {Dfp25.of(2), Dfp25.of(4), Dfp25.of(6), Dfp25.of(8)},
+ {Dfp25.of(4), Dfp25.of(5), Dfp25.of(6), Dfp25.of(7)}
};
// array selections
- protected Fraction[][] subRows02Cols13 = { {new Fraction(2), new Fraction(4)}, {new Fraction(4), new Fraction(8)}};
- protected Fraction[][] subRows03Cols12 = { {new Fraction(2), new Fraction(3)}, {new Fraction(5), new Fraction(6)}};
- protected Fraction[][] subRows03Cols123 = {
- {new Fraction(2), new Fraction(3), new Fraction(4)},
- {new Fraction(5), new Fraction(6), new Fraction(7)}
+ protected Dfp[][] subRows02Cols13 = { {Dfp25.of(2), Dfp25.of(4)}, {Dfp25.of(4), Dfp25.of(8)}};
+ protected Dfp[][] subRows03Cols12 = { {Dfp25.of(2), Dfp25.of(3)}, {Dfp25.of(5), Dfp25.of(6)}};
+ protected Dfp[][] subRows03Cols123 = {
+ {Dfp25.of(2), Dfp25.of(3), Dfp25.of(4)},
+ {Dfp25.of(5), Dfp25.of(6), Dfp25.of(7)}
};
// effective permutations
- protected Fraction[][] subRows20Cols123 = {
- {new Fraction(4), new Fraction(6), new Fraction(8)},
- {new Fraction(2), new Fraction(3), new Fraction(4)}
+ protected Dfp[][] subRows20Cols123 = {
+ {Dfp25.of(4), Dfp25.of(6), Dfp25.of(8)},
+ {Dfp25.of(2), Dfp25.of(3), Dfp25.of(4)}
};
- protected Fraction[][] subRows31Cols31 = {{new Fraction(7), new Fraction(5)}, {new Fraction(9, 2), new Fraction(5, 2)}};
+ protected Dfp[][] subRows31Cols31 = {{Dfp25.of(7), Dfp25.of(5)}, {Dfp25.of(9, 2), Dfp25.of(5, 2)}};
// contiguous ranges
- protected Fraction[][] subRows01Cols23 = {{new Fraction(3),new Fraction(4)} , {new Fraction(7, 2), new Fraction(9, 2)}};
- protected Fraction[][] subRows23Cols00 = {{new Fraction(2)} , {new Fraction(4)}};
- protected Fraction[][] subRows00Cols33 = {{new Fraction(4)}};
+ protected Dfp[][] subRows01Cols23 = {{Dfp25.of(3),Dfp25.of(4)} , {Dfp25.of(7, 2), Dfp25.of(9, 2)}};
+ protected Dfp[][] subRows23Cols00 = {{Dfp25.of(2)} , {Dfp25.of(4)}};
+ protected Dfp[][] subRows00Cols33 = {{Dfp25.of(4)}};
// row matrices
- protected Fraction[][] subRow0 = {{new Fraction(1),new Fraction(2),new Fraction(3),new Fraction(4)}};
- protected Fraction[][] subRow3 = {{new Fraction(4),new Fraction(5),new Fraction(6),new Fraction(7)}};
+ protected Dfp[][] subRow0 = {{Dfp25.of(1),Dfp25.of(2),Dfp25.of(3),Dfp25.of(4)}};
+ protected Dfp[][] subRow3 = {{Dfp25.of(4),Dfp25.of(5),Dfp25.of(6),Dfp25.of(7)}};
// column matrices
- protected Fraction[][] subColumn1 = {{new Fraction(2)}, {new Fraction(5, 2)}, {new Fraction(4)}, {new Fraction(5)}};
- protected Fraction[][] subColumn3 = {{new Fraction(4)}, {new Fraction(9, 2)}, {new Fraction(8)}, {new Fraction(7)}};
+ protected Dfp[][] subColumn1 = {{Dfp25.of(2)}, {Dfp25.of(5, 2)}, {Dfp25.of(4)}, {Dfp25.of(5)}};
+ protected Dfp[][] subColumn3 = {{Dfp25.of(4)}, {Dfp25.of(9, 2)}, {Dfp25.of(8)}, {Dfp25.of(7)}};
// tolerances
protected double entryTolerance = 10E-16;
@@ -167,8 +169,8 @@ public final class BlockFieldMatrixTest {
/** test dimensions */
@Test
public void testDimensions() {
- BlockFieldMatrix<Fraction> m = new BlockFieldMatrix<>(testData);
- BlockFieldMatrix<Fraction> m2 = new BlockFieldMatrix<>(testData2);
+ BlockFieldMatrix<Dfp> m = new BlockFieldMatrix<>(testData);
+ BlockFieldMatrix<Dfp> m2 = new BlockFieldMatrix<>(testData2);
Assert.assertEquals("testData row dimension",3,m.getRowDimension());
Assert.assertEquals("testData column dimension",3,m.getColumnDimension());
Assert.assertTrue("testData is square",m.isSquare());
@@ -181,21 +183,21 @@ public final class BlockFieldMatrixTest {
@Test
public void testCopyFunctions() {
Random r = new Random(66636328996002l);
- BlockFieldMatrix<Fraction> m1 = createRandomMatrix(r, 47, 83);
- BlockFieldMatrix<Fraction> m2 = new BlockFieldMatrix<>(m1.getData());
+ BlockFieldMatrix<Dfp> m1 = createRandomMatrix(r, 47, 83);
+ BlockFieldMatrix<Dfp> m2 = new BlockFieldMatrix<>(m1.getData());
Assert.assertEquals(m1, m2);
- BlockFieldMatrix<Fraction> m3 = new BlockFieldMatrix<>(testData);
- BlockFieldMatrix<Fraction> m4 = new BlockFieldMatrix<>(m3.getData());
+ BlockFieldMatrix<Dfp> m3 = new BlockFieldMatrix<>(testData);
+ BlockFieldMatrix<Dfp> m4 = new BlockFieldMatrix<>(m3.getData());
Assert.assertEquals(m3, m4);
}
/** test add */
@Test
public void testAdd() {
- BlockFieldMatrix<Fraction> m = new BlockFieldMatrix<>(testData);
- BlockFieldMatrix<Fraction> mInv = new BlockFieldMatrix<>(testDataInv);
- FieldMatrix<Fraction> mPlusMInv = m.add(mInv);
- Fraction[][] sumEntries = mPlusMInv.getData();
+ BlockFieldMatrix<Dfp> m = new BlockFieldMatrix<>(testData);
+ BlockFieldMatrix<Dfp> mInv = new BlockFieldMatrix<>(testDataInv);
+ FieldMatrix<Dfp> mPlusMInv = m.add(mInv);
+ Dfp[][] sumEntries = mPlusMInv.getData();
for (int row = 0; row < m.getRowDimension(); row++) {
for (int col = 0; col < m.getColumnDimension(); col++) {
Assert.assertEquals(testDataPlusInv[row][col],sumEntries[row][col]);
@@ -206,8 +208,8 @@ public final class BlockFieldMatrixTest {
/** test add failure */
@Test
public void testAddFail() {
- BlockFieldMatrix<Fraction> m = new BlockFieldMatrix<>(testData);
- BlockFieldMatrix<Fraction> m2 = new BlockFieldMatrix<>(testData2);
+ BlockFieldMatrix<Dfp> m = new BlockFieldMatrix<>(testData);
+ BlockFieldMatrix<Dfp> m2 = new BlockFieldMatrix<>(testData2);
try {
m.add(m2);
Assert.fail("MathIllegalArgumentException expected");
@@ -219,9 +221,9 @@ public final class BlockFieldMatrixTest {
/** test m-n = m + -n */
@Test
public void testPlusMinus() {
- BlockFieldMatrix<Fraction> m = new BlockFieldMatrix<>(testData);
- BlockFieldMatrix<Fraction> m2 = new BlockFieldMatrix<>(testDataInv);
- TestUtils.assertEquals(m.subtract(m2), m2.scalarMultiply(new Fraction(-1)).add(m));
+ BlockFieldMatrix<Dfp> m = new BlockFieldMatrix<>(testData);
+ BlockFieldMatrix<Dfp> m2 = new BlockFieldMatrix<>(testDataInv);
+ TestUtils.assertEquals(m.subtract(m2), m2.scalarMultiply(Dfp25.of(-1)).add(m));
try {
m.subtract(new BlockFieldMatrix<>(testData2));
Assert.fail("Expecting illegalArgumentException");
@@ -233,10 +235,10 @@ public final class BlockFieldMatrixTest {
/** test multiply */
@Test
public void testMultiply() {
- BlockFieldMatrix<Fraction> m = new BlockFieldMatrix<>(testData);
- BlockFieldMatrix<Fraction> mInv = new BlockFieldMatrix<>(testDataInv);
- BlockFieldMatrix<Fraction> identity = new BlockFieldMatrix<>(id);
- BlockFieldMatrix<Fraction> m2 = new BlockFieldMatrix<>(testData2);
+ BlockFieldMatrix<Dfp> m = new BlockFieldMatrix<>(testData);
+ BlockFieldMatrix<Dfp> mInv = new BlockFieldMatrix<>(testDataInv);
+ BlockFieldMatrix<Dfp> identity = new BlockFieldMatrix<>(id);
+ BlockFieldMatrix<Dfp> m2 = new BlockFieldMatrix<>(testData2);
TestUtils.assertEquals(m.multiply(mInv), identity);
TestUtils.assertEquals(mInv.multiply(m), identity);
TestUtils.assertEquals(m.multiply(identity), m);
@@ -252,15 +254,15 @@ public final class BlockFieldMatrixTest {
@Test
public void testSeveralBlocks() {
- FieldMatrix<Fraction> m =
- new BlockFieldMatrix<>(FractionField.getInstance(), 37, 41);
+ FieldMatrix<Dfp> m =
+ new BlockFieldMatrix<>(Dfp25.getField(), 37, 41);
for (int i = 0; i < m.getRowDimension(); ++i) {
for (int j = 0; j < m.getColumnDimension(); ++j) {
- m.setEntry(i, j, new Fraction(i * 11 + j, 11));
+ m.setEntry(i, j, Dfp25.of(i * 11 + j, 11));
}
}
- FieldMatrix<Fraction> mT = m.transpose();
+ FieldMatrix<Dfp> mT = m.transpose();
Assert.assertEquals(m.getRowDimension(), mT.getColumnDimension());
Assert.assertEquals(m.getColumnDimension(), mT.getRowDimension());
for (int i = 0; i < mT.getRowDimension(); ++i) {
@@ -269,99 +271,103 @@ public final class BlockFieldMatrixTest {
}
}
- FieldMatrix<Fraction> mPm = m.add(m);
+ FieldMatrix<Dfp> mPm = m.add(m);
for (int i = 0; i < mPm.getRowDimension(); ++i) {
for (int j = 0; j < mPm.getColumnDimension(); ++j) {
- Assert.assertEquals(m.getEntry(i, j).multiply(new Fraction(2)), mPm.getEntry(i, j));
+ Assert.assertEquals(m.getEntry(i, j).multiply(Dfp25.of(2)), mPm.getEntry(i, j));
}
}
- FieldMatrix<Fraction> mPmMm = mPm.subtract(m);
+ FieldMatrix<Dfp> mPmMm = mPm.subtract(m);
for (int i = 0; i < mPmMm.getRowDimension(); ++i) {
for (int j = 0; j < mPmMm.getColumnDimension(); ++j) {
- Assert.assertEquals(m.getEntry(i, j), mPmMm.getEntry(i, j));
+ Assert.assertEquals(m.getEntry(i, j).toDouble(),
+ mPmMm.getEntry(i, j).toDouble(),
+ 0d);
}
}
- FieldMatrix<Fraction> mTm = mT.multiply(m);
+ FieldMatrix<Dfp> mTm = mT.multiply(m);
for (int i = 0; i < mTm.getRowDimension(); ++i) {
for (int j = 0; j < mTm.getColumnDimension(); ++j) {
- Fraction sum = Fraction.ZERO;
+ Dfp sum = Dfp25.ZERO;
for (int k = 0; k < mT.getColumnDimension(); ++k) {
- sum = sum.add(new Fraction(k * 11 + i, 11).multiply(new Fraction(k * 11 + j, 11)));
+ sum = sum.add(Dfp25.of(k * 11 + i, 11).multiply(Dfp25.of(k * 11 + j, 11)));
}
Assert.assertEquals(sum, mTm.getEntry(i, j));
}
}
- FieldMatrix<Fraction> mmT = m.multiply(mT);
+ FieldMatrix<Dfp> mmT = m.multiply(mT);
for (int i = 0; i < mmT.getRowDimension(); ++i) {
for (int j = 0; j < mmT.getColumnDimension(); ++j) {
- Fraction sum = Fraction.ZERO;
+ Dfp sum = Dfp25.ZERO;
for (int k = 0; k < m.getColumnDimension(); ++k) {
- sum = sum.add(new Fraction(i * 11 + k, 11).multiply(new Fraction(j * 11 + k, 11)));
+ sum = sum.add(Dfp25.of(i * 11 + k, 11).multiply(Dfp25.of(j * 11 + k, 11)));
}
- Assert.assertEquals(sum, mmT.getEntry(i, j));
+ Assert.assertEquals(sum.toDouble(),
+ mmT.getEntry(i, j).toDouble(),
+ 0d);
}
}
- FieldMatrix<Fraction> sub1 = m.getSubMatrix(2, 9, 5, 20);
+ FieldMatrix<Dfp> sub1 = m.getSubMatrix(2, 9, 5, 20);
for (int i = 0; i < sub1.getRowDimension(); ++i) {
for (int j = 0; j < sub1.getColumnDimension(); ++j) {
- Assert.assertEquals(new Fraction((i + 2) * 11 + (j + 5), 11), sub1.getEntry(i, j));
+ Assert.assertEquals(Dfp25.of((i + 2) * 11 + (j + 5), 11), sub1.getEntry(i, j));
}
}
- FieldMatrix<Fraction> sub2 = m.getSubMatrix(10, 12, 3, 40);
+ FieldMatrix<Dfp> sub2 = m.getSubMatrix(10, 12, 3, 40);
for (int i = 0; i < sub2.getRowDimension(); ++i) {
for (int j = 0; j < sub2.getColumnDimension(); ++j) {
- Assert.assertEquals(new Fraction((i + 10) * 11 + (j + 3), 11), sub2.getEntry(i, j));
+ Assert.assertEquals(Dfp25.of((i + 10) * 11 + (j + 3), 11), sub2.getEntry(i, j));
}
}
- FieldMatrix<Fraction> sub3 = m.getSubMatrix(30, 34, 0, 5);
+ FieldMatrix<Dfp> sub3 = m.getSubMatrix(30, 34, 0, 5);
for (int i = 0; i < sub3.getRowDimension(); ++i) {
for (int j = 0; j < sub3.getColumnDimension(); ++j) {
- Assert.assertEquals(new Fraction((i + 30) * 11 + (j + 0), 11), sub3.getEntry(i, j));
+ Assert.assertEquals(Dfp25.of((i + 30) * 11 + (j + 0), 11), sub3.getEntry(i, j));
}
}
- FieldMatrix<Fraction> sub4 = m.getSubMatrix(30, 32, 32, 35);
+ FieldMatrix<Dfp> sub4 = m.getSubMatrix(30, 32, 32, 35);
for (int i = 0; i < sub4.getRowDimension(); ++i) {
for (int j = 0; j < sub4.getColumnDimension(); ++j) {
- Assert.assertEquals(new Fraction((i + 30) * 11 + (j + 32), 11), sub4.getEntry(i, j));
+ Assert.assertEquals(Dfp25.of((i + 30) * 11 + (j + 32), 11), sub4.getEntry(i, j));
}
}
}
- //Additional Test for BlockFieldMatrix<Fraction>Test.testMultiply
+ //Additional Test for BlockFieldMatrix<Dfp>Test.testMultiply
- private Fraction[][] d3 = new Fraction[][] {
- {new Fraction(1),new Fraction(2),new Fraction(3),new Fraction(4)},
- {new Fraction(5),new Fraction(6),new Fraction(7),new Fraction(8)}
+ private Dfp[][] d3 = new Dfp[][] {
+ {Dfp25.of(1),Dfp25.of(2),Dfp25.of(3),Dfp25.of(4)},
+ {Dfp25.of(5),Dfp25.of(6),Dfp25.of(7),Dfp25.of(8)}
};
- private Fraction[][] d4 = new Fraction[][] {
- {new Fraction(1)},
- {new Fraction(2)},
- {new Fraction(3)},
- {new Fraction(4)}
+ private Dfp[][] d4 = new Dfp[][] {
+ {Dfp25.of(1)},
+ {Dfp25.of(2)},
+ {Dfp25.of(3)},
+ {Dfp25.of(4)}
};
- private Fraction[][] d5 = new Fraction[][] {{new Fraction(30)},{new Fraction(70)}};
+ private Dfp[][] d5 = new Dfp[][] {{Dfp25.of(30)},{Dfp25.of(70)}};
@Test
public void testMultiply2() {
- FieldMatrix<Fraction> m3 = new BlockFieldMatrix<>(d3);
- FieldMatrix<Fraction> m4 = new BlockFieldMatrix<>(d4);
- FieldMatrix<Fraction> m5 = new BlockFieldMatrix<>(d5);
+ FieldMatrix<Dfp> m3 = new BlockFieldMatrix<>(d3);
+ FieldMatrix<Dfp> m4 = new BlockFieldMatrix<>(d4);
+ FieldMatrix<Dfp> m5 = new BlockFieldMatrix<>(d5);
TestUtils.assertEquals(m3.multiply(m4), m5);
}
/** test trace */
@Test
public void testTrace() {
- FieldMatrix<Fraction> m = new BlockFieldMatrix<>(id);
- Assert.assertEquals(new Fraction(3),m.getTrace());
+ FieldMatrix<Dfp> m = new BlockFieldMatrix<>(id);
+ Assert.assertEquals(Dfp25.of(3),m.getTrace());
m = new BlockFieldMatrix<>(testData2);
try {
m.getTrace();
@@ -374,15 +380,15 @@ public final class BlockFieldMatrixTest {
/** test scalarAdd */
@Test
public void testScalarAdd() {
- FieldMatrix<Fraction> m = new BlockFieldMatrix<>(testData);
+ FieldMatrix<Dfp> m = new BlockFieldMatrix<>(testData);
TestUtils.assertEquals(new BlockFieldMatrix<>(testDataPlus2),
- m.scalarAdd(new Fraction(2)));
+ m.scalarAdd(Dfp25.of(2)));
}
/** test operate */
@Test
public void testOperate() {
- FieldMatrix<Fraction> m = new BlockFieldMatrix<>(id);
+ FieldMatrix<Dfp> m = new BlockFieldMatrix<>(id);
TestUtils.assertEquals(testVector, m.operate(testVector));
TestUtils.assertEquals(testVector, m.operate(new ArrayFieldVector<>(testVector)).toArray());
m = new BlockFieldMatrix<>(bigSingular);
@@ -400,9 +406,9 @@ public final class BlockFieldMatrixTest {
int q = (11 * BlockFieldMatrix.BLOCK_SIZE) / 10;
int r = BlockFieldMatrix.BLOCK_SIZE / 2;
Random random = new Random(111007463902334l);
- FieldMatrix<Fraction> m1 = createRandomMatrix(random, p, q);
- FieldMatrix<Fraction> m2 = createRandomMatrix(random, q, r);
- FieldMatrix<Fraction> m1m2 = m1.multiply(m2);
+ FieldMatrix<Dfp> m1 = createRandomMatrix(random, p, q);
+ FieldMatrix<Dfp> m2 = createRandomMatrix(random, q, r);
+ FieldMatrix<Dfp> m1m2 = m1.multiply(m2);
for (int i = 0; i < r; ++i) {
TestUtils.assertEquals(m1m2.getColumn(i), m1.operate(m2.getColumn(i)));
}
@@ -414,9 +420,9 @@ public final class BlockFieldMatrixTest {
int q = (11 * BlockFieldMatrix.BLOCK_SIZE) / 10;
int r = BlockFieldMatrix.BLOCK_SIZE / 2;
Random random = new Random(111007463902334l);
- FieldMatrix<Fraction> m1 = createRandomMatrix(random, p, q);
- FieldMatrix<Fraction> m2 = createRandomMatrix(random, q, r);
- FieldMatrix<Fraction> m1m2 = m1.multiply(m2);
+ FieldMatrix<Dfp> m1 = createRandomMatrix(random, p, q);
+ FieldMatrix<Dfp> m2 = createRandomMatrix(random, q, r);
+ FieldMatrix<Dfp> m1m2 = m1.multiply(m2);
for (int i = 0; i < p; ++i) {
TestUtils.assertEquals(m1m2.getRow(i), m2.preMultiply(m1.getRow(i)));
}
@@ -425,34 +431,34 @@ public final class BlockFieldMatrixTest {
/** test issue MATH-209 */
@Test
public void testMath209() {
- FieldMatrix<Fraction> a = new BlockFieldMatrix<>(new Fraction[][] {
- { new Fraction(1), new Fraction(2) },
- { new Fraction(3), new Fraction(4) },
- { new Fraction(5), new Fraction(6) }
+ FieldMatrix<Dfp> a = new BlockFieldMatrix<>(new Dfp[][] {
+ { Dfp25.of(1), Dfp25.of(2) },
+ { Dfp25.of(3), Dfp25.of(4) },
+ { Dfp25.of(5), Dfp25.of(6) }
});
- Fraction[] b = a.operate(new Fraction[] { new Fraction(1), new Fraction(1) });
+ Dfp[] b = a.operate(new Dfp[] { Dfp25.of(1), Dfp25.of(1) });
Assert.assertEquals(a.getRowDimension(), b.length);
- Assert.assertEquals( new Fraction(3), b[0]);
- Assert.assertEquals( new Fraction(7), b[1]);
- Assert.assertEquals(new Fraction(11), b[2]);
+ Assert.assertEquals( Dfp25.of(3), b[0]);
+ Assert.assertEquals( Dfp25.of(7), b[1]);
+ Assert.assertEquals(Dfp25.of(11), b[2]);
}
/** test transpose */
@Test
public void testTranspose() {
- FieldMatrix<Fraction> m = new BlockFieldMatrix<>(testData);
- FieldMatrix<Fraction> mIT = new FieldLUDecomposition<>(m).getSolver().getInverse().transpose();
- FieldMatrix<Fraction> mTI = new FieldLUDecomposition<>(m.transpose()).getSolver().getInverse();
+ FieldMatrix<Dfp> m = new BlockFieldMatrix<>(testData);
+ FieldMatrix<Dfp> mIT = new FieldLUDecomposition<>(m).getSolver().getInverse().transpose();
+ FieldMatrix<Dfp> mTI = new FieldLUDecomposition<>(m.transpose()).getSolver().getInverse();
TestUtils.assertEquals(mIT, mTI);
m = new BlockFieldMatrix<>(testData2);
- FieldMatrix<Fraction> mt = new BlockFieldMatrix<>(testData2T);
+ FieldMatrix<Dfp> mt = new BlockFieldMatrix<>(testData2T);
TestUtils.assertEquals(mt, m.transpose());
}
/** test preMultiply by vector */
@Test
public void testPremultiplyVector() {
- FieldMatrix<Fraction> m = new BlockFieldMatrix<>(testData);
+ FieldMatrix<Dfp> m = new BlockFieldMatrix<>(testData);
TestUtils.assertEquals(m.preMultiply(testVector), preMultTest);
TestUtils.assertEquals(m.preMultiply(new ArrayFieldVector<>(testVector).toArray()),
preMultTest);
@@ -467,14 +473,14 @@ public final class BlockFieldMatrixTest {
@Test
public void testPremultiply() {
- FieldMatrix<Fraction> m3 = new BlockFieldMatrix<>(d3);
- FieldMatrix<Fraction> m4 = new BlockFieldMatrix<>(d4);
- FieldMatrix<Fraction> m5 = new BlockFieldMatrix<>(d5);
+ FieldMatrix<Dfp> m3 = new BlockFieldMatrix<>(d3);
+ FieldMatrix<Dfp> m4 = new BlockFieldMatrix<>(d4);
+ FieldMatrix<Dfp> m5 = new BlockFieldMatrix<>(d5);
TestUtils.assertEquals(m4.preMultiply(m3), m5);
- BlockFieldMatrix<Fraction> m = new BlockFieldMatrix<>(testData);
- BlockFieldMatrix<Fraction> mInv = new BlockFieldMatrix<>(testDataInv);
- BlockFieldMatrix<Fraction> identity = new BlockFieldMatrix<>(id);
+ BlockFieldMatrix<Dfp> m = new BlockFieldMatrix<>(testData);
+ BlockFieldMatrix<Dfp> mInv = new BlockFieldMatrix<>(testDataInv);
+ BlockFieldMatrix<Dfp> identity = new BlockFieldMatrix<>(id);
TestUtils.assertEquals(m.preMultiply(mInv), identity);
TestUtils.assertEquals(mInv.preMultiply(m), identity);
TestUtils.assertEquals(m.preMultiply(identity), m);
@@ -489,7 +495,7 @@ public final class BlockFieldMatrixTest {
@Test
public void testGetVectors() {
- FieldMatrix<Fraction> m = new BlockFieldMatrix<>(testData);
+ FieldMatrix<Dfp> m = new BlockFieldMatrix<>(testData);
TestUtils.assertEquals(m.getRow(0), testDataRow1);
TestUtils.assertEquals(m.getColumn(2), testDataCol3);
try {
@@ -508,8 +514,8 @@ public final class BlockFieldMatrixTest {
@Test
public void testGetEntry() {
- FieldMatrix<Fraction> m = new BlockFieldMatrix<>(testData);
- Assert.assertEquals(m.getEntry(0,1),new Fraction(2));
+ FieldMatrix<Dfp> m = new BlockFieldMatrix<>(testData);
+ Assert.assertEquals(m.getEntry(0,1),Dfp25.of(2));
try {
m.getEntry(10, 4);
Assert.fail ("Expecting OutOfRangeException");
@@ -522,60 +528,63 @@ public final class BlockFieldMatrixTest {
@Test
public void testExamples() {
// Create a real matrix with two rows and three columns
- Fraction[][] matrixData = {
- {new Fraction(1),new Fraction(2),new Fraction(3)},
- {new Fraction(2),new Fraction(5),new Fraction(3)}
+ Dfp[][] matrixData = {
+ {Dfp25.of(1),Dfp25.of(2),Dfp25.of(3)},
+ {Dfp25.of(2),Dfp25.of(5),Dfp25.of(3)}
};
- FieldMatrix<Fraction> m = new BlockFieldMatrix<>(matrixData);
+ FieldMatrix<Dfp> m = new BlockFieldMatrix<>(matrixData);
// One more with three rows, two columns
- Fraction[][] matrixData2 = {
- {new Fraction(1),new Fraction(2)},
- {new Fraction(2),new Fraction(5)},
- {new Fraction(1), new Fraction(7)}
+ Dfp[][] matrixData2 = {
+ {Dfp25.of(1),Dfp25.of(2)},
+ {Dfp25.of(2),Dfp25.of(5)},
+ {Dfp25.of(1), Dfp25.of(7)}
};
- FieldMatrix<Fraction> n = new BlockFieldMatrix<>(matrixData2);
+ FieldMatrix<Dfp> n = new BlockFieldMatrix<>(matrixData2);
// Now multiply m by n
- FieldMatrix<Fraction> p = m.multiply(n);
+ FieldMatrix<Dfp> p = m.multiply(n);
Assert.assertEquals(2, p.getRowDimension());
Assert.assertEquals(2, p.getColumnDimension());
// Invert p
- FieldMatrix<Fraction> pInverse = new FieldLUDecomposition<>(p).getSolver().getInverse();
+ FieldMatrix<Dfp> pInverse = new FieldLUDecomposition<>(p).getSolver().getInverse();
Assert.assertEquals(2, pInverse.getRowDimension());
Assert.assertEquals(2, pInverse.getColumnDimension());
// Solve example
- Fraction[][] coefficientsData = {
- {new Fraction(2), new Fraction(3), new Fraction(-2)},
- {new Fraction(-1), new Fraction(7), new Fraction(6)},
- {new Fraction(4), new Fraction(-3), new Fraction(-5)}
+ Dfp[][] coefficientsData = {
+ {Dfp25.of(2), Dfp25.of(3), Dfp25.of(-2)},
+ {Dfp25.of(-1), Dfp25.of(7), Dfp25.of(6)},
+ {Dfp25.of(4), Dfp25.of(-3), Dfp25.of(-5)}
};
- FieldMatrix<Fraction> coefficients = new BlockFieldMatrix<>(coefficientsData);
- Fraction[] constants = {
- new Fraction(1), new Fraction(-2), new Fraction(1)
+ FieldMatrix<Dfp> coefficients = new BlockFieldMatrix<>(coefficientsData);
+ Dfp[] constants = {
+ Dfp25.of(1), Dfp25.of(-2), Dfp25.of(1)
};
- Fraction[] solution;
+ Dfp[] solution;
solution = new FieldLUDecomposition<>(coefficients)
.getSolver()
.solve(new ArrayFieldVector<>(constants, false)).toArray();
- Assert.assertEquals(new Fraction(2).multiply(solution[0]).
- add(new Fraction(3).multiply(solution[1])).
- subtract(new Fraction(2).multiply(solution[2])),
- constants[0]);
- Assert.assertEquals(new Fraction(-1).multiply(solution[0]).
- add(new Fraction(7).multiply(solution[1])).
- add(new Fraction(6).multiply(solution[2])),
- constants[1]);
- Assert.assertEquals(new Fraction(4).multiply(solution[0]).
- subtract(new Fraction(3).multiply(solution[1])).
- subtract(new Fraction(5).multiply(solution[2])),
- constants[2]);
+ Assert.assertEquals(Dfp25.of(2).multiply(solution[0]).
+ add(Dfp25.of(3).multiply(solution[1])).
+ subtract(Dfp25.of(2).multiply(solution[2])).toDouble(),
+ constants[0].toDouble(),
+ 0d);
+ Assert.assertEquals(Dfp25.of(-1).multiply(solution[0]).
+ add(Dfp25.of(7).multiply(solution[1])).
+ add(Dfp25.of(6).multiply(solution[2])).toDouble(),
+ constants[1].toDouble(),
+ 0d);
+ Assert.assertEquals(Dfp25.of(4).multiply(solution[0]).
+ subtract(Dfp25.of(3).multiply(solution[1])).
+ subtract(Dfp25.of(5).multiply(solution[2])).toDouble(),
+ constants[2].toDouble(),
+ 0d);
}
// test submatrix accessors
@Test
public void testGetSubMatrix() {
- FieldMatrix<Fraction> m = new BlockFieldMatrix<>(subTestData);
+ FieldMatrix<Dfp> m = new BlockFieldMatrix<>(subTestData);
checkGetSubMatrix(m, subRows23Cols00, 2 , 3 , 0, 0);
checkGetSubMatrix(m, subRows00Cols33, 0 , 0 , 3, 3);
checkGetSubMatrix(m, subRows01Cols23, 0 , 1 , 2, 3);
@@ -593,10 +602,10 @@ public final class BlockFieldMatrixTest {
checkGetSubMatrix(m, null, new int[] { 0 }, new int[] { 4 });
}
- private void checkGetSubMatrix(FieldMatrix<Fraction> m, Fraction[][] reference,
+ private void checkGetSubMatrix(FieldMatrix<Dfp> m, Dfp[][] reference,
int startRow, int endRow, int startColumn, int endColumn) {
try {
- FieldMatrix<Fraction> sub = m.getSubMatrix(startRow, endRow, startColumn, endColumn);
+ FieldMatrix<Dfp> sub = m.getSubMatrix(startRow, endRow, startColumn, endColumn);
if (reference != null) {
Assert.assertEquals(new BlockFieldMatrix<>(reference), sub);
} else {
@@ -622,10 +631,10 @@ public final class BlockFieldMatrixTest {
}
}
- private void checkGetSubMatrix(FieldMatrix<Fraction> m, Fraction[][] reference,
+ private void checkGetSubMatrix(FieldMatrix<Dfp> m, Dfp[][] reference,
int[] selectedRows, int[] selectedColumns) {
try {
- FieldMatrix<Fraction> sub = m.getSubMatrix(selectedRows, selectedColumns);
+ FieldMatrix<Dfp> sub = m.getSubMatrix(selectedRows, selectedColumns);
if (reference != null) {
Assert.assertEquals(new BlockFieldMatrix<>(reference), sub);
} else {
@@ -653,18 +662,18 @@ public final class BlockFieldMatrixTest {
@Test
public void testGetSetMatrixLarge() {
int n = 3 * BlockFieldMatrix.BLOCK_SIZE;
- FieldMatrix<Fraction> m =
- new BlockFieldMatrix<>(FractionField.getInstance(), n, n);
- FieldMatrix<Fraction> sub =
- new BlockFieldMatrix<>(FractionField.getInstance(), n - 4, n - 4).scalarAdd(new Fraction(1));
+ FieldMatrix<Dfp> m =
+ new BlockFieldMatrix<>(Dfp25.getField(), n, n);
+ FieldMatrix<Dfp> sub =
+ new BlockFieldMatrix<>(Dfp25.getField(), n - 4, n - 4).scalarAdd(Dfp25.of(1));
m.setSubMatrix(sub.getData(), 2, 2);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if ((i < 2) || (i > n - 3) || (j < 2) || (j > n - 3)) {
- Assert.assertEquals(new Fraction(0), m.getEntry(i, j));
+ Assert.assertEquals(Dfp25.of(0), m.getEntry(i, j));
} else {
- Assert.assertEquals(new Fraction(1), m.getEntry(i, j));
+ Assert.assertEquals(Dfp25.of(1), m.getEntry(i, j));
}
}
}
@@ -673,7 +682,7 @@ public final class BlockFieldMatrixTest {
@Test
public void testCopySubMatrix() {
- FieldMatrix<Fraction> m = new BlockFieldMatrix<>(subTestData);
+ FieldMatrix<Dfp> m = new BlockFieldMatrix<>(subTestData);
checkCopy(m, subRows23Cols00, 2 , 3 , 0, 0);
checkCopy(m, subRows00Cols33, 0 , 0 , 3, 3);
checkCopy(m, subRows01Cols23, 0 , 1 , 2, 3);
@@ -692,12 +701,12 @@ public final class BlockFieldMatrixTest {
checkCopy(m, null, new int[] { 0 }, new int[] { 4 });
}
- private void checkCopy(FieldMatrix<Fraction> m, Fraction[][] reference,
+ private void checkCopy(FieldMatrix<Dfp> m, Dfp[][] reference,
int startRow, int endRow, int startColumn, int endColumn) {
try {
- Fraction[][] sub = (reference == null) ?
- new Fraction[1][1] :
- new Fraction[reference.length][reference[0].length];
+ Dfp[][] sub = (reference == null) ?
+ new Dfp[1][1] :
+ new Dfp[reference.length][reference[0].length];
m.copySubMatrix(startRow, endRow, startColumn, endColumn, sub);
if (reference != null) {
Assert.assertEquals(new BlockFieldMatrix<>(reference), new BlockFieldMatrix<>(sub));
@@ -719,12 +728,12 @@ public final class BlockFieldMatrixTest {
}
}
- private void checkCopy(FieldMatrix<Fraction> m, Fraction[][] reference,
+ private void checkCopy(FieldMatrix<Dfp> m, Dfp[][] reference,
int[] selectedRows, int[] selectedColumns) {
try {
- Fraction[][] sub = (reference == null) ?
- new Fraction[1][1] :
- new Fraction[reference.length][reference[0].length];
+ Dfp[][] sub = (reference == null) ?
+ new Dfp[1][1] :
+ new Dfp[reference.length][reference[0].length];
m.copySubMatrix(selectedRows, selectedColumns, sub);
if (reference != null) {
Assert.assertEquals(new BlockFieldMatrix<>(reference), new BlockFieldMatrix<>(sub));
@@ -748,9 +757,9 @@ public final class BlockFieldMatrixTest {
@Test
public void testGetRowMatrix() {
- FieldMatrix<Fraction> m = new BlockFieldMatrix<>(subTestData);
- FieldMatrix<Fraction> mRow0 = new BlockFieldMatrix<>(subRow0);
- FieldMatrix<Fraction> mRow3 = new BlockFieldMatrix<>(subRow3);
+ FieldMatrix<Dfp> m = new BlockFieldMatrix<>(subTestData);
+ FieldMatrix<Dfp> mRow0 = new BlockFieldMatrix<>(subRow0);
+ FieldMatrix<Dfp> mRow3 = new BlockFieldMatrix<>(subRow3);
Assert.assertEquals("Row0", mRow0, m.getRowMatrix(0));
Assert.assertEquals("Row3", mRow3, m.getRowMatrix(3));
try {
@@ -769,8 +778,8 @@ public final class BlockFieldMatrixTest {
@Test
public void testSetRowMatrix() {
- FieldMatrix<Fraction> m = new BlockFieldMatrix<>(subTestData);
- FieldMatrix<Fraction> mRow3 = new BlockFieldMatrix<>(subRow3);
+ FieldMatrix<Dfp> m = new BlockFieldMatrix<>(subTestData);
+ FieldMatrix<Dfp> mRow3 = new BlockFieldMatrix<>(subRow3);
Assert.assertNotSame(mRow3, m.getRowMatrix(0));
m.setRowMatrix(0, mRow3);
Assert.assertEquals(mRow3, m.getRowMatrix(0));
@@ -791,18 +800,18 @@ public final class BlockFieldMatrixTest {
@Test
public void testGetSetRowMatrixLarge() {
int n = 3 * BlockFieldMatrix.BLOCK_SIZE;
- FieldMatrix<Fraction> m =
- new BlockFieldMatrix<>(FractionField.getInstance(), n, n);
- FieldMatrix<Fraction> sub =
- new BlockFieldMatrix<>(FractionField.getInstance(), 1, n).scalarAdd(new Fraction(1));
+ FieldMatrix<Dfp> m =
+ new BlockFieldMatrix<>(Dfp25.getField(), n, n);
+ FieldMatrix<Dfp> sub =
+ new BlockFieldMatrix<>(Dfp25.getField(), 1, n).scalarAdd(Dfp25.of(1));
m.setRowMatrix(2, sub);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (i != 2) {
- Assert.assertEquals(new Fraction(0), m.getEntry(i, j));
+ Assert.assertEquals(Dfp25.of(0), m.getEntry(i, j));
} else {
- Assert.assertEquals(new Fraction(1), m.getEntry(i, j));
+ Assert.assertEquals(Dfp25.of(1), m.getEntry(i, j));
}
}
}
@@ -812,9 +821,9 @@ public final class BlockFieldMatrixTest {
@Test
public void testGetColumnMatrix() {
- FieldMatrix<Fraction> m = new BlockFieldMatrix<>(subTestData);
- FieldMatrix<Fraction> mColumn1 = new BlockFieldMatrix<>(subColumn1);
- FieldMatrix<Fraction> mColumn3 = new BlockFieldMatrix<>(subColumn3);
+ FieldMatrix<Dfp> m = new BlockFieldMatrix<>(subTestData);
+ FieldMatrix<Dfp> mColumn1 = new BlockFieldMatrix<>(subColumn1);
+ FieldMatrix<Dfp> mColumn3 = new BlockFieldMatrix<>(subColumn3);
Assert.assertEquals(mColumn1, m.getColumnMatrix(1));
Assert.assertEquals(mColumn3, m.getColumnMatrix(3));
try {
@@ -833,8 +842,8 @@ public final class BlockFieldMatrixTest {
@Test
public void testSetColumnMatrix() {
- FieldMatrix<Fraction> m = new BlockFieldMatrix<>(subTestData);
- FieldMatrix<Fraction> mColumn3 = new BlockFieldMatrix<>(subColumn3);
+ FieldMatrix<Dfp> m = new BlockFieldMatrix<>(subTestData);
+ FieldMatrix<Dfp> mColumn3 = new BlockFieldMatrix<>(subColumn3);
Assert.assertNotSame(mColumn3, m.getColumnMatrix(1));
m.setColumnMatrix(1, mColumn3);
Assert.assertEquals(mColumn3, m.getColumnMatrix(1));
@@ -855,18 +864,18 @@ public final class BlockFieldMatrixTest {
@Test
public void testGetSetColumnMatrixLarge() {
int n = 3 * BlockFieldMatrix.BLOCK_SIZE;
- FieldMatrix<Fraction> m =
- new BlockFieldMatrix<>(FractionField.getInstance(), n, n);
- FieldMatrix<Fraction> sub =
- new BlockFieldMatrix<>(FractionField.getInstance(), n, 1).scalarAdd(new Fraction(1));
+ FieldMatrix<Dfp> m =
+ new BlockFieldMatrix<>(Dfp25.getField(), n, n);
+ FieldMatrix<Dfp> sub =
+ new BlockFieldMatrix<>(Dfp25.getField(), n, 1).scalarAdd(Dfp25.of(1));
m.setColumnMatrix(2, sub);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (j != 2) {
- Assert.assertEquals(new Fraction(0), m.getEntry(i, j));
+ Assert.assertEquals(Dfp25.of(0), m.getEntry(i, j));
} else {
- Assert.assertEquals(new Fraction(1), m.getEntry(i, j));
+ Assert.assertEquals(Dfp25.of(1), m.getEntry(i, j));
}
}
}
@@ -876,9 +885,9 @@ public final class BlockFieldMatrixTest {
@Test
public void testGetRowVector() {
- FieldMatrix<Fraction> m = new BlockFieldMatrix<>(subTestData);
- FieldVector<Fraction> mRow0 = new ArrayFieldVector<>(subRow0[0]);
- FieldVector<Fraction> mRow3 = new ArrayFieldVector<>(subRow3[0]);
+ FieldMatrix<Dfp> m = new BlockFieldMatrix<>(subTestData);
+ FieldVector<Dfp> mRow0 = new ArrayFieldVector<>(subRow0[0]);
+ FieldVector<Dfp> mRow3 = new ArrayFieldVector<>(subRow3[0]);
Assert.assertEquals(mRow0, m.getRowVector(0));
Assert.assertEquals(mRow3, m.getRowVector(3));
try {
@@ -897,8 +906,8 @@ public final class BlockFieldMatrixTest {
@Test
public void testSetRowVector() {
- FieldMatrix<Fraction> m = new BlockFieldMatrix<>(subTestData);
- FieldVector<Fraction> mRow3 = new ArrayFieldVector<>(subRow3[0]);
+ FieldMatrix<Dfp> m = new BlockFieldMatrix<>(subTestData);
+ FieldVector<Dfp> mRow3 = new ArrayFieldVector<>(subRow3[0]);
Assert.assertNotSame(mRow3, m.getRowMatrix(0));
m.setRowVector(0, mRow3);
Assert.assertEquals(mRow3, m.getRowVector(0));
@@ -909,7 +918,7 @@ public final class BlockFieldMatrixTest {
// expected
}
try {
- m.setRowVector(0, new ArrayFieldVector<>(FractionField.getInstance(), 5));
+ m.setRowVector(0, new ArrayFieldVector<>(Dfp25.getField(), 5));
Assert.fail("Expecting MatrixDimensionMismatchException");
} catch (MatrixDimensionMismatchException ex) {
// expected
@@ -919,16 +928,16 @@ public final class BlockFieldMatrixTest {
@Test
public void testGetSetRowVectorLarge() {
int n = 3 * BlockFieldMatrix.BLOCK_SIZE;
- FieldMatrix<Fraction> m = new BlockFieldMatrix<>(FractionField.getInstance(), n, n);
- FieldVector<Fraction> sub = new ArrayFieldVector<>(n, new Fraction(1));
+ FieldMatrix<Dfp> m = new BlockFieldMatrix<>(Dfp25.getField(), n, n);
+ FieldVector<Dfp> sub = new ArrayFieldVector<>(n, Dfp25.of(1));
m.setRowVector(2, sub);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (i != 2) {
- Assert.assertEquals(new Fraction(0), m.getEntry(i, j));
+ Assert.assertEquals(Dfp25.of(0), m.getEntry(i, j));
} else {
- Assert.assertEquals(new Fraction(1), m.getEntry(i, j));
+ Assert.assertEquals(Dfp25.of(1), m.getEntry(i, j));
}
}
}
@@ -938,9 +947,9 @@ public final class BlockFieldMatrixTest {
@Test
public void testGetColumnVector() {
- FieldMatrix<Fraction> m = new BlockFieldMatrix<>(subTestData);
- FieldVector<Fraction> mColumn1 = columnToVector(subColumn1);
- FieldVector<Fraction> mColumn3 = columnToVector(subColumn3);
+ FieldMatrix<Dfp> m = new BlockFieldMatrix<>(subTestData);
+ FieldVector<Dfp> mColumn1 = columnToVector(subColumn1);
+ FieldVector<Dfp> mColumn3 = columnToVector(subColumn3);
Assert.assertEquals(mColumn1, m.getColumnVector(1));
Assert.assertEquals(mColumn3, m.getColumnVector(3));
try {
@@ -959,8 +968,8 @@ public final class BlockFieldMatrixTest {
@Test
public void testSetColumnVector() {
- FieldMatrix<Fraction> m = new BlockFieldMatrix<>(subTestData);
- FieldVector<Fraction> mColumn3 = columnToVector(subColumn3);
+ FieldMatrix<Dfp> m = new BlockFieldMatrix<>(subTestData);
+ FieldVector<Dfp> mColumn3 = columnToVector(subColumn3);
Assert.assertNotSame(mColumn3, m.getColumnVector(1));
m.setColumnVector(1, mColumn3);
Assert.assertEquals(mColumn3, m.getColumnVector(1));
@@ -971,7 +980,7 @@ public final class BlockFieldMatrixTest {
// expected
}
try {
- m.setColumnVector(0, new ArrayFieldVector<>(FractionField.getInstance(), 5));
+ m.setColumnVector(0, new ArrayFieldVector<>(Dfp25.getField(), 5));
Assert.fail("Expecting MatrixDimensionMismatchException");
} catch (MatrixDimensionMismatchException ex) {
// expected
@@ -981,16 +990,16 @@ public final class BlockFieldMatrixTest {
@Test
public void testGetSetColumnVectorLarge() {
int n = 3 * BlockFieldMatrix.BLOCK_SIZE;
- FieldMatrix<Fraction> m = new BlockFieldMatrix<>(FractionField.getInstance(), n, n);
- FieldVector<Fraction> sub = new ArrayFieldVector<>(n, new Fraction(1));
+ FieldMatrix<Dfp> m = new BlockFieldMatrix<>(Dfp25.getField(), n, n);
+ FieldVector<Dfp> sub = new ArrayFieldVector<>(n, Dfp25.of(1));
m.setColumnVector(2, sub);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (j != 2) {
- Assert.assertEquals(new Fraction(0), m.getEntry(i, j));
+ Assert.assertEquals(Dfp25.of(0), m.getEntry(i, j));
} else {
- Assert.assertEquals(new Fraction(1), m.getEntry(i, j));
+ Assert.assertEquals(Dfp25.of(1), m.getEntry(i, j));
}
}
}
@@ -998,8 +1007,8 @@ public final class BlockFieldMatrixTest {
}
- private FieldVector<Fraction> columnToVector(Fraction[][] column) {
- Fraction[] data = new Fraction[column.length];
+ private FieldVector<Dfp> columnToVector(Dfp[][] column) {
+ Dfp[] data = new Dfp[column.length];
for (int i = 0; i < data.length; ++i) {
data[i] = column[i][0];
}
@@ -1008,7 +1017,7 @@ public final class BlockFieldMatrixTest {
@Test
public void testGetRow() {
- FieldMatrix<Fraction> m = new BlockFieldMatrix<>(subTestData);
+ FieldMatrix<Dfp> m = new BlockFieldMatrix<>(subTestData);
checkArrays(subRow0[0], m.getRow(0));
checkArrays(subRow3[0], m.getRow(3));
try {
@@ -1027,7 +1036,7 @@ public final class BlockFieldMatrixTest {
@Test
public void testSetRow() {
- FieldMatrix<Fraction> m = new BlockFieldMatrix<>(subTestData);
+ FieldMatrix<Dfp> m = new BlockFieldMatrix<>(subTestData);
Assert.assertTrue(subRow3[0][0] != m.getRow(0)[0]);
m.setRow(0, subRow3[0]);
checkArrays(subRow3[0], m.getRow(0));
@@ -1038,7 +1047,7 @@ public final class BlockFieldMatrixTest {
// expected
}
try {
- m.setRow(0, new Fraction[5]);
+ m.setRow(0, new Dfp[5]);
Assert.fail("Expecting MatrixDimensionMismatchException");
} catch (MatrixDimensionMismatchException ex) {
// expected
@@ -1048,17 +1057,17 @@ public final class BlockFieldMatrixTest {
@Test
public void testGetSetRowLarge() {
int n = 3 * BlockFieldMatrix.BLOCK_SIZE;
- FieldMatrix<Fraction> m = new BlockFieldMatrix<>(FractionField.getInstance(), n, n);
- Fraction[] sub = new Fraction[n];
- Arrays.fill(sub, new Fraction(1));
+ FieldMatrix<Dfp> m = new BlockFieldMatrix<>(Dfp25.getField(), n, n);
+ Dfp[] sub = new Dfp[n];
+ Arrays.fill(sub, Dfp25.of(1));
m.setRow(2, sub);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (i != 2) {
- Assert.assertEquals(new Fraction(0), m.getEntry(i, j));
+ Assert.assertEquals(Dfp25.of(0), m.getEntry(i, j));
} else {
- Assert.assertEquals(new Fraction(1), m.getEntry(i, j));
+ Assert.assertEquals(Dfp25.of(1), m.getEntry(i, j));
}
}
}
@@ -1068,9 +1077,9 @@ public final class BlockFieldMatrixTest {
@Test
public void testGetColumn() {
- FieldMatrix<Fraction> m = new BlockFieldMatrix<>(subTestData);
- Fraction[] mColumn1 = columnToArray(subColumn1);
- Fraction[] mColumn3 = columnToArray(subColumn3);
+ FieldMatrix<Dfp> m = new BlockFieldMatrix<>(subTestData);
+ Dfp[] mColumn1 = columnToArray(subColumn1);
+ Dfp[] mColumn3 = columnToArray(subColumn3);
checkArrays(mColumn1, m.getColumn(1));
checkArrays(mColumn3, m.getColumn(3));
try {
@@ -1089,8 +1098,8 @@ public final class BlockFieldMatrixTest {
@Test
public void testSetColumn() {
- FieldMatrix<Fraction> m = new BlockFieldMatrix<>(subTestData);
- Fraction[] mColumn3 = columnToArray(subColumn3);
+ FieldMatrix<Dfp> m = new BlockFieldMatrix<>(subTestData);
+ Dfp[] mColumn3 = columnToArray(subColumn3);
Assert.assertTrue(mColumn3[0] != m.getColumn(1)[0]);
m.setColumn(1, mColumn3);
checkArrays(mColumn3, m.getColumn(1));
@@ -1101,7 +1110,7 @@ public final class BlockFieldMatrixTest {
// expected
}
try {
- m.setColumn(0, new Fraction[5]);
+ m.setColumn(0, new Dfp[5]);
Assert.fail("Expecting MatrixDimensionMismatchException");
} catch (MatrixDimensionMismatchException ex) {
// expected
@@ -1111,17 +1120,17 @@ public final class BlockFieldMatrixTest {
@Test
public void testGetSetColumnLarge() {
int n = 3 * BlockFieldMatrix.BLOCK_SIZE;
- FieldMatrix<Fraction> m = new BlockFieldMatrix<>(FractionField.getInstance(), n, n);
- Fraction[] sub = new Fraction[n];
- Arrays.fill(sub, new Fraction(1));
+ FieldMatrix<Dfp> m = new BlockFieldMatrix<>(Dfp25.getField(), n, n);
+ Dfp[] sub = new Dfp[n];
+ Arrays.fill(sub, Dfp25.of(1));
m.setColumn(2, sub);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (j != 2) {
- Assert.assertEquals(new Fraction(0), m.getEntry(i, j));
+ Assert.assertEquals(Dfp25.of(0), m.getEntry(i, j));
} else {
- Assert.assertEquals(new Fraction(1), m.getEntry(i, j));
+ Assert.assertEquals(Dfp25.of(1), m.getEntry(i, j));
}
}
}
@@ -1129,15 +1138,15 @@ public final class BlockFieldMatrixTest {
}
- private Fraction[] columnToArray(Fraction[][] column) {
- Fraction[] data = new Fraction[column.length];
+ private Dfp[] columnToArray(Dfp[][] column) {
+ Dfp[] data = new Dfp[column.length];
for (int i = 0; i < data.length; ++i) {
data[i] = column[i][0];
}
return data;
}
- private void checkArrays(Fraction[] expected, Fraction[] actual) {
+ private void checkArrays(Dfp[] expected, Dfp[] actual) {
Assert.assertEquals(expected.length, actual.length);
for (int i = 0; i < expected.length; ++i) {
Assert.assertEquals(expected[i], actual[i]);
@@ -1146,9 +1155,9 @@ public final class BlockFieldMatrixTest {
@Test
public void testEqualsAndHashCode() {
- BlockFieldMatrix<Fraction> m = new BlockFieldMatrix<>(testData);
- BlockFieldMatrix<Fraction> m1 = (BlockFieldMatrix<Fraction>) m.copy();
- BlockFieldMatrix<Fraction> mt = (BlockFieldMatrix<Fraction>) m.transpose();
+ BlockFieldMatrix<Dfp> m = new BlockFieldMatrix<>(testData);
+ BlockFieldMatrix<Dfp> m1 = (BlockFieldMatrix<Dfp>) m.copy();
+ BlockFieldMatrix<Dfp> mt = (BlockFieldMatrix<Dfp>) m.transpose();
Assert.assertTrue(m.hashCode() != mt.hashCode());
Assert.assertEquals(m.hashCode(), m1.hashCode());
Assert.assertEquals(m, m);
@@ -1160,44 +1169,44 @@ public final class BlockFieldMatrixTest {
@Test
public void testToString() {
- BlockFieldMatrix<Fraction> m = new BlockFieldMatrix<>(testData);
- Assert.assertEquals("BlockFieldMatrix{{1,2,3},{2,5,3},{1,0,8}}", m.toString());
+ BlockFieldMatrix<Dfp> m = new BlockFieldMatrix<>(testData);
+ Assert.assertEquals("BlockFieldMatrix{{1.,2.,3.},{2.,5.,3.},{1.,0.,8.}}", m.toString());
}
@Test
public void testSetSubMatrix() {
- BlockFieldMatrix<Fraction> m = new BlockFieldMatrix<>(testData);
+ BlockFieldMatrix<Dfp> m = new BlockFieldMatrix<>(testData);
m.setSubMatrix(detData2,1,1);
- FieldMatrix<Fraction> expected = new BlockFieldMatrix<>
- (new Fraction[][] {{new Fraction(1),new Fraction(2),new Fraction(3)},{new Fraction(2),new Fraction(1),new Fraction(3)},{new Fraction(1),new Fraction(2),new Fraction(4)}});
+ FieldMatrix<Dfp> expected = new BlockFieldMatrix<>
+ (new Dfp[][] {{Dfp25.of(1),Dfp25.of(2),Dfp25.of(3)},{Dfp25.of(2),Dfp25.of(1),Dfp25.of(3)},{Dfp25.of(1),Dfp25.of(2),Dfp25.of(4)}});
Assert.assertEquals(expected, m);
m.setSubMatrix(detData2,0,0);
expected = new BlockFieldMatrix<>
- (new Fraction[][] {{new Fraction(1),new Fraction(3),new Fraction(3)},{new Fraction(2),new Fraction(4),new Fraction(3)},{new Fraction(1),new Fraction(2),new Fraction(4)}});
+ (new Dfp[][] {{Dfp25.of(1),Dfp25.of(3),Dfp25.of(3)},{Dfp25.of(2),Dfp25.of(4),Dfp25.of(3)},{Dfp25.of(1),Dfp25.of(2),Dfp25.of(4)}});
Assert.assertEquals(expected, m);
m.setSubMatrix(testDataPlus2,0,0);
expected = new BlockFieldMatrix<>
- (new Fraction[][] {{new Fraction(3),new Fraction(4),new Fraction(5)},{new Fraction(4),new Fraction(7),new Fraction(5)},{new Fraction(3),new Fraction(2),new Fraction(10)}});
+ (new Dfp[][] {{Dfp25.of(3),Dfp25.of(4),Dfp25.of(5)},{Dfp25.of(4),Dfp25.of(7),Dfp25.of(5)},{Dfp25.of(3),Dfp25.of(2),Dfp25.of(10)}});
Assert.assertEquals(expected, m);
// javadoc example
- BlockFieldMatrix<Fraction> matrix =
- new BlockFieldMatrix<>(new Fraction[][] {
- {new Fraction(1), new Fraction(2), new Fraction(3), new Fraction(4)},
- {new Fraction(5), new Fraction(6), new Fraction(7), new Fraction(8)},
- {new Fraction(9), new Fraction(0), new Fraction(1) , new Fraction(2)}
+ BlockFieldMatrix<Dfp> matrix =
+ new BlockFieldMatrix<>(new Dfp[][] {
+ {Dfp25.of(1), Dfp25.of(2), Dfp25.of(3), Dfp25.of(4)},
+ {Dfp25.of(5), Dfp25.of(6), Dfp25.of(7), Dfp25.of(8)},
+ {Dfp25.of(9), Dfp25.of(0), Dfp25.of(1) , Dfp25.of(2)}
});
- matrix.setSubMatrix(new Fraction[][] {
- {new Fraction(3), new Fraction(4)},
- {new Fraction(5), new Fraction(6)}
+ matrix.setSubMatrix(new Dfp[][] {
+ {Dfp25.of(3), Dfp25.of(4)},
+ {Dfp25.of(5), Dfp25.of(6)}
}, 1, 1);
expected =
- new BlockFieldMatrix<>(new Fraction[][] {
- {new Fraction(1), new Fraction(2), new Fraction(3),new Fraction(4)},
- {new Fraction(5), new Fraction(3), new Fraction(4), new Fraction(8)},
- {new Fraction(9), new Fraction(5) ,new Fraction(6), new Fraction(2)}
+ new BlockFieldMatrix<>(new Dfp[][] {
+ {Dfp25.of(1), Dfp25.of(2), Dfp25.of(3),Dfp25.of(4)},
+ {Dfp25.of(5), Dfp25.of(3), Dfp25.of(4), Dfp25.of(8)},
+ {Dfp25.of(9), Dfp25.of(5) ,Dfp25.of(6), Dfp25.of(2)}
});
Assert.assertEquals(expected, matrix);
@@ -1232,7 +1241,7 @@ public final class BlockFieldMatrixTest {
// ragged
try {
- m.setSubMatrix(new Fraction[][] {{new Fraction(1)}, {new Fraction(2), new Fraction(3)}}, 0, 0);
+ m.setSubMatrix(new Dfp[][] {{Dfp25.of(1)}, {Dfp25.of(2), Dfp25.of(3)}}, 0, 0);
Assert.fail("expecting MathIllegalArgumentException");
} catch (MathIllegalArgumentException e) {
// expected
@@ -1240,7 +1249,7 @@ public final class BlockFieldMatrixTest {
// empty
try {
- m.setSubMatrix(new Fraction[][] {{}}, 0, 0);
+ m.setSubMatrix(new Dfp[][] {{}}, 0, 0);
Assert.fail("expecting MathIllegalArgumentException");
} catch (MathIllegalArgumentException e) {
// expected
@@ -1252,123 +1261,130 @@ public final class BlockFieldMatrixTest {
int rows = 150;
int columns = 75;
- FieldMatrix<Fraction> m = new BlockFieldMatrix<>(FractionField.getInstance(), rows, columns);
+ FieldMatrix<Dfp> m = new BlockFieldMatrix<>(Dfp25.getField(), rows, columns);
m.walkInRowOrder(new SetVisitor());
GetVisitor getVisitor = new GetVisitor();
m.walkInOptimizedOrder(getVisitor);
Assert.assertEquals(rows * columns, getVisitor.getCount());
- m = new BlockFieldMatrix<>(FractionField.getInstance(), rows, columns);
+ m = new BlockFieldMatrix<>(Dfp25.getField(), rows, columns);
m.walkInRowOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2);
getVisitor = new GetVisitor();
m.walkInOptimizedOrder(getVisitor, 1, rows - 2, 1, columns - 2);
Assert.assertEquals((rows - 2) * (columns - 2), getVisitor.getCount());
for (int i = 0; i < rows; ++i) {
- Assert.assertEquals(new Fraction(0), m.getEntry(i, 0));
- Assert.assertEquals(new Fraction(0), m.getEntry(i, columns - 1));
+ Assert.assertEquals(Dfp25.of(0), m.getEntry(i, 0));
+ Assert.assertEquals(Dfp25.of(0), m.getEntry(i, columns - 1));
}
for (int j = 0; j < columns; ++j) {
- Assert.assertEquals(new Fraction(0), m.getEntry(0, j));
- Assert.assertEquals(new Fraction(0), m.getEntry(rows - 1, j));
+ Assert.assertEquals(Dfp25.of(0), m.getEntry(0, j));
+ Assert.assertEquals(Dfp25.of(0), m.getEntry(rows - 1, j));
}
- m = new BlockFieldMatrix<>(FractionField.getInstance(), rows, columns);
+ m = new BlockFieldMatrix<>(Dfp25.getField(), rows, columns);
m.walkInColumnOrder(new SetVisitor());
getVisitor = new GetVisitor();
m.walkInOptimizedOrder(getVisitor);
Assert.assertEquals(rows * columns, getVisitor.getCount());
- m = new BlockFieldMatrix<>(FractionField.getInstance(), rows, columns);
+ m = new BlockFieldMatrix<>(Dfp25.getField(), rows, columns);
m.walkInColumnOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2);
getVisitor = new GetVisitor();
m.walkInOptimizedOrder(getVisitor, 1, rows - 2, 1, columns - 2);
Assert.assertEquals((rows - 2) * (columns - 2), getVisitor.getCount());
for (int i = 0; i < rows; ++i) {
- Assert.assertEquals(new Fraction(0), m.getEntry(i, 0));
- Assert.assertEquals(new Fraction(0), m.getEntry(i, columns - 1));
+ Assert.assertEquals(Dfp25.of(0), m.getEntry(i, 0));
+ Assert.assertEquals(Dfp25.of(0), m.getEntry(i, columns - 1));
}
for (int j = 0; j < columns; ++j) {
- Assert.assertEquals(new Fraction(0), m.getEntry(0, j));
- Assert.assertEquals(new Fraction(0), m.getEntry(rows - 1, j));
+ Assert.assertEquals(Dfp25.of(0), m.getEntry(0, j));
+ Assert.assertEquals(Dfp25.of(0), m.getEntry(rows - 1, j));
}
- m = new BlockFieldMatrix<>(FractionField.getInstance(), rows, columns);
+ m = new BlockFieldMatrix<>(Dfp25.getField(), rows, columns);
m.walkInOptimizedOrder(new SetVisitor());
getVisitor = new GetVisitor();
m.walkInRowOrder(getVisitor);
Assert.assertEquals(rows * columns, getVisitor.getCount());
- m = new BlockFieldMatrix<>(FractionField.getInstance(), rows, columns);
+ m = new BlockFieldMatrix<>(Dfp25.getField(), rows, columns);
m.walkInOptimizedOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2);
getVisitor = new GetVisitor();
m.walkInRowOrder(getVisitor, 1, rows - 2, 1, columns - 2);
Assert.assertEquals((rows - 2) * (columns - 2), getVisitor.getCount());
for (int i = 0; i < rows; ++i) {
- Assert.assertEquals(new Fraction(0), m.getEntry(i, 0));
- Assert.assertEquals(new Fraction(0), m.getEntry(i, columns - 1));
+ Assert.assertEquals(Dfp25.of(0), m.getEntry(i, 0));
+ Assert.assertEquals(Dfp25.of(0), m.getEntry(i, columns - 1));
}
for (int j = 0; j < columns; ++j) {
- Assert.assertEquals(new Fraction(0), m.getEntry(0, j));
- Assert.assertEquals(new Fraction(0), m.getEntry(rows - 1, j));
+ Assert.assertEquals(Dfp25.of(0), m.getEntry(0, j));
+ Assert.assertEquals(Dfp25.of(0), m.getEntry(rows - 1, j));
}
- m = new BlockFieldMatrix<>(FractionField.getInstance(), rows, columns);
+ m = new BlockFieldMatrix<>(Dfp25.getField(), rows, columns);
m.walkInOptimizedOrder(new SetVisitor());
getVisitor = new GetVisitor();
m.walkInColumnOrder(getVisitor);
Assert.assertEquals(rows * columns, getVisitor.getCount());
- m = new BlockFieldMatrix<>(FractionField.getInstance(), rows, columns);
+ m = new BlockFieldMatrix<>(Dfp25.getField(), rows, columns);
m.walkInOptimizedOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2);
getVisitor = new GetVisitor();
m.walkInColumnOrder(getVisitor, 1, rows - 2, 1, columns - 2);
Assert.assertEquals((rows - 2) * (columns - 2), getVisitor.getCount());
for (int i = 0; i < rows; ++i) {
- Assert.assertEquals(new Fraction(0), m.getEntry(i, 0));
- Assert.assertEquals(new Fraction(0), m.getEntry(i, columns - 1));
+ Assert.assertEquals(Dfp25.of(0), m.getEntry(i, 0));
+ Assert.assertEquals(Dfp25.of(0), m.getEntry(i, columns - 1));
}
for (int j = 0; j < columns; ++j) {
- Assert.assertEquals(new Fraction(0), m.getEntry(0, j));
- Assert.assertEquals(new Fraction(0), m.getEntry(rows - 1, j));
+ Assert.assertEquals(Dfp25.of(0), m.getEntry(0, j));
+ Assert.assertEquals(Dfp25.of(0), m.getEntry(rows - 1, j));
}
}
@Test
public void testSerial() {
- BlockFieldMatrix<Fraction> m = new BlockFieldMatrix<>(testData);
+ final int r = 2;
+ final int c = 3;
+ BlockFieldMatrix<BigReal> m = new BlockFieldMatrix<>(BigRealField.getInstance(), r, c);
+ for (int i = 0; i < r; i++) {
+ for (int j = 0; j < c; j++) {
+ m.setEntry(i, j, new BigReal(Math.random()));
+ }
+ }
Assert.assertEquals(m,TestUtils.serializeAndRecover(m));
}
- private static class SetVisitor extends DefaultFieldMatrixChangingVisitor<Fraction> {
+ private static class SetVisitor extends DefaultFieldMatrixChangingVisitor<Dfp> {
public SetVisitor() {
- super(Fraction.ZERO);
+ super(Dfp25.ZERO);
}
@Override
- public Fraction visit(int i, int j, Fraction value) {
- return new Fraction(i * 11 + j, 11);
+ public Dfp visit(int i, int j, Dfp value) {
+ return Dfp25.of(i * 11 + j, 11);
}
}
- private static class GetVisitor extends DefaultFieldMatrixPreservingVisitor<Fraction> {
+ private static class GetVisitor extends DefaultFieldMatrixPreservingVisitor<Dfp> {
private int count;
public GetVisitor() {
- super(Fraction.ZERO);
+ super(Dfp25.ZERO);
count = 0;
}
@Override
- public void visit(int i, int j, Fraction value) {
+ public void visit(int i, int j, Dfp value) {
++count;
- Assert.assertEquals(new Fraction(i * 11 + j, 11), value);
+ Assert.assertEquals(Dfp25.of(i * 11 + j, 11), value);
}
public int getCount() {
return count;
}
}
- private BlockFieldMatrix<Fraction> createRandomMatrix(Random r, int rows, int columns) {
- BlockFieldMatrix<Fraction> m =
- new BlockFieldMatrix<>(FractionField.getInstance(), rows, columns);
+ private BlockFieldMatrix<Dfp> createRandomMatrix(Random r, int rows, int columns) {
+ BlockFieldMatrix<Dfp> m =
+ new BlockFieldMatrix<>(Dfp25.getField(), rows, columns);
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < columns; ++j) {
int p = r.nextInt(20) - 10;
@@ -1376,7 +1392,7 @@ public final class BlockFieldMatrixTest {
if (q == 0) {
q = 1;
}
- m.setEntry(i, j, new Fraction(p, q));
+ m.setEntry(i, j, Dfp25.of(p, q));
}
}
return m;
diff --git a/src/test/java/org/apache/commons/math4/fraction/FractionFieldTest.java b/src/test/java/org/apache/commons/math4/linear/Dfp25.java
similarity index 51%
rename from src/test/java/org/apache/commons/math4/fraction/FractionFieldTest.java
rename to src/test/java/org/apache/commons/math4/linear/Dfp25.java
index 4a85f08..7f6dd32 100644
--- a/src/test/java/org/apache/commons/math4/fraction/FractionFieldTest.java
+++ b/src/test/java/org/apache/commons/math4/linear/Dfp25.java
@@ -14,32 +14,28 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.commons.math4.fraction;
+package org.apache.commons.math4.linear;
+import org.apache.commons.math4.dfp.Dfp;
+import org.apache.commons.math4.dfp.DfpField;
-import org.apache.commons.math4.TestUtils;
-import org.apache.commons.math4.fraction.Fraction;
-import org.apache.commons.math4.fraction.FractionField;
-import org.junit.Assert;
-import org.junit.Test;
-
-public class FractionFieldTest {
+/**
+ * Dummy class for testing {@link org.apache.commons.math4.Field} functionalities.
+ */
+public class Dfp25 {
+ private static final DfpField FIELD = new DfpField(25);
+ public static final Dfp ZERO = FIELD.newDfp(0d);
+ public static final Dfp ONE = of(1d);
+ public static final Dfp TWO = of(2d);
- @Test
- public void testZero() {
- Assert.assertEquals(Fraction.ZERO, FractionField.getInstance().getZero());
+ public static Dfp of(double x) {
+ return ZERO.newInstance(x);
}
-
- @Test
- public void testOne() {
- Assert.assertEquals(Fraction.ONE, FractionField.getInstance().getOne());
+ public static Dfp of(double x, double y) {
+ return of(x).divide(of(y));
}
- @Test
- public void testSerial() {
- // deserializing the singleton should give the singleton itself back
- FractionField field = FractionField.getInstance();
- Assert.assertTrue(field == TestUtils.serializeAndRecover(field));
+ public static DfpField getField() {
+ return FIELD;
}
-
}
diff --git a/src/test/java/org/apache/commons/math4/linear/FieldLUDecompositionTest.java b/src/test/java/org/apache/commons/math4/linear/FieldLUDecompositionTest.java
index 0aa8c06..df74ca6 100644
--- a/src/test/java/org/apache/commons/math4/linear/FieldLUDecompositionTest.java
+++ b/src/test/java/org/apache/commons/math4/linear/FieldLUDecompositionTest.java
@@ -20,48 +20,48 @@ package org.apache.commons.math4.linear;
import org.junit.Test;
import org.junit.Assert;
import org.apache.commons.math4.TestUtils;
-import org.apache.commons.math4.fraction.Fraction;
-import org.apache.commons.math4.fraction.FractionField;
+import org.apache.commons.math4.dfp.Dfp;
+import org.apache.commons.math4.dfp.DfpField;
import org.apache.commons.math4.linear.Array2DRowFieldMatrix;
import org.apache.commons.math4.linear.FieldLUDecomposition;
import org.apache.commons.math4.linear.FieldMatrix;
import org.apache.commons.math4.linear.NonSquareMatrixException;
public class FieldLUDecompositionTest {
- private Fraction[][] testData = {
- { new Fraction(1), new Fraction(2), new Fraction(3)},
- { new Fraction(2), new Fraction(5), new Fraction(3)},
- { new Fraction(1), new Fraction(0), new Fraction(8)}
+ private Dfp[][] testData = {
+ { Dfp25.of(1), Dfp25.of(2), Dfp25.of(3)},
+ { Dfp25.of(2), Dfp25.of(5), Dfp25.of(3)},
+ { Dfp25.of(1), Dfp25.of(0), Dfp25.of(8)}
};
- private Fraction[][] testDataMinus = {
- { new Fraction(-1), new Fraction(-2), new Fraction(-3)},
- { new Fraction(-2), new Fraction(-5), new Fraction(-3)},
- { new Fraction(-1), new Fraction(0), new Fraction(-8)}
+ private Dfp[][] testDataMinus = {
+ { Dfp25.of(-1), Dfp25.of(-2), Dfp25.of(-3)},
+ { Dfp25.of(-2), Dfp25.of(-5), Dfp25.of(-3)},
+ { Dfp25.of(-1), Dfp25.of(0), Dfp25.of(-8)}
};
- private Fraction[][] luData = {
- { new Fraction(2), new Fraction(3), new Fraction(3) },
- { new Fraction(2), new Fraction(3), new Fraction(7) },
- { new Fraction(6), new Fraction(6), new Fraction(8) }
+ private Dfp[][] luData = {
+ { Dfp25.of(2), Dfp25.of(3), Dfp25.of(3) },
+ { Dfp25.of(2), Dfp25.of(3), Dfp25.of(7) },
+ { Dfp25.of(6), Dfp25.of(6), Dfp25.of(8) }
};
// singular matrices
- private Fraction[][] singular = {
- { new Fraction(2), new Fraction(3) },
- { new Fraction(2), new Fraction(3) }
+ private Dfp[][] singular = {
+ { Dfp25.of(2), Dfp25.of(3) },
+ { Dfp25.of(2), Dfp25.of(3) }
};
- private Fraction[][] bigSingular = {
- { new Fraction(1), new Fraction(2), new Fraction(3), new Fraction(4) },
- { new Fraction(2), new Fraction(5), new Fraction(3), new Fraction(4) },
- { new Fraction(7), new Fraction(3), new Fraction(256), new Fraction(1930) },
- { new Fraction(3), new Fraction(7), new Fraction(6), new Fraction(8) }
+ private Dfp[][] bigSingular = {
+ { Dfp25.of(1), Dfp25.of(2), Dfp25.of(3), Dfp25.of(4) },
+ { Dfp25.of(2), Dfp25.of(5), Dfp25.of(3), Dfp25.of(4) },
+ { Dfp25.of(7), Dfp25.of(3), Dfp25.of(256), Dfp25.of(1930) },
+ { Dfp25.of(3), Dfp25.of(7), Dfp25.of(6), Dfp25.of(8) }
}; // 4th row = 1st + 2nd
/** test dimensions */
@Test
public void testDimensions() {
- FieldMatrix<Fraction> matrix =
- new Array2DRowFieldMatrix<>(FractionField.getInstance(), testData);
- FieldLUDecomposition<Fraction> LU = new FieldLUDecomposition<>(matrix);
+ FieldMatrix<Dfp> matrix =
+ new Array2DRowFieldMatrix<>(Dfp25.getField(), testData);
+ FieldLUDecomposition<Dfp> LU = new FieldLUDecomposition<>(matrix);
Assert.assertEquals(testData.length, LU.getL().getRowDimension());
Assert.assertEquals(testData.length, LU.getL().getColumnDimension());
Assert.assertEquals(testData.length, LU.getU().getRowDimension());
@@ -75,11 +75,11 @@ public class FieldLUDecompositionTest {
@Test
public void testNonSquare() {
try {
- // we don't use FractionField.getInstance() for testing purposes
- new FieldLUDecomposition<>(new Array2DRowFieldMatrix<>(new Fraction[][] {
- { Fraction.ZERO, Fraction.ZERO },
- { Fraction.ZERO, Fraction.ZERO },
- { Fraction.ZERO, Fraction.ZERO }
+ // we don't use Dfp25.getField() for testing purposes
+ new FieldLUDecomposition<>(new Array2DRowFieldMatrix<>(new Dfp[][] {
+ { Dfp25.ZERO, Dfp25.ZERO },
+ { Dfp25.ZERO, Dfp25.ZERO },
+ { Dfp25.ZERO, Dfp25.ZERO }
}));
Assert.fail("Expected NonSquareMatrixException");
} catch (NonSquareMatrixException ime) {
@@ -90,23 +90,23 @@ public class FieldLUDecompositionTest {
/** test PA = LU */
@Test
public void testPAEqualLU() {
- FieldMatrix<Fraction> matrix = new Array2DRowFieldMatrix<>(FractionField.getInstance(), testData);
- FieldLUDecomposition<Fraction> lu = new FieldLUDecomposition<>(matrix);
- FieldMatrix<Fraction> l = lu.getL();
- FieldMatrix<Fraction> u = lu.getU();
- FieldMatrix<Fraction> p = lu.getP();
+ FieldMatrix<Dfp> matrix = new Array2DRowFieldMatrix<>(Dfp25.getField(), testData);
+ FieldLUDecomposition<Dfp> lu = new FieldLUDecomposition<>(matrix);
+ FieldMatrix<Dfp> l = lu.getL();
+ FieldMatrix<Dfp> u = lu.getU();
+ FieldMatrix<Dfp> p = lu.getP();
TestUtils.assertEquals(p.multiply(matrix), l.multiply(u));
- matrix = new Array2DRowFieldMatrix<>(FractionField.getInstance(), testDataMinus);
+ matrix = new Array2DRowFieldMatrix<>(Dfp25.getField(), testDataMinus);
lu = new FieldLUDecomposition<>(matrix);
l = lu.getL();
u = lu.getU();
p = lu.getP();
TestUtils.assertEquals(p.multiply(matrix), l.multiply(u));
- matrix = new Array2DRowFieldMatrix<>(FractionField.getInstance(), 17, 17);
+ matrix = new Array2DRowFieldMatrix<>(Dfp25.getField(), 17, 17);
for (int i = 0; i < matrix.getRowDimension(); ++i) {
- matrix.setEntry(i, i, Fraction.ONE);
+ matrix.setEntry(i, i, Dfp25.ONE);
}
lu = new FieldLUDecomposition<>(matrix);
l = lu.getL();
@@ -114,14 +114,14 @@ public class FieldLUDecompositionTest {
p = lu.getP();
TestUtils.assertEquals(p.multiply(matrix), l.multiply(u));
- matrix = new Array2DRowFieldMatrix<>(FractionField.getInstance(), singular);
+ matrix = new Array2DRowFieldMatrix<>(Dfp25.getField(), singular);
lu = new FieldLUDecomposition<>(matrix);
Assert.assertFalse(lu.getSolver().isNonSingular());
Assert.assertNull(lu.getL());
Assert.assertNull(lu.getU());
Assert.assertNull(lu.getP());
- matrix = new Array2DRowFieldMatrix<>(FractionField.getInstance(), bigSingular);
+ matrix = new Array2DRowFieldMatrix<>(Dfp25.getField(), bigSingular);
lu = new FieldLUDecomposition<>(matrix);
Assert.assertFalse(lu.getSolver().isNonSingular());
Assert.assertNull(lu.getL());
@@ -133,12 +133,12 @@ public class FieldLUDecompositionTest {
/** test that L is lower triangular with unit diagonal */
@Test
public void testLLowerTriangular() {
- FieldMatrix<Fraction> matrix = new Array2DRowFieldMatrix<>(FractionField.getInstance(), testData);
- FieldMatrix<Fraction> l = new FieldLUDecomposition<>(matrix).getL();
+ FieldMatrix<Dfp> matrix = new Array2DRowFieldMatrix<>(Dfp25.getField(), testData);
+ FieldMatrix<Dfp> l = new FieldLUDecomposition<>(matrix).getL();
for (int i = 0; i < l.getRowDimension(); i++) {
- Assert.assertEquals(Fraction.ONE, l.getEntry(i, i));
+ Assert.assertEquals(Dfp25.ONE, l.getEntry(i, i));
for (int j = i + 1; j < l.getColumnDimension(); j++) {
- Assert.assertEquals(Fraction.ZERO, l.getEntry(i, j));
+ Assert.assertEquals(Dfp25.ZERO, l.getEntry(i, j));
}
}
}
@@ -146,11 +146,11 @@ public class FieldLUDecompositionTest {
/** test that U is upper triangular */
@Test
public void testUUpperTriangular() {
- FieldMatrix<Fraction> matrix = new Array2DRowFieldMatrix<>(FractionField.getInstance(), testData);
- FieldMatrix<Fraction> u = new FieldLUDecomposition<>(matrix).getU();
+ FieldMatrix<Dfp> matrix = new Array2DRowFieldMatrix<>(Dfp25.getField(), testData);
+ FieldMatrix<Dfp> u = new FieldLUDecomposition<>(matrix).getU();
for (int i = 0; i < u.getRowDimension(); i++) {
for (int j = 0; j < i; j++) {
- Assert.assertEquals(Fraction.ZERO, u.getEntry(i, j));
+ Assert.assertEquals(Dfp25.ZERO, u.getEntry(i, j));
}
}
}
@@ -158,15 +158,15 @@ public class FieldLUDecompositionTest {
/** test that P is a permutation matrix */
@Test
public void testPPermutation() {
- FieldMatrix<Fraction> matrix = new Array2DRowFieldMatrix<>(FractionField.getInstance(), testData);
- FieldMatrix<Fraction> p = new FieldLUDecomposition<>(matrix).getP();
+ FieldMatrix<Dfp> matrix = new Array2DRowFieldMatrix<>(Dfp25.getField(), testData);
+ FieldMatrix<Dfp> p = new FieldLUDecomposition<>(matrix).getP();
- FieldMatrix<Fraction> ppT = p.multiply(p.transpose());
- FieldMatrix<Fraction> id =
- new Array2DRowFieldMatrix<>(FractionField.getInstance(),
+ FieldMatrix<Dfp> ppT = p.multiply(p.transpose());
+ FieldMatrix<Dfp> id =
+ new Array2DRowFieldMatrix<>(Dfp25.getField(),
p.getRowDimension(), p.getRowDimension());
for (int i = 0; i < id.getRowDimension(); ++i) {
- id.setEntry(i, i, Fraction.ONE);
+ id.setEntry(i, i, Dfp25.ONE);
}
TestUtils.assertEquals(id, ppT);
@@ -175,10 +175,10 @@ public class FieldLUDecompositionTest {
int oneCount = 0;
int otherCount = 0;
for (int j = 0; j < p.getColumnDimension(); j++) {
- final Fraction e = p.getEntry(i, j);
- if (e.equals(Fraction.ZERO)) {
+ final Dfp e = p.getEntry(i, j);
+ if (e.equals(Dfp25.ZERO)) {
++zeroCount;
- } else if (e.equals(Fraction.ONE)) {
+ } else if (e.equals(Dfp25.ONE)) {
++oneCount;
} else {
++otherCount;
@@ -194,10 +194,10 @@ public class FieldLUDecompositionTest {
int oneCount = 0;
int otherCount = 0;
for (int i = 0; i < p.getRowDimension(); i++) {
- final Fraction e = p.getEntry(i, j);
- if (e.equals(Fraction.ZERO)) {
+ final Dfp e = p.getEntry(i, j);
+ if (e.equals(Dfp25.ZERO)) {
++zeroCount;
- } else if (e.equals(Fraction.ONE)) {
+ } else if (e.equals(Dfp25.ONE)) {
++oneCount;
} else {
++otherCount;
@@ -214,43 +214,43 @@ public class FieldLUDecompositionTest {
/** test singular */
@Test
public void testSingular() {
- FieldLUDecomposition<Fraction> lu =
- new FieldLUDecomposition<>(new Array2DRowFieldMatrix<>(FractionField.getInstance(), testData));
+ FieldLUDecomposition<Dfp> lu =
+ new FieldLUDecomposition<>(new Array2DRowFieldMatrix<>(Dfp25.getField(), testData));
Assert.assertTrue(lu.getSolver().isNonSingular());
- lu = new FieldLUDecomposition<>(new Array2DRowFieldMatrix<>(FractionField.getInstance(), singular));
+ lu = new FieldLUDecomposition<>(new Array2DRowFieldMatrix<>(Dfp25.getField(), singular));
Assert.assertFalse(lu.getSolver().isNonSingular());
- lu = new FieldLUDecomposition<>(new Array2DRowFieldMatrix<>(FractionField.getInstance(), bigSingular));
+ lu = new FieldLUDecomposition<>(new Array2DRowFieldMatrix<>(Dfp25.getField(), bigSingular));
Assert.assertFalse(lu.getSolver().isNonSingular());
}
/** test matrices values */
@Test
public void testMatricesValues1() {
- FieldLUDecomposition<Fraction> lu =
- new FieldLUDecomposition<>(new Array2DRowFieldMatrix<>(FractionField.getInstance(), testData));
- FieldMatrix<Fraction> lRef = new Array2DRowFieldMatrix<>(FractionField.getInstance(), new Fraction[][] {
- { new Fraction(1), new Fraction(0), new Fraction(0) },
- { new Fraction(2), new Fraction(1), new Fraction(0) },
- { new Fraction(1), new Fraction(-2), new Fraction(1) }
+ FieldLUDecomposition<Dfp> lu =
+ new FieldLUDecomposition<>(new Array2DRowFieldMatrix<>(Dfp25.getField(), testData));
+ FieldMatrix<Dfp> lRef = new Array2DRowFieldMatrix<>(Dfp25.getField(), new Dfp[][] {
+ { Dfp25.of(1), Dfp25.of(0), Dfp25.of(0) },
+ { Dfp25.of(2), Dfp25.of(1), Dfp25.of(0) },
+ { Dfp25.of(1), Dfp25.of(-2), Dfp25.of(1) }
});
- FieldMatrix<Fraction> uRef = new Array2DRowFieldMatrix<>(FractionField.getInstance(), new Fraction[][] {
- { new Fraction(1), new Fraction(2), new Fraction(3) },
- { new Fraction(0), new Fraction(1), new Fraction(-3) },
- { new Fraction(0), new Fraction(0), new Fraction(-1) }
+ FieldMatrix<Dfp> uRef = new Array2DRowFieldMatrix<>(Dfp25.getField(), new Dfp[][] {
+ { Dfp25.of(1), Dfp25.of(2), Dfp25.of(3) },
+ { Dfp25.of(0), Dfp25.of(1), Dfp25.of(-3) },
+ { Dfp25.of(0), Dfp25.of(0), Dfp25.of(-1) }
});
- FieldMatrix<Fraction> pRef = new Array2DRowFieldMatrix<>(FractionField.getInstance(), new Fraction[][] {
- { new Fraction(1), new Fraction(0), new Fraction(0) },
- { new Fraction(0), new Fraction(1), new Fraction(0) },
- { new Fraction(0), new Fraction(0), new Fraction(1) }
+ FieldMatrix<Dfp> pRef = new Array2DRowFieldMatrix<>(Dfp25.getField(), new Dfp[][] {
+ { Dfp25.of(1), Dfp25.of(0), Dfp25.of(0) },
+ { Dfp25.of(0), Dfp25.of(1), Dfp25.of(0) },
+ { Dfp25.of(0), Dfp25.of(0), Dfp25.of(1) }
});
int[] pivotRef = { 0, 1, 2 };
// check values against known references
- FieldMatrix<Fraction> l = lu.getL();
+ FieldMatrix<Dfp> l = lu.getL();
TestUtils.assertEquals(lRef, l);
- FieldMatrix<Fraction> u = lu.getU();
+ FieldMatrix<Dfp> u = lu.getU();
TestUtils.assertEquals(uRef, u);
- FieldMatrix<Fraction> p = lu.getP();
+ FieldMatrix<Dfp> p = lu.getP();
TestUtils.assertEquals(pRef, p);
int[] pivot = lu.getPivot();
for (int i = 0; i < pivotRef.length; ++i) {
@@ -267,31 +267,31 @@ public class FieldLUDecompositionTest {
/** test matrices values */
@Test
public void testMatricesValues2() {
- FieldLUDecomposition<Fraction> lu =
- new FieldLUDecomposition<>(new Array2DRowFieldMatrix<>(FractionField.getInstance(), luData));
- FieldMatrix<Fraction> lRef = new Array2DRowFieldMatrix<>(FractionField.getInstance(), new Fraction[][] {
- { new Fraction(1), new Fraction(0), new Fraction(0) },
- { new Fraction(3), new Fraction(1), new Fraction(0) },
- { new Fraction(1), new Fraction(0), new Fraction(1) }
+ FieldLUDecomposition<Dfp> lu =
+ new FieldLUDecomposition<>(new Array2DRowFieldMatrix<>(Dfp25.getField(), luData));
+ FieldMatrix<Dfp> lRef = new Array2DRowFieldMatrix<>(Dfp25.getField(), new Dfp[][] {
+ { Dfp25.of(1), Dfp25.of(0), Dfp25.of(0) },
+ { Dfp25.of(3), Dfp25.of(1), Dfp25.of(0) },
+ { Dfp25.of(1), Dfp25.of(0), Dfp25.of(1) }
});
- FieldMatrix<Fraction> uRef = new Array2DRowFieldMatrix<>(FractionField.getInstance(), new Fraction[][] {
- { new Fraction(2), new Fraction(3), new Fraction(3) },
- { new Fraction(0), new Fraction(-3), new Fraction(-1) },
- { new Fraction(0), new Fraction(0), new Fraction(4) }
+ FieldMatrix<Dfp> uRef = new Array2DRowFieldMatrix<>(Dfp25.getField(), new Dfp[][] {
+ { Dfp25.of(2), Dfp25.of(3), Dfp25.of(3) },
+ { Dfp25.of(0), Dfp25.of(-3), Dfp25.of(-1) },
+ { Dfp25.of(0), Dfp25.of(0), Dfp25.of(4) }
});
- FieldMatrix<Fraction> pRef = new Array2DRowFieldMatrix<>(FractionField.getInstance(), new Fraction[][] {
- { new Fraction(1), new Fraction(0), new Fraction(0) },
- { new Fraction(0), new Fraction(0), new Fraction(1) },
- { new Fraction(0), new Fraction(1), new Fraction(0) }
+ FieldMatrix<Dfp> pRef = new Array2DRowFieldMatrix<>(Dfp25.getField(), new Dfp[][] {
+ { Dfp25.of(1), Dfp25.of(0), Dfp25.of(0) },
+ { Dfp25.of(0), Dfp25.of(0), Dfp25.of(1) },
+ { Dfp25.of(0), Dfp25.of(1), Dfp25.of(0) }
});
int[] pivotRef = { 0, 2, 1 };
// check values against known references
- FieldMatrix<Fraction> l = lu.getL();
+ FieldMatrix<Dfp> l = lu.getL();
TestUtils.assertEquals(lRef, l);
- FieldMatrix<Fraction> u = lu.getU();
+ FieldMatrix<Dfp> u = lu.getU();
TestUtils.assertEquals(uRef, u);
- FieldMatrix<Fraction> p = lu.getP();
+ FieldMatrix<Dfp> p = lu.getP();
TestUtils.assertEquals(pRef, p);
int[] pivot = lu.getPivot();
for (int i = 0; i < pivotRef.length; ++i) {
diff --git a/src/test/java/org/apache/commons/math4/linear/FieldLUSolverTest.java b/src/test/java/org/apache/commons/math4/linear/FieldLUSolverTest.java
index 70ad971..b46f122 100644
--- a/src/test/java/org/apache/commons/math4/linear/FieldLUSolverTest.java
+++ b/src/test/java/org/apache/commons/math4/linear/FieldLUSolverTest.java
@@ -18,8 +18,8 @@
package org.apache.commons.math4.linear;
import org.apache.commons.math4.exception.MathIllegalArgumentException;
-import org.apache.commons.math4.fraction.Fraction;
-import org.apache.commons.math4.fraction.FractionField;
+import org.apache.commons.math4.dfp.Dfp;
+import org.apache.commons.math4.dfp.DfpField;
import org.apache.commons.math4.linear.Array2DRowFieldMatrix;
import org.apache.commons.math4.linear.FieldDecompositionSolver;
import org.apache.commons.math4.linear.FieldLUDecomposition;
@@ -54,15 +54,15 @@ public class FieldLUSolverTest {
{ 3, 7, 6, 8 }
}; // 4th row = 1st + 2nd
- public static FieldMatrix<Fraction> createFractionMatrix(final int[][] data) {
+ public static FieldMatrix<Dfp> createDfpMatrix(final int[][] data) {
final int numRows = data.length;
final int numCols = data[0].length;
- final Array2DRowFieldMatrix<Fraction> m;
- m = new Array2DRowFieldMatrix<>(FractionField.getInstance(),
+ final Array2DRowFieldMatrix<Dfp> m;
+ m = new Array2DRowFieldMatrix<>(Dfp25.getField(),
numRows, numCols);
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
- m.setEntry(i, j, new Fraction(data[i][j], 1));
+ m.setEntry(i, j, Dfp25.of(data[i][j], 1));
}
}
return m;
@@ -71,14 +71,14 @@ public class FieldLUSolverTest {
/** test singular */
@Test
public void testSingular() {
- FieldDecompositionSolver<Fraction> solver;
- solver = new FieldLUDecomposition<>(createFractionMatrix(testData))
+ FieldDecompositionSolver<Dfp> solver;
+ solver = new FieldLUDecomposition<>(createDfpMatrix(testData))
.getSolver();
Assert.assertTrue(solver.isNonSingular());
- solver = new FieldLUDecomposition<>(createFractionMatrix(singular))
+ solver = new FieldLUDecomposition<>(createDfpMatrix(singular))
.getSolver();
Assert.assertFalse(solver.isNonSingular());
- solver = new FieldLUDecomposition<>(createFractionMatrix(bigSingular))
+ solver = new FieldLUDecomposition<>(createDfpMatrix(bigSingular))
.getSolver();
Assert.assertFalse(solver.isNonSingular());
}
@@ -86,10 +86,10 @@ public class FieldLUSolverTest {
/** test solve dimension errors */
@Test
public void testSolveDimensionErrors() {
- FieldDecompositionSolver<Fraction> solver;
- solver = new FieldLUDecomposition<>(createFractionMatrix(testData))
+ FieldDecompositionSolver<Dfp> solver;
+ solver = new FieldLUDecomposition<>(createDfpMatrix(testData))
.getSolver();
- FieldMatrix<Fraction> b = createFractionMatrix(new int[2][2]);
+ FieldMatrix<Dfp> b = createDfpMatrix(new int[2][2]);
try {
solver.solve(b);
Assert.fail("an exception should have been thrown");
@@ -107,10 +107,10 @@ public class FieldLUSolverTest {
/** test solve singularity errors */
@Test
public void testSolveSingularityErrors() {
- FieldDecompositionSolver<Fraction> solver;
- solver = new FieldLUDecomposition<>(createFractionMatrix(singular))
+ FieldDecompositionSolver<Dfp> solver;
+ solver = new FieldLUDecomposition<>(createDfpMatrix(singular))
.getSolver();
- FieldMatrix<Fraction> b = createFractionMatrix(new int[2][2]);
+ FieldMatrix<Dfp> b = createDfpMatrix(new int[2][2]);
try {
solver.solve(b);
Assert.fail("an exception should have been thrown");
@@ -128,18 +128,18 @@ public class FieldLUSolverTest {
/** test solve */
@Test
public void testSolve() {
- FieldDecompositionSolver<Fraction> solver;
- solver = new FieldLUDecomposition<>(createFractionMatrix(testData))
+ FieldDecompositionSolver<Dfp> solver;
+ solver = new FieldLUDecomposition<>(createDfpMatrix(testData))
.getSolver();
- FieldMatrix<Fraction> b = createFractionMatrix(new int[][] {
+ FieldMatrix<Dfp> b = createDfpMatrix(new int[][] {
{ 1, 0 }, { 2, -5 }, { 3, 1 }
});
- FieldMatrix<Fraction> xRef = createFractionMatrix(new int[][] {
+ FieldMatrix<Dfp> xRef = createDfpMatrix(new int[][] {
{ 19, -71 }, { -6, 22 }, { -2, 9 }
});
// using FieldMatrix
- FieldMatrix<Fraction> x = solver.solve(b);
+ FieldMatrix<Dfp> x = solver.solve(b);
for (int i = 0; i < x.getRowDimension(); i++){
for (int j = 0; j < x.getColumnDimension(); j++){
Assert.assertEquals("(" + i + ", " + j + ")",
@@ -149,7 +149,7 @@ public class FieldLUSolverTest {
// using ArrayFieldVector
for (int j = 0; j < b.getColumnDimension(); j++) {
- final FieldVector<Fraction> xj = solver.solve(b.getColumnVector(j));
+ final FieldVector<Dfp> xj = solver.solve(b.getColumnVector(j));
for (int i = 0; i < xj.getDimension(); i++){
Assert.assertEquals("(" + i + ", " + j + ")",
xRef.getEntry(i, j), xj.getEntry(i));
@@ -158,10 +158,10 @@ public class FieldLUSolverTest {
// using SparseFieldVector
for (int j = 0; j < b.getColumnDimension(); j++) {
- final SparseFieldVector<Fraction> bj;
- bj = new SparseFieldVector<>(FractionField.getInstance(),
+ final SparseFieldVector<Dfp> bj;
+ bj = new SparseFieldVector<>(Dfp25.getField(),
b.getColumn(j));
- final FieldVector<Fraction> xj = solver.solve(bj);
+ final FieldVector<Dfp> xj = solver.solve(bj);
for (int i = 0; i < xj.getDimension(); i++) {
Assert.assertEquals("(" + i + ", " + j + ")",
xRef.getEntry(i, j), xj.getEntry(i));
@@ -172,13 +172,13 @@ public class FieldLUSolverTest {
/** test determinant */
@Test
public void testDeterminant() {
- Assert.assertEquals( -1, getDeterminant(createFractionMatrix(testData)), 1E-15);
- Assert.assertEquals(-10, getDeterminant(createFractionMatrix(luData)), 1E-14);
- Assert.assertEquals( 0, getDeterminant(createFractionMatrix(singular)), 1E-15);
- Assert.assertEquals( 0, getDeterminant(createFractionMatrix(bigSingular)), 1E-15);
+ Assert.assertEquals( -1, getDeterminant(createDfpMatrix(testData)), 1E-15);
+ Assert.assertEquals(-10, getDeterminant(createDfpMatrix(luData)), 1E-14);
+ Assert.assertEquals( 0, getDeterminant(createDfpMatrix(singular)), 1E-15);
+ Assert.assertEquals( 0, getDeterminant(createDfpMatrix(bigSingular)), 1E-15);
}
- private double getDeterminant(final FieldMatrix<Fraction> m) {
- return new FieldLUDecomposition<>(m).getDeterminant().doubleValue();
+ private double getDeterminant(final FieldMatrix<Dfp> m) {
+ return new FieldLUDecomposition<>(m).getDeterminant().toDouble();
}
}
diff --git a/src/test/java/org/apache/commons/math4/linear/FieldMatrixImplTest.java b/src/test/java/org/apache/commons/math4/linear/FieldMatrixImplTest.java
index bd4ccf5..c42e730 100644
--- a/src/test/java/org/apache/commons/math4/linear/FieldMatrixImplTest.java
+++ b/src/test/java/org/apache/commons/math4/linear/FieldMatrixImplTest.java
@@ -27,8 +27,10 @@ import org.apache.commons.math4.exception.NotStrictlyPositiveException;
import org.apache.commons.math4.exception.NullArgumentException;
import org.apache.commons.math4.exception.NumberIsTooSmallException;
import org.apache.commons.math4.exception.OutOfRangeException;
-import org.apache.commons.math4.fraction.Fraction;
-import org.apache.commons.math4.fraction.FractionField;
+import org.apache.commons.math4.dfp.Dfp;
+import org.apache.commons.math4.dfp.DfpField;
+import org.apache.commons.math4.util.BigReal;
+import org.apache.commons.math4.util.BigRealField;
import org.apache.commons.math4.linear.Array2DRowFieldMatrix;
import org.apache.commons.math4.linear.ArrayFieldVector;
import org.apache.commons.math4.linear.DefaultFieldMatrixChangingVisitor;
@@ -47,60 +49,60 @@ import org.apache.commons.math4.linear.NonSquareMatrixException;
public final class FieldMatrixImplTest {
// 3 x 3 identity matrix
- protected Fraction[][] id = { {new Fraction(1),new Fraction(0),new Fraction(0)}, {new Fraction(0),new Fraction(1),new Fraction(0)}, {new Fraction(0),new Fraction(0),new Fraction(1)} };
+ protected Dfp[][] id = { {Dfp25.of(1),Dfp25.of(0),Dfp25.of(0)}, {Dfp25.of(0),Dfp25.of(1),Dfp25.of(0)}, {Dfp25.of(0),Dfp25.of(0),Dfp25.of(1)} };
// Test data for group operations
- protected Fraction[][] testData = { {new Fraction(1),new Fraction(2),new Fraction(3)}, {new Fraction(2),new Fraction(5),new Fraction(3)}, {new Fraction(1),new Fraction(0),new Fraction(8)} };
- protected Fraction[][] testDataLU = {{new Fraction(2), new Fraction(5), new Fraction(3)}, {new Fraction(1, 2), new Fraction(-5, 2), new Fraction(13, 2)}, {new Fraction(1, 2), new Fraction(1, 5), new Fraction(1, 5)}};
- protected Fraction[][] testDataPlus2 = { {new Fraction(3),new Fraction(4),new Fraction(5)}, {new Fraction(4),new Fraction(7),new Fraction(5)}, {new Fraction(3),new Fraction(2),new Fraction(10)} };
- protected Fraction[][] testDataMinus = { {new Fraction(-1),new Fraction(-2),new Fraction(-3)}, {new Fraction(-2),new Fraction(-5),new Fraction(-3)},
- {new Fraction(-1),new Fraction(0),new Fraction(-8)} };
- protected Fraction[] testDataRow1 = {new Fraction(1),new Fraction(2),new Fraction(3)};
- protected Fraction[] testDataCol3 = {new Fraction(3),new Fraction(3),new Fraction(8)};
- protected Fraction[][] testDataInv =
- { {new Fraction(-40),new Fraction(16),new Fraction(9)}, {new Fraction(13),new Fraction(-5),new Fraction(-3)}, {new Fraction(5),new Fraction(-2),new Fraction(-1)} };
- protected Fraction[] preMultTest = {new Fraction(8),new Fraction(12),new Fraction(33)};
- protected Fraction[][] testData2 ={ {new Fraction(1),new Fraction(2),new Fraction(3)}, {new Fraction(2),new Fraction(5),new Fraction(3)}};
- protected Fraction[][] testData2T = { {new Fraction(1),new Fraction(2)}, {new Fraction(2),new Fraction(5)}, {new Fraction(3),new Fraction(3)}};
- protected Fraction[][] testDataPlusInv =
- { {new Fraction(-39),new Fraction(18),new Fraction(12)}, {new Fraction(15),new Fraction(0),new Fraction(0)}, {new Fraction(6),new Fraction(-2),new Fraction(7)} };
+ protected Dfp[][] testData = { {Dfp25.of(1),Dfp25.of(2),Dfp25.of(3)}, {Dfp25.of(2),Dfp25.of(5),Dfp25.of(3)}, {Dfp25.of(1),Dfp25.of(0),Dfp25.of(8)} };
+ protected Dfp[][] testDataLU = {{Dfp25.of(2), Dfp25.of(5), Dfp25.of(3)}, {Dfp25.of(1, 2), Dfp25.of(-5, 2), Dfp25.of(13, 2)}, {Dfp25.of(1, 2), Dfp25.of(1, 5), Dfp25.of(1, 5)}};
+ protected Dfp[][] testDataPlus2 = { {Dfp25.of(3),Dfp25.of(4),Dfp25.of(5)}, {Dfp25.of(4),Dfp25.of(7),Dfp25.of(5)}, {Dfp25.of(3),Dfp25.of(2),Dfp25.of(10)} };
+ protected Dfp[][] testDataMinus = { {Dfp25.of(-1),Dfp25.of(-2),Dfp25.of(-3)}, {Dfp25.of(-2),Dfp25.of(-5),Dfp25.of(-3)},
+ {Dfp25.of(-1),Dfp25.of(0),Dfp25.of(-8)} };
+ protected Dfp[] testDataRow1 = {Dfp25.of(1),Dfp25.of(2),Dfp25.of(3)};
+ protected Dfp[] testDataCol3 = {Dfp25.of(3),Dfp25.of(3),Dfp25.of(8)};
+ protected Dfp[][] testDataInv =
+ { {Dfp25.of(-40),Dfp25.of(16),Dfp25.of(9)}, {Dfp25.of(13),Dfp25.of(-5),Dfp25.of(-3)}, {Dfp25.of(5),Dfp25.of(-2),Dfp25.of(-1)} };
+ protected Dfp[] preMultTest = {Dfp25.of(8),Dfp25.of(12),Dfp25.of(33)};
+ protected Dfp[][] testData2 ={ {Dfp25.of(1),Dfp25.of(2),Dfp25.of(3)}, {Dfp25.of(2),Dfp25.of(5),Dfp25.of(3)}};
+ protected Dfp[][] testData2T = { {Dfp25.of(1),Dfp25.of(2)}, {Dfp25.of(2),Dfp25.of(5)}, {Dfp25.of(3),Dfp25.of(3)}};
+ protected Dfp[][] testDataPlusInv =
+ { {Dfp25.of(-39),Dfp25.of(18),Dfp25.of(12)}, {Dfp25.of(15),Dfp25.of(0),Dfp25.of(0)}, {Dfp25.of(6),Dfp25.of(-2),Dfp25.of(7)} };
// lu decomposition tests
- protected Fraction[][] luData = { {new Fraction(2),new Fraction(3),new Fraction(3)}, {new Fraction(0),new Fraction(5),new Fraction(7)}, {new Fraction(6),new Fraction(9),new Fraction(8)} };
- protected Fraction[][] luDataLUDecomposition = { {new Fraction(6),new Fraction(9),new Fraction(8)}, {new Fraction(0),new Fraction(5),new Fraction(7)},
- {new Fraction(1, 3),new Fraction(0),new Fraction(1, 3)} };
+ protected Dfp[][] luData = { {Dfp25.of(2),Dfp25.of(3),Dfp25.of(3)}, {Dfp25.of(0),Dfp25.of(5),Dfp25.of(7)}, {Dfp25.of(6),Dfp25.of(9),Dfp25.of(8)} };
+ protected Dfp[][] luDataLUDecomposition = { {Dfp25.of(6),Dfp25.of(9),Dfp25.of(8)}, {Dfp25.of(0),Dfp25.of(5),Dfp25.of(7)},
+ {Dfp25.of(1, 3),Dfp25.of(0),Dfp25.of(1, 3)} };
// singular matrices
- protected Fraction[][] singular = { {new Fraction(2),new Fraction(3)}, {new Fraction(2),new Fraction(3)} };
- protected Fraction[][] bigSingular = {{new Fraction(1),new Fraction(2),new Fraction(3),new Fraction(4)}, {new Fraction(2),new Fraction(5),new Fraction(3),new Fraction(4)},
- {new Fraction(7),new Fraction(3),new Fraction(256),new Fraction(1930)}, {new Fraction(3),new Fraction(7),new Fraction(6),new Fraction(8)}}; // 4th row = 1st + 2nd
- protected Fraction[][] detData = { {new Fraction(1),new Fraction(2),new Fraction(3)}, {new Fraction(4),new Fraction(5),new Fraction(6)}, {new Fraction(7),new Fraction(8),new Fraction(10)} };
- protected Fraction[][] detData2 = { {new Fraction(1), new Fraction(3)}, {new Fraction(2), new Fraction(4)}};
+ protected Dfp[][] singular = { {Dfp25.of(2),Dfp25.of(3)}, {Dfp25.of(2),Dfp25.of(3)} };
+ protected Dfp[][] bigSingular = {{Dfp25.of(1),Dfp25.of(2),Dfp25.of(3),Dfp25.of(4)}, {Dfp25.of(2),Dfp25.of(5),Dfp25.of(3),Dfp25.of(4)},
+ {Dfp25.of(7),Dfp25.of(3),Dfp25.of(256),Dfp25.of(1930)}, {Dfp25.of(3),Dfp25.of(7),Dfp25.of(6),Dfp25.of(8)}}; // 4th row = 1st + 2nd
+ protected Dfp[][] detData = { {Dfp25.of(1),Dfp25.of(2),Dfp25.of(3)}, {Dfp25.of(4),Dfp25.of(5),Dfp25.of(6)}, {Dfp25.of(7),Dfp25.of(8),Dfp25.of(10)} };
+ protected Dfp[][] detData2 = { {Dfp25.of(1), Dfp25.of(3)}, {Dfp25.of(2), Dfp25.of(4)}};
// vectors
- protected Fraction[] testVector = {new Fraction(1),new Fraction(2),new Fraction(3)};
- protected Fraction[] testVector2 = {new Fraction(1),new Fraction(2),new Fraction(3),new Fraction(4)};
+ protected Dfp[] testVector = {Dfp25.of(1),Dfp25.of(2),Dfp25.of(3)};
+ protected Dfp[] testVector2 = {Dfp25.of(1),Dfp25.of(2),Dfp25.of(3),Dfp25.of(4)};
// submatrix accessor tests
- protected Fraction[][] subTestData = {{new Fraction(1), new Fraction(2), new Fraction(3), new Fraction(4)}, {new Fraction(3, 2), new Fraction(5, 2), new Fraction(7, 2), new Fraction(9, 2)},
- {new Fraction(2), new Fraction(4), new Fraction(6), new Fraction(8)}, {new Fraction(4), new Fraction(5), new Fraction(6), new Fraction(7)}};
+ protected Dfp[][] subTestData = {{Dfp25.of(1), Dfp25.of(2), Dfp25.of(3), Dfp25.of(4)}, {Dfp25.of(3, 2), Dfp25.of(5, 2), Dfp25.of(7, 2), Dfp25.of(9, 2)},
+ {Dfp25.of(2), Dfp25.of(4), Dfp25.of(6), Dfp25.of(8)}, {Dfp25.of(4), Dfp25.of(5), Dfp25.of(6), Dfp25.of(7)}};
// array selections
- protected Fraction[][] subRows02Cols13 = { {new Fraction(2), new Fraction(4)}, {new Fraction(4), new Fraction(8)}};
- protected Fraction[][] subRows03Cols12 = { {new Fraction(2), new Fraction(3)}, {new Fraction(5), new Fraction(6)}};
- protected Fraction[][] subRows03Cols123 = { {new Fraction(2), new Fraction(3), new Fraction(4)} , {new Fraction(5), new Fraction(6), new Fraction(7)}};
+ protected Dfp[][] subRows02Cols13 = { {Dfp25.of(2), Dfp25.of(4)}, {Dfp25.of(4), Dfp25.of(8)}};
+ protected Dfp[][] subRows03Cols12 = { {Dfp25.of(2), Dfp25.of(3)}, {Dfp25.of(5), Dfp25.of(6)}};
+ protected Dfp[][] subRows03Cols123 = { {Dfp25.of(2), Dfp25.of(3), Dfp25.of(4)} , {Dfp25.of(5), Dfp25.of(6), Dfp25.of(7)}};
// effective permutations
- protected Fraction[][] subRows20Cols123 = { {new Fraction(4), new Fraction(6), new Fraction(8)} , {new Fraction(2), new Fraction(3), new Fraction(4)}};
- protected Fraction[][] subRows31Cols31 = {{new Fraction(7), new Fraction(5)}, {new Fraction(9, 2), new Fraction(5, 2)}};
+ protected Dfp[][] subRows20Cols123 = { {Dfp25.of(4), Dfp25.of(6), Dfp25.of(8)} , {Dfp25.of(2), Dfp25.of(3), Dfp25.of(4)}};
+ protected Dfp[][] subRows31Cols31 = {{Dfp25.of(7), Dfp25.of(5)}, {Dfp25.of(9, 2), Dfp25.of(5, 2)}};
// contiguous ranges
- protected Fraction[][] subRows01Cols23 = {{new Fraction(3),new Fraction(4)} , {new Fraction(7, 2), new Fraction(9, 2)}};
- protected Fraction[][] subRows23Cols00 = {{new Fraction(2)} , {new Fraction(4)}};
- protected Fraction[][] subRows00Cols33 = {{new Fraction(4)}};
+ protected Dfp[][] subRows01Cols23 = {{Dfp25.of(3),Dfp25.of(4)} , {Dfp25.of(7, 2), Dfp25.of(9, 2)}};
+ protected Dfp[][] subRows23Cols00 = {{Dfp25.of(2)} , {Dfp25.of(4)}};
+ protected Dfp[][] subRows00Cols33 = {{Dfp25.of(4)}};
// row matrices
- protected Fraction[][] subRow0 = {{new Fraction(1),new Fraction(2),new Fraction(3),new Fraction(4)}};
- protected Fraction[][] subRow3 = {{new Fraction(4),new Fraction(5),new Fraction(6),new Fraction(7)}};
+ protected Dfp[][] subRow0 = {{Dfp25.of(1),Dfp25.of(2),Dfp25.of(3),Dfp25.of(4)}};
+ protected Dfp[][] subRow3 = {{Dfp25.of(4),Dfp25.of(5),Dfp25.of(6),Dfp25.of(7)}};
// column matrices
- protected Fraction[][] subColumn1 = {{new Fraction(2)}, {new Fraction(5, 2)}, {new Fraction(4)}, {new Fraction(5)}};
- protected Fraction[][] subColumn3 = {{new Fraction(4)}, {new Fraction(9, 2)}, {new Fraction(8)}, {new Fraction(7)}};
+ protected Dfp[][] subColumn1 = {{Dfp25.of(2)}, {Dfp25.of(5, 2)}, {Dfp25.of(4)}, {Dfp25.of(5)}};
+ protected Dfp[][] subColumn3 = {{Dfp25.of(4)}, {Dfp25.of(9, 2)}, {Dfp25.of(8)}, {Dfp25.of(7)}};
// tolerances
protected double entryTolerance = 10E-16;
@@ -109,8 +111,8 @@ public final class FieldMatrixImplTest {
/** test dimensions */
@Test
public void testDimensions() {
- Array2DRowFieldMatrix<Fraction> m = new Array2DRowFieldMatrix<>(testData);
- Array2DRowFieldMatrix<Fraction> m2 = new Array2DRowFieldMatrix<>(testData2);
+ Array2DRowFieldMatrix<Dfp> m = new Array2DRowFieldMatrix<>(testData);
+ Array2DRowFieldMatrix<Dfp> m2 = new Array2DRowFieldMatrix<>(testData2);
Assert.assertEquals("testData row dimension",3,m.getRowDimension());
Assert.assertEquals("testData column dimension",3,m.getColumnDimension());
Assert.assertTrue("testData is square",m.isSquare());
@@ -122,21 +124,21 @@ public final class FieldMatrixImplTest {
/** test copy functions */
@Test
public void testCopyFunctions() {
- Array2DRowFieldMatrix<Fraction> m1 = new Array2DRowFieldMatrix<>(testData);
- Array2DRowFieldMatrix<Fraction> m2 = new Array2DRowFieldMatrix<>(m1.getData());
+ Array2DRowFieldMatrix<Dfp> m1 = new Array2DRowFieldMatrix<>(testData);
+ Array2DRowFieldMatrix<Dfp> m2 = new Array2DRowFieldMatrix<>(m1.getData());
Assert.assertEquals(m2,m1);
- Array2DRowFieldMatrix<Fraction> m3 = new Array2DRowFieldMatrix<>(testData);
- Array2DRowFieldMatrix<Fraction> m4 = new Array2DRowFieldMatrix<>(m3.getData(), false);
+ Array2DRowFieldMatrix<Dfp> m3 = new Array2DRowFieldMatrix<>(testData);
+ Array2DRowFieldMatrix<Dfp> m4 = new Array2DRowFieldMatrix<>(m3.getData(), false);
Assert.assertEquals(m4,m3);
}
/** test add */
@Test
public void testAdd() {
- Array2DRowFieldMatrix<Fraction> m = new Array2DRowFieldMatrix<>(testData);
- Array2DRowFieldMatrix<Fraction> mInv = new Array2DRowFieldMatrix<>(testDataInv);
- FieldMatrix<Fraction> mPlusMInv = m.add(mInv);
- Fraction[][] sumEntries = mPlusMInv.getData();
+ Array2DRowFieldMatrix<Dfp> m = new Array2DRowFieldMatrix<>(testData);
+ Array2DRowFieldMatrix<Dfp> mInv = new Array2DRowFieldMatrix<>(testDataInv);
+ FieldMatrix<Dfp> mPlusMInv = m.add(mInv);
+ Dfp[][] sumEntries = mPlusMInv.getData();
for (int row = 0; row < m.getRowDimension(); row++) {
for (int col = 0; col < m.getColumnDimension(); col++) {
Assert.assertEquals(testDataPlusInv[row][col],sumEntries[row][col]);
@@ -147,8 +149,8 @@ public final class FieldMatrixImplTest {
/** test add failure */
@Test
public void testAddFail() {
- Array2DRowFieldMatrix<Fraction> m = new Array2DRowFieldMatrix<>(testData);
- Array2DRowFieldMatrix<Fraction> m2 = new Array2DRowFieldMatrix<>(testData2);
+ Array2DRowFieldMatrix<Dfp> m = new Array2DRowFieldMatrix<>(testData);
+ Array2DRowFieldMatrix<Dfp> m2 = new Array2DRowFieldMatrix<>(testData2);
try {
m.add(m2);
Assert.fail("MathIllegalArgumentException expected");
@@ -160,9 +162,9 @@ public final class FieldMatrixImplTest {
/** test m-n = m + -n */
@Test
public void testPlusMinus() {
- Array2DRowFieldMatrix<Fraction> m = new Array2DRowFieldMatrix<>(testData);
- Array2DRowFieldMatrix<Fraction> m2 = new Array2DRowFieldMatrix<>(testDataInv);
- TestUtils.assertEquals(m.subtract(m2),m2.scalarMultiply(new Fraction(-1)).add(m));
+ Array2DRowFieldMatrix<Dfp> m = new Array2DRowFieldMatrix<>(testData);
+ Array2DRowFieldMatrix<Dfp> m2 = new Array2DRowFieldMatrix<>(testDataInv);
+ TestUtils.assertEquals(m.subtract(m2),m2.scalarMultiply(Dfp25.of(-1)).add(m));
try {
m.subtract(new Array2DRowFieldMatrix<>(testData2));
Assert.fail("Expecting illegalArgumentException");
@@ -174,10 +176,10 @@ public final class FieldMatrixImplTest {
/** test multiply */
@Test
public void testMultiply() {
- Array2DRowFieldMatrix<Fraction> m = new Array2DRowFieldMatrix<>(testData);
- Array2DRowFieldMatrix<Fraction> mInv = new Array2DRowFieldMatrix<>(testDataInv);
- Array2DRowFieldMatrix<Fraction> identity = new Array2DRowFieldMatrix<>(id);
- Array2DRowFieldMatrix<Fraction> m2 = new Array2DRowFieldMatrix<>(testData2);
+ Array2DRowFieldMatrix<Dfp> m = new Array2DRowFieldMatrix<>(testData);
+ Array2DRowFieldMatrix<Dfp> mInv = new Array2DRowFieldMatrix<>(testDataInv);
+ Array2DRowFieldMatrix<Dfp> identity = new Array2DRowFieldMatrix<>(id);
+ Array2DRowFieldMatrix<Dfp> m2 = new Array2DRowFieldMatrix<>(testData2);
TestUtils.assertEquals(m.multiply(mInv), identity);
TestUtils.assertEquals(mInv.multiply(m), identity);
TestUtils.assertEquals(m.multiply(identity), m);
@@ -191,26 +193,26 @@ public final class FieldMatrixImplTest {
}
}
- //Additional Test for Array2DRowFieldMatrix<Fraction>Test.testMultiply
+ //Additional Test for Array2DRowFieldMatrix<Dfp>Test.testMultiply
- private final Fraction[][] d3 = new Fraction[][] {{new Fraction(1),new Fraction(2),new Fraction(3),new Fraction(4)},{new Fraction(5),new Fraction(6),new Fraction(7),new Fraction(8)}};
- private final Fraction[][] d4 = new Fraction[][] {{new Fraction(1)},{new Fraction(2)},{new Fraction(3)},{new Fraction(4)}};
- private final Fraction[][] d5 = new Fraction[][] {{new Fraction(30)},{new Fraction(70)}};
+ private final Dfp[][] d3 = new Dfp[][] {{Dfp25.of(1),Dfp25.of(2),Dfp25.of(3),Dfp25.of(4)},{Dfp25.of(5),Dfp25.of(6),Dfp25.of(7),Dfp25.of(8)}};
+ private final Dfp[][] d4 = new Dfp[][] {{Dfp25.of(1)},{Dfp25.of(2)},{Dfp25.of(3)},{Dfp25.of(4)}};
+ private final Dfp[][] d5 = new Dfp[][] {{Dfp25.of(30)},{Dfp25.of(70)}};
@Test
public void testMultiply2() {
- FieldMatrix<Fraction> m3 = new Array2DRowFieldMatrix<>(d3);
- FieldMatrix<Fraction> m4 = new Array2DRowFieldMatrix<>(d4);
- FieldMatrix<Fraction> m5 = new Array2DRowFieldMatrix<>(d5);
+ FieldMatrix<Dfp> m3 = new Array2DRowFieldMatrix<>(d3);
+ FieldMatrix<Dfp> m4 = new Array2DRowFieldMatrix<>(d4);
+ FieldMatrix<Dfp> m5 = new Array2DRowFieldMatrix<>(d5);
TestUtils.assertEquals(m3.multiply(m4), m5);
}
@Test
public void testPower() {
- FieldMatrix<Fraction> m = new Array2DRowFieldMatrix<>(testData);
- FieldMatrix<Fraction> mInv = new Array2DRowFieldMatrix<>(testDataInv);
- FieldMatrix<Fraction> mPlusInv = new Array2DRowFieldMatrix<>(testDataPlusInv);
- FieldMatrix<Fraction> identity = new Array2DRowFieldMatrix<>(id);
+ FieldMatrix<Dfp> m = new Array2DRowFieldMatrix<>(testData);
+ FieldMatrix<Dfp> mInv = new Array2DRowFieldMatrix<>(testDataInv);
+ FieldMatrix<Dfp> mPlusInv = new Array2DRowFieldMatrix<>(testDataPlusInv);
+ FieldMatrix<Dfp> identity = new Array2DRowFieldMatrix<>(id);
TestUtils.assertEquals(m.power(0), identity);
TestUtils.assertEquals(mInv.power(0), identity);
@@ -220,9 +222,9 @@ public final class FieldMatrixImplTest {
TestUtils.assertEquals(mInv.power(1), mInv);
TestUtils.assertEquals(mPlusInv.power(1), mPlusInv);
- FieldMatrix<Fraction> C1 = m.copy();
- FieldMatrix<Fraction> C2 = mInv.copy();
- FieldMatrix<Fraction> C3 = mPlusInv.copy();
+ FieldMatrix<Dfp> C1 = m.copy();
+ FieldMatrix<Dfp> C2 = mInv.copy();
+ FieldMatrix<Dfp> C3 = mPlusInv.copy();
// stop at 5 to avoid overflow
for (int i = 2; i <= 5; ++i) {
@@ -236,7 +238,7 @@ public final class FieldMatrixImplTest {
}
try {
- FieldMatrix<Fraction> mNotSquare = new Array2DRowFieldMatrix<>(testData2T);
+ FieldMatrix<Dfp> mNotSquare = new Array2DRowFieldMatrix<>(testData2T);
mNotSquare.power(2);
Assert.fail("Expecting NonSquareMatrixException");
} catch (NonSquareMatrixException ex) {
@@ -254,8 +256,8 @@ public final class FieldMatrixImplTest {
/** test trace */
@Test
public void testTrace() {
- FieldMatrix<Fraction> m = new Array2DRowFieldMatrix<>(id);
- Assert.assertEquals("identity trace",new Fraction(3),m.getTrace());
+ FieldMatrix<Dfp> m = new Array2DRowFieldMatrix<>(id);
+ Assert.assertEquals("identity trace",Dfp25.of(3),m.getTrace());
m = new Array2DRowFieldMatrix<>(testData2);
try {
m.getTrace();
@@ -268,14 +270,14 @@ public final class FieldMatrixImplTest {
/** test sclarAdd */
@Test
public void testScalarAdd() {
- FieldMatrix<Fraction> m = new Array2DRowFieldMatrix<>(testData);
- TestUtils.assertEquals(new Array2DRowFieldMatrix<>(testDataPlus2), m.scalarAdd(new Fraction(2)));
+ FieldMatrix<Dfp> m = new Array2DRowFieldMatrix<>(testData);
+ TestUtils.assertEquals(new Array2DRowFieldMatrix<>(testDataPlus2), m.scalarAdd(Dfp25.of(2)));
}
/** test operate */
@Test
public void testOperate() {
- FieldMatrix<Fraction> m = new Array2DRowFieldMatrix<>(id);
+ FieldMatrix<Dfp> m = new Array2DRowFieldMatrix<>(id);
TestUtils.assertEquals(testVector, m.operate(testVector));
TestUtils.assertEquals(testVector, m.operate(new ArrayFieldVector<>(testVector)).toArray());
m = new Array2DRowFieldMatrix<>(bigSingular);
@@ -290,32 +292,32 @@ public final class FieldMatrixImplTest {
/** test issue MATH-209 */
@Test
public void testMath209() {
- FieldMatrix<Fraction> a = new Array2DRowFieldMatrix<>(new Fraction[][] {
- { new Fraction(1), new Fraction(2) }, { new Fraction(3), new Fraction(4) }, { new Fraction(5), new Fraction(6) }
+ FieldMatrix<Dfp> a = new Array2DRowFieldMatrix<>(new Dfp[][] {
+ { Dfp25.of(1), Dfp25.of(2) }, { Dfp25.of(3), Dfp25.of(4) }, { Dfp25.of(5), Dfp25.of(6) }
}, false);
- Fraction[] b = a.operate(new Fraction[] { new Fraction(1), new Fraction(1) });
+ Dfp[] b = a.operate(new Dfp[] { Dfp25.of(1), Dfp25.of(1) });
Assert.assertEquals(a.getRowDimension(), b.length);
- Assert.assertEquals( new Fraction(3), b[0]);
- Assert.assertEquals( new Fraction(7), b[1]);
- Assert.assertEquals(new Fraction(11), b[2]);
+ Assert.assertEquals( Dfp25.of(3), b[0]);
+ Assert.assertEquals( Dfp25.of(7), b[1]);
+ Assert.assertEquals(Dfp25.of(11), b[2]);
}
/** test transpose */
@Test
public void testTranspose() {
- FieldMatrix<Fraction> m = new Array2DRowFieldMatrix<>(testData);
- FieldMatrix<Fraction> mIT = new FieldLUDecomposition<>(m).getSolver().getInverse().transpose();
- FieldMatrix<Fraction> mTI = new FieldLUDecomposition<>(m.transpose()).getSolver().getInverse();
+ FieldMatrix<Dfp> m = new Array2DRowFieldMatrix<>(testData);
+ FieldMatrix<Dfp> mIT = new FieldLUDecomposition<>(m).getSolver().getInverse().transpose();
+ FieldMatrix<Dfp> mTI = new FieldLUDecomposition<>(m.transpose()).getSolver().getInverse();
TestUtils.assertEquals(mIT, mTI);
m = new Array2DRowFieldMatrix<>(testData2);
- FieldMatrix<Fraction> mt = new Array2DRowFieldMatrix<>(testData2T);
+ FieldMatrix<Dfp> mt = new Array2DRowFieldMatrix<>(testData2T);
TestUtils.assertEquals(mt, m.transpose());
}
/** test preMultiply by vector */
@Test
public void testPremultiplyVector() {
- FieldMatrix<Fraction> m = new Array2DRowFieldMatrix<>(testData);
+ FieldMatrix<Dfp> m = new Array2DRowFieldMatrix<>(testData);
TestUtils.assertEquals(m.preMultiply(testVector), preMultTest);
TestUtils.assertEquals(m.preMultiply(new ArrayFieldVector<>(testVector).toArray()),
preMultTest);
@@ -330,14 +332,14 @@ public final class FieldMatrixImplTest {
@Test
public void testPremultiply() {
- FieldMatrix<Fraction> m3 = new Array2DRowFieldMatrix<>(d3);
- FieldMatrix<Fraction> m4 = new Array2DRowFieldMatrix<>(d4);
- FieldMatrix<Fraction> m5 = new Array2DRowFieldMatrix<>(d5);
+ FieldMatrix<Dfp> m3 = new Array2DRowFieldMatrix<>(d3);
+ FieldMatrix<Dfp> m4 = new Array2DRowFieldMatrix<>(d4);
+ FieldMatrix<Dfp> m5 = new Array2DRowFieldMatrix<>(d5);
TestUtils.assertEquals(m4.preMultiply(m3), m5);
- Array2DRowFieldMatrix<Fraction> m = new Array2DRowFieldMatrix<>(testData);
- Array2DRowFieldMatrix<Fraction> mInv = new Array2DRowFieldMatrix<>(testDataInv);
- Array2DRowFieldMatrix<Fraction> identity = new Array2DRowFieldMatrix<>(id);
+ Array2DRowFieldMatrix<Dfp> m = new Array2DRowFieldMatrix<>(testData);
+ Array2DRowFieldMatrix<Dfp> mInv = new Array2DRowFieldMatrix<>(testDataInv);
+ Array2DRowFieldMatrix<Dfp> identity = new Array2DRowFieldMatrix<>(id);
TestUtils.assertEquals(m.preMultiply(mInv), identity);
TestUtils.assertEquals(mInv.preMultiply(m), identity);
TestUtils.assertEquals(m.preMultiply(identity), m);
@@ -352,7 +354,7 @@ public final class FieldMatrixImplTest {
@Test
public void testGetVectors() {
- FieldMatrix<Fraction> m = new Array2DRowFieldMatrix<>(testData);
+ FieldMatrix<Dfp> m = new Array2DRowFieldMatrix<>(testData);
TestUtils.assertEquals(m.getRow(0), testDataRow1);
TestUtils.assertEquals(m.getColumn(2), testDataCol3);
try {
@@ -371,8 +373,8 @@ public final class FieldMatrixImplTest {
@Test
public void testGetEntry() {
- FieldMatrix<Fraction> m = new Array2DRowFieldMatrix<>(testData);
- Assert.assertEquals("get entry", m.getEntry(0,1), new Fraction(2));
+ FieldMatrix<Dfp> m = new Array2DRowFieldMatrix<>(testData);
+ Assert.assertEquals("get entry", m.getEntry(0,1), Dfp25.of(2));
try {
m.getEntry(10, 4);
Assert.fail ("Expecting OutOfRangeException");
@@ -385,57 +387,63 @@ public final class FieldMatrixImplTest {
@Test
public void testExamples() {
// Create a real matrix with two rows and three columns
- Fraction[][] matrixData = {
- {new Fraction(1),new Fraction(2),new Fraction(3)},
- {new Fraction(2),new Fraction(5),new Fraction(3)}
+ Dfp[][] matrixData = {
+ {Dfp25.of(1),Dfp25.of(2),Dfp25.of(3)},
+ {Dfp25.of(2),Dfp25.of(5),Dfp25.of(3)}
};
- FieldMatrix<Fraction> m = new Array2DRowFieldMatrix<>(matrixData);
+ FieldMatrix<Dfp> m = new Array2DRowFieldMatrix<>(matrixData);
// One more with three rows, two columns
- Fraction[][] matrixData2 = {
- {new Fraction(1),new Fraction(2)},
- {new Fraction(2),new Fraction(5)},
- {new Fraction(1), new Fraction(7)}
+ Dfp[][] matrixData2 = {
+ {Dfp25.of(1),Dfp25.of(2)},
+ {Dfp25.of(2),Dfp25.of(5)},
+ {Dfp25.of(1), Dfp25.of(7)}
};
- FieldMatrix<Fraction> n = new Array2DRowFieldMatrix<>(matrixData2);
+ FieldMatrix<Dfp> n = new Array2DRowFieldMatrix<>(matrixData2);
// Now multiply m by n
- FieldMatrix<Fraction> p = m.multiply(n);
+ FieldMatrix<Dfp> p = m.multiply(n);
Assert.assertEquals(2, p.getRowDimension());
Assert.assertEquals(2, p.getColumnDimension());
// Invert p
- FieldMatrix<Fraction> pInverse = new FieldLUDecomposition<>(p).getSolver().getInverse();
+ FieldMatrix<Dfp> pInverse = new FieldLUDecomposition<>(p).getSolver().getInverse();
Assert.assertEquals(2, pInverse.getRowDimension());
Assert.assertEquals(2, pInverse.getColumnDimension());
// Solve example
- Fraction[][] coefficientsData = {
- {new Fraction(2), new Fraction(3), new Fraction(-2)},
- {new Fraction(-1), new Fraction(7), new Fraction(6)},
- {new Fraction(4), new Fraction(-3), new Fraction(-5)}
+ Dfp[][] coefficientsData = {
+ {Dfp25.of(2), Dfp25.of(3), Dfp25.of(-2)},
+ {Dfp25.of(-1), Dfp25.of(7), Dfp25.of(6)},
+ {Dfp25.of(4), Dfp25.of(-3), Dfp25.of(-5)}
};
- FieldMatrix<Fraction> coefficients = new Array2DRowFieldMatrix<>(coefficientsData);
- Fraction[] constants = {
- new Fraction(1), new Fraction(-2), new Fraction(1)
+ FieldMatrix<Dfp> coefficients = new Array2DRowFieldMatrix<>(coefficientsData);
+ Dfp[] constants = {
+ Dfp25.of(1), Dfp25.of(-2), Dfp25.of(1)
};
- Fraction[] solution;
+ Dfp[] solution;
solution = new FieldLUDecomposition<>(coefficients)
.getSolver()
.solve(new ArrayFieldVector<>(constants, false)).toArray();
- Assert.assertEquals(new Fraction(2).multiply(solution[0]).
- add(new Fraction(3).multiply(solution[1])).
- subtract(new Fraction(2).multiply(solution[2])), constants[0]);
- Assert.assertEquals(new Fraction(-1).multiply(solution[0]).
- add(new Fraction(7).multiply(solution[1])).
- add(new Fraction(6).multiply(solution[2])), constants[1]);
- Assert.assertEquals(new Fraction(4).multiply(solution[0]).
- subtract(new Fraction(3).multiply(solution[1])).
- subtract(new Fraction(5).multiply(solution[2])), constants[2]);
+ Assert.assertEquals(Dfp25.of(2).multiply(solution[0]).
+ add(Dfp25.of(3).multiply(solution[1])).
+ subtract(Dfp25.of(2).multiply(solution[2])).toDouble(),
+ constants[0].toDouble(),
+ 0d);
+ Assert.assertEquals(Dfp25.of(-1).multiply(solution[0]).
+ add(Dfp25.of(7).multiply(solution[1])).
+ add(Dfp25.of(6).multiply(solution[2])).toDouble(),
+ constants[1].toDouble(),
+ 0d);
+ Assert.assertEquals(Dfp25.of(4).multiply(solution[0]).
+ subtract(Dfp25.of(3).multiply(solution[1])).
+ subtract(Dfp25.of(5).multiply(solution[2])).toDouble(),
+ constants[2].toDouble(),
+ 0d);
}
// test submatrix accessors
@Test
public void testGetSubMatrix() {
- FieldMatrix<Fraction> m = new Array2DRowFieldMatrix<>(subTestData);
+ FieldMatrix<Dfp> m = new Array2DRowFieldMatrix<>(subTestData);
checkGetSubMatrix(m, subRows23Cols00, 2 , 3 , 0, 0);
checkGetSubMatrix(m, subRows00Cols33, 0 , 0 , 3, 3);
checkGetSubMatrix(m, subRows01Cols23, 0 , 1 , 2, 3);
@@ -453,10 +461,10 @@ public final class FieldMatrixImplTest {
checkGetSubMatrix(m, null, new int[] { 0 }, new int[] { 4 });
}
- private void checkGetSubMatrix(FieldMatrix<Fraction> m, Fraction[][] reference,
+ private void checkGetSubMatrix(FieldMatrix<Dfp> m, Dfp[][] reference,
int startRow, int endRow, int startColumn, int endColumn) {
try {
- FieldMatrix<Fraction> sub = m.getSubMatrix(startRow, endRow, startColumn, endColumn);
+ FieldMatrix<Dfp> sub = m.getSubMatrix(startRow, endRow, startColumn, endColumn);
if (reference != null) {
Assert.assertEquals(new Array2DRowFieldMatrix<>(reference), sub);
} else {
@@ -482,10 +490,10 @@ public final class FieldMatrixImplTest {
}
}
- private void checkGetSubMatrix(FieldMatrix<Fraction> m, Fraction[][] reference,
+ private void checkGetSubMatrix(FieldMatrix<Dfp> m, Dfp[][] reference,
int[] selectedRows, int[] selectedColumns) {
try {
- FieldMatrix<Fraction> sub = m.getSubMatrix(selectedRows, selectedColumns);
+ FieldMatrix<Dfp> sub = m.getSubMatrix(selectedRows, selectedColumns);
if (reference != null) {
Assert.assertEquals(new Array2DRowFieldMatrix<>(reference), sub);
} else {
@@ -513,7 +521,7 @@ public final class FieldMatrixImplTest {
@Test
public void testCopySubMatrix() {
- FieldMatrix<Fraction> m = new Array2DRowFieldMatrix<>(subTestData);
+ FieldMatrix<Dfp> m = new Array2DRowFieldMatrix<>(subTestData);
checkCopy(m, subRows23Cols00, 2 , 3 , 0, 0);
checkCopy(m, subRows00Cols33, 0 , 0 , 3, 3);
checkCopy(m, subRows01Cols23, 0 , 1 , 2, 3);
@@ -532,12 +540,12 @@ public final class FieldMatrixImplTest {
checkCopy(m, null, new int[] { 0 }, new int[] { 4 });
}
- private void checkCopy(FieldMatrix<Fraction> m, Fraction[][] reference,
+ private void checkCopy(FieldMatrix<Dfp> m, Dfp[][] reference,
int startRow, int endRow, int startColumn, int endColumn) {
try {
- Fraction[][] sub = (reference == null) ?
- new Fraction[1][1] :
- new Fraction[reference.length][reference[0].length];
+ Dfp[][] sub = (reference == null) ?
+ new Dfp[1][1] :
+ new Dfp[reference.length][reference[0].length];
m.copySubMatrix(startRow, endRow, startColumn, endColumn, sub);
if (reference != null) {
Assert.assertEquals(new Array2DRowFieldMatrix<>(reference), new Array2DRowFieldMatrix<>(sub));
@@ -559,12 +567,12 @@ public final class FieldMatrixImplTest {
}
}
- private void checkCopy(FieldMatrix<Fraction> m, Fraction[][] reference,
+ private void checkCopy(FieldMatrix<Dfp> m, Dfp[][] reference,
int[] selectedRows, int[] selectedColumns) {
try {
- Fraction[][] sub = (reference == null) ?
- new Fraction[1][1] :
- new Fraction[reference.length][reference[0].length];
+ Dfp[][] sub = (reference == null) ?
+ new Dfp[1][1] :
+ new Dfp[reference.length][reference[0].length];
m.copySubMatrix(selectedRows, selectedColumns, sub);
if (reference != null) {
Assert.assertEquals(new Array2DRowFieldMatrix<>(reference), new Array2DRowFieldMatrix<>(sub));
@@ -588,9 +596,9 @@ public final class FieldMatrixImplTest {
@Test
public void testGetRowMatrix() {
- FieldMatrix<Fraction> m = new Array2DRowFieldMatrix<>(subTestData);
- FieldMatrix<Fraction> mRow0 = new Array2DRowFieldMatrix<>(subRow0);
- FieldMatrix<Fraction> mRow3 = new Array2DRowFieldMatrix<>(subRow3);
+ FieldMatrix<Dfp> m = new Array2DRowFieldMatrix<>(subTestData);
+ FieldMatrix<Dfp> mRow0 = new Array2DRowFieldMatrix<>(subRow0);
+ FieldMatrix<Dfp> mRow3 = new Array2DRowFieldMatrix<>(subRow3);
Assert.assertEquals("Row0", mRow0,
m.getRowMatrix(0));
Assert.assertEquals("Row3", mRow3,
@@ -611,8 +619,8 @@ public final class FieldMatrixImplTest {
@Test
public void testSetRowMatrix() {
- FieldMatrix<Fraction> m = new Array2DRowFieldMatrix<>(subTestData);
- FieldMatrix<Fraction> mRow3 = new Array2DRowFieldMatrix<>(subRow3);
+ FieldMatrix<Dfp> m = new Array2DRowFieldMatrix<>(subTestData);
+ FieldMatrix<Dfp> mRow3 = new Array2DRowFieldMatrix<>(subRow3);
Assert.assertNotSame(mRow3, m.getRowMatrix(0));
m.setRowMatrix(0, mRow3);
Assert.assertEquals(mRow3, m.getRowMatrix(0));
@@ -632,9 +640,9 @@ public final class FieldMatrixImplTest {
@Test
public void testGetColumnMatrix() {
- FieldMatrix<Fraction> m = new Array2DRowFieldMatrix<>(subTestData);
- FieldMatrix<Fraction> mColumn1 = new Array2DRowFieldMatrix<>(subColumn1);
- FieldMatrix<Fraction> mColumn3 = new Array2DRowFieldMatrix<>(subColumn3);
+ FieldMatrix<Dfp> m = new Array2DRowFieldMatrix<>(subTestData);
+ FieldMatrix<Dfp> mColumn1 = new Array2DRowFieldMatrix<>(subColumn1);
+ FieldMatrix<Dfp> mColumn3 = new Array2DRowFieldMatrix<>(subColumn3);
Assert.assertEquals("Column1", mColumn1,
m.getColumnMatrix(1));
Assert.assertEquals("Column3", mColumn3,
@@ -655,8 +663,8 @@ public final class FieldMatrixImplTest {
@Test
public void testSetColumnMatrix() {
- FieldMatrix<Fraction> m = new Array2DRowFieldMatrix<>(subTestData);
- FieldMatrix<Fraction> mColumn3 = new Array2DRowFieldMatrix<>(subColumn3);
+ FieldMatrix<Dfp> m = new Array2DRowFieldMatrix<>(subTestData);
+ FieldMatrix<Dfp> mColumn3 = new Array2DRowFieldMatrix<>(subColumn3);
Assert.assertNotSame(mColumn3, m.getColumnMatrix(1));
m.setColumnMatrix(1, mColumn3);
Assert.assertEquals(mColumn3, m.getColumnMatrix(1));
@@ -676,9 +684,9 @@ public final class FieldMatrixImplTest {
@Test
public void testGetRowVector() {
- FieldMatrix<Fraction> m = new Array2DRowFieldMatrix<>(subTestData);
- FieldVector<Fraction> mRow0 = new ArrayFieldVector<>(subRow0[0]);
- FieldVector<Fraction> mRow3 = new ArrayFieldVector<>(subRow3[0]);
+ FieldMatrix<Dfp> m = new Array2DRowFieldMatrix<>(subTestData);
+ FieldVector<Dfp> mRow0 = new ArrayFieldVector<>(subRow0[0]);
+ FieldVector<Dfp> mRow3 = new ArrayFieldVector<>(subRow3[0]);
Assert.assertEquals("Row0", mRow0, m.getRowVector(0));
Assert.assertEquals("Row3", mRow3, m.getRowVector(3));
try {
@@ -697,8 +705,8 @@ public final class FieldMatrixImplTest {
@Test
public void testSetRowVector() {
- FieldMatrix<Fraction> m = new Array2DRowFieldMatrix<>(subTestData);
- FieldVector<Fraction> mRow3 = new ArrayFieldVector<>(subRow3[0]);
+ FieldMatrix<Dfp> m = new Array2DRowFieldMatrix<>(subTestData);
+ FieldVector<Dfp> mRow3 = new ArrayFieldVector<>(subRow3[0]);
Assert.assertNotSame(mRow3, m.getRowMatrix(0));
m.setRowVector(0, mRow3);
Assert.assertEquals(mRow3, m.getRowVector(0));
@@ -709,7 +717,7 @@ public final class FieldMatrixImplTest {
// expected
}
try {
- m.setRowVector(0, new ArrayFieldVector<>(FractionField.getInstance(), 5));
+ m.setRowVector(0, new ArrayFieldVector<>(Dfp25.getField(), 5));
Assert.fail("Expecting MatrixDimensionMismatchException");
} catch (MatrixDimensionMismatchException ex) {
// expected
@@ -718,9 +726,9 @@ public final class FieldMatrixImplTest {
@Test
public void testGetColumnVector() {
- FieldMatrix<Fraction> m = new Array2DRowFieldMatrix<>(subTestData);
- FieldVector<Fraction> mColumn1 = columnToVector(subColumn1);
- FieldVector<Fraction> mColumn3 = columnToVector(subColumn3);
+ FieldMatrix<Dfp> m = new Array2DRowFieldMatrix<>(subTestData);
+ FieldVector<Dfp> mColumn1 = columnToVector(subColumn1);
+ FieldVector<Dfp> mColumn3 = columnToVector(subColumn3);
Assert.assertEquals("Column1", mColumn1, m.getColumnVector(1));
Assert.assertEquals("Column3", mColumn3, m.getColumnVector(3));
try {
@@ -739,8 +747,8 @@ public final class FieldMatrixImplTest {
@Test
public void testSetColumnVector() {
- FieldMatrix<Fraction> m = new Array2DRowFieldMatrix<>(subTestData);
- FieldVector<Fraction> mColumn3 = columnToVector(subColumn3);
+ FieldMatrix<Dfp> m = new Array2DRowFieldMatrix<>(subTestData);
+ FieldVector<Dfp> mColumn3 = columnToVector(subColumn3);
Assert.assertNotSame(mColumn3, m.getColumnVector(1));
m.setColumnVector(1, mColumn3);
Assert.assertEquals(mColumn3, m.getColumnVector(1));
@@ -751,15 +759,15 @@ public final class FieldMatrixImplTest {
// expected
}
try {
- m.setColumnVector(0, new ArrayFieldVector<>(FractionField.getInstance(), 5));
+ m.setColumnVector(0, new ArrayFieldVector<>(Dfp25.getField(), 5));
Assert.fail("Expecting MatrixDimensionMismatchException");
} catch (MatrixDimensionMismatchException ex) {
// expected
}
}
- private FieldVector<Fraction> columnToVector(Fraction[][] column) {
- Fraction[] data = new Fraction[column.length];
+ private FieldVector<Dfp> columnToVector(Dfp[][] column) {
+ Dfp[] data = new Dfp[column.length];
for (int i = 0; i < data.length; ++i) {
data[i] = column[i][0];
}
@@ -768,7 +776,7 @@ public final class FieldMatrixImplTest {
@Test
public void testGetRow() {
- FieldMatrix<Fraction> m = new Array2DRowFieldMatrix<>(subTestData);
+ FieldMatrix<Dfp> m = new Array2DRowFieldMatrix<>(subTestData);
checkArrays(subRow0[0], m.getRow(0));
checkArrays(subRow3[0], m.getRow(3));
try {
@@ -787,7 +795,7 @@ public final class FieldMatrixImplTest {
@Test
public void testSetRow() {
- FieldMatrix<Fraction> m = new Array2DRowFieldMatrix<>(subTestData);
+ FieldMatrix<Dfp> m = new Array2DRowFieldMatrix<>(subTestData);
Assert.assertTrue(subRow3[0][0] != m.getRow(0)[0]);
m.setRow(0, subRow3[0]);
checkArrays(subRow3[0], m.getRow(0));
@@ -798,7 +806,7 @@ public final class FieldMatrixImplTest {
// expected
}
try {
- m.setRow(0, new Fraction[5]);
+ m.setRow(0, new Dfp[5]);
Assert.fail("Expecting MatrixDimensionMismatchException");
} catch (MatrixDimensionMismatchException ex) {
// expected
@@ -807,9 +815,9 @@ public final class FieldMatrixImplTest {
@Test
public void testGetColumn() {
- FieldMatrix<Fraction> m = new Array2DRowFieldMatrix<>(subTestData);
- Fraction[] mColumn1 = columnToArray(subColumn1);
- Fraction[] mColumn3 = columnToArray(subColumn3);
+ FieldMatrix<Dfp> m = new Array2DRowFieldMatrix<>(subTestData);
+ Dfp[] mColumn1 = columnToArray(subColumn1);
+ Dfp[] mColumn3 = columnToArray(subColumn3);
checkArrays(mColumn1, m.getColumn(1));
checkArrays(mColumn3, m.getColumn(3));
try {
@@ -828,8 +836,8 @@ public final class FieldMatrixImplTest {
@Test
public void testSetColumn() {
- FieldMatrix<Fraction> m = new Array2DRowFieldMatrix<>(subTestData);
- Fraction[] mColumn3 = columnToArray(subColumn3);
+ FieldMatrix<Dfp> m = new Array2DRowFieldMatrix<>(subTestData);
+ Dfp[] mColumn3 = columnToArray(subColumn3);
Assert.assertTrue(mColumn3[0] != m.getColumn(1)[0]);
m.setColumn(1, mColumn3);
checkArrays(mColumn3, m.getColumn(1));
@@ -840,22 +848,22 @@ public final class FieldMatrixImplTest {
// expected
}
try {
- m.setColumn(0, new Fraction[5]);
+ m.setColumn(0, new Dfp[5]);
Assert.fail("Expecting MatrixDimensionMismatchException");
} catch (MatrixDimensionMismatchException ex) {
// expected
}
}
- private Fraction[] columnToArray(Fraction[][] column) {
- Fraction[] data = new Fraction[column.length];
+ private Dfp[] columnToArray(Dfp[][] column) {
+ Dfp[] data = new Dfp[column.length];
for (int i = 0; i < data.length; ++i) {
data[i] = column[i][0];
}
return data;
}
- private void checkArrays(Fraction[] expected, Fraction[] actual) {
+ private void checkArrays(Dfp[] expected, Dfp[] actual) {
Assert.assertEquals(expected.length, actual.length);
for (int i = 0; i < expected.length; ++i) {
Assert.assertEquals(expected[i], actual[i]);
@@ -864,9 +872,9 @@ public final class FieldMatrixImplTest {
@Test
public void testEqualsAndHashCode() {
- Array2DRowFieldMatrix<Fraction> m = new Array2DRowFieldMatrix<>(testData);
- Array2DRowFieldMatrix<Fraction> m1 = (Array2DRowFieldMatrix<Fraction>) m.copy();
- Array2DRowFieldMatrix<Fraction> mt = (Array2DRowFieldMatrix<Fraction>) m.transpose();
+ Array2DRowFieldMatrix<Dfp> m = new Array2DRowFieldMatrix<>(testData);
+ Array2DRowFieldMatrix<Dfp> m1 = (Array2DRowFieldMatrix<Dfp>) m.copy();
+ Array2DRowFieldMatrix<Dfp> mt = (Array2DRowFieldMatrix<Dfp>) m.transpose();
Assert.assertTrue(m.hashCode() != mt.hashCode());
Assert.assertEquals(m.hashCode(), m1.hashCode());
Assert.assertEquals(m, m);
@@ -878,39 +886,39 @@ public final class FieldMatrixImplTest {
@Test
public void testToString() {
- Array2DRowFieldMatrix<Fraction> m = new Array2DRowFieldMatrix<>(testData);
- Assert.assertEquals("Array2DRowFieldMatrix{{1,2,3},{2,5,3},{1,0,8}}", m.toString());
- m = new Array2DRowFieldMatrix<>(FractionField.getInstance());
+ Array2DRowFieldMatrix<Dfp> m = new Array2DRowFieldMatrix<>(testData);
+ Assert.assertEquals("Array2DRowFieldMatrix{{1.,2.,3.},{2.,5.,3.},{1.,0.,8.}}", m.toString());
+ m = new Array2DRowFieldMatrix<>(Dfp25.getField());
Assert.assertEquals("Array2DRowFieldMatrix{}", m.toString());
}
@Test
public void testSetSubMatrix() {
- Array2DRowFieldMatrix<Fraction> m = new Array2DRowFieldMatrix<>(testData);
+ Array2DRowFieldMatrix<Dfp> m = new Array2DRowFieldMatrix<>(testData);
m.setSubMatrix(detData2,1,1);
- FieldMatrix<Fraction> expected = new Array2DRowFieldMatrix<>
- (new Fraction[][] {
- {new Fraction(1),new Fraction(2),new Fraction(3)},
- {new Fraction(2),new Fraction(1),new Fraction(3)},
- {new Fraction(1),new Fraction(2),new Fraction(4)}
+ FieldMatrix<Dfp> expected = new Array2DRowFieldMatrix<>
+ (new Dfp[][] {
+ {Dfp25.of(1),Dfp25.of(2),Dfp25.of(3)},
+ {Dfp25.of(2),Dfp25.of(1),Dfp25.of(3)},
+ {Dfp25.of(1),Dfp25.of(2),Dfp25.of(4)}
});
Assert.assertEquals(expected, m);
m.setSubMatrix(detData2,0,0);
expected = new Array2DRowFieldMatrix<>
- (new Fraction[][] {
- {new Fraction(1),new Fraction(3),new Fraction(3)},
- {new Fraction(2),new Fraction(4),new Fraction(3)},
- {new Fraction(1),new Fraction(2),new Fraction(4)}
+ (new Dfp[][] {
+ {Dfp25.of(1),Dfp25.of(3),Dfp25.of(3)},
+ {Dfp25.of(2),Dfp25.of(4),Dfp25.of(3)},
+ {Dfp25.of(1),Dfp25.of(2),Dfp25.of(4)}
});
Assert.assertEquals(expected, m);
m.setSubMatrix(testDataPlus2,0,0);
expected = new Array2DRowFieldMatrix<>
- (new Fraction[][] {
- {new Fraction(3),new Fraction(4),new Fraction(5)},
- {new Fraction(4),new Fraction(7),new Fraction(5)},
- {new Fraction(3),new Fraction(2),new Fraction(10)}
+ (new Dfp[][] {
+ {Dfp25.of(3),Dfp25.of(4),Dfp25.of(5)},
+ {Dfp25.of(4),Dfp25.of(7),Dfp25.of(5)},
+ {Dfp25.of(3),Dfp25.of(2),Dfp25.of(10)}
});
Assert.assertEquals(expected, m);
@@ -942,7 +950,7 @@ public final class FieldMatrixImplTest {
} catch (NullArgumentException e) {
// expected
}
- Array2DRowFieldMatrix<Fraction> m2 = new Array2DRowFieldMatrix<>(FractionField.getInstance());
+ Array2DRowFieldMatrix<Dfp> m2 = new Array2DRowFieldMatrix<>(Dfp25.getField());
try {
m2.setSubMatrix(testData,0,1);
Assert.fail("expecting MathIllegalStateException");
@@ -958,7 +966,7 @@ public final class FieldMatrixImplTest {
// ragged
try {
- m.setSubMatrix(new Fraction[][] {{new Fraction(1)}, {new Fraction(2), new Fraction(3)}}, 0, 0);
+ m.setSubMatrix(new Dfp[][] {{Dfp25.of(1)}, {Dfp25.of(2), Dfp25.of(3)}}, 0, 0);
Assert.fail("expecting MathIllegalArgumentException");
} catch (MathIllegalArgumentException e) {
// expected
@@ -966,7 +974,7 @@ public final class FieldMatrixImplTest {
// empty
try {
- m.setSubMatrix(new Fraction[][] {{}}, 0, 0);
+ m.setSubMatrix(new Dfp[][] {{}}, 0, 0);
Assert.fail("expecting MathIllegalArgumentException");
} catch (MathIllegalArgumentException e) {
// expected
@@ -979,114 +987,121 @@ public final class FieldMatrixImplTest {
int rows = 150;
int columns = 75;
- FieldMatrix<Fraction> m =
- new Array2DRowFieldMatrix<>(FractionField.getInstance(), rows, columns);
+ FieldMatrix<Dfp> m =
+ new Array2DRowFieldMatrix<>(Dfp25.getField(), rows, columns);
m.walkInRowOrder(new SetVisitor());
GetVisitor getVisitor = new GetVisitor();
m.walkInOptimizedOrder(getVisitor);
Assert.assertEquals(rows * columns, getVisitor.getCount());
- m = new Array2DRowFieldMatrix<>(FractionField.getInstance(), rows, columns);
+ m = new Array2DRowFieldMatrix<>(Dfp25.getField(), rows, columns);
m.walkInRowOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2);
getVisitor = new GetVisitor();
m.walkInOptimizedOrder(getVisitor, 1, rows - 2, 1, columns - 2);
Assert.assertEquals((rows - 2) * (columns - 2), getVisitor.getCount());
for (int i = 0; i < rows; ++i) {
- Assert.assertEquals(new Fraction(0), m.getEntry(i, 0));
- Assert.assertEquals(new Fraction(0), m.getEntry(i, columns - 1));
+ Assert.assertEquals(Dfp25.of(0), m.getEntry(i, 0));
+ Assert.assertEquals(Dfp25.of(0), m.getEntry(i, columns - 1));
}
for (int j = 0; j < columns; ++j) {
- Assert.assertEquals(new Fraction(0), m.getEntry(0, j));
- Assert.assertEquals(new Fraction(0), m.getEntry(rows - 1, j));
+ Assert.assertEquals(Dfp25.of(0), m.getEntry(0, j));
+ Assert.assertEquals(Dfp25.of(0), m.getEntry(rows - 1, j));
}
- m = new Array2DRowFieldMatrix<>(FractionField.getInstance(), rows, columns);
+ m = new Array2DRowFieldMatrix<>(Dfp25.getField(), rows, columns);
m.walkInColumnOrder(new SetVisitor());
getVisitor = new GetVisitor();
m.walkInOptimizedOrder(getVisitor);
Assert.assertEquals(rows * columns, getVisitor.getCount());
- m = new Array2DRowFieldMatrix<>(FractionField.getInstance(), rows, columns);
+ m = new Array2DRowFieldMatrix<>(Dfp25.getField(), rows, columns);
m.walkInColumnOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2);
getVisitor = new GetVisitor();
m.walkInOptimizedOrder(getVisitor, 1, rows - 2, 1, columns - 2);
Assert.assertEquals((rows - 2) * (columns - 2), getVisitor.getCount());
for (int i = 0; i < rows; ++i) {
- Assert.assertEquals(new Fraction(0), m.getEntry(i, 0));
- Assert.assertEquals(new Fraction(0), m.getEntry(i, columns - 1));
+ Assert.assertEquals(Dfp25.of(0), m.getEntry(i, 0));
+ Assert.assertEquals(Dfp25.of(0), m.getEntry(i, columns - 1));
}
for (int j = 0; j < columns; ++j) {
- Assert.assertEquals(new Fraction(0), m.getEntry(0, j));
- Assert.assertEquals(new Fraction(0), m.getEntry(rows - 1, j));
+ Assert.assertEquals(Dfp25.of(0), m.getEntry(0, j));
+ Assert.assertEquals(Dfp25.of(0), m.getEntry(rows - 1, j));
}
- m = new Array2DRowFieldMatrix<>(FractionField.getInstance(), rows, columns);
+ m = new Array2DRowFieldMatrix<>(Dfp25.getField(), rows, columns);
m.walkInOptimizedOrder(new SetVisitor());
getVisitor = new GetVisitor();
m.walkInRowOrder(getVisitor);
Assert.assertEquals(rows * columns, getVisitor.getCount());
- m = new Array2DRowFieldMatrix<>(FractionField.getInstance(), rows, columns);
+ m = new Array2DRowFieldMatrix<>(Dfp25.getField(), rows, columns);
m.walkInOptimizedOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2);
getVisitor = new GetVisitor();
m.walkInRowOrder(getVisitor, 1, rows - 2, 1, columns - 2);
Assert.assertEquals((rows - 2) * (columns - 2), getVisitor.getCount());
for (int i = 0; i < rows; ++i) {
- Assert.assertEquals(new Fraction(0), m.getEntry(i, 0));
- Assert.assertEquals(new Fraction(0), m.getEntry(i, columns - 1));
+ Assert.assertEquals(Dfp25.of(0), m.getEntry(i, 0));
+ Assert.assertEquals(Dfp25.of(0), m.getEntry(i, columns - 1));
}
for (int j = 0; j < columns; ++j) {
- Assert.assertEquals(new Fraction(0), m.getEntry(0, j));
- Assert.assertEquals(new Fraction(0), m.getEntry(rows - 1, j));
+ Assert.assertEquals(Dfp25.of(0), m.getEntry(0, j));
+ Assert.assertEquals(Dfp25.of(0), m.getEntry(rows - 1, j));
}
- m = new Array2DRowFieldMatrix<>(FractionField.getInstance(), rows, columns);
+ m = new Array2DRowFieldMatrix<>(Dfp25.getField(), rows, columns);
m.walkInOptimizedOrder(new SetVisitor());
getVisitor = new GetVisitor();
m.walkInColumnOrder(getVisitor);
Assert.assertEquals(rows * columns, getVisitor.getCount());
- m = new Array2DRowFieldMatrix<>(FractionField.getInstance(), rows, columns);
+ m = new Array2DRowFieldMatrix<>(Dfp25.getField(), rows, columns);
m.walkInOptimizedOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2);
getVisitor = new GetVisitor();
m.walkInColumnOrder(getVisitor, 1, rows - 2, 1, columns - 2);
Assert.assertEquals((rows - 2) * (columns - 2), getVisitor.getCount());
for (int i = 0; i < rows; ++i) {
- Assert.assertEquals(new Fraction(0), m.getEntry(i, 0));
- Assert.assertEquals(new Fraction(0), m.getEntry(i, columns - 1));
+ Assert.assertEquals(Dfp25.of(0), m.getEntry(i, 0));
+ Assert.assertEquals(Dfp25.of(0), m.getEntry(i, columns - 1));
}
for (int j = 0; j < columns; ++j) {
- Assert.assertEquals(new Fraction(0), m.getEntry(0, j));
- Assert.assertEquals(new Fraction(0), m.getEntry(rows - 1, j));
+ Assert.assertEquals(Dfp25.of(0), m.getEntry(0, j));
+ Assert.assertEquals(Dfp25.of(0), m.getEntry(rows - 1, j));
}
}
@Test
public void testSerial() {
- Array2DRowFieldMatrix<Fraction> m = new Array2DRowFieldMatrix<>(testData);
+ final int r = 2;
+ final int c = 3;
+ Array2DRowFieldMatrix<BigReal> m = new Array2DRowFieldMatrix<>(BigRealField.getInstance(), r, c);
+ for (int i = 0; i < r; i++) {
+ for (int j = 0; j < c; j++) {
+ m.setEntry(i, j, new BigReal(Math.random()));
+ }
+ }
Assert.assertEquals(m,TestUtils.serializeAndRecover(m));
}
- private static class SetVisitor extends DefaultFieldMatrixChangingVisitor<Fraction> {
+ private static class SetVisitor extends DefaultFieldMatrixChangingVisitor<Dfp> {
public SetVisitor() {
- super(Fraction.ZERO);
+ super(Dfp25.ZERO);
}
@Override
- public Fraction visit(int i, int j, Fraction value) {
- return new Fraction(i * 1024 + j, 1024);
+ public Dfp visit(int i, int j, Dfp value) {
+ return Dfp25.of(i * 1024 + j, 1024);
}
}
- private static class GetVisitor extends DefaultFieldMatrixPreservingVisitor<Fraction> {
+ private static class GetVisitor extends DefaultFieldMatrixPreservingVisitor<Dfp> {
private int count;
public GetVisitor() {
- super(Fraction.ZERO);
+ super(Dfp25.ZERO);
count = 0;
}
@Override
- public void visit(int i, int j, Fraction value) {
+ public void visit(int i, int j, Dfp value) {
++count;
- Assert.assertEquals(new Fraction(i * 1024 + j, 1024), value);
+ Assert.assertEquals(Dfp25.of(i * 1024 + j, 1024), value);
}
public int getCount() {
return count;
@@ -1096,9 +1111,9 @@ public final class FieldMatrixImplTest {
//--------------- -----------------Protected methods
/** extracts the l and u matrices from compact lu representation */
- protected void splitLU(FieldMatrix<Fraction> lu,
- Fraction[][] lowerData,
- Fraction[][] upperData) {
+ protected void splitLU(FieldMatrix<Dfp> lu,
+ Dfp[][] lowerData,
+ Dfp[][] upperData) {
if (!lu.isSquare()) {
throw new NonSquareMatrixException(lu.getRowDimension(), lu.getColumnDimension());
}
@@ -1119,12 +1134,12 @@ public final class FieldMatrixImplTest {
for (int j = 0; j < n; j++) {
if (j < i) {
lowerData[i][j] = lu.getEntry(i, j);
- upperData[i][j] = Fraction.ZERO;
+ upperData[i][j] = Dfp25.ZERO;
} else if (i == j) {
- lowerData[i][j] = Fraction.ONE;
+ lowerData[i][j] = Dfp25.ONE;
upperData[i][j] = lu.getEntry(i, j);
} else {
- lowerData[i][j] = Fraction.ZERO;
+ lowerData[i][j] = Dfp25.ZERO;
upperData[i][j] = lu.getEntry(i, j);
}
}
@@ -1132,7 +1147,7 @@ public final class FieldMatrixImplTest {
}
/** Returns the result of applying the given row permutation to the matrix */
- protected FieldMatrix<Fraction> permuteRows(FieldMatrix<Fraction> matrix, int[] permutation) {
+ protected FieldMatrix<Dfp> permuteRows(FieldMatrix<Dfp> matrix, int[] permutation) {
if (!matrix.isSquare()) {
throw new NonSquareMatrixException(matrix.getRowDimension(),
matrix.getColumnDimension());
@@ -1142,7 +1157,7 @@ public final class FieldMatrixImplTest {
}
int n = matrix.getRowDimension();
int m = matrix.getColumnDimension();
- Fraction out[][] = new Fraction[m][n];
+ Dfp out[][] = new Dfp[m][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
out[i][j] = matrix.getEntry(permutation[i], j);
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 c8bd3f9..d0a4931 100644
--- a/src/test/java/org/apache/commons/math4/linear/MatrixUtilsTest.java
+++ b/src/test/java/org/apache/commons/math4/linear/MatrixUtilsTest.java
@@ -23,9 +23,9 @@ 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.fraction.Fraction;
-import org.apache.commons.math4.fraction.FractionConversionException;
-import org.apache.commons.math4.fraction.FractionField;
+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.linear.Array2DRowFieldMatrix;
import org.apache.commons.math4.linear.Array2DRowRealMatrix;
import org.apache.commons.math4.linear.ArrayRealVector;
@@ -56,27 +56,27 @@ public final class MatrixUtilsTest {
protected BigDecimal[] bigRow =
{new BigDecimal(1),new BigDecimal(2),new BigDecimal(3)};
protected String[] stringRow = {"1", "2", "3"};
- protected Fraction[] fractionRow =
- {new Fraction(1),new Fraction(2),new Fraction(3)};
+ protected Dfp[] fractionRow =
+ {Dfp25.of(1),Dfp25.of(2),Dfp25.of(3)};
protected double[][] rowMatrix = {{1,2,3}};
protected BigDecimal[][] bigRowMatrix =
{{new BigDecimal(1), new BigDecimal(2), new BigDecimal(3)}};
protected String[][] stringRowMatrix = {{"1", "2", "3"}};
- protected Fraction[][] fractionRowMatrix =
- {{new Fraction(1), new Fraction(2), new Fraction(3)}};
+ protected Dfp[][] fractionRowMatrix =
+ {{Dfp25.of(1), Dfp25.of(2), Dfp25.of(3)}};
protected double[] col = {0,4,6};
protected BigDecimal[] bigCol =
{new BigDecimal(0),new BigDecimal(4),new BigDecimal(6)};
protected String[] stringCol = {"0","4","6"};
- protected Fraction[] fractionCol =
- {new Fraction(0),new Fraction(4),new Fraction(6)};
+ protected Dfp[] fractionCol =
+ {Dfp25.of(0),Dfp25.of(4),Dfp25.of(6)};
protected double[] nullDoubleArray = null;
protected double[][] colMatrix = {{0},{4},{6}};
protected BigDecimal[][] bigColMatrix =
{{new BigDecimal(0)},{new BigDecimal(4)},{new BigDecimal(6)}};
protected String[][] stringColMatrix = {{"0"}, {"4"}, {"6"}};
- protected Fraction[][] fractionColMatrix =
- {{new Fraction(0)},{new Fraction(4)},{new Fraction(6)}};
+ protected Dfp[][] fractionColMatrix =
+ {{Dfp25.of(0)},{Dfp25.of(4)},{Dfp25.of(6)}};
@Test
public void testCreateRealMatrix() {
@@ -104,24 +104,24 @@ public final class MatrixUtilsTest {
@Test
public void testcreateFieldMatrix() {
- Assert.assertEquals(new Array2DRowFieldMatrix<>(asFraction(testData)),
- MatrixUtils.createFieldMatrix(asFraction(testData)));
- Assert.assertEquals(new Array2DRowFieldMatrix<>(FractionField.getInstance(), fractionColMatrix),
+ Assert.assertEquals(new Array2DRowFieldMatrix<>(asDfp(testData)),
+ MatrixUtils.createFieldMatrix(asDfp(testData)));
+ Assert.assertEquals(new Array2DRowFieldMatrix<>(Dfp25.getField(), fractionColMatrix),
MatrixUtils.createFieldMatrix(fractionColMatrix));
try {
- MatrixUtils.createFieldMatrix(asFraction(new double[][] {{1}, {1,2}})); // ragged
+ MatrixUtils.createFieldMatrix(asDfp(new double[][] {{1}, {1,2}})); // ragged
Assert.fail("Expecting MathIllegalArgumentException");
} catch (MathIllegalArgumentException ex) {
// expected
}
try {
- MatrixUtils.createFieldMatrix(asFraction(new double[][] {{}, {}})); // no columns
+ MatrixUtils.createFieldMatrix(asDfp(new double[][] {{}, {}})); // no columns
Assert.fail("Expecting MathIllegalArgumentException");
} catch (MathIllegalArgumentException ex) {
// expected
}
try {
- MatrixUtils.createFieldMatrix((Fraction[][])null); // null
+ MatrixUtils.createFieldMatrix((Dfp[][])null); // null
Assert.fail("Expecting NullArgumentException");
} catch (NullArgumentException ex) {
// expected
@@ -148,18 +148,18 @@ public final class MatrixUtilsTest {
@Test
public void testCreateRowFieldMatrix() {
- Assert.assertEquals(MatrixUtils.createRowFieldMatrix(asFraction(row)),
- new Array2DRowFieldMatrix<>(asFraction(rowMatrix)));
+ Assert.assertEquals(MatrixUtils.createRowFieldMatrix(asDfp(row)),
+ new Array2DRowFieldMatrix<>(asDfp(rowMatrix)));
Assert.assertEquals(MatrixUtils.createRowFieldMatrix(fractionRow),
new Array2DRowFieldMatrix<>(fractionRowMatrix));
try {
- MatrixUtils.createRowFieldMatrix(new Fraction[] {}); // empty
+ MatrixUtils.createRowFieldMatrix(new Dfp[] {}); // empty
Assert.fail("Expecting MathIllegalArgumentException");
} catch (MathIllegalArgumentException ex) {
// expected
}
try {
- MatrixUtils.createRowFieldMatrix((Fraction[]) null); // null
+ MatrixUtils.createRowFieldMatrix((Dfp[]) null); // null
Assert.fail("Expecting NullArgumentException");
} catch (NullArgumentException ex) {
// expected
@@ -186,19 +186,19 @@ public final class MatrixUtilsTest {
@Test
public void testCreateColumnFieldMatrix() {
- Assert.assertEquals(MatrixUtils.createColumnFieldMatrix(asFraction(col)),
- new Array2DRowFieldMatrix<>(asFraction(colMatrix)));
+ Assert.assertEquals(MatrixUtils.createColumnFieldMatrix(asDfp(col)),
+ new Array2DRowFieldMatrix<>(asDfp(colMatrix)));
Assert.assertEquals(MatrixUtils.createColumnFieldMatrix(fractionCol),
new Array2DRowFieldMatrix<>(fractionColMatrix));
try {
- MatrixUtils.createColumnFieldMatrix(new Fraction[] {}); // empty
+ MatrixUtils.createColumnFieldMatrix(new Dfp[] {}); // empty
Assert.fail("Expecting MathIllegalArgumentException");
} catch (MathIllegalArgumentException ex) {
// expected
}
try {
- MatrixUtils.createColumnFieldMatrix((Fraction[]) null); // null
+ MatrixUtils.createColumnFieldMatrix((Dfp[]) null); // null
Assert.fail("Expecting NullArgumentException");
} catch (NullArgumentException ex) {
// expected
@@ -236,13 +236,13 @@ public final class MatrixUtilsTest {
/**
* Verifies that the matrix is an identity matrix
*/
- protected void checkIdentityFieldMatrix(FieldMatrix<Fraction> m) {
+ protected void checkIdentityFieldMatrix(FieldMatrix<Dfp> m) {
for (int i = 0; i < m.getRowDimension(); i++) {
for (int j =0; j < m.getColumnDimension(); j++) {
if (i == j) {
- Assert.assertEquals(m.getEntry(i, j), Fraction.ONE);
+ Assert.assertEquals(m.getEntry(i, j), Dfp25.ONE);
} else {
- Assert.assertEquals(m.getEntry(i, j), Fraction.ZERO);
+ Assert.assertEquals(m.getEntry(i, j), Dfp25.ZERO);
}
}
}
@@ -250,9 +250,9 @@ public final class MatrixUtilsTest {
@Test
public void testcreateFieldIdentityMatrix() {
- checkIdentityFieldMatrix(MatrixUtils.createFieldIdentityMatrix(FractionField.getInstance(), 3));
- checkIdentityFieldMatrix(MatrixUtils.createFieldIdentityMatrix(FractionField.getInstance(), 2));
- checkIdentityFieldMatrix(MatrixUtils.createFieldIdentityMatrix(FractionField.getInstance(), 1));
+ checkIdentityFieldMatrix(MatrixUtils.createFieldIdentityMatrix(Dfp25.getField(), 3));
+ checkIdentityFieldMatrix(MatrixUtils.createFieldIdentityMatrix(Dfp25.getField(), 2));
+ checkIdentityFieldMatrix(MatrixUtils.createFieldIdentityMatrix(Dfp25.getField(), 1));
try {
MatrixUtils.createRealIdentityMatrix(0);
Assert.fail("Expecting MathIllegalArgumentException");
@@ -262,7 +262,7 @@ public final class MatrixUtilsTest {
}
@Test
- public void testBigFractionConverter() {
+ public void testBigDfpConverter() {
BigFraction[][] bfData = {
{ new BigFraction(1), new BigFraction(2), new BigFraction(3) },
{ new BigFraction(2), new BigFraction(5), new BigFraction(3) },
@@ -274,45 +274,25 @@ public final class MatrixUtilsTest {
Assert.assertEquals(0.0, converted.subtract(reference).getNorm(), 0.0);
}
- @Test
- public void testFractionConverter() {
- Fraction[][] fData = {
- { new Fraction(1), new Fraction(2), new Fraction(3) },
- { new Fraction(2), new Fraction(5), new Fraction(3) },
- { new Fraction(1), new Fraction(0), new Fraction(8) }
- };
- FieldMatrix<Fraction> m = new Array2DRowFieldMatrix<>(fData, false);
- RealMatrix converted = MatrixUtils.fractionMatrixToRealMatrix(m);
- RealMatrix reference = new Array2DRowRealMatrix(testData, false);
- Assert.assertEquals(0.0, converted.subtract(reference).getNorm(), 0.0);
- }
-
- public static final Fraction[][] asFraction(double[][] data) {
- Fraction d[][] = new Fraction[data.length][];
- try {
- for (int i = 0; i < data.length; ++i) {
- double[] dataI = data[i];
- Fraction[] dI = new Fraction[dataI.length];
- for (int j = 0; j < dataI.length; ++j) {
- dI[j] = new Fraction(dataI[j]);
- }
- d[i] = dI;
+ public static final Dfp[][] asDfp(double[][] data) {
+ Dfp d[][] = new Dfp[data.length][];
+ for (int i = 0; i < data.length; ++i) {
+ double[] dataI = data[i];
+ Dfp[] dI = new Dfp[dataI.length];
+ for (int j = 0; j < dataI.length; ++j) {
+ dI[j] = Dfp25.of(dataI[j]);
}
- } catch (FractionConversionException fce) {
- Assert.fail(fce.getMessage());
+ d[i] = dI;
}
return d;
}
- public static final Fraction[] asFraction(double[] data) {
- Fraction d[] = new Fraction[data.length];
- try {
- for (int i = 0; i < data.length; ++i) {
- d[i] = new Fraction(data[i]);
- }
- } catch (FractionConversionException fce) {
- Assert.fail(fce.getMessage());
+ public static final Dfp[] asDfp(double[] data) {
+ Dfp d[] = new Dfp[data.length];
+ for (int i = 0; i < data.length; ++i) {
+ d[i] = Dfp25.of(data[i]);
}
+
return d;
}
diff --git a/src/test/java/org/apache/commons/math4/linear/SparseFieldMatrixTest.java b/src/test/java/org/apache/commons/math4/linear/SparseFieldMatrixTest.java
index 0266b30..8c83e58 100644
--- a/src/test/java/org/apache/commons/math4/linear/SparseFieldMatrixTest.java
+++ b/src/test/java/org/apache/commons/math4/linear/SparseFieldMatrixTest.java
@@ -24,9 +24,8 @@ import org.apache.commons.math4.exception.NoDataException;
import org.apache.commons.math4.exception.NullArgumentException;
import org.apache.commons.math4.exception.NumberIsTooSmallException;
import org.apache.commons.math4.exception.OutOfRangeException;
-import org.apache.commons.math4.fraction.Fraction;
-import org.apache.commons.math4.fraction.FractionConversionException;
-import org.apache.commons.math4.fraction.FractionField;
+import org.apache.commons.math4.dfp.Dfp;
+import org.apache.commons.math4.dfp.DfpField;
import org.apache.commons.math4.linear.Array2DRowFieldMatrix;
import org.apache.commons.math4.linear.ArrayFieldVector;
import org.apache.commons.math4.linear.FieldLUDecomposition;
@@ -41,99 +40,95 @@ import org.apache.commons.math4.linear.SparseFieldMatrix;
*/
public class SparseFieldMatrixTest {
// 3 x 3 identity matrix
- protected Fraction[][] id = { {new Fraction(1), new Fraction(0), new Fraction(0) }, { new Fraction(0), new Fraction(1), new Fraction(0) }, { new Fraction(0), new Fraction(0), new Fraction(1) } };
+ protected Dfp[][] id = { {Dfp25.of(1), Dfp25.of(0), Dfp25.of(0) }, { Dfp25.of(0), Dfp25.of(1), Dfp25.of(0) }, { Dfp25.of(0), Dfp25.of(0), Dfp25.of(1) } };
// Test data for group operations
- protected Fraction[][] testData = { { new Fraction(1), new Fraction(2), new Fraction(3) }, { new Fraction(2), new Fraction(5), new Fraction(3) },
- { new Fraction(1), new Fraction(0), new Fraction(8) } };
- protected Fraction[][] testDataLU = null;
- protected Fraction[][] testDataPlus2 = { { new Fraction(3), new Fraction(4), new Fraction(5) }, { new Fraction(4), new Fraction(7), new Fraction(5) },
- { new Fraction(3), new Fraction(2), new Fraction(10) } };
- protected Fraction[][] testDataMinus = { { new Fraction(-1), new Fraction(-2), new Fraction(-3) },
- { new Fraction(-2), new Fraction(-5), new Fraction(-3) }, { new Fraction(-1), new Fraction(0), new Fraction(-8) } };
- protected Fraction[] testDataRow1 = { new Fraction(1), new Fraction(2), new Fraction(3) };
- protected Fraction[] testDataCol3 = { new Fraction(3), new Fraction(3), new Fraction(8) };
- protected Fraction[][] testDataInv = { { new Fraction(-40), new Fraction(16), new Fraction(9) }, { new Fraction(13), new Fraction(-5), new Fraction(-3) },
- { new Fraction(5), new Fraction(-2), new Fraction(-1) } };
- protected Fraction[] preMultTest = { new Fraction(8), new Fraction(12), new Fraction(33) };
- protected Fraction[][] testData2 = { { new Fraction(1), new Fraction(2), new Fraction(3) }, { new Fraction(2), new Fraction(5), new Fraction(3) } };
- protected Fraction[][] testData2T = { { new Fraction(1), new Fraction(2) }, { new Fraction(2), new Fraction(5) }, { new Fraction(3), new Fraction(3) } };
- protected Fraction[][] testDataPlusInv = { { new Fraction(-39), new Fraction(18), new Fraction(12) },
- { new Fraction(15), new Fraction(0), new Fraction(0) }, { new Fraction(6), new Fraction(-2), new Fraction(7) } };
+ protected Dfp[][] testData = { { Dfp25.of(1), Dfp25.of(2), Dfp25.of(3) }, { Dfp25.of(2), Dfp25.of(5), Dfp25.of(3) },
+ { Dfp25.of(1), Dfp25.of(0), Dfp25.of(8) } };
+ protected Dfp[][] testDataLU = null;
+ protected Dfp[][] testDataPlus2 = { { Dfp25.of(3), Dfp25.of(4), Dfp25.of(5) }, { Dfp25.of(4), Dfp25.of(7), Dfp25.of(5) },
+ { Dfp25.of(3), Dfp25.of(2), Dfp25.of(10) } };
+ protected Dfp[][] testDataMinus = { { Dfp25.of(-1), Dfp25.of(-2), Dfp25.of(-3) },
+ { Dfp25.of(-2), Dfp25.of(-5), Dfp25.of(-3) }, { Dfp25.of(-1), Dfp25.of(0), Dfp25.of(-8) } };
+ protected Dfp[] testDataRow1 = { Dfp25.of(1), Dfp25.of(2), Dfp25.of(3) };
+ protected Dfp[] testDataCol3 = { Dfp25.of(3), Dfp25.of(3), Dfp25.of(8) };
+ protected Dfp[][] testDataInv = { { Dfp25.of(-40), Dfp25.of(16), Dfp25.of(9) }, { Dfp25.of(13), Dfp25.of(-5), Dfp25.of(-3) },
+ { Dfp25.of(5), Dfp25.of(-2), Dfp25.of(-1) } };
+ protected Dfp[] preMultTest = { Dfp25.of(8), Dfp25.of(12), Dfp25.of(33) };
+ protected Dfp[][] testData2 = { { Dfp25.of(1), Dfp25.of(2), Dfp25.of(3) }, { Dfp25.of(2), Dfp25.of(5), Dfp25.of(3) } };
+ protected Dfp[][] testData2T = { { Dfp25.of(1), Dfp25.of(2) }, { Dfp25.of(2), Dfp25.of(5) }, { Dfp25.of(3), Dfp25.of(3) } };
+ protected Dfp[][] testDataPlusInv = { { Dfp25.of(-39), Dfp25.of(18), Dfp25.of(12) },
+ { Dfp25.of(15), Dfp25.of(0), Dfp25.of(0) }, { Dfp25.of(6), Dfp25.of(-2), Dfp25.of(7) } };
// lu decomposition tests
- protected Fraction[][] luData = { { new Fraction(2), new Fraction(3), new Fraction(3) }, { new Fraction(0), new Fraction(5), new Fraction(7) }, { new Fraction(6), new Fraction(9), new Fraction(8) } };
- protected Fraction[][] luDataLUDecomposition = null;
+ protected Dfp[][] luData = { { Dfp25.of(2), Dfp25.of(3), Dfp25.of(3) }, { Dfp25.of(0), Dfp25.of(5), Dfp25.of(7) }, { Dfp25.of(6), Dfp25.of(9), Dfp25.of(8) } };
+ protected Dfp[][] luDataLUDecomposition = null;
// singular matrices
- protected Fraction[][] singular = { { new Fraction(2), new Fraction(3) }, { new Fraction(2), new Fraction(3) } };
- protected Fraction[][] bigSingular = { { new Fraction(1), new Fraction(2), new Fraction(3), new Fraction(4) },
- { new Fraction(2), new Fraction(5), new Fraction(3), new Fraction(4) }, { new Fraction(7), new Fraction(3), new Fraction(256), new Fraction(1930) }, { new Fraction(3), new Fraction(7), new Fraction(6), new Fraction(8) } }; // 4th
+ protected Dfp[][] singular = { { Dfp25.of(2), Dfp25.of(3) }, { Dfp25.of(2), Dfp25.of(3) } };
+ protected Dfp[][] bigSingular = { { Dfp25.of(1), Dfp25.of(2), Dfp25.of(3), Dfp25.of(4) },
+ { Dfp25.of(2), Dfp25.of(5), Dfp25.of(3), Dfp25.of(4) }, { Dfp25.of(7), Dfp25.of(3), Dfp25.of(256), Dfp25.of(1930) }, { Dfp25.of(3), Dfp25.of(7), Dfp25.of(6), Dfp25.of(8) } }; // 4th
// row
// =
// 1st
// +
// 2nd
- protected Fraction[][] detData = { { new Fraction(1), new Fraction(2), new Fraction(3) }, { new Fraction(4), new Fraction(5), new Fraction(6) },
- { new Fraction(7), new Fraction(8), new Fraction(10) } };
- protected Fraction[][] detData2 = { { new Fraction(1), new Fraction(3) }, { new Fraction(2), new Fraction(4) } };
+ protected Dfp[][] detData = { { Dfp25.of(1), Dfp25.of(2), Dfp25.of(3) }, { Dfp25.of(4), Dfp25.of(5), Dfp25.of(6) },
+ { Dfp25.of(7), Dfp25.of(8), Dfp25.of(10) } };
+ protected Dfp[][] detData2 = { { Dfp25.of(1), Dfp25.of(3) }, { Dfp25.of(2), Dfp25.of(4) } };
// vectors
- protected Fraction[] testVector = { new Fraction(1), new Fraction(2), new Fraction(3) };
- protected Fraction[] testVector2 = { new Fraction(1), new Fraction(2), new Fraction(3), new Fraction(4) };
+ protected Dfp[] testVector = { Dfp25.of(1), Dfp25.of(2), Dfp25.of(3) };
+ protected Dfp[] testVector2 = { Dfp25.of(1), Dfp25.of(2), Dfp25.of(3), Dfp25.of(4) };
// submatrix accessor tests
- protected Fraction[][] subTestData = null;
+ protected Dfp[][] subTestData = null;
// array selections
- protected Fraction[][] subRows02Cols13 = { {new Fraction(2), new Fraction(4) }, { new Fraction(4), new Fraction(8) } };
- protected Fraction[][] subRows03Cols12 = { { new Fraction(2), new Fraction(3) }, { new Fraction(5), new Fraction(6) } };
- protected Fraction[][] subRows03Cols123 = { { new Fraction(2), new Fraction(3), new Fraction(4) }, { new Fraction(5), new Fraction(6), new Fraction(7) } };
+ protected Dfp[][] subRows02Cols13 = { {Dfp25.of(2), Dfp25.of(4) }, { Dfp25.of(4), Dfp25.of(8) } };
+ protected Dfp[][] subRows03Cols12 = { { Dfp25.of(2), Dfp25.of(3) }, { Dfp25.of(5), Dfp25.of(6) } };
+ protected Dfp[][] subRows03Cols123 = { { Dfp25.of(2), Dfp25.of(3), Dfp25.of(4) }, { Dfp25.of(5), Dfp25.of(6), Dfp25.of(7) } };
// effective permutations
- protected Fraction[][] subRows20Cols123 = { { new Fraction(4), new Fraction(6), new Fraction(8) }, { new Fraction(2), new Fraction(3), new Fraction(4) } };
- protected Fraction[][] subRows31Cols31 = null;
+ protected Dfp[][] subRows20Cols123 = { { Dfp25.of(4), Dfp25.of(6), Dfp25.of(8) }, { Dfp25.of(2), Dfp25.of(3), Dfp25.of(4) } };
+ protected Dfp[][] subRows31Cols31 = null;
// contiguous ranges
- protected Fraction[][] subRows01Cols23 = null;
- protected Fraction[][] subRows23Cols00 = { { new Fraction(2) }, { new Fraction(4) } };
- protected Fraction[][] subRows00Cols33 = { { new Fraction(4) } };
+ protected Dfp[][] subRows01Cols23 = null;
+ protected Dfp[][] subRows23Cols00 = { { Dfp25.of(2) }, { Dfp25.of(4) } };
+ protected Dfp[][] subRows00Cols33 = { { Dfp25.of(4) } };
// row matrices
- protected Fraction[][] subRow0 = { { new Fraction(1), new Fraction(2), new Fraction(3), new Fraction(4) } };
- protected Fraction[][] subRow3 = { { new Fraction(4), new Fraction(5), new Fraction(6), new Fraction(7) } };
+ protected Dfp[][] subRow0 = { { Dfp25.of(1), Dfp25.of(2), Dfp25.of(3), Dfp25.of(4) } };
+ protected Dfp[][] subRow3 = { { Dfp25.of(4), Dfp25.of(5), Dfp25.of(6), Dfp25.of(7) } };
// column matrices
- protected Fraction[][] subColumn1 = null;
- protected Fraction[][] subColumn3 = null;
+ protected Dfp[][] subColumn1 = null;
+ protected Dfp[][] subColumn3 = null;
// tolerances
protected double entryTolerance = 10E-16;
protected double normTolerance = 10E-14;
- protected Field<Fraction> field = FractionField.getInstance();
+ protected Field<Dfp> field = Dfp25.getField();
public SparseFieldMatrixTest() {
- try {
- testDataLU = new Fraction[][]{ { new Fraction(2), new Fraction(5), new Fraction(3) }, { new Fraction(.5d), new Fraction(-2.5d), new Fraction(6.5d) },
- { new Fraction(0.5d), new Fraction(0.2d), new Fraction(.2d) } };
- luDataLUDecomposition = new Fraction[][]{ { new Fraction(6), new Fraction(9), new Fraction(8) },
- { new Fraction(0), new Fraction(5), new Fraction(7) }, { new Fraction(0.33333333333333), new Fraction(0), new Fraction(0.33333333333333) } };
- subTestData = new Fraction [][]{ { new Fraction(1), new Fraction(2), new Fraction(3), new Fraction(4) },
- { new Fraction(1.5), new Fraction(2.5), new Fraction(3.5), new Fraction(4.5) }, { new Fraction(2), new Fraction(4), new Fraction(6), new Fraction(8) }, { new Fraction(4), new Fraction(5), new Fraction(6), new Fraction(7) } };
- subRows31Cols31 = new Fraction[][]{ { new Fraction(7), new Fraction(5) }, { new Fraction(4.5), new Fraction(2.5) } };
- subRows01Cols23 = new Fraction[][]{ { new Fraction(3), new Fraction(4) }, { new Fraction(3.5), new Fraction(4.5) } };
- subColumn1 = new Fraction [][]{ { new Fraction(2) }, { new Fraction(2.5) }, { new Fraction(4) }, { new Fraction(5) } };
- subColumn3 = new Fraction[][]{ { new Fraction(4) }, { new Fraction(4.5) }, { new Fraction(8) }, { new Fraction(7) } };
- } catch (FractionConversionException e) {
- // ignore, can't happen
- }
+ testDataLU = new Dfp[][]{ { Dfp25.of(2), Dfp25.of(5), Dfp25.of(3) }, { Dfp25.of(.5d), Dfp25.of(-2.5d), Dfp25.of(6.5d) },
+ { Dfp25.of(0.5d), Dfp25.of(0.2d), Dfp25.of(.2d) } };
+ luDataLUDecomposition = new Dfp[][]{ { Dfp25.of(6), Dfp25.of(9), Dfp25.of(8) },
+ { Dfp25.of(0), Dfp25.of(5), Dfp25.of(7) }, { Dfp25.of(0.33333333333333), Dfp25.of(0), Dfp25.of(0.33333333333333) } };
+ subTestData = new Dfp[][]{ { Dfp25.of(1), Dfp25.of(2), Dfp25.of(3), Dfp25.of(4) },
+ { Dfp25.of(1.5), Dfp25.of(2.5), Dfp25.of(3.5), Dfp25.of(4.5) }, { Dfp25.of(2), Dfp25.of(4), Dfp25.of(6), Dfp25.of(8) }, { Dfp25.of(4), Dfp25.of(5), Dfp25.of(6), Dfp25.of(7) } };
+ subRows31Cols31 = new Dfp[][]{ { Dfp25.of(7), Dfp25.of(5) }, { Dfp25.of(4.5), Dfp25.of(2.5) } };
+ subRows01Cols23 = new Dfp[][]{ { Dfp25.of(3), Dfp25.of(4) }, { Dfp25.of(3.5), Dfp25.of(4.5) } };
+ subColumn1 = new Dfp[][]{ { Dfp25.of(2) }, { Dfp25.of(2.5) }, { Dfp25.of(4) }, { Dfp25.of(5) } };
+ subColumn3 = new Dfp[][]{ { Dfp25.of(4) }, { Dfp25.of(4.5) }, { Dfp25.of(8) }, { Dfp25.of(7) } };
}
/** test dimensions */
@Test
public void testDimensions() {
- SparseFieldMatrix<Fraction> m = createSparseMatrix(testData);
- SparseFieldMatrix<Fraction> m2 = createSparseMatrix(testData2);
+ SparseFieldMatrix<Dfp> m = createSparseMatrix(testData);
+ SparseFieldMatrix<Dfp> m2 = createSparseMatrix(testData2);
Assert.assertEquals("testData row dimension", 3, m.getRowDimension());
Assert.assertEquals("testData column dimension", 3, m.getColumnDimension());
Assert.assertTrue("testData is square", m.isSquare());
@@ -145,12 +140,12 @@ public class SparseFieldMatrixTest {
/** test copy functions */
@Test
public void testCopyFunctions() {
- SparseFieldMatrix<Fraction> m1 = createSparseMatrix(testData);
- FieldMatrix<Fraction> m2 = m1.copy();
+ SparseFieldMatrix<Dfp> m1 = createSparseMatrix(testData);
+ FieldMatrix<Dfp> m2 = m1.copy();
Assert.assertEquals(m1.getClass(), m2.getClass());
Assert.assertEquals((m2), m1);
- SparseFieldMatrix<Fraction> m3 = createSparseMatrix(testData);
- FieldMatrix<Fraction> m4 = m3.copy();
+ SparseFieldMatrix<Dfp> m3 = createSparseMatrix(testData);
+ FieldMatrix<Dfp> m4 = m3.copy();
Assert.assertEquals(m3.getClass(), m4.getClass());
Assert.assertEquals((m4), m3);
}
@@ -158,14 +153,14 @@ public class SparseFieldMatrixTest {
/** test add */
@Test
public void testAdd() {
- SparseFieldMatrix<Fraction> m = createSparseMatrix(testData);
- SparseFieldMatrix<Fraction> mInv = createSparseMatrix(testDataInv);
- SparseFieldMatrix<Fraction> mDataPlusInv = createSparseMatrix(testDataPlusInv);
- FieldMatrix<Fraction> mPlusMInv = m.add(mInv);
+ SparseFieldMatrix<Dfp> m = createSparseMatrix(testData);
+ SparseFieldMatrix<Dfp> mInv = createSparseMatrix(testDataInv);
+ SparseFieldMatrix<Dfp> mDataPlusInv = createSparseMatrix(testDataPlusInv);
+ FieldMatrix<Dfp> mPlusMInv = m.add(mInv);
for (int row = 0; row < m.getRowDimension(); row++) {
for (int col = 0; col < m.getColumnDimension(); col++) {
Assert.assertEquals("sum entry entry",
- mDataPlusInv.getEntry(row, col).doubleValue(), mPlusMInv.getEntry(row, col).doubleValue(),
+ mDataPlusInv.getEntry(row, col).toDouble(), mPlusMInv.getEntry(row, col).toDouble(),
entryTolerance);
}
}
@@ -174,8 +169,8 @@ public class SparseFieldMatrixTest {
/** test add failure */
@Test
public void testAddFail() {
- SparseFieldMatrix<Fraction> m = createSparseMatrix(testData);
- SparseFieldMatrix<Fraction> m2 = createSparseMatrix(testData2);
+ SparseFieldMatrix<Dfp> m = createSparseMatrix(testData);
+ SparseFieldMatrix<Dfp> m2 = createSparseMatrix(testData2);
try {
m.add(m2);
Assert.fail("MathIllegalArgumentException expected");
@@ -188,10 +183,10 @@ public class SparseFieldMatrixTest {
/** test m-n = m + -n */
@Test
public void testPlusMinus() {
- SparseFieldMatrix<Fraction> m = createSparseMatrix(testData);
- SparseFieldMatrix<Fraction> n = createSparseMatrix(testDataInv);
+ SparseFieldMatrix<Dfp> m = createSparseMatrix(testData);
+ SparseFieldMatrix<Dfp> n = createSparseMatrix(testDataInv);
assertClose("m-n = m + -n", m.subtract(n),
- n.scalarMultiply(new Fraction(-1)).add(m), entryTolerance);
+ n.scalarMultiply(Dfp25.of(-1)).add(m), entryTolerance);
try {
m.subtract(createSparseMatrix(testData2));
Assert.fail("Expecting illegalArgumentException");
@@ -203,13 +198,13 @@ public class SparseFieldMatrixTest {
/** test multiply */
@Test
public void testMultiply() {
- SparseFieldMatrix<Fraction> m = createSparseMatrix(testData);
- SparseFieldMatrix<Fraction> mInv = createSparseMatrix(testDataInv);
- SparseFieldMatrix<Fraction> identity = createSparseMatrix(id);
- SparseFieldMatrix<Fraction> m2 = createSparseMatrix(testData2);
+ SparseFieldMatrix<Dfp> m = createSparseMatrix(testData);
+ SparseFieldMatrix<Dfp> mInv = createSparseMatrix(testDataInv);
+ SparseFieldMatrix<Dfp> identity = createSparseMatrix(id);
+ SparseFieldMatrix<Dfp> m2 = createSparseMatrix(testData2);
assertClose("inverse multiply", m.multiply(mInv), identity,
entryTolerance);
- assertClose("inverse multiply", m.multiply(new Array2DRowFieldMatrix<>(FractionField.getInstance(), testDataInv)), identity,
+ assertClose("inverse multiply", m.multiply(new Array2DRowFieldMatrix<>(Dfp25.getField(), testDataInv)), identity,
entryTolerance);
assertClose("inverse multiply", mInv.multiply(m), identity,
entryTolerance);
@@ -229,23 +224,23 @@ public class SparseFieldMatrixTest {
// Additional Test for Array2DRowRealMatrixTest.testMultiply
- private Fraction[][] d3 = new Fraction[][] { { new Fraction(1), new Fraction(2), new Fraction(3), new Fraction(4) }, { new Fraction(5), new Fraction(6), new Fraction(7), new Fraction(8) } };
- private Fraction[][] d4 = new Fraction[][] { { new Fraction(1) }, { new Fraction(2) }, { new Fraction(3) }, { new Fraction(4) } };
- private Fraction[][] d5 = new Fraction[][] { { new Fraction(30) }, { new Fraction(70) } };
+ private Dfp[][] d3 = new Dfp[][] { { Dfp25.of(1), Dfp25.of(2), Dfp25.of(3), Dfp25.of(4) }, { Dfp25.of(5), Dfp25.of(6), Dfp25.of(7), Dfp25.of(8) } };
+ private Dfp[][] d4 = new Dfp[][] { { Dfp25.of(1) }, { Dfp25.of(2) }, { Dfp25.of(3) }, { Dfp25.of(4) } };
+ private Dfp[][] d5 = new Dfp[][] { { Dfp25.of(30) }, { Dfp25.of(70) } };
@Test
public void testMultiply2() {
- FieldMatrix<Fraction> m3 = createSparseMatrix(d3);
- FieldMatrix<Fraction> m4 = createSparseMatrix(d4);
- FieldMatrix<Fraction> m5 = createSparseMatrix(d5);
+ FieldMatrix<Dfp> m3 = createSparseMatrix(d3);
+ FieldMatrix<Dfp> m4 = createSparseMatrix(d4);
+ FieldMatrix<Dfp> m5 = createSparseMatrix(d5);
assertClose("m3*m4=m5", m3.multiply(m4), m5, entryTolerance);
}
/** test trace */
@Test
public void testTrace() {
- FieldMatrix<Fraction> m = createSparseMatrix(id);
- Assert.assertEquals("identity trace", 3d, m.getTrace().doubleValue(), entryTolerance);
+ FieldMatrix<Dfp> m = createSparseMatrix(id);
+ Assert.assertEquals("identity trace", 3d, m.getTrace().toDouble(), entryTolerance);
m = createSparseMatrix(testData2);
try {
m.getTrace();
@@ -258,15 +253,15 @@ public class SparseFieldMatrixTest {
/** test sclarAdd */
@Test
public void testScalarAdd() {
- FieldMatrix<Fraction> m = createSparseMatrix(testData);
+ FieldMatrix<Dfp> m = createSparseMatrix(testData);
assertClose("scalar add", createSparseMatrix(testDataPlus2),
- m.scalarAdd(new Fraction(2)), entryTolerance);
+ m.scalarAdd(Dfp25.of(2)), entryTolerance);
}
/** test operate */
@Test
public void testOperate() {
- FieldMatrix<Fraction> m = createSparseMatrix(id);
+ FieldMatrix<Dfp> m = createSparseMatrix(id);
assertClose("identity operate", testVector, m.operate(testVector),
entryTolerance);
assertClose("identity operate", testVector, m.operate(
@@ -283,31 +278,31 @@ public class SparseFieldMatrixTest {
/** test issue MATH-209 */
@Test
public void testMath209() {
- FieldMatrix<Fraction> a = createSparseMatrix(new Fraction[][] {
- { new Fraction(1), new Fraction(2) }, { new Fraction(3), new Fraction(4) }, { new Fraction(5), new Fraction(6) } });
- Fraction[] b = a.operate(new Fraction[] { new Fraction(1), new Fraction(1) });
+ FieldMatrix<Dfp> a = createSparseMatrix(new Dfp[][] {
+ { Dfp25.of(1), Dfp25.of(2) }, { Dfp25.of(3), Dfp25.of(4) }, { Dfp25.of(5), Dfp25.of(6) } });
+ Dfp[] b = a.operate(new Dfp[] { Dfp25.of(1), Dfp25.of(1) });
Assert.assertEquals(a.getRowDimension(), b.length);
- Assert.assertEquals(3.0, b[0].doubleValue(), 1.0e-12);
- Assert.assertEquals(7.0, b[1].doubleValue(), 1.0e-12);
- Assert.assertEquals(11.0, b[2].doubleValue(), 1.0e-12);
+ Assert.assertEquals(3.0, b[0].toDouble(), 1.0e-12);
+ Assert.assertEquals(7.0, b[1].toDouble(), 1.0e-12);
+ Assert.assertEquals(11.0, b[2].toDouble(), 1.0e-12);
}
/** test transpose */
@Test
public void testTranspose() {
- FieldMatrix<Fraction> m = createSparseMatrix(testData);
- FieldMatrix<Fraction> mIT = new FieldLUDecomposition<>(m).getSolver().getInverse().transpose();
- FieldMatrix<Fraction> mTI = new FieldLUDecomposition<>(m.transpose()).getSolver().getInverse();
+ FieldMatrix<Dfp> m = createSparseMatrix(testData);
+ FieldMatrix<Dfp> mIT = new FieldLUDecomposition<>(m).getSolver().getInverse().transpose();
+ FieldMatrix<Dfp> mTI = new FieldLUDecomposition<>(m.transpose()).getSolver().getInverse();
assertClose("inverse-transpose", mIT, mTI, normTolerance);
m = createSparseMatrix(testData2);
- FieldMatrix<Fraction> mt = createSparseMatrix(testData2T);
+ FieldMatrix<Dfp> mt = createSparseMatrix(testData2T);
assertClose("transpose",mt,m.transpose(),normTolerance);
}
/** test preMultiply by vector */
@Test
public void testPremultiplyVector() {
- FieldMatrix<Fraction> m = createSparseMatrix(testData);
+ FieldMatrix<Dfp> m = createSparseMatrix(testData);
assertClose("premultiply", m.preMultiply(testVector), preMultTest,
normTolerance);
assertClose("premultiply", m.preMultiply(
@@ -323,14 +318,14 @@ public class SparseFieldMatrixTest {
@Test
public void testPremultiply() {
- FieldMatrix<Fraction> m3 = createSparseMatrix(d3);
- FieldMatrix<Fraction> m4 = createSparseMatrix(d4);
- FieldMatrix<Fraction> m5 = createSparseMatrix(d5);
+ FieldMatrix<Dfp> m3 = createSparseMatrix(d3);
+ FieldMatrix<Dfp> m4 = createSparseMatrix(d4);
+ FieldMatrix<Dfp> m5 = createSparseMatrix(d5);
assertClose("m3*m4=m5", m4.preMultiply(m3), m5, entryTolerance);
- SparseFieldMatrix<Fraction> m = createSparseMatrix(testData);
- SparseFieldMatrix<Fraction> mInv = createSparseMatrix(testDataInv);
- SparseFieldMatrix<Fraction> identity = createSparseMatrix(id);
+ SparseFieldMatrix<Dfp> m = createSparseMatrix(testData);
+ SparseFieldMatrix<Dfp> mInv = createSparseMatrix(testDataInv);
+ SparseFieldMatrix<Dfp> identity = createSparseMatrix(id);
assertClose("inverse multiply", m.preMultiply(mInv), identity,
entryTolerance);
assertClose("inverse multiply", mInv.preMultiply(m), identity,
@@ -349,7 +344,7 @@ public class SparseFieldMatrixTest {
@Test
public void testGetVectors() {
- FieldMatrix<Fraction> m = createSparseMatrix(testData);
+ FieldMatrix<Dfp> m = createSparseMatrix(testData);
assertClose("get row", m.getRow(0), testDataRow1, entryTolerance);
assertClose("get col", m.getColumn(2), testDataCol3, entryTolerance);
try {
@@ -368,8 +363,8 @@ public class SparseFieldMatrixTest {
@Test
public void testGetEntry() {
- FieldMatrix<Fraction> m = createSparseMatrix(testData);
- Assert.assertEquals("get entry", m.getEntry(0, 1).doubleValue(), 2d, entryTolerance);
+ FieldMatrix<Dfp> m = createSparseMatrix(testData);
+ Assert.assertEquals("get entry", m.getEntry(0, 1).toDouble(), 2d, entryTolerance);
try {
m.getEntry(10, 4);
Assert.fail("Expecting OutOfRangeException");
@@ -382,50 +377,50 @@ public class SparseFieldMatrixTest {
@Test
public void testExamples() {
// Create a real matrix with two rows and three columns
- Fraction[][] matrixData = { { new Fraction(1), new Fraction(2), new Fraction(3) }, { new Fraction(2), new Fraction(5), new Fraction(3) } };
- FieldMatrix<Fraction> m = createSparseMatrix(matrixData);
+ Dfp[][] matrixData = { { Dfp25.of(1), Dfp25.of(2), Dfp25.of(3) }, { Dfp25.of(2), Dfp25.of(5), Dfp25.of(3) } };
+ FieldMatrix<Dfp> m = createSparseMatrix(matrixData);
// One more with three rows, two columns
- Fraction[][] matrixData2 = { { new Fraction(1), new Fraction(2) }, { new Fraction(2), new Fraction(5) }, { new Fraction(1), new Fraction(7) } };
- FieldMatrix<Fraction> n = createSparseMatrix(matrixData2);
+ Dfp[][] matrixData2 = { { Dfp25.of(1), Dfp25.of(2) }, { Dfp25.of(2), Dfp25.of(5) }, { Dfp25.of(1), Dfp25.of(7) } };
+ FieldMatrix<Dfp> n = createSparseMatrix(matrixData2);
// Now multiply m by n
- FieldMatrix<Fraction> p = m.multiply(n);
+ FieldMatrix<Dfp> p = m.multiply(n);
Assert.assertEquals(2, p.getRowDimension());
Assert.assertEquals(2, p.getColumnDimension());
// Invert p
- FieldMatrix<Fraction> pInverse = new FieldLUDecomposition<>(p).getSolver().getInverse();
+ FieldMatrix<Dfp> pInverse = new FieldLUDecomposition<>(p).getSolver().getInverse();
Assert.assertEquals(2, pInverse.getRowDimension());
Assert.assertEquals(2, pInverse.getColumnDimension());
// Solve example
- Fraction[][] coefficientsData = { { new Fraction(2), new Fraction(3), new Fraction(-2) }, { new Fraction(-1), new Fraction(7), new Fraction(6) },
- { new Fraction(4), new Fraction(-3), new Fraction(-5) } };
- FieldMatrix<Fraction> coefficients = createSparseMatrix(coefficientsData);
- Fraction[] constants = { new Fraction(1), new Fraction(-2), new Fraction(1) };
- Fraction[] solution;
+ Dfp[][] coefficientsData = { { Dfp25.of(2), Dfp25.of(3), Dfp25.of(-2) }, { Dfp25.of(-1), Dfp25.of(7), Dfp25.of(6) },
+ { Dfp25.of(4), Dfp25.of(-3), Dfp25.of(-5) } };
+ FieldMatrix<Dfp> coefficients = createSparseMatrix(coefficientsData);
+ Dfp[] constants = { Dfp25.of(1), Dfp25.of(-2), Dfp25.of(1) };
+ Dfp[] solution;
solution = new FieldLUDecomposition<>(coefficients)
.getSolver()
.solve(new ArrayFieldVector<>(constants, false)).toArray();
- Assert.assertEquals((new Fraction(2).multiply((solution[0])).add(new Fraction(3).multiply(solution[1])).subtract(new Fraction(2).multiply(solution[2]))).doubleValue(),
- constants[0].doubleValue(), 1E-12);
- Assert.assertEquals(((new Fraction(-1).multiply(solution[0])).add(new Fraction(7).multiply(solution[1])).add(new Fraction(6).multiply(solution[2]))).doubleValue(),
- constants[1].doubleValue(), 1E-12);
- Assert.assertEquals(((new Fraction(4).multiply(solution[0])).subtract(new Fraction(3).multiply( solution[1])).subtract(new Fraction(5).multiply(solution[2]))).doubleValue(),
- constants[2].doubleValue(), 1E-12);
+ Assert.assertEquals((Dfp25.of(2).multiply((solution[0])).add(Dfp25.of(3).multiply(solution[1])).subtract(Dfp25.of(2).multiply(solution[2]))).toDouble(),
+ constants[0].toDouble(), 1E-12);
+ Assert.assertEquals(((Dfp25.of(-1).multiply(solution[0])).add(Dfp25.of(7).multiply(solution[1])).add(Dfp25.of(6).multiply(solution[2]))).toDouble(),
+ constants[1].toDouble(), 1E-12);
+ Assert.assertEquals(((Dfp25.of(4).multiply(solution[0])).subtract(Dfp25.of(3).multiply( solution[1])).subtract(Dfp25.of(5).multiply(solution[2]))).toDouble(),
+ constants[2].toDouble(), 1E-12);
}
// test submatrix accessors
@Test
public void testSubMatrix() {
- FieldMatrix<Fraction> m = createSparseMatrix(subTestData);
- FieldMatrix<Fraction> mRows23Cols00 = createSparseMatrix(subRows23Cols00);
- FieldMatrix<Fraction> mRows00Cols33 = createSparseMatrix(subRows00Cols33);
- FieldMatrix<Fraction> mRows01Cols23 = createSparseMatrix(subRows01Cols23);
- FieldMatrix<Fraction> mRows02Cols13 = createSparseMatrix(subRows02Cols13);
- FieldMatrix<Fraction> mRows03Cols12 = createSparseMatrix(subRows03Cols12);
- FieldMatrix<Fraction> mRows03Cols123 = createSparseMatrix(subRows03Cols123);
- FieldMatrix<Fraction> mRows20Cols123 = createSparseMatrix(subRows20Cols123);
- FieldMatrix<Fraction> mRows31Cols31 = createSparseMatrix(subRows31Cols31);
+ FieldMatrix<Dfp> m = createSparseMatrix(subTestData);
+ FieldMatrix<Dfp> mRows23Cols00 = createSparseMatrix(subRows23Cols00);
+ FieldMatrix<Dfp> mRows00Cols33 = createSparseMatrix(subRows00Cols33);
+ FieldMatrix<Dfp> mRows01Cols23 = createSparseMatrix(subRows01Cols23);
+ FieldMatrix<Dfp> mRows02Cols13 = createSparseMatrix(subRows02Cols13);
+ FieldMatrix<Dfp> mRows03Cols12 = createSparseMatrix(subRows03Cols12);
+ FieldMatrix<Dfp> mRows03Cols123 = createSparseMatrix(subRows03Cols123);
+ FieldMatrix<Dfp> mRows20Cols123 = createSparseMatrix(subRows20Cols123);
+ FieldMatrix<Dfp> mRows31Cols31 = createSparseMatrix(subRows31Cols31);
Assert.assertEquals("Rows23Cols00", mRows23Cols00, m.getSubMatrix(2, 3, 0, 0));
Assert.assertEquals("Rows00Cols33", mRows00Cols33, m.getSubMatrix(0, 0, 3, 3));
Assert.assertEquals("Rows01Cols23", mRows01Cols23, m.getSubMatrix(0, 1, 2, 3));
@@ -482,9 +477,9 @@ public class SparseFieldMatrixTest {
@Test
public void testGetRowMatrix() {
- FieldMatrix<Fraction> m = createSparseMatrix(subTestData);
- FieldMatrix<Fraction> mRow0 = createSparseMatrix(subRow0);
- FieldMatrix<Fraction> mRow3 = createSparseMatrix(subRow3);
+ FieldMatrix<Dfp> m = createSparseMatrix(subTestData);
+ FieldMatrix<Dfp> mRow0 = createSparseMatrix(subRow0);
+ FieldMatrix<Dfp> mRow3 = createSparseMatrix(subRow3);
Assert.assertEquals("Row0", mRow0, m.getRowMatrix(0));
Assert.assertEquals("Row3", mRow3, m.getRowMatrix(3));
try {
@@ -503,9 +498,9 @@ public class SparseFieldMatrixTest {
@Test
public void testGetColumnMatrix() {
- FieldMatrix<Fraction> m = createSparseMatrix(subTestData);
- FieldMatrix<Fraction> mColumn1 = createSparseMatrix(subColumn1);
- FieldMatrix<Fraction> mColumn3 = createSparseMatrix(subColumn3);
+ FieldMatrix<Dfp> m = createSparseMatrix(subTestData);
+ FieldMatrix<Dfp> mColumn1 = createSparseMatrix(subColumn1);
+ FieldMatrix<Dfp> mColumn3 = createSparseMatrix(subColumn3);
Assert.assertEquals("Column1", mColumn1, m.getColumnMatrix(1));
Assert.assertEquals("Column3", mColumn3, m.getColumnMatrix(3));
try {
@@ -524,9 +519,9 @@ public class SparseFieldMatrixTest {
@Test
public void testGetRowVector() {
- FieldMatrix<Fraction> m = createSparseMatrix(subTestData);
- FieldVector<Fraction> mRow0 = new ArrayFieldVector<>(subRow0[0]);
- FieldVector<Fraction> mRow3 = new ArrayFieldVector<>(subRow3[0]);
+ FieldMatrix<Dfp> m = createSparseMatrix(subTestData);
+ FieldVector<Dfp> mRow0 = new ArrayFieldVector<>(subRow0[0]);
+ FieldVector<Dfp> mRow3 = new ArrayFieldVector<>(subRow3[0]);
Assert.assertEquals("Row0", mRow0, m.getRowVector(0));
Assert.assertEquals("Row3", mRow3, m.getRowVector(3));
try {
@@ -545,9 +540,9 @@ public class SparseFieldMatrixTest {
@Test
public void testGetColumnVector() {
- FieldMatrix<Fraction> m = createSparseMatrix(subTestData);
- FieldVector<Fraction> mColumn1 = columnToVector(subColumn1);
- FieldVector<Fraction> mColumn3 = columnToVector(subColumn3);
+ FieldMatrix<Dfp> m = createSparseMatrix(subTestData);
+ FieldVector<Dfp> mColumn1 = columnToVector(subColumn1);
+ FieldVector<Dfp> mColumn3 = columnToVector(subColumn3);
Assert.assertEquals("Column1", mColumn1, m.getColumnVector(1));
Assert.assertEquals("Column3", mColumn3, m.getColumnVector(3));
try {
@@ -564,8 +559,8 @@ public class SparseFieldMatrixTest {
}
}
- private FieldVector<Fraction> columnToVector(Fraction[][] column) {
- Fraction[] data = new Fraction[column.length];
+ private FieldVector<Dfp> columnToVector(Dfp[][] column) {
+ Dfp[] data = new Dfp[column.length];
for (int i = 0; i < data.length; ++i) {
data[i] = column[i][0];
}
@@ -574,9 +569,9 @@ public class SparseFieldMatrixTest {
@Test
public void testEqualsAndHashCode() {
- SparseFieldMatrix<Fraction> m = createSparseMatrix(testData);
- SparseFieldMatrix<Fraction> m1 = (SparseFieldMatrix<Fraction>) m.copy();
- SparseFieldMatrix<Fraction> mt = (SparseFieldMatrix<Fraction>) m.transpose();
+ SparseFieldMatrix<Dfp> m = createSparseMatrix(testData);
+ SparseFieldMatrix<Dfp> m1 = (SparseFieldMatrix<Dfp>) m.copy();
+ SparseFieldMatrix<Dfp> mt = (SparseFieldMatrix<Dfp>) m.transpose();
Assert.assertTrue(m.hashCode() != mt.hashCode());
Assert.assertEquals(m.hashCode(), m1.hashCode());
Assert.assertEquals(m, m);
@@ -589,39 +584,39 @@ public class SparseFieldMatrixTest {
/* Disable for now
@Test
public void testToString() {
- SparseFieldMatrix<Fraction> m = createSparseMatrix(testData);
- Assert.assertEquals("SparseFieldMatrix<Fraction>{{1.0,2.0,3.0},{2.0,5.0,3.0},{1.0,0.0,8.0}}",
+ SparseFieldMatrix<Dfp> m = createSparseMatrix(testData);
+ Assert.assertEquals("SparseFieldMatrix<Dfp>{{1.0,2.0,3.0},{2.0,5.0,3.0},{1.0,0.0,8.0}}",
m.toString());
- m = new SparseFieldMatrix<Fraction>(field, 1, 1);
- Assert.assertEquals("SparseFieldMatrix<Fraction>{{0.0}}", m.toString());
+ m = new SparseFieldMatrix<Dfp>(field, 1, 1);
+ Assert.assertEquals("SparseFieldMatrix<Dfp>{{0.0}}", m.toString());
}
*/
@Test
public void testSetSubMatrix() {
- SparseFieldMatrix<Fraction> m = createSparseMatrix(testData);
+ SparseFieldMatrix<Dfp> m = createSparseMatrix(testData);
m.setSubMatrix(detData2, 1, 1);
- FieldMatrix<Fraction> expected = createSparseMatrix(new Fraction[][] {
- { new Fraction(1), new Fraction(2), new Fraction(3) }, { new Fraction(2), new Fraction(1), new Fraction(3) }, { new Fraction(1), new Fraction(2), new Fraction(4) } });
+ FieldMatrix<Dfp> expected = createSparseMatrix(new Dfp[][] {
+ { Dfp25.of(1), Dfp25.of(2), Dfp25.of(3) }, { Dfp25.of(2), Dfp25.of(1), Dfp25.of(3) }, { Dfp25.of(1), Dfp25.of(2), Dfp25.of(4) } });
Assert.assertEquals(expected, m);
m.setSubMatrix(detData2, 0, 0);
- expected = createSparseMatrix(new Fraction[][] {
- { new Fraction(1), new Fraction(3), new Fraction(3) }, { new Fraction(2), new Fraction(4), new Fraction(3) }, { new Fraction(1), new Fraction(2), new Fraction(4) } });
+ expected = createSparseMatrix(new Dfp[][] {
+ { Dfp25.of(1), Dfp25.of(3), Dfp25.of(3) }, { Dfp25.of(2), Dfp25.of(4), Dfp25.of(3) }, { Dfp25.of(1), Dfp25.of(2), Dfp25.of(4) } });
Assert.assertEquals(expected, m);
m.setSubMatrix(testDataPlus2, 0, 0);
- expected = createSparseMatrix(new Fraction[][] {
- { new Fraction(3), new Fraction(4), new Fraction(5) }, { new Fraction(4), new Fraction(7), new Fraction(5) }, { new Fraction(3), new Fraction(2), new Fraction(10) } });
+ expected = createSparseMatrix(new Dfp[][] {
+ { Dfp25.of(3), Dfp25.of(4), Dfp25.of(5) }, { Dfp25.of(4), Dfp25.of(7), Dfp25.of(5) }, { Dfp25.of(3), Dfp25.of(2), Dfp25.of(10) } });
Assert.assertEquals(expected, m);
// javadoc example
- SparseFieldMatrix<Fraction> matrix =
- createSparseMatrix(new Fraction[][] {
- { new Fraction(1), new Fraction(2), new Fraction(3), new Fraction(4) }, { new Fraction(5), new Fraction(6), new Fraction(7), new Fraction(8) }, { new Fraction(9), new Fraction(0), new Fraction(1), new Fraction(2) } });
- matrix.setSubMatrix(new Fraction[][] { { new Fraction(3), new Fraction(4) }, { new Fraction(5), new Fraction(6) } }, 1, 1);
- expected = createSparseMatrix(new Fraction[][] {
- { new Fraction(1), new Fraction(2), new Fraction(3), new Fraction(4) }, { new Fraction(5), new Fraction(3), new Fraction(4), new Fraction(8) }, { new Fraction(9), new Fraction(5), new Fraction(6), new Fraction(2) } });
+ SparseFieldMatrix<Dfp> matrix =
+ createSparseMatrix(new Dfp[][] {
+ { Dfp25.of(1), Dfp25.of(2), Dfp25.of(3), Dfp25.of(4) }, { Dfp25.of(5), Dfp25.of(6), Dfp25.of(7), Dfp25.of(8) }, { Dfp25.of(9), Dfp25.of(0), Dfp25.of(1), Dfp25.of(2) } });
+ matrix.setSubMatrix(new Dfp[][] { { Dfp25.of(3), Dfp25.of(4) }, { Dfp25.of(5), Dfp25.of(6) } }, 1, 1);
+ expected = createSparseMatrix(new Dfp[][] {
+ { Dfp25.of(1), Dfp25.of(2), Dfp25.of(3), Dfp25.of(4) }, { Dfp25.of(5), Dfp25.of(3), Dfp25.of(4), Dfp25.of(8) }, { Dfp25.of(9), Dfp25.of(5), Dfp25.of(6), Dfp25.of(2) } });
Assert.assertEquals(expected, matrix);
// dimension overflow
@@ -661,7 +656,7 @@ public class SparseFieldMatrixTest {
// ragged
try {
- m.setSubMatrix(new Fraction[][] { { new Fraction(1) }, { new Fraction(2), new Fraction(3) } }, 0, 0);
+ m.setSubMatrix(new Dfp[][] { { Dfp25.of(1) }, { Dfp25.of(2), Dfp25.of(3) } }, 0, 0);
Assert.fail("expecting MathIllegalArgumentException");
} catch (MathIllegalArgumentException e) {
// expected
@@ -669,7 +664,7 @@ public class SparseFieldMatrixTest {
// empty
try {
- m.setSubMatrix(new Fraction[][] { {} }, 0, 0);
+ m.setSubMatrix(new Dfp[][] { {} }, 0, 0);
Assert.fail("expecting MathIllegalArgumentException");
} catch (MathIllegalArgumentException e) {
// expected
@@ -679,30 +674,30 @@ public class SparseFieldMatrixTest {
// --------------- -----------------Protected methods
/** verifies that two matrices are close (1-norm) */
- protected void assertClose(String msg, FieldMatrix<Fraction> m, FieldMatrix<Fraction> n,
+ protected void assertClose(String msg, FieldMatrix<Dfp> m, FieldMatrix<Dfp> n,
double tolerance) {
for(int i=0; i < m.getRowDimension(); i++){
for(int j=0; j < m.getColumnDimension(); j++){
- Assert.assertEquals(msg, m.getEntry(i,j).doubleValue(), n.getEntry(i,j).doubleValue(), tolerance);
+ Assert.assertEquals(msg, m.getEntry(i,j).toDouble(), n.getEntry(i,j).toDouble(), tolerance);
}
}
}
/** verifies that two vectors are close (sup norm) */
- protected void assertClose(String msg, Fraction[] m, Fraction[] n,
+ protected void assertClose(String msg, Dfp[] m, Dfp[] n,
double tolerance) {
if (m.length != n.length) {
Assert.fail("vectors not same length");
}
for (int i = 0; i < m.length; i++) {
- Assert.assertEquals(msg + " " + i + " elements differ", m[i].doubleValue(), n[i].doubleValue(),
+ Assert.assertEquals(msg + " " + i + " elements differ", m[i].toDouble(), n[i].toDouble(),
tolerance);
}
}
- private SparseFieldMatrix<Fraction> createSparseMatrix(Fraction[][] data) {
- SparseFieldMatrix<Fraction> matrix = new SparseFieldMatrix<>(field, data.length, data[0].length);
+ private SparseFieldMatrix<Dfp> createSparseMatrix(Dfp[][] data) {
+ SparseFieldMatrix<Dfp> matrix = new SparseFieldMatrix<>(field, data.length, data[0].length);
for (int row = 0; row < data.length; row++) {
for (int col = 0; col < data[row].length; col++) {
matrix.setEntry(row, col, data[row][col]);
diff --git a/src/test/java/org/apache/commons/math4/linear/SparseFieldVectorTest.java b/src/test/java/org/apache/commons/math4/linear/SparseFieldVectorTest.java
index 29efdd3..232f485 100644
--- a/src/test/java/org/apache/commons/math4/linear/SparseFieldVectorTest.java
+++ b/src/test/java/org/apache/commons/math4/linear/SparseFieldVectorTest.java
@@ -22,9 +22,8 @@ import java.util.Arrays;
import org.apache.commons.math4.exception.MathIllegalArgumentException;
import org.apache.commons.math4.exception.NumberIsTooSmallException;
import org.apache.commons.math4.exception.OutOfRangeException;
-import org.apache.commons.math4.fraction.Fraction;
-import org.apache.commons.math4.fraction.FractionConversionException;
-import org.apache.commons.math4.fraction.FractionField;
+import org.apache.commons.math4.dfp.Dfp;
+import org.apache.commons.math4.dfp.DfpField;
import org.apache.commons.math4.linear.FieldMatrix;
import org.apache.commons.math4.linear.FieldVector;
import org.apache.commons.math4.linear.FieldVectorChangingVisitor;
@@ -41,169 +40,169 @@ import org.junit.Test;
public class SparseFieldVectorTest {
//
- protected Fraction[][] ma1 = {{new Fraction(1), new Fraction(2), new Fraction(3)}, {new Fraction(4), new Fraction(5), new Fraction(6)}, {new Fraction(7), new Fraction(8), new Fraction(9)}};
- protected Fraction[] vec1 = {new Fraction(1), new Fraction(2), new Fraction(3)};
- protected Fraction[] vec2 = {new Fraction(4), new Fraction(5), new Fraction(6)};
- protected Fraction[] vec3 = {new Fraction(7), new Fraction(8), new Fraction(9)};
- protected Fraction[] vec4 = {new Fraction(1), new Fraction(2), new Fraction(3), new Fraction(4), new Fraction(5), new Fraction(6), new Fraction(7), new Fraction(8), new Fraction(9)};
- protected Fraction[] vec_null = {new Fraction(0), new Fraction(0), new Fraction(0)};
- protected Fraction[] dvec1 = {new Fraction(1), new Fraction(2), new Fraction(3), new Fraction(4), new Fraction(5), new Fraction(6), new Fraction(7), new Fraction(8),new Fraction(9)};
- protected Fraction[][] mat1 = {{new Fraction(1), new Fraction(2), new Fraction(3)}, {new Fraction(4), new Fraction(5), new Fraction(6)},{ new Fraction(7), new Fraction(8), new Fraction(9)}};
+ protected Dfp[][] ma1 = {{Dfp25.of(1), Dfp25.of(2), Dfp25.of(3)}, {Dfp25.of(4), Dfp25.of(5), Dfp25.of(6)}, {Dfp25.of(7), Dfp25.of(8), Dfp25.of(9)}};
+ protected Dfp[] vec1 = {Dfp25.of(1), Dfp25.of(2), Dfp25.of(3)};
+ protected Dfp[] vec2 = {Dfp25.of(4), Dfp25.of(5), Dfp25.of(6)};
+ protected Dfp[] vec3 = {Dfp25.of(7), Dfp25.of(8), Dfp25.of(9)};
+ protected Dfp[] vec4 = {Dfp25.of(1), Dfp25.of(2), Dfp25.of(3), Dfp25.of(4), Dfp25.of(5), Dfp25.of(6), Dfp25.of(7), Dfp25.of(8), Dfp25.of(9)};
+ protected Dfp[] vec_null = {Dfp25.of(0), Dfp25.of(0), Dfp25.of(0)};
+ protected Dfp[] dvec1 = {Dfp25.of(1), Dfp25.of(2), Dfp25.of(3), Dfp25.of(4), Dfp25.of(5), Dfp25.of(6), Dfp25.of(7), Dfp25.of(8),Dfp25.of(9)};
+ protected Dfp[][] mat1 = {{Dfp25.of(1), Dfp25.of(2), Dfp25.of(3)}, {Dfp25.of(4), Dfp25.of(5), Dfp25.of(6)},{ Dfp25.of(7), Dfp25.of(8), Dfp25.of(9)}};
// tolerances
protected double entryTolerance = 10E-16;
protected double normTolerance = 10E-14;
- protected FractionField field = FractionField.getInstance();
+ protected DfpField field = Dfp25.getField();
@Test
- public void testMapFunctions() throws FractionConversionException {
- SparseFieldVector<Fraction> v1 = new SparseFieldVector<>(field,vec1);
+ public void testMapFunctions() {
+ SparseFieldVector<Dfp> v1 = new SparseFieldVector<>(field,vec1);
//octave = v1 .+ 2.0
- FieldVector<Fraction> v_mapAdd = v1.mapAdd(new Fraction(2));
- Fraction[] result_mapAdd = {new Fraction(3), new Fraction(4), new Fraction(5)};
+ FieldVector<Dfp> v_mapAdd = v1.mapAdd(Dfp25.of(2));
+ Dfp[] result_mapAdd = {Dfp25.of(3), Dfp25.of(4), Dfp25.of(5)};
Assert.assertArrayEquals("compare vectors" ,result_mapAdd,v_mapAdd.toArray());
//octave = v1 .+ 2.0
- FieldVector<Fraction> v_mapAddToSelf = v1.copy();
- v_mapAddToSelf.mapAddToSelf(new Fraction(2));
- Fraction[] result_mapAddToSelf = {new Fraction(3), new Fraction(4), new Fraction(5)};
+ FieldVector<Dfp> v_mapAddToSelf = v1.copy();
+ v_mapAddToSelf.mapAddToSelf(Dfp25.of(2));
+ Dfp[] result_mapAddToSelf = {Dfp25.of(3), Dfp25.of(4), Dfp25.of(5)};
Assert.assertArrayEquals("compare vectors" ,result_mapAddToSelf,v_mapAddToSelf.toArray());
//octave = v1 .- 2.0
- FieldVector<Fraction> v_mapSubtract = v1.mapSubtract(new Fraction(2));
- Fraction[] result_mapSubtract = {new Fraction(-1), new Fraction(0), new Fraction(1)};
+ FieldVector<Dfp> v_mapSubtract = v1.mapSubtract(Dfp25.of(2));
+ Dfp[] result_mapSubtract = {Dfp25.of(-1), Dfp25.of(0), Dfp25.of(1)};
Assert.assertArrayEquals("compare vectors" ,result_mapSubtract,v_mapSubtract.toArray());
//octave = v1 .- 2.0
- FieldVector<Fraction> v_mapSubtractToSelf = v1.copy();
- v_mapSubtractToSelf.mapSubtractToSelf(new Fraction(2));
- Fraction[] result_mapSubtractToSelf = {new Fraction(-1), new Fraction(0), new Fraction(1)};
+ FieldVector<Dfp> v_mapSubtractToSelf = v1.copy();
+ v_mapSubtractToSelf.mapSubtractToSelf(Dfp25.of(2));
+ Dfp[] result_mapSubtractToSelf = {Dfp25.of(-1), Dfp25.of(0), Dfp25.of(1)};
Assert.assertArrayEquals("compare vectors" ,result_mapSubtractToSelf,v_mapSubtractToSelf.toArray());
//octave = v1 .* 2.0
- FieldVector<Fraction> v_mapMultiply = v1.mapMultiply(new Fraction(2));
- Fraction[] result_mapMultiply = {new Fraction(2), new Fraction(4), new Fraction(6)};
+ FieldVector<Dfp> v_mapMultiply = v1.mapMultiply(Dfp25.of(2));
+ Dfp[] result_mapMultiply = {Dfp25.of(2), Dfp25.of(4), Dfp25.of(6)};
Assert.assertArrayEquals("compare vectors" ,result_mapMultiply,v_mapMultiply.toArray());
//octave = v1 .* 2.0
- FieldVector<Fraction> v_mapMultiplyToSelf = v1.copy();
- v_mapMultiplyToSelf.mapMultiplyToSelf(new Fraction(2));
- Fraction[] result_mapMultiplyToSelf = {new Fraction(2), new Fraction(4), new Fraction(6)};
+ FieldVector<Dfp> v_mapMultiplyToSelf = v1.copy();
+ v_mapMultiplyToSelf.mapMultiplyToSelf(Dfp25.of(2));
+ Dfp[] result_mapMultiplyToSelf = {Dfp25.of(2), Dfp25.of(4), Dfp25.of(6)};
Assert.assertArrayEquals("compare vectors" ,result_mapMultiplyToSelf,v_mapMultiplyToSelf.toArray());
//octave = v1 ./ 2.0
- FieldVector<Fraction> v_mapDivide = v1.mapDivide(new Fraction(2));
- Fraction[] result_mapDivide = {new Fraction(.5d), new Fraction(1), new Fraction(1.5d)};
+ FieldVector<Dfp> v_mapDivide = v1.mapDivide(Dfp25.of(2));
+ Dfp[] result_mapDivide = {Dfp25.of(.5d), Dfp25.of(1), Dfp25.of(1.5d)};
Assert.assertArrayEquals("compare vectors" ,result_mapDivide,v_mapDivide.toArray());
//octave = v1 ./ 2.0
- FieldVector<Fraction> v_mapDivideToSelf = v1.copy();
- v_mapDivideToSelf.mapDivideToSelf(new Fraction(2));
- Fraction[] result_mapDivideToSelf = {new Fraction(.5d), new Fraction(1), new Fraction(1.5d)};
+ FieldVector<Dfp> v_mapDivideToSelf = v1.copy();
+ v_mapDivideToSelf.mapDivideToSelf(Dfp25.of(2));
+ Dfp[] result_mapDivideToSelf = {Dfp25.of(.5d), Dfp25.of(1), Dfp25.of(1.5d)};
Assert.assertArrayEquals("compare vectors" ,result_mapDivideToSelf,v_mapDivideToSelf.toArray());
//octave = v1 .^-1
- FieldVector<Fraction> v_mapInv = v1.mapInv();
- Fraction[] result_mapInv = {new Fraction(1),new Fraction(0.5d),new Fraction(3.333333333333333e-01d)};
+ FieldVector<Dfp> v_mapInv = v1.mapInv();
+ Dfp[] result_mapInv = {Dfp25.of(1),Dfp25.of(0.5d),Dfp25.of(1, 3)};
Assert.assertArrayEquals("compare vectors" ,result_mapInv,v_mapInv.toArray());
//octave = v1 .^-1
- FieldVector<Fraction> v_mapInvToSelf = v1.copy();
+ FieldVector<Dfp> v_mapInvToSelf = v1.copy();
v_mapInvToSelf.mapInvToSelf();
- Fraction[] result_mapInvToSelf = {new Fraction(1),new Fraction(0.5d),new Fraction(3.333333333333333e-01d)};
+ Dfp[] result_mapInvToSelf = {Dfp25.of(1),Dfp25.of(0.5d),Dfp25.of(1, 3)};
Assert.assertArrayEquals("compare vectors" ,result_mapInvToSelf,v_mapInvToSelf.toArray());
}
@Test
- public void testBasicFunctions() throws FractionConversionException {
- SparseFieldVector<Fraction> v1 = new SparseFieldVector<>(field,vec1);
- SparseFieldVector<Fraction> v2 = new SparseFieldVector<>(field,vec2);
+ public void testBasicFunctions() {
+ SparseFieldVector<Dfp> v1 = new SparseFieldVector<>(field,vec1);
+ SparseFieldVector<Dfp> v2 = new SparseFieldVector<>(field,vec2);
- FieldVector<Fraction> v2_t = new ArrayFieldVectorTest.FieldVectorTestImpl<>(vec2);
+ FieldVector<Dfp> v2_t = new ArrayFieldVectorTest.FieldVectorTestImpl<>(vec2);
//octave = v1 + v2
- FieldVector<Fraction> v_add = v1.add(v2);
- Fraction[] result_add = {new Fraction(5), new Fraction(7), new Fraction(9)};
+ FieldVector<Dfp> v_add = v1.add(v2);
+ Dfp[] result_add = {Dfp25.of(5), Dfp25.of(7), Dfp25.of(9)};
Assert.assertArrayEquals("compare vect" ,v_add.toArray(),result_add);
- FieldVector<Fraction> vt2 = new ArrayFieldVectorTest.FieldVectorTestImpl<>(vec2);
- FieldVector<Fraction> v_add_i = v1.add(vt2);
- Fraction[] result_add_i = {new Fraction(5), new Fraction(7), new Fraction(9)};
+ FieldVector<Dfp> vt2 = new ArrayFieldVectorTest.FieldVectorTestImpl<>(vec2);
+ FieldVector<Dfp> v_add_i = v1.add(vt2);
+ Dfp[] result_add_i = {Dfp25.of(5), Dfp25.of(7), Dfp25.of(9)};
Assert.assertArrayEquals("compare vect" ,v_add_i.toArray(),result_add_i);
//octave = v1 - v2
- SparseFieldVector<Fraction> v_subtract = v1.subtract(v2);
- Fraction[] result_subtract = {new Fraction(-3), new Fraction(-3), new Fraction(-3)};
+ SparseFieldVector<Dfp> v_subtract = v1.subtract(v2);
+ Dfp[] result_subtract = {Dfp25.of(-3), Dfp25.of(-3), Dfp25.of(-3)};
assertClose("compare vect" ,v_subtract.toArray(),result_subtract,normTolerance);
- FieldVector<Fraction> v_subtract_i = v1.subtract(vt2);
- Fraction[] result_subtract_i = {new Fraction(-3), new Fraction(-3), new Fraction(-3)};
+ FieldVector<Dfp> v_subtract_i = v1.subtract(vt2);
+ Dfp[] result_subtract_i = {Dfp25.of(-3), Dfp25.of(-3), Dfp25.of(-3)};
assertClose("compare vect" ,v_subtract_i.toArray(),result_subtract_i,normTolerance);
// octave v1 .* v2
- FieldVector<Fraction> v_ebeMultiply = v1.ebeMultiply(v2);
- Fraction[] result_ebeMultiply = {new Fraction(4), new Fraction(10), new Fraction(18)};
+ FieldVector<Dfp> v_ebeMultiply = v1.ebeMultiply(v2);
+ Dfp[] result_ebeMultiply = {Dfp25.of(4), Dfp25.of(10), Dfp25.of(18)};
assertClose("compare vect" ,v_ebeMultiply.toArray(),result_ebeMultiply,normTolerance);
- FieldVector<Fraction> v_ebeMultiply_2 = v1.ebeMultiply(v2_t);
- Fraction[] result_ebeMultiply_2 = {new Fraction(4), new Fraction(10), new Fraction(18)};
+ FieldVector<Dfp> v_ebeMultiply_2 = v1.ebeMultiply(v2_t);
+ Dfp[] result_ebeMultiply_2 = {Dfp25.of(4), Dfp25.of(10), Dfp25.of(18)};
assertClose("compare vect" ,v_ebeMultiply_2.toArray(),result_ebeMultiply_2,normTolerance);
// octave v1 ./ v2
- FieldVector<Fraction> v_ebeDivide = v1.ebeDivide(v2);
- Fraction[] result_ebeDivide = {new Fraction(0.25d), new Fraction(0.4d), new Fraction(0.5d)};
+ FieldVector<Dfp> v_ebeDivide = v1.ebeDivide(v2);
+ Dfp[] result_ebeDivide = {Dfp25.of(0.25d), Dfp25.of(0.4d), Dfp25.of(0.5d)};
assertClose("compare vect" ,v_ebeDivide.toArray(),result_ebeDivide,normTolerance);
- FieldVector<Fraction> v_ebeDivide_2 = v1.ebeDivide(v2_t);
- Fraction[] result_ebeDivide_2 = {new Fraction(0.25d), new Fraction(0.4d), new Fraction(0.5d)};
+ FieldVector<Dfp> v_ebeDivide_2 = v1.ebeDivide(v2_t);
+ Dfp[] result_ebeDivide_2 = {Dfp25.of(0.25d), Dfp25.of(0.4d), Dfp25.of(0.5d)};
assertClose("compare vect" ,v_ebeDivide_2.toArray(),result_ebeDivide_2,normTolerance);
// octave dot(v1,v2)
- Fraction dot = v1.dotProduct(v2);
- Assert.assertEquals("compare val ",new Fraction(32), dot);
+ Dfp dot = v1.dotProduct(v2);
+ Assert.assertEquals("compare val ",Dfp25.of(32), dot);
// octave dot(v1,v2_t)
- Fraction dot_2 = v1.dotProduct(v2_t);
- Assert.assertEquals("compare val ",new Fraction(32), dot_2);
+ Dfp dot_2 = v1.dotProduct(v2_t);
+ Assert.assertEquals("compare val ",Dfp25.of(32), dot_2);
- FieldMatrix<Fraction> m_outerProduct = v1.outerProduct(v2);
- Assert.assertEquals("compare val ",new Fraction(4), m_outerProduct.getEntry(0,0));
+ FieldMatrix<Dfp> m_outerProduct = v1.outerProduct(v2);
+ Assert.assertEquals("compare val ",Dfp25.of(4), m_outerProduct.getEntry(0,0));
- FieldMatrix<Fraction> m_outerProduct_2 = v1.outerProduct(v2_t);
- Assert.assertEquals("compare val ",new Fraction(4), m_outerProduct_2.getEntry(0,0));
+ FieldMatrix<Dfp> m_outerProduct_2 = v1.outerProduct(v2_t);
+ Assert.assertEquals("compare val ",Dfp25.of(4), m_outerProduct_2.getEntry(0,0));
}
@Test
public void testOuterProduct() {
- final SparseFieldVector<Fraction> u
- = new SparseFieldVector<>(FractionField.getInstance(),
- new Fraction[] {new Fraction(1),
- new Fraction(2),
- new Fraction(-3)});
- final SparseFieldVector<Fraction> v
- = new SparseFieldVector<>(FractionField.getInstance(),
- new Fraction[] {new Fraction(4),
- new Fraction(-2)});
-
- final FieldMatrix<Fraction> uv = u.outerProduct(v);
+ final SparseFieldVector<Dfp> u
+ = new SparseFieldVector<>(Dfp25.getField(),
+ new Dfp[] {Dfp25.of(1),
+ Dfp25.of(2),
+ Dfp25.of(-3)});
+ final SparseFieldVector<Dfp> v
+ = new SparseFieldVector<>(Dfp25.getField(),
+ new Dfp[] {Dfp25.of(4),
+ Dfp25.of(-2)});
+
+ final FieldMatrix<Dfp> uv = u.outerProduct(v);
final double tol = Math.ulp(1d);
- Assert.assertEquals(new Fraction(4).doubleValue(), uv.getEntry(0, 0).doubleValue(), tol);
- Assert.assertEquals(new Fraction(-2).doubleValue(), uv.getEntry(0, 1).doubleValue(), tol);
- Assert.assertEquals(new Fraction(8).doubleValue(), uv.getEntry(1, 0).doubleValue(), tol);
- Assert.assertEquals(new Fraction(-4).doubleValue(), uv.getEntry(1, 1).doubleValue(), tol);
- Assert.assertEquals(new Fraction(-12).doubleValue(), uv.getEntry(2, 0).doubleValue(), tol);
- Assert.assertEquals(new Fraction(6).doubleValue(), uv.getEntry(2, 1).doubleValue(), tol);
+ Assert.assertEquals(Dfp25.of(4).toDouble(), uv.getEntry(0, 0).toDouble(), tol);
+ Assert.assertEquals(Dfp25.of(-2).toDouble(), uv.getEntry(0, 1).toDouble(), tol);
+ Assert.assertEquals(Dfp25.of(8).toDouble(), uv.getEntry(1, 0).toDouble(), tol);
+ Assert.assertEquals(Dfp25.of(-4).toDouble(), uv.getEntry(1, 1).toDouble(), tol);
+ Assert.assertEquals(Dfp25.of(-12).toDouble(), uv.getEntry(2, 0).toDouble(), tol);
+ Assert.assertEquals(Dfp25.of(6).toDouble(), uv.getEntry(2, 1).toDouble(), tol);
}
@Test
public void testMisc() {
- SparseFieldVector<Fraction> v1 = new SparseFieldVector<>(field,vec1);
+ SparseFieldVector<Dfp> v1 = new SparseFieldVector<>(field,vec1);
String out1 = v1.toString();
Assert.assertTrue("some output ", out1.length()!=0);
@@ -220,16 +219,16 @@ public class SparseFieldVectorTest {
@Test
public void testPredicates() {
- SparseFieldVector<Fraction> v = new SparseFieldVector<>(field, new Fraction[] { new Fraction(0), new Fraction(1), new Fraction(2) });
+ SparseFieldVector<Dfp> v = new SparseFieldVector<>(field, new Dfp[] { Dfp25.of(0), Dfp25.of(1), Dfp25.of(2) });
v.setEntry(0, field.getZero());
- Assert.assertEquals(v, new SparseFieldVector<>(field, new Fraction[] { new Fraction(0), new Fraction(1), new Fraction(2) }));
- Assert.assertNotSame(v, new SparseFieldVector<>(field, new Fraction[] { new Fraction(0), new Fraction(1), new Fraction(2), new Fraction(3) }));
+ Assert.assertEquals(v, new SparseFieldVector<>(field, new Dfp[] { Dfp25.of(0), Dfp25.of(1), Dfp25.of(2) }));
+ Assert.assertNotSame(v, new SparseFieldVector<>(field, new Dfp[] { Dfp25.of(0), Dfp25.of(1), Dfp25.of(2), Dfp25.of(3) }));
}
/** verifies that two vectors are close (sup norm) */
- protected void assertEquals(String msg, Fraction[] m, Fraction[] n) {
+ protected void assertEquals(String msg, Dfp[] m, Dfp[] n) {
if (m.length != n.length) {
Assert.fail("vectors have different lengths");
}
@@ -239,12 +238,12 @@ public class SparseFieldVectorTest {
}
/** verifies that two vectors are close (sup norm) */
- protected void assertClose(String msg, Fraction[] m, Fraction[] n, double tolerance) {
+ protected void assertClose(String msg, Dfp[] m, Dfp[] n, double tolerance) {
if (m.length != n.length) {
Assert.fail("vectors have different lengths");
}
for (int i = 0; i < m.length; i++) {
- Assert.assertEquals(msg + " " + i + " elements differ", m[i].doubleValue(),n[i].doubleValue(), tolerance);
+ Assert.assertEquals(msg + " " + i + " elements differ", m[i].toDouble(),n[i].toDouble(), tolerance);
}
}
@@ -255,19 +254,19 @@ public class SparseFieldVectorTest {
/** The whole vector is visited. */
@Test
public void testWalkInDefaultOrderPreservingVisitor1() {
- final Fraction[] data = new Fraction[] {
- Fraction.ZERO, Fraction.ONE, Fraction.ZERO,
- Fraction.ZERO, Fraction.TWO, Fraction.ZERO,
- Fraction.ZERO, Fraction.ZERO, new Fraction(3)
+ final Dfp[] data = new Dfp[] {
+ Dfp25.ZERO, Dfp25.ONE, Dfp25.ZERO,
+ Dfp25.ZERO, Dfp25.TWO, Dfp25.ZERO,
+ Dfp25.ZERO, Dfp25.ZERO, Dfp25.of(3)
};
- final SparseFieldVector<Fraction> v = new SparseFieldVector<>(field, data);
- final FieldVectorPreservingVisitor<Fraction> visitor;
- visitor = new FieldVectorPreservingVisitor<Fraction>() {
+ final SparseFieldVector<Dfp> v = new SparseFieldVector<>(field, data);
+ final FieldVectorPreservingVisitor<Dfp> visitor;
+ visitor = new FieldVectorPreservingVisitor<Dfp>() {
private int expectedIndex;
@Override
- public void visit(final int actualIndex, final Fraction actualValue) {
+ public void visit(final int actualIndex, final Dfp actualValue) {
Assert.assertEquals(expectedIndex, actualIndex);
Assert.assertEquals(Integer.toString(actualIndex),
data[actualIndex], actualValue);
@@ -284,8 +283,8 @@ public class SparseFieldVectorTest {
}
@Override
- public Fraction end() {
- return Fraction.ZERO;
+ public Dfp end() {
+ return Dfp25.ZERO;
}
};
v.walkInDefaultOrder(visitor);
@@ -294,12 +293,12 @@ public class SparseFieldVectorTest {
/** Visiting an invalid subvector. */
@Test
public void testWalkInDefaultOrderPreservingVisitor2() {
- final SparseFieldVector<Fraction> v = create(5);
- final FieldVectorPreservingVisitor<Fraction> visitor;
- visitor = new FieldVectorPreservingVisitor<Fraction>() {
+ final SparseFieldVector<Dfp> v = create(5);
+ final FieldVectorPreservingVisitor<Dfp> visitor;
+ visitor = new FieldVectorPreservingVisitor<Dfp>() {
@Override
- public void visit(int index, Fraction value) {
+ public void visit(int index, Dfp value) {
// Do nothing
}
@@ -309,8 +308,8 @@ public class SparseFieldVectorTest {
}
@Override
- public Fraction end() {
- return Fraction.ZERO;
+ public Dfp end() {
+ return Dfp25.ZERO;
}
};
try {
@@ -348,21 +347,21 @@ public class SparseFieldVectorTest {
/** Visiting a valid subvector. */
@Test
public void testWalkInDefaultOrderPreservingVisitor3() {
- final Fraction[] data = new Fraction[] {
- Fraction.ZERO, Fraction.ONE, Fraction.ZERO,
- Fraction.ZERO, Fraction.TWO, Fraction.ZERO,
- Fraction.ZERO, Fraction.ZERO, new Fraction(3)
+ final Dfp[] data = new Dfp[] {
+ Dfp25.ZERO, Dfp25.ONE, Dfp25.ZERO,
+ Dfp25.ZERO, Dfp25.TWO, Dfp25.ZERO,
+ Dfp25.ZERO, Dfp25.ZERO, Dfp25.of(3)
};
- final SparseFieldVector<Fraction> v = new SparseFieldVector<>(field, data);
+ final SparseFieldVector<Dfp> v = new SparseFieldVector<>(field, data);
final int expectedStart = 2;
final int expectedEnd = 7;
- final FieldVectorPreservingVisitor<Fraction> visitor;
- visitor = new FieldVectorPreservingVisitor<Fraction>() {
+ final FieldVectorPreservingVisitor<Dfp> visitor;
+ visitor = new FieldVectorPreservingVisitor<Dfp>() {
private int expectedIndex;
@Override
- public void visit(final int actualIndex, final Fraction actualValue) {
+ public void visit(final int actualIndex, final Dfp actualValue) {
Assert.assertEquals(expectedIndex, actualIndex);
Assert.assertEquals(Integer.toString(actualIndex),
data[actualIndex], actualValue);
@@ -379,8 +378,8 @@ public class SparseFieldVectorTest {
}
@Override
- public Fraction end() {
- return Fraction.ZERO;
+ public Dfp end() {
+ return Dfp25.ZERO;
}
};
v.walkInDefaultOrder(visitor, expectedStart, expectedEnd);
@@ -389,18 +388,18 @@ public class SparseFieldVectorTest {
/** The whole vector is visited. */
@Test
public void testWalkInOptimizedOrderPreservingVisitor1() {
- final Fraction[] data = new Fraction[] {
- Fraction.ZERO, Fraction.ONE, Fraction.ZERO,
- Fraction.ZERO, Fraction.TWO, Fraction.ZERO,
- Fraction.ZERO, Fraction.ZERO, new Fraction(3)
+ final Dfp[] data = new Dfp[] {
+ Dfp25.ZERO, Dfp25.ONE, Dfp25.ZERO,
+ Dfp25.ZERO, Dfp25.TWO, Dfp25.ZERO,
+ Dfp25.ZERO, Dfp25.ZERO, Dfp25.of(3)
};
- final SparseFieldVector<Fraction> v = new SparseFieldVector<>(field, data);
- final FieldVectorPreservingVisitor<Fraction> visitor;
- visitor = new FieldVectorPreservingVisitor<Fraction>() {
+ final SparseFieldVector<Dfp> v = new SparseFieldVector<>(field, data);
+ final FieldVectorPreservingVisitor<Dfp> visitor;
+ visitor = new FieldVectorPreservingVisitor<Dfp>() {
private final boolean[] visited = new boolean[data.length];
@Override
- public void visit(final int actualIndex, final Fraction actualValue) {
+ public void visit(final int actualIndex, final Dfp actualValue) {
visited[actualIndex] = true;
Assert.assertEquals(Integer.toString(actualIndex),
data[actualIndex], actualValue);
@@ -416,12 +415,12 @@ public class SparseFieldVectorTest {
}
@Override
- public Fraction end() {
+ public Dfp end() {
for (int i = 0; i < data.length; i++) {
Assert.assertTrue("entry " + i + "has not been visited",
visited[i]);
}
- return Fraction.ZERO;
+ return Dfp25.ZERO;
}
};
v.walkInOptimizedOrder(visitor);
@@ -430,12 +429,12 @@ public class SparseFieldVectorTest {
/** Visiting an invalid subvector. */
@Test
public void testWalkInOptimizedOrderPreservingVisitor2() {
- final SparseFieldVector<Fraction> v = create(5);
- final FieldVectorPreservingVisitor<Fraction> visitor;
- visitor = new FieldVectorPreservingVisitor<Fraction>() {
+ final SparseFieldVector<Dfp> v = create(5);
+ final FieldVectorPreservingVisitor<Dfp> visitor;
+ visitor = new FieldVectorPreservingVisitor<Dfp>() {
@Override
- public void visit(int index, Fraction value) {
+ public void visit(int index, Dfp value) {
// Do nothing
}
@@ -445,8 +444,8 @@ public class SparseFieldVectorTest {
}
@Override
- public Fraction end() {
- return Fraction.ZERO;
+ public Dfp end() {
+ return Dfp25.ZERO;
}
};
try {
@@ -484,20 +483,20 @@ public class SparseFieldVectorTest {
/** Visiting a valid subvector. */
@Test
public void testWalkInOptimizedOrderPreservingVisitor3() {
- final Fraction[] data = new Fraction[] {
- Fraction.ZERO, Fraction.ONE, Fraction.ZERO,
- Fraction.ZERO, Fraction.TWO, Fraction.ZERO,
- Fraction.ZERO, Fraction.ZERO, new Fraction(3)
+ final Dfp[] data = new Dfp[] {
+ Dfp25.ZERO, Dfp25.ONE, Dfp25.ZERO,
+ Dfp25.ZERO, Dfp25.TWO, Dfp25.ZERO,
+ Dfp25.ZERO, Dfp25.ZERO, Dfp25.of(3)
};
- final SparseFieldVector<Fraction> v = new SparseFieldVector<>(field, data);
+ final SparseFieldVector<Dfp> v = new SparseFieldVector<>(field, data);
final int expectedStart = 2;
final int expectedEnd = 7;
- final FieldVectorPreservingVisitor<Fraction> visitor;
- visitor = new FieldVectorPreservingVisitor<Fraction>() {
+ final FieldVectorPreservingVisitor<Dfp> visitor;
+ visitor = new FieldVectorPreservingVisitor<Dfp>() {
private final boolean[] visited = new boolean[data.length];
@Override
- public void visit(final int actualIndex, final Fraction actualValue) {
+ public void visit(final int actualIndex, final Dfp actualValue) {
Assert.assertEquals(Integer.toString(actualIndex),
data[actualIndex], actualValue);
visited[actualIndex] = true;
@@ -513,12 +512,12 @@ public class SparseFieldVectorTest {
}
@Override
- public Fraction end() {
+ public Dfp end() {
for (int i = expectedStart; i <= expectedEnd; i++) {
Assert.assertTrue("entry " + i + "has not been visited",
visited[i]);
}
- return Fraction.ZERO;
+ return Dfp25.ZERO;
}
};
v.walkInOptimizedOrder(visitor, expectedStart, expectedEnd);
@@ -527,19 +526,19 @@ public class SparseFieldVectorTest {
/** The whole vector is visited. */
@Test
public void testWalkInDefaultOrderChangingVisitor1() {
- final Fraction[] data = new Fraction[] {
- Fraction.ZERO, Fraction.ONE, Fraction.ZERO,
- Fraction.ZERO, Fraction.TWO, Fraction.ZERO,
- Fraction.ZERO, Fraction.ZERO, new Fraction(3)
+ final Dfp[] data = new Dfp[] {
+ Dfp25.ZERO, Dfp25.ONE, Dfp25.ZERO,
+ Dfp25.ZERO, Dfp25.TWO, Dfp25.ZERO,
+ Dfp25.ZERO, Dfp25.ZERO, Dfp25.of(3)
};
- final SparseFieldVector<Fraction> v = new SparseFieldVector<>(field, data);
- final FieldVectorChangingVisitor<Fraction> visitor;
- visitor = new FieldVectorChangingVisitor<Fraction>() {
+ final SparseFieldVector<Dfp> v = new SparseFieldVector<>(field, data);
+ final FieldVectorChangingVisitor<Dfp> visitor;
+ visitor = new FieldVectorChangingVisitor<Dfp>() {
private int expectedIndex;
@Override
- public Fraction visit(final int actualIndex, final Fraction actualValue) {
+ public Dfp visit(final int actualIndex, final Dfp actualValue) {
Assert.assertEquals(expectedIndex, actualIndex);
Assert.assertEquals(Integer.toString(actualIndex),
data[actualIndex], actualValue);
@@ -557,8 +556,8 @@ public class SparseFieldVectorTest {
}
@Override
- public Fraction end() {
- return Fraction.ZERO;
+ public Dfp end() {
+ return Dfp25.ZERO;
}
};
v.walkInDefaultOrder(visitor);
@@ -570,13 +569,13 @@ public class SparseFieldVectorTest {
/** Visiting an invalid subvector. */
@Test
public void testWalkInDefaultOrderChangingVisitor2() {
- final SparseFieldVector<Fraction> v = create(5);
- final FieldVectorChangingVisitor<Fraction> visitor;
- visitor = new FieldVectorChangingVisitor<Fraction>() {
+ final SparseFieldVector<Dfp> v = create(5);
+ final FieldVectorChangingVisitor<Dfp> visitor;
+ visitor = new FieldVectorChangingVisitor<Dfp>() {
@Override
- public Fraction visit(int index, Fraction value) {
- return Fraction.ZERO;
+ public Dfp visit(int index, Dfp value) {
+ return Dfp25.ZERO;
}
@Override
@@ -585,8 +584,8 @@ public class SparseFieldVectorTest {
}
@Override
- public Fraction end() {
- return Fraction.ZERO;
+ public Dfp end() {
+ return Dfp25.ZERO;
}
};
try {
@@ -624,21 +623,21 @@ public class SparseFieldVectorTest {
/** Visiting a valid subvector. */
@Test
public void testWalkInDefaultOrderChangingVisitor3() {
- final Fraction[] data = new Fraction[] {
- Fraction.ZERO, Fraction.ONE, Fraction.ZERO,
- Fraction.ZERO, Fraction.TWO, Fraction.ZERO,
- Fraction.ZERO, Fraction.ZERO, new Fraction(3)
+ final Dfp[] data = new Dfp[] {
+ Dfp25.ZERO, Dfp25.ONE, Dfp25.ZERO,
+ Dfp25.ZERO, Dfp25.TWO, Dfp25.ZERO,
+ Dfp25.ZERO, Dfp25.ZERO, Dfp25.of(3)
};
- final SparseFieldVector<Fraction> v = new SparseFieldVector<>(field, data);
+ final SparseFieldVector<Dfp> v = new SparseFieldVector<>(field, data);
final int expectedStart = 2;
final int expectedEnd = 7;
- final FieldVectorChangingVisitor<Fraction> visitor;
- visitor = new FieldVectorChangingVisitor<Fraction>() {
+ final FieldVectorChangingVisitor<Dfp> visitor;
+ visitor = new FieldVectorChangingVisitor<Dfp>() {
private int expectedIndex;
@Override
- public Fraction visit(final int actualIndex, final Fraction actualValue) {
+ public Dfp visit(final int actualIndex, final Dfp actualValue) {
Assert.assertEquals(expectedIndex, actualIndex);
Assert.assertEquals(Integer.toString(actualIndex),
data[actualIndex], actualValue);
@@ -656,8 +655,8 @@ public class SparseFieldVectorTest {
}
@Override
- public Fraction end() {
- return Fraction.ZERO;
+ public Dfp end() {
+ return Dfp25.ZERO;
}
};
v.walkInDefaultOrder(visitor, expectedStart, expectedEnd);
@@ -669,18 +668,18 @@ public class SparseFieldVectorTest {
/** The whole vector is visited. */
@Test
public void testWalkInOptimizedOrderChangingVisitor1() {
- final Fraction[] data = new Fraction[] {
- Fraction.ZERO, Fraction.ONE, Fraction.ZERO,
- Fraction.ZERO, Fraction.TWO, Fraction.ZERO,
- Fraction.ZERO, Fraction.ZERO, new Fraction(3)
+ final Dfp[] data = new Dfp[] {
+ Dfp25.ZERO, Dfp25.ONE, Dfp25.ZERO,
+ Dfp25.ZERO, Dfp25.TWO, Dfp25.ZERO,
+ Dfp25.ZERO, Dfp25.ZERO, Dfp25.of(3)
};
- final SparseFieldVector<Fraction> v = new SparseFieldVector<>(field, data);
- final FieldVectorChangingVisitor<Fraction> visitor;
- visitor = new FieldVectorChangingVisitor<Fraction>() {
+ final SparseFieldVector<Dfp> v = new SparseFieldVector<>(field, data);
+ final FieldVectorChangingVisitor<Dfp> visitor;
+ visitor = new FieldVectorChangingVisitor<Dfp>() {
private final boolean[] visited = new boolean[data.length];
@Override
- public Fraction visit(final int actualIndex, final Fraction actualValue) {
+ public Dfp visit(final int actualIndex, final Dfp actualValue) {
visited[actualIndex] = true;
Assert.assertEquals(Integer.toString(actualIndex),
data[actualIndex], actualValue);
@@ -697,12 +696,12 @@ public class SparseFieldVectorTest {
}
@Override
- public Fraction end() {
+ public Dfp end() {
for (int i = 0; i < data.length; i++) {
Assert.assertTrue("entry " + i + "has not been visited",
visited[i]);
}
- return Fraction.ZERO;
+ return Dfp25.ZERO;
}
};
v.walkInOptimizedOrder(visitor);
@@ -714,13 +713,13 @@ public class SparseFieldVectorTest {
/** Visiting an invalid subvector. */
@Test
public void testWalkInOptimizedOrderChangingVisitor2() {
- final SparseFieldVector<Fraction> v = create(5);
- final FieldVectorChangingVisitor<Fraction> visitor;
- visitor = new FieldVectorChangingVisitor<Fraction>() {
+ final SparseFieldVector<Dfp> v = create(5);
+ final FieldVectorChangingVisitor<Dfp> visitor;
+ visitor = new FieldVectorChangingVisitor<Dfp>() {
@Override
- public Fraction visit(int index, Fraction value) {
- return Fraction.ZERO;
+ public Dfp visit(int index, Dfp value) {
+ return Dfp25.ZERO;
}
@Override
@@ -729,8 +728,8 @@ public class SparseFieldVectorTest {
}
@Override
- public Fraction end() {
- return Fraction.ZERO;
+ public Dfp end() {
+ return Dfp25.ZERO;
}
};
try {
@@ -768,20 +767,20 @@ public class SparseFieldVectorTest {
/** Visiting a valid subvector. */
@Test
public void testWalkInOptimizedOrderChangingVisitor3() {
- final Fraction[] data = new Fraction[] {
- Fraction.ZERO, Fraction.ONE, Fraction.ZERO,
- Fraction.ZERO, Fraction.TWO, Fraction.ZERO,
- Fraction.ZERO, Fraction.ZERO, new Fraction(3)
+ final Dfp[] data = new Dfp[] {
+ Dfp25.ZERO, Dfp25.ONE, Dfp25.ZERO,
+ Dfp25.ZERO, Dfp25.TWO, Dfp25.ZERO,
+ Dfp25.ZERO, Dfp25.ZERO, Dfp25.of(3)
};
- final SparseFieldVector<Fraction> v = new SparseFieldVector<>(field, data);
+ final SparseFieldVector<Dfp> v = new SparseFieldVector<>(field, data);
final int expectedStart = 2;
final int expectedEnd = 7;
- final FieldVectorChangingVisitor<Fraction> visitor;
- visitor = new FieldVectorChangingVisitor<Fraction>() {
+ final FieldVectorChangingVisitor<Dfp> visitor;
+ visitor = new FieldVectorChangingVisitor<Dfp>() {
private final boolean[] visited = new boolean[data.length];
@Override
- public Fraction visit(final int actualIndex, final Fraction actualValue) {
+ public Dfp visit(final int actualIndex, final Dfp actualValue) {
Assert.assertEquals(Integer.toString(actualIndex),
data[actualIndex], actualValue);
visited[actualIndex] = true;
@@ -798,12 +797,12 @@ public class SparseFieldVectorTest {
}
@Override
- public Fraction end() {
+ public Dfp end() {
for (int i = expectedStart; i <= expectedEnd; i++) {
Assert.assertTrue("entry " + i + "has not been visited",
visited[i]);
}
- return Fraction.ZERO;
+ return Dfp25.ZERO;
}
};
v.walkInOptimizedOrder(visitor, expectedStart, expectedEnd);
@@ -812,10 +811,10 @@ public class SparseFieldVectorTest {
}
}
- private SparseFieldVector<Fraction> create(int n) {
- Fraction[] t = new Fraction[n];
+ private SparseFieldVector<Dfp> create(int n) {
+ Dfp[] t = new Dfp[n];
for (int i = 0; i < n; ++i) {
- t[i] = Fraction.ZERO;
+ t[i] = Dfp25.ZERO;
}
return new SparseFieldVector<>(field, t);
}
diff --git a/src/test/java/org/apache/commons/math4/util/OpenIntToFieldTest.java b/src/test/java/org/apache/commons/math4/util/OpenIntToFieldTest.java
index 1be299b..229425d 100644
--- a/src/test/java/org/apache/commons/math4/util/OpenIntToFieldTest.java
+++ b/src/test/java/org/apache/commons/math4/util/OpenIntToFieldTest.java
@@ -26,9 +26,9 @@ import java.util.Set;
import java.util.Map.Entry;
import org.apache.commons.math4.Field;
-import org.apache.commons.math4.fraction.Fraction;
-import org.apache.commons.math4.fraction.FractionConversionException;
-import org.apache.commons.math4.fraction.FractionField;
+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.util.OpenIntToFieldHashMap;
import org.junit.Assert;
import org.junit.Before;
@@ -38,47 +38,43 @@ import org.junit.Test;
@SuppressWarnings("boxing")
public class OpenIntToFieldTest {
- private Map<Integer, Fraction> javaMap = new HashMap<>();
- private FractionField field = FractionField.getInstance();
+ private Map<Integer, Dfp> javaMap = new HashMap<>();
+ private DfpField field = Dfp25.getField();
@Before
- public void setUp() throws FractionConversionException {
- javaMap.put(50, new Fraction(100.0));
- javaMap.put(75, new Fraction(75.0));
- javaMap.put(25, new Fraction(500.0));
- javaMap.put(Integer.MAX_VALUE, new Fraction(Integer.MAX_VALUE));
- javaMap.put(0, new Fraction(-1.0));
- javaMap.put(1, new Fraction(0.0));
- javaMap.put(33, new Fraction(-0.1));
- javaMap.put(23234234, new Fraction(-242343.0));
- javaMap.put(23321, new Fraction (Integer.MIN_VALUE));
- javaMap.put(-4444, new Fraction(332.0));
- javaMap.put(-1, new Fraction(-2323.0));
- javaMap.put(Integer.MIN_VALUE, new Fraction(44.0));
+ public void setUp() {
+ javaMap.put(50, Dfp25.of(100.0));
+ javaMap.put(75, Dfp25.of(75.0));
+ javaMap.put(25, Dfp25.of(500.0));
+ javaMap.put(Integer.MAX_VALUE, Dfp25.of(Integer.MAX_VALUE));
+ javaMap.put(0, Dfp25.of(-1.0));
+ javaMap.put(1, Dfp25.of(0.0));
+ javaMap.put(33, Dfp25.of(-0.1));
+ javaMap.put(23234234, Dfp25.of(-242343.0));
+ javaMap.put(23321, Dfp25.of (Integer.MIN_VALUE));
+ javaMap.put(-4444, Dfp25.of(332.0));
+ javaMap.put(-1, Dfp25.of(-2323.0));
+ javaMap.put(Integer.MIN_VALUE, Dfp25.of(44.0));
/* Add a few more to cause the table to rehash */
javaMap.putAll(generate());
}
- private Map<Integer, Fraction> generate() {
- Map<Integer, Fraction> map = new HashMap<>();
+ private Map<Integer, Dfp> generate() {
+ Map<Integer, Dfp> map = new HashMap<>();
Random r = new Random();
double dd=0;
for (int i = 0; i < 2000; ++i) {
dd = r.nextDouble();
}
- try {
- map.put(r.nextInt(), new Fraction(dd));
- } catch (FractionConversionException e) {
- throw new IllegalStateException("Invalid :"+dd, e);
- }
+ map.put(r.nextInt(), Dfp25.of(dd));
return map;
}
- private OpenIntToFieldHashMap<Fraction> createFromJavaMap(Field<Fraction> field) {
- OpenIntToFieldHashMap<Fraction> map = new OpenIntToFieldHashMap<>(field);
- for (Map.Entry<Integer, Fraction> mapEntry : javaMap.entrySet()) {
+ private OpenIntToFieldHashMap<Dfp> createFromJavaMap(Field<Dfp> field) {
+ OpenIntToFieldHashMap<Dfp> map = new OpenIntToFieldHashMap<>(field);
+ for (Map.Entry<Integer, Dfp> mapEntry : javaMap.entrySet()) {
map.put(mapEntry.getKey(), mapEntry.getValue());
}
return map;
@@ -86,30 +82,30 @@ public class OpenIntToFieldTest {
@Test
public void testPutAndGetWith0ExpectedSize() {
- OpenIntToFieldHashMap<Fraction> map = new OpenIntToFieldHashMap<>(field,0);
+ OpenIntToFieldHashMap<Dfp> map = new OpenIntToFieldHashMap<>(field,0);
assertPutAndGet(map);
}
@Test
public void testPutAndGetWithExpectedSize() {
- OpenIntToFieldHashMap<Fraction> map = new OpenIntToFieldHashMap<>(field,500);
+ OpenIntToFieldHashMap<Dfp> map = new OpenIntToFieldHashMap<>(field,500);
assertPutAndGet(map);
}
@Test
public void testPutAndGet() {
- OpenIntToFieldHashMap<Fraction> map = new OpenIntToFieldHashMap<>(field);
+ OpenIntToFieldHashMap<Dfp> map = new OpenIntToFieldHashMap<>(field);
assertPutAndGet(map);
}
- private void assertPutAndGet(OpenIntToFieldHashMap<Fraction> map) {
+ private void assertPutAndGet(OpenIntToFieldHashMap<Dfp> map) {
assertPutAndGet(map, 0, new HashSet<Integer>());
}
- private void assertPutAndGet(OpenIntToFieldHashMap<Fraction> map, int mapSize,
+ private void assertPutAndGet(OpenIntToFieldHashMap<Dfp> map, int mapSize,
Set<Integer> keysInMap) {
Assert.assertEquals(mapSize, map.size());
- for (Map.Entry<Integer, Fraction> mapEntry : javaMap.entrySet()) {
+ for (Map.Entry<Integer, Dfp> mapEntry : javaMap.entrySet()) {
map.put(mapEntry.getKey(), mapEntry.getValue());
if (!keysInMap.contains(mapEntry.getKey())) {
++mapSize;
@@ -121,9 +117,9 @@ public class OpenIntToFieldTest {
@Test
public void testPutAbsentOnExisting() {
- OpenIntToFieldHashMap<Fraction> map = createFromJavaMap(field);
+ OpenIntToFieldHashMap<Dfp> map = createFromJavaMap(field);
int size = javaMap.size();
- for (Map.Entry<Integer, Fraction> mapEntry : generateAbsent().entrySet()) {
+ for (Map.Entry<Integer, Dfp> mapEntry : generateAbsent().entrySet()) {
map.put(mapEntry.getKey(), mapEntry.getValue());
Assert.assertEquals(++size, map.size());
Assert.assertEquals(mapEntry.getValue(), map.get(mapEntry.getKey()));
@@ -132,8 +128,8 @@ public class OpenIntToFieldTest {
@Test
public void testPutOnExisting() {
- OpenIntToFieldHashMap<Fraction> map = createFromJavaMap(field);
- for (Map.Entry<Integer, Fraction> mapEntry : javaMap.entrySet()) {
+ OpenIntToFieldHashMap<Dfp> map = createFromJavaMap(field);
+ for (Map.Entry<Integer, Dfp> mapEntry : javaMap.entrySet()) {
map.put(mapEntry.getKey(), mapEntry.getValue());
Assert.assertEquals(javaMap.size(), map.size());
Assert.assertEquals(mapEntry.getValue(), map.get(mapEntry.getKey()));
@@ -142,17 +138,17 @@ public class OpenIntToFieldTest {
@Test
public void testGetAbsent() {
- Map<Integer, Fraction> generated = generateAbsent();
- OpenIntToFieldHashMap<Fraction> map = createFromJavaMap(field);
+ Map<Integer, Dfp> generated = generateAbsent();
+ OpenIntToFieldHashMap<Dfp> map = createFromJavaMap(field);
- for (Map.Entry<Integer, Fraction> mapEntry : generated.entrySet()) {
+ for (Map.Entry<Integer, Dfp> mapEntry : generated.entrySet()) {
Assert.assertTrue(field.getZero().equals(map.get(mapEntry.getKey())));
}
}
@Test
public void testGetFromEmpty() {
- OpenIntToFieldHashMap<Fraction> map = new OpenIntToFieldHashMap<>(field);
+ OpenIntToFieldHashMap<Dfp> map = new OpenIntToFieldHashMap<>(field);
Assert.assertTrue(field.getZero().equals(map.get(5)));
Assert.assertTrue(field.getZero().equals(map.get(0)));
Assert.assertTrue(field.getZero().equals(map.get(50)));
@@ -160,10 +156,10 @@ public class OpenIntToFieldTest {
@Test
public void testRemove() {
- OpenIntToFieldHashMap<Fraction> map = createFromJavaMap(field);
+ OpenIntToFieldHashMap<Dfp> map = createFromJavaMap(field);
int mapSize = javaMap.size();
Assert.assertEquals(mapSize, map.size());
- for (Map.Entry<Integer, Fraction> mapEntry : javaMap.entrySet()) {
+ for (Map.Entry<Integer, Dfp> mapEntry : javaMap.entrySet()) {
map.remove(mapEntry.getKey());
Assert.assertEquals(--mapSize, map.size());
Assert.assertTrue(field.getZero().equals(map.get(mapEntry.getKey())));
@@ -176,11 +172,11 @@ public class OpenIntToFieldTest {
/* This time only remove some entries */
@Test
public void testRemove2() {
- OpenIntToFieldHashMap<Fraction> map = createFromJavaMap(field);
+ OpenIntToFieldHashMap<Dfp> map = createFromJavaMap(field);
int mapSize = javaMap.size();
int count = 0;
Set<Integer> keysInMap = new HashSet<>(javaMap.keySet());
- for (Map.Entry<Integer, Fraction> mapEntry : javaMap.entrySet()) {
+ for (Map.Entry<Integer, Dfp> mapEntry : javaMap.entrySet()) {
keysInMap.remove(mapEntry.getKey());
map.remove(mapEntry.getKey());
Assert.assertEquals(--mapSize, map.size());
@@ -196,18 +192,18 @@ public class OpenIntToFieldTest {
@Test
public void testRemoveFromEmpty() {
- OpenIntToFieldHashMap<Fraction> map = new OpenIntToFieldHashMap<>(field);
+ OpenIntToFieldHashMap<Dfp> map = new OpenIntToFieldHashMap<>(field);
Assert.assertTrue(field.getZero().equals(map.remove(50)));
}
@Test
public void testRemoveAbsent() {
- Map<Integer, Fraction> generated = generateAbsent();
+ Map<Integer, Dfp> generated = generateAbsent();
- OpenIntToFieldHashMap<Fraction> map = createFromJavaMap(field);
+ OpenIntToFieldHashMap<Dfp> map = createFromJavaMap(field);
int mapSize = map.size();
- for (Map.Entry<Integer, Fraction> mapEntry : generated.entrySet()) {
+ for (Map.Entry<Integer, Dfp> mapEntry : generated.entrySet()) {
map.remove(mapEntry.getKey());
Assert.assertEquals(mapSize, map.size());
Assert.assertTrue(field.getZero().equals(map.get(mapEntry.getKey())));
@@ -217,8 +213,8 @@ public class OpenIntToFieldTest {
/**
* Returns a map with at least 100 elements where each element is absent from javaMap.
*/
- private Map<Integer, Fraction> generateAbsent() {
- Map<Integer, Fraction> generated = new HashMap<>();
+ private Map<Integer, Dfp> generateAbsent() {
+ Map<Integer, Dfp> generated = new HashMap<>();
do {
generated.putAll(generate());
for (Integer key : javaMap.keySet()) {
@@ -230,25 +226,25 @@ public class OpenIntToFieldTest {
@Test
public void testCopy() {
- OpenIntToFieldHashMap<Fraction> copy =
+ OpenIntToFieldHashMap<Dfp> copy =
new OpenIntToFieldHashMap<>(createFromJavaMap(field));
Assert.assertEquals(javaMap.size(), copy.size());
- for (Map.Entry<Integer, Fraction> mapEntry : javaMap.entrySet()) {
+ for (Map.Entry<Integer, Dfp> mapEntry : javaMap.entrySet()) {
Assert.assertEquals(mapEntry.getValue(), copy.get(mapEntry.getKey()));
}
}
@Test
public void testContainsKey() {
- OpenIntToFieldHashMap<Fraction> map = createFromJavaMap(field);
- for (Entry<Integer, Fraction> mapEntry : javaMap.entrySet()) {
+ OpenIntToFieldHashMap<Dfp> map = createFromJavaMap(field);
+ for (Entry<Integer, Dfp> mapEntry : javaMap.entrySet()) {
Assert.assertTrue(map.containsKey(mapEntry.getKey()));
}
- for (Map.Entry<Integer, Fraction> mapEntry : generateAbsent().entrySet()) {
+ for (Map.Entry<Integer, Dfp> mapEntry : generateAbsent().entrySet()) {
Assert.assertFalse(map.containsKey(mapEntry.getKey()));
}
- for (Entry<Integer, Fraction> mapEntry : javaMap.entrySet()) {
+ for (Entry<Integer, Dfp> mapEntry : javaMap.entrySet()) {
int key = mapEntry.getKey();
Assert.assertTrue(map.containsKey(key));
map.remove(key);
@@ -258,8 +254,8 @@ public class OpenIntToFieldTest {
@Test
public void testIterator() {
- OpenIntToFieldHashMap<Fraction> map = createFromJavaMap(field);
- OpenIntToFieldHashMap<Fraction>.Iterator iterator = map.iterator();
+ OpenIntToFieldHashMap<Dfp> map = createFromJavaMap(field);
+ OpenIntToFieldHashMap<Dfp>.Iterator iterator = map.iterator();
for (int i = 0; i < map.size(); ++i) {
Assert.assertTrue(iterator.hasNext());
iterator.advance();
@@ -280,9 +276,9 @@ public class OpenIntToFieldTest {
@Test
public void testConcurrentModification() {
- OpenIntToFieldHashMap<Fraction> map = createFromJavaMap(field);
- OpenIntToFieldHashMap<Fraction>.Iterator iterator = map.iterator();
- map.put(3, new Fraction(3));
+ OpenIntToFieldHashMap<Dfp> map = createFromJavaMap(field);
+ OpenIntToFieldHashMap<Dfp>.Iterator iterator = map.iterator();
+ map.put(3, Dfp25.of(3));
try {
iterator.advance();
Assert.fail("an exception should have been thrown");
@@ -298,9 +294,9 @@ public class OpenIntToFieldTest {
*/
@Test
public void testPutKeysWithCollisions() {
- OpenIntToFieldHashMap<Fraction> map = new OpenIntToFieldHashMap<>(field);
+ OpenIntToFieldHashMap<Dfp> map = new OpenIntToFieldHashMap<>(field);
int key1 = -1996012590;
- Fraction value1 = new Fraction(1);
+ Dfp value1 = Dfp25.of(1);
map.put(key1, value1);
int key2 = 835099822;
map.put(key2, value1);
@@ -310,7 +306,7 @@ public class OpenIntToFieldTest {
Assert.assertEquals(3, map.size());
map.remove(key2);
- Fraction value2 = new Fraction(2);
+ Dfp value2 = Dfp25.of(2);
map.put(key3, value2);
Assert.assertEquals(value2, map.get(key3));
Assert.assertEquals(2, map.size());
@@ -322,9 +318,9 @@ public class OpenIntToFieldTest {
*/
@Test
public void testPutKeysWithCollision2() {
- OpenIntToFieldHashMap<Fraction>map = new OpenIntToFieldHashMap<>(field);
+ OpenIntToFieldHashMap<Dfp>map = new OpenIntToFieldHashMap<>(field);
int key1 = 837989881;
- Fraction value1 = new Fraction(1);
+ Dfp value1 = Dfp25.of(1);
map.put(key1, value1);
int key2 = 476463321;
map.put(key2, value1);
@@ -332,11 +328,9 @@ public class OpenIntToFieldTest {
Assert.assertEquals(value1, map.get(key2));
map.remove(key1);
- Fraction value2 = new Fraction(2);
+ Dfp value2 = Dfp25.of(2);
map.put(key2, value2);
Assert.assertEquals(1, map.size());
Assert.assertEquals(value2, map.get(key2));
}
-
-
}