Skip to content

Fix gradient length mismatch in PhasePoint #426

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/hamiltonian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ struct PhasePoint{T<:AbstractVecOrMat{<:AbstractFloat},V<:DualValue}
θ::T # Position variables / model parameters.
r::T # Momentum variables
ℓπ::V # Cached neg potential energy for the current θ.
ℓκ::V # Cached neg kinect energy for the current r.
ℓκ::V # Cached neg kinetic energy for the current r.
function PhasePoint::T, r::T, ℓπ::V, ℓκ::V) where {T,V}
@argcheck length(θ) == length(r) == length(ℓπ.gradient) == length(ℓπ.gradient)
@argcheck length(θ) == length(r) == length(ℓπ.gradient) == length(ℓκ.gradient)
if !isfinite(ℓπ)
ℓπ = DualValue(
map(v -> isfinite(v) ? v : oftype(v, -Inf), ℓπ.value), ℓπ.gradient
Expand Down
8 changes: 8 additions & 0 deletions test/hamiltonian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ end

@test z1.ℓπ.value == z2.ℓπ.value
@test z1.ℓκ.value == z2.ℓκ.value

# Test gradient length mismatch of neg potential and kinetic energy in PhasePoint
@test_throws ArgumentError PhasePoint(
[T(Inf)],
[T(Inf)],
DualValue(zero(T), [zero(T)]),
DualValue(zero(T), zeros(T, 2)),
)
end
end

Expand Down
Loading