spline1_test.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright 2011 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. goog.provide('goog.math.interpolator.Spline1Test');
  15. goog.setTestOnly('goog.math.interpolator.Spline1Test');
  16. goog.require('goog.math.interpolator.Spline1');
  17. goog.require('goog.testing.jsunit');
  18. function testSpline() {
  19. // Test special case with no data to interpolate.
  20. var x = [];
  21. var y = [];
  22. var interp = new goog.math.interpolator.Spline1();
  23. interp.setData(x, y);
  24. assertTrue(isNaN(interp.interpolate(1)));
  25. // Test special case with 1 data point.
  26. x = [0];
  27. y = [2];
  28. interp = new goog.math.interpolator.Spline1();
  29. interp.setData(x, y);
  30. assertRoughlyEquals(2, interp.interpolate(1), 1e-4);
  31. // Test special case with 2 data points.
  32. x = [0, 1];
  33. y = [2, 5];
  34. interp = new goog.math.interpolator.Spline1();
  35. interp.setData(x, y);
  36. assertRoughlyEquals(3.5, interp.interpolate(.5), 1e-4);
  37. // Test special case with 3 data points.
  38. x = [0, 1, 2];
  39. y = [2, 5, 4];
  40. interp = new goog.math.interpolator.Spline1();
  41. interp.setData(x, y);
  42. assertRoughlyEquals(4, interp.interpolate(.5), 1e-4);
  43. assertRoughlyEquals(-1, interp.interpolate(3), 1e-4);
  44. // Test general case.
  45. x = [0, 1, 3, 6, 7];
  46. y = [0, 0, 0, 0, 0];
  47. for (var i = 0; i < x.length; ++i) {
  48. y[i] = Math.sin(x[i]);
  49. }
  50. interp = new goog.math.interpolator.Spline1();
  51. interp.setData(x, y);
  52. var xi = [0, 0.5, 1, 2, 3, 4, 5, 6, 7];
  53. var expected =
  54. [0, 0.5775, 0.8415, 0.7047, 0.1411, -0.3601, -0.55940, -0.2794, 0.6570];
  55. var result = [0, 0, 0, 0, 0, 0, 0, 0, 0];
  56. for (var i = 0; i < xi.length; ++i) {
  57. result[i] = interp.interpolate(xi[i]);
  58. }
  59. assertElementsRoughlyEqual(expected, result, 1e-4);
  60. }
  61. function testOutOfBounds() {
  62. var x = [0, 1, 2, 4];
  63. var y = [2, 5, 4, 1];
  64. var interp = new goog.math.interpolator.Spline1();
  65. interp.setData(x, y);
  66. assertRoughlyEquals(-7.75, interp.interpolate(-1), 1e-4);
  67. assertRoughlyEquals(4.5, interp.interpolate(5), 1e-4);
  68. }
  69. function testInverse() {
  70. var x = [0, 1, 3, 6, 7];
  71. var y = [0, 2, 7, 8, 10];
  72. var interp = new goog.math.interpolator.Spline1();
  73. interp.setData(x, y);
  74. var invInterp = interp.getInverse();
  75. var xi = [0, 0.5, 1, 2, 3, 4, 5, 6, 7];
  76. var yi = [0, 0.8159, 2, 4.7892, 7, 7.6912, 7.6275, 8, 10];
  77. var expectedX = [0, 0.8142, 1, 0.2638, 3, 5.0534, 4.8544, 6, 7];
  78. var resultX = [0, 0, 0, 0, 0, 0, 0, 0, 0];
  79. var resultY = [0, 0, 0, 0, 0, 0, 0, 0, 0];
  80. for (var i = 0; i < xi.length; ++i) {
  81. resultY[i] = interp.interpolate(xi[i]);
  82. resultX[i] = invInterp.interpolate(yi[i]);
  83. }
  84. assertElementsRoughlyEqual(expectedX, resultX, 1e-4);
  85. assertElementsRoughlyEqual(yi, resultY, 1e-4);
  86. }