HandyModRTransform.deriv3 is wrong for m > 2

Closed Beginner friendly
#347 0 comments 0 reactions 0 assignees View on GitHub

@SajalDevX is already working on this.

Since Nov 4, 2026.

  • #348 by @SajalDevX — open

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
75/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
numpy, python
Domain
data

Research direction

The bug is in HandyModRTransform.deriv3 in grid/rtransform.py, in the first numerator term that uses 2 * two_m. Read that term against the symbolic third derivative of the transform, and check deriv3_inverse, which depends on it. Reproduce with the solve_ode_ivp snippet in the issue for m = 3 and 4. Done means deriv3 matches finite differences for m = 1 to 5 and the reproduction gives (x + 0.5)**3, with a regression test added.

Written by the indexing model from the issue text.

Description

HandyModRTransform.deriv3 returns the wrong third derivative whenever m >= 3. For m = 1 and m = 2 it is correct, which is the only range the current tests use.

The first term in the numerator is written as 2 * two_m * (m - 2) * (m - 1) * (1 - two_m + size_r) ** 2, but differentiating r(x) three times gives two_m**2 there, not 2 * two_m. The term is multiplied by (m - 1)(m - 2), so the mistake only shows up from m = 3 on.

Because deriv3 feeds deriv3_inverse and the transformed ODE solvers, those are off too. A small example with solve_ode_ivp on y''' = 6, y(-0.5) = y'(-0.5) = y''(-0.5) = 0, whose exact solution is (x + 0.5)**3:

import numpy as np
from grid.ode import solve_ode_ivp
from grid.rtransform import HandyModRTransform

xs = np.array([0.0, 0.25, 0.5])
for m in [2, 3, 4]:
    tf = HandyModRTransform(0.1, 10.0, m)
    sol = solve_ode_ivp((-0.5, 0.5), lambda x: 6 * np.ones_like(x), [0, 0, 0, 1],
                        [0.0, 0.0, 0.0], transform=tf, rtol=1e-10, atol=1e-10)
    print(m, sol(xs)[0], (xs + 0.5) ** 3)

On master:

2 [0.125    0.421875 1.      ] [0.125    0.421875 1.      ]
3 [0.12976934 0.45091849 1.10383533] [0.125    0.421875 1.      ]
4 [0.14209262 0.53001896 1.41016422] [0.125    0.421875 1.      ]

Comparing deriv3 directly against the symbolic third derivative (sympy) of transform for rmin=0.1, rmax=10, the relative error is ~1e-15 for m = 1, 2 and 0.75, 0.87, 0.94 for m = 3, 4, 5.

The fix is a one-line change (2 * two_m to two_m**2) plus a test that checks deriv3 against finite differences for m = 1 to 5. I have a patch ready if that's fine.

Dominant language
Python
Stars
102
Forks
43
Avg merge
6d 6h
Merged PRs (30d)
5

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from theochem/grid

All issues in theochem/grid

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.