Repository navigation
Add hamiltonian functions - #15012
Conversation
daltino
left a comment
There was a problem hiding this comment.
Looks like a solid addition overall — Hamiltonian mechanics is a meaningful contribution and the docstring explanation of both classical and quantum cases is a nice touch. Would love to see the full patch to verify, but make sure there are doctest examples in every function (required by the repo's guidelines) and that numpy is already a listed dependency. Also double-check that there's no overlap with any existing physics modules in the repo.
priya-sundaram-dev
left a comment
There was a problem hiding this comment.
Reviewed. I checked all four doctests by hand and the physics is sound: the finite-difference form gives a tridiagonal matrix with diagonal ħ²/(m·dx²) + V(x_i) and off-diagonals -ħ²/(2m·dx²), which matches both the docstring and the numbers in the examples. classical_hamiltonian(2, 3, 5) = 0.5·2·9 + 5 = 14.0 ✅, and the 2×2/3×3 quantum examples check out. CI is green, references are good.
One optional nit: num_points duplicates len(potential), so you could derive it and drop the parameter — but keeping it as an explicit validated arg is also a defensible choice, so no change required. LGTM. 👍
Fixes #13235
Describe your change:
This PR adds
physics/hamiltonian.py, which implements two functions requested in #13235:classical_hamiltonian(mass, velocity, potential_energy)— computes the classical HamiltonianH = T + V, where kinetic energyT = 0.5 * m * v^2.quantum_hamiltonian(num_points, potential, mass, hbar, dx)— constructs the Hamiltonian matrix for a particle in a 1D potential using finite-difference discretization of the time-independent Schrödinger equation.Checklist: