001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.math.transform;
018    
019    import org.apache.commons.math.analysis.*;
020    import org.apache.commons.math.MathException;
021    import junit.framework.TestCase;
022    
023    /**
024     * Testcase for fast cosine transformer.
025     * <p>
026     * FCT algorithm is exact, the small tolerance number is used only
027     * to account for round-off errors.
028     * 
029     * @version $Revision: 764769 $ $Date: 2009-04-14 09:13:54 -0400 (Tue, 14 Apr 2009) $ 
030     */
031    public final class FastCosineTransformerTest extends TestCase {
032    
033        /**
034         * Test of transformer for the ad hoc data.
035         */
036        public void testAdHocData() {
037            FastCosineTransformer transformer = new FastCosineTransformer();
038            double result[], tolerance = 1E-12;
039    
040            double x[] = { 0.0, 1.0, 4.0, 9.0, 16.0, 25.0, 36.0, 49.0, 64.0 };
041            double y[] = { 172.0, -105.096569476353, 27.3137084989848,
042                          -12.9593152353742, 8.0, -5.78585076868676,
043                           4.68629150101524, -4.15826451958632, 4.0 };
044    
045            result = transformer.transform(x);
046            for (int i = 0; i < result.length; i++) {
047                assertEquals(y[i], result[i], tolerance);
048            }
049    
050            result = transformer.inversetransform(y);
051            for (int i = 0; i < result.length; i++) {
052                assertEquals(x[i], result[i], tolerance);
053            }
054    
055            FastFourierTransformer.scaleArray(x, Math.sqrt(0.5 * (x.length-1)));
056    
057            result = transformer.transform2(y);
058            for (int i = 0; i < result.length; i++) {
059                assertEquals(x[i], result[i], tolerance);
060            }
061    
062            result = transformer.inversetransform2(x);
063            for (int i = 0; i < result.length; i++) {
064                assertEquals(y[i], result[i], tolerance);
065            }
066        }
067    
068        /**
069         * Test of transformer for the sine function.
070         */
071        public void testSinFunction() throws MathException {
072            UnivariateRealFunction f = new SinFunction();
073            FastCosineTransformer transformer = new FastCosineTransformer();
074            double min, max, result[], tolerance = 1E-12; int N = 9;
075    
076            double expected[] = { 0.0, 3.26197262739567, 0.0,
077                                 -2.17958042710327, 0.0, -0.648846697642915,
078                                  0.0, -0.433545502649478, 0.0 };
079            min = 0.0; max = 2.0 * Math.PI * N / (N-1);
080            result = transformer.transform(f, min, max, N);
081            for (int i = 0; i < N; i++) {
082                assertEquals(expected[i], result[i], tolerance);
083            }
084    
085            min = -Math.PI; max = Math.PI * (N+1) / (N-1);
086            result = transformer.transform(f, min, max, N);
087            for (int i = 0; i < N; i++) {
088                assertEquals(-expected[i], result[i], tolerance);
089            }
090        }
091    
092        /**
093         * Test of parameters for the transformer.
094         */
095        public void testParameters() throws Exception {
096            UnivariateRealFunction f = new SinFunction();
097            FastCosineTransformer transformer = new FastCosineTransformer();
098    
099            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    }