Skip to content

Commit 7372169

Browse files
committed
fix case
1 parent dd51a49 commit 7372169

1 file changed

Lines changed: 30 additions & 30 deletions

File tree

physics/hamiltonian.py

Lines changed: 30 additions & 30 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(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
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(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
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,12 +27,12 @@
2727
import numpy as np
2828

2929

30-
def classical_hamiltonian(m: float, p: Any, V: Any) -> Any:
30+
def classical_hamiltonian(m: float, p: Any, v: 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
35-
potential energy ``V``; NumPy broadcasting rules apply. If inputs are scalars,
35+
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
@@ -41,50 +41,50 @@ def classical_hamiltonian(m: float, p: Any, V: Any) -> Any:
4141
Mass (must be positive).
4242
p : array-like or scalar
4343
Canonical momentum.
44-
V : array-like or scalar
44+
v : 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
--------
54-
Free particle with p = 3 kg·m/s and m = 2 kg (V = 0):
54+
Free particle with p = 3 kg·m/s and m = 2 kg (v = 0):
5555
>>> classical_hamiltonian(2.0, 3.0, 0.0)
5656
2.25
5757
58-
Harmonic oscillator snapshot with vectorized p and V:
58+
Harmonic oscillator snapshot with vectorized p and v:
5959
>>> m = 1.0
6060
>>> 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()
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()
6363
[0.5, 1.0, 2.5]
6464
"""
6565
if m <= 0:
6666
raise ValueError("Mass m must be positive.")
6767

6868
p_arr = np.asarray(p)
69-
V_arr = np.asarray(V)
69+
v_arr = np.asarray(v)
7070

7171
T = (p_arr * p_arr) / (2.0 * m)
72-
H = T + V_arr
72+
H = T + v_arr
7373

7474
# Preserve scalar type when both inputs are scalar
75-
if np.isscalar(p) and np.isscalar(V):
75+
if np.isscalar(p) and np.isscalar(v):
7676
return float(H)
7777
return H
7878

7979

80-
def quantum_hamiltonian_1d(m: float, hbar: float, V: Any, dx: float) -> np.ndarray:
80+
def quantum_hamiltonian_1d(m: float, hbar: float, v: 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
@@ -97,7 +97,7 @@ def quantum_hamiltonian_1d(m: float, hbar: float, V: Any, dx: float) -> np.ndarr
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+
v : array-like shape (N,)
101101
Potential energy values on the grid. Defines the matrix size.
102102
dx : float
103103
Grid spacing (must be positive).
@@ -109,33 +109,33 @@ def quantum_hamiltonian_1d(m: float, hbar: float, V: Any, dx: float) -> np.ndarr
109109
110110
Examples
111111
--------
112-
Free particle (V=0) on a small grid: main diagonal = 1/dx^2, off = -1/(2*dx^2) in units m=hbar=1.
112+
Free particle (v=0) on a small grid: main diagonal = 1/dx^2, off = -1/(2*dx^2) in units m=hbar=1.
113113
>>> N, dx = 5, 0.1
114-
>>> H = quantum_hamiltonian_1d(m=1.0, hbar=1.0, V=np.zeros(N), dx=dx)
114+
>>> H = quantum_hamiltonian_1d(m=1.0, hbar=1.0, v=np.zeros(N), dx=dx)
115115
>>> float(H[0, 0])
116116
99.99999999999999
117117
>>> float(H[0, 1])
118118
-49.99999999999999
119119
120120
Add a harmonic-like site potential to the diagonal:
121121
>>> 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)
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)
125125
True
126126
"""
127127
if m <= 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:
134-
raise ValueError("V must be a 1D array-like of potential values.")
132+
v_arr = np.asarray(v, dtype=float)
133+
if v_arr.ndim != 1:
134+
raise ValueError("v must be a 1D array-like of potential values.")
135135

136-
N = V_arr.size
136+
N = v_arr.size
137137
if N == 0:
138-
raise ValueError("V must contain at least one grid point.")
138+
raise ValueError("v must contain at least one grid point.")
139139

140140
coeff_main = (hbar * hbar) / (m * dx * dx)
141141
coeff_off = -0.5 * (hbar * hbar) / (m * dx * dx)
@@ -148,7 +148,7 @@ def quantum_hamiltonian_1d(m: float, hbar: float, V: Any, dx: float) -> np.ndarr
148148
H[idx + 1, idx] = coeff_off
149149

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

154154

0 commit comments

Comments
 (0)