1.1. Working environment: VS Code, Python, Jupyter, SymPy, and SageMath#
In this course we work in Visual Studio Code (VS Code) with Python, Jupyter notebooks, and the SymPy library. We will meet SageMath as a “big” computer algebra system, and we will check formal proofs in the browser in Peano Lab. This section is a guide to setting up your computer — complete the steps it describes before the first lab session.
Careful setup of the environment also makes mathematical sense. The result of a computation is trustworthy only if it can be reproduced: on another computer, a year later, in someone else’s hands. That is why every project gets its own environment with specific versions of the libraries, and a notebook must run “from scratch,” from the first cell to the last.
1.1.1. Why VS Code?#
VS Code is a free editor (Windows, macOS, Linux) that gives you, in a single window:
Python files (
.py) and Jupyter notebooks (.ipynb) — code, results, plots, and text with \(\LaTeX\);Markdown with a preview (notes, reports, solutions to exercises);
a terminal, Git, and project-wide search;
extensions for other languages — including the Lean 4 proof assistant.
You can also work in other environments (JupyterLab, PyCharm, CoCalc), but the materials and instructions assume VS Code.
1.1.2. Step-by-step installation#
1.1.2.1. 1. Python#
You need Python 3.12 or newer.
Windows: the installer from python.org/downloads; in the first window, check “Add python.exe to PATH”. The
pycommand will then be available in the terminal.macOS: the installer from python.org, or
brew install python@3.12(Homebrew). Thepython3command will then be available in the terminal.Linux: the system package (e.g.,
sudo apt install python3 python3-venv python3-pip). Check its version: on older distributions (e.g., Ubuntu 22.04, Debian 12) the system Python is older than 3.12; in that case the simplest solution is to install a newer version with the uv tool (uv python install 3.12).
You can check the installed version in a terminal:
python3 --version # Windows: py --version
1.1.2.2. 2. VS Code and extensions#
Download VS Code from code.visualstudio.com and install the extensions
(the Extensions icon, Ctrl+Shift+X, on macOS Cmd+Shift+X):
Extension |
Purpose |
|---|---|
Python (Microsoft) |
interpreter, virtual environments, running code (also installs Pylance) |
Jupyter (Microsoft) |
|
optional: Lean 4 |
the Lean proof assistant (not required in this course) |
1.1.2.3. 3. Course folder and virtual environment#
Each project has its own virtual environment — a separate directory in which the installed Python uses its own set of libraries, in the versions you choose. This way, packages installed for one project do not conflict with the packages of other projects.
mkdir krpm && cd krpm # folder kursu (nazwa dowolna)
python3 -m venv .venv # Windows: py -m venv .venv
source .venv/bin/activate # Windows (PowerShell): .venv\Scripts\Activate.ps1
python -m pip install --upgrade pip
python -m pip install sympy numpy matplotlib ipykernel
Tip
A faster alternative to venv + pip is the uv tool:
uv venv creates the environment, and uv pip install sympy numpy matplotlib ipykernel installs the packages.
1.1.2.4. 4. Your first notebook in VS Code#
File → Open Folder… and choose the course folder.
Ctrl+Shift+P(macOS:Cmd+Shift+P) → Python: Select Interpreter → choose.venv.Ctrl+Shift+P→ Create: New Jupyter Notebook.In the top right corner of the notebook, click Select Kernel and choose the
.venvenvironment.Type some code into a cell and press
Shift+Enter.
If everything works, the cell below prints the versions of Python and of the libraries:
import sys
import sympy, numpy, matplotlib
print("Python ", sys.version.split()[0])
print("SymPy ", sympy.__version__)
print("NumPy ", numpy.__version__)
print("Matplotlib", matplotlib.__version__)
Python 3.12.11
SymPy 1.14.0
NumPy 2.5.3
Matplotlib 3.11.2
The version numbers printed above come from the computer on which this book was built and may differ from yours. What matters is that your Python version is at least 3.12 and that all three imports run without errors.
1.1.2.5. Common problems#
“Running cells requires the ipykernel package” — the selected environment lacks
ipykernel:python -m pip install ipykernel(with.venvactivated).ModuleNotFoundError: No module named 'sympy'— the notebook uses a different interpreter from the one in which the packages were installed. Check the kernel in the top right corner.Windows: the
Activate.ps1script is blocked — runSet-ExecutionPolicy -Scope CurrentUser RemoteSignedonce in PowerShell, or activate fromcmdwith.venv\Scripts\activate.bat.Can’t or don’t want to install anything? — use the faculty server jupyter.wmi.amu.edu.pl (for WMI UAM students; log in with your faculty account) or CoCalc.
1.1.3. The Jupyter notebook: cells, kernel, state#
A notebook consists of cells: code cells and text (Markdown) cells. Code is executed by the kernel — a Python process running in the background that remembers the state between cells.
a = 2**10
This cell prints nothing; it only assigns a value. The next cell uses that value, because the kernel
remembers the variable a:
a + 1 # jądro pamięta zmienną a z poprzedniej komórki
1025
Warning
The order in which cells are executed matters and need not match their order in the file. Before handing in your work, always Restart the kernel and Run All — the notebook must run “from scratch,” from top to bottom.
In Markdown cells we write text with \(\LaTeX\) formulas: $e^{i\pi}+1=0$ gives \(e^{i\pi}+1=0\),
and displayed formulas, i.e., formulas set on a line of their own, are enclosed in $$ … $$:
1.1.4. SymPy — computer algebra in Python#
SymPy is a library for symbolic computation written in pure Python.
It installs with a single command and runs wherever Python does. The basic objects of SymPy are
exact: the symbol x denotes a variable, not a number; \(\sqrt{2}\) remains a square root,
and \(\tfrac13\) remains a fraction. Three short examples show the factorization of a polynomial,
the solution of an equation, and exact arithmetic.
import sympy as sp
x = sp.symbols('x')
sp.factor(x**4 - 1)
The factorization \(x^4 - 1 = (x-1)(x+1)(x^2+1)\) is a factorization into irreducible factors over \(\Q\): the factor \(x^2 + 1\) has degree \(2\) and no rational roots (it has no real roots at all), so it is irreducible over \(\Q\); it splits only after adjoining \(i\), e.g., over \(\Q(i)\) or \(\C\): \(x^2 + 1 = (x - i)(x + i)\). Solutions of equations are exact as well:
sp.solve(x**2 - 2, x) # rozwiązania dokładne, nie przybliżone
[-sqrt(2), sqrt(2)]
The roots \(\pm\sqrt{2}\) are symbolic objects, not decimal expansions, so computations with them remain exact too:
sp.sqrt(2)**2, sp.Rational(1, 3) + sp.Rational(1, 6)
(2, 1/2)
SymPy “knows” that \((\sqrt{2})^2 = 2\) and that \(\tfrac13 + \tfrac16 = \tfrac12\). For comparison, plain Python computes with floating-point numbers:
import math
math.sqrt(2)**2, 1/3 + 1/6
(2.0000000000000004, 0.5)
The first result carries a rounding error: \(\sqrt{2}\) has been replaced by the nearest
machine number (that is, a number that the float type represents exactly), and the square of that number,
once rounded, is no longer equal to \(2\). The second result is correct, but that is a
lucky outcome of the roundings, not the rule. We discuss the difference between exact and
floating-point computation in the sections Python for mathematicians: first steps, How does a computer algebra system work?,
and Computation, verification, proof.
1.1.5. SageMath — a “big” computer algebra system#
SageMath (since 2005, started by William Stein) is built on Python but integrates dozens of specialized libraries, among them PARI/GP (number theory), FLINT (fast arithmetic and polynomials), Maxima (symbolic calculus), Singular (commutative algebra, Gröbner bases), GAP (group theory), NTL, as well as NumPy, SciPy, matplotlib, and SymPy. It is considerably faster than SymPy for heavy algebraic computations, but harder to install.
Sage has its own preparser, which, before the code is executed, changes the meaning of a few notations relative to Python:
Notation |
Python |
SageMath |
|---|---|---|
|
|
|
|
|
|
|
syntax error |
the polynomial ring \(\Q[x]\) |
|
syntax error |
|
A sample Sage session (you can try it without installing anything in SageMathCell):
# SageMath
R.<x> = QQ[]
f = x^4 - 1
f.factor() # (x - 1) * (x + 1) * (x^2 + 1)
factor(2^32 + 1) # 641 * 6700417
K.<a> = NumberField(x^2 - 2)
(1 + a)^10 # 2378*a + 3363
In this session, R.<x> = QQ[] creates the polynomial ring \(\Q[x]\) in the variable x, so f is
a polynomial, not a symbolic expression. In turn, NumberField(x^2 - 2) creates the number field
\(\Q(\sqrt{2})\), in which a is a root of the polynomial \(x^2 - 2\) (we may identify it
with \(\sqrt{2}\)); the last line therefore computes exactly
\((1+\sqrt{2})^{10} = 3363 + 2378\sqrt{2}\). Sage works with algebraic structures
(rings, fields, groups), not just with expressions — this is its main advantage over SymPy.
There are several ways to run Sage:
SageMathCell — in the browser, no login required (short computations);
CoCalc — Sage notebooks in the cloud;
the faculty JupyterHub (for WMI UAM students) — we will check during the lab sessions whether a SageMath kernel is available there;
locally: on Linux/WSL via
conda-forge, or with the macOS installer — see the installation guide.
1.1.6. Peano Lab and Lean — formal proofs#
To show what it means to check a proof, we use Peano Lab: a small proof assistant for Peano arithmetic (in its intuitionistic version, i.e., Heyting arithmetic). It runs in the browser, has more than twenty tactics, and has an independent, short proof-checking kernel (a different “kernel” from the kernel of a Jupyter notebook) — so you can see exactly what each tactic does and what the computer checks.
The “big brother” of Peano Lab is Lean 4 with the Mathlib library, in which modern
mathematics is being formalized. In this course Lean is optional; the examples from the lecture are
in the file Wyklad01.lean.
1.1.7. Checklist before Lab 1#
[ ] VS Code with the Python and Jupyter extensions,
[ ] Python ≥ 3.12 (
python3 --version),[ ] a course folder with a
.venvenvironment and the packagessympy numpy matplotlib ipykernel,[ ] a working notebook: the test cell from this section prints the library versions,
[ ] an account on the faculty JupyterHub (for WMI UAM students) or on CoCalc (as a fallback).