Skip to content

BUG: amos/amos.h: fix translation bugs in binu and rati#138

Merged
steppi merged 3 commits into
scipy:mainfrom
JaRoSchm:amos_binu_rati_fixes
May 10, 2026
Merged

BUG: amos/amos.h: fix translation bugs in binu and rati#138
steppi merged 3 commits into
scipy:mainfrom
JaRoSchm:amos_binu_rati_fixes

Conversation

@JaRoSchm

@JaRoSchm JaRoSchm commented May 6, 2026

Copy link
Copy Markdown
Contributor

Reference issue

What does this implement/fix?

Hi, I detected these errors in the same way as in #92. This time I used besj with z = 70.26595687262548 - 0.7026595687262548j and n = 50 in https://github.com/JaRoSchm/pyamos/blob/main/compare_xsf_zbessel.py for finding the bugs. This was simply incorrectly translated from FORTRAN, see https://github.com/scipy/scipy/blob/4edfcaa3ce8a387450b6efce968572def71be089/scipy/special/amos/. Regarding testing the same question/comment from #92 applies.

Additional information

AI Generation Disclosure

No AI tools used

@dschmitz89

Copy link
Copy Markdown
Contributor

Thanks for digging so deep into this difficult code.

Tests should go into this folder. You could mimic the approach from the other tests in that folder. With a little help from the LLM of your choice, it should hopefully not be too difficult (at least that was my experience as someone who never used ctest before). Do you have any specific questions?

Comment thread include/xsf/amos/amos.h Outdated
Comment on lines +2790 to +2795
if (nw > 0) {
return -1;
nz = -1;
if (nw == -2) {
nz = -2;
}
return nz;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The if condition in 2792 is unreachable as first we check for nw>0 and then for nw==-2. Was this in the original Fortran source?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, of course you are right, I reverted this change.
Yes, this actually was this way in the Fortran source: https://github.com/scipy/scipy/blob/4edfcaa3ce8a387450b6efce968572def71be089/scipy/special/amos/zbinu.f#L99

@JaRoSchm

JaRoSchm commented May 7, 2026

Copy link
Copy Markdown
Contributor Author

Thank you for the review! I added a test which fails on main and passes here. Mainly, I was worried about the size of the reference data. Here, I could reduce it to an array of length 15. However, in one of my other PRs, the length would be larger than 200 and I am not sure, if this is nice to put into the code.

When this is merged, I will update #92 and #93 and add tests to the same file.

Comment thread include/xsf/amos/amos.h
if ((az <= 2.) || (az * az * 0.25 <= (dfnu + 1.0))) {
/* GOTO 10 */
nw = seri(z, fnu, kode, n, cy, tol, elim, alim);
nw = seri(z, fnu, kode, nn, cy, tol, elim, alim);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread include/xsf/amos/amos.h
// MILLER ALGORITHM NORMALIZED BY THE SERIES
//
nw = mlri(z, fnu, kode, n, cy, tol);
nw = mlri(z, fnu, kode, nn, cy, tol);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread include/xsf/amos/amos.h
// ASYMPTOTIC EXPANSION FOR LARGE Z
//
nw = asyi(z, fnu, kode, n, cy, rl, tol, elim, alim);
nw = asyi(z, fnu, kode, nn, cy, rl, tol, elim, alim);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread include/xsf/amos/amos.h
if (az <= rl) {
/* 70 */
nw = mlri(z, fnu, kode, n, cy, tol);
nw = mlri(z, fnu, kode, nn, cy, tol);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread include/xsf/amos/amos.h

if (p1 == 0.) {
p1 = std::complex<double>(0, 0);
p1 = std::complex<double>(tol, tol);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread include/xsf/amos/amos.h

for (i = 2; i < (n + 1); i++) {
pt = cdfnu + t1 * rz * cy[k];
pt = cdfnu + t1 * rz + cy[k];

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dschmitz89

Copy link
Copy Markdown
Contributor

The translation errors look genuine. I am not so convinced about the outcome though. When I run your test data against scipy's main branch, the maximal relative error I get is in the order of 10 eps.

import numpy as np
from mpmath import mp
from scipy.special import jv

mp.dps = 1000


n = 15
z = 70 - 0.7j

for n in range(1, n + 1):
    reference = complex(mp.besselj(n, mp.mpc(z)))
    scipy_result = jv(n, z)
    rel_error = abs(reference - scipy_result) / abs(reference)
    print(f"n={n}, mpmath.j{n}(z)={reference}, scipy.special.jv({n}, z)={scipy_result}, relative error={rel_error}")

Do your changes decrease the relative error even more?

@JaRoSchm

Copy link
Copy Markdown
Contributor Author

No, I expect no changes for scipy from this PR. Instead, this fixes a feature of the amos library which at the moment cannot be accessed through scipy (which is probably the reason why these bugs were not caught during the translation process). In the test file you can find the parameter n which is the number of consecutive orders of Bessel function returned. In the wrappers in xsf/include/xsf/bessel.h used by scipy this is always set to n = 1. However, it is planned to make this feature accessible for scipy, see the discussion in #50. So before approaching this, I want to fix the remaining translation bugs.

@steppi steppi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Thanks @JaRoSchm and thanks @dschmitz89 for the review

@steppi steppi merged commit 36a3c3f into scipy:main May 10, 2026
4 of 6 checks passed
@JaRoSchm JaRoSchm deleted the amos_binu_rati_fixes branch May 11, 2026 05:29
CodeHermit17 pushed a commit to CodeHermit17/xsf that referenced this pull request May 12, 2026
* BUG: amos/amos.h: fix translation bugs in binu and rati

* remove unreachable code

* add test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants