LinearApproximation.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import defined from "./defined.js";
  2. import DeveloperError from "./DeveloperError.js";
  3. /**
  4. * An {@link InterpolationAlgorithm} for performing linear interpolation.
  5. *
  6. * @namespace LinearApproximation
  7. */
  8. const LinearApproximation = {
  9. type: "Linear",
  10. };
  11. /**
  12. * Given the desired degree, returns the number of data points required for interpolation.
  13. * Since linear interpolation can only generate a first degree polynomial, this function
  14. * always returns 2.
  15. * @param {number} degree The desired degree of interpolation.
  16. * @returns {number} This function always returns 2.
  17. *
  18. */
  19. LinearApproximation.getRequiredDataPoints = function (degree) {
  20. return 2;
  21. };
  22. /**
  23. * Interpolates values using linear approximation.
  24. *
  25. * @param {number} x The independent variable for which the dependent variables will be interpolated.
  26. * @param {number[]} xTable The array of independent variables to use to interpolate. The values
  27. * in this array must be in increasing order and the same value must not occur twice in the array.
  28. * @param {number[]} yTable The array of dependent variables to use to interpolate. For a set of three
  29. * dependent values (p,q,w) at time 1 and time 2 this should be as follows: {p1, q1, w1, p2, q2, w2}.
  30. * @param {number} yStride The number of dependent variable values in yTable corresponding to
  31. * each independent variable value in xTable.
  32. * @param {number[]} [result] An existing array into which to store the result.
  33. * @returns {number[]} The array of interpolated values, or the result parameter if one was provided.
  34. */
  35. LinearApproximation.interpolateOrderZero = function (
  36. x,
  37. xTable,
  38. yTable,
  39. yStride,
  40. result
  41. ) {
  42. //>>includeStart('debug', pragmas.debug);
  43. if (xTable.length !== 2) {
  44. throw new DeveloperError(
  45. "The xTable provided to the linear interpolator must have exactly two elements."
  46. );
  47. } else if (yStride <= 0) {
  48. throw new DeveloperError(
  49. "There must be at least 1 dependent variable for each independent variable."
  50. );
  51. }
  52. //>>includeEnd('debug');
  53. if (!defined(result)) {
  54. result = new Array(yStride);
  55. }
  56. let i;
  57. let y0;
  58. let y1;
  59. const x0 = xTable[0];
  60. const x1 = xTable[1];
  61. //>>includeStart('debug', pragmas.debug);
  62. if (x0 === x1) {
  63. throw new DeveloperError(
  64. "Divide by zero error: xTable[0] and xTable[1] are equal"
  65. );
  66. }
  67. //>>includeEnd('debug');
  68. for (i = 0; i < yStride; i++) {
  69. y0 = yTable[i];
  70. y1 = yTable[i + yStride];
  71. result[i] = ((y1 - y0) * x + x1 * y0 - x0 * y1) / (x1 - x0);
  72. }
  73. return result;
  74. };
  75. export default LinearApproximation;