You should definitely open a trac ticket. Don't expect that if you
post some code as text on a ticket that someone else will magically do
the right thing though! If you have not before contributed to Sage
you can make this your chance to learn how that works, and people will
help.
I would separate out two things here: one is a generic computation of
discriminants of univariate polynomials of degree d, the result being
a polynomial in d+1 variables (the generic coefficients) over ZZ (that
is correct -- there's a unique map from ZZ to any other ring). This
should be part of the current invariant_theory module, though when I
just looked at that it only appeared to implement invariants for
binary forms of degrees 2 and 4 (not 3) as well as some ternary stuff.
As a stop-gap, a global function generic_univariate_discriminant(deg)
whose output is a polynomial in ZZ[a0,a1,...,ad] and which caches its
results would do.
Then your function should be a method for the class which is already
computing your discriminants, namely
sage.rings.polynomial.polynomial_element.Polynomial_generic_dense
but as there is already a discriminant function there you don't need a
new function, just special case code to catch small degrees (up to 3
or 4 at least) which gets the generic expression and substitutes
coefficients. This would be as simple as
if f.degree()<5:
return generic_univariate_discriminant(f.list())
As for the generic function, something like
def generic_univariate_discriminant(d):
R = PolynomialRing(ZZ,d,names='a')
a = R.gens()
if d==1: return 1
if d==2: return a[1]^2 - 4*a[0]*a[2]
if d==3: return a[1]**2*a[2]**2 - 4*a[0]*a[2]**3 - 4*a[1]**3*a[3] +
18*a[0]*a[1]*a[2]*a[3] - 27*a[0]**2*a[3]**2
(and so on) would do.
Go for it!
John