aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/math/interpolator/spline1.js
blob: 615f4e85ad267b0b8e0f9d955cbd327b4239475b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Copyright 2012 The Closure Library Authors. All Rights Reserved.
//
// Licensed 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.

/**
 * @fileoverview A one dimensional cubic spline interpolator with not-a-knot
 * boundary conditions.
 *
 * See http://en.wikipedia.org/wiki/Spline_interpolation.
 *
 */

goog.provide('goog.math.interpolator.Spline1');

goog.require('goog.array');
goog.require('goog.math');
goog.require('goog.math.interpolator.Interpolator1');
goog.require('goog.math.tdma');



/**
 * A one dimensional cubic spline interpolator with natural boundary conditions.
 * @implements {goog.math.interpolator.Interpolator1}
 * @constructor
 */
goog.math.interpolator.Spline1 = function() {
  /**
   * The abscissa of the data points.
   * @type {!Array.<number>}
   * @private
   */
  this.x_ = [];

  /**
   * The spline interval coefficients.
   * Note that, in general, the length of coeffs and x is not the same.
   * @type {!Array.<!Array.<number>>}
   * @private
   */
  this.coeffs_ = [[0, 0, 0, Number.NaN]];
};


/** @override */
goog.math.interpolator.Spline1.prototype.setData = function(x, y) {
  goog.asserts.assert(x.length == y.length,
      'input arrays to setData should have the same length');
  if (x.length > 0) {
    this.coeffs_ = this.computeSplineCoeffs_(x, y);
    this.x_ = x.slice();
  } else {
    this.coeffs_ = [[0, 0, 0, Number.NaN]];
    this.x_ = [];
  }
};


/** @override */
goog.math.interpolator.Spline1.prototype.interpolate = function(x) {
  var pos = goog.array.binarySearch(this.x_, x);
  if (pos < 0) {
    pos = -pos - 2;
  }
  pos = goog.math.clamp(pos, 0, this.coeffs_.length - 1);

  var d = x - this.x_[pos];
  var d2 = d * d;
  var d3 = d2 * d;
  var coeffs = this.coeffs_[pos];
  return coeffs[0] * d3 + coeffs[1] * d2 + coeffs[2] * d + coeffs[3];
};


/**
 * Solve for the spline coefficients such that the spline precisely interpolates
 * the data points.
 * @param {Array.<number>} x The abscissa of the spline data points.
 * @param {Array.<number>} y The ordinate of the spline data points.
 * @return {!Array.<!Array.<number>>} The spline interval coefficients.
 * @private
 */
goog.math.interpolator.Spline1.prototype.computeSplineCoeffs_ = function(x, y) {
  var nIntervals = x.length - 1;
  var dx = new Array(nIntervals);
  var delta = new Array(nIntervals);
  for (var i = 0; i < nIntervals; ++i) {
    dx[i] = x[i + 1] - x[i];
    delta[i] = (y[i + 1] - y[i]) / dx[i];
  }

  // Compute the spline coefficients from the 1st order derivatives.
  var coeffs = [];
  if (nIntervals == 0) {
    // Nearest neighbor interpolation.
    coeffs[0] = [0, 0, 0, y[0]];
  } else if (nIntervals == 1) {
    // Straight line interpolation.
    coeffs[0] = [0, 0, delta[0], y[0]];
  } else if (nIntervals == 2) {
    // Parabola interpolation.
    var c3 = 0;
    var c2 = (delta[1] - delta[0]) / (dx[0] + dx[1]);
    var c1 = delta[0] - c2 * dx[0];
    var c0 = y[0];
    coeffs[0] = [c3, c2, c1, c0];
  } else {
    // General Spline interpolation. Compute the 1st order derivatives from
    // the Spline equations.
    var deriv = this.computeDerivatives(dx, delta);
    for (var i = 0; i < nIntervals; ++i) {
      var c3 = (deriv[i] - 2 * delta[i] + deriv[i + 1]) / (dx[i] * dx[i]);
      var c2 = (3 * delta[i] - 2 * deriv[i] - deriv[i + 1]) / dx[i];
      var c1 = deriv[i];
      var c0 = y[i];
      coeffs[i] = [c3, c2, c1, c0];
    }
  }
  return coeffs;
};


/**
 * Computes the derivative at each point of the spline such that
 * the curve is C2. It uses not-a-knot boundary conditions.
 * @param {Array.<number>} dx The spacing between consecutive data points.
 * @param {Array.<number>} slope The slopes between consecutive data points.
 * @return {Array.<number>} The Spline derivative at each data point.
 * @protected
 */
goog.math.interpolator.Spline1.prototype.computeDerivatives = function(
    dx, slope) {
  var nIntervals = dx.length;

  // Compute the main diagonal of the system of equations.
  var mainDiag = new Array(nIntervals + 1);
  mainDiag[0] = dx[1];
  for (var i = 1; i < nIntervals; ++i) {
    mainDiag[i] = 2 * (dx[i] + dx[i - 1]);
  }
  mainDiag[nIntervals] = dx[nIntervals - 2];

  // Compute the sub diagonal of the system of equations.
  var subDiag = new Array(nIntervals);
  for (var i = 0; i < nIntervals; ++i) {
    subDiag[i] = dx[i + 1];
  }
  subDiag[nIntervals - 1] = dx[nIntervals - 2] + dx[nIntervals - 1];

  // Compute the super diagonal of the system of equations.
  var supDiag = new Array(nIntervals);
  supDiag[0] = dx[0] + dx[1];
  for (var i = 1; i < nIntervals; ++i) {
    supDiag[i] = dx[i - 1];
  }

  // Compute the right vector of the system of equations.
  var vecRight = new Array(nIntervals + 1);
  vecRight[0] = ((dx[0] + 2 * supDiag[0]) * dx[1] * slope[0] +
      dx[0] * dx[0] * slope[1]) / supDiag[0];
  for (var i = 1; i < nIntervals; ++i) {
    vecRight[i] = 3 * (dx[i] * slope[i - 1] + dx[i - 1] * slope[i]);
  }
  vecRight[nIntervals] = (dx[nIntervals - 1] * dx[nIntervals - 1] *
      slope[nIntervals - 2] + (2 * subDiag[nIntervals - 1] +
      dx[nIntervals - 1]) * dx[nIntervals - 2] * slope[nIntervals - 1]) /
      subDiag[nIntervals - 1];

  // Solve the system of equations.
  var deriv = goog.math.tdma.solve(
      subDiag, mainDiag, supDiag, vecRight);

  return deriv;
};


/**
 * Note that the inverse of a cubic spline is not a cubic spline in general.
 * As a result the inverse implementation is only approximate. In
 * particular, it only guarantees the exact inverse at the original input data
 * points passed to setData.
 * @override
 */
goog.math.interpolator.Spline1.prototype.getInverse = function() {
  var interpolator = new goog.math.interpolator.Spline1();
  var y = [];
  for (var i = 0; i < this.x_.length; i++) {
    y[i] = this.interpolate(this.x_[i]);
  }
  interpolator.setData(y, this.x_);
  return interpolator;
};