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