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   * Computes bicubic spline interpolation in two dimensions.
22   */
23  public class BicubicSpline2DInterpolator {
24  
25      /**
26       * Length of x1v array.
27       */
28      private final int m;
29  
30      /**
31       * Length of x2v array.
32       */
33      private final int n;
34  
35      /**
36       * Array of x1v.
37       */
38      private final double[] x1;
39  
40      /**
41       * Array of x2v.
42       */
43      private final double[] yv;
44  
45      /**
46       * Array of one dimensional cubic spline interpolators.
47       */
48      private final CubicSplineInterpolator[] srp;
49  
50      /**
51       * Constructor.
52       *
53       * @param x1v array of x1v.
54       * @param x2v array of x2v.
55       * @param ym  matrix of tabulated function values yij.
56       */
57      public BicubicSpline2DInterpolator(final double[] x1v, final double[] x2v, final Matrix ym) {
58          m = x1v.length;
59          n = x2v.length;
60          yv = new double[m];
61          x1 = x1v;
62          srp = new CubicSplineInterpolator[m];
63  
64          for (var i = 0; i < m; i++) {
65              // get i-th row of y
66              final var yi = ym.getSubmatrixAsArray(i, 0, i, ym.getColumns() - 1);
67              srp[i] = new CubicSplineInterpolator(x2v, yi);
68          }
69      }
70  
71      /**
72       * Gets length of x1v array.
73       *
74       * @return length of x1v array.
75       */
76      public int getM() {
77          return m;
78      }
79  
80      /**
81       * Gets length of x2v array.
82       *
83       * @return length of x2v array.
84       */
85      public int getN() {
86          return n;
87      }
88  
89      /**
90       * Given values x1p an x2p, returns an interpolated value.
91       *
92       * @param x1p x1p value where interpolation is estimated.
93       * @param x2p x2p value where interpolation is estimated.
94       * @return interpolated value.
95       * @throws InterpolationException if interpolation fails.
96       */
97      public double interpolate(final double x1p, final double x2p) throws InterpolationException {
98          for (int i = 0; i < m; i++) {
99              yv[i] = srp[i].interpolate(x2p);
100         }
101 
102         final var scol = new CubicSplineInterpolator(x1, yv);
103         return scol.interpolate(x1p);
104     }
105 }