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 * PolynomialFunction2D.java
029 * -------------------------
030 * (C) Copyright 2009, by Object Refinery Limited.
031 *
032 * Original Author:  Peter Kolb;
033 * Contributor(s):   David Gilbert (for Object Refinery Limited);
034 *
035 * Changes:
036 * --------
037 * 23-Mar-2009 : Version 1, patch 2795746 (PK);
038 * 28-May-2009 : Integrated in JFreeChart with modifications (DG);
039 *
040 */
041
042package org.jfree.data.function;
043
044import java.io.Serializable;
045import java.util.Arrays;
046import org.jfree.chart.HashUtilities;
047
048/**
049 * A function in the form <code>y = a0 + a1 * x + a2 * x^2 + ... + an *
050 * x^n</code>.  Instances of this class are immutable.
051 *
052 * @since 1.0.14
053 */
054public class PolynomialFunction2D implements Function2D, Serializable {
055
056    /** The coefficients. */
057    private double[] coefficients;
058
059    /**
060     * Constructs a new polynomial function <code>y = a0 + a1 * x + a2 * x^2 +
061     * ... + an * x^n</code>
062     *
063     * @param coefficients  an array with the coefficients [a0, a1, ..., an]
064     *         (<code>null</code> not permitted).
065     */
066    public PolynomialFunction2D(double[] coefficients) {
067        if (coefficients == null) {
068            throw new IllegalArgumentException("Null 'coefficients' argument");
069        }
070        this.coefficients = (double[]) coefficients.clone();
071    }
072
073    /**
074     * Returns a copy of the coefficients array that was specified in the
075     * constructor.
076     *
077     * @return The coefficients array.
078     */
079    public double[] getCoefficients() {
080        return (double[]) this.coefficients.clone();
081    }
082
083    /**
084     * Returns the order of the polynomial.
085     *
086     * @return The order.
087     */
088    public int getOrder() {
089        return this.coefficients.length - 1;
090    }
091
092    /**
093     * Returns the function value.
094     *
095     * @param x  the x-value.
096     *
097     * @return The value.
098     */
099    public double getValue(double x) {
100        double y = 0;
101        for(int i = 0; i < coefficients.length; i++){
102            y += coefficients[i] * Math.pow(x, i);
103        }
104        return y;
105    }
106
107    /**
108     * Tests this function for equality with an arbitrary object.
109     *
110     * @param obj  the object (<code>null</code> permitted).
111     *
112     * @return A boolean.
113     */
114    public boolean equals(Object obj) {
115        if (!(obj instanceof PolynomialFunction2D)) {
116            return false;
117        }
118        PolynomialFunction2D that = (PolynomialFunction2D) obj;
119        return Arrays.equals(this.coefficients, that.coefficients);
120    }
121
122    /**
123     * Returns a hash code for this instance.
124     *
125     * @return A hash code.
126     */
127    public int hashCode() {
128        return HashUtilities.hashCodeForDoubleArray(this.coefficients);
129    }
130
131}