# Library Coq.Reals.AltSeries

Require Import Rbase.
Require Import Rfunctions.
Require Import Rseries.
Require Import SeqProp.
Require Import PartSum.
Require Import Max.
Local Open Scope R_scope.

# Formalization of alternated series

Definition tg_alt (Un:nat -> R) (i:nat) : R := (-1) ^ i * Un i.
Definition positivity_seq (Un:nat -> R) : Prop := forall n:nat, 0 <= Un n.

Lemma CV_ALT_step0 :
forall Un:nat -> R,
Un_decreasing Un ->
Un_growing (fun N:nat => sum_f_R0 (tg_alt Un) (S (2 * N))).

Lemma CV_ALT_step1 :
forall Un:nat -> R,
Un_decreasing Un ->
Un_decreasing (fun N:nat => sum_f_R0 (tg_alt Un) (2 * N)).

Lemma CV_ALT_step2 :
forall (Un:nat -> R) (N:nat),
Un_decreasing Un ->
positivity_seq Un ->
sum_f_R0 (fun i:nat => tg_alt Un (S i)) (S (2 * N)) <= 0.

A more general inequality
Lemma CV_ALT_step3 :
forall (Un:nat -> R) (N:nat),
Un_decreasing Un ->
positivity_seq Un -> sum_f_R0 (fun i:nat => tg_alt Un (S i)) N <= 0.

Lemma CV_ALT_step4 :
forall Un:nat -> R,
Un_decreasing Un ->
positivity_seq Un ->
has_ub (fun N:nat => sum_f_R0 (tg_alt Un) (S (2 * N))).

This lemma gives an interesting result about alternated series
Lemma CV_ALT :
forall Un:nat -> R,
Un_decreasing Un ->
positivity_seq Un ->
Un_cv Un 0 ->
{ l:R | Un_cv (fun N:nat => sum_f_R0 (tg_alt Un) N) l }.

# Convergence of alternated series

Theorem alternated_series :
forall Un:nat -> R,
Un_decreasing Un ->
Un_cv Un 0 ->
{ l:R | Un_cv (fun N:nat => sum_f_R0 (tg_alt Un) N) l }.

Theorem alternated_series_ineq :
forall (Un:nat -> R) (l:R) (N:nat),
Un_decreasing Un ->
Un_cv Un 0 ->
Un_cv (fun N:nat => sum_f_R0 (tg_alt Un) N) l ->
sum_f_R0 (tg_alt Un) (S (2 * N)) <= l <= sum_f_R0 (tg_alt Un) (2 * N).

# Application : construction of PI

Definition PI_tg (n:nat) := / INR (2 * n + 1).

Lemma PI_tg_pos : forall n:nat, 0 <= PI_tg n.

Lemma PI_tg_decreasing : Un_decreasing PI_tg.

Lemma PI_tg_cv : Un_cv PI_tg 0.

Lemma exist_PI :
{ l:R | Un_cv (fun N:nat => sum_f_R0 (tg_alt PI_tg) N) l }.

Now, PI is defined
Definition Alt_PI : R := 4 * (let (a,_) := exist_PI in a).

We can get an approximation of PI with the following inequality
Lemma Alt_PI_ineq :
forall N:nat,
sum_f_R0 (tg_alt PI_tg) (S (2 * N)) <= Alt_PI / 4 <=
sum_f_R0 (tg_alt PI_tg) (2 * N).

Lemma Alt_PI_RGT_0 : 0 < Alt_PI.