Simplification of a^x * (1/a)^x: not equal to 1?

65 views
Skip to first unread message

Pierre H

unread,
Jul 27, 2024, 8:37:54 AM7/27/24
to sympy
Hello,

This is perhaps a classical question, but since I'm only using SymPy every now and then...

I wonder why the expression a^x * (1/a)^x doesn't simplify to 1. See code (with SymPy 1.12)

a,x = symbols('a x')
simplify(a**x * (1/a)**x)

(then of course the variant a**x * (1/(a**x)) does simplify to 1).

So is a SymPy issue that the power of x isn't propagated inside the 1/(a) expression. Or I'm just missing a mathematical subtlety which requires adding some assertions about x and a?

Pierre

peter.st...@gmail.com

unread,
Jul 27, 2024, 8:49:43 AM7/27/24
to sy...@googlegroups.com

If you declare a to be positive, it simplifies with me.

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/1b37bc0c-aa69-4e89-99d0-fb5700920dccn%40googlegroups.com.

Oscar Benjamin

unread,
Jul 27, 2024, 10:12:34 AM7/27/24
to sy...@googlegroups.com
It is a classic question about SymPy. By default SymPy assumes that
all symbols represent arbitrary complex numbers. For the most part
only simplifications that are compatible with any complex numbers will
be applied either automatically or by explicit simplification
functions such as powsimp, simplify etc. I say "for the most part"
because some evaluation/simplification routines ignore degenerate
cases like x/x -> 1 even though x could be zero.

In this case the question is whether (1/a)^x = 1/(a^x). Suppose that a
= -1 and x = 1/2 then we have:

(1/(-1))^(1/2) = (-1)^(1/2) = i

On the other hand

1/(-1)^(1/2) = 1/i = -i

So for a = -1 and x = 1/2 we have (1/a)^x = -1/(a^x).

Recognising that 1/a = a^-1 the more general question is when will
(a^x)^y be equal to a^(x*y). If all symbols are positive or if y is an
integer then this holds but more generally it does not necessarily
hold. From first principles SymPy defines x^y as being exp(log(x)*y)
where log should be understood as having a branch cut on the negative
reals so that log(x) = log(abs(x)) + I*arg(x) and arg(x) is in
(-pi,pi]. This branch cut determines when identities like (1/a)^x =
1/a^x will hold.

--
Oscar
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/003401dae023%246da70330%2448f50990%24%40gmail.com.

Pierre H

unread,
Jul 27, 2024, 12:00:16 PM7/27/24
to sympy
Thanks a lot for your replies. I suspected it was about domain assumptions, but thanks to Oscar's example of the square root of -1 having two values it's got clear to me.

Now, I can rise the bar to the next question: the example I gave was a minimal one, but the way I stumbled upon was slightly more elaborate:

Let b=symbol('b') and a = 1+b,
so that
a**x * (1/a)**x
is in fact
(1+b) * (1/(1+b))**x

This means Peter's suggestion (assume a,x positive), which applies to my case, cannot be applied for a. Indeed, I just reread the "old assumption mechanism" doc https://docs.sympy.org/latest/guides/assumptions.html and it seems that the assumption I need (b>-1) cannot be implemented. Indeed the doc says "At the time of writing (SymPy 1.7)", but I guess it's still valid in 2024, correct?

Pierre

Pierre H

unread,
Jul 27, 2024, 12:08:06 PM7/27/24
to sympy
Actually, thinking a bit more, I can get away with the assumption that x is an integer (in practice numerically it can be any positive real, but that won't matter for the formula's analysis I'm trying to do).

Oscar Benjamin

unread,
Jul 27, 2024, 12:32:41 PM7/27/24
to sy...@googlegroups.com
For a simple case like assuming that b > -1 you can use a change of variables:

bp1 = Symbol('(b+1)', positive=True)
expr = expr.subs(b, bp1-1)

--
Oscar
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/4c72538d-b230-491c-8f61-c3bced87b44en%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages