View Javadoc
1   // ******************************************************************************
2   //
3   // Title:       Force Field X.
4   // Description: Force Field X - Software for Molecular Biophysics.
5   // Copyright:   Copyright (c) Michael J. Schnieders 2001-2024.
6   //
7   // This file is part of Force Field X.
8   //
9   // Force Field X is free software; you can redistribute it and/or modify it
10  // under the terms of the GNU General Public License version 3 as published by
11  // the Free Software Foundation.
12  //
13  // Force Field X is distributed in the hope that it will be useful, but WITHOUT
14  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16  // details.
17  //
18  // You should have received a copy of the GNU General Public License along with
19  // Force Field X; if not, write to the Free Software Foundation, Inc., 59 Temple
20  // Place, Suite 330, Boston, MA 02111-1307 USA
21  //
22  // Linking this library statically or dynamically with other modules is making a
23  // combined work based on this library. Thus, the terms and conditions of the
24  // GNU General Public License cover the whole combination.
25  //
26  // As a special exception, the copyright holders of this library give you
27  // permission to link this library with independent modules to produce an
28  // executable, regardless of the license terms of these independent modules, and
29  // to copy and distribute the resulting executable under terms of your choice,
30  // provided that you also meet, for each linked independent module, the terms
31  // and conditions of the license of that module. An independent module is a
32  // module which is not derived from or based on this library. If you modify this
33  // library, you may extend this exception to your version of the library, but
34  // you are not obligated to do so. If you do not wish to do so, delete this
35  // exception statement from your version.
36  //
37  // ******************************************************************************
38  package ffx.numerics.integrate;
39  
40  import static java.lang.String.format;
41  import static java.lang.System.arraycopy;
42  
43  /**
44   * A PolynomialCurve describes points along a polynomial function.
45   *
46   * @author Jacob M. Litman
47   */
48  public class PolynomialCurve extends FunctionDataCurve {
49  
50    /** Coefficients of the polynomial curve. */
51    private final double[] coefficients;
52  
53    /**
54     * Default constructor, assumes constant-width bins. Functional form will be a0 + a1x + a2x^2 +
55     * a3x^3 + ... + anx^n.
56     *
57     * @param x an array of {@link double} objects.
58     * @param coefficients Lowest-order coefficients first
59     */
60    public PolynomialCurve(double[] x, double[] coefficients) {
61      this(x, false, coefficients);
62    }
63  
64    /**
65     * Default constructor, assumes constant-width bins. Functional form will be a0 + a1x + a2x^2 +
66     * a3x^3 + ... + anx^n.
67     *
68     * @param x an array of {@link double} objects.
69     * @param halfWidthEnds Specifies that first and last bins are half-width.l
70     * @param coefficients Lowest-order coefficients first
71     */
72    public PolynomialCurve(double[] x, boolean halfWidthEnds, double[] coefficients) {
73      int nPoints = x.length;
74      points = new double[nPoints];
75      this.coefficients = new double[coefficients.length];
76      arraycopy(coefficients, 0, this.coefficients, 0, coefficients.length);
77      this.halfWidthEnd = halfWidthEnds;
78  
79      for (int i = 0; i < points.length; i++) {
80        points[i] = polynomialAt(x[i]);
81      }
82      lb = x[0];
83      ub = x[nPoints - 1];
84      this.x = new double[x.length];
85      arraycopy(x, 0, this.x, 0, x.length);
86    }
87  
88    /** {@inheritDoc} */
89    @Override
90    public double fX(double x) {
91      return polynomialAt(x);
92    }
93  
94    /** {@inheritDoc} */
95    @Override
96    public double integralAt(double x) {
97      double total = 0;
98      for (int i = 0; i < coefficients.length; i++) {
99        double val = 1.0 / ((double) i + 1);
100       val *= coefficients[i];
101       for (int j = 0; j <= i; j++) {
102         val *= x;
103       }
104       total += val;
105     }
106     return total;
107   }
108 
109   /** {@inheritDoc} */
110   @Override
111   public String toString() {
112     StringBuilder sb = new StringBuilder("Polynomial curve of degree ");
113     sb.append(coefficients.length)
114         .append(
115             format(
116                 " with %d points from lower bound %9.3g and upper bound %9.3g",
117                 points.length, lb, ub));
118     if (halfWidthEnd) {
119       sb.append(" and half-width start/end bins");
120     }
121     sb.append(".\nCoefficients: ");
122     if (coefficients.length > 0) {
123       sb.append(coefficients[0]);
124     }
125     for (int i = 1; i < coefficients.length; i++) {
126       sb.append(",").append(coefficients[i]);
127     }
128 
129     return sb.toString();
130   }
131 
132   // Private, non-overrideable method for use in the constructor.
133   private double polynomialAt(double x) {
134     double total = 0.0;
135     for (int i = 0; i < coefficients.length; i++) {
136       double val = coefficients[i];
137       for (int j = 0; j < i; j++) {
138         val *= x;
139       }
140       total += val;
141     }
142     return total;
143   }
144 }