Author: erans
Date: Sun Oct 9 12:04:40 2011
New Revision: 1180588
URL: http://svn.apache.org/viewvc?rev=1180588&view=rev
Log:
Javadoc link to reference.
Remark on numerical accuracy added as code comment.
Added unit tests.
Added normalized sinc function.
Added:
commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/function/SincTest.java
(with props)
Modified:
commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/Sinc.java
Modified:
commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/Sinc.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/Sinc.java?rev=1180588&r1=1180587&r2=1180588&view=diff
==============================================================================
---
commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/Sinc.java
(original)
+++
commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/Sinc.java
Sun Oct 9 12:04:40 2011
@@ -21,15 +21,51 @@ import org.apache.commons.math.analysis.
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;
}
}
Added:
commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/function/SincTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/function/SincTest.java?rev=1180588&view=auto
==============================================================================
---
commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/function/SincTest.java
(added)
+++
commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/function/SincTest.java
Sun Oct 9 12:04:40 2011
@@ -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);
+ }
+}
Propchange:
commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/function/SincTest.java
------------------------------------------------------------------------------
svn:eol-style = native