001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2022, 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-2022, 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.internal.HashUtils;
042import org.jfree.chart.internal.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 *
048 * @since 1.0.14
049 */
050public class PolynomialFunction2D implements Function2D, Serializable {
051
052    /** The coefficients. */
053    private double[] coefficients;
054
055    /**
056     * Constructs a new polynomial function {@code y = a0 + a1 * x + a2 * x^2 +
057     * ... + an * x^n}
058     *
059     * @param coefficients  an array with the coefficients [a0, a1, ..., an]
060     *         ({@code null} not permitted).
061     */
062    public PolynomialFunction2D(double[] coefficients) {
063        Args.nullNotPermitted(coefficients, "coefficients");
064        this.coefficients = (double[]) coefficients.clone();
065    }
066
067    /**
068     * Returns a copy of the coefficients array that was specified in the
069     * constructor.
070     *
071     * @return The coefficients array.
072     */
073    public double[] getCoefficients() {
074        return (double[]) this.coefficients.clone();
075    }
076
077    /**
078     * Returns the order of the polynomial.
079     *
080     * @return The order.
081     */
082    public int getOrder() {
083        return this.coefficients.length - 1;
084    }
085
086    /**
087     * Returns the function value.
088     *
089     * @param x  the x-value.
090     *
091     * @return The value.
092     */
093    @Override
094    public double getValue(double x) {
095        double y = 0;
096        for(int i = 0; i < coefficients.length; i++){
097            y += coefficients[i] * Math.pow(x, i);
098        }
099        return y;
100    }
101
102    /**
103     * Tests this function for equality with an arbitrary object.
104     *
105     * @param obj  the object ({@code null} permitted).
106     *
107     * @return A boolean.
108     */
109    @Override
110    public boolean equals(Object obj) {
111        if (!(obj instanceof PolynomialFunction2D)) {
112            return false;
113        }
114        PolynomialFunction2D that = (PolynomialFunction2D) obj;
115        return Arrays.equals(this.coefficients, that.coefficients);
116    }
117
118    /**
119     * Returns a hash code for this instance.
120     *
121     * @return A hash code.
122     */
123    @Override
124    public int hashCode() {
125        return HashUtils.hashCodeForDoubleArray(this.coefficients);
126    }
127
128}