Help with getting rid of ugly code

Originally asked by @lkastner on Slack :


I have some ugly constructions that I would like to get rid of.

julia> filter(t->leading_exponent_vector(t)==[0,4,4], collect(terms(QC4)))
1-element Vector{MPolyDecRingElem{QQFieldElem, QQMPolyRingElem}}:
 1972628140481//316238254381921*y^4*z^4

I do this to get the term corresponding to a certain exponent vector. Is there something nicer? It does not work without collect. I also find the leading_exponent_vector a bit ugly, since basically it gets a “term”, so this vector should be unique, so I often tried exponent_vector instead, but that did not work.

Originally answered by @YueRen on Slack


There are various coeff commands:

julia> f = x1+2*x2-3*x3
x1 + 2*x2 - 3*x3

julia> f = x1^2+2*x2^2-3*x3^2
x1^2 + 2*x2^2 - 3*x3^2

julia> coeff(f,x1^2)
1

julia> coeff(f,x2^2)
2

You can also give it an exponent vector:

julia> coeff(f,[2,0,0,0])
1

But the exponent vector is not checked for correctness. If you give it a vector that is too short or too long, it’ll return… something.