Public
Edited
Feb 3, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
plt = Plot.plot({
style: "overflow: visible;",
color: {
scheme: "warm",
nice: true,
legend: true
},
y: {
grid: true,
label: "Value",
transform: (d) => d ** power
},
marks: [
Plot.line(data, { x: "idx", y: "A", stroke: "ratio" }),
Plot.line(data, { x: "idx", y: "B", stroke: "ratio" }),
Plot.text(
data,
Plot.selectLast({
x: "idx",
y: "A",
z: "ratio",
text: (d) => d.ratio.toFixed(1),
fill: "ratio",
textAnchor: "start",
fontSize: 15,
dx: 5
})
)
]
})
Insert cell
xInit = {
const x = [...eigs.vectors[1]];
// x[1] *= ratio;
return x;
}
Insert cell
data = {
const { inv, types } = H;

const num = 10;

const data = [];

const ratios = d3.range(0.5, 1.5, 0.1);

ratios.map((ratio) => {
const buffer = [[...xInit]];
buffer[0][1] *= ratio;

var b,
y,
loopCount,
loopLimit = 10;
for (let i = 1; i < num; ++i) {
b = buffer[i - 1];
y = math.multiply(inv, b);

loopCount = 0;
while (y[0] < 0 || y[1] < 0) {
loopCount += 1;
if (loopCount > loopLimit) {
b[0] = 0;
b[1] = 0;
}

if (b[0] < b[1]) {
b[1] *= 0.5;
} else {
b[0] *= 0.5;
}
y = math.multiply(inv, b);
}

buffer.push(y);
}

buffer.map((d, i) => {
const record = { idx: i, ratio };

d.map((d, i) => {
record[types[i]] = d;
});

data.push(record);
});
});

return data;
}
Insert cell
data
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
eigs = {
const eigs = math.eigs(H.m);
eigs.vectors = math.transpose(eigs.vectors);
return eigs;
}
Insert cell
Insert cell
H = {
rndMatrix;

var m;

if (useDefaultMatrix) {
m = [
[0.25, 0.14],
[0.4, 0.12]
];
} else {
const rnd = d3.randomUniform(0.1, 0.5);
m = [
[rnd(), rnd()],
[rnd(), rnd()]
];
}

const inv = math.inv(m);

const types = ["A", "B"];
const data = [];
m.map((a, i) => {
a.map((b, j) => {
data.push({ x: types[j], y: types[i], value: b });
});
});

return { m, inv, data, types };
}
Insert cell
math = require("https://cdnjs.cloudflare.com/ajax/libs/mathjs/11.5.0/math.js")
Insert cell
viewof r1 = Inputs.range([0.1, 2], { label: "r1", value: 1 })
Insert cell
r.result.vars
Insert cell
r.result.status
Insert cell
r.result.z
Insert cell
r = glpk.solve(problem)
Insert cell
glpk.write(problem)
Insert cell
problem = {
return {
name: "LP",
objective: {
direction: glpk.GLP_MAX,
name: "obj",
vars: [
{ name: "a", coef: 1.0 },
{ name: "b", coef: r1 }
]
},
subjectTo: [
{
name: "cons0",
vars: [
{ name: "a", coef: 1.0 },
{ name: "b", coef: -1.0 }
],
bnds: { type: glpk.GLP_LO, lb: 0.0 }
},
{
name: "cons1",
vars: [{ name: "a", coef: 1.0 }],
bnds: { type: glpk.GLP_LO, lb: 0.0 }
},
{
name: "cons2",
vars: [{ name: "b", coef: 1.0 }],
bnds: { type: glpk.GLP_LO, lb: 0.0 }
},
{
name: "cons3",
vars: [
{ name: "a", coef: 0.25 },
{ name: "b", coef: 0.14 }
],
bnds: { type: glpk.GLP_UP, ub: 1.0 }
},
{
name: "cons4",
vars: [
{ name: "a", coef: 0.4 },
{ name: "b", coef: 0.12 }
],
bnds: { type: glpk.GLP_UP, ub: 1.0 }
}
]
};
}
Insert cell
glpk.solve({
name: "LP",
objective: {
direction: glpk.GLP_MIN,
name: "obj",
vars: [{ name: "x", coef: 1 }]
},
subjectTo: [
{
name: "cons1",
vars: [{ name: "x", coef: 1.0 }],
bnds: { type: glpk.GLP_UP, lb: -1.0, ub: 1000 }
}
]
})
Insert cell
glpk = {
const module = await import("https://cdn.skypack.dev/glpk.js@4.0.1?min");
return module.default();
}
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