@@ -12831,6 +12831,83 @@ static bool IsRationalInBoth(Entity e, Entity.Variable x, Entity.Variable L)
1283112831 /// <summary>The largest power of the logarithm the tower ansatz reads or tries.</summary>
1283212832 private const int MaximumTowerDegree = 4;
1283312833
12834+ /// <summary>
12835+ /// <paramref name="sum"/>, every term of which is a constant times powers of the same
12836+ /// bases with <paramref name="x"/> in them, as the product of those bases to the least of
12837+ /// each one's exponents times the sum of what is left. Null where a base is not in every
12838+ /// term, where no exponent of one is the least, or where the least are all whole, which
12839+ /// the rational rules read.
12840+ /// </summary>
12841+ private static Entity? WithTheCommonPowersOut(Entity sum, Entity.Variable x)
12842+ {
12843+ var terms = new List<(Entity Constant, Dictionary<Entity, Entity> Powers)>();
12844+ foreach (var term in Sumf.LinearChildren(sum))
12845+ {
12846+ Entity constant = Number.Integer.One;
12847+ var powers = new Dictionary<Entity, Entity>();
12848+ foreach (var (factor, underneath) in FactorsOfTheIntegrand(term))
12849+ {
12850+ if (!factor.ContainsNode(x))
12851+ {
12852+ constant = underneath ? constant / factor : constant * factor;
12853+ continue;
12854+ }
12855+ var (@base, exponent) = factor is Powf(var b, var e) && !e.ContainsNode(x) ? (b, e) : (factor, (Entity)Number.Integer.One);
12856+ if (@base is Number || !@base.ContainsNode(x))
12857+ return null;
12858+ if (underneath)
12859+ exponent = -exponent;
12860+ powers[@base] = powers.TryGetValue(@base, out var before) ? (before + exponent).InnerSimplified : exponent.InnerSimplified;
12861+ }
12862+ terms.Add((constant, powers));
12863+ }
12864+ if (terms.Count < 2)
12865+ return null;
12866+ var bases = terms[0].Powers.Keys.ToList();
12867+ if (bases.Count == 0 || terms.Any(term => term.Powers.Count != bases.Count || bases.Any(@base => !term.Powers.ContainsKey(@base))))
12868+ return null;
12869+ // For each base the least of its exponents, against which every other is more by a
12870+ // whole number or by a sum of symbols with no sign in front: `x^(m - 1)` beside
12871+ // `x^(m + n - 1)` leaves `x^n` in the bracket, as the derivative of `a + b x^n` does.
12872+ // Simplified, not inner-simplified: `-1 + m + n - (-1 + m)` is left as written by the
12873+ // latter, and is `n`.
12874+ static Entity Difference(Entity exponent, Entity from)
12875+ => Functions.PartialFractions.Bare((exponent - from).Simplify());
12876+ static bool More(Entity difference)
12877+ => difference is Number.Integer { EInteger.Sign: >= 0 }
12878+ || difference is not Number && difference is not Mulf(Number.Real { IsNegative: true }, _) && difference.Vars.Any();
12879+ var least = new Dictionary<Entity, Entity>();
12880+ foreach (var @base in bases)
12881+ {
12882+ var exponents = terms.Select(term => term.Powers[@base]).ToList();
12883+ Entity? lowest = null;
12884+ foreach (var candidate in exponents)
12885+ if (exponents.All(other => Difference(other, candidate) is var difference && (difference == Number.Integer.Zero || More(difference))))
12886+ {
12887+ lowest = candidate;
12888+ break;
12889+ }
12890+ if (lowest is null)
12891+ return null;
12892+ least[@base] = lowest;
12893+ }
12894+ if (least.Values.All(exponent => exponent is Number.Integer))
12895+ return null;
12896+ Entity bracket = Number.Integer.Zero;
12897+ foreach (var (constant, powers) in terms)
12898+ {
12899+ Entity rest = constant;
12900+ foreach (var @base in bases)
12901+ if (Difference(powers[@base], least[@base]) is var raised && raised != Number.Integer.Zero)
12902+ rest = rest * (raised == Number.Integer.One ? @base : MathS.Pow(@base, raised));
12903+ bracket = bracket == Number.Integer.Zero ? rest : bracket + rest;
12904+ }
12905+ Entity common = Number.Integer.One;
12906+ foreach (var @base in bases)
12907+ common = common * MathS.Pow(@base, least[@base]);
12908+ return common * bracket;
12909+ }
12910+
1283412911 /// <summary>
1283512912 /// A product of powers times a sum that is the derivative of the product with some of
1283612913 /// the powers raised by one: <c>e^x x^2 ln(x)^2 (3 + (3 + x) ln(x))</c> is
@@ -12863,6 +12940,13 @@ static bool IsRationalInBoth(Entity e, Entity.Variable x, Entity.Variable L)
1286312940 {
1286412941 if (!Integration.AnsweringTheQuestionAskedOrOneBelow)
1286512942 return null;
12943+ // A sum of products of powers of the same bases is their common product times a
12944+ // bracket, which is the shape read below: `m x^(m - 1) (a + b x^n)^p + b n p x^(m + n - 1)
12945+ // (a + b x^n)^(p - 1)` is `x^(m - 1) (a + b x^n)^(p - 1) (m (a + b x^n) + b n p x^n)`,
12946+ // the derivative of `x^m (a + b x^n)^p`. As a sum it was split, and neither term is
12947+ // elementary.
12948+ if (expr is Sumf or Minusf)
12949+ return WithTheCommonPowersOut(expr, x) is { } factored ? SolveAsTheDerivativeOfAProductOfPowers(factored, x) : null;
1286612950 Entity constant = Number.Integer.One;
1286712951 Entity? bracket = null;
1286812952 var raisable = new List<(Entity Base, Entity Exponent)>();
0 commit comments