alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Commons Math example source code file (SplineInterpolator.java)

This example Commons Math source code file (SplineInterpolator.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Commons Math tags/keywords

polynomialfunction, polynomialfunction, polynomialsplinefunction, polynomialsplinefunction, splineinterpolator, splineinterpolator, univariaterealinterpolator, univariaterealinterpolator

The Commons Math SplineInterpolator.java source code

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.commons.math.analysis.interpolation;

import org.apache.commons.math.MathRuntimeException;
import org.apache.commons.math.analysis.polynomials.PolynomialFunction;
import org.apache.commons.math.analysis.polynomials.PolynomialSplineFunction;

/**
 * Computes a natural (also known as "free", "unclamped") cubic spline interpolation for the data set.
 * <p>
 * The {@link #interpolate(double[], double[])} method returns a {@link PolynomialSplineFunction}
 * consisting of n cubic polynomials, defined over the subintervals determined by the x values,
 * x[0] < x[i] ... < x[n].  The x values are referred to as "knot points." where
 * <code>i is the index of the subinterval.  See {@link PolynomialSplineFunction} for more details.
 * </p>
 * <p>
 * The interpolating polynomials satisfy: <ol>
 * <li>The value of the PolynomialSplineFunction at each of the input x values equals the
 *  corresponding y value.</li>
 * <li>Adjacent polynomials are equal through two derivatives at the knot points (i.e., adjacent polynomials
 *  "match up" at the knot points, as do their first and second derivatives).</li>
 * </ol>

* <p> * The cubic spline interpolation algorithm implemented is as described in R.L. Burden, J.D. Faires, * <u>Numerical Analysis, 4th Ed., 1989, PWS-Kent, ISBN 0-53491-585-X, pp 126-131. * </p> * * @version $Revision: 811685 $ $Date: 2009-09-05 13:36:48 -0400 (Sat, 05 Sep 2009) $ * */ public class SplineInterpolator implements UnivariateRealInterpolator { /** * Computes an interpolating function for the data set. * @param x the arguments for the interpolation points * @param y the values for the interpolation points * @return a function which interpolates the data set */ public PolynomialSplineFunction interpolate(double x[], double y[]) { if (x.length != y.length) { throw MathRuntimeException.createIllegalArgumentException( "dimension mismatch {0} != {1}", x.length, y.length); } if (x.length < 3) { throw MathRuntimeException.createIllegalArgumentException( "{0} points are required, got only {1}", 3, x.length); } // Number of intervals. The number of data points is n + 1. int n = x.length - 1; for (int i = 0; i < n; i++) { if (x[i] >= x[i + 1]) { throw MathRuntimeException.createIllegalArgumentException( "points {0} and {1} are not strictly increasing ({2} >= {3})", i, i+1, x[i], x[i+1]); } } // Differences between knot points double h[] = new double[n]; for (int i = 0; i < n; i++) { h[i] = x[i + 1] - x[i]; } double mu[] = new double[n]; double z[] = new double[n + 1]; mu[0] = 0d; z[0] = 0d; double g = 0; for (int i = 1; i < n; i++) { g = 2d * (x[i+1] - x[i - 1]) - h[i - 1] * mu[i -1]; mu[i] = h[i] / g; z[i] = (3d * (y[i + 1] * h[i - 1] - y[i] * (x[i + 1] - x[i - 1])+ y[i - 1] * h[i]) / (h[i - 1] * h[i]) - h[i - 1] * z[i - 1]) / g; } // cubic spline coefficients -- b is linear, c quadratic, d is cubic (original y's are constants) double b[] = new double[n]; double c[] = new double[n + 1]; double d[] = new double[n]; z[n] = 0d; c[n] = 0d; for (int j = n -1; j >=0; j--) { c[j] = z[j] - mu[j] * c[j + 1]; b[j] = (y[j + 1] - y[j]) / h[j] - h[j] * (c[j + 1] + 2d * c[j]) / 3d; d[j] = (c[j + 1] - c[j]) / (3d * h[j]); } PolynomialFunction polynomials[] = new PolynomialFunction[n]; double coefficients[] = new double[4]; for (int i = 0; i < n; i++) { coefficients[0] = y[i]; coefficients[1] = b[i]; coefficients[2] = c[i]; coefficients[3] = d[i]; polynomials[i] = new PolynomialFunction(coefficients); } return new PolynomialSplineFunction(x, polynomials); } }

Other Commons Math examples (source code examples)

Here is a short list of links related to this Commons Math SplineInterpolator.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.