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

Time:02-18

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)
#powsimp(eq)

Returns:

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

CodePudding user response:

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

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

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

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

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

https://docs.sympy.org/latest/modules/simplify/simplify.html#powsimp

CodePudding user response:

The following seems to work (on sympy 1.7.1), although there is probably a cleaner approach:

powsimp(exp((log(eq)).expand().collect([a, b]).simplify()))
  • Related