/
index.html
194 lines (177 loc) · 5.66 KB
/
index.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta http-equiv="x-ua-compatible" content="ie=edge" />
<!-- Google tag (gtag.js) -->
<script
async
src="https://www.googletagmanager.com/gtag/js?id=G-4DHJEMESJD"
></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('config', 'G-4DHJEMESJD');
</script>
<!-- Google tag end -->
<title>mds - js</title>
<link rel="canonical" href="https://mdsjs.josuakrause.com/" />
<link rel="stylesheet" href="./index.css" />
</head>
<body>
<div class="header">mdsjs</div>
<div class="top">
<div class="main">
<canvas id="canvas" width="400" height="400"></canvas>
</div>
<div class="filler"></div>
<div class="values">
<div>Input:</div>
<pre id="matrix"></pre>
<div>Output:</div>
<pre id="output"></pre>
</div>
</div>
<div class="buttons">
<button id="recompute">recompute</button>
<button id="switch">switch</button>
<button id="source">source</button>
<div>Computing: <span id="mode"></span></div>
</div>
<div class="sourceview">
<pre id="sourceview"></pre>
</div>
<script type="module" id="jsmain" defer>
import mdsjs from './mdsjs.js';
mdsjs.DEBUG = true;
function start() {
if (mds) {
computeMDS();
} else {
computePCA();
}
updateSource();
}
document.getElementById('recompute').addEventListener('click', start);
let mds = true;
function chg() {
mds = !mds;
start();
}
document.getElementById('switch').addEventListener('click', chg);
let showSource = false;
function toggleSource() {
showSource = !showSource;
updateSource();
}
document
.getElementById('source')
.addEventListener('click', toggleSource);
function deindent(/** @type {string} */ text) {
const lines = text.split(/\r?\n|\r/);
const chomp = lines.reduce(
(p, c) =>
!c.trimEnd().length
? p
: Math.min(p, c.length - c.trimLeft().length),
Number.POSITIVE_INFINITY,
);
return lines.map((line) => line.slice(chomp)).join('\n');
}
function updateSource() {
const elem = document.getElementById('sourceview');
if (showSource) {
const text = `${mds ? computeMDS : computePCA}`;
const left = text.indexOf('{');
const right = text.lastIndexOf('}');
elem.innerText = `import mdsjs from 'mdsjs';\n${deindent(
text.slice(left + 1, right - 1),
)}`;
} else {
elem.innerText = '';
}
}
function computeMDS() {
const a = mdsjs.convertToMatrix(
[
[0, 2, 2, 4, 6, 6, 4, 6, 6],
[2, 0, 2, 2, 3, 4, 2, 3, 4],
[2, 2, 0, 4, 4, 6, 4, 4, 6],
[4, 2, 4, 0, 1, 1, 4, 6, 6],
[6, 3, 4, 1, 0, 1, 2, 3, 4],
[6, 4, 6, 1, 1, 0, 4, 4, 6],
[4, 2, 4, 4, 2, 4, 0, 1, 1],
[6, 3, 4, 6, 3, 4, 1, 0, 1],
[6, 4, 6, 6, 4, 6, 1, 1, 0],
],
true,
);
mdsjs.landmarkMDSAsync(a, 2, (points) => {
const ctx = document.getElementById('canvas').getContext('2d');
ctx.clearRect(0, 0, 400, 400);
ctx.beginPath();
ctx.rect(0, 0, 400, 400);
ctx.stroke();
points.rowsIter((row) => {
const x = row[0] * 50 + 200;
const y = row[1] * 50 + 200;
ctx.fillRect(x - 2, y - 2, 4, 4);
});
document.getElementById('mode').innerText = 'mds';
document.getElementById('output').innerText = '' + points;
document.getElementById('matrix').innerText = '' + a;
});
}
function computePCA() {
const /** @type {number[][]} */ posArr = [];
const /** @type {number} */ xspread = 180;
const /** @type {number} */ yspread = 150;
for (let ix = 0; ix < 1000; ix += 1) {
const rnd = Math.random() * 2 * Math.PI;
posArr.push([Math.cos(rnd) * xspread, Math.sin(rnd) * yspread]);
}
const pos = mdsjs.convertToMatrix(posArr);
mdsjs.pcaAsync(pos, (pca) => {
const points = pos.mul(pca);
const ctx = document.getElementById('canvas').getContext('2d');
ctx.clearRect(0, 0, 400, 400);
ctx.beginPath();
ctx.rect(0, 0, 400, 400);
ctx.stroke();
ctx.save();
ctx.fillStyle = 'blue';
pos.rowsIter((row) => {
const x = row[0] + 200;
const y = row[1] + 200;
ctx.fillRect(x - 2, y - 2, 4, 4);
});
ctx.restore();
ctx.save();
ctx.fillStyle = 'red';
points.rowsIter((row) => {
const x = row[0] + 200;
const y = row[1] + 200;
ctx.fillRect(x - 2, y - 2, 4, 4);
});
ctx.restore();
document.getElementById('mode').innerText = 'pca';
document.getElementById('output').innerText = `${pca}\n${points}`;
document.getElementById('matrix').innerText = `${pos}`;
});
}
start();
</script>
<footer>
<div class="footer-style">
© jk 2023
<a href="https://github.com/JosuaKrause/mdsjs" target="_blank">
[source]
</a>
</div>
</footer>
</body>
</html>