Sympy: Is there a way to apply the collect() function to exponents?


I have the following equation:

$$ f(x,y) = x^{-a} \cdot x^{-b} \cdot y^{a} \cdot y^{-b} \cdot z^{-a} $$

I want to express it as follows:

$$ f(x,y) = \left( \frac{x}{yz}\right)^{a} \cdot \left( \frac{x}{y} \right)^{b} $$

For that I tried the collect() function and the powsimp() function, among others, and I couldn’t get the desired result.
Is it possible to do what I want?

This is my code:

a,b = symbols('a,b', constant = True, positive=True, real=True)
x,y,z = symbols('x,y,z', positive=True, real=True)

eq = x**a * x**b * y**-a * y**-b * z**-a
collect(eq,a, exact = True)


$$ x^{a+b}y^{-a-b}z^{-a} $$

>Solution :

You can do this with the combine='base' argument to powsimp:

In [11]: eq
 a  b  -a  -b  -a
x ⋅x ⋅y  ⋅y  ⋅z  

In [12]: powsimp(eq)
       a + b
 -a ⎛x⎞     
z  ⋅⎜─⎟     

In [13]: powsimp(eq, combine='base')
   b      a
⎛x⎞  ⎛ x ⎞ 
⎜─⎟ ⋅⎜───⎟ 
⎝y⎠  ⎝y⋅z⎠ 

In [14]: powsimp(eq, combine='exp')
 a + b  -a - b  -a
x     ⋅y      ⋅z

Leave a Reply Cancel reply