FastCosineTransformer implements RealTransformer, Serializable { public double[] transform(final double[] f, final TransformType type) throws MathIllegalArgumentException { if (type == TransformType.FORWARD) { if (normalization == DctNormalization.ORTHOGONAL_DCT_I) { final double s = FastMath.sqrt(2.0 / (f.length - 1)); return TransformUtils.scaleArray(fct(f), s); } return fct(f); } final double s2 = 2.0 / (f.length - 1); final double s1; if (normalization == DctNormalization.ORTHOGONAL_DCT_I) { s1 = FastMath.sqrt(s2); } else { s1 = s2; } return TransformUtils.scaleArray(fct(f), s1); } FastCosineTransformer(final DctNormalization normalization); double[] transform(final double[] f, final TransformType type); double[] transform(final UnivariateFunction f,
        final double min, final double max, final int n,
        final TransformType type);  }
@Test public void testAdHocData() { FastCosineTransformer transformer; transformer = new FastCosineTransformer(DctNormalization.STANDARD_DCT_I); double result[], tolerance = 1E-12; double x[] = { 0.0, 1.0, 4.0, 9.0, 16.0, 25.0, 36.0, 49.0, 64.0 }; double y[] = { 172.0, -105.096569476353, 27.3137084989848, -12.9593152353742, 8.0, -5.78585076868676, 4.68629150101524, -4.15826451958632, 4.0 }; result = transformer.transform(x, TransformType.FORWARD); for (int i = 0; i < result.length; i++) { Assert.assertEquals(y[i], result[i], tolerance); } result = transformer.transform(y, TransformType.INVERSE); for (int i = 0; i < result.length; i++) { Assert.assertEquals(x[i], result[i], tolerance); } TransformUtils.scaleArray(x, FastMath.sqrt(0.5 * (x.length - 1))); transformer = new FastCosineTransformer(DctNormalization.ORTHOGONAL_DCT_I); result = transformer.transform(y, TransformType.FORWARD); for (int i = 0; i < result.length; i++) { Assert.assertEquals(x[i], result[i], tolerance); } result = transformer.transform(x, TransformType.INVERSE); for (int i = 0; i < result.length; i++) { Assert.assertEquals(y[i], result[i], tolerance); } }
@Test public void testParameters() throws Exception { UnivariateFunction f = new Sin(); FastCosineTransformer transformer; transformer = new FastCosineTransformer(DctNormalization.STANDARD_DCT_I); try { transformer.transform(f, 1, -1, 65, TransformType.FORWARD); Assert.fail("Expecting IllegalArgumentException - bad interval"); } catch (IllegalArgumentException ex) { } try { transformer.transform(f, -1, 1, 1, TransformType.FORWARD); Assert .fail("Expecting IllegalArgumentException - bad samples number"); } catch (IllegalArgumentException ex) { } try { transformer.transform(f, -1, 1, 64, TransformType.FORWARD); Assert .fail("Expecting IllegalArgumentException - bad samples number"); } catch (IllegalArgumentException ex) { } }
@Test public void testSinFunction() { UnivariateFunction f = new Sin(); FastCosineTransformer transformer; transformer = new FastCosineTransformer(DctNormalization.STANDARD_DCT_I); double min, max, result[], tolerance = 1E-12; int N = 9; double expected[] = { 0.0, 3.26197262739567, 0.0, -2.17958042710327, 0.0, -0.648846697642915, 0.0, -0.433545502649478, 0.0 }; min = 0.0; max = 2.0 * FastMath.PI * N / (N - 1); result = transformer.transform(f, min, max, N, TransformType.FORWARD); for (int i = 0; i < N; i++) { Assert.assertEquals(expected[i], result[i], tolerance); } min = -FastMath.PI; max = FastMath.PI * (N + 1) / (N - 1); result = transformer.transform(f, min, max, N, TransformType.FORWARD); for (int i = 0; i < N; i++) { Assert.assertEquals(-expected[i], result[i], tolerance); } }