11"""
2- Hamiltonian functions for classical and quantum mechanics.
2+ Hamiltonian functions for classical and quantum mechanics.
33
44This module provides two educational, minimal implementations:
55
6- - classical_hamiltonian(mass, momentum, potential_energy): Computes H = T + V for a particle/system, where
7- T = p^2 / (2 m) is the kinetic energy expressed in terms of momentum p, and V is
8- the potential energy (can be a scalar or an array broadcastable to p).
6+ - ham_c(mass, momentum, potential_energy):
7+ Computes H = T + V, where T = p^2/(2m), and V is the potential (scalar or array).
98
10- - quantum_hamiltonian_1d (mass, hbar, potential_energy, dx): Builds the 1D Hamiltonian matrix for a
11- particle in a potential V using second-order central finite differences for the
12- kinetic energy operator: T = - (hbar^2 / 2m) d^2/dx^2 with Dirichlet boundaries.
9+ - ham_1d (mass, hbar, potential_energy, dx):
10+ Builds the 1D Hamiltonian using second-order central differences for the kinetic
11+ operator: T = - (hbar^2 / 2m) d^2/dx^2 with Dirichlet boundaries.
1312
14- These functions are intended for learners to quickly prototype and simulate basic
15- physical systems.
13+ These functions help learners quickly prototype and simulate basic physical systems.
1614
1715References
1816----------
19- - Classical Hamiltonian mechanics: https://en.wikipedia.org/wiki/Hamiltonian_mechanics
20- - Discrete 1D Schrödinger operator: https://en.wikipedia.org/wiki/Finite_difference_method
17+ - Classical Hamiltonian mechanics:
18+ https://en.wikipedia.org/wiki/Hamiltonian_mechanics
19+ - Discrete 1D Schrödinger operator:
20+ https://en.wikipedia.org/wiki/Finite_difference_method
2121"""
2222
23- from __future__ import annotations
24-
2523from typing import Any
2624
2725import numpy as np
2826
2927
30- def classical_hamiltonian (mass : float , momentum : Any , potential_energy : Any ) -> Any :
28+ def ham_c (
29+ mass : float ,
30+ momentum : Any ,
31+ potential_energy : Any ,
32+ ) -> Any :
3133 """
3234 Classical Hamiltonian H = T + V with T = p^2 / (2 m).
3335
3436 The function supports scalars or array-like inputs for momentum ``p`` and
35- potential energy ``v``; NumPy broadcasting rules apply. If inputs are scalars,
37+ potential energy ``v``. NumPy broadcasting rules apply. If inputs are scalars,
3638 a float is returned; otherwise a NumPy array is returned.
3739
3840 Parameters
@@ -47,19 +49,19 @@ def classical_hamiltonian(mass: float, momentum: Any, potential_energy: Any) ->
4749 Returns
4850 -------
4951 float | np.ndarray
50- The Hamiltonian value(s) H = p^2/(2m) + V.
52+ The Hamiltonian value(s) H = p^2/(2m) + V.
5153
5254 Examples
5355 --------
5456 Free particle with p = 3 kg·m/s and m = 2 kg (v = 0):
55- >>> classical_hamiltonian (2.0, 3.0, 0.0)
57+ >>> ham_c (2.0, 3.0, 0.0)
5658 2.25
5759
5860 Harmonic oscillator snapshot with vectorized p and v:
5961 >>> mass = 1.0
6062 >>> momentum = np.array([0.0, 1.0, 2.0])
61- >>> potential_energy = np.array([0.5, 0.5, 0.5]) # e.g., 1/2 k x^2 at three positions
62- >>> classical_hamiltonian (mass, momentum, potential_energy).tolist()
63+ >>> potential_energy = np.array([0.5, 0.5, 0.5]) # e.g., 1/2 k x^2 at positions
64+ >>> ham_c (mass, momentum, potential_energy).tolist()
6365 [0.5, 1.0, 2.5]
6466 """
6567 if mass <= 0 :
@@ -77,8 +79,12 @@ def classical_hamiltonian(mass: float, momentum: Any, potential_energy: Any) ->
7779 return h
7880
7981
80- def quantum_hamiltonian_1d (
81- mass : float , hbar : float , potential_energy : Any , dx : float
82+
83+ def ham_1d (
84+ mass : float ,
85+ hbar : float ,
86+ potential_energy : Any ,
87+ dx : float ,
8288) -> np .ndarray :
8389 """
8490 Construct the 1D quantum Hamiltonian matrix using finite differences.
@@ -89,7 +95,7 @@ def quantum_hamiltonian_1d(
8995 H = - (hbar^2 / 2m) d^2/dx^2 + V
9096
9197 On a uniform grid with spacing ``dx`` and N sites, the Laplacian is
92- approximated by the tridiagonal matrix with main diagonal ``-2`` and
98+ approximated by a tridiagonal matrix with main diagonal ``-2`` and
9399 off-diagonals ``+1``. The resulting kinetic term has main diagonal
94100 ``(hbar^2)/(m*dx^2)`` and off-diagonals ``-(hbar^2)/(2*m*dx^2)``.
95101
@@ -111,9 +117,12 @@ def quantum_hamiltonian_1d(
111117
112118 Examples
113119 --------
114- Free particle (v=0) on a small grid: main diagonal = 1/dx^2, off = -1/(2*dx^2) in units m=hbar=1.
120+ Free particle (v=0) on a small grid: main diagonal = 1/dx^2, off = -1/(2*dx^2)
121+ in units m=hbar=1.
115122 >>> n, dx = 5, 0.1
116- >>> h = quantum_hamiltonian_1d(mass=1.0, hbar=1.0, potential_energy=np.zeros(n), dx=dx)
123+ >>> h = ham_1d(
124+ ... mass=1.0, hbar=1.0, potential_energy=np.zeros(n), dx=dx
125+ ... )
117126 >>> float(h[0, 0])
118127 99.99999999999999
119128 >>> float(h[0, 1])
@@ -122,7 +131,7 @@ def quantum_hamiltonian_1d(
122131 Add a harmonic-like site potential to the diagonal:
123132 >>> x = dx * (np.arange(n) - (n-1)/2)
124133 >>> potential_energy = 0.5 * x**2 # k=m=omega=1 for illustration
125- >>> h2 = quantum_hamiltonian_1d (1.0, 1.0, potential_energy, dx)
134+ >>> h2 = ham_1d (1.0, 1.0, potential_energy, dx)
126135 >>> np.allclose(np.diag(h2) - np.diag(h), potential_energy)
127136 True
128137 """
@@ -158,3 +167,7 @@ def quantum_hamiltonian_1d(
158167 import doctest
159168
160169 doctest .testmod (verbose = True )
170+
171+ # Backward-compatible aliases
172+ classical_hamiltonian = ham_c
173+ quantum_hamiltonian_1d = ham_1d
0 commit comments