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 * PolynomialFunction2D.java
029 * -------------------------
030 * (C) Copyright 2009-2013, 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 * 03-Jul-2013 : Use ParamChecks (DG);
040 *
041 */
042
043package org.jfree.data.function;
044
045import java.io.Serializable;
046import java.util.Arrays;
047import org.jfree.chart.HashUtilities;
048import org.jfree.chart.util.ParamChecks;
049
050/**
051 * A function in the form <code>y = a0 + a1 * x + a2 * x^2 + ... + an *
052 * x^n</code>.  Instances of this class are immutable.
053 *
054 * @since 1.0.14
055 */
056public class PolynomialFunction2D implements Function2D, Serializable {
057
058    /** The coefficients. */
059    private double[] coefficients;
060
061    /**
062     * Constructs a new polynomial function <code>y = a0 + a1 * x + a2 * x^2 +
063     * ... + an * x^n</code>
064     *
065     * @param coefficients  an array with the coefficients [a0, a1, ..., an]
066     *         (<code>null</code> not permitted).
067     */
068    public PolynomialFunction2D(double[] coefficients) {
069        ParamChecks.nullNotPermitted(coefficients, "coefficients");
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    @Override
100    public double getValue(double x) {
101        double y = 0;
102        for(int i = 0; i < coefficients.length; i++){
103            y += coefficients[i] * Math.pow(x, i);
104        }
105        return y;
106    }
107
108    /**
109     * Tests this function for equality with an arbitrary object.
110     *
111     * @param obj  the object (<code>null</code> permitted).
112     *
113     * @return A boolean.
114     */
115    @Override
116    public boolean equals(Object obj) {
117        if (!(obj instanceof PolynomialFunction2D)) {
118            return false;
119        }
120        PolynomialFunction2D that = (PolynomialFunction2D) obj;
121        return Arrays.equals(this.coefficients, that.coefficients);
122    }
123
124    /**
125     * Returns a hash code for this instance.
126     *
127     * @return A hash code.
128     */
129    @Override
130    public int hashCode() {
131        return HashUtilities.hashCodeForDoubleArray(this.coefficients);
132    }
133
134}