Skip to content

Commit 1465a1a

Browse files
committed
update
1 parent 7372169 commit 1465a1a

1 file changed

Lines changed: 49 additions & 49 deletions

File tree

physics/hamiltonian.py

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
44
This module provides two educational, minimal implementations:
55
6-
- classical_hamiltonian(m, p, v): 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
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
88
the potential energy (can be a scalar or an array broadcastable to p).
99
10-
- quantum_hamiltonian_1d(m, hbar, v, dx): Builds the 1D Hamiltonian matrix for a
11-
particle in a potential v using second-order central finite differences for the
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
1212
kinetic energy operator: T = - (hbar^2 / 2m) d^2/dx^2 with Dirichlet boundaries.
1313
1414
These functions are intended for learners to quickly prototype and simulate basic
@@ -27,27 +27,27 @@
2727
import numpy as np
2828

2929

30-
def classical_hamiltonian(m: float, p: Any, v: Any) -> Any:
30+
def classical_hamiltonian(mass: float, momentum: Any, potential_energy: Any) -> Any:
3131
"""
32-
Classical Hamiltonian H = T + v with T = p^2 / (2 m).
32+
Classical Hamiltonian H = T + V with T = p^2 / (2 m).
3333
3434
The function supports scalars or array-like inputs for momentum ``p`` and
3535
potential energy ``v``; NumPy broadcasting rules apply. If inputs are scalars,
3636
a float is returned; otherwise a NumPy array is returned.
3737
3838
Parameters
3939
----------
40-
m : float
40+
mass : float
4141
Mass (must be positive).
42-
p : array-like or scalar
42+
momentum : array-like or scalar
4343
Canonical momentum.
44-
v : array-like or scalar
44+
potential_energy : array-like or scalar
4545
Potential energy evaluated for the corresponding configuration.
4646
4747
Returns
4848
-------
4949
float | np.ndarray
50-
The Hamiltonian value(s) H = p^2/(2m) + v.
50+
The Hamiltonian value(s) H = p^2/(2m) + V.
5151
5252
Examples
5353
--------
@@ -56,35 +56,35 @@ def classical_hamiltonian(m: float, p: Any, v: Any) -> Any:
5656
2.25
5757
5858
Harmonic oscillator snapshot with vectorized p and v:
59-
>>> m = 1.0
60-
>>> p = np.array([0.0, 1.0, 2.0])
61-
>>> v = np.array([0.5, 0.5, 0.5]) # e.g., 1/2 k x^2 at three positions
62-
>>> classical_hamiltonian(m, p, v).tolist()
59+
>>> mass = 1.0
60+
>>> 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()
6363
[0.5, 1.0, 2.5]
6464
"""
65-
if m <= 0:
65+
if mass <= 0:
6666
raise ValueError("Mass m must be positive.")
6767

68-
p_arr = np.asarray(p)
69-
v_arr = np.asarray(v)
68+
p = np.asarray(momentum)
69+
v = np.asarray(potential_energy)
7070

71-
T = (p_arr * p_arr) / (2.0 * m)
72-
H = T + v_arr
71+
t = (p * p) / (2.0 * mass)
72+
h = t + v
7373

7474
# Preserve scalar type when both inputs are scalar
75-
if np.isscalar(p) and np.isscalar(v):
76-
return float(H)
77-
return H
75+
if np.isscalar(momentum) and np.isscalar(potential_energy):
76+
return float(h)
77+
return h
7878

7979

80-
def quantum_hamiltonian_1d(m: float, hbar: float, v: Any, dx: float) -> np.ndarray:
80+
def quantum_hamiltonian_1d(mass: float, hbar: float, potential_energy: Any, dx: float) -> np.ndarray:
8181
"""
8282
Construct the 1D quantum Hamiltonian matrix using finite differences.
8383
8484
Discretizes the kinetic operator with second-order central differences and
8585
Dirichlet boundary conditions (wavefunction assumed zero beyond endpoints):
8686
87-
H = - (hbar^2 / 2m) d^2/dx^2 + v
87+
H = - (hbar^2 / 2m) d^2/dx^2 + V
8888
8989
On a uniform grid with spacing ``dx`` and N sites, the Laplacian is
9090
approximated by the tridiagonal matrix with main diagonal ``-2`` and
@@ -93,63 +93,63 @@ def quantum_hamiltonian_1d(m: float, hbar: float, v: Any, dx: float) -> np.ndarr
9393
9494
Parameters
9595
----------
96-
m : float
96+
mass : float
9797
Particle mass (must be positive).
9898
hbar : float
9999
Reduced Planck constant (can be set to 1.0 in natural units).
100-
v : array-like shape (N,)
100+
potential_energy : array-like shape (N,)
101101
Potential energy values on the grid. Defines the matrix size.
102102
dx : float
103103
Grid spacing (must be positive).
104104
105105
Returns
106106
-------
107-
np.ndarray shape (N, N)
107+
np.ndarray shape (n, n)
108108
The Hermitian Hamiltonian matrix.
109109
110110
Examples
111111
--------
112112
Free particle (v=0) on a small grid: main diagonal = 1/dx^2, off = -1/(2*dx^2) in units m=hbar=1.
113-
>>> N, dx = 5, 0.1
114-
>>> H = quantum_hamiltonian_1d(m=1.0, hbar=1.0, v=np.zeros(N), dx=dx)
115-
>>> float(H[0, 0])
113+
>>> n, dx = 5, 0.1
114+
>>> h = quantum_hamiltonian_1d(mass=1.0, hbar=1.0, potential_energy=np.zeros(n), dx=dx)
115+
>>> float(h[0, 0])
116116
99.99999999999999
117-
>>> float(H[0, 1])
117+
>>> float(h[0, 1])
118118
-49.99999999999999
119119
120120
Add a harmonic-like site potential to the diagonal:
121-
>>> x = dx * (np.arange(N) - (N-1)/2)
122-
>>> v = 0.5 * x**2 # k=m=omega=1 for illustration
123-
>>> H2 = quantum_hamiltonian_1d(1.0, 1.0, v, dx)
124-
>>> np.allclose(np.diag(H2) - np.diag(H), v)
121+
>>> x = dx * (np.arange(n) - (n-1)/2)
122+
>>> potential_energy = 0.5 * x**2 # k=m=omega=1 for illustration
123+
>>> h2 = quantum_hamiltonian_1d(1.0, 1.0, potential_energy, dx)
124+
>>> np.allclose(np.diag(h2) - np.diag(h), potential_energy)
125125
True
126126
"""
127-
if m <= 0:
127+
if mass <= 0:
128128
raise ValueError("Mass m must be positive.")
129129
if dx <= 0:
130130
raise ValueError("Grid spacing dx must be positive.")
131131

132-
v_arr = np.asarray(v, dtype=float)
133-
if v_arr.ndim != 1:
132+
v = np.asarray(potential_energy, dtype=float)
133+
if v.ndim != 1:
134134
raise ValueError("v must be a 1D array-like of potential values.")
135135

136-
N = v_arr.size
137-
if N == 0:
136+
n = v.size
137+
if n == 0:
138138
raise ValueError("v must contain at least one grid point.")
139139

140-
coeff_main = (hbar * hbar) / (m * dx * dx)
141-
coeff_off = -0.5 * (hbar * hbar) / (m * dx * dx)
140+
coeff_main = (hbar * hbar) / (mass * dx * dx)
141+
coeff_off = -0.5 * (hbar * hbar) / (mass * dx * dx)
142142

143143
# Build tridiagonal kinetic matrix
144-
H = np.zeros((N, N), dtype=float)
145-
np.fill_diagonal(H, coeff_main)
146-
idx = np.arange(N - 1)
147-
H[idx, idx + 1] = coeff_off
148-
H[idx + 1, idx] = coeff_off
144+
h = np.zeros((n, n), dtype=float)
145+
np.fill_diagonal(h, coeff_main)
146+
idx = np.arange(n - 1)
147+
h[idx, idx + 1] = coeff_off
148+
h[idx + 1, idx] = coeff_off
149149

150150
# Add the potential on the diagonal
151-
H[np.arange(N), np.arange(N)] += v_arr
152-
return H
151+
h[np.arange(n), np.arange(n)] += v
152+
return h
153153

154154

155155
if __name__ == "__main__":

0 commit comments

Comments
 (0)