Skip to content

The Lambert W function compiles, and W_0 left of -2/e is on its branch - #2214

Merged
Rafael-SOWNet merged 1 commit into
masterfrom
the-lambert-w-function-compiles
Oct 30, 2026
Merged

Rafael-SOWNet merged 1 commit into
masterfrom
the-lambert-w-function-compiles

Conversation

@Rafael-SOWNet

Copy link
Copy Markdown
Member

Part of #2195, item 3: lambertw compiles. The PR also fixes a defect in #2202's evaluation that compiling it brought to light.

Compiling. Numerics/LambertWFunction is a double-precision kernel on the interpreter's branches, Corless et al.'s, as #2202 has them:

  • a start, then Halley's iteration;
  • the imaginary part held at 0 where the branch is real;
  • -1 within rounding of the branch point, and NaN on a branch that is not whole.

FastExpression has an instruction for it, and LINQ has Complex and double definitions; over double a complex value is NaN. Evaluation now takes its double-precision start from the same kernel, which replaces InDoublePrecision.

The defect. Comparing the kernel with mpmath found evaluation wrong as well, because the two shared one start:

  • W_0 on the real axis from -1 to -2/e was real. The start there was ln(1 + z), which is real where W_0 is not, so the iteration never left the axis: W_0(-0.975) was -6.61, where mpmath gives -0.3358 + 1.3221i.
  • About -1, the same start is near ln 0, and the iteration reached another branch: W_0(-1.02) and W_0(-1.349 - 0.358i) came back as conjugates.

#2202's 700-point check against mpmath missed it, because random complex points almost never lie on the axis.

The starts now follow SciPy's lambertw regions on the principal branch:

  • the branch-point series within 0.3 of -1/e;
  • ln(1 + z) in a box right of -1 about the axis;
  • the asymptotic start elsewhere.

The other branches are unchanged. lambertw is new since 2.6.0, so there is no 2.6.0 row; its entry says it compiles.

Checked against mpmath (dps 30) at 11,501 points, evaluated and compiled alike: the real axis from -5 to 0 in steps of 0.01 on branches -1, 0 and 1, and 10,000 random points on branches -3 to 3, a third of them near -1 and a tenth far out. All agree to 1e-9.

Tests:

  • LambertWCompilationTest compares both compilers with evaluation on branches -2, -1, 0, 1, 3 and 1/2, at 19 points: the branch point, either side of it, the negative axis, far out, and off the axis.
  • LambertWTest gains five mpmath rows at -0.8, -0.975, -1, -1.02 and -1.349 - 0.358i.
  • Full suite 17940/0, interactive trio, AOT smoke 69/0, all frameworks build.

🤖 Generated with Claude Code

https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura

lambertw compiles in both compilers, through LambertWFunction, a
double-precision kernel on the interpreter's branches: a start, then
Halley's iteration, with the imaginary part held at 0 where the branch
is real, -1 within rounding of the branch point, and NaN on a branch
that is not whole. The evaluator takes its double-precision start from
the same kernel, which replaces InDoublePrecision.

Comparing it with mpmath found the start wrong for W_0 on the axis from
-1 to -2/e and about -1: ln(1 + z) is real there, where W_0 is not, so
the iteration never left the axis (W_0(-0.975) was -6.61), and near -1
it reached another branch (W_0(-1.02) was its conjugate). The starts
are now SciPy's regions on the principal branch: the branch-point
series within 0.3 of -1/e, ln(1 + z) in a box right of -1 about the
axis, and the asymptotic start elsewhere. Evaluation and the compiled
form agree with mpmath at 11,501 points: the real axis from -5 to 0 on
three branches, and 10,000 random points on seven.

Part of #2195

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura
@Rafael-SOWNet Rafael-SOWNet moved this from Todo to In review in AngouriMath Oct 29, 2026
@codecov

codecov Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.54545% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.58%. Comparing base (9164e15) to head (d580573).

Files with missing lines Patch % Lines
Sources/AngouriMath/Numerics/LambertWFunction.cs 95.12% 1 Missing and 1 partial ⚠️
...gouriMath/Functions/Compilation/IntoFE/Compiler.cs 80.00% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2214      +/-   ##
==========================================
- Coverage   86.59%   86.58%   -0.01%     
==========================================
  Files         303      304       +1     
  Lines       67168    67207      +39     
  Branches    19495    19509      +14     
==========================================
+ Hits        58162    58194      +32     
- Misses       4903     4907       +4     
- Partials     4103     4106       +3     
Flag Coverage Δ
cpp 37.77% <ø> (ø)
csharp 87.14% <94.54%> (-0.01%) ⬇️
fsharp 32.97% <ø> (ø)
interactive 40.72% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...ouriMath/Core/Entity/Continuous/Number/LambertW.cs 83.33% <100.00%> (-3.63%) ⬇️
...ath/Functions/Compilation/IntoFE/FastExpression.cs 96.22% <100.00%> (+0.04%) ⬆️
...ctions/Compilation/IntoLinq/CompilationProtocol.cs 90.77% <100.00%> (+0.05%) ⬆️
...h/Functions/Compilation/IntoLinq/MathAllMethods.cs 70.45% <100.00%> (+0.33%) ⬆️
...gouriMath/Functions/Compilation/IntoFE/Compiler.cs 86.75% <80.00%> (+0.21%) ⬆️
Sources/AngouriMath/Numerics/LambertWFunction.cs 95.12% <95.12%> (ø)

... and 8 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Rafael-SOWNet
Rafael-SOWNet merged commit 33b0bd8 into master Oct 30, 2026
36 checks passed
@github-project-automation github-project-automation Bot moved this from In review to Done in AngouriMath Oct 30, 2026
@Rafael-SOWNet
Rafael-SOWNet deleted the the-lambert-w-function-compiles branch October 30, 2026 00:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant