Added gaussian function.
Added "package.html" doc file.


git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1067917 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gilles Sadowski 2011-02-07 11:17:42 +00:00
parent e178411bd4
commit ab0e605ef6
3 changed files with 155 additions and 0 deletions

View File

@ -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);
}
}

View File

@ -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>

View File

@ -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);
}
}