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