Skip to content

Commit 897ba4b

Browse files
image and preimage of a set under an expression (#1442)
Item 6 of the reference's docket (#1409), the half that needs no pair. image(f(x), x in A) is { f(x) : x in A }, which is union({f(x)}, x in A): listed over a listed A, an interval by interval arithmetic where x occurs once in f -- exact for one occurrence and the operations that have images (#1423); the reference's 9c/5 + 32 on (0, 100) is (32, 212) -- and otherwise the family, which answers membership through the quantifiers. preimage(f(x), x in A, Y) is { x in A : f(x) in Y }, and a set builder of that shape is solved on evaluation where the statement solver reads the membership (a listed Y, an interval; #1440), the solutions cut by A: the pre-images of x^2 (Ex 7.3.10), with the one of (0, 1) excluding 0 where the book prints (-1, 1). That shape only: a set builder is not solved on evaluation in general, that being a search on every evaluation. The performance baseline moves to this run. ParseHard reached +3.2% over the baseline recorded at 86af577, and it is the ten function tokens added to the grammar since -- about 12 KB/op each, whatever the input, measured by removing the two of this change and nothing else (3,727,130 to 3,703,015 B/op): a cost in the ANTLR runtime's per-parse work, filed as #1441. Every other row is within 1% of the old baseline. #1409 Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 84f83fd commit 897ba4b

15 files changed

Lines changed: 1143 additions & 903 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,21 @@ shown and the argument list of symbolic length wait for v3. Every one of these w
694694
| `1 * 2 * ... * n` | `UnhandledParseException` | `product(k, k, 1, n)`, which is `n!` for `n >= 1` |
695695
| `{1, 4, 9, ..., n}` | `UnhandledParseException` | `InvalidArgumentParseException`, naming the term off the progression |
696696

697+
### `image` and `preimage` of a set under an expression
698+
699+
`image(f(x), x in A)` is `{ f(x) : x in A }` — `union({f(x)}, x in A)` — listed over a listed
700+
`A`, an interval by interval arithmetic where `x` occurs once in `f`, and a membership object
701+
otherwise; `preimage(f(x), x in A, Y)` is `{ x in A : f(x) in Y }`, and a set builder of that
702+
shape is solved on evaluation where the membership is read (a listed `Y`, an interval)
703+
([#1409](https://github.com/asc-community/AngouriMath/issues/1409), the reference's §7.3). Both
704+
names are keywords now, and were names.
705+
706+
| Input | Was (2.5.0) | Now |
707+
|---|---|---|
708+
| `image(9 c / 5 + 32, c in (0; 100))` | `UnhandledParseException` | `(32; 212)` |
709+
| `preimage(x^2, x in RR, {1})` | `UnhandledParseException` | `{ 1, -1 }` |
710+
| `{ x in ZZ : x^2 in {1, 4} }` | `{ x in ZZ : x ^ 2 in { 1, 4 } }` — left as written | `{ 1, -1, 2, -2 }` |
711+
697712
### `binomial(n, k)` is a function
698713

699714
**Addition, not silent.** The binomial coefficient is a node, `Entity.Binomialf`, spelled

‎Sources/.editorconfig‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
291291
[AngouriMath/Functions/NumberTheory/ResidueClasses.cs]
292292
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
293293

294+
[Tests/UnitTests/Core/Sets/ImageAndPreImageTest.cs]
295+
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
296+
294297
[Tests/UnitTests/Algebra/MembershipSolvingTest.cs]
295298
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
296299

‎Sources/AngouriMath/Convenience/MathS.cs‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6292,6 +6292,43 @@ public static class Sets
62926292
/// </summary>
62936293
public static Set Complement(Entity set, Entity universe) => universe.SetSubtract(set);
62946294

6295+
/// <summary>
6296+
/// The image of <paramref name="over"/> under the expression <paramref name="body"/>
6297+
/// in <paramref name="var"/>: <c>image(f(x), x in A)</c>, the set of the values
6298+
/// <c>f(x)</c> takes on <c>A</c> -- <c>{ f(x) : x in A }</c>, which is
6299+
/// <c>union({f(x)}, x in A)</c>. Listed over a listed set, an interval by interval
6300+
/// arithmetic where the name occurs once, and a membership object otherwise.
6301+
/// </summary>
6302+
/// <example>
6303+
/// <code>
6304+
/// Console.WriteLine("image(x^2, x in {1, 2, 3})".ToEntity().Evaled);
6305+
/// Console.WriteLine("image(9 c / 5 + 32, c in (0; 100))".ToEntity().Evaled);
6306+
/// </code>
6307+
/// Prints
6308+
/// <code>
6309+
/// { 1, 4, 9 }
6310+
/// (32; 212)
6311+
/// </code>
6312+
/// </example>
6313+
public static Set Image(Entity body, Entity var, Entity over) => new IndexedUnionf(var, over, new FiniteSet(body));
6314+
6315+
/// <summary>
6316+
/// The pre-image of <paramref name="target"/> under the expression <paramref name="body"/>
6317+
/// in <paramref name="var"/> ranging over <paramref name="over"/>:
6318+
/// <c>preimage(f(x), x in A, Y)</c> is <c>{ x in A : f(x) in Y }</c>, solved on
6319+
/// evaluation where the membership is one the statement solver reads.
6320+
/// </summary>
6321+
/// <example>
6322+
/// <code>
6323+
/// Console.WriteLine("preimage(x^2, x in RR, {1})".ToEntity().Evaled);
6324+
/// </code>
6325+
/// Prints
6326+
/// <code>
6327+
/// { 1, -1 }
6328+
/// </code>
6329+
/// </example>
6330+
public static Set PreImage(Entity body, Entity var, Entity over, Entity target) => new ConditionalSet(var, var.In(over) & body.In(target));
6331+
62956332
/// <returns>A set of all Complexes/>s</returns>
62966333
public static Set C => SpecialSet.Create(Domain.Complex);
62976334

‎Sources/AngouriMath/Core/Antlr/AngouriMath.g‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,8 @@ atom returns[Entity value]
575575
| 'union(' args = function_arguments ')' { Assert("union", 2, $args.list.Count); $value = $args.list[1] is Entity.Set.Inf { Element: Variable } unionRange ? MathS.Sets.IndexedUnion($args.list[0], unionRange.Element, unionRange.SupSet) : throw new InvalidArgumentParseException("union expects its second argument to say which name ranges over which set, as in union(A_i, i in I)"); }
576576
| 'intersection(' args = function_arguments ')' { Assert("intersection", 2, $args.list.Count); $value = $args.list[1] is Entity.Set.Inf { Element: Variable } intersectionRange ? MathS.Sets.IndexedIntersection($args.list[0], intersectionRange.Element, intersectionRange.SupSet) : throw new InvalidArgumentParseException("intersection expects its second argument to say which name ranges over which set, as in intersection(A_i, i in I)"); }
577577
| 'complement(' args = function_arguments ')' { Assert("complement", 2, $args.list.Count); $value = MathS.Sets.Complement($args.list[0], $args.list[1]); }
578+
| 'image(' args = function_arguments ')' { Assert("image", 2, $args.list.Count); $value = $args.list[1] is Entity.Set.Inf { Element: Variable } imageRange ? MathS.Sets.Image($args.list[0], imageRange.Element, imageRange.SupSet) : throw new InvalidArgumentParseException("image expects its second argument to say which name ranges over which set, as in image(f(x), x in A)"); }
579+
| 'preimage(' args = function_arguments ')' { Assert("preimage", 3, $args.list.Count); $value = $args.list[1] is Entity.Set.Inf { Element: Variable } preimageRange ? MathS.Sets.PreImage($args.list[0], preimageRange.Element, preimageRange.SupSet, $args.list[2]) : throw new InvalidArgumentParseException("preimage expects its second argument to say which name ranges over which set, as in preimage(f(x), x in A, Y)"); }
578580
| 'floor(' args = function_arguments ')' { Assert("floor", 1, $args.list.Count); $value = MathS.Floor($args.list[0]); }
579581
| 'ceil(' args = function_arguments ')' { Assert("ceil", 1, $args.list.Count); $value = MathS.Ceil($args.list[0]); }
580582
/* SymPy's spelling, accepted so that an expression copied from there parses. Stringize

‎Sources/AngouriMath/Core/Antlr/AngouriMath.interp‎

Lines changed: 5 additions & 1 deletion
Large diffs are not rendered by default.

‎Sources/AngouriMath/Core/Antlr/AngouriMath.tokens‎

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,16 @@ T__165=166
167167
T__166=167
168168
T__167=168
169169
T__168=169
170-
NEWLINE=170
171-
NUMBER=171
172-
SPECIALSET=172
173-
BOOLEAN=173
174-
NAN=174
175-
VARIABLE=175
176-
COMMENT=176
177-
WS=177
170+
T__169=170
171+
T__170=171
172+
NEWLINE=172
173+
NUMBER=173
174+
SPECIALSET=174
175+
BOOLEAN=175
176+
NAN=176
177+
VARIABLE=177
178+
COMMENT=178
179+
WS=179
178180
'!'=1
179181
'^'=2
180182
'-'=3
@@ -326,22 +328,24 @@ WS=177
326328
'union('=149
327329
'intersection('=150
328330
'complement('=151
329-
'floor('=152
330-
'ceil('=153
331-
'ceiling('=154
332-
'round('=155
333-
'min('=156
334-
'max('=157
335-
'argmax('=158
336-
'argmin('=159
337-
'gcd('=160
338-
'lcm('=161
339-
'binomial('=162
340-
'trunc('=163
341-
'erf('=164
342-
'conjugate('=165
343-
'domain('=166
344-
'piecewise('=167
345-
'apply('=168
346-
'lambda('=169
347-
'NaN'=174
331+
'image('=152
332+
'preimage('=153
333+
'floor('=154
334+
'ceil('=155
335+
'ceiling('=156
336+
'round('=157
337+
'min('=158
338+
'max('=159
339+
'argmax('=160
340+
'argmin('=161
341+
'gcd('=162
342+
'lcm('=163
343+
'binomial('=164
344+
'trunc('=165
345+
'erf('=166
346+
'conjugate('=167
347+
'domain('=168
348+
'piecewise('=169
349+
'apply('=170
350+
'lambda('=171
351+
'NaN'=176

0 commit comments

Comments
 (0)