Repository: sympy/sympy

Issue:
Collect factor and dimension in exponents
This came up in the discussion of [PR#12961](https://github.com/sympy/sympy/pull/12961):
```
# Testing factors and dimensions in exponents
from sympy.physics.units import Quantity, Dimension, length, time, meter, second
from sympy import Abs
v_w1 = Quantity('v_w1', length/time, 1.5*meter/second)
v_w2 = Quantity('v_w2', length/time, 2*meter/second)
expr = v_w1**(v_w2/v_w1)
print Quantity._collect_factor_and_dimension(expr)

(1.5**(v_w2/v_w1), Dimension((length/time)**(v_w2/v_w1)))
```
The first result shows that the factor and dimension of the exponent was not collected. Here is what should be returned above:
```
expr1 = (v_w2/v_w1)
expr1_vd = Quantity._collect_factor_and_dimension(expr1)
expr = v_w1**expr1_vd[0]
print Quantity._collect_factor_and_dimension(expr)

(1.71707136383000, Dimension((length/time)**1.33333333333333))
```


Discussion:

Patch:
diff --git a/sympy/physics/units/quantities.py b/sympy/physics/units/quantities.py
index 1d6bc127b7..d6b1ad390c 100644
--- a/sympy/physics/units/quantities.py
+++ b/sympy/physics/units/quantities.py
@@ -115,7 +115,10 @@ def _collect_factor_and_dimension(expr):
             return factor, dimension
         elif isinstance(expr, Pow):
             factor, dim = Quantity._collect_factor_and_dimension(expr.base)
-            return factor ** expr.exp, dim ** expr.exp
+            exp_factor, exp_dim = Quantity._collect_factor_and_dimension(expr.exp)
+            if exp_dim.is_dimensionless:
+               exp_dim = 1
+            return factor ** exp_factor, dim ** (exp_factor * exp_dim)
         elif isinstance(expr, Add):
             factor, dim = Quantity._collect_factor_and_dimension(expr.args[0])
             for addend in expr.args[1:]:


Test patch:
diff --git a/sympy/physics/units/tests/test_quantities.py b/sympy/physics/units/tests/test_quantities.py
index 16a18fb891..4e1cff2fd9 100644
--- a/sympy/physics/units/tests/test_quantities.py
+++ b/sympy/physics/units/tests/test_quantities.py
@@ -266,6 +266,10 @@ def test_factor_and_dimension():
     assert (-(2996 + S(1)/4), Dimension(1)) == \
         Quantity._collect_factor_and_dimension(expr)
 
+    expr = v_w1**(v_w2/v_w1)
+    assert ((S(3)/2)**(S(4)/3), (length/time)**(S(4)/3)) == \
+        Quantity._collect_factor_and_dimension(expr)
+
 
 @XFAIL
 def test_factor_and_dimension_with_Abs():