SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
Polynomial.java
1/*
2 * Class: Polynomial
3 * Description:
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 Éric Buist
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.functions;
26
27import java.io.Serializable;
28import umontreal.ssj.util.PrintfFormat;
29
38 MathFunctionWithIntegral, Serializable, Cloneable {
39 private static final long serialVersionUID = -2911550952861456470L;
40 private double[] coeff;
41
50 public Polynomial(double... coeff) {
51 if (coeff == null)
52 throw new NullPointerException();
53 if (coeff.length == 0)
54 throw new IllegalArgumentException("At least one coefficient is needed");
55 this.coeff = coeff.clone();
56 }
57
63 public int getDegree() {
64 return coeff.length - 1;
65 }
66
72 public double[] getCoefficients() {
73 return coeff.clone();
74 }
75
81 public double getCoefficient(int i) {
82 return coeff[i];
83 }
84
92 public void setCoefficients(double... coeff) {
93 if (coeff == null)
94 throw new NullPointerException();
95 if (coeff.length == 0)
96 throw new IllegalArgumentException("At least one coefficient is needed");
97 this.coeff = coeff.clone();
98 }
99
100 public double evaluate(double x) {
101 double res = coeff[coeff.length - 1];
102 for (int i = coeff.length - 2; i >= 0; i--)
103 res = coeff[i] + x * res;
104 return res;
105 }
106
107 public double derivative(double x) {
108 return derivative(x, 1);
109 }
110
111 public double derivative(double x, int n) {
112 if (n < 0)
113 throw new IllegalArgumentException("n < 0");
114 if (n == 0)
115 return evaluate(x);
116 if (n >= coeff.length)
117 return 0;
118// double res = coeff[coeff.length - 1]*(coeff.length - 1);
119// for (int i = coeff.length - 2; i >= n; i--)
120// res = i*(coeff[i] + x * res);
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;
124 return res;
125 }
126
135 if (n < 0)
136 throw new IllegalArgumentException("n < 0");
137 if (n == 0)
138 return this;
139 if (n >= coeff.length)
140 return new Polynomial(0);
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);
144 return new Polynomial(coeffDer);
145 }
146
147 private double getCoeffDer(int i, int n) {
148 double coeffDer = coeff[i];
149 for (int j = i; j > i - n; j--)
150 coeffDer *= j;
151 return coeffDer;
152 }
153
154 public double integral(double a, double b) {
155 return integralA0(b) - integralA0(a);
156 }
157
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;
163 return res;
164 }
165
176 final double[] coeffInt = new double[coeff.length + 1];
177 coeffInt[0] = c;
178 for (int i = 0; i < coeff.length; i++)
179 coeffInt[i + 1] = coeff[i] / (i + 1);
180 return new Polynomial(coeffInt);
181 }
182
183 @Override
184
185 public String toString() {
186 final StringBuilder sb = new StringBuilder();
187 for (int i = 0; i < coeff.length; i++) {
188 if (i > 0) {
189 if (coeff[i] == 0)
190 continue;
191 else if (coeff[i] > 0)
192 sb.append(" + ");
193 else
194 sb.append(" - ");
195 sb.append(PrintfFormat.format(8, 3, 3, Math.abs(coeff[i])));
196 } else
197 sb.append(PrintfFormat.format(8, 3, 3, coeff[i]));
198 if (i > 0) {
199 sb.append("*X");
200 if (i > 1)
201 sb.append("^").append(i);
202 }
203 }
204 return sb.toString();
205 }
206
207 @Override
208 public Polynomial clone() {
209 Polynomial pol;
210 try {
211 pol = (Polynomial) super.clone();
212 } catch (final CloneNotSupportedException cne) {
213 throw new IllegalStateException("Clone not supported");
214 }
215 pol.coeff = coeff.clone();
216 return pol;
217 }
218}
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.
This class acts like a StringBuffer which defines new types of append methods.
static String format(long x)
Same as d(0, 1, x).
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.