Dear Mateusz, what you suggest is exactly what i was hoping for and could not find by googling
i'll definitely try that out :-) Dzienkuje Bardzo! (hope spelling is correct and...that i guessed correctly your nationalty) cheers Riccardo On Saturday, November 28, 2015 at 11:07:20 AM UTC+1, Mateusz Paprocki wrote: > > Hi, > > On 27 November 2015 at 19:34, Riccardo Rossi <[email protected] > <javascript:>> wrote: > > Dear list, > > > > i am a newby to sympy, and i should say that i liked what i found, so > ... > > first of all kudos to the developers. > > > > as of now i can succesfully generate my finite element matrices using > sympy, > > which saves me quite a lot of work. > > > > the point is that now i would like to optimize a bit what i did, and i > would > > like to collect some common factors between the entries of a matrix. > > > > for example imagine that i have (pseudocode and just an example, no > physics > > behind) > > > > a,b = symbols('a b') > > > > A = Matrix(2,1) > > A[0] = a*(exp(a+b)+exp(b^2)) > > A[1] = b*(exp(a+b)+exp(b^2)) > > > > i would like a way to detect that the term > > (exp(a+b)+exp(b^2)) > > > > is common to the different entries and eventually later on do something > of > > the type > > > > aux = (exp(a+b)+exp(b^2)) > > A[0] = a*aux > > A[1] = b*aux > > > > note that later on for me it would be still interesting to do something > > similar on SOME of the entries of the matrix > > > > for example if i had > > > > A = Matrix(3,1) > > A[0] = a*(exp(a+b)+exp(b^2)) > > A[1] = b*(exp(a+b)+exp(b^2)) > > A[2] = a+b > > > > i would still love to have > > > > > > aux = (exp(a+b)+exp(b^2)) > > A[0] = a*aux > > A[1] = b*aux > > A[2] = a+b > > > > 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 [email protected] <javascript:>. > > To post to this group, send email to [email protected] > <javascript:>. > > 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. > -- 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 post to this group, send email to [email protected]. 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/f55e6294-7c01-4aa9-a000-d32cd1af8639%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
