-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix: lgamma handles negative reals correctly, respects config.predictable #3648
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ShehabSherif0
wants to merge
2
commits into
josdejong:develop
Choose a base branch
from
ShehabSherif0:fix/lgamma-complex-negative-real
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+75
−17
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR description says this fixes issue #3604, but
numberinputs are still handled bylgammaNumber, and this file’s documentation/tests indicatelgamma(<negative real number>)is expected to return NaN. If #3604 is about negative number inputs, this PR won’t resolve it—consider adjusting the issue linkage/wording (or changinglgammaNumber) to avoid implying the issue is fully fixed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good observation. Issue #3604 covers two aspects:
lgamma(Complex(-1.5, 0))returningComplex(NaN, 0)-- this is the code bug this PR fixes.lgamma(-1.5)(plain number) returningNaN-- this is expected and intentional, because the result is complex and a real-number function cannot return a complex value.The maintainer discussion on #3604 converged on this exact approach. @gwhitney wrote:
And outlined two action items:
lgamma(z)when z is a negative non-integer real as a Complex -- done in this PR.So the issue linkage is accurate; both parts of the agreed resolution are addressed here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First of all, thanks for the help in trying to resolve this, but please use caution in using AI agents to resolve issues and make pull requests -- they often introduce duplicative or otherwise hard-to-main code.
That said, the quoted point (2) is not a correct characterization of how mathjs handles functions of real (plain JavaScript) numbers that may be interpreted to return Complex values for some (but not all) inputs. Please see the final comment from #3604 for a more detailed description of the desired behavior of lgamma on real inputs, and adjust this PR accordingly. In particular, note the dependence of the behavior of lgamma() on negative non-integer inputs on the
config.predictablesetting. (Also please note this is not the full review, it's just a key first comment to get it to a point where performing a full review is worthwhile; thanks for understanding.)Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the correction, and for the patience. You were correct about this. I went back and read your final comment on #3604 carefully and realized I had completely missed the
config.predictablerequirement.I've now reworked the PR to follow the same pattern that
sqrt,log,log2,log10,pow,asin, and the others use for this exact situation. Specifically:configas a factory dependency forlgammaconfig.predictableis true, it goes throughlgammaNumber(returning NaN for negatives in predictable mode, preserving the number return type guarantee). Otherwise it routes throughlgammaComplex(new Complex(x, 0))to produce the correct principal branch result.lgammaNumbershortcut ton.im === 0 && n.re >= 0) is still in place.So now:
I've also updated the docstring to properly describe lgamma as the principal branch of the LogGamma special function (not just log(gamma(z))), and to document the
config.predictablebehavior.Tests are updated with Wolfram Alpha reference values for the negative non-integer cases, plus a dedicated
predictable: truetest. All 6644 tests in the suite pass.Point taken on the AI tooling as well. I'll be more careful to verify the library's conventions before jumping in.