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-geometry.git
commit eccd310527ee4826f89b4adab6997a5bb8f21fc3 Author: Matt Juntunen <matt.juntu...@hotmail.com> AuthorDate: Mon Sep 17 00:15:52 2018 -0400 GEOMETRY-17: removing Vector getMagnitude() and getMagnitudeSq() alias methods; renaming withMagnitude() to withNorm() --- .../org/apache/commons/geometry/core/Vector.java | 27 ++------- .../commons/geometry/euclidean/oned/Vector1D.java | 16 +----- .../geometry/euclidean/threed/Vector3D.java | 16 +----- .../commons/geometry/euclidean/twod/Vector2D.java | 16 +----- .../geometry/euclidean/oned/Vector1DTest.java | 46 +++++---------- .../geometry/euclidean/threed/Vector3DTest.java | 66 ++++++++-------------- .../geometry/euclidean/twod/Vector2DTest.java | 58 ++++++------------- 7 files changed, 68 insertions(+), 177 deletions(-) diff --git a/commons-geometry-core/src/main/java/org/apache/commons/geometry/core/Vector.java b/commons-geometry-core/src/main/java/org/apache/commons/geometry/core/Vector.java index 4804a80..f835730 100644 --- a/commons-geometry-core/src/main/java/org/apache/commons/geometry/core/Vector.java +++ b/commons-geometry-core/src/main/java/org/apache/commons/geometry/core/Vector.java @@ -44,8 +44,8 @@ public interface Vector<V extends Vector<V>> extends Spatial { double getNorm1(); /** Get the L<sub>2</sub> norm (commonly known as the Euclidean norm) for the vector. - * This corresponds to the common notion of vector magnitude or length. - * This is defined as the square root of the sum of the squares of all vector components. + * This corresponds to the common notion of vector magnitude or length and + * is defined as the square root of the sum of the squares of all vector components. * @see <a href="http://mathworld.wolfram.com/L2-Norm.html">L2 Norm</a> * @return L<sub>2</sub> norm for the vector */ @@ -65,28 +65,13 @@ public interface Vector<V extends Vector<V>> extends Spatial { */ double getNormInf(); - /** Returns the magnitude (i.e. length) of the vector. This is - * the same value as returned by {@link #getNorm()}. - * @return the magnitude, or length, of the vector - * @see #getNorm() - */ - double getMagnitude(); - - /** Returns the squared magnitude of the vector. This is the - * same value as returned by {@link #getNormSq()}. - * @return the squared magnitude of the vector - * @see #getMagnitude() - * @see #getNormSq() - */ - double getMagnitudeSq(); - /** Returns a vector with the same direction but with the given - * magnitude. This is equivalent to calling {@code vec.normalize().scalarMultiply(mag)} + * norm. This is equivalent to calling {@code vec.normalize().scalarMultiply(mag)} * but without the intermediate vector. - * @param magnitude The vector magnitude - * @return a vector with the same direction as the current instance but the given magnitude + * @param norm The vector norm + * @return a vector with the same direction as the current instance but the given norm */ - V withMagnitude(double magnitude); + V withNorm(double norm); /** Add a vector to the instance. * @param v vector to add diff --git a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/oned/Vector1D.java b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/oned/Vector1D.java index b678559..393f0a3 100644 --- a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/oned/Vector1D.java +++ b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/oned/Vector1D.java @@ -104,19 +104,7 @@ public class Vector1D extends Cartesian1D implements EuclideanVector<Point1D, Ve /** {@inheritDoc} */ @Override - public double getMagnitude() { - return getNorm(); - } - - /** {@inheritDoc} */ - @Override - public double getMagnitudeSq() { - return getNormSq(); - } - - /** {@inheritDoc} */ - @Override - public Vector1D withMagnitude(double magnitude) { + public Vector1D withNorm(double magnitude) { Vectors.ensureFiniteNonZeroNorm(getNorm()); return (getX() > 0.0)? new Vector1D(magnitude) : new Vector1D(-magnitude); @@ -415,7 +403,7 @@ public class Vector1D extends Cartesian1D implements EuclideanVector<Point1D, Ve /** {@inheritDoc} */ @Override - public Vector1D withMagnitude(final double mag) { + public Vector1D withNorm(final double mag) { return scalarMultiply(mag); } } diff --git a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/Vector3D.java b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/Vector3D.java index 0014d4d..5dfea76 100644 --- a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/Vector3D.java +++ b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/Vector3D.java @@ -120,19 +120,7 @@ public class Vector3D extends Cartesian3D implements EuclideanVector<Point3D, Ve /** {@inheritDoc} */ @Override - public double getMagnitude() { - return getNorm(); - } - - /** {@inheritDoc} */ - @Override - public double getMagnitudeSq() { - return getNormSq(); - } - - /** {@inheritDoc} */ - @Override - public Vector3D withMagnitude(double magnitude) { + public Vector3D withNorm(double magnitude) { final double invNorm = 1.0 / getFiniteNonZeroNorm(); return new Vector3D( @@ -613,7 +601,7 @@ public class Vector3D extends Cartesian3D implements EuclideanVector<Point3D, Ve /** {@inheritDoc} */ @Override - public Vector3D withMagnitude(final double mag) { + public Vector3D withNorm(final double mag) { return scalarMultiply(mag); } } diff --git a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/twod/Vector2D.java b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/twod/Vector2D.java index 7ae7edf..4b4b903 100644 --- a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/twod/Vector2D.java +++ b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/twod/Vector2D.java @@ -110,19 +110,7 @@ public class Vector2D extends Cartesian2D implements EuclideanVector<Point2D, Ve /** {@inheritDoc} */ @Override - public double getMagnitude() { - return getNorm(); - } - - /** {@inheritDoc} */ - @Override - public double getMagnitudeSq() { - return getNormSq(); - } - - /** {@inheritDoc} */ - @Override - public Vector2D withMagnitude(double magnitude) { + public Vector2D withNorm(double magnitude) { final double invNorm = 1.0 / getFiniteNonZeroNorm(); return new Vector2D( @@ -519,7 +507,7 @@ public class Vector2D extends Cartesian2D implements EuclideanVector<Point2D, Ve /** {@inheritDoc} */ @Override - public Vector2D withMagnitude(final double mag) { + public Vector2D withNorm(final double mag) { return scalarMultiply(mag); } } diff --git a/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/oned/Vector1DTest.java b/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/oned/Vector1DTest.java index 3c7bd50..8e1b797 100644 --- a/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/oned/Vector1DTest.java +++ b/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/oned/Vector1DTest.java @@ -110,58 +110,42 @@ public class Vector1DTest { } @Test - public void testMagnitude() { + public void testWithNorm() { // act/assert - Assert.assertEquals(0.0, Vector1D.ZERO.getMagnitude(), TEST_TOLERANCE); - Assert.assertEquals(3.0, Vector1D.of(3).getMagnitude(), TEST_TOLERANCE); - Assert.assertEquals(3.0, Vector1D.of(-3).getMagnitude(), TEST_TOLERANCE); - } - - @Test - public void testMagnitudeSq() { - // act/assert - Assert.assertEquals(0.0, Vector1D.of(0).getMagnitudeSq(), TEST_TOLERANCE); - Assert.assertEquals(9.0, Vector1D.of(3).getMagnitudeSq(), TEST_TOLERANCE); - Assert.assertEquals(9.0, Vector1D.of(-3).getMagnitudeSq(), TEST_TOLERANCE); - } - - @Test - public void testWithMagnitude() { - // act/assert - checkVector(Vector1D.ONE.withMagnitude(0.0), 0.0); + checkVector(Vector1D.ONE.withNorm(0.0), 0.0); - checkVector(Vector1D.of(0.5).withMagnitude(2.0), 2.0); - checkVector(Vector1D.of(5).withMagnitude(3.0), 3.0); + checkVector(Vector1D.of(0.5).withNorm(2.0), 2.0); + checkVector(Vector1D.of(5).withNorm(3.0), 3.0); - checkVector(Vector1D.of(-0.5).withMagnitude(2.0), -2.0); - checkVector(Vector1D.of(-5).withMagnitude(3.0), -3.0); + checkVector(Vector1D.of(-0.5).withNorm(2.0), -2.0); + checkVector(Vector1D.of(-5).withNorm(3.0), -3.0); } @Test - public void testWithMagnitude_illegalNorm() { + public void testWithNorm_illegalNorm() { // act/assert - GeometryTestUtils.assertThrows(() -> Vector1D.ZERO.withMagnitude(2.0), + GeometryTestUtils.assertThrows(() -> Vector1D.ZERO.withNorm(2.0), IllegalNormException.class); - GeometryTestUtils.assertThrows(() -> Vector1D.NaN.withMagnitude(2.0), + GeometryTestUtils.assertThrows(() -> Vector1D.NaN.withNorm(2.0), IllegalNormException.class); - GeometryTestUtils.assertThrows(() -> Vector1D.POSITIVE_INFINITY.withMagnitude(2.0), + GeometryTestUtils.assertThrows(() -> Vector1D.POSITIVE_INFINITY.withNorm(2.0), IllegalNormException.class); - GeometryTestUtils.assertThrows(() -> Vector1D.NEGATIVE_INFINITY.withMagnitude(2.0), + GeometryTestUtils.assertThrows(() -> Vector1D.NEGATIVE_INFINITY.withNorm(2.0), IllegalNormException.class); } @Test - public void testWithMagnitude_unitVectors() { + public void testWithNorm_unitVectors() { // arrange Vector1D v = Vector1D.of(2.0).normalize(); // act/assert - checkVector(Vector1D.ONE.withMagnitude(2.5), 2.5); - checkVector(Vector1D.MINUS_ONE.withMagnitude(3.14), -3.14); + checkVector(Vector1D.ONE.withNorm(2.5), 2.5); + checkVector(Vector1D.MINUS_ONE.withNorm(3.14), -3.14); for (double mag = -10.0; mag <= 10.0; ++mag) { - Assert.assertEquals(Math.abs(mag), v.withMagnitude(mag).getMagnitude(), TEST_TOLERANCE); + Assert.assertEquals(Math.abs(mag), v.withNorm(mag).getNorm(), TEST_TOLERANCE); } } diff --git a/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/threed/Vector3DTest.java b/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/threed/Vector3DTest.java index 8d6fe9e..0997f57 100644 --- a/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/threed/Vector3DTest.java +++ b/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/threed/Vector3DTest.java @@ -126,23 +126,7 @@ public class Vector3DTest { } @Test - public void testMagnitude() { - // act/assert - Assert.assertEquals(0.0, Vector3D.ZERO.getMagnitude(), 0); - Assert.assertEquals(Math.sqrt(29), Vector3D.of(2, 3, 4).getMagnitude(), EPS); - Assert.assertEquals(Math.sqrt(29), Vector3D.of(-2, -3, -4).getMagnitude(), EPS); - } - - @Test - public void testMagnitudeSq() { - // act/assert - Assert.assertEquals(0.0, Vector3D.ZERO.getMagnitudeSq(), 0); - Assert.assertEquals(29, Vector3D.of(2, 3, 4).getMagnitudeSq(), EPS); - Assert.assertEquals(29, Vector3D.of(-2, -3, -4).getMagnitudeSq(), EPS); - } - - @Test - public void testWithMagnitude() { + public void testWithNorm() { // arrange double x = 2; double y = 3; @@ -155,55 +139,55 @@ public class Vector3DTest { double normZ = z / len; // act/assert - checkVector(Vector3D.of(x, y, z).withMagnitude(0.0), 0.0, 0.0, 0.0); + checkVector(Vector3D.of(x, y, z).withNorm(0.0), 0.0, 0.0, 0.0); - checkVector(Vector3D.of(x, y, z).withMagnitude(1.0), normX, normY, normZ); - checkVector(Vector3D.of(x, y, -z).withMagnitude(1.0), normX, normY, -normZ); - checkVector(Vector3D.of(x, -y, z).withMagnitude(1.0), normX, -normY, normZ); - checkVector(Vector3D.of(x, -y, -z).withMagnitude(1.0), normX, -normY, -normZ); - checkVector(Vector3D.of(-x, y, z).withMagnitude(1.0), -normX, normY, normZ); - checkVector(Vector3D.of(-x, y, -z).withMagnitude(1.0), -normX, normY, -normZ); - checkVector(Vector3D.of(-x, -y, z).withMagnitude(1.0), -normX, -normY, normZ); - checkVector(Vector3D.of(-x, -y, -z).withMagnitude(1.0), -normX, -normY, -normZ); + checkVector(Vector3D.of(x, y, z).withNorm(1.0), normX, normY, normZ); + checkVector(Vector3D.of(x, y, -z).withNorm(1.0), normX, normY, -normZ); + checkVector(Vector3D.of(x, -y, z).withNorm(1.0), normX, -normY, normZ); + checkVector(Vector3D.of(x, -y, -z).withNorm(1.0), normX, -normY, -normZ); + checkVector(Vector3D.of(-x, y, z).withNorm(1.0), -normX, normY, normZ); + checkVector(Vector3D.of(-x, y, -z).withNorm(1.0), -normX, normY, -normZ); + checkVector(Vector3D.of(-x, -y, z).withNorm(1.0), -normX, -normY, normZ); + checkVector(Vector3D.of(-x, -y, -z).withNorm(1.0), -normX, -normY, -normZ); - checkVector(Vector3D.of(x, y, z).withMagnitude(0.5), 0.5 * normX, 0.5 * normY, 0.5 * normZ); - checkVector(Vector3D.of(x, y, z).withMagnitude(3), 3 * normX, 3 * normY, 3 * normZ); + checkVector(Vector3D.of(x, y, z).withNorm(0.5), 0.5 * normX, 0.5 * normY, 0.5 * normZ); + checkVector(Vector3D.of(x, y, z).withNorm(3), 3 * normX, 3 * normY, 3 * normZ); - checkVector(Vector3D.of(x, y, z).withMagnitude(-0.5), -0.5 * normX, -0.5 * normY, -0.5 * normZ); - checkVector(Vector3D.of(x, y, z).withMagnitude(-3), -3 * normX, -3 * normY, -3 * normZ); + checkVector(Vector3D.of(x, y, z).withNorm(-0.5), -0.5 * normX, -0.5 * normY, -0.5 * normZ); + checkVector(Vector3D.of(x, y, z).withNorm(-3), -3 * normX, -3 * normY, -3 * normZ); for (double mag = -10.0; mag <= 10.0; ++mag) { - Assert.assertEquals(Math.abs(mag), Vector3D.of(x, y, z).withMagnitude(mag).getMagnitude(), EPS); + Assert.assertEquals(Math.abs(mag), Vector3D.of(x, y, z).withNorm(mag).getNorm(), EPS); } } @Test - public void testWithMagnitude_illegalNorm() { + public void testWithNorm_illegalNorm() { // act/assert - GeometryTestUtils.assertThrows(() -> Vector3D.ZERO.withMagnitude(2.0), + GeometryTestUtils.assertThrows(() -> Vector3D.ZERO.withNorm(2.0), IllegalNormException.class); - GeometryTestUtils.assertThrows(() -> Vector3D.NaN.withMagnitude(2.0), + GeometryTestUtils.assertThrows(() -> Vector3D.NaN.withNorm(2.0), IllegalNormException.class); - GeometryTestUtils.assertThrows(() -> Vector3D.POSITIVE_INFINITY.withMagnitude(2.0), + GeometryTestUtils.assertThrows(() -> Vector3D.POSITIVE_INFINITY.withNorm(2.0), IllegalNormException.class); - GeometryTestUtils.assertThrows(() -> Vector3D.NEGATIVE_INFINITY.withMagnitude(2.0), + GeometryTestUtils.assertThrows(() -> Vector3D.NEGATIVE_INFINITY.withNorm(2.0), IllegalNormException.class); } @Test - public void testWithMagnitude_unitVectors() { + public void testWithNorm_unitVectors() { // arrange Vector3D v = Vector3D.of(2.0, -3.0, 4.0).normalize(); // act/assert - checkVector(Vector3D.PLUS_X.withMagnitude(2.5), 2.5, 0.0, 0.0); - checkVector(Vector3D.MINUS_Y.withMagnitude(3.14), 0.0, -3.14, 0.0); - checkVector(Vector3D.PLUS_Z.withMagnitude(-1.1), 0.0, 0.0, -1.1); + checkVector(Vector3D.PLUS_X.withNorm(2.5), 2.5, 0.0, 0.0); + checkVector(Vector3D.MINUS_Y.withNorm(3.14), 0.0, -3.14, 0.0); + checkVector(Vector3D.PLUS_Z.withNorm(-1.1), 0.0, 0.0, -1.1); for (double mag = -10.0; mag <= 10.0; ++mag) { - Assert.assertEquals(Math.abs(mag), v.withMagnitude(mag).getMagnitude(), EPS); + Assert.assertEquals(Math.abs(mag), v.withNorm(mag).getNorm(), EPS); } } diff --git a/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/twod/Vector2DTest.java b/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/twod/Vector2DTest.java index 0beb633..183756f 100644 --- a/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/twod/Vector2DTest.java +++ b/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/twod/Vector2DTest.java @@ -128,70 +128,44 @@ public class Vector2DTest { } @Test - public void testMagnitude() { + public void testWithNorm() { // act/assert - Assert.assertEquals(0.0, Vector2D.of(0, 0).getMagnitude(), EPS); + checkVector(Vector2D.of(3, 4).withNorm(1.0), 0.6, 0.8); + checkVector(Vector2D.of(4, 3).withNorm(1.0), 0.8, 0.6); - Assert.assertEquals(5.0, Vector2D.of(3, 4).getMagnitude(), EPS); - Assert.assertEquals(5.0, Vector2D.of(3, -4).getMagnitude(), EPS); - Assert.assertEquals(5.0, Vector2D.of(-3, 4).getMagnitude(), EPS); - Assert.assertEquals(5.0, Vector2D.of(-3, -4).getMagnitude(), EPS); + checkVector(Vector2D.of(-3, 4).withNorm(0.5), -0.3, 0.4); + checkVector(Vector2D.of(3, -4).withNorm(2.0), 1.2, -1.6); + checkVector(Vector2D.of(-3, -4).withNorm(3.0), -1.8, 3.0 * Math.sin(Math.atan2(-4, -3))); - Assert.assertEquals(Math.sqrt(5.0), Vector2D.of(-1, -2).getMagnitude(), EPS); + checkVector(Vector2D.of(0.5, 0.5).withNorm(2), Math.sqrt(2), Math.sqrt(2)); } @Test - public void testMagnitudeSq() { + public void testWithNorm_illegalNorm() { // act/assert - Assert.assertEquals(0.0, Vector2D.of(0, 0).getMagnitudeSq(), EPS); - - Assert.assertEquals(25.0, Vector2D.of(3, 4).getMagnitudeSq(), EPS); - Assert.assertEquals(25.0, Vector2D.of(3, -4).getMagnitudeSq(), EPS); - Assert.assertEquals(25.0, Vector2D.of(-3, 4).getMagnitudeSq(), EPS); - Assert.assertEquals(25.0, Vector2D.of(-3, -4).getMagnitudeSq(), EPS); - - Assert.assertEquals(5.0, Vector2D.of(-1, -2).getMagnitudeSq(), EPS); - } - - @Test - public void testWithMagnitude() { - // act/assert - checkVector(Vector2D.of(3, 4).withMagnitude(1.0), 0.6, 0.8); - checkVector(Vector2D.of(4, 3).withMagnitude(1.0), 0.8, 0.6); - - checkVector(Vector2D.of(-3, 4).withMagnitude(0.5), -0.3, 0.4); - checkVector(Vector2D.of(3, -4).withMagnitude(2.0), 1.2, -1.6); - checkVector(Vector2D.of(-3, -4).withMagnitude(3.0), -1.8, 3.0 * Math.sin(Math.atan2(-4, -3))); - - checkVector(Vector2D.of(0.5, 0.5).withMagnitude(2), Math.sqrt(2), Math.sqrt(2)); - } - - @Test - public void testWithMagnitude_illegalNorm() { - // act/assert - GeometryTestUtils.assertThrows(() -> Vector2D.ZERO.withMagnitude(2.0), + GeometryTestUtils.assertThrows(() -> Vector2D.ZERO.withNorm(2.0), IllegalNormException.class); - GeometryTestUtils.assertThrows(() -> Vector2D.NaN.withMagnitude(2.0), + GeometryTestUtils.assertThrows(() -> Vector2D.NaN.withNorm(2.0), IllegalNormException.class); - GeometryTestUtils.assertThrows(() -> Vector2D.POSITIVE_INFINITY.withMagnitude(2.0), + GeometryTestUtils.assertThrows(() -> Vector2D.POSITIVE_INFINITY.withNorm(2.0), IllegalNormException.class); - GeometryTestUtils.assertThrows(() -> Vector2D.NEGATIVE_INFINITY.withMagnitude(2.0), + GeometryTestUtils.assertThrows(() -> Vector2D.NEGATIVE_INFINITY.withNorm(2.0), IllegalNormException.class); } @Test - public void testWithMagnitude_unitVectors() { + public void testWithNorm_unitVectors() { // arrange double eps = 1e-14; Vector2D v = Vector2D.of(2.0, -3.0).normalize(); // act/assert - checkVector(Vector2D.PLUS_X.withMagnitude(2.5), 2.5, 0.0); - checkVector(Vector2D.MINUS_Y.withMagnitude(3.14), 0.0, -3.14); + checkVector(Vector2D.PLUS_X.withNorm(2.5), 2.5, 0.0); + checkVector(Vector2D.MINUS_Y.withNorm(3.14), 0.0, -3.14); for (double mag = -10.0; mag <= 10.0; ++mag) { - Assert.assertEquals(Math.abs(mag), v.withMagnitude(mag).getMagnitude(), eps); + Assert.assertEquals(Math.abs(mag), v.withNorm(mag).getNorm(), eps); } }