(* Trig.m *)
(* Copyright 1988 Wolfram Research Inc. *)
(* by Roman Maeder *)
(* all kinds of trigonometric simplifications *)

(* MOD BY P. Janhunen: Take advantage of Declare.m *)

(* import Global`, as we anticipate these routines to be called
   frequently by mistake before reading the file *)

BeginPackage["Misc`Trig`", "Global`"]

TrigCanonical::usage = "TrigCanonical[expr] applies basic trigonometric
	simplifications to expr (e.g. Sin[-x] --> -Sin[x])."

TrigFactor::usage = "TrigFactor[expr] tries to write sums of trigonometric
	functions as products."

TrigReduce::usage = "TrigReduce[expr] writes trigonometric functions of
	multiple angles as sums of products of trigonometric functions of
	that angle."
TrigReduce::notes = "TrigReduce simplifies the arguments of trigonometric
	functions. It is in a way the inverse of TrigExpand"

TrigToComplex::usage = "TrigToComplex[expr] writes trigonometric functions
	in terms of complex exponentials."

ComplexToTrig::usage = "ComplexToTrig[expr] writes complex exponentials
	as trigonometric functions of a real angle."

(* note: TrigExpand[] is built in *)


Begin["`Private`"]

`TrigCanonicalRel = {
    (* reduce arguments of the form n Pi / m *)
    Sin[x_. + r_ Pi]		:> Cos[x] /; EvenQ[r-1/2],
    Sin[x_. + Pi]			:> -Sin[x],
    Sin[x_. + n_?OddQ Pi]	:> -Sin[x],
    Sin[_?IntegerQ Pi]		:> 0,
    Cos[n_?IntegerQ Pi]		:> (-1)^n,
    Sin[x_. + r_ Pi]		:> -Cos[x] /; EvenQ[r-3/2],
    Sin[x_. + n_?EvenQ Pi]	:> Sin[x],
    Cos[x_. + r_ Pi]		:> -Sin[x] /; EvenQ[r-1/2],
    Cos[x_. + Pi]			:> -Cos[x],
    Cos[x_. + n_?OddQ Pi]	:> -Cos[x],
    Cos[x_. + r_ Pi]		:> Sin[x] /; EvenQ[r-3/2],
    Cos[x_. + n_?EvenQ Pi]	:> Cos[x],
    Tan[x_. + r_ Pi]		:> -1/Tan[x] /; IntegerQ[r-1/2],
    Tan[x_. + Pi]			:> Tan[x],
    Tan[x_. + n_ Pi]		:> Tan[x],
    Tan[_?IntegerQ Pi]		:> 0,

    (* Sin is an odd function *)
    Sin[n_?Negative x_.]     :> -Sin[-n x],
    Sin[n_?Negative x_ + y_] :> -Sin[-n x - y] /;
    	Order[x, y] == 1 && NumberQ[n],

    (* Cos is an even function *)
    Cos[n_?Negative x_.]     :>  Cos[-n x],
    Cos[n_?Negative x_ + y_] :>  Cos[-n x - y] /;
	Order[x, y] == 1 && NumberQ[n],

    (* Tan is an odd function *)
    Tan[n_?Negative x_.]     :> -Tan[-n x],
    Tan[n_?Negative x_ + y_] :> -Tan[-n x - y] /;
	Order[x, y] == 1 && NumberQ[n],

    (* Sin[x]^2 + Cos[x]^2 --> 1 etc. *)
    a_. Sin[x_]^2 + a_. Cos[x_]^2 :> a,
    n_. Sin[x_]^2 + m_. Cos[x_]^2 :> m + (n-m)Sin[x]^2 /; IntegerQ[n-m],
    a_ + b_. Sin[x_]^2 :> a Cos[x]^2 /; a+b == 0,
    a_ + b_. Cos[x_]^2 :> a Sin[x]^2 /; a+b == 0,

    Sin[x_]^n_. Cos[x_]^m_.	:> Tan[x]^n	/; n+m == 0,

    Sin[r_Rational Pi]	:>   Sin[(r - 2 Floor[r/2]) Pi] /; r > 2,
    Cos[r_Rational Pi]	:>   Cos[(r - 2 Floor[r/2]) Pi] /; r > 2,
    Tan[r_Rational Pi]	:>   Tan[(r - 2 Floor[r/2]) Pi] /; r > 2,
    Sin[r_Rational Pi]	:> - Sin[(r - 1) Pi] /; r > 1,
    Cos[r_Rational Pi]	:> - Cos[(r - 1) Pi] /; r > 1,
    Tan[r_Rational Pi]	:>   Tan[(r - 1) Pi] /; r > 1,
    Sin[r_Rational Pi]	:>   Sin[(1 - r) Pi] /; r > 1/2,
    Cos[r_Rational Pi]	:> - Cos[(1 - r) Pi] /; r > 1/2,
    Tan[r_Rational Pi]	:> - Tan[(1 - r) Pi] /; r > 1/2,
    Sin[r_Rational Pi]	:>   Cos[(1/2 - r) Pi] /; r > 1/4,
    Cos[r_Rational Pi]	:>   Sin[(1/2 - r) Pi] /; r > 1/4
 (* Tan[r_Rational Pi]	:> 1/Tan[(1/2 - r) Pi] /; r > 1/4 *)

}
TrigCanonicalRel = Dispatch[TrigCanonicalRel]
Protect[TrigCanonicalRel]

TrigCanonical[e_] := e //. TrigCanonicalRel


`TrigFactorRel = {
    a_. Sin[x_] + a_. Sin[y_] :> 2 a Sin[x/2+y/2] Cos[x/2-y/2],
    a_. Sin[x_] + b_. Sin[y_] :> 2 a Sin[x/2-y/2] Cos[x/2+y/2] /; a+b == 0,
    a_. Cos[x_] + a_. Cos[y_] :> 2 a Cos[x/2+y/2] Cos[x/2-y/2],
    a_. Cos[x_] + b_. Cos[y_] :> 2 a Sin[x/2+y/2] Sin[y/2-x/2] /; a+b == 0,
    a_. Tan[x_] + a_. Tan[y_] :> a Sin[x+y]/(Cos[x] Cos[y]),
    a_. Tan[x_] + b_. Tan[y_] :> a Sin[x-y]/(Cos[x] Cos[y]) /; a+b == 0,

    a_. Sin[x_] Cos[y_] + a_. Sin[y_] Cos[x_] :> a Sin[x + y],
    a_. Sin[x_] Cos[y_] + b_. Sin[y_] Cos[x_] :> a Sin[x - y] /; a+b == 0,
    a_. Cos[x_] Cos[y_] + b_. Sin[x_] Sin[y_] :> a Cos[x + y] /; a+b == 0,
    a_. Cos[x_] Cos[y_] + a_. Sin[x_] Sin[y_] :> a Cos[x - y]

}
TrigFactorRel = Dispatch[TrigFactorRel]
Protect[TrigFactorRel]

TrigFactor[e_] := FixedPoint[(# /. TrigCanonicalRel /. TrigFactorRel)&, e]


`TrigReduceRel = {

    (* the following two formulae are chosen so as to allow easy
       reconstruction of TrigExpand[Sin[x]^n] or TrigExpand[Cos[x]^n].
       In these cases, Sin[n x] with even n does not occur.
       There we use another formula *)

    Cos[n_Integer x_] :> 2^(n-1) Cos[x]^n +
	Sum[ Binomial[n-i-1, i-1] (-1)^i n/i 2^(n-2i-1) Cos[x]^(n-2i),
	   {i, 1, n/2} ]		/; n > 0,

    Sin[m_Integer?OddQ x_] :>
    	Block[{p = -(m^2-1)/6, s = Sin[x]},
	      Do[s += p Sin[x]^k;
                 p *= -(m^2 - k^2)/(k+2)/(k+1),
                {k, 3, m, 2}];
	      m s]				/; m > 0,

    Sin[n_Integer?EvenQ x_] :> 
	Sum[ Binomial[n, i] (-1)^((i-1)/2) Sin[x]^i Cos[x]^(n-i),
	     {i, 1, n, 2} ]		/; n > 0,

    Tan[n_Integer x_] :> Sin[n x]/Cos[n x],

    Sin[x_ + y_] :> Sin[x] Cos[y] + Sin[y] Cos[x],
    Cos[x_ + y_] :> Cos[x] Cos[y] - Sin[x] Sin[y],
    Tan[x_ + y_] :> (Tan[x] + Tan[y])/(1 - Tan[x] Tan[y]),

    (* rational factors, "symb" does not have a value *)
    Sin[r_Rational x_] :> (Sin[Numerator[r] `symb] /. TrigReduceRel /.
    	`symb -> x/Denominator[r])		/; Numerator[r] != 1,
    Cos[r_Rational x_] :> (Cos[Numerator[r] `symb] /. TrigReduceRel /.
    	`symb -> x/Denominator[r])		/; Numerator[r] != 1,

    (* half angle args *)
    Tan[x_/2] :> (1 - Cos[x])/Sin[x],
    Cos[x_/2]^(n_Integer?EvenQ) :>
    			((1 + Cos[x])/2)^(n/2),
    Sin[x_/2]^(n_Integer?EvenQ) :>
    			((1 - Cos[x])/2)^(n/2),
    Sin[x_/2]^n_. Cos[x_/2]^m_. :> Tan[x/2]^n		/; m == -n,
    Sin[r_ x_.] Cos[r_ x_.]	:> Sin[2 r x]/2	/; IntegerQ[2r]

}
TrigReduceRel = Dispatch[TrigReduceRel]
Protect[TrigReduceRel]

TrigReduce[e_] := FixedPoint[(# /. TrigCanonicalRel /. TrigReduceRel)&, e]


`TrigToComplexRel = {

    Sin[x_] :> -I/2 (Exp[I x] - Exp[-I x]),
    Cos[x_] :>  1/2 (Exp[I x] + Exp[-I x]),
    Tan[x_] :> -I (Exp[I x] - Exp[-I x])/(Exp[I x] + Exp[-I x]),
    Si[x_]  :> -I/2(ExpIntegralE[1, I x] - ExpIntegralE[1, -I x]) + Pi/2,
    Ci[x_]  :> -1/2(ExpIntegralE[1, I x] + ExpIntegralE[1, -I x])
}
TrigToComplexRel = Dispatch[TrigToComplexRel]
Protect[TrigToComplexRel]

TrigToComplex[e_] := e //. TrigCanonicalRel //. TrigToComplexRel


`ComplexToTrigRel = {

    Exp[c_Complex x_.] :> Exp[Re[c] x] (Cos[Im[c] x] + I Sin[Im[c] x])
}
ComplexToTrigRel = Dispatch[ComplexToTrigRel]
Protect[ComplexToTrigRel]

ComplexToTrig[e_] :=
	Cancel[e /. ComplexToTrigRel //. TrigCanonicalRel] //. TrigCanonicalRel

End[]

Protect[TrigCanonical, TrigFactor, TrigReduce, TrigToComplex, ComplexToTrig]

EndPackage[]

Null
