Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions Sources/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// <c>K (alpha x + beta) b^(gamma x + delta) = c</c>, a linear function times an exponential of
/// another, solved by the Lambert W function. https://github.com/asc-community/AngouriMath/issues/2182
/// </summary>
/// <remarks>
/// Written <c>e^((gamma x + delta) ln b)</c>, the exponent is <c>u = g x + d</c>, and with
/// <c>s = beta g / alpha - d</c> the equation is <c>(u + s) e^(u + s) = A</c>,
/// <c>A = c g e^s / (K alpha)</c>, so <c>u + s = W(A)</c> and <c>x = W(A) / g - beta / alpha</c>.
/// Over the complex numbers every branch is a root, <c>W_n</c> for a whole <c>n</c>, as a periodic
/// equation's roots are written. Over the reals two branches can be: <c>W_0</c> where
/// <c>A &gt;= -1/e</c> and <c>W_(-1)</c> where <c>-1/e &lt;= A &lt; 0</c>, and none where
/// <c>A &lt; -1/e</c>, since <c>w e^w &gt;= -1/e</c> for every real <c>w</c>.
/// </remarks>
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;
}
}
84 changes: 84 additions & 0 deletions Sources/Tests/UnitTests/Algebra/SolveTest/LambertWSolveTest.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// 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.
/// <a href="https://github.com/asc-community/AngouriMath/issues/2182">#2182</a>
/// </summary>
[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<Set.FiniteSet>(equation.ToEntity().Solve("x"));
var root = Assert.Single(roots);
Assert.Equal(Number.Integer.Zero, (root - expected.ToEntity()).Simplify());
Satisfies(equation, root);
}

/// <summary>Between -1/e and 0 both real branches are roots, and below -1/e neither is.</summary>
[Fact]
public void BetweenMinusOneOverEAndZeroThereAreTwoRealRoots()
{
using var _ = MathS.Settings.Codomain.Set(Domain.Real);
var roots = Assert.IsType<Set.FiniteSet>("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"));
}

/// <summary>Over the reals a symbolic right side carries where each branch is real.</summary>
[Fact]
public void ASymbolicRightSideCarriesTheConditions()
{
using var _ = MathS.Settings.Codomain.Set(Domain.Real);
var roots = Assert.IsType<Set.FiniteSet>("x e^x = c".ToEntity().Solve("x"));
Assert.Equal(2, roots.Count);
Assert.All(roots, root => Assert.IsType<Providedf>(root));
}

/// <summary>Over the complex numbers every branch is a root, W_n for a whole n.</summary>
[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<Set.FiniteSet>(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));
}
}
}
Loading