Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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} $$

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading