001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-present, by David Gilbert and Contributors.
006 *
007 * Project Info:  http://www.jfree.org/jfreechart/index.html
008 *
009 * This library is free software; you can redistribute it and/or modify it
010 * under the terms of the GNU Lesser General Public License as published by
011 * the Free Software Foundation; either version 2.1 of the License, or
012 * (at your option) any later version.
013 *
014 * This library is distributed in the hope that it will be useful, but
015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017 * License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this library; if not, write to the Free Software
021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
022 * USA.
023 *
024 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 * ---------------------------------
028 * NormalDistributionFunction2D.java
029 * ---------------------------------
030 * (C)opyright 2004-present, by David Gilbert.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   -;
034 *
035 */
036
037package org.jfree.data.function;
038
039import java.io.Serializable;
040
041import org.jfree.chart.HashUtils;
042
043/**
044 * A normal distribution function.  See
045 * http://en.wikipedia.org/wiki/Normal_distribution.
046 */
047public class NormalDistributionFunction2D implements Function2D, Serializable {
048
049    /** The mean. */
050    private double mean;
051
052    /** The standard deviation. */
053    private double std;
054
055    /** Precomputed factor for the function value. */
056    private double factor;
057
058    /** Precomputed denominator for the function value. */
059    private double denominator;
060
061    /**
062     * Constructs a new normal distribution function.
063     *
064     * @param mean  the mean.
065     * @param std  the standard deviation (> 0).
066     */
067    public NormalDistributionFunction2D(double mean, double std) {
068        if (std <= 0) {
069            throw new IllegalArgumentException("Requires 'std' > 0.");
070        }
071        this.mean = mean;
072        this.std = std;
073        // calculate constant values
074        this.factor = 1 / (std * Math.sqrt(2.0 * Math.PI));
075        this.denominator = 2 * std * std;
076    }
077
078    /**
079     * Returns the mean for the function.
080     *
081     * @return The mean.
082     */
083    public double getMean() {
084        return this.mean;
085    }
086    
087    /**
088     * Returns the standard deviation for the function.
089     *
090     * @return The standard deviation.
091     */
092    public double getStandardDeviation() {
093        return this.std;
094    }
095
096    /**
097     * Returns the function value.
098     *
099     * @param x  the x-value.
100     *
101     * @return The value.
102     */
103    @Override
104    public double getValue(double x) {
105        double z = x - this.mean;
106        return this.factor * Math.exp(-z * z / this.denominator);
107    }
108
109    /**
110     * Tests this function for equality with an arbitrary object.
111     *
112     * @param obj  the object ({@code null} permitted).
113     *
114     * @return A boolean.
115     */
116    @Override
117    public boolean equals(Object obj) {
118        if (!(obj instanceof NormalDistributionFunction2D)) {
119            return false;
120        }
121        NormalDistributionFunction2D that = (NormalDistributionFunction2D) obj;
122        if (this.mean != that.mean) {
123            return false;
124        }
125        if (this.std != that.std) {
126            return false;
127        }
128        return true;
129    }
130
131    /**
132     * Returns a hash code for this instance.
133     *
134     * @return A hash code.
135     */
136    @Override
137    public int hashCode() {
138        int result = 29;
139        result = HashUtils.hashCode(result, this.mean);
140        result = HashUtils.hashCode(result, this.std);
141        return result;
142    }
143
144}