Skip to content

Commit 3b30a1e

Browse files
committed
Print a node's Codomain, so the printed form stops losing it (#1022)
`Stringize` printed no node's `Codomain`, so `domain(x, ZZ)` printed as `x` and parsing that gave a `Variable` with `Codomain = Any` back. The annotation is not decoration: `sqrt(-1)` is `i` and the same expression over the reals is `NaN`, and the two printed the same string. The parser already had the syntax -- `domain(expr, SET)` maps onto `WithCodomain` and works for every node -- so only the printing half was missing. A node whose codomain is not the one its type carries by default now prints inside `domain(...)`, and LaTeX renders it as a subscripted set. The default is not uniform, which is why the rule is a comparison and not a check against `Complex`: a variable and a matrix default to `Any`, `abs` and an interval to `Real`, every boolean node to `Boolean`, each numeric literal to its own type's domain. Each node declares that default beside its `Codomain`, both abstract so that a node declaring one and forgetting the other does not compile. The wrapper is one decision, so it lives once on `Entity` and the per-node rendering moved behind `StringizeNode`/`LatexizeNode`. That removes 130 overrides from the recorded public surface; every one of them is still callable, inherited. `Simplify` collected `x` and the same `x` narrowed to the integers as one monomial, because it keys a monomial by its base's printed form: `x - domain(x, ZZ)` was `0`, which is the answer only where `x` is an integer. It is left alone now. Two annotations still do not survive, both the grammar's limit rather than the printer's, and both pinned by tests that fail if they start working: `Any` has no special set to name it, and no input yields a `Rational` whose codomain is `Complex`, since the pass that reads `1/2` as a rational treats `Complex` as "nobody annotated this" (#873). Measured on a build of each side; 8070 -> 8516 passed, 0 failed. The two expected outputs that moved are the test written to fail when this is fixed and the recorded public surface. #1022 #746
1 parent cd0b56b commit 3b30a1e

30 files changed

Lines changed: 781 additions & 311 deletions

‎BREAKING-CHANGES.md‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ read first.
3434
| | any expression mixing a number with a `Complex` argument, `Compile`d in a NativeAOT app — `"x + 1".Compile<Complex, Complex>("x")` | `UncompilableNodeException: ... The binary operator Add is not defined for the types 'System.Numerics.Complex' and 'System.Numerics.Complex'` | the compiled function, answering as it does under the JIT |
3535
| | `Compile` to a nullable integral return type in a NativeAOT app | `AngouriBugException: IsNaN method expected for type System.Double`, which took the process down | the compiled function |
3636
| **Silent** | an app publishing with `PublishTrimmed` or NativeAOT | `AngouriMath.dll` was copied in whole, being unmarked | it is trimmed with the rest, since the assembly now declares `IsTrimmable` |
37+
| **Silent** | `"domain(x, ZZ)".ToEntity().Stringize()`, and `ToString`, and `EntityJsonConverter` | `x`, which reads back with `Codomain = Any` | `domain(x, ZZ)`, which reads back narrowed |
38+
| **Silent** | `"domain(sqrt(-1), RR)".ToEntity().Stringize()` | `sqrt(-1)`, which evaluates to `i` when read back | `domain(sqrt(-1), RR)`, which evaluates to `NaN` |
39+
| **Silent** | `"domain(x, ZZ)".ToEntity().Latexize()` | `x` | `{\left(x\right)}_{\mathbb{Z}}` |
40+
| **Silent** | `"x - domain(x, ZZ)".ToEntity().Simplify()`, and any sum mixing a node with a narrowed codomain and the same node without | `0` — the two were collected as one monomial | `x - domain(x, ZZ)`, left alone |
41+
| | every node type's own `Stringize()` and `Latexize()` overrides — `Entity.Sumf.Stringize()` and 129 more | declared on each node | declared once on `Entity`; still callable on every node, and a consumer compiled against 2.3.0 has to be rebuilt |
3742

3843
### A polynomial inequality of degree three or more is answered
3944

@@ -156,6 +161,78 @@ bracketing for. The change only ever adds `\left(`/`\right)` groups, which CShar
156161
parses, so nothing downstream needs a matching change
157162
([#822](https://github.com/asc-community/AngouriMath/issues/822)).
158163

164+
### A narrowed `Codomain` is printed, so it survives being read back
165+
166+
`Codomain` decides evaluation — `sqrt(-1)` is `i`, and the same expression with `Codomain = Real`
167+
is `NaN`, which is the example on `Entity.Codomain` itself. No node printed it, so the two printed
168+
the same string and the annotation was lost the moment an expression was written out: to a file, to
169+
a database column, through `EntityJsonConverter`, or to another process.
170+
[#1022](https://github.com/asc-community/AngouriMath/issues/1022).
171+
172+
The parser already had the syntax. `domain(expr, SET)` maps onto `WithCodomain` and works for every
173+
node, not only a variable, so only the printing half was missing. Measured on a build of 2.3.0 and a
174+
build of this branch:
175+
176+
| expression | 2.3.0 printed | 2.3.0 read that back as | now prints |
177+
|---|---|---|---|
178+
| `"domain(x, ZZ)".ToEntity()` | `x` | a `Variable` with `Codomain = Any` | `domain(x, ZZ)` |
179+
| `"domain(x + 1, RR)".ToEntity()` | `x + 1` | a `Sumf` with `Codomain = Complex` | `domain(x + 1, RR)` |
180+
| `"domain(sqrt(-1), RR)".ToEntity()` | `sqrt(-1)` | a `Powf` that evaluates to `i` | `domain(sqrt(-1), RR)` |
181+
| `"domain([1, 2], RR)".ToEntity()` | `[1, 2]` | a `Matrix` with `Codomain = Any` | `domain([1, 2], RR)` |
182+
| `Sin(Var("x").WithCodomain(Integer)) + Var("y").WithCodomain(Real)` | `sin(x) + y` | both annotations gone | `sin(domain(x, ZZ)) + domain(y, RR)` |
183+
184+
`EntityJsonConverter` serialises what `Stringize` prints, so it changes with it and needed no code:
185+
`JsonSerializer.Serialize("domain(x, ZZ)".ToEntity())` was `"x"` and is `"domain(x, ZZ)"`.
186+
187+
**A sum stops collecting two terms that are not the same term.** `Simplify`'s polynomial
188+
collection keys a monomial by its base's *printed form*, so while the printed form did not
189+
distinguish `x` from `x` narrowed to the integers, it added them up as one:
190+
191+
```
192+
"x - domain(x, ZZ)".ToEntity().Simplify() 2.3.0: 0 now: x - domain(x, ZZ)
193+
"domain(x, ZZ) + x".ToEntity().Simplify() 2.3.0: 2 * x now: domain(x, ZZ) + x
194+
```
195+
196+
`0` is the answer only where `x` is an integer, and the annotation is what says it might not be, so
197+
this was a wrong answer rather than a tidier one. Confirmed as the cause by putting the collision
198+
back — keying on the base with its codomain erased brings `0` and `2 * x` straight back.
199+
200+
**The ordinary expression is untouched.** The wrapper is printed only where the codomain is *not*
201+
the one that parsing the bare text would give back, and nothing inside the library narrows a
202+
codomain — `WithCodomain` is called from the parser and from callers, and from nowhere else. So
203+
`"x + 1".ToEntity().Stringize()` is `x + 1` on both versions, and of the 8,084 tests in the suite
204+
the only two that moved are the one written to pin this defect and the recorded public surface.
205+
206+
That default is **not the same for every node**, which is why the rule is a comparison and not a
207+
check against `Complex`: a `Variable` and a `Matrix` default to `Any`, `Absf`, `Modf`, `Minf`,
208+
`Maxf` and `Interval` to `Real`, every boolean node to `Boolean`, each numeric literal to its own
209+
type's domain, `Phif` to `Integer`, the set nodes and `Providedf`, `Piecewise`, `Application` and
210+
`Lambda` to `Any`, and the rest to `Complex`. Each node now declares that default next to its
211+
`Codomain`, and a test asserts that every freshly built node of every node type carries it.
212+
213+
**LaTeX** renders it as a subscripted set, `{\left(x\right)}_{\mathbb{Z}}`. The parentheses are
214+
unconditional because a variable renders its own index as a subscript, so `x_{\mathbb{Z}}` would be
215+
indistinguishable from a variable spelled that way. This is new output for
216+
[CSharpMath.Evaluation](https://github.com/verybadcat/CSharpMath/blob/master/CSharpMath.Evaluation/Evaluation.cs),
217+
which reads our LaTeX back and has no notion of a codomain; it appears only for an expression that
218+
carries a narrowed one ([#822](https://github.com/asc-community/AngouriMath/issues/822)).
219+
220+
**Two annotations still do not survive, and both are the grammar's limit.** `Any` cannot be written
221+
at all — the second argument of `domain(...)` has to be one of the five special sets, and none of
222+
them means "no restriction" — so a node *widened* to `Any` from a narrower default still prints as
223+
though it had not been. And no input string yields a rational literal whose codomain is `Complex`,
224+
because the pass that reads `1/2` as a `Rational` rather than a quotient
225+
([#873](https://github.com/asc-community/AngouriMath/issues/873)) uses `Complex` as its "nobody
226+
annotated this" sentinel. Both are pinned by tests that fail if they start working, so neither can
227+
outlive itself.
228+
229+
**The public surface.** Each node type used to declare its own `public override string Stringize()`
230+
and `Latexize()`; the codomain wrapper is one decision and now lives once on `Entity`, with the
231+
per-node rendering behind an internal member. All 130 overrides are therefore gone from
232+
`PublicApi.txt`. Nothing a caller can write stops compiling — `someSumf.Stringize()` still resolves,
233+
inherited from `Entity` — but an assembly *compiled* against 2.3.0 binds to
234+
`Entity+Sumf::Stringize()` where its static type is a concrete node, and has to be rebuilt.
235+
159236
### `Compile` works in a trimmed or NativeAOT application
160237

161238
The Linq compilation path found the method for each node by name —

‎Sources/AngouriMath/Convenience/MathS.cs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5945,8 +5945,12 @@ public static string ToSympyCode(Entity expr)
59455945
{
59465946
var sb = new System.Text.StringBuilder();
59475947
sb.Append("import sympy\n\n");
5948+
// Name rather than Stringize: a variable narrowed with WithCodomain prints as
5949+
// `domain(x, ZZ)` since https://github.com/asc-community/AngouriMath/issues/1022, and
5950+
// that is not a Python identifier. The codomain itself does not survive the export --
5951+
// SymPy would spell it as an assumption on the symbol, which nothing here emits yet.
59485952
foreach (var f in expr.Vars)
5949-
sb.Append($"{f.Stringize()} = sympy.Symbol('{f.Stringize()}')\n");
5953+
sb.Append($"{f.Name} = sympy.Symbol('{f.Name}')\n");
59505954
sb.Append('\n');
59515955
sb.Append("expr = ").Append(expr.ToSymPy());
59525956
return sb.ToString();

0 commit comments

Comments
 (0)