Estava a "tentar" aprender METAFONT, o significado das aspas tornar-se-á claro em poucos instantes, e dei com o seguinte comentário escrito pelo próprio Knuth

Pythagorean addition latex2png equation is implemented by an elegant iterative scheme due to Cleve Moler and Donald Morrison IBM Journal of Research and Development 27 (1983), 577—581. It modifies |a| and |b| in such a way that their Pythagorean sum remains invariant, while the smaller argument decreases.

E, justifica-se as aspas, tive de descobrir o artigo (não encontrei outro sítio para o ler) e implementar o código em Lisp.

(defun pythag (a b &optional tol)
  "Pythagorean addition $\sqrt{a^2+b^2}$ is implemented by an elegant
iterative scheme due to Cleve Moler and Donald Morrison IBM Journal
of Research and Development 27 (1983), 577--581. It modifies |a| and |b|
in such a way that their Pythagorean sum remains invariant, while the
smaller argument decreases."
  (if (>= a b)
      (pythag b a tol)
      (if (<= a (if tol
                    tol
                    (expt 10.0 -16)))
          b
          (let* ((r (expt (/ a b) 2))
                 (s (/ r (+ 4 r)))
                 (b (+ b (* 2 s b)))
                 (a (* s a)))
            (pythag a b tol)))))
> (pythag 1.0 2.0 .000001)
2.236068
> (expt  5 .5)
2.236068

e com a operação inversa

(defun pythag (pm a b &optional tol)
  "Pythagorean addition $\sqrt{a^2\pm b^2}$ is implemented by an elegant
iterative scheme due to Cleve Moler and Donald Morrison IBM Journal
of Research and Development 27 (1983), 577--581. It modifies |a| and |b|
in such a way that their Pythagorean sum remains invariant, while the
smaller argument decreases. pm -> +1 gives sqrt(a^2+b^2); pm -> -1 gives sqrt(a^2-b^2);"
  (if (>= a b)
      (pythag pm b a tol)
      (if (<= a (if tol
                    tol
                    (expt 10.0 -16)))
          b
          (let* ((r (* pm (expt (/ a b) 2)))
                 (s (/ r (+ 4 r)))
                 (b (+ b (* 2 s b)))
                 (a (* s a)))
            (pythag pm a b tol)))))

Voltando ao METAFONT.

Created: NaN

Last updated: 23-01-2025 [00:04]


For attribution, please cite this page as:

Charters, T., "Trying to learn METAFONT (act.)": https://nexp.pt/pythag.html (23-01-2025 [00:04])


(cc-by-sa) Tiago Charters - tiagocharters@nexp.pt