1 /*
2 * Copyright (C) 2016 Alberto Irurueta Carro (alberto@irurueta.com)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package com.irurueta.numerical;
17
18 /**
19 * Utility class to evaluate real polynomials.
20 * This class is useful when the same real polynomial needs to be evaluated
21 * multiple times.
22 */
23 public class RealPolynomialEvaluator extends PolynomialEvaluator {
24
25 /**
26 * Polynomial coefficients.
27 * A polynomial of degree n is defined as:
28 * p(x) = a0 * x^n + a1 * x^(n - 1) + ... a(n-1) * x + an
29 * Hence, the array of polynomial coefficients is [a0, a1, ... a(n-1), an]
30 */
31 private final double[] polyParams;
32
33 /**
34 * Constructor.
35 *
36 * @param polyParams polynomial coefficients.
37 * @throws IllegalArgumentException if provided array is null or has length
38 * 0.
39 */
40 public RealPolynomialEvaluator(final double[] polyParams) {
41 if (polyParams == null || polyParams.length == 0) {
42 throw new IllegalArgumentException();
43 }
44
45 this.polyParams = polyParams;
46 }
47
48 /**
49 * Gets polynomial parameters.
50 *
51 * @return polynomial parameters.
52 */
53 public double[] getPolyParams() {
54 return polyParams;
55 }
56
57 /**
58 * Evaluates polynomial at provided point x.
59 *
60 * @param x point where polynomial is evaluated.
61 * @return result of evaluation.
62 */
63 public double evaluate(final double x) {
64 return evaluate(polyParams, x);
65 }
66 }