Skip to content

Add hamiltonian functions - #15012

Merged
cclauss merged 2 commits into
TheAlgorithms:masterfrom
TejaashriUK:add-hamiltonian-functions
Sep 10, 2026
Merged

cclauss merged 2 commits into
TheAlgorithms:masterfrom
TejaashriUK:add-hamiltonian-functions

Conversation

@TejaashriUK

Copy link
Copy Markdown
Contributor

Fixes #13235

Describe your change:

  • Add an algorithm?

This PR adds physics/hamiltonian.py, which implements two functions requested in #13235:

  1. classical_hamiltonian(mass, velocity, potential_energy) — computes the classical Hamiltonian H = T + V, where kinetic energy T = 0.5 * m * v^2.
  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:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

@algorithms-keeper algorithms-keeper Bot added the awaiting reviews This PR is ready to be reviewed label Aug 8, 2026

@daltino daltino left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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 priya-sundaram-dev left a comment

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.

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. 👍

@algorithms-keeper algorithms-keeper Bot removed the awaiting reviews This PR is ready to be reviewed label Sep 10, 2026
@cclauss
cclauss merged commit aa1855f into TheAlgorithms:master Sep 10, 2026
5 checks passed
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.

Add Classical and Quantum Hamiltonian functions to physics algorithms

4 participants