
(* ****************************************************************
*
*     Expand.m
*
**************************************************************** *)

Begin["System`"]

Unprotect[ExpandAll,ExpandDenominator,ExpandNumerator,PowerExpand]

ExpandAll::usage="ExpandAll[expr] expands out all products and integer powers
in any part of expr. ExpandAll[expr, patt] avoids expanding parts of expr
which do not contain terms matching the pattern patt."

ExpandDenominator::usage="ExpandDenominator[expr] expands out products
and powers that appear as denominators in expr."

ExpandNumerator::usage="ExpandNumerator[expr] expands out products and
powers that appear in the numerator of expr."

PowerExpand::usage="PowerExpand[expr] expands nested powers, powers of
products, logarithms of powers, and logarithms of products.  Use PowerExpand
with caution because PowerExpand does not pay attention to branch cuts."

Begin["System`Private`"]

ExpandAll[input_,options___]:=
  MapAll[Expand[ExpandDenominator[#,options],options]&,input]

ExpandDenominator[input_,options___]:=
  If[SameQ[Head[input],Plus],
     Map[ExpandDenominator[#,options]&,input],
     Numerator[input]/Expand[Denominator[input],options]]

ExpandNumerator[input_,options___]:=
  If[SameQ[Head[input],Plus],
     Map[ExpandNumerator[#,options]&,input],
     Expand[Numerator[input],options]/Denominator[input]]
     
PowerExpand[expr_]:=
  expr//.{(a_*b_)^c_:>a^c*b^c,
          (a_^b_)^c_:>a^(b*c),
	  Log[a_^b_]:>b*Log[a],
	  Log[a_*b_]:>Log[a]+Log[b],
	  Log[Rational[a_,b_]]:>Log[a]-Log[b]}

End[]

Protect[ExpandAll,ExpandDenominator,ExpandNumerator,PowerExpand]

End[]

