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    *      https://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  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  package org.hipparchus.transform;
23  
24  import org.hipparchus.exception.MathIllegalArgumentException;
25  import org.hipparchus.util.Precision;
26  import org.junit.Assert;
27  import org.junit.Test;
28  
29  
30  /**
31   * JUnit Test for HadamardTransformerTest
32   * @see org.hipparchus.transform.FastHadamardTransformer
33   */
34  public final class FastHadamardTransformerTest {
35  
36      /**
37       * Test of transformer for the a 8-point FHT (means n=8)
38       */
39      @Test
40      public void test8Points() {
41          checkAllTransforms(new int[] { 1, 4, -2, 3, 0, 1, 4, -1 },
42                         new int[] { 10, -4, 2, -4, 2, -12, 6, 8 });
43      }
44  
45      /**
46       * Test of transformer for the a 4-points FHT (means n=4)
47       */
48      @Test
49      public void test4Points() {
50          checkAllTransforms(new int[] { 1, 2, 3, 4 },
51                             new int[] { 10, -2, -4, 0 });
52      }
53  
54      /**
55       * Test the inverse transform of an integer vector is not always an integer vector
56       */
57      @Test
58      public void testNoIntInverse() {
59          FastHadamardTransformer transformer = new FastHadamardTransformer();
60          double[] x = transformer.transform(new double[] { 0, 1, 0, 1}, TransformType.INVERSE);
61          Assert.assertEquals( 0.5, x[0], 0);
62          Assert.assertEquals(-0.5, x[1], 0);
63          Assert.assertEquals( 0.0, x[2], 0);
64          Assert.assertEquals( 0.0, x[3], 0);
65      }
66  
67      /**
68       * Test of transformer for wrong number of points
69       */
70      @Test
71      public void test3Points() {
72          try {
73              new FastHadamardTransformer().transform(new double[3], TransformType.FORWARD);
74              Assert.fail("an exception should have been thrown");
75          } catch (MathIllegalArgumentException iae) {
76              // expected
77          }
78      }
79  
80      private void checkAllTransforms(int[]x, int[] y) {
81          checkDoubleTransform(x, y);
82          checkInverseDoubleTransform(x, y);
83          checkIntTransform(x, y);
84      }
85  
86      private void checkDoubleTransform(int[]x, int[] y) {
87          // Initiate the transformer
88          FastHadamardTransformer transformer = new FastHadamardTransformer();
89  
90          // check double transform
91          double[] dX = new double[x.length];
92          for (int i = 0; i < dX.length; ++i) {
93              dX[i] = x[i];
94          }
95          double[] dResult = transformer.transform(dX, TransformType.FORWARD);
96          for (int i = 0; i < dResult.length; i++) {
97              // compare computed results to precomputed results
98              Assert.assertTrue(Precision.equals(y[i], dResult[i], 1));
99          }
100     }
101 
102     private void checkIntTransform(int[]x, int[] y) {
103         // Initiate the transformer
104         FastHadamardTransformer transformer = new FastHadamardTransformer();
105 
106         // check integer transform
107         int[] iResult = transformer.transform(x);
108         for (int i = 0; i < iResult.length; i++) {
109             // compare computed results to precomputed results
110             Assert.assertEquals(y[i], iResult[i]);
111         }
112 
113     }
114 
115     private void checkInverseDoubleTransform(int[]x, int[] y) {
116         // Initiate the transformer
117         FastHadamardTransformer transformer = new FastHadamardTransformer();
118 
119         // check double transform
120         double[] dY = new double[y.length];
121         for (int i = 0; i < dY.length; ++i) {
122             dY[i] = y[i];
123         }
124         double[] dResult = transformer.transform(dY, TransformType.INVERSE);
125         for (int i = 0; i < dResult.length; i++) {
126             // compare computed results to precomputed results
127             Assert.assertTrue(Precision.equals(x[i], dResult[i], 1));
128         }
129 
130     }
131 
132 }