-
Notifications
You must be signed in to change notification settings - Fork 10
/
Polynomial.v
1759 lines (1579 loc) · 56.3 KB
/
Polynomial.v
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
Require Import Psatz.
Require Import String.
Require Export Complex.
Require Import List.
Require Import Setoid.
Declare Scope poly_scope.
Delimit Scope poly_scope with P.
Open Scope poly_scope.
(* some prelim lemmas that should be moved *)
Lemma diff_pow_n : forall (n : nat) (a b : C),
(a^(S n) - b^(S n)) = (a - b) * (big_sum (fun i => a^i * b^(n - i)) (S n)).
Proof. intros.
unfold Cminus.
rewrite Cmult_plus_distr_r.
do 2 rewrite (@big_sum_mult_l C _ _ _ C_is_ring).
rewrite <- big_sum_extend_r, <- big_sum_extend_l, Nat.sub_diag, Nat.sub_0_r.
simpl.
replace (a * (a ^ n * C1)) with (a^ (S n)) by lca.
replace (- b * (C1 * b ^ n)) with (- b^(S n)) by lca.
rewrite (Cplus_comm _ (a ^ _)), <- Cplus_assoc.
apply Cplus_simplify; try easy.
rewrite Cplus_assoc, (Cplus_comm _ (- b ^ _)), <- (Cplus_0_r (- b ^ _)).
do 2 rewrite <- Cplus_assoc.
rewrite <- (Cplus_0_r (- (b * b ^ n))).
apply Cplus_simplify; try easy.
rewrite <- (@big_sum_plus C _ _ C_is_comm_group), Cplus_0_l.
rewrite big_sum_0_bounded; auto.
intros.
replace (n - x)%nat with (S (n - S x))%nat by lia.
lca.
Qed.
Lemma destruct_from_last : forall {X} (n : nat) (d : X) (l : list X),
length l = S n -> l = (firstn n l) ++ [nth n l d].
Proof. induction n as [| n'].
- intros.
repeat (destruct l; try easy).
- intros.
destruct l; try easy.
simpl in *.
apply Nat.succ_inj in H.
rewrite <- IHn'; easy.
Qed.
Lemma ind_from_end_helper : forall {X} (p : list X -> Prop) (n : nat),
p [] ->
(forall (x : X) (l : list X), p l -> p (l ++ [x])) ->
forall l, length l = n -> p l.
Proof. intros X p n H H0.
induction n as [| n'].
- intros.
destruct l; try easy.
- intros. destruct l; try easy.
rewrite (destruct_from_last n' x); auto.
apply H0.
apply IHn'.
rewrite firstn_length, H1.
rewrite min_l; lia.
Qed.
Lemma ind_from_end : forall {X} (p : list X -> Prop),
p [] ->
(forall (x : X) (l : list X), p l -> p (l ++ [x])) ->
forall l, p l.
Proof. intros.
apply (ind_from_end_helper _ (length l)); easy.
Qed.
(* polynomial represented by a list of coeficients and a degree*)
Definition Polynomial := list C.
Definition Peval (p : Polynomial) (x : Complex.C):=
big_sum (fun i => (nth i p C0)* x^i) (length p).
Definition Peq (p1 p2 : Polynomial) :=
Peval p1 = Peval p2.
Fixpoint Pplus (p1 p2 : Polynomial) : Polynomial :=
match p1 with
| [] => p2
| (a1 :: p1') =>
match p2 with
| [] => p1
| (a2 :: p2') => (a1 + a2) :: (Pplus p1' p2')
end
end.
Fixpoint Pmult (p1 p2 : Polynomial) : Polynomial :=
match p1 with
| [] => []
| (a1 :: p1') => Pplus (map (Cmult a1) p2) (C0 :: (Pmult p1' p2))
end.
Definition Popp (p : Polynomial) : Polynomial :=
Pmult [-C1] p.
Global Program Instance P_is_monoid : Monoid Polynomial :=
{ Gzero := []
; Gplus := Pplus
}.
Solve All Obligations with program_simpl; try lca.
Next Obligation. induction g as [| g']; easy. Qed.
Next Obligation.
assert (H' : forall p1 p2 p3, Pplus (Pplus p1 p2) p3 = Pplus p1 (Pplus p2 p3)).
induction p1 as [| a]; try easy.
intros.
destruct p2; destruct p3; try easy; simpl.
rewrite IHp1, Cplus_assoc; easy.
rewrite H'; easy.
Qed.
(*
Fixpoint Psum (f : nat -> Polynomial) (n : nat) : Polynomial :=
match n with
| 0 => [C0]
| S n' => (Pplus (Psum f n') (f n'))
end.
*)
Infix "≅" := Peq (at level 70) : poly_scope.
Infix "+," := Pplus (at level 50, left associativity) : poly_scope.
Infix "*," := Pmult (at level 40, left associativity) : poly_scope.
Notation "-, P" := (Popp P) (at level 35) : poly_scope.
Notation "P [[ x ]]" := (Peval P x) (at level 0) : poly_scope.
(* some useful lemmas about Peval *)
Lemma cons_eval : forall (p1 : Polynomial) (a c : C),
(a :: p1)[[c]] = a + c * (p1[[c]]).
Proof. intros.
unfold Peval.
simpl length.
rewrite <- big_sum_extend_l.
repeat apply f_equal_gen; try easy; try lca.
rewrite (@big_sum_mult_l C _ _ _ C_is_ring); apply big_sum_eq_bounded.
intros.
lca.
Qed.
Lemma Pplus_eval : forall (p1 p2 : Polynomial) (c : C),
(p1 +, p2)[[c]] = p1[[c]] + p2[[c]].
Proof. induction p1 as [| a1].
- intros.
unfold Peval; lca.
- intros.
destruct p2.
+ unfold Peval; lca.
+ simpl in *.
do 3 rewrite cons_eval.
rewrite IHp1; lca.
Qed.
Lemma Pmult_eval : forall (p1 p2 : Polynomial) (c : C),
(p1 *, p2)[[c]] = p1[[c]] * p2[[c]].
Proof. induction p1 as [| a1].
- intros; lca.
- intros; simpl.
rewrite Pplus_eval.
do 2 rewrite cons_eval.
rewrite IHp1, Cmult_plus_distr_r.
apply f_equal_gen; try lca.
apply f_equal_gen; try easy.
unfold Peval.
rewrite map_length, (@big_sum_mult_l C _ _ _ C_is_ring).
apply big_sum_eq_bounded; intros.
replace C0 with (Cmult a1 C0) by lca.
rewrite map_nth, Cmult_0_r.
lca.
Qed.
Lemma Psum_eval : forall (f : nat -> Polynomial) (n : nat) (c : C),
(big_sum f n)[[c]] = big_sum (fun i => (f i)[[c]]) n.
Proof. induction n as [| n'].
- intros. unfold Peval; lca.
- intros; simpl.
rewrite Pplus_eval.
repeat apply f_equal_gen; try easy; try lca.
Qed.
Lemma app_eval : forall (f g : Polynomial),
Peval (f ++ g) = (fun c => f [[ c ]] + ((repeat C0 (length f)) ++ g) [[ c ]]).
Proof. intros.
apply functional_extensionality; intros.
unfold Peval.
rewrite app_length, big_sum_sum.
apply Cplus_simplify.
apply big_sum_eq_bounded; intros.
rewrite app_nth1; easy.
rewrite app_length, big_sum_sum.
rewrite repeat_length.
assert (big_sum (fun i : nat => nth i (repeat C0 (Datatypes.length f) ++ g) C0 * x ^ i)
(length f) = C0).
{ apply (@big_sum_0_bounded C C_is_monoid); intros.
rewrite app_nth1.
destruct (nth_in_or_default x0 (repeat C0 (Datatypes.length f)) C0).
- apply repeat_spec in i.
rewrite i; lca.
- rewrite e; lca.
- rewrite repeat_length; easy. }
rewrite H, Cplus_0_l.
apply big_sum_eq_bounded; intros.
rewrite app_nth2_plus, app_nth2, repeat_length.
replace (Datatypes.length f + x0 - Datatypes.length f)%nat with x0 by lia.
easy.
rewrite repeat_length; lia.
Qed.
Lemma nth_repeat : forall {X} (x : X) (i n : nat),
(nth i (repeat x n) x) = x.
Proof. induction i as [| i'].
- destruct n; easy.
- destruct n; try easy.
simpl in *.
apply IHi'.
Qed.
Lemma mul_by_x_to_n : forall (f : Polynomial) (n : nat) (c : C),
((repeat C0 n) ++ f)[[c]] = f[[c]] * c^n.
Proof. intros.
unfold Peval.
rewrite app_length, big_sum_sum, <- Cplus_0_l.
apply Cplus_simplify.
apply (@big_sum_0_bounded C C_is_monoid); intros.
rewrite app_nth1, nth_repeat; auto; lca.
rewrite (@big_sum_mult_r C _ _ _ C_is_ring).
apply big_sum_eq_bounded; intros.
rewrite app_nth2_plus, repeat_length, Cpow_add_r; lca.
Qed.
Lemma app_eval_to_mul : forall (f g : Polynomial) (c : C),
(f ++ g)[[c]] = f[[c]] + c^(length f) * g[[c]].
Proof. intros.
rewrite Cmult_comm, app_eval, mul_by_x_to_n; easy.
Qed.
(* Now we show that Peq is an equivalence relation, and we prove compatability lemmas *)
Lemma Peq_refl : forall (p : Polynomial), p ≅ p.
Proof. easy. Qed.
Lemma Peq_symm : forall (p1 p2 : Polynomial), p1 ≅ p2 -> p2 ≅ p1.
Proof. easy. Qed.
Lemma Peq_trans : forall (p1 p2 p3 : Polynomial),
p1 ≅ p2 -> p2 ≅ p3 -> p1 ≅ p3.
Proof. intros.
unfold Peq in *; rewrite H; easy.
Qed.
Lemma Pplus_eq_compat : forall (p1 p1' p2 p2' : Polynomial),
p1 ≅ p1' -> p2 ≅ p2' -> p1 +, p2 ≅ p1' +, p2'.
Proof. intros.
unfold Peq in *.
apply functional_extensionality; intros.
do 2 rewrite Pplus_eval.
rewrite H, H0; easy.
Qed.
Lemma Pmult_eq_compat : forall (p1 p1' p2 p2' : Polynomial),
p1 ≅ p1' -> p2 ≅ p2' -> p1 *, p2 ≅ p1' *, p2'.
Proof. intros.
unfold Peq in *.
apply functional_extensionality; intros.
do 2 rewrite Pmult_eval.
rewrite H, H0; easy.
Qed.
Add Parametric Relation : Polynomial Peq
reflexivity proved by Peq_refl
symmetry proved by Peq_symm
transitivity proved by Peq_trans
as Peq_equiv_rel.
Add Parametric Morphism : Pplus
with signature Peq ==> Peq ==> Peq as Pplus_mor.
Proof. intros p1 p2 H p1' p2' H0; apply Pplus_eq_compat; easy. Qed.
Add Parametric Morphism : Pmult
with signature Peq ==> Peq ==> Peq as Pmult_mor.
Proof. intros p1 p2 H p1' p2' H0; apply Pmult_eq_compat; easy. Qed.
Add Parametric Morphism : cons
with signature eq ==> Peq ==> Peq as cons_mor.
Proof. intros c p1 p2 H.
unfold Peq in *.
apply functional_extensionality; intros.
do 2 rewrite cons_eval.
rewrite H; easy.
Qed.
(* now we prove some basic lemmas *)
Lemma Pplus_comm : forall (p1 p2 : Polynomial),
p1 +, p2 = p2 +, p1.
Proof. induction p1 as [| h];
intros; destruct p2; try easy; simpl.
rewrite IHp1, Cplus_comm; easy.
Qed.
Lemma Pplus_assoc : forall (p1 p2 p3 : Polynomial),
(p1 +, p2) +, p3 = p1 +, (p2 +, p3).
Proof. induction p1 as [| h]; try easy.
intros.
destruct p2; destruct p3; try easy; simpl.
rewrite IHp1, Cplus_assoc; easy.
Qed.
Lemma Pplus_0_r : forall (p : Polynomial),
p +, [] = p.
Proof. intros.
destruct p; easy.
Qed.
Lemma C0_Peq_nil : [C0] ≅ [].
Proof. apply functional_extensionality; intros; lca. Qed.
Lemma Pmult_0_r : forall (p : Polynomial),
p *, [] ≅ [].
Proof. induction p as [| a]; try easy.
simpl.
rewrite IHp.
apply C0_Peq_nil.
Qed.
Lemma Pscale_comm_nempty : forall (a : C) (p : Polynomial),
p <> [] -> [a] *, p = p *, [a].
Proof. induction p as [| h]; try easy.
intros; simpl.
destruct p.
+ simpl; rewrite Cmult_comm; easy.
+ rewrite <- IHp; try easy.
apply f_equal_gen; try (apply f_equal_gen; try lca; easy).
simpl.
rewrite Pplus_0_r, Cplus_0_r.
reflexivity.
Qed.
Lemma Pmult_comm_helper : forall (n : nat) (p1 p2 : Polynomial),
(length p1 + length p2 <= n)%nat ->
p1 *, p2 ≅ p2 *, p1.
Proof. induction n as [| n'].
- intros.
destruct p1; destruct p2; try easy.
- intros.
destruct p1; destruct p2; try (rewrite Pmult_0_r; easy).
simpl in *.
rewrite (IHn' p1 (c0 :: p2)), (IHn' p2 (c :: p1)); simpl; try lia.
rewrite (IHn' p1 p2); try lia.
do 2 rewrite <- Pplus_assoc.
rewrite (Pplus_comm _ (map (Cmult c0) p1)), Cmult_comm; easy.
Qed.
Lemma Pmult_comm : forall (p1 p2 : Polynomial),
p1 *, p2 ≅ p2 *, p1.
Proof. intros.
apply (Pmult_comm_helper (length p1 + length p2)).
easy.
Qed.
Lemma map_comm_distr_l : forall (p1 p2 : Polynomial) (c : C),
map (Cmult c) (p1 +, p2) = map (Cmult c) p1 +, map (Cmult c) p2.
Proof. induction p1 as [| a1]; try easy.
intros.
destruct p2; try easy; simpl.
rewrite IHp1.
repeat (apply f_equal_gen; try lca); easy.
Qed.
Lemma Pmult_plus_distr_l : forall (p1 p2 p3 : Polynomial),
p1 *, (p2 +, p3) ≅ p1 *, p2 +, p1 *, p3.
Proof. induction p1 as [| a1]; try easy.
intros.
simpl. rewrite IHp1.
rewrite map_comm_distr_l.
unfold Peq; apply functional_extensionality; intros.
repeat (rewrite Pplus_eval).
repeat (rewrite <- Cplus_assoc).
apply f_equal_gen; try easy.
repeat rewrite cons_eval.
rewrite Pplus_eval.
lca.
Qed.
Lemma Pmult_plus_distr_r : forall (p1 p2 p3 : Polynomial),
(p2 +, p3) *, p1 ≅ p2 *, p1 +, p3 *, p1.
Proof. intros.
rewrite Pmult_comm, Pmult_plus_distr_l.
rewrite Pmult_comm, (Pmult_comm _ p3).
easy.
Qed.
Lemma cons_simplify : forall (p : Polynomial) (a : C),
(a :: p) = [a] +, (C0 :: p).
Proof. intros.
unfold Pplus.
rewrite Cplus_0_r; easy.
Qed.
Lemma cons_0_mult : forall (p1 p2 : Polynomial),
(C0 :: p1) *, p2 ≅ C0 :: (p1 *, p2).
Proof. intros; simpl.
assert (H' : (map (Cmult C0) p2) ≅ ([C0] *, p2)).
{ simpl. rewrite C0_Peq_nil, Pplus_comm; easy. }
rewrite H', C0_Peq_nil.
easy.
Qed.
Lemma cons_singleton_mult : forall (p : Polynomial) (a : C),
[a] *, p ≅ map (Cmult a) p.
Proof. intros; simpl.
rewrite C0_Peq_nil, Pplus_comm; easy.
Qed.
Lemma Pmult_assoc_singleton : forall (a1 : C) (p2 p3 : Polynomial),
([a1] *, p2) *, p3 ≅ [a1] *, (p2 *, p3).
Proof. induction p2 as [| a2].
- intros.
rewrite Pmult_0_r; simpl.
rewrite C0_Peq_nil; easy.
- intros.
rewrite (cons_simplify p2 a2), Pmult_plus_distr_l,
Pmult_plus_distr_r, (Pmult_comm _ (C0 :: p2)).
do 2 rewrite cons_0_mult.
rewrite (Pmult_comm p2), IHp2, Pmult_plus_distr_r, Pmult_plus_distr_l, cons_0_mult.
rewrite (Pmult_comm _ (C0 :: p2 *, p3)), cons_0_mult,
(Pmult_comm _ [a1]), cons_singleton_mult; simpl map.
rewrite cons_singleton_mult, (cons_singleton_mult _ a2),
(cons_singleton_mult (map (Cmult a2) p3)), map_map.
repeat (apply f_equal_gen; try easy).
apply functional_extensionality; intros.
lca.
Qed.
Lemma Pmult_assoc : forall (p1 p2 p3 : Polynomial),
(p1 *, p2) *, p3 ≅ p1 *, (p2 *, p3).
Proof. induction p1 as [| a1]; try easy.
intros.
rewrite cons_simplify.
do 3 rewrite Pmult_plus_distr_r, cons_0_mult.
rewrite Pmult_assoc_singleton, IHp1.
easy.
Qed.
Lemma map_1_id : forall (p : Polynomial),
map (Cmult C1) p = p.
Proof. induction p; try easy.
simpl.
rewrite IHp, Cmult_1_l.
easy.
Qed.
Lemma Pmult_1_l : forall (p : Polynomial),
[C1] *, p ≅ p.
Proof. intros.
simpl.
rewrite map_1_id, C0_Peq_nil, Pplus_0_r.
easy.
Qed.
Lemma Pmult_1_r : forall (p : Polynomial),
p *, [C1] ≅ p.
Proof. intros.
rewrite Pmult_comm.
apply Pmult_1_l.
Qed.
Lemma Pplus_opp_r : forall (p : Polynomial),
p +, -,p ≅ [].
Proof. induction p as [| a].
- unfold Popp; simpl.
rewrite C0_Peq_nil; easy.
- simpl.
assert (H' : (map (Cmult (- C1)) p +, []) ≅ -, p).
{ unfold Popp, Pmult;
rewrite C0_Peq_nil; easy. }
rewrite H', IHp.
replace (a + (- C1 * a + C0)) with C0 by lca.
rewrite C0_Peq_nil; easy.
Qed.
Lemma Pplus_opp_l : forall (p : Polynomial),
-,p +, p ≅ [].
Proof. intros.
rewrite Pplus_comm.
apply Pplus_opp_r.
Qed.
Lemma head_0_Pmult_x : forall (p : Polynomial),
(C0 :: p) ≅ [C0; C1] *, p.
Proof. intros.
rewrite cons_0_mult, Pmult_1_l.
easy.
Qed.
(* We now need to show that if two polynomials are equal at infinite values,
then they are infinite everywhere *)
(* we start by defining limits *)
Definition limit_at_point (f : C -> C) (a L : C) : Prop :=
forall ϵ : R, ϵ > 0 -> exists δ, (δ > 0 /\ forall x, x <> a -> Cmod (x - a) < δ -> Cmod (f(x) - L) < ϵ).
Definition continuous_at (f : C -> C) (a : C) : Prop :=
limit_at_point f a (f a).
Lemma limit_unique : forall (f : C -> C) (a L1 L2 : C),
limit_at_point f a L1 -> limit_at_point f a L2 ->
L1 = L2.
Proof. intros.
destruct (Ceq_dec (L1 - L2) C0).
- rewrite <- Cplus_0_r, <- e.
lca.
- unfold limit_at_point in *.
destruct (H ((Cmod (L1 - L2)) / 2)%R) as [δ1 [H1 H2] ];
destruct (H0 ((Cmod (L1 - L2)) / 2)%R) as [δ2 [H3 H4] ];
try (unfold Rdiv; apply Rmult_gt_0_compat; try lra; apply Cmod_gt_0; easy).
assert (H5 : exists b, b <> a /\ Cmod (b - a) < (Rmin δ1 δ2)).
{ exists (a + ((Rmin δ1 δ2) / 2, 0)%R)%C.
split.
+ unfold not; intros.
apply (f_equal (Cplus (-a))) in H5.
rewrite Cplus_assoc, Cplus_opp_l, Cplus_0_l in H5.
apply (f_equal fst) in H5; simpl in H5.
apply (f_equal (Rmult 2)) in H5.
rewrite Rmult_0_r in H5.
replace (2 * (Rmin δ1 δ2 / 2))%R with (Rmin δ1 δ2) in H5 by lra.
apply (Rmin_pos δ1 δ2) in H1; auto.
lra.
+ unfold Cplus, Cminus, Copp, Cmod; simpl.
replace (snd a + 0 + - snd a)%R with 0 by lra.
replace (fst a + Rmin δ1 δ2 / 2 + - fst a)%R with (Rmin δ1 δ2 / 2)%R by lra.
rewrite Rmult_0_l, Rplus_0_r, Rmult_1_r, sqrt_square.
apply Rlt_eps2_eps.
apply (Rmin_pos δ1 δ2); auto.
unfold Rdiv.
apply Rmult_le_pos; try lra.
left; apply (Rmin_pos δ1 δ2); auto. }
destruct H5 as [b [H5 H6] ].
assert (H7 : Cmod (f b - L1) + Cmod (f b - L2) < Cmod (L1 - L2)).
rewrite (double_var (Cmod (L1 - L2))).
apply Rplus_lt_compat.
apply H2; auto.
assert (H' := (Rmin_l δ1 δ2)). lra.
apply H4; auto.
assert (H' := (Rmin_r δ1 δ2)). lra.
assert (H8 : Cmod (L1 - (f b) + (f b - L2)) <= Cmod (L1 - f b) + Cmod (f b - L2)).
{ apply Cmod_triangle. }
replace (L1 - f b + (f b - L2)) with (L1 - L2) in H8 by lca.
replace (L1 - f b) with (- (f b - L1)) in H8 by lca.
rewrite Cmod_opp in H8. lra.
Qed.
Lemma limit_const : forall (a x : C),
limit_at_point (fun c => a) x a.
Proof. unfold limit_at_point; intros.
exists 1. split; try lra.
intros.
replace (a - a) with C0 by lca.
rewrite Cmod_0.
easy.
Qed.
Lemma limit_const_poly : forall (a x : C),
limit_at_point (Peval [a]) x a.
Proof. unfold limit_at_point.
intros.
exists 1; split.
lra.
intros.
unfold Peval; simpl.
replace (C0 + a * C1 - a) with C0 by lca.
rewrite Cmod_0.
easy.
Qed.
Lemma limit_linear_poly : forall (a x : C),
limit_at_point (Peval [C0;a]) x (a * x).
Proof. unfold limit_at_point; intros.
destruct (Ceq_dec a C0); subst.
- exists 1.
split; try lra.
intros.
replace (([C0; C0]) [[x0]] - C0 * x) with C0 by lca.
rewrite Cmod_0.
easy.
- exists (ϵ / (Cmod a))%R.
split.
unfold Rdiv.
apply Rmult_gt_0_compat; auto.
apply Rinv_0_lt_compat.
apply Cmod_gt_0; easy.
intros.
replace (([C0; a]) [[x0]]) with (a * x0) by lca.
unfold Cminus.
rewrite Copp_mult_distr_r, <- Cmult_plus_distr_l, Cmod_mult.
assert (H2 : Cmod (x0 - x) * Cmod a < ϵ / Cmod a * Cmod a).
{ apply Rmult_lt_compat_r; auto.
apply Cmod_gt_0; easy. }
unfold Rdiv, Cminus in H2.
rewrite Rmult_assoc, Rinv_l in H2.
lra.
unfold not; intros.
apply n.
apply Cmod_eq_0; easy.
Qed.
Lemma limit_scale : forall (f : C -> C) (a c L : C),
limit_at_point f a L ->
limit_at_point (fun x => c * f x) a (c * L).
Proof. intros.
unfold limit_at_point in *.
intros.
destruct (Ceq_dec c 0); subst.
- exists 1.
split; try lra.
intros.
replace (C0 * f x - C0 * L) with C0 by lca.
rewrite Cmod_0; easy.
- destruct (H (ϵ / Cmod c)%R).
unfold Rdiv.
apply Rmult_lt_0_compat; auto.
apply Rinv_0_lt_compat.
apply Cmod_gt_0; auto.
exists (x)%R.
destruct H1.
split. auto.
intros.
unfold Cminus.
rewrite Copp_mult_distr_r, <- Cmult_plus_distr_l, Cmod_mult.
apply H2 in H3; auto.
apply (Rmult_lt_compat_l (Cmod c)) in H3.
unfold Rdiv in H3.
rewrite (Rmult_comm ϵ), <- Rmult_assoc, Rinv_r, Rmult_1_l in H3; auto.
unfold not; intros; apply n.
apply Cmod_eq_0; auto.
apply Cmod_gt_0; auto.
Qed.
Lemma limit_plus : forall (f1 f2 : C -> C) (a L1 L2 : C),
limit_at_point f1 a L1 -> limit_at_point f2 a L2 ->
limit_at_point (fun c => f1 c + f2 c) a (L1 + L2).
Proof. unfold limit_at_point.
intros.
assert (H2 : ϵ / 2 > 0).
{ lra. }
assert (H3 := H2).
apply H in H2.
apply H0 in H3.
destruct H2 as [δ1 [H2 H4] ]; destruct H3 as [δ2 [H3 H5] ].
exists (Rmin δ1 δ2); split.
apply Rmin_Rgt; auto.
intros.
assert (H8 : Cmod (f1 x - L1) < ϵ / 2).
{ apply H4; auto.
assert (H' := (Rmin_l δ1 δ2)); lra. }
assert (H9 : Cmod (f2 x - L2) < ϵ / 2).
{ apply H5; auto.
assert (H' := (Rmin_r δ1 δ2)); lra. }
apply (Rplus_lt_compat (Cmod (f1 x - L1)) (ϵ / 2) (Cmod (f2 x - L2)) (ϵ / 2)) in H8; auto.
replace (ϵ / 2 + ϵ / 2)%R with ϵ in H8 by lra.
replace (f1 x + f2 x - (L1 + L2))%C with ((f1 x - L1) + (f2 x - L2))%C by lca.
assert (H10 : Cmod (f1 x - L1 + (f2 x - L2)) <= Cmod (f1 x - L1) + Cmod (f2 x - L2)).
{ apply Cmod_triangle. }
lra.
Qed.
Lemma limit_mult_0 : forall (f1 f2 : C -> C) (a : C),
limit_at_point f1 a C0 -> limit_at_point f2 a C0 ->
limit_at_point (fun c => f1 c * f2 c) a C0.
Proof. intros.
unfold limit_at_point in *.
intros.
destruct (H (√ ϵ)); try apply sqrt_lt_R0; auto.
destruct (H0 (√ ϵ)); try apply sqrt_lt_R0; auto.
destruct H2; destruct H3.
exists (Rmin x x0).
split.
apply Rmin_pos; auto.
intros.
unfold Cminus in *.
rewrite Copp_0, Cplus_0_r in *.
rewrite Cmod_mult, <- (sqrt_def ϵ); try lra.
apply Rmult_le_0_lt_compat; try apply Cmod_ge_0.
all : rewrite <- (Cplus_0_r (_ x1)).
apply H4; auto.
assert (H8 := Rmin_l x x0). lra.
apply H5; auto.
assert (H8 := Rmin_r x x0). lra.
Qed.
Lemma limit_mult : forall (f1 f2 : C -> C) (a L1 L2 : C),
limit_at_point f1 a L1 -> limit_at_point f2 a L2 ->
limit_at_point (fun c => f1 c * f2 c) a (L1 * L2).
Proof. intros.
assert (H' : (fun c => f1 c * f2 c) =
(fun c => ((f1 c - L1) * (f2 c - L2)) + (L1 * f2 c) + (L2 * f1 c) - (L1 * L2))).
{ apply functional_extensionality.
intros. lca. }
replace (L1 * L2) with (C0 + L1 * L2 + L2 * L1 - L1 * L2) by lca.
rewrite H'.
repeat apply limit_plus.
unfold Cminus.
apply limit_mult_0.
replace C0 with (L1 + - L1) by lca.
apply limit_plus; auto.
apply limit_const.
replace C0 with (L2 + - L2) by lca.
apply limit_plus; auto.
apply limit_const.
all : try apply limit_scale; auto.
apply limit_const.
Qed.
Lemma continuous_plus : forall (f1 f2 : C -> C) (a : C),
continuous_at f1 a -> continuous_at f2 a ->
continuous_at (fun c => f1 c + f2 c) a.
Proof. intros.
unfold continuous_at in *.
apply (limit_plus f1 f2 a (f1 a) (f2 a)); easy.
Qed.
Lemma continuous_mult : forall (f1 f2 : C -> C) (a : C),
continuous_at f1 a -> continuous_at f2 a ->
continuous_at (fun c => f1 c * f2 c) a.
Proof. intros.
unfold continuous_at in *.
apply (limit_mult f1 f2 a (f1 a) (f2 a)); easy.
Qed.
Lemma constant_continuous_poly : forall (c a : C),
continuous_at (Peval [c]) a.
Proof. intros.
unfold continuous_at.
replace (([c]) [[a]]) with c by lca.
apply limit_const_poly.
Qed.
Lemma linear_continuous_poly : forall (c a : C),
continuous_at (Peval [C0; c]) a.
Proof. intros.
unfold continuous_at.
replace (([C0; c]) [[a]]) with (c * a) by lca.
apply limit_linear_poly.
Qed.
Lemma power_x_eval : forall (n : nat) (a x : C),
(repeat C0 n ++ [a]) [[x]] = a * x^n.
Proof. intros.
unfold Peval.
rewrite app_length; simpl.
rewrite Nat.add_1_r, <- big_sum_extend_r, <- (Nat.add_0_r (length (repeat C0 n))),
app_nth2_plus, Nat.add_0_r, repeat_length; simpl.
rewrite <- Cplus_0_l.
apply Cplus_simplify; auto.
apply (@big_sum_0_bounded C C_is_monoid); intros.
rewrite app_nth1.
destruct (nth_in_or_default x0 (repeat C0 n) C0).
- apply repeat_spec in i.
rewrite i; lca.
- rewrite e; lca.
- rewrite repeat_length; easy.
Qed.
Lemma power_x_mul_x : forall (n : nat) (a : C),
(repeat C0 (S n) ++ [a]) ≅ [C0; C1] *, (repeat C0 n ++ [a]).
Proof. intros.
simpl repeat.
rewrite <- app_comm_cons.
apply head_0_Pmult_x.
Qed.
Lemma continuous_power_x : forall (n : nat) (a b : C),
continuous_at (Peval ((repeat C0 n) ++ [b])) a.
Proof. induction n as [| n'].
- intros.
apply constant_continuous_poly.
- intros.
rewrite power_x_mul_x.
assert (H' : forall p q : Polynomial, Peval (p *, q) = fun c => p[[c]] * q[[c]]).
{ intros.
apply functional_extensionality.
intros.
apply Pmult_eval. }
rewrite H'.
apply continuous_mult.
apply linear_continuous_poly.
apply IHn'.
Qed.
Lemma polynomial_continuous : forall (p : Polynomial) (a : C),
(fun p => continuous_at (Peval p) a) p.
Proof. intros.
apply (@ind_from_end C).
- rewrite <- C0_Peq_nil.
apply constant_continuous_poly.
- intros.
rewrite app_eval.
apply continuous_plus.
apply H.
apply continuous_power_x.
Qed.
Lemma constant_ae_continuous : forall (f : C -> C) (a c : C),
continuous_at f c -> (forall x, x <> c -> f x = a) ->
f c = a.
Proof. intros.
unfold continuous_at in H.
assert (H1 : limit_at_point f c a).
{ unfold limit_at_point.
intros.
exists 1.
split; try lra.
intros.
rewrite H0; auto.
unfold Cminus.
rewrite Cplus_opp_r, Cmod_0.
lra. }
apply (limit_unique f c); easy.
Qed.
Lemma almost_0_implies_0 : forall (p : Polynomial),
(forall c, c <> C0 -> p[[c]] = C0) -> p ≅ [].
Proof. intros.
unfold Peq.
apply functional_extensionality; intros.
destruct (Ceq_dec x C0).
- replace (([]) [[x]]) with C0 by easy.
apply constant_ae_continuous.
apply polynomial_continuous.
intros.
apply H; subst; easy.
- apply H; easy.
Qed.
Lemma almost_Peq_implies_Peq : forall (p1 p2 : Polynomial),
(forall c, c <> C0 -> p1[[c]] = p2[[c]]) -> p1 ≅ p2.
Proof. intros.
assert (H' : p1 +, -, p2 ≅ []).
{ apply almost_0_implies_0.
intros.
unfold Popp.
rewrite Pplus_eval, Pmult_eval, H; auto.
replace (([- C1]) [[c]]) with (-C1) by lca.
lca. }
replace p2 with ([] +, p2) by easy.
rewrite <- H', Pplus_assoc, Pplus_opp_l, Pplus_0_r.
easy.
Qed.
Lemma Peq_head_eq : forall (p1 p2 : Polynomial) (a1 a2 : C),
(a1 :: p1) ≅ (a2 :: p2) -> a1 = a2.
Proof. intros.
unfold Peq, Peval in H.
apply (f_equal_inv C0) in H.
simpl length in H.
rewrite (big_sum_unique a1), (big_sum_unique a2) in H; auto.
all : exists O; split; try lia; split; try lca.
all : intros; destruct x'; try easy; lca.
Qed.
Lemma Peq_tail_Peq_helper : forall (p1 p2 : Polynomial),
(C0 :: p1) ≅ (C0 :: p2) -> p1 ≅ p2.
Proof. intros.
apply almost_Peq_implies_Peq; intros.
apply (f_equal_inv c) in H.
unfold Peval in *.
simpl length in *.
do 2 rewrite <- big_sum_extend_l in H.
simpl in H.
rewrite Cmult_0_l, Cplus_0_l, Cplus_0_l in H.
rewrite <- Cmult_1_l, <- (Cinv_l c), <- Cmult_assoc, (@big_sum_mult_l C _ _ _ C_is_ring); auto.
assert (H' : (fun x : nat => nth x p2 C0 * (c * c ^ x)) =
(fun x : nat => c * (nth x p2 C0 * c ^ x))).
{ apply functional_extensionality; intros.
lca. }
simpl in *.
rewrite <- H', <- H.
assert (H'' : (fun x : nat => nth x p1 C0 * (c * c ^ x)) =
(fun x : nat => c * (nth x p1 C0 * c ^ x))).
{ apply functional_extensionality; intros.
lca. }
rewrite H''.
rewrite <- (@big_sum_mult_l C _ _ _ C_is_ring), Cmult_assoc, Cinv_l; auto; lca.
Qed.
Lemma Peq_tail_Peq : forall (p1 p2 : Polynomial) (a1 a2 : C),
(a1 :: p1) ≅ (a2 :: p2) -> p1 ≅ p2.
Proof. intros.
assert (H' : a1 = a2).
{ apply Peq_head_eq in H; easy. }
rewrite cons_simplify, (cons_simplify p2) in H; subst.
assert (H'' : [-a2] +, [a2] +, (C0 :: p1) ≅ [-a2] +, [a2] +, (C0 :: p2)).
{ rewrite Pplus_assoc, H, <- Pplus_assoc.
easy. }
simpl in H''.
replace (- a2 + a2 + C0) with C0 in H'' by lca.
apply Peq_tail_Peq_helper in H''.
easy.
Qed.
Lemma Peq_app_Peq : forall (p p1 p2 : Polynomial),
(p ++ p1) ≅ (p ++ p2) -> p1 ≅ p2.
Proof. induction p as [| a]; try easy.
intros; simpl in H.
apply Peq_tail_Peq in H.
apply IHp; easy.
Qed.
Lemma Peq_length_eq_helper : forall (n : nat) (p1 p2 : Polynomial),
p1 ≅ p2 ->
length p1 = n -> length p2 = n ->
p1 = p2.
Proof. induction n as [| n'].
- intros.
destruct p1; destruct p2; try easy.
- intros.
destruct p1; destruct p2; try easy.
simpl in *.
apply Nat.succ_inj in H0.
apply Nat.succ_inj in H1.
repeat apply f_equal_gen; try easy.
apply (Peq_head_eq p1 p2); easy.
apply IHn'; try easy.
apply (Peq_tail_Peq _ _ c c0); easy.
Qed.
Lemma Peq_length_eq : forall (n : nat) (p1 p2 : Polynomial),
p1 ≅ p2 -> length p1 = length p2 ->
p1 = p2.
Proof. intros.
apply (Peq_length_eq_helper (length p1)); easy.
Qed.
Lemma Peq_nil_reduce : forall (p : Polynomial) (a : C),
(a :: p) ≅ [] -> a = C0 /\ p ≅ [].
Proof. intros.
rewrite <- C0_Peq_nil in H.
split.
- apply Peq_head_eq in H; easy.
- apply Peq_tail_Peq in H; easy.
Qed.
Lemma Peq_nil_contains_C0 : forall (p : Polynomial),
p ≅ [] -> (forall c, In c p -> c = C0).
Proof. induction p; try easy.
intros.
apply Peq_nil_reduce in H.
destruct H0; try (subst; easy).
apply IHp; easy.
Qed.
Lemma same_elem_same_rev : forall (p : Polynomial),
(forall c, In c p -> c = C0) -> p = repeat C0 (length p).
Proof. induction p as [| a]; try easy.
intros; simpl.
rewrite (H a), IHp, repeat_length; try easy.
intros.
apply H; right; easy.
left; easy.
Qed.
Lemma Peq_0_eq_repeat_0 : forall (p : Polynomial),
p ≅ [] -> p = repeat C0 (length p).
Proof. intros.
apply same_elem_same_rev.
apply Peq_nil_contains_C0; easy.
Qed.
Lemma Peq_nil_rev_Peq_nil : forall (p : Polynomial),
p ≅ [] -> rev p = p.
Proof. intros.
rewrite same_elem_same_rev, <- rev_repeat, <- same_elem_same_rev; try easy.
all : apply Peq_nil_contains_C0; easy.
Qed.
Lemma last_C0_Peq_front : forall (p : Polynomial),
p ++ [C0] ≅ p.
Proof. intros.
unfold Peq, Peval.
apply functional_extensionality; intros.
rewrite app_length; simpl.
rewrite Nat.add_1_r, <- big_sum_extend_r.
rewrite app_nth2, Nat.sub_diag; auto; simpl.
rewrite Cmult_0_l, Cplus_0_r.
apply big_sum_eq_bounded; intros.
rewrite app_nth1; easy.
Qed.