Debian Codegraph @

closure-library/closure/goog/math/interpolator/pchip1_test.js

raw
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.Pchip1Test');
16 goog.setTestOnly('goog.math.interpolator.Pchip1Test');
17
18 goog.require('goog.math.interpolator.Pchip1');
19 goog.require('goog.testing.jsunit');
20
21 function testSpline() {
22 var x = [0, 1, 3, 6, 7];
23 var y = [0, 0, 0, 0, 0];
24
25 for (var i = 0; i < x.length; ++i) {
26 y[i] = Math.sin(x[i]);
27 }
28 var interp = new goog.math.interpolator.Pchip1();
29 interp.setData(x, y);
30
31 var xi = [0, 0.5, 1, 2, 3, 4, 5, 6, 7];
32 var expected =
33 [0, 0.5756, 0.8415, 0.5428, 0.1411, -0.0595, -0.2162, -0.2794, 0.657];
34 var result = [0, 0, 0, 0, 0, 0, 0, 0, 0];
35 for (var i = 0; i < xi.length; ++i) {
36 result[i] = interp.interpolate(xi[i]);
37 }
38 assertElementsRoughlyEqual(expected, result, 1e-4);
39 }
40
41
42 function testOutOfBounds() {
43 var x = [0, 1, 2, 4];
44 var y = [2, 5, 4, 2];
45 var interp = new goog.math.interpolator.Pchip1();
46 interp.setData(x, y);
47 assertRoughlyEquals(-3, interp.interpolate(-1), 1e-4);
48 assertRoughlyEquals(1, interp.interpolate(5), 1e-4);
49 }
50
51
52 function testInverse() {
53 var x = [0, 1, 3, 6, 7];
54 var y = [0, 2, 7, 8, 10];
55
56 var interp = new goog.math.interpolator.Pchip1();
57 interp.setData(x, y);
58 var invInterp = interp.getInverse();
59
60 var xi = [0, 0.5, 1, 2, 3, 4, 5, 6, 7];
61 var yi = [0, 0.9548, 2, 4.8938, 7, 7.3906, 7.5902, 8, 10];
62 var expectedX = [0, 0.888, 1, 0.2852, 3, 4.1206, 4.7379, 6, 7];
63 var resultX = [0, 0, 0, 0, 0, 0, 0, 0, 0];
64 var resultY = [0, 0, 0, 0, 0, 0, 0, 0, 0];
65 for (var i = 0; i < xi.length; ++i) {
66 resultY[i] = interp.interpolate(xi[i]);
67 resultX[i] = invInterp.interpolate(yi[i]);
68 }
69 assertElementsRoughlyEqual(expectedX, resultX, 1e-4);
70 assertElementsRoughlyEqual(yi, resultY, 1e-4);
71 }