From 93dcd0ac502b3bc3784a998341f23d57364de2b6 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Sun, 4 Oct 2026 12:23:20 +0000 Subject: [PATCH] A budget stops an expansion and a linear solve over polynomials Expand's recursion and the fraction-free elimination behind the partial-fraction and ansatz solves check for cancellation, once a call and once a row: a search under a budget ran on inside an elimination for nine seconds past it, building rows nothing would read. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura --- .../Algebra/Polynomials/PartialFractions.cs | 3 ++ .../Functions/TreeAnalyzer/Expansion.cs | 9 ++++ .../Multithreading/ExpansionCancelTest.cs | 53 +++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 Sources/Tests/UnitTests/Core/Multithreading/ExpansionCancelTest.cs diff --git a/Sources/AngouriMath/Functions/Algebra/Polynomials/PartialFractions.cs b/Sources/AngouriMath/Functions/Algebra/Polynomials/PartialFractions.cs index a0938ab7a..e413da56d 100644 --- a/Sources/AngouriMath/Functions/Algebra/Polynomials/PartialFractions.cs +++ b/Sources/AngouriMath/Functions/Algebra/Polynomials/PartialFractions.cs @@ -1801,6 +1801,9 @@ private static bool TrySolveOverPolynomials(Entity[][] matrix, Entity[] rhs, [No { if (row == rank) continue; + // A row is a product and an exact division per column, of polynomials that grow with + // the elimination: the step a budget has to be able to stop. + Core.Multithreading.MultithreadingFunctional.ExitIfCancelled(); var factor = augmented[row][column]; for (var k = 0; k <= width; k++) { diff --git a/Sources/AngouriMath/Functions/TreeAnalyzer/Expansion.cs b/Sources/AngouriMath/Functions/TreeAnalyzer/Expansion.cs index b1dcad3e2..a343ae3e8 100644 --- a/Sources/AngouriMath/Functions/TreeAnalyzer/Expansion.cs +++ b/Sources/AngouriMath/Functions/TreeAnalyzer/Expansion.cs @@ -7,6 +7,7 @@ using System; using AngouriMath.Core.Exceptions; +using AngouriMath.Core.Multithreading; using PeterO.Numbers; namespace AngouriMath.Functions @@ -88,8 +89,16 @@ static IEnumerable> Combinations(EInteger min, EInteger max, EInt } } /// is NEITHER NOR + /// + /// Checks for cancellation on every call, as the other steps that can run long do: an + /// expansion of a product of sums is bounded by + /// only once its terms are counted, and + /// the products before that can take seconds a computation under a budget does not have. + /// https://github.com/asc-community/AngouriMath/issues/718 + /// internal static List? SmartExpandOver(Entity expr, Func conditionForUniqueTerms) { + MultithreadingFunctional.ExitIfCancelled(); var newChildren = new List(); switch (expr) { diff --git a/Sources/Tests/UnitTests/Core/Multithreading/ExpansionCancelTest.cs b/Sources/Tests/UnitTests/Core/Multithreading/ExpansionCancelTest.cs new file mode 100644 index 000000000..b0e58eec5 --- /dev/null +++ b/Sources/Tests/UnitTests/Core/Multithreading/ExpansionCancelTest.cs @@ -0,0 +1,53 @@ +// +// 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 System; +using System.Threading; +using System.Threading.Tasks; +using AngouriMath.Extensions; +using Xunit; + +namespace AngouriMath.Tests.Core.Multithreading +{ + /// + /// An expansion stops when the computation it is part of is cancelled, as the other steps that + /// can run long do. + /// #718 + /// + [Trait("Area", "Core")] + public sealed class ExpansionCancelTest + { + [Fact] + public void ACancelledExpansionStops() + { + using var source = new CancellationTokenSource(); + source.Cancel(); + // On a thread of its own, since the token is local to the thread that sets it. + var task = Task.Factory.StartNew(() => + { + MathS.Multithreading.SetLocalCancellationToken(source.Token); + return "(a + b + c)^5 * (d + e)^3".ToEntity().Expand(); + }, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default); + var thrown = Assert.Throws(() => task.Wait()); + Assert.IsAssignableFrom(thrown.InnerException); + } + + [Fact] + public void AnExpansionThatIsNotCancelledCompletes() + { + using var source = new CancellationTokenSource(); + var task = Task.Factory.StartNew(() => + { + MathS.Multithreading.SetLocalCancellationToken(source.Token); + return "(a + b)^2".ToEntity().Expand(); + }, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default); + var expanded = task.Result; + Assert.IsType(expanded); + Assert.Equal((Entity)64, expanded.Substitute("a", 3).Substitute("b", 5).Evaled); + } + } +}