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    *      http://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  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   * Testcase for fast cosine transformer.
25   * <p>
26   * FCT algorithm is exact, the small tolerance number is used only
27   * to account for round-off errors.
28   * 
29   * @version $Revision: 764769 $ $Date: 2009-04-14 09:13:54 -0400 (Tue, 14 Apr 2009) $ 
30   */
31  public final class FastCosineTransformerTest extends TestCase {
32  
33      /**
34       * Test of transformer for the ad hoc data.
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       * Test of transformer for the sine function.
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       * Test of parameters for the transformer.
94       */
95      public void testParameters() throws Exception {
96          UnivariateRealFunction f = new SinFunction();
97          FastCosineTransformer transformer = new FastCosineTransformer();
98  
99          try {
100             // bad interval
101             transformer.transform(f, 1, -1, 65);
102             fail("Expecting IllegalArgumentException - bad interval");
103         } catch (IllegalArgumentException ex) {
104             // expected
105         }
106         try {
107             // bad samples number
108             transformer.transform(f, -1, 1, 1);
109             fail("Expecting IllegalArgumentException - bad samples number");
110         } catch (IllegalArgumentException ex) {
111             // expected
112         }
113         try {
114             // bad samples number
115             transformer.transform(f, -1, 1, 64);
116             fail("Expecting IllegalArgumentException - bad samples number");
117         } catch (IllegalArgumentException ex) {
118             // expected
119         }
120     }
121 }