25package umontreal.ssj.functions;
27import java.io.Serializable;
28import umontreal.ssj.util.PrintfFormat;
39 private static final long serialVersionUID = -2911550952861456470L;
40 private double[] coeff;
52 throw new NullPointerException();
53 if (coeff.length == 0)
54 throw new IllegalArgumentException(
"At least one coefficient is needed");
55 this.coeff = coeff.clone();
64 return coeff.length - 1;
94 throw new NullPointerException();
95 if (coeff.length == 0)
96 throw new IllegalArgumentException(
"At least one coefficient is needed");
97 this.coeff = coeff.clone();
101 double res = coeff[coeff.length - 1];
102 for (
int i = coeff.length - 2; i >= 0; i--)
103 res = coeff[i] + x * res;
113 throw new IllegalArgumentException(
"n < 0");
116 if (n >= coeff.length)
121 double res = getCoeffDer(coeff.length - 1, n);
122 for (
int i = coeff.length - 2; i >= n; i--)
123 res = getCoeffDer(i, n) + x * res;
136 throw new IllegalArgumentException(
"n < 0");
139 if (n >= coeff.length)
141 final double[] coeffDer =
new double[coeff.length - n];
142 for (
int i = coeff.length - 1; i >= n; i--)
143 coeffDer[i - n] = getCoeffDer(i, n);
147 private double getCoeffDer(
int i,
int n) {
148 double coeffDer = coeff[i];
149 for (
int j = i; j > i - n; j--)
155 return integralA0(b) - integralA0(a);
158 private double integralA0(
double u) {
159 final int n = coeff.length - 1;
160 double res = u * coeff[n] / (n + 1);
161 for (
int i = coeff.length - 2; i >= 0; i--)
162 res = coeff[i] * u / (i + 1) + u * res;
176 final double[] coeffInt =
new double[coeff.length + 1];
178 for (
int i = 0; i < coeff.length; i++)
179 coeffInt[i + 1] = coeff[i] / (i + 1);
185 public String toString() {
186 final StringBuilder sb =
new StringBuilder();
187 for (
int i = 0; i < coeff.length; i++) {
191 else if (coeff[i] > 0)
197 sb.append(PrintfFormat.format(8, 3, 3, coeff[i]));
201 sb.append(
"^").append(i);
204 return sb.toString();
212 }
catch (
final CloneNotSupportedException cne) {
213 throw new IllegalStateException(
"Clone not supported");
215 pol.coeff = coeff.clone();
Polynomial integralPolynomial(double c)
Returns a polynomial representing the integral of this polynomial.
double[] getCoefficients()
Returns an array containing the coefficients of the polynomial.
Polynomial(double... coeff)
Constructs a new polynomial with coefficients coeff.
Polynomial derivativePolynomial(int n)
Returns a polynomial corresponding to the th derivative of this polynomial.
double derivative(double x, int n)
Computes (or estimates) the th derivative of the function at point x.
void setCoefficients(double... coeff)
Sets the array of coefficients of this polynomial to coeff.
double evaluate(double x)
Returns the value of the function evaluated at .
double integral(double a, double b)
Computes (or estimates) the integral of the function over the interval .
double getCoefficient(int i)
Returns the th coefficient of the polynomial.
double derivative(double x)
Computes (or estimates) the first derivative of the function at point x.
int getDegree()
Returns the degree of this polynomial.
Represents a mathematical function whose th derivative can be computed using derivative(double,...
Represents a mathematical function whose derivative can be computed using derivative(double).
Represents a mathematical function whose integral can be computed by the integral(double,...
This interface should be implemented by classes which represent univariate mathematical functions.