How can I combine or expand the exponents in an expression in sage? In other words, how can I have sage rewrite an expression from (a**b)**c to a**(b*c), or vise versa?
Examples:
sage: var('x y')
(x, y)
sage: assume(x, 'rational')
sage: assume(y, 'rational')
sage: combine_exponents( (x^2)^y )
x^(2*y)
sage: assume(x > 0)
sage: expand_exponents( x^(1/3*y) )
(x^y)^(1/3)
What I have already tried:
sage: b = x^(2*y)
sage: a = (x^2)^y
sage: bool(a == b)
True
sage: a
(x^2)^y
sage: simplify(a)
(x^2)^y
sage: expand(a)
(x^2)^y
sage: b
x^(2*y)
sage: expand(b)
x^(2*y)
Update:
simplify_exp (codelion's answer) works to convert from (a**b)**c to a**(b*c), but not the other way around. Is it possible to get sage to expand exponents as well?
Starting from Sage 6.5, to transform a into b,
use the method canonicalize_radical.
sage: a.canonicalize_radical()
x^(2*y)
Note that the four methods simplify_exp, exp_simplify,
simplify_radical, radical_simplify, which had the same effect,
are being deprecated in favour of canonicalize_radical.
See Sage trac ticket #11912.
I don't know if there is a built-in function
to transform b into a.
You could define your own function like this:
def power_step(expr, step=None):
a, b = SR.var('a'), SR.var('b')
if str(expr.operator()) == str((a^b).operator()):
aa, mm = expr.operands()
if step is None:
if str(mm.operator()) == str((a*b).operator()):
bb = mm.operands().pop()
return (aa^bb)^(mm/bb)
else:
return expr
return (aa^step)^(mm/step)
else:
if step is None: return expr
else: return (expr^step)^(1/step)
Then you can decompose the powering into steps:
sage: x, y = var('x y')
sage: power_step(x^(2*y),y)
(x^y)^2
sage: power_step(x^(2*y),2)
(x^2)^y
Note that if you don't specify the step, it won't always pick the first one that is displayed.
sage: power_step(2^(x*y))
(2^y)^x
sage: power_step(x^(2*y))
(x^2)^y
You can use the simplify_exp() function. So for your example do the following:
sage: a.simplify_exp()
x^(2*y)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With