1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.math.transform;
18
19 import org.apache.commons.math.analysis.*;
20 import org.apache.commons.math.MathException;
21 import junit.framework.TestCase;
22
23
24
25
26
27
28
29
30
31 public final class FastCosineTransformerTest extends TestCase {
32
33
34
35
36 public void testAdHocData() {
37 FastCosineTransformer transformer = new FastCosineTransformer();
38 double result[], tolerance = 1E-12;
39
40 double x[] = { 0.0, 1.0, 4.0, 9.0, 16.0, 25.0, 36.0, 49.0, 64.0 };
41 double y[] = { 172.0, -105.096569476353, 27.3137084989848,
42 -12.9593152353742, 8.0, -5.78585076868676,
43 4.68629150101524, -4.15826451958632, 4.0 };
44
45 result = transformer.transform(x);
46 for (int i = 0; i < result.length; i++) {
47 assertEquals(y[i], result[i], tolerance);
48 }
49
50 result = transformer.inversetransform(y);
51 for (int i = 0; i < result.length; i++) {
52 assertEquals(x[i], result[i], tolerance);
53 }
54
55 FastFourierTransformer.scaleArray(x, Math.sqrt(0.5 * (x.length-1)));
56
57 result = transformer.transform2(y);
58 for (int i = 0; i < result.length; i++) {
59 assertEquals(x[i], result[i], tolerance);
60 }
61
62 result = transformer.inversetransform2(x);
63 for (int i = 0; i < result.length; i++) {
64 assertEquals(y[i], result[i], tolerance);
65 }
66 }
67
68
69
70
71 public void testSinFunction() throws MathException {
72 UnivariateRealFunction f = new SinFunction();
73 FastCosineTransformer transformer = new FastCosineTransformer();
74 double min, max, result[], tolerance = 1E-12; int N = 9;
75
76 double expected[] = { 0.0, 3.26197262739567, 0.0,
77 -2.17958042710327, 0.0, -0.648846697642915,
78 0.0, -0.433545502649478, 0.0 };
79 min = 0.0; max = 2.0 * Math.PI * N / (N-1);
80 result = transformer.transform(f, min, max, N);
81 for (int i = 0; i < N; i++) {
82 assertEquals(expected[i], result[i], tolerance);
83 }
84
85 min = -Math.PI; max = Math.PI * (N+1) / (N-1);
86 result = transformer.transform(f, min, max, N);
87 for (int i = 0; i < N; i++) {
88 assertEquals(-expected[i], result[i], tolerance);
89 }
90 }
91
92
93
94
95 public void testParameters() throws Exception {
96 UnivariateRealFunction f = new SinFunction();
97 FastCosineTransformer transformer = new FastCosineTransformer();
98
99 try {
100
101 transformer.transform(f, 1, -1, 65);
102 fail("Expecting IllegalArgumentException - bad interval");
103 } catch (IllegalArgumentException ex) {
104
105 }
106 try {
107
108 transformer.transform(f, -1, 1, 1);
109 fail("Expecting IllegalArgumentException - bad samples number");
110 } catch (IllegalArgumentException ex) {
111
112 }
113 try {
114
115 transformer.transform(f, -1, 1, 64);
116 fail("Expecting IllegalArgumentException - bad samples number");
117 } catch (IllegalArgumentException ex) {
118
119 }
120 }
121 }