001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2013, 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 * PowerFunction2D.java
029 * --------------------
030 * (C) Copyright 2002-2009, by Object Refinery Limited.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   -;
034 *
035 * Changes:
036 * --------
037 * 01-Oct-2002 : Version 1 (DG);
038 * 28-May-2009 : Added accessor methods for co-efficients, implemented
039 *               equals() and hashCode(), and added Serialization support (DG);
040 *
041 */
042
043package org.jfree.data.function;
044
045import java.io.Serializable;
046import org.jfree.chart.HashUtilities;
047
048/**
049 * A function of the form y = a * x ^ b.
050 */
051public class PowerFunction2D implements Function2D, Serializable {
052
053    /** The 'a' coefficient. */
054    private double a;
055
056    /** The 'b' coefficient. */
057    private double b;
058
059    /**
060     * Creates a new power function.
061     *
062     * @param a  the 'a' coefficient.
063     * @param b  the 'b' coefficient.
064     */
065    public PowerFunction2D(double a, double b) {
066        this.a = a;
067        this.b = b;
068    }
069
070    /**
071     * Returns the 'a' coefficient that was specified in the constructor.
072     *
073     * @return The 'a' coefficient.
074     *
075     * @since 1.0.14
076     */
077    public double getA() {
078        return this.a;
079    }
080
081    /**
082     * Returns the 'b' coefficient that was specified in the constructor.
083     *
084     * @return The 'b' coefficient.
085     *
086     * @since 1.0.14
087     */
088    public double getB() {
089        return this.b;
090    }
091
092    /**
093     * Returns the value of the function for a given input ('x').
094     *
095     * @param x  the x-value.
096     *
097     * @return The value.
098     */
099    @Override
100    public double getValue(double x) {
101        return this.a * Math.pow(x, this.b);
102    }
103
104    /**
105     * Tests this function for equality with an arbitrary object.
106     *
107     * @param obj  the object (<code>null</code> permitted).
108     *
109     * @return A boolean.
110     */
111    @Override
112    public boolean equals(Object obj) {
113        if (!(obj instanceof PowerFunction2D)) {
114            return false;
115        }
116        PowerFunction2D that = (PowerFunction2D) obj;
117        if (this.a != that.a) {
118            return false;
119        }
120        if (this.b != that.b) {
121            return false;
122        }
123        return true;
124    }
125
126    /**
127     * Returns a hash code for this instance.
128     *
129     * @return A hash code.
130     */
131    @Override
132    public int hashCode() {
133        int result = 29;
134        result = HashUtilities.hashCode(result, this.a);
135        result = HashUtilities.hashCode(result, this.b);
136        return result;
137    }
138
139}