From f01b23ec9332ec7fe90e4545603f90bb10adf596 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Thu, 29 Oct 2026 15:20:42 +0000 Subject: [PATCH] Solve a linear function times an exponential by the Lambert W function An equation K (a x + b) c^(g x + d) = k, with one such term in x, has the roots W(A) / (g ln c) - b / a, where A = k g ln c e^(b g ln c / a - d ln c) / (K a). Over the complex numbers the answer is the family over every branch, lambertw(n, A). Over the reals it is W_0 alone for A >= 0 or A = -1/e, both W_0 and W_-1 for -1/e < A < 0, and nothing below -1/e; a symbolic A carries those ranges as conditions. x e^x = 2 was a decimal root found by Newton's method, and over the reals a set of complex decimals as well; it is now { lambertw(2) }. Part of #2182 Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura --- BREAKING-CHANGES.md | 15 +++ Sources/.editorconfig | 6 ++ .../AnalyticalEquationSolver.cs | 8 ++ .../Solvers/EquationSolver/LambertWSolver.cs | 93 +++++++++++++++++++ .../Algebra/SolveTest/LambertWSolveTest.cs | 84 +++++++++++++++++ 5 files changed, 206 insertions(+) create mode 100644 Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/LambertWSolver.cs create mode 100644 Sources/Tests/UnitTests/Algebra/SolveTest/LambertWSolveTest.cs diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 65ec24164..b44d31176 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -555,6 +555,21 @@ through `digamma` and `polygamma` for a whole `m >= 1`. A negative integer `n` h | `"harmonicnumber(4)".ToEntity().Simplify()` | `4 * harmonicnumber` | `25/12` | | `"harmonicnumber(n, 2)".ToEntity()` | throws `UnhandledParseException` | `harmonicnumber(n, 2)` | +### A linear function times an exponential is solved by the Lambert W function + +**Not silent: `K (a x + b) c^(g x + d) = k` is solved exactly, where it was answered with roots Newton's method found.** The +roots are `x = W(A)/(g ln c) - b/a` for the `A` the equation gives. Over the complex numbers every branch is a root, +`lambertw(n_1, A)` for a whole `n_1`, as a periodic equation's roots are written with `n_1`. Over the reals `W_0` is +one where `A >= -1/e` and `W_(-1)` another where `-1/e <= A < 0`, with those conditions carried where `A` is symbolic, +and below `-1/e` there is none ([#2182](https://github.com/asc-community/AngouriMath/issues/2182)). + +| Input | Was (2.6.0) | Now | +|---|---|---| +| `"x e^x = 2".ToEntity().Solve("x")` | Newton's roots, `{ -2.1569… + 17.1537…i, -1.7023… + … }` | `{ lambertw(n_1, 2) }` | +| `"x e^x = 2".ToEntity().Solve("x")`, over the reals | the same complex numbers | `{ lambertw(2) }` | +| `"x e^x = -1/4".ToEntity().Solve("x")`, over the reals | Newton's complex roots | `{ lambertw(-1/4), lambertw(-1, -1/4) }` | +| `"x e^x = -1".ToEntity().Solve("x")`, over the reals | Newton's complex roots | `{ }` | + ### The Lambert W function **Not silent: `lambertw(z)` and `lambertw(k, z)`, also `productlog`, are the Lambert W function, the solution `w` of diff --git a/Sources/.editorconfig b/Sources/.editorconfig index 849786d9a..24582b422 100644 --- a/Sources/.editorconfig +++ b/Sources/.editorconfig @@ -844,3 +844,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed [Tests/UnitTests/Convenience/LambertWTest.cs] 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 + +[AngouriMath/Functions/Continuous/Solvers/EquationSolver/LambertWSolver.cs] +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 + +[Tests/UnitTests/Algebra/SolveTest/LambertWSolveTest.cs] +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 diff --git a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/AnalyticalEquationSolver.cs b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/AnalyticalEquationSolver.cs index c285ba0d5..c2b46ab42 100644 --- a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/AnalyticalEquationSolver.cs +++ b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/AnalyticalEquationSolver.cs @@ -271,6 +271,14 @@ bool ProductIsDefinedAt(Entity root) if (isIdentity) return MathS.Sets.C; + // A linear function times an exponential of another, by the Lambert W function: x e^x = 2 + // is lambertw(2), on every branch over the complex numbers. Answered with no roots only + // where none is real. Ahead of the spellings and the exponential solvers, which reach + // x 2^x = 5 through a decimal; it needs an exponential, so no polynomial is answered here, + // and a factor linear in x, so a sum of exponentials is still theirs. https://github.com/asc-community/AngouriMath/issues/2182 + if (LambertWSolver.Solve(expr, x) is FiniteSet lambert) + return (Set)lambert.Select(ent => TryDowncast(expr, x, ent)).ToSet().InnerSimplified; + // If the replacement isn't one-variable one, // then solving over replacements is already useless, // so we skip this part and go to other solvers diff --git a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/LambertWSolver.cs b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/LambertWSolver.cs new file mode 100644 index 000000000..cb2c1ac73 --- /dev/null +++ b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/LambertWSolver.cs @@ -0,0 +1,93 @@ +// +// Copyright (c) 2019-2026 Angouri. +// AngouriMath is licensed under MIT. +// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md. +// Website: https://am.angouri.org. +// + +using System.Collections.Generic; +using System.Linq; +using static AngouriMath.Entity; +using static AngouriMath.Entity.Set; + +namespace AngouriMath.Functions.Algebra.AnalyticalSolving +{ + /// + /// K (alpha x + beta) b^(gamma x + delta) = c, a linear function times an exponential of + /// another, solved by the Lambert W function. https://github.com/asc-community/AngouriMath/issues/2182 + /// + /// + /// Written e^((gamma x + delta) ln b), the exponent is u = g x + d, and with + /// s = beta g / alpha - d the equation is (u + s) e^(u + s) = A, + /// A = c g e^s / (K alpha), so u + s = W(A) and x = W(A) / g - beta / alpha. + /// Over the complex numbers every branch is a root, W_n for a whole n, as a periodic + /// equation's roots are written. Over the reals two branches can be: W_0 where + /// A >= -1/e and W_(-1) where -1/e <= A < 0, and none where + /// A < -1/e, since w e^w >= -1/e for every real w. + /// + internal static class LambertWSolver + { + internal static Set? Solve(Entity expr, Variable x) + { + var terms = Sumf.LinearChildren(expr); + var withX = terms.Where(term => term.ContainsNode(x)).ToList(); + if (withX.Count != 1) + return null; + var c = (-terms.Where(term => !term.ContainsNode(x)).Aggregate((Entity)Integer.Zero, (sum, term) => sum + term)).InnerSimplified; + + Entity k = Integer.One; + Entity? linear = null, exponent = null, logBase = null; + foreach (var factor in Mulf.LinearChildren(withX[0])) + { + if (!factor.ContainsNode(x)) + k *= factor; + else if (factor is Powf(var @base, var power) && !@base.ContainsNode(x)) + { + if (exponent is not null) + return null; + exponent = power; + logBase = @base == MathS.e ? Integer.One : MathS.Ln(@base); + } + else if (linear is null) + linear = factor; + else + return null; + } + if (linear is null || exponent is null || logBase is null + || !TreeAnalyzer.TryGetPolyLinear(linear, x, out var alpha, out var beta) + || !TreeAnalyzer.TryGetPolyLinear(exponent, x, out var gammaRaw, out var deltaRaw)) + return null; + // The multiplier, the slope and the rate are divided by, so each is to be a number that is + // not zero; what they are otherwise is a case split this does not make. + k = k.InnerSimplified; + var gamma = (gammaRaw * logBase).InnerSimplified; + var delta = (deltaRaw * logBase).InnerSimplified; + if (!IsANonZeroNumber(k) || !IsANonZeroNumber(alpha) || !IsANonZeroNumber(gamma)) + return null; + + // Simplified, both small, so that the root reads W(6 e^(-5/2)) / 3 + 1/2. + var s = (beta * gamma / alpha - delta).InnerSimplified; + var a = (c * gamma * MathS.Pow(MathS.e, s) / (k * alpha)).Simplify(); + var shift = (-beta / alpha).Simplify(); + Entity Root(Entity branch) => MathS.LambertW(branch, a) / gamma + shift; + + if (MathS.Settings.Codomain.Value != Core.Domain.Real) + return new FiniteSet(Root(Variable.CreateUnique(expr + a, "n"))); + + var aboveTheBranchPoint = (a + 1 / MathS.e).InnerSimplified; + if (aboveTheBranchPoint.Evaled is Real { EDecimal: var distance } && a.Evaled is Real { EDecimal: var at }) + { + if (distance.IsNegative) + return Empty; + if (distance.IsZero || !at.IsNegative) + return new FiniteSet(Root(Integer.Zero)); + return new FiniteSet(Root(Integer.Zero), Root(Integer.MinusOne)); + } + return new FiniteSet( + Root(Integer.Zero).Provided(a >= -1 / MathS.e), + Root(Integer.MinusOne).Provided((a >= -1 / MathS.e) & (a < 0))); + } + + private static bool IsANonZeroNumber(Entity e) => e.Evaled is Complex number && !number.IsZero; + } +} diff --git a/Sources/Tests/UnitTests/Algebra/SolveTest/LambertWSolveTest.cs b/Sources/Tests/UnitTests/Algebra/SolveTest/LambertWSolveTest.cs new file mode 100644 index 000000000..209e73792 --- /dev/null +++ b/Sources/Tests/UnitTests/Algebra/SolveTest/LambertWSolveTest.cs @@ -0,0 +1,84 @@ +// +// Copyright (c) 2019-2026 Angouri. +// AngouriMath is licensed under MIT. +// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md. +// Website: https://am.angouri.org. +// + +using System.Linq; +using AngouriMath; +using AngouriMath.Core; +using AngouriMath.Extensions; +using PeterO.Numbers; +using Xunit; +using static AngouriMath.Entity; + +namespace AngouriMath.Tests.Algebra.SolveTest +{ + /// + /// A linear function times an exponential of another is solved by the Lambert W function, on + /// every branch over the complex numbers and on the real ones over the reals. It was answered with + /// roots found by Newton's method. + /// #2182 + /// + [Trait("Area", "Algebra")] + public sealed class LambertWSolveTest + { + private static void Satisfies(string equation, Entity root) + { + var residual = equation.ToEntity() is Equalsf(var left, var right) ? left - right : equation.ToEntity(); + var value = (Number.Complex)residual.Substitute("x", root).EvalNumerical(); + Assert.True(value.Abs().EDecimal.CompareTo(EDecimal.FromString("1e-25")) < 0, $"{root.Stringize()} leaves {value.Stringize()}"); + } + + [Theory] + [InlineData("x e^x = 2", "lambertw(2)")] + [InlineData("3 x e^x = 6", "lambertw(2)")] + [InlineData("x e^(2 x) = 3", "lambertw(6) / 2")] + [InlineData("x * 2^x = 5", "lambertw(5 ln(2)) / ln(2)")] + [InlineData("x e^x = -1/e", "-1")] + public void OverTheRealsTheRealBranchIsTheRoot(string equation, string expected) + { + using var _ = MathS.Settings.Codomain.Set(Domain.Real); + var roots = Assert.IsType(equation.ToEntity().Solve("x")); + var root = Assert.Single(roots); + Assert.Equal(Number.Integer.Zero, (root - expected.ToEntity()).Simplify()); + Satisfies(equation, root); + } + + /// Between -1/e and 0 both real branches are roots, and below -1/e neither is. + [Fact] + public void BetweenMinusOneOverEAndZeroThereAreTwoRealRoots() + { + using var _ = MathS.Settings.Codomain.Set(Domain.Real); + var roots = Assert.IsType("x e^x = -1/4".ToEntity().Solve("x")); + Assert.Equal(2, roots.Count); + foreach (var root in roots) + Satisfies("x e^x = -1/4", root); + Assert.Equal(Set.Empty, "x e^x = -1".ToEntity().Solve("x")); + } + + /// Over the reals a symbolic right side carries where each branch is real. + [Fact] + public void ASymbolicRightSideCarriesTheConditions() + { + using var _ = MathS.Settings.Codomain.Set(Domain.Real); + var roots = Assert.IsType("x e^x = c".ToEntity().Solve("x")); + Assert.Equal(2, roots.Count); + Assert.All(roots, root => Assert.IsType(root)); + } + + /// Over the complex numbers every branch is a root, W_n for a whole n. + [Theory] + [InlineData("x e^x = 2")] + [InlineData("(x + 1) e^x = 2")] + [InlineData("(2 x - 1) e^(3 x + 1) = 4")] + public void OverTheComplexNumbersEveryBranchIsARoot(string equation) + { + var root = Assert.Single(Assert.IsType(equation.ToEntity().Solve("x"))); + var n = Assert.Single(root.Vars.Where(v => v.Name.StartsWith("n"))); + foreach (var branch in new[] { -2, 0, 1, 3 }) + Satisfies(equation, root.Substitute(n, branch)); + } + } +}