@algorisys/tinyfly 0.115.0 → 0.117.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/README.md +11 -6
- package/docs/api-reference.md +2 -1
- 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 +18 -0
- package/docs/maps.md +2 -2
- package/docs/math.md +313 -0
- package/docs/teaching.md +1 -1
- package/lib/addons/characters.js +2614 -2548
- package/lib/addons/math.js +3384 -0
- package/lib/browser/tinyfly-math.iife.js +53 -0
- package/lib/browser/tinyfly.iife.js +12 -12
- package/lib/browser/tinyfly.js +993 -905
- package/lib/browser/tinyfly.umd.js +12 -12
- package/lib/embed/tinyfly-embed.iife.js +1 -1
- package/lib/embed/tinyfly-embed.js +8 -0
- package/lib/engine/tinyfly-engine.js +112 -98
- package/lib/engine/tinyfly-engine.umd.cjs +2 -2
- package/lib/player/tinyfly-player.es.js +8 -0
- package/lib/player/tinyfly-player.iife.js +1 -1
- package/lib/player/tinyfly-player.umd.js +1 -1
- package/lib/types/characters/capabilities.d.ts +8 -0
- package/lib/types/engine/authoring/random.d.ts +7 -0
- package/lib/types/math/browser-entry.d.ts +3 -0
- package/lib/types/math/embed/auto-mount-math.d.ts +7 -0
- package/lib/types/math/embed/explorable.d.ts +46 -0
- package/lib/types/math/embed/mount-math.d.ts +31 -0
- package/lib/types/math/expr/differentiate.d.ts +7 -0
- package/lib/types/math/expr/evaluate.d.ts +21 -0
- package/lib/types/math/expr/parse-expression.d.ts +50 -0
- package/lib/types/math/figure/derived.d.ts +17 -0
- package/lib/types/math/figure/display-list.d.ts +87 -0
- package/lib/types/math/figure/figure-spec.d.ts +349 -0
- package/lib/types/math/figure/item-context.d.ts +39 -0
- package/lib/types/math/figure/items/calculus.d.ts +7 -0
- package/lib/types/math/figure/items/curves.d.ts +7 -0
- package/lib/types/math/figure/items/grid.d.ts +5 -0
- package/lib/types/math/figure/items/probability.d.ts +13 -0
- package/lib/types/math/figure/items/regions.d.ts +5 -0
- package/lib/types/math/figure/items/text.d.ts +5 -0
- package/lib/types/math/figure/items/vectors.d.ts +9 -0
- package/lib/types/math/figure/layout-figure.d.ts +13 -0
- package/lib/types/math/figure/plane.d.ts +20 -0
- package/lib/types/math/figure/validate-spec.d.ts +2 -0
- package/lib/types/math/index.d.ts +49 -0
- package/lib/types/math/linear/mat2.d.ts +41 -0
- package/lib/types/math/linear/vec2.d.ts +18 -0
- package/lib/types/math/presets/calculus.d.ts +59 -0
- package/lib/types/math/presets/linear-algebra.d.ts +42 -0
- package/lib/types/math/presets/probability.d.ts +62 -0
- package/lib/types/math/prob/bayes.d.ts +16 -0
- package/lib/types/math/prob/distributions.d.ts +58 -0
- package/lib/types/math/prob/galton.d.ts +18 -0
- package/lib/types/math/prob/histogram.d.ts +20 -0
- package/lib/types/math/prob/scatter.d.ts +24 -0
- package/lib/types/math/render/arrow.d.ts +10 -0
- package/lib/types/math/render/display-list-to-svg.d.ts +10 -0
- package/lib/types/math/render/draw-display-list.d.ts +6 -0
- package/lib/types/math/steps/math-steps.d.ts +34 -0
- package/lib/types/math/targets/math-target.d.ts +43 -0
- package/lib/types/math/tex/equation-morph.d.ts +21 -0
- package/lib/types/math/tex/glyph-metrics.d.ts +24 -0
- package/lib/types/math/tex/tex-layout.d.ts +59 -0
- package/lib/types/math/tex/tex-parser.d.ts +88 -0
- package/lib/types/player/player.d.ts +6 -0
- package/llms-full.txt +400 -10
- package/llms.txt +32 -31
- package/package.json +6 -2
- package/skills/tinyfly/SKILL.md +12 -0
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { type MountedEmbed, type MountOptions } from '../../embed/mount';
|
|
2
|
+
import type { TinyflyPlayer } from '../../player/player';
|
|
3
|
+
import type { MathFigureSpec } from '../figure/figure-spec';
|
|
4
|
+
/**
|
|
5
|
+
* Maths figures in lesson pages, stepped like any tinyfly embed:
|
|
6
|
+
*
|
|
7
|
+
* <figure data-tinyfly-math data-target="fig" data-options='{"stepMode": true}'>
|
|
8
|
+
* <script type="application/json" data-tinyfly-figure>{ …MathFigureSpec… }</script>
|
|
9
|
+
* <script type="application/json" data-tinyfly-timeline>{ …timeline JSON… }</script>
|
|
10
|
+
* <figcaption>A shear keeps î and slides ĵ.</figcaption>
|
|
11
|
+
* </figure>
|
|
12
|
+
*
|
|
13
|
+
* The figure is drawn into an `<svg>` it creates, and redrawn from the
|
|
14
|
+
* timeline's values (tracks whose target is `data-target`, default `fig`)
|
|
15
|
+
* every time the player moves: playing, stepping, seeking, or showing the
|
|
16
|
+
* final frame under reduced motion. Everything else (markers, step controls,
|
|
17
|
+
* captions, questions, scenarios, pausing off screen) is the ordinary embed.
|
|
18
|
+
*/
|
|
19
|
+
export interface MountedMathFigure {
|
|
20
|
+
element: HTMLElement;
|
|
21
|
+
/** The embed's player; absent for a still figure with no timeline */
|
|
22
|
+
player?: TinyflyPlayer;
|
|
23
|
+
controls?: MountedEmbed['controls'];
|
|
24
|
+
figure: MathFigureSpec;
|
|
25
|
+
/** Draw now (the player redraws on its own; call after changing values yourself) */
|
|
26
|
+
redraw(): void;
|
|
27
|
+
}
|
|
28
|
+
/** Mount one maths figure (idempotent). */
|
|
29
|
+
export declare function mountMath(element: HTMLElement, options?: MountOptions): Promise<MountedMathFigure | undefined>;
|
|
30
|
+
/** Mount every `[data-tinyfly-math]` figure in `root` (default the document). */
|
|
31
|
+
export declare function mountAllMath(root?: ParentNode, options?: MountOptions): Promise<MountedMathFigure[]>;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Expression } from './parse-expression';
|
|
2
|
+
/** d/d`variable` of `e`, simplified. Throws for functions with no derivative rule (floor, round …). */
|
|
3
|
+
export declare function differentiate(e: Expression, variable?: string): Expression;
|
|
4
|
+
/** The n-th derivative (n = 0 gives the expression itself). */
|
|
5
|
+
export declare function nthDerivative(e: Expression, n: number, variable?: string): Expression;
|
|
6
|
+
/** Back to source text, for showing or caching a derived expression. */
|
|
7
|
+
export declare function expressionToString(e: Expression): string;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { type Expression } from './parse-expression';
|
|
2
|
+
/** Variable values by name: `{ x: 1.5, k: 2 }`. */
|
|
3
|
+
export type Scope = Record<string, number>;
|
|
4
|
+
/**
|
|
5
|
+
* The value of a parsed expression for the given variables. Throws when a
|
|
6
|
+
* name is neither in `scope` nor a constant. Results can be NaN or ±Infinity
|
|
7
|
+
* (`1/0`, `sqrt(-1)`): plotting treats those as gaps.
|
|
8
|
+
*/
|
|
9
|
+
export declare function evaluate(expression: Expression, scope?: Scope): number;
|
|
10
|
+
/**
|
|
11
|
+
* A function of one variable from source text, parsed once:
|
|
12
|
+
* `compile('sin(k x)', 'x')` gives `(x, scope) => number`. Parameters other
|
|
13
|
+
* than the variable come from `scope` at call time.
|
|
14
|
+
*/
|
|
15
|
+
export declare function compile(source: string, variable?: string): (value: number, scope?: Scope) => number;
|
|
16
|
+
/**
|
|
17
|
+
* The n-th derivative of a function of one variable from source text,
|
|
18
|
+
* differentiated symbolically once and cached: `compileDerivative('sin(x)', 1)`
|
|
19
|
+
* evaluates cos(x). n = 0 is the function itself.
|
|
20
|
+
*/
|
|
21
|
+
export declare function compileDerivative(source: string, n: number, variable?: string): (value: number, scope?: Scope) => number;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A small, safe expression language for functions written as data:
|
|
3
|
+
* `"sin(x) * k"`, `"x^2 - 1"`, `"exp(-x^2 / 2) / sqrt(2 * pi)"`.
|
|
4
|
+
*
|
|
5
|
+
* - Numbers (`2`, `0.5`, `1e-3`), names (`x`, `t`, any parameter), `pi`, `e`
|
|
6
|
+
* - `+ - * / ^` with the usual precedence; `^` binds right (`2^3^2` is 2^9),
|
|
7
|
+
* and tighter than unary minus (`-x^2` is −(x²))
|
|
8
|
+
* - Function calls from a fixed list (see FUNCTIONS); nothing else is callable
|
|
9
|
+
* - Implicit multiplication between a number and a name or bracket: `2x`, `3(x + 1)`
|
|
10
|
+
*
|
|
11
|
+
* Parsing builds a plain AST that can be evaluated many times. Nothing is ever
|
|
12
|
+
* passed to `eval` or `Function`, so an expression can only read the names it
|
|
13
|
+
* is given and call the listed functions.
|
|
14
|
+
*/
|
|
15
|
+
export type Expression = {
|
|
16
|
+
type: 'number';
|
|
17
|
+
value: number;
|
|
18
|
+
} | {
|
|
19
|
+
type: 'name';
|
|
20
|
+
name: string;
|
|
21
|
+
} | {
|
|
22
|
+
type: 'unary';
|
|
23
|
+
operator: '-' | '+';
|
|
24
|
+
operand: Expression;
|
|
25
|
+
} | {
|
|
26
|
+
type: 'binary';
|
|
27
|
+
operator: '+' | '-' | '*' | '/' | '^';
|
|
28
|
+
left: Expression;
|
|
29
|
+
right: Expression;
|
|
30
|
+
} | {
|
|
31
|
+
type: 'call';
|
|
32
|
+
name: string;
|
|
33
|
+
args: Expression[];
|
|
34
|
+
};
|
|
35
|
+
/** Callable functions and how many arguments each takes. */
|
|
36
|
+
export declare const FUNCTIONS: Record<string, {
|
|
37
|
+
arity: number;
|
|
38
|
+
apply: (...args: number[]) => number;
|
|
39
|
+
}>;
|
|
40
|
+
export declare const CONSTANTS: Record<string, number>;
|
|
41
|
+
export declare class ExpressionError extends Error {
|
|
42
|
+
readonly source: string;
|
|
43
|
+
/** 0-based index into the source */
|
|
44
|
+
readonly position: number;
|
|
45
|
+
constructor(message: string, source: string, position: number);
|
|
46
|
+
}
|
|
47
|
+
/** Parse `source` into an AST; throws an ExpressionError naming the position. */
|
|
48
|
+
export declare function parseExpression(source: string): Expression;
|
|
49
|
+
/** Every variable name the expression reads (constants excluded), sorted. */
|
|
50
|
+
export declare function expressionNames(expression: Expression): string[];
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type Scope } from '../expr/evaluate';
|
|
2
|
+
import type { MathItem } from './figure-spec';
|
|
3
|
+
import { type Values } from './item-context';
|
|
4
|
+
/**
|
|
5
|
+
* Numbers items work out from the figure's values, so TeX can show them with
|
|
6
|
+
* `\val{…}`: a tangent's `ID.slope`, `ID.x` and `ID.y`; a Riemann sum's
|
|
7
|
+
* `ID.sum`, `ID.exact` (the integral) and `ID.n`; an area's `ID.value`; a
|
|
8
|
+
* point on a curve's `ID.x` and `ID.y`; a Taylor polynomial's `ID.order` and `ID.degree`.
|
|
9
|
+
* Pure: worked out again from the values at every moment.
|
|
10
|
+
*/
|
|
11
|
+
/** ∫ f from a to b by Simpson's rule (512 panels): accurate for smooth teaching functions. */
|
|
12
|
+
export declare function integrate(f: (x: number) => number, a: number, b: number, panels?: number): number;
|
|
13
|
+
/** The sample points of a Riemann sum's pieces: [left, right, height] each. */
|
|
14
|
+
export declare function riemannPieces(f: (x: number) => number, a: number, b: number, n: number, rule: 'left' | 'right' | 'midpoint' | 'trapezoid'): [number, number, number, number][];
|
|
15
|
+
/** How many Riemann pieces a value asks for: a whole number from 1 to 400. */
|
|
16
|
+
export declare const pieceCount: (n: number) => number;
|
|
17
|
+
export declare function derivedValues(items: MathItem[], values: Values, scope: Scope): Scope;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import type { Vec2 } from '../linear/vec2';
|
|
2
|
+
import type { TexLayout, TexPiece } from '../tex/tex-layout';
|
|
3
|
+
/**
|
|
4
|
+
* What a figure looks like at one moment, as plain shapes in screen pixels
|
|
5
|
+
* (y down). Layout produces it as a pure function of the figure and its
|
|
6
|
+
* values; the canvas and SVG renderers only draw it, so a figure looks the
|
|
7
|
+
* same in a video, a canvas demo and an embedded SVG.
|
|
8
|
+
*/
|
|
9
|
+
export type Shape = {
|
|
10
|
+
type: 'line';
|
|
11
|
+
id: string;
|
|
12
|
+
points: Vec2[];
|
|
13
|
+
closed?: boolean;
|
|
14
|
+
stroke?: string;
|
|
15
|
+
width?: number;
|
|
16
|
+
fill?: string;
|
|
17
|
+
opacity?: number;
|
|
18
|
+
dash?: number[];
|
|
19
|
+
} | {
|
|
20
|
+
type: 'arrow';
|
|
21
|
+
id: string;
|
|
22
|
+
points: Vec2[];
|
|
23
|
+
stroke: string;
|
|
24
|
+
width: number;
|
|
25
|
+
/** Arrowhead length, px */
|
|
26
|
+
head: number;
|
|
27
|
+
opacity?: number;
|
|
28
|
+
} | {
|
|
29
|
+
type: 'circle';
|
|
30
|
+
id: string;
|
|
31
|
+
x: number;
|
|
32
|
+
y: number;
|
|
33
|
+
r: number;
|
|
34
|
+
fill?: string;
|
|
35
|
+
stroke?: string;
|
|
36
|
+
width?: number;
|
|
37
|
+
opacity?: number;
|
|
38
|
+
} | {
|
|
39
|
+
type: 'text';
|
|
40
|
+
id: string;
|
|
41
|
+
text: string;
|
|
42
|
+
x: number;
|
|
43
|
+
y: number;
|
|
44
|
+
/** Font size, px */
|
|
45
|
+
size: number;
|
|
46
|
+
/** CSS font family */
|
|
47
|
+
font: string;
|
|
48
|
+
italic?: boolean;
|
|
49
|
+
color: string;
|
|
50
|
+
align: 'left' | 'center' | 'right';
|
|
51
|
+
opacity?: number;
|
|
52
|
+
};
|
|
53
|
+
export interface DisplayList {
|
|
54
|
+
width: number;
|
|
55
|
+
height: number;
|
|
56
|
+
background?: string;
|
|
57
|
+
shapes: Shape[];
|
|
58
|
+
}
|
|
59
|
+
/** Font for axis numbers and plain labels. */
|
|
60
|
+
export declare const UI_FONT = "system-ui, -apple-system, \"Segoe UI\", sans-serif";
|
|
61
|
+
/**
|
|
62
|
+
* The first `fraction` (0..1) of a run of polylines by total length, as
|
|
63
|
+
* though one pen drew them in order: what "draw it on" shows part way.
|
|
64
|
+
*/
|
|
65
|
+
export declare function trimPolylines(lines: Vec2[][], fraction: number): Vec2[][];
|
|
66
|
+
export interface TexPlacement {
|
|
67
|
+
/** Where the layout's anchor point lands, px */
|
|
68
|
+
x: number;
|
|
69
|
+
y: number;
|
|
70
|
+
/** Font size, px (1 em of the layout) */
|
|
71
|
+
size: number;
|
|
72
|
+
color: string;
|
|
73
|
+
/** Which point of the layout sits at x, y (default centre/middle) */
|
|
74
|
+
align?: 'left' | 'center' | 'right';
|
|
75
|
+
baseline?: 'baseline' | 'middle';
|
|
76
|
+
opacity?: number;
|
|
77
|
+
/** Prefixed to every piece's id, so several equations in one figure stay apart */
|
|
78
|
+
idPrefix: string;
|
|
79
|
+
}
|
|
80
|
+
/** The origin (left edge, baseline) of a layout placed by `place`, in px. */
|
|
81
|
+
export declare function texOrigin(layout: TexLayout, place: Pick<TexPlacement, 'x' | 'y' | 'size' | 'align' | 'baseline'>): Vec2;
|
|
82
|
+
/** Typeset pieces (in ems, from a layout or a morph) as shapes at a placement. */
|
|
83
|
+
export declare function texPiecesToShapes(pieces: (TexPiece & {
|
|
84
|
+
opacity?: number;
|
|
85
|
+
})[], origin: Vec2, place: TexPlacement): Shape[];
|
|
86
|
+
/** A whole layout as shapes. */
|
|
87
|
+
export declare function texToShapes(layout: TexLayout, place: TexPlacement): Shape[];
|
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
import type { Vec2 } from '../linear/vec2';
|
|
2
|
+
/**
|
|
3
|
+
* A maths figure as plain JSON: a coordinate plane and the items drawn on it.
|
|
4
|
+
*
|
|
5
|
+
* Everything that moves is a **value**: a number (`k`), a vector (`v: [1, 2]`)
|
|
6
|
+
* or a matrix (`M: [a, b, c, d]`). Items refer to values by name, and a
|
|
7
|
+
* timeline animates them as flat props on the figure's target, with no code:
|
|
8
|
+
*
|
|
9
|
+
* - a value by its name: `M` keyed from `[1, 0, 0, 1]` to `[2, 1, 0, 1]`
|
|
10
|
+
* warps the grid; `k` keyed from 1 to 3 reshapes `sin(k x)`
|
|
11
|
+
* - `ITEM.draw` (0..1): draws an item on, like a pen
|
|
12
|
+
* - `ITEM.opacity` (0..1)
|
|
13
|
+
* - `ITEM.step` on an equation: 0, 1, 2 … shows each step; in between, terms
|
|
14
|
+
* glide from one step to the next
|
|
15
|
+
*
|
|
16
|
+
* The same figure and values always give the same picture.
|
|
17
|
+
*/
|
|
18
|
+
export interface MathFigureSpec {
|
|
19
|
+
/** Size in px */
|
|
20
|
+
width: number;
|
|
21
|
+
height: number;
|
|
22
|
+
/** World x range shown (default [-5, 5]) */
|
|
23
|
+
x?: [number, number];
|
|
24
|
+
/** World y range shown (default: centred on 0, with the same scale as x) */
|
|
25
|
+
y?: [number, number];
|
|
26
|
+
/** Colour scheme (default `dark`, the classic look of maths videos) */
|
|
27
|
+
theme?: MathThemeName;
|
|
28
|
+
/** Background colour; `none` for transparent (default: the theme's) */
|
|
29
|
+
background?: string;
|
|
30
|
+
/** Starting values: numbers, `[x, y]` vectors, `[a, b, c, d]` matrices */
|
|
31
|
+
values?: Record<string, number | number[]>;
|
|
32
|
+
items: MathItem[];
|
|
33
|
+
}
|
|
34
|
+
/** A point or vector given directly, or the name of a value holding one. */
|
|
35
|
+
export type VecRef = Vec2 | string;
|
|
36
|
+
/** A number given directly, or the name of a number value (so a timeline can move it). */
|
|
37
|
+
export type NumRef = number | string;
|
|
38
|
+
interface ItemBase {
|
|
39
|
+
/** Unique within the figure; names the item's props (`grid.draw`) */
|
|
40
|
+
id: string;
|
|
41
|
+
/** Colour: a theme colour name (`blue`, `green`, `red`, `yellow`, `ink`, …) or any CSS colour */
|
|
42
|
+
color?: string;
|
|
43
|
+
/** Drawn part, 0..1 (default 1); animate `ID.draw` */
|
|
44
|
+
draw?: number;
|
|
45
|
+
/** 0..1 (default 1); animate `ID.opacity` */
|
|
46
|
+
opacity?: number;
|
|
47
|
+
}
|
|
48
|
+
/** Lines of the plane, warped by a matrix value: the picture of a linear map. */
|
|
49
|
+
export interface GridItem extends ItemBase {
|
|
50
|
+
kind: 'grid';
|
|
51
|
+
/** World units between lines (default 1) */
|
|
52
|
+
step?: number;
|
|
53
|
+
/** Name of a matrix value applied to the grid */
|
|
54
|
+
transform?: string;
|
|
55
|
+
/** Also draw the untransformed grid, faintly, underneath (default false) */
|
|
56
|
+
ghost?: boolean;
|
|
57
|
+
}
|
|
58
|
+
export interface AxesItem extends ItemBase {
|
|
59
|
+
kind: 'axes';
|
|
60
|
+
/** World units between ticks (default 1); 0 for none */
|
|
61
|
+
ticks?: number;
|
|
62
|
+
/** Number the ticks (default true) */
|
|
63
|
+
labels?: boolean;
|
|
64
|
+
}
|
|
65
|
+
export interface VectorItem extends ItemBase {
|
|
66
|
+
kind: 'vector';
|
|
67
|
+
/** The tip */
|
|
68
|
+
to: VecRef;
|
|
69
|
+
/** The tail (default the origin) */
|
|
70
|
+
from?: VecRef;
|
|
71
|
+
/** Name of a matrix value applied to both ends */
|
|
72
|
+
transform?: string;
|
|
73
|
+
/** TeX label by the tip */
|
|
74
|
+
label?: string;
|
|
75
|
+
/** Where the label goes: beyond the tip (default) or beside the middle, on the left of travel */
|
|
76
|
+
labelAt?: 'tip' | 'middle';
|
|
77
|
+
/** Line width in px at a 600 px figure (default 4) */
|
|
78
|
+
width?: number;
|
|
79
|
+
}
|
|
80
|
+
/** î and ĵ, optionally carried by a matrix value. */
|
|
81
|
+
export interface BasisItem extends ItemBase {
|
|
82
|
+
kind: 'basis';
|
|
83
|
+
transform?: string;
|
|
84
|
+
/** Label the vectors î and ĵ (default true) */
|
|
85
|
+
labels?: boolean;
|
|
86
|
+
}
|
|
87
|
+
export interface PointItem extends ItemBase {
|
|
88
|
+
kind: 'point';
|
|
89
|
+
/** Where it is; or give `on` and `x` to put it on a curve */
|
|
90
|
+
at?: VecRef;
|
|
91
|
+
/** A function of x the point rides on: the point is (x, f(x)) */
|
|
92
|
+
on?: string;
|
|
93
|
+
/** The point's x on `on` (a number or a number value) */
|
|
94
|
+
x?: NumRef;
|
|
95
|
+
transform?: string;
|
|
96
|
+
label?: string;
|
|
97
|
+
/** px at a 600 px figure (default 6) */
|
|
98
|
+
radius?: number;
|
|
99
|
+
/** An open circle: a value the function does not take (a hole, a limit) */
|
|
100
|
+
hollow?: boolean;
|
|
101
|
+
}
|
|
102
|
+
/** y = f(x) from an expression (see expr/parse-expression.ts); number values are its parameters. */
|
|
103
|
+
export interface FunctionItem extends ItemBase {
|
|
104
|
+
kind: 'function';
|
|
105
|
+
expr: string;
|
|
106
|
+
/** Plot this derivative of it instead (1: f′, 2: f″ …), worked out exactly */
|
|
107
|
+
derivative?: number;
|
|
108
|
+
/** x range drawn (default the figure's) */
|
|
109
|
+
domain?: [number, number];
|
|
110
|
+
width?: number;
|
|
111
|
+
/** Dashed line (default false) */
|
|
112
|
+
dashed?: boolean;
|
|
113
|
+
}
|
|
114
|
+
/** The tangent to y = f(x) at x = at; with `h`, the secant through at and at + h instead. */
|
|
115
|
+
export interface TangentItem extends ItemBase {
|
|
116
|
+
kind: 'tangent';
|
|
117
|
+
expr: string;
|
|
118
|
+
at: NumRef;
|
|
119
|
+
/** Secant step: the line through (at, f(at)) and (at + h, f(at + h)); 0 or absent is the tangent */
|
|
120
|
+
h?: NumRef;
|
|
121
|
+
/** World x units the line spans, centred on `at` (default the whole view) */
|
|
122
|
+
length?: number;
|
|
123
|
+
/** Mark the point(s) it touches (default true) */
|
|
124
|
+
points?: boolean;
|
|
125
|
+
/** TeX by the point; `\val{ID.slope}` shows the slope */
|
|
126
|
+
label?: string;
|
|
127
|
+
width?: number;
|
|
128
|
+
}
|
|
129
|
+
/** Rectangles (or trapezoids) under y = f(x): a Riemann sum. */
|
|
130
|
+
export interface RiemannItem extends ItemBase {
|
|
131
|
+
kind: 'riemann';
|
|
132
|
+
expr: string;
|
|
133
|
+
from: NumRef;
|
|
134
|
+
to: NumRef;
|
|
135
|
+
/** How many pieces (rounded; animate it to watch them thin out) */
|
|
136
|
+
n: NumRef;
|
|
137
|
+
/** Where each rectangle meets the curve (default `left`) */
|
|
138
|
+
rule?: 'left' | 'right' | 'midpoint' | 'trapezoid';
|
|
139
|
+
/** Fill opacity (default 0.35) */
|
|
140
|
+
fillOpacity?: number;
|
|
141
|
+
}
|
|
142
|
+
/** The region between y = f(x) and y = lower(x) (default the x axis) from `from` to `to`. */
|
|
143
|
+
export interface AreaItem extends ItemBase {
|
|
144
|
+
kind: 'area';
|
|
145
|
+
expr: string;
|
|
146
|
+
from: NumRef;
|
|
147
|
+
to: NumRef;
|
|
148
|
+
/** The lower boundary, a function of x (default 0) */
|
|
149
|
+
lower?: string;
|
|
150
|
+
/** Colour where the region is below the lower boundary (default `red`) */
|
|
151
|
+
negativeColor?: string;
|
|
152
|
+
fillOpacity?: number;
|
|
153
|
+
}
|
|
154
|
+
/** The Taylor polynomial of y = f(x) about x = at; a fractional order blends into the next term. */
|
|
155
|
+
export interface TaylorItem extends ItemBase {
|
|
156
|
+
kind: 'taylor';
|
|
157
|
+
expr: string;
|
|
158
|
+
at: NumRef;
|
|
159
|
+
/** Degree, 0..20 (animate it to add terms one by one) */
|
|
160
|
+
order: NumRef;
|
|
161
|
+
domain?: [number, number];
|
|
162
|
+
width?: number;
|
|
163
|
+
}
|
|
164
|
+
/** (x(t), y(t)) from two expressions. */
|
|
165
|
+
export interface ParametricItem extends ItemBase {
|
|
166
|
+
kind: 'parametric';
|
|
167
|
+
x: string;
|
|
168
|
+
y: string;
|
|
169
|
+
/** t range (default [0, 2π]) */
|
|
170
|
+
t?: [number, number];
|
|
171
|
+
transform?: string;
|
|
172
|
+
width?: number;
|
|
173
|
+
}
|
|
174
|
+
/** A filled polygon (default the unit square), optionally carried by a matrix: its area shows the determinant. */
|
|
175
|
+
export interface PolygonItem extends ItemBase {
|
|
176
|
+
kind: 'polygon';
|
|
177
|
+
points?: Vec2[];
|
|
178
|
+
transform?: string;
|
|
179
|
+
/** Fill opacity (default 0.3) */
|
|
180
|
+
fillOpacity?: number;
|
|
181
|
+
/** TeX label at its centre; `\val{M.det}` shows the matrix's determinant */
|
|
182
|
+
label?: string;
|
|
183
|
+
}
|
|
184
|
+
/** A line through the origin along a vector: a span, or (with `eigen`) a matrix's eigenvectors. */
|
|
185
|
+
export interface LineItem extends ItemBase {
|
|
186
|
+
kind: 'line';
|
|
187
|
+
/** Direction */
|
|
188
|
+
along?: VecRef;
|
|
189
|
+
/** Instead of `along`: one line per real eigenvector of this matrix value */
|
|
190
|
+
eigen?: string;
|
|
191
|
+
dashed?: boolean;
|
|
192
|
+
width?: number;
|
|
193
|
+
}
|
|
194
|
+
/** TeX placed in world or screen coordinates. */
|
|
195
|
+
export interface LabelItem extends ItemBase {
|
|
196
|
+
kind: 'label';
|
|
197
|
+
tex: string;
|
|
198
|
+
/** Where, in world units (or px with `screen: true`) */
|
|
199
|
+
at: VecRef;
|
|
200
|
+
screen?: boolean;
|
|
201
|
+
/** Font size, px at a 600 px figure (default 28) */
|
|
202
|
+
size?: number;
|
|
203
|
+
align?: 'left' | 'center' | 'right';
|
|
204
|
+
/** A soft panel behind the text, so it reads over a grid (default true for `screen` labels) */
|
|
205
|
+
backdrop?: boolean;
|
|
206
|
+
}
|
|
207
|
+
/** An equation worked in steps; `ID.step` moves between them, terms gliding. */
|
|
208
|
+
export interface EquationItem extends ItemBase {
|
|
209
|
+
kind: 'equation';
|
|
210
|
+
/** TeX for each step */
|
|
211
|
+
steps: string[];
|
|
212
|
+
/** Which step (default 0); fractional values are part way to the next */
|
|
213
|
+
step?: number;
|
|
214
|
+
at: VecRef;
|
|
215
|
+
screen?: boolean;
|
|
216
|
+
size?: number;
|
|
217
|
+
align?: 'left' | 'center' | 'right';
|
|
218
|
+
/** A soft panel behind the text (default true for `screen` equations) */
|
|
219
|
+
backdrop?: boolean;
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* A distribution whose numbers may be value names, so a slider or timeline can
|
|
223
|
+
* move the mean, the spread or n: `{ kind: 'normal', mean: 'mu', sd: 1 }`.
|
|
224
|
+
*/
|
|
225
|
+
export type DistributionRef = {
|
|
226
|
+
kind: 'normal';
|
|
227
|
+
mean?: NumRef;
|
|
228
|
+
sd?: NumRef;
|
|
229
|
+
} | {
|
|
230
|
+
kind: 'uniform';
|
|
231
|
+
min?: NumRef;
|
|
232
|
+
max?: NumRef;
|
|
233
|
+
} | {
|
|
234
|
+
kind: 'exponential';
|
|
235
|
+
rate?: NumRef;
|
|
236
|
+
} | {
|
|
237
|
+
kind: 'binomial';
|
|
238
|
+
n: NumRef;
|
|
239
|
+
p: NumRef;
|
|
240
|
+
} | {
|
|
241
|
+
kind: 'poisson';
|
|
242
|
+
rate: NumRef;
|
|
243
|
+
} | {
|
|
244
|
+
kind: 'dice';
|
|
245
|
+
count?: NumRef;
|
|
246
|
+
sides?: NumRef;
|
|
247
|
+
} | {
|
|
248
|
+
kind: 'mean';
|
|
249
|
+
of: DistributionRef;
|
|
250
|
+
count: NumRef;
|
|
251
|
+
};
|
|
252
|
+
/** Samples from a distribution, binned: fills as `count` grows (the same samples every time, from `seed`). */
|
|
253
|
+
export interface HistogramItem extends ItemBase {
|
|
254
|
+
kind: 'histogram';
|
|
255
|
+
distribution: DistributionRef;
|
|
256
|
+
/** How many samples so far (animate it to watch the bars grow) */
|
|
257
|
+
count: NumRef;
|
|
258
|
+
/** [from, to, number of bins]; discrete distributions default to one bin per whole number */
|
|
259
|
+
bins?: [number, number, number];
|
|
260
|
+
/** Bar heights as a density (area 1, matches the curve; default) or as counts */
|
|
261
|
+
scale?: 'density' | 'count';
|
|
262
|
+
/** Draw the distribution's own density over the bars (default false) */
|
|
263
|
+
curve?: boolean;
|
|
264
|
+
/** Seed for the samples (default the item id) */
|
|
265
|
+
seed?: number | string;
|
|
266
|
+
fillOpacity?: number;
|
|
267
|
+
}
|
|
268
|
+
/** A distribution's density curve (or bars, for whole-number ones), with an optional shaded probability. */
|
|
269
|
+
export interface DistributionItem extends ItemBase {
|
|
270
|
+
kind: 'distribution';
|
|
271
|
+
distribution: DistributionRef;
|
|
272
|
+
/** Shade P(from ≤ X ≤ to); `\val{ID.p}` shows it */
|
|
273
|
+
shade?: [NumRef, NumRef];
|
|
274
|
+
domain?: [number, number];
|
|
275
|
+
width?: number;
|
|
276
|
+
fillOpacity?: number;
|
|
277
|
+
}
|
|
278
|
+
/** A Galton board: balls bounce left or right at each row of pegs and pile up into a bell. */
|
|
279
|
+
export interface GaltonItem extends ItemBase {
|
|
280
|
+
kind: 'galton';
|
|
281
|
+
/** Rows of pegs (default 10) */
|
|
282
|
+
rows?: number;
|
|
283
|
+
/** Balls dropped so far; the fractional part is the next ball's way down */
|
|
284
|
+
balls: NumRef;
|
|
285
|
+
seed?: number | string;
|
|
286
|
+
/** World units between pegs (default 0.5) */
|
|
287
|
+
spacing?: number;
|
|
288
|
+
/** World y of the top peg (default near the top of the view) */
|
|
289
|
+
top?: number;
|
|
290
|
+
}
|
|
291
|
+
/** Random points: a uniform scatter (Monte Carlo) or noisy points about a line (regression). */
|
|
292
|
+
export interface ScatterItem extends ItemBase {
|
|
293
|
+
kind: 'scatter';
|
|
294
|
+
points: {
|
|
295
|
+
kind: 'uniform';
|
|
296
|
+
x: [number, number];
|
|
297
|
+
y: [number, number];
|
|
298
|
+
} | {
|
|
299
|
+
kind: 'linear';
|
|
300
|
+
slope: NumRef;
|
|
301
|
+
intercept: NumRef;
|
|
302
|
+
noise: NumRef;
|
|
303
|
+
x: [number, number];
|
|
304
|
+
};
|
|
305
|
+
/** How many points so far */
|
|
306
|
+
count: NumRef;
|
|
307
|
+
seed?: number | string;
|
|
308
|
+
/** An expression in x and y; points where it is true take `insideColor` (e.g. `x^2 + y^2 - 1`, true when ≤ 0) */
|
|
309
|
+
inside?: string;
|
|
310
|
+
insideColor?: string;
|
|
311
|
+
/** Draw the least-squares line (default false) */
|
|
312
|
+
fit?: boolean;
|
|
313
|
+
/** px at a 600 px figure (default 3) */
|
|
314
|
+
radius?: number;
|
|
315
|
+
}
|
|
316
|
+
/** A unit square cut by a prior and a test's two error rates: Bayes' rule as areas. */
|
|
317
|
+
export interface BayesItem extends ItemBase {
|
|
318
|
+
kind: 'bayes';
|
|
319
|
+
/** P(condition) */
|
|
320
|
+
prior: NumRef;
|
|
321
|
+
/** P(positive | condition) */
|
|
322
|
+
truePositive: NumRef;
|
|
323
|
+
/** P(positive | no condition) */
|
|
324
|
+
falsePositive: NumRef;
|
|
325
|
+
/** World bottom-left corner and size (default most of the view) */
|
|
326
|
+
at?: Vec2;
|
|
327
|
+
size?: Vec2;
|
|
328
|
+
/** Label the four regions (default true) */
|
|
329
|
+
labels?: boolean;
|
|
330
|
+
}
|
|
331
|
+
export type MathItem = GridItem | AxesItem | VectorItem | BasisItem | PointItem | FunctionItem | ParametricItem | PolygonItem | LineItem | LabelItem | EquationItem | TangentItem | RiemannItem | AreaItem | TaylorItem | HistogramItem | DistributionItem | GaltonItem | ScatterItem | BayesItem;
|
|
332
|
+
export type MathItemKind = MathItem['kind'];
|
|
333
|
+
export declare const ITEM_KINDS: readonly MathItemKind[];
|
|
334
|
+
export type MathThemeName = 'dark' | 'light' | 'chalk';
|
|
335
|
+
export interface MathTheme {
|
|
336
|
+
background: string;
|
|
337
|
+
ink: string;
|
|
338
|
+
grid: string;
|
|
339
|
+
ghost: string;
|
|
340
|
+
axes: string;
|
|
341
|
+
colors: Record<string, string>;
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* Colour schemes. `dark` follows the long-standing maths-video palette (blue
|
|
345
|
+
* grid, green î, red ĵ, yellow highlights); `light` suits lesson pages;
|
|
346
|
+
* `chalk` is a blackboard.
|
|
347
|
+
*/
|
|
348
|
+
export declare const MATH_THEMES: Record<MathThemeName, MathTheme>;
|
|
349
|
+
export {};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { type Mat2 } from '../linear/mat2';
|
|
2
|
+
import { type Vec2 } from '../linear/vec2';
|
|
3
|
+
import type { DistributionRef, MathTheme, NumRef, VecRef } from './figure-spec';
|
|
4
|
+
import type { Distribution } from '../prob/distributions';
|
|
5
|
+
import type { Plane } from './plane';
|
|
6
|
+
/** Values by name, as the figure holds them at one moment. */
|
|
7
|
+
export type Values = Record<string, number | number[]>;
|
|
8
|
+
/** What an item's layout reads: where it is, the values, its colours and how much of it shows. */
|
|
9
|
+
export interface ItemContext {
|
|
10
|
+
plane: Plane;
|
|
11
|
+
theme: MathTheme;
|
|
12
|
+
values: Values;
|
|
13
|
+
/**
|
|
14
|
+
* Numbers for expressions and `\val{…}`: every number value, plus for each
|
|
15
|
+
* matrix `M.a M.b M.c M.d M.det` and for each vector `v.x v.y v.length`.
|
|
16
|
+
*/
|
|
17
|
+
scope: Record<string, number>;
|
|
18
|
+
/** Drawn part, 0..1 */
|
|
19
|
+
draw: number;
|
|
20
|
+
/** 0..1 */
|
|
21
|
+
opacity: number;
|
|
22
|
+
/** The item's own props (`step` for an equation) */
|
|
23
|
+
prop(name: string): number | undefined;
|
|
24
|
+
}
|
|
25
|
+
/** Numbers for expressions and TeX values, from all values (see ItemContext.scope). */
|
|
26
|
+
export declare function scopeOf(values: Values): Record<string, number>;
|
|
27
|
+
/** A theme colour by name, or the CSS colour as given, or the fallback. */
|
|
28
|
+
export declare function colorOf(theme: MathTheme, color: string | undefined, fallback: string): string;
|
|
29
|
+
/** A vector given directly or by value name. */
|
|
30
|
+
export declare function vecOf(values: Values, ref: VecRef): Vec2;
|
|
31
|
+
/** A matrix value by name; the identity when no name is given. */
|
|
32
|
+
export declare function matrixOf(values: Values, name: string | undefined): Mat2;
|
|
33
|
+
/** A number given directly or by the name of a number value. */
|
|
34
|
+
export declare function numOf(values: Values, ref: NumRef): number;
|
|
35
|
+
/**
|
|
36
|
+
* A distribution with its value names replaced by the values' numbers. Counts
|
|
37
|
+
* (binomial n, dice count and sides, a mean's count) are rounded to whole numbers.
|
|
38
|
+
*/
|
|
39
|
+
export declare function distributionOf(values: Values, ref: DistributionRef): Distribution;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { AreaItem, RiemannItem, TangentItem, TaylorItem } from '../figure-spec';
|
|
2
|
+
import { type Shape } from '../display-list';
|
|
3
|
+
import { type ItemContext } from '../item-context';
|
|
4
|
+
export declare function layoutTangent(item: TangentItem, ctx: ItemContext): Shape[];
|
|
5
|
+
export declare function layoutRiemann(item: RiemannItem, ctx: ItemContext): Shape[];
|
|
6
|
+
export declare function layoutArea(item: AreaItem, ctx: ItemContext): Shape[];
|
|
7
|
+
export declare function layoutTaylor(item: TaylorItem, ctx: ItemContext): Shape[];
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { FunctionItem, ParametricItem } from '../figure-spec';
|
|
2
|
+
import { type Shape } from '../display-list';
|
|
3
|
+
import { type ItemContext } from '../item-context';
|
|
4
|
+
/** y = f(x) from x0 to x1 as drawn-on runs, broken at gaps and asymptotes. */
|
|
5
|
+
export declare function curveRuns(id: string, f: (x: number) => number, x0: number, x1: number, color: string, width: number, ctx: ItemContext, dash?: number[]): Shape[];
|
|
6
|
+
export declare function layoutFunction(item: FunctionItem, ctx: ItemContext): Shape[];
|
|
7
|
+
export declare function layoutParametric(item: ParametricItem, ctx: ItemContext): Shape[];
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { AxesItem, GridItem } from '../figure-spec';
|
|
2
|
+
import { type Shape } from '../display-list';
|
|
3
|
+
import { type ItemContext } from '../item-context';
|
|
4
|
+
export declare function layoutGrid(item: GridItem, ctx: ItemContext): Shape[];
|
|
5
|
+
export declare function layoutAxes(item: AxesItem, ctx: ItemContext): Shape[];
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { type PointsSpec } from '../../prob/scatter';
|
|
2
|
+
import type { BayesItem, DistributionItem, GaltonItem, HistogramItem, ScatterItem } from '../figure-spec';
|
|
3
|
+
import { type Shape } from '../display-list';
|
|
4
|
+
import { type ItemContext } from '../item-context';
|
|
5
|
+
/** How many samples a histogram or scatter has so far: a whole number, at most 20 000. */
|
|
6
|
+
export declare const sampleCount: (n: number) => number;
|
|
7
|
+
export declare function layoutHistogram(item: HistogramItem, ctx: ItemContext): Shape[];
|
|
8
|
+
export declare function layoutDistribution(item: DistributionItem, ctx: ItemContext): Shape[];
|
|
9
|
+
export declare function layoutGalton(item: GaltonItem, ctx: ItemContext): Shape[];
|
|
10
|
+
export declare function layoutScatter(item: ScatterItem, ctx: ItemContext): Shape[];
|
|
11
|
+
/** A scatter item's points spec with its numbers resolved. */
|
|
12
|
+
export declare function pointsSpec(item: ScatterItem, ctx: Pick<ItemContext, 'values'>): PointsSpec;
|
|
13
|
+
export declare function layoutBayes(item: BayesItem, ctx: ItemContext): Shape[];
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { LineItem, PolygonItem } from '../figure-spec';
|
|
2
|
+
import { type Shape } from '../display-list';
|
|
3
|
+
import { type ItemContext } from '../item-context';
|
|
4
|
+
export declare function layoutPolygon(item: PolygonItem, ctx: ItemContext): Shape[];
|
|
5
|
+
export declare function layoutLine(item: LineItem, ctx: ItemContext): Shape[];
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { EquationItem, LabelItem } from '../figure-spec';
|
|
2
|
+
import { type Shape } from '../display-list';
|
|
3
|
+
import { type ItemContext } from '../item-context';
|
|
4
|
+
export declare function layoutLabel(item: LabelItem, ctx: ItemContext): Shape[];
|
|
5
|
+
export declare function layoutEquation(item: EquationItem, ctx: ItemContext): Shape[];
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type Vec2 } from '../../linear/vec2';
|
|
2
|
+
import type { BasisItem, PointItem, VectorItem } from '../figure-spec';
|
|
3
|
+
import { type Shape } from '../display-list';
|
|
4
|
+
import { type ItemContext } from '../item-context';
|
|
5
|
+
/** A TeX label just beyond `tip`, pushed out along `direction` (screen px). */
|
|
6
|
+
export declare function tipLabel(tex: string, tip: Vec2, direction: Vec2, color: string, id: string, ctx: ItemContext): Shape[];
|
|
7
|
+
export declare function layoutVector(item: VectorItem, ctx: ItemContext): Shape[];
|
|
8
|
+
export declare function layoutBasis(item: BasisItem, ctx: ItemContext): Shape[];
|
|
9
|
+
export declare function layoutPoint(item: PointItem, ctx: ItemContext): Shape[];
|