1dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond/* 2dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Licensed to the Apache Software Foundation (ASF) under one or more 3dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * contributor license agreements. See the NOTICE file distributed with 4dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * this work for additional information regarding copyright ownership. 5dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * The ASF licenses this file to You under the Apache License, Version 2.0 6dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * (the "License"); you may not use this file except in compliance with 7dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * the License. You may obtain a copy of the License at 8dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 9dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * http://www.apache.org/licenses/LICENSE-2.0 10dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 11dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Unless required by applicable law or agreed to in writing, software 12dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * distributed under the License is distributed on an "AS IS" BASIS, 13dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * See the License for the specific language governing permissions and 15dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * limitations under the License. 16dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 17dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondpackage org.apache.commons.math.analysis.interpolation; 18dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 19dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.DimensionMismatchException; 20dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.MathException; 21dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.analysis.UnivariateRealFunction; 22dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.analysis.polynomials.PolynomialSplineFunction; 23dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.exception.NoDataException; 24dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.util.MathUtils; 25dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 26dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond/** 27dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Generates a bicubic interpolating function. 28dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 29dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @version $Revision: 980944 $ $Date: 2010-07-30 22:31:11 +0200 (ven. 30 juil. 2010) $ 30dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @since 2.2 31dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 32dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondpublic class BicubicSplineInterpolator 33dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond implements BivariateRealGridInterpolator { 34dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 35dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * {@inheritDoc} 36dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 37dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public BicubicSplineInterpolatingFunction interpolate(final double[] xval, 38dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double[] yval, 39dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double[][] fval) 40dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond throws MathException, IllegalArgumentException { 41dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond if (xval.length == 0 || yval.length == 0 || fval.length == 0) { 42dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond throw new NoDataException(); 43dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 44dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond if (xval.length != fval.length) { 45dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond throw new DimensionMismatchException(xval.length, fval.length); 46dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 47dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 48dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond MathUtils.checkOrder(xval); 49dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond MathUtils.checkOrder(yval); 50dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 51dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final int xLen = xval.length; 52dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final int yLen = yval.length; 53dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 54dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // Samples (first index is y-coordinate, i.e. subarray variable is x) 55dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // 0 <= i < xval.length 56dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // 0 <= j < yval.length 57dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // fX[j][i] = f(xval[i], yval[j]) 58dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double[][] fX = new double[yLen][xLen]; 59dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int i = 0; i < xLen; i++) { 60dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond if (fval[i].length != yLen) { 61dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond throw new DimensionMismatchException(fval[i].length, yLen); 62dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 63dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 64dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int j = 0; j < yLen; j++) { 65dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond fX[j][i] = fval[i][j]; 66dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 67dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 68dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 69dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final SplineInterpolator spInterpolator = new SplineInterpolator(); 70dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 71dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // For each line y[j] (0 <= j < yLen), construct a 1D spline with 72dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // respect to variable x 73dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final PolynomialSplineFunction[] ySplineX = new PolynomialSplineFunction[yLen]; 74dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int j = 0; j < yLen; j++) { 75dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond ySplineX[j] = spInterpolator.interpolate(xval, fX[j]); 76dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 77dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 78dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // For each line x[i] (0 <= i < xLen), construct a 1D spline with 79dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // respect to variable y generated by array fY_1[i] 80dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final PolynomialSplineFunction[] xSplineY = new PolynomialSplineFunction[xLen]; 81dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int i = 0; i < xLen; i++) { 82dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond xSplineY[i] = spInterpolator.interpolate(yval, fval[i]); 83dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 84dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 85dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // Partial derivatives with respect to x at the grid knots 86dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double[][] dFdX = new double[xLen][yLen]; 87dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int j = 0; j < yLen; j++) { 88dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final UnivariateRealFunction f = ySplineX[j].derivative(); 89dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int i = 0; i < xLen; i++) { 90dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond dFdX[i][j] = f.value(xval[i]); 91dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 92dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 93dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 94dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // Partial derivatives with respect to y at the grid knots 95dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double[][] dFdY = new double[xLen][yLen]; 96dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int i = 0; i < xLen; i++) { 97dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final UnivariateRealFunction f = xSplineY[i].derivative(); 98dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int j = 0; j < yLen; j++) { 99dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond dFdY[i][j] = f.value(yval[j]); 100dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 101dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 102dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 103dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // Cross partial derivatives 104dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double[][] d2FdXdY = new double[xLen][yLen]; 105dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int i = 0; i < xLen ; i++) { 106dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final int nI = nextIndex(i, xLen); 107dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final int pI = previousIndex(i); 108dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int j = 0; j < yLen; j++) { 109dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final int nJ = nextIndex(j, yLen); 110dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final int pJ = previousIndex(j); 111dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond d2FdXdY[i][j] = (fval[nI][nJ] - fval[nI][pJ] - 112dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond fval[pI][nJ] + fval[pI][pJ]) / 113dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond ((xval[nI] - xval[pI]) * (yval[nJ] - yval[pJ])); 114dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 115dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 116dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 117dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // Create the interpolating splines 118dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return new BicubicSplineInterpolatingFunction(xval, yval, fval, 119dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond dFdX, dFdY, d2FdXdY); 120dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 121dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 122dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 123dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Compute the next index of an array, clipping if necessary. 124dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * It is assumed (but not checked) that {@code i} is larger than or equal to 0}. 125dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 126dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param i Index 127dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param max Upper limit of the array 128dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @return the next index 129dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 130dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond private int nextIndex(int i, int max) { 131dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final int index = i + 1; 132dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return index < max ? index : index - 1; 133dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 134dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 135dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Compute the previous index of an array, clipping if necessary. 136dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * It is assumed (but not checked) that {@code i} is smaller than the size of the array. 137dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 138dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param i Index 139dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @return the previous index 140dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 141dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond private int previousIndex(int i) { 142dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final int index = i - 1; 143dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return index >= 0 ? index : 0; 144dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 145dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond} 146