You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by di...@apache.org on 2003/11/15 19:52:31 UTC

cvs commit: jakarta-commons/math/src/test/org/apache/commons/math/util MathUtilsTest.java

dirkv       2003/11/15 10:52:31

  Modified:    math/src/java/org/apache/commons/math/complex
                        ComplexMath.java
               math/src/java/org/apache/commons/math/stat StatUtils.java
               math/src/test/org/apache/commons/math TestUtils.java
               math/src/test/org/apache/commons/math/complex
                        ComplexTest.java
               math/src/test/org/apache/commons/math/util
                        MathUtilsTest.java
  Added:       math/src/test/org/apache/commons/math/complex
                        ComplexMathTest.java
  Log:
    Applied Brent Wardens Bug fixes to Complex implementation and tests.
  
  Revision  Changes    Path
  1.4       +7 -6      jakarta-commons/math/src/java/org/apache/commons/math/complex/ComplexMath.java
  
  Index: ComplexMath.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/math/src/java/org/apache/commons/math/complex/ComplexMath.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ComplexMath.java	14 Nov 2003 22:22:22 -0000	1.3
  +++ ComplexMath.java	15 Nov 2003 18:52:31 -0000	1.4
  @@ -105,9 +105,10 @@
               return Complex.NaN;
           }
           
  +        
           return Complex.I.multiply(
               log(Complex.I.add(z).divide(Complex.I.subtract(z))))
  -            .multiply(new Complex(2.0, 0.0));
  +            .divide(new Complex(2.0, 0.0));
       }
       
       /**
  @@ -219,8 +220,8 @@
           if (a >= 0.0) {
               return new Complex(t, b / (2.0 * t));
           } else {
  -            double s = (b > 0.0 ? 1.0 : (b < 0.0 ? -1.0 : 0.0));
  -            return new Complex(Math.abs(z.getImaginary()) / (2.0 * t), s * t);
  +            return new Complex(Math.abs(z.getImaginary()) / (2.0 * t),
  +                MathUtils.sign(b) * t);
           }
       }
       
  @@ -244,7 +245,7 @@
           double b2 = 2.0 * z.getImaginary();
           double d = Math.cos(a2) + MathUtils.cosh(b2);
           
  -        return new Complex(Math.sin(a2) / d, MathUtils.sinh(b2) / 2);
  +        return new Complex(Math.sin(a2) / d, MathUtils.sinh(b2) / d);
       }
       
       /**
  @@ -259,6 +260,6 @@
           double b2 = 2.0 * z.getImaginary();
           double d = MathUtils.cosh(a2) + Math.cos(b2);
           
  -        return new Complex(MathUtils.sinh(a2) / d, Math.sin(b2) / 2);
  +        return new Complex(MathUtils.sinh(a2) / d, Math.sin(b2) / d);
       }
   }
  
  
  
  1.22      +84 -33    jakarta-commons/math/src/java/org/apache/commons/math/stat/StatUtils.java
  
  Index: StatUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/math/src/java/org/apache/commons/math/stat/StatUtils.java,v
  retrieving revision 1.21
  retrieving revision 1.22
  diff -u -r1.21 -r1.22
  --- StatUtils.java	14 Nov 2003 22:22:18 -0000	1.21
  +++ StatUtils.java	15 Nov 2003 18:52:31 -0000	1.22
  @@ -58,6 +58,7 @@
   import org.apache.commons.math.stat.univariate.moment.Variance;
   import org.apache.commons.math.stat.univariate.rank.Max;
   import org.apache.commons.math.stat.univariate.rank.Min;
  +import org.apache.commons.math.stat.univariate.rank.Percentile;
   import org.apache.commons.math.stat.univariate.summary.Product;
   import org.apache.commons.math.stat.univariate.summary.Sum;
   import org.apache.commons.math.stat.univariate.summary.SumOfLogs;
  @@ -71,30 +72,33 @@
    */
   public final class StatUtils {
   
  -	/** sum */
  -	private static UnivariateStatistic sum = new Sum();
  -	
  -	/** sumSq */
  -	private static UnivariateStatistic sumSq = new SumOfSquares();
  -	
  -	/** prod */
  -	private static UnivariateStatistic prod = new Product();
  -	
  -	/** sumLog */
  -	private static UnivariateStatistic sumLog = new SumOfLogs();
  -	
  -	/** min */
  -	private static UnivariateStatistic min = new Min();	
  -	
  -	/** max */
  -	private static UnivariateStatistic max = new Max();	
  -	
  -	/** mean */
  -	private static UnivariateStatistic mean = new Mean();	
  -	
  -	/** variance */
  -	private static UnivariateStatistic variance = new Variance();	
  -		
  +    /** sum */
  +    private static UnivariateStatistic sum = new Sum();
  +
  +    /** sumSq */
  +    private static UnivariateStatistic sumSq = new SumOfSquares();
  +
  +    /** prod */
  +    private static UnivariateStatistic prod = new Product();
  +
  +    /** sumLog */
  +    private static UnivariateStatistic sumLog = new SumOfLogs();
  +
  +    /** min */
  +    private static UnivariateStatistic min = new Min();
  +
  +    /** max */
  +    private static UnivariateStatistic max = new Max();
  +
  +    /** mean */
  +    private static UnivariateStatistic mean = new Mean();
  +
  +    /** variance */
  +    private static UnivariateStatistic variance = new Variance();
  +
  +    /** variance */
  +    private static Percentile percentile = new Percentile();
  +
       /**
        * Private Constructor
        */
  @@ -107,7 +111,7 @@
        * @return the sum of the values or Double.NaN if the array is empty
        */
       public static double sum(final double[] values) {
  -		return sum.evaluate(values);
  +        return sum.evaluate(values);
       }
   
       /**
  @@ -130,7 +134,7 @@
        * @return the sum of the squared values or Double.NaN if the array is empty
        */
       public static double sumSq(final double[] values) {
  -		return sumSq.evaluate(values);
  +        return sumSq.evaluate(values);
       }
   
       /**
  @@ -176,7 +180,7 @@
        * @return the sumLog value or Double.NaN if the array is empty
        */
       public static double sumLog(final double[] values) {
  -		return sumLog.evaluate(values);
  +        return sumLog.evaluate(values);
       }
   
       /**
  @@ -190,7 +194,7 @@
           final double[] values,
           final int begin,
           final int length) {
  -		return sumLog.evaluate(values, begin, length);
  +        return sumLog.evaluate(values, begin, length);
       }
   
       /**
  @@ -200,7 +204,7 @@
        * @return the mean of the values or Double.NaN if the array is empty
        */
       public static double mean(final double[] values) {
  -		return mean.evaluate(values);
  +        return mean.evaluate(values);
       }
   
       /**
  @@ -232,7 +236,7 @@
        * or 0.0 for a single value set.
        */
       public static double variance(final double[] values) {
  -		return variance.evaluate(values);
  +        return variance.evaluate(values);
       }
   
       /**
  @@ -263,7 +267,7 @@
        * @return the maximum of the values or Double.NaN if the array is empty
        */
       public static double max(final double[] values) {
  -		return max.evaluate(values);
  +        return max.evaluate(values);
       }
   
       /**
  @@ -286,7 +290,7 @@
        * @return the minimum of the values or Double.NaN if the array is empty
        */
       public static double min(final double[] values) {
  -		return min.evaluate(values);
  +        return min.evaluate(values);
       }
   
       /**
  @@ -302,5 +306,52 @@
           final int length) {
           return min.evaluate(values, begin, length);
       }
  +    
  +    /**
  +     * Returns an estimate for the pth percentile of the stored values. 
  +     * This estimate follows the interpolation-adjusted defintion presented 
  +     * <a href="http://www.utdallas.edu/~ammann/stat5311/node8.html">here</a>
  +     * <p/>
  +     * <strong>Preconditions</strong>:<ul>
  +     * <li><code>0 &lt; p &lt; 100</code> (otherwise an 
  +     * <code>IllegalArgumentException</code> is thrown)</li>
  +     * <li>at least one value must be stored (returns <code>Double.NaN
  +     *     </code> otherwise)</li>
  +     * </ul>
  +     * 
  +     * @param values Is a double[] containing the values
  +     * @param p the requested percentile (scaled from 0 - 100)
  +     * @return An estimate for the pth percentile of the data values
  +     */
  +    public static double percentile(final double[] values, final double p) {
  +            return percentile.evaluate(values,p);
  +    }
  +
  +    /**
  +     * Returns an estimate for the pth percentile of the stored values. 
  +     * This estimate follows the interpolation-adjusted defintion presented 
  +     * <a href="http://www.utdallas.edu/~ammann/stat5311/node8.html">here</a>
  +     * <p/>
  +     * <strong>Preconditions</strong>:<ul>
  +     * <li><code>0 &lt; p &lt; 100</code> (otherwise an 
  +     * <code>IllegalArgumentException</code> is thrown)</li>
  +     * <li>at least one value must be stored (returns <code>Double.NaN
  +     *     </code> otherwise)</li>
  +     * </ul>
  +     * 
  +     * @param values Is a double[] containing the values
  +     * @param begin processing at this point in the array
  +     * @param length processing at this point in the array
  +     * @param p the requested percentile (scaled from 0 - 100)
  +     * @return An estimate for the pth percentile of the data values
  +     */
  +    public static double percentile(
  +            final double[] values,
  +            final int begin,
  +            final int length, 
  +            final double p) {
  +            return percentile.evaluate(values, begin, length, p);
  +    }
  +    
   
   }
  
  
  
  1.7       +11 -1     jakarta-commons/math/src/test/org/apache/commons/math/TestUtils.java
  
  Index: TestUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/math/src/test/org/apache/commons/math/TestUtils.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- TestUtils.java	14 Nov 2003 22:22:23 -0000	1.6
  +++ TestUtils.java	15 Nov 2003 18:52:31 -0000	1.7
  @@ -54,6 +54,8 @@
   
   package org.apache.commons.math;
   
  +import org.apache.commons.math.complex.Complex;
  +
   import junit.framework.Assert;
   
   /**
  @@ -74,5 +76,13 @@
           } else {
               Assert.assertEquals(expected, actual, delta);
           }
  +    }
  +
  +    /**
  +     * 
  +     */
  +    public static void assertEquals(Complex expected, Complex actual, double delta) {
  +        assertEquals(expected.getReal(), actual.getReal(), delta);
  +        assertEquals(expected.getImaginary(), actual.getImaginary(), delta);
       }
   }
  
  
  
  1.4       +38 -1     jakarta-commons/math/src/test/org/apache/commons/math/complex/ComplexTest.java
  
  Index: ComplexTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/math/src/test/org/apache/commons/math/complex/ComplexTest.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ComplexTest.java	14 Nov 2003 22:22:24 -0000	1.3
  +++ ComplexTest.java	15 Nov 2003 18:52:31 -0000	1.4
  @@ -85,6 +85,10 @@
           assertEquals(5.0, z.abs(), 1.0e-5);
       }
       
  +    public void testAbsNaN() {
  +        assertTrue(Double.isNaN(Complex.NaN.abs()));
  +    }
  +    
       public void testAdd() {
           Complex x = new Complex(3.0, 4.0);
           Complex y = new Complex(5.0, 6.0);
  @@ -163,5 +167,38 @@
           Complex x = new Complex(3.0, 4.0);
           Complex z = x.subtract(Complex.NaN);
           assertTrue(z.isNaN());
  +    }
  +    
  +    public void testEqualsNull() {
  +        Complex x = new Complex(3.0, 4.0);
  +        assertFalse(x.equals(null));
  +    }
  +    
  +    public void testEqualsClass() {
  +        Complex x = new Complex(3.0, 4.0);
  +        assertFalse(x.equals(this));
  +    }
  +    
  +    public void testEqualsSame() {
  +        Complex x = new Complex(3.0, 4.0);
  +        assertTrue(x.equals(x));
  +    }
  +    
  +    public void testEqualsTrue() {
  +        Complex x = new Complex(3.0, 4.0);
  +        Complex y = new Complex(3.0, 4.0);
  +        assertTrue(x.equals(y));
  +    }
  +    
  +    public void testEqualsRealDifference() {
  +        Complex x = new Complex(0.0, 0.0);
  +        Complex y = new Complex(0.0 + Double.MIN_VALUE, 0.0);
  +        assertFalse(x.equals(y));
  +    }
  +    
  +    public void testEqualsImaginaryDifference() {
  +        Complex x = new Complex(0.0, 0.0);
  +        Complex y = new Complex(0.0, 0.0 + Double.MIN_VALUE);
  +        assertFalse(x.equals(y));
       }
   }
  
  
  
  1.1                  jakarta-commons/math/src/test/org/apache/commons/math/complex/ComplexMathTest.java
  
  Index: ComplexMathTest.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowledgement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgement may appear in the software itself,
   *    if and wherever such third-party acknowledgements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their name without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  package org.apache.commons.math.complex;
  
  import org.apache.commons.math.TestUtils;
  
  import junit.framework.TestCase;
  
  /**
   * @version $Revision: 1.1 $ $Date: 2003/11/15 18:52:31 $
   */
  public class ComplexMathTest extends TestCase {
      
      public void testAcos() {
          Complex z = new Complex(3, 4);
          Complex expected = new Complex(0.936812, -2.30551);
          TestUtils.assertEquals(expected, ComplexMath.acos(z), 1.0e-5);
      }
      
      public void testAcosNaN() {
          assertTrue(ComplexMath.acos(Complex.NaN).isNaN());
      }
      
      public void testAsin() {
          Complex z = new Complex(3, 4);
          Complex expected = new Complex(0.633984, 2.30551);
          TestUtils.assertEquals(expected, ComplexMath.asin(z), 1.0e-5);
      }
      
      public void testAsinNaN() {
          assertTrue(ComplexMath.asin(Complex.NaN).isNaN());
      }
      
      public void testAtan() {
          Complex z = new Complex(3, 4);
          Complex expected = new Complex(1.44831, 0.158997);
          TestUtils.assertEquals(expected, ComplexMath.atan(z), 1.0e-5);
      }
      
      public void testAtanNaN() {
          assertTrue(ComplexMath.atan(Complex.NaN).isNaN());
      }
      
      public void testCos() {
          Complex z = new Complex(3, 4);
          Complex expected = new Complex(-27.03495, -3.851153);
          TestUtils.assertEquals(expected, ComplexMath.cos(z), 1.0e-5);
      }
      
      public void testCosh() {
          Complex z = new Complex(3, 4);
          Complex expected = new Complex(-6.58066, -7.58155);
          TestUtils.assertEquals(expected, ComplexMath.cosh(z), 1.0e-5);
      }
      
      public void testCoshNaN() {
          assertTrue(ComplexMath.cosh(Complex.NaN).isNaN());
      }
      
      public void testCosNaN() {
          assertTrue(ComplexMath.cos(Complex.NaN).isNaN());
      }
      
      public void testExp() {
          Complex z = new Complex(3, 4);
          Complex expected = new Complex(-13.12878, -15.20078);
          TestUtils.assertEquals(expected, ComplexMath.exp(z), 1.0e-5);
      }
      
      public void testExpNaN() {
          assertTrue(ComplexMath.exp(Complex.NaN).isNaN());
      }
      
      public void testLog() {
          Complex z = new Complex(3, 4);
          Complex expected = new Complex(1.60944, 0.927295);
          TestUtils.assertEquals(expected, ComplexMath.log(z), 1.0e-5);
      }
      
      public void testLogNaN() {
          assertTrue(ComplexMath.log(Complex.NaN).isNaN());
      }
      
      public void testPow() {
          Complex x = new Complex(3, 4);
          Complex y = new Complex(5, 6);
          Complex expected = new Complex(-1.860893, 11.83677);
          TestUtils.assertEquals(expected, ComplexMath.pow(x, y), 1.0e-5);
      }
      
      public void testPowNaNBase() {
          Complex x = new Complex(3, 4);
          assertTrue(ComplexMath.pow(Complex.NaN, x).isNaN());
      }
      
      public void testPowNaNExponent() {
          Complex x = new Complex(3, 4);
          assertTrue(ComplexMath.pow(x, Complex.NaN).isNaN());
      }
      
      public void testSin() {
          Complex z = new Complex(3, 4);
          Complex expected = new Complex(3.853738, -27.01681);
          TestUtils.assertEquals(expected, ComplexMath.sin(z), 1.0e-5);
      }
      
      public void testSinh() {
          Complex z = new Complex(3, 4);
          Complex expected = new Complex(-6.54812, -7.61923);
          TestUtils.assertEquals(expected, ComplexMath.sinh(z), 1.0e-5);
      }
      
      public void testSinhNaN() {
          assertTrue(ComplexMath.sinh(Complex.NaN).isNaN());
      }
      
      public void testSinNaN() {
          assertTrue(ComplexMath.sin(Complex.NaN).isNaN());
      }
      
      public void testSqrtRealPositive() {
          Complex z = new Complex(3, 4);
          Complex expected = new Complex(2, 1);
          TestUtils.assertEquals(expected, ComplexMath.sqrt(z), 1.0e-5);
      }
      
      public void testSqrtRealZero() {
          Complex z = new Complex(0.0, 4);
          Complex expected = new Complex(1.41421, 1.41421);
          TestUtils.assertEquals(expected, ComplexMath.sqrt(z), 1.0e-5);
      }
      
      public void testSqrtRealNegative() {
          Complex z = new Complex(-3.0, 4);
          Complex expected = new Complex(1, 2);
          TestUtils.assertEquals(expected, ComplexMath.sqrt(z), 1.0e-5);
      }
      
      public void testSqrtImaginaryZero() {
          Complex z = new Complex(-3.0, 0.0);
          Complex expected = new Complex(0.0, 1.73205);
          TestUtils.assertEquals(expected, ComplexMath.sqrt(z), 1.0e-5);
      }
      
      public void testSqrtImaginaryNegative() {
          Complex z = new Complex(-3.0, -4.0);
          Complex expected = new Complex(1.0, -2.0);
          TestUtils.assertEquals(expected, ComplexMath.sqrt(z), 1.0e-5);
      }
  
      public void testSqrt1z() {
          Complex z = new Complex(3, 4);
          Complex expected = new Complex(4.08033, -2.94094);
          TestUtils.assertEquals(expected, ComplexMath.sqrt1z(z), 1.0e-5);
      }
      
      public void testSqrt1zNaN() {
          assertTrue(ComplexMath.sqrt1z(Complex.NaN).isNaN());
      }
      
      public void testSqrtNaN() {
          assertTrue(ComplexMath.sqrt(Complex.NaN).isNaN());
      }
      
      public void testTan() {
          Complex z = new Complex(3, 4);
          Complex expected = new Complex(-0.000187346, 0.999356);
          TestUtils.assertEquals(expected, ComplexMath.tan(z), 1.0e-5);
      }
      
      public void testTanh() {
          Complex z = new Complex(3, 4);
          Complex expected = new Complex(1.00071, 0.00490826);
          TestUtils.assertEquals(expected, ComplexMath.tanh(z), 1.0e-5);
      }
      
      public void testTanhNaN() {
          assertTrue(ComplexMath.tanh(Complex.NaN).isNaN());
      }
      
      public void testTanNaN() {
          assertTrue(ComplexMath.tan(Complex.NaN).isNaN());
      }
  }
  
  
  
  1.7       +23 -1     jakarta-commons/math/src/test/org/apache/commons/math/util/MathUtilsTest.java
  
  Index: MathUtilsTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/math/src/test/org/apache/commons/math/util/MathUtilsTest.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- MathUtilsTest.java	14 Nov 2003 22:22:23 -0000	1.6
  +++ MathUtilsTest.java	15 Nov 2003 18:52:31 -0000	1.7
  @@ -53,6 +53,8 @@
    */
   package org.apache.commons.math.util;
   
  +import org.apache.commons.math.TestUtils;
  +
   import junit.framework.Test;
   import junit.framework.TestCase;
   import junit.framework.TestSuite;
  @@ -349,4 +351,24 @@
           assertEquals( 1L, MathUtils.sign( 2L ) ) ;
           assertEquals( -1L, MathUtils.sign( -2L ) ) ;
       }
  +    
  +    public void testCosh() {
  +        double x = 3.0;
  +        double expected = 10.06766;
  +        assertEquals(expected, MathUtils.cosh(x), 1.0e-5);
  +    }   
  +    
  +    public void testSinh() {
  +        double x = 3.0;
  +        double expected = 10.01787;
  +        assertEquals(expected, MathUtils.sinh(x), 1.0e-5);
  +    }   
  +    
  +    public void testCoshNaN() {
  +        assertTrue(Double.isNaN(MathUtils.cosh(Double.NaN)));
  +    }   
  +    
  +    public void testSinhNaN() {
  +        assertTrue(Double.isNaN(MathUtils.sinh(Double.NaN)));
  +    }   
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org