1 /* 2 * Copyright (C) 2023 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.interpolation; 17 18 import com.irurueta.numerical.polynomials.Polynomial; 19 20 /** 21 * Base class for interpolating polynomial estimators. 22 */ 23 public abstract class InterpolatingPolynomialEstimator { 24 /** 25 * Estimates polynomial from provided x and y points. 26 * 27 * @param x x points the estimated polynomial passes through. 28 * @param y y points the estimated polynomial passes through. 29 * @return estimated polynomial. 30 * @throws IllegalArgumentException if any of the provided values doesn't have the same length. 31 * @throws InterpolationException if interpolation fails for numerical reasons. 32 */ 33 public Polynomial estimate(final double[] x, final double[] y) throws InterpolationException { 34 final var polynomial = new Polynomial(x.length); 35 estimate(x, y, polynomial); 36 return polynomial; 37 } 38 39 /** 40 * Estimates polynomial coefficients from provided x and y points. 41 * 42 * @param x x points the estimated polynomial passes through. 43 * @param y y points the estimated polynomial passes through. 44 * @return coefficients of estimated polynomial. 45 * @throws IllegalArgumentException if any of the provided values doesn't have the same length. 46 * @throws InterpolationException if interpolation fails for numerical reasons. 47 */ 48 public double[] estimateCoefficients(final double[] x, final double[] y) throws InterpolationException { 49 final var result = new double[x.length]; 50 estimate(x, y, result); 51 return result; 52 } 53 54 /** 55 * Estimates polynomial from provided x and y points. 56 * 57 * @param x x points the estimated polynomial passes through. 58 * @param y y points the estimated polynomial passes through. 59 * @param result instance where estimated polynomial will be stored. 60 * @throws IllegalArgumentException if any of the provided values doesn't have the same length 61 * or polynomial doesn't have expected order. 62 * @throws InterpolationException if interpolation fails for numerical reasons. 63 */ 64 public void estimate(final double[] x, final double[] y, final Polynomial result) throws InterpolationException { 65 estimate(x, y, result.getPolyParams()); 66 } 67 68 /** 69 * Estimates polynomial coefficients from provided x and y points. 70 * 71 * @param x x points the estimated polynomial passes through. 72 * @param y y points the estimated polynomial passes through. 73 * @param cof instance where coefficients of estimated polynomial will be stored. 74 * @throws IllegalArgumentException if any of the provided values doesn't have the same length. 75 * @throws InterpolationException if interpolation fails for numerical reasons. 76 */ 77 public abstract void estimate(final double[] x, final double[] y, final double[] cof) 78 throws InterpolationException; 79 }