SymPy partial factoring solution

21 views
Skip to first unread message

Conrad Schiff

unread,
Dec 19, 2025, 9:09:25 AM (2 days ago) Dec 19
to 'Martin Fuller' via sympy
All,

  I have a polynomial where I want to factor the portion of it that is a perfect square (example below).  I have a solution that works but I want feedback if there are better or more generalizable ways to accomplish the same thing.  Any specific feedback on best practices or 'professional'/'power user' approaches would be most welcome.

 Example:  The polynomial (which comes from the planar restricted three-body problem):  x**2 + 2*mu*x + mu**2 + y**2  -> (x+mu)**2 + y**2.  The SymPy solution I have working in a Jupyter notebook:

____________________________________________________________
import sympy as sym
sym.init_printing() 

mu, x, y = sym.symbols('mu x y')

expr = x**2 + 2*mu*x + mu**2 + y**2

mx_part = sum(
    term for term in expr.as_ordered_terms()
    if term.has(mu, x) and not term.has(y)
)

rest = expr - mx_part

sym.factor(mx_part) + rest
____________________________________________________________

Thank you all in advance,
Conrad Schiff, PhD
Professor of Physics
Capitol Technology University

Chris Smith

unread,
Dec 19, 2025, 12:02:26 PM (2 days ago) Dec 19
to sympy
That is how I would do it -- target terms that have symbols of interest.

/c

Reply all
Reply to author
Forward
0 new messages