-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
112 lines (101 loc) · 2.75 KB
/
Copy pathindex.js
File metadata and controls
112 lines (101 loc) · 2.75 KB
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
import "https://cdn.plot.ly/plotly-3.1.0-rc.0.min.js";
let { solve_abrams_strogatz, solve_diaz_switkes } = wasmBindings;
const model = document.getElementById("model").value;
document.getElementById("abrams").style.display =
model === "abrams" ? "flex" : "none";
document.getElementById("diaz").style.display =
model === "diaz" ? "flex" : "none";
document.getElementById("model").addEventListener("change", async (e) => {
const model = e.target.value;
document.getElementById("abrams").style.display =
model === "abrams" ? "flex" : "none";
document.getElementById("diaz").style.display =
model === "diaz" ? "flex" : "none";
await solve();
});
async function solve(e) {
const model = document.getElementById("model").value;
const params = {
abrams: {
x: Number(document.getElementById("x").value),
y: Number(document.getElementById("y").value),
c: Number(document.getElementById("c").value),
a: Number(document.getElementById("a").value),
s: Number(document.getElementById("s").value),
},
diaz: {
x_1: Number(document.getElementById("x_1").value),
x_2: Number(document.getElementById("x_2").value),
b: Number(document.getElementById("b").value),
m_1: Number(document.getElementById("m_1").value),
m_2: Number(document.getElementById("m_2").value),
g_1: Number(document.getElementById("g_1").value),
g_2: Number(document.getElementById("g_2").value),
alpha: Number(document.getElementById("alpha").value),
beta: Number(document.getElementById("beta").value),
},
}[model];
const t = document.getElementById("t").value;
if (model === "abrams") {
const solution = await solve_abrams_strogatz(
params.x,
params.y,
params.c,
params.a,
params.s,
t,
);
plot([
[solution, "Monolingual 1"],
[solution.map((y) => params.x + params.y - y), "Monolingual 2"],
]);
} else {
const x = await solve_diaz_switkes(
[params.x_1, params.x_2, params.b],
[params.m_1, params.m_2],
[params.g_1, params.g_2],
params.alpha,
params.beta,
t,
0,
);
const b = await solve_diaz_switkes(
[params.x_1, params.x_2, params.b],
[params.m_1, params.m_2],
[params.g_1, params.g_2],
params.alpha,
params.beta,
t,
1,
);
plot([
[x, "Monlingual 1"],
[b, "Bilingual"],
[
x.map(
(y, i) => params.x_1 + params.x_2 + params.b - (y + b[i]),
),
"Monlingual 2",
],
]);
}
}
document.getElementById("solve").addEventListener("click", solve);
solve();
function plot(data) {
Plotly.purge("plot");
let input = [];
for (let i = 0; i < data.length; i++) {
input.push({ y: data[i][0], name: `${data[i][1]}` });
}
const layout = {
title: "Lang Shift",
xaxis: {
title: "Time",
},
yaxis: {
title: "Population",
},
};
Plotly.newPlot("plot", input, layout);
}