SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
PolInterp.java
1/*
2 * Class: PolInterp
3 * Description: polynomial that interpolates through a set of points
4 * Environment: Java
5 * Software: SSJ
6 * Copyright (C) 2001 Pierre L'Ecuyer and Universite de Montreal
7 * Organization: DIRO, Universite de Montreal
8 * @author
9 * @since
10 *
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 *
24 */
25package umontreal.ssj.functionfit;
26
27import java.io.Serializable;
28import umontreal.ssj.functions.Polynomial;
29
30import umontreal.ssj.util.PrintfFormat;
31import cern.colt.matrix.DoubleMatrix2D;
32import cern.colt.matrix.impl.DenseDoubleMatrix2D;
33import cern.colt.matrix.linalg.Algebra;
34
44public class PolInterp extends Polynomial implements Serializable {
45 private static final long serialVersionUID = -710451931485296501L;
46 private static final Algebra alg = new Algebra();
47 private double[] x;
48 private double[] y;
49
62 public PolInterp(double[] x, double[] y) {
63 super(getCoefficients(x, y));
64 this.x = x.clone();
65 this.y = y.clone();
66 }
67
77 public static double[] getCoefficients(double[] x, double[] y) {
78 if (x.length != y.length)
79 throw new IllegalArgumentException("x and y must have the same length");
80 if (x.length <= 1)
81 throw new IllegalArgumentException("At least two points are needed");
82 final DoubleMatrix2D u = new DenseDoubleMatrix2D(x.length, x.length);
83 for (int i = 0; i < x.length; i++) {
84 double v = 1;
85 for (int j = 0; j < x.length; j++) {
86 u.setQuick(i, j, v);
87 v *= x[i];
88 }
89 }
90 final DoubleMatrix2D yMat = new DenseDoubleMatrix2D(x.length, 1);
91 yMat.viewColumn(0).assign(y);
92 final DoubleMatrix2D bMat = alg.solve(u, yMat);
93 return bMat.viewColumn(0).toArray();
94 }
95
101 public double[] getX() {
102 return x.clone();
103 }
104
110 public double[] getY() {
111 return y.clone();
112 }
113
121 public static String toString(double[] x, double[] y) {
122 final StringBuilder sb = new StringBuilder("Points: ");
123 for (int i = 0; i < x.length; i++) {
124 if (i > 0)
125 sb.append(", ");
126 final String xstr = PrintfFormat.format(8, 3, 3, x[i]);
127 final String ystr = PrintfFormat.format(8, 3, 3, y[i]);
128 sb.append('(').append(xstr).append(", ").append(ystr).append(')');
129 }
130 return sb.toString();
131 }
132
139 @Override
140 public String toString() {
141 return toString(x, y);
142 }
143
144 public PolInterp clone() {
145 final PolInterp p = (PolInterp) super.clone();
146 p.x = x.clone();
147 p.y = y.clone();
148 return p;
149 }
150}
Represents a polynomial that interpolates through a set of points.
PolInterp(double[] x, double[] y)
Constructs a new polynomial interpolating through the given points (x[0], y[0]), ....
double[] getY()
Returns the coordinates of the interpolated points.
String toString()
Calls toString(double[], double[]) with the associated points.
double[] getX()
Returns the coordinates of the interpolated points.
static double[] getCoefficients(double[] x, double[] y)
Computes and returns the coefficients the polynomial interpolating through the given points (x[0],...
static String toString(double[] x, double[] y)
Makes a string representation of a set of points.
double[] getCoefficients()
Returns an array containing the coefficients of the polynomial.
Polynomial(double... coeff)
Constructs a new polynomial with coefficients coeff.
This class acts like a StringBuffer which defines new types of append methods.
static String format(long x)
Same as d(0, 1, x).