PL · Polski
English edition · free course materials

Komputerowe rozwiązywanie problemów matematycznych

Python, SymPy and SageMath in a mathematician's work: floating-point arithmetic, numerical methods, symbolic computation, linear algebra and visualization — and the question of what we actually know after a computation.

numerics symbolic computation formal proof

Bartosz Naskręcki

Wydział Matematyki i Informatyki · Uniwersytet im. Adama Mickiewicza w Poznaniu

Python · SymPy · SageMath Peano Lab · Lean 4 translated from Polish
image: Newton basins of attraction for z³ − 1

Course book

The full text of the lectures with formulas and executed code (Jupyter Book). Every chapter can be downloaded as a notebook and run in VS Code.

Open the book →

Lecture slides

reveal.js presentations with diagrams and speaker notes. Available: Lecture 1 — How does a computer do mathematics?, Lecture 2 — Computer algebra and floating-point numbers

See the slides →

Interactive applets

Experiments in the browser: the anatomy of a floating-point number, an expression tree, patterns that break, the three worlds of Gauss's sum.

Explore the applets →

Labs

Problem sets to work through at the computer (Jupyter notebooks). Lab 1: VS Code, Jupyter and Python; Lab 2: SymPy and SageMath; Lab 3: floating-point arithmetic (IEEE 754).

Problem sets →

Peano Lab

A small theorem prover for Heyting arithmetic in the browser: a dozen or so tactics and an independent kernel that checks every proof.

Open Peano Lab ↗

The formal course (in Polish)

The course is taught in Polish at Wydział Matematyki i Informatyki UAM. Schedule, rules and syllabus are only in the Polish original.

Strona kursu →

Three ways a computer “does mathematics”

The guiding idea of the course — from the first lecture on, we compare them on the same examples.

Numerics

Machine numbers (float), fast and approximate. Physics, statistics, plots — but rounding errors can surprise you.

0.1 + 0.2 → 0.30000000000000004

Symbolic computation (CAS)

Exact expressions transformed by algorithms: SymPy, SageMath. The result is exact, but we trust the implementation.

summation(k, (k, 1, n)) → n*(n + 1)/2

Formal proof

A theorem and a proof checked by a small kernel: Peano Lab, Lean. A guarantee for all cases.

∀ n ∃ x. n·(n+1) = 2·x — QED

Deep dives

Topics that go beyond the lectures — with complete proofs, definitions, references and problems for independent work.

Richardson's theorem: why a computer cannot recognize every zero

For expressions built with $\sin$, $\exp$, $\pi$ and the absolute value there is no algorithm that decides whether an expression is identically zero. The proof combines Hilbert's tenth problem, a “gap” that turns approximate zeros into exact ones, and the curve $(t\sin t,\ t\sin t^3)$, whose image is dense in the plane.

Sketch and complete proof · preliminaries · questions and problems · applet · everything as a printable PDF

IEEE 754: floating-point numbers from the inside

What the standard promises and what follows from it in practice: a single rule of correct rounding, signed zero, subnormal numbers, the exact error of addition, the fast inverse square root from Quake III, a compiler that makes $\sqrt{a\cdot a-a\cdot a}$ produce NaN, and a sequence that tends to $100$ instead of $6$.

Overview and map of examples · part 1: the standard · part 2: implementation · exercises (Lab 3) · applet

Pythonic laziness: good practice and fast code

The same result is computed by programs whose running times differ by several orders of magnitude. A formula instead of a loop, a ready-made function instead of your own, NumPy, SciPy, SymPy and the Numba compiler on examples from the lectures and labs, with running times measured at every build of the site. The compiler finds the formula for a sum by itself, threads do not speed up pure Python, and a better algorithm on one core beats a worse one on all of them.

Overview and map of examples · part 1: how to measure time · cheat sheet · part 9: compilation and parallel computing

The circle group: purely symbolic proofs

The circle $a^2+b^2=1$ with multiplication of points is a group, and every axiom is checked by a symbolic computation that ends with a certificate. Powers of a single point give the Chebyshev polynomials and their composition $T_m\circ T_n=T_{mn}$, points of finite order give minimal polynomials and Gauss's 17-gon, and the Lie algebra with dual numbers gives Machin's formula for $\pi$ and Frobenius in characteristic $p$.

Overview and map of examples · part 1: axioms and certificates · part 3: the 17-gon · part 5: the Lie group (advanced) · applet

The course at WMI UAM (in Polish)

The formal course is taught in Polish. Schedule, rooms, groups, assessment and the syllabus exist only in the Polish originals:

Literature and links

Prerequisites: basic programming.

Literature

  • P. Zimmermann et al., Computational Mathematics with SageMath, SIAM 2018 (CC BY-SA) — PDF
  • J. von zur Gathen, J. Gerhard, Modern Computer Algebra, 3rd ed., Cambridge University Press 2013
  • D. E. Knuth, The Art of Computer Programming — author's page

Links

About the header image

This illustration is the result of a computation, not a drawing.

Every pixel is a starting point $z_0$ of Newton's method for the polynomial $p(z) = z^3 - 1$ in the complex plane: z ← z − (z³ − 1)/(3z²). The color tells which of the three cube roots of unity the iteration converged to, and the brightness how long it took: fast convergence is dark, while the boundaries of the basins glow — there the method “hesitates” the longest. The boundary is a fractal: at each of its points all three basins meet. The three colors are the three worlds of the course: numerics, symbolic computation and formal proof. Newton's method and fractals come up later in the course; the image was generated by a Python script (NumPy, SciPy, Pillow).