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 2017/05/24 16:02:33 UTC

commons-numbers git commit: NUMBERS-30: Cosine of angle between two vectors (in Cartesian coordinates).

Repository: commons-numbers
Updated Branches:
  refs/heads/master 9f1d9ef6f -> d195a7bbb


NUMBERS-30: Cosine of angle between two vectors (in Cartesian coordinates).


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

Branch: refs/heads/master
Commit: d195a7bbbfe3d0bdcb4b7d2a36aa5a120fd496e8
Parents: 9f1d9ef
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
Authored: Wed May 24 18:00:56 2017 +0200
Committer: Gilles Sadowski <gi...@harfang.homelinux.org>
Committed: Wed May 24 18:00:56 2017 +0200

----------------------------------------------------------------------
 .../apache/commons/numbers/core/CosAngle.java   | 35 +++++++++
 .../commons/numbers/core/CosAngleTest.java      | 77 ++++++++++++++++++++
 2 files changed, 112 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/d195a7bb/commons-numbers-core/src/main/java/org/apache/commons/numbers/core/CosAngle.java
----------------------------------------------------------------------
diff --git a/commons-numbers-core/src/main/java/org/apache/commons/numbers/core/CosAngle.java b/commons-numbers-core/src/main/java/org/apache/commons/numbers/core/CosAngle.java
new file mode 100644
index 0000000..e5f7e9c
--- /dev/null
+++ b/commons-numbers-core/src/main/java/org/apache/commons/numbers/core/CosAngle.java
@@ -0,0 +1,35 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.numbers.core;
+
+/**
+ * Computes the cosine of the angle between two vectors.
+ */
+public class CosAngle {
+    /**
+     * Computes the cosine of the angle between {@code v1} and {@code v2}.
+     *
+     * @param v1 Cartesian coordinates of the first vector.
+     * @param v2 Cartesian coordinates of the second vector.
+     * @return the cosine of the angle between the vectors.
+     */
+    public static double value(double[] v1,
+                               double[] v2) {
+        return LinearCombination.value(v1, v2) / SafeNorm.value(v1) / SafeNorm.value(v2);
+    }
+}
+

http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/d195a7bb/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/CosAngleTest.java
----------------------------------------------------------------------
diff --git a/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/CosAngleTest.java b/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/CosAngleTest.java
new file mode 100644
index 0000000..63eeca0
--- /dev/null
+++ b/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/CosAngleTest.java
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
+ * or agreed to in writing, software distributed under the License is
+ * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the specific language
+ * governing permissions and limitations under the License.
+ */
+package org.apache.commons.numbers.core;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Test cases for the {@link CosAngle} class.
+ */
+public class CosAngleTest {
+    @Test
+    public void testCosAngle2D() {
+        double expected;
+
+        final double[] v1 = { 1, 0 };
+        expected = 1;
+        Assert.assertEquals(expected, CosAngle.value(v1, v1), 0d);
+
+        final double[] v2 = { 0, 1 };
+        expected = 0;
+        Assert.assertEquals(expected, CosAngle.value(v1, v2), 0d);
+
+        final double[] v3 = { 7, 7 };
+        expected = Math.sqrt(2) / 2;
+        Assert.assertEquals(expected, CosAngle.value(v1, v3), 1e-15);
+        Assert.assertEquals(expected, CosAngle.value(v3, v2), 1e-15);
+
+        final double[] v4 = { -5, 0 };
+        expected = -1;
+        Assert.assertEquals(expected, CosAngle.value(v1, v4), 0);
+
+        final double[] v5 = { -100, 100 };
+        expected = 0;
+        Assert.assertEquals(expected, CosAngle.value(v3, v5), 0);
+    }
+
+    @Test
+    public void testCosAngle3D() {
+        double expected;
+
+        final double[] v1 = { 1, 1, 0 };
+        expected = 1;
+        Assert.assertEquals(expected, CosAngle.value(v1, v1), 1e-15);
+
+        final double[] v2 = { 1, 1, 1 };
+        expected = Math.sqrt(2) / Math.sqrt(3);
+        Assert.assertEquals(expected, CosAngle.value(v1, v2), 1e-15);
+    }
+
+    @Test
+    public void testCosAngleExtreme() {
+        double expected;
+
+        final double tiny = 1e-200;
+        final double[] v1 = { tiny, tiny };
+        final double big = 1e200;
+        final double[] v2 = { -big, -big };
+        expected = -1;
+        Assert.assertEquals(expected, CosAngle.value(v1, v2), 1e-15);
+
+        final double[] v3 = { big, -big };
+        expected = 0;
+        Assert.assertEquals(expected, CosAngle.value(v1, v3), 1e-15);
+    }
+}