diff --git a/src/main/java/org/apache/commons/math/transform/FastCosineTransformer.java b/src/main/java/org/apache/commons/math/transform/FastCosineTransformer.java index 69fbc97e8..51b1a45b4 100644 --- a/src/main/java/org/apache/commons/math/transform/FastCosineTransformer.java +++ b/src/main/java/org/apache/commons/math/transform/FastCosineTransformer.java @@ -40,167 +40,142 @@ import org.apache.commons.math.util.FastMath; * @since 1.2 */ public class FastCosineTransformer implements RealTransformer { + /** + * {@code true} if the orthogonal version of the DCT should be used. + * + * @see #create() + * @see #createOrthogonal() + */ + private final boolean orthogonal; - /** Construct a default transformer. */ - public FastCosineTransformer() { - super(); + /** + * Creates a new instance of this class, with various normalization + * conventions. + * + * @param orthogonal {@code false} if the DCT is not to be scaled, + * {@code true} if it is to be scaled so as to make the transform + * orthogonal. + * @see #create() + * @see #createOrthogonal() + */ + public FastCosineTransformer(final boolean orthogonal) { + this.orthogonal = orthogonal; } /** - * Transform the given real data set. *
- * The formula is Fn = (1/2) [f0 + (-1)n fN] + - * ∑k=1N-1 fk cos(π nk/N) + * Returns a new instance of this class. The returned transformer uses the + * normalizing conventions described below. + *
+ * Returns a new instance of this class. The returned transformer uses the + * normalizing conventions described below. + *
- * The formula is Fn = (1/2) [f0 + (-1)n fN] + - * ∑k=1N-1 fk cos(π nk/N) - *
+ * Returns the forward transform of the specified real function, sampled on + * the specified interval. * * @param f the function to be sampled and transformed - * @param min the lower bound for the interval - * @param max the upper bound for the interval + * @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 real transformed array * @throws IllegalArgumentException if any parameters are invalid */ public double[] transform(UnivariateFunction f, - double min, double max, int n) - throws IllegalArgumentException { - double[] data = FastFourierTransformer.sample(f, min, max, n); - return fct(data); + double min, double max, int n) throws IllegalArgumentException { + + final double[] data = FastFourierTransformer.sample(f, min, max, n); + return transform(data); } /** - * Transform the given real data set. - *- * The formula is Fn = √(1/2N) [f0 + (-1)n fN] + - * √(2/N) ∑k=1N-1 fk cos(π nk/N) - *
- * - * @param f the real data array to be transformed - * @return the real transformed array - * @throws IllegalArgumentException if any parameters are invalid - */ - public double[] transform2(double[] f) throws IllegalArgumentException { - - double scalingCoefficient = FastMath.sqrt(2.0 / (f.length - 1)); - return FastFourierTransformer.scaleArray(fct(f), scalingCoefficient); - } - - /** - * Transform the given real function, sampled on the given interval. - *- * The formula is Fn = √(1/2N) [f0 + (-1)n fN] + - * √(2/N) ∑k=1N-1 fk cos(π nk/N) - * - *
- * - * @param f the function to be sampled and transformed - * @param min the lower bound for the interval - * @param max the upper bound for the interval - * @param n the number of sample points - * @return the real transformed array - * @throws IllegalArgumentException if any parameters are invalid - */ - public double[] transform2(UnivariateFunction f, - double min, double max, int n) - throws IllegalArgumentException { - - double[] data = FastFourierTransformer.sample(f, min, max, n); - double scalingCoefficient = FastMath.sqrt(2.0 / (n - 1)); - return FastFourierTransformer.scaleArray(fct(data), scalingCoefficient); - } - - /** - * Inversely transform the given real data set. - *- * The formula is fk = (1/N) [F0 + (-1)k FN] + - * (2/N) ∑n=1N-1 Fn cos(π nk/N) - *
+ * Returns the inverse transform of the specified real data set. * * @param f the real data array to be inversely transformed * @return the real inversely transformed array * @throws IllegalArgumentException if any parameters are invalid */ public double[] inverseTransform(double[] f) - throws IllegalArgumentException { + throws IllegalArgumentException { - double scalingCoefficient = 2.0 / (f.length - 1); - return FastFourierTransformer.scaleArray(fct(f), scalingCoefficient); + final double s2 = 2.0 / (f.length - 1); + final double s1 = orthogonal ? FastMath.sqrt(s2) : s2; + return FastFourierTransformer.scaleArray(fct(f), s1); } /** - * Inversely transform the given real function, sampled on the given - * interval. - *- * The formula is fk = (1/N) [F0 + (-1)k FN] + - * (2/N) ∑n=1N-1 Fn cos(π nk/N) - *
+ * Returns the inverse transform of the specified real function, sampled + * on the given interval. * * @param f the function to be sampled and inversely transformed - * @param min the lower bound for the interval - * @param max the upper bound for the interval + * @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 real inversely transformed array * @throws IllegalArgumentException if any parameters are invalid */ public double[] inverseTransform(UnivariateFunction f, - double min, double max, int n) - throws IllegalArgumentException { + double min, double max, int n) throws IllegalArgumentException { - double[] data = FastFourierTransformer.sample(f, min, max, n); - double scalingCoefficient = 2.0 / (n - 1); - return FastFourierTransformer.scaleArray(fct(data), scalingCoefficient); - } - - /** - * Inversely transform the given real data set. - *- * The formula is fk = √(1/2N) [F0 + (-1)k FN] + - * √(2/N) ∑n=1N-1 Fn cos(π nk/N) - *
- * - * @param f the real data array to be inversely transformed - * @return the real inversely transformed array - * @throws IllegalArgumentException if any parameters are invalid - */ - public double[] inverseTransform2(double[] f) - throws IllegalArgumentException { - return transform2(f); - } - - /** - * Inversely transform the given real function, sampled on the given - * interval. - *- * The formula is fk = √(1/2N) [F0 + (-1)k FN] + - * √(2/N) ∑n=1N-1 Fn cos(π nk/N) - *
- * - * @param f the function to be sampled and inversely transformed - * @param min the lower bound for the interval - * @param max the upper bound for the interval - * @param n the number of sample points - * @return the real inversely transformed array - * @throws IllegalArgumentException if any parameters are invalid - */ - public double[] inverseTransform2(UnivariateFunction f, - double min, double max, int n) - throws IllegalArgumentException { - - return transform2(f, min, max, n); + final double[] data = FastFourierTransformer.sample(f, min, max, n); + return inverseTransform(data); } /** diff --git a/src/test/java/org/apache/commons/math/transform/FastCosineTransformerTest.java b/src/test/java/org/apache/commons/math/transform/FastCosineTransformerTest.java index 1c94d04e4..881a522c4 100644 --- a/src/test/java/org/apache/commons/math/transform/FastCosineTransformerTest.java +++ b/src/test/java/org/apache/commons/math/transform/FastCosineTransformerTest.java @@ -36,7 +36,7 @@ public final class FastCosineTransformerTest { */ @Test public void testAdHocData() { - FastCosineTransformer transformer = new FastCosineTransformer(); + FastCosineTransformer transformer = FastCosineTransformer.create(); double result[], tolerance = 1E-12; double x[] = { 0.0, 1.0, 4.0, 9.0, 16.0, 25.0, 36.0, 49.0, 64.0 }; @@ -56,12 +56,13 @@ public final class FastCosineTransformerTest { FastFourierTransformer.scaleArray(x, FastMath.sqrt(0.5 * (x.length-1))); - result = transformer.transform2(y); + transformer = FastCosineTransformer.createOrthogonal(); + result = transformer.transform(y); for (int i = 0; i < result.length; i++) { Assert.assertEquals(x[i], result[i], tolerance); } - result = transformer.inverseTransform2(x); + result = transformer.inverseTransform(x); for (int i = 0; i < result.length; i++) { Assert.assertEquals(y[i], result[i], tolerance); } @@ -73,7 +74,7 @@ public final class FastCosineTransformerTest { @Test public void testSinFunction() { UnivariateFunction f = new SinFunction(); - FastCosineTransformer transformer = new FastCosineTransformer(); + FastCosineTransformer transformer = FastCosineTransformer.create(); double min, max, result[], tolerance = 1E-12; int N = 9; double expected[] = { 0.0, 3.26197262739567, 0.0, @@ -98,7 +99,7 @@ public final class FastCosineTransformerTest { @Test public void testParameters() throws Exception { UnivariateFunction f = new SinFunction(); - FastCosineTransformer transformer = new FastCosineTransformer(); + FastCosineTransformer transformer = FastCosineTransformer.create(); try { // bad interval