@algorisys/tinyfly 0.117.0 → 0.119.1
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/README.md +6 -6
- package/docs/editor-guide.md +1 -1
- package/docs/examples.md +1 -1
- package/docs/getting-started.md +1 -1
- package/docs/gsap-compat.md +1 -1
- package/docs/llm-guide.md +5 -1
- package/docs/maps.md +2 -2
- package/docs/math.md +74 -9
- package/docs/teaching.md +1 -1
- package/lib/addons/characters.js +2311 -2299
- package/lib/addons/math.js +2120 -1166
- package/lib/browser/tinyfly-math.iife.js +4 -4
- package/lib/browser/tinyfly.iife.js +1 -1
- package/lib/browser/tinyfly.js +13 -1
- package/lib/browser/tinyfly.umd.js +1 -1
- package/lib/types/math/discrete/graph-search.d.ts +38 -0
- package/lib/types/math/discrete/set-expression.d.ts +33 -0
- package/lib/types/math/figure/display-list.d.ts +10 -0
- package/lib/types/math/figure/figure-spec.d.ts +168 -1
- package/lib/types/math/figure/items/discrete.d.ts +23 -0
- package/lib/types/math/figure/items/ml.d.ts +47 -0
- package/lib/types/math/figure/items/shapes.d.ts +3 -0
- package/lib/types/math/index.d.ts +11 -1
- package/lib/types/math/ml/classify.d.ts +30 -0
- package/lib/types/math/ml/colormap.d.ts +14 -0
- package/lib/types/math/ml/contours.d.ts +15 -0
- package/lib/types/math/ml/descent.d.ts +11 -0
- package/lib/types/math/ml/kmeans.d.ts +11 -0
- package/lib/types/math/ml/network.d.ts +19 -0
- package/lib/types/math/presets/discrete.d.ts +49 -0
- package/lib/types/math/presets/ml.d.ts +50 -0
- package/lib/types/math/tex/glyph-metrics.d.ts +8 -0
- package/lib/types/math/tex/tex-parser.d.ts +2 -1
- package/llms-full.txt +99 -18
- package/llms.txt +31 -31
- package/package.json +1 -1
- package/skills/tinyfly/SKILL.md +3 -1
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Graph searches as lists of events, so a figure can show any moment of
|
|
3
|
+
* one: which nodes are done, what waits in the queue or stack, and which
|
|
4
|
+
* edges the search used to reach each node. Pure and deterministic:
|
|
5
|
+
* neighbours are taken in the order the nodes are listed, so the same
|
|
6
|
+
* graph always gives the same story.
|
|
7
|
+
*/
|
|
8
|
+
export interface GraphEdge {
|
|
9
|
+
from: string;
|
|
10
|
+
to: string;
|
|
11
|
+
/** For shortest paths (default 1) */
|
|
12
|
+
weight?: number;
|
|
13
|
+
}
|
|
14
|
+
export type SearchKind = 'bfs' | 'dfs' | 'dijkstra';
|
|
15
|
+
export declare const SEARCH_KINDS: readonly SearchKind[];
|
|
16
|
+
/** One step: a node is reached (and finished) through an edge from `parent`. */
|
|
17
|
+
export interface SearchEvent {
|
|
18
|
+
node: string;
|
|
19
|
+
/** The node it was reached from (none for the start) */
|
|
20
|
+
parent?: string;
|
|
21
|
+
/** What waits to be visited after this step: the queue (bfs), the stack (dfs) or the open set (dijkstra) */
|
|
22
|
+
frontier: string[];
|
|
23
|
+
/** Shortest distances known after this step (dijkstra only; Infinity when unknown) */
|
|
24
|
+
distances?: Record<string, number>;
|
|
25
|
+
}
|
|
26
|
+
/** Neighbours of each node, in the order the nodes are listed. */
|
|
27
|
+
export declare function adjacency(nodes: string[], edges: GraphEdge[], directed: boolean): Map<string, {
|
|
28
|
+
to: string;
|
|
29
|
+
weight: number;
|
|
30
|
+
}[]>;
|
|
31
|
+
/** Every step of a search from `start`; nodes it cannot reach never appear. */
|
|
32
|
+
export declare function searchEvents(kind: SearchKind, start: string, nodes: string[], edges: GraphEdge[], directed?: boolean): SearchEvent[];
|
|
33
|
+
/**
|
|
34
|
+
* Positions for a tree drawn top down from `root`: each level one unit lower,
|
|
35
|
+
* leaves one unit apart, each parent centred over its children. Nodes the
|
|
36
|
+
* root cannot reach are left out.
|
|
37
|
+
*/
|
|
38
|
+
export declare function treeLayout(root: string, nodes: string[], edges: GraphEdge[]): Map<string, [number, number]>;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Set expressions over named sets, for shading Venn diagrams:
|
|
3
|
+
*
|
|
4
|
+
* A | B union A & B intersection
|
|
5
|
+
* A - B difference A ^ B symmetric difference
|
|
6
|
+
* A' or !A complement (within the universe)
|
|
7
|
+
*
|
|
8
|
+
* with brackets and the usual precedence (complement, then &, then | - ^,
|
|
9
|
+
* left to right). `∪ ∩ ∖ △ ¬` are accepted too. An expression is
|
|
10
|
+
* evaluated per Venn cell: given which sets a point is in, is it in the region?
|
|
11
|
+
*/
|
|
12
|
+
export type SetExpression = {
|
|
13
|
+
type: 'set';
|
|
14
|
+
name: string;
|
|
15
|
+
} | {
|
|
16
|
+
type: 'not';
|
|
17
|
+
operand: SetExpression;
|
|
18
|
+
} | {
|
|
19
|
+
type: 'op';
|
|
20
|
+
operator: '&' | '|' | '-' | '^';
|
|
21
|
+
left: SetExpression;
|
|
22
|
+
right: SetExpression;
|
|
23
|
+
};
|
|
24
|
+
export declare function parseSetExpression(source: string): SetExpression;
|
|
25
|
+
/** Every set name an expression uses. */
|
|
26
|
+
export declare function setNames(e: SetExpression): string[];
|
|
27
|
+
/** Is a point that lies in exactly the sets in `inside` within the region? */
|
|
28
|
+
export declare function inRegion(e: SetExpression, inside: ReadonlySet<string>): boolean;
|
|
29
|
+
/**
|
|
30
|
+
* The Venn cells (which sets a point is in) that make up the region, as
|
|
31
|
+
* lists of set names: `[]` is the cell outside every set.
|
|
32
|
+
*/
|
|
33
|
+
export declare function regionCells(e: SetExpression, sets: string[]): string[][];
|
|
@@ -16,6 +16,14 @@ export type Shape = {
|
|
|
16
16
|
fill?: string;
|
|
17
17
|
opacity?: number;
|
|
18
18
|
dash?: number[];
|
|
19
|
+
/**
|
|
20
|
+
* Draw only inside every one of these polygons (or, with `outside`,
|
|
21
|
+
* only outside it): how a Venn diagram shades A ∩ B or A − B.
|
|
22
|
+
*/
|
|
23
|
+
clip?: {
|
|
24
|
+
points: Vec2[];
|
|
25
|
+
outside?: boolean;
|
|
26
|
+
}[];
|
|
19
27
|
} | {
|
|
20
28
|
type: 'arrow';
|
|
21
29
|
id: string;
|
|
@@ -76,6 +84,8 @@ export interface TexPlacement {
|
|
|
76
84
|
opacity?: number;
|
|
77
85
|
/** Prefixed to every piece's id, so several equations in one figure stay apart */
|
|
78
86
|
idPrefix: string;
|
|
87
|
+
/** Colour names TeX's \\color{…} may use (a theme's palette), mapped to CSS colours */
|
|
88
|
+
palette?: Record<string, string>;
|
|
79
89
|
}
|
|
80
90
|
/** The origin (left edge, baseline) of a layout placed by `place`, in px. */
|
|
81
91
|
export declare function texOrigin(layout: TexLayout, place: Pick<TexPlacement, 'x' | 'y' | 'size' | 'align' | 'baseline'>): Vec2;
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import type { Vec2 } from '../linear/vec2';
|
|
2
|
+
import type { SearchKind } from '../discrete/graph-search';
|
|
3
|
+
import type { Activation } from '../ml/network';
|
|
4
|
+
import type { Colormap } from '../ml/colormap';
|
|
2
5
|
/**
|
|
3
6
|
* A maths figure as plain JSON: a coordinate plane and the items drawn on it.
|
|
4
7
|
*
|
|
@@ -328,7 +331,171 @@ export interface BayesItem extends ItemBase {
|
|
|
328
331
|
/** Label the four regions (default true) */
|
|
329
332
|
labels?: boolean;
|
|
330
333
|
}
|
|
331
|
-
|
|
334
|
+
/** A node of a graph: where it is (world units; optional when the graph is a tree) and its TeX label (default its id). */
|
|
335
|
+
export interface GraphNode {
|
|
336
|
+
id: string;
|
|
337
|
+
at?: Vec2;
|
|
338
|
+
label?: string;
|
|
339
|
+
}
|
|
340
|
+
/** An edge: `{ from, to, weight? }` or the shorthand `[from, to]` / `[from, to, weight]`. */
|
|
341
|
+
export type GraphEdgeSpec = {
|
|
342
|
+
from: string;
|
|
343
|
+
to: string;
|
|
344
|
+
weight?: number;
|
|
345
|
+
} | [string, string] | [string, string, number];
|
|
346
|
+
/** Nodes and edges, optionally searched step by step (breadth first, depth first, or shortest paths). */
|
|
347
|
+
export interface GraphItem extends ItemBase {
|
|
348
|
+
kind: 'graph';
|
|
349
|
+
nodes: GraphNode[];
|
|
350
|
+
edges: GraphEdgeSpec[];
|
|
351
|
+
/** Arrows instead of lines; searches follow edge direction (default false) */
|
|
352
|
+
directed?: boolean;
|
|
353
|
+
/** Lay the nodes out as a tree from `root`, top down (`spacing` [across, down] world units, `at` the root) */
|
|
354
|
+
tree?: {
|
|
355
|
+
root: string;
|
|
356
|
+
spacing?: [number, number];
|
|
357
|
+
at?: Vec2;
|
|
358
|
+
};
|
|
359
|
+
/** The search to show */
|
|
360
|
+
search?: {
|
|
361
|
+
kind: SearchKind;
|
|
362
|
+
start: string;
|
|
363
|
+
};
|
|
364
|
+
/** How many search steps are done; the fractional part is the next node being reached (default all) */
|
|
365
|
+
step?: NumRef;
|
|
366
|
+
/** Show the queue / stack / open set as a row of labels, at this screen position (px) */
|
|
367
|
+
frontier?: Vec2;
|
|
368
|
+
/** Show edge weights (default: when a shortest-path search is shown) */
|
|
369
|
+
weights?: boolean;
|
|
370
|
+
/** Node radius, px at a 600 px figure (default 18) */
|
|
371
|
+
radius?: number;
|
|
372
|
+
}
|
|
373
|
+
/** Two or three sets as circles, with a set expression shaded: `A & B`, `(A | B)'`, `A ^ B`. */
|
|
374
|
+
export interface VennItem extends ItemBase {
|
|
375
|
+
kind: 'venn';
|
|
376
|
+
sets: {
|
|
377
|
+
id: string;
|
|
378
|
+
label?: string;
|
|
379
|
+
color?: string;
|
|
380
|
+
}[];
|
|
381
|
+
/** The region to shade, a set expression over the set ids */
|
|
382
|
+
highlight?: string;
|
|
383
|
+
highlightColor?: string;
|
|
384
|
+
/** Centre, world units (default the origin) */
|
|
385
|
+
at?: Vec2;
|
|
386
|
+
/** Circle radius, world units (default 1.5) */
|
|
387
|
+
radius?: number;
|
|
388
|
+
/** Draw the universe as a box around them (default true) */
|
|
389
|
+
universe?: boolean;
|
|
390
|
+
}
|
|
391
|
+
/** A clock of `modulus` hours: clock arithmetic. The hand points at `value` mod `modulus`, gliding between hours. */
|
|
392
|
+
export interface ClockItem extends ItemBase {
|
|
393
|
+
kind: 'clock';
|
|
394
|
+
modulus: number;
|
|
395
|
+
value: NumRef;
|
|
396
|
+
at?: Vec2;
|
|
397
|
+
/** World units (default 2) */
|
|
398
|
+
radius?: number;
|
|
399
|
+
}
|
|
400
|
+
/** A fully connected neural network; activations flow layer by layer as `flow` grows. */
|
|
401
|
+
export interface NetworkItem extends ItemBase {
|
|
402
|
+
kind: 'network';
|
|
403
|
+
/** Nodes per layer, input first: [3, 4, 4, 2] */
|
|
404
|
+
layers: number[];
|
|
405
|
+
/** Seed for the weights (default the item id) */
|
|
406
|
+
seed?: number | string;
|
|
407
|
+
activation?: Activation;
|
|
408
|
+
/** Input values (numbers or value names; default 0.5 each) */
|
|
409
|
+
inputs?: NumRef[];
|
|
410
|
+
/** How far activations have flowed: 0 the inputs only, 1 the first hidden layer … (default all) */
|
|
411
|
+
flow?: NumRef;
|
|
412
|
+
/** Centre and size, world units (default most of the view) */
|
|
413
|
+
at?: Vec2;
|
|
414
|
+
size?: Vec2;
|
|
415
|
+
/** TeX labels beside the input and output nodes */
|
|
416
|
+
labels?: {
|
|
417
|
+
inputs?: string[];
|
|
418
|
+
outputs?: string[];
|
|
419
|
+
};
|
|
420
|
+
/** Node radius, px at a 600 px figure (default 16) */
|
|
421
|
+
radius?: number;
|
|
422
|
+
}
|
|
423
|
+
/** A grid of coloured cells: given values (attention weights) or a function of x and y. */
|
|
424
|
+
export interface HeatmapItem extends ItemBase {
|
|
425
|
+
kind: 'heatmap';
|
|
426
|
+
/** Rows of values, top row first */
|
|
427
|
+
cells?: number[][];
|
|
428
|
+
/** Or a function of x and y, sampled over `region` */
|
|
429
|
+
expr?: string;
|
|
430
|
+
region?: [[number, number], [number, number]];
|
|
431
|
+
/** Samples per side for `expr` (default 48) */
|
|
432
|
+
resolution?: number;
|
|
433
|
+
/** Normalise each row with softmax first, as attention does */
|
|
434
|
+
softmax?: boolean;
|
|
435
|
+
/** World bottom-left corner and size, for `cells` (default most of the view) */
|
|
436
|
+
at?: Vec2;
|
|
437
|
+
size?: Vec2;
|
|
438
|
+
/** Value range for the colours (default the cells' min and max) */
|
|
439
|
+
range?: [number, number];
|
|
440
|
+
colormap?: Colormap;
|
|
441
|
+
rowLabels?: string[];
|
|
442
|
+
colLabels?: string[];
|
|
443
|
+
/** Write each value in its cell (default false) */
|
|
444
|
+
values?: boolean;
|
|
445
|
+
}
|
|
446
|
+
/** Level curves of f(x, y). */
|
|
447
|
+
export interface ContoursItem extends ItemBase {
|
|
448
|
+
kind: 'contours';
|
|
449
|
+
expr: string;
|
|
450
|
+
/** How many levels between the sampled min and max (default 10), or the levels themselves */
|
|
451
|
+
levels?: number | number[];
|
|
452
|
+
region?: [[number, number], [number, number]];
|
|
453
|
+
resolution?: number;
|
|
454
|
+
colormap?: Colormap;
|
|
455
|
+
width?: number;
|
|
456
|
+
}
|
|
457
|
+
/** Gradient descent on f(x, y) from `start`: `steps` taken so far (fractional: the next one under way). */
|
|
458
|
+
export interface DescentItem extends ItemBase {
|
|
459
|
+
kind: 'descent';
|
|
460
|
+
expr: string;
|
|
461
|
+
start: VecRef;
|
|
462
|
+
rate: NumRef;
|
|
463
|
+
steps: NumRef;
|
|
464
|
+
momentum?: NumRef;
|
|
465
|
+
width?: number;
|
|
466
|
+
}
|
|
467
|
+
/** Logistic regression on two seeded clusters, trained epoch by epoch: the decision boundary moves into place. */
|
|
468
|
+
export interface ClassifierItem extends ItemBase {
|
|
469
|
+
kind: 'classifier';
|
|
470
|
+
/** Cluster centres (default [[-1.5, -1], [1.5, 1]]) */
|
|
471
|
+
centres?: [Vec2, Vec2];
|
|
472
|
+
/** Points per cluster (default 40) */
|
|
473
|
+
count?: number;
|
|
474
|
+
spread?: number;
|
|
475
|
+
seed?: number | string;
|
|
476
|
+
/** Epochs trained (fractional: part way to the next) */
|
|
477
|
+
epochs: NumRef;
|
|
478
|
+
/** Learning rate (default 0.5) */
|
|
479
|
+
rate?: number;
|
|
480
|
+
/** The model before training, [w₁, w₂, b] (default a deliberately wrong guess, so the boundary visibly turns into place) */
|
|
481
|
+
start?: [number, number, number];
|
|
482
|
+
/** Shade P(class) behind the points (default true) */
|
|
483
|
+
probability?: boolean;
|
|
484
|
+
}
|
|
485
|
+
/** k-means on seeded clusters: points coloured by their nearest centroid, centroids moving each iteration. */
|
|
486
|
+
export interface KMeansItem extends ItemBase {
|
|
487
|
+
kind: 'kmeans';
|
|
488
|
+
/** Where the true clusters are */
|
|
489
|
+
centres: Vec2[];
|
|
490
|
+
count?: number;
|
|
491
|
+
spread?: number;
|
|
492
|
+
seed?: number | string;
|
|
493
|
+
/** How many centroids (default one per centre) */
|
|
494
|
+
k?: number;
|
|
495
|
+
/** Iterations done (fractional: centroids part way to the next) */
|
|
496
|
+
iteration: NumRef;
|
|
497
|
+
}
|
|
498
|
+
export type MathItem = GridItem | AxesItem | VectorItem | BasisItem | PointItem | FunctionItem | ParametricItem | PolygonItem | LineItem | LabelItem | EquationItem | TangentItem | RiemannItem | AreaItem | TaylorItem | HistogramItem | DistributionItem | GaltonItem | ScatterItem | BayesItem | GraphItem | VennItem | ClockItem | NetworkItem | HeatmapItem | ContoursItem | DescentItem | ClassifierItem | KMeansItem;
|
|
332
499
|
export type MathItemKind = MathItem['kind'];
|
|
333
500
|
export declare const ITEM_KINDS: readonly MathItemKind[];
|
|
334
501
|
export type MathThemeName = 'dark' | 'light' | 'chalk';
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type GraphEdge, type SearchEvent } from '../../discrete/graph-search';
|
|
2
|
+
import { type Vec2 } from '../../linear/vec2';
|
|
3
|
+
import type { ClockItem, GraphItem, VennItem } from '../figure-spec';
|
|
4
|
+
import { type Shape } from '../display-list';
|
|
5
|
+
import { type ItemContext, type Values } from '../item-context';
|
|
6
|
+
/** A graph's edges in object form. */
|
|
7
|
+
export declare function graphEdges(item: GraphItem): GraphEdge[];
|
|
8
|
+
/** Where each node is, in world units: its own `at`, else its place in the tree layout. */
|
|
9
|
+
export declare function graphPositions(item: GraphItem): Map<string, Vec2>;
|
|
10
|
+
export interface SearchState {
|
|
11
|
+
events: SearchEvent[];
|
|
12
|
+
/** Events fully done */
|
|
13
|
+
done: number;
|
|
14
|
+
/** How far the next event is, 0..1 */
|
|
15
|
+
partial: number;
|
|
16
|
+
}
|
|
17
|
+
/** The search shown at the current `step` (all of it when no step is given). */
|
|
18
|
+
export declare function searchState(item: GraphItem, values: Values): SearchState | undefined;
|
|
19
|
+
export declare function layoutGraph(item: GraphItem, ctx: ItemContext): Shape[];
|
|
20
|
+
export declare function layoutVenn(item: VennItem, ctx: ItemContext): Shape[];
|
|
21
|
+
/** The hour a clock value shows: value mod modulus, in 0..modulus − 1. */
|
|
22
|
+
export declare const clockHour: (value: number, modulus: number) => number;
|
|
23
|
+
export declare function layoutClock(item: ClockItem, ctx: ItemContext): Shape[];
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { type LogisticModel } from '../../ml/classify';
|
|
2
|
+
import { type Vec2 } from '../../linear/vec2';
|
|
3
|
+
import type { ClassifierItem, ContoursItem, DescentItem, HeatmapItem, KMeansItem, NetworkItem } from '../figure-spec';
|
|
4
|
+
import { type Shape } from '../display-list';
|
|
5
|
+
import { type ItemContext, type Values } from '../item-context';
|
|
6
|
+
/** Every layer's activations, and how far they have flowed. */
|
|
7
|
+
export declare function networkState(item: NetworkItem, values: Values): {
|
|
8
|
+
network: import("../..").Network;
|
|
9
|
+
activations: number[][];
|
|
10
|
+
flow: number;
|
|
11
|
+
};
|
|
12
|
+
export declare function layoutNetwork(item: NetworkItem, ctx: ItemContext): Shape[];
|
|
13
|
+
/** The cells a heatmap shows: given (optionally softmaxed by row) or sampled from its expression. */
|
|
14
|
+
export declare function heatmapCells(item: HeatmapItem, ctx: Pick<ItemContext, 'plane' | 'scope'>): {
|
|
15
|
+
cells: number[][];
|
|
16
|
+
box: [Vec2, Vec2];
|
|
17
|
+
};
|
|
18
|
+
export declare function layoutHeatmap(item: HeatmapItem, ctx: ItemContext): Shape[];
|
|
19
|
+
export declare function layoutContours(item: ContoursItem, ctx: ItemContext): Shape[];
|
|
20
|
+
/** The descent path so far: whole steps plus the fraction of the next. */
|
|
21
|
+
export declare function descentState(item: DescentItem, values: Values, scope: Record<string, number>): {
|
|
22
|
+
path: Vec2[];
|
|
23
|
+
here: Vec2;
|
|
24
|
+
step: number;
|
|
25
|
+
diverged: boolean;
|
|
26
|
+
};
|
|
27
|
+
export declare function layoutDescent(item: DescentItem, ctx: ItemContext): Shape[];
|
|
28
|
+
export declare function classifierState(item: ClassifierItem, values: Values): {
|
|
29
|
+
data: {
|
|
30
|
+
point: Vec2;
|
|
31
|
+
label: number;
|
|
32
|
+
}[];
|
|
33
|
+
model: LogisticModel;
|
|
34
|
+
epochs: number;
|
|
35
|
+
};
|
|
36
|
+
export declare function layoutClassifier(item: ClassifierItem, ctx: ItemContext): Shape[];
|
|
37
|
+
export declare function kmeansState(item: KMeansItem, values: Values): {
|
|
38
|
+
points: Vec2[];
|
|
39
|
+
history: Vec2[][];
|
|
40
|
+
centroids: Vec2[];
|
|
41
|
+
owners: number[];
|
|
42
|
+
iteration: number;
|
|
43
|
+
iterations: number;
|
|
44
|
+
};
|
|
45
|
+
export declare function layoutKMeans(item: KMeansItem, ctx: ItemContext): Shape[];
|
|
46
|
+
/** Derived numbers for the ML items, for TeX readouts. */
|
|
47
|
+
export declare function mlDerived(item: NetworkItem | DescentItem | ClassifierItem | KMeansItem, values: Values, scope: Record<string, number>): Record<string, number>;
|
|
@@ -21,10 +21,18 @@ export { binSamples, defaultBins, summary, type Bin } from './prob/histogram';
|
|
|
21
21
|
export { scatterPoints, leastSquares, type PointsSpec } from './prob/scatter';
|
|
22
22
|
export { ballPath, ballAt, binOf } from './prob/galton';
|
|
23
23
|
export { bayesAreas, type BayesAreas } from './prob/bayes';
|
|
24
|
+
export { searchEvents, treeLayout, adjacency, SEARCH_KINDS, type SearchKind, type SearchEvent, type GraphEdge } from './discrete/graph-search';
|
|
25
|
+
export { parseSetExpression, inRegion, regionCells, setNames, type SetExpression } from './discrete/set-expression';
|
|
26
|
+
export { seededNetwork, forward, activate, ACTIVATIONS, type Network, type Activation } from './ml/network';
|
|
27
|
+
export { contourLines, sampleGrid } from './ml/contours';
|
|
28
|
+
export { descentPath, surface } from './ml/descent';
|
|
29
|
+
export { blobs, trainLogistic, predict, accuracy, logLoss, type LogisticModel } from './ml/classify';
|
|
30
|
+
export { kmeansHistory, assign, inertia } from './ml/kmeans';
|
|
31
|
+
export { colorAt, blendOver, COLORMAPS, type Colormap } from './ml/colormap';
|
|
24
32
|
export { parseTex, TexError, TEX_COMMANDS, SYMBOLS, type TexNode, type TexClass } from './tex/tex-parser';
|
|
25
33
|
export { layoutTex, type TexLayout, type TexPiece, type TexGlyph, type TexRule, type TexStroke, type TexLayoutOptions } from './tex/tex-layout';
|
|
26
34
|
export { morphEquation, type MorphPiece, type PlacedLayout } from './tex/equation-morph';
|
|
27
|
-
export { MATH_FONT } from './tex/glyph-metrics';
|
|
35
|
+
export { MATH_FONT, MATH_SYMBOL_FONT } from './tex/glyph-metrics';
|
|
28
36
|
export * from './figure/figure-spec';
|
|
29
37
|
export { validateMathFigure } from './figure/validate-spec';
|
|
30
38
|
export { layoutFigure, type MathProps } from './figure/layout-figure';
|
|
@@ -39,6 +47,8 @@ export { mathExplorable, type MathExplorable, type ExplorableOptions, type Slide
|
|
|
39
47
|
export { linearMapFigure, determinantFigure, eigenFigure, vectorSumFigure, spanFigure, MATRIX_TEX, type PresetOptions, } from './presets/linear-algebra';
|
|
40
48
|
export { tangentFigure, secantFigure, riemannFigure, areaFigure, taylorFigure, limitFigure, type CalculusOptions } from './presets/calculus';
|
|
41
49
|
export { normalFigure, binomialFigure, cltFigure, galtonFigure, monteCarloPiFigure, regressionFigure, bayesFigure, type ProbabilityOptions } from './presets/probability';
|
|
50
|
+
export { graphSearchFigure, shortestPathFigure, recursionTreeFigure, vennFigure, clockFigure, SAMPLE_GRAPH, type DiscreteOptions } from './presets/discrete';
|
|
51
|
+
export { neuralNetworkFigure, gradientDescentFigure, attentionFigure, decisionBoundaryFigure, kmeansFigure, activationFigure, type MlOptions } from './presets/ml';
|
|
42
52
|
import type { MathFigureSpec } from './figure/figure-spec';
|
|
43
53
|
import { type MathProps } from './figure/layout-figure';
|
|
44
54
|
/** A figure checked up front: returns it unchanged, or throws a message that says how to fix it. */
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { Vec2 } from '../linear/vec2';
|
|
2
|
+
/** Points in clusters round `centres`, `count` per cluster, `spread` apart: [point, cluster] pairs. Seeded. */
|
|
3
|
+
export declare function blobs(centres: Vec2[], count: number, spread: number, seed: number | string): {
|
|
4
|
+
point: Vec2;
|
|
5
|
+
label: number;
|
|
6
|
+
}[];
|
|
7
|
+
export interface LogisticModel {
|
|
8
|
+
w: Vec2;
|
|
9
|
+
b: number;
|
|
10
|
+
}
|
|
11
|
+
/** P(class 1) for a point. */
|
|
12
|
+
export declare const predict: (model: LogisticModel, [x, y]: Vec2) => number;
|
|
13
|
+
/**
|
|
14
|
+
* Logistic regression trained by full-batch gradient descent from `start`
|
|
15
|
+
* (default zero weights): the model after each epoch, the start first.
|
|
16
|
+
*/
|
|
17
|
+
export declare function trainLogistic(data: {
|
|
18
|
+
point: Vec2;
|
|
19
|
+
label: number;
|
|
20
|
+
}[], epochs: number, rate: number, start?: LogisticModel): LogisticModel[];
|
|
21
|
+
/** Share of points the model puts on the right side of 0.5. */
|
|
22
|
+
export declare function accuracy(model: LogisticModel, data: {
|
|
23
|
+
point: Vec2;
|
|
24
|
+
label: number;
|
|
25
|
+
}[]): number;
|
|
26
|
+
/** Mean cross-entropy loss. */
|
|
27
|
+
export declare function logLoss(model: LogisticModel, data: {
|
|
28
|
+
point: Vec2;
|
|
29
|
+
label: number;
|
|
30
|
+
}[]): number;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Colour scales for heatmaps, as plain functions of 0..1 (clamped):
|
|
3
|
+
* `viridis` (dark blue to yellow, readable in grey), `heat` (dark to orange
|
|
4
|
+
* to pale yellow), `diverging` (blue, white, red; for signed values).
|
|
5
|
+
*/
|
|
6
|
+
export type Colormap = 'viridis' | 'heat' | 'diverging';
|
|
7
|
+
export declare const COLORMAPS: readonly Colormap[];
|
|
8
|
+
export declare function colorAt(map: Colormap, t: number): string;
|
|
9
|
+
/**
|
|
10
|
+
* `color` laid over `background` at `amount` (0..1), as one opaque colour.
|
|
11
|
+
* Neighbouring cells drawn this way can overlap without the darker seams
|
|
12
|
+
* that overlapping translucent fills leave.
|
|
13
|
+
*/
|
|
14
|
+
export declare function blendOver(color: string, background: string, amount: number): string;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Vec2 } from '../linear/vec2';
|
|
2
|
+
/**
|
|
3
|
+
* Level curves of a sampled function by marching squares: for each level,
|
|
4
|
+
* the line segments where the grid crosses it, joined into polylines.
|
|
5
|
+
* `values[j][i]` is f at x = x0 + i·dx, y = y0 + j·dy. Pure.
|
|
6
|
+
*/
|
|
7
|
+
export declare function contourLines(values: number[][], x0: number, y0: number, dx: number, dy: number, level: number): Vec2[][];
|
|
8
|
+
/** f sampled on a grid over [x0, x1] × [y0, y1] with `n` + 1 points each way. */
|
|
9
|
+
export declare function sampleGrid(f: (x: number, y: number) => number, [x0, x1]: [number, number], [y0, y1]: [number, number], n: number): {
|
|
10
|
+
values: number[][];
|
|
11
|
+
x0: number;
|
|
12
|
+
y0: number;
|
|
13
|
+
dx: number;
|
|
14
|
+
dy: number;
|
|
15
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type Scope } from '../expr/evaluate';
|
|
2
|
+
import type { Vec2 } from '../linear/vec2';
|
|
3
|
+
/**
|
|
4
|
+
* Gradient descent on f(x, y), with the gradient worked out symbolically:
|
|
5
|
+
* each step moves against the gradient by `rate`, plus `momentum` times the
|
|
6
|
+
* last step. Returns every point visited, the start first. Stops early if a
|
|
7
|
+
* step leaves the real numbers (a rate too large can diverge).
|
|
8
|
+
*/
|
|
9
|
+
export declare function descentPath(expr: string, start: Vec2, rate: number, steps: number, momentum?: number, scope?: Scope): Vec2[];
|
|
10
|
+
/** A parsed f(x, y) to evaluate many times. */
|
|
11
|
+
export declare function surface(expr: string): (x: number, y: number, scope?: Scope) => number;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Vec2 } from '../linear/vec2';
|
|
2
|
+
/** The centroid each point is nearest to (ties go to the lower index). */
|
|
3
|
+
export declare function assign(points: Vec2[], centroids: Vec2[]): number[];
|
|
4
|
+
/**
|
|
5
|
+
* Lloyd's algorithm from `start` (default the first `k` points), so it is
|
|
6
|
+
* deterministic: the centroids after each iteration, the start first,
|
|
7
|
+
* until they stop moving or `maxIterations` is reached.
|
|
8
|
+
*/
|
|
9
|
+
export declare function kmeansHistory(points: Vec2[], k: number, maxIterations?: number, start?: Vec2[]): Vec2[][];
|
|
10
|
+
/** Sum of squared distances from each point to its centroid. */
|
|
11
|
+
export declare function inertia(points: Vec2[], centroids: Vec2[]): number;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A small fully connected network, for drawing: layer sizes, seeded weights
|
|
3
|
+
* and a forward pass. Pure: the same layers, seed and inputs always give the
|
|
4
|
+
* same weights and activations.
|
|
5
|
+
*/
|
|
6
|
+
export type Activation = 'sigmoid' | 'tanh' | 'relu';
|
|
7
|
+
export declare const ACTIVATIONS: readonly Activation[];
|
|
8
|
+
export declare const activate: (kind: Activation, x: number) => number;
|
|
9
|
+
export interface Network {
|
|
10
|
+
layers: number[];
|
|
11
|
+
/** weights[l][j][i]: from node i of layer l to node j of layer l + 1 */
|
|
12
|
+
weights: number[][][];
|
|
13
|
+
/** biases[l][j]: of node j of layer l + 1 */
|
|
14
|
+
biases: number[][];
|
|
15
|
+
}
|
|
16
|
+
/** A network with weights drawn from a seed (normal, scaled by 1/√fan-in). */
|
|
17
|
+
export declare function seededNetwork(layers: number[], seed: number | string): Network;
|
|
18
|
+
/** Every layer's activations for `inputs` (layer 0 is the inputs themselves). */
|
|
19
|
+
export declare function forward(network: Network, inputs: number[], activation: Activation): number[][];
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { GraphEdgeSpec, GraphNode, MathFigureSpec, MathThemeName } from '../figure/figure-spec';
|
|
2
|
+
/**
|
|
3
|
+
* Ready-made discrete maths figures. Each returns a plain MathFigureSpec
|
|
4
|
+
* whose moving part is a number value:
|
|
5
|
+
*
|
|
6
|
+
* - graphSearchFigure / shortestPathFigure / recursionTreeFigure: `step`,
|
|
7
|
+
* how many search steps are done (fractional: the next node being reached)
|
|
8
|
+
* - clockFigure: `value`, the number the hand points at (mod the modulus)
|
|
9
|
+
* - vennFigure: no values; animate the item's draw, or show one figure per region
|
|
10
|
+
*/
|
|
11
|
+
export interface DiscreteOptions {
|
|
12
|
+
width?: number;
|
|
13
|
+
height?: number;
|
|
14
|
+
theme?: MathThemeName;
|
|
15
|
+
}
|
|
16
|
+
/** A small graph of eight nodes (A–H), for searches. */
|
|
17
|
+
export declare const SAMPLE_GRAPH: {
|
|
18
|
+
nodes: GraphNode[];
|
|
19
|
+
edges: [string, string, number][];
|
|
20
|
+
};
|
|
21
|
+
/** Breadth-first or depth-first search on a graph, step by step, with its queue or stack. */
|
|
22
|
+
export declare function graphSearchFigure(options?: DiscreteOptions & {
|
|
23
|
+
search?: 'bfs' | 'dfs';
|
|
24
|
+
start?: string;
|
|
25
|
+
nodes?: GraphNode[];
|
|
26
|
+
edges?: GraphEdgeSpec[];
|
|
27
|
+
}): MathFigureSpec;
|
|
28
|
+
/** Dijkstra's shortest paths on a weighted graph: each node settles at its distance from the start. */
|
|
29
|
+
export declare function shortestPathFigure(options?: DiscreteOptions & {
|
|
30
|
+
start?: string;
|
|
31
|
+
target?: string;
|
|
32
|
+
nodes?: GraphNode[];
|
|
33
|
+
edges?: GraphEdgeSpec[];
|
|
34
|
+
}): MathFigureSpec;
|
|
35
|
+
/** The call tree of a naive recursive Fibonacci, F(n) = F(n − 1) + F(n − 2), visited in call order. */
|
|
36
|
+
export declare function recursionTreeFigure(options?: DiscreteOptions & {
|
|
37
|
+
n?: number;
|
|
38
|
+
}): MathFigureSpec;
|
|
39
|
+
/** Two or three sets as a Venn diagram with a set expression shaded. */
|
|
40
|
+
export declare function vennFigure(options?: DiscreteOptions & {
|
|
41
|
+
sets?: string[];
|
|
42
|
+
highlight?: string;
|
|
43
|
+
label?: string;
|
|
44
|
+
}): MathFigureSpec;
|
|
45
|
+
/** A clock of `modulus` hours with the hand at `value` (mod modulus). */
|
|
46
|
+
export declare function clockFigure(options?: DiscreteOptions & {
|
|
47
|
+
modulus?: number;
|
|
48
|
+
value?: number;
|
|
49
|
+
}): MathFigureSpec;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { MathFigureSpec, MathThemeName } from '../figure/figure-spec';
|
|
2
|
+
import type { Activation } from '../ml/network';
|
|
3
|
+
/**
|
|
4
|
+
* Ready-made AI/ML figures. Each returns a plain MathFigureSpec whose moving
|
|
5
|
+
* part is a number value:
|
|
6
|
+
*
|
|
7
|
+
* - neuralNetworkFigure: `flow` (how far activations have travelled) and inputs `in0`, `in1` …
|
|
8
|
+
* - gradientDescentFigure: `steps`, with `rate` and `momentum`
|
|
9
|
+
* - attentionFigure: no values; animate the heatmap's draw
|
|
10
|
+
* - decisionBoundaryFigure: `epochs`
|
|
11
|
+
* - kmeansFigure: `iteration`
|
|
12
|
+
* - activationFigure: no values
|
|
13
|
+
*/
|
|
14
|
+
export interface MlOptions {
|
|
15
|
+
width?: number;
|
|
16
|
+
height?: number;
|
|
17
|
+
theme?: MathThemeName;
|
|
18
|
+
}
|
|
19
|
+
/** A small network; activations flow from the inputs to the outputs as `flow` goes 0 → layers − 1. */
|
|
20
|
+
export declare function neuralNetworkFigure(options?: MlOptions & {
|
|
21
|
+
layers?: number[];
|
|
22
|
+
activation?: Activation;
|
|
23
|
+
inputs?: number[];
|
|
24
|
+
seed?: string;
|
|
25
|
+
}): MathFigureSpec;
|
|
26
|
+
/** Gradient descent on a long, narrow bowl: a small rate crawls, a large one zigzags, momentum speeds it up. */
|
|
27
|
+
export declare function gradientDescentFigure(options?: MlOptions & {
|
|
28
|
+
expr?: string;
|
|
29
|
+
start?: [number, number];
|
|
30
|
+
rate?: number;
|
|
31
|
+
momentum?: number;
|
|
32
|
+
}): MathFigureSpec;
|
|
33
|
+
/** Attention weights between tokens: each row (a query) softmaxed over the columns (the keys). */
|
|
34
|
+
export declare function attentionFigure(options?: MlOptions & {
|
|
35
|
+
tokens?: string[];
|
|
36
|
+
scores?: number[][];
|
|
37
|
+
seed?: string;
|
|
38
|
+
}): MathFigureSpec;
|
|
39
|
+
/** Logistic regression finding the line between two clusters, epoch by epoch. */
|
|
40
|
+
export declare function decisionBoundaryFigure(options?: MlOptions & {
|
|
41
|
+
rate?: number;
|
|
42
|
+
spread?: number;
|
|
43
|
+
}): MathFigureSpec;
|
|
44
|
+
/** k-means finding clusters: points recolour as the centroids move. */
|
|
45
|
+
export declare function kmeansFigure(options?: MlOptions & {
|
|
46
|
+
centres?: [number, number][];
|
|
47
|
+
k?: number;
|
|
48
|
+
}): MathFigureSpec;
|
|
49
|
+
/** The usual activation functions side by side. */
|
|
50
|
+
export declare function activationFigure(options?: MlOptions): MathFigureSpec;
|
|
@@ -11,6 +11,14 @@
|
|
|
11
11
|
* first in MATH_FONT.
|
|
12
12
|
*/
|
|
13
13
|
export declare const MATH_FONT = "\"Latin Modern Math\", \"STIX Two Math\", \"Cambria Math\", \"Times New Roman\", \"Liberation Serif\", \"Noto Serif\", Times, serif";
|
|
14
|
+
/**
|
|
15
|
+
* Font for operators and symbols (∪ ∩ △ ∈ ≤ ∑ …): maths faces first, then
|
|
16
|
+
* DejaVu, whose serif and maths faces cover them where text serifs such as
|
|
17
|
+
* Liberation Serif do not. Letters, digits and Greek use MATH_FONT.
|
|
18
|
+
*/
|
|
19
|
+
export declare const MATH_SYMBOL_FONT = "\"Latin Modern Math\", \"STIX Two Math\", \"Cambria Math\", \"DejaVu Math TeX Gyre\", \"DejaVu Serif\", serif";
|
|
20
|
+
/** The font a glyph is drawn in: MATH_FONT for letters, digits and Greek, MATH_SYMBOL_FONT for the rest. */
|
|
21
|
+
export declare const fontFor: (text: string) => "\"Latin Modern Math\", \"STIX Two Math\", \"Cambria Math\", \"Times New Roman\", \"Liberation Serif\", \"Noto Serif\", Times, serif" | "\"Latin Modern Math\", \"STIX Two Math\", \"Cambria Math\", \"DejaVu Math TeX Gyre\", \"DejaVu Serif\", serif";
|
|
14
22
|
/** Height above the baseline of the maths axis (where fraction bars and − sit). */
|
|
15
23
|
export declare const AXIS_HEIGHT = 0.25;
|
|
16
24
|
/** Rule thickness for fraction bars and radicals. */
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* Supported:
|
|
5
5
|
* - Letters (italic), digits, `+ - = < > ( ) [ ] | , . ; : ! '`
|
|
6
6
|
* - `^` and `_` with a single token or a `{group}`
|
|
7
|
-
* - `\frac{a}{b}`, `\sqrt{x}`, `\vec{v}`, `\hat{x}`, `\bar{x}`
|
|
7
|
+
* - `\frac{a}{b}`, `\binom{n}{k}`, `\sqrt{x}`, `\vec{v}`, `\hat{x}`, `\bar{x}`
|
|
8
8
|
* - Big operators `\sum \prod \int` (limits from `_` and `^`), and named
|
|
9
9
|
* operators `\sin \cos \tan \log \ln \exp \det \lim \max \min \arg`
|
|
10
10
|
* - Greek letters and the symbols in SYMBOLS (`\cdot \times \le \to \infty` …)
|
|
@@ -36,6 +36,7 @@ export type TexNode = {
|
|
|
36
36
|
type: 'frac';
|
|
37
37
|
num: TexNode;
|
|
38
38
|
den: TexNode;
|
|
39
|
+
bar?: boolean;
|
|
39
40
|
} | {
|
|
40
41
|
type: 'sqrt';
|
|
41
42
|
body: TexNode;
|