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
20 changes: 13 additions & 7 deletions Sources/AngouriMath/Functions/SingleQuotient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

using PeterO.Numbers;
using AngouriMath.Core.Transformations;
using static AngouriMath.Entity;
using static AngouriMath.Entity.Number;

Expand Down Expand Up @@ -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
/// <c>((u - 1) (u + 1))^2</c> shares <c>u - 1</c>, and each factor inner-simplified, with a
/// negative number added written as one subtracted, so that <c>u + -1</c> is <c>u - 1</c>,
/// which inner simplification leaves as it is.
/// negative number added written as one subtracted anywhere in it, so that <c>u + -1</c> is
/// <c>u - 1</c>, which inner simplification leaves as it is, and its operands in canonical
/// order, so that <c>1 + u</c> and <c>u + 1</c> are one factor.
/// </summary>
private static Entity AsFactors(Entity denominator)
{
Expand All @@ -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<Entity>? carried)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <c>(e^(2x) + 1)^6 (e^(2x) - 1)^3</c>. 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: <c>-1</c> 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.
/// </summary>
/// <remarks>
/// Checked by value: the denominator is a constant multiple of the least common multiple, and
Expand All @@ -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());
Expand Down
Loading