Author: psteitz Date: Sun Nov 27 12:55:08 2005 New Revision: 349309 URL: http://svn.apache.org/viewcvs?rev=349309&view=rev Log: Added some tests, javadoc fixes.
Modified: jakarta/commons/proper/math/branches/MATH_1_1/src/java/org/apache/commons/math/complex/Complex.java jakarta/commons/proper/math/branches/MATH_1_1/src/java/org/apache/commons/math/complex/ComplexUtils.java jakarta/commons/proper/math/branches/MATH_1_1/src/test/org/apache/commons/math/complex/ComplexUtilsTest.java Modified: jakarta/commons/proper/math/branches/MATH_1_1/src/java/org/apache/commons/math/complex/Complex.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/math/branches/MATH_1_1/src/java/org/apache/commons/math/complex/Complex.java?rev=349309&r1=349308&r2=349309&view=diff ============================================================================== --- jakarta/commons/proper/math/branches/MATH_1_1/src/java/org/apache/commons/math/complex/Complex.java (original) +++ jakarta/commons/proper/math/branches/MATH_1_1/src/java/org/apache/commons/math/complex/Complex.java Sun Nov 27 12:55:08 2005 @@ -41,10 +41,10 @@ /** Serializable version identifier */ private static final long serialVersionUID = -6530173849413811929L; - /** The square root of -1. A number representing "0.0 + 1.0i".*/ + /** The square root of -1. A number representing "0.0 + 1.0i" */ public static final Complex I = new Complex(0.0, 1.0); - /** A complex number analogous to [EMAIL PROTECTED] java.lang.Double#NaN} */ + /** A complex number representing "NaN + NaNi" */ public static final Complex NaN = new Complex(Double.NaN, Double.NaN); /** A complex number representing "1.0 + 0.0i" */ @@ -53,17 +53,17 @@ /** A complex number representing "0.0 + 0.0i" */ public static final Complex ZERO = new Complex(0.0, 0.0); - /** The imaginary part. */ + /** The imaginary part */ protected double imaginary; - /** The real part. */ + /** The real part */ protected double real; /** * Create a complex number given the real and imaginary parts. * - * @param real the real part. - * @param imaginary the imaginary part. + * @param real the real part + * @param imaginary the imaginary part */ public Complex(double real, double imaginary) { super(); @@ -79,7 +79,7 @@ * neither part is <code>NaN</code>, but at least one part takes an infinite * value. * - * @return the absolute value. + * @return the absolute value */ public double abs() { if (isNaN()) { @@ -117,11 +117,10 @@ * [EMAIL PROTECTED] #NaN} is returned; otherwise Inifinite and NaN values are * returned in the parts of the result according to the rules for * [EMAIL PROTECTED] java.lang.Double} arithmetic. - * <p> - * Throws <code>NullPointerException</code> if <code>rhs</code> is null. * - * @param rhs the other complex number. - * @return the complex number sum. + * @param rhs the other complex number + * @return the complex number sum + * @throws NullPointerException if <code>rhs</code> is null */ public Complex add(Complex rhs) { return new Complex(real + rhs.getReal(), @@ -179,10 +178,10 @@ * returned in the parts of the result if the [EMAIL PROTECTED] java.lang.Double} * rules applied to the definitional formula force NaN results.</li> * </ul> - * Throws <code>NullPointerException</code> if <code>rhs</code> is null. * * @param rhs the other complex number * @return the complex number quotient + * @throws NullPointerException if <code>rhs</code> is null */ public Complex divide(Complex rhs) { if (isNaN() || rhs.isNaN()) { @@ -281,7 +280,7 @@ /** * Access the imaginary part. * - * @return the imaginary part. + * @return the imaginary part */ public double getImaginary() { return imaginary; @@ -290,18 +289,18 @@ /** * Access the real part. * - * @return the real part. + * @return the real part */ public double getReal() { return real; } /** - * Returns true if this complex number is equal to the special - * Not-a-Number (NaN) value. + * Returns true if either or both parts of this complex number is NaN; + * false otherwise * - * @return true if either or both parts of this complex number take - * NaN values; false otherwise. + * @return true if either or both parts of this complex number is NaN; + * false otherwise */ public boolean isNaN() { return Double.isNaN(real) || Double.isNaN(imaginary); @@ -332,7 +331,7 @@ * Returns [EMAIL PROTECTED] #NaN} if either this or <code>rhs</code> has one or more * NaN parts. * <p> - * Returns NaN or infintite values in components of the result per the + * Returns NaN or infinite values in components of the result per the * definitional formula and and the rules for [EMAIL PROTECTED] java.lang.Double} * arithmetic. Examples: * <pre><code> @@ -341,10 +340,9 @@ * (-INF + -INFi)(1 + NaNi) = NaN + NaNi * </code></pre> * - * Throws <code>NullPointerException</code> if <code>rhs</code> is null. - * - * @param rhs the other complex number. - * @return the complex number product. + * @param rhs the other complex number + * @return the complex number product + * @throws NullPointerException if <code>rhs</code> is null */ public Complex multiply(Complex rhs) { if (isNaN() || rhs.isNaN()) { @@ -360,7 +358,7 @@ * Returns <code>Complex.NaN</code> if either real or imaginary * part of this Complex number equals <code>Double.NaN</code>. * - * @return the negation of this complex number. + * @return the negation of this complex number */ public Complex negate() { if (isNaN()) { @@ -383,11 +381,10 @@ * [EMAIL PROTECTED] #NaN} is returned; otherwise inifinite and NaN values are * returned in the parts of the result according to the rules for * [EMAIL PROTECTED] java.lang.Double} arithmetic. - * <p> - * Throws <code>NullPointerException</code> if <code>rhs</code> is null. * - * @param rhs the other complex number. - * @return the complex number difference. + * @param rhs the other complex number + * @return the complex number difference + * @throws NullPointerException if <code>rhs</code> is null */ public Complex subtract(Complex rhs) { if (isNaN() || rhs.isNaN()) { Modified: jakarta/commons/proper/math/branches/MATH_1_1/src/java/org/apache/commons/math/complex/ComplexUtils.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/math/branches/MATH_1_1/src/java/org/apache/commons/math/complex/ComplexUtils.java?rev=349309&r1=349308&r2=349309&view=diff ============================================================================== --- jakarta/commons/proper/math/branches/MATH_1_1/src/java/org/apache/commons/math/complex/ComplexUtils.java (original) +++ jakarta/commons/proper/math/branches/MATH_1_1/src/java/org/apache/commons/math/complex/ComplexUtils.java Sun Nov 27 12:55:08 2005 @@ -46,7 +46,8 @@ } /** - * Compute the <a href="http://mathworld.wolfram.com/InverseCosine.html"> + * Compute the + * <a href="http://mathworld.wolfram.com/InverseCosine.html" TARGET="_top"> * inverse cosine</a> for the given complex argument. * <p> * Implements the formula: <pre> @@ -54,11 +55,10 @@ * <p> * Returns [EMAIL PROTECTED] Complex#NaN} if either real or imaginary part of the * input argument is <code>NaN</code> or infinite. - * <p> - * Throws <code>NullPointerException</code> if z is null. * - * @param z the value whose inverse cosine is to be returned. - * @return the inverse cosine of <code>z</code>. + * @param z the value whose inverse cosine is to be returned + * @return the inverse cosine of <code>z</code> + * @throws NullPointerException if <code>z</code> is null */ public static Complex acos(Complex z) { if (z.isNaN()) { @@ -70,7 +70,8 @@ } /** - * Compute the <a href="http://mathworld.wolfram.com/InverseSine.html"> + * Compute the + * <a href="http://mathworld.wolfram.com/InverseSine.html" TARGET="_top"> * inverse sine</a> for the given complex argument. * <p> * Implements the formula: <pre> @@ -78,11 +79,10 @@ * <p> * Returns [EMAIL PROTECTED] Complex#NaN} if either real or imaginary part of the * input argument is <code>NaN</code> or infinite. - * <p> - * Throws <code>NullPointerException</code> if z is null. * * @param z the value whose inverse sine is to be returned. * @return the inverse sine of <code>z</code>. + * @throws NullPointerException if <code>z</code> is null */ public static Complex asin(Complex z) { if (z.isNaN()) { @@ -94,19 +94,19 @@ } /** - * Compute the <a href="http://mathworld.wolfram.com/InverseTangent.html"> + * Compute the + * <a href="http://mathworld.wolfram.com/InverseTangent.html" TARGET="_top"> * inverse tangent</a> for the given complex argument. * <p> * Implements the formula: <pre> * <code> atan(z) = (i/2) log((i + z)/(i - z)) </code></pre> * <p> * Returns [EMAIL PROTECTED] Complex#NaN} if either real or imaginary part of the - * input argument is <code>NaN</code> or infinite. - * <p> - * Throws <code>NullPointerException</code> if z is null. + * input argument is <code>NaN</code> or infinite. * - * @param z the value whose inverse tangent is to be returned. - * @return the inverse tangent of <code>z</code>. + * @param z the value whose inverse tangent is to be returned + * @return the inverse tangent of <code>z</code> + * @throws NullPointerException if <code>z</code> is null */ public static Complex atan(Complex z) { if (z.isNaN()) { @@ -119,7 +119,9 @@ } /** - * Compute the <a href="http://mathworld.wolfram.com/Cosine.html">cosine</a> + * Compute the + * <a href="http://mathworld.wolfram.com/Cosine.html" TARGET="_top"> + * cosine</a> * for the given complex argument. * <p> * Implements the formula: <pre> @@ -139,10 +141,9 @@ * cos(±INFINITY + i) = NaN + NaN i * cos(±INFINITY ± INFINITY i) = NaN + NaN i</code></pre> * - * Throws <code>NullPointerException</code> if z is null. - * - * @param z the value whose cosine is to be returned. - * @return the cosine of <code>z</code>. + * @param z the value whose cosine is to be returned + * @return the cosine of <code>z</code> + * @throws NullPointerException if <code>z</code> is null */ public static Complex cos(Complex z) { if (z.isNaN()) { @@ -157,7 +158,8 @@ } /** - * Compute the <a href="http://mathworld.wolfram.com/HyperbolicCosine.html"> + * Compute the + * <a href="http://mathworld.wolfram.com/HyperbolicCosine.html" TARGET="_top"> * hyperbolic cosine</a> for the given complex argument. * <p> * Implements the formula: <pre> @@ -196,7 +198,7 @@ /** * Compute the - * <a href="http://mathworld.wolfram.com/ExponentialFunction.html"> + * <a href="http://mathworld.wolfram.com/ExponentialFunction.html" TARGET="_top"> * exponential function</a> for the given complex argument. * <p> * Implements the formula: <pre> @@ -219,8 +221,8 @@ * <p> * Throws <code>NullPointerException</code> if z is null. * - * @param z the value. - * @return <i>e</i><sup><code>z</code></sup>. + * @param z the value + * @return <i>e</i><sup><code>z</code></sup> */ public static Complex exp(Complex z) { if (z.isNaN()) { @@ -233,7 +235,8 @@ } /** - * Compute the <a href="http://mathworld.wolfram.com/NaturalLogarithm.html"> + * Compute the + * <a href="http://mathworld.wolfram.com/NaturalLogarithm.html" TARGET="_top"> * natural logarithm</a> for the given complex argument. * <p> * Implements the formula: <pre> @@ -245,8 +248,8 @@ * Returns [EMAIL PROTECTED] Complex#NaN} if either real or imaginary part of the * input argument is <code>NaN</code>. * <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> + * 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 @@ -254,6 +257,7 @@ * 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> * Throws <code>NullPointerException</code> if z is null. * @@ -272,8 +276,8 @@ /** * Creates a complex number from the given polar representation. * <p> - * The value returned is <code>r·e<sup>i·theta</sup></code>, computed as - * <code>r·cos(theta) + r·sin(theta)i</code> + * The value returned is <code>r·e<sup>i·theta</sup></code>, + * computed as <code>r·cos(theta) + r·sin(theta)i</code> * <p> * If either <code>r</code> or <code>theta</code> is NaN, or * <code>theta</code> is infinite, [EMAIL PROTECTED] Complex#NaN} is returned. @@ -288,8 +292,6 @@ * polar2Complex(INFINITY, -π/4) = INFINITY - INFINITY i * polar2Complex(INFINITY, 5π/4) = -INFINITY - INFINITY i </code></pre> * - * Throws <code>IllegalArgumentException</code> if <code>r < 0</code> - * * @param r the modulus of the complex number to create * @param theta the argument of the complex number to create * @return <code>r·e<sup>i·theta</sup></code> @@ -312,20 +314,22 @@ * [EMAIL PROTECTED] #log}, respectively. * <p> * Returns [EMAIL PROTECTED] Complex#NaN} if either real or imaginary part of the - * input argument is <code>NaN</code> or infinite. - * <p> - * Throws <code>NullPointerException</code> if either x or y is null. + * input argument is <code>NaN</code> or infinite, or if <code>y</code> + * equals [EMAIL PROTECTED] Complex#ZERO}. * * @param y the base. * @param x the exponent. * @return <code>y</code><sup><code>x</code></sup> + * @throws NullpointerException if either x or y is null */ public static Complex pow(Complex y, Complex x) { return exp(x.multiply(log(y))); } /** - * Compute the <a href="http://mathworld.wolfram.com/Sine.html">sine</a> + * Compute the + * <a href="http://mathworld.wolfram.com/Sine.html" TARGET="_top"> + * sine</a> * for the given complex argument. * <p> * Implements the formula: <pre> @@ -363,7 +367,8 @@ } /** - * Compute the <a href="http://mathworld.wolfram.com/HyperbolicSine.html"> + * Compute the + * <a href="http://mathworld.wolfram.com/HyperbolicSine.html" TARGET="_top"> * hyperbolic sine</a> for the given complex argument. * <p> * Implements the formula: <pre> @@ -381,12 +386,11 @@ * <code> * sinh(1 ± INFINITY i) = NaN + NaN i * sinh(±INFINITY + i) = ± INFINITY + INFINITY i - * sinh(±INFINITY ± INFINITY i) = NaN + NaN i</code></pre> - * <p> - * Throws <code>NullPointerException</code> if z is null. + * sinh(±INFINITY ± INFINITY i) = NaN + NaN i</code></pre * - * @param z the value whose hyperbolic sine is to be returned. - * @return the hyperbolic sine of <code>z</code>. + * @param z the value whose hyperbolic sine is to be returned + * @return the hyperbolic sine of <code>z</code> + * @throws NullPointerException if <code>z</code> is null */ public static Complex sinh(Complex z) { if (z.isNaN()) { @@ -401,8 +405,9 @@ } /** - * Compute the <a href="http://mathworld.wolfram.com/SquareRoot.html">square - * root</a> for the given complex argument. + * Compute the + * <a href="http://mathworld.wolfram.com/SquareRoot.html" TARGET="_top"> + * square root</a> for the given complex argument. * <p> * Implements the following algorithm to compute <code>sqrt(a + bi)</code>: * <ol><li>Let <code>t = sqrt((|a| + |a + bi|) / 2)</code></li> @@ -429,10 +434,9 @@ * sqrt(-INFINITY ± INFINITY i) = NaN ± INFINITY i * </code></pre> * - * Throws <code>NullPointerException</code> if z is null. - * - * @param z the value whose square root is to be returned. - * @return the square root of <code>z</code>. + * @param z the value whose square root is to be returned + * @return the square root of <code>z</code> + * @throws NullPointerException if <code>z</code> is null */ public static Complex sqrt(Complex z) { if (z.isNaN()) { @@ -452,8 +456,10 @@ } /** - * Compute the <a href="http://mathworld.wolfram.com/SquareRoot.html">square - * root</a> of 1 - <code>z</code><sup>2</sup> for the given complex argument. + * Compute the + * <a href="http://mathworld.wolfram.com/SquareRoot.html" TARGET="_top"> + * square root</a> of 1 - <code>z</code><sup>2</sup> for the given complex + * argument. * <p> * Computes the result directly as * <code>sqrt(Complex.ONE.subtract(z.multiply(z)))</code>. @@ -463,18 +469,18 @@ * <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> - * Throws <code>NullPointerException</code> if z is null. * - * @param z the value. - * @return the square root of 1 - <code>z</code><sup>2</sup>. + * @param z the value + * @return the square root of 1 - <code>z</code><sup>2</sup> + * @throws NullPointerException if <code>z</code> is null */ public static Complex sqrt1z(Complex z) { return sqrt(Complex.ONE.subtract(z.multiply(z))); } /** - * Compute the <a href="http://mathworld.wolfram.com/Tangent.html"> + * Compute the + * <a href="http://mathworld.wolfram.com/Tangent.html" TARGET="_top"> * tangent</a> for the given complex argument. * <p> * Implements the formula: <pre> @@ -486,18 +492,18 @@ * Returns [EMAIL PROTECTED] Complex#NaN} if either real or imaginary part of the * input argument is <code>NaN</code>. * <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> + * 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(1 ± INFINITY i) = 0 + NaN i * tan(±INFINITY + i) = NaN + NaN i - * tan(±INFINITY ± INFINITY i) = NaN + NaN i</code></pre> - * - * Throws <code>NullPointerException</code> if z is null. + * tan(±INFINITY ± INFINITY i) = NaN + NaN i + * tan(±&pi/2 + 0 i) = ±INFINITY + NaN i</code></pre> * - * @param z the value whose tangent is to be returned. - * @return the tangent of <code>z</code>. + * @param z the value whose tangent is to be returned + * @return the tangent of <code>z</code> + * @throws NullPointerException if <code>z</code> is null */ public static Complex tan(Complex z) { if (z.isNaN()) { @@ -513,7 +519,7 @@ /** * Compute the - * <a href="http://mathworld.wolfram.com/HyperbolicTangent.html"> + * <a href="http://mathworld.wolfram.com/HyperbolicTangent.html" TARGET="_top"> * hyperbolic tangent</a> for the given complex argument. * <p> * Implements the formula: <pre> @@ -529,14 +535,14 @@ * infinite or NaN values returned in parts of the result.<pre> * Examples: * <code> - * tan(1 ± INFINITY i) = NaN + NaN i - * tan(±INFINITY + i) = NaN + 0 i - * tan(±INFINITY ± INFINITY i) = NaN + NaN i</code></pre> - * - * Throws <code>NullPointerException</code> if z is null. - * - * @param z the value whose hyperbolic tangent is to be returned. - * @return the hyperbolic tangent of <code>z</code>. + * tanh(1 ± INFINITY i) = NaN + NaN i + * tanh(±INFINITY + i) = NaN + 0 i + * tanh(±INFINITY ± INFINITY i) = NaN + NaN i + * tanh(0 + (&pi/2)i) = NaN + INFINITY i</code></pre> + * + * @param z the value whose hyperbolic tangent is to be returned + * @return the hyperbolic tangent of <code>z</code> + * @throws NullPointerException if <code>z</code> is null */ public static Complex tanh(Complex z) { if (z.isNaN()) { Modified: jakarta/commons/proper/math/branches/MATH_1_1/src/test/org/apache/commons/math/complex/ComplexUtilsTest.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/math/branches/MATH_1_1/src/test/org/apache/commons/math/complex/ComplexUtilsTest.java?rev=349309&r1=349308&r2=349309&view=diff ============================================================================== --- jakarta/commons/proper/math/branches/MATH_1_1/src/test/org/apache/commons/math/complex/ComplexUtilsTest.java (original) +++ jakarta/commons/proper/math/branches/MATH_1_1/src/test/org/apache/commons/math/complex/ComplexUtilsTest.java Sun Nov 27 12:55:08 2005 @@ -47,6 +47,8 @@ private Complex nanZero = new Complex(nan, 0); private Complex infZero = new Complex(inf, 0); private Complex zeroInf = new Complex(0, inf); + private Complex zeroNegInf = new Complex(0, negInf); + private Complex negInfZero = new Complex(negInf, 0); private ComplexFormat fmt = new ComplexFormat(); @@ -207,6 +209,11 @@ Complex z = new Complex(3, 4); Complex expected = new Complex(-13.12878, -15.20078); TestUtils.assertEquals(expected, ComplexUtils.exp(z), 1.0e-5); + TestUtils.assertEquals(Complex.ONE, + ComplexUtils.exp(Complex.ZERO), 10e-12); + Complex iPi = Complex.I.multiply(new Complex(pi,0)); + TestUtils.assertEquals(Complex.ONE.negate(), + ComplexUtils.exp(iPi), 10e-12); } public void testExpNaN() { @@ -261,6 +268,10 @@ ComplexUtils.log(negInfNegInf), 10e-12); } + public void testLogZero() { + TestUtils.assertSame(negInfZero, ComplexUtils.log(Complex.ZERO)); + } + public void testlogNull() { try { Complex z = ComplexUtils.log(null); @@ -375,6 +386,21 @@ TestUtils.assertSame(Complex.NaN,ComplexUtils.pow(infNegInf, negInfNegInf)); TestUtils.assertSame(Complex.NaN,ComplexUtils.pow(infNegInf, infInf)); } + + public void testPowZero() { + TestUtils.assertSame(Complex.NaN, + ComplexUtils.pow(Complex.ZERO, Complex.ONE)); + TestUtils.assertSame(Complex.NaN, + ComplexUtils.pow(Complex.ZERO, Complex.ZERO)); + TestUtils.assertSame(Complex.NaN, + ComplexUtils.pow(Complex.ZERO, Complex.I)); + TestUtils.assertEquals(Complex.ONE, + ComplexUtils.pow(Complex.ONE, Complex.ZERO), 10e-12); + TestUtils.assertEquals(Complex.ONE, + ComplexUtils.pow(Complex.I, Complex.ZERO), 10e-12); + TestUtils.assertEquals(Complex.ONE, + ComplexUtils.pow(new Complex(-1, 3), Complex.ZERO), 10e-12); + } public void testpowNull() { try { @@ -559,6 +585,11 @@ TestUtils.assertSame(Complex.NaN, ComplexUtils.tan(negInfNegInf)); } + public void testTanCritical() { + TestUtils.assertSame(infNaN, ComplexUtils.tan(new Complex(pi/2, 0))); + TestUtils.assertSame(negInfNaN, ComplexUtils.tan(new Complex(-pi/2, 0))); + } + public void testTanNull() { try { Complex z = ComplexUtils.tan(null); @@ -587,6 +618,10 @@ TestUtils.assertSame(Complex.NaN, ComplexUtils.tanh(infNegInf)); TestUtils.assertSame(Complex.NaN, ComplexUtils.tanh(negInfInf)); TestUtils.assertSame(Complex.NaN, ComplexUtils.tanh(negInfNegInf)); + } + + public void testTanhCritical() { + TestUtils.assertSame(nanInf, ComplexUtils.tanh(new Complex(0, pi/2))); } public void testTanhNull() { --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]