-
-
Notifications
You must be signed in to change notification settings - Fork 14
Bugfix/Workaround sign error Asso. Legendre Function at z=-1 #76
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
mberz
wants to merge
1
commit into
scipy:main
Choose a base branch
from
mberz:bugfix/legendre_sign_error
base: main
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.
Conversation
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
lucascolley
approved these changes
Nov 18, 2025
Member
lucascolley
left a comment
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.
LGTM, thanks @mberz ! I've confirmed that the test fails on main and passes on this branch:
scipy on 🎋 bug/assoc_legendre is 📦 v1.17.0.dev0 via 🐍 via 🧚 v0.59.0 took 4s
❯ pixi run test -t scipy.special.tests.test_legendre "--" -k "assoc_legendre_bug"
✨ Pixi task (build in build): spin build --setup-args=-Dblas=blas --setup-args=-Dlapack=lapack --setup-args=-Duse-g77-abi=true: (Build SciPy (default settings)) $ meson compile -j 8 -C build
INFO: autodetecting backend as ninja
INFO: calculating backend command to run: /Users/lucascolley/ghq/github.com/rgommers/pixi-dev-scipystack/scipy/scipy/.pixi/envs/build/bin/ninja -C /Users/lucascolley/ghq/github.com/rgommers/pixi-dev-scipystack/scipy/scipy/build -j 8
ninja: Entering directory `/Users/lucascolley/ghq/github.com/rgommers/pixi-dev-scipystack/scipy/scipy/build'
[2/338] Generating subprojects/highs/src/HConfig.h with a custom command
$ meson install --only-changed -C build --destdir ../build-install --tags=runtime,python-runtime,tests,devel
✨ Pixi task (test in test): spin test --no-build -t scipy.special.tests.test_legendre -- -k assoc_legendre_bug: (Test SciPy (default settings)) $ export PYTHONPATH="/Users/lucascolley/ghq/github.com/rgommers/pixi-dev-scipystack/scipy/scipy/build-install/usr/lib/python3.14/site-packages"
$ /Users/lucascolley/ghq/github.com/rgommers/pixi-dev-scipystack/scipy/scipy/.pixi/envs/test/bin/python3.14 -P -c 'import scipy'
$ cd /Users/lucascolley/ghq/github.com/rgommers/pixi-dev-scipystack/scipy/scipy/build-install/usr/lib/python3.14/site-packages
$ /Users/lucascolley/ghq/github.com/rgommers/pixi-dev-scipystack/scipy/scipy/.pixi/envs/test/bin/python3.14 -P -m pytest -m 'not slow' -k assoc_legendre_bug --pyargs scipy.special.tests.test_legendre
================================================ test session starts =================================================
platform darwin -- Python 3.14.0, pytest-9.0.0, pluggy-1.6.0
rootdir: /Users/lucascolley/ghq/github.com/rgommers/pixi-dev-scipystack/scipy/scipy
configfile: pytest.ini
plugins: hypothesis-6.147.0, xdist-3.8.0, timeout-2.4.0
collected 417 items / 416 deselected / 1 selected
scipy/special/tests/test_legendre.py F [100%]
====================================================== FAILURES ======================================================
_____________________________________ TestAssocLegendreP.test_assoc_legendre_bug _____________________________________
self = <scipy.special.tests.test_legendre.TestAssocLegendreP object at 0x11a0bf750>
def test_assoc_legendre_bug(self):
"""
This test detects the bug reported in
https://github.com/scipy/scipy/issues/23101
"""
z = np.array([-1, -.5, 0, .5, 1])
expected = assoc_legendre_p_1_0(z)
result = assoc_legendre_p(1, 0, z)
> assert_allclose(np.squeeze(result), expected)
E AssertionError:
E Not equal to tolerance rtol=1e-07, atol=0
E
E Mismatched elements: 1 / 5 (20%)
E Max absolute difference among violations: 2.
E Max relative difference among violations: 2.
E ACTUAL: array([ 1. , -0.5, 0. , 0.5, 1. ])
E DESIRED: array([-1. , -0.5, 0. , 0.5, 1. ])
expected = array([-1. , -0.5, 0. , 0.5, 1. ])
result = array([[ 1. , -0.5, 0. , 0.5, 1. ]])
self = <scipy.special.tests.test_legendre.TestAssocLegendreP object at 0x11a0bf750>
z = array([-1. , -0.5, 0. , 0.5, 1. ])
scipy/special/tests/test_legendre.py:79: AssertionError
============================================== short test summary info ===============================================
FAILED scipy/special/tests/test_legendre.py::TestAssocLegendreP::test_assoc_legendre_bug - AssertionError:
========================================= 1 failed, 416 deselected in 0.52s ==========================================
scipy on 🎋 bug/assoc_legendre is 📦 v1.17.0.dev0 via 🐍 via 🧚 v0.59.0 took 5s
❯ cd subprojects/xsf/
xsf on 🎋 HEAD (33768a0) is 📦 v0.1.3 via △ v4.1.2 via 🧚 v0.59.0
❯ gpc
? Select a pull request 76 OPEN Bugfix/Workaround sign error Asso. Legendre Function at z=-1 [mberz:bugfix/legendre_sign_error]
remote: Enumerating objects: 11, done.
remote: Counting objects: 100% (11/11), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 11 (delta 5), reused 10 (delta 5), pack-reused 0 (from 0)
Unpacking objects: 100% (11/11), 1.97 KiB | 155.00 KiB/s, done.
From https://github.com/scipy/xsf
* [new ref] refs/pull/76/head -> bugfix/legendre_sign_error
Previous HEAD position was 33768a0 REL: bump version to 0.1.3 (#67)
Switched to branch 'bugfix/legendre_sign_error'
xsf on 🎋 bugfix/legendre_sign_error is 📦 v0.1.3 via △ v4.1.2 via 🧚 v0.59.0 took 6s
❯ cd ../..
scipy on 🎋 bug/assoc_legendre [🔨] is 📦 v1.17.0.dev0 via 🐍 via 🧚 v0.59.0
❯ pixi run test -t scipy.special.tests.test_legendre "--" -k "assoc_legendre_bug"
✨ Pixi task (build in build): spin build --setup-args=-Dblas=blas --setup-args=-Dlapack=lapack --setup-args=-Duse-g77-abi=true: (Build SciPy (default settings)) $ meson compile -j 8 -C build
INFO: autodetecting backend as ninja
INFO: calculating backend command to run: /Users/lucascolley/ghq/github.com/rgommers/pixi-dev-scipystack/scipy/scipy/.pixi/envs/build/bin/ninja -C /Users/lucascolley/ghq/github.com/rgommers/pixi-dev-scipystack/scipy/scipy/build -j 8
ninja: Entering directory `/Users/lucascolley/ghq/github.com/rgommers/pixi-dev-scipystack/scipy/scipy/build'
[12/12] Linking target scipy/special/_special_ufuncs.cpython-314-darwin.so
$ meson install --only-changed -C build --destdir ../build-install --tags=runtime,python-runtime,tests,devel
✨ Pixi task (test in test): spin test --no-build -t scipy.special.tests.test_legendre -- -k assoc_legendre_bug: (Test SciPy (default settings)) $ export PYTHONPATH="/Users/lucascolley/ghq/github.com/rgommers/pixi-dev-scipystack/scipy/scipy/build-install/usr/lib/python3.14/site-packages"
$ /Users/lucascolley/ghq/github.com/rgommers/pixi-dev-scipystack/scipy/scipy/.pixi/envs/test/bin/python3.14 -P -c 'import scipy'
$ cd /Users/lucascolley/ghq/github.com/rgommers/pixi-dev-scipystack/scipy/scipy/build-install/usr/lib/python3.14/site-packages
$ /Users/lucascolley/ghq/github.com/rgommers/pixi-dev-scipystack/scipy/scipy/.pixi/envs/test/bin/python3.14 -P -m pytest -m 'not slow' -k assoc_legendre_bug --pyargs scipy.special.tests.test_legendre
==================================================================== test session starts ====================================================================
platform darwin -- Python 3.14.0, pytest-9.0.0, pluggy-1.6.0
rootdir: /Users/lucascolley/ghq/github.com/rgommers/pixi-dev-scipystack/scipy/scipy
configfile: pytest.ini
plugins: hypothesis-6.147.0, xdist-3.8.0, timeout-2.4.0
collected 417 items / 416 deselected / 1 selected
scipy/special/tests/test_legendre.py . [100%]
============================================================= 1 passed, 416 deselected in 0.39s =============================================================
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This might fix the bug reported in scipy/scipy#23101.
It seems that
xsf/include/xsf/legendre.h
Line 313 in 15ffa02
This is an edge case for abs(z)=1, imag(z)=0 (see
xsf/include/xsf/legendre.h
Line 402 in 15ffa02
I've implemented a test which catches the original issue in scipy/scipy#24017.
I am not sure if this is a proper solution to the problem or only a workaround. I did not find references in the code of the Legendre functions which were used for the original implementation.