Javadoc link to reference.
Remark on numerical accuracy added as code comment. Added unit tests. Added normalized sinc function. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1180588 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
394a367edc
commit
a7fea11074
|
@ -21,15 +21,51 @@ import org.apache.commons.math.analysis.UnivariateRealFunction;
|
|||
import org.apache.commons.math.util.FastMath;
|
||||
|
||||
/**
|
||||
* The sinc function, {code sin(x)/x}.
|
||||
* Returns {code 1} if {code |x| < 1e-9}.
|
||||
* <a href="http://en.wikipedia.org/wiki/Sinc_function">Sinc</a> function.
|
||||
*
|
||||
* @version $Id$
|
||||
* @since 3.0
|
||||
*/
|
||||
public class Sinc implements UnivariateRealFunction {
|
||||
/** For normalized sinc function. */
|
||||
private final boolean normalized;
|
||||
|
||||
/**
|
||||
* The sinc function, {@code sin(x) / x}.
|
||||
*/
|
||||
public Sinc() {
|
||||
this(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiates the sinc function.
|
||||
*
|
||||
* @param normalized If {@code true}, the function is
|
||||
* <code> sin(πx) / πx</code>, otherwise {@code sin(x) / x}.
|
||||
*/
|
||||
public Sinc(boolean normalized) {
|
||||
this.normalized = normalized;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public double value(double x) {
|
||||
if (normalized) {
|
||||
final double piTimesX = Math.PI * x;
|
||||
return sinc(piTimesX);
|
||||
} else {
|
||||
return sinc(x);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param x Argument.
|
||||
* @return {@code sin(x) / x}.
|
||||
*/
|
||||
private static double sinc(double x) {
|
||||
// The direct assignment to 1 for values below 1e-9 is an efficiency
|
||||
// optimization on the ground that the result of the full computation
|
||||
// is indistinguishable from 1 due to the limited accuracy of the
|
||||
// floating point representation.
|
||||
return FastMath.abs(x) < 1e-9 ? 1 : FastMath.sin(x) / x;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,72 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.commons.math.analysis.function;
|
||||
|
||||
import org.apache.commons.math.analysis.UnivariateRealFunction;
|
||||
import org.apache.commons.math.util.FastMath;
|
||||
import org.apache.commons.math.util.MathUtils;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.Assert;
|
||||
|
||||
public class SincTest {
|
||||
|
||||
@Test
|
||||
public void testShortcut() {
|
||||
final Sinc s = new Sinc();
|
||||
final UnivariateRealFunction f = new UnivariateRealFunction() {
|
||||
public double value(double x) {
|
||||
return FastMath.sin(x) / x;
|
||||
}
|
||||
};
|
||||
|
||||
for (double x = 1e-30; x < 1e10; x *= 5) {
|
||||
final double fX = f.value(x);
|
||||
final double sX = s.value(x);
|
||||
Assert.assertEquals("x=" + x, fX, sX, 0);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCrossings() {
|
||||
final Sinc s = new Sinc(true);
|
||||
final int numCrossings = 1000;
|
||||
final double tol = 2e-16;
|
||||
for (int i = 1; i <= numCrossings; i++) {
|
||||
Assert.assertEquals("i=" + i, 0, s.value(i), tol);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testZero() {
|
||||
final Sinc s = new Sinc();
|
||||
Assert.assertEquals(1d, s.value(0), 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEuler() {
|
||||
final Sinc s = new Sinc();
|
||||
final double x = 123456.789;
|
||||
double prod = 1;
|
||||
double xOverPow2 = x / 2;
|
||||
while (xOverPow2 > 0) {
|
||||
prod *= FastMath.cos(xOverPow2);
|
||||
xOverPow2 /= 2;
|
||||
}
|
||||
Assert.assertEquals(prod, s.value(x), 1e-13);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue