commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From celes...@apache.org
Subject svn commit: r1243263 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math/transform/ test/java/org/apache/commons/math/transform/
Date Sun, 12 Feb 2012 15:18:17 GMT
Author: celestin
Date: Sun Feb 12 15:18:15 2012
New Revision: 1243263

URL: http://svn.apache.org/viewvc?rev=1243263&view=rev
Log:
In package o.a.c.m.transform, created enum DctNormalization to be passed to the constructor
of FastCosineTransformer (instead of boolean parameter).
Made constructor public, and removed factory methods.
See MATH-743

Added:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/transform/DctNormalization.java
  (with props)
Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/transform/DftNormalization.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/transform/FastCosineTransformer.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/transform/FastCosineTransformerTest.java

Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math/transform/DctNormalization.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/transform/DctNormalization.java?rev=1243263&view=auto
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/transform/DctNormalization.java
(added)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/transform/DctNormalization.java
Sun Feb 12 15:18:15 2012
@@ -0,0 +1,70 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math.transform;
+
+/**
+ * This enumeration defines the various types of normalizations that can be
+ * applied to discrete cosine transforms (DCT). The exact definition of these
+ * normalizations is detailed below.
+ *
+ * @see FastCosineTransformer
+ * @version $Id Revision$
+ * @since 3.0
+ */
+public enum DctNormalization {
+    /**
+     * <p>
+     * Should be passed to the constructor of {@link FastCosineTransformer}
+     * to use the <em>standard</em> normalization convention.  The standard
+     * DCT-I normalization convention is defined as follows
+     * <ul>
+     * <li>forward transform:
+     * y<sub>n</sub> = (1/2) [x<sub>0</sub> + (-1)<sup>n</sup>x<sub>N-1</sub>]
+     * + &sum;<sub>k=1</sub><sup>N-2</sup>
+     * x<sub>k</sub> cos[&pi; nk / (N - 1)],</li>
+     * <li>inverse transform:
+     * x<sub>k</sub> = [1 / (N - 1)] [y<sub>0</sub>
+     * + (-1)<sup>k</sup>y<sub>N-1</sub>]
+     * + [2 / (N - 1)] &sum;<sub>n=1</sub><sup>N-2</sup>
+     * y<sub>n</sub> cos[&pi; nk / (N - 1)],</li>
+     * </ul>
+     * where N is the size of the data sample.
+     * </p>
+     */
+    STANDARD_DCT_I,
+
+    /**
+     * <p>
+     * Should be passed to the constructor of {@link FastCosineTransformer}
+     * to use the <em>orthogonal</em> normalization convention. The orthogonal
+     * DCT-I normalization convention is defined as follows
+     * <ul>
+     * <li>forward transform:
+     * y<sub>n</sub> = [2(N - 1)]<sup>-1/2</sup> [x<sub>0</sub>
+     * + (-1)<sup>n</sup>x<sub>N-1</sub>]
+     * + [2 / (N - 1)]<sup>1/2</sup> &sum;<sub>k=1</sub><sup>N-2</sup>
+     * x<sub>k</sub> cos[&pi; nk / (N - 1)],</li>
+     * <li>inverse transform:
+     * x<sub>k</sub> = [2(N - 1)]<sup>-1/2</sup> [y<sub>0</sub>
+     * + (-1)<sup>k</sup>y<sub>N-1</sub>]
+     * + [2 / (N - 1)]<sup>1/2</sup> &sum;<sub>n=1</sub><sup>N-2</sup>
+     * y<sub>n</sub> cos[&pi; nk / (N - 1)],</li>
+     * </ul>
+     * which makes the transform orthogonal. N is the size of the data sample.
+     */
+    ORTHOGONAL_DCT_I;
+}

Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math/transform/DctNormalization.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math/transform/DctNormalization.java
------------------------------------------------------------------------------
    svn:keywords = Id Revision

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/transform/DftNormalization.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/transform/DftNormalization.java?rev=1243263&r1=1243262&r2=1243263&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/transform/DftNormalization.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/transform/DftNormalization.java
Sun Feb 12 15:18:15 2012
@@ -28,9 +28,9 @@ package org.apache.commons.math.transfor
 public enum DftNormalization {
     /**
      * <p>
-     * Should be passed to the constructor of e.g.
-     * {@link FastFourierTransformer} to use the <em>standard</em> normalization
-     * convention. This normalization convention is defined as follows
+     * Should be passed to the constructor of {@link FastFourierTransformer}
+     * to use the <em>standard</em> normalization convention. This normalization
+     * convention is defined as follows
      * <ul>
      * <li>forward transform: y<sub>n</sub> = &sum;<sub>k=0</sub><sup>N-1</sup>
      * x<sub>k</sub> exp(-2&pi;i n k / N),</li>
@@ -44,9 +44,9 @@ public enum DftNormalization {
 
     /**
      * <p>
-     * Should be passed to the constructor of e.g.
-     * {@link FastFourierTransformer} to use the <em>unitary</em> normalization
-     * convention. This normalization convention is defined as follows
+     * Should be passed to the constructor of {@link FastFourierTransformer}
+     * to use the <em>unitary</em> normalization convention. This normalization
+     * convention is defined as follows
      * <ul>
      * <li>forward transform: y<sub>n</sub> = (1 / &radic;N)
      * &sum;<sub>k=0</sub><sup>N-1</sup> x<sub>k</sub>

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=1243263&r1=1243262&r2=1243263&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
Sun Feb 12 15:18:15 2012
@@ -35,48 +35,8 @@ import org.apache.commons.math.util.Fast
  * <p>
  * There are several variants of the discrete cosine transform. The present
  * implementation corresponds to DCT-I, with various normalization conventions,
- * which are described below.
+ * which are specified by the parameter {@link DctNormalization}.
  * </p>
- * <h3><a id="standard">Standard DCT-I</a></h3>
- * <p>
- * The standard normalization convention is defined as follows
- * <ul>
- * <li>forward transform:
- * y<sub>n</sub> = (1/2) [x<sub>0</sub> + (-1)<sup>n</sup>x<sub>N-1</sub>]
- * + &sum;<sub>k=1</sub><sup>N-2</sup>
- * x<sub>k</sub> cos[&pi; nk / (N - 1)],</li>
- * <li>inverse transform:
- * x<sub>k</sub> = [1 / (N - 1)] [y<sub>0</sub>
- * + (-1)<sup>k</sup>y<sub>N-1</sub>]
- * + [2 / (N - 1)] &sum;<sub>n=1</sub><sup>N-2</sup>
- * y<sub>n</sub> cos[&pi; nk / (N - 1)],</li>
- * </ul>
- * where N is the size of the data sample.
- * </p>
- * <p> {@link RealTransformer}s following this convention are returned by the
- * factory method {@link #create()}.
- * </p>
- * <h3><a id="orthogonal">Orthogonal DCT-I</a></h3>
- * <p>
- * The orthogonal normalization convention is defined as follows
- * <ul>
- * <li>forward transform:
- * y<sub>n</sub> = [2(N - 1)]<sup>-1/2</sup> [x<sub>0</sub>
- * + (-1)<sup>n</sup>x<sub>N-1</sub>]
- * + [2 / (N - 1)]<sup>1/2</sup> &sum;<sub>k=1</sub><sup>N-2</sup>
- * x<sub>k</sub> cos[&pi; nk / (N - 1)],</li>
- * <li>inverse transform:
- * x<sub>k</sub> = [2(N - 1)]<sup>-1/2</sup> [y<sub>0</sub>
- * + (-1)<sup>k</sup>y<sub>N-1</sub>]
- * + [2 / (N - 1)]<sup>1/2</sup> &sum;<sub>n=1</sub><sup>N-2</sup>
- * y<sub>n</sub> cos[&pi; nk / (N - 1)],</li>
- * </ul>
- * which makes the transform orthogonal. N is the size of the data sample.
- * </p>
- * <p> {@link RealTransformer}s following this convention are returned by the
- * factory method {@link #createOrthogonal()}.
- * </p>
- * <h3>Link with the DFT, and assumptions on the layout of the data set</h3>
  * <p>
  * DCT-I is equivalent to DFT of an <em>even extension</em> of the data series.
  * More precisely, if x<sub>0</sub>, &hellip;, x<sub>N-1</sub>
is the data set
@@ -105,7 +65,6 @@ import org.apache.commons.math.util.Fast
  * (N&nbsp;=&nbsp;2<sup>n</sup>&nbsp;+&nbsp;1). Besides, it implicitly
assumes
  * that the sampled function is even.
  * </p>
- * <p>As of version 2.0 this no longer implements Serializable.</p>
  *
  * @version $Id$
  * @since 1.2
@@ -113,52 +72,20 @@ import org.apache.commons.math.util.Fast
 public class FastCosineTransformer implements RealTransformer, Serializable {
 
     /** Serializable version identifier. */
-    static final long serialVersionUID = 20120211L;
+    static final long serialVersionUID = 20120212L;
 
-    /**
-     * {@code true} if the orthogonal version of the DCT should be used.
-     *
-     * @see #create()
-     * @see #createOrthogonal()
-     */
-    private final boolean orthogonal;
+    /** The type of DCT to be performed. */
+    private final DctNormalization normalization;
 
     /**
      * Creates a new instance of this class, with various normalization
      * conventions.
      *
-     * @param orthogonal {@code false} if the DCT is <em>not</em> to be scaled,
-     * {@code true} if it is to be scaled so as to make the transform
-     * orthogonal.
-     * @see #create()
-     * @see #createOrthogonal()
-     */
-    private FastCosineTransformer(final boolean orthogonal) {
-        this.orthogonal = orthogonal;
-    }
-
-    /**
-     * <p>
-     * Returns a new instance of this class. The returned transformer uses the
-     * <a href="#standard">standard normalizing conventions</a>.
-     * </p>
-     *
-     * @return a new DCT transformer, with standard normalizing conventions
+     * @param normalization the type of normalization to be applied to the
+     * transformed data
      */
-    public static FastCosineTransformer create() {
-        return new FastCosineTransformer(false);
-    }
-
-    /**
-     * <p>
-     * Returns a new instance of this class. The returned transformer uses the
-     * <a href="#orthogonal">orthogonal normalizing conventions</a>.
-     * </p>
-     *
-     * @return a new DCT transformer, with orthogonal normalizing conventions
-     */
-    public static FastCosineTransformer createOrthogonal() {
-        return new FastCosineTransformer(true);
+    public FastCosineTransformer(final DctNormalization normalization) {
+        this.normalization = normalization;
     }
 
     /**
@@ -169,14 +96,19 @@ public class FastCosineTransformer imple
      */
     public double[] transform(final double[] f, final TransformType type) {
         if (type == TransformType.FORWARD) {
-            if (orthogonal) {
+            if (normalization == DctNormalization.ORTHOGONAL_DCT_I) {
                 final double s = FastMath.sqrt(2.0 / (f.length - 1));
                 return TransformUtils.scaleArray(fct(f), s);
             }
             return fct(f);
         }
         final double s2 = 2.0 / (f.length - 1);
-        final double s1 = orthogonal ? FastMath.sqrt(s2) : s2;
+        final double s1;
+        if (normalization == DctNormalization.ORTHOGONAL_DCT_I) {
+            s1 = FastMath.sqrt(s2);
+        } else {
+            s1 = s2;
+        }
         return TransformUtils.scaleArray(fct(f), s1);
     }
 

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/transform/FastCosineTransformerTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/transform/FastCosineTransformerTest.java?rev=1243263&r1=1243262&r2=1243263&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/transform/FastCosineTransformerTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/transform/FastCosineTransformerTest.java
Sun Feb 12 15:18:15 2012
@@ -42,7 +42,7 @@ import org.junit.runners.Parameterized.P
 public final class FastCosineTransformerTest
     extends RealTransformerAbstractTest {
 
-    private final boolean standard;
+    private DctNormalization normalization;
 
     private final int[] invalidDataSize;
 
@@ -50,8 +50,8 @@ public final class FastCosineTransformer
 
     private final int[] validDataSize;
 
-    public FastCosineTransformerTest(final boolean standard) {
-        this.standard = standard;
+    public FastCosineTransformerTest(final DctNormalization normalization) {
+        this.normalization = normalization;
         this.validDataSize = new int[] {
             2, 3, 5, 9, 17, 33, 65, 129
         };
@@ -71,23 +71,17 @@ public final class FastCosineTransformer
      */
     @Parameters
     public static Collection<Object[]> data() {
-        final Object[][] data = new Boolean[][] {
-            {
-                Boolean.TRUE
-            }, {
-                Boolean.FALSE
-            }
-        };
+        final DctNormalization[] normalization = DctNormalization.values();
+        final Object[][] data = new DctNormalization[normalization.length][1];
+        for (int i = 0; i < normalization.length; i++){
+            data[i][0] = normalization[i];
+        }
         return Arrays.asList(data);
     }
 
     @Override
     RealTransformer createRealTransformer() {
-        if (standard) {
-            return FastCosineTransformer.create();
-        } else {
-            return FastCosineTransformer.createOrthogonal();
-        }
+        return new FastCosineTransformer(normalization);
     }
 
     @Override
@@ -149,9 +143,21 @@ public final class FastCosineTransformer
         }
         final double s;
         if (type == TransformType.FORWARD) {
-            s = standard ? 1.0 : FastMath.sqrt(2.0 / (n - 1.0));
+            if (normalization == DctNormalization.STANDARD_DCT_I) {
+                s = 1.0;
+            } else if (normalization == DctNormalization.ORTHOGONAL_DCT_I) {
+                s = FastMath.sqrt(2.0 / (n - 1.0));
+            } else {
+                throw new MathIllegalStateException();
+            }
         } else if (type == TransformType.INVERSE) {
-            s = standard ? 2.0 / (n - 1.0) : FastMath.sqrt(2.0 / (n - 1.0));
+            if (normalization == DctNormalization.STANDARD_DCT_I) {
+                s = 2.0 / (n - 1.0);
+            } else if (normalization == DctNormalization.ORTHOGONAL_DCT_I) {
+                s = FastMath.sqrt(2.0 / (n - 1.0));
+            } else {
+                throw new MathIllegalStateException();
+            }
         } else {
             /*
              * Should never occur. This clause is a safeguard in case other
@@ -170,7 +176,8 @@ public final class FastCosineTransformer
     /** Test of transformer for the ad hoc data. */
     @Test
     public void testAdHocData() {
-        FastCosineTransformer transformer = FastCosineTransformer.create();
+        FastCosineTransformer transformer;
+        transformer = new FastCosineTransformer(DctNormalization.STANDARD_DCT_I);
         double result[], tolerance = 1E-12;
 
         double x[] = {
@@ -195,7 +202,7 @@ public final class FastCosineTransformer
 
         TransformUtils.scaleArray(x, FastMath.sqrt(0.5 * (x.length - 1)));
 
-        transformer = FastCosineTransformer.createOrthogonal();
+        transformer = new FastCosineTransformer(DctNormalization.ORTHOGONAL_DCT_I);
         result = transformer.transform(y, TransformType.FORWARD);
         for (int i = 0; i < result.length; i++) {
             Assert.assertEquals(x[i], result[i], tolerance);
@@ -212,7 +219,8 @@ public final class FastCosineTransformer
     public void testParameters()
         throws Exception {
         UnivariateFunction f = new SinFunction();
-        FastCosineTransformer transformer = FastCosineTransformer.create();
+        FastCosineTransformer transformer;
+        transformer = new FastCosineTransformer(DctNormalization.STANDARD_DCT_I);
 
         try {
             // bad interval
@@ -243,7 +251,8 @@ public final class FastCosineTransformer
     @Test
     public void testSinFunction() {
         UnivariateFunction f = new SinFunction();
-        FastCosineTransformer transformer = FastCosineTransformer.create();
+        FastCosineTransformer transformer;
+        transformer = new FastCosineTransformer(DctNormalization.STANDARD_DCT_I);
         double min, max, result[], tolerance = 1E-12;
         int N = 9;
 



Mime
View raw message