Created
November 10, 2014 12:23
-
-
Save mattpap/c59ed5529744c06f23b1 to your computer and use it in GitHub Desktop.
Simplification using multiset_partitions()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In [1]: var('x1,x2,y1,y2,r1,r2') | |
Out[1]: (x₁, x₂, y₁, y₂, r₁, r₂) | |
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]: n, d = E.as_numer_denom() | |
In [4]: _, D = factor(d).as_coeff_Mul() | |
In [5]: D | |
Out[5]: | |
2 2 2 2 | |
x₁ - 2⋅x₁⋅x₂ + x₂ + y₁ - 2⋅y₁⋅y₂ + y₂ | |
In [6]: from sympy.utilities.iterables import multiset_partitions | |
In [7]: %time P = [ factor(sum(f)) + factor(sum(g)) for f, g in multiset_partitions(D.args, 2) ] | |
CPU times: user 283 ms, sys: 0 ns, total: 283 ms | |
Wall time: 280 ms | |
In [8]: min([ p.count_ops() for p in P ]) | |
Out[8]: 5 | |
In [9]: [ p for p in P if p.count_ops() == _ ] | |
Out[9]: | |
⎡ 2 2⎤ | |
⎣(x₁ - x₂) + (y₁ - y₂) ⎦ | |
In [10]: _[0] | |
Out[10]: | |
2 2 | |
(x₁ - x₂) + (y₁ - y₂) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment