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 2018/12/14 15:31:36 UTC

[commons-numbers] branch master updated (c0bf9f7 -> a9cd5d9)

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 c0bf9f7  NUMBERS-76: Refactoring so class can be final (ValJO criterion).
     new bf1d61e  CheckStyle.
     new 59af118  Unit tests.
     new 9076074  NUMBERS-84: Adding NaN and infinite checks to Quaternion normalize() and inverse()
     new 3eedf4c  Ordering of the preconditions.
     new d7fe20e  Merge branch 'fix_NUMBERS-84__matt'
     new a9cd5d9  Nit (less verbose unit tests).

The 6 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/quaternion/Quaternion.java     |  40 ++++----
 .../commons/numbers/quaternion/QuaternionTest.java | 104 ++++++++++++++++++---
 2 files changed, 113 insertions(+), 31 deletions(-)


[commons-numbers] 06/06: Nit (less verbose unit tests).

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 a9cd5d9c510a4323bb05f822be440526e3b5d068
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
AuthorDate: Fri Dec 14 16:30:02 2018 +0100

    Nit (less verbose unit tests).
---
 .../commons/numbers/quaternion/QuaternionTest.java | 30 +++++++++-------------
 1 file changed, 12 insertions(+), 18 deletions(-)

diff --git a/commons-numbers-quaternion/src/test/java/org/apache/commons/numbers/quaternion/QuaternionTest.java b/commons-numbers-quaternion/src/test/java/org/apache/commons/numbers/quaternion/QuaternionTest.java
index bce52da..5d5f99b 100644
--- a/commons-numbers-quaternion/src/test/java/org/apache/commons/numbers/quaternion/QuaternionTest.java
+++ b/commons-numbers-quaternion/src/test/java/org/apache/commons/numbers/quaternion/QuaternionTest.java
@@ -470,42 +470,36 @@ public class QuaternionTest {
 
     @Test
     public final void testPositivePolarFormWhenScalarPositive() {
-        Quaternion q = Quaternion.of(3, -3, -3, 3);
-        Quaternion actual = q.positivePolarForm();
+        Quaternion q = Quaternion.of(3, -3, -3, 3).positivePolarForm();
         Quaternion expected = Quaternion.of(0.5, -0.5, -0.5, 0.5);
-        assertEquals(actual, expected, EPS);
+        assertEquals(q, expected, EPS);
 
-        Assert.assertTrue(actual.positivePolarForm() == actual);
+        Assert.assertTrue(q.positivePolarForm() == q);
     }
 
     @Test
     public final void testPositivePolarFormWhenScalarNegative() {
-        Quaternion q = Quaternion.of(-3, 3, -3, 3);
-        Quaternion actual = q.positivePolarForm();
+        Quaternion q = Quaternion.of(-3, 3, -3, 3).positivePolarForm();
         Quaternion expected = Quaternion.of(0.5, -0.5, 0.5, -0.5);
-        assertEquals(actual, expected, EPS);
+        assertEquals(q, expected, EPS);
 
-        Assert.assertTrue(actual.positivePolarForm() == actual);
+        Assert.assertTrue(q.positivePolarForm() == q);
     }
 
     @Test
     public final void testPositivePolarFormWhenScalarPositiveAndNormalized() {
-        Quaternion q = Quaternion.of(3, -3, -3, 3).normalize();
-        Quaternion actual = q.positivePolarForm();
-        Quaternion expected = Quaternion.of(0.5, -0.5, -0.5, 0.5);
-        assertEquals(actual, expected, EPS);
+        Quaternion q = Quaternion.of(123, 45, 67, 89).normalize().positivePolarForm();
 
-        Assert.assertTrue(actual.positivePolarForm() == actual);
+        Assert.assertTrue(q.getW() >= 0);
+        Assert.assertTrue(q.positivePolarForm() == q);
     }
 
     @Test
     public final void testPositivePolarFormWhenScalarNegativeAndNormalized() {
-        Quaternion q = Quaternion.of(-3, 3, -3, 3).normalize();
-        Quaternion actual = q.positivePolarForm();
-        Quaternion expected = Quaternion.of(0.5, -0.5, 0.5, -0.5);
-        assertEquals(actual, expected, EPS);
+        Quaternion q = Quaternion.of(123, 45, 67, 89).normalize().negate().positivePolarForm();
 
-        Assert.assertTrue(actual.positivePolarForm() == actual);
+        Assert.assertTrue(q.getW() >= 0);
+        Assert.assertTrue(q.positivePolarForm() == q);
     }
 
     @Test


[commons-numbers] 04/06: Ordering of the preconditions.

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 3eedf4c2b0654f55d8cf02ca0a16124db4e9aef3
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
AuthorDate: Fri Dec 14 14:41:25 2018 +0100

    Ordering of the preconditions.
---
 .../main/java/org/apache/commons/numbers/quaternion/Quaternion.java | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/commons-numbers-quaternion/src/main/java/org/apache/commons/numbers/quaternion/Quaternion.java b/commons-numbers-quaternion/src/main/java/org/apache/commons/numbers/quaternion/Quaternion.java
index fc954d6..867deaf 100644
--- a/commons-numbers-quaternion/src/main/java/org/apache/commons/numbers/quaternion/Quaternion.java
+++ b/commons-numbers-quaternion/src/main/java/org/apache/commons/numbers/quaternion/Quaternion.java
@@ -390,7 +390,8 @@ public class Quaternion implements Serializable {
         case DEFAULT:
             final double norm = norm();
 
-            if (!Double.isFinite(norm) || norm < Precision.SAFE_MIN) {
+            if (norm < Precision.SAFE_MIN ||
+                !Double.isFinite(norm)) {
                 throw new IllegalStateException(ILLEGAL_NORM_MSG + norm);
             }
 
@@ -528,7 +529,8 @@ public class Quaternion implements Serializable {
             return new Quaternion(type, w, -x, -y, -z);
         case DEFAULT:
             final double squareNorm = normSq();
-            if (!Double.isFinite(squareNorm) || squareNorm < Precision.SAFE_MIN) {
+            if (squareNorm < Precision.SAFE_MIN ||
+                !Double.isFinite(squareNorm)) {
                 throw new IllegalStateException(ILLEGAL_NORM_MSG + Math.sqrt(squareNorm));
             }
 


[commons-numbers] 02/06: Unit tests.

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 59af118b192a2d97939baacb56236684f5a0b7c7
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
AuthorDate: Fri Dec 14 14:29:22 2018 +0100

    Unit tests.
---
 .../commons/numbers/quaternion/QuaternionTest.java | 46 +++++++++++++++++++++-
 1 file changed, 44 insertions(+), 2 deletions(-)

diff --git a/commons-numbers-quaternion/src/test/java/org/apache/commons/numbers/quaternion/QuaternionTest.java b/commons-numbers-quaternion/src/test/java/org/apache/commons/numbers/quaternion/QuaternionTest.java
index aa89b1d..2b77a4f 100644
--- a/commons-numbers-quaternion/src/test/java/org/apache/commons/numbers/quaternion/QuaternionTest.java
+++ b/commons-numbers-quaternion/src/test/java/org/apache/commons/numbers/quaternion/QuaternionTest.java
@@ -451,7 +451,7 @@ public class QuaternionTest {
     }
 
     @Test
-    public final void testPositivePolarFormWhenScalarPositive(){
+    public final void testPositivePolarFormWhenScalarPositive() {
         Quaternion q = Quaternion.of(3, -3, -3, 3);
         Quaternion actual = q.positivePolarForm();
         Quaternion expected = Quaternion.of(0.5, -0.5, -0.5, 0.5);
@@ -461,7 +461,7 @@ public class QuaternionTest {
     }
 
     @Test
-    public final void testPositivePolarFormWhenScalarNegative(){
+    public final void testPositivePolarFormWhenScalarNegative() {
         Quaternion q = Quaternion.of(-3, 3, -3, 3);
         Quaternion actual = q.positivePolarForm();
         Quaternion expected = Quaternion.of(0.5, -0.5, 0.5, -0.5);
@@ -471,6 +471,26 @@ public class QuaternionTest {
     }
 
     @Test
+    public final void testPositivePolarFormWhenScalarPositiveAndNormalized() {
+        Quaternion q = Quaternion.of(3, -3, -3, 3).normalize();
+        Quaternion actual = q.positivePolarForm();
+        Quaternion expected = Quaternion.of(0.5, -0.5, -0.5, 0.5);
+        assertEquals(actual, expected, EPS);
+
+        Assert.assertTrue(actual.positivePolarForm() == actual);
+    }
+
+    @Test
+    public final void testPositivePolarFormWhenScalarNegativeAndNormalized() {
+        Quaternion q = Quaternion.of(-3, 3, -3, 3).normalize();
+        Quaternion actual = q.positivePolarForm();
+        Quaternion expected = Quaternion.of(0.5, -0.5, 0.5, -0.5);
+        assertEquals(actual, expected, EPS);
+
+        Assert.assertTrue(actual.positivePolarForm() == actual);
+    }
+
+    @Test
     public void testNegate() {
         final double a = -1;
         final double b = 2;
@@ -486,6 +506,28 @@ public class QuaternionTest {
         Assert.assertTrue(q.equals(qNeg.negate(), 0d));
     }
 
+    @Test
+    public void testNegateNormalized() {
+        final double a = -1;
+        final double b = 2;
+        final double c = -3;
+        final double d = 4;
+        final Quaternion q = Quaternion.of(a, b, c, d).normalize();
+        final Quaternion qNeg = q.negate();
+        Assert.assertTrue(q.equals(qNeg.negate(), 0d));
+    }
+
+    @Test
+    public void testNegatePositivePolarForm() {
+        final double a = -1;
+        final double b = 2;
+        final double c = -3;
+        final double d = 4;
+        final Quaternion q = Quaternion.of(a, b, c, d).positivePolarForm();
+        final Quaternion qNeg = q.negate();
+        Assert.assertTrue(q.equals(qNeg.negate(), 0d));
+    }
+
     /* TODO remove dependency on Rotation
     @Test
     public final void testPolarForm() {


[commons-numbers] 05/06: Merge branch 'fix_NUMBERS-84__matt'

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 d7fe20e5d71fe6b473d9491f4a09beafe52633f0
Merge: 59af118 3eedf4c
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
AuthorDate: Fri Dec 14 14:44:42 2018 +0100

    Merge branch 'fix_NUMBERS-84__matt'
    
    Closes #21.

 .../commons/numbers/quaternion/Quaternion.java     | 18 ++++----
 .../commons/numbers/quaternion/QuaternionTest.java | 48 ++++++++++++++++++++--
 2 files changed, 56 insertions(+), 10 deletions(-)


[commons-numbers] 01/06: CheckStyle.

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 bf1d61e51a1dc77cdf3b3e5ec855f70055a66ea6
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
AuthorDate: Fri Dec 14 14:23:26 2018 +0100

    CheckStyle.
---
 .../commons/numbers/quaternion/Quaternion.java     | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)

diff --git a/commons-numbers-quaternion/src/main/java/org/apache/commons/numbers/quaternion/Quaternion.java b/commons-numbers-quaternion/src/main/java/org/apache/commons/numbers/quaternion/Quaternion.java
index 7d555fb..b09d7dc 100644
--- a/commons-numbers-quaternion/src/main/java/org/apache/commons/numbers/quaternion/Quaternion.java
+++ b/commons-numbers-quaternion/src/main/java/org/apache/commons/numbers/quaternion/Quaternion.java
@@ -395,9 +395,9 @@ public class Quaternion implements Serializable {
 
             final Quaternion unit = divide(norm);
 
-            return (w >= 0 ?
-                    new Quaternion(Type.POSITIVE_POLAR_FORM, unit) :
-                    new Quaternion(Type.NORMALIZED, unit));
+            return w >= 0 ?
+                new Quaternion(Type.POSITIVE_POLAR_FORM, unit) :
+                new Quaternion(Type.NORMALIZED, unit);
         default:
             throw new IllegalStateException(); // Should never happen.
         }
@@ -480,15 +480,15 @@ public class Quaternion implements Serializable {
         case POSITIVE_POLAR_FORM:
             return this;
         case NORMALIZED:
-            return (w >= 0 ?
-                    new Quaternion(Type.POSITIVE_POLAR_FORM, this) :
-                    new Quaternion(Type.POSITIVE_POLAR_FORM, negate()));
+            return w >= 0 ?
+                new Quaternion(Type.POSITIVE_POLAR_FORM, this) :
+                new Quaternion(Type.POSITIVE_POLAR_FORM, negate());
         case DEFAULT:
-            return (w >= 0 ?
-                    normalize() :
-                    // The quaternion of rotation (normalized quaternion) q and -q
-                    // are equivalent (i.e. represent the same rotation).
-                    negate().normalize());
+            return w >= 0 ?
+                normalize() :
+                // The quaternion of rotation (normalized quaternion) q and -q
+                // are equivalent (i.e. represent the same rotation).
+                negate().normalize();
         default:
             throw new IllegalStateException(); // Should never happen.
         }


[commons-numbers] 03/06: NUMBERS-84: Adding NaN and infinite checks to Quaternion normalize() and inverse()

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 9076074737bfc5bcbc64d675a084bea981261715
Author: Matt Juntunen <ma...@hotmail.com>
AuthorDate: Fri Dec 14 07:48:31 2018 -0500

    NUMBERS-84: Adding NaN and infinite checks to Quaternion normalize() and inverse()
---
 .../commons/numbers/quaternion/Quaternion.java     | 16 ++++----
 .../commons/numbers/quaternion/QuaternionTest.java | 48 ++++++++++++++++++++--
 2 files changed, 54 insertions(+), 10 deletions(-)

diff --git a/commons-numbers-quaternion/src/main/java/org/apache/commons/numbers/quaternion/Quaternion.java b/commons-numbers-quaternion/src/main/java/org/apache/commons/numbers/quaternion/Quaternion.java
index b09d7dc..fc954d6 100644
--- a/commons-numbers-quaternion/src/main/java/org/apache/commons/numbers/quaternion/Quaternion.java
+++ b/commons-numbers-quaternion/src/main/java/org/apache/commons/numbers/quaternion/Quaternion.java
@@ -44,7 +44,7 @@ public class Quaternion implements Serializable {
     /** Serializable version identifier. */
     private static final long serialVersionUID = 20170118L;
     /** Error message. */
-    private static final String ZERO_NORM_MSG = "Norm is zero";
+    private static final String ILLEGAL_NORM_MSG = "Illegal norm: ";
 
     /** {@link #toString() String representation}. */
     private static final String FORMAT_START = "[";
@@ -379,7 +379,8 @@ public class Quaternion implements Serializable {
      * The norm of the quaternion must not be near zero.
      *
      * @return a normalized quaternion.
-     * @throws IllegalStateException if the norm of the quaternion is near zero.
+     * @throws IllegalStateException if the norm of the quaternion is NaN, infinite,
+     *      or near zero.
      */
     public Quaternion normalize() {
         switch (type) {
@@ -389,8 +390,8 @@ public class Quaternion implements Serializable {
         case DEFAULT:
             final double norm = norm();
 
-            if (norm < Precision.SAFE_MIN) {
-                throw new IllegalStateException(ZERO_NORM_MSG);
+            if (!Double.isFinite(norm) || norm < Precision.SAFE_MIN) {
+                throw new IllegalStateException(ILLEGAL_NORM_MSG + norm);
             }
 
             final Quaternion unit = divide(norm);
@@ -517,7 +518,8 @@ public class Quaternion implements Serializable {
      * The norm of the quaternion must not be zero.
      *
      * @return the inverse.
-     * @throws IllegalArgumentException if the norm (squared) of the quaternion is zero.
+     * @throws IllegalStateException if the norm (squared) of the quaternion is NaN,
+     *      infinite, or near zero.
      */
     public Quaternion inverse() {
         switch (type) {
@@ -526,8 +528,8 @@ public class Quaternion implements Serializable {
             return new Quaternion(type, w, -x, -y, -z);
         case DEFAULT:
             final double squareNorm = normSq();
-            if (squareNorm < Precision.SAFE_MIN) {
-                throw new IllegalStateException(ZERO_NORM_MSG);
+            if (!Double.isFinite(squareNorm) || squareNorm < Precision.SAFE_MIN) {
+                throw new IllegalStateException(ILLEGAL_NORM_MSG + Math.sqrt(squareNorm));
             }
 
             return of(w / squareNorm,
diff --git a/commons-numbers-quaternion/src/test/java/org/apache/commons/numbers/quaternion/QuaternionTest.java b/commons-numbers-quaternion/src/test/java/org/apache/commons/numbers/quaternion/QuaternionTest.java
index 2b77a4f..bce52da 100644
--- a/commons-numbers-quaternion/src/test/java/org/apache/commons/numbers/quaternion/QuaternionTest.java
+++ b/commons-numbers-quaternion/src/test/java/org/apache/commons/numbers/quaternion/QuaternionTest.java
@@ -332,9 +332,27 @@ public class QuaternionTest {
     }
 
     @Test(expected=IllegalStateException.class)
-    public final void testNormalizeFail() {
-        final Quaternion zeroQ = Quaternion.of(0, 0, 0, 0);
-        zeroQ.normalize();
+    public final void testNormalizeFail_zero() {
+        final Quaternion q = Quaternion.of(0, 0, 0, 0);
+        q.normalize();
+    }
+
+    @Test(expected=IllegalStateException.class)
+    public final void testNormalizeFail_nan() {
+        final Quaternion q = Quaternion.of(0, 0, 0, Double.NaN);
+        q.normalize();
+    }
+
+    @Test(expected=IllegalStateException.class)
+    public final void testNormalizeFail_positiveInfinity() {
+        final Quaternion q = Quaternion.of(0, 0, Double.POSITIVE_INFINITY, 0);
+        q.normalize();
+    }
+
+    @Test(expected=IllegalStateException.class)
+    public final void testNormalizeFail_negativeInfinity() {
+        final Quaternion q = Quaternion.of(0, Double.NEGATIVE_INFINITY, 0, 0);
+        q.normalize();
     }
 
     @Test
@@ -582,6 +600,30 @@ public class QuaternionTest {
         }
     }
 
+    @Test(expected=IllegalStateException.class)
+    public void testInverse_zeroNorm() {
+        Quaternion q = Quaternion.of(0, 0, 0, 0);
+        q.inverse();
+    }
+
+    @Test(expected=IllegalStateException.class)
+    public void testInverse_nanNorm() {
+        Quaternion q = Quaternion.of(Double.NaN, 0, 0, 0);
+        q.inverse();
+    }
+
+    @Test(expected=IllegalStateException.class)
+    public void testInverse_positiveInfinityNorm() {
+        Quaternion q = Quaternion.of(0, Double.POSITIVE_INFINITY, 0, 0);
+        q.inverse();
+    }
+
+    @Test(expected=IllegalStateException.class)
+    public void testInverse_negativeInfinityNorm() {
+        Quaternion q = Quaternion.of(0, 0, Double.NEGATIVE_INFINITY, 0);
+        q.inverse();
+    }
+
     @Test
     public void testInverseNormalized() {
         final Quaternion invQ = Quaternion.of(-1.2, 3.4, -5.6, -7.8).normalize().inverse();