From e6b4d83097f4442edf2290236e1b092724d8fd74 Mon Sep 17 00:00:00 2001 From: Jaron Kent-Dobias Date: Tue, 12 Jan 2021 17:39:27 +0100 Subject: Changed code to now find nearby saddles and perturb J. --- tensor.hpp | 64 ++++++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 50 insertions(+), 14 deletions(-) (limited to 'tensor.hpp') diff --git a/tensor.hpp b/tensor.hpp index 528a51a..05fec36 100644 --- a/tensor.hpp +++ b/tensor.hpp @@ -1,27 +1,53 @@ #pragma once #include +#include #include +#include #include "factorial.hpp" template -Eigen::Tensor initializeJ(unsigned N, std::index_sequence) { +Eigen::Tensor initializeJHelper(unsigned N, std::index_sequence) { std::array Ns; std::fill_n(Ns.begin(), p, N); return Eigen::Tensor(std::get(Ns)...); } -template -void populateCouplings(Eigen::Tensor& J, unsigned N, unsigned l, - std::array is, Distribution d, Generator& r, - std::index_sequence ii) { +template +Eigen::Tensor initializeJ(unsigned N) { + return initializeJHelper(N, std::make_index_sequence

()); +} + +template +void setJHelper(Eigen::Tensor& J, const std::array& ind, Scalar val, std::index_sequence) { + J(std::get(ind)...) = val; +} + +template +void setJ(Eigen::Tensor& J, std::array ind, Scalar val) { + do { + setJHelper(J, ind, val, std::make_index_sequence

()); + } while (std::next_permutation(ind.begin(), ind.end())); +} + +template +Scalar getJHelper(const Eigen::Tensor& J, const std::array& ind, std::index_sequence) { + return J(std::get(ind)...); +} + +template +Scalar getJ(const Eigen::Tensor& J, const std::array& ind) { + return getJHelper(J, ind, std::make_index_sequence

()); +} + +template +void iterateOverHelper(Eigen::Tensor& J, + std::function&, std::array)>& f, + unsigned l, std::array is) { if (l == 0) { - Scalar z = d(r); - do { - J(std::get(is)...) = z; - } while (std::next_permutation(is.begin(), is.end())); + f(J, is); } else { unsigned iMin; if (l == p) { @@ -29,20 +55,30 @@ void populateCouplings(Eigen::Tensor& J, unsigned N, unsigned l, } else { iMin = is[p - l - 1]; } - for (unsigned i = iMin; i < N; i++) { + for (unsigned i = iMin; i < J.dimension(p - 1); i++) { std::array js = is; js[p - l] = i; - populateCouplings(J, N, l - 1, js, d, r, ii); + iterateOverHelper(J, f, l - 1, js); } } } +template +void iterateOver(Eigen::Tensor& J, std::function&, std::array)>& f) { + std::array is; + iterateOverHelper(J, f, p, is); +} + template Eigen::Tensor generateCouplings(unsigned N, Distribution d, Generator& r) { - Eigen::Tensor J = initializeJ(N, std::make_index_sequence

()); + Eigen::Tensor J = initializeJ(N); - std::array is; - populateCouplings(J, N, p, is, d, r, std::make_index_sequence

()); + std::function&, std::array)> setRandom = + [&d, &r] (Eigen::Tensor& JJ, std::array ind) { + setJ(JJ, ind, d(r)); + }; + + iterateOver(J, setRandom); return J; } -- cgit v1.2.3-54-g00ecf