Hi,
you could use cse() (common subexpression elimination) for this, e.g.:
In [1]: from sympy import *
In [2]: var('a,b')
Out[2]: (a, b)
In [3]: aux = exp(a + b) + exp(b**2)
In [4]: Matrix([a*aux, b*aux, a + b])
Out[4]:
Matrix([
[a*(exp(b**2) + exp(a + b))],
[b*(exp(b**2) + exp(a + b))],
[ a + b]])
In [5]: replacements, (M,) = cse(_)
In [6]: M
Out[6]:
Matrix([
[a*x1],
[b*x1],
[ x0]])
In [7]: replacements
Out[7]: [(x0, a + b), (x1, exp(b**2) + exp(x0))]
In [8]: M.subs(list(reversed(replacements)))
Out[8]:
Matrix([
[a*(exp(b**2) + exp(a + b))],
[b*(exp(b**2) + exp(a + b))],
[ a + b]])
However, this may not be exactly what you want, because it eliminates
`a + b` as well.
Mateusz
>
>
> thanks in advance for any suggestion.
>
> cheers
> Riccardo
>
> --
> 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 post to this group, send email to
sy...@googlegroups.com.
> Visit this group at
http://groups.google.com/group/sympy.
> To view this discussion on the web visit
>
https://groups.google.com/d/msgid/sympy/a72b7481-c0c8-49a2-9a1f-3e88ae8f5f3d%40googlegroups.com.
> For more options, visit
https://groups.google.com/d/optout.