forked from benfred/mds.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mds_test.html
69 lines (61 loc) · 2.76 KB
/
mds_test.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>mds.js tests</title>
<link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-1.11.0.css">
<script src="http://code.jquery.com/qunit/qunit-1.11.0.js"></script>
<script src="./numeric-1.2.6.min.js"></script>
<script src="./mds.js"></script>
<script>
function nearlyEqual(left, right, tolerance, message) {
message = message || "nearlyEqual";
tolerance = tolerance || 1e-5;
ok(Math.abs(left - right) / (1e-15 + Math.min(left, right)) < tolerance,
message + ": " + left + " ~== " + right);
}
// simple 4 points in a box configuration
var boxDistances = [
[ 0., 1., 1., 1.41421356],
[ 1., 0., 1.41421356, 1. ],
[ 1., 1.41421356, 0., 1. ],
[ 1.41421356, 1., 1., 0. ]];
// distance data for 10 US cities
var cityDistances = [
[0, 587, 1212, 701, 1936, 604, 748, 2139, 2182, 543],
[587, 0, 920, 940, 1745, 1188, 713, 1858, 1737, 597],
[1212, 920, 0, 879, 831, 1726, 1631, 949, 1021, 1494],
[701, 940, 879, 0, 1374, 968, 1420, 1645, 1891, 1220],
[1936, 1745, 831, 1374, 0, 2339, 2451, 347, 959, 2300],
[604, 1188, 1726, 968, 2339, 0, 1092, 2594, 2734, 923],
[748, 713, 1631, 1420, 2451, 1092, 0, 2571, 2408, 205],
[2139, 1858, 949, 1645, 347, 2594, 2571, 0, 678, 2442],
[2182, 1737, 1021, 1891, 959, 2734, 2408, 678, 0, 2329],
[543, 597, 1494, 1220, 2300, 923, 205, 2442, 2329, 0]];
// tests out a MDS algorithm on a distance matrix
function testDistances(f, distances, msg, tolerance) {
msg = msg || "testDistances"
tolerance = tolerance || 1e-3;
var points = f(distances);
for (var i = 0; i < distances.length; ++i) {
for (var j = i; j < distances.length; ++j) {
var p1 = points[i],
p2 = points[j],
desired = distances[i][j],
actual = Math.sqrt((p1[0] - p2[0]) * (p1[0] - p2[0]) +
(p1[1] - p2[1]) * (p1[1] - p2[1]));
nearlyEqual(desired, actual, tolerance,
msg + "(" + i + ", " + j + ")");
}
}
}
test("classicMDS", function() {
testDistances(mds.classic, boxDistances, "boxDistances");
testDistances(mds.classic, cityDistances, "cityDistances", .05);
});
</script>
</head>
<body>
<div id="qunit"></div>
</body>
</html>