- * Preconditions: - *

- *
• n >= 0 (otherwise - * IllegalArgumentException is thrown)
• - *
• The result is small enough to fit into a double. The - * largest value of n for which n! < - * Double.MAX_VALUE is 170. If the computed value exceeds - * Double.MAX_VALUE, Double.POSITIVE_INFINITY is returned
• - *
- *

- * - * @param n argument - * @return n! - * @throws IllegalArgumentException if n < 0 + * Compute n!, the + * factorial of {@code n} (the product of the numbers 1 to n), as a + * {@code double}. + * The result should be small enough to fit into a {@code double}: The + * largest {@code n} for which {@code n! < Double.MAX_VALUE} is 170. + * If the computed value exceeds {@code Double.MAX_VALUE}, + * {@code Double.POSITIVE_INFINITY} is returned. + * + * @param n Argument. + * @return {@code n!} + * @throws NotPositiveException if {@code n < 0}. */ public static double factorialDouble(final int n) { if (n < 0) { - throw MathRuntimeException.createIllegalArgumentException( - LocalizedFormats.FACTORIAL_NEGATIVE_PARAMETER, - n); + throw new NotPositiveException(LocalizedFormats.FACTORIAL_NEGATIVE_PARAMETER, + n); } if (n < 21) { return factorial(n); @@ -627,23 +629,16 @@ public final class MathUtils { } /** - * Returns the natural logarithm of n!. - *

- * Preconditions: - *

- *
• n >= 0 (otherwise - * IllegalArgumentException is thrown)
• - *

+ * Compute the natural logarithm of the factorial of {@code n}. * - * @param n argument - * @return n! - * @throws IllegalArgumentException if preconditions are not met. + * @param n Argument. + * @return {@code n!} + * @throws NotPositiveException if {@code n < 0}. */ public static double factorialLog(final int n) { if (n < 0) { - throw MathRuntimeException.createIllegalArgumentException( - LocalizedFormats.FACTORIAL_NEGATIVE_PARAMETER, - n); + throw new NotPositiveException(LocalizedFormats.FACTORIAL_NEGATIVE_PARAMETER, + n); } if (n < 21) { return FastMath.log(factorial(n)); @@ -677,11 +672,11 @@ public final class MathUtils { * 0. * * - * @param p any number - * @param q any number - * @return the greatest common divisor, never negative - * @throws ArithmeticException if the result cannot be represented as a - * nonnegative int value + * @param p Number. + * @param q Number. + * @return the greatest common divisor, never negative. + * @throws MathArithmeticException if the result cannot be represented as + * a non-negative {@code int} value. * @since 1.1 */ public static int gcd(final int p, final int q) { @@ -689,9 +684,8 @@ public final class MathUtils { int v = q; if ((u == 0) || (v == 0)) { if ((u == Integer.MIN_VALUE) || (v == Integer.MIN_VALUE)) { - throw MathRuntimeException.createArithmeticException( - LocalizedFormats.GCD_OVERFLOW_32_BITS, - p, q); + throw new MathArithmeticException(LocalizedFormats.GCD_OVERFLOW_32_BITS, + p, q); } return FastMath.abs(u) + FastMath.abs(v); } @@ -715,9 +709,8 @@ public final class MathUtils { k++; // cast out twos. } if (k == 31) { - throw MathRuntimeException.createArithmeticException( - LocalizedFormats.GCD_OVERFLOW_32_BITS, - p, q); + throw new MathArithmeticException(LocalizedFormats.GCD_OVERFLOW_32_BITS, + p, q); } // B2. Initialize: u and v have been divided by 2^k and at least // one is odd. @@ -766,11 +759,11 @@ public final class MathUtils { * 0L. * * - * @param p any number - * @param q any number - * @return the greatest common divisor, never negative - * @throws ArithmeticException if the result cannot be represented as a nonnegative long - * value + * @param p Number. + * @param q Number. + * @return the greatest common divisor, never negative. + * @throws MathArithmeticException if the result cannot be represented as + * a non-negative {@code long} value. * @since 2.1 */ public static long gcd(final long p, final long q) { @@ -778,9 +771,8 @@ public final class MathUtils { long v = q; if ((u == 0) || (v == 0)) { if ((u == Long.MIN_VALUE) || (v == Long.MIN_VALUE)){ - throw MathRuntimeException.createArithmeticException( - LocalizedFormats.GCD_OVERFLOW_64_BITS, - p, q); + throw new MathArithmeticException(LocalizedFormats.GCD_OVERFLOW_64_BITS, + p, q); } return FastMath.abs(u) + FastMath.abs(v); } @@ -804,9 +796,8 @@ public final class MathUtils { k++; // cast out twos. } if (k == 63) { - throw MathRuntimeException.createArithmeticException( - LocalizedFormats.GCD_OVERFLOW_64_BITS, - p, q); + throw new MathArithmeticException(LocalizedFormats.GCD_OVERFLOW_64_BITS, + p, q); } // B2. Initialize: u and v have been divided by 2^k and at least // one is odd. @@ -940,23 +931,21 @@ public final class MathUtils { * 0 for any x. * * - * @param a any number - * @param b any number - * @return the least common multiple, never negative - * @throws ArithmeticException - * if the result cannot be represented as a nonnegative int - * value + * @param a Number. + * @param b Number. + * @return the least common multiple, never negative. + * @throws MathArithmeticException if the result cannot be represented as + * a non-negative {@code int} value. * @since 1.1 */ public static int lcm(int a, int b) { - if (a==0 || b==0){ + if (a == 0 || b == 0){ return 0; } int lcm = FastMath.abs(mulAndCheck(a / gcd(a, b), b)); if (lcm == Integer.MIN_VALUE) { - throw MathRuntimeException.createArithmeticException( - LocalizedFormats.LCM_OVERFLOW_32_BITS, - a, b); + throw new MathArithmeticException(LocalizedFormats.LCM_OVERFLOW_32_BITS, + a, b); } return lcm; } @@ -976,22 +965,21 @@ public final class MathUtils { * 0L for any x. * * - * @param a any number - * @param b any number - * @return the least common multiple, never negative - * @throws ArithmeticException if the result cannot be represented as a nonnegative long - * value + * @param a Number. + * @param b Number. + * @return the least common multiple, never negative. + * @throws MathArithmeticException if the result cannot be represented + * as a non-negative {@code long} value. * @since 2.1 */ public static long lcm(long a, long b) { - if (a==0 || b==0){ + if (a == 0 || b == 0){ return 0; } long lcm = FastMath.abs(mulAndCheck(a / gcd(a, b), b)); if (lcm == Long.MIN_VALUE){ - throw MathRuntimeException.createArithmeticException( - LocalizedFormats.LCM_OVERFLOW_64_BITS, - a, b); + throw new MathArithmeticException(LocalizedFormats.LCM_OVERFLOW_64_BITS, + a, b); } return lcm; } @@ -1019,17 +1007,17 @@ public final class MathUtils { /** * Multiply two integers, checking for overflow. * - * @param x a factor - * @param y a factor - * @return the product x*y - * @throws ArithmeticException if the result can not be represented as an - * int + * @param x Factor. + * @param y Factor. + * @return the product {@code x * y}. + * @throws MathArithmeticException if the result can not be + * represented as an {@code int}. * @since 1.1 */ public static int mulAndCheck(int x, int y) { long m = ((long)x) * ((long)y); if (m < Integer.MIN_VALUE || m > Integer.MAX_VALUE) { - throw new ArithmeticException("overflow: mul"); + throw new MathArithmeticException(); } return (int)m; } @@ -1037,16 +1025,15 @@ public final class MathUtils { /** * Multiply two long integers, checking for overflow. * - * @param a first value - * @param b second value - * @return the product a * b - * @throws ArithmeticException if the result can not be represented as an - * long + * @param a Factor. + * @param b Factor. + * @return the product {@code a * b}. + * @throws MathArithmeticException if the result can not be represented + * as a {@code long}. * @since 1.2 */ public static long mulAndCheck(long a, long b) { long ret; - String msg = "overflow: multiply"; if (a > b) { // use symmetry to reduce boundary cases ret = mulAndCheck(b, a); @@ -1057,14 +1044,14 @@ public final class MathUtils { if (a >= Long.MAX_VALUE / b) { ret = a * b; } else { - throw new ArithmeticException(msg); + throw new MathArithmeticException(); } } else if (b > 0) { // check for negative overflow with negative a, positive b if (Long.MIN_VALUE / b <= a) { ret = a * b; } else { - throw new ArithmeticException(msg); + throw new MathArithmeticException(); } } else { @@ -1079,7 +1066,7 @@ public final class MathUtils { if (a <= Long.MAX_VALUE / b) { ret = a * b; } else { - throw new ArithmeticException(msg); + throw new MathArithmeticException(); } } else { // assert a == 0 @@ -1160,8 +1147,7 @@ public final class MathUtils { * @throws IllegalArgumentException if the target sum is infinite or NaN * @since 2.1 */ - public static double[] normalizeArray(double[] values, double normalizedSum) - throws ArithmeticException, IllegalArgumentException { + public static double[] normalizeArray(double[] values, double normalizedSum) { if (Double.isInfinite(normalizedSum)) { throw MathRuntimeException.createIllegalArgumentException( LocalizedFormats.NORMALIZE_INFINITE); @@ -1271,15 +1257,15 @@ public final class MathUtils { * determined by the rounding method specified. Rounding methods are defined * in {@link BigDecimal}. * - * @param unscaled the value to round. - * @param sign the sign of the original, scaled value. - * @param roundingMethod the rounding method as defined in - * {@link BigDecimal}. + * @param unscaled Value to round. + * @param sign Sign of the original, scaled value. + * @param roundingMethod Rounding method, as defined in {@link BigDecimal}. * @return the rounded value. * @since 1.1 */ - private static double roundUnscaled(double unscaled, double sign, - int roundingMethod) { + private static double roundUnscaled(double unscaled, + double sign, + int roundingMethod) { switch (roundingMethod) { case BigDecimal.ROUND_CEILING : if (sign == -1) { @@ -1337,24 +1323,23 @@ public final class MathUtils { } case BigDecimal.ROUND_UNNECESSARY : if (unscaled != FastMath.floor(unscaled)) { - throw new ArithmeticException("Inexact result from rounding"); + throw new MathArithmeticException(); } break; case BigDecimal.ROUND_UP : unscaled = FastMath.ceil(FastMath.nextAfter(unscaled, Double.POSITIVE_INFINITY)); break; default : - throw MathRuntimeException.createIllegalArgumentException( - LocalizedFormats.INVALID_ROUNDING_METHOD, - roundingMethod, - "ROUND_CEILING", BigDecimal.ROUND_CEILING, - "ROUND_DOWN", BigDecimal.ROUND_DOWN, - "ROUND_FLOOR", BigDecimal.ROUND_FLOOR, - "ROUND_HALF_DOWN", BigDecimal.ROUND_HALF_DOWN, - "ROUND_HALF_EVEN", BigDecimal.ROUND_HALF_EVEN, - "ROUND_HALF_UP", BigDecimal.ROUND_HALF_UP, - "ROUND_UNNECESSARY", BigDecimal.ROUND_UNNECESSARY, - "ROUND_UP", BigDecimal.ROUND_UP); + throw new MathIllegalArgumentException(LocalizedFormats.INVALID_ROUNDING_METHOD, + roundingMethod, + "ROUND_CEILING", BigDecimal.ROUND_CEILING, + "ROUND_DOWN", BigDecimal.ROUND_DOWN, + "ROUND_FLOOR", BigDecimal.ROUND_FLOOR, + "ROUND_HALF_DOWN", BigDecimal.ROUND_HALF_DOWN, + "ROUND_HALF_EVEN", BigDecimal.ROUND_HALF_EVEN, + "ROUND_HALF_UP", BigDecimal.ROUND_HALF_UP, + "ROUND_UNNECESSARY", BigDecimal.ROUND_UNNECESSARY, + "ROUND_UP", BigDecimal.ROUND_UP); } return unscaled; } @@ -1439,26 +1424,22 @@ public final class MathUtils { } /** - * Returns the sign - * for short value x. - *

- * For a short value x, this method returns (short)(+1) if x > 0, (short)(0) - * if x = 0, and (short)(-1) if x < 0.

+ * Compute the sign + * of the argument. * * @param x the value, a short - * @return (short)(+1), (short)(0), or (short)(-1), depending on the sign of - * x + * @return 1 if {@code x > 0}, 0 if {@code x == 0}, and -1 if {@code x < 0}. */ public static short sign(final short x) { return (x == ZS) ? ZS : (x > ZS) ? PS : NS; } /** - * Returns the - * hyperbolic sine of x. + * Compute the + * hyperbolic sine of the argument. * - * @param x double value for which to find the hyperbolic sine - * @return hyperbolic sine of x + * @param x Value for which to find the hyperbolic sine. + * @return hyperbolic sine of {@code x}. */ public static double sinh(double x) { return (FastMath.exp(x) - FastMath.exp(-x)) / 2.0; @@ -1467,17 +1448,17 @@ public final class MathUtils { /** * Subtract two integers, checking for overflow. * - * @param x the minuend - * @param y the subtrahend - * @return the difference x-y - * @throws ArithmeticException if the result can not be represented as an - * int + * @param x Minuend. + * @param y Subtrahend. + * @return the difference {@code x - y}. + * @throws MathArithmeticException if the result can not be represented + * as an {@code int}. * @since 1.1 */ public static int subAndCheck(int x, int y) { long s = (long)x - (long)y; if (s < Integer.MIN_VALUE || s > Integer.MAX_VALUE) { - throw MathRuntimeException.createArithmeticException(LocalizedFormats.OVERFLOW_IN_SUBTRACTION, x, y); + throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_SUBTRACTION, x, y); } return (int)s; } @@ -1485,11 +1466,11 @@ public final class MathUtils { /** * Subtract two long integers, checking for overflow. * - * @param a first value - * @param b second value - * @return the difference a-b - * @throws ArithmeticException if the result can not be represented as an - * long + * @param a Value. + * @param b Value. + * @return the difference {@code a - b}. + * @throws MathArithmeticException if the result can not be represented as a + * {@code long}. * @since 1.2 */ public static long subAndCheck(long a, long b) { @@ -1510,18 +1491,15 @@ public final class MathUtils { /** * Raise an int to an int power. - * @param k number to raise - * @param e exponent (must be positive or null) + * + * @param k Number to raise. + * @param e Exponent (must be positive or zero). * @return ke - * @exception IllegalArgumentException if e is negative + * @throws NotPositiveException if {@code e < 0}. */ - public static int pow(final int k, int e) - throws IllegalArgumentException { - + public static int pow(final int k, int e) { if (e < 0) { - throw MathRuntimeException.createIllegalArgumentException( - LocalizedFormats.POWER_NEGATIVE_PARAMETERS, - k, e); + throw new NotPositiveException(LocalizedFormats.EXPONENT, e); } int result = 1; @@ -1535,23 +1513,19 @@ public final class MathUtils { } return result; - } /** * Raise an int to a long power. - * @param k number to raise - * @param e exponent (must be positive or null) + * + * @param k Number to raise. + * @param e Exponent (must be positive or zero). * @return ke - * @exception IllegalArgumentException if e is negative + * @throws NotPositiveException if {@code e < 0}. */ - public static int pow(final int k, long e) - throws IllegalArgumentException { - + public static int pow(final int k, long e) { if (e < 0) { - throw MathRuntimeException.createIllegalArgumentException( - LocalizedFormats.POWER_NEGATIVE_PARAMETERS, - k, e); + throw new NotPositiveException(LocalizedFormats.EXPONENT, e); } int result = 1; @@ -1565,23 +1539,19 @@ public final class MathUtils { } return result; - } /** * Raise a long to an int power. - * @param k number to raise - * @param e exponent (must be positive or null) + * + * @param k Number to raise. + * @param e Exponent (must be positive or zero). * @return ke - * @exception IllegalArgumentException if e is negative + * @throws NotPositiveException if {@code e < 0}. */ - public static long pow(final long k, int e) - throws IllegalArgumentException { - + public static long pow(final long k, int e) { if (e < 0) { - throw MathRuntimeException.createIllegalArgumentException( - LocalizedFormats.POWER_NEGATIVE_PARAMETERS, - k, e); + throw new NotPositiveException(LocalizedFormats.EXPONENT, e); } long result = 1l; @@ -1595,23 +1565,19 @@ public final class MathUtils { } return result; - } /** * Raise a long to a long power. - * @param k number to raise - * @param e exponent (must be positive or null) + * + * @param k Number to raise. + * @param e Exponent (must be positive or zero). * @return ke - * @exception IllegalArgumentException if e is negative + * @throws NotPositiveException if {@code e < 0}. */ - public static long pow(final long k, long e) - throws IllegalArgumentException { - + public static long pow(final long k, long e) { if (e < 0) { - throw MathRuntimeException.createIllegalArgumentException( - LocalizedFormats.POWER_NEGATIVE_PARAMETERS, - k, e); + throw new NotPositiveException(LocalizedFormats.EXPONENT, e); } long result = 1l; @@ -1625,43 +1591,35 @@ public final class MathUtils { } return result; - } /** * Raise a BigInteger to an int power. - * @param k number to raise - * @param e exponent (must be positive or null) + * + * @param k Number to raise. + * @param e Exponent (must be positive or zero). * @return ke - * @exception IllegalArgumentException if e is negative + * @throws NotPositiveException if {@code e < 0}. */ - public static BigInteger pow(final BigInteger k, int e) - throws IllegalArgumentException { - + public static BigInteger pow(final BigInteger k, int e) { if (e < 0) { - throw MathRuntimeException.createIllegalArgumentException( - LocalizedFormats.POWER_NEGATIVE_PARAMETERS, - k, e); + throw new NotPositiveException(LocalizedFormats.EXPONENT, e); } return k.pow(e); - } /** * Raise a BigInteger to a long power. - * @param k number to raise - * @param e exponent (must be positive or null) + * + * @param k Number to raise. + * @param e Exponent (must be positive or zero). * @return ke - * @exception IllegalArgumentException if e is negative + * @throws NotPositiveException if {@code e < 0}. */ - public static BigInteger pow(final BigInteger k, long e) - throws IllegalArgumentException { - + public static BigInteger pow(final BigInteger k, long e) { if (e < 0) { - throw MathRuntimeException.createIllegalArgumentException( - LocalizedFormats.POWER_NEGATIVE_PARAMETERS, - k, e); + throw new NotPositiveException(LocalizedFormats.EXPONENT, e); } BigInteger result = BigInteger.ONE; @@ -1680,18 +1638,15 @@ public final class MathUtils { /** * Raise a BigInteger to a BigInteger power. - * @param k number to raise - * @param e exponent (must be positive or null) + * + * @param k Number to raise. + * @param e Exponent (must be positive or zero). * @return ke - * @exception IllegalArgumentException if e is negative + * @throws NotPositiveException if {@code e < 0}. */ - public static BigInteger pow(final BigInteger k, BigInteger e) - throws IllegalArgumentException { - + public static BigInteger pow(final BigInteger k, BigInteger e) { if (e.compareTo(BigInteger.ZERO) < 0) { - throw MathRuntimeException.createIllegalArgumentException( - LocalizedFormats.POWER_NEGATIVE_PARAMETERS, - k, e); + throw new NotPositiveException(LocalizedFormats.EXPONENT, e); } BigInteger result = BigInteger.ONE; @@ -1705,7 +1660,6 @@ public final class MathUtils { } return result; - } /** @@ -1816,9 +1770,13 @@ public final class MathUtils { * @param val Values. * @param dir Ordering direction. * @param strict Whether the order should be strict. - * @throws NonMonotonousSequenceException if the array is not sorted. + * @param abort Whether to throw an exception if the check fails. + * @return {@code true} if the array is sorted. + * @throws NonMonotonousSequenceException if the array is not sorted + * and {@code abort} is {@code true}. */ - public static void checkOrder(double[] val, OrderDirection dir, boolean strict) { + public static boolean checkOrder(double[] val, OrderDirection dir, + boolean strict, boolean abort) { double previous = val[0]; boolean ok = true; @@ -1852,21 +1810,40 @@ public final class MathUtils { throw new IllegalArgumentException(); } - if (!ok) { + if (!ok && + abort) { throw new NonMonotonousSequenceException(val[i], previous, i, dir, strict); } previous = val[i]; } + + return ok; + } + + /** + * Checks that the given array is sorted. + * + * @param val Values. + * @param dir Ordering direction. + * @param strict Whether the order should be strict. + * @return {@code true} if the array is sorted. + * @throws NonMonotonousSequenceException if the array is not sorted + * and {@code abort} is {@code true}. + */ + public static boolean checkOrder(double[] val, OrderDirection dir, + boolean strict) { + return checkOrder(val, dir, strict, true); } /** * Checks that the given array is sorted in strictly increasing order. * * @param val Values. + * @return {@code true} if the array is sorted. * @throws NonMonotonousSequenceException if the array is not sorted. */ - public static void checkOrder(double[] val) { - checkOrder(val, OrderDirection.INCREASING, true); + public static boolean checkOrder(double[] val) { + return checkOrder(val, OrderDirection.INCREASING, true); } /** @@ -1929,58 +1906,139 @@ public final class MathUtils { * @return the 2-norm of the vector */ public static double safeNorm(double[] v) { - double rdwarf = 3.834e-20; - double rgiant = 1.304e+19; - double s1=0.0; - double s2=0.0; - double s3=0.0; - double x1max = 0.0; - double x3max = 0.0; - double floatn = (double)v.length; - double agiant = rgiant/floatn; - for (int i=0;iagiant) { - if (xabs>rdwarf) { - if (xabs>x1max) { - double r=x1max/xabs; - s1=1.0+s1*r*r; - x1max=xabs; - } else { - double r=xabs/x1max; - s1+=r*r; - } - } else { - if (xabs>x3max) { - double r=x3max/xabs; - s3=1.0+s3*r*r; - x3max=xabs; + 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 = (double) v.length; + double agiant = rgiant / floatn; + for (int i = 0; i < v.length; i++) { + double xabs = Math.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!=0.0) { - double r=xabs/x3max; - s3+=r*r; + 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; } - } else { - s2+=xabs*xabs; } - } - double norm; - if (s1!=0.0) { - norm = x1max*Math.sqrt(s1+(s2/x1max)/x1max); - } else { - if (s2==0.0) { - norm = x3max*Math.sqrt(s3); + double norm; + if (s1 != 0) { + norm = x1max * Math.sqrt(s1 + (s2 / x1max) / x1max); } else { - if (s2>=x3max) { - norm = Math.sqrt(s2*(1.0+(x3max/s2)*(x3max*s3))); + if (s2 == 0) { + norm = x3max * Math.sqrt(s3); } else { - norm = Math.sqrt(x3max*((s2/x3max)+(x3max*s3))); + if (s2 >= x3max) { + norm = Math.sqrt(s2 * (1 + (x3max / s2) * (x3max * s3))); + } else { + norm = Math.sqrt(x3max * ((s2 / x3max) + (x3max * s3))); + } } } + return norm; } - return norm; -} + /** + * Sort an array in increasing order, performing the same reordering of + * entries on other arrays. + * + * @param x Array to be sorted. + * @param yList Set of arrays whose permutations of entries must follow + * those performed on {@code x}. + * @throws DimensionMismatchException if any {@code y} has not the same + * size as {@code x}. + */ + public static void sortInPlace(double[] x, + double[] ... yList) { + sortInPlace(x, OrderDirection.INCREASING, yList); + } + + /** + * Sort an array, performing the same reordering of entries on other arrays. + * + * @param x Array to be sorted. + * @param dir Order direction. + * @param yList Set of arrays whose permutations of entries must follow + * those performed on {@code x}. + * @throws DimensionMismatchException if any {@code y} has not the same + * size as {@code x}. + */ + public static void sortInPlace(double[] x, + final OrderDirection dir, + double[] ... yList) { + if (x == null || + yList == null) { + throw new NullArgumentException(); + } + + final int len = x.length; + final List> list + = new ArrayList>(len); + + final int yListLen = yList.length; + for (int i = 0; i < len; i++) { + final double[] yValues = new double[yListLen]; + for (int j = 0; j < yListLen; j++) { + double[] y = yList[j]; + if (y.length != len) { + throw new DimensionMismatchException(y.length, len); + } + yValues[j] = y[i]; + } + list.add(new AbstractMap.SimpleEntry(x[i], yValues)); + } + + final Comparator> comp + = new Comparator>() { + public int compare(Map.Entry o1, + Map.Entry o2) { + int val; + switch (dir) { + case INCREASING: + val = o1.getKey().compareTo(o2.getKey()); + break; + case DECREASING: + val = o2.getKey().compareTo(o1.getKey()); + break; + default: + // Should never happen. + throw new IllegalArgumentException(); + } + return val; + } + }; + + Collections.sort(list, comp); + + for (int i = 0; i < len; i++) { + final Map.Entry e = list.get(i); + x[i] = e.getKey(); + final double[] yValues = e.getValue(); + for (int j = 0; j < yListLen; j++) { + yList[j][i] = yValues[j]; + } + } + } } Modified: commons/proper/math/trunk/src/main/resources/META-INF/localization/LocalizedFormats_fr.properties URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/resources/META-INF/localization/LocalizedFormats_fr.properties?rev=1030464&r1=1030463&r2=1030464&view=diff ============================================================================== --- commons/proper/math/trunk/src/main/resources/META-INF/localization/LocalizedFormats_fr.properties (original) +++ commons/proper/math/trunk/src/main/resources/META-INF/localization/LocalizedFormats_fr.properties Wed Nov 3 13:46:04 2010 @@ -20,8 +20,8 @@ ASSYMETRIC_EIGEN_NOT_SUPPORTED = la d\u0 AT_LEAST_ONE_COLUMN = une matrice doit comporter au moins une colonne AT_LEAST_ONE_ROW = une matrice doit comporter au moins une ligne BANDWIDTH_OUT_OF_INTERVAL = la bande passante devrait \u00eatre dans l''intervalle [0,1], elle est de {0} -BINOMIAL_INVALID_PARAMETERS_ORDER = n doit \u00eatre sup\u00e9rieur ou \u00e9gal \u00e0 k pour le coefficient du bin\u00f4me (n,k), or n = {0}, k = {1} -BINOMIAL_NEGATIVE_PARAMETER = n doit \u00eatre positif pour le coefficient du bin\u00f4me (n,k), or n = {0} +BINOMIAL_INVALID_PARAMETERS_ORDER = n doit \u00eatre sup\u00e9rieur ou \u00e9gal \u00e0 k pour le coefficient du bin\u00f4me (n, k), or k = {0}, n = {1} +BINOMIAL_NEGATIVE_PARAMETER = n doit \u00eatre positif pour le coefficient du bin\u00f4me (n, k), or n = {0} CANNOT_CLEAR_STATISTIC_CONSTRUCTED_FROM_EXTERNAL_MOMENTS = les statistiques bas\u00e9es sur des moments externes ne peuvent pas \u00eatre remises \u00e0 z\u00e9ro CANNOT_COMPUTE_0TH_ROOT_OF_UNITY = impossible de calculer la racine z\u00e9roi\u00e8me de l''unit\u00e9, CANNOT_COMPUTE_BETA_DENSITY_AT_0_FOR_SOME_ALPHA = impossible de calculer la densit\u00e9 beta en 0 lorsque alpha = {0,number} @@ -67,6 +67,7 @@ ENDPOINTS_NOT_AN_INTERVAL = les bornes n EQUAL_VERTICES_IN_SIMPLEX = sommets {0} et {1} \u00e9gaux dans la configuration du simplex EULER_ANGLES_SINGULARITY = singularit\u00e9 d''angles d''Euler EVALUATION_FAILED = erreur d''\u00e9valuation pour l''argument {0} +EVALUATION = \u00e9valuation EXPANSION_FACTOR_SMALLER_THAN_ONE = facteur d''extension inf\u00e9rieur \u00e0 un ({0}) FACTORIAL_NEGATIVE_PARAMETER = n doit \u00eatre positif pour le calcul de n!, or n = {0} FAILED_BRACKETING = nombre d''it\u00e9rations = {0}, it\u00e9rations maximum = {1}, valeur initiale = {2}, borne inf\u00e9rieure = {3}, borne sup\u00e9rieure = {4}, valeur a finale = {5}, valeur b finale = {6}, f(a) = {7}, f(b) = {8} Modified: commons/proper/math/trunk/src/site/xdoc/changes.xml URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/site/xdoc/changes.xml?rev=1030464&r1=1030463&r2=1030464&view=diff ============================================================================== --- commons/proper/math/trunk/src/site/xdoc/changes.xml (original) +++ commons/proper/math/trunk/src/site/xdoc/changes.xml Wed Nov 3 13:46:04 2010 @@ -60,7 +60,11 @@ The type attribute can be add,u classes were deprecated and removed. - Replaced old exceptions. + Created "MatrixDimensionMismatchException" to replace + "InvalidMatrixException" (when the latter is used to signal that + matrices are not compatible for some operation). + Replaced "MatrixIndexException" with "OutOfRangeException" (when the + former is used to signal a row or column dimension mismatch). Made "sample" methods part of the "IntegerDistribution" and @@ -70,8 +74,9 @@ The type attribute can be add,u All distribution classes (in package "distribution") are immutable. - Created "MatrixDimensionMismatch" to replace "InvalidMatrixException" - (when the latter is used to signal a row or column dimension mismatch). + Created an unchecked "FunctionEvaluationException". + Modified the "UnivariateRealFunction" interface: Removed the checked + "FunctionEvaluationException" from the signature of the "value" method. Modified semantics: "equals" methods now consider that NaNs are not Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/BinaryFunctionTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/BinaryFunctionTest.java?rev=1030464&r1=1030463&r2=1030464&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/BinaryFunctionTest.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/BinaryFunctionTest.java Wed Nov 3 13:46:04 2010 @@ -16,7 +16,7 @@ */ package org.apache.commons.math.analysis; -import org.apache.commons.math.FunctionEvaluationException; +import org.apache.commons.math.exception.FunctionEvaluationException; import org.apache.commons.math.util.FastMath; import org.junit.Assert; import org.junit.Test; Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/ComposableFunctionTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/ComposableFunctionTest.java?rev=1030464&r1=1030463&r2=1030464&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/ComposableFunctionTest.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/ComposableFunctionTest.java Wed Nov 3 13:46:04 2010 @@ -16,7 +16,7 @@ */ package org.apache.commons.math.analysis; -import org.apache.commons.math.FunctionEvaluationException; +import org.apache.commons.math.exception.FunctionEvaluationException; import org.apache.commons.math.util.FastMath; import org.junit.Assert; import org.junit.Test; Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/Expm1Function.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/Expm1Function.java?rev=1030464&r1=1030463&r2=1030464&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/Expm1Function.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/Expm1Function.java Wed Nov 3 13:46:04 2010 @@ -16,7 +16,7 @@ */ package org.apache.commons.math.analysis; -import org.apache.commons.math.FunctionEvaluationException; +import org.apache.commons.math.exception.FunctionEvaluationException; import org.apache.commons.math.util.FastMath; /** Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/MonitoredFunction.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/MonitoredFunction.java?rev=1030464&r1=1030463&r2=1030464&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/MonitoredFunction.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/MonitoredFunction.java Wed Nov 3 13:46:04 2010 @@ -16,7 +16,7 @@ */ package org.apache.commons.math.analysis; -import org.apache.commons.math.FunctionEvaluationException; +import org.apache.commons.math.exception.FunctionEvaluationException; /** * Wrapper class for counting functions calls. Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/QuinticFunction.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/QuinticFunction.java?rev=1030464&r1=1030463&r2=1030464&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/QuinticFunction.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/QuinticFunction.java Wed Nov 3 13:46:04 2010 @@ -16,7 +16,7 @@ */ package org.apache.commons.math.analysis; -import org.apache.commons.math.FunctionEvaluationException; +import org.apache.commons.math.exception.FunctionEvaluationException; /** * Auxiliary class for testing solvers. Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/SinFunction.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/SinFunction.java?rev=1030464&r1=1030463&r2=1030464&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/SinFunction.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/SinFunction.java Wed Nov 3 13:46:04 2010 @@ -16,7 +16,7 @@ */ package org.apache.commons.math.analysis; -import org.apache.commons.math.FunctionEvaluationException; +import org.apache.commons.math.exception.FunctionEvaluationException; import org.apache.commons.math.util.FastMath; /** Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/SumSincFunction.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/SumSincFunction.java?rev=1030464&r1=1030463&r2=1030464&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/SumSincFunction.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/SumSincFunction.java Wed Nov 3 13:46:04 2010 @@ -16,7 +16,7 @@ */ package org.apache.commons.math.analysis; -import org.apache.commons.math.FunctionEvaluationException; +import org.apache.commons.math.exception.FunctionEvaluationException; /** * Auxiliary class for testing optimizers. Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/integration/LegendreGaussIntegratorTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/integration/LegendreGaussIntegratorTest.java?rev=1030464&r1=1030463&r2=1030464&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/integration/LegendreGaussIntegratorTest.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/integration/LegendreGaussIntegratorTest.java Wed Nov 3 13:46:04 2010 @@ -19,7 +19,7 @@ package org.apache.commons.math.analysis import java.util.Random; import org.apache.commons.math.ConvergenceException; -import org.apache.commons.math.FunctionEvaluationException; +import org.apache.commons.math.exception.FunctionEvaluationException; import org.apache.commons.math.MathException; import org.apache.commons.math.analysis.QuinticFunction; import org.apache.commons.math.analysis.SinFunction; Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/interpolation/DividedDifferenceInterpolatorTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/interpolation/DividedDifferenceInterpolatorTest.java?rev=1030464&r1=1030463&r2=1030464&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/interpolation/DividedDifferenceInterpolatorTest.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/interpolation/DividedDifferenceInterpolatorTest.java Wed Nov 3 13:46:04 2010 @@ -17,6 +17,7 @@ package org.apache.commons.math.analysis.interpolation; import org.apache.commons.math.MathException; +import org.apache.commons.math.exception.NonMonotonousSequenceException; import org.apache.commons.math.analysis.Expm1Function; import org.apache.commons.math.analysis.SinFunction; import org.apache.commons.math.analysis.UnivariateRealFunction; @@ -118,8 +119,8 @@ public final class DividedDifferenceInte double y[] = { 0.0, 4.0, 4.0, 2.5 }; UnivariateRealFunction p = interpolator.interpolate(x, y); p.value(0.0); - fail("Expecting MathException - bad abscissas array"); - } catch (MathException ex) { + fail("Expecting NonMonotonousSequenceException - bad abscissas array"); + } catch (NonMonotonousSequenceException ex) { // expected } } Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/interpolation/NevilleInterpolatorTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/interpolation/NevilleInterpolatorTest.java?rev=1030464&r1=1030463&r2=1030464&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/interpolation/NevilleInterpolatorTest.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/interpolation/NevilleInterpolatorTest.java Wed Nov 3 13:46:04 2010 @@ -17,6 +17,7 @@ package org.apache.commons.math.analysis.interpolation; import org.apache.commons.math.MathException; +import org.apache.commons.math.exception.NonMonotonousSequenceException; import org.apache.commons.math.analysis.Expm1Function; import org.apache.commons.math.analysis.SinFunction; import org.apache.commons.math.analysis.UnivariateRealFunction; @@ -118,8 +119,8 @@ public final class NevilleInterpolatorTe double y[] = { 0.0, 4.0, 4.0, 2.5 }; UnivariateRealFunction p = interpolator.interpolate(x, y); p.value(0.0); - fail("Expecting MathException - bad abscissas array"); - } catch (MathException ex) { + fail("Expecting NonMonotonousSequenceException - bad abscissas array"); + } catch (NonMonotonousSequenceException ex) { // expected } } Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/polynomials/PolynomialSplineFunctionTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/polynomials/PolynomialSplineFunctionTest.java?rev=1030464&r1=1030463&r2=1030464&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/polynomials/PolynomialSplineFunctionTest.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/polynomials/PolynomialSplineFunctionTest.java Wed Nov 3 13:46:04 2010 @@ -19,7 +19,7 @@ package org.apache.commons.math.analysis import java.util.Arrays; import junit.framework.TestCase; -import org.apache.commons.math.FunctionEvaluationException; +import org.apache.commons.math.exception.OutOfRangeException; import org.apache.commons.math.analysis.UnivariateRealFunction; /** @@ -116,15 +116,15 @@ public class PolynomialSplineFunctionTes try { //outside of domain -- under min x = spline.value(-1.5); - fail("Expecting IllegalArgumentException"); - } catch (FunctionEvaluationException ex) { + fail("Expecting OutOfRangeException"); + } catch (OutOfRangeException ex) { // expected } try { //outside of domain -- over max x = spline.value(2.5); - fail("Expecting IllegalArgumentException"); - } catch (FunctionEvaluationException ex) { + fail("Expecting OutOfRangeException"); + } catch (OutOfRangeException ex) { // expected } } Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/ArrayRealVectorTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/ArrayRealVectorTest.java?rev=1030464&r1=1030463&r2=1030464&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/ArrayRealVectorTest.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/ArrayRealVectorTest.java Wed Nov 3 13:46:04 2010 @@ -21,7 +21,7 @@ import java.util.Iterator; import junit.framework.TestCase; -import org.apache.commons.math.FunctionEvaluationException; +import org.apache.commons.math.exception.FunctionEvaluationException; import org.apache.commons.math.TestUtils; import org.apache.commons.math.analysis.UnivariateRealFunction; import org.apache.commons.math.util.FastMath;