Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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++)
{
Expand Down
9 changes: 9 additions & 0 deletions Sources/AngouriMath/Functions/TreeAnalyzer/Expansion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

using System;
using AngouriMath.Core.Exceptions;
using AngouriMath.Core.Multithreading;
using PeterO.Numbers;

namespace AngouriMath.Functions
Expand Down Expand Up @@ -88,8 +89,16 @@ static IEnumerable<List<EInteger>> Combinations(EInteger min, EInteger max, EInt
}
}
/// <summary><paramref name="expr"/> is NEITHER <see cref="Sumf"/> NOR <see cref="Minusf"/></summary>
/// <remarks>
/// 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
/// <see cref="MathS.Settings.MaxExpansionTermCount"/> 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
/// </remarks>
internal static List<Entity>? SmartExpandOver(Entity expr, Func<Entity, bool> conditionForUniqueTerms)
{
MultithreadingFunctional.ExitIfCancelled();
var newChildren = new List<Entity>();
switch (expr)
{
Expand Down
53 changes: 53 additions & 0 deletions Sources/Tests/UnitTests/Core/Multithreading/ExpansionCancelTest.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// An expansion stops when the computation it is part of is cancelled, as the other steps that
/// can run long do.
/// <a href="https://github.com/asc-community/AngouriMath/issues/718">#718</a>
/// </summary>
[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<AggregateException>(() => task.Wait());
Assert.IsAssignableFrom<OperationCanceledException>(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<Entity.Sumf>(expanded);
Assert.Equal((Entity)64, expanded.Substitute("a", 3).Substitute("b", 5).Evaled);
}
}
}
Loading