Skip to content

Instantly share code, notes, and snippets.

@mattpap
Created August 12, 2011 06:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattpap/1141548 to your computer and use it in GitHub Desktop.
Save mattpap/1141548 to your computer and use it in GitHub Desktop.
diff --git a/sympy/core/add.py b/sympy/core/add.py
index c20c76b..a2d274e 100644
--- a/sympy/core/add.py
+++ b/sympy/core/add.py
@@ -542,22 +542,26 @@ def primitive(self):
(1, 2*x/3 + 4.1*y)
"""
+ terms = [ arg.as_coeff_Mul() for arg in self.args ]
cont = S.Zero
- terms = []
- terms = [a.as_coeff_Mul() for a in self.args]
- for term in terms:
- cont = cont.gcd(term[0])
- if cont != 1: # not S.One in case Float is ever handled
- continue
- return S.One, self
+
+ for coeff, _ in terms:
+ cont = cont.gcd(coeff)
+
+ if cont == 1: # not S.One in case Float is ever handled
+ return S.One, self
M = object.__new__(Mul)
- for i, term in enumerate(terms):
- c = term[0]/cont
+
+ for i, (coeff, term) in enumerate(terms):
+ c = coeff/cont
+
if c == 1: # not S.One in case Float is ever handled
- terms[i] = term[1]
+ terms[i] = term
+ elif term == 1:
+ terms[i] = c
else:
- terms[i] = M._new_rawargs(c, term[1])
+ terms[i] = M._new_rawargs(*((c,) + Mul.make_args(term)))
# it would be better not to use Add, but since ordering may have
# changed, _new_rawargs cannot be used
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment