Section B.11 SymPy Symbolic Computation
This section helps you read SymPy commands for exact symbolic formulas, matrices, Jacobians, substitutions, and null spaces.
SymPy is for exact symbolic expressions. NumPy is for numerical arrays and numerical linear algebra.
import sympy as sp
import numpy as np
x, y = sp.symbols("x y")
F = sp.Matrix([
x**2 * y,
x * sp.exp(y)
])
J = F.jacobian([x, y])
J_at_a = J.subs({x: 1.0, y: 0.0})
J_at_a = np.array(J_at_a, dtype=float)
sp.diff(x**3 + x*y, x)
B = sp.Matrix([[1, 2, 3],
[2, 4, 6],
[0, 1, 1]])
B.rref()
B.nullspace()
sp.eye(3)
A = [[1, 2, 3],
[2, 4, 6],
[0, 1, 1]]
sp.Matrix(A).nullspace()
Symbols.
sp.symbols("x y")
Read as. Create symbolic variables.
Shape/return. Symbols.
Used for. Symbolic formulas.
Symbolic matrix.
sp.Matrix([...])
Read as. A symbolic vector or matrix.
Shape/return. A SymPy Matrix.
Used for. Exact linear algebra.
Jacobian.
F.jacobian([x, y])
Read as. A symbolic Jacobian.
Shape/return. A SymPy Matrix.
Used for. Local linearization.
Symbolic derivative.
sp.diff(f, x)
Read as. Differentiate a symbolic expression with respect to
x.
Shape/return. A symbolic expression.
Used for. Exact derivative checks.
Substitution.
J.subs({...})
Read as. Substitute values.
Shape/return. A symbolic or numeric expression.
Used for. Evaluate at a point.
NumPy conversion.
np.array(J_at_a, dtype=float)
Read as. Convert to NumPy.
Shape/return. A NumPy array.
Used for. Numerical matrix multiplication.
Reduced row echelon form.
M.rref()
Read as. Compute the reduced row echelon form exactly.
Output. A pair
(R, pivots).
Used for. Pivot columns, free variables, and solution sets.
Watch for. Pivot indices are zero-based; 0 means the first column.
Nullspace.
M.nullspace()
Read as. Return a basis for the null space.
Shape/return. A list of SymPy column vectors.
Used for. Exact null-space directions.
Symbolic identity matrix.
sp.eye(n)
Read as. The \(n\times n\) identity matrix in SymPy.
Shape/return. A SymPy Matrix.
Used for. Exact matrix examples.
Convert symbolic matrices to NumPy arrays with
np.array(..., dtype=float) before numerical matrix multiplication.
