Widened the scope of tests for transform.FastSineTransformer (MATH-677).
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1234685 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
427ba1c6d3
commit
cd57ad4c3d
|
@ -16,10 +16,19 @@
|
||||||
*/
|
*/
|
||||||
package org.apache.commons.math.transform;
|
package org.apache.commons.math.transform;
|
||||||
|
|
||||||
import org.apache.commons.math.analysis.*;
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import org.apache.commons.math.analysis.SinFunction;
|
||||||
|
import org.apache.commons.math.analysis.UnivariateFunction;
|
||||||
|
import org.apache.commons.math.analysis.function.Sinc;
|
||||||
|
import org.apache.commons.math.exception.MathIllegalArgumentException;
|
||||||
import org.apache.commons.math.util.FastMath;
|
import org.apache.commons.math.util.FastMath;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.junit.runners.Parameterized;
|
||||||
|
import org.junit.runners.Parameterized.Parameters;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test case for fast sine transformer.
|
* Test case for fast sine transformer.
|
||||||
|
@ -29,7 +38,159 @@ import org.junit.Test;
|
||||||
*
|
*
|
||||||
* @version $Id$
|
* @version $Id$
|
||||||
*/
|
*/
|
||||||
public final class FastSineTransformerTest {
|
@RunWith(value = Parameterized.class)
|
||||||
|
public final class FastSineTransformerTest extends RealTransformerAbstractTest {
|
||||||
|
|
||||||
|
private final boolean standard;
|
||||||
|
|
||||||
|
private final int[] invalidDataSize;
|
||||||
|
|
||||||
|
private final double[] relativeTolerance;
|
||||||
|
|
||||||
|
private final int[] validDataSize;
|
||||||
|
|
||||||
|
public FastSineTransformerTest(final boolean standard) {
|
||||||
|
this.standard = standard;
|
||||||
|
this.validDataSize = new int[] {
|
||||||
|
1, 2, 4, 8, 16, 32, 64, 128
|
||||||
|
};
|
||||||
|
this.invalidDataSize = new int[] {
|
||||||
|
129
|
||||||
|
};
|
||||||
|
this.relativeTolerance = new double[] {
|
||||||
|
1E-15, 1E-15, 1E-14, 1E-14, 1E-13, 1E-12, 1E-11, 1E-11
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an array containing {@code true, false} in order to check both
|
||||||
|
* standard and orthogonal DSTs.
|
||||||
|
*
|
||||||
|
* @return an array of parameters for this parameterized test
|
||||||
|
*/
|
||||||
|
@Parameters
|
||||||
|
public static Collection<Object[]> data() {
|
||||||
|
final Object[][] data = new Boolean[][] {
|
||||||
|
{
|
||||||
|
Boolean.TRUE
|
||||||
|
}, {
|
||||||
|
Boolean.FALSE
|
||||||
|
}
|
||||||
|
};
|
||||||
|
return Arrays.asList(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Overriding the default implementation allows to ensure that the first
|
||||||
|
* element of the data set is zero.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
double[] createRealData(final int n) {
|
||||||
|
final double[] data = super.createRealData(n);
|
||||||
|
data[0] = 0.0;
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
RealTransformer createRealTransformer() {
|
||||||
|
if (standard) {
|
||||||
|
return FastSineTransformer.create();
|
||||||
|
} else {
|
||||||
|
return FastSineTransformer.createOrthogonal();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
int getInvalidDataSize(final int i) {
|
||||||
|
return invalidDataSize[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
int getNumberOfInvalidDataSizes() {
|
||||||
|
return invalidDataSize.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
int getNumberOfValidDataSizes() {
|
||||||
|
return validDataSize.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
double getRelativeTolerance(final int i) {
|
||||||
|
return relativeTolerance[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
int getValidDataSize(final int i) {
|
||||||
|
return validDataSize[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
UnivariateFunction getValidFunction() {
|
||||||
|
return new Sinc();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
double getValidLowerBound() {
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
double getValidUpperBound() {
|
||||||
|
return FastMath.PI;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
double[] transform(final double[] x, final boolean forward) {
|
||||||
|
final int n = x.length;
|
||||||
|
final double[] y = new double[n];
|
||||||
|
final double[] sin = new double[2 * n];
|
||||||
|
for (int i = 0; i < sin.length; i++) {
|
||||||
|
sin[i] = FastMath.sin(FastMath.PI * i / (double) n);
|
||||||
|
}
|
||||||
|
for (int j = 0; j < n; j++) {
|
||||||
|
double yj = 0.0;
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
yj += x[i] * sin[(i * j) % sin.length];
|
||||||
|
}
|
||||||
|
y[j] = yj;
|
||||||
|
}
|
||||||
|
final double s;
|
||||||
|
if (forward) {
|
||||||
|
s = standard ? 1.0 : FastMath.sqrt(2.0 / (double) n);
|
||||||
|
} else {
|
||||||
|
s = standard ? 2.0 / n : FastMath.sqrt(2.0 / (double) n);
|
||||||
|
}
|
||||||
|
TransformUtils.scaleArray(y, s);
|
||||||
|
return y;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Additional tests.
|
||||||
|
*/
|
||||||
|
@Test(expected = MathIllegalArgumentException.class)
|
||||||
|
public void testTransformRealFirstElementNotZero() {
|
||||||
|
final double[] data = new double[] {
|
||||||
|
1.0, 1.0, 1.0, 1.0
|
||||||
|
};
|
||||||
|
final RealTransformer transformer = createRealTransformer();
|
||||||
|
transformer.transform(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = MathIllegalArgumentException.class)
|
||||||
|
public void testInverseTransformRealFirstElementNotZero() {
|
||||||
|
final double[] data = new double[] {
|
||||||
|
1.0, 1.0, 1.0, 1.0
|
||||||
|
};
|
||||||
|
final RealTransformer transformer = createRealTransformer();
|
||||||
|
transformer.inverseTransform(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Additional (legacy) tests.
|
||||||
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test of transformer for the ad hoc data.
|
* Test of transformer for the ad hoc data.
|
||||||
|
|
|
@ -43,22 +43,6 @@ public abstract class RealTransformerAbstractTest {
|
||||||
/** The common seed of all random number generators used in this test. */
|
/** The common seed of all random number generators used in this test. */
|
||||||
private final static long SEED = 20110119L;
|
private final static long SEED = 20110119L;
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns a random array of doubles. Random generator always uses the same
|
|
||||||
* seed.
|
|
||||||
*
|
|
||||||
* @param n the size of the array to be returned
|
|
||||||
* @return a random array of specified size
|
|
||||||
*/
|
|
||||||
static double[] createRealData(final int n) {
|
|
||||||
final Random random = new Random(SEED);
|
|
||||||
final double[] data = new double[n];
|
|
||||||
for (int i = 0; i < n; i++) {
|
|
||||||
data[i] = 2.0 * random.nextDouble() - 1.0;
|
|
||||||
}
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a new instance of the {@link RealTransformer} to be tested.
|
* Returns a new instance of the {@link RealTransformer} to be tested.
|
||||||
*
|
*
|
||||||
|
@ -416,6 +400,26 @@ public abstract class RealTransformerAbstractTest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Utility methods.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a random array of doubles. Random generator always uses the same
|
||||||
|
* seed.
|
||||||
|
*
|
||||||
|
* @param n the size of the array to be returned
|
||||||
|
* @return a random array of specified size
|
||||||
|
*/
|
||||||
|
double[] createRealData(final int n) {
|
||||||
|
final Random random = new Random(SEED);
|
||||||
|
final double[] data = new double[n];
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
data[i] = 2.0 * random.nextDouble() - 1.0;
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* The tests per se.
|
* The tests per se.
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in New Issue