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
13 changes: 13 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2493,6 +2493,19 @@ where it was `0.1 * x ^ 2 / 2 + C`
| `limit(sin(c*x)/x, x, 0)` | `NaN` | `c` |
| `limit((1 - cos(c*x))/x^2, x, 0)` | `NaN` | `c ^ 2 / 2` |

### `Complexity` counts to `int.MaxValue` and stops there

`Entity.Complexity` threw `OverflowException` for an expression larger than `int.MaxValue` as a
tree, which an expression built by sharing reaches while it is small in memory: thirty-one squarings
of `x` are 32 nodes, and `2^32 - 1` as a tree. It now saturates at `int.MaxValue`, and the bounds
that ask it whether a value is too large to go on with decline such a value, where they threw out of
the operation that asked ([#1600](https://github.com/asc-community/AngouriMath/issues/1600)).

| Input | Was (2.5.0) | Now |
|---|---|---|
| `Complexity` of `x` squared thirty-one or sixty times | `OverflowException` | `int.MaxValue` |
| `Complexity` of `x` squared thirty times | `2147483647` | `2147483647`, the number of nodes, as below the bound |

### With the downcasting off, a comparison of real values is decided

With `DowncastingEnabled` off, `cos(3)` evaluates to a complex number whose imaginary part is zero,
Expand Down
18 changes: 17 additions & 1 deletion Sources/AngouriMath/Core/Entity/Entity.Definition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,23 @@ public Entity Substitute<TFrom, TTo>(IReadOnlyDictionary<TFrom, TTo> replacement
/// sin(x) + cos(x + 2): 7
/// </code>
/// </example>
public int Complexity => complexity.GetValue(static @this => 1 + @this.DirectChildren.Sum(x => x.Complexity), this);
/// <remarks>
/// Saturated at <see cref="int.MaxValue"/>: a subtree counts once for every place it is
/// used, so an expression built by sharing can be larger than that as a tree while being
/// small in memory, and a caller asking whether it is too large to go on with is owed an
/// answer rather than an <see cref="System.OverflowException"/>.
/// https://github.com/asc-community/AngouriMath/issues/1600
/// </remarks>
public int Complexity => complexity.GetValue(static @this =>
{
// By index: a foreach over the interface allocates an enumerator for every node counted.
var children = @this.DirectChildren;
long total = 1;
for (var i = 0; i < children.Count; i++)
if ((total += children[i].Complexity) >= int.MaxValue)
return int.MaxValue;
return (int)total;
}, this);
private LazyPropertyA<int> complexity;

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ internal static bool TryCancel(Entity numerator, Entity denominator,
// caller that has built one quotient it needs in lowest terms -- the
// coefficients of a partial fraction, whose whole point is to be small -- may
// raise it.
if (numerator.Complexity + denominator.Complexity > maxComplexity)
if ((long)numerator.Complexity + denominator.Complexity > maxComplexity)
return false;

var variables = numerator.Vars
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11458,7 +11458,7 @@ static Entity WithoutConditions(Entity coefficient)
return null;
var pValue = WithoutConditions((2 * g * qOfFirst - 2 * h * qOfConstant) / determinant);
var qValue = WithoutConditions((2 * h * pOfConstant - 2 * g * pOfFirst) / determinant);
if (pValue.Complexity + qValue.Complexity > LargestReducedCoefficients)
if ((long)pValue.Complexity + qValue.Complexity > LargestReducedCoefficients)
return null;
var values = new[]
{
Expand Down
55 changes: 55 additions & 0 deletions Sources/Tests/UnitTests/Core/ComplexityTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//
// 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
{
/// <summary>
/// <see cref="Entity.Complexity"/> counts a shared subtree once for every place it is used,
/// so an expression built by sharing is larger than <see cref="int.MaxValue"/> as a tree long
/// before it is large in memory, and the count threw <see cref="System.OverflowException"/>.
/// <a href="https://github.com/asc-community/AngouriMath/issues/1600">#1600</a>
/// </summary>
/// <remarks>
/// Nothing here enumerates the nodes: as a tree they are exponential in the number of
/// squarings, which is the point.
/// </remarks>
[Trait("Area", "Core")]
public sealed class ComplexityTest
{
private static Entity Squared(int times)
{
Entity squared = "x";
for (var i = 0; i < times; i++)
squared = squared * squared;
return squared;
}

/// <summary>
/// Thirty-one squarings make <c>2^32 - 1</c> nodes, and the count of each operand,
/// <c>2^31 - 1</c>, is already all an <c>int</c> holds.
/// </summary>
[Theory]
[InlineData(31)]
[InlineData(32)]
[InlineData(60)]
public void AnExpressionLargerThanAnIntAsATreeSaturates(int times)
=> Assert.Equal(int.MaxValue, Squared(times).Complexity);

/// <summary>Below the bound the count is the count: <c>2^(n+1) - 1</c> nodes for <c>n</c> squarings.</summary>
[Theory]
[InlineData(0, 1)]
[InlineData(1, 3)]
[InlineData(10, 2047)]
[InlineData(29, 1073741823)]
[InlineData(30, 2147483647)]
public void BelowTheBoundItIsTheNumberOfNodes(int times, int nodes)
=> Assert.Equal(nodes, Squared(times).Complexity);
}
}
Loading