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