Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

MonotonicInterpolant.js 3.3 KiB

há 3 anos
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. 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; }; }();
  6. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  7. /* eslint
  8. no-plusplus: off,
  9. no-mixed-operators: off
  10. */
  11. var MonotonicInterpolant = function () {
  12. function MonotonicInterpolant(xs, ys) {
  13. _classCallCheck(this, MonotonicInterpolant);
  14. var length = xs.length;
  15. // Rearrange xs and ys so that xs is sorted
  16. var indexes = [];
  17. for (var i = 0; i < length; i++) {
  18. indexes.push(i);
  19. }
  20. indexes.sort(function (a, b) {
  21. return xs[a] < xs[b] ? -1 : 1;
  22. });
  23. // Get consecutive differences and slopes
  24. var dys = [];
  25. var dxs = [];
  26. var ms = [];
  27. var dx = void 0;
  28. var dy = void 0;
  29. for (var _i = 0; _i < length - 1; _i++) {
  30. dx = xs[_i + 1] - xs[_i];
  31. dy = ys[_i + 1] - ys[_i];
  32. dxs.push(dx);
  33. dys.push(dy);
  34. ms.push(dy / dx);
  35. }
  36. // Get degree-1 coefficients
  37. var c1s = [ms[0]];
  38. for (var _i2 = 0; _i2 < dxs.length - 1; _i2++) {
  39. var _m = ms[_i2];
  40. var mNext = ms[_i2 + 1];
  41. if (_m * mNext <= 0) {
  42. c1s.push(0);
  43. } else {
  44. dx = dxs[_i2];
  45. var dxNext = dxs[_i2 + 1];
  46. var common = dx + dxNext;
  47. c1s.push(3 * common / ((common + dxNext) / _m + (common + dx) / mNext));
  48. }
  49. }
  50. c1s.push(ms[ms.length - 1]);
  51. // Get degree-2 and degree-3 coefficients
  52. var c2s = [];
  53. var c3s = [];
  54. var m = void 0;
  55. for (var _i3 = 0; _i3 < c1s.length - 1; _i3++) {
  56. m = ms[_i3];
  57. var c1 = c1s[_i3];
  58. var invDx = 1 / dxs[_i3];
  59. var _common = c1 + c1s[_i3 + 1] - m - m;
  60. c2s.push((m - c1 - _common) * invDx);
  61. c3s.push(_common * invDx * invDx);
  62. }
  63. this.xs = xs;
  64. this.ys = ys;
  65. this.c1s = c1s;
  66. this.c2s = c2s;
  67. this.c3s = c3s;
  68. }
  69. _createClass(MonotonicInterpolant, [{
  70. key: "interpolate",
  71. value: function interpolate(x) {
  72. var xs = this.xs,
  73. ys = this.ys,
  74. c1s = this.c1s,
  75. c2s = this.c2s,
  76. c3s = this.c3s;
  77. // The rightmost point in the dataset should give an exact result
  78. var i = xs.length - 1;
  79. if (x === xs[i]) {
  80. return ys[i];
  81. }
  82. // Search for the interval x is in, returning the corresponding y if x is one of the original xs
  83. var low = 0;
  84. var high = c3s.length - 1;
  85. var mid = void 0;
  86. while (low <= high) {
  87. mid = Math.floor(0.5 * (low + high));
  88. var xHere = xs[mid];
  89. if (xHere < x) {
  90. low = mid + 1;
  91. } else if (xHere > x) {
  92. high = mid - 1;
  93. } else {
  94. return ys[mid];
  95. }
  96. }
  97. i = Math.max(0, high);
  98. // Interpolate
  99. var diff = x - xs[i];
  100. var diffSq = diff * diff;
  101. return ys[i] + c1s[i] * diff + c2s[i] * diffSq + c3s[i] * diff * diffSq;
  102. }
  103. }]);
  104. return MonotonicInterpolant;
  105. }();
  106. exports.default = MonotonicInterpolant;