1 | initial version |
Each polynomial can be seen as a dictionary whose items encode the monomials.
The keys are tuple of degrees and the values are the corresponding coefficients.
We can filter that dictionary, keeping only monomials of bounded total degree.
Then we can build the polynomial corresponding to that filtered dictionary.
Here is a function to do that:
def truncate(P, N):
r"""
Return this polynomial truncated to total degree N.
"""
trunc = {dd: c for dd, c in P.dict().items() if sum(dd) <= N}
return P.parent()(trunc)
Usage:
sage: R.<a, b, c> = PolynomialRing(QQ)
sage: PP = [R.random_element(5) for _ in range(3)]
sage: PP
[a*b^4 + 1/2*b^4*c + 2/3*b^3*c - 4/9*b^2*c - b^2,
5*b^5 - 3/2*a^3*c^2 + 3/5*b^2*c - 1,
-1/6*b^3*c^2 - 3*a*c^3 + b^2 - 3*b*c]
sage: [truncate(P, 3) for P in PP]
[-4/9*b^2*c - b^2, 3/5*b^2*c - 1, b^2 - 3*b*c]
2 | No.2 Revision |
Each polynomial can be seen as a dictionary whose items encode the monomials.
The keys are tuple of degrees and the values are the corresponding coefficients.
We can filter that dictionary, keeping only monomials of bounded total degree.
Then we can build the polynomial corresponding to that filtered dictionary.
Here is a function to do that:
def truncate(P, N):
r"""
Return this polynomial truncated to total degree N.
"""
trunc = {dd: c for dd, c in P.dict().items() if sum(dd) <= N}
return P.parent()(trunc)
Usage:
sage: R.<a, b, c> = PolynomialRing(QQ)
sage: PP = [R.random_element(5) for _ in range(3)]
sage: PP
[a*b^4 + 1/2*b^4*c + 2/3*b^3*c - 4/9*b^2*c - b^2,
5*b^5 - 3/2*a^3*c^2 + 3/5*b^2*c - 1,
-1/6*b^3*c^2 - 3*a*c^3 + b^2 - 3*b*c]
sage: [truncate(P, 3) for P in PP]
[-4/9*b^2*c - b^2, 3/5*b^2*c - 1, b^2 - 3*b*c]
The multivariate polynomials method truncate
only allows
to truncate with respect to the degree in one of the variables.
It could be improved to perform truncation by total degree.