ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 15 Oct 2021 20:29:33 +0200How do we filter the degree of polynomials?https://ask.sagemath.org/question/59360/how-do-we-filter-the-degree-of-polynomials/ I have an output of symbolic expressions.. Is there a way to filter out the outcome using the 'IF' statement that only outputs the polynomial of degree 1 or 2Fri, 15 Oct 2021 06:00:18 +0200https://ask.sagemath.org/question/59360/how-do-we-filter-the-degree-of-polynomials/Answer by tmonteil for <p>I have an output of symbolic expressions.. Is there a way to filter out the outcome using the 'IF' statement that only outputs the polynomial of degree 1 or 2</p>
https://ask.sagemath.org/question/59360/how-do-we-filter-the-degree-of-polynomials/?answer=59372#post-id-59372You can use *list comprehension* and the fact that symbolic expressions have a `degree` method as follows:
sage: L = [3*x+2, 4*x^3, x+x^2, SR(2)]
sage: [i for i in L if 1 <= i.degree(x) <= 2]
[3*x + 2, x^2 + x]
Fri, 15 Oct 2021 20:29:33 +0200https://ask.sagemath.org/question/59360/how-do-we-filter-the-degree-of-polynomials/?answer=59372#post-id-59372