@@ -34500,6 +34500,161 @@ Entity Named(Entity value)
3450034500 }
3450134501 }
3450234502
34503+ /// <summary>
34504+ /// A power of a sum beside a constant multiple of the sum's derivative, with symbolic powers
34505+ /// about: <c>c g' g^k</c> is <c>c g^(k + 1)/(k + 1)</c>, and <c>c ln(g)</c> for <c>k = -1</c>.
34506+ /// Each term is read as a coefficient times a power of <c>x</c> times powers of the other
34507+ /// functions of <c>x</c> in it, the powers of each merged, so that
34508+ /// <c>d/dx ln(c x^n)^q = q n ln(c x^n)^(q - 1)/x</c> is matched against
34509+ /// <c>b n q ln(c x^n)^(q - 1)/x</c> as written. Where the terms do not match, a power of
34510+ /// <c>x</c> common to every term of the sum is taken out of it into the rest:
34511+ /// <c>a x^m + b x ln(c x^n)^q</c> is <c>x (a x^(m - 1) + b ln(c x^n)^q)</c>. Rubi's 3.5.
34512+ /// https://github.com/asc-community/AngouriMath/issues/233
34513+ /// </summary>
34514+ /// <remarks>
34515+ /// <see cref="SolveALogarithmicDerivativeWithASymbolicExponent"/> reads only powers of
34516+ /// <c>x</c>, and the substitution's simplification of the quotient by <c>du/dx</c> never
34517+ /// brings <c>c n x^(n - 1)/(c x^n)</c> down to <c>n/x</c>. Only where a power is symbolic:
34518+ /// otherwise the substitution answers it.
34519+ /// </remarks>
34520+ internal static Entity? SolveAPowerOfASumBesideItsDerivativeWithSymbolicPowers(Entity expr, Entity.Variable x)
34521+ {
34522+ if (!expr.Nodes.Any(node => node is Powf(var @base, var exponent) && @base.ContainsNode(x) && !exponent.ContainsNode(x) && exponent.Vars.Any()))
34523+ return null;
34524+ var factors = FactorsOfTheIntegrand(expr);
34525+ for (var chosen = 0; chosen < factors.Count; chosen++)
34526+ {
34527+ var (factor, underneath) = factors[chosen];
34528+ var (sum, power) = factor is Powf(var b, var e) && !e.ContainsNode(x) ? (b, e) : (factor, (Entity)Number.Integer.One);
34529+ if (sum is not (Sumf or Minusf) || !sum.ContainsNode(x) || power.Evaled is not Number.Rational k)
34530+ continue;
34531+ if (underneath)
34532+ k = (Number.Rational)(-k);
34533+ // The rest: at most one sum above the bar, every other factor one term.
34534+ Entity? restSum = null;
34535+ Entity monomial = Number.Integer.One;
34536+ var readable = true;
34537+ for (var other = 0; other < factors.Count && readable; other++)
34538+ {
34539+ if (other == chosen)
34540+ continue;
34541+ var (f, below) = factors[other];
34542+ if (!below && restSum is null && f is Sumf or Minusf && f.ContainsNode(x))
34543+ restSum = f;
34544+ else if (f is Sumf or Minusf && f.ContainsNode(x))
34545+ readable = false;
34546+ else
34547+ monomial = below ? monomial / f : monomial * f;
34548+ }
34549+ if (!readable)
34550+ continue;
34551+ var rest = (restSum is null ? new[] { monomial } : Sumf.LinearChildren(restSum).Select(term => term * monomial)).ToList();
34552+ if (Matched(rest, sum, x) is { } constant)
34553+ return Answer(constant, sum, k);
34554+ // A power of x common to the sum's terms, taken out: x^j g, so that c x^(j k) g'... is read against g.
34555+ if (Terms(Sumf.LinearChildren(sum), x) is not { } terms)
34556+ continue;
34557+ foreach (var common in terms.Select(t => t.Exponent).Where(e => e.Evaled is Number.Integer { IsZero: false }).Distinct().ToList())
34558+ {
34559+ Entity reduced = Number.Integer.Zero;
34560+ foreach (var t in terms)
34561+ {
34562+ var exponent = (t.Exponent - common).InnerSimplified;
34563+ Entity written = t.Atoms.Aggregate(t.Coefficient, (product, atom) => product * MathS.Pow(atom.Base, atom.Power));
34564+ if (exponent.Evaled is not Number.Complex { IsZero: true })
34565+ written *= exponent == Number.Integer.One ? x : MathS.Pow(x, exponent);
34566+ reduced = reduced == Number.Integer.Zero ? written : reduced + written;
34567+ }
34568+ // x^(common k) moves into the rest.
34569+ var moved = MathS.Pow(x, (common * k).InnerSimplified);
34570+ if (Matched(rest.Select(term => term * moved).ToList(), reduced, x) is { } c)
34571+ return Answer(c, reduced, k);
34572+ }
34573+ }
34574+ return null;
34575+
34576+ static Entity Answer(Entity constant, Entity sum, Number.Rational k)
34577+ => k == Number.Integer.MinusOne
34578+ ? constant * MathS.Ln(sum)
34579+ : constant / (k + 1) * MathS.Pow(sum, (Entity)(k + 1));
34580+
34581+ // The constant c with the rest's terms c times the sum's derivative's, or null.
34582+ static Entity? Matched(List<Entity> rest, Entity sum, Entity.Variable x)
34583+ {
34584+ var derivative = sum.Differentiate(x);
34585+ if (derivative is Providedf(var bare, _))
34586+ derivative = bare;
34587+ if (Terms(rest, x) is not { } above || Terms(Sumf.LinearChildren(derivative), x) is not { } below || above.Count != below.Count)
34588+ return null;
34589+ Entity? constant = null;
34590+ foreach (var term in above)
34591+ {
34592+ var matching = below.Where(other => SameMonomial(term, other)).ToList();
34593+ if (matching.Count != 1)
34594+ return null;
34595+ var ratio = Bare((term.Coefficient / matching[0].Coefficient).Simplify());
34596+ if (ratio.ContainsNode(x) || ratio.Nodes.Any(node => node == MathS.NaN))
34597+ return null;
34598+ if (constant is null)
34599+ constant = ratio;
34600+ else if (Bare((constant - ratio).Simplify()) != Number.Integer.Zero)
34601+ return null;
34602+ }
34603+ return constant;
34604+ }
34605+
34606+ static bool SameMonomial((Entity Coefficient, Entity Exponent, List<(Entity Base, Entity Power)> Atoms) one,
34607+ (Entity Coefficient, Entity Exponent, List<(Entity Base, Entity Power)> Atoms) other)
34608+ => Bare((one.Exponent - other.Exponent).Simplify()) == Number.Integer.Zero
34609+ && one.Atoms.Count == other.Atoms.Count
34610+ && one.Atoms.All(atom => other.Atoms.Any(theirs => theirs.Base == atom.Base && Bare((theirs.Power - atom.Power).Simplify()) == Number.Integer.Zero));
34611+
34612+ static Entity Bare(Entity simplified) => simplified is Providedf(var inner, _) ? inner : simplified;
34613+
34614+ // Each term as a coefficient free of x, a power of x, and powers of other functions of
34615+ // x, each base's powers merged; null where a factor is a sum holding x.
34616+ static List<(Entity Coefficient, Entity Exponent, List<(Entity Base, Entity Power)> Atoms)>? Terms(IEnumerable<Entity> terms, Entity.Variable x)
34617+ {
34618+ var read = new List<(Entity, Entity, List<(Entity, Entity)>)>();
34619+ foreach (var term in terms)
34620+ {
34621+ var (numerator, denominator) = Functions.SingleQuotient.Of(term is Providedf(var t, _) ? t : term);
34622+ Entity exponent = Number.Integer.Zero;
34623+ Entity coefficient = Number.Integer.One;
34624+ var atoms = new List<(Entity Base, Entity Power)>();
34625+ foreach (var (side, underneath) in new[] { (numerator, false), (denominator, true) })
34626+ foreach (var factor in Mulf.LinearChildren(side))
34627+ {
34628+ if (!factor.ContainsNode(x))
34629+ {
34630+ coefficient = underneath ? coefficient / factor : coefficient * factor;
34631+ continue;
34632+ }
34633+ var (@base, power) = factor is Powf(var b, var e) && !e.ContainsNode(x) ? (b, e) : (factor, (Entity)Number.Integer.One);
34634+ if (underneath)
34635+ power = -power;
34636+ if (@base == x)
34637+ exponent += power;
34638+ else if (@base is Sumf or Minusf)
34639+ return null;
34640+ else
34641+ {
34642+ var at = atoms.FindIndex(atom => atom.Base == @base);
34643+ if (at < 0)
34644+ atoms.Add((@base, power));
34645+ else
34646+ atoms[at] = (@base, atoms[at].Power + power);
34647+ }
34648+ }
34649+ // A term the derivative writes as 0 times something is no term.
34650+ if (coefficient.InnerSimplified.Evaled is Number.Complex { IsZero: true })
34651+ continue;
34652+ read.Add((coefficient, exponent.InnerSimplified, atoms.Select(atom => (atom.Base, atom.Power.InnerSimplified)).ToList()));
34653+ }
34654+ return read;
34655+ }
34656+ }
34657+
3450334658 /// <summary>
3450434659 /// An exponential of a polynomial beside the polynomial's derivative, with the rest a
3450534660 /// function of the polynomial: <c>G^P k P' f(P)</c> is <c>k G^u f(u)</c> under <c>u = P</c>.
0 commit comments