diff --git a/Sources/AngouriMath/Functions/SingleQuotient.cs b/Sources/AngouriMath/Functions/SingleQuotient.cs
index dda6f09cf..c3ffca8a7 100644
--- a/Sources/AngouriMath/Functions/SingleQuotient.cs
+++ b/Sources/AngouriMath/Functions/SingleQuotient.cs
@@ -6,6 +6,7 @@
//
using PeterO.Numbers;
+using AngouriMath.Core.Transformations;
using static AngouriMath.Entity;
using static AngouriMath.Entity.Number;
@@ -160,8 +161,9 @@ internal static (Entity Common, Entity ForA, Entity ForB)? LeastCommonMultipleOf
/// A denominator written so that a factor shared with another is written alike: a whole
/// power of a product as the product of its factors' powers, so that
/// ((u - 1) (u + 1))^2 shares u - 1, and each factor inner-simplified, with a
- /// negative number added written as one subtracted, so that u + -1 is u - 1,
- /// which inner simplification leaves as it is.
+ /// negative number added written as one subtracted anywhere in it, so that u + -1 is
+ /// u - 1, which inner simplification leaves as it is, and its operands in canonical
+ /// order, so that 1 + u and u + 1 are one factor.
///
private static Entity AsFactors(Entity denominator)
{
@@ -172,12 +174,16 @@ private static Entity AsFactors(Entity denominator)
: factor is Powf(var @base, Integer { EInteger.Sign: > 0 } raisedTo) ? MathS.Pow(Written(@base), raisedTo) : Written(factor);
return product;
+ // Anywhere in the factor, not only at its top: a shared factor can hold the sum inside
+ // a quotient of its own, and e^(2x) - 1 written there as e^(2x) + -1 kept two copies of
+ // one denominator apart, so they were multiplied together. Differentiating
+ // ln(tanh(x) + x^2) twice made that sum, and simplifying it took seventeen seconds.
+ // And in one order: (1 + e^(2x))^6 and (e^(2x) + 1)^2 share their base, and with the
+ // order as written they were multiplied together too, each step raising the power.
static Entity Written(Entity factor)
- => factor.InnerSimplified switch
- {
- Sumf(var augend, Real { IsNegative: true } negative) => (augend - (-negative)).InnerSimplified,
- var simplified => simplified,
- };
+ => factor.InnerSimplified.Replace(static node => node is Sumf(var augend, Real { IsNegative: true } negative)
+ ? augend - (-negative) : node).InnerSimplified
+ .Rewrite(RewriteRules.CanonicalOrderExact);
}
private static void Carry(Entity denominator, List? carried)
diff --git a/Sources/Tests/UnitTests/Common/ASumOfFractionsTakesTheLeastCommonDenominatorTest.cs b/Sources/Tests/UnitTests/Common/ASumOfFractionsTakesTheLeastCommonDenominatorTest.cs
index 7a2ea5fdf..6907efa17 100644
--- a/Sources/Tests/UnitTests/Common/ASumOfFractionsTakesTheLeastCommonDenominatorTest.cs
+++ b/Sources/Tests/UnitTests/Common/ASumOfFractionsTakesTheLeastCommonDenominatorTest.cs
@@ -18,7 +18,9 @@ namespace AngouriMath.Tests.Common
/// exponentials took two seconds to simplify, nearly all of it in two sums whose denominators
/// had grown to (e^(2x) + 1)^6 (e^(2x) - 1)^3. Where the denominators share a factor,
/// the sum now goes over their least common multiple. Found while tracing a slow limit; no
- /// issue was filed.
+ /// issue was filed. A factor is shared however its sums are spelled: -1 added deep
+ /// inside it, or its terms in another order, kept the two copies of a factor apart, and
+ /// ln(tanh(x) + x^2) / ln(x) at +oo took eighteen seconds.
///
///
/// Checked by value: the denominator is a constant multiple of the least common multiple, and
@@ -36,6 +38,10 @@ public sealed class ASumOfFractionsTakesTheLeastCommonDenominatorTest
[InlineData("1", "((e^(2x) - 1) * (e^(2x) + 1))^2", "x", "(e^(2x) + 1)^2 * (e^(2x) - 1)", "(e^(2x) - 1)^2 * (e^(2x) + 1)^2")]
// The same factor written as a sum with -1 in one and a difference in the other.
[InlineData("x", "e^(2x) + -1", "1", "(e^(2x) - 1) * (e^(2x) + 1)^2", "(e^(2x) - 1) * (e^(2x) + 1)^2")]
+ // The same, inside a factor that is itself a sum with a quotient in it.
+ [InlineData("1", "(x + 1 / (e^(2x) + -1))^2", "x", "x + 1 / (e^(2x) - 1)", "(x + 1 / (e^(2x) - 1))^2")]
+ // The same factor with its terms in the other order.
+ [InlineData("x", "(1 + e^(2x))^6", "1", "(e^(2x) + 1)^2", "(e^(2x) + 1)^6")]
public void GoesOverTheLeastCommonMultiple(string leftNum, string leftDen, string rightNum, string rightDen, string multiple)
{
var (ln, ld, rn, rd) = (leftNum.ToEntity(), leftDen.ToEntity(), rightNum.ToEntity(), rightDen.ToEntity());