@motionscript/math 0.1.0-alpha.9 → 0.1.1-alpha.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.
Files changed (252) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/dist/annotate/angle-mark.d.ts +4 -14
  3. package/dist/annotate/angle-mark.d.ts.map +1 -1
  4. package/dist/annotate/angle-mark.js +2 -1
  5. package/dist/annotate/angle-mark.js.map +1 -1
  6. package/dist/annotate/curly-brace.d.ts +4 -16
  7. package/dist/annotate/curly-brace.d.ts.map +1 -1
  8. package/dist/annotate/curly-brace.js.map +1 -1
  9. package/dist/annotate/emphasis-mark.d.ts +0 -5
  10. package/dist/annotate/emphasis-mark.d.ts.map +1 -1
  11. package/dist/annotate/emphasis-mark.js.map +1 -1
  12. package/dist/browser/chunks/chunk-FZ2YBTPL.js +2 -0
  13. package/dist/browser/chunks/chunk-FZ2YBTPL.js.map +7 -0
  14. package/dist/browser/chunks/chunk-T42HWKBC.js +2 -0
  15. package/dist/browser/chunks/chunk-T42HWKBC.js.map +7 -0
  16. package/dist/browser/chunks/{mathjax-WV55AC2P.js → mathjax-SOM6RSPV.js} +1 -1
  17. package/dist/browser/chunks/{mathjax-WV55AC2P.js.map → mathjax-SOM6RSPV.js.map} +2 -2
  18. package/dist/browser/index.js +1 -1
  19. package/dist/browser/index.js.map +3 -3
  20. package/dist/browser/kit.js +1 -1
  21. package/dist/browser/manifest.json +1 -1
  22. package/dist/browser/tex.js +1 -1
  23. package/dist/engine.d.ts +3 -4
  24. package/dist/engine.d.ts.map +1 -1
  25. package/dist/engine.js +3 -4
  26. package/dist/engine.js.map +1 -1
  27. package/dist/kit/cache.d.ts +10 -21
  28. package/dist/kit/cache.d.ts.map +1 -1
  29. package/dist/kit/cache.js +33 -24
  30. package/dist/kit/cache.js.map +1 -1
  31. package/dist/kit/component.d.ts +5 -14
  32. package/dist/kit/component.d.ts.map +1 -1
  33. package/dist/kit/component.js +1 -8
  34. package/dist/kit/component.js.map +1 -1
  35. package/dist/kit/coords.d.ts +1 -1
  36. package/dist/kit/coords.d.ts.map +1 -1
  37. package/dist/kit/coords.js +2 -1
  38. package/dist/kit/coords.js.map +1 -1
  39. package/dist/kit/curve.d.ts +1 -5
  40. package/dist/kit/curve.d.ts.map +1 -1
  41. package/dist/kit/curve.js.map +1 -1
  42. package/dist/kit/equation.d.ts +10 -30
  43. package/dist/kit/equation.d.ts.map +1 -1
  44. package/dist/kit/equation.js +9 -44
  45. package/dist/kit/equation.js.map +1 -1
  46. package/dist/kit/equation3d.d.ts +3 -3
  47. package/dist/kit/equation3d.d.ts.map +1 -1
  48. package/dist/kit/equation3d.js +27 -59
  49. package/dist/kit/equation3d.js.map +1 -1
  50. package/dist/kit/expression.d.ts +7 -7
  51. package/dist/kit/expression.d.ts.map +1 -1
  52. package/dist/kit/expression.js +18 -17
  53. package/dist/kit/expression.js.map +1 -1
  54. package/dist/kit/field-mark.d.ts +0 -7
  55. package/dist/kit/field-mark.d.ts.map +1 -1
  56. package/dist/kit/field-mark.js.map +1 -1
  57. package/dist/kit/field.d.ts +2 -2
  58. package/dist/kit/field.d.ts.map +1 -1
  59. package/dist/kit/field.js +11 -31
  60. package/dist/kit/field.js.map +1 -1
  61. package/dist/kit/follow.d.ts +3 -10
  62. package/dist/kit/follow.d.ts.map +1 -1
  63. package/dist/kit/follow.js +5 -11
  64. package/dist/kit/follow.js.map +1 -1
  65. package/dist/kit/furniture.d.ts +2 -1
  66. package/dist/kit/furniture.d.ts.map +1 -1
  67. package/dist/kit/furniture.js +6 -3
  68. package/dist/kit/furniture.js.map +1 -1
  69. package/dist/kit/geometry.d.ts +1 -0
  70. package/dist/kit/geometry.d.ts.map +1 -1
  71. package/dist/kit/geometry.js +3 -0
  72. package/dist/kit/geometry.js.map +1 -1
  73. package/dist/kit/mark.d.ts +4 -10
  74. package/dist/kit/mark.d.ts.map +1 -1
  75. package/dist/kit/mark.js.map +1 -1
  76. package/dist/kit/place.d.ts +3 -5
  77. package/dist/kit/place.d.ts.map +1 -1
  78. package/dist/kit/place.js +3 -5
  79. package/dist/kit/place.js.map +1 -1
  80. package/dist/kit/sampler.d.ts +0 -5
  81. package/dist/kit/sampler.d.ts.map +1 -1
  82. package/dist/kit/sampler.js.map +1 -1
  83. package/dist/kit/system.d.ts +0 -1
  84. package/dist/kit/system.d.ts.map +1 -1
  85. package/dist/kit/system.js +3 -10
  86. package/dist/kit/system.js.map +1 -1
  87. package/dist/marks/curve-area.d.ts +2 -12
  88. package/dist/marks/curve-area.d.ts.map +1 -1
  89. package/dist/marks/curve-area.js +4 -9
  90. package/dist/marks/curve-area.js.map +1 -1
  91. package/dist/marks/drop-lines.d.ts +1 -5
  92. package/dist/marks/drop-lines.d.ts.map +1 -1
  93. package/dist/marks/drop-lines.js.map +1 -1
  94. package/dist/marks/plot-point.d.ts +4 -13
  95. package/dist/marks/plot-point.d.ts.map +1 -1
  96. package/dist/marks/plot-point.js.map +1 -1
  97. package/dist/marks/riemann-sum.d.ts +1 -7
  98. package/dist/marks/riemann-sum.d.ts.map +1 -1
  99. package/dist/marks/riemann-sum.js +2 -2
  100. package/dist/marks/riemann-sum.js.map +1 -1
  101. package/dist/marks/tangent-line.d.ts +2 -12
  102. package/dist/marks/tangent-line.d.ts.map +1 -1
  103. package/dist/marks/tangent-line.js +1 -5
  104. package/dist/marks/tangent-line.js.map +1 -1
  105. package/dist/marks/vector-arrow.d.ts +2 -11
  106. package/dist/marks/vector-arrow.d.ts.map +1 -1
  107. package/dist/marks/vector-arrow.js +3 -6
  108. package/dist/marks/vector-arrow.js.map +1 -1
  109. package/dist/nodes.d.ts +1 -12
  110. package/dist/nodes.d.ts.map +1 -1
  111. package/dist/nodes.js.map +1 -1
  112. package/dist/plots/function-plot.d.ts +1 -6
  113. package/dist/plots/function-plot.d.ts.map +1 -1
  114. package/dist/plots/function-plot.js +1 -5
  115. package/dist/plots/function-plot.js.map +1 -1
  116. package/dist/plots/points-plot.d.ts +1 -6
  117. package/dist/plots/points-plot.d.ts.map +1 -1
  118. package/dist/plots/points-plot.js.map +1 -1
  119. package/dist/plots/stream-lines.d.ts +0 -7
  120. package/dist/plots/stream-lines.d.ts.map +1 -1
  121. package/dist/plots/stream-lines.js.map +1 -1
  122. package/dist/plots/traced-path.d.ts +1 -12
  123. package/dist/plots/traced-path.d.ts.map +1 -1
  124. package/dist/plots/traced-path.js +1 -6
  125. package/dist/plots/traced-path.js.map +1 -1
  126. package/dist/plots/vector-field.d.ts +1 -10
  127. package/dist/plots/vector-field.d.ts.map +1 -1
  128. package/dist/plots/vector-field.js +3 -6
  129. package/dist/plots/vector-field.js.map +1 -1
  130. package/dist/structures/network-graph.d.ts +15 -34
  131. package/dist/structures/network-graph.d.ts.map +1 -1
  132. package/dist/structures/network-graph.js +2 -5
  133. package/dist/structures/network-graph.js.map +1 -1
  134. package/dist/structures/sample-space.d.ts +8 -22
  135. package/dist/structures/sample-space.d.ts.map +1 -1
  136. package/dist/structures/sample-space.js +3 -5
  137. package/dist/structures/sample-space.js.map +1 -1
  138. package/dist/systems/axes2d.d.ts +0 -1
  139. package/dist/systems/axes2d.d.ts.map +1 -1
  140. package/dist/systems/axes2d.js +3 -4
  141. package/dist/systems/axes2d.js.map +1 -1
  142. package/dist/systems/number-line.d.ts +7 -22
  143. package/dist/systems/number-line.d.ts.map +1 -1
  144. package/dist/systems/number-line.js +2 -2
  145. package/dist/systems/number-line.js.map +1 -1
  146. package/dist/systems/number-plane.d.ts +12 -27
  147. package/dist/systems/number-plane.d.ts.map +1 -1
  148. package/dist/systems/number-plane.js +1 -7
  149. package/dist/systems/number-plane.js.map +1 -1
  150. package/dist/systems/polar-plane.d.ts +11 -26
  151. package/dist/systems/polar-plane.d.ts.map +1 -1
  152. package/dist/systems/polar-plane.js +7 -10
  153. package/dist/systems/polar-plane.js.map +1 -1
  154. package/dist/tex/geometry.d.ts +5 -24
  155. package/dist/tex/geometry.d.ts.map +1 -1
  156. package/dist/tex/geometry.js +19 -90
  157. package/dist/tex/geometry.js.map +1 -1
  158. package/dist/tex/mathjax.d.ts +0 -13
  159. package/dist/tex/mathjax.d.ts.map +1 -1
  160. package/dist/tex/mathjax.js +4 -24
  161. package/dist/tex/mathjax.js.map +1 -1
  162. package/dist/tex/matrix.d.ts +1 -10
  163. package/dist/tex/matrix.d.ts.map +1 -1
  164. package/dist/tex/matrix.js +1 -5
  165. package/dist/tex/matrix.js.map +1 -1
  166. package/dist/tex/tween.d.ts +9 -30
  167. package/dist/tex/tween.d.ts.map +1 -1
  168. package/dist/tex/tween.js +19 -67
  169. package/dist/tex/tween.js.map +1 -1
  170. package/dist/tex/typeset.d.ts +2 -23
  171. package/dist/tex/typeset.d.ts.map +1 -1
  172. package/dist/tex/typeset.js +4 -30
  173. package/dist/tex/typeset.js.map +1 -1
  174. package/dist/three/axes3d.d.ts +32 -62
  175. package/dist/three/axes3d.d.ts.map +1 -1
  176. package/dist/three/axes3d.js +5 -13
  177. package/dist/three/axes3d.js.map +1 -1
  178. package/dist/three/curve-plot3d.d.ts +4 -15
  179. package/dist/three/curve-plot3d.d.ts.map +1 -1
  180. package/dist/three/curve-plot3d.js.map +1 -1
  181. package/dist/three/plot-point3d.d.ts +7 -14
  182. package/dist/three/plot-point3d.d.ts.map +1 -1
  183. package/dist/three/plot-point3d.js.map +1 -1
  184. package/dist/three/space.d.ts +3 -8
  185. package/dist/three/space.d.ts.map +1 -1
  186. package/dist/three/space.js +1 -8
  187. package/dist/three/space.js.map +1 -1
  188. package/dist/three/surface-plot3d.d.ts +9 -23
  189. package/dist/three/surface-plot3d.d.ts.map +1 -1
  190. package/dist/three/surface-plot3d.js.map +1 -1
  191. package/dist/three/vector-arrow3d.d.ts +1 -7
  192. package/dist/three/vector-arrow3d.d.ts.map +1 -1
  193. package/dist/three/vector-arrow3d.js.map +1 -1
  194. package/package.json +4 -4
  195. package/src/annotate/angle-mark.ts +4 -14
  196. package/src/annotate/curly-brace.ts +2 -14
  197. package/src/annotate/emphasis-mark.ts +1 -6
  198. package/src/engine.ts +3 -4
  199. package/src/kit/cache.ts +33 -25
  200. package/src/kit/component.ts +5 -19
  201. package/src/kit/coords.ts +2 -2
  202. package/src/kit/curve.ts +1 -5
  203. package/src/kit/equation.ts +12 -50
  204. package/src/kit/equation3d.ts +23 -59
  205. package/src/kit/expression.ts +18 -23
  206. package/src/kit/field-mark.ts +1 -7
  207. package/src/kit/field.ts +10 -26
  208. package/src/kit/follow.ts +5 -13
  209. package/src/kit/furniture.ts +7 -4
  210. package/src/kit/geometry.ts +4 -0
  211. package/src/kit/mark.ts +4 -11
  212. package/src/kit/place.ts +3 -5
  213. package/src/kit/sampler.ts +0 -6
  214. package/src/kit/system.ts +3 -12
  215. package/src/marks/curve-area.ts +3 -16
  216. package/src/marks/drop-lines.ts +0 -6
  217. package/src/marks/plot-point.ts +2 -12
  218. package/src/marks/riemann-sum.ts +2 -10
  219. package/src/marks/tangent-line.ts +1 -13
  220. package/src/marks/vector-arrow.ts +3 -13
  221. package/src/nodes.ts +1 -12
  222. package/src/plots/function-plot.ts +1 -6
  223. package/src/plots/points-plot.ts +0 -7
  224. package/src/plots/stream-lines.ts +1 -8
  225. package/src/plots/traced-path.ts +2 -13
  226. package/src/plots/vector-field.ts +4 -12
  227. package/src/structures/network-graph.ts +6 -25
  228. package/src/structures/sample-space.ts +5 -20
  229. package/src/systems/axes2d.ts +3 -6
  230. package/src/systems/number-line.ts +4 -20
  231. package/src/systems/number-plane.ts +4 -19
  232. package/src/systems/polar-plane.ts +11 -25
  233. package/src/tex/geometry.ts +24 -126
  234. package/src/tex/mathjax.ts +4 -26
  235. package/src/tex/matrix.ts +1 -11
  236. package/src/tex/tween.ts +21 -75
  237. package/src/tex/typeset.ts +4 -37
  238. package/src/three/axes3d.ts +7 -42
  239. package/src/three/curve-plot3d.ts +0 -12
  240. package/src/three/plot-point3d.ts +0 -10
  241. package/src/three/space.ts +2 -11
  242. package/src/three/surface-plot3d.ts +0 -15
  243. package/src/three/vector-arrow3d.ts +0 -8
  244. package/dist/browser/chunks/chunk-IRZCG6PM.js +0 -2
  245. package/dist/browser/chunks/chunk-IRZCG6PM.js.map +0 -7
  246. package/dist/browser/chunks/chunk-WMVW6ZIV.js +0 -2
  247. package/dist/browser/chunks/chunk-WMVW6ZIV.js.map +0 -7
  248. package/dist/kit/withdrawn.d.ts +0 -9
  249. package/dist/kit/withdrawn.d.ts.map +0 -1
  250. package/dist/kit/withdrawn.js +0 -9
  251. package/dist/kit/withdrawn.js.map +0 -1
  252. package/src/kit/withdrawn.ts +0 -8
package/src/kit/coords.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Extents, Vector2 } from "@motionscript/sdk";
1
+ import { lerpNumber, type Extents, type Vector2 } from "@motionscript/sdk";
2
2
 
3
3
  /** A closed span of graph values, low to high. */
4
4
  export type Interval = readonly [number, number];
@@ -119,7 +119,7 @@ export function readMatrix(value: unknown, previous: Matrix2 = IDENTITY_MATRIX):
119
119
  }
120
120
 
121
121
  export function lerpMatrix(from: Matrix2, to: Matrix2, t: number): Matrix2 {
122
- return [0, 1, 2, 3].map((i) => from[i] + (to[i] - from[i]) * t) as unknown as Matrix2;
122
+ return [0, 1, 2, 3].map((i) => lerpNumber(from[i], to[i], t)) as unknown as Matrix2;
123
123
  }
124
124
 
125
125
  export function isIdentityMatrix([a, b, c, d]: Matrix2): boolean {
package/src/kit/curve.ts CHANGED
@@ -3,11 +3,7 @@ import { equationParams, parseEquation } from "./equation";
3
3
  import type { ExpressionScope } from "./expression";
4
4
  import { scopeFor, type PlotSource } from "./source";
5
5
 
6
- /**
7
- * A curve as a mark reads it: what a plot is drawn from, its own `x`/`y`, and
8
- * whether a system laid it out, which is when those are a shift in graph units.
9
- * Data rather than a class, so a copied plot is ridden like the kit's own.
10
- */
6
+ /** A curve as a mark reads it. Data rather than a class, so a copied plot is ridden like the kit's own. */
11
7
  export interface CurveValues {
12
8
  readonly expression: PlotSource;
13
9
  readonly params: ExpressionScope;
@@ -1,26 +1,20 @@
1
1
  import { expressionOps, type ExpressionNode } from "@motionscript/sdk";
2
- import { createBoundedCache } from "./cache";
3
- import { compileExpressionCached, MATH_DIALECT, type CompiledExpression } from "./expression";
2
+ import { createBoundedCache, memoParse } from "./cache";
3
+ import { compilerFor, MATH_DIALECT, type CompiledExpression } from "./expression";
4
4
 
5
5
  /**
6
6
  * A plotted equation, read the way it is written rather than by a `kind` prop:
7
- *
8
- * | Written as | Is |
9
- * |---|---|
10
- * | `sin(x)`, `y = sin(x)` | a function of `x` |
11
- * | `x = y^2` | a function of `y` |
12
- * | `r = 1 + cos(theta)` | polar, in `theta` |
13
- * | `(cos(t), sin(t))` | parametric, in `t` |
14
- * | `x^2 + y^2 = 4` | implicit, in `x` and `y` |
7
+ * `sin(x)` or `y = sin(x)` is a function of `x`, `x = y^2` of `y`,
8
+ * `r = 1 + cos(theta)` polar, `(cos(t), sin(t))` parametric, and
9
+ * `x^2 + y^2 = 4` implicit.
15
10
  */
16
11
  export type Equation =
17
12
  | { kind: "function"; of: "x" | "y"; f: CompiledExpression }
18
13
  | { kind: "polar"; f: CompiledExpression }
19
14
  | { kind: "parametric"; x: CompiledExpression; y: CompiledExpression }
20
- /** Drawn where `f(x, y)` is zero. */
21
15
  | { kind: "implicit"; f: CompiledExpression };
22
16
 
23
- /** The free variable each kind is sampled over, for a host's help text. */
17
+ /** The free variable each kind is sampled over, for a host's help. */
24
18
  export const EQUATION_VARIABLES = {
25
19
  function: ["x"],
26
20
  polar: ["theta"],
@@ -28,31 +22,10 @@ export const EQUATION_VARIABLES = {
28
22
  implicit: ["x", "y"],
29
23
  } as const satisfies Record<Equation["kind"], readonly string[]>;
30
24
 
31
- const parsed = createBoundedCache<Equation | Error>(256);
25
+ /** `source` as an {@link Equation}, or the reason it is not one. */
26
+ export const parseEquation = memoParse<Equation>(256, read);
32
27
 
33
- /**
34
- * `source` as an {@link Equation}, or the reason it is not one. Memoised, so an
35
- * unchanged equation is the identical object and a geometry cache can key on it.
36
- */
37
- export function parseEquation(source: string, params: readonly string[] = []): Equation | Error {
38
- const key = `${params.join(",")}|${source}`;
39
- const hit = parsed.get(key);
40
- if (hit !== undefined) return hit;
41
- let result: Equation | Error;
42
- try {
43
- result = read(source.trim(), params);
44
- } catch (error) {
45
- result = error instanceof Error ? error : new Error(String(error));
46
- }
47
- parsed.set(key, result);
48
- return result;
49
- }
50
-
51
- /**
52
- * Why `source` will not plot, or `null` when it will. An empty one is not an
53
- * error (nobody has written it yet), and neither is a name it does not know:
54
- * that is a named value, see {@link equationParams}.
55
- */
28
+ /** Why `source` will not plot, or `null`. Empty text is no error, and neither is an unknown name: that is a named value ({@link equationParams}). */
56
29
  export function equationError(source: string): string | null {
57
30
  if (source.trim() === "") return null;
58
31
  const equation = parseEquation(source, equationParams(source));
@@ -64,10 +37,7 @@ const VARIABLE_NAMES: ReadonlySet<string> = new Set(["r", ...Object.values(EQUAT
64
37
 
65
38
  const named = createBoundedCache<readonly string[]>(256);
66
39
 
67
- /**
68
- * Every name `source` reads, in the order it first names them, variables
69
- * included. Empty for text that does not parse.
70
- */
40
+ /** Every name `source` reads, in the order first named, variables included; empty for text that does not parse. */
71
41
  export function equationNames(source: string): readonly string[] {
72
42
  const hit = named.get(source);
73
43
  if (hit !== undefined) return hit;
@@ -85,10 +55,7 @@ export function equationNames(source: string): readonly string[] {
85
55
  return result;
86
56
  }
87
57
 
88
- /**
89
- * The named values `source` reads: every name that is not a variable, a
90
- * function or a constant.
91
- */
58
+ /** The named values `source` reads: every name that is not a variable, function or constant. */
92
59
  export function equationParams(source: string): readonly string[] {
93
60
  return equationNames(source).filter((name) => !VARIABLE_NAMES.has(name));
94
61
  }
@@ -113,12 +80,7 @@ function collectNames(node: ExpressionNode, into: Set<string>): void {
113
80
  }
114
81
 
115
82
  function read(source: string, params: readonly string[]): Equation {
116
- if (source === "") throw new Error("Empty equation");
117
- const compile = (text: string, variables: readonly string[]): CompiledExpression => {
118
- const result = compileExpressionCached(text.trim(), variables, params);
119
- if (result instanceof Error) throw result;
120
- return result;
121
- };
83
+ const compile = compilerFor(params);
122
84
 
123
85
  const pair = splitTuple(source);
124
86
  if (pair?.length === 2) return { kind: "parametric", x: compile(pair[0], ["t"]), y: compile(pair[1], ["t"]) };
@@ -1,6 +1,6 @@
1
- import { createBoundedCache } from "./cache";
1
+ import { memoParse } from "./cache";
2
2
  import { equationError, equationNames, equationParams, EQUATION_VARIABLES, splitTopLevel, splitTuple } from "./equation";
3
- import { compileExpressionCached, type CompiledExpression } from "./expression";
3
+ import { compilerFor, type CompiledExpression } from "./expression";
4
4
  import { fieldError, fieldParams, FIELD_VARIABLES } from "./field";
5
5
 
6
6
  /**
@@ -32,9 +32,6 @@ export const EQUATION_VARIABLES_3D = {
32
32
  const SURFACE_NAMES: ReadonlySet<string> = new Set(["x", "y", "z", "u", "v"]);
33
33
  const SPACE_CURVE_NAMES: ReadonlySet<string> = new Set(["t"]);
34
34
 
35
- const surfaces = createBoundedCache<SurfaceEquation | Error>(128);
36
- const curves = createBoundedCache<SpaceCurveEquation | Error>(128);
37
-
38
35
  /** The named values a surface's equation reads: every name that is not one of its variables. */
39
36
  export function surfaceParams(source: string): readonly string[] {
40
37
  return equationNames(source).filter((name) => !SURFACE_NAMES.has(name));
@@ -45,33 +42,29 @@ export function spaceCurveParams(source: string): readonly string[] {
45
42
  return equationNames(source).filter((name) => !SPACE_CURVE_NAMES.has(name));
46
43
  }
47
44
 
48
- /** `source` as a surface, or the reason it is not one. Memoised, so an unchanged equation is the identical object. */
49
- export function parseSurface(source: string, params: readonly string[] = []): SurfaceEquation | Error {
50
- return memo(surfaces, source, params, (text) => {
51
- const compile = compiler(params);
52
- const parts = splitTuple(text);
53
- if (parts) {
54
- if (parts.length !== 3) throw new Error("A parametric surface has three parts: (x, y, z)");
55
- const [x, y, z] = parts.map((part) => compile(part, EQUATION_VARIABLES_3D.surface));
56
- return { kind: "parametric", x, y, z };
57
- }
58
- const sides = splitTopLevel(text, "=");
59
- if (sides.length > 2) throw new Error("An equation has one '='");
60
- if (sides.length === 2 && sides[0].trim() !== "z") throw new Error("A surface is written z = …, or as three parts (x, y, z)");
61
- return { kind: "height", f: compile(sides[sides.length - 1], EQUATION_VARIABLES_3D.height) };
62
- });
63
- }
45
+ /** `source` as a surface, or the reason it is not one. */
46
+ export const parseSurface = memoParse<SurfaceEquation>(128, (text, params) => {
47
+ const compile = compilerFor(params);
48
+ const parts = splitTuple(text);
49
+ if (parts) {
50
+ if (parts.length !== 3) throw new Error("A parametric surface has three parts: (x, y, z)");
51
+ const [x, y, z] = parts.map((part) => compile(part, EQUATION_VARIABLES_3D.surface));
52
+ return { kind: "parametric", x, y, z };
53
+ }
54
+ const sides = splitTopLevel(text, "=");
55
+ if (sides.length > 2) throw new Error("An equation has one '='");
56
+ if (sides.length === 2 && sides[0].trim() !== "z") throw new Error("A surface is written z = …, or as three parts (x, y, z)");
57
+ return { kind: "height", f: compile(sides[sides.length - 1], EQUATION_VARIABLES_3D.height) };
58
+ });
64
59
 
65
60
  /** `source` as a space curve, or the reason it is not one. */
66
- export function parseSpaceCurve(source: string, params: readonly string[] = []): SpaceCurveEquation | Error {
67
- return memo(curves, source, params, (text) => {
68
- const compile = compiler(params);
69
- const parts = splitTuple(text);
70
- if (parts?.length !== 3) throw new Error("A space curve has three parts: (x, y, z)");
71
- const [x, y, z] = parts.map((part) => compile(part, EQUATION_VARIABLES_3D.spaceCurve));
72
- return { x, y, z };
73
- });
74
- }
61
+ export const parseSpaceCurve = memoParse<SpaceCurveEquation>(128, (text, params) => {
62
+ const compile = compilerFor(params);
63
+ const parts = splitTuple(text);
64
+ if (parts?.length !== 3) throw new Error("A space curve has three parts: (x, y, z)");
65
+ const [x, y, z] = parts.map((part) => compile(part, EQUATION_VARIABLES_3D.spaceCurve));
66
+ return { x, y, z };
67
+ });
75
68
 
76
69
  /** Why `source` will not draw as a surface, or `null`; empty text is not an error. */
77
70
  export function surfaceError(source: string): string | null {
@@ -87,35 +80,6 @@ export function spaceCurveError(source: string): string | null {
87
80
  return equation instanceof Error ? equation.message : null;
88
81
  }
89
82
 
90
- function compiler(params: readonly string[]) {
91
- return (text: string, variables: readonly string[]): CompiledExpression => {
92
- const result = compileExpressionCached(text.trim(), variables, params);
93
- if (result instanceof Error) throw result;
94
- return result;
95
- };
96
- }
97
-
98
- function memo<T extends object>(
99
- cache: ReturnType<typeof createBoundedCache<T | Error>>,
100
- source: string,
101
- params: readonly string[],
102
- read: (text: string) => T,
103
- ): T | Error {
104
- const key = `${params.join(",")}|${source}`;
105
- const hit = cache.get(key);
106
- if (hit !== undefined) return hit;
107
- let result: T | Error;
108
- try {
109
- const text = source.trim();
110
- if (text === "") throw new Error("Empty equation");
111
- result = read(text);
112
- } catch (error) {
113
- result = error instanceof Error ? error : new Error(String(error));
114
- }
115
- cache.set(key, result);
116
- return result;
117
- }
118
-
119
83
  /** How a host checks an equation as it is typed, and finds its named values, by the `kind` its property declares. */
120
84
  export interface EquationLanguage {
121
85
  error(source: string): string | null;
@@ -1,14 +1,6 @@
1
1
  import { expressionOps } from "@motionscript/sdk";
2
2
  import type { ExpressionDialect, ExpressionNode } from "@motionscript/sdk";
3
- import { createBoundedCache } from "./cache";
4
-
5
- /**
6
- * The language a plot's equation is written in: the engine's own expression
7
- * grammar (`expressionOps.parse`) under a dialect of its own, where trig is in
8
- * radians, names are bare identifiers, and the logs and hyperbolics a plot
9
- * wants are present. Never `new Function`: an equation is text stored in a
10
- * document, and the shared parser can only produce arithmetic.
11
- */
3
+ import { createBoundedCache, remember } from "./cache";
12
4
 
13
5
  /** Named values an expression reads at call time, beside its two variables. */
14
6
  export type ExpressionScope = Readonly<Record<string, number>>;
@@ -41,6 +33,11 @@ const CONSTANTS: Record<string, number> = {
41
33
  pi: Math.PI, PI: Math.PI, e: Math.E, E: Math.E, tau: Math.PI * 2, phi: (1 + Math.sqrt(5)) / 2,
42
34
  };
43
35
 
36
+ /**
37
+ * The language a plot's equation is written in: the engine's expression grammar
38
+ * under a dialect of its own (trig in radians, bare identifiers, logs and
39
+ * hyperbolics). Never `new Function`: an equation is stored text.
40
+ */
44
41
  export const MATH_DIALECT: ExpressionDialect = {
45
42
  functions: {
46
43
  ...Object.fromEntries(Object.keys(UNARY).map((name) => [name, { arity: [1, 1] as [number, number] }])),
@@ -62,7 +59,7 @@ export function expressionVocabulary(variables: readonly string[]): ExpressionVo
62
59
  return {
63
60
  variables,
64
61
  functions: [...Object.keys(UNARY), ...Object.keys(NARY)].sort(),
65
- constants: ["pi", "e", "tau", "phi"],
62
+ constants: Object.keys(CONSTANTS).filter((name) => name === name.toLowerCase()),
66
63
  };
67
64
  }
68
65
 
@@ -75,8 +72,7 @@ export function compileExpression(
75
72
  variables: readonly string[],
76
73
  params: readonly string[] = [],
77
74
  ): CompiledExpression {
78
- // The parser folds a constant before it yields a reference, so a name that
79
- // is both would silently stop being the variable it was passed as.
75
+ // The parser folds a constant before it yields a reference, so a name that is both stops being the variable.
80
76
  for (const name of [...variables, ...params]) {
81
77
  if (Object.hasOwn(CONSTANTS, name)) throw new Error(`'${name}' is a constant, so it cannot be a variable`);
82
78
  }
@@ -145,15 +141,14 @@ export function compileExpressionCached(
145
141
  variables: readonly string[],
146
142
  params: readonly string[] = [],
147
143
  ): CompiledExpression | Error {
148
- const key = `${variables.join(",")}|${params.join(",")}|${source}`;
149
- const hit = compiled.get(key);
150
- if (hit !== undefined) return hit;
151
- let result: CompiledExpression | Error;
152
- try {
153
- result = compileExpression(source, variables, params);
154
- } catch (error) {
155
- result = error instanceof Error ? error : new Error(String(error));
156
- }
157
- compiled.set(key, result);
158
- return result;
144
+ return remember(compiled, `${variables.join(",")}|${params.join(",")}|${source}`, () => compileExpression(source, variables, params));
145
+ }
146
+
147
+ /** A compiler that throws a failure instead of returning it, for a reader running inside `memoParse`. */
148
+ export function compilerFor(params: readonly string[]) {
149
+ return (text: string, variables: readonly string[]): CompiledExpression => {
150
+ const result = compileExpressionCached(text.trim(), variables, params);
151
+ if (result instanceof Error) throw result;
152
+ return result;
153
+ };
159
154
  }
@@ -23,12 +23,7 @@ import { drawMark, type MarkPaint, type MarkReading } from "./mark";
23
23
  import { fillAt, lerpFillScale, readFillScale } from "./scale";
24
24
  import { lerpParams, lerpSource, readSource, scopeFor, type PlotSource } from "./source";
25
25
 
26
- /**
27
- * What a mark drawn from a vector field, `(P(x, y), Q(x, y))`, shares: arrows,
28
- * or the lines that follow it, all in one stroke. Without strength fills it is
29
- * one path in the stroke's paint; with them, what it draws is sorted by the
30
- * field's strength into a few shades.
31
- */
26
+ // Without strength fills a field mark is one path in the stroke's paint; with them it is sorted by strength into a few shades.
32
27
 
33
28
  export interface FieldProps extends PaintProps {
34
29
  expression?: string | PlotSource;
@@ -70,7 +65,6 @@ export const FIELD_COMMANDS = {
70
65
  paramsTo: { args: ["params"] as const },
71
66
  };
72
67
 
73
- /** What a field mark is drawn from. */
74
68
  export interface FieldValues {
75
69
  readonly expression: PlotSource;
76
70
  readonly params: ExpressionScope;
package/src/kit/field.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { createBoundedCache } from "./cache";
1
+ import { memoParse } from "./cache";
2
2
  import { equationNames, splitTuple } from "./equation";
3
- import { compileExpressionCached, type CompiledExpression } from "./expression";
3
+ import { compilerFor, type CompiledExpression } from "./expression";
4
4
 
5
5
  /** A vector at every point of the plane, two parts in `x` and `y`: `(-y, x)`. A warp is written the same way. */
6
6
  export interface FieldEquation {
@@ -11,36 +11,20 @@ export interface FieldEquation {
11
11
  export const FIELD_VARIABLES = ["x", "y"] as const;
12
12
 
13
13
  const NAMES: ReadonlySet<string> = new Set(FIELD_VARIABLES);
14
- const fields = createBoundedCache<FieldEquation | Error>(128);
15
14
 
16
15
  /** The named values a field's equation reads: every name that is not `x` or `y`. */
17
16
  export function fieldParams(source: string): readonly string[] {
18
17
  return equationNames(source).filter((name) => !NAMES.has(name));
19
18
  }
20
19
 
21
- /** `source` as a field, or the reason it is not one. Memoised, so an unchanged equation is the identical object. */
22
- export function parseField(source: string, params: readonly string[] = []): FieldEquation | Error {
23
- const key = `${params.join(",")}|${source}`;
24
- const hit = fields.get(key);
25
- if (hit !== undefined) return hit;
26
- let result: FieldEquation | Error;
27
- try {
28
- const text = source.trim();
29
- if (text === "") throw new Error("Empty equation");
30
- const parts = splitTuple(text);
31
- if (parts?.length !== 2) throw new Error("A field has two parts: (x, y)");
32
- const [x, y] = parts.map((part) => {
33
- const compiled = compileExpressionCached(part.trim(), FIELD_VARIABLES, params);
34
- if (compiled instanceof Error) throw compiled;
35
- return compiled;
36
- });
37
- result = { x, y };
38
- } catch (error) {
39
- result = error instanceof Error ? error : new Error(String(error));
40
- }
41
- fields.set(key, result);
42
- return result;
43
- }
20
+ /** `source` as a field, or the reason it is not one. */
21
+ export const parseField = memoParse<FieldEquation>(128, (text, params) => {
22
+ const parts = splitTuple(text);
23
+ if (parts?.length !== 2) throw new Error("A field has two parts: (x, y)");
24
+ const compile = compilerFor(params);
25
+ const [x, y] = parts.map((part) => compile(part, FIELD_VARIABLES));
26
+ return { x, y };
27
+ });
44
28
 
45
29
  /** Why `source` is not a field, or `null`; empty text is not an error. */
46
30
  export function fieldError(source: string): string | null {
package/src/kit/follow.ts CHANGED
@@ -18,6 +18,7 @@ import { omit, PAINT, PAINT_COMMANDS } from "./component";
18
18
  import { LatexMatrix } from "../tex/matrix";
19
19
  import { Latex } from "../tex/node";
20
20
  import { isFormula, latexPartBounds } from "../tex/typeset";
21
+ import { radians } from "./geometry";
21
22
  import { refId } from "./graph";
22
23
 
23
24
  /** A node with parts that can be pointed at by name: a formula's terms. */
@@ -70,10 +71,8 @@ const REF = Types.custom<string, NodeView | null>("", { kind: "nodeRef", animata
70
71
  const HELD_REF = Types.custom<string, NodeView | null, { readonly private: true }>("", { kind: "nodeRef", private: true });
71
72
 
72
73
  /**
73
- * A mark drawn on another node, or on one part of a formula: a brace, a box,
74
- * an underline. It has no size of its own and takes no place in a layout; it
75
- * is drawn where its target is, wherever the two are in the tree, and its
76
- * selection box is its ink. Its entries, and what it draws on.
74
+ * A mark drawn on another node or one part of a formula (a brace, a box, an
75
+ * underline). It has no size and takes no place in a layout; its selection box is its ink.
77
76
  */
78
77
  export const FOLLOW_STATE = {
79
78
  // It is only an outline.
@@ -121,7 +120,6 @@ export const FOLLOW_COMMANDS = {
121
120
  }),
122
121
  };
123
122
 
124
- /** What a follow mark reads of its state. */
125
123
  export interface FollowValues {
126
124
  readonly on: NodeView | null;
127
125
  readonly part: string;
@@ -137,11 +135,7 @@ function restingOf(s: FollowValues): Target {
137
135
 
138
136
  const containers = new WeakMap<object, NodeView | null>();
139
137
 
140
- /**
141
- * The formula a follow mark sits in, as its render last found it: what a mark
142
- * naming no node is drawn on. Its bounds and handles read it from there, where
143
- * there is no context to ask.
144
- */
138
+ /** The formula a follow mark sits in, as its render last found it: its bounds and handles read it where there is no context to ask. */
145
139
  export function container(ctx: RenderContext | null, s: object): NodeView | null {
146
140
  if (!ctx) return containers.get(s) ?? null;
147
141
  const found = ctx.ancestor(Latex) ?? ctx.ancestor(LatexMatrix);
@@ -175,7 +169,7 @@ function quadOf(node: NodeView | null, target: Omit<Target, "on">): Quad | null
175
169
  : { x: 0, y: 0, width: node.size.width, height: node.size.height };
176
170
  if (!box) return null;
177
171
  const frame = node.frame;
178
- const turn = (frame.rotation * Math.PI) / 180;
172
+ const turn = radians(frame.rotation);
179
173
  const cos = Math.cos(turn);
180
174
  const sin = Math.sin(turn);
181
175
  // The engine's turn is clockwise, in a y-up frame.
@@ -202,8 +196,6 @@ export function outlineBounds(outline: readonly PathCommand[]): BoxBounds {
202
196
  /** Every drawn point's centre frame: the mark's own centre, so its outline is where it says. */
203
197
  export const OWN_FRAME: [number, number, number, number] = [-1, -1, 1, 1];
204
198
 
205
- // --- Geometry the marks share ---
206
-
207
199
  export const add = (a: Vector2, b: Vector2, k = 1): Vector2 => ({ x: a.x + b.x * k, y: a.y + b.y * k });
208
200
  export const between = (a: Vector2, b: Vector2, t = 0.5): Vector2 => ({ x: a.x + (b.x - a.x) * t, y: a.y + (b.y - a.y) * t });
209
201
 
@@ -1,9 +1,7 @@
1
- import { Fills, Graphics2D, toPathString, type AssetScope, type Color, type Loader, type StrokeResolved, type Vector2 } from "@motionscript/sdk";
1
+ import { Fills, Graphics2D, lerpNumber, toPathString, Types, type AssetScope, type Color, type Loader, type StrokeResolved, type Vector2 } from "@motionscript/sdk";
2
2
  import { buildLatexPath, LatexNotLoaded, loadMathJax, MATHJAX_LOADER } from "../tex/geometry";
3
3
  import type { Interval } from "./coords";
4
4
 
5
- /** Shared drawing for the systems' own furniture: axis lines, tick marks and their numbers. */
6
-
7
5
  /** What a system draws its axes and numbers in, and a plot its curve, until told otherwise. */
8
6
  export const AXIS_COLOR = "#9ca3af";
9
7
  export const PLOT_COLOR = "#58a6ff";
@@ -104,7 +102,12 @@ export function along(at: Vector2, from: Vector2, to: Vector2): number {
104
102
  }
105
103
 
106
104
  export function lerpInterval(from: Interval, to: Interval, t: number): Interval {
107
- return [from[0] + (to[0] - from[0]) * t, from[1] + (to[1] - from[1]) * t];
105
+ return [lerpNumber(from[0], to[0], t), lerpNumber(from[1], to[1], t)];
106
+ }
107
+
108
+ /** An entry holding a `[min, max]` range, read forgivingly and lerped end by end. */
109
+ export function intervalEntry(initial: Interval) {
110
+ return Types.custom<Interval>(initial, { resolve: readInterval, lerp: lerpInterval });
108
111
  }
109
112
 
110
113
  export function readInterval(value: Interval | undefined, previous?: Interval): Interval {
@@ -34,6 +34,10 @@ export function inflate(area: Extents, margin: number): Extents {
34
34
  return { minX: area.minX - margin, minY: area.minY - margin, maxX: area.maxX + margin, maxY: area.maxY + margin };
35
35
  }
36
36
 
37
+ export function radians(degrees: number): number {
38
+ return (degrees * Math.PI) / 180;
39
+ }
40
+
37
41
  export function clamp(value: number, min: number, max: number): number {
38
42
  return value < min ? min : value > max ? max : value;
39
43
  }
package/src/kit/mark.ts CHANGED
@@ -16,17 +16,10 @@ import { boundsOfRuns, runsToPath } from "./geometry";
16
16
  import type { GraphPlane } from "./graph";
17
17
 
18
18
  /**
19
- * A mark is a node drawn in graph coordinates: a plot, an area, an arrow
20
- * between two graph points. It answers with runs of pixel points and is
21
- * otherwise a shape, so its box is the tight box of its ink.
22
- *
23
- * Inside a coordinate system (or a plot) the holder lays it out on that ink,
24
- * calling the mark's own trace with its map, and the mark's `x`/`y` are a shift
25
- * in graph units. Outside one a graph unit is a pixel and `x`/`y` are the usual
26
- * offset.
19
+ * What a mark is traced through: the map, its own `x`/`y` as a shift, and the
20
+ * plane it reads curves from. A mark is a shape whose box is the tight box of its
21
+ * ink; outside a system a graph unit is a pixel and `x`/`y` the usual offset.
27
22
  */
28
-
29
- /** What a mark is traced through: the map, its own `x`/`y` as a shift, and the plane it reads curves from. */
30
23
  export interface MarkReading {
31
24
  readonly map: CoordinateMap;
32
25
  /** Zero outside a system, where `x`/`y` are pixels. */
@@ -94,7 +87,7 @@ export function graphPoint(read: MarkReading, runs: readonly (readonly Vector2[]
94
87
  return { x: point.x - read.shift.x, y: point.y - read.shift.y };
95
88
  }
96
89
 
97
- /** What a mark paints with: a shape's paints, any it withdrew left out. */
90
+ /** What a mark paints with. */
98
91
  export interface MarkPaint {
99
92
  readonly fill?: Fill;
100
93
  readonly stroke?: Stroke;
package/src/kit/place.ts CHANGED
@@ -16,11 +16,9 @@ export function anchored<C extends { is(child: ChildView): boolean }>(component:
16
16
  }
17
17
 
18
18
  /**
19
- * Lay `children` out on `map`, for whichever node holds them: a coordinate
20
- * system, or a plot holding the marks that read it. A mark goes on its ink;
21
- * anything else keeps its own size and has its `x`/`y` read as graph
22
- * coordinates. `origin` is the holder's own centre in the map's pixels (y-up),
23
- * since a child's box is placed from there.
19
+ * Lay `children` out on `map` for a system or a plot: a mark goes on its ink,
20
+ * anything else keeps its size and reads `x`/`y` as graph coordinates.
21
+ * `origin` is the holder's centre in the map's pixels (y-up).
24
22
  */
25
23
  export function placeOnMap(children: readonly ChildView[], map: CoordinateMap, origin: Vector2, room: Size2D, plane: GraphPlane): Placement2D[] {
26
24
  return children.map((child) => {
@@ -1,11 +1,5 @@
1
1
  import type { Extents, Vector2 } from "@motionscript/sdk";
2
2
 
3
- /**
4
- * Turns a curve into the polylines that draw it: adaptively, so a straight
5
- * stretch costs two points and a tight bend as many as it needs, and broken
6
- * wherever the curve is, at an asymptote or the edge of its domain.
7
- */
8
-
9
3
  /** A curve's parameter to its pixel, written into `out`. `false` where the curve is undefined. */
10
4
  export type CurveProjection = (t: number, out: Vector2) => boolean;
11
5
 
package/src/kit/system.ts CHANGED
@@ -19,20 +19,11 @@ import type { CoordinateMap } from "./coords";
19
19
  import { settle, SYSTEM, systemPlane } from "./graph";
20
20
  import { openPlane, placeOnMap } from "./place";
21
21
 
22
- /**
23
- * What a coordinate system shares: a node whose children sit in its graph
24
- * coordinates. A child's `x`/`y` are in the system's units and the child keeps
25
- * its own size, so anything can be put on a graph: a dot, a label, an image. A
26
- * mark is drawn through the system's map instead and laid out on its ink.
27
- *
28
- * The system's `stroke` is its axis line (so `endCap` is the tip) and its `fill`
29
- * the backdrop. Ticks, grid and labels are drawing ops, never child nodes: a
30
- * document's children would otherwise share their indices with them.
31
- */
32
-
22
+ // A child's `x`/`y` are in the system's units and it keeps its own size; a mark is drawn through the map and laid out on its ink.
23
+ // The `stroke` is the axis line (`endCap` is its tip), the `fill` the backdrop. Ticks, grid and labels are drawing ops, never
24
+ // child nodes: they would share their indices with a document's children.
33
25
  const CENTRE: Vector2 = { x: 0, y: 0 };
34
26
 
35
- /** What a system's helpers read of its state. */
36
27
  export interface SystemState {
37
28
  readonly padding: InsetsResolved;
38
29
  readonly fill: Fill;
@@ -1,9 +1,7 @@
1
1
  import {
2
2
  component2d,
3
- Types,
4
3
  type NodeOf,
5
4
  type NodeView,
6
- type PaintProps,
7
5
  type Vector2,
8
6
  // Named so the emitted declaration reaches these through the sdk, not core.
9
7
  type Fill as _Fill,
@@ -16,17 +14,11 @@ import {
16
14
  import { omit, PAINT, PAINT_COMMANDS } from "../kit/component";
17
15
  import type { Interval } from "../kit/coords";
18
16
  import { curveValue } from "../kit/curve";
19
- import { lerpInterval, PLOT_COLOR, readInterval } from "../kit/furniture";
17
+ import { intervalEntry, PLOT_COLOR } from "../kit/furniture";
20
18
  import { clamp } from "../kit/geometry";
21
19
  import { curveFor, PLANE_ENTRY, PLOT_REF } from "../kit/graph";
22
20
  import { drawMark, inkSize, reading, traced, type MarkReading } from "../kit/mark";
23
21
 
24
- export interface CurveAreaProps extends PaintProps {
25
- on?: string;
26
- under?: string;
27
- domain?: Interval;
28
- }
29
-
30
22
  const state = {
31
23
  // An area is only its fill, and has no outline to trim.
32
24
  ...omit(PAINT, "stroke", "start", "end"),
@@ -34,7 +26,7 @@ const state = {
34
26
  on: PLOT_REF,
35
27
  /** A second plot to fill down to, in place of the x axis. */
36
28
  under: PLOT_REF,
37
- domain: Types.custom<Interval>([0, 1], { resolve: readInterval, lerp: lerpInterval }),
29
+ domain: intervalEntry([0, 1]),
38
30
  graph: PLANE_ENTRY,
39
31
  };
40
32
 
@@ -44,12 +36,7 @@ interface AreaValues {
44
36
  readonly domain: Interval;
45
37
  }
46
38
 
47
- /**
48
- * The region between a plot and the x axis, or between two plots, over a span
49
- * of x. It sits beside the plots it reads rather than inside one: it may read
50
- * two, and it has a life of its own on the timeline, swept open or kept while
51
- * a curve changes.
52
- */
39
+ /** The region between a plot and the x axis, or between two plots, over a span of x. It sits beside the plots it reads, since it may read two. */
53
40
  export const CurveArea = traced(component2d({
54
41
  key: "curveArea",
55
42
  state,