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 On 26 March 2014 12:09, <martin.vgag...@gmx.net> wrote: > On Wednesday, March 26, 2014 10:34:35 AM UTC+1, John Cremona wrote: >> >> Looking at the code used, it uses the resultant formula which in turn >> evaluates a determinant. I agree with you that for small degrees it >> would be better (almost certainly in a lot of cases) be better to >> substitute into the generic formula. > > > Here is some code which does that: > > def generic_discriminant(p): > """ > Compute discriminant of univariate polynomial. > > The discriminant is computed in two steps. First all non-zero > coefficients of the polynomial are replaced with variables, and > the discriminant is computed using these variables. Then the > original coefficients are substituted into the result of this > computation. This should be faster in many cases, particularly > with big coefficients like complicated polynomials. In those > cases, the matrix computation on simple generators is a lot > faster, and the final substitution is cheaper than carrying all > that information along at all times. > """ > pr = p.parent() > if not pr.ngens() == 1: > raise ValueError("Must be univariate") > ex = p.exponents() > pr1 = PolynomialRing(ZZ, "x", len(ex)) > pr2.<y> = pr1[] > py = sum(v*y^e for v, e in zip(pr1.gens(), ex)) > d = py.discriminant() > return d(p.coefficients()) > > Should I file a trac ticket for this? If so, should I try to determine the > cases when to use it, or should I simply get the function in there and ask > someone else to take care of the required connections to existing code? > > What is the correct ring to choose here? I thought about using > pr.base_ring() instead of ZZ, but that would mean that in the case of > stacked polynomial rings, I'd be doing the computation in a more difficult > setup than needed. It seems that sage will automatically coerce things > correctly: if the input is a polynomial over a polynomial ring over QQ or > GF(p), then so is the output. But I'm not sure whether that will always be > the case, or whether there might be cases where conversion from ZZ to > something else fails to do the right thing. > > -- > You received this message because you are subscribed to the Google Groups > "sage-support" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to sage-support+unsubscr...@googlegroups.com. > To post to this group, send email to sage-support@googlegroups.com. > Visit this group at http://groups.google.com/group/sage-support. > For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups "sage-support" group. To unsubscribe from this group and stop receiving emails from it, send an email to sage-support+unsubscr...@googlegroups.com. To post to this group, send email to sage-support@googlegroups.com. Visit this group at http://groups.google.com/group/sage-support. For more options, visit https://groups.google.com/d/optout.