@beff/aula 0.8.2 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/generated/aula.core5.wasm +0 -0
- package/dist/index.d.ts +7304 -6
- package/dist/index.js +14864 -11
- package/dist/index.js.map +1 -0
- package/package.json +8 -25
- package/dist/animations.d.ts +0 -635
- package/dist/animations.js +0 -1778
- package/dist/braces.d.ts +0 -84
- package/dist/braces.js +0 -253
- package/dist/card.d.ts +0 -17
- package/dist/card.js +0 -90
- package/dist/colors-palette.d.ts +0 -2242
- package/dist/colors-palette.js +0 -2241
- package/dist/colors.d.ts +0 -120
- package/dist/colors.js +0 -403
- package/dist/curves.d.ts +0 -10
- package/dist/curves.js +0 -128
- package/dist/engine.d.ts +0 -1705
- package/dist/engine.js +0 -3561
- package/dist/errors.d.ts +0 -19
- package/dist/errors.js +0 -25
- package/dist/geometry.d.ts +0 -551
- package/dist/geometry.js +0 -1555
- package/dist/graph.d.ts +0 -117
- package/dist/graph.js +0 -526
- package/dist/graphing.d.ts +0 -597
- package/dist/graphing.js +0 -1504
- package/dist/host.d.ts +0 -19
- package/dist/host.js +0 -85
- package/dist/lexicon.d.ts +0 -10
- package/dist/lexicon.js +0 -23
- package/dist/load.d.ts +0 -21
- package/dist/load.js +0 -109
- package/dist/logo.d.ts +0 -30
- package/dist/logo.js +0 -186
- package/dist/manim.d.ts +0 -22
- package/dist/manim.js +0 -19
- package/dist/media.d.ts +0 -34
- package/dist/media.js +0 -391
- package/dist/mobjects.d.ts +0 -127
- package/dist/mobjects.js +0 -316
- package/dist/picture.d.ts +0 -23
- package/dist/picture.js +0 -291
- package/dist/pointcloud.d.ts +0 -144
- package/dist/pointcloud.js +0 -384
- package/dist/rates.d.ts +0 -19
- package/dist/rates.js +0 -54
- package/dist/run.d.ts +0 -101
- package/dist/run.js +0 -323
- package/dist/tables.d.ts +0 -179
- package/dist/tables.js +0 -403
- package/dist/tex.d.ts +0 -8
- package/dist/tex.js +0 -257
- package/dist/text-layouts.d.ts +0 -64
- package/dist/text-layouts.js +0 -225
- package/dist/text.d.ts +0 -61
- package/dist/text.js +0 -78
- package/dist/theme.d.ts +0 -25
- package/dist/theme.js +0 -12
- package/dist/user-code.d.ts +0 -5
- package/dist/user-code.js +0 -20
- package/dist/utils.d.ts +0 -152
- package/dist/utils.js +0 -517
- package/dist/vector-field.d.ts +0 -132
- package/dist/vector-field.js +0 -361
package/dist/graph.d.ts
DELETED
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
import { type Color, type Point, type StyleOptions, Mobject } from "./engine.ts";
|
|
2
|
-
/** A vertex name. */
|
|
3
|
-
export type Vertex = string | number;
|
|
4
|
-
/** An edge: two vertex names. */
|
|
5
|
-
export type Edge = readonly [Vertex, Vertex];
|
|
6
|
-
/** The style of a vertex: the options of `vertexType` (by default those of `m.Dot`). */
|
|
7
|
-
export type VertexConfig = StyleOptions & {
|
|
8
|
-
radius?: number;
|
|
9
|
-
label?: Mobject;
|
|
10
|
-
};
|
|
11
|
-
/** The style of an edge, with `tipConfig` for DiGraph (the options of `addTip`). */
|
|
12
|
-
export type EdgeConfig = StyleOptions & {
|
|
13
|
-
buff?: number;
|
|
14
|
-
pathArc?: number;
|
|
15
|
-
tipConfig?: {
|
|
16
|
-
tipLength?: number;
|
|
17
|
-
tipWidth?: number;
|
|
18
|
-
};
|
|
19
|
-
};
|
|
20
|
-
/** A layout name of Manim (from networkx). */
|
|
21
|
-
export type LayoutName = "circular" | "kamada_kawai" | "partite" | "planar" | "random" | "shell" | "spectral" | "spiral" | "spring" | "tree";
|
|
22
|
-
/** The options of a layout (Manim `layout_config`, networkx's keyword arguments). */
|
|
23
|
-
export interface LayoutConfig {
|
|
24
|
-
/** `"shell"`: the vertices of each shell. */
|
|
25
|
-
nlist?: readonly (readonly Vertex[])[];
|
|
26
|
-
/** `"shell"`: the turn between shells. */
|
|
27
|
-
rotate?: number;
|
|
28
|
-
/** `"spiral"`: the distance between turns. */
|
|
29
|
-
resolution?: number;
|
|
30
|
-
/** `"tree"`: `"down"` (the default) or `"up"`. */
|
|
31
|
-
orientation?: "up" | "down";
|
|
32
|
-
/** `"tree"`: the space between vertices, `[x, y]`. */
|
|
33
|
-
vertexSpacing?: readonly [number, number];
|
|
34
|
-
/** `"partite"`: the vertex sets. */
|
|
35
|
-
partitions?: readonly (readonly Vertex[])[];
|
|
36
|
-
/** `"tree"`: the root. */
|
|
37
|
-
rootVertex?: Vertex;
|
|
38
|
-
/** `"spring"`: the number of steps. */
|
|
39
|
-
iterations?: number;
|
|
40
|
-
}
|
|
41
|
-
/** The options of `addEdges`. */
|
|
42
|
-
export interface AddEdgesOptions {
|
|
43
|
-
edgeType?: (start: Point, end: Point, config: EdgeConfig) => Mobject;
|
|
44
|
-
edgeConfig?: EdgeConfig & Readonly<Record<string, unknown>>;
|
|
45
|
-
}
|
|
46
|
-
/** A layout function: the position of each vertex. */
|
|
47
|
-
export type LayoutFunction = (vertices: readonly Vertex[], edges: readonly Edge[], opts: {
|
|
48
|
-
scale: number;
|
|
49
|
-
} & LayoutConfig) => ReadonlyMap<Vertex, Point> | Readonly<Record<string, Point>>;
|
|
50
|
-
/** A layout: a name, the positions of the vertices, or a function. */
|
|
51
|
-
export type Layout = LayoutName | ReadonlyMap<Vertex, Point> | Readonly<Record<string, Point>> | LayoutFunction;
|
|
52
|
-
/** The options of `changeLayout`. */
|
|
53
|
-
export interface LayoutOptions {
|
|
54
|
-
layout?: Layout;
|
|
55
|
-
layoutScale?: number;
|
|
56
|
-
/** Options of the layout, such as `nlist` (shell), `resolution` (spiral), `vertexSpacing`
|
|
57
|
-
* and `orientation` (tree). */
|
|
58
|
-
layoutConfig?: LayoutConfig;
|
|
59
|
-
/** The vertex sets of the `"partite"` layout. */
|
|
60
|
-
partitions?: readonly (readonly Vertex[])[];
|
|
61
|
-
/** The root of the `"tree"` layout. */
|
|
62
|
-
rootVertex?: Vertex;
|
|
63
|
-
}
|
|
64
|
-
/** The options of Graph and DiGraph (Manim's keyword arguments). */
|
|
65
|
-
export interface GraphOptions extends LayoutOptions {
|
|
66
|
-
/** True: a `MathTex` of each name. An object: the label of each vertex. */
|
|
67
|
-
labels?: boolean | Readonly<Record<string, Mobject>>;
|
|
68
|
-
labelFillColor?: Color;
|
|
69
|
-
/** Makes a vertex from its config (default: `m.Dot`, or a labelled dot with a label). */
|
|
70
|
-
vertexType?: (config: VertexConfig) => Mobject;
|
|
71
|
-
/** The config of every vertex; a key that is a vertex name gives the config of that vertex. */
|
|
72
|
-
vertexConfig?: VertexConfig & Readonly<Record<string, unknown>>;
|
|
73
|
-
/** Mobjects to use as vertices, by name. */
|
|
74
|
-
vertexMobjects?: Readonly<Record<string, Mobject>>;
|
|
75
|
-
/** Makes an edge (default: `m.Line`). */
|
|
76
|
-
edgeType?: (start: Point, end: Point, config: EdgeConfig) => Mobject;
|
|
77
|
-
/** The config of every edge; a key `"u,v"` gives the config of that edge. */
|
|
78
|
-
edgeConfig?: EdgeConfig & Readonly<Record<string, unknown>>;
|
|
79
|
-
}
|
|
80
|
-
/** A graph of vertices and edges: Manim's `Graph` (undirected) and `DiGraph` (directed, with a
|
|
81
|
-
* tip on each edge). An updater keeps the edges on their vertices. */
|
|
82
|
-
export declare class GraphMobject extends Mobject {
|
|
83
|
-
#private;
|
|
84
|
-
/** The vertices by name (Manim `vertices`). */
|
|
85
|
-
readonly vertices: Map<Vertex, Mobject>;
|
|
86
|
-
/** The edges by `"u,v"` (Manim `edges`). */
|
|
87
|
-
readonly edges: Map<string, Mobject>;
|
|
88
|
-
/** A vertex by name, or an edge by its two names (Manim `graph[k]`). */
|
|
89
|
-
get(k: Vertex | Edge): Mobject;
|
|
90
|
-
/** Moves each edge onto its vertices (Manim `update_edges`). */
|
|
91
|
-
updateEdges(graph?: GraphMobject): this;
|
|
92
|
-
/** Adds vertices at `positions` (by default the centre of the graph) and returns them. */
|
|
93
|
-
addVertices(...args: (Vertex | {
|
|
94
|
-
positions?: Readonly<Record<string, Point>>;
|
|
95
|
-
labels?: boolean | Readonly<Record<string, Mobject>>;
|
|
96
|
-
labelFillColor?: Color;
|
|
97
|
-
vertexType?: (config: VertexConfig) => Mobject;
|
|
98
|
-
vertexConfig?: VertexConfig & Readonly<Record<string, unknown>>;
|
|
99
|
-
vertexMobjects?: Readonly<Record<string, Mobject>>;
|
|
100
|
-
})[]): Mobject[];
|
|
101
|
-
/** Removes vertices and their edges, and returns them as a VGroup. */
|
|
102
|
-
removeVertices(...vertices: Vertex[]): Mobject;
|
|
103
|
-
/** Adds edges (and their missing vertices), and returns the new mobjects as a VGroup. */
|
|
104
|
-
addEdges(...args: (Edge | AddEdgesOptions)[]): Mobject;
|
|
105
|
-
/** Removes edges and returns them as a VGroup. */
|
|
106
|
-
removeEdges(...edges: Edge[]): Mobject;
|
|
107
|
-
/** Moves the vertices to a new layout (Manim `change_layout`). */
|
|
108
|
-
changeLayout(opts?: LayoutOptions): this;
|
|
109
|
-
/** Sets up a new graph: the defaults, the vertices, the layout and the edges. */
|
|
110
|
-
init(vertices: readonly Vertex[], edges: readonly Edge[], o: GraphOptions, directed: boolean): this;
|
|
111
|
-
}
|
|
112
|
-
/** Manim's `Graph(vertices, edges, opts)`: dots joined by lines, in the `"spring"` layout by
|
|
113
|
-
* default. */
|
|
114
|
-
export declare function Graph(vertices: readonly Vertex[], edges: readonly Edge[], opts?: GraphOptions): GraphMobject;
|
|
115
|
-
/** Manim's `DiGraph(vertices, edges, opts)`: a Graph with a tip on each edge, from the border of
|
|
116
|
-
* one vertex to the border of the other. */
|
|
117
|
-
export declare function DiGraph(vertices: readonly Vertex[], edges: readonly Edge[], opts?: GraphOptions): GraphMobject;
|
package/dist/graph.js
DELETED
|
@@ -1,526 +0,0 @@
|
|
|
1
|
-
// Manim CE's network graphs: Graph and DiGraph, with the layouts of networkx that Manim names.
|
|
2
|
-
// aula computes the layouts itself (no networkx): "spring" and "kamada_kawai" start from a
|
|
3
|
-
// circle, so they are the same on every run.
|
|
4
|
-
import { BLACK, MathTex, Mobject, ORIGIN, VGroup, add, mul, norm, sub, wrong, } from "./engine.js";
|
|
5
|
-
import { Dot, Line } from "./geometry.js";
|
|
6
|
-
import { engine } from "./load.js";
|
|
7
|
-
const edgeKey = (u, v) => `${String(u)},${String(v)}`;
|
|
8
|
-
/** A dot with a label in its centre (Manim `LabeledDot`), black on white. */
|
|
9
|
-
function labeledDot(config) {
|
|
10
|
-
const { label, radius, ...style } = config;
|
|
11
|
-
const size = label === undefined ? 0 : Math.max(label.width, label.height);
|
|
12
|
-
const dot = Dot(ORIGIN, { radius: radius ?? 0.1 + size / 2, ...style });
|
|
13
|
-
if (label !== undefined)
|
|
14
|
-
dot.add(label.moveTo(dot.getCenter()));
|
|
15
|
-
return dot;
|
|
16
|
-
}
|
|
17
|
-
/** networkx's `rescale_layout`: centred, with the largest coordinate `scale`. */
|
|
18
|
-
function rescale(pos, scale) {
|
|
19
|
-
const ps = [...pos.values()];
|
|
20
|
-
if (ps.length === 0)
|
|
21
|
-
return pos;
|
|
22
|
-
const mean = [ps.reduce((a, p) => a + p[0], 0) / ps.length, ps.reduce((a, p) => a + p[1], 0) / ps.length];
|
|
23
|
-
const lim = Math.max(...ps.map((p) => Math.max(Math.abs(p[0] - mean[0]), Math.abs(p[1] - mean[1]))));
|
|
24
|
-
const k = lim > 0 ? scale / lim : 1;
|
|
25
|
-
return new Map([...pos].map(([v, p]) => [v, mul(sub(p, mean), k)]));
|
|
26
|
-
}
|
|
27
|
-
function circular(vs, scale) {
|
|
28
|
-
if (vs.length === 1)
|
|
29
|
-
return new Map([[vs[0] ?? 0, [0, 0]]]);
|
|
30
|
-
return rescale(new Map(vs.map((v, i) => [v, [Math.cos((2 * Math.PI * i) / vs.length), Math.sin((2 * Math.PI * i) / vs.length)]])), scale);
|
|
31
|
-
}
|
|
32
|
-
/** A seeded random number from 0 to 1 (mulberry32). */
|
|
33
|
-
function random(seed) {
|
|
34
|
-
let a = seed;
|
|
35
|
-
return () => {
|
|
36
|
-
a = (a + 0x6d2b79f5) | 0;
|
|
37
|
-
let t = Math.imul(a ^ (a >>> 15), 1 | a);
|
|
38
|
-
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
|
|
39
|
-
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
/** The shortest path lengths between all vertices (breadth first). */
|
|
43
|
-
function distances(vs, es) {
|
|
44
|
-
const index = new Map(vs.map((v, i) => [v, i]));
|
|
45
|
-
const next = vs.map(() => []);
|
|
46
|
-
for (const [u, v] of es) {
|
|
47
|
-
const [a, b] = [index.get(u), index.get(v)];
|
|
48
|
-
if (a === undefined || b === undefined)
|
|
49
|
-
continue;
|
|
50
|
-
next[a]?.push(b);
|
|
51
|
-
next[b]?.push(a);
|
|
52
|
-
}
|
|
53
|
-
return vs.map((_, s) => {
|
|
54
|
-
const d = vs.map(() => Infinity);
|
|
55
|
-
d[s] = 0;
|
|
56
|
-
const queue = [s];
|
|
57
|
-
for (let q = 0; q < queue.length; q++) {
|
|
58
|
-
const x = queue[q] ?? 0;
|
|
59
|
-
for (const y of next[x] ?? []) {
|
|
60
|
-
if (d[y] === Infinity) {
|
|
61
|
-
d[y] = (d[x] ?? 0) + 1;
|
|
62
|
-
queue.push(y);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
return d;
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
/** Fruchterman-Reingold, as networkx's `spring_layout`, from a circle. */
|
|
70
|
-
function spring(vs, es, scale, iterations = 50) {
|
|
71
|
-
const n = vs.length;
|
|
72
|
-
if (n <= 1)
|
|
73
|
-
return circular(vs, scale);
|
|
74
|
-
const index = new Map(vs.map((v, i) => [v, i]));
|
|
75
|
-
const pos = vs.map((_, i) => [
|
|
76
|
-
Math.cos((2 * Math.PI * i) / n) * 0.5 + 0.5,
|
|
77
|
-
Math.sin((2 * Math.PI * i) / n) * 0.5 + 0.5,
|
|
78
|
-
]);
|
|
79
|
-
const k = Math.sqrt(1 / n);
|
|
80
|
-
let t = 0.1;
|
|
81
|
-
const dt = t / (iterations + 1);
|
|
82
|
-
for (let it = 0; it < iterations; it++) {
|
|
83
|
-
const disp = pos.map(() => [0, 0]);
|
|
84
|
-
for (let i = 0; i < n; i++) {
|
|
85
|
-
for (let j = 0; j < n; j++) {
|
|
86
|
-
if (i === j)
|
|
87
|
-
continue;
|
|
88
|
-
const d = sub(pos[i] ?? [0, 0], pos[j] ?? [0, 0]);
|
|
89
|
-
const dist = Math.max(norm(d), 0.01);
|
|
90
|
-
disp[i] = add(disp[i] ?? [0, 0], mul(d, (k * k) / (dist * dist)));
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
for (const [u, v] of es) {
|
|
94
|
-
const [a, b] = [index.get(u) ?? 0, index.get(v) ?? 0];
|
|
95
|
-
const d = sub(pos[a] ?? [0, 0], pos[b] ?? [0, 0]);
|
|
96
|
-
const dist = Math.max(norm(d), 0.01);
|
|
97
|
-
const f = mul(d, dist / k);
|
|
98
|
-
disp[a] = sub(disp[a] ?? [0, 0], f);
|
|
99
|
-
disp[b] = add(disp[b] ?? [0, 0], f);
|
|
100
|
-
}
|
|
101
|
-
for (let i = 0; i < n; i++) {
|
|
102
|
-
const d = disp[i] ?? [0, 0];
|
|
103
|
-
const length = Math.max(norm(d), 0.01);
|
|
104
|
-
pos[i] = add(pos[i] ?? [0, 0], mul(d, t / length));
|
|
105
|
-
}
|
|
106
|
-
t -= dt;
|
|
107
|
-
}
|
|
108
|
-
return rescale(new Map(vs.map((v, i) => [v, pos[i] ?? [0, 0]])), scale);
|
|
109
|
-
}
|
|
110
|
-
/** Stress majorization on the path lengths (the energy of networkx's `kamada_kawai_layout`). */
|
|
111
|
-
function kamadaKawai(vs, es, scale) {
|
|
112
|
-
const n = vs.length;
|
|
113
|
-
if (n <= 2)
|
|
114
|
-
return circular(vs, scale);
|
|
115
|
-
const raw0 = distances(vs, es);
|
|
116
|
-
const finite = raw0.flat().filter((d) => Number.isFinite(d));
|
|
117
|
-
const far = Math.max(1, ...finite) + 1;
|
|
118
|
-
const d = raw0.map((row) => row.map((x) => (Number.isFinite(x) ? x : far)));
|
|
119
|
-
const pos = vs.map((_, i) => [Math.cos((2 * Math.PI * i) / n), Math.sin((2 * Math.PI * i) / n)]);
|
|
120
|
-
for (let it = 0; it < 200; it++) {
|
|
121
|
-
for (let i = 0; i < n; i++) {
|
|
122
|
-
let [sx, sy, sw] = [0, 0, 0];
|
|
123
|
-
for (let j = 0; j < n; j++) {
|
|
124
|
-
if (i === j)
|
|
125
|
-
continue;
|
|
126
|
-
const dij = d[i]?.[j] ?? 1;
|
|
127
|
-
const w = 1 / (dij * dij);
|
|
128
|
-
const pj = pos[j] ?? [0, 0];
|
|
129
|
-
const delta = sub(pos[i] ?? [0, 0], pj);
|
|
130
|
-
const dist = Math.max(norm(delta), 1e-9);
|
|
131
|
-
sx += w * (pj[0] + (dij * delta[0]) / dist);
|
|
132
|
-
sy += w * (pj[1] + (dij * delta[1]) / dist);
|
|
133
|
-
sw += w;
|
|
134
|
-
}
|
|
135
|
-
pos[i] = [sx / sw, sy / sw];
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
return rescale(new Map(vs.map((v, i) => [v, pos[i] ?? [0, 0]])), scale);
|
|
139
|
-
}
|
|
140
|
-
/** `x[i][j]`, or 0. */
|
|
141
|
-
function at(x, i, j) {
|
|
142
|
-
return x[i]?.[j] ?? 0;
|
|
143
|
-
}
|
|
144
|
-
/** Sets `x[i][j]`. */
|
|
145
|
-
function set(x, i, j, value) {
|
|
146
|
-
const row = x[i];
|
|
147
|
-
if (row !== undefined)
|
|
148
|
-
row[j] = value;
|
|
149
|
-
}
|
|
150
|
-
/** The eigenvectors of a symmetric matrix (Jacobi), sorted by eigenvalue. */
|
|
151
|
-
function eigenvectors(a) {
|
|
152
|
-
const n = a.length;
|
|
153
|
-
const m = a.map((r) => [...r]);
|
|
154
|
-
const v = Array.from({ length: n }, (_r, i) => Array.from({ length: n }, (_c, j) => (i === j ? 1 : 0)));
|
|
155
|
-
for (let sweep = 0; sweep < 100; sweep++) {
|
|
156
|
-
let off = 0;
|
|
157
|
-
for (let p = 0; p < n; p++)
|
|
158
|
-
for (let q = p + 1; q < n; q++)
|
|
159
|
-
off += at(m, p, q) ** 2;
|
|
160
|
-
if (off < 1e-18)
|
|
161
|
-
break;
|
|
162
|
-
for (let p = 0; p < n; p++) {
|
|
163
|
-
for (let q = p + 1; q < n; q++) {
|
|
164
|
-
if (Math.abs(at(m, p, q)) < 1e-15)
|
|
165
|
-
continue;
|
|
166
|
-
const theta = (at(m, q, q) - at(m, p, p)) / (2 * at(m, p, q));
|
|
167
|
-
const t = Math.sign(theta || 1) / (Math.abs(theta) + Math.sqrt(theta * theta + 1));
|
|
168
|
-
const c = 1 / Math.sqrt(t * t + 1);
|
|
169
|
-
const s = t * c;
|
|
170
|
-
for (let k = 0; k < n; k++) {
|
|
171
|
-
const [mkp, mkq] = [at(m, k, p), at(m, k, q)];
|
|
172
|
-
set(m, k, p, c * mkp - s * mkq);
|
|
173
|
-
set(m, k, q, s * mkp + c * mkq);
|
|
174
|
-
}
|
|
175
|
-
for (let k = 0; k < n; k++) {
|
|
176
|
-
const [mpk, mqk] = [at(m, p, k), at(m, q, k)];
|
|
177
|
-
set(m, p, k, c * mpk - s * mqk);
|
|
178
|
-
set(m, q, k, s * mpk + c * mqk);
|
|
179
|
-
}
|
|
180
|
-
for (let k = 0; k < n; k++) {
|
|
181
|
-
const [vkp, vkq] = [at(v, k, p), at(v, k, q)];
|
|
182
|
-
set(v, k, p, c * vkp - s * vkq);
|
|
183
|
-
set(v, k, q, s * vkp + c * vkq);
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
return Array.from({ length: n }, (_, i) => ({ value: at(m, i, i), vector: v.map((row) => row[i] ?? 0) })).toSorted((x, y) => x.value - y.value);
|
|
189
|
-
}
|
|
190
|
-
/** The eigenvectors of the second and third smallest eigenvalues of the Laplacian (networkx's
|
|
191
|
-
* `spectral_layout`). */
|
|
192
|
-
function spectral(vs, es, scale) {
|
|
193
|
-
const n = vs.length;
|
|
194
|
-
if (n <= 2)
|
|
195
|
-
return circular(vs, scale);
|
|
196
|
-
const index = new Map(vs.map((v, i) => [v, i]));
|
|
197
|
-
const l = vs.map(() => vs.map(() => 0));
|
|
198
|
-
for (const [u, v] of es) {
|
|
199
|
-
const [a, b] = [index.get(u), index.get(v)];
|
|
200
|
-
if (a === undefined || b === undefined || a === b)
|
|
201
|
-
continue;
|
|
202
|
-
for (const [i, j, w] of [
|
|
203
|
-
[a, b, -1],
|
|
204
|
-
[b, a, -1],
|
|
205
|
-
[a, a, 1],
|
|
206
|
-
[b, b, 1],
|
|
207
|
-
]) {
|
|
208
|
-
const row = l[i];
|
|
209
|
-
if (row !== undefined)
|
|
210
|
-
row[j] = (row[j] ?? 0) + w;
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
const e = eigenvectors(l);
|
|
214
|
-
const [x, y] = [e[1]?.vector ?? [], e[2]?.vector ?? []];
|
|
215
|
-
return rescale(new Map(vs.map((v, i) => [v, [x[i] ?? 0, y[i] ?? 0]])), scale);
|
|
216
|
-
}
|
|
217
|
-
function shell(vs, scale, config) {
|
|
218
|
-
const nlist = config.nlist ?? [vs];
|
|
219
|
-
if (vs.length === 1)
|
|
220
|
-
return new Map([[vs[0] ?? 0, [0, 0]]]);
|
|
221
|
-
const bump = scale / nlist.length;
|
|
222
|
-
const rotate = config.rotate ?? Math.PI / nlist.length;
|
|
223
|
-
let radius = (nlist[0]?.length ?? 0) === 1 ? 0 : bump;
|
|
224
|
-
let first = rotate;
|
|
225
|
-
const pos = new Map();
|
|
226
|
-
for (const nodes of nlist) {
|
|
227
|
-
nodes.forEach((v, i) => {
|
|
228
|
-
const theta = (2 * Math.PI * i) / nodes.length + first;
|
|
229
|
-
pos.set(v, [radius * Math.cos(theta), radius * Math.sin(theta)]);
|
|
230
|
-
});
|
|
231
|
-
radius += bump;
|
|
232
|
-
first += rotate;
|
|
233
|
-
}
|
|
234
|
-
return pos;
|
|
235
|
-
}
|
|
236
|
-
function spiral(vs, scale, config) {
|
|
237
|
-
const resolution = config.resolution ?? 0.35;
|
|
238
|
-
return rescale(new Map(vs.map((v, i) => [v, [i * Math.cos(resolution * i), i * Math.sin(resolution * i)]])), scale);
|
|
239
|
-
}
|
|
240
|
-
function partite(vs, scale, partitions) {
|
|
241
|
-
if (partitions === undefined || partitions.length === 0) {
|
|
242
|
-
return wrong("Graph", 'the "partite" layout needs the partitions', "give partitions: [[...], [...]]");
|
|
243
|
-
}
|
|
244
|
-
const rest = vs.filter((v) => !partitions.some((p) => p.includes(v)));
|
|
245
|
-
const layers = rest.length > 0 ? [...partitions, rest] : [...partitions];
|
|
246
|
-
const pos = new Map();
|
|
247
|
-
layers.forEach((layer, i) => {
|
|
248
|
-
layer.forEach((v, j) => pos.set(v, [i - (layers.length - 1) / 2, j - (layer.length - 1) / 2]));
|
|
249
|
-
});
|
|
250
|
-
return rescale(pos, scale);
|
|
251
|
-
}
|
|
252
|
-
/** Manim's `_tree_layout`: the root on top, each level one unit down. */
|
|
253
|
-
function tree(vs, es, scale, root, config) {
|
|
254
|
-
if (root === undefined)
|
|
255
|
-
return wrong("Graph", 'the "tree" layout needs the root vertex', "give rootVertex");
|
|
256
|
-
if (es.length !== vs.length - 1)
|
|
257
|
-
return wrong("Graph", 'the "tree" layout needs a tree');
|
|
258
|
-
const neighbours = new Map(vs.map((v) => [v, []]));
|
|
259
|
-
for (const [u, v] of es) {
|
|
260
|
-
neighbours.get(u)?.push(v);
|
|
261
|
-
neighbours.get(v)?.push(u);
|
|
262
|
-
}
|
|
263
|
-
const o = config.orientation === "up" ? 1 : -1;
|
|
264
|
-
const children = new Map([[root, [...(neighbours.get(root) ?? [])]]]);
|
|
265
|
-
const stack = [[...(children.get(root) ?? [])]];
|
|
266
|
-
const stick = [root];
|
|
267
|
-
const parent = new Map((children.get(root) ?? []).map((c) => [c, root]));
|
|
268
|
-
const pos = new Map();
|
|
269
|
-
const obstruction = new Map();
|
|
270
|
-
const slide = (v, dx) => {
|
|
271
|
-
let level = [v];
|
|
272
|
-
while (level.length > 0) {
|
|
273
|
-
const next = [];
|
|
274
|
-
for (const u of level) {
|
|
275
|
-
const [x, y] = pos.get(u) ?? [0, 0];
|
|
276
|
-
pos.set(u, [x + dx, y]);
|
|
277
|
-
obstruction.set(y, Math.max(x + dx + 1, obstruction.get(y) ?? 0));
|
|
278
|
-
next.push(...(children.get(u) ?? []));
|
|
279
|
-
}
|
|
280
|
-
level = next;
|
|
281
|
-
}
|
|
282
|
-
};
|
|
283
|
-
while (stack.length > 0) {
|
|
284
|
-
const top = stack.at(-1) ?? [];
|
|
285
|
-
if (top.length === 0) {
|
|
286
|
-
const p = stick.pop() ?? root;
|
|
287
|
-
stack.pop();
|
|
288
|
-
const cp = children.get(p) ?? [];
|
|
289
|
-
const y = o * stack.length;
|
|
290
|
-
if (cp.length === 0)
|
|
291
|
-
pos.set(p, [obstruction.get(y) ?? 0, y]);
|
|
292
|
-
else {
|
|
293
|
-
let x = cp.reduce((a, c) => a + (pos.get(c)?.[0] ?? 0), 0) / cp.length;
|
|
294
|
-
pos.set(p, [x, y]);
|
|
295
|
-
const ox = obstruction.get(y) ?? 0;
|
|
296
|
-
if (x < ox) {
|
|
297
|
-
slide(p, ox - x);
|
|
298
|
-
x = ox;
|
|
299
|
-
}
|
|
300
|
-
obstruction.set(y, x + 1);
|
|
301
|
-
continue;
|
|
302
|
-
}
|
|
303
|
-
obstruction.set(y, (pos.get(p)?.[0] ?? 0) + 1);
|
|
304
|
-
continue;
|
|
305
|
-
}
|
|
306
|
-
const t = top.pop() ?? root;
|
|
307
|
-
const pt = parent.get(t);
|
|
308
|
-
const ct = (neighbours.get(t) ?? []).filter((u) => u !== pt);
|
|
309
|
-
for (const c of ct)
|
|
310
|
-
parent.set(c, t);
|
|
311
|
-
children.set(t, [...ct]);
|
|
312
|
-
stack.push(ct);
|
|
313
|
-
stick.push(t);
|
|
314
|
-
}
|
|
315
|
-
const ps = [...pos.values()];
|
|
316
|
-
const [xMin, xMax] = [Math.min(...ps.map((p) => p[0])), Math.max(...ps.map((p) => p[0]))];
|
|
317
|
-
const [yMin, yMax] = [Math.min(...ps.map((p) => p[1])), Math.max(...ps.map((p) => p[1]))];
|
|
318
|
-
const center = [(xMin + xMax) / 2, (yMin + yMax) / 2];
|
|
319
|
-
const [width, height] = [xMax - xMin, yMax - yMin];
|
|
320
|
-
const spacing = config.vertexSpacing;
|
|
321
|
-
const sf = spacing !== undefined
|
|
322
|
-
? [spacing[0], spacing[1]]
|
|
323
|
-
: width > 0 || height > 0
|
|
324
|
-
? [(2 * scale) / Math.max(width, height), (2 * scale) / Math.max(width, height)]
|
|
325
|
-
: [1, 1];
|
|
326
|
-
return new Map([...pos].map(([v, [x, y]]) => [v, [(x - center[0]) * sf[0], (y - center[1]) * sf[1]]]));
|
|
327
|
-
}
|
|
328
|
-
function positionsOf(p) {
|
|
329
|
-
return p instanceof Map ? new Map(p) : new Map(Object.entries(p));
|
|
330
|
-
}
|
|
331
|
-
/** The position of each vertex in `layout` (Manim `_determine_graph_layout`). */
|
|
332
|
-
function layoutOf(vs, es, o) {
|
|
333
|
-
const { layout = "spring", layoutScale = 2, layoutConfig = {}, partitions, rootVertex } = o;
|
|
334
|
-
if (typeof layout === "function")
|
|
335
|
-
return positionsOf(layout(vs, es, { scale: layoutScale, ...layoutConfig }));
|
|
336
|
-
if (typeof layout !== "string")
|
|
337
|
-
return positionsOf(layout);
|
|
338
|
-
switch (layout) {
|
|
339
|
-
case "circular":
|
|
340
|
-
return circular(vs, layoutScale);
|
|
341
|
-
case "kamada_kawai":
|
|
342
|
-
return kamadaKawai(vs, es, layoutScale);
|
|
343
|
-
case "partite":
|
|
344
|
-
return partite(vs, layoutScale, partitions ?? layoutConfig.partitions);
|
|
345
|
-
case "random": {
|
|
346
|
-
const r = random(0);
|
|
347
|
-
return new Map(vs.map((v) => [v, [(2 * r() - 1) * layoutScale, (2 * r() - 1) * layoutScale]]));
|
|
348
|
-
}
|
|
349
|
-
case "shell":
|
|
350
|
-
return shell(vs, layoutScale, layoutConfig);
|
|
351
|
-
case "spectral":
|
|
352
|
-
return spectral(vs, es, layoutScale);
|
|
353
|
-
case "spiral":
|
|
354
|
-
return spiral(vs, layoutScale, layoutConfig);
|
|
355
|
-
case "spring":
|
|
356
|
-
return spring(vs, es, layoutScale, layoutConfig.iterations ?? 50);
|
|
357
|
-
case "tree":
|
|
358
|
-
return tree(vs, es, layoutScale, rootVertex ?? layoutConfig.rootVertex, layoutConfig);
|
|
359
|
-
default:
|
|
360
|
-
return wrong("Graph", `the "${layout}" layout is not available`, "use another layout or give the positions");
|
|
361
|
-
}
|
|
362
|
-
}
|
|
363
|
-
/** A graph of vertices and edges: Manim's `Graph` (undirected) and `DiGraph` (directed, with a
|
|
364
|
-
* tip on each edge). An updater keeps the edges on their vertices. */
|
|
365
|
-
export class GraphMobject extends Mobject {
|
|
366
|
-
/** The vertices by name (Manim `vertices`). */
|
|
367
|
-
vertices = new Map();
|
|
368
|
-
/** The edges by `"u,v"` (Manim `edges`). */
|
|
369
|
-
edges = new Map();
|
|
370
|
-
#ends = new Map();
|
|
371
|
-
#edgeConfig = new Map();
|
|
372
|
-
#defaultVertexConfig = {};
|
|
373
|
-
#defaultEdgeConfig = {};
|
|
374
|
-
#directed = false;
|
|
375
|
-
#edgeType = (a, b, c) => Line(a, b, c);
|
|
376
|
-
/** A vertex by name, or an edge by its two names (Manim `graph[k]`). */
|
|
377
|
-
get(k) {
|
|
378
|
-
const mob = typeof k === "object" ? this.edges.get(edgeKey(k[0], k[1])) : this.vertices.get(k);
|
|
379
|
-
return mob ?? wrong("Graph", `could not find ${JSON.stringify(k)} in the vertices or the edges`);
|
|
380
|
-
}
|
|
381
|
-
/** Moves each edge onto its vertices (Manim `update_edges`). */
|
|
382
|
-
updateEdges(graph = this) {
|
|
383
|
-
for (const [key, edge] of graph.edges) {
|
|
384
|
-
const [u, v] = graph.#ends.get(key) ?? ["", ""];
|
|
385
|
-
edge.become(graph.#edgeMobject(u, v, graph.#edgeConfig.get(key) ?? {}));
|
|
386
|
-
}
|
|
387
|
-
return this;
|
|
388
|
-
}
|
|
389
|
-
#edgeMobject(u, v, config) {
|
|
390
|
-
const [a, b] = [this.get(u), this.get(v)];
|
|
391
|
-
let [start, end] = [a.getCenter(), b.getCenter()];
|
|
392
|
-
const { tipConfig, ...lineConfig } = config;
|
|
393
|
-
if (this.#directed) {
|
|
394
|
-
const d = sub(end, start);
|
|
395
|
-
const n = norm(d);
|
|
396
|
-
if (n > 0) {
|
|
397
|
-
const unit = mul(d, 1 / n);
|
|
398
|
-
start = add(start, mul(unit, Math.max(a.width, a.height) / 2));
|
|
399
|
-
end = sub(end, mul(unit, Math.max(b.width, b.height) / 2));
|
|
400
|
-
}
|
|
401
|
-
}
|
|
402
|
-
const edge = this.#edgeType(start, end, lineConfig);
|
|
403
|
-
return this.#directed ? edge.addTip(tipConfig ?? {}) : edge;
|
|
404
|
-
}
|
|
405
|
-
/** Adds vertices at `positions` (by default the centre of the graph) and returns them. */
|
|
406
|
-
addVertices(...args) {
|
|
407
|
-
const vertices = args.filter((a) => typeof a !== "object");
|
|
408
|
-
const o = args.find((a) => typeof a === "object") ?? {};
|
|
409
|
-
const center = this.length > 0 ? this.getCenter() : ORIGIN;
|
|
410
|
-
return vertices.map((v) => {
|
|
411
|
-
if (this.vertices.has(v))
|
|
412
|
-
wrong("addVertices", `the vertex ${JSON.stringify(v)} is already in the graph`);
|
|
413
|
-
const labels = o.labels ?? false;
|
|
414
|
-
const label = typeof labels === "boolean"
|
|
415
|
-
? labels
|
|
416
|
-
? MathTex(String(v), { color: o.labelFillColor ?? BLACK })
|
|
417
|
-
: undefined
|
|
418
|
-
: labels[String(v)];
|
|
419
|
-
const config = {
|
|
420
|
-
...this.#defaultVertexConfig,
|
|
421
|
-
...vertexConfigOf(o.vertexConfig, v),
|
|
422
|
-
...(label === undefined ? {} : { label }),
|
|
423
|
-
};
|
|
424
|
-
const make = o.vertexType ?? (label === undefined ? (c) => Dot(ORIGIN, c) : labeledDot);
|
|
425
|
-
const mob = (o.vertexMobjects?.[String(v)] ?? make(config)).moveTo(o.positions?.[String(v)] ?? center);
|
|
426
|
-
this.vertices.set(v, mob);
|
|
427
|
-
this.add(mob);
|
|
428
|
-
return mob;
|
|
429
|
-
});
|
|
430
|
-
}
|
|
431
|
-
/** Removes vertices and their edges, and returns them as a VGroup. */
|
|
432
|
-
removeVertices(...vertices) {
|
|
433
|
-
const removed = [];
|
|
434
|
-
for (const v of vertices) {
|
|
435
|
-
const mob = this.vertices.get(v) ?? wrong("removeVertices", `the graph has no vertex ${JSON.stringify(v)}`);
|
|
436
|
-
for (const [key, [a, b]] of this.#ends) {
|
|
437
|
-
if (a === v || b === v)
|
|
438
|
-
removed.push(this.#removeEdge(key));
|
|
439
|
-
}
|
|
440
|
-
this.vertices.delete(v);
|
|
441
|
-
this.remove(mob);
|
|
442
|
-
removed.push(mob);
|
|
443
|
-
}
|
|
444
|
-
return VGroup(removed);
|
|
445
|
-
}
|
|
446
|
-
/** Adds edges (and their missing vertices), and returns the new mobjects as a VGroup. */
|
|
447
|
-
addEdges(...args) {
|
|
448
|
-
const edges = args.filter((a) => Array.isArray(a));
|
|
449
|
-
const o = args.find((a) => !Array.isArray(a));
|
|
450
|
-
const added = [];
|
|
451
|
-
const missing = [...new Set(edges.flat())].filter((v) => !this.vertices.has(v));
|
|
452
|
-
added.push(...this.addVertices(...missing));
|
|
453
|
-
if (o?.edgeType !== undefined)
|
|
454
|
-
this.#edgeType = o.edgeType;
|
|
455
|
-
for (const [u, v] of edges) {
|
|
456
|
-
const key = edgeKey(u, v);
|
|
457
|
-
const config = { ...this.#defaultEdgeConfig, ...edgeConfigOf(o?.edgeConfig, key) };
|
|
458
|
-
const edge = this.#edgeMobject(u, v, config);
|
|
459
|
-
this.#ends.set(key, [u, v]);
|
|
460
|
-
this.#edgeConfig.set(key, config);
|
|
461
|
-
this.edges.set(key, edge);
|
|
462
|
-
this.add(edge);
|
|
463
|
-
added.push(edge);
|
|
464
|
-
}
|
|
465
|
-
return VGroup(added);
|
|
466
|
-
}
|
|
467
|
-
#removeEdge(key) {
|
|
468
|
-
const edge = this.edges.get(key) ?? wrong("removeEdges", `the graph has no edge ${key}`);
|
|
469
|
-
this.edges.delete(key);
|
|
470
|
-
this.#ends.delete(key);
|
|
471
|
-
this.#edgeConfig.delete(key);
|
|
472
|
-
this.remove(edge);
|
|
473
|
-
return edge;
|
|
474
|
-
}
|
|
475
|
-
/** Removes edges and returns them as a VGroup. */
|
|
476
|
-
removeEdges(...edges) {
|
|
477
|
-
return VGroup(edges.map(([u, v]) => this.#removeEdge(edgeKey(u, v))));
|
|
478
|
-
}
|
|
479
|
-
/** Moves the vertices to a new layout (Manim `change_layout`). */
|
|
480
|
-
changeLayout(opts = {}) {
|
|
481
|
-
const pos = layoutOf([...this.vertices.keys()], [...this.#ends.values()], opts);
|
|
482
|
-
for (const [v, mob] of this.vertices)
|
|
483
|
-
mob.moveTo(pos.get(v) ?? pos.get(String(v)) ?? ORIGIN);
|
|
484
|
-
return this.updateEdges();
|
|
485
|
-
}
|
|
486
|
-
/** Sets up a new graph: the defaults, the vertices, the layout and the edges. */
|
|
487
|
-
init(vertices, edges, o, directed) {
|
|
488
|
-
this.#directed = directed;
|
|
489
|
-
const names = new Set(vertices.map(String));
|
|
490
|
-
this.#defaultVertexConfig = Object.fromEntries(Object.entries(o.vertexConfig ?? {}).filter(([k]) => !names.has(k)));
|
|
491
|
-
this.#defaultEdgeConfig = Object.fromEntries(Object.entries(o.edgeConfig ?? {}).filter(([k]) => !k.includes(",")));
|
|
492
|
-
if (o.edgeType !== undefined)
|
|
493
|
-
this.#edgeType = o.edgeType;
|
|
494
|
-
this.addVertices(...vertices, {
|
|
495
|
-
...(o.labels === undefined ? {} : { labels: o.labels }),
|
|
496
|
-
labelFillColor: o.labelFillColor ?? BLACK,
|
|
497
|
-
...(o.vertexType === undefined ? {} : { vertexType: o.vertexType }),
|
|
498
|
-
...(o.vertexConfig === undefined ? {} : { vertexConfig: o.vertexConfig }),
|
|
499
|
-
...(o.vertexMobjects === undefined ? {} : { vertexMobjects: o.vertexMobjects }),
|
|
500
|
-
});
|
|
501
|
-
const pos = layoutOf(vertices, edges, o);
|
|
502
|
-
for (const [v, mob] of this.vertices)
|
|
503
|
-
mob.moveTo(pos.get(v) ?? pos.get(String(v)) ?? ORIGIN);
|
|
504
|
-
this.addEdges(...edges, o.edgeConfig === undefined ? {} : { edgeConfig: o.edgeConfig });
|
|
505
|
-
this.addUpdater((g) => g.updateEdges());
|
|
506
|
-
return this;
|
|
507
|
-
}
|
|
508
|
-
}
|
|
509
|
-
function vertexConfigOf(config, v) {
|
|
510
|
-
const own = config?.[String(v)];
|
|
511
|
-
return typeof own === "object" && own !== null ? own : {};
|
|
512
|
-
}
|
|
513
|
-
function edgeConfigOf(config, key) {
|
|
514
|
-
const own = config?.[key];
|
|
515
|
-
return typeof own === "object" && own !== null ? own : {};
|
|
516
|
-
}
|
|
517
|
-
/** Manim's `Graph(vertices, edges, opts)`: dots joined by lines, in the `"spring"` layout by
|
|
518
|
-
* default. */
|
|
519
|
-
export function Graph(vertices, edges, opts = {}) {
|
|
520
|
-
return new GraphMobject(engine.vGroup([])).init(vertices, edges, opts, false);
|
|
521
|
-
}
|
|
522
|
-
/** Manim's `DiGraph(vertices, edges, opts)`: a Graph with a tip on each edge, from the border of
|
|
523
|
-
* one vertex to the border of the other. */
|
|
524
|
-
export function DiGraph(vertices, edges, opts = {}) {
|
|
525
|
-
return new GraphMobject(engine.vGroup([])).init(vertices, edges, opts, true);
|
|
526
|
-
}
|