That is how I would do it -- target terms that have symbols of interest.

/c

On Friday, December 19, 2025 at 8:09:25 AM UTC-6 [email protected] wrote:

> 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
>

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/sympy/fd58ad6e-a705-4798-8b62-4336c56acdacn%40googlegroups.com.

Reply via email to