diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md
index 2d8c7ac2b..fc8bb42ed 100644
--- a/BREAKING-CHANGES.md
+++ b/BREAKING-CHANGES.md
@@ -852,6 +852,25 @@ a listed index set, and over any other an object whose membership is decided by
| `0.1 in intersection([0; 1/n), n in ZZ+)` | `UnhandledParseException` | `False` |
| `complement({1, 2}, {1, 2, 3})` | `UnhandledParseException` | `{ 3 }` |
+### A set builder over a set of numbers is listed where that takes no search
+
+`{ x in S : p }` was left as written unless `p` came out a truth value, or was a membership
+`f(x) in Y`. Now it is listed, or written as intervals, where that takes no search: a listed `S` is
+filtered member by member, and over the reals, an interval or a set of whole numbers, `S` is met with
+the solutions of `p` where `p` compares rational functions of `x` of degree at most four, with numbers
+for coefficients, or is an equation in moduli of linear functions. A set of whole numbers is met only
+where that comes out listed, so `{ x in ZZ : x > 5 }` stays as written, as does anything with a
+parameter or a function the solvers would have to search. Sullivan and Mackey's §3.3.3-4, §3.3.7 Q6,
+Prop 3.9.6 and Prob 3.11.4 list as the book lists them
+([#1409](https://github.com/asc-community/AngouriMath/issues/1409)).
+
+| Input | Was (2.5.0) | Now |
+|---|---|---|
+| `"{ x in RR : x^2 - 2 = 0 }".Simplify()` | as written | `{ -sqrt(2), sqrt(2) }` |
+| `"{ x : x in ZZ and x^2 < 10 }".Simplify()` | as written | `{ -3, -2, -1, 0, 1, 2, 3 }` |
+| `"{ x in { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } : x >= 7 }".Simplify()` | as written | `{ 7, 8, 9, 10 }` |
+| `"{ x in ZZ+ : x + 8/x <= 6 }".Simplify()` | `UnhandledParseException` (`ZZ+` is new since) | `{ 2, 3, 4 }` |
+
### An interval whose ends are reversed meets nothing
`(3; 1)` holds no number, and membership in it was decided so, but its meeting with another interval
diff --git a/Sources/AngouriMath/Functions/Evaluation/Evaluation.Omni/Evaluation.Omni.Classes.cs b/Sources/AngouriMath/Functions/Evaluation/Evaluation.Omni/Evaluation.Omni.Classes.cs
index 9482b0d62..c4710019f 100644
--- a/Sources/AngouriMath/Functions/Evaluation/Evaluation.Omni/Evaluation.Omni.Classes.cs
+++ b/Sources/AngouriMath/Functions/Evaluation/Evaluation.Omni/Evaluation.Omni.Classes.cs
@@ -8,6 +8,7 @@
using AngouriMath.Core.Sets;
using System;
using System.Linq;
+using PeterO.Numbers;
using static AngouriMath.Entity.Set;
namespace AngouriMath
@@ -99,10 +100,200 @@ protected override Entity InnerSimplify(bool isExact)
Boolean(true) => Codomain is AngouriMath.Core.Domain.Any
? New(Var, Boolean.True)
: SpecialSet.Create(Codomain),
- _ => PreImage(predicate, isExact) ?? New(Var, predicate)
+ _ => PreImage(predicate, isExact) ?? Listed(predicate, isExact) ?? New(Var, predicate)
};
}
+ ///
+ /// { x in S : p } for a set of numbers S, listed or written as intervals
+ /// where that takes no search: over a listed S, the members at which p is
+ /// decided true; over the reals, an interval or a set of whole numbers, S met with
+ /// the solutions of p, where p compares rational functions of x of
+ /// low degree with numbers for coefficients, or is an equation in moduli of linear
+ /// functions, which the solvers answer exactly. A set of whole numbers is met only where
+ /// that comes out listed. Sullivan and Mackey's §3.3.3-4, Prop 3.9.6 and Prob 3.11.4:
+ /// { x in ZZ+ : x + 8/x <= 6 } is { 2, 3, 4 }, and
+ /// { x in RR : x^2 - 2 = 0 } is { -sqrt(2), sqrt(2) }.
+ /// https://github.com/asc-community/AngouriMath/issues/1409
+ ///
+ private Entity? Listed(Entity predicate, bool isExact)
+ {
+ if (Var is not Variable x || Declared(predicate, x) is not (var declared, var rest) || !rest.ContainsNode(x))
+ return null;
+ if (declared is FiniteSet listed)
+ return Increasing(Filtered(listed, x, rest, isExact));
+ if (!WithinTheReals(declared) || !ComparesRationalFunctions(rest, x))
+ return null;
+ var solved = rest is Equalsf(var left, var right) && (left - right).Nodes.Any(node => node is Absf(var argument) && argument.ContainsNode(x))
+ ? Functions.Algebra.AnalyticalSolving.ModulusSolver.SolveOverTheReals(left - right, x)
+ : Functions.Algebra.AnalyticalSolving.StatementSolver.Solved(rest, x);
+ return solved is null or ConditionalSet ? null : Increasing(Met(declared, solved, isExact));
+ }
+
+ ///
+ /// A union of intervals and listed real numbers written as its disjoint pieces in
+ /// increasing order, which is how it reads, with a point at an open end closing it:
+ /// { 1, 2 } \/ (-oo; 1) \/ (2; +oo) is (-oo; 1] \/ [2; +oo). So two set
+ /// builders for one set of numbers come out the same. As it was where an end is not a
+ /// real number.
+ ///
+ private static Entity? Increasing(Entity? set)
+ {
+ var pieces = new System.Collections.Generic.List<(Entity Left, bool LeftClosed, Entity Right, bool RightClosed)>();
+ bool Collect(Entity piece)
+ {
+ switch (piece)
+ {
+ case Unionf(var left, var right):
+ return Collect(left) && Collect(right);
+ case Interval(var left, var leftClosed, var right, var rightClosed) when left.Evaled is Number.Real && right.Evaled is Number.Real:
+ pieces.Add((left, leftClosed, right, rightClosed));
+ return true;
+ case FiniteSet listed when listed.All(static member => member.Evaled is Number.Real):
+ foreach (var member in listed)
+ pieces.Add((member, true, member, true));
+ return true;
+ default:
+ return false;
+ }
+ }
+ if (set is null || !Collect(set) || pieces.Count < 2)
+ return set;
+ static EDecimal At(Entity end) => ((Number.Real)end.Evaled).EDecimal;
+ // By the left end, a closed end before an open one at the same point.
+ pieces.Sort(static (a, b) => At(a.Left).CompareTo(At(b.Left)) is var byLeft and not 0 ? byLeft : b.LeftClosed.CompareTo(a.LeftClosed));
+ var merged = new System.Collections.Generic.List<(Entity Left, bool LeftClosed, Entity Right, bool RightClosed)> { pieces[0] };
+ foreach (var piece in pieces.Skip(1))
+ {
+ var last = merged[^1];
+ var gap = At(piece.Left).CompareTo(At(last.Right));
+ if (gap > 0 || gap == 0 && !last.RightClosed && !piece.LeftClosed)
+ {
+ merged.Add(piece);
+ continue;
+ }
+ var further = At(piece.Right).CompareTo(At(last.Right));
+ merged[^1] = further > 0 ? (last.Left, last.LeftClosed, piece.Right, piece.RightClosed)
+ : further == 0 ? (last.Left, last.LeftClosed, last.Right, last.RightClosed || piece.RightClosed)
+ : last;
+ }
+ // Points on their own are one listed set; each interval stands where it lies.
+ var points = merged.Where(static piece => At(piece.Left).CompareTo(At(piece.Right)) == 0).Select(static piece => piece.Left).ToList();
+ var intervals = merged.Where(static piece => At(piece.Left).CompareTo(At(piece.Right)) != 0)
+ .Select(static piece => (Entity)new Interval(piece.Left, piece.LeftClosed, piece.Right, piece.RightClosed)).ToList();
+ if (intervals.Count == 0)
+ return new FiniteSet(points);
+ var union = intervals.Aggregate(static (left, right) => new Unionf(left, right));
+ return points.Count == 0 ? union : new Unionf(new FiniteSet(points), union);
+ }
+
+ /// The membership a set builder's predicate declares, wherever it sits among the conjuncts, and the rest.
+ private static (Set Declared, Entity Condition)? Declared(Entity predicate, Variable x)
+ {
+ var conjuncts = Andf.LinearChildren(predicate).ToList();
+ var at = conjuncts.FindIndex(conjunct => conjunct is Inf(var name, Set) && name == x);
+ if (at < 0)
+ return null;
+ var declared = (Set)((Inf)conjuncts[at]).SupSet;
+ conjuncts.RemoveAt(at);
+ return (declared, conjuncts.Count == 0 ? Boolean.True : conjuncts.Aggregate(static (left, right) => left & right));
+ }
+
+ /// The members of a listed set at which is decided true; where one is not decided.
+ private static Entity? Filtered(FiniteSet listed, Variable x, Entity rest, bool isExact)
+ {
+ if (listed.Count > LargestFiltered)
+ return null;
+ var kept = new System.Collections.Generic.List();
+ foreach (var member in listed)
+ switch (rest.Substitute(x, member).InnerSimplified(isExact).Evaled)
+ {
+ case Boolean(true): kept.Add(member); break;
+ case Boolean(false): break;
+ default: return null;
+ }
+ return new FiniteSet(kept);
+ }
+
+ /// How many members of a listed set are asked about before the set builder is left as written.
+ private const int LargestFiltered = 4096;
+
+ private static bool WithinTheReals(Set set)
+ => set is Interval { IsNumeric: true } || set is SpecialSet special
+ && special.ToDomain() is AngouriMath.Core.Domain.Real or AngouriMath.Core.Domain.Rational or AngouriMath.Core.Domain.Integer
+ or AngouriMath.Core.Domain.NonNegativeInteger or AngouriMath.Core.Domain.PositiveInteger or AngouriMath.Core.Domain.Prime;
+
+ ///
+ /// Whether every comparison in the statement, under and, or and not,
+ /// compares rational functions of of degree at most four together,
+ /// with numbers for coefficients -- a modulus of a linear function counting as degree one.
+ ///
+ private static bool ComparesRationalFunctions(Entity statement, Variable x)
+ => statement switch
+ {
+ Andf(var left, var right) => ComparesRationalFunctions(left, x) && ComparesRationalFunctions(right, x),
+ Orf(var either, var other) => ComparesRationalFunctions(either, x) && ComparesRationalFunctions(other, x),
+ Notf(var negated) => ComparesRationalFunctions(negated, x),
+ Equalsf or Greaterf or GreaterOrEqualf or Lessf or LessOrEqualf
+ when statement is IBinaryNode { NodeFirstChild: var left, NodeSecondChild: var right }
+ => Degree(left, x) is { } leftDegree && Degree(right, x) is { } rightDegree && leftDegree + rightDegree <= 4,
+ _ => false,
+ };
+
+ ///
+ /// A bound on the degree of as a rational function of
+ /// once its denominators are cleared, or
+ /// where it is not one: a quotient counts both of its sides.
+ ///
+ private static int? Degree(Entity expr, Variable x)
+ => expr switch
+ {
+ _ when !expr.ContainsNode(x) => expr.Vars.Any() ? null : 0,
+ Variable => 1,
+ Sumf(var augend, var addend) => Larger(Degree(augend, x), Degree(addend, x)),
+ Minusf(var minuend, var subtrahend) => Larger(Degree(minuend, x), Degree(subtrahend, x)),
+ Mulf(var multiplier, var multiplicand) => Added(Degree(multiplier, x), Degree(multiplicand, x)),
+ Divf(var dividend, var divisor) => Added(Degree(dividend, x), Degree(divisor, x)),
+ Powf(var @base, Number.Integer { EInteger: var power }) when power.Abs().CompareTo(4) <= 0
+ => Degree(@base, x) is { } d ? d * power.Abs().ToInt32Checked() : null,
+ Absf(var argument) => Degree(argument, x) is 1 ? 1 : null,
+ _ => null,
+ };
+
+ private static int? Larger(int? one, int? other) => one is { } a && other is { } b ? System.Math.Max(a, b) : null;
+
+ private static int? Added(int? one, int? other) => one is { } a && other is { } b ? a + b : null;
+
+ ///
+ /// met with the solutions, piece by piece: a listed piece by
+ /// membership, an interval as the declared interval or the reals meet it, and a set of
+ /// whole numbers only where it comes out listed. where a piece
+ /// does not.
+ ///
+ private static Entity? Met(Set declared, Set solved, bool isExact)
+ {
+ switch (solved)
+ {
+ case FiniteSet roots:
+ return MathS.Intersection(declared, roots).InnerSimplified(isExact) as FiniteSet;
+ case Unionf(Set left, Set right):
+ return Met(declared, left, isExact) is { } metLeft && Met(declared, right, isExact) is { } metRight
+ ? MathS.Union(metLeft, metRight).InnerSimplified(isExact) : null;
+ case Interval interval:
+ return declared switch
+ {
+ SpecialSet.Reals => interval,
+ Interval within => MathS.Intersection(within, interval).InnerSimplified(isExact) is (Interval or FiniteSet) and var met ? met : null,
+ SpecialSet special => SetOperators.IntersectSpecialSetAndInterval(special, interval) as FiniteSet,
+ _ => null,
+ };
+ case SpecialSet.Reals:
+ return declared;
+ default:
+ return null;
+ }
+ }
+
///
/// { x in A : f(x) in Y }, a pre-image, is the solutions of the membership
/// cut by A where the statement solver reads it -- a listed Y or an
diff --git a/Sources/Tests/UnitTests/Core/Sets/SetBuilderListedTest.cs b/Sources/Tests/UnitTests/Core/Sets/SetBuilderListedTest.cs
new file mode 100644
index 000000000..a9a8f283c
--- /dev/null
+++ b/Sources/Tests/UnitTests/Core/Sets/SetBuilderListedTest.cs
@@ -0,0 +1,50 @@
+//
+// Copyright (c) 2019-2026 Angouri.
+// AngouriMath is licensed under MIT.
+// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.
+// Website: https://am.angouri.org.
+//
+
+using AngouriMath.Extensions;
+using Xunit;
+
+namespace AngouriMath.Tests.Core.Sets
+{
+ ///
+ /// A set builder over a set of numbers is listed, or written as intervals, where that takes no
+ /// search: a listed set is filtered, and otherwise the predicate compares rational functions of
+ /// low degree, or is an equation in moduli of linear functions, which the solvers answer
+ /// exactly. Sullivan and Mackey's §3.3.3-4, §3.3.7 Q6, Prop 3.9.6 and Prob 3.11.4.
+ /// https://github.com/asc-community/AngouriMath/issues/1409
+ ///
+ [Trait("Area", "Core")]
+ public sealed class SetBuilderListedTest
+ {
+ [Theory]
+ [InlineData("{ x in RR : x^2 - 2 = 0 }", "{ -sqrt(2), sqrt(2) }")]
+ [InlineData("{ x in ZZ+ : x^2 - 2 = 0 }", "{ }")]
+ [InlineData("{ x in ZZ+ : abs(x) = 5 }", "{ 5 }")]
+ [InlineData("{ x in RR : abs(x) = 5 }", "{ -5, 5 }")]
+ [InlineData("{ a in ZZ+ : a < 0 }", "{ }")]
+ [InlineData("{ r in RR : r^2 < 0 }", "{ }")]
+ [InlineData("{ x in { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } : x >= 7 }", "{ 7, 8, 9, 10 }")]
+ [InlineData("{ x in ZZ+ : x + 8/x <= 6 }", "{ 2, 3, 4 }")]
+ [InlineData("{ n in ZZ+ : n^2 < 39 }", "{ 1, 2, 3, 4, 5, 6 }")]
+ [InlineData("{ x in ZZ : x^2 < 10 }", "{ -3, -2, -1, 0, 1, 2, 3 }")]
+ [InlineData("{ n in PP : n < 20 }", "{ 2, 3, 5, 7, 11, 13, 17, 19 }")]
+ [InlineData("{ x in [0; 10] : x^2 > 4 }", "(2; 10]")]
+ [InlineData("{ x in RR : x^2 + 1 = 0 }", "{ }")]
+ [InlineData("{ x in ZZ : x > 0 and x < 4 or x = 10 }", "{ 1, 2, 3, 10 }")]
+ public void Listed(string written, string listed)
+ => Assert.Equal(listed.ToEntity().InnerSimplified, written.ToEntity().Simplify());
+
+ /// Where listing would take a search, or has no end, the set builder is left as written.
+ [Theory]
+ [InlineData("{ x in RR : sin(x) > 0 }")]
+ [InlineData("{ x in ZZ : x > 5 }")]
+ [InlineData("{ x in RR : x > a }")]
+ [InlineData("{ x in QQ : not x in ZZ }")]
+ public void LeftAsWritten(string written)
+ => Assert.IsType(written.ToEntity().Simplify());
+ }
+}