001/*
002 *   PolynomialFit  -- A basis function for generating polynomial curve fits.
003 *
004 *   Copyright (C) 2002-2014 by Joseph A. Huwaldt
005 *   All rights reserved.
006 *
007 *   This library is free software; you can redistribute it and/or
008 *   modify it under the terms of the GNU Lesser General Public
009 *   License as published by the Free Software Foundation; either
010 *   version 2 of the License, or (at your option) any later version.
011 *
012 *   This library is distributed in the hope that it will be useful,
013 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
014 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
015 *   Lesser General Public License for more details.
016 *
017 *   You should have received a copy of the GNU Lesser General Public License
018 *   along with this program; if not, write to the Free Software
019 *   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
020 *   Or visit:  http://www.gnu.org/licenses/lgpl.html
021 */
022package jahuwaldt.tools.math;
023
024/**
025 * A basis function for generating a polynomial curve fits of degree p[].length-1 with
026 * coefficients in the array p[]. This is intended for use with ModelData.fit().
027 *
028 * <p> Modified by: Joseph A. Huwaldt </p>
029 *
030 * @author Joseph A. Huwaldt    Date:   October 21, 2002
031 * @version February 12, 2014
032 */
033public class PolynomialFit implements BasisFunction {
034
035    /**
036     * Method that returns the minimum number of coefficients allowed by a polynomial
037     * (method returns a value of 2 corresponding to a degree=1 polynomial).
038     */
039    @Override
040    public int getMinNumCoef() {
041        return 2;
042    }
043
044    /**
045     * Basis function that takes an input x and calculates the parameters of the
046     * polynomial function placing them in p[].
047     */
048    @Override
049    public void func(double x, double[] p) {
050        p[0] = 1;
051        int np = p.length;
052        for (int j = 1; j < np; ++j) {
053            p[j] = p[j - 1] * x;
054        }
055    }
056}