@motionscript/math 0.1.0-alpha.8 → 0.1.0-alpha.9
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/CHANGELOG.md +2 -0
- package/LICENSE +201 -201
- package/NOTICE +49 -49
- package/README.md +390 -391
- package/dist/annotate/angle-mark.js.map +1 -1
- package/dist/annotate/curly-brace.js.map +1 -1
- package/dist/annotate/emphasis-mark.js.map +1 -1
- package/dist/browser/chunks/{chunk-FETQZEN2.js → chunk-IRZCG6PM.js} +2 -2
- package/dist/browser/chunks/chunk-IRZCG6PM.js.map +7 -0
- package/dist/browser/chunks/{chunk-K77KLQX4.js → chunk-WMVW6ZIV.js} +2 -2
- package/dist/browser/chunks/chunk-WMVW6ZIV.js.map +7 -0
- package/dist/browser/chunks/{mathjax-DNP3YRES.js → mathjax-WV55AC2P.js} +1 -1
- package/dist/browser/chunks/{mathjax-DNP3YRES.js.map → mathjax-WV55AC2P.js.map} +1 -1
- package/dist/browser/index.js +1 -1
- package/dist/browser/index.js.map +1 -1
- package/dist/browser/kit.js +1 -1
- package/dist/browser/tex.js +1 -1
- package/dist/browser/tex.js.map +1 -1
- package/dist/engine.d.ts +1 -1
- package/dist/engine.js +1 -1
- package/dist/engine.js.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/kit/brace.js.map +1 -1
- package/dist/kit/cache.js.map +1 -1
- package/dist/kit/component.d.ts.map +1 -1
- package/dist/kit/component.js.map +1 -1
- package/dist/kit/contour.js.map +1 -1
- package/dist/kit/coords.js.map +1 -1
- package/dist/kit/curve.js.map +1 -1
- package/dist/kit/equation.js.map +1 -1
- package/dist/kit/equation3d.js.map +1 -1
- package/dist/kit/expression.js.map +1 -1
- package/dist/kit/field-mark.d.ts.map +1 -1
- package/dist/kit/field-mark.js.map +1 -1
- package/dist/kit/field.js.map +1 -1
- package/dist/kit/follow.d.ts.map +1 -1
- package/dist/kit/follow.js.map +1 -1
- package/dist/kit/furniture.js.map +1 -1
- package/dist/kit/geometry.js.map +1 -1
- package/dist/kit/graph.js.map +1 -1
- package/dist/kit/mark.js.map +1 -1
- package/dist/kit/part-fills.js.map +1 -1
- package/dist/kit/place.js.map +1 -1
- package/dist/kit/sampler.js.map +1 -1
- package/dist/kit/scale.js.map +1 -1
- package/dist/kit/source.js.map +1 -1
- package/dist/kit/system.js.map +1 -1
- package/dist/kit/ticks.js.map +1 -1
- package/dist/kit/withdrawn.js.map +1 -1
- package/dist/marks/curve-area.js.map +1 -1
- package/dist/marks/drop-lines.js.map +1 -1
- package/dist/marks/plot-point.js.map +1 -1
- package/dist/marks/riemann-sum.js.map +1 -1
- package/dist/marks/tangent-line.js.map +1 -1
- package/dist/marks/vector-arrow.js.map +1 -1
- package/dist/nodes.js.map +1 -1
- package/dist/plots/function-plot.d.ts.map +1 -1
- package/dist/plots/function-plot.js.map +1 -1
- package/dist/plots/points-plot.js.map +1 -1
- package/dist/plots/stream-lines.js.map +1 -1
- package/dist/plots/traced-path.js.map +1 -1
- package/dist/plots/vector-field.js.map +1 -1
- package/dist/structures/graph-layout.js.map +1 -1
- package/dist/structures/network-graph.js.map +1 -1
- package/dist/structures/sample-space.js.map +1 -1
- package/dist/systems/axes2d.d.ts.map +1 -1
- package/dist/systems/axes2d.js.map +1 -1
- package/dist/systems/number-line.js.map +1 -1
- package/dist/systems/number-plane.js.map +1 -1
- package/dist/systems/polar-plane.js.map +1 -1
- package/dist/tex/geometry.js.map +1 -1
- package/dist/tex/index.js.map +1 -1
- package/dist/tex/mathjax.js.map +1 -1
- package/dist/tex/matrix.js.map +1 -1
- package/dist/tex/node.js.map +1 -1
- package/dist/tex/tween.js.map +1 -1
- package/dist/tex/typeset.d.ts.map +1 -1
- package/dist/tex/typeset.js.map +1 -1
- package/dist/three/axes3d.js.map +1 -1
- package/dist/three/curve-plot3d.js.map +1 -1
- package/dist/three/entries.js.map +1 -1
- package/dist/three/label3d.js.map +1 -1
- package/dist/three/plot-point3d.js.map +1 -1
- package/dist/three/space.js.map +1 -1
- package/dist/three/surface-plot3d.js.map +1 -1
- package/dist/three/vector-arrow3d.js.map +1 -1
- package/package.json +4 -4
- package/registry.json +251 -251
- package/src/annotate/angle-mark.ts +137 -137
- package/src/annotate/curly-brace.ts +148 -148
- package/src/annotate/emphasis-mark.ts +118 -118
- package/src/engine.ts +86 -86
- package/src/index.ts +29 -29
- package/src/kit/brace.ts +37 -37
- package/src/kit/cache.ts +61 -61
- package/src/kit/component.ts +57 -57
- package/src/kit/contour.ts +94 -94
- package/src/kit/coords.ts +278 -278
- package/src/kit/curve.ts +47 -47
- package/src/kit/equation.ts +181 -181
- package/src/kit/equation3d.ts +137 -137
- package/src/kit/expression.ts +159 -159
- package/src/kit/field-mark.ts +171 -171
- package/src/kit/field.ts +50 -50
- package/src/kit/follow.ts +229 -229
- package/src/kit/furniture.ts +113 -113
- package/src/kit/geometry.ts +49 -49
- package/src/kit/graph.ts +116 -116
- package/src/kit/mark.ts +152 -152
- package/src/kit/part-fills.ts +61 -61
- package/src/kit/place.ts +77 -77
- package/src/kit/sampler.ts +281 -281
- package/src/kit/scale.ts +39 -39
- package/src/kit/source.ts +57 -57
- package/src/kit/system.ts +105 -105
- package/src/kit/ticks.ts +85 -85
- package/src/kit/withdrawn.ts +8 -8
- package/src/marks/curve-area.ts +89 -89
- package/src/marks/drop-lines.ts +71 -71
- package/src/marks/plot-point.ts +102 -102
- package/src/marks/riemann-sum.ts +99 -99
- package/src/marks/tangent-line.ts +119 -119
- package/src/marks/vector-arrow.ts +98 -98
- package/src/nodes.ts +99 -99
- package/src/plots/function-plot.ts +220 -220
- package/src/plots/points-plot.ts +103 -103
- package/src/plots/stream-lines.ts +160 -160
- package/src/plots/traced-path.ts +70 -70
- package/src/plots/vector-field.ts +107 -107
- package/src/structures/graph-layout.ts +145 -145
- package/src/structures/network-graph.ts +291 -291
- package/src/structures/sample-space.ts +210 -210
- package/src/systems/axes2d.ts +226 -226
- package/src/systems/number-line.ts +130 -130
- package/src/systems/number-plane.ts +232 -232
- package/src/systems/polar-plane.ts +177 -177
- package/src/tex/geometry.ts +703 -703
- package/src/tex/index.ts +15 -15
- package/src/tex/mathjax.ts +82 -82
- package/src/tex/matrix.ts +97 -97
- package/src/tex/node.ts +74 -74
- package/src/tex/tween.ts +308 -308
- package/src/tex/typeset.ts +461 -461
- package/src/three/axes3d.ts +267 -267
- package/src/three/curve-plot3d.ts +146 -146
- package/src/three/entries.ts +42 -42
- package/src/three/label3d.ts +52 -52
- package/src/three/plot-point3d.ts +63 -63
- package/src/three/space.ts +92 -92
- package/src/three/surface-plot3d.ts +318 -318
- package/src/three/vector-arrow3d.ts +88 -88
- package/dist/browser/chunks/chunk-FETQZEN2.js.map +0 -7
- package/dist/browser/chunks/chunk-K77KLQX4.js.map +0 -7
- package/dist/kit/placement.d.ts +0 -8
- package/dist/kit/placement.d.ts.map +0 -1
- package/dist/kit/placement.js +0 -8
- package/dist/kit/placement.js.map +0 -1
package/src/kit/equation.ts
CHANGED
|
@@ -1,181 +1,181 @@
|
|
|
1
|
-
import { expressionOps, type ExpressionNode } from "@motionscript/sdk";
|
|
2
|
-
import { createBoundedCache } from "./cache";
|
|
3
|
-
import { compileExpressionCached, MATH_DIALECT, type CompiledExpression } from "./expression";
|
|
4
|
-
|
|
5
|
-
/**
|
|
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` |
|
|
15
|
-
*/
|
|
16
|
-
export type Equation =
|
|
17
|
-
| { kind: "function"; of: "x" | "y"; f: CompiledExpression }
|
|
18
|
-
| { kind: "polar"; f: CompiledExpression }
|
|
19
|
-
| { kind: "parametric"; x: CompiledExpression; y: CompiledExpression }
|
|
20
|
-
/** Drawn where `f(x, y)` is zero. */
|
|
21
|
-
| { kind: "implicit"; f: CompiledExpression };
|
|
22
|
-
|
|
23
|
-
/** The free variable each kind is sampled over, for a host's help text. */
|
|
24
|
-
export const EQUATION_VARIABLES = {
|
|
25
|
-
function: ["x"],
|
|
26
|
-
polar: ["theta"],
|
|
27
|
-
parametric: ["t"],
|
|
28
|
-
implicit: ["x", "y"],
|
|
29
|
-
} as const satisfies Record<Equation["kind"], readonly string[]>;
|
|
30
|
-
|
|
31
|
-
const parsed = createBoundedCache<Equation | Error>(256);
|
|
32
|
-
|
|
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
|
-
*/
|
|
56
|
-
export function equationError(source: string): string | null {
|
|
57
|
-
if (source.trim() === "") return null;
|
|
58
|
-
const equation = parseEquation(source, equationParams(source));
|
|
59
|
-
return equation instanceof Error ? equation.message : null;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/** A name that is always a variable of some kind of equation, so never a named value. */
|
|
63
|
-
const VARIABLE_NAMES: ReadonlySet<string> = new Set(["r", ...Object.values(EQUATION_VARIABLES).flat()]);
|
|
64
|
-
|
|
65
|
-
const named = createBoundedCache<readonly string[]>(256);
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Every name `source` reads, in the order it first names them, variables
|
|
69
|
-
* included. Empty for text that does not parse.
|
|
70
|
-
*/
|
|
71
|
-
export function equationNames(source: string): readonly string[] {
|
|
72
|
-
const hit = named.get(source);
|
|
73
|
-
if (hit !== undefined) return hit;
|
|
74
|
-
const names = new Set<string>();
|
|
75
|
-
const text = source.trim();
|
|
76
|
-
try {
|
|
77
|
-
for (const part of splitTuple(text) ?? splitTopLevel(text, "=")) {
|
|
78
|
-
collectNames(expressionOps.parse(part.trim(), MATH_DIALECT).root, names);
|
|
79
|
-
}
|
|
80
|
-
} catch {
|
|
81
|
-
names.clear();
|
|
82
|
-
}
|
|
83
|
-
const result = [...names];
|
|
84
|
-
named.set(source, result);
|
|
85
|
-
return result;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* The named values `source` reads: every name that is not a variable, a
|
|
90
|
-
* function or a constant.
|
|
91
|
-
*/
|
|
92
|
-
export function equationParams(source: string): readonly string[] {
|
|
93
|
-
return equationNames(source).filter((name) => !VARIABLE_NAMES.has(name));
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
function collectNames(node: ExpressionNode, into: Set<string>): void {
|
|
97
|
-
switch (node.kind) {
|
|
98
|
-
case "ref":
|
|
99
|
-
into.add(node.name);
|
|
100
|
-
return;
|
|
101
|
-
case "unary":
|
|
102
|
-
collectNames(node.operand, into);
|
|
103
|
-
return;
|
|
104
|
-
case "binary":
|
|
105
|
-
collectNames(node.left, into);
|
|
106
|
-
collectNames(node.right, into);
|
|
107
|
-
return;
|
|
108
|
-
case "call":
|
|
109
|
-
for (const arg of node.args) collectNames(arg, into);
|
|
110
|
-
return;
|
|
111
|
-
default:
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
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
|
-
};
|
|
122
|
-
|
|
123
|
-
const pair = splitTuple(source);
|
|
124
|
-
if (pair?.length === 2) return { kind: "parametric", x: compile(pair[0], ["t"]), y: compile(pair[1], ["t"]) };
|
|
125
|
-
|
|
126
|
-
const sides = splitTopLevel(source, "=");
|
|
127
|
-
if (sides.length > 2) throw new Error("An equation has one '='");
|
|
128
|
-
if (sides.length === 1) return { kind: "function", of: "x", f: compile(source, ["x"]) };
|
|
129
|
-
|
|
130
|
-
const [left, right] = sides;
|
|
131
|
-
const subject = left.trim();
|
|
132
|
-
const implicit = (): Equation => {
|
|
133
|
-
const l = compile(left, ["x", "y"]);
|
|
134
|
-
const r = compile(right, ["x", "y"]);
|
|
135
|
-
return { kind: "implicit", f: (x, y, scope) => l(x, y, scope) - r(x, y, scope) };
|
|
136
|
-
};
|
|
137
|
-
if (subject === "r") return { kind: "polar", f: compile(right, ["theta"]) };
|
|
138
|
-
if (subject === "y" || subject === "x") {
|
|
139
|
-
const of = subject === "y" ? "x" : "y";
|
|
140
|
-
// `y = x + y` names its own subject on the right, so it is not a function of one variable.
|
|
141
|
-
try {
|
|
142
|
-
return { kind: "function", of, f: compile(right, [of]) };
|
|
143
|
-
} catch (error) {
|
|
144
|
-
try {
|
|
145
|
-
return implicit();
|
|
146
|
-
} catch {
|
|
147
|
-
throw error;
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
return implicit();
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
/** `(a, b, …)` as its parts, or `null` when `source` is not one parenthesised list of two or more. */
|
|
155
|
-
export function splitTuple(source: string): string[] | null {
|
|
156
|
-
if (!source.startsWith("(") || !source.endsWith(")")) return null;
|
|
157
|
-
let depth = 0;
|
|
158
|
-
for (let i = 0; i < source.length - 1; i++) {
|
|
159
|
-
if (source[i] === "(") depth++;
|
|
160
|
-
else if (source[i] === ")" && --depth === 0) return null;
|
|
161
|
-
}
|
|
162
|
-
const parts = splitTopLevel(source.slice(1, -1), ",");
|
|
163
|
-
return parts.length >= 2 ? parts : null;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
export function splitTopLevel(source: string, separator: string): string[] {
|
|
167
|
-
const parts: string[] = [];
|
|
168
|
-
let depth = 0;
|
|
169
|
-
let from = 0;
|
|
170
|
-
for (let i = 0; i < source.length; i++) {
|
|
171
|
-
const c = source[i];
|
|
172
|
-
if (c === "(") depth++;
|
|
173
|
-
else if (c === ")") depth--;
|
|
174
|
-
else if (c === separator && depth === 0) {
|
|
175
|
-
parts.push(source.slice(from, i));
|
|
176
|
-
from = i + 1;
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
parts.push(source.slice(from));
|
|
180
|
-
return parts;
|
|
181
|
-
}
|
|
1
|
+
import { expressionOps, type ExpressionNode } from "@motionscript/sdk";
|
|
2
|
+
import { createBoundedCache } from "./cache";
|
|
3
|
+
import { compileExpressionCached, MATH_DIALECT, type CompiledExpression } from "./expression";
|
|
4
|
+
|
|
5
|
+
/**
|
|
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` |
|
|
15
|
+
*/
|
|
16
|
+
export type Equation =
|
|
17
|
+
| { kind: "function"; of: "x" | "y"; f: CompiledExpression }
|
|
18
|
+
| { kind: "polar"; f: CompiledExpression }
|
|
19
|
+
| { kind: "parametric"; x: CompiledExpression; y: CompiledExpression }
|
|
20
|
+
/** Drawn where `f(x, y)` is zero. */
|
|
21
|
+
| { kind: "implicit"; f: CompiledExpression };
|
|
22
|
+
|
|
23
|
+
/** The free variable each kind is sampled over, for a host's help text. */
|
|
24
|
+
export const EQUATION_VARIABLES = {
|
|
25
|
+
function: ["x"],
|
|
26
|
+
polar: ["theta"],
|
|
27
|
+
parametric: ["t"],
|
|
28
|
+
implicit: ["x", "y"],
|
|
29
|
+
} as const satisfies Record<Equation["kind"], readonly string[]>;
|
|
30
|
+
|
|
31
|
+
const parsed = createBoundedCache<Equation | Error>(256);
|
|
32
|
+
|
|
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
|
+
*/
|
|
56
|
+
export function equationError(source: string): string | null {
|
|
57
|
+
if (source.trim() === "") return null;
|
|
58
|
+
const equation = parseEquation(source, equationParams(source));
|
|
59
|
+
return equation instanceof Error ? equation.message : null;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** A name that is always a variable of some kind of equation, so never a named value. */
|
|
63
|
+
const VARIABLE_NAMES: ReadonlySet<string> = new Set(["r", ...Object.values(EQUATION_VARIABLES).flat()]);
|
|
64
|
+
|
|
65
|
+
const named = createBoundedCache<readonly string[]>(256);
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Every name `source` reads, in the order it first names them, variables
|
|
69
|
+
* included. Empty for text that does not parse.
|
|
70
|
+
*/
|
|
71
|
+
export function equationNames(source: string): readonly string[] {
|
|
72
|
+
const hit = named.get(source);
|
|
73
|
+
if (hit !== undefined) return hit;
|
|
74
|
+
const names = new Set<string>();
|
|
75
|
+
const text = source.trim();
|
|
76
|
+
try {
|
|
77
|
+
for (const part of splitTuple(text) ?? splitTopLevel(text, "=")) {
|
|
78
|
+
collectNames(expressionOps.parse(part.trim(), MATH_DIALECT).root, names);
|
|
79
|
+
}
|
|
80
|
+
} catch {
|
|
81
|
+
names.clear();
|
|
82
|
+
}
|
|
83
|
+
const result = [...names];
|
|
84
|
+
named.set(source, result);
|
|
85
|
+
return result;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* The named values `source` reads: every name that is not a variable, a
|
|
90
|
+
* function or a constant.
|
|
91
|
+
*/
|
|
92
|
+
export function equationParams(source: string): readonly string[] {
|
|
93
|
+
return equationNames(source).filter((name) => !VARIABLE_NAMES.has(name));
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function collectNames(node: ExpressionNode, into: Set<string>): void {
|
|
97
|
+
switch (node.kind) {
|
|
98
|
+
case "ref":
|
|
99
|
+
into.add(node.name);
|
|
100
|
+
return;
|
|
101
|
+
case "unary":
|
|
102
|
+
collectNames(node.operand, into);
|
|
103
|
+
return;
|
|
104
|
+
case "binary":
|
|
105
|
+
collectNames(node.left, into);
|
|
106
|
+
collectNames(node.right, into);
|
|
107
|
+
return;
|
|
108
|
+
case "call":
|
|
109
|
+
for (const arg of node.args) collectNames(arg, into);
|
|
110
|
+
return;
|
|
111
|
+
default:
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
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
|
+
};
|
|
122
|
+
|
|
123
|
+
const pair = splitTuple(source);
|
|
124
|
+
if (pair?.length === 2) return { kind: "parametric", x: compile(pair[0], ["t"]), y: compile(pair[1], ["t"]) };
|
|
125
|
+
|
|
126
|
+
const sides = splitTopLevel(source, "=");
|
|
127
|
+
if (sides.length > 2) throw new Error("An equation has one '='");
|
|
128
|
+
if (sides.length === 1) return { kind: "function", of: "x", f: compile(source, ["x"]) };
|
|
129
|
+
|
|
130
|
+
const [left, right] = sides;
|
|
131
|
+
const subject = left.trim();
|
|
132
|
+
const implicit = (): Equation => {
|
|
133
|
+
const l = compile(left, ["x", "y"]);
|
|
134
|
+
const r = compile(right, ["x", "y"]);
|
|
135
|
+
return { kind: "implicit", f: (x, y, scope) => l(x, y, scope) - r(x, y, scope) };
|
|
136
|
+
};
|
|
137
|
+
if (subject === "r") return { kind: "polar", f: compile(right, ["theta"]) };
|
|
138
|
+
if (subject === "y" || subject === "x") {
|
|
139
|
+
const of = subject === "y" ? "x" : "y";
|
|
140
|
+
// `y = x + y` names its own subject on the right, so it is not a function of one variable.
|
|
141
|
+
try {
|
|
142
|
+
return { kind: "function", of, f: compile(right, [of]) };
|
|
143
|
+
} catch (error) {
|
|
144
|
+
try {
|
|
145
|
+
return implicit();
|
|
146
|
+
} catch {
|
|
147
|
+
throw error;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return implicit();
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** `(a, b, …)` as its parts, or `null` when `source` is not one parenthesised list of two or more. */
|
|
155
|
+
export function splitTuple(source: string): string[] | null {
|
|
156
|
+
if (!source.startsWith("(") || !source.endsWith(")")) return null;
|
|
157
|
+
let depth = 0;
|
|
158
|
+
for (let i = 0; i < source.length - 1; i++) {
|
|
159
|
+
if (source[i] === "(") depth++;
|
|
160
|
+
else if (source[i] === ")" && --depth === 0) return null;
|
|
161
|
+
}
|
|
162
|
+
const parts = splitTopLevel(source.slice(1, -1), ",");
|
|
163
|
+
return parts.length >= 2 ? parts : null;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export function splitTopLevel(source: string, separator: string): string[] {
|
|
167
|
+
const parts: string[] = [];
|
|
168
|
+
let depth = 0;
|
|
169
|
+
let from = 0;
|
|
170
|
+
for (let i = 0; i < source.length; i++) {
|
|
171
|
+
const c = source[i];
|
|
172
|
+
if (c === "(") depth++;
|
|
173
|
+
else if (c === ")") depth--;
|
|
174
|
+
else if (c === separator && depth === 0) {
|
|
175
|
+
parts.push(source.slice(from, i));
|
|
176
|
+
from = i + 1;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
parts.push(source.slice(from));
|
|
180
|
+
return parts;
|
|
181
|
+
}
|
package/src/kit/equation3d.ts
CHANGED
|
@@ -1,137 +1,137 @@
|
|
|
1
|
-
import { createBoundedCache } from "./cache";
|
|
2
|
-
import { equationError, equationNames, equationParams, EQUATION_VARIABLES, splitTopLevel, splitTuple } from "./equation";
|
|
3
|
-
import { compileExpressionCached, type CompiledExpression } from "./expression";
|
|
4
|
-
import { fieldError, fieldParams, FIELD_VARIABLES } from "./field";
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* A surface's equation, read the way it is written:
|
|
8
|
-
*
|
|
9
|
-
* | Written as | Is |
|
|
10
|
-
* |---|---|
|
|
11
|
-
* | `sin(x) * cos(y)`, `z = x^2 - y^2` | a height over `x` and `y` |
|
|
12
|
-
* | `(cos(u) sin(v), sin(u) sin(v), cos(v))` | parametric, in `u` and `v` |
|
|
13
|
-
*/
|
|
14
|
-
export type SurfaceEquation =
|
|
15
|
-
| { kind: "height"; f: CompiledExpression }
|
|
16
|
-
| { kind: "parametric"; x: CompiledExpression; y: CompiledExpression; z: CompiledExpression };
|
|
17
|
-
|
|
18
|
-
/** A curve through space, three parts in `t`: `(cos(t), sin(t), t / 4)`. */
|
|
19
|
-
export interface SpaceCurveEquation {
|
|
20
|
-
x: CompiledExpression;
|
|
21
|
-
y: CompiledExpression;
|
|
22
|
-
z: CompiledExpression;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/** What each 3D form is written over, for a host's help text. */
|
|
26
|
-
export const EQUATION_VARIABLES_3D = {
|
|
27
|
-
height: ["x", "y"],
|
|
28
|
-
surface: ["u", "v"],
|
|
29
|
-
spaceCurve: ["t"],
|
|
30
|
-
} as const;
|
|
31
|
-
|
|
32
|
-
const SURFACE_NAMES: ReadonlySet<string> = new Set(["x", "y", "z", "u", "v"]);
|
|
33
|
-
const SPACE_CURVE_NAMES: ReadonlySet<string> = new Set(["t"]);
|
|
34
|
-
|
|
35
|
-
const surfaces = createBoundedCache<SurfaceEquation | Error>(128);
|
|
36
|
-
const curves = createBoundedCache<SpaceCurveEquation | Error>(128);
|
|
37
|
-
|
|
38
|
-
/** The named values a surface's equation reads: every name that is not one of its variables. */
|
|
39
|
-
export function surfaceParams(source: string): readonly string[] {
|
|
40
|
-
return equationNames(source).filter((name) => !SURFACE_NAMES.has(name));
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/** The named values a space curve's equation reads. */
|
|
44
|
-
export function spaceCurveParams(source: string): readonly string[] {
|
|
45
|
-
return equationNames(source).filter((name) => !SPACE_CURVE_NAMES.has(name));
|
|
46
|
-
}
|
|
47
|
-
|
|
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
|
-
}
|
|
64
|
-
|
|
65
|
-
/** `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
|
-
}
|
|
75
|
-
|
|
76
|
-
/** Why `source` will not draw as a surface, or `null`; empty text is not an error. */
|
|
77
|
-
export function surfaceError(source: string): string | null {
|
|
78
|
-
if (source.trim() === "") return null;
|
|
79
|
-
const equation = parseSurface(source, surfaceParams(source));
|
|
80
|
-
return equation instanceof Error ? equation.message : null;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/** Why `source` will not draw as a space curve, or `null`. */
|
|
84
|
-
export function spaceCurveError(source: string): string | null {
|
|
85
|
-
if (source.trim() === "") return null;
|
|
86
|
-
const equation = parseSpaceCurve(source, spaceCurveParams(source));
|
|
87
|
-
return equation instanceof Error ? equation.message : null;
|
|
88
|
-
}
|
|
89
|
-
|
|
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
|
-
/** How a host checks an equation as it is typed, and finds its named values, by the `kind` its property declares. */
|
|
120
|
-
export interface EquationLanguage {
|
|
121
|
-
error(source: string): string | null;
|
|
122
|
-
params(source: string): readonly string[];
|
|
123
|
-
variables: readonly string[];
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
export const EQUATION_KINDS: Readonly<
|
|
127
|
-
Record<"equation" | "surfaceEquation" | "spaceCurveEquation" | "fieldEquation", EquationLanguage>
|
|
128
|
-
> = {
|
|
129
|
-
equation: { error: equationError, params: equationParams, variables: [...new Set(Object.values(EQUATION_VARIABLES).flat())] },
|
|
130
|
-
surfaceEquation: {
|
|
131
|
-
error: surfaceError,
|
|
132
|
-
params: surfaceParams,
|
|
133
|
-
variables: [...EQUATION_VARIABLES_3D.height, ...EQUATION_VARIABLES_3D.surface],
|
|
134
|
-
},
|
|
135
|
-
spaceCurveEquation: { error: spaceCurveError, params: spaceCurveParams, variables: EQUATION_VARIABLES_3D.spaceCurve },
|
|
136
|
-
fieldEquation: { error: fieldError, params: fieldParams, variables: FIELD_VARIABLES },
|
|
137
|
-
};
|
|
1
|
+
import { createBoundedCache } from "./cache";
|
|
2
|
+
import { equationError, equationNames, equationParams, EQUATION_VARIABLES, splitTopLevel, splitTuple } from "./equation";
|
|
3
|
+
import { compileExpressionCached, type CompiledExpression } from "./expression";
|
|
4
|
+
import { fieldError, fieldParams, FIELD_VARIABLES } from "./field";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A surface's equation, read the way it is written:
|
|
8
|
+
*
|
|
9
|
+
* | Written as | Is |
|
|
10
|
+
* |---|---|
|
|
11
|
+
* | `sin(x) * cos(y)`, `z = x^2 - y^2` | a height over `x` and `y` |
|
|
12
|
+
* | `(cos(u) sin(v), sin(u) sin(v), cos(v))` | parametric, in `u` and `v` |
|
|
13
|
+
*/
|
|
14
|
+
export type SurfaceEquation =
|
|
15
|
+
| { kind: "height"; f: CompiledExpression }
|
|
16
|
+
| { kind: "parametric"; x: CompiledExpression; y: CompiledExpression; z: CompiledExpression };
|
|
17
|
+
|
|
18
|
+
/** A curve through space, three parts in `t`: `(cos(t), sin(t), t / 4)`. */
|
|
19
|
+
export interface SpaceCurveEquation {
|
|
20
|
+
x: CompiledExpression;
|
|
21
|
+
y: CompiledExpression;
|
|
22
|
+
z: CompiledExpression;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** What each 3D form is written over, for a host's help text. */
|
|
26
|
+
export const EQUATION_VARIABLES_3D = {
|
|
27
|
+
height: ["x", "y"],
|
|
28
|
+
surface: ["u", "v"],
|
|
29
|
+
spaceCurve: ["t"],
|
|
30
|
+
} as const;
|
|
31
|
+
|
|
32
|
+
const SURFACE_NAMES: ReadonlySet<string> = new Set(["x", "y", "z", "u", "v"]);
|
|
33
|
+
const SPACE_CURVE_NAMES: ReadonlySet<string> = new Set(["t"]);
|
|
34
|
+
|
|
35
|
+
const surfaces = createBoundedCache<SurfaceEquation | Error>(128);
|
|
36
|
+
const curves = createBoundedCache<SpaceCurveEquation | Error>(128);
|
|
37
|
+
|
|
38
|
+
/** The named values a surface's equation reads: every name that is not one of its variables. */
|
|
39
|
+
export function surfaceParams(source: string): readonly string[] {
|
|
40
|
+
return equationNames(source).filter((name) => !SURFACE_NAMES.has(name));
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** The named values a space curve's equation reads. */
|
|
44
|
+
export function spaceCurveParams(source: string): readonly string[] {
|
|
45
|
+
return equationNames(source).filter((name) => !SPACE_CURVE_NAMES.has(name));
|
|
46
|
+
}
|
|
47
|
+
|
|
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
|
+
}
|
|
64
|
+
|
|
65
|
+
/** `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
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Why `source` will not draw as a surface, or `null`; empty text is not an error. */
|
|
77
|
+
export function surfaceError(source: string): string | null {
|
|
78
|
+
if (source.trim() === "") return null;
|
|
79
|
+
const equation = parseSurface(source, surfaceParams(source));
|
|
80
|
+
return equation instanceof Error ? equation.message : null;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Why `source` will not draw as a space curve, or `null`. */
|
|
84
|
+
export function spaceCurveError(source: string): string | null {
|
|
85
|
+
if (source.trim() === "") return null;
|
|
86
|
+
const equation = parseSpaceCurve(source, spaceCurveParams(source));
|
|
87
|
+
return equation instanceof Error ? equation.message : null;
|
|
88
|
+
}
|
|
89
|
+
|
|
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
|
+
/** How a host checks an equation as it is typed, and finds its named values, by the `kind` its property declares. */
|
|
120
|
+
export interface EquationLanguage {
|
|
121
|
+
error(source: string): string | null;
|
|
122
|
+
params(source: string): readonly string[];
|
|
123
|
+
variables: readonly string[];
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export const EQUATION_KINDS: Readonly<
|
|
127
|
+
Record<"equation" | "surfaceEquation" | "spaceCurveEquation" | "fieldEquation", EquationLanguage>
|
|
128
|
+
> = {
|
|
129
|
+
equation: { error: equationError, params: equationParams, variables: [...new Set(Object.values(EQUATION_VARIABLES).flat())] },
|
|
130
|
+
surfaceEquation: {
|
|
131
|
+
error: surfaceError,
|
|
132
|
+
params: surfaceParams,
|
|
133
|
+
variables: [...EQUATION_VARIABLES_3D.height, ...EQUATION_VARIABLES_3D.surface],
|
|
134
|
+
},
|
|
135
|
+
spaceCurveEquation: { error: spaceCurveError, params: spaceCurveParams, variables: EQUATION_VARIABLES_3D.spaceCurve },
|
|
136
|
+
fieldEquation: { error: fieldError, params: fieldParams, variables: FIELD_VARIABLES },
|
|
137
|
+
};
|