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.MathRuntimeException; 21dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.MathException; 22dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.util.MathUtils; 23dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.util.MathUtils.OrderDirection; 24dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.analysis.BivariateRealFunction; 25dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.analysis.UnivariateRealFunction; 26dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.analysis.polynomials.PolynomialSplineFunction; 27dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.exception.util.LocalizedFormats; 28dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 29dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond/** 30dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Generates a bicubic interpolation function. 31dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Before interpolating, smoothing of the input data is performed using 32dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * splines. 33dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * See <b>Handbook on splines for the user</b>, ISBN 084939404X, 34dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * chapter 2. 35dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 36dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @version $Revision: 1059400 $ $Date: 2011-01-15 20:35:27 +0100 (sam. 15 janv. 2011) $ 37dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @since 2.1 38dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @deprecated This class does not perform smoothing; the name is thus misleading. 39dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Please use {@link org.apache.commons.math.analysis.interpolation.BicubicSplineInterpolator} 40dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * instead. If smoothing is desired, a tentative implementation is provided in class 41dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * {@link org.apache.commons.math.analysis.interpolation.SmoothingPolynomialBicubicSplineInterpolator}. 42dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * This class will be removed in math 3.0. 43dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 44dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond@Deprecated 45dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondpublic class SmoothingBicubicSplineInterpolator 46dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond implements BivariateRealGridInterpolator { 47dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 48dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * {@inheritDoc} 49dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 50dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond public BivariateRealFunction interpolate(final double[] xval, 51dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double[] yval, 52dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double[][] zval) 53dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond throws MathException, IllegalArgumentException { 54dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond if (xval.length == 0 || yval.length == 0 || zval.length == 0) { 55dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond throw MathRuntimeException.createIllegalArgumentException(LocalizedFormats.NO_DATA); 56dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 57dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond if (xval.length != zval.length) { 58dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond throw new DimensionMismatchException(xval.length, zval.length); 59dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 60dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 61dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond MathUtils.checkOrder(xval, OrderDirection.INCREASING, true); 62dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond MathUtils.checkOrder(yval, OrderDirection.INCREASING, true); 63dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 64dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final int xLen = xval.length; 65dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final int yLen = yval.length; 66dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 67dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // Samples (first index is y-coordinate, i.e. subarray variable is x) 68dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // 0 <= i < xval.length 69dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // 0 <= j < yval.length 70dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // zX[j][i] = f(xval[i], yval[j]) 71dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double[][] zX = new double[yLen][xLen]; 72dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int i = 0; i < xLen; i++) { 73dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond if (zval[i].length != yLen) { 74dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond throw new DimensionMismatchException(zval[i].length, yLen); 75dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 76dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 77dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int j = 0; j < yLen; j++) { 78dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond zX[j][i] = zval[i][j]; 79dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 80dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 81dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 82dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final SplineInterpolator spInterpolator = new SplineInterpolator(); 83dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 84dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // For each line y[j] (0 <= j < yLen), construct a 1D spline with 85dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // respect to variable x 86dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final PolynomialSplineFunction[] ySplineX = new PolynomialSplineFunction[yLen]; 87dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int j = 0; j < yLen; j++) { 88dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond ySplineX[j] = spInterpolator.interpolate(xval, zX[j]); 89dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 90dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 91dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // For every knot (xval[i], yval[j]) of the grid, calculate corrected 92dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // values zY_1 93dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double[][] zY_1 = new double[xLen][yLen]; 94dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int j = 0; j < yLen; j++) { 95dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final PolynomialSplineFunction f = ySplineX[j]; 96dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int i = 0; i < xLen; i++) { 97dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond zY_1[i][j] = f.value(xval[i]); 98dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 99dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 100dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 101dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // For each line x[i] (0 <= i < xLen), construct a 1D spline with 102dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // respect to variable y generated by array zY_1[i] 103dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final PolynomialSplineFunction[] xSplineY = new PolynomialSplineFunction[xLen]; 104dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int i = 0; i < xLen; i++) { 105dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond xSplineY[i] = spInterpolator.interpolate(yval, zY_1[i]); 106dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 107dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 108dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // For every knot (xval[i], yval[j]) of the grid, calculate corrected 109dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // values zY_2 110dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double[][] zY_2 = new double[xLen][yLen]; 111dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int i = 0; i < xLen; i++) { 112dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final PolynomialSplineFunction f = xSplineY[i]; 113dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int j = 0; j < yLen; j++) { 114dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond zY_2[i][j] = f.value(yval[j]); 115dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 116dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 117dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 118dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // Partial derivatives with respect to x at the grid knots 119dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double[][] dZdX = new double[xLen][yLen]; 120dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int j = 0; j < yLen; j++) { 121dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final UnivariateRealFunction f = ySplineX[j].derivative(); 122dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int i = 0; i < xLen; i++) { 123dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond dZdX[i][j] = f.value(xval[i]); 124dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 125dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 126dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 127dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // Partial derivatives with respect to y at the grid knots 128dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double[][] dZdY = new double[xLen][yLen]; 129dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int i = 0; i < xLen; i++) { 130dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final UnivariateRealFunction f = xSplineY[i].derivative(); 131dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int j = 0; j < yLen; j++) { 132dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond dZdY[i][j] = f.value(yval[j]); 133dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 134dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 135dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 136dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // Cross partial derivatives 137dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final double[][] dZdXdY = new double[xLen][yLen]; 138dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int i = 0; i < xLen ; i++) { 139dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final int nI = nextIndex(i, xLen); 140dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final int pI = previousIndex(i); 141dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond for (int j = 0; j < yLen; j++) { 142dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final int nJ = nextIndex(j, yLen); 143dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final int pJ = previousIndex(j); 144dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond dZdXdY[i][j] = (zY_2[nI][nJ] - zY_2[nI][pJ] - 145dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond zY_2[pI][nJ] + zY_2[pI][pJ]) / 146dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond ((xval[nI] - xval[pI]) * (yval[nJ] - yval[pJ])); 147dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 148dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 149dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 150dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond // Create the interpolating splines 151dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return new BicubicSplineInterpolatingFunction(xval, yval, zY_2, 152dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond dZdX, dZdY, dZdXdY); 153dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 154dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 155dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 156dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Compute the next index of an array, clipping if necessary. 157dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * It is assumed (but not checked) that {@code i} is larger than or equal to 0}. 158dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 159dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param i Index 160dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param max Upper limit of the array 161dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @return the next index 162dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 163dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond private int nextIndex(int i, int max) { 164dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final int index = i + 1; 165dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return index < max ? index : index - 1; 166dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 167dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** 168dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Compute the previous index of an array, clipping if necessary. 169dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * It is assumed (but not checked) that {@code i} is smaller than the size of the array. 170dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 171dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param i Index 172dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @return the previous index 173dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 174dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond private int previousIndex(int i) { 175dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond final int index = i - 1; 176dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond return index >= 0 ? index : 0; 177dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond } 178dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond} 179