(* ========================================================================= *)
(* Complex transcendental functions. *)
(* ========================================================================= *)
needs "Library/transc.ml";;
needs "Library/floor.ml";;
needs "Complex/complexnumbers.ml";;
unparse_as_infix "exp";;
remove_interface "exp";;
(* ------------------------------------------------------------------------- *)
(* Complex square roots. *)
(* ------------------------------------------------------------------------- *)
let csqrt = new_definition
`csqrt(z) = if Im(z) = &0 then
if &0 <= Re(z) then complex(sqrt(Re(z)),&0)
else complex(&0,sqrt(--Re(z)))
else complex(sqrt((norm(z) + Re(z)) / &2),
(Im(z) / abs(Im(z))) *
sqrt((norm(z) - Re(z)) / &2))`;;
(* ------------------------------------------------------------------------- *)
(* Complex exponential. *)
(* ------------------------------------------------------------------------- *)
(* ------------------------------------------------------------------------- *)
(* Complex trig functions. *)
(* ------------------------------------------------------------------------- *)
let CX_CSIN,CX_CCOS = (CONJ_PAIR o prove)
(`(!x. Cx(sin x) = csin(Cx x)) /\ (!x. Cx(cos x) = ccos(Cx x))`,
REWRITE_TAC[csin; ccos; cexp; CX_DEF; ii; RE; IM; complex_mul; complex_add;
REAL_MUL_RZERO; REAL_MUL_LZERO; REAL_SUB_RZERO;
REAL_MUL_LID; complex_neg; REAL_EXP_0; REAL_ADD_LID;
REAL_MUL_LNEG; REAL_NEG_0; REAL_ADD_RID; complex_sub;
SIN_NEG; COS_NEG; GSYM REAL_MUL_2; GSYM real_sub;
complex_div; REAL_SUB_REFL; complex_inv; REAL_SUB_RNEG] THEN
CONJ_TAC THEN GEN_TAC THEN CONV_TAC REAL_RAT_REDUCE_CONV THEN
REWRITE_TAC[REAL_MUL_RZERO] THEN
AP_TERM_TAC THEN AP_THM_TAC THEN AP_TERM_TAC THEN CONV_TAC REAL_RING);;
let CSIN_ADD = prove
(`!w z. csin(w + z) = csin(w) * ccos(z) + ccos(w) * csin(z)`,
let CCOS_ADD = prove
(`!w z. ccos(w + z) = ccos(w) * ccos(z) - csin(w) * csin(z)`,
let CSIN_DOUBLE = prove
(`!z. csin(Cx(&2) * z) = Cx(&2) * csin(z) * ccos(z)`,
REWRITE_TAC[COMPLEX_RING `Cx(&2) * x = x + x`;
CSIN_ADD] THEN
CONV_TAC COMPLEX_RING);;
let CCOS_DOUBLE = prove
(`!z. ccos(Cx(&2) * z) = (ccos(z) pow 2) - (csin(z) pow 2)`,
REWRITE_TAC[COMPLEX_RING `Cx(&2) * x = x + x`;
CCOS_ADD] THEN
CONV_TAC COMPLEX_RING);;
(* ------------------------------------------------------------------------- *)
(* Euler and de Moivre formulas. *)
(* ------------------------------------------------------------------------- *)
let DEMOIVRE = prove
(`!z n. (ccos z + ii * csin z) pow n =
ccos(Cx(&n) * z) + ii * csin(Cx(&n) * z)`,
(* ------------------------------------------------------------------------- *)
(* Some lemmas. *)
(* ------------------------------------------------------------------------- *)
(* ------------------------------------------------------------------------- *)
(* Complex logarithms (the conventional principal value). *)
(* ------------------------------------------------------------------------- *)
let CLOG_WORKS = prove
(`!z. ~(z = Cx(&0))
==> cexp(clog z) = z /\ --pi < Im(clog z) /\ Im(clog z) <= pi`,
(* ------------------------------------------------------------------------- *)
(* Unwinding number. *)
(* ------------------------------------------------------------------------- *)
(* ------------------------------------------------------------------------- *)
(* An example of how to get nice identities with unwinding number. *)
(* ------------------------------------------------------------------------- *)
let CLOG_MUL = prove
(`!w z. ~(w = Cx(&0)) /\ ~(z = Cx(&0))
==> clog(w * z) =
clog(w) + clog(z) -
Cx(&2 * pi) * ii * unwinding(clog w + clog z)`,