Use functionality defined in "Commons Numbers".

This commit is contained in:
Gilles Sadowski 2021-05-26 18:49:51 +02:00
parent fcc47a2c88
commit 8df971ce64
2 changed files with 13 additions and 8 deletions

View File

@ -16,6 +16,8 @@
*/
package org.apache.commons.math4.legacy.analysis.interpolation;
import org.apache.commons.numbers.angle.Reduce;
import org.apache.commons.math4.legacy.analysis.UnivariateFunction;
import org.apache.commons.math4.legacy.exception.MathIllegalArgumentException;
import org.apache.commons.math4.legacy.exception.NonMonotonicSequenceException;
@ -91,24 +93,25 @@ public class UnivariatePeriodicInterpolator
MathArrays.checkOrder(xval);
final double offset = xval[0];
final Reduce reduce = new Reduce(offset, period);
final int len = xval.length + extend * 2;
final double[] x = new double[len];
final double[] y = new double[len];
for (int i = 0; i < xval.length; i++) {
final int index = i + extend;
x[index] = MathUtils.reduce(xval[i], period, offset);
x[index] = reduce.applyAsDouble(xval[i]);
y[index] = yval[i];
}
// Wrap to enable interpolation at the boundaries.
for (int i = 0; i < extend; i++) {
int index = xval.length - extend + i;
x[i] = MathUtils.reduce(xval[index], period, offset) - period;
x[i] = reduce.applyAsDouble(xval[index]) - period;
y[i] = yval[index];
index = len - extend + i;
x[index] = MathUtils.reduce(xval[i], period, offset) + period;
x[index] = reduce.applyAsDouble(xval[i]) + period;
y[index] = yval[i];
}
@ -119,7 +122,7 @@ public class UnivariatePeriodicInterpolator
/** {@inheritDoc} */
@Override
public double value(final double x) throws MathIllegalArgumentException {
return f.value(MathUtils.reduce(x, period, offset));
return f.value(reduce.applyAsDouble(x));
}
};
}

View File

@ -16,21 +16,23 @@
*/
package org.apache.commons.math4.legacy.analysis.interpolation;
import java.util.Random;
import org.junit.Assert;
import org.junit.Test;
import org.apache.commons.rng.UniformRandomProvider;
import org.apache.commons.rng.simple.RandomSource;
import org.apache.commons.math4.legacy.analysis.UnivariateFunction;
import org.apache.commons.math4.legacy.exception.NonMonotonicSequenceException;
import org.apache.commons.math4.legacy.exception.NumberIsTooSmallException;
import org.apache.commons.math4.legacy.util.FastMath;
import org.junit.Assert;
import org.junit.Test;
/**
* Test for {@link UnivariatePeriodicInterpolator}.
*/
public class UnivariatePeriodicInterpolatorTest {
private final Random rng = new Random(1224465L);
private final UniformRandomProvider rng = RandomSource.create(RandomSource.KISS);
@Test
public void testSine() {