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 sine transformer. 025 * <p> 026 * FST 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 FastSineTransformerTest extends TestCase { 032 033 /** 034 * Test of transformer for the ad hoc data. 035 */ 036 public void testAdHocData() { 037 FastSineTransformer transformer = new FastSineTransformer(); 038 double result[], tolerance = 1E-12; 039 040 double x[] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 }; 041 double y[] = { 0.0, 20.1093579685034, -9.65685424949238, 042 5.98642305066196, -4.0, 2.67271455167720, 043 -1.65685424949238, 0.795649469518633 }; 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(x.length / 2.0)); 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 FastSineTransformer transformer = new FastSineTransformer(); 074 double min, max, result[], tolerance = 1E-12; int N = 1 << 8; 075 076 min = 0.0; max = 2.0 * Math.PI; 077 result = transformer.transform(f, min, max, N); 078 assertEquals(N >> 1, result[2], tolerance); 079 for (int i = 0; i < N; i += (i == 1 ? 2 : 1)) { 080 assertEquals(0.0, result[i], tolerance); 081 } 082 083 min = -Math.PI; max = Math.PI; 084 result = transformer.transform(f, min, max, N); 085 assertEquals(-(N >> 1), result[2], tolerance); 086 for (int i = 0; i < N; i += (i == 1 ? 2 : 1)) { 087 assertEquals(0.0, result[i], tolerance); 088 } 089 } 090 091 /** 092 * Test of parameters for the transformer. 093 */ 094 public void testParameters() throws Exception { 095 UnivariateRealFunction f = new SinFunction(); 096 FastSineTransformer transformer = new FastSineTransformer(); 097 098 try { 099 // 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 }