@motionscript/math 0.1.0-alpha.8 → 0.1.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 (158) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/LICENSE +201 -201
  3. package/NOTICE +49 -49
  4. package/README.md +390 -391
  5. package/dist/annotate/angle-mark.js.map +1 -1
  6. package/dist/annotate/curly-brace.js.map +1 -1
  7. package/dist/annotate/emphasis-mark.js.map +1 -1
  8. package/dist/browser/chunks/{chunk-FETQZEN2.js → chunk-IRZCG6PM.js} +2 -2
  9. package/dist/browser/chunks/chunk-IRZCG6PM.js.map +7 -0
  10. package/dist/browser/chunks/{chunk-K77KLQX4.js → chunk-WMVW6ZIV.js} +2 -2
  11. package/dist/browser/chunks/chunk-WMVW6ZIV.js.map +7 -0
  12. package/dist/browser/chunks/{mathjax-DNP3YRES.js → mathjax-WV55AC2P.js} +1 -1
  13. package/dist/browser/chunks/{mathjax-DNP3YRES.js.map → mathjax-WV55AC2P.js.map} +1 -1
  14. package/dist/browser/index.js +1 -1
  15. package/dist/browser/index.js.map +1 -1
  16. package/dist/browser/kit.js +1 -1
  17. package/dist/browser/manifest.json +1 -1
  18. package/dist/browser/tex.js +1 -1
  19. package/dist/browser/tex.js.map +1 -1
  20. package/dist/engine.d.ts +1 -1
  21. package/dist/engine.js +1 -1
  22. package/dist/engine.js.map +1 -1
  23. package/dist/index.js.map +1 -1
  24. package/dist/kit/brace.js.map +1 -1
  25. package/dist/kit/cache.js.map +1 -1
  26. package/dist/kit/component.d.ts.map +1 -1
  27. package/dist/kit/component.js.map +1 -1
  28. package/dist/kit/contour.js.map +1 -1
  29. package/dist/kit/coords.js.map +1 -1
  30. package/dist/kit/curve.js.map +1 -1
  31. package/dist/kit/equation.js.map +1 -1
  32. package/dist/kit/equation3d.js.map +1 -1
  33. package/dist/kit/expression.js.map +1 -1
  34. package/dist/kit/field-mark.d.ts.map +1 -1
  35. package/dist/kit/field-mark.js.map +1 -1
  36. package/dist/kit/field.js.map +1 -1
  37. package/dist/kit/follow.d.ts.map +1 -1
  38. package/dist/kit/follow.js.map +1 -1
  39. package/dist/kit/furniture.js.map +1 -1
  40. package/dist/kit/geometry.js.map +1 -1
  41. package/dist/kit/graph.js.map +1 -1
  42. package/dist/kit/mark.js.map +1 -1
  43. package/dist/kit/part-fills.js.map +1 -1
  44. package/dist/kit/place.js.map +1 -1
  45. package/dist/kit/sampler.js.map +1 -1
  46. package/dist/kit/scale.js.map +1 -1
  47. package/dist/kit/source.js.map +1 -1
  48. package/dist/kit/system.js.map +1 -1
  49. package/dist/kit/ticks.js.map +1 -1
  50. package/dist/kit/withdrawn.js.map +1 -1
  51. package/dist/marks/curve-area.js.map +1 -1
  52. package/dist/marks/drop-lines.js.map +1 -1
  53. package/dist/marks/plot-point.js.map +1 -1
  54. package/dist/marks/riemann-sum.js.map +1 -1
  55. package/dist/marks/tangent-line.js.map +1 -1
  56. package/dist/marks/vector-arrow.js.map +1 -1
  57. package/dist/nodes.js.map +1 -1
  58. package/dist/plots/function-plot.d.ts.map +1 -1
  59. package/dist/plots/function-plot.js.map +1 -1
  60. package/dist/plots/points-plot.js.map +1 -1
  61. package/dist/plots/stream-lines.js.map +1 -1
  62. package/dist/plots/traced-path.js.map +1 -1
  63. package/dist/plots/vector-field.js.map +1 -1
  64. package/dist/structures/graph-layout.js.map +1 -1
  65. package/dist/structures/network-graph.js.map +1 -1
  66. package/dist/structures/sample-space.js.map +1 -1
  67. package/dist/systems/axes2d.d.ts.map +1 -1
  68. package/dist/systems/axes2d.js.map +1 -1
  69. package/dist/systems/number-line.js.map +1 -1
  70. package/dist/systems/number-plane.js.map +1 -1
  71. package/dist/systems/polar-plane.js.map +1 -1
  72. package/dist/tex/geometry.js.map +1 -1
  73. package/dist/tex/index.js.map +1 -1
  74. package/dist/tex/mathjax.js.map +1 -1
  75. package/dist/tex/matrix.js.map +1 -1
  76. package/dist/tex/node.js.map +1 -1
  77. package/dist/tex/tween.js.map +1 -1
  78. package/dist/tex/typeset.d.ts.map +1 -1
  79. package/dist/tex/typeset.js.map +1 -1
  80. package/dist/three/axes3d.js.map +1 -1
  81. package/dist/three/curve-plot3d.js.map +1 -1
  82. package/dist/three/entries.js.map +1 -1
  83. package/dist/three/label3d.js.map +1 -1
  84. package/dist/three/plot-point3d.js.map +1 -1
  85. package/dist/three/space.js.map +1 -1
  86. package/dist/three/surface-plot3d.js.map +1 -1
  87. package/dist/three/vector-arrow3d.js.map +1 -1
  88. package/package.json +4 -4
  89. package/registry.json +251 -251
  90. package/src/annotate/angle-mark.ts +137 -137
  91. package/src/annotate/curly-brace.ts +148 -148
  92. package/src/annotate/emphasis-mark.ts +118 -118
  93. package/src/engine.ts +86 -86
  94. package/src/index.ts +29 -29
  95. package/src/kit/brace.ts +37 -37
  96. package/src/kit/cache.ts +61 -61
  97. package/src/kit/component.ts +57 -57
  98. package/src/kit/contour.ts +94 -94
  99. package/src/kit/coords.ts +278 -278
  100. package/src/kit/curve.ts +47 -47
  101. package/src/kit/equation.ts +181 -181
  102. package/src/kit/equation3d.ts +137 -137
  103. package/src/kit/expression.ts +159 -159
  104. package/src/kit/field-mark.ts +171 -171
  105. package/src/kit/field.ts +50 -50
  106. package/src/kit/follow.ts +229 -229
  107. package/src/kit/furniture.ts +113 -113
  108. package/src/kit/geometry.ts +49 -49
  109. package/src/kit/graph.ts +116 -116
  110. package/src/kit/mark.ts +152 -152
  111. package/src/kit/part-fills.ts +61 -61
  112. package/src/kit/place.ts +77 -77
  113. package/src/kit/sampler.ts +281 -281
  114. package/src/kit/scale.ts +39 -39
  115. package/src/kit/source.ts +57 -57
  116. package/src/kit/system.ts +105 -105
  117. package/src/kit/ticks.ts +85 -85
  118. package/src/kit/withdrawn.ts +8 -8
  119. package/src/marks/curve-area.ts +89 -89
  120. package/src/marks/drop-lines.ts +71 -71
  121. package/src/marks/plot-point.ts +102 -102
  122. package/src/marks/riemann-sum.ts +99 -99
  123. package/src/marks/tangent-line.ts +119 -119
  124. package/src/marks/vector-arrow.ts +98 -98
  125. package/src/nodes.ts +99 -99
  126. package/src/plots/function-plot.ts +220 -220
  127. package/src/plots/points-plot.ts +103 -103
  128. package/src/plots/stream-lines.ts +160 -160
  129. package/src/plots/traced-path.ts +70 -70
  130. package/src/plots/vector-field.ts +107 -107
  131. package/src/structures/graph-layout.ts +145 -145
  132. package/src/structures/network-graph.ts +291 -291
  133. package/src/structures/sample-space.ts +210 -210
  134. package/src/systems/axes2d.ts +226 -226
  135. package/src/systems/number-line.ts +130 -130
  136. package/src/systems/number-plane.ts +232 -232
  137. package/src/systems/polar-plane.ts +177 -177
  138. package/src/tex/geometry.ts +703 -703
  139. package/src/tex/index.ts +15 -15
  140. package/src/tex/mathjax.ts +82 -82
  141. package/src/tex/matrix.ts +97 -97
  142. package/src/tex/node.ts +74 -74
  143. package/src/tex/tween.ts +308 -308
  144. package/src/tex/typeset.ts +461 -461
  145. package/src/three/axes3d.ts +267 -267
  146. package/src/three/curve-plot3d.ts +146 -146
  147. package/src/three/entries.ts +42 -42
  148. package/src/three/label3d.ts +52 -52
  149. package/src/three/plot-point3d.ts +63 -63
  150. package/src/three/space.ts +92 -92
  151. package/src/three/surface-plot3d.ts +318 -318
  152. package/src/three/vector-arrow3d.ts +88 -88
  153. package/dist/browser/chunks/chunk-FETQZEN2.js.map +0 -7
  154. package/dist/browser/chunks/chunk-K77KLQX4.js.map +0 -7
  155. package/dist/kit/placement.d.ts +0 -8
  156. package/dist/kit/placement.d.ts.map +0 -1
  157. package/dist/kit/placement.js +0 -8
  158. package/dist/kit/placement.js.map +0 -1
@@ -1,159 +1,159 @@
1
- import { expressionOps } from "@motionscript/sdk";
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
- */
12
-
13
- /** Named values an expression reads at call time, beside its two variables. */
14
- export type ExpressionScope = Readonly<Record<string, number>>;
15
-
16
- /**
17
- * A compiled expression. The two variables are fixed slots because this is the
18
- * hot path of every sampled curve; a one-variable expression never reads `b`.
19
- */
20
- export type CompiledExpression = (a: number, b: number, scope?: ExpressionScope) => number;
21
-
22
- const UNARY: Record<string, (v: number) => number> = {
23
- sin: Math.sin, cos: Math.cos, tan: Math.tan,
24
- asin: Math.asin, acos: Math.acos, atan: Math.atan,
25
- sinh: Math.sinh, cosh: Math.cosh, tanh: Math.tanh,
26
- sqrt: Math.sqrt, cbrt: Math.cbrt, abs: Math.abs, exp: Math.exp,
27
- log: Math.log, ln: Math.log, log2: Math.log2, log10: Math.log10,
28
- floor: Math.floor, ceil: Math.ceil, round: Math.round, sign: Math.sign, trunc: Math.trunc,
29
- };
30
-
31
- const NARY: Record<string, { fn: (...args: number[]) => number; arity: [number, number] }> = {
32
- atan2: { fn: Math.atan2, arity: [2, 2] },
33
- pow: { fn: Math.pow, arity: [2, 2] },
34
- mod: { fn: (a, b) => a % b, arity: [2, 2] },
35
- hypot: { fn: Math.hypot, arity: [1, Infinity] },
36
- min: { fn: Math.min, arity: [1, Infinity] },
37
- max: { fn: Math.max, arity: [1, Infinity] },
38
- };
39
-
40
- const CONSTANTS: Record<string, number> = {
41
- pi: Math.PI, PI: Math.PI, e: Math.E, E: Math.E, tau: Math.PI * 2, phi: (1 + Math.sqrt(5)) / 2,
42
- };
43
-
44
- export const MATH_DIALECT: ExpressionDialect = {
45
- functions: {
46
- ...Object.fromEntries(Object.keys(UNARY).map((name) => [name, { arity: [1, 1] as [number, number] }])),
47
- ...Object.fromEntries(Object.entries(NARY).map(([name, { arity }]) => [name, { arity }])),
48
- },
49
- constants: CONSTANTS,
50
- refs: "bare",
51
- unknownName: (name) => `Unknown name '${name}'`,
52
- };
53
-
54
- /** What an expression may name, for a host's "what can I write here" help. */
55
- export interface ExpressionVocabulary {
56
- variables: readonly string[];
57
- functions: readonly string[];
58
- constants: readonly string[];
59
- }
60
-
61
- export function expressionVocabulary(variables: readonly string[]): ExpressionVocabulary {
62
- return {
63
- variables,
64
- functions: [...Object.keys(UNARY), ...Object.keys(NARY)].sort(),
65
- constants: ["pi", "e", "tau", "phi"],
66
- };
67
- }
68
-
69
- /**
70
- * Compile `source` over up to two `variables`, in slot order, and any `params`
71
- * it may read from the scope. Throws on a syntax error or an unknown name.
72
- */
73
- export function compileExpression(
74
- source: string,
75
- variables: readonly string[],
76
- params: readonly string[] = [],
77
- ): 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.
80
- for (const name of [...variables, ...params]) {
81
- if (Object.hasOwn(CONSTANTS, name)) throw new Error(`'${name}' is a constant, so it cannot be a variable`);
82
- }
83
- return emit(expressionOps.parse(source, MATH_DIALECT).root, variables, params);
84
- }
85
-
86
- function emit(node: ExpressionNode, variables: readonly string[], params: readonly string[]): CompiledExpression {
87
- switch (node.kind) {
88
- case "const": {
89
- const { value } = node;
90
- return () => value;
91
- }
92
- case "ref": {
93
- const { name } = node;
94
- const slot = variables.indexOf(name);
95
- if (slot === 0) return (a) => a;
96
- if (slot === 1) return (_a, b) => b;
97
- if (params.includes(name)) return (_a, _b, scope) => scope?.[name] ?? NaN;
98
- throw new Error(MATH_DIALECT.unknownName(name));
99
- }
100
- case "unary": {
101
- const operand = emit(node.operand, variables, params);
102
- return (a, b, s) => -operand(a, b, s);
103
- }
104
- case "binary": {
105
- const l = emit(node.left, variables, params);
106
- const r = emit(node.right, variables, params);
107
- switch (node.op) {
108
- case "+": return (a, b, s) => l(a, b, s) + r(a, b, s);
109
- case "-": return (a, b, s) => l(a, b, s) - r(a, b, s);
110
- case "*": return (a, b, s) => l(a, b, s) * r(a, b, s);
111
- case "/": return (a, b, s) => l(a, b, s) / r(a, b, s);
112
- case "%": return (a, b, s) => l(a, b, s) % r(a, b, s);
113
- case "^": return (a, b, s) => Math.pow(l(a, b, s), r(a, b, s));
114
- }
115
- break;
116
- }
117
- case "call": {
118
- const args = node.args.map((arg) => emit(arg, variables, params));
119
- const unary = UNARY[node.name];
120
- if (unary) {
121
- const [x] = args;
122
- return (a, b, s) => unary(x(a, b, s));
123
- }
124
- const { fn } = NARY[node.name];
125
- // The common arity, so the hot path allocates no array per sample.
126
- if (args.length === 2) {
127
- const [x, y] = args;
128
- return (a, b, s) => fn(x(a, b, s), y(a, b, s));
129
- }
130
- return (a, b, s) => fn(...args.map((arg) => arg(a, b, s)));
131
- }
132
- }
133
- throw new Error("Unsupported expression");
134
- }
135
-
136
- const compiled = createBoundedCache<CompiledExpression | Error>(256);
137
-
138
- /**
139
- * {@link compileExpression}, memoised: equal source under equal names is the
140
- * identical closure, so "did this expression change" is an `===`, and a failure
141
- * is remembered rather than re-parsed every frame.
142
- */
143
- export function compileExpressionCached(
144
- source: string,
145
- variables: readonly string[],
146
- params: readonly string[] = [],
147
- ): 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;
159
- }
1
+ import { expressionOps } from "@motionscript/sdk";
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
+ */
12
+
13
+ /** Named values an expression reads at call time, beside its two variables. */
14
+ export type ExpressionScope = Readonly<Record<string, number>>;
15
+
16
+ /**
17
+ * A compiled expression. The two variables are fixed slots because this is the
18
+ * hot path of every sampled curve; a one-variable expression never reads `b`.
19
+ */
20
+ export type CompiledExpression = (a: number, b: number, scope?: ExpressionScope) => number;
21
+
22
+ const UNARY: Record<string, (v: number) => number> = {
23
+ sin: Math.sin, cos: Math.cos, tan: Math.tan,
24
+ asin: Math.asin, acos: Math.acos, atan: Math.atan,
25
+ sinh: Math.sinh, cosh: Math.cosh, tanh: Math.tanh,
26
+ sqrt: Math.sqrt, cbrt: Math.cbrt, abs: Math.abs, exp: Math.exp,
27
+ log: Math.log, ln: Math.log, log2: Math.log2, log10: Math.log10,
28
+ floor: Math.floor, ceil: Math.ceil, round: Math.round, sign: Math.sign, trunc: Math.trunc,
29
+ };
30
+
31
+ const NARY: Record<string, { fn: (...args: number[]) => number; arity: [number, number] }> = {
32
+ atan2: { fn: Math.atan2, arity: [2, 2] },
33
+ pow: { fn: Math.pow, arity: [2, 2] },
34
+ mod: { fn: (a, b) => a % b, arity: [2, 2] },
35
+ hypot: { fn: Math.hypot, arity: [1, Infinity] },
36
+ min: { fn: Math.min, arity: [1, Infinity] },
37
+ max: { fn: Math.max, arity: [1, Infinity] },
38
+ };
39
+
40
+ const CONSTANTS: Record<string, number> = {
41
+ pi: Math.PI, PI: Math.PI, e: Math.E, E: Math.E, tau: Math.PI * 2, phi: (1 + Math.sqrt(5)) / 2,
42
+ };
43
+
44
+ export const MATH_DIALECT: ExpressionDialect = {
45
+ functions: {
46
+ ...Object.fromEntries(Object.keys(UNARY).map((name) => [name, { arity: [1, 1] as [number, number] }])),
47
+ ...Object.fromEntries(Object.entries(NARY).map(([name, { arity }]) => [name, { arity }])),
48
+ },
49
+ constants: CONSTANTS,
50
+ refs: "bare",
51
+ unknownName: (name) => `Unknown name '${name}'`,
52
+ };
53
+
54
+ /** What an expression may name, for a host's "what can I write here" help. */
55
+ export interface ExpressionVocabulary {
56
+ variables: readonly string[];
57
+ functions: readonly string[];
58
+ constants: readonly string[];
59
+ }
60
+
61
+ export function expressionVocabulary(variables: readonly string[]): ExpressionVocabulary {
62
+ return {
63
+ variables,
64
+ functions: [...Object.keys(UNARY), ...Object.keys(NARY)].sort(),
65
+ constants: ["pi", "e", "tau", "phi"],
66
+ };
67
+ }
68
+
69
+ /**
70
+ * Compile `source` over up to two `variables`, in slot order, and any `params`
71
+ * it may read from the scope. Throws on a syntax error or an unknown name.
72
+ */
73
+ export function compileExpression(
74
+ source: string,
75
+ variables: readonly string[],
76
+ params: readonly string[] = [],
77
+ ): 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.
80
+ for (const name of [...variables, ...params]) {
81
+ if (Object.hasOwn(CONSTANTS, name)) throw new Error(`'${name}' is a constant, so it cannot be a variable`);
82
+ }
83
+ return emit(expressionOps.parse(source, MATH_DIALECT).root, variables, params);
84
+ }
85
+
86
+ function emit(node: ExpressionNode, variables: readonly string[], params: readonly string[]): CompiledExpression {
87
+ switch (node.kind) {
88
+ case "const": {
89
+ const { value } = node;
90
+ return () => value;
91
+ }
92
+ case "ref": {
93
+ const { name } = node;
94
+ const slot = variables.indexOf(name);
95
+ if (slot === 0) return (a) => a;
96
+ if (slot === 1) return (_a, b) => b;
97
+ if (params.includes(name)) return (_a, _b, scope) => scope?.[name] ?? NaN;
98
+ throw new Error(MATH_DIALECT.unknownName(name));
99
+ }
100
+ case "unary": {
101
+ const operand = emit(node.operand, variables, params);
102
+ return (a, b, s) => -operand(a, b, s);
103
+ }
104
+ case "binary": {
105
+ const l = emit(node.left, variables, params);
106
+ const r = emit(node.right, variables, params);
107
+ switch (node.op) {
108
+ case "+": return (a, b, s) => l(a, b, s) + r(a, b, s);
109
+ case "-": return (a, b, s) => l(a, b, s) - r(a, b, s);
110
+ case "*": return (a, b, s) => l(a, b, s) * r(a, b, s);
111
+ case "/": return (a, b, s) => l(a, b, s) / r(a, b, s);
112
+ case "%": return (a, b, s) => l(a, b, s) % r(a, b, s);
113
+ case "^": return (a, b, s) => Math.pow(l(a, b, s), r(a, b, s));
114
+ }
115
+ break;
116
+ }
117
+ case "call": {
118
+ const args = node.args.map((arg) => emit(arg, variables, params));
119
+ const unary = UNARY[node.name];
120
+ if (unary) {
121
+ const [x] = args;
122
+ return (a, b, s) => unary(x(a, b, s));
123
+ }
124
+ const { fn } = NARY[node.name];
125
+ // The common arity, so the hot path allocates no array per sample.
126
+ if (args.length === 2) {
127
+ const [x, y] = args;
128
+ return (a, b, s) => fn(x(a, b, s), y(a, b, s));
129
+ }
130
+ return (a, b, s) => fn(...args.map((arg) => arg(a, b, s)));
131
+ }
132
+ }
133
+ throw new Error("Unsupported expression");
134
+ }
135
+
136
+ const compiled = createBoundedCache<CompiledExpression | Error>(256);
137
+
138
+ /**
139
+ * {@link compileExpression}, memoised: equal source under equal names is the
140
+ * identical closure, so "did this expression change" is an `===`, and a failure
141
+ * is remembered rather than re-parsed every frame.
142
+ */
143
+ export function compileExpressionCached(
144
+ source: string,
145
+ variables: readonly string[],
146
+ params: readonly string[] = [],
147
+ ): 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;
159
+ }