-
Notifications
You must be signed in to change notification settings - Fork 1
/
HS_Preliminaries.thy
1337 lines (1172 loc) · 66.3 KB
/
HS_Preliminaries.thy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(* Title: Preliminaries for hybrid systems verification
Maintainer: Jonathan Julián Huerta y Munive <[email protected]>
*)
section \<open> Hybrid Systems Preliminaries \<close>
text \<open>Hybrid systems combine continuous dynamics with discrete control. This section contains
auxiliary lemmas for verification of hybrid systems.\<close>
theory HS_Preliminaries
imports
"Ordinary_Differential_Equations.Picard_Lindeloef_Qualitative"
"Hybrid-Library.Matrix_Syntax"
begin
subsection \<open> Notation \<close>
bundle derivative_notation
begin
no_notation has_vderiv_on (infix "(has'_vderiv'_on)" 50)
notation has_derivative ("(1(D _ \<mapsto> (_))/ _)" [65,65] 61)
and has_vderiv_on ("(1 D _ = (_)/ on _)" [65,65] 61)
end
bundle derivative_no_notation
begin
notation has_vderiv_on (infix "(has'_vderiv'_on)" 50)
no_notation has_derivative ("(1(D _ \<mapsto> (_))/ _)" [65,65] 61)
and has_vderiv_on ("(1 D _ = (_)/ on _)" [65,65] 61)
end
text \<open>Special syntax for cubes, power to the 4th and 5th. \<close>
abbreviation power3 :: "'a::power \<Rightarrow> 'a" ("(_\<^sup>3)" [1000] 999)
where "x\<^sup>3 \<equiv> x ^ 3"
abbreviation power4 :: "'a::power \<Rightarrow> 'a" ("(_\<^sup>4)" [1000] 999)
where "x\<^sup>4 \<equiv> x ^ 4"
abbreviation power5 :: "'a::power \<Rightarrow> 'a" ("(_\<^sup>5)" [1000] 999)
where "x\<^sup>5 \<equiv> x ^ 5"
bundle power_notation
begin
notation power2 ("(_\<^sup>2)" [1000] 999)
and power3 ("(_\<^sup>3)" [1000] 999)
and power4 ("(_\<^sup>4)" [1000] 999)
and power5 ("(_\<^sup>5)" [1000] 999)
end
bundle power_no_notation
begin
no_notation power2 ("(_\<^sup>2)" [1000] 999)
and power3 ("(_\<^sup>3)" [1000] 999)
and power4 ("(_\<^sup>4)" [1000] 999)
and power5 ("(_\<^sup>5)" [1000] 999)
end
unbundle power_no_notation
unbundle derivative_notation
unbundle power_notation \<comment> \<open> enable notation \<close>
lemma nat_wf_induct[case_names zero induct]:
assumes "P 0"
and "(\<And>n. (\<And>m. m \<le> n \<Longrightarrow> P m) \<Longrightarrow> P (Suc n))"
shows "P n"
using assms
apply (induct n rule: full_nat_induct)
by simp (metis Suc_le_mono not0_implies_Suc)
subsection \<open> Real vector arithmetic \<close>
definition vec_upd :: "('a^'b) \<Rightarrow> 'b \<Rightarrow> 'a \<Rightarrow> 'a^'b"
where "vec_upd s i a = (\<chi> j. ((($) s)(i := a)) j)"
lemma vec_upd_eq: "vec_upd s i a = (\<chi> j. if j = i then a else s$j)"
by (simp add: vec_upd_def)
lemma nonneg_real_within_Suc: "r \<ge> 0 \<Longrightarrow> \<exists>n. Suc n > r \<and> r \<ge> n" for r::real
by (metis Groups.add_ac(2) Suc_n_not_le_n Suc_neq_Zero
less_add_one less_le_not_le linorder_le_cases linorder_not_less
nat.inject nat_ceiling_le_eq of_nat_0_less_iff of_nat_Suc
old.nat.exhaust order_less_le_trans real_nat_ceiling_ge)
lemma pos_real_within_Suc: "r > 0 \<Longrightarrow> \<exists>n. Suc n \<ge> r \<and> r > n" for r::real
by (metis gr0_implies_Suc lessI of_nat_0_less_iff of_nat_less_iff
order_le_less nonneg_real_within_Suc)
lemma abs_le_eq:
shows "(r::real) > 0 \<Longrightarrow> (\<bar>x\<bar> < r) = (-r < x \<and> x < r)"
and "(r::real) > 0 \<Longrightarrow> (\<bar>x\<bar> \<le> r) = (-r \<le> x \<and> x \<le> r)"
by linarith+
lemma real_ivl_eqs:
assumes "0 < r"
shows "ball x r = {x-r<--< x+r}" and "{x-r<--< x+r} = {x-r<..< x+r}"
and "ball (r / 2) (r / 2) = {0<--<r}" and "{0<--<r} = {0<..<r}"
and "ball 0 r = {-r<--<r}" and "{-r<--<r} = {-r<..<r}"
and "cball x r = {x-r--x+r}" and "{x-r--x+r} = {x-r..x+r}"
and "cball (r / 2) (r / 2) = {0--r}" and "{0--r} = {0..r}"
and "cball 0 r = {-r--r}" and "{-r--r} = {-r..r}"
unfolding open_segment_eq_real_ivl closed_segment_eq_real_ivl
using assms by (auto simp: cball_def ball_def dist_norm field_simps)
lemma is_interval_real_nonneg[simp]: "is_interval (Collect ((\<le>) (0::real)))"
by (auto simp: is_interval_def)
lemma open_real_segment: "open {a<--<b}" for a::real
unfolding open_segment_eq_real_ivl by auto
lemma norm_rotate_eq[simp]:
fixes x :: "'a:: {banach,real_normed_field}"
shows "(x * cos t - y * sin t)\<^sup>2 + (x * sin t + y * cos t)\<^sup>2 = x\<^sup>2 + y\<^sup>2"
and "(x * cos t + y * sin t)\<^sup>2 + (y * cos t - x * sin t)\<^sup>2 = x\<^sup>2 + y\<^sup>2"
proof-
have "(x * cos t - y * sin t)\<^sup>2 = x\<^sup>2 * (cos t)\<^sup>2 + y\<^sup>2 * (sin t)\<^sup>2 - 2 * (x * cos t) * (y * sin t)"
by(simp add: power2_diff power_mult_distrib)
also have "(x * sin t + y * cos t)\<^sup>2 = y\<^sup>2 * (cos t)\<^sup>2 + x\<^sup>2 * (sin t)\<^sup>2 + 2 * (x * cos t) * (y * sin t)"
by(simp add: power2_sum power_mult_distrib)
ultimately show "(x * cos t - y * sin t)\<^sup>2 + (x * sin t + y * cos t)\<^sup>2 = x\<^sup>2 + y\<^sup>2"
by (simp add: Groups.mult_ac(2) Groups.mult_ac(3) right_diff_distrib sin_squared_eq)
thus "(x * cos t + y * sin t)\<^sup>2 + (y * cos t - x * sin t)\<^sup>2 = x\<^sup>2 + y\<^sup>2"
by (simp add: add.commute add.left_commute power2_diff power2_sum)
qed
lemma sum_eq_Sum:
assumes "inj_on f A"
shows "(\<Sum>x\<in>A. f x) = (\<Sum> {f x |x. x \<in> A})"
proof-
have "(\<Sum> {f x |x. x \<in> A}) = (\<Sum> (f ` A))"
apply(auto simp: image_def)
by (rule_tac f=Sum in arg_cong, auto)
also have "... = (\<Sum>x\<in>A. f x)"
by (subst sum.image_eq[OF assms], simp)
finally show "(\<Sum>x\<in>A. f x) = (\<Sum> {f x |x. x \<in> A})"
by simp
qed
lemma triangle_norm_vec_le_sum: "\<parallel>x\<parallel> \<le> (\<Sum>i\<in>UNIV. \<parallel>x $ i\<parallel>)"
by (simp add: L2_set_le_sum norm_vec_def)
subsection \<open> Single variable derivatives \<close>
lemma has_derivative_at_within_iff: "(D f \<mapsto> f' (at x within S))
\<longleftrightarrow> bounded_linear f'
\<and> (\<forall>X. open X \<longrightarrow> 0 \<in> X \<longrightarrow> (\<exists>d>0. \<forall>s\<in>S. s \<noteq> x \<and> \<parallel>s - x\<parallel> < d
\<longrightarrow> (f s - f x - f' (s - x)) /\<^sub>R \<parallel>s - x\<parallel> \<in> X))"
unfolding has_derivative_at_within tendsto_def
eventually_at dist_norm by simp
lemma has_vderiv_on_iff: "(D f = f' on T)
\<longleftrightarrow> (\<forall>x\<in>T. D f \<mapsto> (\<lambda>h. h *\<^sub>R f' x) (at x within T))"
unfolding has_vderiv_on_def has_vector_derivative_def by simp
named_theorems vderiv_intros "optimised compilation of derivative rules."
declare has_vderiv_on_const [vderiv_intros]
and has_vderiv_on_id [vderiv_intros]
and has_vderiv_on_add[THEN has_vderiv_on_eq_rhs, vderiv_intros]
and has_vderiv_on_diff[THEN has_vderiv_on_eq_rhs, vderiv_intros]
and has_vderiv_on_mult[THEN has_vderiv_on_eq_rhs, vderiv_intros]
and has_vderiv_on_ln[vderiv_intros]
lemma vderiv_compI:
fixes f :: "'a::real_normed_vector \<Rightarrow> 'b::real_normed_vector"
assumes dg: "D g = g' on T"
and df: "\<forall>t\<in>T. D f \<mapsto> f' at (g t) within g ` T"
and h_eq: "\<forall>t\<in>T. h t = f' (g' t)"
shows "D (f \<circ> g) = h on T"
using assms vector_derivative_diff_chain_within[of g _ _ T f f']
unfolding has_vderiv_on_def by metis
lemma vderiv_composeI:
assumes "D f = f' on g ` T"
and " D g = g' on T"
and "h = (\<lambda>t. g' t *\<^sub>R f' (g t))"
shows "D (\<lambda>t. f (g t)) = h on T"
apply (rule has_vderiv_on_compose[THEN has_vderiv_on_eq_rhs, unfolded comp_def])
using assms by auto
lemma vderiv_uminusI[vderiv_intros]:
"D f = f' on T \<Longrightarrow> g = (\<lambda>t. - f' t) \<Longrightarrow> D (\<lambda>t. - f t) = g on T"
using has_vderiv_on_uminus by auto
lemma vderiv_npowI[vderiv_intros]:
fixes f::"real \<Rightarrow> real"
assumes "n \<ge> 1" and "D f = f' on T" and "g = (\<lambda>t. n * (f' t) * (f t)^(n-1))"
shows "D (\<lambda>t. (f t)^n) = g on T"
using assms unfolding has_vderiv_on_def has_vector_derivative_def
by (auto intro: derivative_eq_intros simp: field_simps)
lemma vderiv_divI[vderiv_intros]:
assumes "\<forall>t\<in>T. g t \<noteq> (0::real)" and "D f = f' on T"and "D g = g' on T"
and "h = (\<lambda>t. (f' t * g t - f t * (g' t)) / (g t)^2)"
shows "D (\<lambda>t. (f t)/(g t)) = h on T"
apply(subgoal_tac "(\<lambda>t. (f t)/(g t)) = (\<lambda>t. (f t) * (1/(g t)))")
apply(erule ssubst, rule vderiv_intros(5)[OF assms(2)])
apply(rule vderiv_composeI[where g=g and f="\<lambda>t. 1/t" and f'="\<lambda>t. - 1/t^2", OF _ assms(3)])
apply(subst has_vderiv_on_def, subst has_vector_derivative_def, clarsimp)
using assms(1) apply(force intro!: derivative_eq_intros simp: fun_eq_iff power2_eq_square)
using assms by (auto simp: field_simps)
lemma vderiv_cosI[vderiv_intros]:
assumes "D (f::real \<Rightarrow> real) = f' on T" and "g = (\<lambda>t. - (f' t) * sin (f t))"
shows "D (\<lambda>t. cos (f t)) = g on T"
by (rule vderiv_composeI[OF _ assms(1), of "\<lambda>t. cos t"])
(auto intro!: derivative_eq_intros simp: assms has_vderiv_on_iff)
lemma vderiv_sinI[vderiv_intros]:
assumes "D (f::real \<Rightarrow> real) = f' on T" and "g = (\<lambda>t. (f' t) * cos (f t))"
shows "D (\<lambda>t. sin (f t)) = g on T"
by (rule vderiv_composeI[OF _ assms(1), of "\<lambda>t. sin t"])
(auto intro!: derivative_eq_intros simp: assms has_vderiv_on_iff)
lemma vderiv_tanI[vderiv_intros]:
assumes "D (f::real \<Rightarrow> real) = f' on T" and "\<forall>t\<in>T. cos (f t) \<noteq> 0"
and "g = (\<lambda>t. f' t * inverse (cos (f t) * cos (f t)))"
shows "D (\<lambda>t. tan (f t)) = g on T"
by (rule vderiv_composeI[OF _ assms(1), of "\<lambda>t. tan t"])
(auto intro!: derivative_eq_intros simp: power2_eq_square assms has_vderiv_on_iff)
lemma vderiv_cotI[vderiv_intros]:
assumes "D (f::real \<Rightarrow> real) = f' on T" and "\<forall>t\<in>T. sin (f t) \<noteq> 0"
and "g = (\<lambda>t. - f' t * inverse (sin (f t) * sin (f t)))"
shows "D (\<lambda>t. cot (f t)) = g on T"
using assms
unfolding has_vderiv_on_def has_vector_derivative_def
apply clarsimp
apply (subst has_derivative_eq_rhs)
by (rule DERIV_cot[THEN DERIV_compose_FDERIV]; force)
(auto simp add: field_simps)
lemma vderiv_expI[vderiv_intros]:
assumes "D (f::real \<Rightarrow> real) = f' on T" and "g = (\<lambda>t. (f' t) * exp (f t))"
shows "D (\<lambda>t. exp (f t)) = g on T"
by (rule vderiv_composeI[OF _ assms(1), of "\<lambda>t. exp t"])
(auto intro!: derivative_eq_intros simp: assms has_vderiv_on_iff)
lemma has_vderiv_on_Pair: "\<lbrakk> D f = f' on T; D g = g' on T \<rbrakk>
\<Longrightarrow> D (\<lambda>x. (f x, g x)) = (\<lambda> x. (f' x, g' x)) on T"
by (auto intro: has_vector_derivative_Pair
simp add: has_vderiv_on_def)
lemma vderiv_pairI[vderiv_intros]:
assumes "D f1 = f1' on T"
and "D f2 = f2' on T"
and "g = (\<lambda>t. (f1' t, f2' t))"
shows "D (\<lambda>t. (f1 t, f2 t)) = g on T"
using assms
by (clarsimp simp: scaleR_vec_def has_vderiv_on_def has_vector_derivative_def)
(rule has_derivative_Pair, auto)
lemma has_vderiv_on_proj:
assumes "D f = f' on T " and "f' = (\<lambda>t. (f1' t, f2' t))"
shows has_vderiv_on_fst: "D (\<lambda>t. fst (f t)) = (\<lambda>t. f1' t) on T"
and has_vderiv_on_snd: "D (\<lambda>t. snd (f t)) = (\<lambda>t. f2' t) on T"
using assms
unfolding has_vderiv_on_def comp_def[symmetric]
by (auto intro!: has_vector_derivative_fst'
has_vector_derivative_snd'')
lemma vderiv_fstI [vderiv_intros]:
assumes "D f = f' on T " and "g = (\<lambda>t. fst (f' t))"
shows "D (\<lambda>t. fst (f t)) = g on T"
using assms
apply (unfold has_vderiv_on_def comp_def[symmetric], safe)
subgoal for x by (rule_tac has_vector_derivative_fst'[of _ _ "(snd \<circ> f') x"], force)
done
lemma vderiv_sndI [vderiv_intros]:
assumes "D f = f' on T " and "g = (\<lambda>t. snd (f' t))"
shows "D (\<lambda>t. snd (f t)) = g on T"
using assms
apply (unfold has_vderiv_on_def comp_def[symmetric], safe)
subgoal for x by (rule_tac has_vector_derivative_snd''[of _ "(fst \<circ> f') x"], force)
done
lemma has_vderiv_on_inverse: "D f = f' on T \<Longrightarrow> \<forall>t\<in>T. f t \<noteq> 0
\<Longrightarrow> D (\<lambda>t. inverse (f t)) = (\<lambda>t. - (inverse (f t)) * (f' t) * (inverse (f t))) on T"
for f :: "real \<Rightarrow> 'b :: real_normed_div_algebra"
unfolding has_vderiv_on_def apply (clarsimp simp: )
unfolding has_vector_derivative_def
apply (subst has_derivative_eq_rhs; clarsimp?)
by (rule Deriv.has_derivative_inverse) auto
lemmas vderiv_scaleR[vderiv_intros] = has_vderiv_on_scaleR[THEN has_vderiv_on_eq_rhs]
and vderiv_inverse[vderiv_intros] = has_vderiv_on_inverse[THEN has_vderiv_on_eq_rhs]
lemma has_vderiv_on_divideR: "\<forall>t\<in>T. g t \<noteq> (0::real) \<Longrightarrow> D f = f' on T \<Longrightarrow> D g = g' on T
\<Longrightarrow> D (\<lambda>t. f t /\<^sub>R g t) = (\<lambda>t. (f' t *\<^sub>R g t - f t *\<^sub>R (g' t)) /\<^sub>R (g t)^2) on T"
by (auto intro!: vderiv_intros)
(clarsimp simp: field_simps)
lemmas vderiv_divideRI = has_vderiv_on_divideR[THEN has_vderiv_on_eq_rhs]
lemma vderiv_sqrtI [vderiv_intros]:
assumes "D f = f' on T" and "f \<in> T \<rightarrow> {t. t > 0}"
and "g = (\<lambda>t. f' t * (inverse (sqrt (f t)) / 2))"
shows "D (\<lambda>t. sqrt (f t)) = g on T"
using assms
unfolding has_vderiv_on_def has_vector_derivative_def
apply clarsimp
apply (subst has_derivative_eq_rhs)
by (rule has_derivative_real_sqrt; force)
(auto simp add: field_simps)
lemma vderiv_powrI [vderiv_intros]:
fixes f :: "real \<Rightarrow> real"
assumes "D f = f' on T"
and "D g = g' on T" and "f \<in> T \<rightarrow> {t. t > 0}"
and "h = (\<lambda>t. f t powr g t * (g' t * ln (f t) + f' t * g t / f t))"
shows "D (\<lambda>t. f t powr g t) = h on T"
using assms
unfolding has_vderiv_on_def has_vector_derivative_def
apply clarsimp
apply (subst has_derivative_eq_rhs)
by (rule has_derivative_powr; force)
(auto simp add: field_simps)
lemma vderiv_innerI [vderiv_intros]:
assumes "D f = f' on T"
and "D g = g' on T"
and "h = (\<lambda>t. f t \<bullet> g' t + f' t \<bullet> g t)"
shows "D (\<lambda>t. f t \<bullet> g t) = h on T"
using assms
unfolding has_vderiv_on_def has_vector_derivative_def
apply clarsimp
apply (subst has_derivative_eq_rhs)
by (rule has_derivative_inner; force)
(auto simp add: field_simps)
lemma vderiv_normI [vderiv_intros]:
assumes "D f = f' on T" and "f \<in> T \<rightarrow> {t. t \<noteq> 0}"
and "g = (\<lambda>t. f' t \<bullet> sgn (f t))"
shows "D (\<lambda>t. \<parallel>f t\<parallel>) = g on T"
using assms
unfolding has_vderiv_on_def has_vector_derivative_def
apply clarsimp
apply (subst has_derivative_eq_rhs)
apply (rule_tac g=norm in has_derivative_compose, force)
by (rule has_derivative_norm, force)
(auto simp add: field_simps)
lemmas vderiv_ivl_integralI[vderiv_intros] = ivl_integral_has_vderiv_on[OF vderiv_on_continuous_on]
lemma vderiv_exp_scaleR_leftI:
assumes "D f = f' on T" and "g' = (\<lambda>x. f' x *\<^sub>R exp (f x *\<^sub>R A) * A)"
shows "D (\<lambda>x. exp (f x *\<^sub>R A)) = g' on T"
using assms
by (auto intro!: exp_scaleR_has_derivative_right
simp: fun_eq_iff has_vderiv_on_iff)
text \<open>Examples for checking derivatives\<close>
lemma "D (*) a = (\<lambda>t. a) on T"
by (auto intro!: vderiv_intros)
lemma "a \<noteq> 0 \<Longrightarrow> D (\<lambda>t. t/a) = (\<lambda>t. 1/a) on T"
by (auto intro!: vderiv_intros simp: power2_eq_square)
lemma "(a::real) \<noteq> 0 \<Longrightarrow> D f = f' on T \<Longrightarrow> g = (\<lambda>t. (f' t)/a) \<Longrightarrow> D (\<lambda>t. (f t)/a) = g on T"
by (auto intro!: vderiv_intros simp: power2_eq_square)
lemma "\<forall>t\<in>T. f t \<noteq> (0::real) \<Longrightarrow> D f = f' on T \<Longrightarrow> g = (\<lambda>t. - a * f' t / (f t)^2) \<Longrightarrow>
D (\<lambda>t. a/(f t)) = g on T"
by (auto intro!: vderiv_intros simp: power2_eq_square)
lemma "D (\<lambda>t. a * t\<^sup>2 / 2 + v * t + x) = (\<lambda>t. a * t + v) on T"
by(auto intro!: vderiv_intros)
lemma "D (\<lambda>t. v * t - a * t\<^sup>2 / 2 + x) = (\<lambda>x. v - a * x) on T"
by(auto intro!: vderiv_intros)
lemma "D x = x' on (\<lambda>\<tau>. \<tau> + t) ` T \<Longrightarrow> D (\<lambda>\<tau>. x (\<tau> + t)) = (\<lambda>\<tau>. x' (\<tau> + t)) on T"
by (rule vderiv_composeI, auto intro: vderiv_intros)
lemma "a \<noteq> 0 \<Longrightarrow> D (\<lambda>t. t/a) = (\<lambda>t. 1/a) on T"
by (auto intro!: vderiv_intros simp: power2_eq_square)
lemma "c \<noteq> 0 \<Longrightarrow> D (\<lambda>t. a5 * t^5 + a3 * (t^3 / c) - a2 * exp (t^2) + a1 * cos t + a0) =
(\<lambda>t. 5 * a5 * t^4 + 3 * a3 * (t^2 / c) - 2 * a2 * t * exp (t^2) - a1 * sin t) on T"
by (auto intro!: vderiv_intros simp: power2_eq_square)
lemma "c \<noteq> 0 \<Longrightarrow> D (\<lambda>t. - a3 * exp (t^3 / c) + a1 * sin t + a2 * t^2) =
(\<lambda>t. a1 * cos t + 2 * a2 * t - 3 * a3 * t^2 / c * exp (t^3 / c)) on T"
by (auto intro!: vderiv_intros simp: power2_eq_square)
lemma "c \<noteq> 0 \<Longrightarrow> D (\<lambda>t. exp (a * sin (cos (t^4) / c))) =
(\<lambda>t. - 4 * a * t^3 * sin (t^4) / c * cos (cos (t^4) / c) * exp (a * sin (cos (t^4) / c))) on T"
by (intro vderiv_intros)
(auto intro!: vderiv_intros simp: power2_eq_square)
subsection \<open> Bounded linear and bounded bilinear \<close>
thm bounded_bilinear.bounded_linear_prod_right
bounded_bilinear.bounded_linear_left
bounded_bilinear.bounded_linear_right
thm linear_iff bounded_linear.bounded
bounded_linear_def[unfolded bounded_linear_axioms_def]
bounded_bilinear_def
thm bounded_bilinear.diff_left bounded_bilinear.diff_right
thm bounded_bilinear.has_vector_derivative
lemma bdd_linear_iff_has_derivative:
"bounded_linear f \<longleftrightarrow> D f \<mapsto> f F"
using bounded_linear_imp_has_derivative
has_derivative_bounded_linear
by blast
lemma bdd_bilinear_derivativeL:
"bounded_bilinear f \<Longrightarrow> D (\<lambda>x. f x y) \<mapsto> (\<lambda>x. f x y) F"
by (subst bdd_linear_iff_has_derivative[symmetric])
(rule bounded_bilinear.bounded_linear_left)
lemma bdd_bilinear_derivativeR:
"bounded_bilinear f \<Longrightarrow> D (f x) \<mapsto> (f x) F"
by (subst bdd_linear_iff_has_derivative[symmetric])
(rule bounded_bilinear.bounded_linear_right)
lemma has_derivative_bdd_bilinear:
assumes "bounded_bilinear op"
and "D f \<mapsto> (\<lambda>t. t *\<^sub>R f') at x within S"
and "D g \<mapsto> (\<lambda>t. t *\<^sub>R g') at x within S"
shows "D (\<lambda>x. op (f x) (g x)) \<mapsto> (\<lambda>t. t *\<^sub>R op (f x) g' + t *\<^sub>R op f' (g x)) at x within S"
using bounded_bilinear.has_vector_derivative[OF assms(1)] assms
unfolding has_vector_derivative_def
by (clarsimp simp: scaleR_add_right)
lemma vderiv_bdd_bilinearI:
assumes "bounded_bilinear op"
and h_eq: "h = (\<lambda>t. op (f t) (g' t) + op (f' t) (g t))"
and df: "D f = f' on S" and dg: "D g = g' on S'" and "S \<subseteq> S'"
shows "D (\<lambda>t. op (f t) (g t)) = h on S"
unfolding has_vderiv_on_def
by (auto simp: h_eq intro!: bounded_bilinear.has_vector_derivative[OF assms(1)]
has_vderiv_on_subset[OF dg \<open>S \<subseteq> S'\<close>, unfolded has_vderiv_on_def, rule_format]
df[unfolded has_vderiv_on_def, rule_format])
lemma bdd_linear_2op_has_vderiv_onL:
fixes op :: "'a::real_normed_vector \<Rightarrow> 'b \<Rightarrow> 'c::real_normed_vector"
assumes bdd_linear_put: "\<forall>v. bounded_linear (\<lambda>s. op s v)"
and non_trivial_state_space: "\<exists>s::'a. s \<noteq> 0"
shows "D f = f' on T
\<Longrightarrow> D (\<lambda>t. op (f t) v) = (\<lambda>t. op (f' t) v) on T"
proof (clarsimp simp: has_vderiv_on_def has_vector_derivative_def has_derivative_at_within, safe)
fix \<tau>
let "?quot f f'" = "\<lambda>\<tau> t. (f t - f \<tau> - (t - \<tau>) *\<^sub>R f' \<tau>) /\<^sub>R \<bar>t - \<tau>\<bar>"
let "?lim f f'" = "\<lambda>\<tau>. ((\<lambda>t. ?quot f f' \<tau> t) \<longlongrightarrow> 0) (at \<tau> within T)"
assume "\<tau> \<in> T" and f_hyps: "\<forall>x\<in>T. bounded_linear (\<lambda>xa. xa *\<^sub>R f' x) \<and> ?lim f f' x"
hence linearv: "linear (\<lambda>s. op s v)"
and bdd_put: "\<exists>K. \<forall>x. \<parallel>op x v\<parallel> \<le> \<parallel>x\<parallel> * K"
using bdd_linear_put
unfolding bounded_linear_def bounded_linear_axioms_def
by auto
show "bounded_linear (\<lambda>z. z *\<^sub>R op (f' \<tau>) v)"
using bounded_linear_scaleR_const[OF bounded_linear_ident] .
{fix e::real
assume "e > 0"
obtain K where K_def: "\<forall>x. \<parallel>op x v\<parallel> \<le> \<parallel>x\<parallel> * K"
using bdd_put by auto
hence "K \<ge> 0"
using order.trans[OF norm_ge_zero, of "op _ v" "\<parallel>_\<parallel> * K", unfolded zero_le_mult_iff]
non_trivial_state_space by auto
have "\<parallel>op (?quot f f' \<tau> t) v\<parallel> \<le> \<parallel>?quot f f' \<tau> t\<parallel> * K" for t
using K_def
by (erule_tac x="?quot f f' \<tau> t" in allE, force)
moreover note f_hyps[THEN bspec[OF _ \<open>\<tau> \<in> T\<close>], THEN conjunct2, unfolded tendsto_iff eventually_at dist_norm]
ultimately have "\<exists>d>0. \<forall>t\<in>T. t \<noteq> \<tau> \<and> \<parallel>t - \<tau>\<parallel> < d \<longrightarrow> \<parallel>op (?quot f f' \<tau> t) v\<parallel> < e"
using \<open>e > 0\<close> \<open>K \<ge> 0\<close>
apply (cases "K = 0", force)
apply (erule_tac x="e / K" in allE, clarsimp)
apply (rule_tac x=d in exI, clarsimp)
apply (erule_tac x=t in ballE; clarsimp)
by (smt (verit, ccfv_SIG) mult_imp_div_pos_le)
}
thus "((\<lambda>y. (op (f y) v - op (f \<tau>) v - (y - \<tau>) *\<^sub>R op (f' \<tau>) v) /\<^sub>R \<bar>y - \<tau>\<bar>) \<longlongrightarrow> 0) (at \<tau> within T)"
apply (fold linear_cmul[OF linearv] linear_diff[OF linearv])+
unfolding tendsto_iff dist_norm eventually_at by force
qed
lemma vderiv_on_blopI1:
fixes op :: "'a::real_normed_vector \<Rightarrow> 'b \<Rightarrow> 'c::real_normed_vector"
assumes "\<forall>v. bounded_linear (\<lambda>s. op s v)"
and "\<exists>s::'a. s \<noteq> 0"
and "D f = f' on T"
and "g = (\<lambda>t. op (f' t) v)"
shows "D (\<lambda>t. op (f t) v) = g on T"
using bdd_linear_2op_has_vderiv_onL[OF assms(1-3)] assms(4)
by auto
lemma leibniz_rule_bdd_bilinear':
fixes f :: "real \<Rightarrow> 'a::banach"
and op :: "'a \<Rightarrow> 'b::real_normed_vector \<Rightarrow> 'c::banach"
assumes bdd_bil: "bounded_bilinear op"
and h_eq: "h = (\<lambda>t. op (f t) (g' t) + op (f' t) (g t))"
and df: "D f = f' on {a--b}"
and dg: "D g = g' on S'"
and dg': "D g' = g'' on S''"
and "{a--b} \<subseteq> S'" "{a--b} \<subseteq> S''"
shows "D (\<lambda>t. op (f t) (g t) - (ivl_integral a t (\<lambda>\<tau>. (op (f \<tau>) (g' \<tau>))))) = (\<lambda>t. op (f' t) (g t)) on {a--b}"
by (auto intro!: vderiv_intros vderiv_bdd_bilinearI[OF bdd_bil _ df dg \<open>{a--b} \<subseteq> S'\<close>]
vderiv_bdd_bilinearI[OF bdd_bil _ df dg' \<open>{a--b} \<subseteq> S''\<close>])
thm vderiv_ivl_integralI
ivl_integral_has_vector_derivative_compact_interval
ivl_integral_has_vderiv_on_compact_interval
ivl_integral_has_vderiv_on_subset_segment
ivl_integral_has_vderiv_on
ivl_integral_has_vector_derivative
has_vector_derivative_transform
integral_has_vector_derivative
has_integral_def[unfolded tendsto_iff dist_norm]
subsection \<open> Intermediate Value Theorem \<close>
lemma IVT_two_functions:
fixes f :: "('a::{linear_continuum_topology, real_vector}) \<Rightarrow>
('b::{linorder_topology,real_normed_vector,ordered_ab_group_add})"
assumes conts: "continuous_on {a..b} f" "continuous_on {a..b} g"
and ahyp: "f a < g a" and bhyp: "g b < f b " and "a \<le> b"
shows "\<exists>x\<in>{a..b}. f x = g x"
proof-
let "?h x" = "f x - g x"
have "?h a \<le> 0" and "?h b \<ge> 0"
using ahyp bhyp by simp_all
also have "continuous_on {a..b} ?h"
using conts continuous_on_diff by blast
ultimately obtain x where "a \<le> x" "x \<le> b" and "?h x = 0"
using IVT'[of "?h"] \<open>a \<le> b\<close> by blast
thus ?thesis
using \<open>a \<le> b\<close> by auto
qed
lemma IVT_two_functions_real_ivl:
fixes f :: "real \<Rightarrow> real"
assumes conts: "continuous_on {a--b} f" "continuous_on {a--b} g"
and ahyp: "f a < g a" and bhyp: "g b < f b "
shows "\<exists>x\<in>{a--b}. f x = g x"
proof(cases "a \<le> b")
case True
then show ?thesis
using IVT_two_functions assms
unfolding closed_segment_eq_real_ivl by auto
next
case False
hence "a \<ge> b"
by auto
hence "continuous_on {b..a} f" "continuous_on {b..a} g"
using conts False unfolding closed_segment_eq_real_ivl by auto
hence "\<exists>x\<in>{b..a}. g x = f x"
using IVT_two_functions[of b a g f] assms(3,4) False by auto
then show ?thesis
using \<open>a \<ge> b\<close> unfolding closed_segment_eq_real_ivl by auto force
qed
lemma mvt_ivl_general:
fixes f :: "real \<Rightarrow> 'a::real_inner"
assumes "a \<noteq> b" and "continuous_on {a--b} f"
and "\<forall>x\<in>{a<--<b}. D f \<mapsto> (f' x) (at x)"
shows "\<exists>x\<in>{a<--<b}. \<parallel>f b - f a\<parallel> \<le> \<parallel>f' x \<bar>b - a\<bar>\<parallel>"
proof(cases "a < b")
case True
thus ?thesis
using closed_segment_eq_real_ivl open_segment_eq_real_ivl
assms mvt_general[of a b f] by force
next
case False
hence "b < a" "{a--b} = {b..a}" "{a<--<b} = {b<..<a}"
using assms closed_segment_eq_real_ivl open_segment_eq_real_ivl by auto
hence cont: "continuous_on {b..a} f" and "\<forall>x\<in>{b<..<a}. D f \<mapsto> (f' x) (at x)"
using assms by auto
hence "\<exists>x\<in>{b<..<a}. \<parallel>f b - f a\<parallel> \<le> \<parallel>f' x \<bar>a - b\<bar>\<parallel>"
using mvt_general[OF \<open>b < a\<close> cont, of f']
by (auto simp: Real_Vector_Spaces.real_normed_vector_class.norm_minus_commute)
thus ?thesis
by (subst \<open>{a<--<b} = {b<..<a}\<close>) auto
qed
subsection \<open> Derivative tests \<close>
definition "increasing_on T f \<longleftrightarrow> (\<forall>x\<in>T. \<forall>y\<in>T. x \<le> y \<longrightarrow> f x \<le> f y)"
lemma increasing_on_trans:
fixes f :: "'a::linorder \<Rightarrow> 'b::preorder"
shows "a \<le> b \<Longrightarrow> b \<le> c \<Longrightarrow> increasing_on {a..b} f
\<Longrightarrow> increasing_on {b..c} f \<Longrightarrow> increasing_on {a..c} f"
unfolding increasing_on_def
by (auto simp: Ball_def)
(metis dual_order.trans nle_le)
definition "decreasing_on T f \<longleftrightarrow> (\<forall>x\<in>T. \<forall>y\<in>T. x \<le> y \<longrightarrow> f y \<le> f x)"
lemma decreasing_on_trans:
fixes f :: "'a::linorder \<Rightarrow> 'b::preorder"
shows "a \<le> b \<Longrightarrow> b \<le> c \<Longrightarrow> decreasing_on {a..b} f
\<Longrightarrow> decreasing_on {b..c} f \<Longrightarrow> decreasing_on {a..c} f"
unfolding decreasing_on_def
by auto (smt (verit, best) intervalE nle_le order_trans)
definition "strict_increasing_on T f \<longleftrightarrow> (\<forall>x\<in>T. \<forall>y\<in>T. x < y \<longrightarrow> f x < f y)"
lemma strict_increasing_on_trans:
fixes f :: "'a::linorder \<Rightarrow> 'b::preorder"
shows "a \<le> b \<Longrightarrow> b \<le> c \<Longrightarrow> strict_increasing_on {a..b} f
\<Longrightarrow> strict_increasing_on {b..c} f \<Longrightarrow> strict_increasing_on {a..c} f"
unfolding strict_increasing_on_def
by auto (smt (verit, best) intervalE linorder_le_cases
order_less_trans verit_comp_simplify1(3))
definition "strict_decreasing_on T f \<longleftrightarrow> (\<forall>x\<in>T. \<forall>y\<in>T. x < y \<longrightarrow> f y < f x)"
lemma strict_decreasing_on_trans:
fixes f :: "'a::linorder \<Rightarrow> 'b::preorder"
shows "a \<le> b \<Longrightarrow> b \<le> c \<Longrightarrow> strict_decreasing_on {a..b} f
\<Longrightarrow> strict_decreasing_on {b..c} f \<Longrightarrow> strict_decreasing_on {a..c} f"
unfolding strict_decreasing_on_def
by auto (smt (verit, best) intervalE linorder_le_cases
order_less_trans verit_comp_simplify1(3))
definition "local_maximum_at T f x \<longleftrightarrow> (\<forall>y\<in>T. f y \<le> f x)"
lemma increasing_on_local_maximum:
fixes f :: "'a::preorder \<Rightarrow> 'b::preorder"
shows "a \<le> b \<Longrightarrow> increasing_on {a..b} f \<Longrightarrow> local_maximum_at {a..b} f b"
by (auto simp: increasing_on_def local_maximum_at_def)
lemma decreasing_on_local_maximum:
fixes f :: "'a::preorder \<Rightarrow> 'b::preorder"
shows "a \<le> b \<Longrightarrow> decreasing_on {a..b} f \<Longrightarrow> local_maximum_at {a..b} f a"
by (auto simp: decreasing_on_def local_maximum_at_def)
lemma incr_decr_local_maximum:
fixes f :: "'a::linorder \<Rightarrow> 'b::preorder"
shows "a \<le> b \<Longrightarrow> b \<le> c \<Longrightarrow> increasing_on {a..b} f
\<Longrightarrow> decreasing_on {b..c} f \<Longrightarrow> local_maximum_at {a..c} f b"
unfolding increasing_on_def decreasing_on_def local_maximum_at_def
by auto (metis intervalE linorder_le_cases)
definition "local_minimum_at T f x \<longleftrightarrow> (\<forall>y\<in>T. f y \<ge> f x)"
lemma increasing_on_local_minimum:
fixes f :: "'a::preorder \<Rightarrow> 'b::preorder"
shows "a \<le> b \<Longrightarrow> increasing_on {a..b} f \<Longrightarrow> local_minimum_at {a..b} f a"
by (auto simp: increasing_on_def local_minimum_at_def)
lemma decreasing_on_local_minimum:
fixes f :: "'a::preorder \<Rightarrow> 'b::preorder"
shows "a \<le> b \<Longrightarrow> decreasing_on {a..b} f \<Longrightarrow> local_minimum_at {a..b} f b"
by (auto simp: decreasing_on_def local_minimum_at_def)
lemma incr_decr_local_minimum:
fixes f :: "'a::linorder \<Rightarrow> 'b::preorder"
shows "a \<le> b \<Longrightarrow> b \<le> c \<Longrightarrow> decreasing_on {a..b} f
\<Longrightarrow> increasing_on {b..c} f \<Longrightarrow> local_minimum_at {a..c} f b"
unfolding increasing_on_def decreasing_on_def local_minimum_at_def
by auto (metis intervalE linorder_le_cases)
lemma has_vderiv_mono_test:
assumes T_hyp: "is_interval T"
and d_hyp: "D f = f' on T"
and xy_hyp: "x\<in>T" "y\<in>T" "x \<le> y"
shows "\<forall>x\<in>T. (0::real) \<le> f' x \<Longrightarrow> f x \<le> f y"
and "\<forall>x\<in>T. f' x \<le> 0 \<Longrightarrow> f x \<ge> f y"
proof-
have "{x..y} \<subseteq> T"
using T_hyp xy_hyp by (meson atLeastAtMost_iff mem_is_interval_1_I subsetI)
hence "D f = f' on {x..y}"
using has_vderiv_on_subset[OF d_hyp(1)] by blast
hence "(\<And>t. x \<le> t \<Longrightarrow> t \<le> y \<Longrightarrow> D f \<mapsto> (\<lambda>\<tau>. \<tau> *\<^sub>R f' t) at t within {x..y})"
unfolding has_vderiv_on_def has_vector_derivative_def by auto
then obtain c where c_hyp: "c \<in> {x..y} \<and> f y - f x = (y - x) *\<^sub>R f' c"
using mvt_very_simple[OF xy_hyp(3), of f "(\<lambda>t \<tau>. \<tau> *\<^sub>R f' t)"] by blast
hence mvt_hyp: "f x = f y - f' c * (y - x)"
by (simp add: mult.commute)
also have "\<forall>x\<in>T. 0 \<le> f' x \<Longrightarrow> ... \<le> f y"
using xy_hyp d_hyp c_hyp \<open>{x..y} \<subseteq> T\<close> by auto
finally show "\<forall>x\<in>T. 0 \<le> f' x \<Longrightarrow> f x \<le> f y" .
have "\<forall>x\<in>T. f' x \<le> 0 \<Longrightarrow> f y - f' c * (y - x) \<ge> f y"
using xy_hyp d_hyp c_hyp \<open>{x..y} \<subseteq> T\<close> by (auto simp: mult_le_0_iff)
thus "\<forall>x\<in>T. f' x \<le> 0 \<Longrightarrow> f x \<ge> f y"
using mvt_hyp by auto
qed
lemma first_derivative_test:
assumes T_hyp: "is_interval T"
and d_hyp: "D f = f' on T"
shows "\<forall>x\<in>T. (0::real) \<le> f' x \<Longrightarrow> increasing_on T f"
and "\<forall>x\<in>T. f' x \<le> 0 \<Longrightarrow> decreasing_on T f"
unfolding increasing_on_def decreasing_on_def
using has_vderiv_mono_test[OF assms] by blast+
definition "neighbourhood N x \<longleftrightarrow> (\<exists>X. open X \<and> x \<in> X \<and> X \<subseteq> N)"
lemma neighbourhood_iff: "neighbourhood N x \<longleftrightarrow> (\<exists>\<epsilon>>0. ball x \<epsilon> \<subseteq> N)"
using neighbourhood_def[of N x, unfolded open_contains_ball]
by (metis Elementary_Metric_Spaces.open_ball centre_in_ball
open_contains_ball_eq order_trans)
lemma in_neighbourhood: "neighbourhood N x \<Longrightarrow> x \<in> N"
by (auto simp: neighbourhood_def)
lemma tendsto_at_within_topological:
"((f::'a::topological_space \<Rightarrow> 'b::topological_space) \<longlongrightarrow> l) (at x within X)
\<longleftrightarrow> (\<forall>B. open B \<longrightarrow> l \<in> B \<longrightarrow> (\<exists>A. open A \<and> x \<in> A \<and> (\<forall>y\<in>A. y \<noteq> x \<longrightarrow> y \<in> X \<longrightarrow> f y \<in> B)))"
using tendsto_def[where F="at _ within _", unfolded eventually_at_topological, of f l x X]
by blast
lemma continuous_on_Ex_open_less:
fixes f :: "'a :: topological_space \<Rightarrow> real"
assumes "continuous_on T f"
and "neighbourhood T t"
shows "f t > c \<Longrightarrow> \<exists>X. open X \<and> t \<in> X \<and> X \<subseteq> T \<and> (\<forall>\<tau>\<in>X. f \<tau> > c)"
and "f t < c \<Longrightarrow> \<exists>X. open X \<and> t \<in> X \<and> X \<subseteq> T \<and> (\<forall>\<tau>\<in>X. f \<tau> < c)"
proof-
assume "c < f t"
then obtain X\<^sub>1 where "open X\<^sub>1" and "t \<in> X\<^sub>1"
and ge_dist: "\<forall>y\<in>T. y \<in> X\<^sub>1 \<longrightarrow> dist (f t) (f y) < f t - c"
using continuous_on_topological[THEN iffD1, rule_format,
OF assms(1)
in_neighbourhood[OF assms(2)]
open_ball[of "f t" "f t - c"]
centre_in_ball[THEN iffD2, of "f t - c" "f t"],
unfolded ball_def, simplified]
by blast
moreover obtain X\<^sub>2 where "open X\<^sub>2" and "t \<in> X\<^sub>2" and "X\<^sub>2 \<subseteq> T"
using \<open>neighbourhood T t\<close> neighbourhood_def
by metis
moreover define X where X_def: "X = X\<^sub>1 \<inter> X\<^sub>2"
ultimately have "open X" and "X \<subseteq> T" and "t \<in> X" and "\<forall>\<tau>\<in>X. f \<tau> > c"
using \<open>open X\<^sub>1\<close> \<open>open X\<^sub>2\<close> \<open>X\<^sub>2 \<subseteq> T\<close> dist_real_def
by (auto simp: X_def open_Int)
thus "\<exists>X. open X \<and> t \<in> X \<and> X \<subseteq> T \<and> (\<forall>\<tau>\<in>X. f \<tau> > c)"
by blast
next
assume "f t < c"
then obtain X\<^sub>1 where "open X\<^sub>1" and "t \<in> X\<^sub>1"
and ge_dist: "\<forall>y\<in>T. y \<in> X\<^sub>1 \<longrightarrow> dist (f t) (f y) < c - f t"
using continuous_on_topological[THEN iffD1, rule_format,
OF assms(1)
in_neighbourhood[OF assms(2)]
open_ball[of "f t" "c - f t"]
centre_in_ball[THEN iffD2, of "c - f t" "f t"],
unfolded ball_def, simplified]
by blast
moreover obtain X\<^sub>2 where "open X\<^sub>2" and "t \<in> X\<^sub>2" and "X\<^sub>2 \<subseteq> T"
using \<open>neighbourhood T t\<close> neighbourhood_def
by metis
moreover define X where X_def: "X = X\<^sub>1 \<inter> X\<^sub>2"
ultimately have "open X" and "X \<subseteq> T" and "t \<in> X" and "\<forall>\<tau>\<in>X. f \<tau> < c"
using \<open>open X\<^sub>1\<close> \<open>open X\<^sub>2\<close> \<open>X\<^sub>2 \<subseteq> T\<close> dist_real_def
by (auto simp: X_def open_Int)
thus "\<exists>X. open X \<and> t \<in> X \<and> X \<subseteq> T \<and> (\<forall>\<tau>\<in>X. f \<tau> < c)"
by blast
qed
lemma continuous_on_Ex_ball_less':
"continuous_on T f \<Longrightarrow> x \<in> T \<Longrightarrow> f x > (k::real) \<Longrightarrow> \<exists>\<epsilon>>0. \<forall>y\<in>ball x \<epsilon> \<inter> T. f y > k"
unfolding continuous_on_iff apply(erule_tac x=x in ballE; clarsimp?)
apply(erule_tac x="f x - k" in allE, clarsimp simp: dist_norm)
apply(rename_tac \<delta>, rule_tac x=\<delta> in exI, clarsimp)
apply(erule_tac x=y in ballE; clarsimp?)
by (subst (asm) abs_le_eq, simp_all add: dist_commute)
lemma continuous_on_Ex_ball_less:
fixes f :: "'a :: metric_space \<Rightarrow> real"
assumes "continuous_on T f"
and "neighbourhood T t"
shows "f t > c \<Longrightarrow> \<exists>\<epsilon>>0. \<forall>\<tau>\<in>ball t \<epsilon>. f \<tau> > c \<and> \<tau> \<in> T"
and "f t < c \<Longrightarrow> \<exists>\<epsilon>>0. \<forall>\<tau>\<in>ball t \<epsilon>. f \<tau> < c \<and> \<tau> \<in> T"
proof-
obtain X where Ex_ball: "\<forall>x\<in>X. \<exists>e>0. ball x e \<subseteq> X"
and "X \<subseteq> T"
and conseq1: "c < f t \<longrightarrow> t \<in> X \<and> (\<forall>\<tau>\<in>X. c < f \<tau>)"
and conseq2: "c > f t \<longrightarrow> t \<in> X \<and> (\<forall>\<tau>\<in>X. c > f \<tau>)"
using continuous_on_Ex_open_less[OF assms, unfolded open_contains_ball, of c]
by (cases "c < f t") (atomize_elim, clarsimp, blast)+
{assume "c < f t"
then obtain \<epsilon> where "\<epsilon> > 0" and "ball t \<epsilon> \<subseteq> X" and "\<forall>\<tau>\<in>ball t \<epsilon>. f \<tau> > c"
using Ex_ball conseq1
by (meson subsetD)
hence "\<exists>\<epsilon>>0. \<forall>\<tau>\<in>ball t \<epsilon>. f \<tau> > c \<and> \<tau> \<in> T"
using \<open>X \<subseteq> T\<close>
by auto}
thus "f t > c \<Longrightarrow> \<exists>\<epsilon>>0. \<forall>\<tau>\<in>ball t \<epsilon>. f \<tau> > c \<and> \<tau> \<in> T" .
{assume "c > f t"
then obtain \<epsilon> where "\<epsilon> > 0" and "ball t \<epsilon> \<subseteq> X" and "\<forall>\<tau>\<in>ball t \<epsilon>. f \<tau> < c"
using Ex_ball conseq2
by (meson subsetD)
hence "\<exists>\<epsilon>>0. \<forall>\<tau>\<in>ball t \<epsilon>. f \<tau> < c \<and> \<tau> \<in> T"
using \<open>X \<subseteq> T\<close>
by auto}
thus "f t < c \<Longrightarrow> \<exists>\<epsilon>>0. \<forall>\<tau>\<in>ball t \<epsilon>. f \<tau> < c \<and> \<tau> \<in> T" .
qed
lemma has_vderiv_max_test:
assumes "continuous_on T f''"
and "neighbourhood T t"
and f': "D f = f' on T"
and f'': "D f' = f'' on T"
and "f' t = (0 :: real)"
shows "f'' t < 0 \<Longrightarrow> \<exists>a b. a < t \<and> t < b \<and> {a--b} \<subseteq> T \<and> (\<forall>\<tau>\<in>{a--b}. f \<tau> \<le> f t)"
proof-
assume "f'' t < 0"
then obtain a b where Ex_ivl: "a < t \<and> t < b \<and> {a--b} \<subseteq> T \<and> (\<forall>\<tau>\<in>{a--b}. f'' \<tau> < 0)"
using continuous_on_Ex_open_less(2)[OF assms(1,2)] open_contains_cball real_ivl_eqs(7)
by (smt (verit) centre_in_cball dual_order.trans subsetD)
hence "{a--t} \<subseteq> T" and "{t--b} \<subseteq> T"
and "a < t" and "t < b"
by (auto simp: closed_segment_eq_real_ivl)
{fix \<tau>
assume "a \<le> \<tau>" and "\<tau> \<le> t"
hence "{\<tau>--t} \<subseteq> T"
using \<open>{a--t} \<subseteq> T\<close> closed_segment_eq_real_ivl1
by force
then obtain r where "r \<in> {\<tau>--t}" and r_obs: "f' t - f' \<tau> = (t - \<tau>) * f'' r"
using \<open>\<tau> \<le> t\<close> mvt_very_simple_closed_segmentE[OF has_vderiv_on_subset[OF f'' \<open>{\<tau>--t} \<subseteq> T\<close>]]
by blast
hence "r \<in> {a--b}"
by (metis Ex_ivl \<open>\<tau> <= t\<close> \<open>a \<le> \<tau>\<close> atLeastAtMost_iff closed_segment_eq_real_ivl1
order.order_iff_strict order_le_less_trans)
hence "f'' r < 0"
using Ex_ivl
by blast
hence "0 \<le> f' \<tau>"
using r_obs \<open>f' t = 0\<close> \<open>\<tau> \<le> t\<close>
by (metis diff_gt_0_iff_gt linorder_not_less order.order_iff_strict zero_le_mult_iff)
}
hence "\<forall>x\<in>{a..t}. 0 \<le> f' x"
by (simp add: order.order_iff_strict)
hence "\<And>x y. a \<le> x \<and> x \<le> t \<Longrightarrow> a \<le> y \<and> y \<le> t \<Longrightarrow> x \<le> y \<Longrightarrow> f x \<le> f y"
using has_vderiv_mono_test(1)[OF _ has_vderiv_on_subset[OF f' \<open>{a--t} \<subseteq> T\<close>]]
by (auto simp: closed_segment_eq_real_ivl)
hence max_left: "\<forall>\<tau>\<in>{a--t}. f \<tau> \<le> f t"
using \<open>a < t\<close> \<open>t < b\<close>
by (auto simp: closed_segment_eq_real_ivl)
{fix \<tau>
assume "t \<le> \<tau>" and "\<tau> \<le> b"
hence "{t--\<tau>} \<subseteq> T"
using \<open>{t--b} \<subseteq> T\<close> closed_segment_eq_real_ivl1
by force
then obtain r where "r \<in> {t--\<tau>}" and r_obs: "f' \<tau> - f' t = (\<tau> - t) * f'' r"
using \<open>t \<le> \<tau>\<close> mvt_very_simple_closed_segmentE[OF has_vderiv_on_subset[OF f'' \<open>{t--\<tau>} \<subseteq> T\<close>]]
by blast
hence "r \<in> {a--b}"
by (metis Ex_ivl \<open>t \<le> \<tau>\<close> \<open>\<tau> \<le> b\<close> atLeastAtMost_iff closed_segment_eq_real_ivl1
order.order_iff_strict order_le_less_trans)
hence "f'' r < 0"
using Ex_ivl
by blast
hence "0 \<ge> f' \<tau>"
using r_obs \<open>f' t = 0\<close> \<open>\<tau> \<ge> t\<close>
by (metis diff_gt_0_iff_gt linorder_not_less order.order_iff_strict zero_le_mult_iff)
}
hence "\<forall>x\<in>{t..b}. f' x \<le> 0"
by (simp add: order.order_iff_strict)
hence "\<And>x y. t \<le> x \<and> x \<le> b \<Longrightarrow> t \<le> y \<and> y \<le> b \<Longrightarrow> x \<le> y \<Longrightarrow> f y \<le> f x"
using has_vderiv_mono_test(2)[OF _ has_vderiv_on_subset[OF f' \<open>{t--b} \<subseteq> T\<close>]]
by (auto simp: closed_segment_eq_real_ivl)
hence max_right: "\<forall>\<tau>\<in>{t--b}. f \<tau> \<le> f t"
using \<open>a < t\<close> \<open>t < b\<close>
by (auto simp: closed_segment_eq_real_ivl)
hence "\<forall>\<tau>\<in>{a--b}. f \<tau> \<le> f t"
using \<open>a < t\<close> \<open>t < b\<close> max_left
by (metis atLeastAtMost_iff closed_segment_eq_real_ivl1 inf.order_iff
le_inf_iff linorder_le_cases order.order_iff_strict)
thus ?thesis
using Ex_ivl by blast
qed
lemma has_vderiv_min_test:
assumes "continuous_on T f''"
and "neighbourhood T t"
and f': "D f = f' on T"
and f'': "D f' = f'' on T"
and "f' t = (0 :: real)"
shows "f'' t > 0 \<Longrightarrow> \<exists>a b. a < t \<and> t < b \<and> {a--b} \<subseteq> T \<and> (\<forall>\<tau>\<in>{a--b}. f t \<le> f \<tau>)"
proof-
assume "f'' t > 0"
then obtain a b where Ex_ivl: "a < t \<and> t < b \<and> {a--b} \<subseteq> T \<and> (\<forall>\<tau>\<in>{a--b}. f'' \<tau> > 0)"
using continuous_on_Ex_open_less(1)[OF assms(1,2)] open_contains_cball real_ivl_eqs(7)
by (smt (verit) centre_in_cball dual_order.trans subsetD)
hence "{a--t} \<subseteq> T" and "{t--b} \<subseteq> T"
and "a < t" and "t < b"
by (auto simp: closed_segment_eq_real_ivl)
{fix \<tau>
assume "a \<le> \<tau>" and "\<tau> \<le> t"
hence "{\<tau>--t} \<subseteq> T"
using \<open>{a--t} \<subseteq> T\<close> closed_segment_eq_real_ivl1
by force
then obtain r where "r \<in> {\<tau>--t}" and r_obs: "f' t - f' \<tau> = (t - \<tau>) * f'' r"
using \<open>\<tau> \<le> t\<close> mvt_very_simple_closed_segmentE[OF has_vderiv_on_subset[OF f'' \<open>{\<tau>--t} \<subseteq> T\<close>]]
by blast
hence "r \<in> {a--b}"
by (metis Ex_ivl \<open>\<tau> <= t\<close> \<open>a \<le> \<tau>\<close> atLeastAtMost_iff closed_segment_eq_real_ivl1
order.order_iff_strict order_le_less_trans)
hence "f'' r > 0"
using Ex_ivl
by blast
hence "0 \<ge> f' \<tau>"
using r_obs \<open>f' t = 0\<close> \<open>\<tau> \<le> t\<close>
by (metis diff_gt_0_iff_gt less_iff_diff_less_0 linorder_not_le
not_less_iff_gr_or_eq pos_prod_lt)
}
hence "\<forall>x\<in>{a..t}. f' x \<le> 0"
by (simp add: order.order_iff_strict)
hence "\<And>x y. a \<le> x \<and> x \<le> t \<Longrightarrow> a \<le> y \<and> y \<le> t \<Longrightarrow> x \<le> y \<Longrightarrow> f y \<le> f x"
using has_vderiv_mono_test(2)[OF _ has_vderiv_on_subset[OF f' \<open>{a--t} \<subseteq> T\<close>]]
by (auto simp: closed_segment_eq_real_ivl)
hence min_left: "\<forall>\<tau>\<in>{a--t}. f t \<le> f \<tau>"
using \<open>a < t\<close> \<open>t < b\<close>
by (auto simp: closed_segment_eq_real_ivl)
{fix \<tau>
assume "t \<le> \<tau>" and "\<tau> \<le> b"
hence "{t--\<tau>} \<subseteq> T"
using \<open>{t--b} \<subseteq> T\<close> closed_segment_eq_real_ivl1
by force
then obtain r where "r \<in> {t--\<tau>}" and r_obs: "f' \<tau> - f' t = (\<tau> - t) * f'' r"
using \<open>t \<le> \<tau>\<close> mvt_very_simple_closed_segmentE[OF has_vderiv_on_subset[OF f'' \<open>{t--\<tau>} \<subseteq> T\<close>]]
by blast
hence "r \<in> {a--b}"
by (metis Ex_ivl \<open>t \<le> \<tau>\<close> \<open>\<tau> \<le> b\<close> atLeastAtMost_iff closed_segment_eq_real_ivl1
order.order_iff_strict order_le_less_trans)
hence "f'' r > 0"
using Ex_ivl
by blast
hence "0 \<le> f' \<tau>"
using r_obs \<open>f' t = 0\<close> \<open>\<tau> \<ge> t\<close>
by fastforce
}
hence "\<forall>x\<in>{t..b}. f' x \<ge> 0"
by (simp add: order.order_iff_strict)
hence "\<And>x y. t \<le> x \<and> x \<le> b \<Longrightarrow> t \<le> y \<and> y \<le> b \<Longrightarrow> x \<le> y \<Longrightarrow> f x \<le> f y"
using has_vderiv_mono_test(1)[OF _ has_vderiv_on_subset[OF f' \<open>{t--b} \<subseteq> T\<close>]]
by (auto simp: closed_segment_eq_real_ivl)
hence max_right: "\<forall>\<tau>\<in>{t--b}. f t \<le> f \<tau>"
using \<open>a < t\<close> \<open>t < b\<close>
by (auto simp: closed_segment_eq_real_ivl)
hence "\<forall>\<tau>\<in>{a--b}. f t \<le> f \<tau>"
using \<open>a < t\<close> \<open>t < b\<close> min_left
by (metis atLeastAtMost_iff closed_segment_eq_real_ivl1 inf.order_iff
le_inf_iff linorder_le_cases order.order_iff_strict)
thus ?thesis
using Ex_ivl by blast
qed
lemma second_derivative_test:
assumes "continuous_on T f''"
and "neighbourhood T t"
and f': "D f = f' on T"
and f'': "D f' = f'' on T"
and "f' t = (0 :: real)"
shows "f'' t < 0 \<Longrightarrow> \<exists>a b. a < t \<and> t < b \<and> {a--b} \<subseteq> T \<and> local_maximum_at {a--b} f t"
and "f'' t > 0 \<Longrightarrow> \<exists>a b. a < t \<and> t < b \<and> {a--b} \<subseteq> T \<and> local_minimum_at {a--b} f t"
unfolding local_maximum_at_def local_minimum_at_def
using has_vderiv_max_test[OF assms] has_vderiv_min_test[OF assms]
by blast+
subsection \<open> Filters \<close>
lemma eventually_at_within_mono:
assumes "t \<in> interior T" and "T \<subseteq> S"
and "eventually P (at t within T)"
shows "eventually P (at t within S)"
by (meson assms eventually_within_interior interior_mono subsetD)
lemma netlimit_at_within_mono:
fixes t::"'a::{perfect_space,t2_space}"
assumes "t \<in> interior T" and "T \<subseteq> S"
shows "netlimit (at t within S) = t"
using assms(1) interior_mono[OF \<open>T \<subseteq> S\<close>] netlimit_within_interior by auto
lemma has_derivative_at_within_mono:
fixes t::"'a::{perfect_space,t2_space,real_normed_vector}"
assumes "t \<in> interior T" and "T \<subseteq> S"
and "D f \<mapsto> f' at t within T"
shows "D f \<mapsto> f' at t within S"
using assms(3) apply(unfold has_derivative_def tendsto_iff, safe)
unfolding netlimit_at_within_mono[OF assms(1,2)] netlimit_within_interior[OF assms(1)]
by (rule eventually_at_within_mono[OF assms(1,2)]) simp
lemma eventually_all_finite2:
fixes P :: "('a::finite) \<Rightarrow> 'b \<Rightarrow> bool"
assumes h:"\<forall>i. eventually (P i) F"
shows "eventually (\<lambda>x. \<forall>i. P i x) F"
proof(unfold eventually_def)