Skip to content

Commit 9cd0c64

Browse files
An equation linear in logarithms is solved by exponentiating (#2316)
An equation in two or more logarithms of x had no route of its own, and came to Newton's method, which answered it in decimals and left out the roots it did not reach: ln(x) + ln(x + 1) = 0 was { 0.618... } and ln(1 + 3x^2) - ln(4x) = 1 was { 3.53 }, without 0.0944. k_1 log_b(P_1) + ... + k_n log_b(P_n) = c, for polynomials P_i, rational k_i and one base, holds only where P_1^(k_1 L) ... P_n^(k_n L) = b^(c L), L being the common denominator of the k_i: a polynomial equation, whose roots are found exactly. Each is put back into the equation as written under the principal logarithm and kept only where it holds, since over the complex numbers ln(u) + ln(v) is ln(u v) only up to 2 pi i: at (-1 - sqrt(5))/2 both logarithms of ln(x) + ln(x + 1) carry i pi, and the sum is 2 pi i. Over the reals each argument is to be positive as well. A residual too small to tell from rounding, or a candidate that is not a root of the polynomial, leaves the equation to the routes after, since the roots the polynomial solver missed would be left out: 3x^3 + 9x^2 + 9x + 1, a shifted cube, comes back as -1 three times until #2315. On 400 random equations of this kind, checked against a Newton search under the principal logarithm, master answers 292, leaves 108 unsolved and misses 25 roots in 22 of its answers; this answers all 400, none unsound and none missing a root. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 9880e18 commit 9cd0c64

5 files changed

Lines changed: 265 additions & 0 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,21 @@ as asked the limit of `ln(f)` again, through a limit node of its own
205205
| `"limit(ln(2 + sin(x)), x, +oo)".ToEntity().Simplify()` | stack overflow | `limit(ln(2 + sin(x)), x, +oo)` |
206206
| `"limitright(ln(sin(1/x) + 2), x, 0)".ToEntity().Simplify()` | stack overflow | `limitright(ln(2 + sin(1 / x)), x, 0)` |
207207

208+
### An equation linear in logarithms is solved by exponentiating
209+
210+
**Silent: an equation `k_1 log_b(P_1) + … + k_n log_b(P_n) = c`, with at least two logarithms of polynomials to one
211+
base and rational `k_i`, is answered exactly and completely, where Newton's method answered it in decimals and left out
212+
the roots it did not reach.** Every root is one of the polynomial equation `P_1^(k_1 L) … P_n^(k_n L) = b^(c L)`, for
213+
`L` the common denominator of the `k_i`, and each of those is put back into the equation as written under the
214+
principal logarithm, since over the complex numbers `ln(u) + ln(v)` is `ln(u v)` only up to `2 pi i`. Over the reals
215+
each argument is to be positive as well.
216+
217+
| Input | Was (2.6.0) | Now |
218+
|---|---|---|
219+
| `"ln(x) + ln(x + 1) = 0".ToEntity().Solve("x")` | `{ 0.6180339887498947… }` | `{ (-1 + sqrt(5)) / 2 }` |
220+
| `"ln(1 + 3x^2) - ln(4x) = 1".ToEntity().Solve("x")` | `{ 3.529945609977906… }` | `{ (-(-e * 4) - sqrt((-e * 4) ^ 2 - 12)) / 6, (-(-e * 4) + sqrt((-e * 4) ^ 2 - 12)) / 6 }` |
221+
| `"ln(x^2 + 1) - ln(x) = 1".ToEntity().Solve("x")` | `{ 0.4386714553485325…, 2.279610373110512… }` | `{ (e - sqrt((-e) ^ 2 - 4)) / 2, (e + sqrt((-e) ^ 2 - 4)) / 2 }` |
222+
208223
### Primality is decided by Miller–Rabin and Lucas–Lehmer, and finishes
209224

210225
**Silent: a membership in `PP` left open past the machine word is decided where a test proves it, and one that did

‎Sources/.editorconfig‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
105105
[Tests/UnitTests/Algebra/ExponentialTwoBasesTest.cs]
106106
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
107107

108+
[Tests/UnitTests/Algebra/SolveTest/LogarithmSumEquationTest.cs]
109+
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
110+
108111
[Tests/UnitTests/Algebra/SolveTest/ADepressedBiquadraticQuarticTest.cs]
109112
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
110113

@@ -450,6 +453,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
450453
[AngouriMath/Functions/ExponentialQuotients.cs]
451454
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
452455

456+
[AngouriMath/Functions/Continuous/Solvers/EquationSolver/LogarithmSolver.cs]
457+
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
458+
453459
[AngouriMath/Functions/Compilation/CompiledMembership.cs]
454460
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
455461

‎Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/AnalyticalEquationSolver.cs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,14 @@ internal static Set Solve(Entity expr, Variable x, bool compensateSolving = fals
140140
&& ModulusSolver.SolveOverTheComplexNumbers(expr, x) is { } overTheComplexNumbers)
141141
return overTheComplexNumbers;
142142

143+
// A sum of rational multiples of logarithms of polynomials to one base is a polynomial
144+
// equation once exponentiated, and each of that one's roots is put back into this one,
145+
// since over the complex numbers ln(u) + ln(v) is ln(u v) only up to 2 pi i:
146+
// ln(x) + ln(x + 1) = 0 is { (sqrt(5) - 1) / 2 }, which Newton's method found in
147+
// decimals. Asked of the equation as it was asked, as the route above is.
148+
if (!compensateSolving && LogarithmSolver.Solve(expr, x) is { } byExponentiating)
149+
return byExponentiating;
150+
143151
// Whether a candidate root really is one. The loose tolerance that guesses the
144152
// rational must not also be what decides this: at 1e-7, x^41 + 6x + 1 accepted
145153
// -1/6, whose residual is -1.25e-32 -- small, but the difference between an
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
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+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 AngouriMath.Core;
9+
using AngouriMath.Extensions;
10+
using Xunit;
11+
using static AngouriMath.Entity;
12+
using static AngouriMath.Entity.Set;
13+
14+
namespace AngouriMath.Tests.Algebra
15+
{
16+
/// <summary>
17+
/// An equation linear in logarithms of polynomials to one base is solved by exponentiating:
18+
/// every root is one of the polynomial equation's, and each of those is put back, since over the
19+
/// complex numbers <c>ln(u) + ln(v)</c> is <c>ln(u v)</c> only up to <c>2 pi i</c>. Newton's
20+
/// method answered these in decimals, and where it did not reach a root it left it out:
21+
/// <c>ln(1 + 3x^2) - ln(4x) = 1</c> was <c>{ 3.53 }</c>, without <c>0.0944</c>. Found by a
22+
/// cross-check of equations in logarithms against a Newton search; no issue was filed.
23+
/// </summary>
24+
[Trait("Area", "Algebra")]
25+
public sealed class LogarithmSumEquationTest
26+
{
27+
private static void AssertRoots(string equation, params string[] roots)
28+
{
29+
var answer = Assert.IsType<FiniteSet>(equation.ToEntity().Solve("x"));
30+
Assert.Equal(roots.Length, answer.Count);
31+
foreach (var root in roots)
32+
Assert.Contains(answer, found => Number.IsZero((found - root.ToEntity()).EvalNumerical()));
33+
// Exact, not a decimal from a numerical search.
34+
Assert.DoesNotContain(answer, found => found is Number.Real and not Number.Rational);
35+
}
36+
37+
[Theory]
38+
// (-1 - sqrt(5)) / 2 makes x(x + 1) = 1 too, and there both logarithms carry i pi.
39+
[InlineData("ln(x) + ln(x + 1) = 0", "(sqrt(5) - 1) / 2")]
40+
[InlineData("ln(1 + 3x^2) - ln(4x) = 1", "(4e - sqrt(16e^2 - 12)) / 6", "(4e + sqrt(16e^2 - 12)) / 6")]
41+
// Both arguments negative: their i pi cancel.
42+
[InlineData("ln(-1 - 3x^2) - ln(4x) = 1", "-(4e - sqrt(16e^2 - 12)) / 6", "-(4e + sqrt(16e^2 - 12)) / 6")]
43+
[InlineData("ln(x^2 + 1) - ln(x) = 1", "(e - sqrt(e^2 - 4)) / 2", "(e + sqrt(e^2 - 4)) / 2")]
44+
// x = -2 makes x(x - 1) = 6, where the sum is ln(6) + 2 pi i.
45+
[InlineData("ln(x) + ln(x - 1) = ln(6)", "3")]
46+
[InlineData("ln(x - 2) + ln(x - 3) = ln(2)", "4")]
47+
// x = -1 makes x^2 = x + 2, where 2 ln(x) is 2 pi i and ln(1) is 0.
48+
[InlineData("2 * ln(x) = ln(x + 2)", "2")]
49+
[InlineData("log(2, x) + log(2, x - 2) = 3", "4")]
50+
[InlineData("ln(x) / 2 + ln(x + 1) = 1",
51+
"-(2 + ((-2 - 27 e^2 + sqrt((-2 - 27 e^2)^2 - 4)) / 2)^(1/3) + 1 / ((-2 - 27 e^2 + sqrt((-2 - 27 e^2)^2 - 4)) / 2)^(1/3)) / 3")]
52+
public void SolvedByExponentiating(string equation, params string[] roots)
53+
=> AssertRoots(equation, roots);
54+
55+
/// <summary>Over the reals a logarithm of a negative number has no value, and the root that needs one goes.</summary>
56+
[Fact]
57+
public void OverTheRealsTheArgumentsArePositive()
58+
{
59+
using var _ = MathS.Settings.Codomain.Set(Domain.Real);
60+
AssertRoots("ln(1 + 3x^2) - ln(4x) = 1", "(4e - sqrt(16e^2 - 12)) / 6", "(4e + sqrt(16e^2 - 12)) / 6");
61+
Assert.Equal("{ }".ToEntity(), "ln(-1 - 3x^2) - ln(4x) = 1".ToEntity().Solve("x"));
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)