CStandardTest runs with problematic tests commented out (work will continue)
Project: http://git-wip-us.apache.org/repos/asf/commons-numbers/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-numbers/commit/28d6b98f Tree: http://git-wip-us.apache.org/repos/asf/commons-numbers/tree/28d6b98f Diff: http://git-wip-us.apache.org/repos/asf/commons-numbers/diff/28d6b98f Branch: refs/heads/master Commit: 28d6b98f08c89bef488cae2f0d058293151e70ba Parents: 15136c2 Author: Eric Barnhill <ericbarnh...@apache.org> Authored: Fri Jun 16 18:42:40 2017 +0200 Committer: Eric Barnhill <ericbarnh...@apache.org> Committed: Fri Jun 16 18:42:40 2017 +0200 ---------------------------------------------------------------------- .../commons/numbers/complex/.Complex.java.swo | Bin 65536 -> 0 bytes .../apache/commons/numbers/complex/Complex.java | 377 +------------------ .../numbers/complex/.CStandardTest.java.swo | Bin 28672 -> 0 bytes .../commons/numbers/complex/CStandardTest.java | 286 +++++++------- 4 files changed, 161 insertions(+), 502 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/28d6b98f/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/.Complex.java.swo ---------------------------------------------------------------------- diff --git a/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/.Complex.java.swo b/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/.Complex.java.swo deleted file mode 100644 index 7720390..0000000 Binary files a/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/.Complex.java.swo and /dev/null differ http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/28d6b98f/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java ---------------------------------------------------------------------- diff --git a/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java b/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java index f8510a5..2692cc5 100644 --- a/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java +++ b/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java @@ -65,10 +65,6 @@ public class Complex implements Serializable { private final double imaginary; /** The real part. */ private final double real; - /** Record whether this complex number is equal to NaN. */ - private final transient boolean isNaN; - /** Record whether this complex number is infinite. */ - private final transient boolean isInfinite; /** * Create a complex number given only the real part. @@ -88,10 +84,6 @@ public class Complex implements Serializable { public Complex(double real, double imaginary) { this.real = real; this.imaginary = imaginary; - - isNaN = Double.isNaN(real) || Double.isNaN(imaginary); - isInfinite = !isNaN && - (Double.isInfinite(real) || Double.isInfinite(imaginary)); } /** @@ -146,7 +138,7 @@ public class Complex implements Serializable { * @return {@code Complex} projected onto the Riemann sphere. */ public Complex proj() { - if (isInfinite) { + if (Double.isInfinite(real) || Double.isInfinite(imaginary)) { return new Complex(Double.POSITIVE_INFINITY); } else { return this; @@ -163,12 +155,6 @@ public class Complex implements Serializable { * @return the absolute value. */ public double abs() { - if (isNaN) { - return Double.NaN; - } - if (isInfinite()) { - return Double.POSITIVE_INFINITY; - } if (Math.abs(real) < Math.abs(imaginary)) { if (imaginary == 0.0) { return Math.abs(real); @@ -187,11 +173,8 @@ public class Complex implements Serializable { /** * Return the norm of this complex number, defined as the square of the magnitude * (Matches C++ 11 standards.) - * Returns {@code NaN} if either real or imaginary part is {@code NaN} - * and {@code Double.POSITIVE_INFINITY} if neither part is {@code NaN}, - * but at least one part is infinite. * - * @return the absolute value. + * @return the norm. */ public double norm() { return abs()*abs(); @@ -204,10 +187,6 @@ public class Complex implements Serializable { * <p> * {@code (a + bi) + (c + di) = (a+c) + (b+d)i} * </p> - * If either {@code this} or {@code addend} has a {@code NaN} value in - * either part, {@link #NaN} is returned; otherwise {@code Infinite} - * and {@code NaN} values are returned in the parts of the result - * according to the rules for {@link java.lang.Double} arithmetic. * * @param addend Value to be added to this {@code Complex}. * @return {@code this + addend}. @@ -215,10 +194,6 @@ public class Complex implements Serializable { */ public Complex add(Complex addend) { checkNotNull(addend); - if (isNaN || addend.isNaN) { - return NaN; - } - return createComplex(real + addend.getReal(), imaginary + addend.getImaginary()); } @@ -232,32 +207,16 @@ public class Complex implements Serializable { * @see #add(Complex) */ public Complex add(double addend) { - if (isNaN || Double.isNaN(addend)) { - return NaN; - } - return createComplex(real + addend, imaginary); } /** * Returns the conjugate of this complex number. * The conjugate of {@code a + bi} is {@code a - bi}. - * <p> - * {@link #NaN} is returned if either the real or imaginary - * part of this complex number equals {@code Double.NaN}. - * </p><p> - * If the imaginary part is infinite, and the real part is not - * {@code NaN}, the returned value has infinite imaginary part - * of the opposite sign, e.g. the conjugate of - * {@code 1 + POSITIVE_INFINITY i} is {@code 1 - NEGATIVE_INFINITY i}. - * </p> + * * @return the conjugate of this complex object. */ public Complex conjugate() { - if (isNaN) { - return NaN; - } - return createComplex(real, -imaginary); } @@ -291,9 +250,6 @@ public class Complex implements Serializable { * {@code Infinite} and {@code NaN} values are handled according to the * following rules, applied in the order presented: * <ul> - * <li>If either {@code this} or {@code divisor} has a {@code NaN} value - * in either part, {@link #NaN} is returned. - * </li> * <li>If {@code divisor} equals {@link #ZERO}, {@link #NaN} is returned. * </li> * <li>If {@code this} and {@code divisor} are both infinite, @@ -316,9 +272,6 @@ public class Complex implements Serializable { */ public Complex divide(Complex divisor) { checkNotNull(divisor); - if (isNaN || divisor.isNaN) { - return NaN; - } final double c = divisor.getReal(); final double d = divisor.getImaginary(); @@ -326,7 +279,7 @@ public class Complex implements Serializable { return NaN; } - if (divisor.isInfinite() && !isInfinite()) { + if ( (Double.isInfinite(c) || Double.isInfinite(d))&& (Double.isInfinite(real) || Double.isInfinite(imaginary))) { return ZERO; } @@ -352,35 +305,28 @@ public class Complex implements Serializable { * @see #divide(Complex) */ public Complex divide(double divisor) { - if (isNaN || Double.isNaN(divisor)) { - return NaN; - } if (divisor == 0d) { return NaN; } if (Double.isInfinite(divisor)) { - return !isInfinite() ? ZERO : NaN; + return !(Double.isInfinite(real) || Double.isInfinite(imaginary)) ? ZERO : NaN; } return createComplex(real / divisor, imaginary / divisor); } /** - * Returns the multiplicative inverse this instance. + * Returns the multiplicative inverse of this instance. * * @return {@code 1 / this}. * @see #divide(Complex) */ public Complex reciprocal() { - if (isNaN) { - return NaN; - } - if (real == 0.0 && imaginary == 0.0) { return INF; } - if (isInfinite) { + if (Double.isInfinite(real) || Double.isInfinite(imaginary)) { return ZERO; } @@ -426,12 +372,8 @@ public class Complex implements Serializable { } if (other instanceof Complex){ Complex c = (Complex) other; - if (c.isNaN) { - return isNaN; - } else { - return equals(real, c.real) && - equals(imaginary, c.imaginary); - } + return equals(real, c.real) && + equals(imaginary, c.imaginary); } return false; } @@ -517,7 +459,7 @@ public class Complex implements Serializable { */ @Override public int hashCode() { - if (isNaN) { + if (Double.isNaN(real) || Double.isNaN(imaginary)) { return 7; } return 37 * 17 * (hash(imaginary) + @@ -565,30 +507,6 @@ public class Complex implements Serializable { return real; } - /** - * Checks whether either or both parts of this complex number is - * {@code NaN}. - * - * @return true if either or both parts of this complex number is - * {@code NaN}; false otherwise. - */ - public boolean isNaN() { - return isNaN; - } - - /** - * Checks whether either the real or imaginary part of this complex number - * takes an infinite value (either {@code Double.POSITIVE_INFINITY} or - * {@code Double.NEGATIVE_INFINITY}) and neither part - * is {@code NaN}. - * - * @return true if one or both parts of this complex number are infinite - * and neither part is {@code NaN}. - */ - public boolean isInfinite() { - return isInfinite; - } - /** * Returns a {@code Complex} whose value is {@code this * factor}. * Implements preliminary checks for {@code NaN} and infinity followed by @@ -613,16 +531,6 @@ public class Complex implements Serializable { */ public Complex multiply(Complex factor) { checkNotNull(factor); - if (isNaN || factor.isNaN) { - return NaN; - } - if (Double.isInfinite(real) || - Double.isInfinite(imaginary) || - Double.isInfinite(factor.real) || - Double.isInfinite(factor.imaginary)) { - // we don't use isInfinite() to avoid testing for NaN again - return INF; - } return createComplex(real * factor.real - imaginary * factor.imaginary, real * factor.imaginary + imaginary * factor.real); } @@ -636,13 +544,6 @@ public class Complex implements Serializable { * @see #multiply(Complex) */ public Complex multiply(final int factor) { - if (isNaN) { - return NaN; - } - if (Double.isInfinite(real) || - Double.isInfinite(imaginary)) { - return INF; - } return createComplex(real * factor, imaginary * factor); } @@ -655,15 +556,6 @@ public class Complex implements Serializable { * @see #multiply(Complex) */ public Complex multiply(double factor) { - if (isNaN || Double.isNaN(factor)) { - return NaN; - } - if (Double.isInfinite(real) || - Double.isInfinite(imaginary) || - Double.isInfinite(factor)) { - // we don't use isInfinite() to avoid testing for NaN again - return INF; - } return createComplex(real * factor, imaginary * factor); } @@ -675,10 +567,6 @@ public class Complex implements Serializable { * @return {@code -this}. */ public Complex negate() { - if (isNaN) { - return NaN; - } - return createComplex(-real, -imaginary); } @@ -689,20 +577,12 @@ public class Complex implements Serializable { * <p> * {@code (a + bi) - (c + di) = (a-c) + (b-d)i} * </p> - * If either {@code this} or {@code subtrahend} has a {@code NaN]} value in either part, - * {@link #NaN} is returned; otherwise infinite and {@code NaN} values are - * returned in the parts of the result according to the rules for - * {@link java.lang.Double} arithmetic. * * @param subtrahend value to be subtracted from this {@code Complex}. * @return {@code this - subtrahend}. */ public Complex subtract(Complex subtrahend) { checkNotNull(subtrahend); - if (isNaN || subtrahend.isNaN) { - return NaN; - } - return createComplex(real - subtrahend.getReal(), imaginary - subtrahend.getImaginary()); } @@ -716,9 +596,6 @@ public class Complex implements Serializable { * @see #subtract(Complex) */ public Complex subtract(double subtrahend) { - if (isNaN || Double.isNaN(subtrahend)) { - return NaN; - } return createComplex(real - subtrahend, imaginary); } @@ -730,16 +607,10 @@ public class Complex implements Serializable { * <p> * {@code acos(z) = -i (log(z + i (sqrt(1 - z<sup>2</sup>))))} * </p> - * Returns {@link Complex#NaN} if either real or imaginary part of the - * input argument is {@code NaN} or infinite. * * @return the inverse cosine of this complex number. */ public Complex acos() { - if (isNaN) { - return NaN; - } - return this.add(this.sqrt1z().multiply(I)).log().multiply(I.negate()); } @@ -751,16 +622,9 @@ public class Complex implements Serializable { * <p> * {@code asin(z) = -i (log(sqrt(1 - z<sup>2</sup>) + iz))} * </p><p> - * Returns {@link Complex#NaN} if either real or imaginary part of the - * input argument is {@code NaN} or infinite.</p> - * * @return the inverse sine of this complex number. */ public Complex asin() { - if (isNaN) { - return NaN; - } - return sqrt1z().add(this.multiply(I)).log().multiply(I.negate()); } @@ -772,16 +636,9 @@ public class Complex implements Serializable { * <p> * {@code atan(z) = (i/2) log((i + z)/(i - z))} * </p><p> - * Returns {@link Complex#NaN} if either real or imaginary part of the - * input argument is {@code NaN} or infinite.</p> - * * @return the inverse tangent of this complex number */ public Complex atan() { - if (isNaN) { - return NaN; - } - return this.add(I).divide(I.subtract(this)).log() .multiply(I.divide(createComplex(2.0, 0.0))); } @@ -794,17 +651,10 @@ public class Complex implements Serializable { * <p> * {@code asinh(z) = log(z+sqrt(z^2+1))} * </p><p> - * Returns {@link Complex#NaN} if either real or imaginary part of the - * input argument is {@code NaN} or infinite.</p> - * * @return the inverse hyperbolic cosine of this complex number * @since 1.2 */ public Complex asinh(){ - if (isNaN) { - return NaN; - } - return square().add(Complex.ONE).sqrt().add(this).log(); } @@ -816,17 +666,10 @@ public class Complex implements Serializable { * <p> * {@code atanh(z) = log((1+z)/(1-z))/2} * </p><p> - * Returns {@link Complex#NaN} if either real or imaginary part of the - * input argument is {@code NaN} or infinite.</p> - * * @return the inverse hyperbolic cosine of this complex number * @since 1.2 */ public Complex atanh(){ - if (isNaN) { - return NaN; - } - return this.add(Complex.ONE).divide(Complex.ONE.subtract(this)).log().divide(new Complex(2)); } /** @@ -837,17 +680,10 @@ public class Complex implements Serializable { * <p> * {@code acosh(z) = log(z+sqrt(z^2-1))} * </p><p> - * Returns {@link Complex#NaN} if either real or imaginary part of the - * input argument is {@code NaN} or infinite.</p> - * * @return the inverse hyperbolic cosine of this complex number * @since 1.2 */ public Complex acosh() { - if (isNaN) { - return NaN; - } - return square().subtract(Complex.ONE).sqrt().add(this).log(); } @@ -857,10 +693,6 @@ public class Complex implements Serializable { * @return square of this complex number */ public Complex square(){ - if (isNaN) { - return NaN; - } - return this.multiply(this); } @@ -876,27 +708,10 @@ public class Complex implements Serializable { * {@link Math#sin}, {@link Math#cos}, * {@link Math#cosh} and {@link Math#sinh}. * </p><p> - * Returns {@link Complex#NaN} if either real or imaginary part of the - * input argument is {@code NaN}. - * </p><p> - * Infinite values in real or imaginary parts of the input may result in - * infinite or NaN values returned in parts of the result.</p> - * <pre> - * Examples: - * <code> - * cos(1 ± INFINITY i) = 1 \u2213 INFINITY i - * cos(±INFINITY + i) = NaN + NaN i - * cos(±INFINITY ± INFINITY i) = NaN + NaN i - * </code> - * </pre> * * @return the cosine of this complex number. */ public Complex cos() { - if (isNaN) { - return NaN; - } - return createComplex(Math.cos(real) * Math.cosh(imaginary), -Math.sin(real) * Math.sinh(imaginary)); } @@ -915,27 +730,10 @@ public class Complex implements Serializable { * {@link Math#sin}, {@link Math#cos}, * {@link Math#cosh} and {@link Math#sinh}. * <p> - * Returns {@link Complex#NaN} if either real or imaginary part of the - * input argument is {@code NaN}. - * </p> - * Infinite values in real or imaginary parts of the input may result in - * infinite or NaN values returned in parts of the result. - * <pre> - * Examples: - * <code> - * cosh(1 ± INFINITY i) = NaN + NaN i - * cosh(±INFINITY + i) = INFINITY ± INFINITY i - * cosh(±INFINITY ± INFINITY i) = NaN + NaN i - * </code> - * </pre> * * @return the hyperbolic cosine of this complex number. */ public Complex cosh() { - if (isNaN) { - return NaN; - } - return createComplex(Math.cosh(real) * Math.cos(imaginary), Math.sinh(real) * Math.sin(imaginary)); } @@ -953,29 +751,10 @@ public class Complex implements Serializable { * where the (real) functions on the right-hand side are * {@link Math#exp}, {@link Math#cos}, and * {@link Math#sin}. - * <p> - * Returns {@link Complex#NaN} if either real or imaginary part of the - * input argument is {@code NaN}. - * </p> - * Infinite values in real or imaginary parts of the input may result in - * infinite or NaN values returned in parts of the result. - * <pre> - * Examples: - * <code> - * exp(1 ± INFINITY i) = NaN + NaN i - * exp(INFINITY + i) = INFINITY + INFINITY i - * exp(-INFINITY + i) = 0 + 0i - * exp(±INFINITY ± INFINITY i) = NaN + NaN i - * </code> - * </pre> * * @return <code><i>e</i><sup>this</sup></code>. */ public Complex exp() { - if (isNaN) { - return NaN; - } - double expReal = Math.exp(real); return createComplex(expReal * Math.cos(imaginary), expReal * Math.sin(imaginary)); @@ -994,32 +773,11 @@ public class Complex implements Serializable { * where ln on the right hand side is {@link Math#log}, * {@code |a + bi|} is the modulus, {@link Complex#abs}, and * {@code arg(a + bi) = }{@link Math#atan2}(b, a). - * <p> - * Returns {@link Complex#NaN} if either real or imaginary part of the - * input argument is {@code NaN}. - * </p> - * Infinite (or critical) values in real or imaginary parts of the input may - * result in infinite or NaN values returned in parts of the result. - * <pre> - * Examples: - * <code> - * log(1 ± INFINITY i) = INFINITY ± (π/2)i - * log(INFINITY + i) = INFINITY + 0i - * log(-INFINITY + i) = INFINITY + πi - * log(INFINITY ± INFINITY i) = INFINITY ± (π/4)i - * log(-INFINITY ± INFINITY i) = INFINITY ± (3π/4)i - * log(0 + 0i) = -INFINITY + 0i - * </code> - * </pre> * * @return the value <code>ln this</code>, the natural logarithm * of {@code this}. */ public Complex log() { - if (isNaN) { - return NaN; - } - return createComplex(Math.log(abs()), Math.atan2(imaginary, real)); } @@ -1046,10 +804,6 @@ public class Complex implements Serializable { * </pre> * where {@code exp} and {@code log} are {@link #exp} and * {@link #log}, respectively. - * <p> - * Returns {@link Complex#NaN} if either real or imaginary part of the - * input argument is {@code NaN} or infinite, or if {@code y} - * equals {@link Complex#ZERO}.</p> * * @param x exponent to which this {@code Complex} is to be raised. * @return <code> this<sup>x</sup></code>. @@ -1102,28 +856,10 @@ public class Complex implements Serializable { * where the (real) functions on the right-hand side are * {@link Math#sin}, {@link Math#cos}, * {@link Math#cosh} and {@link Math#sinh}. - * <p> - * Returns {@link Complex#NaN} if either real or imaginary part of the - * input argument is {@code NaN}. - * </p><p> - * Infinite values in real or imaginary parts of the input may result in - * infinite or {@code NaN} values returned in parts of the result. - * <pre> - * Examples: - * <code> - * sin(1 ± INFINITY i) = 1 ± INFINITY i - * sin(±INFINITY + i) = NaN + NaN i - * sin(±INFINITY ± INFINITY i) = NaN + NaN i - * </code> - * </pre> * * @return the sine of this complex number. */ public Complex sin() { - if (isNaN) { - return NaN; - } - return createComplex(Math.sin(real) * Math.cosh(imaginary), Math.cos(real) * Math.sinh(imaginary)); } @@ -1141,28 +877,10 @@ public class Complex implements Serializable { * where the (real) functions on the right-hand side are * {@link Math#sin}, {@link Math#cos}, * {@link Math#cosh} and {@link Math#sinh}. - * <p> - * Returns {@link Complex#NaN} if either real or imaginary part of the - * input argument is {@code NaN}. - * </p><p> - * Infinite values in real or imaginary parts of the input may result in - * infinite or NaN values returned in parts of the result. - * <pre> - * Examples: - * <code> - * sinh(1 ± INFINITY i) = NaN + NaN i - * sinh(±INFINITY + i) = ± INFINITY + INFINITY i - * sinh(±INFINITY ± INFINITY i) = NaN + NaN i - * </code> - * </pre> * * @return the hyperbolic sine of {@code this}. */ public Complex sinh() { - if (isNaN) { - return NaN; - } - return createComplex(Math.sinh(real) * Math.cos(imaginary), Math.cosh(real) * Math.sin(imaginary)); } @@ -1181,30 +899,10 @@ public class Complex implements Serializable { * <li>{@code |a + bi| = }{@link Complex#abs}(a + bi)</li> * <li>{@code sign(b) = }{@link Math#copySign(double,double) copySign(1d, b)} * </ul> - * <p> - * Returns {@link Complex#NaN} if either real or imaginary part of the - * input argument is {@code NaN}. - * </p> - * Infinite values in real or imaginary parts of the input may result in - * infinite or NaN values returned in parts of the result. - * <pre> - * Examples: - * <code> - * sqrt(1 ± INFINITY i) = INFINITY + NaN i - * sqrt(INFINITY + i) = INFINITY + 0i - * sqrt(-INFINITY + i) = 0 + INFINITY i - * sqrt(INFINITY ± INFINITY i) = INFINITY + NaN i - * sqrt(-INFINITY ± INFINITY i) = NaN ± INFINITY i - * </code> - * </pre> * * @return the square root of {@code this}. */ public Complex sqrt() { - if (isNaN) { - return NaN; - } - if (real == 0.0 && imaginary == 0.0) { return createComplex(0.0, 0.0); } @@ -1225,12 +923,6 @@ public class Complex implements Serializable { * number. * Computes the result directly as * {@code sqrt(ONE.subtract(z.multiply(z)))}. - * <p> - * Returns {@link Complex#NaN} if either real or imaginary part of the - * input argument is {@code NaN}. - * </p> - * Infinite values in real or imaginary parts of the input may result in - * infinite or NaN values returned in parts of the result. * * @return the square root of <code>1 - this<sup>2</sup></code>. */ @@ -1251,28 +943,10 @@ public class Complex implements Serializable { * where the (real) functions on the right-hand side are * {@link Math#sin}, {@link Math#cos}, {@link Math#cosh} and * {@link Math#sinh}. - * <p> - * Returns {@link Complex#NaN} if either real or imaginary part of the - * input argument is {@code NaN}. - * </p> - * Infinite (or critical) values in real or imaginary parts of the input may - * result in infinite or NaN values returned in parts of the result. - * <pre> - * Examples: - * <code> - * tan(a ± INFINITY i) = 0 ± i - * tan(±INFINITY + bi) = NaN + NaN i - * tan(±INFINITY ± INFINITY i) = NaN + NaN i - * tan(±π/2 + 0 i) = ±INFINITY + NaN i - * </code> - * </pre> * * @return the tangent of {@code this}. */ public Complex tan() { - if (isNaN || Double.isInfinite(real)) { - return NaN; - } if (imaginary > 20.0) { return createComplex(0.0, 1.0); } @@ -1301,28 +975,10 @@ public class Complex implements Serializable { * where the (real) functions on the right-hand side are * {@link Math#sin}, {@link Math#cos}, {@link Math#cosh} and * {@link Math#sinh}. - * <p> - * Returns {@link Complex#NaN} if either real or imaginary part of the - * input argument is {@code NaN}. - * </p> - * Infinite values in real or imaginary parts of the input may result in - * infinite or NaN values returned in parts of the result. - * <pre> - * Examples: - * <code> - * tanh(a ± INFINITY i) = NaN + NaN i - * tanh(±INFINITY + bi) = ±1 + 0 i - * tanh(±INFINITY ± INFINITY i) = NaN + NaN i - * tanh(0 + (π/2)i) = NaN + INFINITY i - * </code> - * </pre> * * @return the hyperbolic tangent of {@code this}. */ public Complex tanh() { - if (isNaN || Double.isInfinite(imaginary)) { - return NaN; - } if (real > 20.0) { return createComplex(1.0, 0.0); } @@ -1396,15 +1052,6 @@ public class Complex implements Serializable { final List<Complex> result = new ArrayList<Complex>(); - if (isNaN) { - result.add(NaN); - return result; - } - if (isInfinite()) { - result.add(INF); - return result; - } - // nth root of abs -- faster / more accurate to use a solver here? final double nthRootOfAbs = Math.pow(abs(), 1.0 / n); @@ -1445,10 +1092,6 @@ public class Complex implements Serializable { */ public static Complex valueOf(double realPart, double imaginaryPart) { - if (Double.isNaN(realPart) || - Double.isNaN(imaginaryPart)) { - return NaN; - } return new Complex(realPart, imaginaryPart); } http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/28d6b98f/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/.CStandardTest.java.swo ---------------------------------------------------------------------- diff --git a/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/.CStandardTest.java.swo b/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/.CStandardTest.java.swo deleted file mode 100644 index 430efd7..0000000 Binary files a/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/.CStandardTest.java.swo and /dev/null differ http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/28d6b98f/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/CStandardTest.java ---------------------------------------------------------------------- diff --git a/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/CStandardTest.java b/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/CStandardTest.java index 88183de..a528170 100644 --- a/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/CStandardTest.java +++ b/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/CStandardTest.java @@ -26,66 +26,82 @@ import org.junit.Test; public class CStandardTest { private double inf = Double.POSITIVE_INFINITY; - private double neginf = Double.NEGATIVE_INFINITY; + private double negInf = Double.NEGATIVE_INFINITY; private double nan = Double.NaN; private double pi = Math.PI; private double piOverFour = Math.PI / 4.0; private double piOverTwo = Math.PI / 2.0; - private double threePiOverFour = 3.0*Math.PI/4.0 + private double threePiOverFour = 3.0*Math.PI/4.0; + private Complex oneOne = new Complex(1, 1); + private Complex oneZero = new Complex(1, 0); private Complex oneInf = new Complex(1, inf); - private Complex oneNegInf = new Complex(1, neginf); - private Complex infOne = new Complex(inf, 1); - private Complex infZero = new Complex(inf, 0); - private Complex infNaN = new Complex(inf, nan); - private Complex infNegInf = new Complex(inf, neginf); - private Complex infInf = new Complex(inf, inf); - private Complex negInfInf = new Complex(neginf, inf); - private Complex negInfZero = new Complex(neginf, 0); - private Complex negInfOne = new Complex(neginf, 1); - private Complex negInfNaN = new Complex(neginf, nan); - private Complex negInfNegInf = new Complex(neginf, neginf); + private Complex oneNegInf = new Complex(1, negInf); private Complex oneNaN = new Complex(1, nan); private Complex zeroInf = new Complex(0, inf); private Complex zeroNaN = new Complex(0, nan); - private Complex nanInf = new Complex(nan, inf); - private Complex nanNegInf = new Complex(nan, neginf); - private Complex nanZero = new Complex(nan, 0); + private Complex zeroPiTwo = new Complex(0.0, piOverTwo); private Complex negZeroZero = new Complex(-0.0, 0); private Complex negZeroNan = new Complex(-0.0, nan); private Complex negI = new Complex(0.0, -1.0); - private Complex zeroPiTwo = new Complex(0.0, piOverTwo); + private Complex infOne = new Complex(inf, 1); + private Complex infZero = new Complex(inf, 0); + private Complex infNaN = new Complex(inf, nan); + private Complex infNegInf = new Complex(inf, negInf); + private Complex infInf = new Complex(inf, inf); + private Complex infPiTwo = new Complex(inf, piOverTwo); + private Complex infPiFour = new Complex(inf, piOverFour); + private Complex infPi = new Complex(inf, Math.PI); + private Complex negInfInf = new Complex(negInf, inf); + private Complex negInfZero = new Complex(negInf, 0); + private Complex negInfOne = new Complex(negInf, 1); + private Complex negInfNaN = new Complex(negInf, nan); + private Complex negInfNegInf = new Complex(negInf, negInf); + private Complex negInfPosInf = new Complex(negInf, inf); + private Complex negInfPi = new Complex(negInf, Math.PI); + private Complex nanInf = new Complex(nan, inf); + private Complex nanNegInf = new Complex(nan, negInf); + private Complex nanZero = new Complex(nan, 0); + private Complex nanOne = new Complex(nan, 1); private Complex piTwoNaN = new Complex(piOverTwo, nan); private Complex piNegInf = new Complex(Math.PI, negInf); private Complex piTwoNegInf = new Complex(piOverTwo, negInf); - private Complex negInfPosInf = new Complex(negInf, inf); private Complex piTwoNegZero = new Complex(piOverTwo, -0.0); private Complex threePiFourNegInf = new Complex(threePiOverFour,negInf); private Complex piFourNegInf = new Complex(piOverFour, negInf); - private Complex infPiTwo = new Complex(inf, piOverTwo); - private Complex infPiFour = new Complex(inf, piOverFour); - private Complex negInfPi = new Complex(negInf, Math.PI); + + public void assertComplex(Complex c1, Complex c2, double realTol, double imagTol) { + Assert.assertEquals(c1.getReal(), c2.getReal(), realTol); + Assert.assertEquals(c1.getImaginary(), c2.getImaginary(), imagTol); + } + + public void assertComplex(Complex c1, Complex c2) { + Assert.assertEquals(c1.getReal(), c2.getReal(),0.0); + Assert.assertEquals(c1.getImaginary(), c2.getImaginary(), 0.0); + } + + /** * ISO C Standard G.6.3 */ @Test - public void testSqrt() { + public void testSqrt1() { Complex z1 = new Complex(-2.0, 0.0); Complex z2 = new Complex(0.0, Math.sqrt(2)); - Assert.assertEquals(z1.sqrt(), z2); + assertComplex(z1.sqrt(), z2); z1 = new Complex(-2.0, -0.0); z2 = new Complex(0.0, -Math.sqrt(2)); - Assert.assertEquals(z1.sqrt(), z2); + assertComplex(z1.sqrt(), z2); } @Test public void testImplicitTrig() { Complex z1 = new Complex(3.0); Complex z2 = new Complex(0.0, 3.0); - Assert.assertEquals(z1.asin(), negI.multiply(z2.asinh())); - Assert.assertEquals(z1.atan(), negI.multiply(z2.atanh())); - Assert.assertEquals(z1.cos(), z2.cosh()); - Assert.assertEquals(z1.sin(), negI.multiply(z2.sinh())); - Assert.assertEquals(z1.tan(), negI.multiply(z1.tanh())); + assertComplex(z1.asin(), negI.multiply(z2.asinh())); + assertComplex(z1.atan(), negI.multiply(z2.atanh()), Math.ulp(1), Math.ulp(1)); + assertComplex(z1.cos(), z2.cosh()); + assertComplex(z1.sin(), negI.multiply(z2.sinh())); + assertComplex(z1.tan(), negI.multiply(z1.tanh())); } /** @@ -93,21 +109,21 @@ public class CStandardTest { */ @Test public void testAcos() { - Assert.assertEquals(oneOne.acos().conj(), oneOne.conj().acos()); - Assert.assertEquals(Complex.ZERO.acos(), piTwoNegZero); - Assert.assertEquals(negZeroZero.acos(), piTwoNegZero); - Assert.assertEquals(zeroNaN.acos(), piTwoNaN); - Assert.assertEquals(oneInf.acos(), piTwoNegInf); - Assert.assertEquals(oneNaN.acos(), Complex.NaN); - Assert.assertEquals(negInfOne.acos(), piNegInf); - Assert.assertEquals(infOne.acos(), zeroInf); - Assert.assertEquals(negInfPosInf.acos(), threePiFourNegInf); - Assert.assertEquals(infInf.acos(), piFourNegInf); - Assert.assertEquals(infNaN.acos(), naNInf); - Assert.assertEquals(negInfNan.acos(), nanNegInf); - Assert.assertEquals(nanOne.acos(), Complex.NaN); - Assert.assertEquals(nanInf.acos(), nanNegInf); - Assert.assertEquals(Complex.NaN.acos(), Complex.NaN); + assertComplex(oneOne.acos().conj(), oneOne.conj().acos(), Math.ulp(1), Math.ulp(1)); + assertComplex(Complex.ZERO.acos(), piTwoNegZero); + assertComplex(negZeroZero.acos(), piTwoNegZero); + assertComplex(zeroNaN.acos(), piTwoNaN); + assertComplex(oneInf.acos(), piTwoNegInf); + assertComplex(oneNaN.acos(), Complex.NaN); + assertComplex(negInfOne.acos(), piNegInf); + assertComplex(infOne.acos(), zeroInf); + assertComplex(negInfPosInf.acos(), threePiFourNegInf); + assertComplex(infInf.acos(), piFourNegInf); + assertComplex(infNaN.acos(), nanInf); + assertComplex(negInfNaN.acos(), nanNegInf); + assertComplex(nanOne.acos(), Complex.NaN); + assertComplex(nanInf.acos(), nanNegInf); + assertComplex(Complex.NaN.acos(), Complex.NaN); } /** @@ -116,17 +132,17 @@ public class CStandardTest { @Test public void testAsinh() { // TODO: test for which Asinh is odd - Assert.assertEquals(oneOne.conj().asinh(), oneOne.asinh().conj()); - Assert.assertEquals(Complex.ZERO.asinh(), Complex.ZERO); - Assert.assertEquals(oneInf.asinh(), infPiTwo); - Assert.assertEquals(oneNaN.asinh(), Complex.NaN); - Assert.assertEquals(infOne.asinh(), infZero); - Assert.assertEquals(infInf.asinh(), infPiFour); - Assert.assertEquals(infNaN.asinh(), z1); - Assert.assertEquals(nanZero.asinh(), nanZero); - Assert.assertEquals(nanOne.asinh(), Complex.NaN); - Assert.assertEquals(nanInf.asinh(), infNan); - Assert.assertEquals(Complex.NaN, Complex.NaN); + assertComplex(oneOne.conj().asinh(), oneOne.asinh().conj()); + assertComplex(Complex.ZERO.asinh(), Complex.ZERO); + assertComplex(oneInf.asinh(), infPiTwo); + assertComplex(oneNaN.asinh(), Complex.NaN); + assertComplex(infOne.asinh(), infZero); + assertComplex(infInf.asinh(), infPiFour); + assertComplex(infNaN.asinh(), infNaN); + assertComplex(nanZero.asinh(), nanZero); + assertComplex(nanOne.asinh(), Complex.NaN); + assertComplex(nanInf.asinh(), infNaN); + assertComplex(Complex.NaN, Complex.NaN); } /** @@ -134,18 +150,18 @@ public class CStandardTest { */ @Test public void testAtanh() { - Assert.assertEquals(oneOne.conj().atanh(), oneOne.atanh().conj()); - Assert.assertEquals(Complex.ZERO.atanh(), Complex.ZERO); - Assert.assertEquals(zeroNaN.atanh(), zeroNaN); - Assert.assertEquals(oneZero.atanh(), infZero); - Assert.assertEquals(oneInf.atanh(),zeroPiTwo); - Assert.assertEquals(oneNaN.atanh(), Complex.NaN); - Assert.assertEquals(infOne.atanh(), zeroPiTwo); - Assert.assertEquals(infInf.atanh(), zeroPiTwo); - Assert.assertEquals(infNaN.atanh(), zeroNaN); - Assert.assertEquals(nanOne.atanh(), Complex.NaN); - Assert.assertEquals(nanInf.atanh(), zeroPiTwo); - Assert.assertEquals(Complex.NaN.atanh(), Complex.NaN); + assertComplex(oneOne.conj().atanh(), oneOne.atanh().conj()); + assertComplex(Complex.ZERO.atanh(), Complex.ZERO); + assertComplex(zeroNaN.atanh(), zeroNaN); + assertComplex(oneZero.atanh(), infZero); + assertComplex(oneInf.atanh(),zeroPiTwo); + assertComplex(oneNaN.atanh(), Complex.NaN); + assertComplex(infOne.atanh(), zeroPiTwo); + assertComplex(infInf.atanh(), zeroPiTwo); + assertComplex(infNaN.atanh(), zeroNaN); + assertComplex(nanOne.atanh(), Complex.NaN); + assertComplex(nanInf.atanh(), zeroPiTwo); + assertComplex(Complex.NaN.atanh(), Complex.NaN); } /** @@ -153,21 +169,21 @@ public class CStandardTest { */ @Test public void testCosh() { - Assert.assertEquals(oneOne.cosh().conj(), oneOne.conj().cosh()); - Assert.assertEquals(Complex.ZERO.cosh(), Complex.ONE); - Assert.assertEquals(zeroInf.cosh(), nanZero); - Assert.assertEquals(zeroNan.cosh(), nanZero); - Assert.assertEquals(oneInf.cosh(), Complex.NaN); - Assert.assertEquals(oneNan.cosh(), Complex.NaN); - Assert.assertEquals(infZero.cosh(), infZero); + assertComplex(oneOne.cosh().conj(), oneOne.conj().cosh()); + assertComplex(Complex.ZERO.cosh(), Complex.ONE); + assertComplex(zeroInf.cosh(), nanZero); + assertComplex(zeroNaN.cosh(), nanZero); + assertComplex(oneInf.cosh(), Complex.NaN); + assertComplex(oneNaN.cosh(), Complex.NaN); + assertComplex(infZero.cosh(), infZero); // the next test does not appear to make sense: // (inf + iy) = inf + cis(y) // skipped - Assert.assertEquals(infInf.cosh(), infNaN); - Assert.assertEquals(infNaN.cosh(), infNaN); - Assert.assertEquals(nanZero.cosh(), nanZero); - Assert.assertEquals(nanOne.cosh(), Complex.NaN); - Assert.assertEquals(Complex.NaN.cosh(), Complex.NaN); + assertComplex(infInf.cosh(), infNaN); + assertComplex(infNaN.cosh(), infNaN); + assertComplex(nanZero.cosh(), nanZero); + assertComplex(nanOne.cosh(), Complex.NaN); + assertComplex(Complex.NaN.cosh(), Complex.NaN); } /** @@ -175,19 +191,19 @@ public class CStandardTest { */ @Test public void testSinh() { - Assert.assertEquals(oneOne.sinh().conj(), oneOne.conj().sinh()); // AND CSINH IS ODD - Assert.assertEquals(Complex.ZERO.sinh(), Complex.ZERO); - Assert.assertEquals(zeroInf.sinh(), zeroNaN); - Assert.assertEquals(zeroNaN.sinh(), zeroNaN); - Assert.assertEquals(oneInf.sinh(), Complex.NaN); - Assert.assertEquals(oneNaN.sinh(), Complex.NaN); - Assert.assertEquals(infZero.sinh(), infZero); + assertComplex(oneOne.sinh().conj(), oneOne.conj().sinh()); // AND CSINH IS ODD + assertComplex(Complex.ZERO.sinh(), Complex.ZERO); + assertComplex(zeroInf.sinh(), zeroNaN); + assertComplex(zeroNaN.sinh(), zeroNaN); + assertComplex(oneInf.sinh(), Complex.NaN); + assertComplex(oneNaN.sinh(), Complex.NaN); + assertComplex(infZero.sinh(), infZero); // skipped test similar to previous section - Assert.assertEquals(infInf.sinh(), infNaN); - Assert.assertEquals(infNaN.sinh(), infNaN); - Assert.assertEquals(nanZero.sinh(), nanZero); - Assert.assertEquals(nanOne.sinh(), Complex.NaN); - Assert.assertEquals(Complex.NaN.sinh(), Complex.NaN); + assertComplex(infInf.sinh(), infNaN); + assertComplex(infNaN.sinh(), infNaN); + assertComplex(nanZero.sinh(), nanZero); + assertComplex(nanOne.sinh(), Complex.NaN); + assertComplex(Complex.NaN.sinh(), Complex.NaN); } /** @@ -195,16 +211,16 @@ public class CStandardTest { */ @Test public void testTanh() { - Assert.assertEquals(oneOne.tanh().conj(), oneOne.conj().tanh()); // AND CSINH IS ODD - Assert.assertEquals(Complex.ZERO.tanh(), Complex.ZERO); - Assert.assertEquals(oneInf.tanh(), Complex.NaN); - Assert.assertEquals(oneNaN.tanh(), Complex.NaN); + assertComplex(oneOne.tanh().conj(), oneOne.conj().tanh()); // AND CSINH IS ODD + assertComplex(Complex.ZERO.tanh(), Complex.ZERO); + assertComplex(oneInf.tanh(), Complex.NaN); + assertComplex(oneNaN.tanh(), Complex.NaN); //Do Not Understand the Next Test - Assert.assertEquals(infInf.tanh(), oneZero); - Assert.assertEquals(infNaN.tanh(), oneZero); - Assert.assertEquals(nanZero.tanh(), nanZero); - Assert.assertEquals(nanOne.tanh(), Complex.NaN); - Assert.assertEquals(Complex.NaN.tanh(), Complex.NaN); + assertComplex(infInf.tanh(), oneZero); + assertComplex(infNaN.tanh(), oneZero); + assertComplex(nanZero.tanh(), nanZero); + assertComplex(nanOne.tanh(), Complex.NaN); + assertComplex(Complex.NaN.tanh(), Complex.NaN); } /** @@ -212,20 +228,20 @@ public class CStandardTest { */ @Test public void testExp() { - Assert.assertEquals(oneOne.conj().exp(), oneOne.exp().conj()); - Assert.assertEquals(Complex.ZERO.exp(), oneZero); - Assert.assertEquals(negZero.exp(), oneZero); - Assert.assertEquals(oneInf.exp(), Complex.NaN); - Assert.assertEquals(oneNaN.exp(), Complex.NaN); - Assert.assertEquals(infZero.exp(), infZero); + assertComplex(oneOne.conj().exp(), oneOne.exp().conj()); + assertComplex(Complex.ZERO.exp(), oneZero); + assertComplex(negZeroZero.exp(), oneZero); + assertComplex(oneInf.exp(), Complex.NaN); + assertComplex(oneNaN.exp(), Complex.NaN); + assertComplex(infZero.exp(), infZero); // Do not understand next test - Assert.assertEquals(negInfInf.exp(), Complex.ZERO); - Assert.assertEquals(infInf.exp(), infNaN); - Assert.assertEquals(negInfNaN.exp(), Complex.ZERO); - Assert.assertEquals(infNaN.exp(), infNaN); - Assert.assertEquals(nanZero.exp(), nanZero); - Assert.assertEquals(nanOne.exp(), Complex.NaN); - Assert.assertEquals(Complex.NaN.exp(), Complex.NaN); + assertComplex(negInfInf.exp(), Complex.ZERO); + assertComplex(infInf.exp(), infNaN); + assertComplex(negInfNaN.exp(), Complex.ZERO); + assertComplex(infNaN.exp(), infNaN); + assertComplex(nanZero.exp(), nanZero); + assertComplex(nanOne.exp(), Complex.NaN); + assertComplex(Complex.NaN.exp(), Complex.NaN); } /** @@ -233,33 +249,33 @@ public class CStandardTest { */ @Test public void testLog() { - Assert.assertEquals(oneOne.log().conj(), oneOne.conj().log()); - Assert.assertEquals(negZeroZero.log(), negInfPi); - Assert.assertEquals(Complex.ZERO.log(), negInfZero); - Assert.assertEquals(oneInf.log(), infPiTwo); - Assert.assertEquals(oneNaN.log(), Complex.NaN); - Assert.assertEquals(negInfOne.log(), infPi); - Assert.assertEquals(infOne.log(), infZero); - Assert.assertEquals(infInf.log(), infPiFour); - Assert.assertEquals(infNaN.log(), infNaN); - Assert.assertEquals(nanOne.log(), Complex.NaN); - Assert.assertEquals(nanInf.log(), infNaN); - Assert.assertEquals(Complex.NaN.log(), Complex.NaN); + assertComplex(oneOne.log().conj(), oneOne.conj().log()); + assertComplex(negZeroZero.log(), negInfPi); + assertComplex(Complex.ZERO.log(), negInfZero); + assertComplex(oneInf.log(), infPiTwo); + assertComplex(oneNaN.log(), Complex.NaN); + assertComplex(negInfOne.log(), infPi); + assertComplex(infOne.log(), infZero); + assertComplex(infInf.log(), infPiFour); + assertComplex(infNaN.log(), infNaN); + assertComplex(nanOne.log(), Complex.NaN); + assertComplex(nanInf.log(), infNaN); + assertComplex(Complex.NaN.log(), Complex.NaN); } /** * ISO C Standard G.6.4.2 */ @Test - public void testSqrt() { - Assert.assertEquals(oneOne.sqrt().conj(), oneOne.conj(), sqrt()); - Assert.assertEquals(Complex.ZERO.sqrt(), Complex.ZERO); - Assert.assertEquals(oneInf.sqrt(), infInf); - Assert.assertEquals(negInfOne.sqrt(), zeroNaN); - Assert.assertEquals(infOne.sqrt(), infZero); - Assert.assertEquals(negInfNaN.sqrt(), nanInf); - Assert.assertEquals(infNaN.sqrt(), infNaN); - Assert.assertEquals(nanOne.sqrt(), Complex.NaN); - Assert.assertEquals(Complex.NaN.sqrt(), Complex.NaN); + public void testSqrt2() { + assertComplex(oneOne.sqrt().conj(), oneOne.conj().sqrt()); + assertComplex(Complex.ZERO.sqrt(), Complex.ZERO); + assertComplex(oneInf.sqrt(), infInf); + assertComplex(negInfOne.sqrt(), zeroNaN); + assertComplex(infOne.sqrt(), infZero); + assertComplex(negInfNaN.sqrt(), nanInf); + assertComplex(infNaN.sqrt(), infNaN); + assertComplex(nanOne.sqrt(), Complex.NaN); + assertComplex(Complex.NaN.sqrt(), Complex.NaN); } }