Path: blob/trunk/third_party/closure/goog/math/interpolator/pchip1.js
2868 views
// Copyright 2012 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview A one dimensional monotone cubic spline interpolator.16*17* See http://en.wikipedia.org/wiki/Monotone_cubic_interpolation.18*19*/2021goog.provide('goog.math.interpolator.Pchip1');2223goog.require('goog.math');24goog.require('goog.math.interpolator.Spline1');25262728/**29* A one dimensional monotone cubic spline interpolator.30* @extends {goog.math.interpolator.Spline1}31* @constructor32* @final33*/34goog.math.interpolator.Pchip1 = function() {35goog.math.interpolator.Pchip1.base(this, 'constructor');36};37goog.inherits(goog.math.interpolator.Pchip1, goog.math.interpolator.Spline1);383940/** @override */41goog.math.interpolator.Pchip1.prototype.computeDerivatives = function(42dx, slope) {43var len = dx.length;44var deriv = new Array(len + 1);45for (var i = 1; i < len; ++i) {46if (goog.math.sign(slope[i - 1]) * goog.math.sign(slope[i]) <= 0) {47deriv[i] = 0;48} else {49var w1 = 2 * dx[i] + dx[i - 1];50var w2 = dx[i] + 2 * dx[i - 1];51deriv[i] = (w1 + w2) / (w1 / slope[i - 1] + w2 / slope[i]);52}53}54deriv[0] =55this.computeDerivativeAtBoundary_(dx[0], dx[1], slope[0], slope[1]);56deriv[len] = this.computeDerivativeAtBoundary_(57dx[len - 1], dx[len - 2], slope[len - 1], slope[len - 2]);58return deriv;59};606162/**63* Computes the derivative of a data point at a boundary.64* @param {number} dx0 The spacing of the 1st data point.65* @param {number} dx1 The spacing of the 2nd data point.66* @param {number} slope0 The slope of the 1st data point.67* @param {number} slope1 The slope of the 2nd data point.68* @return {number} The derivative at the 1st data point.69* @private70*/71goog.math.interpolator.Pchip1.prototype.computeDerivativeAtBoundary_ = function(72dx0, dx1, slope0, slope1) {73var deriv = ((2 * dx0 + dx1) * slope0 - dx0 * slope1) / (dx0 + dx1);74if (goog.math.sign(deriv) != goog.math.sign(slope0)) {75deriv = 0;76} else if (77goog.math.sign(slope0) != goog.math.sign(slope1) &&78Math.abs(deriv) > Math.abs(3 * slope0)) {79deriv = 3 * slope0;80}81return deriv;82};838485