|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- "use strict";
-
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
-
- var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
-
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
-
- /* eslint
- no-plusplus: off,
- no-mixed-operators: off
- */
- var MonotonicInterpolant = function () {
- function MonotonicInterpolant(xs, ys) {
- _classCallCheck(this, MonotonicInterpolant);
-
- var length = xs.length;
-
- // Rearrange xs and ys so that xs is sorted
- var indexes = [];
- for (var i = 0; i < length; i++) {
- indexes.push(i);
- }
- indexes.sort(function (a, b) {
- return xs[a] < xs[b] ? -1 : 1;
- });
-
- // Get consecutive differences and slopes
- var dys = [];
- var dxs = [];
- var ms = [];
- var dx = void 0;
- var dy = void 0;
- for (var _i = 0; _i < length - 1; _i++) {
- dx = xs[_i + 1] - xs[_i];
- dy = ys[_i + 1] - ys[_i];
- dxs.push(dx);
- dys.push(dy);
- ms.push(dy / dx);
- }
-
- // Get degree-1 coefficients
- var c1s = [ms[0]];
- for (var _i2 = 0; _i2 < dxs.length - 1; _i2++) {
- var _m = ms[_i2];
- var mNext = ms[_i2 + 1];
- if (_m * mNext <= 0) {
- c1s.push(0);
- } else {
- dx = dxs[_i2];
- var dxNext = dxs[_i2 + 1];
- var common = dx + dxNext;
- c1s.push(3 * common / ((common + dxNext) / _m + (common + dx) / mNext));
- }
- }
- c1s.push(ms[ms.length - 1]);
-
- // Get degree-2 and degree-3 coefficients
- var c2s = [];
- var c3s = [];
- var m = void 0;
- for (var _i3 = 0; _i3 < c1s.length - 1; _i3++) {
- m = ms[_i3];
- var c1 = c1s[_i3];
- var invDx = 1 / dxs[_i3];
- var _common = c1 + c1s[_i3 + 1] - m - m;
- c2s.push((m - c1 - _common) * invDx);
- c3s.push(_common * invDx * invDx);
- }
-
- this.xs = xs;
- this.ys = ys;
- this.c1s = c1s;
- this.c2s = c2s;
- this.c3s = c3s;
- }
-
- _createClass(MonotonicInterpolant, [{
- key: "interpolate",
- value: function interpolate(x) {
- var xs = this.xs,
- ys = this.ys,
- c1s = this.c1s,
- c2s = this.c2s,
- c3s = this.c3s;
-
- // The rightmost point in the dataset should give an exact result
-
- var i = xs.length - 1;
- if (x === xs[i]) {
- return ys[i];
- }
-
- // Search for the interval x is in, returning the corresponding y if x is one of the original xs
- var low = 0;
- var high = c3s.length - 1;
- var mid = void 0;
- while (low <= high) {
- mid = Math.floor(0.5 * (low + high));
- var xHere = xs[mid];
- if (xHere < x) {
- low = mid + 1;
- } else if (xHere > x) {
- high = mid - 1;
- } else {
- return ys[mid];
- }
- }
- i = Math.max(0, high);
-
- // Interpolate
- var diff = x - xs[i];
- var diffSq = diff * diff;
- return ys[i] + c1s[i] * diff + c2s[i] * diffSq + c3s[i] * diff * diffSq;
- }
- }]);
-
- return MonotonicInterpolant;
- }();
-
- exports.default = MonotonicInterpolant;
|