MATH-1416: Delete utilities now in "Commons Numbers".

This commit is contained in:
Gilles 2017-05-25 02:02:16 +02:00
parent 3200db1671
commit 4a37273818
4 changed files with 15 additions and 787 deletions

View File

@ -243,18 +243,6 @@ public class MathArrays {
return FastMath.sqrt(sum);
}
/**
* Calculates the cosine of the angle between two vectors.
*
* @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.
* @since 3.6
*/
public static double cosAngle(double[] v1, double[] v2) {
return linearCombination(v1, v2) / (safeNorm(v1) * safeNorm(v2));
}
/**
* Calculates the L<sub>2</sub> (Euclidean) distance between two points.
*
@ -634,121 +622,6 @@ public class MathArrays {
}
}
/**
* Returns the Cartesian norm (2-norm), handling both overflow and underflow.
* Translation of the minpack enorm subroutine.
* <p>
* The redistribution policy for MINPACK is available
* <a href="http://www.netlib.org/minpack/disclaimer">here</a>, for
* convenience, it is reproduced below.</p>
*
* <table style="text-align: center; background-color: #E0E0E0" border="0" width="80%" cellpadding="10" summary="MINPACK redistribution policy">
* <tr><td>
* Minpack Copyright Notice (1999) University of Chicago.
* All rights reserved
* </td></tr>
* <tr><td>
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* <ol>
* <li>Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.</li>
* <li>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.</li>
* <li>The end-user documentation included with the redistribution, if any,
* must include the following acknowledgment:
* {@code 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.</li>
* <li><strong>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.</strong></li>
* <li><strong>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.</strong></li>
* </ol></td></tr>
* </table>
*
* @param v Vector of doubles.
* @return the 2-norm of the vector.
* @since 2.2
*/
public static double safeNorm(double[] v) {
double rdwarf = 3.834e-20;
double rgiant = 1.304e+19;
double s1 = 0;
double s2 = 0;
double s3 = 0;
double x1max = 0;
double x3max = 0;
double floatn = v.length;
double agiant = rgiant / floatn;
for (int i = 0; i < v.length; i++) {
double xabs = FastMath.abs(v[i]);
if (xabs < rdwarf || xabs > agiant) {
if (xabs > rdwarf) {
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;
}
/**
* Sort an array in ascending order in place and perform the same reordering
* of entries on other arrays. For example, if
@ -957,354 +830,6 @@ public class MathArrays {
return output;
}
/**
* Compute a linear combination 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.
* <br>
* 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 SIAM J. Sci. Comput.
*
* @param a Factors.
* @param b Factors.
* @return <code>&Sigma;<sub>i</sub> a<sub>i</sub> b<sub>i</sub></code>.
* @throws DimensionMismatchException if arrays dimensions don't match
*/
public static double linearCombination(final double[] a, final double[] b)
throws DimensionMismatchException {
checkEqualLength(a, b);
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 = Double.longBitsToDouble(Double.doubleToRawLongBits(ai) & ((-1L) << 27));
final double aLow = ai - aHigh;
final double bi = b[i];
final double bHigh = Double.longBitsToDouble(Double.doubleToRawLongBits(bi) & ((-1L) << 27));
final double bLow = bi - bHigh;
prodHigh[i] = ai * bi;
final double prodLow = aLow * bLow - (((prodHigh[i] -
aHigh * bHigh) -
aLow * bHigh) -
aHigh * bLow);
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;
}
/**
* Compute a linear combination accurately.
* <p>
* This method computes a<sub>1</sub>&times;b<sub>1</sub> +
* a<sub>2</sub>&times;b<sub>2</sub> 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 SIAM J. Sci. Comput.
* </p>
* @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<sub>1</sub>&times;b<sub>1</sub> +
* a<sub>2</sub>&times;b<sub>2</sub>
* @see #linearCombination(double, double, double, double, double, double)
* @see #linearCombination(double, double, double, double, double, double, double, double)
*/
public static double linearCombination(final double a1, final double b1,
final double a2, final double b2) {
// the code below is split in many additions/subtractions that may
// appear redundant. However, they should NOT be simplified, as they
// use IEEE754 floating point arithmetic rounding properties.
// The variable 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
// split a1 and b1 as one 26 bits number and one 27 bits number
final double a1High = Double.longBitsToDouble(Double.doubleToRawLongBits(a1) & ((-1L) << 27));
final double a1Low = a1 - a1High;
final double b1High = Double.longBitsToDouble(Double.doubleToRawLongBits(b1) & ((-1L) << 27));
final double b1Low = b1 - b1High;
// accurate multiplication a1 * b1
final double prod1High = a1 * b1;
final double prod1Low = a1Low * b1Low - (((prod1High - a1High * b1High) - a1Low * b1High) - a1High * b1Low);
// split a2 and b2 as one 26 bits number and one 27 bits number
final double a2High = Double.longBitsToDouble(Double.doubleToRawLongBits(a2) & ((-1L) << 27));
final double a2Low = a2 - a2High;
final double b2High = Double.longBitsToDouble(Double.doubleToRawLongBits(b2) & ((-1L) << 27));
final double b2Low = b2 - b2High;
// accurate multiplication a2 * b2
final double prod2High = a2 * b2;
final double prod2Low = a2Low * b2Low - (((prod2High - a2High * b2High) - a2Low * b2High) - a2High * b2Low);
// 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;
}
/**
* Compute a linear combination accurately.
* <p>
* This method computes a<sub>1</sub>&times;b<sub>1</sub> +
* a<sub>2</sub>&times;b<sub>2</sub> + a<sub>3</sub>&times;b<sub>3</sub>
* 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 SIAM J. Sci. Comput.
* </p>
* @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<sub>1</sub>&times;b<sub>1</sub> +
* a<sub>2</sub>&times;b<sub>2</sub> + a<sub>3</sub>&times;b<sub>3</sub>
* @see #linearCombination(double, double, double, double)
* @see #linearCombination(double, double, double, double, double, double, double, double)
*/
public static double linearCombination(final double a1, final double b1,
final double a2, final double b2,
final double a3, final double b3) {
// 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
// split a1 and b1 as one 26 bits number and one 27 bits number
final double a1High = Double.longBitsToDouble(Double.doubleToRawLongBits(a1) & ((-1L) << 27));
final double a1Low = a1 - a1High;
final double b1High = Double.longBitsToDouble(Double.doubleToRawLongBits(b1) & ((-1L) << 27));
final double b1Low = b1 - b1High;
// accurate multiplication a1 * b1
final double prod1High = a1 * b1;
final double prod1Low = a1Low * b1Low - (((prod1High - a1High * b1High) - a1Low * b1High) - a1High * b1Low);
// split a2 and b2 as one 26 bits number and one 27 bits number
final double a2High = Double.longBitsToDouble(Double.doubleToRawLongBits(a2) & ((-1L) << 27));
final double a2Low = a2 - a2High;
final double b2High = Double.longBitsToDouble(Double.doubleToRawLongBits(b2) & ((-1L) << 27));
final double b2Low = b2 - b2High;
// accurate multiplication a2 * b2
final double prod2High = a2 * b2;
final double prod2Low = a2Low * b2Low - (((prod2High - a2High * b2High) - a2Low * b2High) - a2High * b2Low);
// split a3 and b3 as one 26 bits number and one 27 bits number
final double a3High = Double.longBitsToDouble(Double.doubleToRawLongBits(a3) & ((-1L) << 27));
final double a3Low = a3 - a3High;
final double b3High = Double.longBitsToDouble(Double.doubleToRawLongBits(b3) & ((-1L) << 27));
final double b3Low = b3 - b3High;
// accurate multiplication a3 * b3
final double prod3High = a3 * b3;
final double prod3Low = a3Low * b3Low - (((prod3High - a3High * b3High) - a3Low * b3High) - a3High * b3Low);
// 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;
}
/**
* Compute a linear combination accurately.
* <p>
* This method computes a<sub>1</sub>&times;b<sub>1</sub> +
* a<sub>2</sub>&times;b<sub>2</sub> + a<sub>3</sub>&times;b<sub>3</sub> +
* a<sub>4</sub>&times;b<sub>4</sub>
* 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 SIAM J. Sci. Comput.
* </p>
* @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 third term
* @param b4 second factor of the third term
* @return a<sub>1</sub>&times;b<sub>1</sub> +
* a<sub>2</sub>&times;b<sub>2</sub> + a<sub>3</sub>&times;b<sub>3</sub> +
* a<sub>4</sub>&times;b<sub>4</sub>
* @see #linearCombination(double, double, double, double)
* @see #linearCombination(double, double, double, double, double, double)
*/
public static double linearCombination(final double a1, final double b1,
final double a2, final double b2,
final double a3, final double b3,
final double a4, final double b4) {
// 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
// split a1 and b1 as one 26 bits number and one 27 bits number
final double a1High = Double.longBitsToDouble(Double.doubleToRawLongBits(a1) & ((-1L) << 27));
final double a1Low = a1 - a1High;
final double b1High = Double.longBitsToDouble(Double.doubleToRawLongBits(b1) & ((-1L) << 27));
final double b1Low = b1 - b1High;
// accurate multiplication a1 * b1
final double prod1High = a1 * b1;
final double prod1Low = a1Low * b1Low - (((prod1High - a1High * b1High) - a1Low * b1High) - a1High * b1Low);
// split a2 and b2 as one 26 bits number and one 27 bits number
final double a2High = Double.longBitsToDouble(Double.doubleToRawLongBits(a2) & ((-1L) << 27));
final double a2Low = a2 - a2High;
final double b2High = Double.longBitsToDouble(Double.doubleToRawLongBits(b2) & ((-1L) << 27));
final double b2Low = b2 - b2High;
// accurate multiplication a2 * b2
final double prod2High = a2 * b2;
final double prod2Low = a2Low * b2Low - (((prod2High - a2High * b2High) - a2Low * b2High) - a2High * b2Low);
// split a3 and b3 as one 26 bits number and one 27 bits number
final double a3High = Double.longBitsToDouble(Double.doubleToRawLongBits(a3) & ((-1L) << 27));
final double a3Low = a3 - a3High;
final double b3High = Double.longBitsToDouble(Double.doubleToRawLongBits(b3) & ((-1L) << 27));
final double b3Low = b3 - b3High;
// accurate multiplication a3 * b3
final double prod3High = a3 * b3;
final double prod3Low = a3Low * b3Low - (((prod3High - a3High * b3High) - a3Low * b3High) - a3High * b3Low);
// split a4 and b4 as one 26 bits number and one 27 bits number
final double a4High = Double.longBitsToDouble(Double.doubleToRawLongBits(a4) & ((-1L) << 27));
final double a4Low = a4 - a4High;
final double b4High = Double.longBitsToDouble(Double.doubleToRawLongBits(b4) & ((-1L) << 27));
final double b4Low = b4 - b4High;
// accurate multiplication a4 * b4
final double prod4High = a4 * b4;
final double prod4Low = a4Low * b4Low - (((prod4High - a4High * b4High) - a4Low * b4High) - a4High * b4Low);
// 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;
}
/**
* Returns true iff both arguments are null or have same dimensions and all
* their elements are equal as defined by

View File

@ -16,9 +16,10 @@
*/
package org.apache.commons.math4;
import org.apache.commons.math4.RealFieldElement;
import org.apache.commons.numbers.arrays.LinearCombination;
import org.apache.commons.rng.UniformRandomProvider;
import org.apache.commons.rng.simple.RandomSource;
import org.apache.commons.math4.RealFieldElement;
import org.apache.commons.math4.util.FastMath;
import org.apache.commons.math4.util.MathArrays;
import org.junit.Assert;
@ -399,7 +400,7 @@ public abstract class ExtendedFieldElementAbstractTest<T extends RealFieldElemen
double[] bD = generateDouble(r, 10);
T[] aF = toFieldArray(aD);
T[] bF = toFieldArray(bD);
checkRelative(MathArrays.linearCombination(aD, bD),
checkRelative(LinearCombination.value(aD, bD),
aF[0].linearCombination(aF, bF));
}
}
@ -411,7 +412,7 @@ public abstract class ExtendedFieldElementAbstractTest<T extends RealFieldElemen
double[] aD = generateDouble(r, 10);
double[] bD = generateDouble(r, 10);
T[] bF = toFieldArray(bD);
checkRelative(MathArrays.linearCombination(aD, bD),
checkRelative(LinearCombination.value(aD, bD),
bF[0].linearCombination(aD, bF));
}
}
@ -424,7 +425,7 @@ public abstract class ExtendedFieldElementAbstractTest<T extends RealFieldElemen
double[] bD = generateDouble(r, 2);
T[] aF = toFieldArray(aD);
T[] bF = toFieldArray(bD);
checkRelative(MathArrays.linearCombination(aD[0], bD[0], aD[1], bD[1]),
checkRelative(LinearCombination.value(aD[0], bD[0], aD[1], bD[1]),
aF[0].linearCombination(aF[0], bF[0], aF[1], bF[1]));
}
}
@ -436,7 +437,7 @@ public abstract class ExtendedFieldElementAbstractTest<T extends RealFieldElemen
double[] aD = generateDouble(r, 2);
double[] bD = generateDouble(r, 2);
T[] bF = toFieldArray(bD);
checkRelative(MathArrays.linearCombination(aD[0], bD[0], aD[1], bD[1]),
checkRelative(LinearCombination.value(aD[0], bD[0], aD[1], bD[1]),
bF[0].linearCombination(aD[0], bF[0], aD[1], bF[1]));
}
}
@ -449,7 +450,7 @@ public abstract class ExtendedFieldElementAbstractTest<T extends RealFieldElemen
double[] bD = generateDouble(r, 3);
T[] aF = toFieldArray(aD);
T[] bF = toFieldArray(bD);
checkRelative(MathArrays.linearCombination(aD[0], bD[0], aD[1], bD[1], aD[2], bD[2]),
checkRelative(LinearCombination.value(aD[0], bD[0], aD[1], bD[1], aD[2], bD[2]),
aF[0].linearCombination(aF[0], bF[0], aF[1], bF[1], aF[2], bF[2]));
}
}
@ -461,7 +462,7 @@ public abstract class ExtendedFieldElementAbstractTest<T extends RealFieldElemen
double[] aD = generateDouble(r, 3);
double[] bD = generateDouble(r, 3);
T[] bF = toFieldArray(bD);
checkRelative(MathArrays.linearCombination(aD[0], bD[0], aD[1], bD[1], aD[2], bD[2]),
checkRelative(LinearCombination.value(aD[0], bD[0], aD[1], bD[1], aD[2], bD[2]),
bF[0].linearCombination(aD[0], bF[0], aD[1], bF[1], aD[2], bF[2]));
}
}
@ -474,7 +475,7 @@ public abstract class ExtendedFieldElementAbstractTest<T extends RealFieldElemen
double[] bD = generateDouble(r, 4);
T[] aF = toFieldArray(aD);
T[] bF = toFieldArray(bD);
checkRelative(MathArrays.linearCombination(aD[0], bD[0], aD[1], bD[1], aD[2], bD[2], aD[3], bD[3]),
checkRelative(LinearCombination.value(aD[0], bD[0], aD[1], bD[1], aD[2], bD[2], aD[3], bD[3]),
aF[0].linearCombination(aF[0], bF[0], aF[1], bF[1], aF[2], bF[2], aF[3], bF[3]));
}
}
@ -486,7 +487,7 @@ public abstract class ExtendedFieldElementAbstractTest<T extends RealFieldElemen
double[] aD = generateDouble(r, 4);
double[] bD = generateDouble(r, 4);
T[] bF = toFieldArray(bD);
checkRelative(MathArrays.linearCombination(aD[0], bD[0], aD[1], bD[1], aD[2], bD[2], aD[3], bD[3]),
checkRelative(LinearCombination.value(aD[0], bD[0], aD[1], bD[1], aD[2], bD[2], aD[3], bD[3]),
bF[0].linearCombination(aD[0], bF[0], aD[1], bF[1], aD[2], bF[2], aD[3], bF[3]));
}
}

View File

@ -22,6 +22,10 @@ import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.apache.commons.numbers.core.Precision;
import org.apache.commons.numbers.arrays.LinearCombination;
import org.apache.commons.rng.UniformRandomProvider;
import org.apache.commons.rng.simple.RandomSource;
import org.apache.commons.math4.exception.NullArgumentException;
import org.apache.commons.math4.geometry.euclidean.twod.Euclidean2D;
import org.apache.commons.math4.geometry.euclidean.twod.Cartesian2D;
@ -29,11 +33,7 @@ import org.apache.commons.math4.geometry.euclidean.twod.hull.ConvexHull2D;
import org.apache.commons.math4.geometry.euclidean.twod.hull.ConvexHullGenerator2D;
import org.apache.commons.math4.geometry.partitioning.Region;
import org.apache.commons.math4.geometry.partitioning.Region.Location;
import org.apache.commons.rng.UniformRandomProvider;
import org.apache.commons.rng.simple.RandomSource;
import org.apache.commons.math4.util.FastMath;
import org.apache.commons.math4.util.MathArrays;
import org.apache.commons.numbers.core.Precision;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
@ -409,7 +409,7 @@ public abstract class ConvexHullGenerator2DAbstractTest {
Assert.assertTrue(d1.getNorm() > 1e-10);
Assert.assertTrue(d2.getNorm() > 1e-10);
final double cross = MathArrays.linearCombination(d1.getX(), d2.getY(), -d1.getY(), d2.getX());
final double cross = LinearCombination.value(d1.getX(), d2.getY(), -d1.getY(), d2.getX());
final int cmp = Precision.compareTo(cross, 0.0, tolerance);
if (sign != 0 && cmp != sign) {

View File

@ -176,61 +176,6 @@ public class MathArraysTest {
Assert.assertEquals(4, MathArrays.distanceInf(p1, p2));
}
@Test
public void testCosAngle2D() {
double expected;
final double[] v1 = { 1, 0 };
expected = 1;
Assert.assertEquals(expected, MathArrays.cosAngle(v1, v1), 0d);
final double[] v2 = { 0, 1 };
expected = 0;
Assert.assertEquals(expected, MathArrays.cosAngle(v1, v2), 0d);
final double[] v3 = { 7, 7 };
expected = Math.sqrt(2) / 2;
Assert.assertEquals(expected, MathArrays.cosAngle(v1, v3), 1e-15);
Assert.assertEquals(expected, MathArrays.cosAngle(v3, v2), 1e-15);
final double[] v4 = { -5, 0 };
expected = -1;
Assert.assertEquals(expected, MathArrays.cosAngle(v1, v4), 0);
final double[] v5 = { -100, 100 };
expected = 0;
Assert.assertEquals(expected, MathArrays.cosAngle(v3, v5), 0);
}
@Test
public void testCosAngle3D() {
double expected;
final double[] v1 = { 1, 1, 0 };
expected = 1;
Assert.assertEquals(expected, MathArrays.cosAngle(v1, v1), 1e-15);
final double[] v2 = { 1, 1, 1 };
expected = Math.sqrt(2) / Math.sqrt(3);
Assert.assertEquals(expected, MathArrays.cosAngle(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, MathArrays.cosAngle(v1, v2), 1e-15);
final double[] v3 = { big, -big };
expected = 0;
Assert.assertEquals(expected, MathArrays.cosAngle(v1, v3), 1e-15);
}
@Test
public void testCheckOrder() {
MathArrays.checkOrder(new double[] {-15, -5.5, -1, 2, 15},
@ -723,249 +668,6 @@ public class MathArraysTest {
}
}
// MATH-1005
@Test
public void testLinearCombinationWithSingleElementArray() {
final double[] a = { 1.23456789 };
final double[] b = { 98765432.1 };
Assert.assertEquals(a[0] * b[0], MathArrays.linearCombination(a, b), 0d);
}
@Test
public void testLinearCombination1() {
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 double abSumInline = MathArrays.linearCombination(a[0], b[0],
a[1], b[1],
a[2], b[2]);
final double abSumArray = MathArrays.linearCombination(a, b);
Assert.assertEquals(abSumInline, abSumArray, 0);
Assert.assertEquals(-1.8551294182586248737720779899, abSumInline, 1.0e-15);
final double naive = a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
Assert.assertTrue(FastMath.abs(naive - abSumInline) > 1.5);
}
@Test
public void testLinearCombination2() {
// we compare accurate versus naive dot product implementations
// on regular vectors (i.e. not extreme cases like in the previous test)
UniformRandomProvider random = RandomSource.create(RandomSource.XOR_SHIFT_1024_S,
553267312521321234l);
for (int i = 0; i < 10000; ++i) {
final double ux = 1e17 * random.nextDouble();
final double uy = 1e17 * random.nextDouble();
final double uz = 1e17 * random.nextDouble();
final double vx = 1e17 * random.nextDouble();
final double vy = 1e17 * random.nextDouble();
final double vz = 1e17 * random.nextDouble();
final double sInline = MathArrays.linearCombination(ux, vx,
uy, vy,
uz, vz);
final double sArray = MathArrays.linearCombination(new double[] {ux, uy, uz},
new double[] {vx, vy, vz});
Assert.assertEquals(sInline, sArray, 0);
}
}
@Test
public void testLinearCombinationHuge() {
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
};
double[] scaledA = new double[a.length];
double[] scaledB = new double[b.length];
for (int i = 0; i < scaledA.length; ++i) {
scaledA[i] = FastMath.scalb(a[i], -scale);
scaledB[i] = FastMath.scalb(b[i], +scale);
}
final double abSumInline = MathArrays.linearCombination(scaledA[0], scaledB[0],
scaledA[1], scaledB[1],
scaledA[2], scaledB[2]);
final double abSumArray = MathArrays.linearCombination(scaledA, scaledB);
Assert.assertEquals(abSumInline, abSumArray, 0);
Assert.assertEquals(-1.8551294182586248737720779899, abSumInline, 1.0e-15);
final double naive = scaledA[0] * scaledB[0] + scaledA[1] * scaledB[1] + scaledA[2] * scaledB[2];
Assert.assertTrue(FastMath.abs(naive - abSumInline) > 1.5);
}
@Test
public void testLinearCombinationInfinite() {
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,
MathArrays.linearCombination(a[0][0], b[0][0],
a[0][1], b[0][1]),
1.0e-10);
Assert.assertEquals(6,
MathArrays.linearCombination(a[0][0], b[0][0],
a[0][1], b[0][1],
a[0][2], b[0][2]),
1.0e-10);
Assert.assertEquals(22,
MathArrays.linearCombination(a[0][0], b[0][0],
a[0][1], b[0][1],
a[0][2], b[0][2],
a[0][3], b[0][3]),
1.0e-10);
Assert.assertEquals(22, MathArrays.linearCombination(a[0], b[0]), 1.0e-10);
Assert.assertEquals(Double.NEGATIVE_INFINITY,
MathArrays.linearCombination(a[1][0], b[1][0],
a[1][1], b[1][1]),
1.0e-10);
Assert.assertEquals(Double.NEGATIVE_INFINITY,
MathArrays.linearCombination(a[1][0], b[1][0],
a[1][1], b[1][1],
a[1][2], b[1][2]),
1.0e-10);
Assert.assertEquals(Double.NEGATIVE_INFINITY,
MathArrays.linearCombination(a[1][0], b[1][0],
a[1][1], b[1][1],
a[1][2], b[1][2],
a[1][3], b[1][3]),
1.0e-10);
Assert.assertEquals(Double.NEGATIVE_INFINITY, MathArrays.linearCombination(a[1], b[1]), 1.0e-10);
Assert.assertEquals(-3,
MathArrays.linearCombination(a[2][0], b[2][0],
a[2][1], b[2][1]),
1.0e-10);
Assert.assertEquals(Double.POSITIVE_INFINITY,
MathArrays.linearCombination(a[2][0], b[2][0],
a[2][1], b[2][1],
a[2][2], b[2][2]),
1.0e-10);
Assert.assertEquals(Double.POSITIVE_INFINITY,
MathArrays.linearCombination(a[2][0], b[2][0],
a[2][1], b[2][1],
a[2][2], b[2][2],
a[2][3], b[2][3]),
1.0e-10);
Assert.assertEquals(Double.POSITIVE_INFINITY, MathArrays.linearCombination(a[2], b[2]), 1.0e-10);
Assert.assertEquals(Double.NEGATIVE_INFINITY,
MathArrays.linearCombination(a[3][0], b[3][0],
a[3][1], b[3][1]),
1.0e-10);
Assert.assertEquals(Double.NEGATIVE_INFINITY,
MathArrays.linearCombination(a[3][0], b[3][0],
a[3][1], b[3][1],
a[3][2], b[3][2]),
1.0e-10);
Assert.assertEquals(Double.NEGATIVE_INFINITY,
MathArrays.linearCombination(a[3][0], b[3][0],
a[3][1], b[3][1],
a[3][2], b[3][2],
a[3][3], b[3][3]),
1.0e-10);
Assert.assertEquals(Double.NEGATIVE_INFINITY, MathArrays.linearCombination(a[3], b[3]), 1.0e-10);
Assert.assertEquals(Double.POSITIVE_INFINITY,
MathArrays.linearCombination(a[4][0], b[4][0],
a[4][1], b[4][1]),
1.0e-10);
Assert.assertEquals(Double.POSITIVE_INFINITY,
MathArrays.linearCombination(a[4][0], b[4][0],
a[4][1], b[4][1],
a[4][2], b[4][2]),
1.0e-10);
Assert.assertEquals(Double.POSITIVE_INFINITY,
MathArrays.linearCombination(a[4][0], b[4][0],
a[4][1], b[4][1],
a[4][2], b[4][2],
a[4][3], b[4][3]),
1.0e-10);
Assert.assertEquals(Double.POSITIVE_INFINITY, MathArrays.linearCombination(a[4], b[4]), 1.0e-10);
Assert.assertEquals(-3,
MathArrays.linearCombination(a[5][0], b[5][0],
a[5][1], b[5][1]),
1.0e-10);
Assert.assertEquals(Double.POSITIVE_INFINITY,
MathArrays.linearCombination(a[5][0], b[5][0],
a[5][1], b[5][1],
a[5][2], b[5][2]),
1.0e-10);
Assert.assertEquals(Double.POSITIVE_INFINITY,
MathArrays.linearCombination(a[5][0], b[5][0],
a[5][1], b[5][1],
a[5][2], b[5][2],
a[5][3], b[5][3]),
1.0e-10);
Assert.assertEquals(Double.POSITIVE_INFINITY, MathArrays.linearCombination(a[5], b[5]), 1.0e-10);
Assert.assertEquals(Double.POSITIVE_INFINITY,
MathArrays.linearCombination(a[6][0], b[6][0],
a[6][1], b[6][1]),
1.0e-10);
Assert.assertEquals(Double.POSITIVE_INFINITY,
MathArrays.linearCombination(a[6][0], b[6][0],
a[6][1], b[6][1],
a[6][2], b[6][2]),
1.0e-10);
Assert.assertTrue(Double.isNaN(MathArrays.linearCombination(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(MathArrays.linearCombination(a[6], b[6])));
Assert.assertTrue(Double.isNaN(MathArrays.linearCombination(a[7][0], b[7][0],
a[7][1], b[7][1])));
Assert.assertTrue(Double.isNaN(MathArrays.linearCombination(a[7][0], b[7][0],
a[7][1], b[7][1],
a[7][2], b[7][2])));
Assert.assertTrue(Double.isNaN(MathArrays.linearCombination(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(MathArrays.linearCombination(a[7], b[7])));
}
@Test
public void testArrayEquals() {
Assert.assertFalse(MathArrays.equals(new double[] { 1d }, null));