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/10/22 13:45:30 UTC

[commons-numbers] branch master updated (595b12a -> e2e46b1)

This is an automated email from the ASF dual-hosted git repository.

erans pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-numbers.git.


    from 595b12a  Use consistent spelling for identifiers (except "camel-case").
     new 450287f  Class is conceptually a singleton.
     new 44ea3e2  NUMBERS-99: Remove requirement that the sign be held in the numerator.
     new 5c6b4b0  Allow "BigFraction" to be used as a field elements.
     new a8aedea  NUMBERS-138: Square matrix for "Field" elements.
     new e2e46b1  POM file standardization.

The 5 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 commons-numbers-field/pom.xml                      |   5 +
 .../{FractionField.java => BigFractionField.java}  |  25 +-
 .../apache/commons/numbers/field/FP64Field.java    |  10 +
 .../commons/numbers/field/FieldSquareMatrix.java   | 289 +++++++++++++++++++++
 .../commons/numbers/field/FractionField.java       |  11 +
 .../numbers/field/FP64FieldSquareMatrixTest.java   | 190 ++++++++++++++
 .../apache/commons/numbers/field/FieldsList.java   |  12 +-
 .../commons/numbers/fraction/BigFraction.java      | 100 +++----
 .../apache/commons/numbers/fraction/Fraction.java  |  82 +++---
 .../commons/numbers/fraction/BigFractionTest.java  |  27 +-
 .../commons/numbers/fraction/CommonTestCases.java  |  21 +-
 .../commons/numbers/fraction/FractionTest.java     |  24 +-
 pom.xml                                            | 212 +++++++--------
 13 files changed, 759 insertions(+), 249 deletions(-)
 copy commons-numbers-field/src/main/java/org/apache/commons/numbers/field/{FractionField.java => BigFractionField.java} (63%)
 create mode 100644 commons-numbers-field/src/main/java/org/apache/commons/numbers/field/FieldSquareMatrix.java
 create mode 100644 commons-numbers-field/src/test/java/org/apache/commons/numbers/field/FP64FieldSquareMatrixTest.java


[commons-numbers] 05/05: POM file standardization.

Posted by er...@apache.org.
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-numbers.git

commit e2e46b1093f25238a0b21dafaf9675ab0e2eb904
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
AuthorDate: Tue Oct 22 15:37:08 2019 +0200

    POM file standardization.
    
    Cf. https://markmail.org/message/obiklp3oeuu7y2fc
---
 pom.xml | 216 ++++++++++++++++++++++++++++++++--------------------------------
 1 file changed, 108 insertions(+), 108 deletions(-)

diff --git a/pom.xml b/pom.xml
index 90d2d3d..4bb5963 100644
--- a/pom.xml
+++ b/pom.xml
@@ -16,113 +16,23 @@
    limitations under the License.
 -->
 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+
   <parent>
     <groupId>org.apache.commons</groupId>
     <artifactId>commons-parent</artifactId>
     <version>47</version>
   </parent>
 
-  <modelVersion>4.0.0</modelVersion>
   <groupId>org.apache.commons</groupId>
   <artifactId>commons-numbers-parent</artifactId>
-  <packaging>pom</packaging>
   <version>1.0-SNAPSHOT</version>
+  <packaging>pom</packaging>
   <name>Apache Commons Numbers</name>
-
-  <inceptionYear>2017</inceptionYear>
   <description>The Apache Commons Numbers project provides number types and utilities.</description>
   <url>http://commons.apache.org/proper/commons-numbers/</url>
 
-  <issueManagement>
-    <system>jira</system>
-    <url>https://issues.apache.org/jira/browse/NUMBERS</url>
-  </issueManagement>
-
-  <scm>
-    <connection>scm:git:http://gitbox.apache.org/repos/asf/commons-numbers.git</connection>
-    <developerConnection>scm:git:https://gitbox.apache.org/repos/asf/commons-numbers.git</developerConnection>
-    <url>https://gitbox.apache.org/repos/asf/commons-numbers.git</url>
-  </scm>
-
-  <distributionManagement>
-    <site>
-      <id>apache.website</id>
-      <name>Apache Commons Site</name>
-      <url>scm:svn:https://svn.apache.org/repos/infra/websites/production/commons/content/proper/commons-numbers/</url>
-    </site>
-  </distributionManagement>
-
-  <developers>
-    <developer>
-      <name>Eric Barnhill</name>
-      <id>ericbarnhill</id>
-      <email>ericbarnhill at apache dot org</email>
-    </developer>
-    <developer>
-      <name>Gilles Sadowski</name>
-      <id>erans</id>
-      <email>erans at apache dot org</email>
-    </developer>
-  </developers>
-
-  <contributors>
-    <contributor>
-      <name>Raymond DeCampo</name>
-      <email>ray at decampo dot org</email>
-    </contributor>
-  </contributors>
-
-  <dependencyManagement>
-    <dependencies>
-      <dependency>
-        <groupId>org.apache.commons</groupId>
-        <artifactId>commons-numbers-core</artifactId>
-        <version>${project.version}</version>
-      </dependency>
-      <dependency>
-        <groupId>org.apache.commons</groupId>
-        <artifactId>commons-numbers-complex</artifactId>
-        <version>${project.version}</version>
-      </dependency>
-      <dependency>
-        <groupId>org.apache.commons</groupId>
-        <artifactId>commons-numbers-fraction</artifactId>
-        <version>${project.version}</version>
-      </dependency>
-      <dependency>
-        <groupId>org.apache.commons</groupId>
-        <artifactId>commons-numbers-core</artifactId>
-        <version>${project.version}</version>
-        <type>test-jar</type>
-        <scope>test</scope>
-      </dependency>
-      <dependency>
-        <groupId>org.apache.commons</groupId>
-        <artifactId>commons-math3</artifactId>
-        <version>${numbers.commons.math3.version}</version>
-      </dependency>
-      <dependency>
-        <groupId>org.junit</groupId>
-        <artifactId>junit-bom</artifactId>
-        <version>${numbers.junit.bom.version}</version>
-        <scope>import</scope>
-        <type>pom</type>
-      </dependency>
-    </dependencies>
-  </dependencyManagement>
-
-  <dependencies>
-    <dependency>
-      <groupId>org.junit.jupiter</groupId>
-      <artifactId>junit-jupiter-engine</artifactId>
-      <scope>test</scope>
-    </dependency>
-    <dependency>
-      <groupId>org.junit.jupiter</groupId>
-      <artifactId>junit-jupiter-params</artifactId>
-      <scope>test</scope>
-    </dependency>
-  </dependencies>
+  <inceptionYear>2017</inceptionYear>
 
   <properties>
     <!-- Do not change: "numbers" is the name of the component even if the
@@ -191,6 +101,91 @@
     <doclint.javadoc.qualifier></doclint.javadoc.qualifier>
   </properties>
 
+  <modules>
+    <module>commons-numbers-core</module>
+    <module>commons-numbers-complex</module>
+    <module>commons-numbers-complex-streams</module>
+    <module>commons-numbers-primes</module>
+    <module>commons-numbers-quaternion</module>
+    <module>commons-numbers-fraction</module>
+    <module>commons-numbers-angle</module>
+    <module>commons-numbers-gamma</module>
+    <module>commons-numbers-combinatorics</module>
+    <module>commons-numbers-arrays</module>
+    <module>commons-numbers-field</module>
+    <module>commons-numbers-rootfinder</module>
+  </modules>
+
+  <scm>
+    <connection>scm:git:http://gitbox.apache.org/repos/asf/commons-numbers.git</connection>
+    <developerConnection>scm:git:https://gitbox.apache.org/repos/asf/commons-numbers.git</developerConnection>
+    <url>https://gitbox.apache.org/repos/asf/commons-numbers.git</url>
+  </scm>
+  <issueManagement>
+    <system>jira</system>
+    <url>https://issues.apache.org/jira/browse/NUMBERS</url>
+  </issueManagement>
+
+  <distributionManagement>
+    <site>
+      <id>apache.website</id>
+      <name>Apache Commons Site</name>
+      <url>scm:svn:https://svn.apache.org/repos/infra/websites/production/commons/content/proper/commons-numbers/</url>
+    </site>
+  </distributionManagement>
+
+  <dependencyManagement>
+    <dependencies>
+      <dependency>
+        <groupId>org.apache.commons</groupId>
+        <artifactId>commons-numbers-core</artifactId>
+        <version>${project.version}</version>
+      </dependency>
+      <dependency>
+        <groupId>org.apache.commons</groupId>
+        <artifactId>commons-numbers-complex</artifactId>
+        <version>${project.version}</version>
+      </dependency>
+      <dependency>
+        <groupId>org.apache.commons</groupId>
+        <artifactId>commons-numbers-fraction</artifactId>
+        <version>${project.version}</version>
+      </dependency>
+      <dependency>
+        <groupId>org.apache.commons</groupId>
+        <artifactId>commons-numbers-core</artifactId>
+        <version>${project.version}</version>
+        <type>test-jar</type>
+        <scope>test</scope>
+      </dependency>
+      <dependency>
+        <groupId>org.apache.commons</groupId>
+        <artifactId>commons-math3</artifactId>
+        <version>${numbers.commons.math3.version}</version>
+      </dependency>
+      <dependency>
+        <groupId>org.junit</groupId>
+        <artifactId>junit-bom</artifactId>
+        <version>${numbers.junit.bom.version}</version>
+        <scope>import</scope>
+        <type>pom</type>
+      </dependency>
+    </dependencies>
+  </dependencyManagement>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.junit.jupiter</groupId>
+      <artifactId>junit-jupiter-engine</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.junit.jupiter</groupId>
+      <artifactId>junit-jupiter-params</artifactId>
+      <scope>test</scope>
+    </dependency>
+  </dependencies>
+
   <build>
     <plugins>
       <plugin>
@@ -617,19 +612,24 @@
     </profile>
   </profiles>
 
-  <modules>
-    <module>commons-numbers-core</module>
-    <module>commons-numbers-complex</module>
-    <module>commons-numbers-complex-streams</module>
-    <module>commons-numbers-primes</module>
-    <module>commons-numbers-quaternion</module>
-    <module>commons-numbers-fraction</module>
-    <module>commons-numbers-angle</module>
-    <module>commons-numbers-gamma</module>
-    <module>commons-numbers-combinatorics</module>
-    <module>commons-numbers-arrays</module>
-    <module>commons-numbers-field</module>
-    <module>commons-numbers-rootfinder</module>
-  </modules>
+  <developers>
+    <developer>
+      <name>Eric Barnhill</name>
+      <id>ericbarnhill</id>
+      <email>ericbarnhill at apache dot org</email>
+    </developer>
+    <developer>
+      <name>Gilles Sadowski</name>
+      <id>erans</id>
+      <email>erans at apache dot org</email>
+    </developer>
+  </developers>
+
+  <contributors>
+    <contributor>
+      <name>Raymond DeCampo</name>
+      <email>ray at decampo dot org</email>
+    </contributor>
+  </contributors>
 
 </project>


[commons-numbers] 02/05: NUMBERS-99: Remove requirement that the sign be held in the numerator.

Posted by er...@apache.org.
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-numbers.git

commit 44ea3e26fa7610f3635aa225d24c4573ad5e54f2
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
AuthorDate: Tue Oct 22 10:59:10 2019 +0200

    NUMBERS-99: Remove requirement that the sign be held in the numerator.
    
    New "signum" method.
    Unit tests updated.
---
 .../commons/numbers/fraction/BigFraction.java      | 86 +++++++++-------------
 .../apache/commons/numbers/fraction/Fraction.java  | 82 +++++++++++----------
 .../commons/numbers/fraction/BigFractionTest.java  | 27 ++-----
 .../commons/numbers/fraction/CommonTestCases.java  | 21 ++++--
 .../commons/numbers/fraction/FractionTest.java     | 24 ++----
 5 files changed, 105 insertions(+), 135 deletions(-)

diff --git a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/BigFraction.java b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/BigFraction.java
index ae1e284..c5c3fc3 100644
--- a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/BigFraction.java
+++ b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/BigFraction.java
@@ -26,7 +26,10 @@ import org.apache.commons.numbers.core.ArithmeticUtils;
  * Representation of a rational number without any overflow. This class is
  * immutable.
  */
-public class BigFraction extends Number implements Comparable<BigFraction>, Serializable {
+public class BigFraction
+    extends Number
+    implements Comparable<BigFraction>,
+               Serializable {
     /** A fraction representing "0". */
     public static final BigFraction ZERO = of(0);
 
@@ -64,28 +67,17 @@ public class BigFraction extends Number implements Comparable<BigFraction>, Seri
         if (den.signum() == 0) {
             throw new FractionException(FractionException.ERROR_ZERO_DENOMINATOR);
         }
-        if (num.signum() == 0) {
-            numerator   = BigInteger.ZERO;
-            denominator = BigInteger.ONE;
-        } else {
-
-            // reduce numerator and denominator by greatest common denominator
-            final BigInteger gcd = num.gcd(den);
-            if (BigInteger.ONE.compareTo(gcd) < 0) {
-                num = num.divide(gcd);
-                den = den.divide(gcd);
-            }
 
-            // move sign to numerator
-            if (den.signum() == -1) {
-                num = num.negate();
-                den = den.negate();
-            }
-
-            // store the values in the final fields
-            numerator   = num;
-            denominator = den;
+        // reduce numerator and denominator by greatest common denominator
+        final BigInteger gcd = num.gcd(den);
+        if (BigInteger.ONE.compareTo(gcd) < 0) {
+            num = num.divide(gcd);
+            den = den.divide(gcd);
         }
+
+        // store the values in the final fields
+        numerator = num;
+        denominator = den;
     }
 
     /**
@@ -387,41 +379,15 @@ public class BigFraction extends Number implements Comparable<BigFraction>, Seri
 
     /**
      * <p>
-     * Creates a <code>BigFraction</code> instance with the 2 parts of a fraction
-     * Y/Z.
-     * </p>
-     *
-     * <p>
-     * Any negative signs are resolved to be on the numerator.
-     * </p>
-     *
-     * @param numerator
-     *            the numerator, for example the three in 'three sevenths'.
-     * @param denominator
-     *            the denominator, for example the seven in 'three sevenths'.
-     * @return a new fraction instance, with the numerator and denominator
-     *         reduced.
-     * @throws ArithmeticException
-     *             if the denominator is <code>zero</code>.
-     */
-    public static BigFraction getReducedFraction(final int numerator,
-                                                 final int denominator) {
-        if (numerator == 0) {
-            return ZERO; // normalize zero.
-        }
-
-        return of(numerator, denominator);
-    }
-
-    /**
-     * <p>
      * Returns the absolute value of this {@link BigFraction}.
      * </p>
      *
      * @return the absolute value as a {@link BigFraction}.
      */
     public BigFraction abs() {
-        return (numerator.signum() == 1) ? this : negate();
+        return signum() >= 0 ?
+            this :
+            negate();
     }
 
     /**
@@ -1090,6 +1056,26 @@ public class BigFraction extends Number implements Comparable<BigFraction>, Seri
     }
 
     /**
+     * Retrieves the sign of this fraction.
+     *
+     * @return -1 if the value is strictly negative, 1 if it is strictly
+     * positive, 0 if it is 0.
+     */
+    public int signum() {
+        final int numS = numerator.signum();
+        final int denS = denominator.signum();
+
+        if ((numS > 0 && denS > 0) ||
+            (numS < 0 && denS < 0)) {
+            return 1;
+        } else if (numS == 0) {
+            return 0;
+        } else {
+            return -1;
+        }
+    }
+
+    /**
      * <p>
      * Return the additive inverse of this fraction, returning the result in
      * reduced form.
diff --git a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java
index 68a2ca5..920dc62 100644
--- a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java
+++ b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java
@@ -163,33 +163,28 @@ public class Fraction
             throw new ArithmeticException("division by zero");
         }
 
-        // If num and den are both 2^-31, or if one is 0 and the other is 2^-31,
-        // the calculation of the gcd below will fail. Ensure that this does not
-        // happen by dividing both by 2 in case both are even.
-        if (((num | den) & 1) == 0) {
-            num >>= 1;
-            den >>= 1;
-        }
-
-        // Reduce numerator and denominator by greatest common divisor.
-        final int d = ArithmeticUtils.gcd(num, den);
-        if (d > 1) {
-            num /= d;
-            den /= d;
-        }
+        if (num == den) {
+            numerator = 1;
+            denominator = 1;
+        } else {
+            // If num and den are both 2^-31, or if one is 0 and the other is 2^-31,
+            // the calculation of the gcd below will fail. Ensure that this does not
+            // happen by dividing both by 2 in case both are even.
+            if (((num | den) & 1) == 0) {
+                num >>= 1;
+                den >>= 1;
+            }
 
-        // Move sign to numerator.
-        if (den < 0) {
-            if (num == Integer.MIN_VALUE ||
-                den == Integer.MIN_VALUE) {
-                throw new FractionException(FractionException.ERROR_NEGATION_OVERFLOW, num, den);
+            // Reduce numerator and denominator by greatest common divisor.
+            final int d = ArithmeticUtils.gcd(num, den);
+            if (d > 1) {
+                num /= d;
+                den /= d;
             }
-            num = -num;
-            den = -den;
-        }
 
-        this.numerator   = num;
-        this.denominator = den;
+            numerator = num;
+            denominator = den;
+        }
     }
 
     /**
@@ -277,13 +272,9 @@ public class Fraction
      * @return the absolute value.
      */
     public Fraction abs() {
-        Fraction ret;
-        if (numerator >= 0) {
-            ret = this;
-        } else {
-            ret = negate();
-        }
-        return ret;
+        return signum() >= 0 ?
+            this :
+            negate();
     }
 
     /**
@@ -346,7 +337,7 @@ public class Fraction
      */
     @Override
     public float floatValue() {
-        return (float)doubleValue();
+        return (float) doubleValue();
     }
 
     /**
@@ -392,18 +383,31 @@ public class Fraction
     }
 
     /**
+     * Retrieves the sign of this fraction.
+     *
+     * @return -1 if the value is strictly negative, 1 if it is strictly
+     * positive, 0 if it is 0.
+     */
+    public int signum() {
+        if ((numerator > 0 && denominator > 0) ||
+            (numerator < 0 && denominator < 0)) {
+            return 1;
+        } else if (numerator == 0) {
+            return 0;
+        } else {
+            return -1;
+        }
+    }
+
+    /**
      * Computes the additive inverse of this fraction.
      *
      * @return the opposite.
      */
     public Fraction negate() {
-        if (numerator == Integer.MIN_VALUE) {
-            throw new FractionException(FractionException.ERROR_NEGATION_OVERFLOW,
-                                        numerator,
-                                        denominator);
-        }
-
-        return new Fraction(-numerator, denominator);
+        return numerator == Integer.MIN_VALUE ?
+            new Fraction(numerator, -denominator) :
+            new Fraction(-numerator, denominator);
     }
 
     /**
diff --git a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/BigFractionTest.java b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/BigFractionTest.java
index c36aa15..162d567 100644
--- a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/BigFractionTest.java
+++ b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/BigFractionTest.java
@@ -488,18 +488,18 @@ public class BigFractionTest {
 
         f1 = BigFraction.of(Integer.MIN_VALUE, Integer.MAX_VALUE);
         f = f1.divide(BigInteger.valueOf(Integer.MIN_VALUE));
-        Assertions.assertEquals(Integer.MAX_VALUE, f.getDenominatorAsInt());
-        Assertions.assertEquals(1, f.getNumeratorAsInt());
+        Assertions.assertEquals(-Integer.MAX_VALUE, f.getDenominatorAsInt());
+        Assertions.assertEquals(-1, f.getNumeratorAsInt());
 
         f1 = BigFraction.of(Integer.MIN_VALUE, Integer.MAX_VALUE);
         f = f1.divide(Integer.MIN_VALUE);
-        Assertions.assertEquals(Integer.MAX_VALUE, f.getDenominatorAsInt());
-        Assertions.assertEquals(1, f.getNumeratorAsInt());
+        Assertions.assertEquals(-Integer.MAX_VALUE, f.getDenominatorAsInt());
+        Assertions.assertEquals(-1, f.getNumeratorAsInt());
 
         f1 = BigFraction.of(Integer.MIN_VALUE, Integer.MAX_VALUE);
         f = f1.divide((long) Integer.MIN_VALUE);
-        Assertions.assertEquals(Integer.MAX_VALUE, f.getDenominatorAsInt());
-        Assertions.assertEquals(1, f.getNumeratorAsInt());
+        Assertions.assertEquals(-Integer.MAX_VALUE, f.getDenominatorAsInt());
+        Assertions.assertEquals(-1, f.getNumeratorAsInt());
 
     }
 
@@ -568,21 +568,6 @@ public class BigFractionTest {
     }
 
     @Test
-    public void testGetReducedFraction() {
-        BigFraction threeFourths = BigFraction.of(3, 4);
-        Assertions.assertEquals(threeFourths, BigFraction.getReducedFraction(6, 8));
-        Assertions.assertEquals(BigFraction.ZERO, BigFraction.getReducedFraction(0, -1));
-        try {
-            BigFraction.getReducedFraction(1, 0);
-            Assertions.fail("expecting ArithmeticException");
-        } catch (ArithmeticException ex) {
-            // expected
-        }
-        Assertions.assertEquals(-1, BigFraction.getReducedFraction(2, Integer.MIN_VALUE).getNumeratorAsInt());
-        Assertions.assertEquals(-1, BigFraction.getReducedFraction(1, -1).getNumeratorAsInt());
-    }
-
-    @Test
     public void testPow() {
         Assertions.assertEquals(BigFraction.of(8192, 1594323), BigFraction.of(2, 3).pow(13));
         Assertions.assertEquals(BigFraction.of(8192, 1594323), BigFraction.of(2, 3).pow(13l));
diff --git a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/CommonTestCases.java b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/CommonTestCases.java
index 805de0c..6753282 100644
--- a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/CommonTestCases.java
+++ b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/CommonTestCases.java
@@ -78,16 +78,16 @@ class CommonTestCases {
 
         testCases.add(new UnaryOperatorTestCase(0, 1, 0, 1));
         testCases.add(new UnaryOperatorTestCase(0, 2, 0, 1));
-        testCases.add(new UnaryOperatorTestCase(0, -1, 0, 1));
+        testCases.add(new UnaryOperatorTestCase(0, -1, 0, -1));
         testCases.add(new UnaryOperatorTestCase(1, 2, 1, 2));
         testCases.add(new UnaryOperatorTestCase(2, 4, 1, 2));
         testCases.add(new UnaryOperatorTestCase(-1, 2, -1, 2));
-        testCases.add(new UnaryOperatorTestCase(1, -2, -1, 2));
+        testCases.add(new UnaryOperatorTestCase(1, -2, 1, -2));
         testCases.add(new UnaryOperatorTestCase(-2, 4, -1, 2));
-        testCases.add(new UnaryOperatorTestCase(2, -4, -1, 2));
+        testCases.add(new UnaryOperatorTestCase(2, -4, 1, -2));
 
-        testCases.add(new UnaryOperatorTestCase(2, Integer.MIN_VALUE, -1, - (Integer.MIN_VALUE / 2)));
-        testCases.add(new UnaryOperatorTestCase(Integer.MIN_VALUE, -2, - (Integer.MIN_VALUE / 2), 1));
+        testCases.add(new UnaryOperatorTestCase(2, Integer.MIN_VALUE, 1, Integer.MIN_VALUE / 2));
+        testCases.add(new UnaryOperatorTestCase(Integer.MIN_VALUE, -2, -Integer.MIN_VALUE / 2, -1));
 
         return testCases;
     }
@@ -158,8 +158,8 @@ class CommonTestCases {
         List<UnaryOperatorTestCase> testCases = new ArrayList<>();
 
         testCases.add(new UnaryOperatorTestCase(10, 21, 10, 21));
-        testCases.add(new UnaryOperatorTestCase(-10, 21, 10, 21));
-        testCases.add(new UnaryOperatorTestCase(10, -21, 10, 21));
+        testCases.add(new UnaryOperatorTestCase(-11, 23, 11, 23));
+        testCases.add(new UnaryOperatorTestCase(13, -24, -13, -24));
 
         return testCases;
     }
@@ -174,7 +174,7 @@ class CommonTestCases {
 
         testCases.add(new UnaryOperatorTestCase(50, 75, 3, 2));
         testCases.add(new UnaryOperatorTestCase(4, 3, 3, 4));
-        testCases.add(new UnaryOperatorTestCase(-15, 47, -47, 15));
+        testCases.add(new UnaryOperatorTestCase(-15, 47, 47, -15));
         testCases.add(new UnaryOperatorTestCase(Integer.MAX_VALUE, 1, 1, Integer.MAX_VALUE));
 
         return testCases;
@@ -191,6 +191,11 @@ class CommonTestCases {
         testCases.add(new UnaryOperatorTestCase(50, 75, -2, 3));
         testCases.add(new UnaryOperatorTestCase(-50, 75, 2, 3));
         testCases.add(new UnaryOperatorTestCase(Integer.MAX_VALUE - 1, Integer.MAX_VALUE, Integer.MIN_VALUE + 2, Integer.MAX_VALUE));
+        testCases.add(new UnaryOperatorTestCase(1, Integer.MIN_VALUE, -1, Integer.MIN_VALUE));
+
+        // XXX Failed by "BigFraction" (whose implementation differs from "Fraction").
+        // testCases.add(new UnaryOperatorTestCase(Integer.MIN_VALUE, Integer.MIN_VALUE, -1, 1));
+        // testCases.add(new UnaryOperatorTestCase(Integer.MIN_VALUE, 1, Integer.MIN_VALUE, -1));
 
         return testCases;
     }
diff --git a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/FractionTest.java b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/FractionTest.java
index 2cf711c..ddf1011 100644
--- a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/FractionTest.java
+++ b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/FractionTest.java
@@ -41,13 +41,10 @@ public class FractionTest {
             );
         }
 
-        // overflow
-        Assertions.assertThrows(ArithmeticException.class,
-                () -> Fraction.of(Integer.MIN_VALUE, -1)
-        );
-        Assertions.assertThrows(ArithmeticException.class,
-                () -> Fraction.of(1, Integer.MIN_VALUE)
-        );
+        // Special cases.
+        assertFraction(Integer.MIN_VALUE, -1, Fraction.of(Integer.MIN_VALUE, -1));
+        assertFraction(1, Integer.MIN_VALUE, Fraction.of(1, Integer.MIN_VALUE));
+        assertFraction(1, 1, Fraction.of(Integer.MIN_VALUE, Integer.MIN_VALUE));
     }
 
     @Test
@@ -229,13 +226,6 @@ public class FractionTest {
             Fraction f = Fraction.of(testCase.operandNumerator, testCase.operandDenominator);
             assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f.negate());
         }
-
-        {
-            final Fraction f = Fraction.of(Integer.MIN_VALUE, 1);
-            Assertions.assertThrows(ArithmeticException.class,
-                    f::negate
-            );
-        }
     }
 
     @Test
@@ -387,7 +377,7 @@ public class FractionTest {
         {
             Fraction b = Fraction.of(3, -7);
             assertFraction(1, 1, b.pow(0));
-            assertFraction(-3, 7, b.pow(1));
+            assertFraction(3, -7, b.pow(1));
             assertFraction(-7, 3, b.pow(-1));
             assertFraction(9, 49, b.pow(2));
             assertFraction(49, 9, b.pow(-2));
@@ -395,7 +385,7 @@ public class FractionTest {
 
         {
             Fraction c = Fraction.of(0, -11);
-            assertFraction(0, 1, c.pow(Integer.MAX_VALUE));
+            assertFraction(0, -1, c.pow(Integer.MAX_VALUE));
         }
     }
 
@@ -486,7 +476,7 @@ public class FractionTest {
         Assertions.assertEquals("3", Fraction.of(6, 2).toString());
         Assertions.assertEquals("2 / 3", Fraction.of(18, 27).toString());
         Assertions.assertEquals("-10 / 11", Fraction.of(-10, 11).toString());
-        Assertions.assertEquals("-10 / 11", Fraction.of(10, -11).toString());
+        Assertions.assertEquals("10 / -11", Fraction.of(10, -11).toString());
     }
 
     @Test


[commons-numbers] 04/05: NUMBERS-138: Square matrix for "Field" elements.

Posted by er...@apache.org.
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-numbers.git

commit a8aedea0758d155cee66edd56920acd121281e44
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
AuthorDate: Tue Oct 22 15:27:21 2019 +0200

    NUMBERS-138: Square matrix for "Field" elements.
---
 commons-numbers-field/pom.xml                      |   5 +
 .../commons/numbers/field/FieldSquareMatrix.java   | 289 +++++++++++++++++++++
 .../numbers/field/FP64FieldSquareMatrixTest.java   | 190 ++++++++++++++
 pom.xml                                            |   6 +
 4 files changed, 490 insertions(+)

diff --git a/commons-numbers-field/pom.xml b/commons-numbers-field/pom.xml
index 32c74fd..bc40526 100644
--- a/commons-numbers-field/pom.xml
+++ b/commons-numbers-field/pom.xml
@@ -58,6 +58,11 @@
       <type>test-jar</type>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.apache.commons</groupId>
+      <artifactId>commons-math3</artifactId>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 
 </project>
diff --git a/commons-numbers-field/src/main/java/org/apache/commons/numbers/field/FieldSquareMatrix.java b/commons-numbers-field/src/main/java/org/apache/commons/numbers/field/FieldSquareMatrix.java
new file mode 100644
index 0000000..f8cc7ba
--- /dev/null
+++ b/commons-numbers-field/src/main/java/org/apache/commons/numbers/field/FieldSquareMatrix.java
@@ -0,0 +1,289 @@
+/*
+ * 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.numbers.field;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.ArrayList;
+
+/**
+ * Square matrix whose elements define a {@link Field}.
+ *
+ * @param <T> Type of the field elements.
+ */
+public class FieldSquareMatrix<T> {
+    /** Field. */
+    private final Field<T> field;
+    /** Dimension. */
+    private final int dim;
+    /** Data storage (in row-major order). */
+    private final T[] data;
+
+    /**
+     * @param f Field.
+     * @param n Dimension of the matrix.
+     * @throws IllegalArgumentException if {@code n <= 0}.
+     */
+    private FieldSquareMatrix(Field<T> f,
+                              int n) {
+        if (n <= 0) {
+            throw new IllegalArgumentException("Negative size");
+        }
+
+        field = f;
+        dim = n;
+        data = (T[]) new Object[n * n];
+    }
+
+    /**
+     * Factory method.
+     *
+     * @param f Field.
+     * @param n Dimension of the matrix.
+     * @return a new instance.
+     * @throws IllegalArgumentException if {@code n <= 0}.
+     */
+    public static <T> FieldSquareMatrix<T> create(Field<T> f,
+                                                  int n) {
+        return new FieldSquareMatrix<>(f, n);
+    }
+
+    /**
+     * Factory method.
+     *
+     * @param f Field.
+     * @param n Dimension of the matrix.
+     * @return a matrix with elements zet to {@link Field#zero() zero}.
+     * @throws IllegalArgumentException if {@code n <= 0}.
+     */
+    public static <T> FieldSquareMatrix<T> zero(Field<T> f,
+                                                int n) {
+        return create(f, n).fill(f.zero());
+    }
+
+    /**
+     * Factory method.
+     *
+     * @param f Field.
+     * @param n Dimension of the matrix.
+     * @return the identity matrix.
+     * @throws IllegalArgumentException if {@code n <= 0}.
+     */
+    public static <T> FieldSquareMatrix<T> identity(Field<T> f,
+                                                    int n) {
+        final FieldSquareMatrix<T> r = zero(f, n);
+
+        for (int i = 0; i < n; i++) {
+            r.set(i, i, f.one());
+        }
+
+        return r;
+    }
+
+    /**
+     * Copies this matrix.
+     *
+     * @return a new instance.
+     */
+    public FieldSquareMatrix<T> copy() {
+        final FieldSquareMatrix<T> r = create(field, dim);
+        System.arraycopy(data, 0, r.data, 0, data.length);
+        return r;
+    }
+
+    /**
+     * @return the dimension of the matrix.
+     */
+    public int getDimension() {
+        return dim;
+    }
+
+    /**
+     * Sets all elements to the given value.
+     *
+     * @param value Value of the elements of the matrix.
+     * @return {@code this}.
+     */
+    public FieldSquareMatrix<T> fill(T value) {
+        Arrays.fill(data, value);
+        return this;
+    }
+
+    /**
+     * Gets an element.
+     *
+     * @param i Row.
+     * @param j Column.
+     * @return the element at (i, j).
+     */
+    public T get(int i,
+                 int j) {
+        return data[i * dim + j];
+    }
+
+    /**
+     * Sets an element.
+     *
+     * @param i Row.
+     * @param j Column.
+     * @param value Value.
+     */
+    public void set(int i,
+                    int j,
+                    T value) {
+        data[i * dim + j] = value;
+    }
+
+    /**
+     * Addition.
+     *
+     * @param other Matrix to add.
+     * @return a new instance with the result of the addition.
+     */
+    public FieldSquareMatrix<T> add(FieldSquareMatrix<T> other) {
+        checkDimension(other);
+        final FieldSquareMatrix<T> r = create(field, dim);
+
+        for (int i = 0; i < data.length; i++) {
+            r.data[i] = field.add(data[i], other.data[i]);
+        }
+
+        return r;
+    }
+
+    /**
+     * Subtraction.
+     *
+     * @param other Matrix to subtract.
+     * @return a new instance with the result of the subtraction.
+     */
+    public FieldSquareMatrix<T> subtract(FieldSquareMatrix<T> other) {
+        checkDimension(other);
+        final FieldSquareMatrix<T> r = create(field, dim);
+
+        for (int i = 0; i < data.length; i++) {
+            r.data[i] = field.subtract(data[i], other.data[i]);
+        }
+
+        return r;
+    }
+
+    /**
+     * Negate.
+     *
+     * @return a new instance with the opposite matrix.
+     */
+    public FieldSquareMatrix<T> negate() {
+        final FieldSquareMatrix<T> r = create(field, dim);
+
+        for (int i = 0; i < data.length; i++) {
+            r.data[i] = field.negate(data[i]);
+        }
+
+        return r;
+    }
+
+    /**
+     * Multiplication.
+     *
+     * @param factor Matrix to multiply with.
+     * @return a new instance with the result of the multiplication.
+     */
+    public FieldSquareMatrix<T> multiply(FieldSquareMatrix<T> other) {
+        checkDimension(other);
+        final FieldSquareMatrix<T> r = zero(field, dim);
+
+        for (int i = 0; i < dim; i++) {
+            final int o1 = i * dim;
+            for (int j = 0; j < dim; j++) {
+                final int o2 = o1 + j;
+                for (int k = 0; k < dim; k++) {
+                    r.data[o2] = field.add(r.data[o2],
+                                           field.multiply(data[o1 + k],
+                                                          other.data[k * dim + j]));
+                }
+            }
+        }
+
+        return r;
+    }
+
+    /**
+     * Multiplies the matrix with itself {@code p} times.
+     *
+     * @param p Exponent.
+     * @return a new instance.
+     * @throws IllegalArgumentException if {@code p < 0}.
+     */
+    public FieldSquareMatrix<T> pow(int p) {
+        if (p < 0) {
+            throw new IllegalArgumentException("Negative exponent: " + p);
+        }
+
+        if (p == 0) {
+            return identity(field, dim);
+        }
+
+        if (p == 1) {
+            return copy();
+        }
+
+        final int power = p - 1;
+
+        // Only log_2(p) operations are necessary by doing as follows:
+        //    5^214 = 5^128 * 5^64 * 5^16 * 5^4 * 5^2
+        // The same approach is used for A^p.
+
+        final char[] binary = Integer.toBinaryString(power).toCharArray();
+        final ArrayList<Integer> nonZeroPositions = new ArrayList<>();
+
+        for (int i = 0; i < binary.length; i++) {
+            if (binary[i] == '1') {
+                final int pos = binary.length - i - 1;
+                nonZeroPositions.add(pos);
+            }
+        }
+
+        final List<FieldSquareMatrix<T>> results = new ArrayList<>(binary.length);
+        results.add(this);
+        for (int i = 1; i < binary.length; i++) {
+            final FieldSquareMatrix<T> s = results.get(i - 1);
+            final FieldSquareMatrix<T> r = s.multiply(s);
+            results.add(r);
+        }
+
+        FieldSquareMatrix<T> r = this;
+        for (Integer i : nonZeroPositions) {
+            r = r.multiply(results.get(i));
+        }
+
+        return r;
+    }
+
+    /**
+     * Check that the given matrix has the same dimensions.
+     *
+     * @param factor Matrix to check.
+     * @throws IllegalArgumentException if the dimensions do not match.
+     */
+    private void checkDimension(FieldSquareMatrix<T> other) {
+        if (dim != other.dim) {
+            throw new IllegalArgumentException("Dimension mismatch: " +
+                                               dim + " != " + other.dim);
+        }
+    }
+}
diff --git a/commons-numbers-field/src/test/java/org/apache/commons/numbers/field/FP64FieldSquareMatrixTest.java b/commons-numbers-field/src/test/java/org/apache/commons/numbers/field/FP64FieldSquareMatrixTest.java
new file mode 100644
index 0000000..d512e6f
--- /dev/null
+++ b/commons-numbers-field/src/test/java/org/apache/commons/numbers/field/FP64FieldSquareMatrixTest.java
@@ -0,0 +1,190 @@
+/*
+ * 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.numbers.field;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.apache.commons.math3.linear.RealMatrix;
+import org.apache.commons.math3.linear.Array2DRowRealMatrix;
+import org.apache.commons.math3.util.Pair;
+
+/**
+ * Tests for {@link FieldSquareMatrix} (using {@link FP64} as field elements).
+ */
+public class FP64FieldSquareMatrixTest {
+    @Test
+    public void testGetDimension() {
+        final int dim = 6;
+        final FieldSquareMatrix<FP64> a = FieldSquareMatrix.create(FP64Field.get(), dim);
+        Assertions.assertEquals(dim, a.getDimension());
+    }
+
+    @Test
+    public void testSetGet() {
+        final int dim = 20;
+        final FieldSquareMatrix<FP64> a = FieldSquareMatrix.create(FP64Field.get(), dim);
+
+        int count = 0;
+        for (int i = 0; i < dim; i++) {
+            for (int j = 0; j < dim; j++) {
+                a.set(i, j, FP64.of(count++));
+            }
+        }
+        Assertions.assertEquals(dim * dim, count);
+
+        count = 0;
+        for (int i = 0; i < dim; i++) {
+            for (int j = 0; j < dim; j++) {
+                Assertions.assertEquals((double) count++,
+                                        a.get(i, j).doubleValue(),
+                                        0d);
+            }
+        }
+    }
+
+    @Test
+    public void testAdd() {
+        final int dim = 6;
+        final double scale = 1e3;
+        final Pair<FieldSquareMatrix<FP64>, RealMatrix> p1 = createRandom(dim, scale);
+        final Pair<FieldSquareMatrix<FP64>, RealMatrix> p2 = createRandom(dim, scale);
+
+        assertEquals(p1.getFirst().add(p2.getFirst()),
+                     p1.getSecond().add(p2.getSecond()),
+                     0d);
+    }
+
+    @Test
+    public void testSubtract() {
+        final int dim = 6;
+        final double scale = 1e3;
+        final Pair<FieldSquareMatrix<FP64>, RealMatrix> p1 = createRandom(dim, scale);
+        final Pair<FieldSquareMatrix<FP64>, RealMatrix> p2 = createRandom(dim, scale);
+
+        assertEquals(p1.getFirst().subtract(p2.getFirst()),
+                     p1.getSecond().subtract(p2.getSecond()),
+                     0d);
+    }
+
+    @Test
+    public void testMultiply() {
+        final int dim = 7;
+        final double scale = 1e2;
+        final Pair<FieldSquareMatrix<FP64>, RealMatrix> p1 = createRandom(dim, scale);
+        final Pair<FieldSquareMatrix<FP64>, RealMatrix> p2 = createRandom(dim, scale);
+
+        assertEquals(p1.getFirst().multiply(p2.getFirst()),
+                     p1.getSecond().multiply(p2.getSecond()),
+                     0d);
+    }
+
+    @Test
+    public void testNegate() {
+        final int dim = 13;
+        final double scale = 1;
+        final Pair<FieldSquareMatrix<FP64>, RealMatrix> p = createRandom(dim, scale);
+
+        assertEquals(p.getFirst().negate(),
+                     p.getSecond().scalarMultiply(-1),
+                     0d);
+    }
+
+    @Test
+    public void testPowZero() {
+        final int dim = 5;
+        final double scale = 1e100;
+        final Pair<FieldSquareMatrix<FP64>, RealMatrix> p = createRandom(dim, scale);
+
+        final int exp = 0;
+        assertEquals(p.getFirst().pow(exp),
+                     p.getSecond().power(exp),
+                     0d);
+    }
+
+    @Test
+    public void testPowOne() {
+        final int dim = 5;
+        final double scale = 1e100;
+        final Pair<FieldSquareMatrix<FP64>, RealMatrix> p = createRandom(dim, scale);
+
+        final int exp = 1;
+        assertEquals(p.getFirst().pow(exp),
+                     p.getSecond().power(exp),
+                     0d);
+    }
+
+    @Test
+    public void testPow() {
+        final int dim = 5;
+        final double scale = 1e2;
+        final Pair<FieldSquareMatrix<FP64>, RealMatrix> p = createRandom(dim, scale);
+
+        final int exp = 4;
+        assertEquals(p.getFirst().pow(exp),
+                     p.getSecond().power(exp),
+                     0d);
+    }
+
+    /**
+     * Compares with result obtained from "Commons Math".
+     *
+     * @param a "Commons Numbers" result.
+     * @param b "Commons Math" result.
+     * @param tol Tolerance.
+     */
+    private void assertEquals(FieldSquareMatrix<FP64> a,
+                              RealMatrix b,
+                              double tol) {
+        final int dim = a.getDimension();
+        if (dim != b.getRowDimension() ||
+            dim != b.getColumnDimension()) {
+            Assertions.fail("Dimension mismatch"); 
+        }
+
+        for (int i = 0; i < dim; i++) {
+            for (int j = 0; j < dim; j++) {
+                Assertions.assertEquals(a.get(i, j).doubleValue(),
+                                        b.getEntry(i, j),
+                                        tol,
+                                        "(" + i + ", " + j + ")");
+            }
+        }
+    }
+
+    /**
+     * Creates test matrices with random entries.
+     *
+     * @param dim Dimension.
+     * @param scale Range of the entries.
+     * @return a pair of matrices whose entries are in the interval
+     * {@code [-scale, scale]}.
+     */
+    private Pair<FieldSquareMatrix<FP64>, RealMatrix> createRandom(int dim,
+                                                                   double scale) {
+        final FieldSquareMatrix<FP64> a = FieldSquareMatrix.create(FP64Field.get(), dim);
+        final RealMatrix b = new Array2DRowRealMatrix(dim, dim);
+        for (int i = 0; i < dim; i++) {
+            for (int j = 0; j < dim; j++) {
+                final double v = scale * (2 * Math.random() - 1);
+                a.set(i, j, FP64.of(v));
+                b.setEntry(i, j, v);
+            }
+        }
+
+        return new Pair<>(a, b);
+    }
+}
diff --git a/pom.xml b/pom.xml
index 1d3bfbc..90d2d3d 100644
--- a/pom.xml
+++ b/pom.xml
@@ -97,6 +97,11 @@
         <scope>test</scope>
       </dependency>
       <dependency>
+        <groupId>org.apache.commons</groupId>
+        <artifactId>commons-math3</artifactId>
+        <version>${numbers.commons.math3.version}</version>
+      </dependency>
+      <dependency>
         <groupId>org.junit</groupId>
         <artifactId>junit-bom</artifactId>
         <version>${numbers.junit.bom.version}</version>
@@ -145,6 +150,7 @@
     <numbers.checkstyle.version>3.0.0</numbers.checkstyle.version>
     <numbers.mathjax.version>2.7.2</numbers.mathjax.version>
     <numbers.junit.bom.version>5.4.2</numbers.junit.bom.version>
+    <numbers.commons.math3.version>3.6.1</numbers.commons.math3.version>
     <!-- Workaround to avoid duplicating config files. -->
     <numbers.parent.dir>${basedir}</numbers.parent.dir>
 


[commons-numbers] 01/05: Class is conceptually a singleton.

Posted by er...@apache.org.
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-numbers.git

commit 450287f3d6967c0148a9b616a5f5667795a3bace
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
AuthorDate: Tue Oct 22 10:46:47 2019 +0200

    Class is conceptually a singleton.
---
 .../main/java/org/apache/commons/numbers/field/FP64Field.java | 10 ++++++++++
 .../java/org/apache/commons/numbers/field/FractionField.java  | 11 +++++++++++
 .../java/org/apache/commons/numbers/field/FieldsList.java     |  4 ++--
 3 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/commons-numbers-field/src/main/java/org/apache/commons/numbers/field/FP64Field.java b/commons-numbers-field/src/main/java/org/apache/commons/numbers/field/FP64Field.java
index 491c8d4..8d929af 100644
--- a/commons-numbers-field/src/main/java/org/apache/commons/numbers/field/FP64Field.java
+++ b/commons-numbers-field/src/main/java/org/apache/commons/numbers/field/FP64Field.java
@@ -24,6 +24,16 @@ public class FP64Field extends AbstractField<FP64> {
     private static final FP64 ZERO = FP64.of(0d);
     /** 1d */
     private static final FP64 ONE = FP64.of(1d);
+    /** Singleton. */
+    private static final FP64Field INSTANCE = new FP64Field();
+
+    /** Singleton. */
+    private FP64Field() {}
+
+    /** @return the field instance. */
+    public static FP64Field get() {
+        return INSTANCE;
+    }
 
     /** {@inheritDoc} */
     @Override
diff --git a/commons-numbers-field/src/main/java/org/apache/commons/numbers/field/FractionField.java b/commons-numbers-field/src/main/java/org/apache/commons/numbers/field/FractionField.java
index 941225d..0cb9dc8 100644
--- a/commons-numbers-field/src/main/java/org/apache/commons/numbers/field/FractionField.java
+++ b/commons-numbers-field/src/main/java/org/apache/commons/numbers/field/FractionField.java
@@ -22,6 +22,17 @@ import org.apache.commons.numbers.fraction.Fraction;
  * {@link Fraction} field.
  */
 public class FractionField extends AbstractField<Fraction> {
+    /** Singleton. */
+    private static final FractionField INSTANCE = new FractionField();
+
+    /** Singleton. */
+    private FractionField() {}
+
+    /** @return the field instance. */
+    public static FractionField get() {
+        return INSTANCE;
+    }
+
     /** {@inheritDoc} */
     @Override
     public Fraction one() {
diff --git a/commons-numbers-field/src/test/java/org/apache/commons/numbers/field/FieldsList.java b/commons-numbers-field/src/test/java/org/apache/commons/numbers/field/FieldsList.java
index 76a15cd..a3c9143 100644
--- a/commons-numbers-field/src/test/java/org/apache/commons/numbers/field/FieldsList.java
+++ b/commons-numbers-field/src/test/java/org/apache/commons/numbers/field/FieldsList.java
@@ -34,11 +34,11 @@ final class FieldsList {
     static {
         try {
             // List of fields to test.
-            add(new FractionField(),
+            add(FractionField.get(),
                 Fraction.of(13, 4),
                 Fraction.of(5, 29),
                 Fraction.of(-279, 11));
-            add(new FP64Field(),
+            add(FP64Field.get(),
                 FP64.of(23.45678901),
                 FP64.of(-543.2109876),
                 FP64.of(-234.5678901));


[commons-numbers] 03/05: Allow "BigFraction" to be used as a field elements.

Posted by er...@apache.org.
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-numbers.git

commit 5c6b4b02f210724008aa2390315142f9eaa2cdd3
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
AuthorDate: Tue Oct 22 11:11:42 2019 +0200

    Allow "BigFraction" to be used as a field elements.
---
 .../commons/numbers/field/BigFractionField.java    | 47 ++++++++++++++++++++++
 .../apache/commons/numbers/field/FieldsList.java   |  8 +++-
 .../commons/numbers/fraction/BigFraction.java      | 14 +++++++
 3 files changed, 67 insertions(+), 2 deletions(-)

diff --git a/commons-numbers-field/src/main/java/org/apache/commons/numbers/field/BigFractionField.java b/commons-numbers-field/src/main/java/org/apache/commons/numbers/field/BigFractionField.java
new file mode 100644
index 0000000..79e563f
--- /dev/null
+++ b/commons-numbers-field/src/main/java/org/apache/commons/numbers/field/BigFractionField.java
@@ -0,0 +1,47 @@
+/*
+ * 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.numbers.field;
+
+import org.apache.commons.numbers.fraction.BigFraction;
+
+/**
+ * {@link BigFraction} field.
+ */
+public class BigFractionField extends AbstractField<BigFraction> {
+    /** Singleton. */
+    private static final BigFractionField INSTANCE = new BigFractionField();
+
+    /** Singleton. */
+    private BigFractionField() {}
+
+    /** @return the field instance. */
+    public static BigFractionField get() {
+        return INSTANCE;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public BigFraction one() {
+        return BigFraction.ONE;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public BigFraction zero() {
+        return BigFraction.ZERO;
+    }
+}
diff --git a/commons-numbers-field/src/test/java/org/apache/commons/numbers/field/FieldsList.java b/commons-numbers-field/src/test/java/org/apache/commons/numbers/field/FieldsList.java
index a3c9143..a6bc264 100644
--- a/commons-numbers-field/src/test/java/org/apache/commons/numbers/field/FieldsList.java
+++ b/commons-numbers-field/src/test/java/org/apache/commons/numbers/field/FieldsList.java
@@ -22,6 +22,7 @@ import java.util.ArrayList;
 import java.util.Collections;
 
 import org.apache.commons.numbers.fraction.Fraction;
+import org.apache.commons.numbers.fraction.BigFraction;
 
 /**
  * List of fields.
@@ -38,6 +39,10 @@ final class FieldsList {
                 Fraction.of(13, 4),
                 Fraction.of(5, 29),
                 Fraction.of(-279, 11));
+            add(BigFractionField.get(),
+                BigFraction.of(13256093L, 43951044L),
+                BigFraction.of(543016315L, 29L),
+                BigFraction.of(-27930919051L, 11L));
             add(FP64Field.get(),
                 FP64.of(23.45678901),
                 FP64.of(-543.2109876),
@@ -50,8 +55,7 @@ final class FieldsList {
         }
     }
 
-    private FieldsList() {
-    }
+    private FieldsList() {}
 
     /**
      * @param field Field.
diff --git a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/BigFraction.java b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/BigFraction.java
index c5c3fc3..4405d49 100644
--- a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/BigFraction.java
+++ b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/BigFraction.java
@@ -21,6 +21,7 @@ import java.math.BigDecimal;
 import java.math.BigInteger;
 import java.math.RoundingMode;
 import org.apache.commons.numbers.core.ArithmeticUtils;
+import org.apache.commons.numbers.core.NativeOperators;
 
 /**
  * Representation of a rational number without any overflow. This class is
@@ -29,6 +30,7 @@ import org.apache.commons.numbers.core.ArithmeticUtils;
 public class BigFraction
     extends Number
     implements Comparable<BigFraction>,
+               NativeOperators<BigFraction>,
                Serializable {
     /** A fraction representing "0". */
     public static final BigFraction ZERO = of(0);
@@ -1290,6 +1292,18 @@ public class BigFraction
         return str;
     }
 
+    /** {@inheritDoc} */
+    @Override
+    public BigFraction zero() {
+        return ZERO;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public BigFraction one() {
+        return ONE;
+    }
+
     /**
      * Parses a string that would be produced by {@link #toString()}
      * and instantiates the corresponding object.