Math Utilities (Calculus & Linear Algebra) Tutorial¶
cds.math_utils provides pure-Python calculus primitives and a compact linear-algebra toolkit.
1. Calculus¶
derivative and integral take a single-argument callable of one variable.
gradient instead takes one callable of n scalar arguments plus the point
list, so declare the parameters individually rather than indexing v[0].
import math
from cds.math_utils import derivative, integral, gradient
print(f"{derivative(lambda x: x**2, x=3.0):.9f}") # ≈ 6
print(f"{integral(lambda x: math.sin(x), a=0.0, b=math.pi):.9f}") # ≈ 2
print(gradient(lambda a, b: a**2 + b**2, point=[1.0, 2.0])) # ≈ [2, 4]
2. Linear Algebra Basics¶
from cds.math_utils import mat_mul, transpose, determinant, solve_linear
A = [[2.0, 1.0], [1.0, 3.0]]
print(mat_mul(A, [[1, 0], [0, 1]])) # back to A
print(transpose(A)) # [[2,1],[1,3]]
print(determinant(A)) # 5.0
print(solve_linear(A, [3.0, 4.0])) # solution of A·x = b
3. Decompositions¶
from cds.math_utils import lu_decomposition, qr_decomposition, power_iteration
L, U, P = lu_decomposition(A) # PLU factorisation
Q, R = qr_decomposition(A) # QR factorisation
eigval, eigvec = power_iteration(A) # dominant eigenpair via power iteration
4. Vector ops¶
Run the full demo with python examples/math_utils_demo.py.