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/11/30 14:00:21 UTC

[1/8] commons-numbers git commit: NUMBERS-76 Make "Quaternion" a VALJO

Repository: commons-numbers
Updated Branches:
  refs/heads/master 447ced4cc -> 851f85cc9


NUMBERS-76 Make "Quaternion" a VALJO


Project: http://git-wip-us.apache.org/repos/asf/commons-numbers/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-numbers/commit/9c6f7780
Tree: http://git-wip-us.apache.org/repos/asf/commons-numbers/tree/9c6f7780
Diff: http://git-wip-us.apache.org/repos/asf/commons-numbers/diff/9c6f7780

Branch: refs/heads/master
Commit: 9c6f77801265abe079b4054c3c90d55e956ee7cd
Parents: 447ced4
Author: Steve Bosman <St...@gmail.com>
Authored: Wed Nov 28 23:15:01 2018 +0000
Committer: Steve Bosman <St...@gmail.com>
Committed: Wed Nov 28 23:15:01 2018 +0000

----------------------------------------------------------------------
 .../commons/numbers/quaternion/Quaternion.java  | 183 ++++++++++---
 .../numbers/quaternion/QuaternionTest.java      | 270 +++++++++++++++----
 2 files changed, 363 insertions(+), 90 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/9c6f7780/commons-numbers-quaternion/src/main/java/org/apache/commons/numbers/quaternion/Quaternion.java
----------------------------------------------------------------------
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 86d7fdf..0582625 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
@@ -61,7 +61,45 @@ public final class Quaternion implements Serializable {
      * @param c Second vector component.
      * @param d Third vector component.
      */
-    public Quaternion(final double a,
+    public static Quaternion of(final double a,
+                                final double b,
+                                final double c,
+                                final double d) {
+        return new Quaternion(a, b, c, d);
+    }
+
+    /**
+     * Builds a quaternion from scalar and vector parts.
+     *
+     * @param scalar Scalar part of the quaternion.
+     * @param v Components of the vector part of the quaternion.
+     *
+     * @throws IllegalArgumentException if the array length is not 3.
+     */
+    public static Quaternion of(final double scalar,
+                                final double[] v) {
+        return new Quaternion(scalar, v);
+    }
+
+    /**
+     * Builds a pure quaternion from a vector (assuming that the scalar
+     * part is zero).
+     *
+     * @param v Components of the vector part of the pure quaternion.
+     */
+    public static Quaternion of(final double[] v) {
+        return new Quaternion(0, v);
+    }
+
+    /**
+     * Builds a quaternion from its components.
+     *
+     * @param a Scalar component.
+     * @param b First vector component.
+     * @param c Second vector component.
+     * @param d Third vector component.
+     */
+    private Quaternion(final double a,
                       final double b,
                       final double c,
                       final double d) {
@@ -79,7 +117,7 @@ public final class Quaternion implements Serializable {
      *
      * @throws IllegalArgumentException if the array length is not 3.
      */
-    public Quaternion(final double scalar,
+    private Quaternion(final double scalar,
                       final double[] v) {
         if (v.length != 3) {
             throw new IllegalArgumentException("Size of array must be 3");
@@ -92,21 +130,12 @@ public final class Quaternion implements Serializable {
     }
 
     /**
-     * Builds a pure quaternion from a vector (assuming that the scalar
-     * part is zero).
+     * Returns the conjugate of this quaternion number.
+     * The conjugate of {@code a + bi + cj + dk} is {@code a - bi -cj -dk}.
      *
-     * @param v Components of the vector part of the pure quaternion.
+     * @return the conjugate of this quaternion object.
      */
-    public Quaternion(final double[] v) {
-        this(0, v);
-    }
-
-    /**
-     * Returns the conjugate quaternion of the instance.
-     *
-     * @return the conjugate quaternion
-     */
-    public Quaternion getConjugate() {
+    public Quaternion conjugate() {
         return new Quaternion(q0, -q1, -q2, -q3);
     }
 
@@ -230,31 +259,37 @@ public final class Quaternion implements Serializable {
      *
      * @return the norm.
      */
-    public double getNorm() {
-        return Math.sqrt(q0 * q0 +
-                         q1 * q1 +
-                         q2 * q2 +
-                         q3 * q3);
+    public double norm() {
+        return Math.sqrt(norm2());
+    }
+
+    /**
+     * Computes the square of the norm of the quaternion.
+     *
+     * @return the square of the norm.
+     */
+    public double norm2() {
+        return q0 * q0 +
+                q1 * q1 +
+                q2 * q2 +
+                q3 * q3;
     }
 
     /**
      * Computes the normalized quaternion (the versor of the instance).
-     * The norm of the quaternion must not be zero.
+     * The norm of the quaternion must not be near zero.
      *
      * @return a normalized quaternion.
-     * @throws IllegalStateException if the norm of the quaternion is zero.
+     * @throws IllegalStateException if the norm of the quaternion is near zero.
      */
     public Quaternion normalize() {
-        final double norm = getNorm();
+        final double norm = norm();
 
         if (norm < Precision.SAFE_MIN) {
             throw new IllegalStateException(ZERO_NORM_MSG);
         }
 
-        return new Quaternion(q0 / norm,
-                              q1 / norm,
-                              q2 / norm,
-                              q3 / norm);
+        return this.divide(norm);
     }
 
     /**
@@ -267,10 +302,10 @@ public final class Quaternion implements Serializable {
         }
         if (other instanceof Quaternion) {
             final Quaternion q = (Quaternion) other;
-            return q0 == q.q0 &&
-                q1 == q.q1 &&
-                q2 == q.q2 &&
-                q3 == q.q3;
+            return ((Double)q0).equals(q.q0) &&
+                    ((Double)q1).equals(q.q1) &&
+                    ((Double)q2).equals(q.q2) &&
+                    ((Double)q3).equals(q.q3) ;
         }
 
         return false;
@@ -310,7 +345,7 @@ public final class Quaternion implements Serializable {
      * {@code false} otherwise
      */
     public boolean isUnitQuaternion(double eps) {
-        return Precision.equals(getNorm(), 1d, eps);
+        return Precision.equals(norm(), 1d, eps);
     }
 
     /**
@@ -329,7 +364,7 @@ public final class Quaternion implements Serializable {
      *
      * @return the unit quaternion with positive scalar part.
      */
-    public Quaternion getPositivePolarForm() {
+    public Quaternion positivePolarForm() {
         if (q0 < 0) {
             final Quaternion unitQ = normalize();
             // The quaternion of rotation (normalized quaternion) q and -q
@@ -350,8 +385,8 @@ public final class Quaternion implements Serializable {
      * @return the inverse.
      * @throws IllegalArgumentException if the norm (squared) of the quaternion is zero.
      */
-    public Quaternion getInverse() {
-        final double squareNorm = q0 * q0 + q1 * q1 + q2 * q2 + q3 * q3;
+    public Quaternion inverse() {
+        final double squareNorm = norm2();
         if (squareNorm < Precision.SAFE_MIN) {
             throw new IllegalStateException(ZERO_NORM_MSG);
         }
@@ -437,6 +472,71 @@ public final class Quaternion implements Serializable {
     }
 
     /**
+     * Divides the instance by a scalar.
+     *
+     * @param alpha Scalar factor.
+     * @return a scaled quaternion.
+     */
+    public Quaternion divide(final double alpha) {
+        return new Quaternion(q0 / alpha,
+                              q1 / alpha,
+                              q2 / alpha,
+                              q3 / alpha);
+    }
+
+    /**
+     * Parses a string that would be produced by {@link #toString()}
+     * and instantiates the corresponding object.
+     *
+     * @param s String representation.
+     * @return an instance.
+     * @throws IllegalArgumentException if the string does not
+     * conform to the specification.
+     */
+    public static Quaternion parse(String s) {
+        final int len = s.length();
+        final int startBracket = s.indexOf("[");
+        if (startBracket != 0) {
+            throw new QuaternionParsingException("Missing opening square bracket");
+        }
+        final int endBracket = s.indexOf("]");
+        if (endBracket != len - 1) {
+            throw new QuaternionParsingException("Missing closing square bracket");
+        }
+        String[] elements = s.substring(1, s.length()-1).split(" ");
+        if (elements.length != 4) {
+            throw new QuaternionParsingException("Incorrect number of parts");
+        }
+
+        final double q1;
+        try {
+            q1 = Double.parseDouble(elements[0]);
+        } catch (Exception ex) {
+            throw new QuaternionParsingException("Could not parse scalar part" + elements[0]);
+        }
+        final double q2;
+        try {
+            q2 = Double.parseDouble(elements[1]);
+        } catch (Exception ex) {
+            throw new QuaternionParsingException("Could not parse i part" + elements[1]);
+        }
+        final double q3;
+        try {
+            q3 = Double.parseDouble(elements[2]);
+        } catch (Exception ex) {
+            throw new QuaternionParsingException("Could not parse j part" + elements[2]);
+        }
+        final double q4;
+        try {
+            q4 = Double.parseDouble(elements[3]);
+        } catch (Exception ex) {
+            throw new QuaternionParsingException("Could not parse k part" + elements[3]);
+        }
+
+        return of(q1, q2, q3, q4);
+    }
+
+    /**
      * {@inheritDoc}
      */
     @Override
@@ -452,4 +552,17 @@ public final class Quaternion implements Serializable {
 
         return s.toString();
     }
+
+    /** See {@link #parse(String)}. */
+    static class QuaternionParsingException extends IllegalArgumentException {
+        /** Serializable version identifier. */
+        private static final long serialVersionUID = 20181128L;
+
+        /**
+         * @param msg Error message.
+         */
+        QuaternionParsingException(String msg) {
+            super(msg);
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/9c6f7780/commons-numbers-quaternion/src/test/java/org/apache/commons/numbers/quaternion/QuaternionTest.java
----------------------------------------------------------------------
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 b96a0b4..fa2f342 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
@@ -18,7 +18,6 @@ package org.apache.commons.numbers.quaternion;
 
 import java.util.Random;
 
-import org.apache.commons.numbers.quaternion.Quaternion;
 import org.junit.Test;
 import org.junit.Assert;
 
@@ -34,7 +33,7 @@ public class QuaternionTest {
         final double q1 = 5.4;
         final double q2 = 17;
         final double q3 = 0.0005;
-        final Quaternion q = new Quaternion(q0, q1, q2, q3);
+        final Quaternion q = Quaternion.of(q0, q1, q2, q3);
 
         Assert.assertEquals(q0, q.getQ0(), 0);
         Assert.assertEquals(q1, q.getQ1(), 0);
@@ -48,7 +47,7 @@ public class QuaternionTest {
         final double q1 = 5.4;
         final double q2 = 17;
         final double q3 = 0.0005;
-        final Quaternion q = new Quaternion(q0, q1, q2, q3);
+        final Quaternion q = Quaternion.of(q0, q1, q2, q3);
 
         final double sP = q.getScalarPart();
         final double[] vP = q.getVectorPart();
@@ -65,7 +64,7 @@ public class QuaternionTest {
         final double q1 = 5.4;
         final double q2 = 17;
         final double q3 = 0.0005;
-        final Quaternion q = new Quaternion(q0, new double[] { q1, q2, q3 });
+        final Quaternion q = Quaternion.of(q0, new double[] { q1, q2, q3 });
 
         final double sP = q.getScalarPart();
         final double[] vP = q.getVectorPart();
@@ -78,7 +77,7 @@ public class QuaternionTest {
 
     @Test(expected=IllegalArgumentException.class)
     public void testWrongDimension() {
-        new Quaternion(new double[] { 1, 2 });
+        Quaternion.of(new double[] { 1, 2 });
     }
 
     @Test
@@ -87,9 +86,9 @@ public class QuaternionTest {
         final double q1 = 5.4;
         final double q2 = 17;
         final double q3 = 0.0005;
-        final Quaternion q = new Quaternion(q0, q1, q2, q3);
+        final Quaternion q = Quaternion.of(q0, q1, q2, q3);
 
-        final Quaternion qConjugate = q.getConjugate();
+        final Quaternion qConjugate = q.conjugate();
 
         Assert.assertEquals(q0, qConjugate.getQ0(), 0);
         Assert.assertEquals(-q1, qConjugate.getQ1(), 0);
@@ -103,8 +102,8 @@ public class QuaternionTest {
 
         // Case : analytic test case
 
-        final Quaternion qA = new Quaternion(1, 0.5, -3, 4);
-        final Quaternion qB = new Quaternion(6, 2, 1, -9);
+        final Quaternion qA = Quaternion.of(1, 0.5, -3, 4);
+        final Quaternion qB = Quaternion.of(6, 2, 1, -9);
         final Quaternion qResult = Quaternion.multiply(qA, qB);
 
         Assert.assertEquals(44, qResult.getQ0(), EPS);
@@ -126,7 +125,7 @@ public class QuaternionTest {
 
         final Vector3D vectorPartRef = ((vectorA.scalarMultiply(qB.getScalarPart())).add(vectorB.scalarMultiply(qA
                 .getScalarPart()))).add(Vector3D.crossProduct(vectorA, vectorB));
-        final double norm = (vectorResult.subtract(vectorPartRef)).getNorm();
+        final double norm = (vectorResult.subtract(vectorPartRef)).norm();
 
         Assert.assertEquals(0, norm, EPS);
 
@@ -148,9 +147,9 @@ public class QuaternionTest {
 
         // Case : Product between a vector and a quaternion : QxV
 
-        final Quaternion quaternion = new Quaternion(4, 7, -1, 2);
+        final Quaternion quaternion = Quaternion.of(4, 7, -1, 2);
         final double[] vector = {2.0, 1.0, 3.0};
-        final Quaternion qResultQxV = Quaternion.multiply(quaternion, new Quaternion(vector));
+        final Quaternion qResultQxV = Quaternion.multiply(quaternion, Quaternion.of(vector));
 
         Assert.assertEquals(-19, qResultQxV.getQ0(), EPS);
         Assert.assertEquals(3, qResultQxV.getQ1(), EPS);
@@ -168,12 +167,12 @@ public class QuaternionTest {
 
         final Vector3D vectorPartRefQxV = (new Vector3D(vector).scalarMultiply(quaternion.getScalarPart())).add(Vector3D
                 .crossProduct(new Vector3D(vectorQ), new Vector3D(vector)));
-        final double normQxV = (new Vector3D(vectorResultQxV).subtract(vectorPartRefQxV)).getNorm();
+        final double normQxV = (new Vector3D(vectorResultQxV).subtract(vectorPartRefQxV)).norm();
         Assert.assertEquals(0, normQxV, EPS);
 
         // Case : Product between a vector and a quaternion : VxQ
 
-        final Quaternion qResultVxQ = Quaternion.multiply(new Quaternion(vector), quaternion);
+        final Quaternion qResultVxQ = Quaternion.multiply(Quaternion.of(vector), quaternion);
 
         Assert.assertEquals(-19, qResultVxQ.getQ0(), EPS);
         Assert.assertEquals(13, qResultVxQ.getQ1(), EPS);
@@ -190,7 +189,7 @@ public class QuaternionTest {
 
         final Vector3D vectorPartRefVxQ = (new Vector3D(vector).scalarMultiply(quaternion.getScalarPart())).add(Vector3D
                 .crossProduct(new Vector3D(vector), new Vector3D(vectorQ)));
-        final double normVxQ = (new Vector3D(vectorResultVxQ).subtract(vectorPartRefVxQ)).getNorm();
+        final double normVxQ = (new Vector3D(vectorResultVxQ).subtract(vectorPartRefVxQ)).norm();
         Assert.assertEquals(0, normVxQ, EPS);
     }
     */
@@ -199,8 +198,8 @@ public class QuaternionTest {
         // expected output
         final double expected = -6.;
         // inputs
-        final Quaternion q1 = new Quaternion(1, 2, 2, 1);
-        final Quaternion q2 = new Quaternion(3, -2, -1, -3);
+        final Quaternion q1 = Quaternion.of(1, 2, 2, 1);
+        final Quaternion q2 = Quaternion.of(3, -2, -1, -3);
 
         final double actual1 = Quaternion.dotProduct(q1, q2);
         final double actual2 = q1.dotProduct(q2);
@@ -217,7 +216,7 @@ public class QuaternionTest {
         final double y = 11.20;
         final double z = 2.56;
         // inputs
-        final Quaternion q1 = new Quaternion(0.5, -1.5, 3.5, 0.8);
+        final Quaternion q1 = Quaternion.of(0.5, -1.5, 3.5, 0.8);
         final double a = 3.2;
 
         final Quaternion q = q1.multiply(a);
@@ -236,8 +235,8 @@ public class QuaternionTest {
         final double y = 2;
         final double z = -4;
         // inputs
-        final Quaternion q1 = new Quaternion(1., 2., -2., -1.);
-        final Quaternion q2 = new Quaternion(3., -3., 4., -3.);
+        final Quaternion q1 = Quaternion.of(1., 2., -2., -1.);
+        final Quaternion q2 = Quaternion.of(3., -3., 4., -3.);
 
         final Quaternion qa = Quaternion.add(q1, q2);
         final Quaternion qb = q1.add(q2);
@@ -261,8 +260,8 @@ public class QuaternionTest {
         final double y = -6.;
         final double z = 2.;
         // inputs
-        final Quaternion q1 = new Quaternion(1., 2., -2., -1.);
-        final Quaternion q2 = new Quaternion(3., -3., 4., -3.);
+        final Quaternion q1 = Quaternion.of(1., 2., -2., -1.);
+        final Quaternion q2 = Quaternion.of(3., -3., 4., -3.);
 
         final Quaternion qa = Quaternion.subtract(q1, q2);
         final Quaternion qb = q1.subtract(q2);
@@ -285,20 +284,20 @@ public class QuaternionTest {
         final double q1 = 1;
         final double q2 = -4;
         final double q3 = 3;
-        final Quaternion q = new Quaternion(q0, q1, q2, q3);
+        final Quaternion q = Quaternion.of(q0, q1, q2, q3);
 
-        final double norm = q.getNorm();
+        final double norm = q.norm();
 
         Assert.assertEquals(Math.sqrt(30), norm, 0);
 
-        final double normSquareRef = Quaternion.multiply(q, q.getConjugate()).getScalarPart();
+        final double normSquareRef = Quaternion.multiply(q, q.conjugate()).getScalarPart();
         Assert.assertEquals(Math.sqrt(normSquareRef), norm, 0);
     }
 
     @Test
     public final void testNormalize() {
 
-        final Quaternion q = new Quaternion(2, 1, -4, -2);
+        final Quaternion q = Quaternion.of(2, 1, -4, -2);
 
         final Quaternion versor = q.normalize();
 
@@ -307,36 +306,71 @@ public class QuaternionTest {
         Assert.assertEquals(-4.0 / 5.0, versor.getQ2(), 0);
         Assert.assertEquals(-2.0 / 5.0, versor.getQ3(), 0);
 
-        Assert.assertEquals(1, versor.getNorm(), 0);
+        Assert.assertEquals(1, versor.norm(), 0);
     }
 
     @Test(expected=IllegalStateException.class)
     public final void testNormalizeFail() {
-        final Quaternion zeroQ = new Quaternion(0, 0, 0, 0);
+        final Quaternion zeroQ = Quaternion.of(0, 0, 0, 0);
         zeroQ.normalize();
     }
 
     @Test
     public final void testObjectEquals() {
         final double one = 1;
-        final Quaternion q1 = new Quaternion(one, one, one, one);
+        final Quaternion q1 = Quaternion.of(one, one, one, one);
         Assert.assertTrue(q1.equals(q1));
 
-        final Quaternion q2 = new Quaternion(one, one, one, one);
+        final Quaternion q2 = Quaternion.of(one, one, one, one);
         Assert.assertTrue(q2.equals(q1));
 
-        final Quaternion q3 = new Quaternion(one, Math.nextUp(one), one, one);
+        final Quaternion q3 = Quaternion.of(one, Math.nextUp(one), one, one);
         Assert.assertFalse(q3.equals(q1));
+
+        Assert.assertFalse(q3.equals("bar"));
+    }
+
+    @Test
+    public void testHashCode() {
+        Quaternion x = Quaternion.of(0.0, 0.0, 0.0, 0.0);
+        Quaternion y = Quaternion.of(0.0, 0.0 + Double.MIN_VALUE, 0.0, 0.0);
+        Assert.assertFalse(x.hashCode()==y.hashCode());
+        y = Quaternion.of(0.0 + Double.MIN_VALUE, 0.0, 0.0, 0.0);
+        Assert.assertFalse(x.hashCode()==y.hashCode());
+
+        // "equals" and "hashCode" must be compatible: if two objects have
+        // different hash codes, "equals" must return false.
+        final String msg = "'equals' not compatible with 'hashCode'";
+
+        x = Quaternion.of(0.0, 0.0, 0.0, 0.0);
+        y = Quaternion.of(-0.0, 0.0, 0.0, 0.0);
+        Assert.assertTrue(x.hashCode() != y.hashCode());
+        Assert.assertFalse(msg, x.equals(y));
+
+        x = Quaternion.of(0.0, 0.0, 0.0, 0.0);
+        y = Quaternion.of(0.0, -0.0, 0.0, 0.0);
+        Assert.assertTrue(x.hashCode() != y.hashCode());
+        Assert.assertFalse(msg, x.equals(y));
+
+        x = Quaternion.of(0.0, 0.0, 0.0, 0.0);
+        y = Quaternion.of(0.0, 0.0, -0.0, 0.0);
+        Assert.assertTrue(x.hashCode() != y.hashCode());
+        Assert.assertFalse(msg, x.equals(y));
+
+        x = Quaternion.of(0.0, 0.0, 0.0, 0.0);
+        y = Quaternion.of(0.0, 0.0, 0.0, -0.0);
+        Assert.assertTrue(x.hashCode() != y.hashCode());
+        Assert.assertFalse(msg, x.equals(y));
     }
 
     @Test
     public final void testQuaternionEquals() {
         final double inc = 1e-5;
-        final Quaternion q1 = new Quaternion(2, 1, -4, -2);
-        final Quaternion q2 = new Quaternion(q1.getQ0() + inc, q1.getQ1(), q1.getQ2(), q1.getQ3());
-        final Quaternion q3 = new Quaternion(q1.getQ0(), q1.getQ1() + inc, q1.getQ2(), q1.getQ3());
-        final Quaternion q4 = new Quaternion(q1.getQ0(), q1.getQ1(), q1.getQ2() + inc, q1.getQ3());
-        final Quaternion q5 = new Quaternion(q1.getQ0(), q1.getQ1(), q1.getQ2(), q1.getQ3() + inc);
+        final Quaternion q1 = Quaternion.of(2, 1, -4, -2);
+        final Quaternion q2 = Quaternion.of(q1.getQ0() + inc, q1.getQ1(), q1.getQ2(), q1.getQ3());
+        final Quaternion q3 = Quaternion.of(q1.getQ0(), q1.getQ1() + inc, q1.getQ2(), q1.getQ3());
+        final Quaternion q4 = Quaternion.of(q1.getQ0(), q1.getQ1(), q1.getQ2() + inc, q1.getQ3());
+        final Quaternion q5 = Quaternion.of(q1.getQ0(), q1.getQ1(), q1.getQ2(), q1.getQ3() + inc);
 
         Assert.assertFalse(q1.equals(q2, 0.9 * inc));
         Assert.assertFalse(q1.equals(q3, 0.9 * inc));
@@ -351,9 +385,9 @@ public class QuaternionTest {
 
     @Test
     public final void testQuaternionEquals2() {
-        final Quaternion q1 = new Quaternion(1, 4, 2, 3);
+        final Quaternion q1 = Quaternion.of(1, 4, 2, 3);
         final double gap = 1e-5;
-        final Quaternion q2 = new Quaternion(1 + gap, 4 + gap, 2 + gap, 3 + gap);
+        final Quaternion q2 = Quaternion.of(1 + gap, 4 + gap, 2 + gap, 3 + gap);
 
         Assert.assertTrue(q1.equals(q2, 10 * gap));
         Assert.assertFalse(q1.equals(q2, gap));
@@ -365,44 +399,60 @@ public class QuaternionTest {
         final Random r = new Random(48);
         final int numberOfTrials = 1000;
         for (int i = 0; i < numberOfTrials; i++) {
-            final Quaternion q1 = new Quaternion(r.nextDouble(), r.nextDouble(), r.nextDouble(), r.nextDouble());
+            final Quaternion q1 = Quaternion.of(r.nextDouble(), r.nextDouble(), r.nextDouble(), r.nextDouble());
             final Quaternion q2 = q1.normalize();
             Assert.assertTrue(q2.isUnitQuaternion(COMPARISON_EPS));
         }
 
-        final Quaternion q = new Quaternion(1, 1, 1, 1);
+        final Quaternion q = Quaternion.of(1, 1, 1, 1);
         Assert.assertFalse(q.isUnitQuaternion(COMPARISON_EPS));
     }
 
     @Test
     public final void testIsPureQuaternion() {
-        final Quaternion q1 = new Quaternion(0, 5, 4, 8);
+        final Quaternion q1 = Quaternion.of(0, 5, 4, 8);
         Assert.assertTrue(q1.isPureQuaternion(EPS));
 
-        final Quaternion q2 = new Quaternion(0 - EPS, 5, 4, 8);
+        final Quaternion q2 = Quaternion.of(0 - EPS, 5, 4, 8);
         Assert.assertTrue(q2.isPureQuaternion(EPS));
 
-        final Quaternion q3 = new Quaternion(0 - 1.1 * EPS, 5, 4, 8);
+        final Quaternion q3 = Quaternion.of(0 - 1.1 * EPS, 5, 4, 8);
         Assert.assertFalse(q3.isPureQuaternion(EPS));
 
         final Random r = new Random(48);
         final double[] v = {r.nextDouble(), r.nextDouble(), r.nextDouble()};
-        final Quaternion q4 = new Quaternion(v);
+        final Quaternion q4 = Quaternion.of(v);
         Assert.assertTrue(q4.isPureQuaternion(0));
 
-        final Quaternion q5 = new Quaternion(0, v);
+        final Quaternion q5 = Quaternion.of(0, v);
         Assert.assertTrue(q5.isPureQuaternion(0));
     }
 
-    /*
+    @Test
+    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);
+        assertEquals(actual, expected, EPS);
+    }
+
+    @Test
+    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);
+        assertEquals(actual, expected, EPS);
+    }
+
+    /* TODO remove dependency on Rotation
     @Test
     public final void testPolarForm() {
         final Random r = new Random(48);
         final int numberOfTrials = 1000;
         for (int i = 0; i < numberOfTrials; i++) {
-            final Quaternion q = new Quaternion(2 * (r.nextDouble() - 0.5), 2 * (r.nextDouble() - 0.5),
+            final Quaternion q = Quaternion.of(2 * (r.nextDouble() - 0.5), 2 * (r.nextDouble() - 0.5),
                                                 2 * (r.nextDouble() - 0.5), 2 * (r.nextDouble() - 0.5));
-            final Quaternion qP = q.getPositivePolarForm();
+            final Quaternion qP = q.positivePolarForm();
 
             Assert.assertTrue(qP.isUnitQuaternion(COMPARISON_EPS));
             Assert.assertTrue(qP.getQ0() >= 0);
@@ -424,10 +474,10 @@ public class QuaternionTest {
     }
 */
     @Test
-    public final void testGetInverse() {
-        final Quaternion q = new Quaternion(1.5, 4, 2, -2.5);
+    public final void testInverse() {
+        final Quaternion q = Quaternion.of(1.5, 4, 2, -2.5);
 
-        final Quaternion inverseQ = q.getInverse();
+        final Quaternion inverseQ = q.inverse();
         Assert.assertEquals(1.5 / 28.5, inverseQ.getQ0(), 0);
         Assert.assertEquals(-4.0 / 28.5, inverseQ.getQ1(), 0);
         Assert.assertEquals(-2.0 / 28.5, inverseQ.getQ2(), 0);
@@ -439,9 +489,9 @@ public class QuaternionTest {
         Assert.assertEquals(0, product.getQ2(), EPS);
         Assert.assertEquals(0, product.getQ3(), EPS);
 
-        final Quaternion qNul = new Quaternion(0, 0, 0, 0);
+        final Quaternion qNul = Quaternion.of(0, 0, 0, 0);
         try {
-            final Quaternion inverseQNul = qNul.getInverse();
+            final Quaternion inverseQNul = qNul.inverse();
             Assert.fail("expecting ZeroException but got : " + inverseQNul);
         } catch (IllegalStateException ex) {
             // expected
@@ -449,8 +499,118 @@ public class QuaternionTest {
     }
 
     @Test
+    public final void testMultiply() {
+        final Quaternion q1 = Quaternion.of(1, 2, 3, 4);
+        final Quaternion q2 = Quaternion.of(4, 3, 2, 1);
+        final Quaternion actual = q1.multiply(q2);
+        final double w = 1 * 4 - 2 * 3 - 3 * 2 - 4 * 1;
+        final double x = 1 * 3 + 2 * 4 + 3 * 1 - 4 * 2;
+        final double y = 1 * 2 - 2 * 1 + 3 * 4 + 4 * 3;
+        final double z = 1 * 1 + 2 * 2 - 3 * 3 + 4 * 4;
+        final Quaternion expected = Quaternion.of(w, x, y, z);
+        assertEquals(actual, expected, EPS);
+    }
+
+    @Test
+    public final void testParseFromToString() {
+        final Quaternion q = Quaternion.of(1.1, 2.2, 3.3, 4.4);
+        Quaternion parsed = Quaternion.parse(q.toString());
+        assertEquals(parsed, q, EPS);
+    }
+
+    @Test
+    public final void testParseSpecials() {
+        Quaternion parsed = Quaternion.parse("[1e-5 Infinity NaN -0xa.cp0]");
+        Assert.assertEquals(1e-5, parsed.getQ0(), EPS);
+        Assert.assertTrue(Double.isInfinite(parsed.getQ1()));
+        Assert.assertTrue(Double.isNaN(parsed.getQ2()));
+        Assert.assertEquals(-0xa.cp0, parsed.getQ3(), EPS);
+    }
+
+    @Test
+    public final void testParseMissingStart() {
+        try {
+            final Quaternion parsed = Quaternion.parse("1.0 2.0 3.0 4.0]");
+            Assert.fail("expecting QuaternionParsingException but got : " + parsed);
+        } catch (Quaternion.QuaternionParsingException ex) {
+            // expected
+        }
+    }
+
+    @Test
+    public final void testParseMissingEnd() {
+        try {
+            final Quaternion parsed = Quaternion.parse("[1.0 2.0 3.0 4.0");
+            Assert.fail("expecting QuaternionParsingException but got : " + parsed);
+        } catch (Quaternion.QuaternionParsingException ex) {
+            // expected
+        }
+    }
+
+    @Test
+    public final void testParseMissingPart() {
+        try {
+            final Quaternion parsed = Quaternion.parse("[1.0 2.0 3.0 ]");
+            Assert.fail("expecting QuaternionParsingException but got : " + parsed);
+        } catch (Quaternion.QuaternionParsingException ex) {
+            // expected
+        }
+    }
+
+    @Test
+    public final void testParseInvalidScalar() {
+        try {
+            final Quaternion parsed = Quaternion.parse("[1.x 2.0 3.0 4.0]");
+            Assert.fail("expecting QuaternionParsingException but got : " + parsed);
+        } catch (Quaternion.QuaternionParsingException ex) {
+            // expected
+        }
+    }
+
+    @Test
+    public final void testParseInvalidI() {
+        try {
+            final Quaternion parsed = Quaternion.parse("[1.0 2.0x 3.0 4.0]");
+            Assert.fail("expecting QuaternionParsingException but got : " + parsed);
+        } catch (Quaternion.QuaternionParsingException ex) {
+            // expected
+        }
+    }
+
+    @Test
+    public final void testParseInvalidJ() {
+        try {
+            final Quaternion parsed = Quaternion.parse("[1.0 2.0 3.0x 4.0]");
+            Assert.fail("expecting QuaternionParsingException but got : " + parsed);
+        } catch (Quaternion.QuaternionParsingException ex) {
+            // expected
+        }
+    }
+
+    @Test
+    public final void testParseInvalidK() {
+        try {
+            final Quaternion parsed = Quaternion.parse("[1.0 2.0 3.0 4.0x]");
+            Assert.fail("expecting QuaternionParsingException but got : " + parsed);
+        } catch (Quaternion.QuaternionParsingException ex) {
+            // expected
+        }
+    }
+
+    @Test
     public final void testToString() {
-        final Quaternion q = new Quaternion(1, 2, 3, 4);
-        Assert.assertTrue(q.toString().equals("[1.0 2.0 3.0 4.0]"));
+        final Quaternion q = Quaternion.of(1, 2, 3, 4);
+        Assert.assertEquals("[1.0 2.0 3.0 4.0]", q.toString());
+    }
+
+    /**
+     * Assert that two quaternions are equal within tolerance
+     * @param actual
+     * @param expected
+     * @param tolerance
+     */
+    private void assertEquals(Quaternion actual, Quaternion expected, double tolerance) {
+        Assert.assertTrue("expecting " + expected + " but got " + actual, actual.equals(expected, tolerance));
     }
+
 }


[8/8] commons-numbers git commit: Merge branch 'NUMBERS-76__steve'

Posted by er...@apache.org.
Merge branch 'NUMBERS-76__steve'

Closes #17.


Project: http://git-wip-us.apache.org/repos/asf/commons-numbers/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-numbers/commit/851f85cc
Tree: http://git-wip-us.apache.org/repos/asf/commons-numbers/tree/851f85cc
Diff: http://git-wip-us.apache.org/repos/asf/commons-numbers/diff/851f85cc

Branch: refs/heads/master
Commit: 851f85cc92868acef7a892215554c235d8c46ac7
Parents: 447ced4 f4bafc2
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
Authored: Fri Nov 30 14:03:42 2018 +0100
Committer: Gilles Sadowski <gi...@harfang.homelinux.org>
Committed: Fri Nov 30 14:03:42 2018 +0100

----------------------------------------------------------------------
 .../apache/commons/numbers/complex/Complex.java |  46 +++-
 .../commons/numbers/quaternion/Quaternion.java  | 190 ++++++++++++---
 .../numbers/quaternion/QuaternionTest.java      | 235 ++++++++++++++-----
 3 files changed, 373 insertions(+), 98 deletions(-)
----------------------------------------------------------------------



[4/8] commons-numbers git commit: Javadoc.

Posted by er...@apache.org.
Javadoc.

Actual exception type is not part of the public API.


Project: http://git-wip-us.apache.org/repos/asf/commons-numbers/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-numbers/commit/2d454318
Tree: http://git-wip-us.apache.org/repos/asf/commons-numbers/tree/2d454318
Diff: http://git-wip-us.apache.org/repos/asf/commons-numbers/diff/2d454318

Branch: refs/heads/master
Commit: 2d454318eae9686192d4eb6b1fd4b330088b03be
Parents: e9e0164
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
Authored: Fri Nov 30 12:16:32 2018 +0100
Committer: Gilles Sadowski <gi...@harfang.homelinux.org>
Committed: Fri Nov 30 12:16:32 2018 +0100

----------------------------------------------------------------------
 .../java/org/apache/commons/numbers/quaternion/Quaternion.java     | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/2d454318/commons-numbers-quaternion/src/main/java/org/apache/commons/numbers/quaternion/Quaternion.java
----------------------------------------------------------------------
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 785b7e0..46b4766 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
@@ -493,7 +493,7 @@ public final class Quaternion implements Serializable {
      *
      * @param s String representation.
      * @return an instance.
-     * @throws QuaternionParsingException if the string does not
+     * @throws IllegalArgumentException if the string does not
      * conform to the specification.
      */
     public static Quaternion parse(String s) {


[5/8] commons-numbers git commit: Restrict visibility.

Posted by er...@apache.org.
Restrict visibility.


Project: http://git-wip-us.apache.org/repos/asf/commons-numbers/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-numbers/commit/a058f412
Tree: http://git-wip-us.apache.org/repos/asf/commons-numbers/tree/a058f412
Diff: http://git-wip-us.apache.org/repos/asf/commons-numbers/diff/a058f412

Branch: refs/heads/master
Commit: a058f4127604e745cf44661ad6f23a33557ea89d
Parents: 2d45431
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
Authored: Fri Nov 30 12:17:48 2018 +0100
Committer: Gilles Sadowski <gi...@harfang.homelinux.org>
Committed: Fri Nov 30 12:17:48 2018 +0100

----------------------------------------------------------------------
 .../java/org/apache/commons/numbers/quaternion/Quaternion.java     | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/a058f412/commons-numbers-quaternion/src/main/java/org/apache/commons/numbers/quaternion/Quaternion.java
----------------------------------------------------------------------
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 46b4766..4126a85 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
@@ -557,7 +557,7 @@ public final class Quaternion implements Serializable {
     }
 
     /** See {@link #parse(String)}. */
-    static class QuaternionParsingException extends IllegalArgumentException {
+    private static class QuaternionParsingException extends IllegalArgumentException {
         /** Serializable version identifier. */
         private static final long serialVersionUID = 20181128L;
 


[6/8] commons-numbers git commit: Use named constants.

Posted by er...@apache.org.
Use named constants.


Project: http://git-wip-us.apache.org/repos/asf/commons-numbers/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-numbers/commit/daa6a061
Tree: http://git-wip-us.apache.org/repos/asf/commons-numbers/tree/daa6a061
Diff: http://git-wip-us.apache.org/repos/asf/commons-numbers/diff/daa6a061

Branch: refs/heads/master
Commit: daa6a0616ac85869a2e078eb276438380643398a
Parents: a058f41
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
Authored: Fri Nov 30 14:01:49 2018 +0100
Committer: Gilles Sadowski <gi...@harfang.homelinux.org>
Committed: Fri Nov 30 14:01:49 2018 +0100

----------------------------------------------------------------------
 .../apache/commons/numbers/complex/Complex.java | 46 +++++++++++++++-----
 .../commons/numbers/quaternion/Quaternion.java  | 34 +++++++++------
 2 files changed, 57 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/daa6a061/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java
----------------------------------------------------------------------
diff --git a/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java b/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java
index 979c474..90a140c 100644
--- a/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java
+++ b/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java
@@ -56,9 +56,17 @@ public final class Complex implements Serializable  {
     public static final Complex ZERO = new Complex(0, 0);
     /** A complex number representing "NaN + NaN i" */
     private static final Complex NAN = new Complex(Double.NaN, Double.NaN);
+
     /** Serializable version identifier. */
     private static final long serialVersionUID = 20180201L;
 
+    /** {@link #toString() String representation}. */
+    private static final String FORMAT_START = "(";
+    /** {@link #toString() String representation}. */
+    private static final String FORMAT_END = ")";
+    /** {@link #toString() String representation}. */
+    private static final String FORMAT_SEP = ",";
+
     /** The imaginary part. */
     private final double imaginary;
     /** The real part. */
@@ -143,21 +151,33 @@ public final class Complex implements Serializable  {
      */
     public static Complex parse(String s) {
         final int len = s.length();
-        final int startParen = s.indexOf("(");
+        final int startParen = s.indexOf(FORMAT_START);
         if (startParen != 0) {
-            throw new ComplexParsingException("Missing start parenthesis");
+            throw new ComplexParsingException("Expected start string: " + FORMAT_START);
         }
-        final int endParen = s.indexOf(")");
+        final int endParen = s.indexOf(FORMAT_END);
         if (endParen != len - 1) {
-            throw new ComplexParsingException("Missing end parenthesis");
+            throw new ComplexParsingException("Expected end string: " + FORMAT_END);
         }
-        final int comma = s.indexOf(",");
-        if (comma == -1) {
-            throw new ComplexParsingException("Missing comma");
+        final String[] elements = s.substring(1, s.length() - 1).split(FORMAT_SEP);
+        if (elements.length != 2) {
+            throw new ComplexParsingException("Incorrect number of parts: Expected 2 but was " +
+                                              elements.length +
+                                              " (separator is '" + FORMAT_SEP + "')");
         }
 
-        final double re = Double.parseDouble(s.substring(startParen + 1, comma));
-        final double im = Double.parseDouble(s.substring(comma + 1, endParen));
+        final double re;
+        try {
+            re = Double.parseDouble(elements[0]);
+        } catch (NumberFormatException ex) {
+            throw new ComplexParsingException("Could not parse real part" + elements[0]);
+        }
+        final double im;
+        try {
+            im = Double.parseDouble(elements[1]);
+        } catch (NumberFormatException ex) {
+            throw new ComplexParsingException("Could not parse imaginary part" + elements[1]);
+        }
 
         return ofCartesian(re, im);
     }
@@ -1314,7 +1334,13 @@ public final class Complex implements Serializable  {
     /** {@inheritDoc} */
     @Override
     public String toString() {
-        return "(" + real + ", " + imaginary + ")";
+        final StringBuilder s = new StringBuilder();
+        s.append(FORMAT_START)
+            .append(real).append(FORMAT_SEP)
+            .append(imaginary)
+            .append(FORMAT_END);
+
+        return s.toString();
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/daa6a061/commons-numbers-quaternion/src/main/java/org/apache/commons/numbers/quaternion/Quaternion.java
----------------------------------------------------------------------
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 4126a85..afd1a63 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,6 +44,13 @@ public final class Quaternion implements Serializable {
     /** Error message. */
     private static final String ZERO_NORM_MSG = "Norm is zero";
 
+    /** {@link #toString() String representation}. */
+    private static final String FORMAT_START = "[";
+    /** {@link #toString() String representation}. */
+    private static final String FORMAT_END = "]";
+    /** {@link #toString() String representation}. */
+    private static final String FORMAT_SEP = " ";
+
     /** First component (scalar part). */
     private final double q0;
     /** Second component (first vector part). */
@@ -308,7 +315,7 @@ public final class Quaternion implements Serializable {
             return ((Double)q0).equals(q.q0) &&
                     ((Double)q1).equals(q.q1) &&
                     ((Double)q2).equals(q.q2) &&
-                    ((Double)q3).equals(q.q3) ;
+                    ((Double)q3).equals(q.q3);
         }
 
         return false;
@@ -498,17 +505,19 @@ public final class Quaternion implements Serializable {
      */
     public static Quaternion parse(String s) {
         final int len = s.length();
-        final int startBracket = s.indexOf("[");
+        final int startBracket = s.indexOf(FORMAT_START);
         if (startBracket != 0) {
-            throw new QuaternionParsingException("Missing opening square bracket");
+            throw new QuaternionParsingException("Expected start string: " + FORMAT_START);
         }
-        final int endBracket = s.indexOf("]");
+        final int endBracket = s.indexOf(FORMAT_END);
         if (endBracket != len - 1) {
-            throw new QuaternionParsingException("Missing closing square bracket");
+            throw new QuaternionParsingException("Expected end string: " + FORMAT_END);
         }
-        String[] elements = s.substring(1, s.length()-1).split(" ");
+        final String[] elements = s.substring(1, s.length() - 1).split(FORMAT_SEP);
         if (elements.length != 4) {
-            throw new QuaternionParsingException("Incorrect number of parts");
+            throw new QuaternionParsingException("Incorrect number of parts: Expected 4 but was " +
+                                                 elements.length +
+                                                 " (separator is '" + FORMAT_SEP + "')");
         }
 
         final double q1;
@@ -544,14 +553,13 @@ public final class Quaternion implements Serializable {
      */
     @Override
     public String toString() {
-        final String sp = " ";
         final StringBuilder s = new StringBuilder();
-        s.append("[")
-            .append(q0).append(sp)
-            .append(q1).append(sp)
-            .append(q2).append(sp)
+        s.append(FORMAT_START)
+            .append(q0).append(FORMAT_SEP)
+            .append(q1).append(FORMAT_SEP)
+            .append(q2).append(FORMAT_SEP)
             .append(q3)
-            .append("]");
+            .append(FORMAT_END);
 
         return s.toString();
     }


[3/8] commons-numbers git commit: NUMBERS-76 Make "Quaternion" a VALJO - Fix Checkstyle issues

Posted by er...@apache.org.
NUMBERS-76 Make "Quaternion" a VALJO - Fix Checkstyle issues


Project: http://git-wip-us.apache.org/repos/asf/commons-numbers/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-numbers/commit/e9e0164f
Tree: http://git-wip-us.apache.org/repos/asf/commons-numbers/tree/e9e0164f
Diff: http://git-wip-us.apache.org/repos/asf/commons-numbers/diff/e9e0164f

Branch: refs/heads/master
Commit: e9e0164fbb9676034a6c3597d2e63d2505295346
Parents: 2f33d38
Author: Steve Bosman <St...@gmail.com>
Authored: Fri Nov 30 00:28:19 2018 +0000
Committer: Steve Bosman <St...@gmail.com>
Committed: Fri Nov 30 00:28:19 2018 +0000

----------------------------------------------------------------------
 .../commons/numbers/quaternion/Quaternion.java  | 79 ++++++++++----------
 1 file changed, 41 insertions(+), 38 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/e9e0164f/commons-numbers-quaternion/src/main/java/org/apache/commons/numbers/quaternion/Quaternion.java
----------------------------------------------------------------------
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 5007ef4..785b7e0 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
@@ -61,11 +61,14 @@ public final class Quaternion implements Serializable {
      * @param c Second vector component.
      * @param d Third vector component.
      */
-    public static Quaternion of(final double a,
-                                final double b,
-                                final double c,
-                                final double d) {
-        return new Quaternion(a, b, c, d);
+    private Quaternion(final double a,
+                      final double b,
+                      final double c,
+                      final double d) {
+        q0 = a;
+        q1 = b;
+        q2 = c;
+        q3 = d;
     }
 
     /**
@@ -76,19 +79,16 @@ public final class Quaternion implements Serializable {
      *
      * @throws IllegalArgumentException if the array length is not 3.
      */
-    public static Quaternion of(final double scalar,
-                                final double[] v) {
-        return new Quaternion(scalar, v);
-    }
+    private Quaternion(final double scalar,
+                      final double[] v) {
+        if (v.length != 3) {
+            throw new IllegalArgumentException("Size of array must be 3");
+        }
 
-    /**
-     * Builds a pure quaternion from a vector (assuming that the scalar
-     * part is zero).
-     *
-     * @param v Components of the vector part of the pure quaternion.
-     */
-    public static Quaternion of(final double[] v) {
-        return new Quaternion(0, v);
+        q0 = scalar;
+        q1 = v[0];
+        q2 = v[1];
+        q3 = v[2];
     }
 
     /**
@@ -98,15 +98,13 @@ public final class Quaternion implements Serializable {
      * @param b First vector component.
      * @param c Second vector component.
      * @param d Third vector component.
+     * @return a quaternion instance
      */
-    private Quaternion(final double a,
-                      final double b,
-                      final double c,
-                      final double d) {
-        q0 = a;
-        q1 = b;
-        q2 = c;
-        q3 = d;
+    public static Quaternion of(final double a,
+                                final double b,
+                                final double c,
+                                final double d) {
+        return new Quaternion(a, b, c, d);
     }
 
     /**
@@ -114,19 +112,24 @@ public final class Quaternion implements Serializable {
      *
      * @param scalar Scalar part of the quaternion.
      * @param v Components of the vector part of the quaternion.
+     * @return a quaternion instance
      *
      * @throws IllegalArgumentException if the array length is not 3.
      */
-    private Quaternion(final double scalar,
-                      final double[] v) {
-        if (v.length != 3) {
-            throw new IllegalArgumentException("Size of array must be 3");
-        }
+    public static Quaternion of(final double scalar,
+                                final double[] v) {
+        return new Quaternion(scalar, v);
+    }
 
-        q0 = scalar;
-        q1 = v[0];
-        q2 = v[1];
-        q3 = v[2];
+    /**
+     * Builds a pure quaternion from a vector (assuming that the scalar
+     * part is zero).
+     *
+     * @param v Components of the vector part of the pure quaternion.
+     * @return a quaternion instance
+     */
+    public static Quaternion of(final double[] v) {
+        return new Quaternion(0, v);
     }
 
     /**
@@ -511,25 +514,25 @@ public final class Quaternion implements Serializable {
         final double q1;
         try {
             q1 = Double.parseDouble(elements[0]);
-        } catch (Exception ex) {
+        } catch (NumberFormatException ex) {
             throw new QuaternionParsingException("Could not parse scalar part" + elements[0]);
         }
         final double q2;
         try {
             q2 = Double.parseDouble(elements[1]);
-        } catch (Exception ex) {
+        } catch (NumberFormatException ex) {
             throw new QuaternionParsingException("Could not parse i part" + elements[1]);
         }
         final double q3;
         try {
             q3 = Double.parseDouble(elements[2]);
-        } catch (Exception ex) {
+        } catch (NumberFormatException ex) {
             throw new QuaternionParsingException("Could not parse j part" + elements[2]);
         }
         final double q4;
         try {
             q4 = Double.parseDouble(elements[3]);
-        } catch (Exception ex) {
+        } catch (NumberFormatException ex) {
             throw new QuaternionParsingException("Could not parse k part" + elements[3]);
         }
 


[7/8] commons-numbers git commit: Simplify unit tests.

Posted by er...@apache.org.
Simplify unit tests.


Project: http://git-wip-us.apache.org/repos/asf/commons-numbers/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-numbers/commit/f4bafc24
Tree: http://git-wip-us.apache.org/repos/asf/commons-numbers/tree/f4bafc24
Diff: http://git-wip-us.apache.org/repos/asf/commons-numbers/diff/f4bafc24

Branch: refs/heads/master
Commit: f4bafc24f3267340e00e39ee4983e349361bb4c0
Parents: daa6a06
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
Authored: Fri Nov 30 14:02:45 2018 +0100
Committer: Gilles Sadowski <gi...@harfang.homelinux.org>
Committed: Fri Nov 30 14:02:45 2018 +0100

----------------------------------------------------------------------
 .../numbers/quaternion/QuaternionTest.java      | 63 +++++---------------
 1 file changed, 14 insertions(+), 49 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/f4bafc24/commons-numbers-quaternion/src/test/java/org/apache/commons/numbers/quaternion/QuaternionTest.java
----------------------------------------------------------------------
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 fa2f342..9dc3074 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
@@ -527,74 +527,39 @@ public class QuaternionTest {
         Assert.assertEquals(-0xa.cp0, parsed.getQ3(), EPS);
     }
 
-    @Test
+    @Test(expected = IllegalArgumentException.class)
     public final void testParseMissingStart() {
-        try {
-            final Quaternion parsed = Quaternion.parse("1.0 2.0 3.0 4.0]");
-            Assert.fail("expecting QuaternionParsingException but got : " + parsed);
-        } catch (Quaternion.QuaternionParsingException ex) {
-            // expected
-        }
+        Quaternion.parse("1.0 2.0 3.0 4.0]");
     }
 
-    @Test
+    @Test(expected = IllegalArgumentException.class)
     public final void testParseMissingEnd() {
-        try {
-            final Quaternion parsed = Quaternion.parse("[1.0 2.0 3.0 4.0");
-            Assert.fail("expecting QuaternionParsingException but got : " + parsed);
-        } catch (Quaternion.QuaternionParsingException ex) {
-            // expected
-        }
+        Quaternion.parse("[1.0 2.0 3.0 4.0");
     }
 
-    @Test
+    @Test(expected = IllegalArgumentException.class)
     public final void testParseMissingPart() {
-        try {
-            final Quaternion parsed = Quaternion.parse("[1.0 2.0 3.0 ]");
-            Assert.fail("expecting QuaternionParsingException but got : " + parsed);
-        } catch (Quaternion.QuaternionParsingException ex) {
-            // expected
-        }
+        Quaternion.parse("[1.0 2.0 3.0 ]");
     }
 
-    @Test
+    @Test(expected = IllegalArgumentException.class)
     public final void testParseInvalidScalar() {
-        try {
-            final Quaternion parsed = Quaternion.parse("[1.x 2.0 3.0 4.0]");
-            Assert.fail("expecting QuaternionParsingException but got : " + parsed);
-        } catch (Quaternion.QuaternionParsingException ex) {
-            // expected
-        }
+        Quaternion.parse("[1.x 2.0 3.0 4.0]");
     }
 
-    @Test
+    @Test(expected = IllegalArgumentException.class)
     public final void testParseInvalidI() {
-        try {
-            final Quaternion parsed = Quaternion.parse("[1.0 2.0x 3.0 4.0]");
-            Assert.fail("expecting QuaternionParsingException but got : " + parsed);
-        } catch (Quaternion.QuaternionParsingException ex) {
-            // expected
-        }
+        Quaternion.parse("[1.0 2.0x 3.0 4.0]");
     }
 
-    @Test
+    @Test(expected = IllegalArgumentException.class)
     public final void testParseInvalidJ() {
-        try {
-            final Quaternion parsed = Quaternion.parse("[1.0 2.0 3.0x 4.0]");
-            Assert.fail("expecting QuaternionParsingException but got : " + parsed);
-        } catch (Quaternion.QuaternionParsingException ex) {
-            // expected
-        }
+        Quaternion.parse("[1.0 2.0 3.0x 4.0]");
     }
 
-    @Test
+    @Test(expected = IllegalArgumentException.class)
     public final void testParseInvalidK() {
-        try {
-            final Quaternion parsed = Quaternion.parse("[1.0 2.0 3.0 4.0x]");
-            Assert.fail("expecting QuaternionParsingException but got : " + parsed);
-        } catch (Quaternion.QuaternionParsingException ex) {
-            // expected
-        }
+        Quaternion.parse("[1.0 2.0 3.0 4.0x]");
     }
 
     @Test


[2/8] commons-numbers git commit: NUMBERS-76 Make "Quaternion" a VALJO - correct Javadoc

Posted by er...@apache.org.
NUMBERS-76 Make "Quaternion" a VALJO - correct Javadoc


Project: http://git-wip-us.apache.org/repos/asf/commons-numbers/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-numbers/commit/2f33d38d
Tree: http://git-wip-us.apache.org/repos/asf/commons-numbers/tree/2f33d38d
Diff: http://git-wip-us.apache.org/repos/asf/commons-numbers/diff/2f33d38d

Branch: refs/heads/master
Commit: 2f33d38d6ec5513ebbad44ffad237ebbc1ae9199
Parents: 9c6f778
Author: Steve Bosman <St...@gmail.com>
Authored: Wed Nov 28 23:23:57 2018 +0000
Committer: Steve Bosman <St...@gmail.com>
Committed: Wed Nov 28 23:23:57 2018 +0000

----------------------------------------------------------------------
 .../java/org/apache/commons/numbers/quaternion/Quaternion.java     | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/2f33d38d/commons-numbers-quaternion/src/main/java/org/apache/commons/numbers/quaternion/Quaternion.java
----------------------------------------------------------------------
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 0582625..5007ef4 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
@@ -490,7 +490,7 @@ public final class Quaternion implements Serializable {
      *
      * @param s String representation.
      * @return an instance.
-     * @throws IllegalArgumentException if the string does not
+     * @throws QuaternionParsingException if the string does not
      * conform to the specification.
      */
     public static Quaternion parse(String s) {