    001/* ===========================================================
    002 * JFreeChart : a free chart library for the Java(tm) platform
    003 * ===========================================================
    004 *
    005 * (C) Copyright 2000-2011, by Object Refinery Limited 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-2009, by Object Refinery Limited.
    031 *
    032 * Original Author:  David Gilbert (for Object Refinery Limited);
    033 * Contributor(s):   -;
    034 *
    035 * Changes
    036 * -------
    037 * 25-May-2004 : Version 1 (DG);
    038 * 21-Nov-2005 : Added getters for the mean and standard deviation (DG);
    039 * 12-Feb-2009 : Precompute some constants from the function - see bug
    040 *               2572016 (DG);
    041 * 28-May-2009 : Implemented equals() and hashCode(), and added serialization
    042 *               support (DG);
    043 *
    044 */
    045
    046package org.jfree.data.function;
    047
    048import java.io.Serializable;
    049
    050import org.jfree.chart.HashUtilities;
    051
    052/**
    053 * A normal distribution function.  See
    054 * http://en.wikipedia.org/wiki/Normal_distribution.
    055 */
    056public class NormalDistributionFunction2D implements Function2D, Serializable {
    057
    058    /** The mean. */
    059    private double mean;
    060
    061    /** The standard deviation. */
    062    private double std;
    063
    064    /** Precomputed factor for the function value. */
    065    private double factor;
    066
    067    /** Precomputed denominator for the function value. */
    068    private double denominator;
    069
    070    /**
    071     * Constructs a new normal distribution function.
    072     *
    073     * @param mean  the mean.
    074     * @param std  the standard deviation (> 0).
    075     */
    076    public NormalDistributionFunction2D(double mean, double std) {
    077        if (std <= 0) {
    078            throw new IllegalArgumentException("Requires 'std' > 0.");
    079        }
    080        this.mean = mean;
    081        this.std = std;
    082        // calculate constant values
    083        this.factor = 1 / (std * Math.sqrt(2.0 * Math.PI));
    084        this.denominator = 2 * std * std;
    085    }
    086
    087    /**
    088     * Returns the mean for the function.
    089     *
    090     * @return The mean.
    091     */
    092    public double getMean() {
    093        return this.mean;
    094    }
    095
    096    /**
    097     * Returns the standard deviation for the function.
    098     *
    099     * @return The standard deviation.
    100     */
    101    public double getStandardDeviation() {
    102        return this.std;
    103    }
    104
    105    /**
    106     * Returns the function value.
    107     *
    108     * @param x  the x-value.
    109     *
    110     * @return The value.
    111     */
    112    public double getValue(double x) {
    113        double z = x - this.mean;
    114        return this.factor * Math.exp(-z * z / this.denominator);
    115    }
    116
    117    /**
    118     * Tests this function for equality with an arbitrary object.
    119     *
    120     * @param obj  the object (<code>null</code> permitted).
    121     *
    122     * @return A boolean.
    123     */
    124    public boolean equals(Object obj) {
    125        if (!(obj instanceof NormalDistributionFunction2D)) {
    126            return false;
    127        }
    128        NormalDistributionFunction2D that = (NormalDistributionFunction2D) obj;
    129        if (this.mean != that.mean) {
    130            return false;
    131        }
    132        if (this.std != that.std) {
    133            return false;
    134        }
    135        return true;
    136    }
    137
    138    /**
    139     * Returns a hash code for this instance.
    140     *
    141     * @return A hash code.
    142     */
    143    public int hashCode() {
    144        int result = 29;
    145        result = HashUtilities.hashCode(result, this.mean);
    146        result = HashUtilities.hashCode(result, this.std);
    147        return result;
    148    }
    149
    150}