- if (is_a<symbol>(e.op(0)))
- return e * lcm;
- else
- return pow(multiply_lcm(e.op(0), lcm.power(ex_to<numeric>(e.op(1)).inverse())), e.op(1));
- } else
- return e * lcm;
+ if (!is_a<symbol>(e.op(0))) {
+ // (b^e)*lcm -> (b*lcm^(1/e))^e if lcm^(1/e) ∈ ℚ (i.e. not a float)
+ // but not for symbolic b, as evaluation would undo this again
+ numeric root_of_lcm = lcm.power(ex_to<numeric>(e.op(1)).inverse());
+ if (root_of_lcm.is_rational())
+ return pow(multiply_lcm(e.op(0), root_of_lcm), e.op(1));
+ }
+ }
+ // can't recurse down into e
+ return dynallocate<mul>(e, lcm);