Hi,

On 27 November 2015 at 19:34, Riccardo Rossi <[email protected]> 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].
> 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/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/CAGBZUCZETCr7mkoV7mL-7Wn5cB_vWKecQLf62Oa1w2auPsg2rg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to