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 > <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. > > > 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 debur...@gmail.com 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 sympy+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/0cfdac80-1415-4ef5-b21f-9bda43e4265fn%40googlegroups.com.