View Javadoc
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.algebra.Matrix;
19  
20  /**
21   * Base class for interpolation methods based on Radial Basis Functions to interpolate sparse
22   * points.
23   */
24  public abstract class BaseRadialBasisFunctionInterpolator {
25      /**
26       * Dimension of points to be interpolated.
27       */
28      protected final int dim;
29  
30      /**
31       * Number of points provided in a matrix as a basis for interpolation.
32       */
33      protected final int n;
34  
35      /**
36       * Matrix containing points to interpolate from.
37       * Each row contains one point.
38       * Matrix will have n points (rows) having a dimension (columns) equal to dim.
39       */
40      protected final Matrix pts;
41  
42      /**
43       * ith point to make comparisons.
44       */
45      protected final double[] pi;
46  
47      /**
48       * Constructor.
49       * @param ptss  Matrix containing points to interpolate from. Each row contains one point.
50       *              Matrix will have n points (rows) having a dimension (columns) equal to dim.
51       */
52      protected BaseRadialBasisFunctionInterpolator(final Matrix ptss) {
53          this.dim = ptss.getColumns();
54          n = ptss.getRows();
55          pts = ptss;
56  
57          pi = new double[dim];
58      }
59  
60      /**
61       * Returns the interpolated function value at a dim-dimensional point pt.
62       *
63       * @param pt dim-dimensional point where interpolation must be computed.
64       * @return result of interpolation.
65       * @throws IllegalArgumentException if provided point has an invalid length.
66       */
67      public abstract double interpolate(final double[] pt);
68  
69      /**
70       * Computes the euclidean distance between two points.
71       *
72       * @param p1 first point.
73       * @param p2 second point.
74       * @return euclidean distance.
75       */
76      protected double rad(final double[] p1, final double[] p2) {
77          var sum = 0.0;
78          for (int i = 0; i < dim; i++) {
79              final var value = p1[i] - p2[i];
80              sum += value * value;
81          }
82  
83          return Math.sqrt(sum);
84      }
85  }