function [vtilde,gammatilde,iter] = APGAlg3(F,PAR,v)
%APGALG3 Adaptive proximal-gradient iteration from Algorithm 3 of arXiv:2206.01209.
%
%   [vtilde,gammatilde,iter] = APGAlg3(F,PAR,v)
%
% The smooth part is F.fs with gradient F.grad; the nonsmooth part P is
% accessed through F.prox(u,lambda) = prox_{lambda P}(u). The line search
% starts from PAR.gamma0 and decreases by PAR.delta until
%
%   2*gamma*(f(vtilde)-f(v)-<grad f(v),vtilde-v>) <= ||vtilde-v||^2.
%
% iter is the number of proximal evaluations.

iter = 0;

gammatilde = PAR.gamma0;
gradv = F.grad(v);
fv = F.fs(v);

vtilde = F.prox(v - gammatilde*gradv, gammatilde);
iter = iter + 1;

NormDiff = PAR.norm_fn(vtilde - v)^2;
LinearTerm = 2*gammatilde*(F.fs(vtilde) - fv ...
    - PAR.prod_fn(gradv, vtilde - v));
Ftest=NormDiff-LinearTerm;

while Ftest<0

    gammatilde = gammatilde * PAR.delta;

    vtilde = F.prox(v - gammatilde*gradv, gammatilde);
    iter = iter + 1;

    NormDiff = PAR.norm_fn(vtilde - v)^2;
    LinearTerm = 2*gammatilde*(F.fs(vtilde) - fv ...
        - PAR.prod_fn(gradv, vtilde - v));

    Ftest=NormDiff-LinearTerm;

end

end
