Introduction
Sage is a free, open-source computer algebra system that integrates over 150 mathematical packages like GAP, Maxima, and Singular, outperforming Mathematica for research experts. In 2026, with Sage 10.4+, it shines in commutative algebra, algebraic geometry, and quantum simulations, thanks to its extended Python kernel and native Jupyter support.
Why adopt it? For exact scalable computations (10,000x10,000 matrices without crashing), automated formal proofs, and full interoperability (LaTeX export, Cython for 100x perf). This expert tutorial walks you from installation to finite groups with copy-paste code for your pro projects. Imagine solving a nonlinear system in 3 lines instead of 300 in NumPy. Ready to level up your math?
Prerequisites
- SageMath 10.4+ (standalone—no separate Python needed)
- JupyterLab for interactive notebooks
- Advanced knowledge: rings, fields, algebraic varieties
- 8 GB RAM minimum for large matrices
- OS: Linux/Mac preferred (Windows via WSL2)
Installation via Binary
#!/bin/bash
wget https://github.com/sagemath/sage/releases/download/10.4/sage-10.4-Ubuntu_22.04-x86_64.tar.bz2
tar -xjf sage-10.4-Ubuntu_22.04-x86_64.tar.bz2
cd sage-10.4-Ubuntu_22.04-x86_64/
make
# Add to PATH
echo 'export PATH=$PATH:$(pwd)/bin' >> ~/.bashrc
source ~/.bashrc
# Test
sage -c 'print(2^100)'This script downloads, extracts, builds, and configures Sage 10.4 for Ubuntu 22.04. The make step takes 30-60 minutes; avoid proxies without setting http_proxy. Test with 2^100 to verify exact arithmetic.
First Jupyter Notebook
Run sage -n jupyterlab to launch the interface. Create a .sws notebook (Sage Worksheet). Sage extends Python with symbolic variables via var('x') and vector plots. Think of it like a mathematical GPS: it charts exact paths where NumPy only approximates.
Basic Symbolic Computations
var('x y z')
f = x^3 + sin(y) * exp(z)
print(f.simplify())
print(integrate(f, x))
print(f.series(x, 0, 5))
# Exact solution
solve([x^2 + y == 1, x - y == 2], [x, y])Declare symbolic variables, simplify x^3 + sin(y)exp(z), integrate, and expand in Taylor series. solve handles the system exactly, unlike numpy.linalg.solve which goes numerical. Pitfall: Forget var() and everything stays numeric.
Advanced Linear Algebra
Move to matrices over finite fields or rationals. Sage handles Smith decompositions (for crystal lattices) or Jordan forms, ideal for quantum physics.
Matrices and Decompositions
R = QQ # Rational numbers
M = Matrix(R, [[1,2,3],[0,5,6],[0,0,7]])
print(M.echelon_form())
print(M.Jordan_form())
# Exact inverse
print(M.inverse())
# Determinant over integers
N = Matrix(ZZ, [[2,3],[5,7]])
print(N.det())Create a triangular matrix over ℚ, apply echelon/Jordan forms and exact inverse. Over ℤ, det() returns -1 without floats. Avoid GF(p) without R=GF(prime) for modular arithmetic.
Differential and Integral Calculus
Sage excels at ODEs/PDEs with desolve, or elliptic integrals via Maxima. For experts: nonlinear systems with boundary conditions.
Differential Equations
var('t')
y = function('y')(t)
de = diff(y(t), t, 2) + y(t)
print(desolve(de, y(t)))
# Nonlinear system
u = function('u')(t)
v = function('v')(t)
sys = [diff(u,t)==v, diff(v,t)==-u]
print(desolve(sys, [u(t), v(t)]))
# Plot solution
p = plot(sin(t), (t,0,2*pi)); p.save('oscillateur.png')Solve the harmonic oscillator equation and a pendulum system. desolve yields exact cos/sin solutions. Save the plot for reports. Pitfall: Use ics for initial conditions on non-homogeneous equations.
Abstract Algebra: Groups
Explore finite groups via integrated GAP for cryptography or particle physics. Analogy: Sage as a virtual lab for testing conjectures.
Groups and Rings
G = AlternatingGroup(5)
print(G.order())
print(G.conjugacy_classes())
# Subgroup
H = G.sylow_subgroup(2)
print(H.order())
# Polynomial ring
R.<x> = PolynomialRing(QQ)
I = R.ideal([x^2-2, x^3-3])
print(I.groebner_basis())Generate A5 (order 60), conjugacy classes, and Sylow subgroup. For varieties: Gröbner basis over ℚ[x]. Avoid libgap() without load('gap') for performance.
Geometry and 3D Plots
Visualize tori or implicit surfaces. Sage exports to POV-Ray for pro renders.
Advanced 3D Plots
var('u v')
tore = parametric_plot3d(( (2+cos(u))*cos(v), (2+cos(u))*sin(v), sin(u) ), (u,0,2*pi), (v,0,2*pi), color='red')
tore.save('tore.png')
# Implicit surface
S = implicit_plot3d(x^2 + y^2 - z^2 == 1, (x,-2,2), (y,-2,2), (z,-2,2))
S.show()Plot a parametric torus and implicit hyperboloid cone. Use save() for PNG/SVG. Pitfall: Poorly chosen bounds (x,a,b) cause artifacts; check plot3d? for options.
Optimization with Cython
For speed: compile loops to C via %cython. Get 50x gains on Monte-Carlo sims.
Cython for Simulations
%cython
cpdef long fib(long n):
cdef long a=0, b=1, i
for i in range(n):
a, b = b, a+b
return a
print(fib(1000)) # 1000th Fibonacci, instantaneous
# Monte-Carlo pi
cpdef double pi_monte(int n):
cdef double x,y,s=0
cdef int i
for i in range(n):
x = random()
y = random()
if x*x + y*y <=1: s+=1
return 4*s/n
print(pi_monte(10**7))Compile recursive Fibonacci (n=1000 works) and Monte-Carlo π (10M iterations <1s). cpdef exposes to Sage scripts. Avoid plain def (100x slower).
Best Practices
- Always specify domains:
QQfor exact,RDFfor fast floats. - Profile with
%timeitandcythonizefor >10^6 ops. - Save sessions:
save('my_project.sobj'); reload withload(). - Integrate Git:
.sagefiles +sage --preparsefor type hints. - Parallelize:
@parallelon SLURM clusters for giant matrices.
Common Errors to Avoid
- Forgetting
var(): Everything goes numeric, breaking symbolic results. - Matrices without
Matrix(R, ...): Defaults to ℤ, causing overflow. - Plots without bounds: Empty renders; always use
(var,min,max). - Cython without
cdef: No speedup, false sense of performance.
Next Steps
- Official docs: Sage Reference
- GAP for groups:
sage -gap - Expert training: Learni Group Courses
- Community: forums.sagemath.org for exotic cases like schemes.
- Project: Implement Z3 via Sage for post-quantum crypto.