Performance issue in Poly.discriminant() over composite domains
Author: ForeverHaibaraCreated Sep 15, 2026Updated Sep 17, 2026
Enviroment SymPy version: 1.14 dev. Tested with python-flint installed.
SymPy sometimes takes very long time to compute the discriminant of a (low-degree, e.g., quadratic or cubic) polynomial in a composite domain. Consider the following example. It generates a multivariate polynomial of degree (3, 3, 3, d) with respect to (x, y, z, w), and then considers it as a univariate polynomial in w. Thus the resulting polynomial is on ZZ[x, y, z].
Two methods are used and compared:
- Compute the discriminant for generic coefficients and then substitute the actual polynomial coefficients.
- Call
poly.discriminant()directly.
from random import randint, seed
from itertools import product
from sympy import Poly, symbols, QQ
from time import perf_counter
def randpoly(gens, ds, inf=-100, sup=100):
return Poly({k: randint(inf, sup) for k in
product(*[range(d+1) for d in ds])}, *gens)
# seed(0)
for d in [2, 3]:
poly = randpoly(symbols('x y z w'),(3,3,3,d)).as_poly(symbols('w'))
t0 = perf_counter()
disc1 = Poly(symbols(f'a:{d+1}'), symbols('x')).discriminant()
disc1 = disc1.xreplace(dict(zip(symbols(f'a:{d+1}'), poly.all_coeffs())))
disc1 = QQ[symbols('x y z')](disc1)
disc1 = disc1.parent().to_sympy(disc1)
t1 = perf_counter()
print(f'degree {d}, method 1, time: {t1 - t0}') # fast
t0 = perf_counter()
disc2 = poly.discriminant()
t1 = perf_counter()
print(f'degree {d}, method 2, time: {t1 - t0}') # slow
print('diff =', disc1-disc2) # == 0On my computer, the above gives
degree 2, method 1, time: 0.03362850000848994
degree 2, method 2, time: 1.2406258000410162
diff = 0
degree 3, method 1, time: 0.3299261999782175
degree 3, method 2, time: 114.58903769997414
diff = 0The current .discriminant() method (method 2) is much slower than using the discriminant formula (method 1) in these examples, which I think should be improved.
Source: sympy/sympy