// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute // SPDX-License-Identifier: GPL-3.0-only #pragma once #include #include #include #include // Acceleration of a fixed-point iteration x -> F(x) that converges linearly. Each round hands in the // state x it started from and F(x), where one plain round took it; the residual r = F(x) - x is what // the plain iteration would step. The state the next round starts from is F(x) corrected by the // combination of the last few rounds' steps that best cancels r, so the slow directions the plain // iteration creeps along - a handful of them, whose contraction is close to one - are taken in a few // rounds. Where r = 0 the correction is 0: the fixed point is the plain iteration's. // // Each component carries a weight in the norm r is measured in; a component of weight 0 takes F(x) // unchanged and is not part of the fit. The history is dropped, and the round is a plain one, when // the set of components of non-zero weight changed, since the differences of the history are then // differences of different things. // // Acceleration is for a map that contracts. A residual that grows from one round to the next says the // iteration is not on its way to a fixed point here - it walks, or the map itself moved - and // extrapolating from its history takes it further along the walk. From that round on every round is // a plain one. class AndersonMixing { public: explicit AndersonMixing(int depth) : depth(depth) {} std::vector Next(const std::vector &x, const std::vector &fx, const std::vector &w); int Restarts() const { return restarts; } bool Stopped() const { return stopped; } private: int depth; int restarts = 0; bool stopped = false; std::deque dx, dr; // x and r differences of consecutive rounds, newest last Eigen::VectorXd x_last, r_last; std::vector support_last; double r_norm_last = 0.0; };