		/** Simplification of Powers **/

/*K: powers; expression simplification; prime decomposition */
/*A: John Gottschalk */
/*S: University of Western Australia */
/*D: March 1985 */

/*P: XPrimep,XIntp and XEvenpOddp are automatically loaded. */
/*U: Overcomes a bug in XPow and reduces expressions not considered in XPow. */

/* This is more general and much faster than Numbsmp in XSimp. */

If[~P[_XLoadonce[Loaded]],<XLoadonce]
/*W: The application of some rules in XIntp may result in an infinite loop
	in SMP 1.5.0 due to a pattern matcher bug so it is commented out 
	for now. */
/*Loadonce[XIntp]*/
Loadonce[XEvenpOddp]
Loadonce[XPrimep]

/* SPow[1] reduces powers of composite numbers to powers of primes. */
SPow[1]  : ($x_=Natp[$x] & Numbp[$x] & ~Primep[$x])^$y --> \   
			      Ap[Mult,Map[Ap[$1^($2 $y),$0],Nfac[$x]]]
 
SPow[2]  : ($x_=$x<0 & $x~=-1 & Numbp[$x])^$y   --> (-1)^$y (-$x)^$y

/* SPow[4] to SPow[10] reduce the complexity of the exponent in powers */

SPow[3]  : ($x_=Numbp[$x])^(($a_=Intp[$a] & Numbp[$a])+$$a) -->$x^$a $x^$$a

SPow[4]  : (-1)^(($a_=Evenp[$a])+$$a) -->  (-1)^$$a
SPow[5]  : (-1)^(($a_= Oddp[$a])+$$a) --> -(-1)^$$a
SPow[6]  : (-1)^($a_=Evenp[$a]) ->  1
SPow[7]  : (-1)^($a_= Oddp[$a]) -> -1
 
SPow[8]  : (-1)^(($a_=Intp[$a]&Numbp[$a]) ($$a_=Intp[$$a]))     --> \
			                        ((-1)^$a)^$$a
SPow[9] : (-1)^(($a_=Intp[$a]&Numbp[$a]) ($$a_=Intp[$$a])+$$b) --> \
			                        ((-1)^$a)^$$a (-1)^$$b

SPow[10] : ($x_=Numbp[$x])^($y_= Numbp[$y] & $y>1) --> \
				       $x^($y-Floor[$y]) $x^Floor[$y]

/* The case of $y<1 need not be considered as SMP automatically
       recasts negative powers as inverses if $y is a number rather
       than a symbol, eg x^(-2) is recast as 1/x^2. */

SPowEx : $a^$b --> $a^Ex[$b]
SPowinit : ($x_=Numbp[Den[$x]] & Den[$x]~=1)^$y --> Num[$x]^$y Den[$x]^(-$y)

SPowDen : $x^($y_=Numbp[$y]) --> $x^Ceil[$y]/$x^(Ceil[$y]-$y)

SCombPow[1] : ($x_=Numbp[$x])^$r ($y_=Numbp[$y])^$r    --> ($x $y)^$r
SCombPow[2] : ($x_=Numbp[$x])^$r ($y_=Numbp[$y])^(-$r) --> ($x/$y)^$r
/* SMP rewrites expressions such as (x y)^r as x^r y^r so $x and $y are
         restricted to being numbers rather than symbols in SCombPow. */

/*: Tidypow[$exp] 
	 reduces the complexity of an expression, $exp, containing 
	 powers using the substitutions defined in this file. */
TPow[$x] :: Si[$x,SPow]
TPow[$x_=$x[0] = 'Plus] :: Map[TPow,$x]
TPow[$x_=$x[0] = ' Div] :: (Num[$x]/S[Den[$x],SPowDen];\
					TPow[Num[%%]]/TPow[Den[%%]])
Tidypow[$x] :: Si[TPow[Si[S[$x,SPowEx],SPowinit]],SCombPow]

_XTidypow[Loaded] : 1

/*E:
SMP 1.4.0

#I[1]::  <XTidypow

#I[2]::  Post :: Tidypow

#O[2]:   ' Tidypow

#I[3]::  (-1)^(3+r)

	       r
#O[3]:   - (-1)

#I[4]::  2^(2+2r)

	    2r
#O[4]:   4 2

#I[5]::  x^(3+r)

	  3 + r
#O[5]:   x

#I[6]::  Intp[n]:Intp[m]:1

#O[6]:   1

#I[7]::  (-1)^(n+r)

	     n + r
#O[7]:   (-1)

#I[8]::  (-1)^(3(m+n))

	     m + n
#O[8]:   (-1)

#I[9]::  Evenp[n]:Oddp[r]:Intp[w]:1

#O[9]:   1

#I[10]::  (-1)^(n+r+2w+x)

		x
#O[10]:   - (-1)

#I[11]::  8^(2r) 10^(r)/(4^(3r))

	    r
#O[11]:   10

#I[12]::  8^(2r) 10^(r) (1/4)^(3r)

	    r
#O[12]:   10

#I[13]::  (-x)^(2r) (-1/4)^(3r) 20^r/((-y)^(2r))

	     -4r  r  2r  -2r
#O[13]:   - 2    5  x   y

#I[14]::  4^(3r) (1/2)^(2r)

	   4r
#O[14]:   2

#I[15]::  1/2^(5/3)

	   1/3
	  2
#O[15]:   ----
	   4

#I[16]::  1/3^(1/5)+3^(4/5)

	     4/5
	  4 3
#O[16]:   ------
	    3

#I[17]::  2^(3/2)/(2^(1/2)+3^(1/4))

	       1/2
	    2 2
#O[17]:   -----------
	   1/2    1/4
	  2    + 3

#I[18]::  8 (1/4)^(5/2)/(4/3)^(5/2) (5/6)^(-5/2)/(4/3)^(-8/3)

	     5/6  1/3  1/2
	  9 2    3    5
#O[18]:   ----------------
		125

#I[19]::  4 (1/7)^(3/2)+3(1/7)^(1/2)-5/3 (1/7)^(5/2)+6 (1/7)^(-5/2)

		  1/2
	  303046 7
#O[19]:   -----------
	     1029

#I[20]::  (1/7)^(4/3) 7 (7^(4/3+n))

	     n
#O[20]:   7 7

*/
