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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
package org.apache.commons.math.transform;
19
import junit.framework.TestCase;
22
* JUnit Test for HadamardTransformerTest
23
* @see org.apache.commons.math.transform.FastHadamardTransformer
25
public final class FastHadamardTransformerTest extends TestCase {
28
* Test of transformer for the a 8-point FHT (means n=8)
30
public void test8Points() {
31
checkAllTransforms(new int[] { 1, 4, -2, 3, 0, 1, 4, -1 },
32
new int[] { 10, -4, 2, -4, 2, -12, 6, 8 });
36
* Test of transformer for the a 4-points FHT (means n=4)
38
public void test4Points() {
39
checkAllTransforms(new int[] { 1, 2, 3, 4 },
40
new int[] { 10, -2, -4, 0 });
44
* Test the inverse transform of an integer vector is not always an integer vector
46
public void testNoIntInverse() {
47
FastHadamardTransformer transformer = new FastHadamardTransformer();
48
double[] x = transformer.inversetransform(new double[] { 0, 1, 0, 1});
49
assertEquals( 0.5, x[0], 0);
50
assertEquals(-0.5, x[1], 0);
51
assertEquals( 0.0, x[2], 0);
52
assertEquals( 0.0, x[3], 0);
56
* Test of transformer for wrong number of points
58
public void test3Points() {
60
new FastHadamardTransformer().transform(new double[3]);
61
fail("an exception should have been thrown");
62
} catch (IllegalArgumentException iae) {
67
private void checkAllTransforms(int[]x, int[] y) {
68
checkDoubleTransform(x, y);
69
checkInverseDoubleTransform(x, y);
70
checkIntTransform(x, y);
73
private void checkDoubleTransform(int[]x, int[] y) {
74
// Initiate the transformer
75
FastHadamardTransformer transformer = new FastHadamardTransformer();
77
// check double transform
78
double[] dX = new double[x.length];
79
for (int i = 0; i < dX.length; ++i) {
82
double dResult[] = transformer.transform(dX);
83
for (int i = 0; i < dResult.length; i++) {
84
// compare computed results to precomputed results
85
assertEquals((double) y[i], dResult[i]);
89
private void checkIntTransform(int[]x, int[] y) {
90
// Initiate the transformer
91
FastHadamardTransformer transformer = new FastHadamardTransformer();
93
// check integer transform
94
int iResult[] = transformer.transform(x);
95
for (int i = 0; i < iResult.length; i++) {
96
// compare computed results to precomputed results
97
assertEquals(y[i], iResult[i]);
102
private void checkInverseDoubleTransform(int[]x, int[] y) {
103
// Initiate the transformer
104
FastHadamardTransformer transformer = new FastHadamardTransformer();
106
// check double transform
107
double[] dY = new double[y.length];
108
for (int i = 0; i < dY.length; ++i) {
111
double dResult[] = transformer.inversetransform(dY);
112
for (int i = 0; i < dResult.length; i++) {
113
// compare computed results to precomputed results
114
assertEquals((double) x[i], dResult[i]);