@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,13 @@
|
|
|
1
|
+
import { type MathFigureSpec } from './figure-spec';
|
|
2
|
+
import type { DisplayList } from './display-list';
|
|
3
|
+
/**
|
|
4
|
+
* Animated values for a figure, as a timeline writes them on its target:
|
|
5
|
+
* a value by name (`M`, `k`, `v`) or an item prop (`grid.draw`, `eq.step`).
|
|
6
|
+
*/
|
|
7
|
+
export type MathProps = Record<string, number | number[] | string>;
|
|
8
|
+
/**
|
|
9
|
+
* The figure at one moment: its spec with these props applied, as shapes.
|
|
10
|
+
* Pure and deterministic. Items draw in the order they are listed, so later
|
|
11
|
+
* items sit on top. Validate the spec once with `validateMathFigure` first.
|
|
12
|
+
*/
|
|
13
|
+
export declare function layoutFigure(spec: MathFigureSpec, props?: MathProps): DisplayList;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { Vec2 } from '../linear/vec2';
|
|
2
|
+
import type { MathFigureSpec } from './figure-spec';
|
|
3
|
+
/**
|
|
4
|
+
* The mapping between world units (maths axes, y up) and the figure's pixels
|
|
5
|
+
* (y down). Pure data plus two functions; no drawing.
|
|
6
|
+
*/
|
|
7
|
+
export interface Plane {
|
|
8
|
+
width: number;
|
|
9
|
+
height: number;
|
|
10
|
+
x: [number, number];
|
|
11
|
+
y: [number, number];
|
|
12
|
+
/** px per world unit, horizontally and vertically */
|
|
13
|
+
unitX: number;
|
|
14
|
+
unitY: number;
|
|
15
|
+
/** Line widths and text sizes are given at a 600 px figure; this scales them */
|
|
16
|
+
scale: number;
|
|
17
|
+
toScreen(p: Vec2): Vec2;
|
|
18
|
+
toWorld(p: Vec2): Vec2;
|
|
19
|
+
}
|
|
20
|
+
export declare function planeOf(spec: Pick<MathFigureSpec, 'width' | 'height' | 'x' | 'y'>): Plane;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @algorisys/tinyfly/math: maths teaching animations.
|
|
3
|
+
*
|
|
4
|
+
* A figure is JSON (a coordinate plane and items: grids, axes, vectors,
|
|
5
|
+
* functions, polygons, equations); its values (numbers, vectors, matrices)
|
|
6
|
+
* and its items' draw / opacity / step are animated by ordinary timeline
|
|
7
|
+
* tracks. One pure layout step turns figure + values into shapes, drawn on a
|
|
8
|
+
* canvas (videos, Workers, live demos) or as SVG (lesson embeds, static
|
|
9
|
+
* export), so every output shows the same picture.
|
|
10
|
+
*/
|
|
11
|
+
export * as vec2 from './linear/vec2';
|
|
12
|
+
export * as mat2 from './linear/mat2';
|
|
13
|
+
export type { Vec2 } from './linear/vec2';
|
|
14
|
+
export type { Mat2, Eigenpair } from './linear/mat2';
|
|
15
|
+
export { parseExpression, expressionNames, ExpressionError, FUNCTIONS, CONSTANTS, type Expression as MathExpression } from './expr/parse-expression';
|
|
16
|
+
export { evaluate, compile, compileDerivative, type Scope } from './expr/evaluate';
|
|
17
|
+
export { differentiate, nthDerivative, expressionToString } from './expr/differentiate';
|
|
18
|
+
export { integrate } from './figure/derived';
|
|
19
|
+
export { density, meanOf, sdOf, supportOf, sample, samples, isDiscrete, distributionProblem, DISTRIBUTION_KINDS, type Distribution, type DistributionKind } from './prob/distributions';
|
|
20
|
+
export { binSamples, defaultBins, summary, type Bin } from './prob/histogram';
|
|
21
|
+
export { scatterPoints, leastSquares, type PointsSpec } from './prob/scatter';
|
|
22
|
+
export { ballPath, ballAt, binOf } from './prob/galton';
|
|
23
|
+
export { bayesAreas, type BayesAreas } from './prob/bayes';
|
|
24
|
+
export { parseTex, TexError, TEX_COMMANDS, SYMBOLS, type TexNode, type TexClass } from './tex/tex-parser';
|
|
25
|
+
export { layoutTex, type TexLayout, type TexPiece, type TexGlyph, type TexRule, type TexStroke, type TexLayoutOptions } from './tex/tex-layout';
|
|
26
|
+
export { morphEquation, type MorphPiece, type PlacedLayout } from './tex/equation-morph';
|
|
27
|
+
export { MATH_FONT } from './tex/glyph-metrics';
|
|
28
|
+
export * from './figure/figure-spec';
|
|
29
|
+
export { validateMathFigure } from './figure/validate-spec';
|
|
30
|
+
export { layoutFigure, type MathProps } from './figure/layout-figure';
|
|
31
|
+
export { planeOf, type Plane } from './figure/plane';
|
|
32
|
+
export { texToShapes, trimPolylines, type DisplayList, type Shape, type TexPlacement } from './figure/display-list';
|
|
33
|
+
export { drawDisplayList } from './render/draw-display-list';
|
|
34
|
+
export { displayListToSvg, displayListToSvgContent } from './render/display-list-to-svg';
|
|
35
|
+
export { mathTarget, mathFigureProps, type MathCanvasTarget } from './targets/math-target';
|
|
36
|
+
export { mathSteps, type MathSteps } from './steps/math-steps';
|
|
37
|
+
export { mountMath, mountAllMath, type MountedMathFigure } from './embed/mount-math';
|
|
38
|
+
export { mathExplorable, type MathExplorable, type ExplorableOptions, type SliderControl, type DragHandle } from './embed/explorable';
|
|
39
|
+
export { linearMapFigure, determinantFigure, eigenFigure, vectorSumFigure, spanFigure, MATRIX_TEX, type PresetOptions, } from './presets/linear-algebra';
|
|
40
|
+
export { tangentFigure, secantFigure, riemannFigure, areaFigure, taylorFigure, limitFigure, type CalculusOptions } from './presets/calculus';
|
|
41
|
+
export { normalFigure, binomialFigure, cltFigure, galtonFigure, monteCarloPiFigure, regressionFigure, bayesFigure, type ProbabilityOptions } from './presets/probability';
|
|
42
|
+
import type { MathFigureSpec } from './figure/figure-spec';
|
|
43
|
+
import { type MathProps } from './figure/layout-figure';
|
|
44
|
+
/** A figure checked up front: returns it unchanged, or throws a message that says how to fix it. */
|
|
45
|
+
export declare function mathFigure(spec: MathFigureSpec): MathFigureSpec;
|
|
46
|
+
/** A figure at some values as a standalone SVG document (for static pages, RSS, email, print). */
|
|
47
|
+
export declare function mathFigureToSvg(spec: MathFigureSpec, props?: MathProps, options?: {
|
|
48
|
+
title?: string;
|
|
49
|
+
}): string;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { Vec2 } from './vec2';
|
|
2
|
+
/**
|
|
3
|
+
* 2×2 matrices as `[a, b, c, d]`, row by row:
|
|
4
|
+
*
|
|
5
|
+
* | a b |
|
|
6
|
+
* | c d |
|
|
7
|
+
*
|
|
8
|
+
* so the first column `[a, c]` is where î lands and the second `[b, d]` is
|
|
9
|
+
* where ĵ lands. A flat number array is what a timeline track interpolates,
|
|
10
|
+
* element by element: keying `[1, 0, 0, 1]` to a matrix M moves every point
|
|
11
|
+
* in a straight line from where it starts to where M sends it, which is the
|
|
12
|
+
* classic "watch the grid transform" animation. Every function is pure.
|
|
13
|
+
*/
|
|
14
|
+
export type Mat2 = [number, number, number, number];
|
|
15
|
+
export declare const IDENTITY: Mat2;
|
|
16
|
+
/** M·v */
|
|
17
|
+
export declare const apply: ([a, b, c, d]: Mat2, [x, y]: Vec2) => Vec2;
|
|
18
|
+
/** A·B: apply B first, then A. */
|
|
19
|
+
export declare function multiply(A: Mat2, B: Mat2): Mat2;
|
|
20
|
+
/** The signed factor areas are scaled by; negative when orientation flips. */
|
|
21
|
+
export declare const determinant: ([a, b, c, d]: Mat2) => number;
|
|
22
|
+
/** The inverse, or undefined when the matrix squashes the plane (determinant 0). */
|
|
23
|
+
export declare function inverse(m: Mat2): Mat2 | undefined;
|
|
24
|
+
/** Element-wise blend from `from` (t = 0) to `to` (t = 1). */
|
|
25
|
+
export declare const lerp: (from: Mat2, to: Mat2, t: number) => Mat2;
|
|
26
|
+
/** A rotation by `radians`, counter-clockwise in maths axes (y up). */
|
|
27
|
+
export declare const rotation: (radians: number) => Mat2;
|
|
28
|
+
export interface Eigenpair {
|
|
29
|
+
value: number;
|
|
30
|
+
/** Unit length */
|
|
31
|
+
vector: Vec2;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The real eigenvalues and unit eigenvectors, largest value first. Empty
|
|
35
|
+
* when the eigenvalues are complex (a rotation turns every direction). A
|
|
36
|
+
* repeated eigenvalue gives one pair, or two when every vector is an
|
|
37
|
+
* eigenvector (a uniform scale).
|
|
38
|
+
*/
|
|
39
|
+
export declare function eigen(m: Mat2): Eigenpair[];
|
|
40
|
+
/** True when `value` is four finite numbers. */
|
|
41
|
+
export declare function isMat2(value: unknown): value is Mat2;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 2D vectors as plain `[x, y]` tuples: the same shape a timeline track
|
|
3
|
+
* interpolates element by element, so a vector can be keyed directly.
|
|
4
|
+
* Every function is pure and returns a new tuple.
|
|
5
|
+
*/
|
|
6
|
+
export type Vec2 = [number, number];
|
|
7
|
+
export declare const add: (a: Vec2, b: Vec2) => Vec2;
|
|
8
|
+
export declare const sub: (a: Vec2, b: Vec2) => Vec2;
|
|
9
|
+
export declare const scale: (a: Vec2, k: number) => Vec2;
|
|
10
|
+
export declare const dot: (a: Vec2, b: Vec2) => number;
|
|
11
|
+
/** The z of the 3D cross product: the signed area of the parallelogram on a and b. */
|
|
12
|
+
export declare const cross: (a: Vec2, b: Vec2) => number;
|
|
13
|
+
export declare const length: (a: Vec2) => number;
|
|
14
|
+
/** `a` scaled to length 1; the zero vector stays zero. */
|
|
15
|
+
export declare function normalize(a: Vec2): Vec2;
|
|
16
|
+
export declare const lerp: (a: Vec2, b: Vec2, t: number) => Vec2;
|
|
17
|
+
/** True when `value` is a finite `[x, y]`. */
|
|
18
|
+
export declare function isVec2(value: unknown): value is Vec2;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { MathFigureSpec, MathThemeName } from '../figure/figure-spec';
|
|
2
|
+
/**
|
|
3
|
+
* Ready-made calculus figures. Each returns a plain MathFigureSpec whose
|
|
4
|
+
* moving parts are number values, so a timeline (or a slider) slides the
|
|
5
|
+
* point, shrinks the secant, adds rectangles or Taylor terms:
|
|
6
|
+
*
|
|
7
|
+
* - tangentFigure: value `a`, the point of tangency
|
|
8
|
+
* - secantFigure: values `a` and `h`; key `h` toward 0 to watch the secant become the tangent
|
|
9
|
+
* - riemannFigure: value `n`, the number of rectangles
|
|
10
|
+
* - areaFigure: value `b`, the right end of the shaded area
|
|
11
|
+
* - taylorFigure: value `order`, the degree (fractional values grow the next term in)
|
|
12
|
+
* - limitFigure: value `p`, a point sliding along the curve toward the hole at `at`
|
|
13
|
+
*/
|
|
14
|
+
export interface CalculusOptions {
|
|
15
|
+
width?: number;
|
|
16
|
+
height?: number;
|
|
17
|
+
theme?: MathThemeName;
|
|
18
|
+
/** World x range (default [-5, 5]) */
|
|
19
|
+
x?: [number, number];
|
|
20
|
+
/** World y range (default: centred on 0 with square units) */
|
|
21
|
+
y?: [number, number];
|
|
22
|
+
/** The function, an expression in x */
|
|
23
|
+
expr: string;
|
|
24
|
+
}
|
|
25
|
+
/** The tangent at x = a, with its slope shown. */
|
|
26
|
+
export declare function tangentFigure(options: CalculusOptions & {
|
|
27
|
+
at?: number;
|
|
28
|
+
}): MathFigureSpec;
|
|
29
|
+
/** The secant through x = a and x = a + h; shrink `h` to 0 and it becomes the tangent. */
|
|
30
|
+
export declare function secantFigure(options: CalculusOptions & {
|
|
31
|
+
at?: number;
|
|
32
|
+
h?: number;
|
|
33
|
+
}): MathFigureSpec;
|
|
34
|
+
/** n rectangles under the curve from `from` to `to`, their total beside the exact area. */
|
|
35
|
+
export declare function riemannFigure(options: CalculusOptions & {
|
|
36
|
+
from: number;
|
|
37
|
+
to: number;
|
|
38
|
+
n?: number;
|
|
39
|
+
rule?: 'left' | 'right' | 'midpoint' | 'trapezoid';
|
|
40
|
+
}): MathFigureSpec;
|
|
41
|
+
/** The area under the curve from `from` to b, growing as `b` moves. */
|
|
42
|
+
export declare function areaFigure(options: CalculusOptions & {
|
|
43
|
+
from: number;
|
|
44
|
+
to: number;
|
|
45
|
+
}): MathFigureSpec;
|
|
46
|
+
/** Taylor polynomials of the function about x = at, degree `order`. */
|
|
47
|
+
export declare function taylorFigure(options: CalculusOptions & {
|
|
48
|
+
at?: number;
|
|
49
|
+
order?: number;
|
|
50
|
+
}): MathFigureSpec;
|
|
51
|
+
/**
|
|
52
|
+
* A limit: a point sliding along the curve toward x = at, where the function
|
|
53
|
+
* has a hole (pass `limit`, the value it approaches, to draw the hole there).
|
|
54
|
+
*/
|
|
55
|
+
export declare function limitFigure(options: CalculusOptions & {
|
|
56
|
+
at: number;
|
|
57
|
+
limit: number;
|
|
58
|
+
from?: number;
|
|
59
|
+
}): MathFigureSpec;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { type Mat2 } from '../linear/mat2';
|
|
2
|
+
import type { Vec2 } from '../linear/vec2';
|
|
3
|
+
import type { MathFigureSpec, MathThemeName } from '../figure/figure-spec';
|
|
4
|
+
/**
|
|
5
|
+
* Ready-made linear algebra figures. Each returns a plain MathFigureSpec, so
|
|
6
|
+
* it can be changed, saved as JSON or extended with more items. The matrix is
|
|
7
|
+
* the value `M`: start it at the identity and key it to a matrix to watch the
|
|
8
|
+
* plane transform (see steps/math-lesson.ts `applyMatrix`).
|
|
9
|
+
*/
|
|
10
|
+
export interface PresetOptions {
|
|
11
|
+
width?: number;
|
|
12
|
+
height?: number;
|
|
13
|
+
theme?: MathThemeName;
|
|
14
|
+
/** World x range (default [-5, 5]) */
|
|
15
|
+
x?: [number, number];
|
|
16
|
+
/** Starting matrix (default the identity) */
|
|
17
|
+
matrix?: Mat2;
|
|
18
|
+
}
|
|
19
|
+
/** TeX for the live matrix `M`, as `[a b; c d]` with its current entries. */
|
|
20
|
+
export declare const MATRIX_TEX = "\\begin{bmatrix} \\val{M.a} & \\val{M.b} \\\\ \\val{M.c} & \\val{M.d} \\end{bmatrix}";
|
|
21
|
+
/** The grid, î and ĵ carried by `M`, with the unit square and the matrix shown live. */
|
|
22
|
+
export declare function linearMapFigure(options?: PresetOptions): MathFigureSpec;
|
|
23
|
+
/** The unit square becomes a parallelogram whose area is the determinant. */
|
|
24
|
+
export declare function determinantFigure(options?: PresetOptions): MathFigureSpec;
|
|
25
|
+
/**
|
|
26
|
+
* Eigenvectors: the lines that stay put. Set `matrix` to the target, start
|
|
27
|
+
* `M` at the identity and key it there; the eigenlines are those of the
|
|
28
|
+
* target (value `A`), so they are visible before the plane moves.
|
|
29
|
+
*/
|
|
30
|
+
export declare function eigenFigure(options: PresetOptions & {
|
|
31
|
+
matrix: Mat2;
|
|
32
|
+
}): MathFigureSpec;
|
|
33
|
+
/** u + v, tip to tail, with the sum. */
|
|
34
|
+
export declare function vectorSumFigure(options: PresetOptions & {
|
|
35
|
+
u: Vec2;
|
|
36
|
+
v: Vec2;
|
|
37
|
+
}): MathFigureSpec;
|
|
38
|
+
/** Every combination a u + b v: two vectors and the line or plane they span. */
|
|
39
|
+
export declare function spanFigure(options: PresetOptions & {
|
|
40
|
+
u: Vec2;
|
|
41
|
+
v?: Vec2;
|
|
42
|
+
}): MathFigureSpec;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { DistributionRef, MathFigureSpec, MathThemeName } from '../figure/figure-spec';
|
|
2
|
+
/**
|
|
3
|
+
* Ready-made probability and statistics figures. Each returns a plain
|
|
4
|
+
* MathFigureSpec whose moving parts are number values:
|
|
5
|
+
*
|
|
6
|
+
* - normalFigure: `mu`, `sigma`, and the shaded range `a`..`b`
|
|
7
|
+
* - binomialFigure: `n` and `p`
|
|
8
|
+
* - cltFigure: `k` (how many draws are averaged) and `samples`
|
|
9
|
+
* - galtonFigure: `balls` (fractional: the next ball is on its way down)
|
|
10
|
+
* - monteCarloPiFigure: `count` points
|
|
11
|
+
* - regressionFigure: `count`, `slope`, `noise`
|
|
12
|
+
* - bayesFigure: `prior`, `sensitivity`, `falsePositive`
|
|
13
|
+
*/
|
|
14
|
+
export interface ProbabilityOptions {
|
|
15
|
+
width?: number;
|
|
16
|
+
height?: number;
|
|
17
|
+
theme?: MathThemeName;
|
|
18
|
+
x?: [number, number];
|
|
19
|
+
y?: [number, number];
|
|
20
|
+
}
|
|
21
|
+
/** The normal curve, with P(a ≤ X ≤ b) shaded. */
|
|
22
|
+
export declare function normalFigure(options?: ProbabilityOptions & {
|
|
23
|
+
mean?: number;
|
|
24
|
+
sd?: number;
|
|
25
|
+
shade?: [number, number];
|
|
26
|
+
}): MathFigureSpec;
|
|
27
|
+
/** Binomial(n, p) as bars. */
|
|
28
|
+
export declare function binomialFigure(options?: ProbabilityOptions & {
|
|
29
|
+
n?: number;
|
|
30
|
+
p?: number;
|
|
31
|
+
}): MathFigureSpec;
|
|
32
|
+
/**
|
|
33
|
+
* The central limit theorem: the mean of k draws from a skewed distribution
|
|
34
|
+
* (exponential by default) piles up into a normal curve as k grows.
|
|
35
|
+
*/
|
|
36
|
+
export declare function cltFigure(options?: ProbabilityOptions & {
|
|
37
|
+
of?: DistributionRef;
|
|
38
|
+
k?: number;
|
|
39
|
+
samples?: number;
|
|
40
|
+
}): MathFigureSpec;
|
|
41
|
+
/** A Galton board in a tall frame: balls bounce through the pegs into a bell. */
|
|
42
|
+
export declare function galtonFigure(options?: ProbabilityOptions & {
|
|
43
|
+
rows?: number;
|
|
44
|
+
balls?: number;
|
|
45
|
+
}): MathFigureSpec;
|
|
46
|
+
/** Estimate π by throwing points at a square and counting those inside the circle. */
|
|
47
|
+
export declare function monteCarloPiFigure(options?: ProbabilityOptions & {
|
|
48
|
+
count?: number;
|
|
49
|
+
}): MathFigureSpec;
|
|
50
|
+
/** Noisy points about a line, and the least-squares line through them. */
|
|
51
|
+
export declare function regressionFigure(options?: ProbabilityOptions & {
|
|
52
|
+
slope?: number;
|
|
53
|
+
intercept?: number;
|
|
54
|
+
noise?: number;
|
|
55
|
+
count?: number;
|
|
56
|
+
}): MathFigureSpec;
|
|
57
|
+
/** A medical test as areas: how likely is the condition, given a positive result? */
|
|
58
|
+
export declare function bayesFigure(options?: ProbabilityOptions & {
|
|
59
|
+
prior?: number;
|
|
60
|
+
sensitivity?: number;
|
|
61
|
+
falsePositive?: number;
|
|
62
|
+
}): MathFigureSpec;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bayes' rule as areas of a unit square: the prior splits it into a column
|
|
3
|
+
* with the condition and one without; each column splits into those who test
|
|
4
|
+
* positive and those who do not.
|
|
5
|
+
*/
|
|
6
|
+
export interface BayesAreas {
|
|
7
|
+
/** P(condition and positive) */
|
|
8
|
+
truePositive: number;
|
|
9
|
+
/** P(no condition and positive) */
|
|
10
|
+
falsePositive: number;
|
|
11
|
+
/** P(positive) */
|
|
12
|
+
positive: number;
|
|
13
|
+
/** P(condition | positive) */
|
|
14
|
+
posterior: number;
|
|
15
|
+
}
|
|
16
|
+
export declare function bayesAreas(prior: number, sensitivity: number, falsePositiveRate: number): BayesAreas;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { type RandomSource } from '../../engine/authoring/random';
|
|
2
|
+
/**
|
|
3
|
+
* Probability distributions as plain data, `{ kind, …parameters }`, with
|
|
4
|
+
* their density (or mass), mean, spread and a seeded sampler. Discrete ones
|
|
5
|
+
* take whole-number values. Pure: samples come from a seed, never from
|
|
6
|
+
* Math.random, so a histogram fills the same way every time it plays.
|
|
7
|
+
*/
|
|
8
|
+
export type Distribution = {
|
|
9
|
+
kind: 'normal';
|
|
10
|
+
mean?: number;
|
|
11
|
+
sd?: number;
|
|
12
|
+
} | {
|
|
13
|
+
kind: 'uniform';
|
|
14
|
+
min?: number;
|
|
15
|
+
max?: number;
|
|
16
|
+
} | {
|
|
17
|
+
kind: 'exponential';
|
|
18
|
+
rate?: number;
|
|
19
|
+
} | {
|
|
20
|
+
kind: 'binomial';
|
|
21
|
+
n: number;
|
|
22
|
+
p: number;
|
|
23
|
+
} | {
|
|
24
|
+
kind: 'poisson';
|
|
25
|
+
rate: number;
|
|
26
|
+
}
|
|
27
|
+
/** The number of heads in `count` fair coin tosses is binomial; this is the total of `count` fair dice */
|
|
28
|
+
| {
|
|
29
|
+
kind: 'dice';
|
|
30
|
+
count?: number;
|
|
31
|
+
sides?: number;
|
|
32
|
+
}
|
|
33
|
+
/** The mean of `count` draws of another distribution: the central limit theorem's subject */
|
|
34
|
+
| {
|
|
35
|
+
kind: 'mean';
|
|
36
|
+
of: Distribution;
|
|
37
|
+
count: number;
|
|
38
|
+
};
|
|
39
|
+
export type DistributionKind = Distribution['kind'];
|
|
40
|
+
export declare const DISTRIBUTION_KINDS: readonly DistributionKind[];
|
|
41
|
+
/** The whole numbers a discrete distribution can take, [low, high] (high may be Infinity). */
|
|
42
|
+
export declare function supportOf(d: Distribution): [number, number];
|
|
43
|
+
/** True for distributions on whole numbers (drawn as bars, not a curve). */
|
|
44
|
+
export declare function isDiscrete(d: Distribution): boolean;
|
|
45
|
+
/** The density (continuous) or probability mass (discrete) at x. */
|
|
46
|
+
export declare function density(d: Distribution, x: number): number;
|
|
47
|
+
export declare function meanOf(d: Distribution): number;
|
|
48
|
+
export declare function sdOf(d: Distribution): number;
|
|
49
|
+
/** One draw. */
|
|
50
|
+
export declare function sample(d: Distribution, source: RandomSource): number;
|
|
51
|
+
/**
|
|
52
|
+
* The first `count` draws from `d` with `seed`. Asking for more continues
|
|
53
|
+
* the same sequence, so a histogram that grows from 10 to 1000 samples keeps
|
|
54
|
+
* its first 10. Cached by distribution and seed.
|
|
55
|
+
*/
|
|
56
|
+
export declare function samples(d: Distribution, seed: number | string, count: number): number[];
|
|
57
|
+
/** Checks a distribution's parameters; returns a message, or undefined when it is fine. */
|
|
58
|
+
export declare function distributionProblem(d: Distribution): string | undefined;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A Galton board's balls: each bounces left or right at each row of pegs, by
|
|
3
|
+
* its own seeded coin flips, and lands in the bin numbered by its rights.
|
|
4
|
+
* Pure geometry in "peg units": row i of pegs sits at depth i, its pegs at
|
|
5
|
+
* x = j − i / 2 (j = 0..i); bin b sits at x = b − rows / 2.
|
|
6
|
+
*/
|
|
7
|
+
export declare function ballPath(seed: number | string, ball: number, rows: number): boolean[];
|
|
8
|
+
/** The bin a ball lands in: how many times it went right. */
|
|
9
|
+
export declare const binOf: (path: boolean[]) => number;
|
|
10
|
+
/**
|
|
11
|
+
* Where a ball is, part way down (`progress` 0 at the top peg, 1 when it
|
|
12
|
+
* reaches the bins), in peg units: x across, depth down. Between rows it
|
|
13
|
+
* hops in a small arc, as balls on a real board do.
|
|
14
|
+
*/
|
|
15
|
+
export declare function ballAt(path: boolean[], progress: number): {
|
|
16
|
+
x: number;
|
|
17
|
+
depth: number;
|
|
18
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type Distribution } from './distributions';
|
|
2
|
+
export interface Bin {
|
|
3
|
+
left: number;
|
|
4
|
+
right: number;
|
|
5
|
+
count: number;
|
|
6
|
+
/** count, or count / (total × width) when scaled as a density */
|
|
7
|
+
height: number;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* The default bins for a distribution: one per whole number for discrete
|
|
11
|
+
* ones (centred on it), else 30 across mean ± 4 sd.
|
|
12
|
+
*/
|
|
13
|
+
export declare function defaultBins(d: Distribution): [number, number, number];
|
|
14
|
+
/** Samples counted into `bins`; heights as counts or as a density (total area 1). */
|
|
15
|
+
export declare function binSamples(values: number[], [from, to, count]: [number, number, number], scale: 'density' | 'count'): Bin[];
|
|
16
|
+
/** Sample mean and standard deviation (0 and 0 for no samples). */
|
|
17
|
+
export declare function summary(values: number[]): {
|
|
18
|
+
mean: number;
|
|
19
|
+
sd: number;
|
|
20
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Vec2 } from '../linear/vec2';
|
|
2
|
+
export type PointsSpec = {
|
|
3
|
+
kind: 'uniform';
|
|
4
|
+
x: [number, number];
|
|
5
|
+
y: [number, number];
|
|
6
|
+
} | {
|
|
7
|
+
kind: 'linear';
|
|
8
|
+
slope: number;
|
|
9
|
+
intercept: number;
|
|
10
|
+
noise: number;
|
|
11
|
+
x: [number, number];
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* `count` random points from a seed. Each point draws the same random numbers
|
|
15
|
+
* whatever the parameters, so moving the slope or the noise moves every
|
|
16
|
+
* point smoothly instead of reshuffling them.
|
|
17
|
+
*/
|
|
18
|
+
export declare function scatterPoints(spec: PointsSpec, seed: number | string, count: number): Vec2[];
|
|
19
|
+
/** The least-squares line through points, and the correlation r. Undefined for fewer than two distinct x. */
|
|
20
|
+
export declare function leastSquares(points: Vec2[]): {
|
|
21
|
+
slope: number;
|
|
22
|
+
intercept: number;
|
|
23
|
+
r: number;
|
|
24
|
+
} | undefined;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Vec2 } from '../linear/vec2';
|
|
2
|
+
/**
|
|
3
|
+
* An arrow's geometry: the shaft (stopping where the head begins, so the line
|
|
4
|
+
* never pokes through the point) and the head's three corners. Pure; both
|
|
5
|
+
* renderers draw from it.
|
|
6
|
+
*/
|
|
7
|
+
export declare function arrowGeometry(points: Vec2[], head: number): {
|
|
8
|
+
shaft: Vec2[];
|
|
9
|
+
head: [Vec2, Vec2, Vec2] | undefined;
|
|
10
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { DisplayList } from '../figure/display-list';
|
|
2
|
+
/** The inner markup of a display list (what goes inside its `<svg>`). */
|
|
3
|
+
export declare function displayListToSvgContent(list: DisplayList): string;
|
|
4
|
+
/**
|
|
5
|
+
* A display list as a standalone SVG document: for embedding, static export,
|
|
6
|
+
* RSS, email and print. `title` becomes the figure's accessible name.
|
|
7
|
+
*/
|
|
8
|
+
export declare function displayListToSvg(list: DisplayList, options?: {
|
|
9
|
+
title?: string;
|
|
10
|
+
}): string;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { DisplayList } from '../figure/display-list';
|
|
2
|
+
/**
|
|
3
|
+
* Draws a display list on a Canvas 2D context (a page, an OffscreenCanvas in a
|
|
4
|
+
* Worker, or @napi-rs/canvas in Node), clipped to the figure's box at 0, 0.
|
|
5
|
+
*/
|
|
6
|
+
export declare function drawDisplayList(ctx: CanvasRenderingContext2D, list: DisplayList): void;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { AnimatableValue } from '../../engine/types';
|
|
2
|
+
import type { LessonBuilder, StepOptions } from '../../teach/lesson';
|
|
3
|
+
import type { MathFigureSpec } from '../figure/figure-spec';
|
|
4
|
+
/**
|
|
5
|
+
* Lesson steps for a maths figure, written on a `lesson()` builder from
|
|
6
|
+
* `@algorisys/tinyfly/teach`. Every helper writes ordinary tracks on the
|
|
7
|
+
* figure's target, so the result is plain timeline JSON with markers and
|
|
8
|
+
* captions, playable in the embed player, a video or the editor.
|
|
9
|
+
*
|
|
10
|
+
* const l = lesson({ id: 'shear' })
|
|
11
|
+
* const m = mathSteps(l, 'fig', figure, { hidden: ['basis'] })
|
|
12
|
+
* m.drawOn('basis')
|
|
13
|
+
* l.marker('basis', { label: 'î and ĵ', pause: true })
|
|
14
|
+
* m.applyMatrix([1, 1, 0, 1])
|
|
15
|
+
* l.marker('sheared', { label: 'A shear keeps î and moves ĵ', pause: true })
|
|
16
|
+
*/
|
|
17
|
+
export interface MathSteps {
|
|
18
|
+
/** Animate a value (a number, vector or matrix) to `value` */
|
|
19
|
+
toValue(name: string, value: AnimatableValue, options?: StepOptions): MathSteps;
|
|
20
|
+
/** Move the matrix value (default `M`) to `matrix`: the plane transforms */
|
|
21
|
+
applyMatrix(matrix: number[], options?: StepOptions & {
|
|
22
|
+
name?: string;
|
|
23
|
+
}): MathSteps;
|
|
24
|
+
/** Draw an item on, like a pen (default 900 ms) */
|
|
25
|
+
drawOn(itemId: string, options?: StepOptions): MathSteps;
|
|
26
|
+
/** Fade an item in or out */
|
|
27
|
+
fadeIn(itemId: string, options?: StepOptions): MathSteps;
|
|
28
|
+
fadeOut(itemId: string, options?: StepOptions): MathSteps;
|
|
29
|
+
/** Move an equation to step `step`, its terms gliding (default 900 ms) */
|
|
30
|
+
showStep(itemId: string, step: number, options?: StepOptions): MathSteps;
|
|
31
|
+
}
|
|
32
|
+
export declare function mathSteps(l: LessonBuilder, target: string, figure: MathFigureSpec, options?: {
|
|
33
|
+
hidden?: string[];
|
|
34
|
+
}): MathSteps;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { AnimatableValue } from '../../engine/types';
|
|
2
|
+
import type { MathFigureSpec } from '../figure/figure-spec';
|
|
3
|
+
/**
|
|
4
|
+
* A maths figure as a canvas target: register it with a CanvasAdapter (or a
|
|
5
|
+
* headless video scene's `targets`) and a timeline animates its values and
|
|
6
|
+
* item props as plain tracks:
|
|
7
|
+
*
|
|
8
|
+
* { target: 'fig', property: 'M', keyframes: [{ time: 0, value: [1,0,0,1] }, { time: 1500, value: [2,1,0,1] }] }
|
|
9
|
+
* { target: 'fig', property: 'grid.draw', keyframes: … }
|
|
10
|
+
*
|
|
11
|
+
* It has the shape of the canvas adapter's custom target (`type: 'custom'`)
|
|
12
|
+
* without importing it, so the maths module stays independent of adapters.
|
|
13
|
+
*/
|
|
14
|
+
export interface MathCanvasTarget {
|
|
15
|
+
type: 'custom';
|
|
16
|
+
x: number;
|
|
17
|
+
y: number;
|
|
18
|
+
width: number;
|
|
19
|
+
height: number;
|
|
20
|
+
props: Record<string, AnimatableValue>;
|
|
21
|
+
acceptsProp(property: string): boolean;
|
|
22
|
+
draw(ctx: CanvasRenderingContext2D, target: {
|
|
23
|
+
props?: Record<string, AnimatableValue>;
|
|
24
|
+
}): void;
|
|
25
|
+
about: {
|
|
26
|
+
kind: string;
|
|
27
|
+
summary: string;
|
|
28
|
+
props: Record<string, {
|
|
29
|
+
description: string;
|
|
30
|
+
kind?: 'number' | 'list';
|
|
31
|
+
unit?: string;
|
|
32
|
+
min?: number;
|
|
33
|
+
max?: number;
|
|
34
|
+
}>;
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
/** Every prop a figure accepts: its values by name, and each item's draw, opacity (and an equation's step). */
|
|
38
|
+
export declare function mathFigureProps(spec: MathFigureSpec): MathCanvasTarget['about']['props'];
|
|
39
|
+
/** A canvas target that draws `spec`, placed at x, y (its top left corner). */
|
|
40
|
+
export declare function mathTarget(spec: MathFigureSpec, place?: {
|
|
41
|
+
x?: number;
|
|
42
|
+
y?: number;
|
|
43
|
+
}): MathCanvasTarget;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { TexLayout, TexPiece } from './tex-layout';
|
|
2
|
+
/**
|
|
3
|
+
* One frame of a change from one typeset equation to the next, the way a
|
|
4
|
+
* teacher rewrites a line: terms that appear in both glide from their old
|
|
5
|
+
* place to their new one, terms that go fade out, terms that arrive fade in.
|
|
6
|
+
*
|
|
7
|
+
* Pieces pair by id (see tex-layout.ts: `x#1`, `=#1`, or an explicit
|
|
8
|
+
* `\id{name}`), in order within an id. A pair whose glyphs differ (a value
|
|
9
|
+
* that changed) cross-fades in place of gliding. Pure.
|
|
10
|
+
*/
|
|
11
|
+
export type MorphPiece = TexPiece & {
|
|
12
|
+
opacity: number;
|
|
13
|
+
};
|
|
14
|
+
export interface PlacedLayout {
|
|
15
|
+
layout: TexLayout;
|
|
16
|
+
/** Where the layout's origin (left edge, baseline) sits, in the same units as the layout */
|
|
17
|
+
x: number;
|
|
18
|
+
y: number;
|
|
19
|
+
}
|
|
20
|
+
/** The pieces of the change at `progress` 0 (all `from`) to 1 (all `to`). */
|
|
21
|
+
export declare function morphEquation(from: PlacedLayout, to: PlacedLayout, progress: number): MorphPiece[];
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fixed glyph metrics for typesetting equations, in ems.
|
|
3
|
+
*
|
|
4
|
+
* Equations are laid out from this table, never from a font's measurements,
|
|
5
|
+
* so the same TeX gives the same positions in a browser, a Web Worker and
|
|
6
|
+
* Node (the precedent is the whiteboard's and code panel's fixed cells).
|
|
7
|
+
* Each glyph is drawn centred in its advance, so a font that is a little
|
|
8
|
+
* wider or narrower than the table still sits where the layout put it.
|
|
9
|
+
*
|
|
10
|
+
* The widths approximate Computer Modern / Latin Modern, the faces listed
|
|
11
|
+
* first in MATH_FONT.
|
|
12
|
+
*/
|
|
13
|
+
export declare const MATH_FONT = "\"Latin Modern Math\", \"STIX Two Math\", \"Cambria Math\", \"Times New Roman\", \"Liberation Serif\", \"Noto Serif\", Times, serif";
|
|
14
|
+
/** Height above the baseline of the maths axis (where fraction bars and − sit). */
|
|
15
|
+
export declare const AXIS_HEIGHT = 0.25;
|
|
16
|
+
/** Rule thickness for fraction bars and radicals. */
|
|
17
|
+
export declare const RULE = 0.05;
|
|
18
|
+
/** Advance width of one glyph, in ems at size 1. */
|
|
19
|
+
export declare function advance(text: string): number;
|
|
20
|
+
/** Height above and depth below the baseline, in ems at size 1. */
|
|
21
|
+
export declare function extent(text: string): {
|
|
22
|
+
height: number;
|
|
23
|
+
depth: number;
|
|
24
|
+
};
|