Apply factor to subexpressions

41 views
Skip to first unread message

Don Burgess

unread,
May 21, 2024, 7:06:15 AM5/21/24
to sympy

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?

Chris Smith

unread,
May 21, 2024, 11:30:18 AM5/21/24
to sympy

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

Chris Smith

unread,
May 21, 2024, 11:32:11 AM5/21/24
to sympy

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)

Don Burgess

unread,
May 21, 2024, 4:27:44 PM5/21/24
to sympy
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() )
```

Oscar Benjamin

unread,
May 21, 2024, 4:32:56 PM5/21/24
to sy...@googlegroups.com
You can pass factor as the third argument to collect like collect(rhs,
ρ, factor). Then the collected coefficients will be factored.
> --
> 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 sympy+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/eba5031f-4770-4fa1-a1b8-73e38787cfa3n%40googlegroups.com.

Don Burgess

unread,
May 23, 2024, 5:06:51 PM5/23/24
to sympy
Thanks Oscar
Reply all
Reply to author
Forward
0 new messages