Komputerowe rozwiązywanie problemów matematycznych · lecture
Lecture 1: How does a computer do mathematics?
Working environment · computer algebra systems · numerics, symbolic computation and formal proof
numerics symbolic computation (CAS) formal proof
Dr. Bartosz Naskręcki · Wydział Matematyki i Informatyki UAM · 06-S1MA03-P16780 winter semester 2026/27 · course taught in Polish — dates and rooms: Harmonogram (in Polish)
Welcome; introduce yourself. Goal of the course: to learn to use the computer as a mathematician’s tool — for numerical experiments, for exact symbolic computation and (on the horizon) for checking proofs. Today: organization, the working environment, how a computer algebra system works, and how computation differs from verification and from proof. Three colors on the slides: amber = numerics, indigo = symbolic computation, green = formal proof.
Lecture outline
0
Organization
schedule, assessment, materials
5 min
I
Three worlds of computation
numerics · symbolic computation · proof
7 min
II
Working environment
VS Code, Python, SymPy, SageMath
18 min
III–VI
Continued
how a CAS works → Lecture 2 · the sum 1 + 2 + … + n in three worlds, Peano Lab, Lean → the book, chapter 4
→ Lecture 2 and the book
numerics
symbolic computation (CAS)
formal proof
Lecture 1 · outline
A plan for 90 minutes. First organization, then the “three worlds” of computation: numerics, symbolic computation and formal proof. Then practice: the working environment (VS Code, Python, SymPy, SageMath) and Python for mathematicians. The inside of a computer algebra system moved to Lecture 2 (together with Rump’s example, interval arithmetic and the pyramid of trust); the sum 1+2+…+n through all three worlds up to a proof in Peano Lab, and Lean as the “older brother”, are in chapter 4 of the book.
Organization
The course at WMI UAM
These slides were prepared for the course “Komputerowe rozwiązywanie problemów matematycznych”, taught in Polish at Wydział Matematyki i Informatyki UAM (Poznań) in the winter semester 2026/27. Dates, rooms, lab groups and all other formal course information are available only in Polish:
Materials: bnaskrecki.faculty.wmi.amu.edu.pl/math-comp/en — course book (Jupyter Book), slides, applets, lab problem sets.
Lecture 1 · organization
Strona kursu (in Polish) →
The course itself is taught in Polish at WMI UAM. Dates, rooms and lab groups are given only in the Polish originals: Strona kursu and Harmonogram. All materials of the English edition — the course book, slides, applets and lab problem sets — are at bnaskrecki.faculty.wmi.amu.edu.pl/math-comp/en.
Organization
Assessment
The requirements for passing the course and the grading scale follow the syllabus. They apply to the course taught at WMI UAM and are published only in Polish:
Lecture 1 · organization
Warunki zaliczenia (in Polish) →
The assessment rules for the lectures and the labs follow the syllabus and apply only to the course at WMI UAM. They are published only in Polish: in Warunki zaliczenia and in the syllabus itself.
Organization
What lies ahead: topics and literature
Course contents (syllabus)
The official list of topics is part of the syllabus, which is available only in Polish:
Literature
P. Zimmermann et al., Computational Mathematics with SageMath , SIAM 2018 (CC-BY-SA license, PDF available online)
J. von zur Gathen, J. Gerhard, Modern Computer Algebra , 3rd ed., Cambridge UP 2013
D. E. Knuth, The Art of Computer Programming
python.org · sympy.org · sagemath.org
Lecture: ideas and the “why”. Lab: hands on the keyboard — the “how”.
Lecture 1 · organization
Sylabus w skrócie (in Polish) →
The topics of the course follow the syllabus — see Sylabus w skrócie (in Polish). The main book is free: Computational Mathematics with SageMath. The theoretical classic: von zur Gathen and Gerhard. Knuth — for those hungry for algorithms.
Part I
Three worlds of computation
numerics · symbolic computation · formal proof
We begin with the most important distinction of the whole course: a computer can compute approximately, it can compute exactly with symbols, and it can check proofs. These are three different activities with three different kinds of guarantee.
Warm-up: is $\sqrt{2}\cdot\sqrt{2} = 2$?
Numerics · Python float
>>> import math
>>> math.sqrt(2)**2
2.0000000000000004
>>> math.sqrt(2)**2 == 2
False
√2 ≈ 1.4142135623730951 — an approximation; rounding error ≈ 4.4·10⁻¹⁶.
Symbolic computation · SymPy
>>> import sympy as sp
>>> sp.sqrt(2)**2
2
>>> sp.sqrt(2)**2 == 2
True
√2 is a symbolic object; the rule $(\sqrt a)^2 = a$ gives an exact result.
Proof · Lean 4 + Mathlib
example (a : ℝ) (h : 0 ≤ a) :
Real.sqrt a * Real.sqrt a = a :=
Real.mul_self_sqrt h
A theorem for every $a \ge 0$, checked by the Lean kernel.
Three different questions: what is it approximately? · what is the exact result of the computation? · is it true for all $a$ — and why?
Lecture 1 · Part I — Three worlds
Open in the book →
A simple question, three answers. Python computes with floating-point numbers: √2 is rounded, so its square is not exactly 2. SymPy keeps √2 as a symbol and applies an algebraic rule — the result is exact. Lean does not compute a single value; it proves a theorem for all nonnegative a, and the kernel checks the proof. Show the first two columns live in the terminal.
Three worlds — how they complement each other
Formal proof
Lean 4 + Mathlib · Peano Lab
“true for all n”
Numerics
float, NumPy, C
“≈ 1.4142135623730951”
Symbolic computation
CAS: SymPy, SageMath
“√2, n(n+1)/2”
numerics suggests conjectures
CAS finds formulas, certificates
✓ Lean checks proofs
we test formulas on numbers,
and generalize numbers to formulas
You:
question & idea
Lecture 1 · Part I — Three worlds
This is the map of the whole course. Numerics is fast and lets us experiment — it suggests conjectures. A computer algebra system finds exact formulas and can produce certificates, e.g., a factorization. A proof assistant, such as Lean or our educational Peano Lab, checks proofs step by step. We verify formulas on numbers, and from numbers we guess formulas. At the center there is always a human being, who asks the question and has the idea.
Numerics vs. symbolic computation vs. proof
Numerics Symbolic computation (CAS) Formal proof
Objects machine numbers (float64) expressions, polynomials, exact numbers propositions and proofs (types and terms)
Answer an approximate value an exact result or formula “true” + a checked proof
Guarantee none, unless we bound the error modulo the correctness of the program and the assumptions small kernel — small trusted base
Speed enormous (hardware, billions of operations/s) moderate; expressions “swell” slow; a lot of human work
Pitfalls rounding, loss of digits, overflow branches of functions, assumptions, the heuristic simplify have we formalized the right statement?
Tools Python float, NumPy, C, Fortran SymPy, SageMath, Mathematica, Maple, Magma, PARI/GP Lean 4 + Mathlib, Rocq, Isabelle, Peano Lab
A good mathematician-programmer knows which world they are working in at the moment — and what guarantee they can expect.
Lecture 1 · Part I — Three worlds
Open in the book →
Go through the table row by row. The most important row is “guarantee”: numerics without error analysis guarantees nothing, a CAS guarantees correctness only insofar as we trust its implementation and the assumptions, and a formal proof rests on a small, checkable kernel. The last row: tools — in class we use Python, SymPy and SageMath, and Peano Lab for proofs, with Lean in the background.
Part II
Working environment
VS Code · Python · Jupyter · SymPy · SageMath
Now the practical part: what we will work with in the labs and at home. The goal is one coherent environment: the VS Code editor, Python in a virtual environment, Jupyter notebooks, the SymPy library and — when more is needed — the SageMath system.
Why Visual Studio Code?
One editor for everything: Python (.py), Jupyter notebooks (.ipynb), Markdown + $\LaTeX$, Lean 4, Git
free, for Windows, macOS and Linux
built-in terminal, code completion, debugger
the same environment in the lab and at home
Extensions (Ctrl/⌘+Shift+X):
✔ Python · ✔ Jupyter · ✔ Pylance (Microsoft)
optional: LaTeX Workshop · Lean 4
demo.ipynb — krpm — Visual Studio Code
EXPLORER
▸ .venv
demo.ipynb
fib.py
notatki.md
Gauss.lean
Integral of a polynomial
∫₀¹ x² dx = 1/3
Markdown
[1]
import
sympy as sp
x = sp.symbols('x')
sp.integrate(x**2, (x, 0, 1))
output:
1/3
TERMINAL
(.venv) $ pip install sympy
Successfully installed sympy …
Python 3.12 (.venv) · Jupyter · UTF-8
Lecture 1 · Part II — Environment
Instructions in the book →
VS Code is a free editor that handles everything we need: Python scripts, notebooks, Markdown notes with formulas, and even Lean. Three extensions are required: Python, Jupyter and Pylance. Show the window: the file explorer, a notebook with a text cell and a code cell, the terminal at the bottom and the status bar with the selected interpreter.
Installation: Python + a virtual environment
Install VS Code (code.visualstudio.com) and Python 3 (python.org; on Windows, check “Add python.exe to PATH”).
Create a course folder and a virtual environment , and install the libraries:
mkdir krpm && cd krpm
python3 -m venv .venv # Windows: py -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
pip install sympy numpy matplotlib ipykernel
Alternative (faster): uv venv, then uv pip install sympy numpy matplotlib ipykernel.
In VS Code: Ctrl/⌘+Shift+P → “Python: Select Interpreter” → .venv.
Virtual environment = a separate set of libraries for one project. We do not break the system Python, and the project is easy to reproduce on another computer.
Lecture 1 · Part II — Environment
Instructions in the book →
Step by step, just as in the lab. On Windows the most common mistake is Python missing from PATH — that is why we check this option during installation. We create the virtual environment once per project; we activate it in the terminal and select it as the interpreter in VS Code. Those who prefer a modern tool can use uv. Detailed instructions for each operating system are in the book.
A Jupyter notebook in VS Code — live demo
Notebook = code cells + text cells (Markdown + $\LaTeX$)
a .ipynb file; in the top right corner we select the kernel : .venv
Shift+Enter — run the cell and move on
formulas as in $\LaTeX$: $\int_0^1 x^2\,dx$ → $\int_0^1 x^2\,dx = \tfrac13$
Caution: the kernel state depends on the order in which the cells were run — before handing in your work: “Restart” and “Run All”
Demo — checklist
new folder, .venv, install SymPy
new notebook demo.ipynb, choose the kernel
cell: 2**100 and 0.1 + 0.2
Markdown cell with a heading and a formula
SymPy: sp.factor(x**4 - 1)
plot: matplotlib — a parabola
Restart → Run All → save
Lecture 1 · Part II — Environment
Open in the book →
Live demo following the checklist on the right. Point out the choice of kernel — the most common source of “I don’t have the sympy module” problems. Show that a Markdown cell renders formulas. Finally, show the trap: a variable defined in a cell further down that was run earlier — after Restart and Run All the notebook crashes. That is why we always check a notebook from scratch.
SymPy: a CAS as a Python library
import sympy as sp
x = sp.symbols('x')
sp.expand((x + 1)**3) # x**3 + 3*x**2 + 3*x + 1
sp.factor(x**4 - 1) # (x - 1)*(x + 1)*(x**2 + 1)
sp.solve(x**2 - 2, x) # [-sqrt(2), sqrt(2)]
sp.diff(sp.sin(x)*sp.exp(x), x) # exp(x)*sin(x) + exp(x)*cos(x)
sp.integrate(1/(1 + x**2), x) # atan(x)
sp.limit(sp.sin(x)/x, x, 0) # 1
sp.Rational(1, 3) + sp.Rational(1, 6) # 1/2
pure Python: pip install sympy
expressions are Python objects — they can be stored in variables and passed to functions
exact results: √2, π, 1/3 remain symbols
approximation only on demand: sp.N(sp.pi, 50)
Documentation and tutorial: sympy.org
Lecture 1 · Part II — Environment
Open in the book →
SymPy is a CAS written in pure Python, so it installs with a single command. First we declare symbols, then we build expressions and call functions: expand, factor, solve, diff, integrate, limit. Point out that the results are exact: roots and fractions remain symbols. All results in the comments have been checked in SymPy 1.14.
SageMath — a system built on top of Python
SageMath
Python + preparser + a common interface to many libraries
PARI/GP
FLINT
Maxima
Singular
GAP
NTL
SymPy
NumPy / SciPy
matplotlib
… and many more
number theory
fast arithmetic
symbolic calculus
polynomials, Gröbner
group theory
lattices, polynomials
symbolic computation
numerics
plots
sagemath.org
One language (Python), one convention — and underneath, decades of work by specialists from many fields.
Lecture 1 · Part II — Environment
Open in the book →
SageMath is not a single library but a system that ties together many specialized programs: PARI/GP for number theory, FLINT for fast arithmetic, Maxima for symbolic calculus, Singular for multivariate polynomials and Gröbner bases, GAP for group theory, as well as SymPy, NumPy and matplotlib. The user writes Python, and Sage itself chooses the right engine. That is why Sage is stronger than SymPy in advanced algebra and number theory.
SageMath: the preparser and how to run Sage
sage: 2^100 # ^ to potęga!
1267650600228229401496703205376
sage: 1/3 + 1/6 # ułamek, nie float
1/2
sage: R.<x> = QQ[] # pierścień Q[x]
sage: factor(x^4 - 1)
(x - 1) * (x + 1) * (x^2 + 1)
sage: preparse("R.<x> = QQ[]")
"R = QQ['x']; (x,) = R._first_ngens(1)"
sage: preparse("2^100 + 1/2")
'Integer(2)**Integer(100) + Integer(1)/Integer(2)'
The preparser translates Sage’s “mathematical” syntax into plain Python.
Where to run Sage?
SageMathCell — sagecell.sagemath.org: in the browser, no installation
CoCalc — cocalc.com: Sage/Jupyter notebooks in the cloud
Faculty JupyterHub — jupyter.wmi.amu.edu.pl
Locally — Linux, or WSL2 on Windows; on macOS, the SageMath app
In plain Python, 2^100 is XOR (= 102) and 1/3 is a float. In Sage — a power and a fraction.
Lecture 1 · Part II — Environment
Open in the book →
Sage changes a few things compared with Python: the caret means exponentiation, and dividing integers gives a fraction. This is the preparser’s work — it is worth showing exactly what it produces: numbers become Integer objects, and the notation R.<x> = QQ[] creates a polynomial ring. The easiest start without installation is SageMathCell or CoCalc; there is also the faculty’s JupyterHub. Results on the slide checked in the modular distribution of Sage (passagemath 10.8).
The same task in three dialects
Task Python SymPy SageMath
$2^{100}$ 2**100sp.Integer(2)**1002^100
→ 1267650600228229401496703205376 in all three (integers are exact)
$\tfrac13 + \tfrac16$ 1/3 + 1/6 → 0.5 (float!) sp.Rational(1, 3) + sp.Rational(1, 6) → 1/21/3 + 1/6 → 1/2
factor $x^4-1$ — (none) sp.factor(x**4 - 1) → (x - 1)*(x + 1)*(x**2 + 1)factor(x^4 - 1) → (x^2 + 1)*(x + 1)*(x - 1)
$x^2 = 2$ math.sqrt(2) → 1.4142135623730951sp.solve(x**2 - 2, x) → [-sqrt(2), sqrt(2)]solve(x^2 - 2 == 0, x) → [x == -sqrt(2), x == sqrt(2)]
pitfall 2^100 → 102 (XOR)in SymPy, too, ^ is not exponentiation — use **
Lecture 1 · Part II — Environment
Open in the book →
The same task in three dialects. Integers are exact everywhere. Division: Python gives a float, SymPy requires an explicit Rational, Sage gives a fraction. Factorization: plain Python has no such function. Point out that for the symbolic variable x, Sage prints the factors in a different order than SymPy — the same answer, printed differently. All results checked.
Python for mathematicians (1): numbers
>>> 2**100 # int: dowolna precyzja
1267650600228229401496703205376
>>> 7 / 2, 7 // 2, 7 % 2
(3.5, 3, 1)
>>> -7 // 2, -7 % 2
(-4, 1)
>>> 2^100 # to NIE jest potęga!
102
Division with remainder: $a = (a \,/\!/\, b)\cdot b + a \,\%\, b$, where $0 \le a \,\%\, b < b$ for $b > 0$.
^ is bitwise XOR ; exponentiation is **: $2 \oplus 100 = 0000010_2 \oplus 1100100_2 = 1100110_2 = 102$
>>> 0.1 + 0.2
0.30000000000000004
>>> from fractions import Fraction
>>> Fraction(1, 10) + Fraction(2, 10)
Fraction(3, 10)
>>> Fraction(0.1) # co naprawdę jest w 0.1
Fraction(3602879701896397, 36028797018963968)
int — exact, no limit on digits; float — 64 bits, an approximation; Fraction — exact fractions.
Lecture 1 · Part II — Python for mathematicians
Open in the book →
Python’s int type has arbitrary precision — 2 to the 100th power is exact. Three kinds of division: ordinary division gives a float, // is the integer quotient, % is the remainder; for negative numbers Python rounds the quotient down, so that the remainder is nonnegative. The caret is XOR — a classic pitfall. The number 0.1 in the computer is not one tenth: Fraction(0.1) shows which binary fraction is actually stored.
Python for mathematicians (2): sets, functions, loops
$\{\, n^2 : n \in \{1,\dots,10\},\ 2 \nmid n \,\}$
⟷ [n**2 for n in range(1, 11) if n % 2] → [1, 9, 25, 49, 81]
def nwd(a, b):
"""Euklides: NWD(a, b) = NWD(b, a mod b)."""
while b != 0:
a, b = b, a % b
return abs(a)
nwd(1071, 462) # 21
def collatz(n):
"""Kroki do 1: n → n/2 lub 3n+1."""
kroki = 0
while n != 1:
n = n // 2 if n % 2 == 0 else 3*n + 1
kroki += 1
return kroki
[collatz(n) for n in range(1, 11)]
# [0, 1, 7, 2, 5, 8, 16, 3, 19, 6]
collatz(27) # 111
Does collatz(n) terminate for every $n \ge 1$? — Nobody knows: the Collatz problem (1937) is open.
Lecture 1 · Part II — Python for mathematicians
Open in the book →
A Python list comprehension is almost literally set-builder notation from mathematics, except that range(1, 11) means the numbers from 1 to 10 — the right endpoint is not included. The function nwd (Polish for gcd) shows a while loop and the simultaneous assignment a, b = b, a % b. The function collatz shows a conditional. A question to finish: does this loop always terminate? It is an open problem — computers have checked enormous ranges, but that is not a proof. Chapter 4 of the book develops this theme (“An example is not a proof”).
Summary
✓ Three worlds: numerics (fast, approximate), symbolic computation (exact), formal proof (certain).
✓ Environment: VS Code + Python + Jupyter; SymPy (a library), SageMath (a system).
→ In Lecture 2: how a computer algebra system works, and floating-point numbers and errors. The sum 1 + 2 + … + n in three worlds, Peano Lab and Lean: the book, chapter 4.
Before the lab
install VS Code, Python and the Python + Jupyter extensions
create .venv and install sympy numpy matplotlib ipykernel
run your first notebook: 2**100, sp.factor(x**4 - 1)
read the Lecture 1 chapters in the book
play with the applets and Peano Lab
Lecture 1 · Summary
Installation instructions →
Two sentences to remember: the three worlds of computation and the working environment; the rest — how a CAS works — follows in Lecture 2, and verification and a proof in Peano Lab are in chapter 4 of the book. Before the next lab, everyone should have a working environment: VS Code, Python, a virtual environment with SymPy and a first notebook that runs. Step-by-step instructions are in the book.
Thank you!
Next lecture: for the date, see Harmonogram (in Polish). Lecture 2: how a computer algebra system works, floating-point numbers and errors. The sum 1 + 2 + … + n in Python, SymPy and Peano Lab: the book, chapter 4.
ESC slide overview · S speaker notes · ? keyboard shortcuts
The end. All links on this slide are clickable: the four book chapters for Lecture 1, the applets and Peano Lab. For the dates of the next lectures, see Harmonogram (in Polish). How a CAS works is in Lecture 2; computation, verification and proof (three worlds, Peano Lab, Lean) are in chapter 4 of the book. Chapters 3 and 4 can be read in advance. Remind the audience of the shortcuts: ESC — overview, S — notes.