Module FormalML.QLearn.sumtest
Require Import Reals.Rbase.
Require Import Reals.Rfunctions.
Require Import Ranalysis_reg.
Require Import Reals.Integration.
Require Import Rtrigo_def.
Require Import SeqProp.
Require Import Coquelicot.Coquelicot.
Require Import Lra Lia.
Require Import Utils.
Set Bullet Behavior "
Strict Subproofs".
Local Open Scope R_scope.
Implicit Type f :
R ->
R.
Lemma RiemannInt_p1 f (
a:
R) (
pr1:
Riemann_integrable f a (
a+1)) :
(
forall x y :
R,
a <=
x ->
y <=
a+1 ->
x<=
y ->
f y <=
f x)
->
RiemannInt pr1 <=
f a .
Proof.
Lemma RiemannInt_p2 f (
a:
R) (
pr1:
Riemann_integrable f a (
a+1)) :
(
forall x y :
R,
a <=
x ->
y <=
a+1 ->
x<=
y ->
f y <=
f x)
->
RiemannInt pr1 >=
f (
a+1).
Proof.
Lemma RiemannInt_pn1 f (
n:
nat) (
pr1:
Riemann_integrable f 1 (2 +
INR n)) :
(
forall x y :
R, 1 <=
x ->
y <= 2 + (
INR n) ->
x<=
y ->
f y <=
f x)
->
RiemannInt pr1 <=
sum_f 1 (
n+1) (
fun j:
nat =>
f (
INR j)).
Proof.
revert pr1.
induction n;
simpl.
-
assert (
eqq:(2 + 0)=2)
by (
compute;
lra).
rewrite eqq.
intros.
generalize (
RiemannInt_p1 _ _ pr1);
intros.
cut_to H0.
+
auto.
+
intros.
apply H;
lra.
-
intros.
replace (
match n with
| 0%
nat => 1
|
S _ =>
INR n + 1
end)
with (
INR n + 1)
in *
by repeat (
destruct n;
simpl;
try lra).
assert (
leqs:1 <= (2 +
INR n) <= (2 +
match n with
| 0%
nat => 1
|
S _ =>
INR n + 1
end)).
{
destruct n.
-
simpl;
split;
intros;
lra.
-
rewrite <-
Rplus_assoc.
split;
intros; [ |
lra].
replace 1
with (1+0)
by lra.
apply Rplus_le_compat; [
lra | ].
apply pos_INR.
}
generalize (
RiemannInt_P22 pr1 leqs);
intros pr2.
specialize (
IHn pr2).
cut_to IHn.
+
unfold sum_f in *.
replace ((
S (
n + 1) - 1)%
nat)
with ((
S n)%
nat)
by lia.
replace ((
n + 1 - 1)%
nat)
with (
n%
nat)
in IHn by lia.
simpl.
assert (
eqn:
match n with
| 0%
nat => 1
|
S _ =>
INR n + 1
end =
INR n + 1).
{
destruct n;
trivial;
simpl;
lra. }
assert (
pr3:
Riemann_integrable f (2 +
INR n)
(2 +
match n with
| 0%
nat => 1
|
S _ =>
INR n + 1
end)).
{
apply (@
RiemannInt_P23 f 1 (2 +
match n with
| 0%
nat => 1
|
S _ =>
INR n + 1
end));
trivial.
}
generalize (
RiemannInt_P26 pr2 pr3 pr1);
intros eqr.
rewrite <-
eqr.
clear pr1 eqr.
apply Rplus_le_compat;
trivial.
revert pr3 H;
clear.
assert (
eqn:2 +
match n with
| 0%
nat => 1
|
S _ =>
INR n + 1
end = (2 +
INR n) + 1).
{
destruct n.
-
simpl;
lra.
-
lra.
}
rewrite eqn.
intros pr3 H.
replace (
match (
n + 1)%
nat with
| 0%
nat => 1
|
S _ =>
INR (
n + 1) + 1
end)
with (2 +
INR n).
*
apply (
RiemannInt_p1 _ _ pr3).
intros.
apply H;
try lra.
eapply Rle_trans; [|
eapply H0].
replace 1
with (1 + 0)
by lra.
apply Rplus_le_compat;
try lra.
apply pos_INR.
* {
assert (
forall x,
match x%
nat with
| 0%
nat => 1
|
S _ =>
INR x + 1
end =
INR x+1).
{
destruct x; [
simpl | ];
lra. }
rewrite H0.
rewrite plus_INR.
simpl;
lra.
}
+
intros.
apply H;
lra.
Qed.
Lemma RiemannInt_pn2 f (
n:
nat) (
pr1:
Riemann_integrable f 1 (2 +
INR n)) :
(
forall x y :
R, 1 <=
x ->
y <= (2 +
INR n) ->
x<=
y ->
f y <=
f x)
->
RiemannInt pr1 >=
sum_f 2 (2+
n) (
fun j:
nat =>
f (
INR j)).
Proof.
revert pr1.
induction n;
simpl.
-
assert (
eqq:(2 + 0)=2)
by (
compute;
lra).
rewrite eqq.
intros.
generalize (
RiemannInt_p2 _ _ pr1);
intros.
cut_to H0.
+
auto.
+
intros.
apply H;
trivial.
-
intros.
replace (
match n with
| 0%
nat => 1
|
S _ =>
INR n + 1
end)
with (
INR n + 1)
in *
by repeat (
destruct n;
simpl;
try lra).
assert (
leqs:1 <= (2 +
INR n) <= (2 +
match n with
| 0%
nat => 1
|
S _ =>
INR n + 1
end)).
{
destruct n.
-
simpl;
split;
intros;
lra.
-
rewrite <-
Rplus_assoc.
split;
intros; [ |
lra].
replace 1
with (1+0)
by lra.
apply Rplus_le_compat; [
lra | ].
apply pos_INR.
}
generalize (
RiemannInt_P22 pr1 leqs);
intros pr2.
specialize (
IHn pr2).
cut_to IHn.
+
unfold sum_f in *.
replace ( (
S (
S (
S n)) - 2))%
nat with ((
S n)%
nat)
by lia.
replace ((2 +
n - 2)%
nat)
with (
n%
nat)
in IHn by lia.
simpl.
assert (
eqn:
match (
n + 2)%
nat with
| 0%
nat => 1
|
S _ =>
INR (
n + 2) + 1
end =
INR (
n + 2) + 1).
{
destruct n;
simpl;
lra. }
assert (
pr3:
Riemann_integrable f (2 +
INR n)
(2 +
match n with
| 0%
nat => 1
|
S _ =>
INR n + 1
end)).
{
apply (@
RiemannInt_P23 f 1 (2 +
match n with
| 0%
nat => 1
|
S _ =>
INR n + 1
end));
trivial.
}
generalize (
RiemannInt_P26 pr2 pr3 pr1);
intros eqr.
rewrite <-
eqr.
clear pr1 eqr.
apply Rplus_ge_compat;
trivial.
revert pr3 H;
clear.
assert (
eqn:2 +
match n with
| 0%
nat => 1
|
S _ =>
INR n + 1
end = (2 +
INR n) + 1).
{
destruct n.
-
simpl;
lra.
-
lra.
}
rewrite eqn.
intros pr3 H.
replace (
match (
n + 2)%
nat with
| 0%
nat => 1
|
S _ =>
INR (
n + 2) + 1
end)
with ((2 +
INR n) + 1).
*
apply (
RiemannInt_p2 _ _ pr3).
intros.
apply H;
try lra.
eapply Rle_trans; [|
eapply H0].
replace 1
with (1 + 0)
by lra.
apply Rplus_le_compat;
try lra.
apply pos_INR.
* {
assert (
forall x,
match x%
nat with
| 0%
nat => 1
|
S _ =>
INR x + 1
end =
INR x+1).
{
destruct x; [
simpl | ];
lra. }
rewrite H0.
rewrite plus_INR.
simpl;
lra.
}
+
intros.
apply H;
lra.
Qed.
Lemma RiemannInt_pn f (
n:
nat) (
pr1:
Riemann_integrable f 1 (2 +
INR n)) :
(
forall x y :
R, 1 <=
x ->
y <= 2 + (
INR n) ->
x<=
y ->
f y <=
f x)
->
sum_f 2 (2+
n) (
fun j:
nat =>
f (
INR j))
<=
RiemannInt pr1 <=
sum_f 1 (
n+1) (
fun j:
nat =>
f (
INR j)).
Proof.
Lemma ale21 n : 1 <= 2 +
INR n.
Proof.
generalize (
pos_INR n);
intros.
lra.
Qed.
Lemma RiemannInt_cont_pn1 f (
n:
nat) :
forall (
C0:
forall x:
R, 1 <=
x <= 2 + (
INR n) ->
continuity_pt f x),
(
forall x y :
R, 1 <=
x ->
y <= 2 + (
INR n) ->
x<=
y ->
f y <=
f x)
->
RiemannInt (@
continuity_implies_RiemannInt f 1 (2 + (
INR n)) (
ale21 n)
C0) <=
sum_f 1 (
n+1) (
fun j:
nat =>
f (
INR j)).
Proof.
Lemma RiemannInt_cont_pn2 f (
n:
nat):
forall (
C0:
forall x:
R, 1 <=
x <= 2 + (
INR n) ->
continuity_pt f x),
(
forall x y :
R, 1 <=
x ->
y <= (2 +
INR n) ->
x<=
y ->
f y <=
f x)
->
RiemannInt (@
continuity_implies_RiemannInt f 1 (2 + (
INR n)) (
ale21 n)
C0) >=
sum_f 2 (2+
n) (
fun j:
nat =>
f (
INR j)).
Proof.
Lemma RiemannInt_cont_pn f (
n:
nat):
forall (
C0:
forall x:
R, 1 <=
x <= 2 + (
INR n) ->
continuity_pt f x),
(
forall x y :
R, 1 <=
x ->
y <= (2 +
INR n) ->
x<=
y ->
f y <=
f x)
->
sum_f 2 (2+
n) (
fun j:
nat =>
f (
INR j))
<=
RiemannInt (@
continuity_implies_RiemannInt f 1 (2 + (
INR n)) (
ale21 n)
C0) <=
sum_f 1 (
n+1) (
fun j:
nat =>
f (
INR j)).
Proof.
Lemma sum_bound_22 n : 0 <=
n -> 0 <= 2-1/(
n+2) - (2-1/(
n+1)) - 1/((
n+2)*(
n+2)).
Proof.
intros.
replace (2 - 1 / (
n + 2) - (2 - 1 / (
n + 1)) - 1 / ((
n + 2) * (
n + 2)))
with (1 / (
n + 1) - 1 / (
n + 2) - 1 / ((
n + 2) * (
n + 2)))
by lra.
field_simplify (1 / (
n + 1) - 1 / (
n + 2) - 1 / ((
n + 2) * (
n + 2)));
try lra.
rewrite (
Fdiv_def Rfield).
destruct H.
-
left.
apply Rmult_lt_0_compat; [
lra | ].
apply Rinv_0_lt_compat.
replace 0
with (0 + 0 + 0 + 0)
by lra.
repeat try apply Rplus_lt_compat.
*
simpl pow.
repeat (
apply Rmult_lt_0_compat;
trivial).
lra.
*
simpl pow.
repeat (
apply Rmult_lt_0_compat;
trivial)
;
lra.
*
lra.
*
lra.
-
subst.
simpl pow.
lra.
Qed.
Lemma sum_f_bound n :
sum_f_R0 (
fun i => 1 /
Rsqr (
INR i+1))
n <= 2 - 1 / (
INR (
n + 1)).
Proof.
induction n.
-
simpl.
unfold Rsqr.
lra.
-
simpl.
replace ((
match n with
| 0%
nat => 1
|
S _ =>
INR n + 1
end))
with (
INR n + 1).
+
replace (
match (
n + 1)%
nat with
| 0%
nat => 1
|
S _ =>
INR (
n + 1) + 1
end)
with (
INR (
n + 1) + 1).
*
replace (2 - 1 / (
INR (
n + 1) + 1))
with (2 - 1 /
INR (
n + 1) + ((2 - 1 / (
INR (
n + 1) + 1)) - (2 - 1 /
INR (
n + 1))))
by lra.
apply Rplus_le_compat;
trivial.
generalize (
sum_bound_22 (
INR n));
intros sb.
cut_to sb; [|
apply pos_INR].
replace ((
INR n + 1 + 1)²)
with ((
INR n + 2) * (
INR n + 2)); [|
unfold Rsqr;
lra].
{
replace (
INR (
n + 1) + 1)
with (
INR (
n+2)).
-
apply (
Rplus_le_compat_r (1 / ((
INR n + 2) * (
INR n + 2))))
in sb.
rewrite Rplus_0_l in sb.
eapply Rle_trans; [
eapply sb |].
clear.
replace ( 2 - 1 / (
INR n + 2) - (2 - 1 / (
INR n + 1)) - 1 / ((
INR n + 2) * (
INR n + 2)) +
1 / ((
INR n + 2) * (
INR n + 2)) )
with
(( 2 - 1 / (
INR n + 2) - (2 - 1 / (
INR n + 1))))
by lra.
unfold Rminus.
repeat rewrite Rplus_assoc.
apply Rplus_le_compat_l.
repeat rewrite plus_INR.
apply Rplus_le_compat_l.
simpl;
lra.
-
repeat rewrite plus_INR;
simpl;
lra.
}
*
destruct n;
simpl;
trivial.
+
destruct n;
simpl;
trivial;
lra.
Qed.
Definition f_inv := (
fun x :
R => /
x).
Definition f_inv_sq := (
fun x :
R => /
Rsqr x).
Lemma continuity_pt_inv_x a : 0 <
a ->
continuity_pt f_inv a.
Proof.
Lemma continuity_pt_inv_xsq a : 0 <
a ->
continuity_pt f_inv_sq a.
Proof.
Lemma integrable_inv a : 1 <=
a ->
Riemann_integrable f_inv 1
a.
Proof.
Lemma integrable_inv_sq a : 1 <=
a ->
Riemann_integrable f_inv_sq 1
a.
Proof.
Lemma ub_sum_inv_sq (
n:
nat) :
sum_f 2 (2+
n) (
fun j:
nat =>
f_inv_sq (
INR j))
<=
RiemannInt (@
integrable_inv_sq (2 + (
INR n)) (
ale21 n)).
Proof.
Lemma lb_sum_inv (
n:
nat) :
RiemannInt (@
integrable_inv (2 + (
INR n)) (
ale21 n))
<=
sum_f 1 (
n+1) (
fun j:
nat =>
f_inv (
INR j)).
Proof.
Lemma ln_int_unbounded :
forall x:
R, 0 <
x -> {
y |
ln y -
ln 1 >
x}.
Proof.
intros.
exists (
exp(
x+1)).
rewrite ln_1.
rewrite ln_exp.
rewrite <- (
Rplus_0_r x)
at 2.
replace (1-0)
with 1;
lra.
Qed.
Lemma inv_int_bounded :
forall x:
R, 0 <
x -> 1 - (/
x) < 1.
Proof.
Lemma is_RInt_inv (
b:
R) (
pr:1 <=
b) :
is_RInt Rinv 1
b ((
ln b) - (
ln 1)).
Proof.
Lemma is_lim_RInt_inv0:
is_lim (
fun b => (
ln b) - (
ln 1))
p_infty p_infty.
Proof.
Lemma is_lim_RInt_inv:
is_lim (
fun b => (
RInt Rinv 1
b))
p_infty p_infty.
Proof.
Lemma is_RInt_inv_Rsqr (
b:
R) (
pr:1 <=
b) :
is_RInt (
fun x:
R => /
Rsqr x) 1
b (1 - 1 /
b).
Proof.
Lemma is_lim_Rint_inv_Rsqr0 :
is_lim (
fun b => (1 - 1 /
b))
p_infty 1.
Proof.
Lemma is_lim_Rint_inv_Rsqr :
is_lim (
fun b =>
RInt (
fun x:
R => /
Rsqr x) 1
b)
p_infty 1.
Proof.
Lemma is_RInt_gen_inv_Rsqr :
is_RInt_gen (
fun x:
R => /
Rsqr x) (
at_point 1) (
Rbar_locally'
p_infty) 1.
Proof.
Lemma sum_inv_sqr_bounded :
ex_finite_lim_seq (
fun n =>
sum_f_R0 (
fun i => 1 /
Rsqr (
INR i + 1))
n).
Proof.
Lemma converges_2harmonic :
exists sum:
R,
infinite_sum (
fun i => 1 /
Rsqr (
INR i + 1))
sum.
Proof.