Attached is a patch that allows expand to handle negative integer
powers.  This effectively allows expressions like:
expr = (x+y)**(-2)
to be expanded by calling
expr.expand()

~Luke

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sympy-patches" group.
To post to this group, send email to sympy-patches@googlegroups.com
To unsubscribe from this group, send email to 
sympy-patches+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sympy-patches?hl=en
-~----------~----~----~----~------~----~------~--~---

From be5d5d7b451ecb2e88fb1a18d4fdd0e7755fb8c8 Mon Sep 17 00:00:00 2001
From: Luke Peterson <hazelnu...@gmail.com>
Date: Sun, 9 Aug 2009 00:22:46 -0700
Subject: [PATCH] Implemented code to expand negative integer powers

---
 sympy/core/power.py             |    4 +++-
 sympy/core/tests/test_expand.py |   15 +++++++++++++--
 2 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/sympy/core/power.py b/sympy/core/power.py
index 7cdb36a..79e29f8 100644
--- a/sympy/core/power.py
+++ b/sympy/core/power.py
@@ -275,7 +275,7 @@ class Pow(Basic):
         return self.new(*terms)
 
     def _eval_expand_multinomial(self, deep=True, **hints):
-        """(a+b+..) ** n -> a**n + n*a**(n-1)*b + .., n is positive integer"""
+        """(a+b+..) ** n -> a**n + n*a**(n-1)*b + .., n is nonzero integer"""
         if deep:
             b = self.base.expand(deep=deep, **hints)
             e = self.exp.expand(deep=deep, **hints)
@@ -387,6 +387,8 @@ class Pow(Basic):
                         return Add(*[f*g for f in base.args for g in base.args])
                     else:
                         return Add(*[f*multi for f in base.args])
+        elif exp.is_Integer and exp.p < 0 and base.is_Add:
+            return 1 / Pow(base, -exp.p).expand(deep=deep, **hints)
         elif exp.is_Add and base.is_Number:
             #  a + b      a  b
             # n      --> n  n  , where n, a, b are Numbers
diff --git a/sympy/core/tests/test_expand.py b/sympy/core/tests/test_expand.py
index 691f21f..2c1aa9e 100644
--- a/sympy/core/tests/test_expand.py
+++ b/sympy/core/tests/test_expand.py
@@ -1,5 +1,5 @@
-from sympy import Symbol, log
-x = Symbol('x')
+from sympy import symbols, log
+x, y, z = symbols('x y z')
 
 def test_expand_no_log():
     assert ((1+log(x**4))**2).expand(log=False) == 1 + 2*log(x**4) + log(x**4)**2
@@ -7,3 +7,14 @@ def test_expand_no_log():
 
 def test_expand_no_multinomial():
     assert ((1+x)*(1+(1+x)**4)).expand(multinomial=False) == 1 + x + (1+x)**4 + x*(1+x)**4
+
+def test_expand_negative_integer_powers():
+    expr = (x+y)**(-2)
+    assert expr.expand() == 1 / (2*x*y + x**2 + y**2)
+    assert expr.expand(multinomial=False) == (x+y)**(-2)
+    expr = (x+y)**(-3)
+    assert expr.expand() == 1 / (3*x*x*y + 3*x*y*y + x**3 + y**3)
+    assert expr.expand(multinomial=False) == (x+y)**(-3)
+    expr = (x+y)**(2) * (x+y)**(-4)
+    assert expr.expand() == 1 / (2*x*y + x**2 + y**2)
+    assert expr.expand(multinomial=False) == (x+y)**(-2)
-- 
1.6.0.4

Reply via email to