(*********************************************************************

	Copyright 1989 by Roman E. Maeder
	
	Adapted from
	Roman E. Maeder: Programming in Mathematica, Addison-Wesley, 1989.

	Permission is hereby granted to make copies of this file for
	any purpose other than direct profit, or as part of a
	commercial product, provided this copyright notice is left
	intact.  Sale, other than for the cost of media, is prohibited.
	
	Permission is hereby granted to reproduce part or all of
	this file, provided that the source is acknowledged.

 *********************************************************************)


BeginPackage["RMPackages`TrigSimplification`"]

TrigNormal::usage = "TrigNormal[e] puts expressions with trigonometric
	functions into normal form."

TrigLinear::usage = "TrigLinear[e] expands products and powers of trigonometric
	functions."

TrigArgument::usage = "TrigArgument[e] writes trigonometric functions of multiple
	angles as products of functions of that angle."

Begin["`Private`"]

TrigCanonicalRules = {
	Sin[n_?Negative x_.] :> -Sin[-n x],
	Cos[n_?Negative x_.] :>  Cos[-n x],
	Tan[n_?Negative x_.] :> -Tan[-n x],
	Sin[n_?Negative x_ + y_] :> - Sin[-n x - y] /; OrderedQ[{x, y}],
	Cos[n_?Negative x_ + y_] :>   Cos[-n x - y] /; OrderedQ[{x, y}],
	Tan[n_?Negative x_ + y_] :> - Tan[-n x - y] /; OrderedQ[{x, y}]
}

TrigLinearRules = {
	Sin[x_] Sin[y_] :> Cos[x-y]/2 - Cos[x+y]/2,
	Cos[x_] Cos[y_] :> Cos[x+y]/2 + Cos[x-y]/2,
	Sin[x_] Cos[y_] :> Sin[x+y]/2 + Sin[x-y]/2,
	Sin[x_]^(m1_Integer?EvenQ) :> Block[{m=Abs[m1]},
	 (2^(-m+1)
	  (Sum[(-1)^(m/2-k) Binomial[m,k] Cos[(m-2k)x], {k, 0, m/2-1}]+
	   Binomial[m,m/2]/2))^Sign[m1] ],

	Cos[x_]^(m1_Integer?EvenQ) :> Block[{m=Abs[m1]},
	 (2^(-m+1)
	  (Sum[Binomial[m,k] Cos[(m-2k)x], {k, 0, m/2-1}] +
	   Binomial[m,m/2]/2))^Sign[m1] ],

	Sin[x_]^(m1_Integer?OddQ) :> Block[{m=Abs[m1]},
	 (2^(-m+1)
	  Sum[(-1)^((m-1)/2-k)
	     Binomial[m,k] Sin[(m-2k)x], {k, 0, (m-1)/2}])^Sign[m1] ],

	Cos[x_]^(m1_Integer?OddQ) :> Block[{m=Abs[m1]},
	 (2^(-m+1)
	  Sum[Binomial[m,k] Cos[(m-2k)x], {k, 0, (m-1)/2}])^Sign[m1] ]
}

TrigArgumentRules = {
	Sin[x_ + y_] :> Sin[x] Cos[y] + Sin[y] Cos[x],
	Cos[x_ + y_] :> Cos[x] Cos[y] - Sin[x] Sin[y],
	Sin[n_Integer?Positive x_.] :>
		Sum[ (-1)^((i-1)/2) Binomial[n, i] Cos[x]^(n-i) Sin[x]^i,
		    {i, 1, n, 2} ],
	Cos[n_Integer?Positive x_.] :>
		Sum[ (-1)^(i/2) Binomial[n, i] Cos[x]^(n-i) Sin[x]^i,
		    {i, 0, n, 2} ]
}

SetAttributes[{TrigNormal, TrigLinear, TrigArgument}, Listable]

TrigNormal[e_] := e /. TrigCanonicalRules

TrigLinear[e_] :=
	FixedPoint[ Expand[# //. TrigLinearRules /. TrigCanonicalRules]&, e ]

TrigArgument[e_] :=
	Together[ FixedPoint[ (# //. TrigArgumentRules /. TrigCanonicalRules)&, e ] ]

End[]

Protect[TrigNormal, TrigLinear, TrigArgument]

EndPackage[]
