commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From celes...@apache.org
Subject svn commit: r1227468 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math/analysis/ main/java/org/apache/commons/math/transform/ test/java/org/apache/commons/math/analysis/
Date Thu, 05 Jan 2012 03:46:48 GMT
Author: celestin
Date: Thu Jan  5 03:46:47 2012
New Revision: 1227468

URL: http://svn.apache.org/viewvc?rev=1227468&view=rev
Log:
Moved
transform.FastFourierTransformer.sample(UnivariateFunction, double, double, int)
to
analysis.FunctionUtils.sample(UnivariateFunction, double, double, int)
and created simple unit tests.

Related to MATH-677.

Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/FunctionUtils.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/transform/FastCosineTransformer.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/transform/FastFourierTransformer.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/transform/FastHadamardTransformer.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/transform/FastSineTransformer.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/FunctionUtilsTest.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/FunctionUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/FunctionUtils.java?rev=1227468&r1=1227467&r2=1227468&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/FunctionUtils.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/FunctionUtils.java
Thu Jan  5 03:46:47 2012
@@ -18,6 +18,9 @@
 package org.apache.commons.math.analysis;
 
 import org.apache.commons.math.analysis.function.Identity;
+import org.apache.commons.math.exception.NonMonotonicSequenceException;
+import org.apache.commons.math.exception.NotStrictlyPositiveException;
+import org.apache.commons.math.exception.util.LocalizedFormats;
 
 /**
  * Utilities for manipulating function objects.
@@ -290,4 +293,49 @@ public class FunctionUtils {
             }
         };
     }
+
+    /**
+     * <p>
+     * Samples the specified univariate real function on the specified interval.
+     * </p>
+     * <p>
+     * The interval is divided equally into {@code n} sections and sample points
+     * are taken from {@code min} to {@code max - (max - min) / n}; therefore
+     * {@code f} is not sampled at the upper bound {@code max}.
+     * </p>
+     *
+     * @param f the function to be sampled
+     * @param min the (inclusive) lower bound of the interval
+     * @param max the (exclusive) upper bound of the interval
+     * @param n the number of sample points
+     * @return the array of samples
+     * @throws NonMonotonicSequenceException if the lower bound {@code min} is
+     * greater than, or equal to the upper bound {@code max}
+     * @throws NotStrictlyPositiveException if the number of sample points
+     * {@code n} is negative
+     */
+    public static double[] sample(UnivariateFunction f,
+            double min, double max, int n) throws
+            NonMonotonicSequenceException,
+            NotStrictlyPositiveException {
+
+        if (n <= 0) {
+            throw new NotStrictlyPositiveException(
+                    LocalizedFormats.NOT_POSITIVE_NUMBER_OF_SAMPLES,
+                    Integer.valueOf(n));
+        }
+        if (min >= max) {
+            throw new NonMonotonicSequenceException(
+                    Double.valueOf(max),
+                    Double.valueOf(min),
+                    1);
+        }
+
+        double[] s = new double[n];
+        double h = (max - min) / n;
+        for (int i = 0; i < n; i++) {
+            s[i] = f.value(min + i * h);
+        }
+        return s;
+    }
 }

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/transform/FastCosineTransformer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/transform/FastCosineTransformer.java?rev=1227468&r1=1227467&r2=1227468&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/transform/FastCosineTransformer.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/transform/FastCosineTransformer.java
Thu Jan  5 03:46:47 2012
@@ -16,6 +16,7 @@
  */
 package org.apache.commons.math.transform;
 
+import org.apache.commons.math.analysis.FunctionUtils;
 import org.apache.commons.math.analysis.UnivariateFunction;
 import org.apache.commons.math.complex.Complex;
 import org.apache.commons.math.exception.MathIllegalArgumentException;
@@ -187,7 +188,7 @@ public class FastCosineTransformer imple
         NotStrictlyPositiveException,
         MathIllegalArgumentException {
 
-        final double[] data = FastFourierTransformer.sample(f, min, max, n);
+        final double[] data = FunctionUtils.sample(f, min, max, n);
         return transform(data);
     }
 
@@ -221,7 +222,7 @@ public class FastCosineTransformer imple
         NotStrictlyPositiveException,
         MathIllegalArgumentException {
 
-        final double[] data = FastFourierTransformer.sample(f, min, max, n);
+        final double[] data = FunctionUtils.sample(f, min, max, n);
         return inverseTransform(data);
     }
 

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/transform/FastFourierTransformer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/transform/FastFourierTransformer.java?rev=1227468&r1=1227467&r2=1227468&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/transform/FastFourierTransformer.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/transform/FastFourierTransformer.java
Thu Jan  5 03:46:47 2012
@@ -19,6 +19,7 @@ package org.apache.commons.math.transfor
 import java.io.Serializable;
 import java.lang.reflect.Array;
 
+import org.apache.commons.math.analysis.FunctionUtils;
 import org.apache.commons.math.analysis.UnivariateFunction;
 import org.apache.commons.math.complex.Complex;
 import org.apache.commons.math.exception.DimensionMismatchException;
@@ -180,7 +181,7 @@ public class FastFourierTransformer impl
             NotStrictlyPositiveException,
             MathIllegalArgumentException {
 
-        final double[] data = sample(f, min, max, n);
+        final double[] data = FunctionUtils.sample(f, min, max, n);
         if (unitary) {
             final double s = 1.0 / FastMath.sqrt(n);
             return scaleArray(fft(data, false), s);
@@ -245,7 +246,7 @@ public class FastFourierTransformer impl
             NotStrictlyPositiveException,
             MathIllegalArgumentException {
 
-        final double[] data = sample(f, min, max, n);
+        final double[] data = FunctionUtils.sample(f, min, max, n);
         final double s = 1.0 / (unitary ? FastMath.sqrt(n) : n);
         return scaleArray(fft(data, true), s);
     }
@@ -393,48 +394,6 @@ public class FastFourierTransformer impl
     }
 
     /**
-     * <p>
-     * Sample the given univariate real function on the given interval.
-     * </p>
-     * <p>
-     * The interval is divided equally into {@code n} sections and sample points
-     * are taken from {@code min} to {@code max - (max - min) / N}. Usually
-     * {@code f(x)} is periodic such that {@code f(min) = f(max)} (note that
-     * {@code max} is not sampled), but this condition is not required by the
-     * present method.
-     * </p>
-     *
-     * @param f the function to be sampled
-     * @param min the (inclusive) lower bound for the interval
-     * @param max the (exclusive) upper bound for the interval
-     * @param n the number of sample points
-     * @return the samples array
-     * @throws NonMonotonicSequenceException if the lower bound is greater
-     * than, or equal to the upper bound
-     * @throws NotStrictlyPositiveException if the number of sample points
-     * {@code n} is negative
-     */
-    public static double[] sample(UnivariateFunction f,
-            double min, double max, int n) throws
-            NonMonotonicSequenceException,
-            NotStrictlyPositiveException {
-
-        if (n <= 0) {
-            throw new NotStrictlyPositiveException(
-                    LocalizedFormats.NOT_POSITIVE_NUMBER_OF_SAMPLES,
-                    Integer.valueOf(n));
-        }
-        verifyInterval(min, max);
-
-        double[] s = new double[n];
-        double h = (max - min) / n;
-        for (int i = 0; i < n; i++) {
-            s[i] = f.value(min + i * h);
-        }
-        return s;
-    }
-
-    /**
      * Multiply every component in the given real array by the
      * given real number. The change is made in place.
      *
@@ -499,25 +458,6 @@ public class FastFourierTransformer impl
     }
 
     /**
-     * Verifies that the end-points specify an interval.
-     *
-     * @param lower the lower end-point
-     * @param upper the upper end-point
-     * @throws NonMonotonicSequenceException if the lower end-point is greater
-     * than, or equal to the upper end-point
-     */
-    public static void verifyInterval(double lower, double upper)
-        throws NonMonotonicSequenceException {
-
-        if (lower >= upper) {
-            throw new NonMonotonicSequenceException(
-                    Double.valueOf(upper),
-                    Double.valueOf(lower),
-                    1);
-        }
-    }
-
-    /**
      * Performs a multi-dimensional Fourier transform on a given array. Use
      * {@link #transform(Complex[])} and {@link #inverseTransform(Complex[])} in
      * a row-column implementation in any number of dimensions with

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/transform/FastHadamardTransformer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/transform/FastHadamardTransformer.java?rev=1227468&r1=1227467&r2=1227468&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/transform/FastHadamardTransformer.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/transform/FastHadamardTransformer.java
Thu Jan  5 03:46:47 2012
@@ -16,6 +16,7 @@
  */
 package org.apache.commons.math.transform;
 
+import org.apache.commons.math.analysis.FunctionUtils;
 import org.apache.commons.math.analysis.UnivariateFunction;
 import org.apache.commons.math.exception.MathIllegalArgumentException;
 import org.apache.commons.math.exception.NonMonotonicSequenceException;
@@ -62,7 +63,7 @@ public class FastHadamardTransformer imp
         NotStrictlyPositiveException,
         MathIllegalArgumentException {
 
-        return fht(FastFourierTransformer.sample(f, min, max, n));
+        return fht(FunctionUtils.sample(f, min, max, n));
     }
 
     /**
@@ -94,7 +95,7 @@ public class FastHadamardTransformer imp
         MathIllegalArgumentException {
 
         final double[] unscaled =
-            fht(FastFourierTransformer.sample(f, min, max, n));
+            fht(FunctionUtils.sample(f, min, max, n));
         return FastFourierTransformer.scaleArray(unscaled, 1.0 / n);
     }
 

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/transform/FastSineTransformer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/transform/FastSineTransformer.java?rev=1227468&r1=1227467&r2=1227468&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/transform/FastSineTransformer.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/transform/FastSineTransformer.java
Thu Jan  5 03:46:47 2012
@@ -16,6 +16,7 @@
  */
 package org.apache.commons.math.transform;
 
+import org.apache.commons.math.analysis.FunctionUtils;
 import org.apache.commons.math.analysis.UnivariateFunction;
 import org.apache.commons.math.complex.Complex;
 import org.apache.commons.math.exception.MathIllegalArgumentException;
@@ -189,7 +190,7 @@ public class FastSineTransformer impleme
         NotStrictlyPositiveException,
         MathIllegalArgumentException {
 
-        final double[] data = FastFourierTransformer.sample(f, min, max, n);
+        final double[] data = FunctionUtils.sample(f, min, max, n);
         data[0] = 0.0;
         if (orthogonal) {
             final double s = FastMath.sqrt(2.0 / n);
@@ -238,7 +239,7 @@ public class FastSineTransformer impleme
             return transform(f, min, max, n);
         }
 
-        final double[] data = FastFourierTransformer.sample(f, min, max, n);
+        final double[] data = FunctionUtils.sample(f, min, max, n);
         data[0] = 0.0;
         final double s = 2.0 / n;
 

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/FunctionUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/FunctionUtilsTest.java?rev=1227468&r1=1227467&r2=1227468&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/FunctionUtilsTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/FunctionUtilsTest.java
Thu Jan  5 03:46:47 2012
@@ -17,26 +17,25 @@
 
 package org.apache.commons.math.analysis;
 
-import org.apache.commons.math.analysis.UnivariateFunction;
-import org.apache.commons.math.analysis.function.Identity;
+import org.apache.commons.math.analysis.function.Add;
 import org.apache.commons.math.analysis.function.Constant;
-import org.apache.commons.math.analysis.function.Minus;
-import org.apache.commons.math.analysis.function.Inverse;
-import org.apache.commons.math.analysis.function.Power;
-import org.apache.commons.math.analysis.function.Sin;
-import org.apache.commons.math.analysis.function.Sinc;
 import org.apache.commons.math.analysis.function.Cos;
 import org.apache.commons.math.analysis.function.Cosh;
-import org.apache.commons.math.analysis.BivariateRealFunction;
-import org.apache.commons.math.analysis.function.Add;
-import org.apache.commons.math.analysis.function.Multiply;
 import org.apache.commons.math.analysis.function.Divide;
-import org.apache.commons.math.analysis.function.Min;
+import org.apache.commons.math.analysis.function.Identity;
+import org.apache.commons.math.analysis.function.Inverse;
+import org.apache.commons.math.analysis.function.Log;
 import org.apache.commons.math.analysis.function.Max;
+import org.apache.commons.math.analysis.function.Min;
+import org.apache.commons.math.analysis.function.Minus;
+import org.apache.commons.math.analysis.function.Multiply;
 import org.apache.commons.math.analysis.function.Pow;
-import org.apache.commons.math.analysis.function.Log;
-import org.apache.commons.math.analysis.MultivariateFunction;
-
+import org.apache.commons.math.analysis.function.Power;
+import org.apache.commons.math.analysis.function.Sin;
+import org.apache.commons.math.analysis.function.Sinc;
+import org.apache.commons.math.exception.NonMonotonicSequenceException;
+import org.apache.commons.math.exception.NotStrictlyPositiveException;
+import org.apache.commons.math.util.FastMath;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -204,4 +203,31 @@ public class FunctionUtilsTest {
             Assert.assertEquals(pow1.value(x), pow2.value(x), 0);
         }
     }
+
+    @Test(expected = NonMonotonicSequenceException.class)
+    public void testSampleWrongBounds(){
+        FunctionUtils.sample(new Sin(), Math.PI, 0.0, 10);
+    }
+
+    @Test(expected = NotStrictlyPositiveException.class)
+    public void testSampleNegativeNumberOfPoints(){
+        FunctionUtils.sample(new Sin(), 0.0, Math.PI, -1);
+    }
+
+    @Test(expected = NotStrictlyPositiveException.class)
+    public void testSampleNullNumberOfPoints(){
+        FunctionUtils.sample(new Sin(), 0.0, Math.PI, 0);
+    }
+
+    @Test
+    public void testSample() {
+        final int n = 11;
+        final double min = 0.0;
+        final double max = Math.PI;
+        final double[] actual = FunctionUtils.sample(new Sin(), min, max, n);
+        for (int i = 0; i < n; i++) {
+            final double x = min + (max - min) / n * i;
+            Assert.assertEquals("x = " + x, FastMath.sin(x), actual[i], 0.0);
+        }
+    }
 }



Mime
View raw message