summaryrefslogtreecommitdiff
path: root/langevin.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'langevin.cpp')
-rw-r--r--langevin.cpp19
1 files changed, 7 insertions, 12 deletions
diff --git a/langevin.cpp b/langevin.cpp
index 1b328b6..b67d358 100644
--- a/langevin.cpp
+++ b/langevin.cpp
@@ -17,7 +17,7 @@
using Rng = randutils::random_generator<pcg32>;
Vector normalize(const Vector& z) {
- return z * sqrt(z.size()) / sqrt((Scalar)(z.transpose() * z));
+ return z * sqrt((double)z.size() / (Scalar)(z.transpose() * z));
}
template <class Distribution, class Generator>
@@ -33,19 +33,14 @@ Vector randomVector(unsigned N, Distribution d, Generator& r) {
std::tuple<double, Vector> gradientDescent(const Tensor& J, const Vector& z0, double ε, double γ0 = 1, double δγ = 2) {
Vector z = z0;
-
- double W;
- Vector dW;
- std::tie(W, dW) = WdW(J, z);
-
double γ = γ0;
+ auto [W, dW] = WdW(J, z);
+
while (W > ε) {
Vector zNew = normalize(z - γ * dW.conjugate());
- double WNew;
- Vector dWNew;
- std::tie(WNew, dWNew) = WdW(J, zNew);
+ auto [WNew, dWNew] = WdW(J, zNew);
if (WNew < W) { // If the step lowered the objective, accept it!
z = zNew;
@@ -66,12 +61,12 @@ std::tuple<double, Vector> gradientDescent(const Tensor& J, const Vector& z0, do
}
Vector findSaddle(const Tensor& J, const Vector& z0, double ε, double δW = 2, double γ0 = 1, double δγ = 2) {
- double W;
- std::tie(W, std::ignore) = WdW(J, z0);
-
Vector z = z0;
Vector ζ = euclideanToStereographic(z);
+ double W;
+ std::tie(W, std::ignore) = WdW(J, z);
+
Vector dH;
Matrix ddH;
std::tie(std::ignore, dH, ddH) = stereographicHamGradHess(J, ζ, z);