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 22:25:06 UTC
[1/2] commons-numbers git commit: Created module
"commons-numbers-arrays" to eliminate dependency cycle.
Repository: commons-numbers
Updated Branches:
refs/heads/master d195a7bbb -> e4c21beac
http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/e4c21bea/commons-numbers-core/src/main/java/org/apache/commons/numbers/core/LinearCombination.java
----------------------------------------------------------------------
diff --git a/commons-numbers-core/src/main/java/org/apache/commons/numbers/core/LinearCombination.java b/commons-numbers-core/src/main/java/org/apache/commons/numbers/core/LinearCombination.java
deleted file mode 100644
index 147ff5b..0000000
--- a/commons-numbers-core/src/main/java/org/apache/commons/numbers/core/LinearCombination.java
+++ /dev/null
@@ -1,341 +0,0 @@
-/*
- * 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 linear combinations accurately.
- * This method computes the sum of the products
- * <code>a<sub>i</sub> b<sub>i</sub></code> to high accuracy.
- * It does so by using specific multiplication and addition algorithms to
- * preserve accuracy and reduce cancellation effects.
- *
- * It is based on the 2005 paper
- * <a href="http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.2.1547">
- * Accurate Sum and Dot Product</a> by Takeshi Ogita, Siegfried M. Rump,
- * and Shin'ichi Oishi published in <em>SIAM J. Sci. Comput</em>.
- */
-public class LinearCombination {
- /*
- * Caveat:
- *
- * The code below is split in many additions/subtractions that may
- * appear redundant. However, they should NOT be simplified, as they
- * do use IEEE754 floating point arithmetic rounding properties.
- * The variables naming conventions are that xyzHigh contains the most significant
- * bits of xyz and xyzLow contains its least significant bits. So theoretically
- * xyz is the sum xyzHigh + xyzLow, but in many cases below, this sum cannot
- * be represented in only one double precision number so we preserve two numbers
- * to hold it as long as we can, combining the high and low order bits together
- * only at the end, after cancellation may have occurred on high order bits
- */
-
- /**
- * @param a Factors.
- * @param b Factors.
- * @return \( \Sum_i a_i b_i \).
- * @throws IllegalArgumentException if the sizes of the arrays are different.
- */
- public static double value(double[] a,
- double[] b) {
- if (a.length != b.length) {
- throw new IllegalArgumentException("Dimension mismatch: " + a.length + " != " + b.length);
- }
-
- final int len = a.length;
-
- if (len == 1) {
- // Revert to scalar multiplication.
- return a[0] * b[0];
- }
-
- final double[] prodHigh = new double[len];
- double prodLowSum = 0;
-
- for (int i = 0; i < len; i++) {
- final double ai = a[i];
- final double aHigh = highPart(ai);
- final double aLow = ai - aHigh;
-
- final double bi = b[i];
- final double bHigh = highPart(bi);
- final double bLow = bi - bHigh;
- prodHigh[i] = ai * bi;
- final double prodLow = prodLow(aLow, bLow, prodHigh[i], aHigh, bHigh);
- prodLowSum += prodLow;
- }
-
-
- final double prodHighCur = prodHigh[0];
- double prodHighNext = prodHigh[1];
- double sHighPrev = prodHighCur + prodHighNext;
- double sPrime = sHighPrev - prodHighNext;
- double sLowSum = (prodHighNext - (sHighPrev - sPrime)) + (prodHighCur - sPrime);
-
- final int lenMinusOne = len - 1;
- for (int i = 1; i < lenMinusOne; i++) {
- prodHighNext = prodHigh[i + 1];
- final double sHighCur = sHighPrev + prodHighNext;
- sPrime = sHighCur - prodHighNext;
- sLowSum += (prodHighNext - (sHighCur - sPrime)) + (sHighPrev - sPrime);
- sHighPrev = sHighCur;
- }
-
- double result = sHighPrev + (prodLowSum + sLowSum);
-
- if (Double.isNaN(result)) {
- // either we have split infinite numbers or some coefficients were NaNs,
- // just rely on the naive implementation and let IEEE754 handle this
- result = 0;
- for (int i = 0; i < len; ++i) {
- result += a[i] * b[i];
- }
- }
-
- return result;
- }
-
- /**
- * @param a1 First factor of the first term.
- * @param b1 Second factor of the first term.
- * @param a2 First factor of the second term.
- * @param b2 Second factor of the second term.
- * @return \( a_1 b_1 + a_2 b_2 \)
- *
- * @see #value(double, double, double, double, double, double)
- * @see #value(double, double, double, double, double, double, double, double)
- * @see #value(double[], double[])
- */
- public static double value(double a1, double b1,
- double a2, double b2) {
- // split a1 and b1 as one 26 bits number and one 27 bits number
- final double a1High = highPart(a1);
- final double a1Low = a1 - a1High;
- final double b1High = highPart(b1);
- final double b1Low = b1 - b1High;
-
- // accurate multiplication a1 * b1
- final double prod1High = a1 * b1;
- final double prod1Low = prodLow(a1Low, b1Low, prod1High, a1High, b1High);
-
- // split a2 and b2 as one 26 bits number and one 27 bits number
- final double a2High = highPart(a2);
- final double a2Low = a2 - a2High;
- final double b2High = highPart(b2);
- final double b2Low = b2 - b2High;
-
- // accurate multiplication a2 * b2
- final double prod2High = a2 * b2;
- final double prod2Low = prodLow(a2Low, b2Low, prod2High, a2High, b2High);
-
- // accurate addition a1 * b1 + a2 * b2
- final double s12High = prod1High + prod2High;
- final double s12Prime = s12High - prod2High;
- final double s12Low = (prod2High - (s12High - s12Prime)) + (prod1High - s12Prime);
-
- // final rounding, s12 may have suffered many cancellations, we try
- // to recover some bits from the extra words we have saved up to now
- double result = s12High + (prod1Low + prod2Low + s12Low);
-
- if (Double.isNaN(result)) {
- // either we have split infinite numbers or some coefficients were NaNs,
- // just rely on the naive implementation and let IEEE754 handle this
- result = a1 * b1 + a2 * b2;
- }
-
- return result;
- }
-
- /**
- * @param a1 First factor of the first term.
- * @param b1 Second factor of the first term.
- * @param a2 First factor of the second term.
- * @param b2 Second factor of the second term.
- * @param a3 First factor of the third term.
- * @param b3 Second factor of the third term.
- * @return \( a_1 b_1 + a_2 b_2 + a_3 b_3 \)
- *
- * @see #value(double, double, double, double)
- * @see #value(double, double, double, double, double, double, double, double)
- * @see #value(double[], double[])
- */
- public static double value(double a1, double b1,
- double a2, double b2,
- double a3, double b3) {
- // split a1 and b1 as one 26 bits number and one 27 bits number
- final double a1High = highPart(a1);
- final double a1Low = a1 - a1High;
- final double b1High = highPart(b1);
- final double b1Low = b1 - b1High;
-
- // accurate multiplication a1 * b1
- final double prod1High = a1 * b1;
- final double prod1Low = prodLow(a1Low, b1Low, prod1High, a1High, b1High);
-
- // split a2 and b2 as one 26 bits number and one 27 bits number
- final double a2High = highPart(a2);
- final double a2Low = a2 - a2High;
- final double b2High = highPart(b2);
- final double b2Low = b2 - b2High;
-
- // accurate multiplication a2 * b2
- final double prod2High = a2 * b2;
- final double prod2Low = prodLow(a2Low, b2Low, prod2High, a2High, b2High);
-
- // split a3 and b3 as one 26 bits number and one 27 bits number
- final double a3High = highPart(a3);
- final double a3Low = a3 - a3High;
- final double b3High = highPart(b3);
- final double b3Low = b3 - b3High;
-
- // accurate multiplication a3 * b3
- final double prod3High = a3 * b3;
- final double prod3Low = prodLow(a3Low, b3Low, prod3High, a3High, b3High);
-
- // accurate addition a1 * b1 + a2 * b2
- final double s12High = prod1High + prod2High;
- final double s12Prime = s12High - prod2High;
- final double s12Low = (prod2High - (s12High - s12Prime)) + (prod1High - s12Prime);
-
- // accurate addition a1 * b1 + a2 * b2 + a3 * b3
- final double s123High = s12High + prod3High;
- final double s123Prime = s123High - prod3High;
- final double s123Low = (prod3High - (s123High - s123Prime)) + (s12High - s123Prime);
-
- // final rounding, s123 may have suffered many cancellations, we try
- // to recover some bits from the extra words we have saved up to now
- double result = s123High + (prod1Low + prod2Low + prod3Low + s12Low + s123Low);
-
- if (Double.isNaN(result)) {
- // either we have split infinite numbers or some coefficients were NaNs,
- // just rely on the naive implementation and let IEEE754 handle this
- result = a1 * b1 + a2 * b2 + a3 * b3;
- }
-
- return result;
- }
-
- /**
- * @param a1 First factor of the first term.
- * @param b1 Second factor of the first term.
- * @param a2 First factor of the second term.
- * @param b2 Second factor of the second term.
- * @param a3 First factor of the third term.
- * @param b3 Second factor of the third term.
- * @param a4 First factor of the fourth term.
- * @param b4 Second factor of the fourth term.
- * @return \( a_1 b_1 + a_2 b_2 + a_3 b_3 + a_4 b_4 \)
- *
- * @see #value(double, double, double, double)
- * @see #value(double, double, double, double, double, double)
- * @see #value(double[], double[])
- */
- public static double value(double a1, double b1,
- double a2, double b2,
- double a3, double b3,
- double a4, double b4) {
- // split a1 and b1 as one 26 bits number and one 27 bits number
- final double a1High = highPart(a1);
- final double a1Low = a1 - a1High;
- final double b1High = highPart(b1);
- final double b1Low = b1 - b1High;
-
- // accurate multiplication a1 * b1
- final double prod1High = a1 * b1;
- final double prod1Low = prodLow(a1Low, b1Low, prod1High, a1High, b1High);
-
- // split a2 and b2 as one 26 bits number and one 27 bits number
- final double a2High = highPart(a2);
- final double a2Low = a2 - a2High;
- final double b2High = highPart(b2);
- final double b2Low = b2 - b2High;
-
- // accurate multiplication a2 * b2
- final double prod2High = a2 * b2;
- final double prod2Low = prodLow(a2Low, b2Low, prod2High, a2High, b2High);
-
- // split a3 and b3 as one 26 bits number and one 27 bits number
- final double a3High = highPart(a3);
- final double a3Low = a3 - a3High;
- final double b3High = highPart(b3);
- final double b3Low = b3 - b3High;
-
- // accurate multiplication a3 * b3
- final double prod3High = a3 * b3;
- final double prod3Low = prodLow(a3Low, b3Low, prod3High, a3High, b3High);
-
- // split a4 and b4 as one 26 bits number and one 27 bits number
- final double a4High = highPart(a4);
- final double a4Low = a4 - a4High;
- final double b4High = highPart(b4);
- final double b4Low = b4 - b4High;
-
- // accurate multiplication a4 * b4
- final double prod4High = a4 * b4;
- final double prod4Low = prodLow(a4Low, b4Low, prod4High, a4High, b4High);
-
- // accurate addition a1 * b1 + a2 * b2
- final double s12High = prod1High + prod2High;
- final double s12Prime = s12High - prod2High;
- final double s12Low = (prod2High - (s12High - s12Prime)) + (prod1High - s12Prime);
-
- // accurate addition a1 * b1 + a2 * b2 + a3 * b3
- final double s123High = s12High + prod3High;
- final double s123Prime = s123High - prod3High;
- final double s123Low = (prod3High - (s123High - s123Prime)) + (s12High - s123Prime);
-
- // accurate addition a1 * b1 + a2 * b2 + a3 * b3 + a4 * b4
- final double s1234High = s123High + prod4High;
- final double s1234Prime = s1234High - prod4High;
- final double s1234Low = (prod4High - (s1234High - s1234Prime)) + (s123High - s1234Prime);
-
- // final rounding, s1234 may have suffered many cancellations, we try
- // to recover some bits from the extra words we have saved up to now
- double result = s1234High + (prod1Low + prod2Low + prod3Low + prod4Low + s12Low + s123Low + s1234Low);
-
- if (Double.isNaN(result)) {
- // either we have split infinite numbers or some coefficients were NaNs,
- // just rely on the naive implementation and let IEEE754 handle this
- result = a1 * b1 + a2 * b2 + a3 * b3 + a4 * b4;
- }
-
- return result;
- }
-
- /**
- * @param value Value.
- * @return the high part of the value.
- */
- private static double highPart(double value) {
- return Double.longBitsToDouble(Double.doubleToRawLongBits(value) & ((-1L) << 27));
- }
-
- /**
- * @param aLow Low part of first factor.
- * @param bLow Low part of second factor.
- * @param prodHigh Product of the factors.
- * @param aHigh High part of first factor.
- * @param bHigh High part of second factor.
- * @return <code>aLow * bLow - (((prodHigh - aHigh * bHigh) - aLow * bHigh) - aHigh * bLow)</code>
- */
- private static double prodLow(double aLow,
- double bLow,
- double prodHigh,
- double aHigh,
- double bHigh) {
- return aLow * bLow - (((prodHigh - aHigh * bHigh) - aLow * bHigh) - aHigh * bLow);
- }
-}
http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/e4c21bea/commons-numbers-core/src/main/java/org/apache/commons/numbers/core/SafeNorm.java
----------------------------------------------------------------------
diff --git a/commons-numbers-core/src/main/java/org/apache/commons/numbers/core/SafeNorm.java b/commons-numbers-core/src/main/java/org/apache/commons/numbers/core/SafeNorm.java
deleted file mode 100644
index 5d077bc..0000000
--- a/commons-numbers-core/src/main/java/org/apache/commons/numbers/core/SafeNorm.java
+++ /dev/null
@@ -1,86 +0,0 @@
-/*
- * 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 Cartesian norm (2-norm), handling both overflow and underflow.
- * Translation of the <a href="http://www.netlib.org/minpack">minpack</a>
- * "enorm" subroutine.
- */
-public class SafeNorm {
- /** Constant. */
- private static final double R_DWARF = 3.834e-20;
- /** Constant. */
- private static final double R_GIANT = 1.304e+19;
-
- /**
- * @param v Cartesian coordinates.
- * @return the 2-norm of the vector.
- */
- public static double value(double[] v) {
- double s1 = 0;
- double s2 = 0;
- double s3 = 0;
- double x1max = 0;
- double x3max = 0;
- double floatn = v.length;
- double agiant = R_GIANT / floatn;
- for (int i = 0; i < v.length; i++) {
- double xabs = Math.abs(v[i]);
- if (xabs < R_DWARF || xabs > agiant) {
- if (xabs > R_DWARF) {
- if (xabs > x1max) {
- double r = x1max / xabs;
- s1 = 1 + s1 * r * r;
- x1max = xabs;
- } else {
- double r = xabs / x1max;
- s1 += r * r;
- }
- } else {
- if (xabs > x3max) {
- double r = x3max / xabs;
- s3 = 1 + s3 * r * r;
- x3max = xabs;
- } else {
- if (xabs != 0) {
- double r = xabs / x3max;
- s3 += r * r;
- }
- }
- }
- } else {
- s2 += xabs * xabs;
- }
- }
- double norm;
- if (s1 != 0) {
- norm = x1max * Math.sqrt(s1 + (s2 / x1max) / x1max);
- } else {
- if (s2 == 0) {
- norm = x3max * Math.sqrt(s3);
- } else {
- if (s2 >= x3max) {
- norm = Math.sqrt(s2 * (1 + (x3max / s2) * (x3max * s3)));
- } else {
- norm = Math.sqrt(x3max * ((s2 / x3max) + (x3max * s3)));
- }
- }
- }
- return norm;
- }
-}
http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/e4c21bea/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
deleted file mode 100644
index 63eeca0..0000000
--- a/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/CosAngleTest.java
+++ /dev/null
@@ -1,77 +0,0 @@
-/*
- * 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);
- }
-}
http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/e4c21bea/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/LinearCombinationTest.java
----------------------------------------------------------------------
diff --git a/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/LinearCombinationTest.java b/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/LinearCombinationTest.java
deleted file mode 100644
index d8a00c9..0000000
--- a/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/LinearCombinationTest.java
+++ /dev/null
@@ -1,297 +0,0 @@
-/*
- * 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;
-
-import org.apache.commons.rng.UniformRandomProvider;
-import org.apache.commons.rng.simple.RandomSource;
-import org.apache.commons.numbers.fraction.BigFraction;
-
-/**
- * Test cases for the {@link LinearCombination} class.
- */
-public class LinearCombinationTest {
- // MATH-1005
- @Test
- public void testSingleElementArray() {
- final double[] a = { 1.23456789 };
- final double[] b = { 98765432.1 };
-
- Assert.assertEquals(a[0] * b[0], LinearCombination.value(a, b), 0d);
- }
-
- @Test
- public void testTwoSums() {
- final BigFraction[] aF = new BigFraction[] {
- new BigFraction(-1321008684645961L, 268435456L),
- new BigFraction(-5774608829631843L, 268435456L),
- new BigFraction(-7645843051051357L, 8589934592L)
- };
- final BigFraction[] bF = new BigFraction[] {
- new BigFraction(-5712344449280879L, 2097152L),
- new BigFraction(-4550117129121957L, 2097152L),
- new BigFraction(8846951984510141L, 131072L)
- };
-
- final int len = aF.length;
- final double[] a = new double[len];
- final double[] b = new double[len];
- for (int i = 0; i < len; i++) {
- a[i] = aF[i].getNumerator().doubleValue() / aF[i].getDenominator().doubleValue();
- b[i] = bF[i].getNumerator().doubleValue() / bF[i].getDenominator().doubleValue();
- }
-
- // Ensure "array" and "inline" implementations give the same result.
- final double abSumInline = LinearCombination.value(a[0], b[0],
- a[1], b[1],
- a[2], b[2]);
- final double abSumArray = LinearCombination.value(a, b);
- Assert.assertEquals(abSumInline, abSumArray, 0);
-
- // Compare with arbitrary precision computation.
- BigFraction result = BigFraction.ZERO;
- for (int i = 0; i < a.length; i++) {
- result = result.add(aF[i].multiply(bF[i]));
- }
- final double expected = result.doubleValue();
- Assert.assertEquals(expected, abSumInline, 1e-15);
-
- final double naive = a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
- Assert.assertTrue(Math.abs(naive - abSumInline) > 1.5);
- }
-
- @Test
- public void testArrayVsInline() {
- final UniformRandomProvider rng = RandomSource.create(RandomSource.XOR_SHIFT_1024_S);
-
- double sInline;
- double sArray;
- final double scale = 1e17;
- for (int i = 0; i < 10000; ++i) {
- final double u1 = scale * rng.nextDouble();
- final double u2 = scale * rng.nextDouble();
- final double u3 = scale * rng.nextDouble();
- final double u4 = scale * rng.nextDouble();
- final double v1 = scale * rng.nextDouble();
- final double v2 = scale * rng.nextDouble();
- final double v3 = scale * rng.nextDouble();
- final double v4 = scale * rng.nextDouble();
-
- // One sum.
- sInline = LinearCombination.value(u1, v1, u2, v2);
- sArray = LinearCombination.value(new double[] { u1, u2 },
- new double[] { v1, v2 });
- Assert.assertEquals(sInline, sArray, 0);
-
- // Two sums.
- sInline = LinearCombination.value(u1, v1, u2, v2, u3, v3);
- sArray = LinearCombination.value(new double[] { u1, u2, u3 },
- new double[] { v1, v2, v3 });
- Assert.assertEquals(sInline, sArray, 0);
-
- // Three sums.
- sInline = LinearCombination.value(u1, v1, u2, v2, u3, v3, u4, v4);
- sArray = LinearCombination.value(new double[] { u1, u2, u3, u4 },
- new double[] { v1, v2, v3, v4 });
- Assert.assertEquals(sInline, sArray, 0);
- }
- }
-
- @Test
- public void testHuge() {
- int scale = 971;
- final double[] a = new double[] {
- -1321008684645961.0 / 268435456.0,
- -5774608829631843.0 / 268435456.0,
- -7645843051051357.0 / 8589934592.0
- };
- final double[] b = new double[] {
- -5712344449280879.0 / 2097152.0,
- -4550117129121957.0 / 2097152.0,
- 8846951984510141.0 / 131072.0
- };
-
- final int len = a.length;
- final double[] scaledA = new double[len];
- final double[] scaledB = new double[len];
- for (int i = 0; i < len; ++i) {
- scaledA[i] = Math.scalb(a[i], -scale);
- scaledB[i] = Math.scalb(b[i], scale);
- }
- final double abSumInline = LinearCombination.value(scaledA[0], scaledB[0],
- scaledA[1], scaledB[1],
- scaledA[2], scaledB[2]);
- final double abSumArray = LinearCombination.value(scaledA, scaledB);
-
- Assert.assertEquals(abSumInline, abSumArray, 0);
- Assert.assertEquals(-1.8551294182586248737720779899, abSumInline, 1e-15);
-
- final double naive = scaledA[0] * scaledB[0] + scaledA[1] * scaledB[1] + scaledA[2] * scaledB[2];
- Assert.assertTrue(Math.abs(naive - abSumInline) > 1.5);
- }
-
- @Test
- public void testInfinite() {
- final double[][] a = new double[][] {
- { 1, 2, 3, 4 },
- { 1, Double.POSITIVE_INFINITY, 3, 4 },
- { 1, 2, Double.POSITIVE_INFINITY, 4 },
- { 1, Double.POSITIVE_INFINITY, 3, Double.NEGATIVE_INFINITY },
- { 1, 2, 3, 4 },
- { 1, 2, 3, 4 },
- { 1, 2, 3, 4 },
- { 1, 2, 3, 4 }
- };
- final double[][] b = new double[][] {
- { 1, -2, 3, 4 },
- { 1, -2, 3, 4 },
- { 1, -2, 3, 4 },
- { 1, -2, 3, 4 },
- { 1, Double.POSITIVE_INFINITY, 3, 4 },
- { 1, -2, Double.POSITIVE_INFINITY, 4 },
- { 1, Double.POSITIVE_INFINITY, 3, Double.NEGATIVE_INFINITY },
- { Double.NaN, -2, 3, 4 }
- };
-
- Assert.assertEquals(-3,
- LinearCombination.value(a[0][0], b[0][0],
- a[0][1], b[0][1]),
- 1e-10);
- Assert.assertEquals(6,
- LinearCombination.value(a[0][0], b[0][0],
- a[0][1], b[0][1],
- a[0][2], b[0][2]),
- 1e-10);
- Assert.assertEquals(22,
- LinearCombination.value(a[0][0], b[0][0],
- a[0][1], b[0][1],
- a[0][2], b[0][2],
- a[0][3], b[0][3]),
- 1e-10);
- Assert.assertEquals(22, LinearCombination.value(a[0], b[0]), 1e-10);
-
- Assert.assertEquals(Double.NEGATIVE_INFINITY,
- LinearCombination.value(a[1][0], b[1][0],
- a[1][1], b[1][1]),
- 1e-10);
- Assert.assertEquals(Double.NEGATIVE_INFINITY,
- LinearCombination.value(a[1][0], b[1][0],
- a[1][1], b[1][1],
- a[1][2], b[1][2]),
- 1e-10);
- Assert.assertEquals(Double.NEGATIVE_INFINITY,
- LinearCombination.value(a[1][0], b[1][0],
- a[1][1], b[1][1],
- a[1][2], b[1][2],
- a[1][3], b[1][3]),
- 1e-10);
- Assert.assertEquals(Double.NEGATIVE_INFINITY, LinearCombination.value(a[1], b[1]), 1e-10);
-
- Assert.assertEquals(-3,
- LinearCombination.value(a[2][0], b[2][0],
- a[2][1], b[2][1]),
- 1e-10);
- Assert.assertEquals(Double.POSITIVE_INFINITY,
- LinearCombination.value(a[2][0], b[2][0],
- a[2][1], b[2][1],
- a[2][2], b[2][2]),
- 1e-10);
- Assert.assertEquals(Double.POSITIVE_INFINITY,
- LinearCombination.value(a[2][0], b[2][0],
- a[2][1], b[2][1],
- a[2][2], b[2][2],
- a[2][3], b[2][3]),
- 1e-10);
- Assert.assertEquals(Double.POSITIVE_INFINITY, LinearCombination.value(a[2], b[2]), 1e-10);
-
- Assert.assertEquals(Double.NEGATIVE_INFINITY,
- LinearCombination.value(a[3][0], b[3][0],
- a[3][1], b[3][1]),
- 1e-10);
- Assert.assertEquals(Double.NEGATIVE_INFINITY,
- LinearCombination.value(a[3][0], b[3][0],
- a[3][1], b[3][1],
- a[3][2], b[3][2]),
- 1e-10);
- Assert.assertEquals(Double.NEGATIVE_INFINITY,
- LinearCombination.value(a[3][0], b[3][0],
- a[3][1], b[3][1],
- a[3][2], b[3][2],
- a[3][3], b[3][3]),
- 1e-10);
- Assert.assertEquals(Double.NEGATIVE_INFINITY, LinearCombination.value(a[3], b[3]), 1e-10);
-
- Assert.assertEquals(Double.POSITIVE_INFINITY,
- LinearCombination.value(a[4][0], b[4][0],
- a[4][1], b[4][1]),
- 1e-10);
- Assert.assertEquals(Double.POSITIVE_INFINITY,
- LinearCombination.value(a[4][0], b[4][0],
- a[4][1], b[4][1],
- a[4][2], b[4][2]),
- 1e-10);
- Assert.assertEquals(Double.POSITIVE_INFINITY,
- LinearCombination.value(a[4][0], b[4][0],
- a[4][1], b[4][1],
- a[4][2], b[4][2],
- a[4][3], b[4][3]),
- 1e-10);
- Assert.assertEquals(Double.POSITIVE_INFINITY, LinearCombination.value(a[4], b[4]), 1e-10);
-
- Assert.assertEquals(-3,
- LinearCombination.value(a[5][0], b[5][0],
- a[5][1], b[5][1]),
- 1e-10);
- Assert.assertEquals(Double.POSITIVE_INFINITY,
- LinearCombination.value(a[5][0], b[5][0],
- a[5][1], b[5][1],
- a[5][2], b[5][2]),
- 1e-10);
- Assert.assertEquals(Double.POSITIVE_INFINITY,
- LinearCombination.value(a[5][0], b[5][0],
- a[5][1], b[5][1],
- a[5][2], b[5][2],
- a[5][3], b[5][3]),
- 1e-10);
- Assert.assertEquals(Double.POSITIVE_INFINITY, LinearCombination.value(a[5], b[5]), 1e-10);
-
- Assert.assertEquals(Double.POSITIVE_INFINITY,
- LinearCombination.value(a[6][0], b[6][0],
- a[6][1], b[6][1]),
- 1e-10);
- Assert.assertEquals(Double.POSITIVE_INFINITY,
- LinearCombination.value(a[6][0], b[6][0],
- a[6][1], b[6][1],
- a[6][2], b[6][2]),
- 1e-10);
- Assert.assertTrue(Double.isNaN(LinearCombination.value(a[6][0], b[6][0],
- a[6][1], b[6][1],
- a[6][2], b[6][2],
- a[6][3], b[6][3])));
- Assert.assertTrue(Double.isNaN(LinearCombination.value(a[6], b[6])));
-
- Assert.assertTrue(Double.isNaN(LinearCombination.value(a[7][0], b[7][0],
- a[7][1], b[7][1])));
- Assert.assertTrue(Double.isNaN(LinearCombination.value(a[7][0], b[7][0],
- a[7][1], b[7][1],
- a[7][2], b[7][2])));
- Assert.assertTrue(Double.isNaN(LinearCombination.value(a[7][0], b[7][0],
- a[7][1], b[7][1],
- a[7][2], b[7][2],
- a[7][3], b[7][3])));
- Assert.assertTrue(Double.isNaN(LinearCombination.value(a[7], b[7])));
- }
-}
http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/e4c21bea/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/SafeNormTest.java
----------------------------------------------------------------------
diff --git a/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/SafeNormTest.java b/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/SafeNormTest.java
deleted file mode 100644
index 0c4e9bc..0000000
--- a/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/SafeNormTest.java
+++ /dev/null
@@ -1,62 +0,0 @@
-/*
- * 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 SafeNorm} class.
- */
-public class SafeNormTest {
-
- @Test
- public void testTiny() {
- final double s = 1e-320;
- final double[] v = new double[] { s, s };
- Assert.assertEquals(Math.sqrt(2) * s, SafeNorm.value(v), 0d);
- }
-
- @Test
- public void testBig() {
- final double s = 1e300;
- final double[] v = new double[] { s, s };
- Assert.assertEquals(Math.sqrt(2) * s, SafeNorm.value(v), 0d);
- }
-
- @Test
- public void testOne3D() {
- final double s = 1;
- final double[] v = new double[] { s, s, s };
- Assert.assertEquals(Math.sqrt(3), SafeNorm.value(v), 0d);
- }
-
- @Test
- public void testUnit3D() {
- Assert.assertEquals(1, SafeNorm.value(new double[] { 1, 0, 0 }), 0d);
- Assert.assertEquals(1, SafeNorm.value(new double[] { 0, 1, 0 }), 0d);
- Assert.assertEquals(1, SafeNorm.value(new double[] { 0, 0, 1 }), 0d);
- }
-
- @Test
- public void testSimple() {
- final double[] v = new double[] { -0.9, 8.7, -6.5, -4.3, -2.1, 0, 1.2, 3.4, -5.6, 7.8, 9.0 };
- double n = 0;
- for (int i = 0; i < v.length; i++) {
- n += v[i] * v[i];
- }
- final double expected = Math.sqrt(n);
- Assert.assertEquals(expected, SafeNorm.value(v), 0d);
- }
-}
http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/e4c21bea/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index 248a42f..7f18506 100644
--- a/pom.xml
+++ b/pom.xml
@@ -600,6 +600,7 @@
<module>commons-numbers-angle</module>
<module>commons-numbers-gamma</module>
<module>commons-numbers-combinatorics</module>
+ <module>commons-numbers-arrays</module>
</modules>
</project>
[2/2] commons-numbers git commit: Created module
"commons-numbers-arrays" to eliminate dependency cycle.
Posted by er...@apache.org.
Created module "commons-numbers-arrays" to eliminate dependency cycle.
Project: http://git-wip-us.apache.org/repos/asf/commons-numbers/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-numbers/commit/e4c21bea
Tree: http://git-wip-us.apache.org/repos/asf/commons-numbers/tree/e4c21bea
Diff: http://git-wip-us.apache.org/repos/asf/commons-numbers/diff/e4c21bea
Branch: refs/heads/master
Commit: e4c21beac3f404e81e2fa369cbe225f06dc3e347
Parents: d195a7b
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
Authored: Thu May 25 00:23:12 2017 +0200
Committer: Gilles Sadowski <gi...@harfang.homelinux.org>
Committed: Thu May 25 00:23:12 2017 +0200
----------------------------------------------------------------------
commons-numbers-arrays/LICENSE.txt | 266 +++++++++++++++
commons-numbers-arrays/NOTICE.txt | 6 +
commons-numbers-arrays/README.md | 98 ++++++
commons-numbers-arrays/pom.xml | 61 ++++
.../apache/commons/numbers/arrays/CosAngle.java | 35 ++
.../numbers/arrays/LinearCombination.java | 341 +++++++++++++++++++
.../apache/commons/numbers/arrays/SafeNorm.java | 86 +++++
.../commons/numbers/arrays/package-info.java | 20 ++
commons-numbers-arrays/src/site/site.xml | 35 ++
commons-numbers-arrays/src/site/xdoc/index.xml | 40 +++
.../commons/numbers/arrays/CosAngleTest.java | 77 +++++
.../numbers/arrays/LinearCombinationTest.java | 297 ++++++++++++++++
.../commons/numbers/arrays/SafeNormTest.java | 62 ++++
commons-numbers-core/LICENSE.txt | 65 ----
commons-numbers-core/pom.xml | 16 -
.../apache/commons/numbers/core/CosAngle.java | 35 --
.../commons/numbers/core/LinearCombination.java | 341 -------------------
.../apache/commons/numbers/core/SafeNorm.java | 86 -----
.../commons/numbers/core/CosAngleTest.java | 77 -----
.../numbers/core/LinearCombinationTest.java | 297 ----------------
.../commons/numbers/core/SafeNormTest.java | 62 ----
pom.xml | 1 +
22 files changed, 1425 insertions(+), 979 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/e4c21bea/commons-numbers-arrays/LICENSE.txt
----------------------------------------------------------------------
diff --git a/commons-numbers-arrays/LICENSE.txt b/commons-numbers-arrays/LICENSE.txt
new file mode 100644
index 0000000..67c36eb
--- /dev/null
+++ b/commons-numbers-arrays/LICENSE.txt
@@ -0,0 +1,266 @@
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
+ APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "[]"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+ Copyright [yyyy] [name of copyright owner]
+
+ Licensed 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.
+
+===============================================================================
+
+Apache "Commons Numbers" derivative works:
+
+The "commons-numbers-core" library includes a number of subcomponents
+whose implementation is derived from original sources written in C or
+Fortran. License terms of the original sources are reproduced below.
+
+===============================================================================
+Code for "SafeNorm" comes from the MINPACK library.
+
+Original source copyright and license statement:
+
+Minpack Copyright Notice (1999) University of Chicago. All rights reserved
+
+Redistribution and use in source and binary forms, with or
+without modification, are permitted provided that the
+following conditions are met:
+
+1. Redistributions of source code must retain the above
+copyright notice, this list of conditions and the following
+disclaimer.
+
+2. Redistributions in binary form must reproduce the above
+copyright notice, this list of conditions and the following
+disclaimer in the documentation and/or other materials
+provided with the distribution.
+
+3. The end-user documentation included with the
+redistribution, if any, must include the following
+acknowledgment:
+
+ "This product includes software developed by the
+ University of Chicago, as Operator of Argonne National
+ Laboratory.
+
+Alternately, this acknowledgment may appear in the software
+itself, if and wherever such third-party acknowledgments
+normally appear.
+
+4. WARRANTY DISCLAIMER. THE SOFTWARE IS SUPPLIED "AS IS"
+WITHOUT WARRANTY OF ANY KIND. THE COPYRIGHT HOLDER, THE
+UNITED STATES, THE UNITED STATES DEPARTMENT OF ENERGY, AND
+THEIR EMPLOYEES: (1) DISCLAIM ANY WARRANTIES, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO ANY IMPLIED WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE
+OR NON-INFRINGEMENT, (2) DO NOT ASSUME ANY LEGAL LIABILITY
+OR RESPONSIBILITY FOR THE ACCURACY, COMPLETENESS, OR
+USEFULNESS OF THE SOFTWARE, (3) DO NOT REPRESENT THAT USE OF
+THE SOFTWARE WOULD NOT INFRINGE PRIVATELY OWNED RIGHTS, (4)
+DO NOT WARRANT THAT THE SOFTWARE WILL FUNCTION
+UNINTERRUPTED, THAT IT IS ERROR-FREE OR THAT ANY ERRORS WILL
+BE CORRECTED.
+
+5. LIMITATION OF LIABILITY. IN NO EVENT WILL THE COPYRIGHT
+HOLDER, THE UNITED STATES, THE UNITED STATES DEPARTMENT OF
+ENERGY, OR THEIR EMPLOYEES: BE LIABLE FOR ANY INDIRECT,
+INCIDENTAL, CONSEQUENTIAL, SPECIAL OR PUNITIVE DAMAGES OF
+ANY KIND OR NATURE, INCLUDING BUT NOT LIMITED TO LOSS OF
+PROFITS OR LOSS OF DATA, FOR ANY REASON WHATSOEVER, WHETHER
+SUCH LIABILITY IS ASSERTED ON THE BASIS OF CONTRACT, TORT
+(INCLUDING NEGLIGENCE OR STRICT LIABILITY), OR OTHERWISE,
+EVEN IF ANY OF SAID PARTIES HAS BEEN WARNED OF THE
+POSSIBILITY OF SUCH LOSS OR DAMAGES.
http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/e4c21bea/commons-numbers-arrays/NOTICE.txt
----------------------------------------------------------------------
diff --git a/commons-numbers-arrays/NOTICE.txt b/commons-numbers-arrays/NOTICE.txt
new file mode 100644
index 0000000..9091baa
--- /dev/null
+++ b/commons-numbers-arrays/NOTICE.txt
@@ -0,0 +1,6 @@
+Apache Commons Numbers
+Copyright 2001-2017 The Apache Software Foundation
+
+This product includes software developed at
+The Apache Software Foundation (http://www.apache.org/).
+
http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/e4c21bea/commons-numbers-arrays/README.md
----------------------------------------------------------------------
diff --git a/commons-numbers-arrays/README.md b/commons-numbers-arrays/README.md
new file mode 100644
index 0000000..7f6176a
--- /dev/null
+++ b/commons-numbers-arrays/README.md
@@ -0,0 +1,98 @@
+<!---
+ 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.
+-->
+<!---
+ +======================================================================+
+ |**** ****|
+ |**** THIS FILE IS GENERATED BY THE COMMONS BUILD PLUGIN ****|
+ |**** DO NOT EDIT DIRECTLY ****|
+ |**** ****|
+ +======================================================================+
+ | TEMPLATE FILE: readme-md-template.md |
+ | commons-build-plugin/trunk/src/main/resources/commons-xdoc-templates |
+ +======================================================================+
+ | |
+ | 1) Re-generate using: mvn commons:readme-md |
+ | |
+ | 2) Set the following properties in the component's pom: |
+ | - commons.componentid (required, alphabetic, lower case) |
+ | - commons.release.version (required) |
+ | |
+ | 3) Example Properties |
+ | |
+ | <properties> |
+ | <commons.componentid>math</commons.componentid> |
+ | <commons.release.version>1.2</commons.release.version> |
+ | </properties> |
+ | |
+ +======================================================================+
+--->
+Apache Commons Numbers Arrays
+===================
+
+Basic utilities.
+
+Documentation
+-------------
+
+More information can be found on the [homepage](https://commons.apache.org/proper/commons-numbers).
+The [JavaDoc](https://commons.apache.org/proper/commons-numbers/javadocs/api-release) can be browsed.
+Questions related to the usage of Apache Commons Numbers Arrays should be posted to the [user mailing list][ml].
+
+Where can I get the latest release?
+-----------------------------------
+You can download source and binaries from our [download page](https://commons.apache.org/proper/commons-numbers/download_numbers.cgi).
+
+Alternatively you can pull it from the central Maven repositories:
+
+```xml
+<dependency>
+ <groupId>org.apache.commons</groupId>
+ <artifactId>commons-numbers-arrays</artifactId>
+ <version>1.0</version>
+</dependency>
+```
+
+Contributing
+------------
+
+We accept PRs via github. The [developer mailing list][ml] is the main channel of communication for contributors.
+There are some guidelines which will make applying PRs easier for us:
++ No tabs! Please use spaces for indentation.
++ Respect the code style.
++ Create minimal diffs - disable on save actions like reformat source code or organize imports. If you feel the source code should be reformatted create a separate PR for this change.
++ Provide JUnit tests for your changes and make sure your changes don't break any existing tests by running ```mvn clean test```.
+
+If you plan to contribute on a regular basis, please consider filing a [contributor license agreement](https://www.apache.org/licenses/#clas).
+You can learn more about contributing via GitHub in our [contribution guidelines](CONTRIBUTING.md).
+
+License
+-------
+Code is under the [Apache Licence v2](https://www.apache.org/licenses/LICENSE-2.0.txt).
+
+Donations
+---------
+You like Apache Commons Numbers Arrays? Then [donate back to the ASF](https://www.apache.org/foundation/contributing.html) to support the development.
+
+Additional Resources
+--------------------
+
++ [Apache Commons Homepage](https://commons.apache.org/)
++ [Apache Bugtracker (JIRA)](https://issues.apache.org/jira/)
++ [Apache Commons Twitter Account](https://twitter.com/ApacheCommons)
++ #apachecommons IRC channel on freenode.org
+
+[ml]:https://commons.apache.org/mail-lists.html
http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/e4c21bea/commons-numbers-arrays/pom.xml
----------------------------------------------------------------------
diff --git a/commons-numbers-arrays/pom.xml b/commons-numbers-arrays/pom.xml
new file mode 100644
index 0000000..4baecd0
--- /dev/null
+++ b/commons-numbers-arrays/pom.xml
@@ -0,0 +1,61 @@
+<?xml version="1.0"?>
+<!--
+ 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.
+-->
+<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
+ xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.apache.commons</groupId>
+ <artifactId>commons-numbers-parent</artifactId>
+ <version>1.0-SNAPSHOT</version>
+ </parent>
+
+ <groupId>org.apache.commons</groupId>
+ <artifactId>commons-numbers-arrays</artifactId>
+ <version>1.0-SNAPSHOT</version>
+ <name>Apache Commons Numbers Arrays</name>
+
+ <description>Array utilities.</description>
+
+ <properties>
+ <!-- This value must reflect the current name of the base package. -->
+ <commons.osgi.symbolicName>org.apache.commons.numbers.arrays</commons.osgi.symbolicName>
+ <!-- OSGi -->
+ <commons.osgi.export>org.apache.commons.numbers.arrays</commons.osgi.export>
+ <!-- Workaround to avoid duplicating config files. -->
+ <numbers.parent.dir>${basedir}/..</numbers.parent.dir>
+ </properties>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.commons</groupId>
+ <artifactId>commons-rng-simple</artifactId>
+ <version>1.0</version>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.commons</groupId>
+ <artifactId>commons-numbers-fraction</artifactId>
+ <version>1.0-SNAPSHOT</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+
+</project>
http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/e4c21bea/commons-numbers-arrays/src/main/java/org/apache/commons/numbers/arrays/CosAngle.java
----------------------------------------------------------------------
diff --git a/commons-numbers-arrays/src/main/java/org/apache/commons/numbers/arrays/CosAngle.java b/commons-numbers-arrays/src/main/java/org/apache/commons/numbers/arrays/CosAngle.java
new file mode 100644
index 0000000..58e5f51
--- /dev/null
+++ b/commons-numbers-arrays/src/main/java/org/apache/commons/numbers/arrays/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.arrays;
+
+/**
+ * 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/e4c21bea/commons-numbers-arrays/src/main/java/org/apache/commons/numbers/arrays/LinearCombination.java
----------------------------------------------------------------------
diff --git a/commons-numbers-arrays/src/main/java/org/apache/commons/numbers/arrays/LinearCombination.java b/commons-numbers-arrays/src/main/java/org/apache/commons/numbers/arrays/LinearCombination.java
new file mode 100644
index 0000000..51bf70f
--- /dev/null
+++ b/commons-numbers-arrays/src/main/java/org/apache/commons/numbers/arrays/LinearCombination.java
@@ -0,0 +1,341 @@
+/*
+ * 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.arrays;
+
+/**
+ * Computes linear combinations accurately.
+ * This method computes the sum of the products
+ * <code>a<sub>i</sub> b<sub>i</sub></code> to high accuracy.
+ * It does so by using specific multiplication and addition algorithms to
+ * preserve accuracy and reduce cancellation effects.
+ *
+ * It is based on the 2005 paper
+ * <a href="http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.2.1547">
+ * Accurate Sum and Dot Product</a> by Takeshi Ogita, Siegfried M. Rump,
+ * and Shin'ichi Oishi published in <em>SIAM J. Sci. Comput</em>.
+ */
+public class LinearCombination {
+ /*
+ * Caveat:
+ *
+ * The code below is split in many additions/subtractions that may
+ * appear redundant. However, they should NOT be simplified, as they
+ * do use IEEE754 floating point arithmetic rounding properties.
+ * The variables naming conventions are that xyzHigh contains the most significant
+ * bits of xyz and xyzLow contains its least significant bits. So theoretically
+ * xyz is the sum xyzHigh + xyzLow, but in many cases below, this sum cannot
+ * be represented in only one double precision number so we preserve two numbers
+ * to hold it as long as we can, combining the high and low order bits together
+ * only at the end, after cancellation may have occurred on high order bits
+ */
+
+ /**
+ * @param a Factors.
+ * @param b Factors.
+ * @return \( \Sum_i a_i b_i \).
+ * @throws IllegalArgumentException if the sizes of the arrays are different.
+ */
+ public static double value(double[] a,
+ double[] b) {
+ if (a.length != b.length) {
+ throw new IllegalArgumentException("Dimension mismatch: " + a.length + " != " + b.length);
+ }
+
+ final int len = a.length;
+
+ if (len == 1) {
+ // Revert to scalar multiplication.
+ return a[0] * b[0];
+ }
+
+ final double[] prodHigh = new double[len];
+ double prodLowSum = 0;
+
+ for (int i = 0; i < len; i++) {
+ final double ai = a[i];
+ final double aHigh = highPart(ai);
+ final double aLow = ai - aHigh;
+
+ final double bi = b[i];
+ final double bHigh = highPart(bi);
+ final double bLow = bi - bHigh;
+ prodHigh[i] = ai * bi;
+ final double prodLow = prodLow(aLow, bLow, prodHigh[i], aHigh, bHigh);
+ prodLowSum += prodLow;
+ }
+
+
+ final double prodHighCur = prodHigh[0];
+ double prodHighNext = prodHigh[1];
+ double sHighPrev = prodHighCur + prodHighNext;
+ double sPrime = sHighPrev - prodHighNext;
+ double sLowSum = (prodHighNext - (sHighPrev - sPrime)) + (prodHighCur - sPrime);
+
+ final int lenMinusOne = len - 1;
+ for (int i = 1; i < lenMinusOne; i++) {
+ prodHighNext = prodHigh[i + 1];
+ final double sHighCur = sHighPrev + prodHighNext;
+ sPrime = sHighCur - prodHighNext;
+ sLowSum += (prodHighNext - (sHighCur - sPrime)) + (sHighPrev - sPrime);
+ sHighPrev = sHighCur;
+ }
+
+ double result = sHighPrev + (prodLowSum + sLowSum);
+
+ if (Double.isNaN(result)) {
+ // either we have split infinite numbers or some coefficients were NaNs,
+ // just rely on the naive implementation and let IEEE754 handle this
+ result = 0;
+ for (int i = 0; i < len; ++i) {
+ result += a[i] * b[i];
+ }
+ }
+
+ return result;
+ }
+
+ /**
+ * @param a1 First factor of the first term.
+ * @param b1 Second factor of the first term.
+ * @param a2 First factor of the second term.
+ * @param b2 Second factor of the second term.
+ * @return \( a_1 b_1 + a_2 b_2 \)
+ *
+ * @see #value(double, double, double, double, double, double)
+ * @see #value(double, double, double, double, double, double, double, double)
+ * @see #value(double[], double[])
+ */
+ public static double value(double a1, double b1,
+ double a2, double b2) {
+ // split a1 and b1 as one 26 bits number and one 27 bits number
+ final double a1High = highPart(a1);
+ final double a1Low = a1 - a1High;
+ final double b1High = highPart(b1);
+ final double b1Low = b1 - b1High;
+
+ // accurate multiplication a1 * b1
+ final double prod1High = a1 * b1;
+ final double prod1Low = prodLow(a1Low, b1Low, prod1High, a1High, b1High);
+
+ // split a2 and b2 as one 26 bits number and one 27 bits number
+ final double a2High = highPart(a2);
+ final double a2Low = a2 - a2High;
+ final double b2High = highPart(b2);
+ final double b2Low = b2 - b2High;
+
+ // accurate multiplication a2 * b2
+ final double prod2High = a2 * b2;
+ final double prod2Low = prodLow(a2Low, b2Low, prod2High, a2High, b2High);
+
+ // accurate addition a1 * b1 + a2 * b2
+ final double s12High = prod1High + prod2High;
+ final double s12Prime = s12High - prod2High;
+ final double s12Low = (prod2High - (s12High - s12Prime)) + (prod1High - s12Prime);
+
+ // final rounding, s12 may have suffered many cancellations, we try
+ // to recover some bits from the extra words we have saved up to now
+ double result = s12High + (prod1Low + prod2Low + s12Low);
+
+ if (Double.isNaN(result)) {
+ // either we have split infinite numbers or some coefficients were NaNs,
+ // just rely on the naive implementation and let IEEE754 handle this
+ result = a1 * b1 + a2 * b2;
+ }
+
+ return result;
+ }
+
+ /**
+ * @param a1 First factor of the first term.
+ * @param b1 Second factor of the first term.
+ * @param a2 First factor of the second term.
+ * @param b2 Second factor of the second term.
+ * @param a3 First factor of the third term.
+ * @param b3 Second factor of the third term.
+ * @return \( a_1 b_1 + a_2 b_2 + a_3 b_3 \)
+ *
+ * @see #value(double, double, double, double)
+ * @see #value(double, double, double, double, double, double, double, double)
+ * @see #value(double[], double[])
+ */
+ public static double value(double a1, double b1,
+ double a2, double b2,
+ double a3, double b3) {
+ // split a1 and b1 as one 26 bits number and one 27 bits number
+ final double a1High = highPart(a1);
+ final double a1Low = a1 - a1High;
+ final double b1High = highPart(b1);
+ final double b1Low = b1 - b1High;
+
+ // accurate multiplication a1 * b1
+ final double prod1High = a1 * b1;
+ final double prod1Low = prodLow(a1Low, b1Low, prod1High, a1High, b1High);
+
+ // split a2 and b2 as one 26 bits number and one 27 bits number
+ final double a2High = highPart(a2);
+ final double a2Low = a2 - a2High;
+ final double b2High = highPart(b2);
+ final double b2Low = b2 - b2High;
+
+ // accurate multiplication a2 * b2
+ final double prod2High = a2 * b2;
+ final double prod2Low = prodLow(a2Low, b2Low, prod2High, a2High, b2High);
+
+ // split a3 and b3 as one 26 bits number and one 27 bits number
+ final double a3High = highPart(a3);
+ final double a3Low = a3 - a3High;
+ final double b3High = highPart(b3);
+ final double b3Low = b3 - b3High;
+
+ // accurate multiplication a3 * b3
+ final double prod3High = a3 * b3;
+ final double prod3Low = prodLow(a3Low, b3Low, prod3High, a3High, b3High);
+
+ // accurate addition a1 * b1 + a2 * b2
+ final double s12High = prod1High + prod2High;
+ final double s12Prime = s12High - prod2High;
+ final double s12Low = (prod2High - (s12High - s12Prime)) + (prod1High - s12Prime);
+
+ // accurate addition a1 * b1 + a2 * b2 + a3 * b3
+ final double s123High = s12High + prod3High;
+ final double s123Prime = s123High - prod3High;
+ final double s123Low = (prod3High - (s123High - s123Prime)) + (s12High - s123Prime);
+
+ // final rounding, s123 may have suffered many cancellations, we try
+ // to recover some bits from the extra words we have saved up to now
+ double result = s123High + (prod1Low + prod2Low + prod3Low + s12Low + s123Low);
+
+ if (Double.isNaN(result)) {
+ // either we have split infinite numbers or some coefficients were NaNs,
+ // just rely on the naive implementation and let IEEE754 handle this
+ result = a1 * b1 + a2 * b2 + a3 * b3;
+ }
+
+ return result;
+ }
+
+ /**
+ * @param a1 First factor of the first term.
+ * @param b1 Second factor of the first term.
+ * @param a2 First factor of the second term.
+ * @param b2 Second factor of the second term.
+ * @param a3 First factor of the third term.
+ * @param b3 Second factor of the third term.
+ * @param a4 First factor of the fourth term.
+ * @param b4 Second factor of the fourth term.
+ * @return \( a_1 b_1 + a_2 b_2 + a_3 b_3 + a_4 b_4 \)
+ *
+ * @see #value(double, double, double, double)
+ * @see #value(double, double, double, double, double, double)
+ * @see #value(double[], double[])
+ */
+ public static double value(double a1, double b1,
+ double a2, double b2,
+ double a3, double b3,
+ double a4, double b4) {
+ // split a1 and b1 as one 26 bits number and one 27 bits number
+ final double a1High = highPart(a1);
+ final double a1Low = a1 - a1High;
+ final double b1High = highPart(b1);
+ final double b1Low = b1 - b1High;
+
+ // accurate multiplication a1 * b1
+ final double prod1High = a1 * b1;
+ final double prod1Low = prodLow(a1Low, b1Low, prod1High, a1High, b1High);
+
+ // split a2 and b2 as one 26 bits number and one 27 bits number
+ final double a2High = highPart(a2);
+ final double a2Low = a2 - a2High;
+ final double b2High = highPart(b2);
+ final double b2Low = b2 - b2High;
+
+ // accurate multiplication a2 * b2
+ final double prod2High = a2 * b2;
+ final double prod2Low = prodLow(a2Low, b2Low, prod2High, a2High, b2High);
+
+ // split a3 and b3 as one 26 bits number and one 27 bits number
+ final double a3High = highPart(a3);
+ final double a3Low = a3 - a3High;
+ final double b3High = highPart(b3);
+ final double b3Low = b3 - b3High;
+
+ // accurate multiplication a3 * b3
+ final double prod3High = a3 * b3;
+ final double prod3Low = prodLow(a3Low, b3Low, prod3High, a3High, b3High);
+
+ // split a4 and b4 as one 26 bits number and one 27 bits number
+ final double a4High = highPart(a4);
+ final double a4Low = a4 - a4High;
+ final double b4High = highPart(b4);
+ final double b4Low = b4 - b4High;
+
+ // accurate multiplication a4 * b4
+ final double prod4High = a4 * b4;
+ final double prod4Low = prodLow(a4Low, b4Low, prod4High, a4High, b4High);
+
+ // accurate addition a1 * b1 + a2 * b2
+ final double s12High = prod1High + prod2High;
+ final double s12Prime = s12High - prod2High;
+ final double s12Low = (prod2High - (s12High - s12Prime)) + (prod1High - s12Prime);
+
+ // accurate addition a1 * b1 + a2 * b2 + a3 * b3
+ final double s123High = s12High + prod3High;
+ final double s123Prime = s123High - prod3High;
+ final double s123Low = (prod3High - (s123High - s123Prime)) + (s12High - s123Prime);
+
+ // accurate addition a1 * b1 + a2 * b2 + a3 * b3 + a4 * b4
+ final double s1234High = s123High + prod4High;
+ final double s1234Prime = s1234High - prod4High;
+ final double s1234Low = (prod4High - (s1234High - s1234Prime)) + (s123High - s1234Prime);
+
+ // final rounding, s1234 may have suffered many cancellations, we try
+ // to recover some bits from the extra words we have saved up to now
+ double result = s1234High + (prod1Low + prod2Low + prod3Low + prod4Low + s12Low + s123Low + s1234Low);
+
+ if (Double.isNaN(result)) {
+ // either we have split infinite numbers or some coefficients were NaNs,
+ // just rely on the naive implementation and let IEEE754 handle this
+ result = a1 * b1 + a2 * b2 + a3 * b3 + a4 * b4;
+ }
+
+ return result;
+ }
+
+ /**
+ * @param value Value.
+ * @return the high part of the value.
+ */
+ private static double highPart(double value) {
+ return Double.longBitsToDouble(Double.doubleToRawLongBits(value) & ((-1L) << 27));
+ }
+
+ /**
+ * @param aLow Low part of first factor.
+ * @param bLow Low part of second factor.
+ * @param prodHigh Product of the factors.
+ * @param aHigh High part of first factor.
+ * @param bHigh High part of second factor.
+ * @return <code>aLow * bLow - (((prodHigh - aHigh * bHigh) - aLow * bHigh) - aHigh * bLow)</code>
+ */
+ private static double prodLow(double aLow,
+ double bLow,
+ double prodHigh,
+ double aHigh,
+ double bHigh) {
+ return aLow * bLow - (((prodHigh - aHigh * bHigh) - aLow * bHigh) - aHigh * bLow);
+ }
+}
http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/e4c21bea/commons-numbers-arrays/src/main/java/org/apache/commons/numbers/arrays/SafeNorm.java
----------------------------------------------------------------------
diff --git a/commons-numbers-arrays/src/main/java/org/apache/commons/numbers/arrays/SafeNorm.java b/commons-numbers-arrays/src/main/java/org/apache/commons/numbers/arrays/SafeNorm.java
new file mode 100644
index 0000000..a04fb4d
--- /dev/null
+++ b/commons-numbers-arrays/src/main/java/org/apache/commons/numbers/arrays/SafeNorm.java
@@ -0,0 +1,86 @@
+/*
+ * 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.arrays;
+
+/**
+ * Computes the Cartesian norm (2-norm), handling both overflow and underflow.
+ * Translation of the <a href="http://www.netlib.org/minpack">minpack</a>
+ * "enorm" subroutine.
+ */
+public class SafeNorm {
+ /** Constant. */
+ private static final double R_DWARF = 3.834e-20;
+ /** Constant. */
+ private static final double R_GIANT = 1.304e+19;
+
+ /**
+ * @param v Cartesian coordinates.
+ * @return the 2-norm of the vector.
+ */
+ public static double value(double[] v) {
+ double s1 = 0;
+ double s2 = 0;
+ double s3 = 0;
+ double x1max = 0;
+ double x3max = 0;
+ double floatn = v.length;
+ double agiant = R_GIANT / floatn;
+ for (int i = 0; i < v.length; i++) {
+ double xabs = Math.abs(v[i]);
+ if (xabs < R_DWARF || xabs > agiant) {
+ if (xabs > R_DWARF) {
+ if (xabs > x1max) {
+ double r = x1max / xabs;
+ s1 = 1 + s1 * r * r;
+ x1max = xabs;
+ } else {
+ double r = xabs / x1max;
+ s1 += r * r;
+ }
+ } else {
+ if (xabs > x3max) {
+ double r = x3max / xabs;
+ s3 = 1 + s3 * r * r;
+ x3max = xabs;
+ } else {
+ if (xabs != 0) {
+ double r = xabs / x3max;
+ s3 += r * r;
+ }
+ }
+ }
+ } else {
+ s2 += xabs * xabs;
+ }
+ }
+ double norm;
+ if (s1 != 0) {
+ norm = x1max * Math.sqrt(s1 + (s2 / x1max) / x1max);
+ } else {
+ if (s2 == 0) {
+ norm = x3max * Math.sqrt(s3);
+ } else {
+ if (s2 >= x3max) {
+ norm = Math.sqrt(s2 * (1 + (x3max / s2) * (x3max * s3)));
+ } else {
+ norm = Math.sqrt(x3max * ((s2 / x3max) + (x3max * s3)));
+ }
+ }
+ }
+ return norm;
+ }
+}
http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/e4c21bea/commons-numbers-arrays/src/main/java/org/apache/commons/numbers/arrays/package-info.java
----------------------------------------------------------------------
diff --git a/commons-numbers-arrays/src/main/java/org/apache/commons/numbers/arrays/package-info.java b/commons-numbers-arrays/src/main/java/org/apache/commons/numbers/arrays/package-info.java
new file mode 100644
index 0000000..be18674
--- /dev/null
+++ b/commons-numbers-arrays/src/main/java/org/apache/commons/numbers/arrays/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * 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.
+ */
+/**
+ * Array utilities.
+ */
+package org.apache.commons.numbers.arrays;
http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/e4c21bea/commons-numbers-arrays/src/site/site.xml
----------------------------------------------------------------------
diff --git a/commons-numbers-arrays/src/site/site.xml b/commons-numbers-arrays/src/site/site.xml
new file mode 100644
index 0000000..dd0901d
--- /dev/null
+++ b/commons-numbers-arrays/src/site/site.xml
@@ -0,0 +1,35 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<!--
+ 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.
+-->
+<project name="Numbers">
+ <bannerRight>
+ <name>Apache Commons Numbers</name>
+ <src>/images/commons_numbers.small.png</src>
+ <href>/index.html</href>
+ </bannerRight>
+
+ <body>
+ <menu name="Numbers Arrays">
+ <item name="Overview" href="index.html"/>
+ <item name="Latest API docs (development)"
+ href="apidocs/index.html"/>
+ <!--item name="Javadoc (1.0 release)"
+ href="http://commons.apache.org/rng/commons-numbers-arrays/javadocs/api-1.0/index.html"/-->
+ </menu>
+
+ </body>
+</project>
http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/e4c21bea/commons-numbers-arrays/src/site/xdoc/index.xml
----------------------------------------------------------------------
diff --git a/commons-numbers-arrays/src/site/xdoc/index.xml b/commons-numbers-arrays/src/site/xdoc/index.xml
new file mode 100644
index 0000000..e2e9482
--- /dev/null
+++ b/commons-numbers-arrays/src/site/xdoc/index.xml
@@ -0,0 +1,40 @@
+<?xml version="1.0"?>
+
+<!--
+ 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.
+ -->
+
+<document>
+
+ <properties>
+ <title>Commons Numbers Arrays</title>
+ </properties>
+
+ <body>
+
+ <section name="Apache Commons Numbers: Number types" href="summary">
+ <p>
+ Commons Numbers provides utilities such as complex numbers and fractions.
+ </p>
+
+ <p>
+ The "arrays" module contains array utilities.
+ </p>
+ </section>
+
+ </body>
+
+</document>
http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/e4c21bea/commons-numbers-arrays/src/test/java/org/apache/commons/numbers/arrays/CosAngleTest.java
----------------------------------------------------------------------
diff --git a/commons-numbers-arrays/src/test/java/org/apache/commons/numbers/arrays/CosAngleTest.java b/commons-numbers-arrays/src/test/java/org/apache/commons/numbers/arrays/CosAngleTest.java
new file mode 100644
index 0000000..06b1c62
--- /dev/null
+++ b/commons-numbers-arrays/src/test/java/org/apache/commons/numbers/arrays/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.arrays;
+
+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);
+ }
+}
http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/e4c21bea/commons-numbers-arrays/src/test/java/org/apache/commons/numbers/arrays/LinearCombinationTest.java
----------------------------------------------------------------------
diff --git a/commons-numbers-arrays/src/test/java/org/apache/commons/numbers/arrays/LinearCombinationTest.java b/commons-numbers-arrays/src/test/java/org/apache/commons/numbers/arrays/LinearCombinationTest.java
new file mode 100644
index 0000000..49843ac
--- /dev/null
+++ b/commons-numbers-arrays/src/test/java/org/apache/commons/numbers/arrays/LinearCombinationTest.java
@@ -0,0 +1,297 @@
+/*
+ * 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.arrays;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import org.apache.commons.rng.UniformRandomProvider;
+import org.apache.commons.rng.simple.RandomSource;
+import org.apache.commons.numbers.fraction.BigFraction;
+
+/**
+ * Test cases for the {@link LinearCombination} class.
+ */
+public class LinearCombinationTest {
+ // MATH-1005
+ @Test
+ public void testSingleElementArray() {
+ final double[] a = { 1.23456789 };
+ final double[] b = { 98765432.1 };
+
+ Assert.assertEquals(a[0] * b[0], LinearCombination.value(a, b), 0d);
+ }
+
+ @Test
+ public void testTwoSums() {
+ final BigFraction[] aF = new BigFraction[] {
+ new BigFraction(-1321008684645961L, 268435456L),
+ new BigFraction(-5774608829631843L, 268435456L),
+ new BigFraction(-7645843051051357L, 8589934592L)
+ };
+ final BigFraction[] bF = new BigFraction[] {
+ new BigFraction(-5712344449280879L, 2097152L),
+ new BigFraction(-4550117129121957L, 2097152L),
+ new BigFraction(8846951984510141L, 131072L)
+ };
+
+ final int len = aF.length;
+ final double[] a = new double[len];
+ final double[] b = new double[len];
+ for (int i = 0; i < len; i++) {
+ a[i] = aF[i].getNumerator().doubleValue() / aF[i].getDenominator().doubleValue();
+ b[i] = bF[i].getNumerator().doubleValue() / bF[i].getDenominator().doubleValue();
+ }
+
+ // Ensure "array" and "inline" implementations give the same result.
+ final double abSumInline = LinearCombination.value(a[0], b[0],
+ a[1], b[1],
+ a[2], b[2]);
+ final double abSumArray = LinearCombination.value(a, b);
+ Assert.assertEquals(abSumInline, abSumArray, 0);
+
+ // Compare with arbitrary precision computation.
+ BigFraction result = BigFraction.ZERO;
+ for (int i = 0; i < a.length; i++) {
+ result = result.add(aF[i].multiply(bF[i]));
+ }
+ final double expected = result.doubleValue();
+ Assert.assertEquals(expected, abSumInline, 1e-15);
+
+ final double naive = a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
+ Assert.assertTrue(Math.abs(naive - abSumInline) > 1.5);
+ }
+
+ @Test
+ public void testArrayVsInline() {
+ final UniformRandomProvider rng = RandomSource.create(RandomSource.XOR_SHIFT_1024_S);
+
+ double sInline;
+ double sArray;
+ final double scale = 1e17;
+ for (int i = 0; i < 10000; ++i) {
+ final double u1 = scale * rng.nextDouble();
+ final double u2 = scale * rng.nextDouble();
+ final double u3 = scale * rng.nextDouble();
+ final double u4 = scale * rng.nextDouble();
+ final double v1 = scale * rng.nextDouble();
+ final double v2 = scale * rng.nextDouble();
+ final double v3 = scale * rng.nextDouble();
+ final double v4 = scale * rng.nextDouble();
+
+ // One sum.
+ sInline = LinearCombination.value(u1, v1, u2, v2);
+ sArray = LinearCombination.value(new double[] { u1, u2 },
+ new double[] { v1, v2 });
+ Assert.assertEquals(sInline, sArray, 0);
+
+ // Two sums.
+ sInline = LinearCombination.value(u1, v1, u2, v2, u3, v3);
+ sArray = LinearCombination.value(new double[] { u1, u2, u3 },
+ new double[] { v1, v2, v3 });
+ Assert.assertEquals(sInline, sArray, 0);
+
+ // Three sums.
+ sInline = LinearCombination.value(u1, v1, u2, v2, u3, v3, u4, v4);
+ sArray = LinearCombination.value(new double[] { u1, u2, u3, u4 },
+ new double[] { v1, v2, v3, v4 });
+ Assert.assertEquals(sInline, sArray, 0);
+ }
+ }
+
+ @Test
+ public void testHuge() {
+ int scale = 971;
+ final double[] a = new double[] {
+ -1321008684645961.0 / 268435456.0,
+ -5774608829631843.0 / 268435456.0,
+ -7645843051051357.0 / 8589934592.0
+ };
+ final double[] b = new double[] {
+ -5712344449280879.0 / 2097152.0,
+ -4550117129121957.0 / 2097152.0,
+ 8846951984510141.0 / 131072.0
+ };
+
+ final int len = a.length;
+ final double[] scaledA = new double[len];
+ final double[] scaledB = new double[len];
+ for (int i = 0; i < len; ++i) {
+ scaledA[i] = Math.scalb(a[i], -scale);
+ scaledB[i] = Math.scalb(b[i], scale);
+ }
+ final double abSumInline = LinearCombination.value(scaledA[0], scaledB[0],
+ scaledA[1], scaledB[1],
+ scaledA[2], scaledB[2]);
+ final double abSumArray = LinearCombination.value(scaledA, scaledB);
+
+ Assert.assertEquals(abSumInline, abSumArray, 0);
+ Assert.assertEquals(-1.8551294182586248737720779899, abSumInline, 1e-15);
+
+ final double naive = scaledA[0] * scaledB[0] + scaledA[1] * scaledB[1] + scaledA[2] * scaledB[2];
+ Assert.assertTrue(Math.abs(naive - abSumInline) > 1.5);
+ }
+
+ @Test
+ public void testInfinite() {
+ final double[][] a = new double[][] {
+ { 1, 2, 3, 4 },
+ { 1, Double.POSITIVE_INFINITY, 3, 4 },
+ { 1, 2, Double.POSITIVE_INFINITY, 4 },
+ { 1, Double.POSITIVE_INFINITY, 3, Double.NEGATIVE_INFINITY },
+ { 1, 2, 3, 4 },
+ { 1, 2, 3, 4 },
+ { 1, 2, 3, 4 },
+ { 1, 2, 3, 4 }
+ };
+ final double[][] b = new double[][] {
+ { 1, -2, 3, 4 },
+ { 1, -2, 3, 4 },
+ { 1, -2, 3, 4 },
+ { 1, -2, 3, 4 },
+ { 1, Double.POSITIVE_INFINITY, 3, 4 },
+ { 1, -2, Double.POSITIVE_INFINITY, 4 },
+ { 1, Double.POSITIVE_INFINITY, 3, Double.NEGATIVE_INFINITY },
+ { Double.NaN, -2, 3, 4 }
+ };
+
+ Assert.assertEquals(-3,
+ LinearCombination.value(a[0][0], b[0][0],
+ a[0][1], b[0][1]),
+ 1e-10);
+ Assert.assertEquals(6,
+ LinearCombination.value(a[0][0], b[0][0],
+ a[0][1], b[0][1],
+ a[0][2], b[0][2]),
+ 1e-10);
+ Assert.assertEquals(22,
+ LinearCombination.value(a[0][0], b[0][0],
+ a[0][1], b[0][1],
+ a[0][2], b[0][2],
+ a[0][3], b[0][3]),
+ 1e-10);
+ Assert.assertEquals(22, LinearCombination.value(a[0], b[0]), 1e-10);
+
+ Assert.assertEquals(Double.NEGATIVE_INFINITY,
+ LinearCombination.value(a[1][0], b[1][0],
+ a[1][1], b[1][1]),
+ 1e-10);
+ Assert.assertEquals(Double.NEGATIVE_INFINITY,
+ LinearCombination.value(a[1][0], b[1][0],
+ a[1][1], b[1][1],
+ a[1][2], b[1][2]),
+ 1e-10);
+ Assert.assertEquals(Double.NEGATIVE_INFINITY,
+ LinearCombination.value(a[1][0], b[1][0],
+ a[1][1], b[1][1],
+ a[1][2], b[1][2],
+ a[1][3], b[1][3]),
+ 1e-10);
+ Assert.assertEquals(Double.NEGATIVE_INFINITY, LinearCombination.value(a[1], b[1]), 1e-10);
+
+ Assert.assertEquals(-3,
+ LinearCombination.value(a[2][0], b[2][0],
+ a[2][1], b[2][1]),
+ 1e-10);
+ Assert.assertEquals(Double.POSITIVE_INFINITY,
+ LinearCombination.value(a[2][0], b[2][0],
+ a[2][1], b[2][1],
+ a[2][2], b[2][2]),
+ 1e-10);
+ Assert.assertEquals(Double.POSITIVE_INFINITY,
+ LinearCombination.value(a[2][0], b[2][0],
+ a[2][1], b[2][1],
+ a[2][2], b[2][2],
+ a[2][3], b[2][3]),
+ 1e-10);
+ Assert.assertEquals(Double.POSITIVE_INFINITY, LinearCombination.value(a[2], b[2]), 1e-10);
+
+ Assert.assertEquals(Double.NEGATIVE_INFINITY,
+ LinearCombination.value(a[3][0], b[3][0],
+ a[3][1], b[3][1]),
+ 1e-10);
+ Assert.assertEquals(Double.NEGATIVE_INFINITY,
+ LinearCombination.value(a[3][0], b[3][0],
+ a[3][1], b[3][1],
+ a[3][2], b[3][2]),
+ 1e-10);
+ Assert.assertEquals(Double.NEGATIVE_INFINITY,
+ LinearCombination.value(a[3][0], b[3][0],
+ a[3][1], b[3][1],
+ a[3][2], b[3][2],
+ a[3][3], b[3][3]),
+ 1e-10);
+ Assert.assertEquals(Double.NEGATIVE_INFINITY, LinearCombination.value(a[3], b[3]), 1e-10);
+
+ Assert.assertEquals(Double.POSITIVE_INFINITY,
+ LinearCombination.value(a[4][0], b[4][0],
+ a[4][1], b[4][1]),
+ 1e-10);
+ Assert.assertEquals(Double.POSITIVE_INFINITY,
+ LinearCombination.value(a[4][0], b[4][0],
+ a[4][1], b[4][1],
+ a[4][2], b[4][2]),
+ 1e-10);
+ Assert.assertEquals(Double.POSITIVE_INFINITY,
+ LinearCombination.value(a[4][0], b[4][0],
+ a[4][1], b[4][1],
+ a[4][2], b[4][2],
+ a[4][3], b[4][3]),
+ 1e-10);
+ Assert.assertEquals(Double.POSITIVE_INFINITY, LinearCombination.value(a[4], b[4]), 1e-10);
+
+ Assert.assertEquals(-3,
+ LinearCombination.value(a[5][0], b[5][0],
+ a[5][1], b[5][1]),
+ 1e-10);
+ Assert.assertEquals(Double.POSITIVE_INFINITY,
+ LinearCombination.value(a[5][0], b[5][0],
+ a[5][1], b[5][1],
+ a[5][2], b[5][2]),
+ 1e-10);
+ Assert.assertEquals(Double.POSITIVE_INFINITY,
+ LinearCombination.value(a[5][0], b[5][0],
+ a[5][1], b[5][1],
+ a[5][2], b[5][2],
+ a[5][3], b[5][3]),
+ 1e-10);
+ Assert.assertEquals(Double.POSITIVE_INFINITY, LinearCombination.value(a[5], b[5]), 1e-10);
+
+ Assert.assertEquals(Double.POSITIVE_INFINITY,
+ LinearCombination.value(a[6][0], b[6][0],
+ a[6][1], b[6][1]),
+ 1e-10);
+ Assert.assertEquals(Double.POSITIVE_INFINITY,
+ LinearCombination.value(a[6][0], b[6][0],
+ a[6][1], b[6][1],
+ a[6][2], b[6][2]),
+ 1e-10);
+ Assert.assertTrue(Double.isNaN(LinearCombination.value(a[6][0], b[6][0],
+ a[6][1], b[6][1],
+ a[6][2], b[6][2],
+ a[6][3], b[6][3])));
+ Assert.assertTrue(Double.isNaN(LinearCombination.value(a[6], b[6])));
+
+ Assert.assertTrue(Double.isNaN(LinearCombination.value(a[7][0], b[7][0],
+ a[7][1], b[7][1])));
+ Assert.assertTrue(Double.isNaN(LinearCombination.value(a[7][0], b[7][0],
+ a[7][1], b[7][1],
+ a[7][2], b[7][2])));
+ Assert.assertTrue(Double.isNaN(LinearCombination.value(a[7][0], b[7][0],
+ a[7][1], b[7][1],
+ a[7][2], b[7][2],
+ a[7][3], b[7][3])));
+ Assert.assertTrue(Double.isNaN(LinearCombination.value(a[7], b[7])));
+ }
+}
http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/e4c21bea/commons-numbers-arrays/src/test/java/org/apache/commons/numbers/arrays/SafeNormTest.java
----------------------------------------------------------------------
diff --git a/commons-numbers-arrays/src/test/java/org/apache/commons/numbers/arrays/SafeNormTest.java b/commons-numbers-arrays/src/test/java/org/apache/commons/numbers/arrays/SafeNormTest.java
new file mode 100644
index 0000000..dc949df
--- /dev/null
+++ b/commons-numbers-arrays/src/test/java/org/apache/commons/numbers/arrays/SafeNormTest.java
@@ -0,0 +1,62 @@
+/*
+ * 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.arrays;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Test cases for the {@link SafeNorm} class.
+ */
+public class SafeNormTest {
+
+ @Test
+ public void testTiny() {
+ final double s = 1e-320;
+ final double[] v = new double[] { s, s };
+ Assert.assertEquals(Math.sqrt(2) * s, SafeNorm.value(v), 0d);
+ }
+
+ @Test
+ public void testBig() {
+ final double s = 1e300;
+ final double[] v = new double[] { s, s };
+ Assert.assertEquals(Math.sqrt(2) * s, SafeNorm.value(v), 0d);
+ }
+
+ @Test
+ public void testOne3D() {
+ final double s = 1;
+ final double[] v = new double[] { s, s, s };
+ Assert.assertEquals(Math.sqrt(3), SafeNorm.value(v), 0d);
+ }
+
+ @Test
+ public void testUnit3D() {
+ Assert.assertEquals(1, SafeNorm.value(new double[] { 1, 0, 0 }), 0d);
+ Assert.assertEquals(1, SafeNorm.value(new double[] { 0, 1, 0 }), 0d);
+ Assert.assertEquals(1, SafeNorm.value(new double[] { 0, 0, 1 }), 0d);
+ }
+
+ @Test
+ public void testSimple() {
+ final double[] v = new double[] { -0.9, 8.7, -6.5, -4.3, -2.1, 0, 1.2, 3.4, -5.6, 7.8, 9.0 };
+ double n = 0;
+ for (int i = 0; i < v.length; i++) {
+ n += v[i] * v[i];
+ }
+ final double expected = Math.sqrt(n);
+ Assert.assertEquals(expected, SafeNorm.value(v), 0d);
+ }
+}
http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/e4c21bea/commons-numbers-core/LICENSE.txt
----------------------------------------------------------------------
diff --git a/commons-numbers-core/LICENSE.txt b/commons-numbers-core/LICENSE.txt
index 67c36eb..261eeb9 100644
--- a/commons-numbers-core/LICENSE.txt
+++ b/commons-numbers-core/LICENSE.txt
@@ -199,68 +199,3 @@
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.
-
-===============================================================================
-
-Apache "Commons Numbers" derivative works:
-
-The "commons-numbers-core" library includes a number of subcomponents
-whose implementation is derived from original sources written in C or
-Fortran. License terms of the original sources are reproduced below.
-
-===============================================================================
-Code for "SafeNorm" comes from the MINPACK library.
-
-Original source copyright and license statement:
-
-Minpack Copyright Notice (1999) University of Chicago. All rights reserved
-
-Redistribution and use in source and binary forms, with or
-without modification, are permitted provided that the
-following conditions are met:
-
-1. Redistributions of source code must retain the above
-copyright notice, this list of conditions and the following
-disclaimer.
-
-2. Redistributions in binary form must reproduce the above
-copyright notice, this list of conditions and the following
-disclaimer in the documentation and/or other materials
-provided with the distribution.
-
-3. The end-user documentation included with the
-redistribution, if any, must include the following
-acknowledgment:
-
- "This product includes software developed by the
- University of Chicago, as Operator of Argonne National
- Laboratory.
-
-Alternately, this acknowledgment may appear in the software
-itself, if and wherever such third-party acknowledgments
-normally appear.
-
-4. WARRANTY DISCLAIMER. THE SOFTWARE IS SUPPLIED "AS IS"
-WITHOUT WARRANTY OF ANY KIND. THE COPYRIGHT HOLDER, THE
-UNITED STATES, THE UNITED STATES DEPARTMENT OF ENERGY, AND
-THEIR EMPLOYEES: (1) DISCLAIM ANY WARRANTIES, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO ANY IMPLIED WARRANTIES
-OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE
-OR NON-INFRINGEMENT, (2) DO NOT ASSUME ANY LEGAL LIABILITY
-OR RESPONSIBILITY FOR THE ACCURACY, COMPLETENESS, OR
-USEFULNESS OF THE SOFTWARE, (3) DO NOT REPRESENT THAT USE OF
-THE SOFTWARE WOULD NOT INFRINGE PRIVATELY OWNED RIGHTS, (4)
-DO NOT WARRANT THAT THE SOFTWARE WILL FUNCTION
-UNINTERRUPTED, THAT IT IS ERROR-FREE OR THAT ANY ERRORS WILL
-BE CORRECTED.
-
-5. LIMITATION OF LIABILITY. IN NO EVENT WILL THE COPYRIGHT
-HOLDER, THE UNITED STATES, THE UNITED STATES DEPARTMENT OF
-ENERGY, OR THEIR EMPLOYEES: BE LIABLE FOR ANY INDIRECT,
-INCIDENTAL, CONSEQUENTIAL, SPECIAL OR PUNITIVE DAMAGES OF
-ANY KIND OR NATURE, INCLUDING BUT NOT LIMITED TO LOSS OF
-PROFITS OR LOSS OF DATA, FOR ANY REASON WHATSOEVER, WHETHER
-SUCH LIABILITY IS ASSERTED ON THE BASIS OF CONTRACT, TORT
-(INCLUDING NEGLIGENCE OR STRICT LIABILITY), OR OTHERWISE,
-EVEN IF ANY OF SAID PARTIES HAS BEEN WARNED OF THE
-POSSIBILITY OF SUCH LOSS OR DAMAGES.
http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/e4c21bea/commons-numbers-core/pom.xml
----------------------------------------------------------------------
diff --git a/commons-numbers-core/pom.xml b/commons-numbers-core/pom.xml
index 181bab7..76c2512 100644
--- a/commons-numbers-core/pom.xml
+++ b/commons-numbers-core/pom.xml
@@ -42,22 +42,6 @@
<numbers.parent.dir>${basedir}/..</numbers.parent.dir>
</properties>
- <dependencies>
- <dependency>
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-rng-simple</artifactId>
- <version>1.0</version>
- <scope>test</scope>
- </dependency>
-
- <dependency>
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-numbers-fraction</artifactId>
- <version>1.0-SNAPSHOT</version>
- <scope>test</scope>
- </dependency>
- </dependencies>
-
<build>
<plugins>
<plugin>
http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/e4c21bea/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
deleted file mode 100644
index e5f7e9c..0000000
--- a/commons-numbers-core/src/main/java/org/apache/commons/numbers/core/CosAngle.java
+++ /dev/null
@@ -1,35 +0,0 @@
-/*
- * 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);
- }
-}
-