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 sine transformer.
25   * <p>
26   * FST 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 FastSineTransformerTest extends TestCase {
32  
33      /**
34       * Test of transformer for the ad hoc data.
35       */
36      public void testAdHocData() {
37          FastSineTransformer transformer = new FastSineTransformer();
38          double result[], tolerance = 1E-12;
39  
40          double x[] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 };
41          double y[] = { 0.0, 20.1093579685034, -9.65685424949238,
42                         5.98642305066196, -4.0, 2.67271455167720,
43                        -1.65685424949238, 0.795649469518633 };
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(x.length / 2.0));
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          FastSineTransformer transformer = new FastSineTransformer();
74          double min, max, result[], tolerance = 1E-12; int N = 1 << 8;
75  
76          min = 0.0; max = 2.0 * Math.PI;
77          result = transformer.transform(f, min, max, N);
78          assertEquals(N >> 1, result[2], tolerance);
79          for (int i = 0; i < N; i += (i == 1 ? 2 : 1)) {
80              assertEquals(0.0, result[i], tolerance);
81          }
82  
83          min = -Math.PI; max = Math.PI;
84          result = transformer.transform(f, min, max, N);
85          assertEquals(-(N >> 1), result[2], tolerance);
86          for (int i = 0; i < N; i += (i == 1 ? 2 : 1)) {
87              assertEquals(0.0, result[i], tolerance);
88          }
89      }
90  
91      /**
92       * Test of parameters for the transformer.
93       */
94      public void testParameters() throws Exception {
95          UnivariateRealFunction f = new SinFunction();
96          FastSineTransformer transformer = new FastSineTransformer();
97  
98          try {
99              // bad interval
100             transformer.transform(f, 1, -1, 64);
101             fail("Expecting IllegalArgumentException - bad interval");
102         } catch (IllegalArgumentException ex) {
103             // expected
104         }
105         try {
106             // bad samples number
107             transformer.transform(f, -1, 1, 0);
108             fail("Expecting IllegalArgumentException - bad samples number");
109         } catch (IllegalArgumentException ex) {
110             // expected
111         }
112         try {
113             // bad samples number
114             transformer.transform(f, -1, 1, 100);
115             fail("Expecting IllegalArgumentException - bad samples number");
116         } catch (IllegalArgumentException ex) {
117             // expected
118         }
119     }
120 }