You can pass factor as the third argument to collect like collect(rhs,
ρ, factor). Then the collected coefficients will be factored.

On Tue, 21 May 2024 at 21:27, Don Burgess <[email protected]> wrote:
>
> Thank you very much for your helpful reply.
>
> Since I was using collect, I used the following code:
>
> ```python
> import sympy as sp
> from sympy import sin, cos, atan, integrate, diff
> t,μ,ρ,ψ = sp.symbols(['t','μ','ρ','ψ'])
>
> rhs = 2*μ*(1 - ρ*cos(ψ)**2)*ρ*sin(ψ)**2
>
> from sympy.simplify.fu import TR8
> rhs = TR8(rhs).expand()
>
> rhs = sp.collect(rhs, ρ)
>
> for i in range(3):
>     rhs = rhs.replace(rhs.coeff(ρ,i), rhs.coeff(ρ,i).factor() )
> ```
>
> On Tuesday, May 21, 2024 at 11:32:11 AM UTC-4 [email protected] wrote:
>>
>> This is what your code looks like after prefixing with ‘’’python and 
>> suffixing with ‘’’ (where back tics were used instead of single quotes):
>>
>> def factor_subexpressions(expr): """Factors all subexpressions of a SymPy 
>> expression. Args: expr: A SymPy expression. Returns: A SymPy expression with 
>> all subexpressions factored. """ if isinstance(expr, sp.Add): return 
>> sp.Add(*[arg.factor() for arg in expr.args]) elif isinstance(expr, sp.Mul): 
>> return sp.Mul(*[arg.factor() for arg in expr.args]) else: return 
>> sp.factor(expr)
>> On Tuesday, May 21, 2024 at 10:30:18 AM UTC-5 Chris Smith wrote:
>>>
>>> I would use expr.factor(deep=True) and if that didn’t work, then 
>>> expr.replace(lambda x: x.is_Mul or x.is_Add, lambda x: x.factor()).
>>>
>>> Regarding formatting in Google groups, see here where the recommended 
>>> Markdown extension for Chrome was used to format this message.
>>>
>>>
>>> I typed the following and then hit the extension icon and got what you see 
>>> above
>>>
>>>
>>> I would use `expr.factor(deep=True)` and if that didn't work, then 
>>> `expr.replace(lambda x: x.is_Mul or x.is_Add, lambda x: x.factor())`.
>>>
>>> Regarding formatting in Google groups, see 
>>> [here](https://webapps.stackexchange.com/questions/36030/insert-code-sample-into-a-google-groups-post)
>>>  where the recommended Markdown extension for Chrome was used to format 
>>> this message.
>>>
>>> /c
>>>
>>> On Tuesday, May 21, 2024 at 6:06:15 AM UTC-5 [email protected] wrote:
>>>>
>>>>
>>>> Is this function a good way to apply factor to subexpressions?
>>>>
>>>> def factor_subexpressions(expr):
>>>>   """Factors all subexpressions of a SymPy expression.
>>>>
>>>>   Args:
>>>>     expr: A SymPy expression.
>>>>
>>>>   Returns:
>>>>     A SymPy expression with all subexpressions factored.
>>>>   """
>>>>
>>>>   if isinstance(expr, sp.Add):
>>>>     return sp.Add(*[arg.factor() for arg in expr.args])
>>>>   elif isinstance(expr, sp.Mul):
>>>>     return sp.Mul(*[arg.factor() for arg in expr.args])
>>>>   else:
>>>>     return sp.factor(expr)
>>>>
>>>> How do I post code?
>
> --
> 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 on the web visit 
> https://groups.google.com/d/msgid/sympy/eba5031f-4770-4fa1-a1b8-73e38787cfa3n%40googlegroups.com.

-- 
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 on the web visit 
https://groups.google.com/d/msgid/sympy/CAHVvXxTwgm2mK00SfDTKspLJsV_YCkHzo1p9rpjZ00264U3%2Bcw%40mail.gmail.com.

Reply via email to