View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.math4.transform;
18  
19  import org.junit.Assert;
20  import org.junit.Test;
21  
22  import org.apache.commons.numbers.core.Precision;
23  
24  
25  /**
26   * Test for {@link FastHadamardTransform}.
27   */
28  public final class FastHadamardTransformerTest {
29      /**
30       * Test of transformer for the a 8-point FHT (means n=8)
31       */
32      @Test
33      public void test8Points() {
34          checkAllTransforms(new int[] {1, 4, -2, 3, 0, 1, 4, -1},
35                             new int[] {10, -4, 2, -4, 2, -12, 6, 8});
36      }
37  
38      /**
39       * Test of transformer for the a 4-points FHT (means n=4)
40       */
41      @Test
42      public void test4Points() {
43          checkAllTransforms(new int[] {1, 2, 3, 4},
44                             new int[] {10, -2, -4, 0});
45      }
46  
47      /**
48       * Test the inverse transform of an integer vector is not always an integer vector
49       */
50      @Test
51      public void testNoIntInverse() {
52          final FastHadamardTransform transformer = new FastHadamardTransform(true);
53          final double[] x = transformer.apply(new double[] {0, 1, 0, 1});
54          Assert.assertEquals(0.5, x[0], 0);
55          Assert.assertEquals(-0.5, x[1], 0);
56          Assert.assertEquals(0.0, x[2], 0);
57          Assert.assertEquals(0.0, x[3], 0);
58      }
59  
60      /**
61       * Test of transformer for wrong number of points
62       */
63      @Test
64      public void test3Points() {
65          try {
66              new FastHadamardTransform().apply(new double[3]);
67              Assert.fail("an exception should have been thrown");
68          } catch (IllegalArgumentException iae) {
69              // expected
70          }
71      }
72  
73      private void checkAllTransforms(int[] x, int[] y) {
74          checkDoubleTransform(x, y);
75          checkInverseDoubleTransform(x, y);
76          checkIntTransform(x, y);
77      }
78  
79      private void checkDoubleTransform(int[] x, int[] y) {
80          // Initiate the transformer
81          final FastHadamardTransform transformer = new FastHadamardTransform();
82  
83          // check double transform
84          final double[] dX = new double[x.length];
85          for (int i = 0; i < dX.length; ++i) {
86              dX[i] = x[i];
87          }
88          final double[] dResult = transformer.apply(dX);
89          for (int i = 0; i < dResult.length; i++) {
90              // compare computed results to precomputed results
91              Assert.assertTrue(Precision.equals(y[i], dResult[i], 1));
92          }
93      }
94  
95      private void checkIntTransform(int[] x, int[] y) {
96          // Initiate the transformer
97          final FastHadamardTransform transformer = new FastHadamardTransform();
98  
99          // check integer transform
100         final int[] iResult = transformer.apply(x);
101         for (int i = 0; i < iResult.length; i++) {
102             // compare computed results to precomputed results
103             Assert.assertEquals(y[i], iResult[i]);
104         }
105     }
106 
107     private void checkInverseDoubleTransform(int[]x, int[] y) {
108         // Initiate the transformer
109         final FastHadamardTransform transformer = new FastHadamardTransform(true);
110 
111         // check double transform
112         final double[] dY = new double[y.length];
113         for (int i = 0; i < dY.length; ++i) {
114             dY[i] = y[i];
115         }
116         final double[] dResult = transformer.apply(dY);
117         for (int i = 0; i < dResult.length; i++) {
118             // compare computed results to precomputed results
119             Assert.assertTrue(Precision.equals(x[i], dResult[i], 1));
120         }
121     }
122 }