From 12f15f49cd8cc4ab9c809700e8cb88a0efe198d8 Mon Sep 17 00:00:00 2001 From: Jaron Kent-Dobias Date: Wed, 17 Feb 2021 16:14:33 +0100 Subject: Rearranged some functions among files, and wrote the normalize function to take generic Eigen expressions. --- dynamics.hpp | 6 ++---- p-spin.hpp | 17 ++++++++--------- stokes.hpp | 16 ++++------------ 3 files changed, 14 insertions(+), 25 deletions(-) diff --git a/dynamics.hpp b/dynamics.hpp index 22d590a..d421d13 100644 --- a/dynamics.hpp +++ b/dynamics.hpp @@ -21,8 +21,7 @@ std::tuple> gradientDescent(const Tensor& J, c auto [W, dW] = WdW(J, z); while (W > ε) { - Vector zNewTmp = z - γ * dW.conjugate(); - Vector zNew = normalize(zNewTmp); + Vector zNew = normalize(z - γ * dW.conjugate()); auto [WNew, dWNew] = WdW(J, zNew); @@ -102,8 +101,7 @@ std::tuple> metropolis(const Tensor& J, const std::uniform_real_distribution D(0, 1); for (unsigned i = 0; i < N; i++) { - Vector zNewTmp = z + γ * randomVector(z.size(), d, r); - Vector zNew = normalize(zNewTmp); + Vector zNew = normalize(z + γ * randomVector(z.size(), d, r)); double ENew = energy(J, zNew); diff --git a/p-spin.hpp b/p-spin.hpp index 480b3ca..bd3cacc 100644 --- a/p-spin.hpp +++ b/p-spin.hpp @@ -5,6 +5,11 @@ #include "tensor.hpp" #include "factorial.hpp" +template +Vector normalize(const Eigen::MatrixBase& z) { + return z * sqrt((double)z.size() / (typename Derived::Scalar)(z.transpose() * z)); +} + template std::tuple, Matrix> hamGradHess(const Tensor& J, const Vector& z) { Matrix Jz = contractDown(J, z); // Contracts J into p - 2 copies of z. @@ -21,14 +26,8 @@ std::tuple, Matrix> hamGradHess(const Tensor -Vector normalize(const Vector& z) { - return z * sqrt((double)z.size() / (Scalar)(z.transpose() * z)); -} - -template -Vector project(const Vector& z, const Vector& x) { - Scalar xz = x.transpose() * z; - return x - (xz / z.squaredNorm()) * z.conjugate(); +Vector zDot(const Vector& z, const Vector& dH) { + return -dH.conjugate() + (dH.dot(z) / z.squaredNorm()) * z.conjugate(); } template @@ -37,7 +36,7 @@ std::tuple> WdW(const Tensor& J, const Vector< Matrix ddH; std::tie(std::ignore, dH, ddH) = hamGradHess(J, z); - Vector dzdt = project(z, dH.conjugate().eval()); + Vector dzdt = zDot(z, dH); double a = z.squaredNorm(); Scalar A = (Scalar)(z.transpose() * dzdt) / a; diff --git a/stokes.hpp b/stokes.hpp index 95bb9c4..117f4de 100644 --- a/stokes.hpp +++ b/stokes.hpp @@ -1,10 +1,5 @@ #include "p-spin.hpp" -template -Vector zDot(const Vector& z, const Vector& dH) { - return -dH.conjugate() + (dH.dot(z) / z.squaredNorm()) * z.conjugate(); -} - template double segmentCost(const Vector& z, const Vector& dz, const Vector& dH) { Vector zD = zDot(z, dH); @@ -68,8 +63,7 @@ class Rope { Rope(unsigned N, const Vector& z1, const Vector& z2) : z(N + 2) { for (unsigned i = 0; i < N + 2; i++) { - z[i] = z1 + (z2 - z1) * ((double)i / (N + 1.0)); - z[i] = normalize(z[i]); + z[i] = normalize(z1 + (z2 - z1) * ((double)i / (N + 1.0))); } } @@ -106,8 +100,7 @@ class Rope { while (rNew.cost(J) >= this->cost(J)) { for (unsigned i = 1; i < z.size() - 1; i++) { - rNew.z[i] = z[i] - δ * Δz[i].conjugate(); - rNew.z[i] = normalize(rNew.z[i]); + rNew.z[i] = normalize(z[i] - δ * Δz[i].conjugate()); } δ /= 2; @@ -144,8 +137,7 @@ class Rope { Vector δz = z[pos] - z[pos - 1]; - zNew[i] = z[pos] - (a - b) / δz.norm() * δz; - zNew[i] = normalize(zNew[i]); + zNew[i] = normalize(z[pos] - (a - b) / δz.norm() * δz); } z = zNew; @@ -192,7 +184,7 @@ class Rope { } for (unsigned i = 0; i < z.size() - 1; i++) { - r.z[2 * i + 1] = normalize(((z[i] + z[i + 1]) / 2.0).eval()); + r.z[2 * i + 1] = normalize(((z[i] + z[i + 1]) / 2.0)); } return r; -- cgit v1.2.3-70-g09d2