diff options
author | Jaron Kent-Dobias <jaron@kent-dobias.com> | 2024-12-26 11:34:59 +0100 |
---|---|---|
committer | Jaron Kent-Dobias <jaron@kent-dobias.com> | 2024-12-26 11:34:59 +0100 |
commit | 07c5fee22dd83f791b97567df301cacc3f147d23 (patch) | |
tree | 35d613cd6fbc27d809bc9b70a96b5938c9044c56 | |
parent | 2769a7233886c8f77bf51e29feb9b14f7e207489 (diff) | |
download | code-07c5fee22dd83f791b97567df301cacc3f147d23.tar.gz code-07c5fee22dd83f791b97567df301cacc3f147d23.tar.bz2 code-07c5fee22dd83f791b97567df301cacc3f147d23.zip |
Another small refactor.
-rw-r--r-- | walk.cpp | 11 |
1 files changed, 7 insertions, 4 deletions
@@ -24,6 +24,10 @@ Vector randomVector(unsigned N, Rng& r, Real σ = 1) { return v; } +Vector projectionOfOn(const Vector& v, const Vector& u) { + return (v.dot(u) / u.squaredNorm()) * u; +} + Real wignerCDF(Real λ) { return 0.5 + (λ * sqrt(4 - pow(λ, 2)) / 4 + atan(λ / sqrt(4 - pow(λ, 2)))) / M_PI; } @@ -63,7 +67,7 @@ public: Vector ∇H(const Vector& x) const { Vector ∂H = J.cwiseProduct(x); - return ∂H - (∂H.dot(x) / x.squaredNorm()) * x; + return ∂H - projectionOfOn(∂H, x); } }; @@ -98,9 +102,8 @@ Vector gradientDescent(const QuadraticModel& M, const Vector& x₀, Real E, Real Vector randomStep(const QuadraticModel& M, const Vector& x₀, Real E, Rng& r, Real Δt = 1e-4) { Vector η = randomVector(M.N, r, sqrt(2 * Δt)); - η -= (η.dot(x₀) / x₀.squaredNorm()) * x₀; - Vector ∇H₀ = M.∇H(x₀); - η -= (η.dot(∇H₀) / ∇H₀.squaredNorm()) * ∇H₀; + η -= projectionOfOn(η, x₀); + η -= projectionOfOn(η, M.∇H(x₀)); return gradientDescent(M, normalizeVector(x₀ + η), E); } |