Hi,

On 9 November 2014 17:24, Darek Cidlinský <[email protected]> wrote:
> I would like to have sympy simplify a monstrous polynom ratio:
>
> In [2]: E=1/(2*x1**2 - 4*x1*x2 + 2*x2**2 + 2*y1**2 - 4*y1*y2 + 2*y2**2) *
> (-y1*r1**2 + y2*r1**2 + y1*r2**2 - y2*r2**2 + y1*x1**2 + y2*x1**2 -
> 2*x1*x2*y1 - 2*x1*x2*y2 + y1*x2**2 + y2*x2**2 + y1**3 - y2*y1**2 - y1*y2**2
> + y2**3)
>
> In [3]: simplify(E)
>
>    2       2       2       2       2      2                            2
> 2      3     2         2    3
> - r₁ ⋅y₁ + r₁ ⋅y₂ + r₂ ⋅y₁ - r₂ ⋅y₂ + x₁ ⋅y₁ + x₁ ⋅y₂ - 2⋅x₁⋅x₂⋅y₁ -
> 2⋅x₁⋅x₂⋅y₂ + x₂ ⋅y₁ + x₂ ⋅y₂ + y₁  - y₁ ⋅y₂ - y₁⋅y₂  + y₂
> ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
>                                            ⎛ 2             2     2
> 2⎞
>                                          2⋅⎝x₁  - 2⋅x₁⋅x₂ + x₂  + y₁  -
> 2⋅y₁⋅y₂ + y₂ ⎠
>
>
> The only thing that was simplified was the denominator, and even there it
> didn't output 2[(x1-x2)^2 + (y1-y2)^2] as could be expected, but it only
> moved the 2 out of the way. Which is OK, but it's one of 100 steps that need
> be taken.
>
> I sat down with pencil and paper and after some effort simplified this
> bestiality to
>
> In [4]: F = ((y1-y2)*(r2**2 - r1**2 + y1**2 - y2**2) +
> (y1+y2)*(x1-x2)**2)/(2*((x1-x2)**2 + (y1-y2)**2))
>
> In [5]: F
> Out[5]:
>         2                     ⎛    2    2    2     2⎞
> (x₁ - x₂) ⋅(y₁ + y₂) + (y₁ - y₂)⋅⎝- r₁  + r₂  + y₁  - y₂ ⎠
> ──────────────────────────────────────────────────────────
>                          2             2
>                2⋅(x₁ - x₂)  + 2⋅(y₁ - y₂)
>
> In [8]: simplify(E-F)
> Out[8]: 0
>
> F is truly equal to E ... so why couldn't it be simplified at least to that?
>
> I even searched the docs and tried to use various dedicated simplification
> functions, but to no avail (factor() left it alone -- eventhough I thought
> that when it simplifies x^2 + 2xy + y^2 to (x+y)^2, it could do something
> here, and so did the other functions). The only "salvation" here seems to be
> collect(), but the problem is that I need to tell the symbol which should be
> collected, so the best I could do with that would be some kind of "guided
> tour" -- I'd need to look at it, then think out what should be done next,
> then write the command and have it done.
>
> Is there a way to automate this?

sympy doesn't have a built-in automatic method to do this kind of
simplifications. factor() doesn't help here because it works on the
whole expression and must give a product if expression has factors.
collect() doesn't help either, because it's not smart about what
subexpressions to factor out (that's by design). You can, however,
take all the low-level tools and compose them to get an algorithm for
your purpose. See for example
https://gist.github.com/mattpap/c59ed5529744c06f23b1, where I use
factor(), multiset_partitions() and count_ops() to get the desired
behavior. This approach is horribly inefficient, so this isn't
anything we would provide by default in sympy (or at least run by
default in simplify()).

Mateusz

> --
> 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/25f22412-038b-420b-af5f-05733d251f99%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/CAGBZUCaxgvqMNE04xCh32tosPYOGh65YFvvKUO0CzUBeXN9V_g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to