trilean 1.9.2 → 1.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -181,7 +181,9 @@ The inputs are validated in full before anything is evaluated, every element of
181
181
 
182
182
  The result is printed as JSON in the `Evaluation` shape, a `definite` value or an `indeterminate` reason (see [The evaluation model](#the-evaluation-model)). The exit status separates the three outcomes a script cares about: `0` for a definite result, `1` for an indeterminate one, `2` when no result was printed (bad usage, an unreadable file, JSON that is malformed, a tree or an input that fails validation, a tree that folds or quantifies over a list the inputs do not hold, or a result JSON cannot carry, which the built-in registry never produces). Everything that is not a result goes to standard error, the usage text included unless `--help` asked for it, and problems with the tree or an input are one `path: message` line each. A reader of standard output that closes early (`| true`) does not change the exit status or print an error.
183
183
 
184
- `call` nodes resolve against `builtinFunctions`, which the library also exports (frozen, so changing the export cannot change what the command resolves): `minimum` and `maximum` (one or more numbers sharing a unit), `absoluteValue`, `round` (halves away from zero, working on the decimal number as written, so `round(1.005, 2)` is `1.01`, with an optional count of decimal places, and a value too large to have a digit at that precision returned unchanged), `squareRoot`, `exponential`, and `logarithm` (natural, or to an optional base, exact for powers of 10 and 2), and the two projections of a complex value onto the reals: `magnitude` (the absolute value of a real number or the modulus of a complex one) and `phase` (the angle of a complex number in radians, in the range [-pi, pi]: a negative real number is +pi whatever the sign of its zero imaginary part, so a signed zero never gives -pi, though a tiny negative imaginary part can round to exactly -pi; zero is 0; a component that is not finite is a `domain-error`). Every function but those two takes real numbers only and refuses a complex value, `absoluteValue` included; `magnitude` takes a real or a complex number and `phase` a complex one. They follow the unit rules above: `minimum`, `maximum`, `absoluteValue`, `round` and `magnitude` keep the argument's unit, `phase` is dimensionless, `squareRoot` halves each dimension's exponent and refuses an odd one, and `exponential` and `logarithm` accept dimensionless numbers only. An argument a function cannot accept (the wrong kind or count, or mismatched units, as in `minimum(1 m, 2)`) is `wrong-type`; an argument of the right kind outside the function's domain, or a result that is not a finite number, is `domain-error`; and any other name is `wrong-type`.
184
+ `call` nodes resolve against `builtinFunctions`, which the library also exports (frozen, so changing the export cannot change what the command resolves): `minimum` and `maximum` (one or more numbers sharing a unit), `absoluteValue`, `round` (halves away from zero, working on the decimal number as written, so `round(1.005, 2)` is `1.01`, with an optional count of decimal places, and a value too large to have a digit at that precision returned unchanged), `squareRoot`, `exponential`, and `logarithm` (natural, or to an optional real base, exact for powers of 10 and 2), the two projections of a complex value onto the reals: `magnitude` (the absolute value of a real number or the modulus of a complex one) and `phase` (the angle of a complex number in radians, in the range [-pi, pi]: a negative real number is +pi whatever the sign of its zero imaginary part, so a signed zero never gives -pi, though a tiny negative imaginary part can round to exactly -pi; zero is 0; a component that is not finite is a `domain-error`), and the trigonometric functions `sine`, `cosine`, `tangent`, `arcSine`, `arcCosine`, `arcTangent` and `arcTangent2` (the angle of the point `(x, y)`, called as `arcTangent2(y, x)`: the `phase` of `x + yi`, so it shares that range and signed-zero handling).
185
+
186
+ `squareRoot`, `exponential`, `logarithm` and `magnitude` take a real or a complex number, `phase` a complex one only, and every other function takes real numbers only and refuses a complex value, `absoluteValue` and the trigonometric functions included. A real argument gives a real result exactly as it always has, so a negative `squareRoot` and a `logarithm` of zero or a negative number stay `domain-error` rather than turning complex. A complex argument gives a complex result, the principal value, even when its imaginary part comes out zero, as complex arithmetic does (see [Complex values](#complex-values)): the branch cut is the negative real axis with `phase`'s convention on it, so `logarithm` is `log|z| + i phase(z)` (divided through by the natural logarithm of a base when one is given), `logarithm(-1)` is `pi i`, `squareRoot` is the root whose real part is not negative (`squareRoot(-4 + 0i)` is `2i` exactly, whatever the sign of the zero), and `exponential(x + yi)` is `e^x (cos y + i sin y)`; a complex zero is a `domain-error` for `logarithm`. They follow the unit rules above: `minimum`, `maximum`, `absoluteValue`, `round` and `magnitude` keep the argument's unit, `phase` is dimensionless, `squareRoot` halves each dimension's exponent and refuses an odd one, and `exponential`, `logarithm` and the trigonometric functions accept dimensionless numbers only and return one, angles in radians. An argument a function cannot accept (the wrong kind or count, or mismatched units, as in `minimum(1 m, 2)`) is `wrong-type`; an argument of the right kind outside the function's domain (an `arcSine` or `arcCosine` outside -1 to 1 among them), or a result that is not a finite number in either component, is `domain-error`; and any other name is `wrong-type`. A `tangent` near a pole is large but finite, since no double is an odd multiple of pi/2 exactly.
185
187
 
186
188
  The command has no table, delegate or tree store to hand a `lookup`, `delegate` or `treeReference` node. A `lookup` is `not-found` and a `delegate` or `treeReference` is `wrong-type`. A tree that folds or quantifies over a list the inputs do not hold exits `2` rather than treating the missing list as empty. That holds even when another operand would settle the answer: `and(false, some(...))` still exits `2`, because the synchronous evaluator asks for the collection before it combines the two. For trees that need a table, a delegate or a tree store, embed the library and supply the [resolvers](#resolvers) yourself.
187
189
 
@@ -546,7 +548,7 @@ The magnitude-and-phase view stays reachable through four exported conversion he
546
548
 
547
549
  - `add`/`subtract` are component-wise, requiring **identical** dimensional-exponent maps exactly as real numbers do (see [Units](#units)).
548
550
  - `multiply`/`divide` are real complex multiplication and division — `(a + bi)(c + di) = (ac − bd) + (ad + bc)i`, and the corresponding quotient — never component-wise. The divisor and the dividend are each scaled by their own power of two, which brings the larger component of each to about a quarter, the quotient is computed on those scaled values, and the combined power of two is applied to the result once. Where the quotient is a normal number the error over the pair is a few units of 2^-53 (the measured worst case over a large exact-rational check is under 4); where it is subnormal the result is within a few spacings of the true value (under 5 measured), not correctly rounded. A quotient too large for a double is `domain-error`, and one below about half the smallest subnormal is zero; both thresholds are fuzzy by that error near the edge (measured, a true quotient up to about 2 ulps above the largest double came back finite, and a representable one a little over 1 ulp below it was refused). A zero dividend gives zero without preserving the signs of its zeros, which is visible only through `Object.is` on the library function, not through JSON, `compare` or `memberOf`. Units combine by the same dimensional analysis real numbers use. A zero divisor means both components zero; a divisor with only a zero real part divides perfectly well.
549
- - `power` is defined for a **real integer exponent** and evaluated as the repeated multiplication that integer exponentiation is, grouped by squaring so the work grows with the logarithm of the exponent. The price is accuracy: the error of squaring grows in proportion to |n|, so for large exponents the result is many times less accurate than a plain multiplication loop's. These are observations from samples, not bounds, and the figures move from sample to sample. Against a 140-digit reference on random unit-magnitude bases, the mean error of squaring was between 15 and 56 times a plain loop's for |n| from 1e3 to 1e7, growing by about a factor of ten per tenfold increase in |n| (roughly 0.23 |n| units of 2^-53). For |n| up to 24 the worst error was about 20 units of 2^-53: 18.5 for positive and 18.2 for negative exponents over 50,000 bases per exponent, and up to 19.7 in another sample of 40,000. A negative exponent is the reciprocal of the positive power, except where that power overflows or underflows, when the reciprocal is raised instead so a representable result is not lost. Like a real `power`, it requires dimensionless operands. An arbitrary complex exponent is a genuinely bigger question — it needs the complex logarithm, which is multivalued, so it needs a branch-cut convention this design has not chosen — and is deliberately out of scope for now: it is `wrong-type`, as is a non-integer real exponent, on the same reading of that code used throughout ("an answer exists, but this operator does not accept this operand" — compare `power`'s existing dimensionless-operands requirement, also `wrong-type`).
551
+ - `power` is defined for a **real integer exponent** and evaluated as the repeated multiplication that integer exponentiation is, grouped by squaring so the work grows with the logarithm of the exponent. The price is accuracy: the error of squaring grows in proportion to |n|, so for large exponents the result is many times less accurate than a plain multiplication loop's. These are observations from samples, not bounds, and the figures move from sample to sample. Against a 140-digit reference on random unit-magnitude bases, the mean error of squaring was between 15 and 56 times a plain loop's for |n| from 1e3 to 1e7, growing by about a factor of ten per tenfold increase in |n| (roughly 0.23 |n| units of 2^-53). For |n| up to 24 the worst error was about 20 units of 2^-53: 18.5 for positive and 18.2 for negative exponents over 50,000 bases per exponent, and up to 19.7 in another sample of 40,000. A negative exponent is the reciprocal of the positive power, except where that power overflows or underflows, when the reciprocal is raised instead so a representable result is not lost. Like a real `power`, it requires dimensionless operands. An arbitrary complex exponent is a genuinely bigger question, and deliberately out of scope for now: it needs the complex logarithm, which is multivalued, and although the command line's built-in `logarithm` returns its principal value, the evaluator's own operators carry no function set and have not adopted that branch cut. It is `wrong-type`, as is a non-integer real exponent, on the same reading of that code used throughout ("an answer exists, but this operator does not accept this operand" — compare `power`'s existing dimensionless-operands requirement, also `wrong-type`).
550
552
  - `modulo` is `domain-error`, not `wrong-type`: a remainder needs a canonical notion of how many whole divisors fit, and the complex plane has no ordering to supply one. There is no answer to accept, which is the same category as division by zero.
551
553
 
552
554
  **A real operand is promoted, never rejected.** Mixing a `number` with a `complex` in one `arithmetic` node works: every real number *is* a complex number with a zero imaginary part, so the promotion is exact, total, and canonical — unlike the temporal cross-kind combinations above, which had to be enumerated one by one precisely because no such embedding exists between an `instant` and a `duration`. Scaling a complex value by a real one, or offsetting it by a real constant, is the common case, and forcing every real literal in such a formula to be rewritten as a complex one would defeat the point. The result is `complex` whenever either operand is, even when the imaginary part comes out zero: a node's result kind follows its operand kinds, never the values that happen to flow through it.
@@ -35,6 +35,80 @@ function singleNumber(name, args) {
35
35
  if (args.length !== 1 || !isNumberValue(arg)) return { wrongType: `${name} requires exactly one number argument` };
36
36
  return arg;
37
37
  }
38
+ /** The one exit for every complex result, as `finiteNumber` is for real ones. A complex argument always gives a complex result, even when the imaginary part comes out zero, as the evaluator's own complex arithmetic does: the result kind follows the argument kind, never the value. */
39
+ function finiteComplex(name, { re, im }, unit) {
40
+ if (!Number.isFinite(re) || !Number.isFinite(im)) return { domainError: `${name} produced a result that is not a finite number` };
41
+ return unit === void 0 ? {
42
+ kind: "complex",
43
+ re,
44
+ im
45
+ } : {
46
+ kind: "complex",
47
+ re,
48
+ im,
49
+ unit
50
+ };
51
+ }
52
+ function singleNumberOrComplex(name, args) {
53
+ const [arg] = args;
54
+ if (args.length !== 1 || arg?.kind !== "number" && arg?.kind !== "complex") return { wrongType: `${name} requires exactly one number or complex number argument` };
55
+ return arg;
56
+ }
57
+ /** The value of a single dimensionless real argument, or the `wrongType` refusing anything else (a complex value included). */
58
+ function singleDimensionless(name, args) {
59
+ const arg = singleNumber(name, args);
60
+ if ("wrongType" in arg) return arg;
61
+ if (!isDimensionless(arg)) return { wrongType: `${name} requires a dimensionless number` };
62
+ return arg.value;
63
+ }
64
+ /** A real function of one dimensionless number, returning a dimensionless number. */
65
+ function dimensionlessFunction(name, apply) {
66
+ return (args) => {
67
+ const value = singleDimensionless(name, args);
68
+ if (typeof value !== "number") return value;
69
+ return finiteNumber(name, apply(value));
70
+ };
71
+ }
72
+ /** An inverse sine or cosine, whose real domain is the closed interval from -1 to 1; outside it the inverse is not a real number, which is a `domainError` rather than the `NaN` `Math.asin` and `Math.acos` return. */
73
+ function unitIntervalInverse(name, apply) {
74
+ return (args) => {
75
+ const value = singleDimensionless(name, args);
76
+ if (typeof value !== "number") return value;
77
+ if (!(Math.abs(value) <= 1)) return { domainError: `${name} requires a number from -1 to 1` };
78
+ return finiteNumber(name, apply(value));
79
+ };
80
+ }
81
+ /**
82
+ * The principal square root of a complex value: the root whose real part is not negative, with the branch cut along the negative real axis. On the cut the root lies on the positive imaginary axis whatever the sign of the zero imaginary part, matching `complexPhase`'s +pi for a negative real number, so `sqrt(z)` is `sqrt(|z|) * e^(i * phase(z) / 2)` everywhere.
83
+ *
84
+ * It is computed by the closed form rather than through that polar expression: with `t = sqrt((|re| + |z|) / 2)`, the root is `t + (im / 2t)i` when `re` is not negative and `|im| / 2t + t i` (with the sign of `im` on `t`) otherwise. Neither form subtracts two nearly equal quantities, and an exactly representable root comes out exact (`sqrt(-4)` is `2i`, where `cos(pi / 2)` would leave a stray real part of about 1e-16).
85
+ */
86
+ function principalSquareRoot(value) {
87
+ const modulus = require_complex.complexMagnitude(value).value;
88
+ if (modulus === 0) return {
89
+ re: 0,
90
+ im: 0
91
+ };
92
+ const sum = Math.abs(value.re) + modulus;
93
+ const half = Number.isFinite(sum) ? sum / 2 : Math.abs(value.re) / 2 + modulus / 2;
94
+ const t = Math.sqrt(half);
95
+ const { im } = value;
96
+ if (value.re >= 0) return {
97
+ re: t,
98
+ im: im / (2 * t)
99
+ };
100
+ return {
101
+ re: Math.abs(im) / (2 * t),
102
+ im: im < 0 ? -t : t
103
+ };
104
+ }
105
+ /** The logarithm of a positive real number, natural or to a base already checked to be above zero and not one. */
106
+ function realLogarithm(value, base) {
107
+ if (base === void 0) return Math.log(value);
108
+ if (base === DECIMAL_BASE) return Math.log10(value);
109
+ if (base === BINARY_BASE) return Math.log2(value);
110
+ return Math.log(value) / Math.log(base);
111
+ }
38
112
  /** Halves every dimension's exponent, or `false` when one is odd and the root would need a fractional exponent. */
39
113
  function halveUnit(unit) {
40
114
  if (unit === void 0) return void 0;
@@ -74,11 +148,13 @@ function roundHalfAwayFromZero(value, places) {
74
148
  return rounded === 0 ? 0 : Math.sign(value) * rounded;
75
149
  }
76
150
  /**
77
- * The numeric functions a `call` node can name when an embedder has no registry of its own: `minimum` and `maximum` (one or more numbers in a shared unit), `absoluteValue`, `round` (half away from zero, with an optional count of decimal places), `squareRoot`, `exponential`, `logarithm` (natural, or to an optional base), and the two projections of a complex value onto the reals, `magnitude` and `phase`. Every function but those two takes real numbers only, and refuses a complex value; `magnitude` takes a real or a complex number, and `phase` a complex one.
151
+ * The numeric functions a `call` node can name when an embedder has no registry of its own: `minimum` and `maximum` (one or more numbers in a shared unit), `absoluteValue`, `round` (half away from zero, with an optional count of decimal places), `squareRoot`, `exponential`, `logarithm` (natural, or to an optional real base), the two projections of a complex value onto the reals, `magnitude` and `phase`, and the trigonometric functions `sine`, `cosine`, `tangent`, `arcSine`, `arcCosine`, `arcTangent` and `arcTangent2` (the angle of the point `(x, y)`, called with `y` first).
78
152
  *
79
- * Failures split as the evaluator's own operators split. An argument a function cannot accept at all (the wrong kind, the wrong count, or units that do not fit) is a `wrongType`; an argument of the right kind outside the function's domain (a negative `squareRoot`, a `logarithm` of zero, a base of one, a fractional count of decimal places) or a result that is not a finite number is a `domainError`.
153
+ * Which take a complex value: `squareRoot`, `exponential`, `logarithm` and `magnitude` take a real or a complex number, and `phase` a complex one only. Every other function takes real numbers only and refuses a complex value. A real argument to `squareRoot`, `exponential` or `logarithm` gives a real result, so a negative `squareRoot` or a `logarithm` of zero or a negative number stays a `domainError` rather than becoming complex; a complex argument gives a complex result, the principal value, even when its imaginary part comes out zero, as the evaluator's own complex arithmetic does. The principal values take the branch cut along the negative real axis with `phase`'s convention on it: `logarithm` is `log|z| + i phase(z)` (divided through by the natural logarithm of a base when one is given), so its imaginary part lies in [-pi, pi], and `squareRoot` is the root with a real part that is not negative, on the positive imaginary axis for a negative real number whatever the sign of its zero imaginary part.
80
154
  *
81
- * Units follow the evaluator's rule that dimensions are never silently converted. `minimum`, `maximum`, `absoluteValue`, `round` and `magnitude` keep the argument's unit (and the first two require all arguments to share one); `phase` returns a dimensionless angle in radians in the range [-pi, pi], whatever unit its argument carries, with a negative real number +pi whatever the sign of its zero imaginary part (a signed zero never gives -pi, though a tiny negative imaginary part can round to it) and zero 0 (a component that is not finite is a `domainError`); `squareRoot` halves each dimension's exponent and refuses a unit whose exponents are not all even; `exponential` and `logarithm` accept only dimensionless numbers and return one.
155
+ * Failures split as the evaluator's own operators split. An argument a function cannot accept at all (the wrong kind, the wrong count, or units that do not fit) is a `wrongType`; an argument of the right kind outside the function's domain (a negative real `squareRoot`, a `logarithm` of zero, a base of one, a fractional count of decimal places, an `arcSine` or `arcCosine` outside -1 to 1) or a result that is not a finite number (in either component of a complex one) is a `domainError`. A `tangent` near a pole is large but finite, since no double is an odd multiple of pi/2 exactly.
156
+ *
157
+ * Units follow the evaluator's rule that dimensions are never silently converted. `minimum`, `maximum`, `absoluteValue`, `round` and `magnitude` keep the argument's unit (and the first two require all arguments to share one); `phase` returns a dimensionless angle in radians in the range [-pi, pi], whatever unit its argument carries, with a negative real number +pi whatever the sign of its zero imaginary part (a signed zero never gives -pi, though a tiny negative imaginary part can round to it) and zero 0 (a component that is not finite is a `domainError`); `squareRoot` halves each dimension's exponent and refuses a unit whose exponents are not all even; `exponential`, `logarithm` and the trigonometric functions accept only dimensionless numbers and return one, angles in radians. `arcTangent2` is the `phase` of `x + yi`, so it shares that range and signed-zero handling, and its two arguments must be finite.
82
158
  *
83
159
  * Frozen, so mutating the export cannot change what `evaluateDocument` resolves.
84
160
  */
@@ -99,31 +175,35 @@ const builtinFunctions = Object.freeze({
99
175
  return finiteNumber("round", roundHalfAwayFromZero(value.value, places.value), value.unit);
100
176
  },
101
177
  squareRoot: (args) => {
102
- const arg = singleNumber("squareRoot", args);
178
+ const arg = singleNumberOrComplex("squareRoot", args);
103
179
  if ("wrongType" in arg) return arg;
104
180
  const unit = halveUnit(arg.unit);
105
181
  if (unit === false) return { wrongType: "squareRoot requires every unit exponent to be even" };
182
+ if (arg.kind === "complex") return finiteComplex("squareRoot", principalSquareRoot(arg), unit);
106
183
  if (arg.value < 0) return { domainError: "squareRoot of a negative number is not a real number" };
107
184
  return finiteNumber("squareRoot", Math.sqrt(arg.value), unit);
108
185
  },
109
186
  exponential: (args) => {
110
- const arg = singleNumber("exponential", args);
187
+ const arg = singleNumberOrComplex("exponential", args);
111
188
  if ("wrongType" in arg) return arg;
112
- if (!isDimensionless(arg)) return { wrongType: "exponential requires a dimensionless number" };
189
+ if (!require_computed_value.unitsEqual(arg.unit, void 0)) return { wrongType: "exponential requires a dimensionless number" };
190
+ if (arg.kind === "complex") return finiteComplex("exponential", require_complex.complexFromPolar(Math.exp(arg.re), arg.im));
113
191
  return finiteNumber("exponential", Math.exp(arg.value));
114
192
  },
115
193
  logarithm: (args) => {
116
194
  const [value, base, ...extra] = args;
117
- if (!isNumberValue(value) || extra.length > 0) return { wrongType: "logarithm requires a number and an optional base" };
118
- if (!isDimensionless(value)) return { wrongType: "logarithm requires a dimensionless number" };
195
+ if (value?.kind !== "number" && value?.kind !== "complex" || extra.length > 0) return { wrongType: "logarithm requires a number or a complex number and an optional base" };
196
+ if (!require_computed_value.unitsEqual(value.unit, void 0)) return { wrongType: "logarithm requires a dimensionless number" };
119
197
  const baseNumber = base === void 0 ? void 0 : dimensionlessNumber(base);
120
198
  if (base !== void 0 && baseNumber === void 0) return { wrongType: "logarithm requires its base to be a dimensionless number" };
121
- if (value.value <= 0) return { domainError: "logarithm requires a number above zero" };
122
- if (baseNumber === void 0) return finiteNumber("logarithm", Math.log(value.value));
123
- if (baseNumber <= 0 || baseNumber === 1) return { domainError: "logarithm requires its base to be above zero and not one" };
124
- if (baseNumber === DECIMAL_BASE) return finiteNumber("logarithm", Math.log10(value.value));
125
- if (baseNumber === BINARY_BASE) return finiteNumber("logarithm", Math.log2(value.value));
126
- return finiteNumber("logarithm", Math.log(value.value) / Math.log(baseNumber));
199
+ if (value.kind === "number" && value.value <= 0) return { domainError: "logarithm requires a number above zero" };
200
+ if (value.kind === "complex" && value.re === 0 && value.im === 0) return { domainError: "logarithm requires a complex number other than zero" };
201
+ if (baseNumber !== void 0 && (baseNumber <= 0 || baseNumber === 1)) return { domainError: "logarithm requires its base to be above zero and not one" };
202
+ if (value.kind === "number") return finiteNumber("logarithm", realLogarithm(value.value, baseNumber));
203
+ return finiteComplex("logarithm", {
204
+ re: realLogarithm(require_complex.complexMagnitude(value).value, baseNumber),
205
+ im: require_complex.complexPhase(value).value / (baseNumber === void 0 ? 1 : Math.log(baseNumber))
206
+ });
127
207
  },
128
208
  magnitude: (args) => {
129
209
  const [arg] = args;
@@ -140,6 +220,23 @@ const builtinFunctions = Object.freeze({
140
220
  if (args.length !== 1 || arg?.kind !== "complex") return { wrongType: "phase requires exactly one complex argument" };
141
221
  if (!Number.isFinite(arg.re) || !Number.isFinite(arg.im)) return { domainError: "phase requires finite components" };
142
222
  return require_complex.complexPhase(arg);
223
+ },
224
+ sine: dimensionlessFunction("sine", Math.sin),
225
+ cosine: dimensionlessFunction("cosine", Math.cos),
226
+ tangent: dimensionlessFunction("tangent", Math.tan),
227
+ arcSine: unitIntervalInverse("arcSine", Math.asin),
228
+ arcCosine: unitIntervalInverse("arcCosine", Math.acos),
229
+ arcTangent: dimensionlessFunction("arcTangent", Math.atan),
230
+ arcTangent2: (args) => {
231
+ const [y, x] = args;
232
+ if (args.length !== 2 || !isNumberValue(y) || !isNumberValue(x)) return { wrongType: "arcTangent2 requires exactly two number arguments" };
233
+ if (!isDimensionless(y) || !isDimensionless(x)) return { wrongType: "arcTangent2 requires dimensionless numbers" };
234
+ if (!Number.isFinite(y.value) || !Number.isFinite(x.value)) return { domainError: "arcTangent2 requires finite arguments" };
235
+ return require_complex.complexPhase({
236
+ kind: "complex",
237
+ re: x.value,
238
+ im: y.value
239
+ });
143
240
  }
144
241
  });
145
242
  //#endregion
@@ -1,11 +1,13 @@
1
1
  import { FunctionRegistry } from "./functions.cjs";
2
2
  //#region src/builtin-functions.d.ts
3
3
  /**
4
- * The numeric functions a `call` node can name when an embedder has no registry of its own: `minimum` and `maximum` (one or more numbers in a shared unit), `absoluteValue`, `round` (half away from zero, with an optional count of decimal places), `squareRoot`, `exponential`, `logarithm` (natural, or to an optional base), and the two projections of a complex value onto the reals, `magnitude` and `phase`. Every function but those two takes real numbers only, and refuses a complex value; `magnitude` takes a real or a complex number, and `phase` a complex one.
4
+ * The numeric functions a `call` node can name when an embedder has no registry of its own: `minimum` and `maximum` (one or more numbers in a shared unit), `absoluteValue`, `round` (half away from zero, with an optional count of decimal places), `squareRoot`, `exponential`, `logarithm` (natural, or to an optional real base), the two projections of a complex value onto the reals, `magnitude` and `phase`, and the trigonometric functions `sine`, `cosine`, `tangent`, `arcSine`, `arcCosine`, `arcTangent` and `arcTangent2` (the angle of the point `(x, y)`, called with `y` first).
5
5
  *
6
- * Failures split as the evaluator's own operators split. An argument a function cannot accept at all (the wrong kind, the wrong count, or units that do not fit) is a `wrongType`; an argument of the right kind outside the function's domain (a negative `squareRoot`, a `logarithm` of zero, a base of one, a fractional count of decimal places) or a result that is not a finite number is a `domainError`.
6
+ * Which take a complex value: `squareRoot`, `exponential`, `logarithm` and `magnitude` take a real or a complex number, and `phase` a complex one only. Every other function takes real numbers only and refuses a complex value. A real argument to `squareRoot`, `exponential` or `logarithm` gives a real result, so a negative `squareRoot` or a `logarithm` of zero or a negative number stays a `domainError` rather than becoming complex; a complex argument gives a complex result, the principal value, even when its imaginary part comes out zero, as the evaluator's own complex arithmetic does. The principal values take the branch cut along the negative real axis with `phase`'s convention on it: `logarithm` is `log|z| + i phase(z)` (divided through by the natural logarithm of a base when one is given), so its imaginary part lies in [-pi, pi], and `squareRoot` is the root with a real part that is not negative, on the positive imaginary axis for a negative real number whatever the sign of its zero imaginary part.
7
7
  *
8
- * Units follow the evaluator's rule that dimensions are never silently converted. `minimum`, `maximum`, `absoluteValue`, `round` and `magnitude` keep the argument's unit (and the first two require all arguments to share one); `phase` returns a dimensionless angle in radians in the range [-pi, pi], whatever unit its argument carries, with a negative real number +pi whatever the sign of its zero imaginary part (a signed zero never gives -pi, though a tiny negative imaginary part can round to it) and zero 0 (a component that is not finite is a `domainError`); `squareRoot` halves each dimension's exponent and refuses a unit whose exponents are not all even; `exponential` and `logarithm` accept only dimensionless numbers and return one.
8
+ * Failures split as the evaluator's own operators split. An argument a function cannot accept at all (the wrong kind, the wrong count, or units that do not fit) is a `wrongType`; an argument of the right kind outside the function's domain (a negative real `squareRoot`, a `logarithm` of zero, a base of one, a fractional count of decimal places, an `arcSine` or `arcCosine` outside -1 to 1) or a result that is not a finite number (in either component of a complex one) is a `domainError`. A `tangent` near a pole is large but finite, since no double is an odd multiple of pi/2 exactly.
9
+ *
10
+ * Units follow the evaluator's rule that dimensions are never silently converted. `minimum`, `maximum`, `absoluteValue`, `round` and `magnitude` keep the argument's unit (and the first two require all arguments to share one); `phase` returns a dimensionless angle in radians in the range [-pi, pi], whatever unit its argument carries, with a negative real number +pi whatever the sign of its zero imaginary part (a signed zero never gives -pi, though a tiny negative imaginary part can round to it) and zero 0 (a component that is not finite is a `domainError`); `squareRoot` halves each dimension's exponent and refuses a unit whose exponents are not all even; `exponential`, `logarithm` and the trigonometric functions accept only dimensionless numbers and return one, angles in radians. `arcTangent2` is the `phase` of `x + yi`, so it shares that range and signed-zero handling, and its two arguments must be finite.
9
11
  *
10
12
  * Frozen, so mutating the export cannot change what `evaluateDocument` resolves.
11
13
  */
@@ -1,11 +1,13 @@
1
1
  import { FunctionRegistry } from "./functions.js";
2
2
  //#region src/builtin-functions.d.ts
3
3
  /**
4
- * The numeric functions a `call` node can name when an embedder has no registry of its own: `minimum` and `maximum` (one or more numbers in a shared unit), `absoluteValue`, `round` (half away from zero, with an optional count of decimal places), `squareRoot`, `exponential`, `logarithm` (natural, or to an optional base), and the two projections of a complex value onto the reals, `magnitude` and `phase`. Every function but those two takes real numbers only, and refuses a complex value; `magnitude` takes a real or a complex number, and `phase` a complex one.
4
+ * The numeric functions a `call` node can name when an embedder has no registry of its own: `minimum` and `maximum` (one or more numbers in a shared unit), `absoluteValue`, `round` (half away from zero, with an optional count of decimal places), `squareRoot`, `exponential`, `logarithm` (natural, or to an optional real base), the two projections of a complex value onto the reals, `magnitude` and `phase`, and the trigonometric functions `sine`, `cosine`, `tangent`, `arcSine`, `arcCosine`, `arcTangent` and `arcTangent2` (the angle of the point `(x, y)`, called with `y` first).
5
5
  *
6
- * Failures split as the evaluator's own operators split. An argument a function cannot accept at all (the wrong kind, the wrong count, or units that do not fit) is a `wrongType`; an argument of the right kind outside the function's domain (a negative `squareRoot`, a `logarithm` of zero, a base of one, a fractional count of decimal places) or a result that is not a finite number is a `domainError`.
6
+ * Which take a complex value: `squareRoot`, `exponential`, `logarithm` and `magnitude` take a real or a complex number, and `phase` a complex one only. Every other function takes real numbers only and refuses a complex value. A real argument to `squareRoot`, `exponential` or `logarithm` gives a real result, so a negative `squareRoot` or a `logarithm` of zero or a negative number stays a `domainError` rather than becoming complex; a complex argument gives a complex result, the principal value, even when its imaginary part comes out zero, as the evaluator's own complex arithmetic does. The principal values take the branch cut along the negative real axis with `phase`'s convention on it: `logarithm` is `log|z| + i phase(z)` (divided through by the natural logarithm of a base when one is given), so its imaginary part lies in [-pi, pi], and `squareRoot` is the root with a real part that is not negative, on the positive imaginary axis for a negative real number whatever the sign of its zero imaginary part.
7
7
  *
8
- * Units follow the evaluator's rule that dimensions are never silently converted. `minimum`, `maximum`, `absoluteValue`, `round` and `magnitude` keep the argument's unit (and the first two require all arguments to share one); `phase` returns a dimensionless angle in radians in the range [-pi, pi], whatever unit its argument carries, with a negative real number +pi whatever the sign of its zero imaginary part (a signed zero never gives -pi, though a tiny negative imaginary part can round to it) and zero 0 (a component that is not finite is a `domainError`); `squareRoot` halves each dimension's exponent and refuses a unit whose exponents are not all even; `exponential` and `logarithm` accept only dimensionless numbers and return one.
8
+ * Failures split as the evaluator's own operators split. An argument a function cannot accept at all (the wrong kind, the wrong count, or units that do not fit) is a `wrongType`; an argument of the right kind outside the function's domain (a negative real `squareRoot`, a `logarithm` of zero, a base of one, a fractional count of decimal places, an `arcSine` or `arcCosine` outside -1 to 1) or a result that is not a finite number (in either component of a complex one) is a `domainError`. A `tangent` near a pole is large but finite, since no double is an odd multiple of pi/2 exactly.
9
+ *
10
+ * Units follow the evaluator's rule that dimensions are never silently converted. `minimum`, `maximum`, `absoluteValue`, `round` and `magnitude` keep the argument's unit (and the first two require all arguments to share one); `phase` returns a dimensionless angle in radians in the range [-pi, pi], whatever unit its argument carries, with a negative real number +pi whatever the sign of its zero imaginary part (a signed zero never gives -pi, though a tiny negative imaginary part can round to it) and zero 0 (a component that is not finite is a `domainError`); `squareRoot` halves each dimension's exponent and refuses a unit whose exponents are not all even; `exponential`, `logarithm` and the trigonometric functions accept only dimensionless numbers and return one, angles in radians. `arcTangent2` is the `phase` of `x + yi`, so it shares that range and signed-zero handling, and its two arguments must be finite.
9
11
  *
10
12
  * Frozen, so mutating the export cannot change what `evaluateDocument` resolves.
11
13
  */
@@ -1,5 +1,5 @@
1
1
  import { unitsEqual } from "./computed-value.js";
2
- import { complexMagnitude, complexPhase } from "./complex.js";
2
+ import { complexFromPolar, complexMagnitude, complexPhase } from "./complex.js";
3
3
  //#region src/builtin-functions.ts
4
4
  const DECIMAL_BASE = 10;
5
5
  const BINARY_BASE = 2;
@@ -34,6 +34,80 @@ function singleNumber(name, args) {
34
34
  if (args.length !== 1 || !isNumberValue(arg)) return { wrongType: `${name} requires exactly one number argument` };
35
35
  return arg;
36
36
  }
37
+ /** The one exit for every complex result, as `finiteNumber` is for real ones. A complex argument always gives a complex result, even when the imaginary part comes out zero, as the evaluator's own complex arithmetic does: the result kind follows the argument kind, never the value. */
38
+ function finiteComplex(name, { re, im }, unit) {
39
+ if (!Number.isFinite(re) || !Number.isFinite(im)) return { domainError: `${name} produced a result that is not a finite number` };
40
+ return unit === void 0 ? {
41
+ kind: "complex",
42
+ re,
43
+ im
44
+ } : {
45
+ kind: "complex",
46
+ re,
47
+ im,
48
+ unit
49
+ };
50
+ }
51
+ function singleNumberOrComplex(name, args) {
52
+ const [arg] = args;
53
+ if (args.length !== 1 || arg?.kind !== "number" && arg?.kind !== "complex") return { wrongType: `${name} requires exactly one number or complex number argument` };
54
+ return arg;
55
+ }
56
+ /** The value of a single dimensionless real argument, or the `wrongType` refusing anything else (a complex value included). */
57
+ function singleDimensionless(name, args) {
58
+ const arg = singleNumber(name, args);
59
+ if ("wrongType" in arg) return arg;
60
+ if (!isDimensionless(arg)) return { wrongType: `${name} requires a dimensionless number` };
61
+ return arg.value;
62
+ }
63
+ /** A real function of one dimensionless number, returning a dimensionless number. */
64
+ function dimensionlessFunction(name, apply) {
65
+ return (args) => {
66
+ const value = singleDimensionless(name, args);
67
+ if (typeof value !== "number") return value;
68
+ return finiteNumber(name, apply(value));
69
+ };
70
+ }
71
+ /** An inverse sine or cosine, whose real domain is the closed interval from -1 to 1; outside it the inverse is not a real number, which is a `domainError` rather than the `NaN` `Math.asin` and `Math.acos` return. */
72
+ function unitIntervalInverse(name, apply) {
73
+ return (args) => {
74
+ const value = singleDimensionless(name, args);
75
+ if (typeof value !== "number") return value;
76
+ if (!(Math.abs(value) <= 1)) return { domainError: `${name} requires a number from -1 to 1` };
77
+ return finiteNumber(name, apply(value));
78
+ };
79
+ }
80
+ /**
81
+ * The principal square root of a complex value: the root whose real part is not negative, with the branch cut along the negative real axis. On the cut the root lies on the positive imaginary axis whatever the sign of the zero imaginary part, matching `complexPhase`'s +pi for a negative real number, so `sqrt(z)` is `sqrt(|z|) * e^(i * phase(z) / 2)` everywhere.
82
+ *
83
+ * It is computed by the closed form rather than through that polar expression: with `t = sqrt((|re| + |z|) / 2)`, the root is `t + (im / 2t)i` when `re` is not negative and `|im| / 2t + t i` (with the sign of `im` on `t`) otherwise. Neither form subtracts two nearly equal quantities, and an exactly representable root comes out exact (`sqrt(-4)` is `2i`, where `cos(pi / 2)` would leave a stray real part of about 1e-16).
84
+ */
85
+ function principalSquareRoot(value) {
86
+ const modulus = complexMagnitude(value).value;
87
+ if (modulus === 0) return {
88
+ re: 0,
89
+ im: 0
90
+ };
91
+ const sum = Math.abs(value.re) + modulus;
92
+ const half = Number.isFinite(sum) ? sum / 2 : Math.abs(value.re) / 2 + modulus / 2;
93
+ const t = Math.sqrt(half);
94
+ const { im } = value;
95
+ if (value.re >= 0) return {
96
+ re: t,
97
+ im: im / (2 * t)
98
+ };
99
+ return {
100
+ re: Math.abs(im) / (2 * t),
101
+ im: im < 0 ? -t : t
102
+ };
103
+ }
104
+ /** The logarithm of a positive real number, natural or to a base already checked to be above zero and not one. */
105
+ function realLogarithm(value, base) {
106
+ if (base === void 0) return Math.log(value);
107
+ if (base === DECIMAL_BASE) return Math.log10(value);
108
+ if (base === BINARY_BASE) return Math.log2(value);
109
+ return Math.log(value) / Math.log(base);
110
+ }
37
111
  /** Halves every dimension's exponent, or `false` when one is odd and the root would need a fractional exponent. */
38
112
  function halveUnit(unit) {
39
113
  if (unit === void 0) return void 0;
@@ -73,11 +147,13 @@ function roundHalfAwayFromZero(value, places) {
73
147
  return rounded === 0 ? 0 : Math.sign(value) * rounded;
74
148
  }
75
149
  /**
76
- * The numeric functions a `call` node can name when an embedder has no registry of its own: `minimum` and `maximum` (one or more numbers in a shared unit), `absoluteValue`, `round` (half away from zero, with an optional count of decimal places), `squareRoot`, `exponential`, `logarithm` (natural, or to an optional base), and the two projections of a complex value onto the reals, `magnitude` and `phase`. Every function but those two takes real numbers only, and refuses a complex value; `magnitude` takes a real or a complex number, and `phase` a complex one.
150
+ * The numeric functions a `call` node can name when an embedder has no registry of its own: `minimum` and `maximum` (one or more numbers in a shared unit), `absoluteValue`, `round` (half away from zero, with an optional count of decimal places), `squareRoot`, `exponential`, `logarithm` (natural, or to an optional real base), the two projections of a complex value onto the reals, `magnitude` and `phase`, and the trigonometric functions `sine`, `cosine`, `tangent`, `arcSine`, `arcCosine`, `arcTangent` and `arcTangent2` (the angle of the point `(x, y)`, called with `y` first).
77
151
  *
78
- * Failures split as the evaluator's own operators split. An argument a function cannot accept at all (the wrong kind, the wrong count, or units that do not fit) is a `wrongType`; an argument of the right kind outside the function's domain (a negative `squareRoot`, a `logarithm` of zero, a base of one, a fractional count of decimal places) or a result that is not a finite number is a `domainError`.
152
+ * Which take a complex value: `squareRoot`, `exponential`, `logarithm` and `magnitude` take a real or a complex number, and `phase` a complex one only. Every other function takes real numbers only and refuses a complex value. A real argument to `squareRoot`, `exponential` or `logarithm` gives a real result, so a negative `squareRoot` or a `logarithm` of zero or a negative number stays a `domainError` rather than becoming complex; a complex argument gives a complex result, the principal value, even when its imaginary part comes out zero, as the evaluator's own complex arithmetic does. The principal values take the branch cut along the negative real axis with `phase`'s convention on it: `logarithm` is `log|z| + i phase(z)` (divided through by the natural logarithm of a base when one is given), so its imaginary part lies in [-pi, pi], and `squareRoot` is the root with a real part that is not negative, on the positive imaginary axis for a negative real number whatever the sign of its zero imaginary part.
79
153
  *
80
- * Units follow the evaluator's rule that dimensions are never silently converted. `minimum`, `maximum`, `absoluteValue`, `round` and `magnitude` keep the argument's unit (and the first two require all arguments to share one); `phase` returns a dimensionless angle in radians in the range [-pi, pi], whatever unit its argument carries, with a negative real number +pi whatever the sign of its zero imaginary part (a signed zero never gives -pi, though a tiny negative imaginary part can round to it) and zero 0 (a component that is not finite is a `domainError`); `squareRoot` halves each dimension's exponent and refuses a unit whose exponents are not all even; `exponential` and `logarithm` accept only dimensionless numbers and return one.
154
+ * Failures split as the evaluator's own operators split. An argument a function cannot accept at all (the wrong kind, the wrong count, or units that do not fit) is a `wrongType`; an argument of the right kind outside the function's domain (a negative real `squareRoot`, a `logarithm` of zero, a base of one, a fractional count of decimal places, an `arcSine` or `arcCosine` outside -1 to 1) or a result that is not a finite number (in either component of a complex one) is a `domainError`. A `tangent` near a pole is large but finite, since no double is an odd multiple of pi/2 exactly.
155
+ *
156
+ * Units follow the evaluator's rule that dimensions are never silently converted. `minimum`, `maximum`, `absoluteValue`, `round` and `magnitude` keep the argument's unit (and the first two require all arguments to share one); `phase` returns a dimensionless angle in radians in the range [-pi, pi], whatever unit its argument carries, with a negative real number +pi whatever the sign of its zero imaginary part (a signed zero never gives -pi, though a tiny negative imaginary part can round to it) and zero 0 (a component that is not finite is a `domainError`); `squareRoot` halves each dimension's exponent and refuses a unit whose exponents are not all even; `exponential`, `logarithm` and the trigonometric functions accept only dimensionless numbers and return one, angles in radians. `arcTangent2` is the `phase` of `x + yi`, so it shares that range and signed-zero handling, and its two arguments must be finite.
81
157
  *
82
158
  * Frozen, so mutating the export cannot change what `evaluateDocument` resolves.
83
159
  */
@@ -98,31 +174,35 @@ const builtinFunctions = Object.freeze({
98
174
  return finiteNumber("round", roundHalfAwayFromZero(value.value, places.value), value.unit);
99
175
  },
100
176
  squareRoot: (args) => {
101
- const arg = singleNumber("squareRoot", args);
177
+ const arg = singleNumberOrComplex("squareRoot", args);
102
178
  if ("wrongType" in arg) return arg;
103
179
  const unit = halveUnit(arg.unit);
104
180
  if (unit === false) return { wrongType: "squareRoot requires every unit exponent to be even" };
181
+ if (arg.kind === "complex") return finiteComplex("squareRoot", principalSquareRoot(arg), unit);
105
182
  if (arg.value < 0) return { domainError: "squareRoot of a negative number is not a real number" };
106
183
  return finiteNumber("squareRoot", Math.sqrt(arg.value), unit);
107
184
  },
108
185
  exponential: (args) => {
109
- const arg = singleNumber("exponential", args);
186
+ const arg = singleNumberOrComplex("exponential", args);
110
187
  if ("wrongType" in arg) return arg;
111
- if (!isDimensionless(arg)) return { wrongType: "exponential requires a dimensionless number" };
188
+ if (!unitsEqual(arg.unit, void 0)) return { wrongType: "exponential requires a dimensionless number" };
189
+ if (arg.kind === "complex") return finiteComplex("exponential", complexFromPolar(Math.exp(arg.re), arg.im));
112
190
  return finiteNumber("exponential", Math.exp(arg.value));
113
191
  },
114
192
  logarithm: (args) => {
115
193
  const [value, base, ...extra] = args;
116
- if (!isNumberValue(value) || extra.length > 0) return { wrongType: "logarithm requires a number and an optional base" };
117
- if (!isDimensionless(value)) return { wrongType: "logarithm requires a dimensionless number" };
194
+ if (value?.kind !== "number" && value?.kind !== "complex" || extra.length > 0) return { wrongType: "logarithm requires a number or a complex number and an optional base" };
195
+ if (!unitsEqual(value.unit, void 0)) return { wrongType: "logarithm requires a dimensionless number" };
118
196
  const baseNumber = base === void 0 ? void 0 : dimensionlessNumber(base);
119
197
  if (base !== void 0 && baseNumber === void 0) return { wrongType: "logarithm requires its base to be a dimensionless number" };
120
- if (value.value <= 0) return { domainError: "logarithm requires a number above zero" };
121
- if (baseNumber === void 0) return finiteNumber("logarithm", Math.log(value.value));
122
- if (baseNumber <= 0 || baseNumber === 1) return { domainError: "logarithm requires its base to be above zero and not one" };
123
- if (baseNumber === DECIMAL_BASE) return finiteNumber("logarithm", Math.log10(value.value));
124
- if (baseNumber === BINARY_BASE) return finiteNumber("logarithm", Math.log2(value.value));
125
- return finiteNumber("logarithm", Math.log(value.value) / Math.log(baseNumber));
198
+ if (value.kind === "number" && value.value <= 0) return { domainError: "logarithm requires a number above zero" };
199
+ if (value.kind === "complex" && value.re === 0 && value.im === 0) return { domainError: "logarithm requires a complex number other than zero" };
200
+ if (baseNumber !== void 0 && (baseNumber <= 0 || baseNumber === 1)) return { domainError: "logarithm requires its base to be above zero and not one" };
201
+ if (value.kind === "number") return finiteNumber("logarithm", realLogarithm(value.value, baseNumber));
202
+ return finiteComplex("logarithm", {
203
+ re: realLogarithm(complexMagnitude(value).value, baseNumber),
204
+ im: complexPhase(value).value / (baseNumber === void 0 ? 1 : Math.log(baseNumber))
205
+ });
126
206
  },
127
207
  magnitude: (args) => {
128
208
  const [arg] = args;
@@ -139,6 +219,23 @@ const builtinFunctions = Object.freeze({
139
219
  if (args.length !== 1 || arg?.kind !== "complex") return { wrongType: "phase requires exactly one complex argument" };
140
220
  if (!Number.isFinite(arg.re) || !Number.isFinite(arg.im)) return { domainError: "phase requires finite components" };
141
221
  return complexPhase(arg);
222
+ },
223
+ sine: dimensionlessFunction("sine", Math.sin),
224
+ cosine: dimensionlessFunction("cosine", Math.cos),
225
+ tangent: dimensionlessFunction("tangent", Math.tan),
226
+ arcSine: unitIntervalInverse("arcSine", Math.asin),
227
+ arcCosine: unitIntervalInverse("arcCosine", Math.acos),
228
+ arcTangent: dimensionlessFunction("arcTangent", Math.atan),
229
+ arcTangent2: (args) => {
230
+ const [y, x] = args;
231
+ if (args.length !== 2 || !isNumberValue(y) || !isNumberValue(x)) return { wrongType: "arcTangent2 requires exactly two number arguments" };
232
+ if (!isDimensionless(y) || !isDimensionless(x)) return { wrongType: "arcTangent2 requires dimensionless numbers" };
233
+ if (!Number.isFinite(y.value) || !Number.isFinite(x.value)) return { domainError: "arcTangent2 requires finite arguments" };
234
+ return complexPhase({
235
+ kind: "complex",
236
+ re: x.value,
237
+ im: y.value
238
+ });
142
239
  }
143
240
  });
144
241
  //#endregion
@@ -21,4 +21,5 @@ export declare const complexMagnitude: (value: ComplexValue) => NumberValue;
21
21
  * arg(z), as a dimensionless real number of radians: an angle is a ratio of two lengths, so it carries no unit of its own regardless of what the value it was read from carried. The range is [-pi, pi]: `Math.atan2` returns -pi for a negative real number whose imaginary part is `-0` and +pi when it is `+0`, although the two are the same number and JSON writes both zeros as `0`, and the closed range is what the result can be because a tiny negative imaginary part also rounds to exactly -pi (`phase(-1 - 1e-17i)` is -pi, the true angle being a hair inside). Adding `+0` turns `-0` into `+0` without changing any other value, so a signed zero never gives -pi: a negative real number is +pi whatever the sign of its zero, and zero is 0.
22
22
  */
23
23
  export declare const complexPhase: (value: ComplexValue) => NumberValue;
24
- //#endregion
24
+ //#endregion
25
+ export {};
package/dist/complex.d.ts CHANGED
@@ -21,4 +21,5 @@ export declare const complexMagnitude: (value: ComplexValue) => NumberValue;
21
21
  * arg(z), as a dimensionless real number of radians: an angle is a ratio of two lengths, so it carries no unit of its own regardless of what the value it was read from carried. The range is [-pi, pi]: `Math.atan2` returns -pi for a negative real number whose imaginary part is `-0` and +pi when it is `+0`, although the two are the same number and JSON writes both zeros as `0`, and the closed range is what the result can be because a tiny negative imaginary part also rounds to exactly -pi (`phase(-1 - 1e-17i)` is -pi, the true angle being a hair inside). Adding `+0` turns `-0` into `+0` without changing any other value, so a signed zero never gives -pi: a negative real number is +pi whatever the sign of its zero, and zero is 0.
22
22
  */
23
23
  export declare const complexPhase: (value: ComplexValue) => NumberValue;
24
- //#endregion
24
+ //#endregion
25
+ export {};
@@ -19,4 +19,5 @@ export declare function compareDurations(left: Readonly<{
19
19
  value: number;
20
20
  unit: DurationUnit;
21
21
  }>): Ordering | undefined;
22
- //#endregion
22
+ //#endregion
23
+ export {};
@@ -19,4 +19,5 @@ export declare function compareDurations(left: Readonly<{
19
19
  value: number;
20
20
  unit: DurationUnit;
21
21
  }>): Ordering | undefined;
22
- //#endregion
22
+ //#endregion
23
+ export {};
@@ -34,4 +34,5 @@ export declare function runAsync<T>(task: Readonly<Runnable<T>>): Promise<T>;
34
34
  * Runs a task to completion without awaiting anything: an `all` group's tasks run one after another, and a `wait` on a promise throws, since a synchronous evaluator has nothing to do with one.
35
35
  */
36
36
  export declare function runSync<T>(task: Readonly<Runnable<T>>): T;
37
- //#endregion
37
+ //#endregion
38
+ export {};
@@ -34,4 +34,5 @@ export declare function runAsync<T>(task: Readonly<Runnable<T>>): Promise<T>;
34
34
  * Runs a task to completion without awaiting anything: an `all` group's tasks run one after another, and a `wait` on a promise throws, since a synchronous evaluator has nothing to do with one.
35
35
  */
36
36
  export declare function runSync<T>(task: Readonly<Runnable<T>>): T;
37
- //#endregion
37
+ //#endregion
38
+ export {};
@@ -53,4 +53,5 @@ export interface FixtureOverrides {
53
53
  export declare function pred(description: string, node: PredicateNode, expected: Expected, overrides?: FixtureOverrides): Fixture;
54
54
  export declare function expr(description: string, node: ExpressionNode, expected: Expected, overrides?: FixtureOverrides): Fixture;
55
55
  export declare function runFixture({ run, expected }: Fixture): Promise<void>;
56
- //#endregion
56
+ //#endregion
57
+ export {};
@@ -53,4 +53,5 @@ export interface FixtureOverrides {
53
53
  export declare function pred(description: string, node: PredicateNode, expected: Expected, overrides?: FixtureOverrides): Fixture;
54
54
  export declare function expr(description: string, node: ExpressionNode, expected: Expected, overrides?: FixtureOverrides): Fixture;
55
55
  export declare function runFixture({ run, expected }: Fixture): Promise<void>;
56
- //#endregion
56
+ //#endregion
57
+ export {};
@@ -4,7 +4,7 @@ import { parseArgs } from "node:util";
4
4
  import { z } from "zod";
5
5
  import { RegexParseError, compilePattern } from "trilean-regex";
6
6
  //#region package.json
7
- var version = "1.9.2";
7
+ var version = "1.10.1";
8
8
  //#endregion
9
9
  //#region src/computed-value.ts
10
10
  /** Dimension symbol -\> exponent, e.g. `{ m: 1, s: -1 }` for metres per second. A bare symbol like `"kg"` is shorthand for `{ kg: 1 }`. */
@@ -1384,6 +1384,80 @@ function singleNumber(name, args) {
1384
1384
  if (args.length !== 1 || !isNumberValue(arg)) return { wrongType: `${name} requires exactly one number argument` };
1385
1385
  return arg;
1386
1386
  }
1387
+ /** The one exit for every complex result, as `finiteNumber` is for real ones. A complex argument always gives a complex result, even when the imaginary part comes out zero, as the evaluator's own complex arithmetic does: the result kind follows the argument kind, never the value. */
1388
+ function finiteComplex(name, { re, im }, unit) {
1389
+ if (!Number.isFinite(re) || !Number.isFinite(im)) return { domainError: `${name} produced a result that is not a finite number` };
1390
+ return unit === void 0 ? {
1391
+ kind: "complex",
1392
+ re,
1393
+ im
1394
+ } : {
1395
+ kind: "complex",
1396
+ re,
1397
+ im,
1398
+ unit
1399
+ };
1400
+ }
1401
+ function singleNumberOrComplex(name, args) {
1402
+ const [arg] = args;
1403
+ if (args.length !== 1 || arg?.kind !== "number" && arg?.kind !== "complex") return { wrongType: `${name} requires exactly one number or complex number argument` };
1404
+ return arg;
1405
+ }
1406
+ /** The value of a single dimensionless real argument, or the `wrongType` refusing anything else (a complex value included). */
1407
+ function singleDimensionless(name, args) {
1408
+ const arg = singleNumber(name, args);
1409
+ if ("wrongType" in arg) return arg;
1410
+ if (!isDimensionless(arg)) return { wrongType: `${name} requires a dimensionless number` };
1411
+ return arg.value;
1412
+ }
1413
+ /** A real function of one dimensionless number, returning a dimensionless number. */
1414
+ function dimensionlessFunction(name, apply) {
1415
+ return (args) => {
1416
+ const value = singleDimensionless(name, args);
1417
+ if (typeof value !== "number") return value;
1418
+ return finiteNumber(name, apply(value));
1419
+ };
1420
+ }
1421
+ /** An inverse sine or cosine, whose real domain is the closed interval from -1 to 1; outside it the inverse is not a real number, which is a `domainError` rather than the `NaN` `Math.asin` and `Math.acos` return. */
1422
+ function unitIntervalInverse(name, apply) {
1423
+ return (args) => {
1424
+ const value = singleDimensionless(name, args);
1425
+ if (typeof value !== "number") return value;
1426
+ if (!(Math.abs(value) <= 1)) return { domainError: `${name} requires a number from -1 to 1` };
1427
+ return finiteNumber(name, apply(value));
1428
+ };
1429
+ }
1430
+ /**
1431
+ * The principal square root of a complex value: the root whose real part is not negative, with the branch cut along the negative real axis. On the cut the root lies on the positive imaginary axis whatever the sign of the zero imaginary part, matching `complexPhase`'s +pi for a negative real number, so `sqrt(z)` is `sqrt(|z|) * e^(i * phase(z) / 2)` everywhere.
1432
+ *
1433
+ * It is computed by the closed form rather than through that polar expression: with `t = sqrt((|re| + |z|) / 2)`, the root is `t + (im / 2t)i` when `re` is not negative and `|im| / 2t + t i` (with the sign of `im` on `t`) otherwise. Neither form subtracts two nearly equal quantities, and an exactly representable root comes out exact (`sqrt(-4)` is `2i`, where `cos(pi / 2)` would leave a stray real part of about 1e-16).
1434
+ */
1435
+ function principalSquareRoot(value) {
1436
+ const modulus = complexMagnitude(value).value;
1437
+ if (modulus === 0) return {
1438
+ re: 0,
1439
+ im: 0
1440
+ };
1441
+ const sum = Math.abs(value.re) + modulus;
1442
+ const half = Number.isFinite(sum) ? sum / 2 : Math.abs(value.re) / 2 + modulus / 2;
1443
+ const t = Math.sqrt(half);
1444
+ const { im } = value;
1445
+ if (value.re >= 0) return {
1446
+ re: t,
1447
+ im: im / (2 * t)
1448
+ };
1449
+ return {
1450
+ re: Math.abs(im) / (2 * t),
1451
+ im: im < 0 ? -t : t
1452
+ };
1453
+ }
1454
+ /** The logarithm of a positive real number, natural or to a base already checked to be above zero and not one. */
1455
+ function realLogarithm(value, base) {
1456
+ if (base === void 0) return Math.log(value);
1457
+ if (base === DECIMAL_BASE) return Math.log10(value);
1458
+ if (base === BINARY_BASE) return Math.log2(value);
1459
+ return Math.log(value) / Math.log(base);
1460
+ }
1387
1461
  /** Halves every dimension's exponent, or `false` when one is odd and the root would need a fractional exponent. */
1388
1462
  function halveUnit(unit) {
1389
1463
  if (unit === void 0) return void 0;
@@ -1423,11 +1497,13 @@ function roundHalfAwayFromZero(value, places) {
1423
1497
  return rounded === 0 ? 0 : Math.sign(value) * rounded;
1424
1498
  }
1425
1499
  /**
1426
- * The numeric functions a `call` node can name when an embedder has no registry of its own: `minimum` and `maximum` (one or more numbers in a shared unit), `absoluteValue`, `round` (half away from zero, with an optional count of decimal places), `squareRoot`, `exponential`, `logarithm` (natural, or to an optional base), and the two projections of a complex value onto the reals, `magnitude` and `phase`. Every function but those two takes real numbers only, and refuses a complex value; `magnitude` takes a real or a complex number, and `phase` a complex one.
1500
+ * The numeric functions a `call` node can name when an embedder has no registry of its own: `minimum` and `maximum` (one or more numbers in a shared unit), `absoluteValue`, `round` (half away from zero, with an optional count of decimal places), `squareRoot`, `exponential`, `logarithm` (natural, or to an optional real base), the two projections of a complex value onto the reals, `magnitude` and `phase`, and the trigonometric functions `sine`, `cosine`, `tangent`, `arcSine`, `arcCosine`, `arcTangent` and `arcTangent2` (the angle of the point `(x, y)`, called with `y` first).
1427
1501
  *
1428
- * Failures split as the evaluator's own operators split. An argument a function cannot accept at all (the wrong kind, the wrong count, or units that do not fit) is a `wrongType`; an argument of the right kind outside the function's domain (a negative `squareRoot`, a `logarithm` of zero, a base of one, a fractional count of decimal places) or a result that is not a finite number is a `domainError`.
1502
+ * Which take a complex value: `squareRoot`, `exponential`, `logarithm` and `magnitude` take a real or a complex number, and `phase` a complex one only. Every other function takes real numbers only and refuses a complex value. A real argument to `squareRoot`, `exponential` or `logarithm` gives a real result, so a negative `squareRoot` or a `logarithm` of zero or a negative number stays a `domainError` rather than becoming complex; a complex argument gives a complex result, the principal value, even when its imaginary part comes out zero, as the evaluator's own complex arithmetic does. The principal values take the branch cut along the negative real axis with `phase`'s convention on it: `logarithm` is `log|z| + i phase(z)` (divided through by the natural logarithm of a base when one is given), so its imaginary part lies in [-pi, pi], and `squareRoot` is the root with a real part that is not negative, on the positive imaginary axis for a negative real number whatever the sign of its zero imaginary part.
1429
1503
  *
1430
- * Units follow the evaluator's rule that dimensions are never silently converted. `minimum`, `maximum`, `absoluteValue`, `round` and `magnitude` keep the argument's unit (and the first two require all arguments to share one); `phase` returns a dimensionless angle in radians in the range [-pi, pi], whatever unit its argument carries, with a negative real number +pi whatever the sign of its zero imaginary part (a signed zero never gives -pi, though a tiny negative imaginary part can round to it) and zero 0 (a component that is not finite is a `domainError`); `squareRoot` halves each dimension's exponent and refuses a unit whose exponents are not all even; `exponential` and `logarithm` accept only dimensionless numbers and return one.
1504
+ * Failures split as the evaluator's own operators split. An argument a function cannot accept at all (the wrong kind, the wrong count, or units that do not fit) is a `wrongType`; an argument of the right kind outside the function's domain (a negative real `squareRoot`, a `logarithm` of zero, a base of one, a fractional count of decimal places, an `arcSine` or `arcCosine` outside -1 to 1) or a result that is not a finite number (in either component of a complex one) is a `domainError`. A `tangent` near a pole is large but finite, since no double is an odd multiple of pi/2 exactly.
1505
+ *
1506
+ * Units follow the evaluator's rule that dimensions are never silently converted. `minimum`, `maximum`, `absoluteValue`, `round` and `magnitude` keep the argument's unit (and the first two require all arguments to share one); `phase` returns a dimensionless angle in radians in the range [-pi, pi], whatever unit its argument carries, with a negative real number +pi whatever the sign of its zero imaginary part (a signed zero never gives -pi, though a tiny negative imaginary part can round to it) and zero 0 (a component that is not finite is a `domainError`); `squareRoot` halves each dimension's exponent and refuses a unit whose exponents are not all even; `exponential`, `logarithm` and the trigonometric functions accept only dimensionless numbers and return one, angles in radians. `arcTangent2` is the `phase` of `x + yi`, so it shares that range and signed-zero handling, and its two arguments must be finite.
1431
1507
  *
1432
1508
  * Frozen, so mutating the export cannot change what `evaluateDocument` resolves.
1433
1509
  */
@@ -1448,31 +1524,35 @@ const builtinFunctions = Object.freeze({
1448
1524
  return finiteNumber("round", roundHalfAwayFromZero(value.value, places.value), value.unit);
1449
1525
  },
1450
1526
  squareRoot: (args) => {
1451
- const arg = singleNumber("squareRoot", args);
1527
+ const arg = singleNumberOrComplex("squareRoot", args);
1452
1528
  if ("wrongType" in arg) return arg;
1453
1529
  const unit = halveUnit(arg.unit);
1454
1530
  if (unit === false) return { wrongType: "squareRoot requires every unit exponent to be even" };
1531
+ if (arg.kind === "complex") return finiteComplex("squareRoot", principalSquareRoot(arg), unit);
1455
1532
  if (arg.value < 0) return { domainError: "squareRoot of a negative number is not a real number" };
1456
1533
  return finiteNumber("squareRoot", Math.sqrt(arg.value), unit);
1457
1534
  },
1458
1535
  exponential: (args) => {
1459
- const arg = singleNumber("exponential", args);
1536
+ const arg = singleNumberOrComplex("exponential", args);
1460
1537
  if ("wrongType" in arg) return arg;
1461
- if (!isDimensionless(arg)) return { wrongType: "exponential requires a dimensionless number" };
1538
+ if (!unitsEqual(arg.unit, void 0)) return { wrongType: "exponential requires a dimensionless number" };
1539
+ if (arg.kind === "complex") return finiteComplex("exponential", complexFromPolar(Math.exp(arg.re), arg.im));
1462
1540
  return finiteNumber("exponential", Math.exp(arg.value));
1463
1541
  },
1464
1542
  logarithm: (args) => {
1465
1543
  const [value, base, ...extra] = args;
1466
- if (!isNumberValue(value) || extra.length > 0) return { wrongType: "logarithm requires a number and an optional base" };
1467
- if (!isDimensionless(value)) return { wrongType: "logarithm requires a dimensionless number" };
1544
+ if (value?.kind !== "number" && value?.kind !== "complex" || extra.length > 0) return { wrongType: "logarithm requires a number or a complex number and an optional base" };
1545
+ if (!unitsEqual(value.unit, void 0)) return { wrongType: "logarithm requires a dimensionless number" };
1468
1546
  const baseNumber = base === void 0 ? void 0 : dimensionlessNumber(base);
1469
1547
  if (base !== void 0 && baseNumber === void 0) return { wrongType: "logarithm requires its base to be a dimensionless number" };
1470
- if (value.value <= 0) return { domainError: "logarithm requires a number above zero" };
1471
- if (baseNumber === void 0) return finiteNumber("logarithm", Math.log(value.value));
1472
- if (baseNumber <= 0 || baseNumber === 1) return { domainError: "logarithm requires its base to be above zero and not one" };
1473
- if (baseNumber === DECIMAL_BASE) return finiteNumber("logarithm", Math.log10(value.value));
1474
- if (baseNumber === BINARY_BASE) return finiteNumber("logarithm", Math.log2(value.value));
1475
- return finiteNumber("logarithm", Math.log(value.value) / Math.log(baseNumber));
1548
+ if (value.kind === "number" && value.value <= 0) return { domainError: "logarithm requires a number above zero" };
1549
+ if (value.kind === "complex" && value.re === 0 && value.im === 0) return { domainError: "logarithm requires a complex number other than zero" };
1550
+ if (baseNumber !== void 0 && (baseNumber <= 0 || baseNumber === 1)) return { domainError: "logarithm requires its base to be above zero and not one" };
1551
+ if (value.kind === "number") return finiteNumber("logarithm", realLogarithm(value.value, baseNumber));
1552
+ return finiteComplex("logarithm", {
1553
+ re: realLogarithm(complexMagnitude(value).value, baseNumber),
1554
+ im: complexPhase(value).value / (baseNumber === void 0 ? 1 : Math.log(baseNumber))
1555
+ });
1476
1556
  },
1477
1557
  magnitude: (args) => {
1478
1558
  const [arg] = args;
@@ -1489,6 +1569,23 @@ const builtinFunctions = Object.freeze({
1489
1569
  if (args.length !== 1 || arg?.kind !== "complex") return { wrongType: "phase requires exactly one complex argument" };
1490
1570
  if (!Number.isFinite(arg.re) || !Number.isFinite(arg.im)) return { domainError: "phase requires finite components" };
1491
1571
  return complexPhase(arg);
1572
+ },
1573
+ sine: dimensionlessFunction("sine", Math.sin),
1574
+ cosine: dimensionlessFunction("cosine", Math.cos),
1575
+ tangent: dimensionlessFunction("tangent", Math.tan),
1576
+ arcSine: unitIntervalInverse("arcSine", Math.asin),
1577
+ arcCosine: unitIntervalInverse("arcCosine", Math.acos),
1578
+ arcTangent: dimensionlessFunction("arcTangent", Math.atan),
1579
+ arcTangent2: (args) => {
1580
+ const [y, x] = args;
1581
+ if (args.length !== 2 || !isNumberValue(y) || !isNumberValue(x)) return { wrongType: "arcTangent2 requires exactly two number arguments" };
1582
+ if (!isDimensionless(y) || !isDimensionless(x)) return { wrongType: "arcTangent2 requires dimensionless numbers" };
1583
+ if (!Number.isFinite(y.value) || !Number.isFinite(x.value)) return { domainError: "arcTangent2 requires finite arguments" };
1584
+ return complexPhase({
1585
+ kind: "complex",
1586
+ re: x.value,
1587
+ im: y.value
1588
+ });
1492
1589
  }
1493
1590
  });
1494
1591
  //#endregion
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "trilean",
3
3
  "description": "Three-valued predicate and expression evaluation trees, stored as JSON and evaluated against injected resolvers, for domain logic — business rules, eligibility checks, formulas, search filters, and more — that needs to be data instead of code.",
4
- "version": "1.9.2",
4
+ "version": "1.10.1",
5
5
  "bin": {
6
6
  "trilean": "./dist-cli/trilean.mjs"
7
7
  },
@@ -9,7 +9,7 @@
9
9
  "url": "https://github.com/ExaDev/trilean/issues"
10
10
  },
11
11
  "dependencies": {
12
- "trilean-regex": "1.0.15",
12
+ "trilean-regex": "1.0.16",
13
13
  "zod": "^4.6.5"
14
14
  },
15
15
  "devDependencies": {
@@ -25,7 +25,7 @@
25
25
  "globals": "^17.13.0",
26
26
  "prettier": "^3.9.10",
27
27
  "publint": "^0.3.25",
28
- "tsdown": "^0.23.0",
28
+ "tsdown": "^0.23.1",
29
29
  "tsx": "^4.23.15",
30
30
  "turbo": "^2.11.7",
31
31
  "typescript": "^6.0.3",
@@ -1 +1 @@
1
- {"$defs":{"ExpressionNode":{"anyOf":[{"oneOf":[{"additionalProperties":false,"properties":{"kind":{"const":"numberLiteral","type":"string"},"unit":{"additionalProperties":{"type":"number"},"propertyNames":{"type":"string"},"type":"object"},"value":{"type":"number"}},"required":["kind","value"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"textLiteral","type":"string"},"value":{"type":"string"}},"required":["kind","value"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"booleanLiteral","type":"string"},"value":{"type":"boolean"}},"required":["kind","value"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"instantLiteral","type":"string"},"value":{"type":"string"}},"required":["kind","value"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"durationLiteral","type":"string"},"unit":{"enum":["ms","s","min","h","d"],"type":"string"},"value":{"type":"number"}},"required":["kind","value","unit"],"type":"object"},{"additionalProperties":false,"properties":{"key":{"$ref":"#/$defs/schema0"},"kind":{"const":"reference","type":"string"},"unit":{"additionalProperties":{"type":"number"},"propertyNames":{"type":"string"},"type":"object"}},"required":["kind","key"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"arithmetic","type":"string"},"left":{"$ref":"#/$defs/ExpressionNode"},"op":{"enum":["add","subtract","multiply","divide","power","modulo"],"type":"string"},"right":{"$ref":"#/$defs/ExpressionNode"}},"required":["kind","op","left","right"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"negate","type":"string"},"operand":{"$ref":"#/$defs/ExpressionNode"}},"required":["kind","operand"],"type":"object"},{"additionalProperties":false,"properties":{"args":{"items":{"$ref":"#/$defs/ExpressionNode"},"type":"array"},"fn":{"type":"string"},"kind":{"const":"call","type":"string"}},"required":["kind","fn","args"],"type":"object"},{"additionalProperties":false,"properties":{"keys":{"items":{"$ref":"#/$defs/ExpressionNode"},"type":"array"},"kind":{"const":"lookup","type":"string"},"table":{"$ref":"#/$defs/schema0"}},"required":["kind","table","keys"],"type":"object"},{"additionalProperties":false,"properties":{"cases":{"items":{"additionalProperties":false,"properties":{"then":{"$ref":"#/$defs/ExpressionNode"},"when":{"$ref":"#/$defs/PredicateNode"}},"required":["when","then"],"type":"object"},"type":"array"},"fallback":{"$ref":"#/$defs/ExpressionNode"},"hitPolicy":{"enum":["first","unique"],"type":"string"},"kind":{"const":"conditional","type":"string"}},"required":["kind","cases","fallback"],"type":"object"},{"additionalProperties":false,"properties":{"collection":{"$ref":"#/$defs/schema0"},"combiner":{"oneOf":[{"additionalProperties":false,"properties":{"item":{"$ref":"#/$defs/ExpressionNode"},"mode":{"const":"max","type":"string"}},"required":["mode","item"],"type":"object"},{"additionalProperties":false,"properties":{"item":{"$ref":"#/$defs/ExpressionNode"},"mode":{"const":"min","type":"string"}},"required":["mode","item"],"type":"object"},{"additionalProperties":false,"properties":{"combine":{"$ref":"#/$defs/ExpressionNode"},"initial":{"$ref":"#/$defs/ExpressionNode"},"mode":{"const":"reduce","type":"string"}},"required":["mode","initial","combine"],"type":"object"}]},"filter":{"$ref":"#/$defs/PredicateNode"},"kind":{"const":"fold","type":"string"}},"required":["kind","collection","combiner"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"accumulator","type":"string"}},"required":["kind"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"delegate","type":"string"},"payload":{"$ref":"#/$defs/schema0"},"system":{"type":"string"}},"required":["kind","system","payload"],"type":"object"},{"additionalProperties":false,"properties":{"key":{"$ref":"#/$defs/schema0"},"kind":{"const":"treeReference","type":"string"}},"required":["kind","key"],"type":"object"}]},{"anyOf":[{"additionalProperties":false,"properties":{"im":{"type":"number"},"kind":{"const":"complexLiteral","type":"string"},"re":{"type":"number"},"unit":{"additionalProperties":{"type":"number"},"propertyNames":{"type":"string"},"type":"object"}},"required":["kind","re","im"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"complexLiteral","type":"string"},"magnitude":{"type":"number"},"phase":{"type":"number"},"unit":{"additionalProperties":{"type":"number"},"propertyNames":{"type":"string"},"type":"object"}},"required":["kind","magnitude","phase"],"type":"object"}]}]},"PredicateNode":{"oneOf":[{"additionalProperties":false,"properties":{"kind":{"const":"not","type":"string"},"operand":{"$ref":"#/$defs/PredicateNode"}},"required":["kind","operand"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"and","type":"string"},"left":{"$ref":"#/$defs/PredicateNode"},"right":{"$ref":"#/$defs/PredicateNode"}},"required":["kind","left","right"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"or","type":"string"},"left":{"$ref":"#/$defs/PredicateNode"},"right":{"$ref":"#/$defs/PredicateNode"}},"required":["kind","left","right"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"allOf","type":"string"},"operands":{"items":{"$ref":"#/$defs/PredicateNode"},"type":"array"}},"required":["kind","operands"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"anyOf","type":"string"},"operands":{"items":{"$ref":"#/$defs/PredicateNode"},"type":"array"}},"required":["kind","operands"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"compare","type":"string"},"left":{"$ref":"#/$defs/ExpressionNode"},"op":{"enum":["gt","gte","lt","lte","eq","neq"],"type":"string"},"right":{"$ref":"#/$defs/ExpressionNode"}},"required":["kind","op","left","right"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"textCompare","type":"string"},"left":{"$ref":"#/$defs/ExpressionNode"},"op":{"enum":["equals","notEquals","matches","notMatches","portableMatches","portableNotMatches"],"type":"string"},"right":{"$ref":"#/$defs/ExpressionNode"}},"required":["kind","op","left","right"],"type":"object"},{"additionalProperties":false,"properties":{"candidates":{"items":{"$ref":"#/$defs/ExpressionNode"},"type":"array"},"kind":{"const":"memberOf","type":"string"},"op":{"enum":["in","notIn"],"type":"string"},"operand":{"$ref":"#/$defs/ExpressionNode"}},"required":["kind","op","operand","candidates"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"exists","type":"string"},"operand":{"$ref":"#/$defs/ExpressionNode"}},"required":["kind","operand"],"type":"object"},{"additionalProperties":false,"properties":{"collection":{"$ref":"#/$defs/schema0"},"filter":{"$ref":"#/$defs/PredicateNode"},"item":{"$ref":"#/$defs/PredicateNode"},"kind":{"const":"some","type":"string"}},"required":["kind","collection","item"],"type":"object"},{"additionalProperties":false,"properties":{"collection":{"$ref":"#/$defs/schema0"},"filter":{"$ref":"#/$defs/PredicateNode"},"item":{"$ref":"#/$defs/PredicateNode"},"kind":{"const":"every","type":"string"}},"required":["kind","collection","item"],"type":"object"},{"additionalProperties":false,"properties":{"key":{"$ref":"#/$defs/schema0"},"kind":{"const":"treeReference","type":"string"}},"required":["kind","key"],"type":"object"}]},"schema0":{"anyOf":[{"type":"string"},{"type":"number"},{"type":"boolean"},{"type":"null"},{"items":{"$ref":"#/$defs/schema0"},"type":"array"},{"additionalProperties":{"$ref":"#/$defs/schema0"},"propertyNames":{"type":"string"},"type":"object"}]}},"$id":"https://cdn.jsdelivr.net/npm/trilean@1.9.2/schemas/trilean.schema.json","$schema":"https://json-schema.org/draft/2020-12/schema","oneOf":[{"$ref":"#/$defs/PredicateNode"},{"$ref":"#/$defs/ExpressionNode"}]}
1
+ {"$defs":{"ExpressionNode":{"anyOf":[{"oneOf":[{"additionalProperties":false,"properties":{"kind":{"const":"numberLiteral","type":"string"},"unit":{"additionalProperties":{"type":"number"},"propertyNames":{"type":"string"},"type":"object"},"value":{"type":"number"}},"required":["kind","value"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"textLiteral","type":"string"},"value":{"type":"string"}},"required":["kind","value"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"booleanLiteral","type":"string"},"value":{"type":"boolean"}},"required":["kind","value"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"instantLiteral","type":"string"},"value":{"type":"string"}},"required":["kind","value"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"durationLiteral","type":"string"},"unit":{"enum":["ms","s","min","h","d"],"type":"string"},"value":{"type":"number"}},"required":["kind","value","unit"],"type":"object"},{"additionalProperties":false,"properties":{"key":{"$ref":"#/$defs/schema0"},"kind":{"const":"reference","type":"string"},"unit":{"additionalProperties":{"type":"number"},"propertyNames":{"type":"string"},"type":"object"}},"required":["kind","key"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"arithmetic","type":"string"},"left":{"$ref":"#/$defs/ExpressionNode"},"op":{"enum":["add","subtract","multiply","divide","power","modulo"],"type":"string"},"right":{"$ref":"#/$defs/ExpressionNode"}},"required":["kind","op","left","right"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"negate","type":"string"},"operand":{"$ref":"#/$defs/ExpressionNode"}},"required":["kind","operand"],"type":"object"},{"additionalProperties":false,"properties":{"args":{"items":{"$ref":"#/$defs/ExpressionNode"},"type":"array"},"fn":{"type":"string"},"kind":{"const":"call","type":"string"}},"required":["kind","fn","args"],"type":"object"},{"additionalProperties":false,"properties":{"keys":{"items":{"$ref":"#/$defs/ExpressionNode"},"type":"array"},"kind":{"const":"lookup","type":"string"},"table":{"$ref":"#/$defs/schema0"}},"required":["kind","table","keys"],"type":"object"},{"additionalProperties":false,"properties":{"cases":{"items":{"additionalProperties":false,"properties":{"then":{"$ref":"#/$defs/ExpressionNode"},"when":{"$ref":"#/$defs/PredicateNode"}},"required":["when","then"],"type":"object"},"type":"array"},"fallback":{"$ref":"#/$defs/ExpressionNode"},"hitPolicy":{"enum":["first","unique"],"type":"string"},"kind":{"const":"conditional","type":"string"}},"required":["kind","cases","fallback"],"type":"object"},{"additionalProperties":false,"properties":{"collection":{"$ref":"#/$defs/schema0"},"combiner":{"oneOf":[{"additionalProperties":false,"properties":{"item":{"$ref":"#/$defs/ExpressionNode"},"mode":{"const":"max","type":"string"}},"required":["mode","item"],"type":"object"},{"additionalProperties":false,"properties":{"item":{"$ref":"#/$defs/ExpressionNode"},"mode":{"const":"min","type":"string"}},"required":["mode","item"],"type":"object"},{"additionalProperties":false,"properties":{"combine":{"$ref":"#/$defs/ExpressionNode"},"initial":{"$ref":"#/$defs/ExpressionNode"},"mode":{"const":"reduce","type":"string"}},"required":["mode","initial","combine"],"type":"object"}]},"filter":{"$ref":"#/$defs/PredicateNode"},"kind":{"const":"fold","type":"string"}},"required":["kind","collection","combiner"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"accumulator","type":"string"}},"required":["kind"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"delegate","type":"string"},"payload":{"$ref":"#/$defs/schema0"},"system":{"type":"string"}},"required":["kind","system","payload"],"type":"object"},{"additionalProperties":false,"properties":{"key":{"$ref":"#/$defs/schema0"},"kind":{"const":"treeReference","type":"string"}},"required":["kind","key"],"type":"object"}]},{"anyOf":[{"additionalProperties":false,"properties":{"im":{"type":"number"},"kind":{"const":"complexLiteral","type":"string"},"re":{"type":"number"},"unit":{"additionalProperties":{"type":"number"},"propertyNames":{"type":"string"},"type":"object"}},"required":["kind","re","im"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"complexLiteral","type":"string"},"magnitude":{"type":"number"},"phase":{"type":"number"},"unit":{"additionalProperties":{"type":"number"},"propertyNames":{"type":"string"},"type":"object"}},"required":["kind","magnitude","phase"],"type":"object"}]}]},"PredicateNode":{"oneOf":[{"additionalProperties":false,"properties":{"kind":{"const":"not","type":"string"},"operand":{"$ref":"#/$defs/PredicateNode"}},"required":["kind","operand"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"and","type":"string"},"left":{"$ref":"#/$defs/PredicateNode"},"right":{"$ref":"#/$defs/PredicateNode"}},"required":["kind","left","right"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"or","type":"string"},"left":{"$ref":"#/$defs/PredicateNode"},"right":{"$ref":"#/$defs/PredicateNode"}},"required":["kind","left","right"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"allOf","type":"string"},"operands":{"items":{"$ref":"#/$defs/PredicateNode"},"type":"array"}},"required":["kind","operands"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"anyOf","type":"string"},"operands":{"items":{"$ref":"#/$defs/PredicateNode"},"type":"array"}},"required":["kind","operands"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"compare","type":"string"},"left":{"$ref":"#/$defs/ExpressionNode"},"op":{"enum":["gt","gte","lt","lte","eq","neq"],"type":"string"},"right":{"$ref":"#/$defs/ExpressionNode"}},"required":["kind","op","left","right"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"textCompare","type":"string"},"left":{"$ref":"#/$defs/ExpressionNode"},"op":{"enum":["equals","notEquals","matches","notMatches","portableMatches","portableNotMatches"],"type":"string"},"right":{"$ref":"#/$defs/ExpressionNode"}},"required":["kind","op","left","right"],"type":"object"},{"additionalProperties":false,"properties":{"candidates":{"items":{"$ref":"#/$defs/ExpressionNode"},"type":"array"},"kind":{"const":"memberOf","type":"string"},"op":{"enum":["in","notIn"],"type":"string"},"operand":{"$ref":"#/$defs/ExpressionNode"}},"required":["kind","op","operand","candidates"],"type":"object"},{"additionalProperties":false,"properties":{"kind":{"const":"exists","type":"string"},"operand":{"$ref":"#/$defs/ExpressionNode"}},"required":["kind","operand"],"type":"object"},{"additionalProperties":false,"properties":{"collection":{"$ref":"#/$defs/schema0"},"filter":{"$ref":"#/$defs/PredicateNode"},"item":{"$ref":"#/$defs/PredicateNode"},"kind":{"const":"some","type":"string"}},"required":["kind","collection","item"],"type":"object"},{"additionalProperties":false,"properties":{"collection":{"$ref":"#/$defs/schema0"},"filter":{"$ref":"#/$defs/PredicateNode"},"item":{"$ref":"#/$defs/PredicateNode"},"kind":{"const":"every","type":"string"}},"required":["kind","collection","item"],"type":"object"},{"additionalProperties":false,"properties":{"key":{"$ref":"#/$defs/schema0"},"kind":{"const":"treeReference","type":"string"}},"required":["kind","key"],"type":"object"}]},"schema0":{"anyOf":[{"type":"string"},{"type":"number"},{"type":"boolean"},{"type":"null"},{"items":{"$ref":"#/$defs/schema0"},"type":"array"},{"additionalProperties":{"$ref":"#/$defs/schema0"},"propertyNames":{"type":"string"},"type":"object"}]}},"$id":"https://cdn.jsdelivr.net/npm/trilean@1.10.1/schemas/trilean.schema.json","$schema":"https://json-schema.org/draft/2020-12/schema","oneOf":[{"$ref":"#/$defs/PredicateNode"},{"$ref":"#/$defs/ExpressionNode"}]}