diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index bea003534..0873cea76 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -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, diff --git a/Sources/AngouriMath/Core/Entity/Entity.Definition.cs b/Sources/AngouriMath/Core/Entity/Entity.Definition.cs index c28d8c584..21ad2c884 100644 --- a/Sources/AngouriMath/Core/Entity/Entity.Definition.cs +++ b/Sources/AngouriMath/Core/Entity/Entity.Definition.cs @@ -509,7 +509,23 @@ public Entity Substitute(IReadOnlyDictionary replacement /// sin(x) + cos(x + 2): 7 /// /// - public int Complexity => complexity.GetValue(static @this => 1 + @this.DirectChildren.Sum(x => x.Complexity), this); + /// + /// Saturated at : 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 . + /// https://github.com/asc-community/AngouriMath/issues/1600 + /// + 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 complexity; /// diff --git a/Sources/AngouriMath/Functions/Algebra/Polynomials/PolynomialGcd.cs b/Sources/AngouriMath/Functions/Algebra/Polynomials/PolynomialGcd.cs index fd3482a14..ee69ac404 100644 --- a/Sources/AngouriMath/Functions/Algebra/Polynomials/PolynomialGcd.cs +++ b/Sources/AngouriMath/Functions/Algebra/Polynomials/PolynomialGcd.cs @@ -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 diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs index 3b7b002b0..8442bef49 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs @@ -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[] { diff --git a/Sources/Tests/UnitTests/Core/ComplexityTest.cs b/Sources/Tests/UnitTests/Core/ComplexityTest.cs new file mode 100644 index 000000000..dafeb9cbe --- /dev/null +++ b/Sources/Tests/UnitTests/Core/ComplexityTest.cs @@ -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 +{ + /// + /// counts a shared subtree once for every place it is used, + /// so an expression built by sharing is larger than as a tree long + /// before it is large in memory, and the count threw . + /// #1600 + /// + /// + /// Nothing here enumerates the nodes: as a tree they are exponential in the number of + /// squarings, which is the point. + /// + [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; + } + + /// + /// Thirty-one squarings make 2^32 - 1 nodes, and the count of each operand, + /// 2^31 - 1, is already all an int holds. + /// + [Theory] + [InlineData(31)] + [InlineData(32)] + [InlineData(60)] + public void AnExpressionLargerThanAnIntAsATreeSaturates(int times) + => Assert.Equal(int.MaxValue, Squared(times).Complexity); + + /// Below the bound the count is the count: 2^(n+1) - 1 nodes for n squarings. + [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); + } +}