"""Algorithm 2 within 72 characters a line (it overflowed the page), same content; and the one character pdflatex does not know (U+2265 in the availability statement). python -u apply_listing_width.py """ import re PATH = "article/sim_numerics.md" NEW = """``` state: bracket [A,B], FA, FB; points x, xp, xp2; values fx, fp, fp2 memory: configs (configurations), moves (displacements) flags: last_model, in_regime, analogy_seen (with order m_a), shallow_seen (with order m_s) initialise as Algorithm 1; allow <- 2^n_max * xtol/2 while |B - A| > xtol: far <- end of [A,B] further from x; mid <- (A+B)/2; model <- false f' <- derivative at x of the parabola through (x, xp, xp2) sigma <- |f'| * |far - x| if sigma is 0 or not finite: xn <- mid else: z <- |fx| / sigma # the flat regime (6.1-6.3) c <- (xi, phi, |x-xp|, |fx-fp|), last three points, if defined # unkept promise, and unkept promise with a crossing flag <- not in_regime and last_model and fx/fp >= 1/4 flag_over <- not in_regime and last_model and fx/fp <= -1/4 # order (6) of the analogous pair: the past configuration # nearest to c (Euclidean in (xi, phi)), if nearer than the # line phi = xi; inf if the pair shows L_f < 1 <= L_x; else none m <- order of the analogous pair if m >= 2: analogy_seen <- true; m_a <- m if 0 < m <= 2/3: shallow_seen <- true; m_s <- m if flag and analogy_seen: in_regime <- true elif flag_over and shallow_seen: in_regime <- true; m_a <- m_s append c to configs if in_regime: xn <- split point (7) with m = m_a elif x != xp and sign((fx-fp)/(x-xp)) != sign((FB-FA)/(B-A)): xn <- mid elif S(z) >= 1/2 and |f(far)| < |fx|: xn <- mid else: step <- (1 - S(z)) * (far - x) # eq. (4) append x - xp to moves # step pattern: the lag 2 .. half the history of highest # mean similarity of successive length ratios has mean > 1/2, # and step and x - xp point the same way if the pattern repeats: s <- min(|step|,|x-xp|) / max(|step|,|x-xp|) step <- step / (1 - s), at most |far - x| long if |step| < xtol/2 and last_model and |fx/fp| < 1/4: step <- sign(far - x) * xtol/2 # tolerance step xn <- x + step; model <- true xn <- clamp(xn, hi - allow, lo + allow) # projection (3) if xn outside (A,B) or xn in {x, xp}: xn <- mid; if mid in {x, xp}: stop last_model <- model and xn is the model's own point fn <- f(xn) if in_regime: # exit t <- (xn - A)/(B - A) if |fn - (FA(1-t) + FB t)| < |fn|: in_regime <- false; analogy_seen <- false allow <- allow/2; (xp2, xp, x) <- (xp, x, xn) keep the sign change in [A,B] return midpoint(A,B) ```""" src = open(PATH, encoding="utf8").read() start = src.index("**Algorithm 2 (A′).**") a = src.index("```", start) b = src.index("```", a + 3) + 3 assert src.count("**Algorithm 2 (A′).**") == 1 src = src[:a] + NEW + src[b:] assert src.count("Python ≥ 3.10") == 1 src = src.replace("Python ≥ 3.10", "Python 3.10 or later") open(PATH, "w", encoding="utf8").write(src) print("longest listing line:", max(len(l) for l in NEW.split("\n"))) print("non-latin1 characters left outside math:", sorted({c for c in re.sub(r"\$[^$]*\$", "", src) if ord(c) > 0x2FFF}))