1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
32
33
34 public final class FastHadamardTransformerTest {
35
36
37
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
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
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
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
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
88 FastHadamardTransformer transformer = new FastHadamardTransformer();
89
90
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
98 Assert.assertTrue(Precision.equals(y[i], dResult[i], 1));
99 }
100 }
101
102 private void checkIntTransform(int[]x, int[] y) {
103
104 FastHadamardTransformer transformer = new FastHadamardTransformer();
105
106
107 int[] iResult = transformer.transform(x);
108 for (int i = 0; i < iResult.length; i++) {
109
110 Assert.assertEquals(y[i], iResult[i]);
111 }
112
113 }
114
115 private void checkInverseDoubleTransform(int[]x, int[] y) {
116
117 FastHadamardTransformer transformer = new FastHadamardTransformer();
118
119
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
127 Assert.assertTrue(Precision.equals(x[i], dResult[i], 1));
128 }
129
130 }
131
132 }