Public
Edited
Oct 7, 2023
1 fork
Insert cell
Insert cell
Insert cell
Insert cell
{
const flatten = flatMat(powerMat(mat, order));

const plt1 = Plot.plot({
color: { scheme: "Blues", nice: true, legend: true, domain: [0, 2 / 7] },
aspectRatio: 1.0,
width: 500,
marks: [
Plot.cell(flatten, {
x: "i",
y: (d) => d.j - mat.length + order,
fill: "x"
}),
Plot.text(flatten, {
x: "i",
y: (d) => d.j - mat.length + order,
text: "x",
fill: "white",
fontSize: "large"
})
]
});

const plt2 = Plot.plot({
grid: true,
color: { scheme: "Blues", nice: true, legend: true, domain: [0, 2 / 7] },
x: { nice: true },
y: { nice: true },
marks: [
Plot.ruleY([2 / 7], { stroke: (d) => 2 / 7 }),
Plot.ruleX([order - 1], { stroke: "gray" }),
Plot.line(ground.res, { x: "i", y: "p", stroke: "gray" }),
Plot.dot(ground.res, { x: "i", y: "p", tip: true, fill: "p" }),
Plot.line(simulation.res, {
x: "i",
y: "p",
z: "j",
stroke: (d) => d3.schemeCategory10[d.j % 10]
})
]
});

return htl.html`
<div id='div-1'>
${plt1}
${plt2}
</div>
`;
}
Insert cell
<style>
#div-1 {
display: flex
}
</style>
Insert cell
powerMat = (mat, n) => {
var res = new ML.Matrix(mat),
m = new ML.Matrix(mat);

for (let i = 1; i < n; ++i) {
res = res.mmul(m);
}

return res.data;
}
Insert cell
flatMat = (mat) => {
const arr = [];

mat.map((vec, j) =>
vec.map((x, i) => {
arr.push({ i, j, x });
})
);

return arr;
}
Insert cell
simulation = {
shuffle;

const m = new ML.Matrix(mat),
res = [],
n = 7,
rnd = d3.randomNormal(),
rndVec = () => [...new Array(n)].map(rnd);

var v = new ML.Matrix([...new Array(m.rows)].map(rndVec));

for (let i = 0; i < 30; ++i) {
v = m.mmul(v);
for (let j = 0; j < n; ++j) {
res.push({ i: i, j, p: v.data[m.rows - 1][j] });
}
}

return { v, res };
}
Insert cell
ground = {
const m = new ML.Matrix(mat),
res = [];

var v = new ML.Matrix([[0], [0], [0], [0], [0], [1]]);

for (let i = 0; i < 30; ++i) {
v = m.mmul(v);

res.push({ i: i, p: v.data[m.rows - 1][0] });
}

return { v, res };
}
Insert cell
ev = new ML.EigenvalueDecomposition(m)
Insert cell
m = new ML.Matrix(mat)
Insert cell
mat = [
[0, 1, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0],
[0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 1],
[1 / 6, 1 / 6, 1 / 6, 1 / 6, 1 / 6, 1 / 6]
]
Insert cell
ML = require("https://cdn.jsdelivr.net/npm/ml-matrix@6.10.5/matrix.umd.min.js")
Insert cell
d3 = require("d3")
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more