Author: erans
Date: Mon Feb 7 11:17:42 2011
New Revision: 1067917
URL: http://svn.apache.org/viewvc?rev=1067917&view=rev
Log:
MATH-503
Added gaussian function.
Added "package.html" doc file.
Added:
commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/Gaussian.java
(with props)
commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/package.html
(with props)
commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/function/GaussianTest.java
(with props)
Added:
commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/Gaussian.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/Gaussian.java?rev=1067917&view=auto
==============================================================================
---
commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/Gaussian.java
(added)
+++
commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/Gaussian.java
Mon Feb 7 11:17:42 2011
@@ -0,0 +1,83 @@
+/*
+ * 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.exception.NotStrictlyPositiveException;
+import org.apache.commons.math.util.FastMath;
+
+/**
+ * <a href="http://en.wikipedia.org/wiki/Gaussian_function">
+ * Gaussian</a> function.
+ *
+ * @version $Revision$ $Date$
+ * @since 3.0
+ */
+public class Gaussian implements UnivariateRealFunction {
+ /** Mean. */
+ private final double mean;
+ /** Inverse of twice the square of the standard deviation. */
+ private final double i2s2;
+ /** Normalization factor. */
+ private final double norm;
+
+ /**
+ * Gaussian with given normalization factor, mean and standard deviation.
+ *
+ * @param norm Normalization factor.
+ * @param mean Mean.
+ * @param sigma Standard deviation.
+ * @throws NotStrictlyPositiveException if {@code sigma <= 0}.
+ */
+ public Gaussian(double norm,
+ double mean,
+ double sigma) {
+ if (sigma <= 0) {
+ throw new NotStrictlyPositiveException(sigma);
+ }
+
+ this.norm = norm;
+ this.mean = mean;
+ this.i2s2 = 1 / (2 * sigma * sigma);
+ }
+
+ /**
+ * Normalized gaussian with given mean and standard deviation.
+ *
+ * @param mean Mean.
+ * @param sigma Standard deviation.
+ * @throws NotStrictlyPositiveException if {@code sigma <= 0}.
+ */
+ public Gaussian(double mean,
+ double sigma) {
+ this(1 / (sigma * FastMath.sqrt(2 * Math.PI)), mean, sigma);
+ }
+
+ /**
+ * Normalized gaussian with zero mean and unit standard deviation.
+ */
+ public Gaussian() {
+ this(0, 1);
+ }
+
+ /** {@inheritDoc} */
+ public double value(double x) {
+ final double diff = x - mean;
+ return norm * FastMath.exp(-diff * diff * i2s2);
+ }
+}
Propchange:
commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/Gaussian.java
------------------------------------------------------------------------------
svn:eol-style = native
Added:
commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/package.html
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/package.html?rev=1067917&view=auto
==============================================================================
---
commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/package.html
(added)
+++
commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/package.html
Mon Feb 7 11:17:42 2011
@@ -0,0 +1,26 @@
+<html>
+<!--
+ 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.
+ -->
+ <!-- $Revision$ $Date$ -->
+ <body>
+ <p>
+ The {@code function} package contains function objects that wrap the
+ methods contained in {@link java.lang.Math}, as well as common
+ mathematical functions such as the gaussian and sinc functions.
+ </p>
+ </body>
+</html>
Propchange:
commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/package.html
------------------------------------------------------------------------------
svn:eol-style = native
Added:
commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/function/GaussianTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/function/GaussianTest.java?rev=1067917&view=auto
==============================================================================
---
commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/function/GaussianTest.java
(added)
+++
commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/function/GaussianTest.java
Mon Feb 7 11:17:42 2011
@@ -0,0 +1,46 @@
+/*
+ * 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.exception.NotStrictlyPositiveException;
+import org.apache.commons.math.util.FastMath;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Test for class {@link Gaussian}.
+ */
+public class GaussianTest {
+ private final double EPS = Math.ulp(1d);
+
+ @Test(expected=NotStrictlyPositiveException.class)
+ public void testPreconditions() {
+ new Gaussian(1, 2, -1);
+ }
+
+ @Test
+ public void testSomeValues() {
+ final UnivariateRealFunction f = new Gaussian();
+
+ Assert.assertEquals(0, f.value(Double.NEGATIVE_INFINITY), 0);
+ Assert.assertEquals(1 / FastMath.sqrt(2 * Math.PI), f.value(0), EPS);
+ Assert.assertEquals(0, f.value(Double.POSITIVE_INFINITY), 0);
+ }
+}
Propchange:
commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/function/GaussianTest.java
------------------------------------------------------------------------------
svn:eol-style = native