Debian Codegraph @

closure-library/closure/goog/math/interpolator/pchip1.js

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