25package umontreal.ssj.functionfit;
27import java.io.Serializable;
28import umontreal.ssj.functions.Polynomial;
30import umontreal.ssj.util.PrintfFormat;
31import cern.colt.matrix.DoubleMatrix2D;
32import cern.colt.matrix.impl.DenseDoubleMatrix2D;
33import cern.colt.matrix.linalg.Algebra;
45 private static final long serialVersionUID = -710451931485296501L;
46 private static final Algebra alg =
new Algebra();
78 if (x.length != y.length)
79 throw new IllegalArgumentException(
"x and y must have the same length");
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++) {
85 for (
int j = 0; j < x.length; j++) {
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();
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++) {
128 sb.append(
'(').append(xstr).append(
", ").append(ystr).append(
')');
130 return sb.toString();
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.