(*^

::[paletteColors = 128; 
	fontset = title, "New York", 24, L3, center, bold, nohscroll;
	fontset = subtitle, "New York", 18, L2, center, bold, nohscroll;
	fontset = subsubtitle, "New York", 14, L2, center, bold, nohscroll;
	fontset = section, "New York", 14, L2, bold, nohscroll, grayBox;
	fontset = subsection, "New York", 12, L2, bold, nohscroll, blackBox;
	fontset = subsubsection, "New York", 10, L2, bold, nohscroll, whiteBox;
	fontset = text, "New York", 10, L2, nohscroll;
	fontset = smalltext, "New York", 9, L2, nohscroll;
	fontset = input, "Courier", 10, L2, bold, nowordwrap;
	fontset = output, "Courier", 10, L2, nowordwrap;
	fontset = message, "Courier", 10, L2, R65535, nowordwrap;
	fontset = print, "Courier", 10, L2, nowordwrap;
	fontset = info, "Courier", 10, L2, nowordwrap;
	fontset = postscript, "Courier", 12, L2, nowordwrap;
	fontset = name, "Geneva", 10, L2, italic, B65535, nowordwrap, nohscroll;
	fontset = header, "Times", 10, L2;
	fontset = footer, "Times", 12, L2, center;
	fontset = help, "Geneva", 10, L2, nohscroll;
	fontset = clipboard, "New York", 12, L2;
	fontset = completions, "New York", 12, L2, nowordwrap;
	fontset = network, "Courier", 10, L2, nowordwrap;
	fontset = graphlabel, "Courier", 12, L2, nowordwrap;
	fontset = special1, "New York", 12, L2, nowordwrap;
	fontset = special2, "New York", 12, L2, center, nowordwrap;
	fontset = special3, "New York", 12, L2, right, nowordwrap;
	fontset = special4, "New York", 12, L2, nowordwrap;
	fontset = special5, "New York", 12, L2, nowordwrap;]
:[font = input; initialization; ]
*)

(* 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
(*
^*)