|
| 1 | +// |
| 2 | +// Copyright (c) 2019-2026 Angouri. |
| 3 | +// AngouriMath is licensed under MIT. |
| 4 | +// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md. |
| 5 | +// Website: https://am.angouri.org. |
| 6 | +// |
| 7 | + |
| 8 | +using System.Collections.Generic; |
| 9 | +using System.Linq; |
| 10 | +using PeterO.Numbers; |
| 11 | +using static AngouriMath.Entity; |
| 12 | +using static AngouriMath.Entity.Number; |
| 13 | +using static AngouriMath.Entity.Set; |
| 14 | + |
| 15 | +namespace AngouriMath.Functions.Algebra.AnalyticalSolving |
| 16 | +{ |
| 17 | + /// <summary> |
| 18 | + /// An equation linear in logarithms of polynomials to one base, |
| 19 | + /// <c>k_1 log_b(P_1) + ... + k_n log_b(P_n) = c</c> with rational <c>k_i</c> and at least two |
| 20 | + /// logarithms, solved by exponentiating. |
| 21 | + /// </summary> |
| 22 | + /// <remarks> |
| 23 | + /// Every root makes <c>b</c> to either side the same, so it is a root of |
| 24 | + /// <c>P_1^(k_1 L) ... P_n^(k_n L) = b^(c L)</c>, for <c>L</c> the common denominator of the |
| 25 | + /// <c>k_i</c>: a polynomial equation, with finitely many roots, which holds at every root of the |
| 26 | + /// original. The converse does not hold. Over the complex numbers <c>log(u) + log(v)</c> is |
| 27 | + /// <c>log(u v)</c> only up to a multiple of <c>2 pi i</c>, and a factor can be zero, so each |
| 28 | + /// candidate is put back into the equation as written, under the principal logarithm, and kept |
| 29 | + /// only where it holds. <c>ln(x) + ln(x + 1) = 0</c> is <c>x (x + 1) = 1</c>, whose roots are |
| 30 | + /// <c>(-1 +- sqrt(5)) / 2</c>; at the negative one both logarithms carry <c>i pi</c> and their sum |
| 31 | + /// is <c>2 pi i</c>, so the answer is <c>{ (sqrt(5) - 1) / 2 }</c>. Newton's method answered it |
| 32 | + /// in decimals, and <c>ln(1 + 3x^2) - ln(4x) = 1</c> with one of its two roots. |
| 33 | + /// </remarks> |
| 34 | + internal static class LogarithmSolver |
| 35 | + { |
| 36 | + /// <summary>The highest degree the exponentiated equation may have.</summary> |
| 37 | + private const int HighestDegree = 8; |
| 38 | + |
| 39 | + internal static Set? Solve(Entity expr, Variable x) |
| 40 | + { |
| 41 | + if (expr.Vars.Any(variable => variable != x)) |
| 42 | + return null; |
| 43 | + var logarithms = new List<(ERational Multiplier, Entity Argument)>(); |
| 44 | + Entity constant = Integer.Zero; |
| 45 | + Entity? logBase = null; |
| 46 | + if (!Gather(expr, ERational.One, x, logarithms, ref constant, ref logBase) || logarithms.Count < 2 || logBase is null) |
| 47 | + return null; |
| 48 | + |
| 49 | + var common = EInteger.One; |
| 50 | + foreach (var (multiplier, _) in logarithms) |
| 51 | + common = common.Multiply(multiplier.Denominator).Divide(common.Gcd(multiplier.Denominator)); |
| 52 | + Entity numerator = Integer.One, denominator = Integer.One; |
| 53 | + var degree = 0; |
| 54 | + foreach (var (multiplier, argument) in logarithms) |
| 55 | + { |
| 56 | + // In whole numbers: an ERational product is not reduced, and (1/2) 2 is 2/2. |
| 57 | + var power = multiplier.Numerator.Multiply(common.Divide(multiplier.Denominator)); |
| 58 | + if (!TreeAnalyzer.TryGetPolynomial(argument, x, out var monomials) |
| 59 | + || monomials.Keys.Any(exponent => exponent.Sign < 0) |
| 60 | + || monomials.Values.Any(coefficient => coefficient.ContainsNode(x))) |
| 61 | + return null; |
| 62 | + degree += power.Abs().ToInt32Checked() * monomials.Keys.Max()!.ToInt32Checked(); |
| 63 | + if (degree > HighestDegree) |
| 64 | + return null; |
| 65 | + if (power.Sign > 0) |
| 66 | + numerator *= MathS.Pow(argument, Integer.Create(power)); |
| 67 | + else |
| 68 | + denominator *= MathS.Pow(argument, Integer.Create(power.Negate())); |
| 69 | + } |
| 70 | + // The sum of the logarithms is -constant, so the product of the powers is b to -constant L. |
| 71 | + var target = MathS.Pow(logBase, -constant * Integer.Create(common)).InnerSimplified; |
| 72 | + var polynomial = numerator - target * denominator; |
| 73 | + if (AnalyticalEquationSolver.Solve(polynomial, x) is not FiniteSet candidates) |
| 74 | + return null; |
| 75 | + // Each candidate is to be a root of the polynomial: one that is not is the polynomial |
| 76 | + // solver failing, and the roots it did not find would be left out, the answer claiming |
| 77 | + // there are none. 3x^3 + 9x^2 + 9x + 1 = 0 came back as -1 three times, which is no |
| 78 | + // root, while a shifted cube's C was 0. And a polynomial of degree one or more has a |
| 79 | + // complex root, so no candidates at all is the same failure. |
| 80 | + if (candidates.Count == 0 && degree > 0 && MathS.Settings.Codomain.Value != Core.Domain.Real) |
| 81 | + return null; |
| 82 | + foreach (var candidate in candidates) |
| 83 | + if (candidate.Vars.Any() || polynomial.Substitute(x, candidate).Evaled is not Complex { IsFinite: true } residual || !IsZero(residual)) |
| 84 | + return null; |
| 85 | + |
| 86 | + // Over the reals a logarithm has a value only of a positive number, which the evaluation |
| 87 | + // below, over the complex numbers, does not ask. |
| 88 | + var overTheReals = MathS.Settings.Codomain.Value == Core.Domain.Real; |
| 89 | + var roots = new List<Entity>(); |
| 90 | + foreach (var candidate in candidates) |
| 91 | + { |
| 92 | + if (candidate.Vars.Any()) |
| 93 | + return null; |
| 94 | + if (overTheReals) |
| 95 | + { |
| 96 | + var positive = true; |
| 97 | + foreach (var (_, argument) in logarithms) |
| 98 | + switch (argument.Substitute(x, candidate).Evaled) |
| 99 | + { |
| 100 | + case Real { IsPositive: true }: |
| 101 | + break; |
| 102 | + case Complex value when value is Real || IsClearOfZero(Complex.Create(Integer.Zero, value.ImaginaryPart)): |
| 103 | + positive = false; |
| 104 | + break; |
| 105 | + default: |
| 106 | + return null; |
| 107 | + } |
| 108 | + if (!positive) |
| 109 | + continue; |
| 110 | + } |
| 111 | + switch (expr.Substitute(x, candidate).Evaled) |
| 112 | + { |
| 113 | + case Rational { IsZero: true }: |
| 114 | + case Complex { IsFinite: true } residual when residual is not Rational && IsZero(residual): |
| 115 | + roots.Add(candidate); |
| 116 | + break; |
| 117 | + // A candidate on another branch is off by a multiple of 2 pi i, and one where a |
| 118 | + // factor is zero has no value: either is plainly no root. A residual too small |
| 119 | + // to tell from rounding is not evidence, and dropping the candidate on it could |
| 120 | + // leave a root out, so the equation is not answered. |
| 121 | + case Complex { IsFinite: true } residual when !IsClearOfZero(residual): |
| 122 | + return null; |
| 123 | + case Complex: |
| 124 | + break; |
| 125 | + default: |
| 126 | + return null; |
| 127 | + } |
| 128 | + } |
| 129 | + return (Set)new FiniteSet(roots).InnerSimplified; |
| 130 | + } |
| 131 | + |
| 132 | + private static bool IsClearOfZero(Complex residual) |
| 133 | + => residual.Abs().EDecimal.CompareTo(EDecimal.FromString("1E-10")) > 0; |
| 134 | + |
| 135 | + /// <summary> |
| 136 | + /// Reads <paramref name="term"/>, times <paramref name="sign"/>, as rational multiples of |
| 137 | + /// logarithms of <paramref name="x"/> to one constant base, and a constant; <see langword="false"/> |
| 138 | + /// where it is anything else. |
| 139 | + /// </summary> |
| 140 | + private static bool Gather(Entity term, ERational sign, Variable x, |
| 141 | + List<(ERational Multiplier, Entity Argument)> logarithms, ref Entity constant, ref Entity? logBase) |
| 142 | + { |
| 143 | + switch (term) |
| 144 | + { |
| 145 | + case var _ when !term.ContainsNode(x): |
| 146 | + constant += Rational.Create(sign) * term; |
| 147 | + return true; |
| 148 | + case Sumf(var augend, var addend): |
| 149 | + return Gather(augend, sign, x, logarithms, ref constant, ref logBase) |
| 150 | + && Gather(addend, sign, x, logarithms, ref constant, ref logBase); |
| 151 | + case Minusf(var minuend, var subtrahend): |
| 152 | + return Gather(minuend, sign, x, logarithms, ref constant, ref logBase) |
| 153 | + && Gather(subtrahend, sign.Negate(), x, logarithms, ref constant, ref logBase); |
| 154 | + case Mulf(Rational multiplier, var rest): |
| 155 | + return Gather(rest, sign.Multiply(multiplier.ERational), x, logarithms, ref constant, ref logBase); |
| 156 | + case Mulf(var rest, Rational multiplier): |
| 157 | + return Gather(rest, sign.Multiply(multiplier.ERational), x, logarithms, ref constant, ref logBase); |
| 158 | + case Divf(var rest, Rational { IsZero: false } divisor): |
| 159 | + return Gather(rest, sign.Divide(divisor.ERational), x, logarithms, ref constant, ref logBase); |
| 160 | + case Logf(var @base, var argument) when !@base.ContainsNode(x): |
| 161 | + if (logBase is null) |
| 162 | + logBase = @base; |
| 163 | + else if (logBase != @base) |
| 164 | + return false; |
| 165 | + logarithms.Add((sign, argument)); |
| 166 | + return true; |
| 167 | + default: |
| 168 | + return false; |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | +} |
0 commit comments