@nakafa/aksara-contracts 6.0.0 → 6.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.
- package/dist/assessment/figure.d.ts +468 -0
- package/dist/math/bounds.d.ts +3 -2
- package/dist/math/bounds.js +37 -8
- package/dist/math/curve.d.ts +42 -0
- package/dist/math/curve.js +55 -0
- package/dist/math/formula/box.d.ts +61 -0
- package/dist/math/formula/box.js +68 -0
- package/dist/math/formula/compile.d.ts +11 -0
- package/dist/math/formula/compile.js +100 -0
- package/dist/math/formula/contour.d.ts +17 -0
- package/dist/math/formula/contour.js +162 -0
- package/dist/math/formula/field.d.ts +19 -0
- package/dist/math/formula/field.js +35 -0
- package/dist/math/formula/mesh.d.ts +25 -0
- package/dist/math/formula/mesh.js +28 -0
- package/dist/math/formula/names.d.ts +4 -0
- package/dist/math/formula/names.js +23 -0
- package/dist/math/formula/parse.d.ts +10 -0
- package/dist/math/formula/parse.js +240 -0
- package/dist/math/formula/plane.d.ts +62 -0
- package/dist/math/formula/plane.js +175 -0
- package/dist/math/formula/reader.d.ts +102 -0
- package/dist/math/formula/reader.js +58 -0
- package/dist/math/formula/sample.d.ts +14 -0
- package/dist/math/formula/sample.js +182 -0
- package/dist/math/formula/scope.d.ts +37 -0
- package/dist/math/formula/scope.js +137 -0
- package/dist/math/formula/space.d.ts +10 -0
- package/dist/math/formula/space.js +94 -0
- package/dist/math/formula/syntax.d.ts +77 -0
- package/dist/math/formula/syntax.js +111 -0
- package/dist/math/formula/token.d.ts +43 -0
- package/dist/math/formula/token.js +150 -0
- package/dist/math/plane.d.ts +85 -0
- package/dist/math/plane.js +8 -2
- package/dist/math/scene.js +11 -2
- package/dist/math/solid.d.ts +124 -0
- package/dist/math/solid.js +86 -0
- package/dist/math/space.d.ts +213 -0
- package/dist/math/space.js +8 -2
- package/dist/math/visual.d.ts +454 -0
- package/package.json +57 -1
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Result } from "effect";
|
|
2
|
+
import { type FormulaError, type FormulaNode } from "#contracts/math/formula/syntax";
|
|
3
|
+
/** Reads a formula that is one expression, such as `2x^2 - 3x + 1`. */
|
|
4
|
+
export declare function readFormula(text: string): Result.Result<FormulaNode, FormulaError>;
|
|
5
|
+
/** Reads one equation or inequality, such as `x^2 + y^2 = 4` or `y < 2x + 1`. */
|
|
6
|
+
export declare function readRelation(text: string): Result.Result<{
|
|
7
|
+
comparison: "<" | "<=" | "=" | ">" | ">=";
|
|
8
|
+
left: FormulaNode;
|
|
9
|
+
right: FormulaNode;
|
|
10
|
+
}, FormulaError>;
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import { Array as Arr, MutableList, Option, Result, Schema } from "effect";
|
|
2
|
+
import { at, expect, finished, here, peek, problemAt, startReader, } from "#contracts/math/formula/reader";
|
|
3
|
+
import { FormulaFunctionSchema, } from "#contracts/math/formula/syntax";
|
|
4
|
+
/** How tightly each operator holds its operands; implicit products bind like `*`. */
|
|
5
|
+
const SUM = 10;
|
|
6
|
+
const PRODUCT = 20;
|
|
7
|
+
const SIGN = 25;
|
|
8
|
+
const POWER = 30;
|
|
9
|
+
const DEGREE = 40;
|
|
10
|
+
const isFunction = Schema.is(FormulaFunctionSchema);
|
|
11
|
+
/** Whether the current token can begin a factor of an implicit product. */
|
|
12
|
+
function startsFactor(reader) {
|
|
13
|
+
return Option.exists(peek(reader), (token) => {
|
|
14
|
+
if (token.kind === "number" || token.kind === "name") {
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
return (token.kind === "symbol" &&
|
|
18
|
+
(token.symbol === "(" ||
|
|
19
|
+
token.symbol === "√" ||
|
|
20
|
+
(token.symbol === "|" && reader.bars === 0)));
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
/** Reads the comma-separated arguments of one call, checking their count. */
|
|
24
|
+
function readArguments(reader, callee, start) {
|
|
25
|
+
return Result.gen(function* () {
|
|
26
|
+
if (!at(reader, "(")) {
|
|
27
|
+
return yield* problemAt("function-arguments", start, `Expected "(" after ${callee}.`);
|
|
28
|
+
}
|
|
29
|
+
reader.index += 1;
|
|
30
|
+
const outerBars = reader.bars;
|
|
31
|
+
reader.bars = 0;
|
|
32
|
+
const first = yield* readExpression(reader, 0);
|
|
33
|
+
const rest = MutableList.make();
|
|
34
|
+
while (at(reader, ",")) {
|
|
35
|
+
reader.index += 1;
|
|
36
|
+
MutableList.append(rest, yield* readExpression(reader, 0));
|
|
37
|
+
}
|
|
38
|
+
yield* expect(reader, ")");
|
|
39
|
+
reader.bars = outerBars;
|
|
40
|
+
const args = [
|
|
41
|
+
first,
|
|
42
|
+
...MutableList.toArray(rest),
|
|
43
|
+
];
|
|
44
|
+
const many = callee === "max" || callee === "min";
|
|
45
|
+
if (many ? args.length < 2 : args.length !== 1) {
|
|
46
|
+
return yield* problemAt("function-arguments", start, many
|
|
47
|
+
? `${callee} takes two or more arguments.`
|
|
48
|
+
: `${callee} takes one argument.`);
|
|
49
|
+
}
|
|
50
|
+
return args;
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
/** Reads a function call, with `sin^2(x)` powers and `log_b(x)` bases. */
|
|
54
|
+
function readCall(reader, callee, start) {
|
|
55
|
+
return Result.gen(function* () {
|
|
56
|
+
const base = callee === "log" && at(reader, "_")
|
|
57
|
+
? Option.some(yield* readSubscript(reader))
|
|
58
|
+
: Option.none();
|
|
59
|
+
const power = at(reader, "^")
|
|
60
|
+
? Option.some(yield* readExponent(reader))
|
|
61
|
+
: Option.none();
|
|
62
|
+
const args = yield* readArguments(reader, callee, start);
|
|
63
|
+
const [value] = args;
|
|
64
|
+
const call = Option.match(base, {
|
|
65
|
+
onNone: () => ({ args, callee, kind: "call" }),
|
|
66
|
+
onSome: (each) => ({ base: each, kind: "logarithm", value }),
|
|
67
|
+
});
|
|
68
|
+
return Option.match(power, {
|
|
69
|
+
onNone: () => call,
|
|
70
|
+
onSome: (right) => ({
|
|
71
|
+
kind: "binary",
|
|
72
|
+
left: call,
|
|
73
|
+
operator: "^",
|
|
74
|
+
right,
|
|
75
|
+
}),
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
/** Reads the `_b` base of a logarithm: one number, name, or group. */
|
|
80
|
+
function readSubscript(reader) {
|
|
81
|
+
reader.index += 1;
|
|
82
|
+
return readPrefix(reader);
|
|
83
|
+
}
|
|
84
|
+
/** Reads the `^n` power written between a function's name and its arguments. */
|
|
85
|
+
function readExponent(reader) {
|
|
86
|
+
reader.index += 1;
|
|
87
|
+
return readPrefix(reader);
|
|
88
|
+
}
|
|
89
|
+
/** Reads what one symbol opens: a group, absolute bars, a root, or a sign. */
|
|
90
|
+
function readSymbol(reader, symbol, position) {
|
|
91
|
+
return Result.gen(function* () {
|
|
92
|
+
if (symbol === "(") {
|
|
93
|
+
const outerBars = reader.bars;
|
|
94
|
+
reader.bars = 0;
|
|
95
|
+
const inner = yield* readExpression(reader, 0);
|
|
96
|
+
yield* expect(reader, ")");
|
|
97
|
+
reader.bars = outerBars;
|
|
98
|
+
return inner;
|
|
99
|
+
}
|
|
100
|
+
if (symbol === "|") {
|
|
101
|
+
reader.bars += 1;
|
|
102
|
+
const inner = yield* readExpression(reader, 0);
|
|
103
|
+
yield* expect(reader, "|");
|
|
104
|
+
reader.bars -= 1;
|
|
105
|
+
return {
|
|
106
|
+
args: [inner],
|
|
107
|
+
callee: "abs",
|
|
108
|
+
kind: "call",
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
if (symbol === "√") {
|
|
112
|
+
const inner = yield* readExpression(reader, POWER);
|
|
113
|
+
return {
|
|
114
|
+
args: [inner],
|
|
115
|
+
callee: "sqrt",
|
|
116
|
+
kind: "call",
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
if (symbol === "-" || symbol === "+") {
|
|
120
|
+
const operand = yield* readExpression(reader, SIGN);
|
|
121
|
+
return symbol === "-"
|
|
122
|
+
? { kind: "negate", operand }
|
|
123
|
+
: operand;
|
|
124
|
+
}
|
|
125
|
+
return yield* problemAt("value-expected", position, `Expected a value, not "${symbol}".`);
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
/** Reads one factor: a number, a name, a call, or what a symbol opens. */
|
|
129
|
+
function readPrefix(reader) {
|
|
130
|
+
return Result.gen(function* () {
|
|
131
|
+
const token = peek(reader);
|
|
132
|
+
if (Option.isNone(token)) {
|
|
133
|
+
return yield* problemAt("value-expected", reader.length, "Expected a value.");
|
|
134
|
+
}
|
|
135
|
+
const current = token.value;
|
|
136
|
+
reader.index += 1;
|
|
137
|
+
if (current.kind === "number") {
|
|
138
|
+
return { kind: "number", value: current.value };
|
|
139
|
+
}
|
|
140
|
+
if (current.kind === "name") {
|
|
141
|
+
return isFunction(current.name)
|
|
142
|
+
? yield* readCall(reader, current.name, current.position)
|
|
143
|
+
: { kind: "name", name: current.name };
|
|
144
|
+
}
|
|
145
|
+
if (current.kind === "comparison") {
|
|
146
|
+
return yield* problemAt("comparison-unexpected", current.position, "Expected a value before the comparison.");
|
|
147
|
+
}
|
|
148
|
+
return yield* readSymbol(reader, current.symbol, current.position);
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
/** How tightly each written operator binds, and how its right side reads. */
|
|
152
|
+
const INFIX = [
|
|
153
|
+
{ operator: "+", power: SUM, right: SUM + 1 },
|
|
154
|
+
{ operator: "-", power: SUM, right: SUM + 1 },
|
|
155
|
+
{ operator: "*", power: PRODUCT, right: PRODUCT + 1 },
|
|
156
|
+
{ operator: "/", power: PRODUCT, right: PRODUCT + 1 },
|
|
157
|
+
{ operator: "^", power: POWER, right: POWER - 1 },
|
|
158
|
+
];
|
|
159
|
+
/** The written operator ahead and its binding, if one stands there. */
|
|
160
|
+
function infixAhead(reader) {
|
|
161
|
+
return Arr.findFirst(INFIX, ({ operator }) => at(reader, operator));
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Extends what has been read with every operator ahead that binds tighter
|
|
165
|
+
* than `minimum`: degrees, written operators, and implicit products.
|
|
166
|
+
*/
|
|
167
|
+
function extend(reader, minimum, left) {
|
|
168
|
+
return Result.gen(function* () {
|
|
169
|
+
if (at(reader, "°") && DEGREE > minimum) {
|
|
170
|
+
reader.index += 1;
|
|
171
|
+
const right = { kind: "number", value: Math.PI / 180 };
|
|
172
|
+
return yield* extend(reader, minimum, {
|
|
173
|
+
kind: "binary",
|
|
174
|
+
left,
|
|
175
|
+
operator: "*",
|
|
176
|
+
right,
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
const infix = infixAhead(reader);
|
|
180
|
+
if (Option.isSome(infix)) {
|
|
181
|
+
if (infix.value.power <= minimum) {
|
|
182
|
+
return left;
|
|
183
|
+
}
|
|
184
|
+
reader.index += 1;
|
|
185
|
+
const right = yield* readExpression(reader, infix.value.right);
|
|
186
|
+
const { operator } = infix.value;
|
|
187
|
+
return yield* extend(reader, minimum, {
|
|
188
|
+
kind: "binary",
|
|
189
|
+
left,
|
|
190
|
+
operator,
|
|
191
|
+
right,
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
if (startsFactor(reader) && PRODUCT > minimum) {
|
|
195
|
+
const right = yield* readExpression(reader, PRODUCT + 1);
|
|
196
|
+
return yield* extend(reader, minimum, {
|
|
197
|
+
kind: "binary",
|
|
198
|
+
left,
|
|
199
|
+
operator: "*",
|
|
200
|
+
right,
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
return left;
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
/** Reads operators that bind tighter than `minimum`, the core of a Pratt parser. */
|
|
207
|
+
function readExpression(reader, minimum) {
|
|
208
|
+
return Result.flatMap(readPrefix(reader), (left) => extend(reader, minimum, left));
|
|
209
|
+
}
|
|
210
|
+
/** Reads a formula that is one expression, such as `2x^2 - 3x + 1`. */
|
|
211
|
+
export function readFormula(text) {
|
|
212
|
+
return Result.gen(function* () {
|
|
213
|
+
const reader = yield* startReader(text);
|
|
214
|
+
if (reader.tokens.length === 0) {
|
|
215
|
+
return yield* problemAt("empty", 0, "Expected a formula.");
|
|
216
|
+
}
|
|
217
|
+
const node = yield* readExpression(reader, 0);
|
|
218
|
+
yield* finished(reader);
|
|
219
|
+
return node;
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
/** Reads one equation or inequality, such as `x^2 + y^2 = 4` or `y < 2x + 1`. */
|
|
223
|
+
export function readRelation(text) {
|
|
224
|
+
return Result.gen(function* () {
|
|
225
|
+
const reader = yield* startReader(text);
|
|
226
|
+
if (reader.tokens.length === 0) {
|
|
227
|
+
return yield* problemAt("empty", 0, "Expected a formula.");
|
|
228
|
+
}
|
|
229
|
+
const left = yield* readExpression(reader, 0);
|
|
230
|
+
const token = peek(reader);
|
|
231
|
+
if (Option.isNone(token) || token.value.kind !== "comparison") {
|
|
232
|
+
return yield* problemAt("comparison-expected", here(reader), "Expected =, <, <=, >, or >=.");
|
|
233
|
+
}
|
|
234
|
+
const comparison = token.value.comparison;
|
|
235
|
+
reader.index += 1;
|
|
236
|
+
const right = yield* readExpression(reader, 0);
|
|
237
|
+
yield* finished(reader);
|
|
238
|
+
return { comparison, left, right };
|
|
239
|
+
});
|
|
240
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { Result, Schema } from "effect";
|
|
2
|
+
import type { PlaneCurveObjectSchemas } from "#contracts/math/curve";
|
|
3
|
+
import { type SampleBox } from "#contracts/math/formula/box";
|
|
4
|
+
import { type FormulaEvaluator, type FormulaScope } from "#contracts/math/formula/compile";
|
|
5
|
+
import type { FormulaError } from "#contracts/math/formula/syntax";
|
|
6
|
+
/**
|
|
7
|
+
* What a formula object draws once sampled: curves, a shaded region with
|
|
8
|
+
* solid or dashed edges, a surface grid, or the flat faces of a solid.
|
|
9
|
+
*/
|
|
10
|
+
export declare const FormulaGeometrySchema: Schema.Union<readonly [Schema.Struct<{
|
|
11
|
+
readonly branches: Schema.$Array<Schema.$Array<Schema.Struct<{
|
|
12
|
+
readonly x: Schema.Finite;
|
|
13
|
+
readonly y: Schema.Finite;
|
|
14
|
+
readonly z: Schema.Finite;
|
|
15
|
+
}>>>;
|
|
16
|
+
readonly kind: Schema.Literal<"curves">;
|
|
17
|
+
}>, Schema.Struct<{
|
|
18
|
+
readonly boundary: Schema.$Array<Schema.Struct<{
|
|
19
|
+
readonly points: Schema.$Array<Schema.Struct<{
|
|
20
|
+
readonly x: Schema.Finite;
|
|
21
|
+
readonly y: Schema.Finite;
|
|
22
|
+
readonly z: Schema.Finite;
|
|
23
|
+
}>>;
|
|
24
|
+
readonly strict: Schema.Boolean;
|
|
25
|
+
}>>;
|
|
26
|
+
readonly fill: Schema.$Array<Schema.$Array<Schema.Struct<{
|
|
27
|
+
readonly x: Schema.Finite;
|
|
28
|
+
readonly y: Schema.Finite;
|
|
29
|
+
readonly z: Schema.Finite;
|
|
30
|
+
}>>>;
|
|
31
|
+
readonly kind: Schema.Literal<"region">;
|
|
32
|
+
}>, Schema.Struct<{
|
|
33
|
+
readonly grid: Schema.Struct<{
|
|
34
|
+
readonly columns: Schema.Int;
|
|
35
|
+
readonly points: Schema.$Array<Schema.Option<Schema.Struct<{
|
|
36
|
+
readonly x: Schema.Finite;
|
|
37
|
+
readonly y: Schema.Finite;
|
|
38
|
+
readonly z: Schema.Finite;
|
|
39
|
+
}>>>;
|
|
40
|
+
readonly rows: Schema.Int;
|
|
41
|
+
}>;
|
|
42
|
+
readonly kind: Schema.Literal<"grid">;
|
|
43
|
+
}>, Schema.Struct<{
|
|
44
|
+
readonly faces: Schema.$Array<Schema.$Array<Schema.Struct<{
|
|
45
|
+
readonly x: Schema.Finite;
|
|
46
|
+
readonly y: Schema.Finite;
|
|
47
|
+
readonly z: Schema.Finite;
|
|
48
|
+
}>>>;
|
|
49
|
+
readonly kind: Schema.Literal<"faces">;
|
|
50
|
+
}>]>;
|
|
51
|
+
export type FormulaGeometry = typeof FormulaGeometrySchema.Type;
|
|
52
|
+
/** Compiles one authored expression into its evaluator. */
|
|
53
|
+
export declare function compileText(text: string): Result.Result<FormulaEvaluator, FormulaError>;
|
|
54
|
+
/** Evaluates one compiled formula with one more variable bound. */
|
|
55
|
+
export declare function evaluateWith(evaluate: FormulaEvaluator, scope: FormulaScope, name: string, value: number): number;
|
|
56
|
+
/** The plane objects a formula defines. */
|
|
57
|
+
export type PlaneFormulaObject = (typeof PlaneCurveObjectSchemas)[number]["Type"];
|
|
58
|
+
/**
|
|
59
|
+
* Samples one plane formula object inside its frame. `named` holds the input
|
|
60
|
+
* values a function must hit exactly, such as the x of each of its labels.
|
|
61
|
+
*/
|
|
62
|
+
export declare function planeFormulaGeometry(object: PlaneFormulaObject, box: SampleBox, scope: FormulaScope, named?: readonly number[]): Result.Result<FormulaGeometry, FormulaError>;
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { Array as Arr, MutableList, Option, Order, Result, Schema, } from "effect";
|
|
2
|
+
import { SamplePointSchema, } from "#contracts/math/formula/box";
|
|
3
|
+
import { compileFormula, } from "#contracts/math/formula/compile";
|
|
4
|
+
import { contourFill, contourLines, } from "#contracts/math/formula/contour";
|
|
5
|
+
import { SampleGridSchema } from "#contracts/math/formula/mesh";
|
|
6
|
+
import { readFormula, readRelation } from "#contracts/math/formula/parse";
|
|
7
|
+
import { sampleCurve } from "#contracts/math/formula/sample";
|
|
8
|
+
const BranchSchema = Schema.Array(SamplePointSchema);
|
|
9
|
+
/**
|
|
10
|
+
* What a formula object draws once sampled: curves, a shaded region with
|
|
11
|
+
* solid or dashed edges, a surface grid, or the flat faces of a solid.
|
|
12
|
+
*/
|
|
13
|
+
export const FormulaGeometrySchema = Schema.Union([
|
|
14
|
+
Schema.Struct({
|
|
15
|
+
branches: Schema.Array(BranchSchema),
|
|
16
|
+
kind: Schema.Literal("curves"),
|
|
17
|
+
}),
|
|
18
|
+
Schema.Struct({
|
|
19
|
+
boundary: Schema.Array(Schema.Struct({ points: BranchSchema, strict: Schema.Boolean })),
|
|
20
|
+
fill: Schema.Array(BranchSchema),
|
|
21
|
+
kind: Schema.Literal("region"),
|
|
22
|
+
}),
|
|
23
|
+
Schema.Struct({ grid: SampleGridSchema, kind: Schema.Literal("grid") }),
|
|
24
|
+
Schema.Struct({
|
|
25
|
+
faces: Schema.Array(BranchSchema),
|
|
26
|
+
kind: Schema.Literal("faces"),
|
|
27
|
+
}),
|
|
28
|
+
]);
|
|
29
|
+
/** Compiles one authored expression into its evaluator. */
|
|
30
|
+
export function compileText(text) {
|
|
31
|
+
return Result.map(readFormula(text), compileFormula);
|
|
32
|
+
}
|
|
33
|
+
/** Evaluates one compiled formula with one more variable bound. */
|
|
34
|
+
export function evaluateWith(evaluate, scope, name, value) {
|
|
35
|
+
return evaluate({ ...scope, [name]: value });
|
|
36
|
+
}
|
|
37
|
+
/** A plane point, with z kept at zero. */
|
|
38
|
+
function planePoint(x, y) {
|
|
39
|
+
return { x, y, z: 0 };
|
|
40
|
+
}
|
|
41
|
+
/** How far a boundary piece lies from one inequality's edge, and whether that edge is strict. */
|
|
42
|
+
const ConstraintGapSchema = Schema.Struct({
|
|
43
|
+
gap: Schema.Finite,
|
|
44
|
+
strict: Schema.Boolean,
|
|
45
|
+
});
|
|
46
|
+
/** One piece of a region's outline, between two consecutive samples. */
|
|
47
|
+
const EdgePieceSchema = Schema.Struct({
|
|
48
|
+
next: SamplePointSchema,
|
|
49
|
+
point: SamplePointSchema,
|
|
50
|
+
strict: Schema.Boolean,
|
|
51
|
+
});
|
|
52
|
+
/** One run of an outline that is all strict or all inclusive. */
|
|
53
|
+
const EdgeRunSchema = Schema.Struct({
|
|
54
|
+
points: Schema.Array(SamplePointSchema),
|
|
55
|
+
strict: Schema.Boolean,
|
|
56
|
+
});
|
|
57
|
+
/**
|
|
58
|
+
* Joins consecutive outline pieces of the same kind into one line, so a
|
|
59
|
+
* renderer draws a few dashed and solid edges instead of every short piece.
|
|
60
|
+
*/
|
|
61
|
+
function edgeRuns(pieces) {
|
|
62
|
+
const runs = MutableList.make();
|
|
63
|
+
let points = MutableList.make();
|
|
64
|
+
let strict = false;
|
|
65
|
+
Arr.forEach(pieces, (piece, index) => {
|
|
66
|
+
if (index === 0 || piece.strict !== strict) {
|
|
67
|
+
if (index > 0) {
|
|
68
|
+
MutableList.append(runs, {
|
|
69
|
+
points: MutableList.toArray(points),
|
|
70
|
+
strict,
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
points = MutableList.make();
|
|
74
|
+
MutableList.append(points, piece.point);
|
|
75
|
+
({ strict } = piece);
|
|
76
|
+
}
|
|
77
|
+
MutableList.append(points, piece.next);
|
|
78
|
+
});
|
|
79
|
+
MutableList.append(runs, { points: MutableList.toArray(points), strict });
|
|
80
|
+
return MutableList.toArray(runs);
|
|
81
|
+
}
|
|
82
|
+
/** The signed room an inequality leaves at a point: positive where it holds. */
|
|
83
|
+
function margin(text) {
|
|
84
|
+
return Result.map(readRelation(text), ({ comparison, left, right }) => {
|
|
85
|
+
const leftValue = compileFormula(left);
|
|
86
|
+
const rightValue = compileFormula(right);
|
|
87
|
+
const below = comparison === "<" || comparison === "<=";
|
|
88
|
+
return {
|
|
89
|
+
field: (scope) => (x, y) => {
|
|
90
|
+
const point = { ...scope, x, y };
|
|
91
|
+
const gap = rightValue(point) - leftValue(point);
|
|
92
|
+
return below ? gap : -gap;
|
|
93
|
+
},
|
|
94
|
+
strict: comparison === "<" || comparison === ">",
|
|
95
|
+
};
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
/** The region where every inequality holds, with each edge marked strict or not. */
|
|
99
|
+
function regionGeometry(inequalities, box, scope) {
|
|
100
|
+
return Result.map(Result.all(Arr.map(inequalities, margin)), (margins) => {
|
|
101
|
+
const constraints = Arr.map(margins, (each) => ({
|
|
102
|
+
field: each.field(scope),
|
|
103
|
+
strict: each.strict,
|
|
104
|
+
}));
|
|
105
|
+
/** The tightest margin at a point: where it is not negative, every inequality holds. */
|
|
106
|
+
const field = (x, y) => Math.min(...Arr.map(constraints, (each) => each.field(x, y)));
|
|
107
|
+
const byGap = Order.mapInput(Order.Number, (piece) => piece.gap);
|
|
108
|
+
/** Whether the inequality nearest a boundary piece is strict, so it draws dashed. */
|
|
109
|
+
const strictAt = (from, to) => {
|
|
110
|
+
const x = (from.x + to.x) / 2;
|
|
111
|
+
const y = (from.y + to.y) / 2;
|
|
112
|
+
const gaps = Arr.map(constraints, (each) => ({
|
|
113
|
+
gap: Math.abs(each.field(x, y)),
|
|
114
|
+
strict: each.strict,
|
|
115
|
+
}));
|
|
116
|
+
return Option.exists(Arr.head(Arr.sort(gaps, byGap)), ({ strict }) => strict);
|
|
117
|
+
};
|
|
118
|
+
const boundary = Arr.flatMap(contourLines(field, box), (line) => edgeRuns(Arr.zipWith(line, Arr.drop(line, 1), (point, next) => ({
|
|
119
|
+
next,
|
|
120
|
+
point,
|
|
121
|
+
strict: strictAt(point, next),
|
|
122
|
+
}))));
|
|
123
|
+
return {
|
|
124
|
+
boundary,
|
|
125
|
+
fill: contourFill(field, box),
|
|
126
|
+
kind: "region",
|
|
127
|
+
};
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Samples one plane formula object inside its frame. `named` holds the input
|
|
132
|
+
* values a function must hit exactly, such as the x of each of its labels.
|
|
133
|
+
*/
|
|
134
|
+
export function planeFormulaGeometry(object, box, scope, named = []) {
|
|
135
|
+
return Result.gen(function* () {
|
|
136
|
+
if (object.kind === "function") {
|
|
137
|
+
const f = yield* compileText(object.formula);
|
|
138
|
+
const axis = object.inputAxis ?? "x";
|
|
139
|
+
const range = object.domain ?? box[axis];
|
|
140
|
+
const branches = sampleCurve((input) => {
|
|
141
|
+
const output = evaluateWith(f, scope, axis, input);
|
|
142
|
+
return axis === "x"
|
|
143
|
+
? planePoint(input, output)
|
|
144
|
+
: planePoint(output, input);
|
|
145
|
+
}, box, range, named);
|
|
146
|
+
return { branches, kind: "curves" };
|
|
147
|
+
}
|
|
148
|
+
if (object.kind === "parametric") {
|
|
149
|
+
const fx = yield* compileText(object.x);
|
|
150
|
+
const fy = yield* compileText(object.y);
|
|
151
|
+
const branches = sampleCurve((t) => planePoint(evaluateWith(fx, scope, "t", t), evaluateWith(fy, scope, "t", t)), box, object.parameter);
|
|
152
|
+
return { branches, kind: "curves" };
|
|
153
|
+
}
|
|
154
|
+
if (object.kind === "polar") {
|
|
155
|
+
const fr = yield* compileText(object.radius);
|
|
156
|
+
const branches = sampleCurve((theta) => {
|
|
157
|
+
const r = evaluateWith(fr, scope, "theta", theta);
|
|
158
|
+
return planePoint(r * Math.cos(theta), r * Math.sin(theta));
|
|
159
|
+
}, box, object.angle);
|
|
160
|
+
return { branches, kind: "curves" };
|
|
161
|
+
}
|
|
162
|
+
if (object.kind === "implicit") {
|
|
163
|
+
const relation = yield* readRelation(object.equation);
|
|
164
|
+
const left = compileFormula(relation.left);
|
|
165
|
+
const right = compileFormula(relation.right);
|
|
166
|
+
/** How far the two sides of the equation are apart at a point. */
|
|
167
|
+
const field = (x, y) => {
|
|
168
|
+
const point = { ...scope, x, y };
|
|
169
|
+
return left(point) - right(point);
|
|
170
|
+
};
|
|
171
|
+
return { branches: contourLines(field, box), kind: "curves" };
|
|
172
|
+
}
|
|
173
|
+
return yield* regionGeometry(object.inequalities, box, scope);
|
|
174
|
+
});
|
|
175
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { Option, Result, Schema } from "effect";
|
|
2
|
+
import { FormulaError, type FormulaProblem } from "#contracts/math/formula/syntax";
|
|
3
|
+
/**
|
|
4
|
+
* The tokens of one formula and the reader's place in them: the token it
|
|
5
|
+
* stands at and how many absolute bars are open around it.
|
|
6
|
+
*/
|
|
7
|
+
declare const ReaderSchema: Schema.Struct<{
|
|
8
|
+
readonly bars: Schema.mutableKey<Schema.Int>;
|
|
9
|
+
readonly index: Schema.mutableKey<Schema.Int>;
|
|
10
|
+
readonly length: Schema.Int;
|
|
11
|
+
readonly tokens: Schema.$Array<Schema.Union<readonly [Schema.Struct<{
|
|
12
|
+
readonly kind: Schema.Literal<"number">;
|
|
13
|
+
readonly position: Schema.Int;
|
|
14
|
+
readonly value: Schema.Finite;
|
|
15
|
+
}>, Schema.Struct<{
|
|
16
|
+
readonly kind: Schema.Literal<"name">;
|
|
17
|
+
readonly name: Schema.String;
|
|
18
|
+
readonly position: Schema.Int;
|
|
19
|
+
}>, Schema.Struct<{
|
|
20
|
+
readonly kind: Schema.Literal<"symbol">;
|
|
21
|
+
readonly position: Schema.Int;
|
|
22
|
+
readonly symbol: Schema.Literals<readonly ["(", ")", "*", "+", ",", "-", "/", "^", "_", "|", "°", "√"]>;
|
|
23
|
+
}>, Schema.Struct<{
|
|
24
|
+
readonly comparison: Schema.Literals<readonly ["=", "<", "<=", ">", ">="]>;
|
|
25
|
+
readonly kind: Schema.Literal<"comparison">;
|
|
26
|
+
readonly position: Schema.Int;
|
|
27
|
+
}>]>>;
|
|
28
|
+
}>;
|
|
29
|
+
export type Reader = typeof ReaderSchema.Type;
|
|
30
|
+
/** Builds the failure for one problem at one place in the text. */
|
|
31
|
+
export declare function problemAt(problem: FormulaProblem, position: number, message: string): Result.Result<never, FormulaError>;
|
|
32
|
+
/** Returns the current token without moving past it. */
|
|
33
|
+
export declare function peek(reader: Reader): Option.Option<{
|
|
34
|
+
readonly kind: "number";
|
|
35
|
+
readonly position: number;
|
|
36
|
+
readonly value: number;
|
|
37
|
+
} | {
|
|
38
|
+
readonly kind: "name";
|
|
39
|
+
readonly name: string;
|
|
40
|
+
readonly position: number;
|
|
41
|
+
} | {
|
|
42
|
+
readonly kind: "symbol";
|
|
43
|
+
readonly position: number;
|
|
44
|
+
readonly symbol: "(" | ")" | "*" | "+" | "," | "-" | "/" | "^" | "_" | "|" | "°" | "√";
|
|
45
|
+
} | {
|
|
46
|
+
readonly comparison: "<" | "<=" | "=" | ">" | ">=";
|
|
47
|
+
readonly kind: "comparison";
|
|
48
|
+
readonly position: number;
|
|
49
|
+
}>;
|
|
50
|
+
/** Whether the current token is one symbol. */
|
|
51
|
+
export declare function at(reader: Reader, symbol: string): boolean;
|
|
52
|
+
/** Where the current token starts, or the end of the text. */
|
|
53
|
+
export declare function here(reader: Reader): number;
|
|
54
|
+
/** Moves past one expected symbol, or fails where it should stand. */
|
|
55
|
+
export declare function expect(reader: Reader, symbol: string): Result.Result<string, never> | Result.Result<never, FormulaError>;
|
|
56
|
+
/** Starts reading the tokens of one formula. */
|
|
57
|
+
export declare function startReader(text: string): Result.Result<{
|
|
58
|
+
bars: number;
|
|
59
|
+
index: number;
|
|
60
|
+
readonly length: number;
|
|
61
|
+
readonly tokens: readonly ({
|
|
62
|
+
readonly kind: "number";
|
|
63
|
+
readonly position: number;
|
|
64
|
+
readonly value: number;
|
|
65
|
+
} | {
|
|
66
|
+
readonly kind: "name";
|
|
67
|
+
readonly name: string;
|
|
68
|
+
readonly position: number;
|
|
69
|
+
} | {
|
|
70
|
+
readonly kind: "symbol";
|
|
71
|
+
readonly position: number;
|
|
72
|
+
readonly symbol: "(" | ")" | "*" | "+" | "," | "-" | "/" | "^" | "_" | "|" | "°" | "√";
|
|
73
|
+
} | {
|
|
74
|
+
readonly comparison: "<" | "<=" | "=" | ">" | ">=";
|
|
75
|
+
readonly kind: "comparison";
|
|
76
|
+
readonly position: number;
|
|
77
|
+
})[];
|
|
78
|
+
}, FormulaError>;
|
|
79
|
+
/** Fails on the first token a complete reading left behind. */
|
|
80
|
+
export declare function finished(reader: Reader): Result.Result<never, FormulaError> | Result.Result<{
|
|
81
|
+
bars: number;
|
|
82
|
+
index: number;
|
|
83
|
+
readonly length: number;
|
|
84
|
+
readonly tokens: readonly ({
|
|
85
|
+
readonly kind: "number";
|
|
86
|
+
readonly position: number;
|
|
87
|
+
readonly value: number;
|
|
88
|
+
} | {
|
|
89
|
+
readonly kind: "name";
|
|
90
|
+
readonly name: string;
|
|
91
|
+
readonly position: number;
|
|
92
|
+
} | {
|
|
93
|
+
readonly kind: "symbol";
|
|
94
|
+
readonly position: number;
|
|
95
|
+
readonly symbol: "(" | ")" | "*" | "+" | "," | "-" | "/" | "^" | "_" | "|" | "°" | "√";
|
|
96
|
+
} | {
|
|
97
|
+
readonly comparison: "<" | "<=" | "=" | ">" | ">=";
|
|
98
|
+
readonly kind: "comparison";
|
|
99
|
+
readonly position: number;
|
|
100
|
+
})[];
|
|
101
|
+
}, never>;
|
|
102
|
+
export {};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { Array as Arr, Option, Result, Schema } from "effect";
|
|
2
|
+
import { FormulaError, } from "#contracts/math/formula/syntax";
|
|
3
|
+
import { FormulaTokenSchema, tokenizeFormula, } from "#contracts/math/formula/token";
|
|
4
|
+
/**
|
|
5
|
+
* The tokens of one formula and the reader's place in them: the token it
|
|
6
|
+
* stands at and how many absolute bars are open around it.
|
|
7
|
+
*/
|
|
8
|
+
const ReaderSchema = Schema.Struct({
|
|
9
|
+
bars: Schema.mutableKey(Schema.Int),
|
|
10
|
+
index: Schema.mutableKey(Schema.Int),
|
|
11
|
+
length: Schema.Int,
|
|
12
|
+
tokens: Schema.Array(FormulaTokenSchema),
|
|
13
|
+
});
|
|
14
|
+
/** Builds the failure for one problem at one place in the text. */
|
|
15
|
+
export function problemAt(problem, position, message) {
|
|
16
|
+
return Result.fail(new FormulaError({ message, position, problem }));
|
|
17
|
+
}
|
|
18
|
+
/** Returns the current token without moving past it. */
|
|
19
|
+
export function peek(reader) {
|
|
20
|
+
return Arr.get(reader.tokens, reader.index);
|
|
21
|
+
}
|
|
22
|
+
/** Whether the current token is one symbol. */
|
|
23
|
+
export function at(reader, symbol) {
|
|
24
|
+
return Option.exists(peek(reader), (token) => token.kind === "symbol" && token.symbol === symbol);
|
|
25
|
+
}
|
|
26
|
+
/** Where the current token starts, or the end of the text. */
|
|
27
|
+
export function here(reader) {
|
|
28
|
+
return Option.match(peek(reader), {
|
|
29
|
+
onNone: () => reader.length,
|
|
30
|
+
onSome: (token) => token.position,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
/** Moves past one expected symbol, or fails where it should stand. */
|
|
34
|
+
export function expect(reader, symbol) {
|
|
35
|
+
if (!at(reader, symbol)) {
|
|
36
|
+
return problemAt("group-unclosed", here(reader), `Expected "${symbol}".`);
|
|
37
|
+
}
|
|
38
|
+
reader.index += 1;
|
|
39
|
+
return Result.succeed(symbol);
|
|
40
|
+
}
|
|
41
|
+
/** Starts reading the tokens of one formula. */
|
|
42
|
+
export function startReader(text) {
|
|
43
|
+
return Result.map(tokenizeFormula(text), (tokens) => ({
|
|
44
|
+
bars: 0,
|
|
45
|
+
index: 0,
|
|
46
|
+
length: text.length,
|
|
47
|
+
tokens,
|
|
48
|
+
}));
|
|
49
|
+
}
|
|
50
|
+
/** Fails on the first token a complete reading left behind. */
|
|
51
|
+
export function finished(reader) {
|
|
52
|
+
return Option.match(peek(reader), {
|
|
53
|
+
onNone: () => Result.succeed(reader),
|
|
54
|
+
onSome: (token) => token.kind === "comparison"
|
|
55
|
+
? problemAt("comparison-unexpected", token.position, "Expected an expression, not an equation or an inequality.")
|
|
56
|
+
: problemAt("operator-expected", token.position, "Expected an operator or the end of the formula."),
|
|
57
|
+
});
|
|
58
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type SampleBox, type SamplePoint, type SampleRange } from "#contracts/math/formula/box";
|
|
2
|
+
/** One visible, unbroken run of a sampled curve, in authored coordinates. */
|
|
3
|
+
export type SampleBranch = readonly SamplePoint[];
|
|
4
|
+
/** Where a curve stands for one parameter value; NaN where it is undefined. */
|
|
5
|
+
export type SampleTrace = (parameter: number) => SamplePoint;
|
|
6
|
+
/** Fewest samples on each visible branch, the Aksara visuals rule. */
|
|
7
|
+
export declare const MINIMUM_BRANCH_SAMPLES = 200;
|
|
8
|
+
/**
|
|
9
|
+
* Samples one curve inside its frame. Every visible branch holds at least 200
|
|
10
|
+
* samples, more where it bends or climbs, with an exact sample at each named
|
|
11
|
+
* parameter value and at each frame edge it crosses. Breaks, undefined
|
|
12
|
+
* stretches, and asymptotes split the curve into separate branches.
|
|
13
|
+
*/
|
|
14
|
+
export declare function sampleCurve(trace: SampleTrace, box: SampleBox, range: SampleRange, named?: readonly number[]): readonly SampleBranch[];
|