Skip to content

Commit a697185

Browse files
authored
Merge pull request #69 from clayrat/refactor
refactor proofs in theory
2 parents 429b6c6 + bc58d87 commit a697185

30 files changed

+1109
-1282
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ The theory directory has the following content:
105105
- `companion`, `frobenius_form`, `jordan`, `perm_eq_image`,
106106
`smith_complements`: Results on normal forms of matrices.
107107

108-
- `bareiss_dvdring`, `bareiss`, `gauss`, `karatsuba`, `rank`
109-
`strassen` `toomcook`, `smithpid`, `smith`: Various efficient
108+
- `bareiss_dvdring`, `bareiss`, `gauss`, `karatsuba`, `rank`,
109+
`strassen`, `toomcook`, `smithpid`, `smith`: Various efficient
110110
algorithms for computing operations on polynomials or matrices.
111111

112112
## Refinements

meta.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ documentation: |-
168168
- `companion`, `frobenius_form`, `jordan`, `perm_eq_image`,
169169
`smith_complements`: Results on normal forms of matrices.
170170
171-
- `bareiss_dvdring`, `bareiss`, `gauss`, `karatsuba`, `rank`
172-
`strassen` `toomcook`, `smithpid`, `smith`: Various efficient
171+
- `bareiss_dvdring`, `bareiss`, `gauss`, `karatsuba`, `rank`,
172+
`strassen`, `toomcook`, `smithpid`, `smith`: Various efficient
173173
algorithms for computing operations on polynomials or matrices.
174174
175175
## Refinements
@@ -201,7 +201,7 @@ documentation: |-
201201
202202
- `seqmatrix` and `seqmx_complements`: Refinement of MathComp
203203
matrices (`M[R]_(m,n)`) to lists of lists (`seq (seq R)`).
204-
204+
205205
- `seqpoly`: Refinement of MathComp polynomials (`{poly R}`) to lists (`seq R`).
206206
207207
- `multipoly`: Refinement of

theory/atomic_operations.v

Lines changed: 30 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,14 @@ Lemma lines_scale_row m n a (M: 'M[R]_(m,n)):
4444
Proof.
4545
move => s.
4646
elim : s n M => [ | hd tl hi] //= n M /andP [h1 h2].
47-
split => i.
48-
- rewrite in_cons => /orP [] hin.
49-
+ rewrite (eqP hin) {i hin}.
50-
case: (hi _ (line_scale hd a M) h2) => _ hr.
47+
split => i; rewrite in_cons.
48+
- move/orP => [/eqP{i}-> | hin].
49+
+ case: (hi _ (line_scale hd a M) h2) => _ hr.
5150
by rewrite hr // line_scale_row_eq.
52-
case: (hi _ (line_scale hd a M) h2) => hl _.
53-
rewrite hl // line_scale_row_neq //.
54-
apply/negP => /eqP heq.
55-
by move: h1; rewrite heq hin.
56-
rewrite in_cons negb_or => /andP [hl hr].
51+
case: (hi _ (line_scale hd a M) h2) => -> // _.
52+
rewrite line_scale_row_neq //.
53+
by apply: contraNneq h1 => ->.
54+
rewrite negb_or => /andP[hl hr].
5755
case: (hi _ (line_scale hd a M) h2) => _ hR.
5856
by rewrite hR // line_scale_row_neq // eq_sym.
5957
Qed.
@@ -79,7 +77,7 @@ Lemma det_line_scale_mx : forall n k a (M: 'M[R]_n),
7977
Proof.
8078
rewrite /line_scale_mx => n k a M.
8179
rewrite det_mulmx det_diag (bigD1 k) //= big1 /=;
82-
first by rewrite !mxE mulr1 eqxx /=.
80+
first by rewrite !mxE mulr1 eqxx.
8381
by move => i /negbTE h; rewrite !mxE h.
8482
Qed.
8583

@@ -130,17 +128,14 @@ Proof.
130128
move => s.
131129
elim : s M => [ | hd tl hi] //= M /andP [h1 h2].
132130
rewrite in_cons negb_or => /andP [hl1 hl2].
133-
split => i.
134-
- rewrite in_cons => /orP [] hin.
135-
+ rewrite (eqP hin) {i hin}.
136-
case: (hi (line_comb hd l a M) h2 hl2) => _ hr.
131+
split => i; rewrite in_cons.
132+
- move/orP => [/eqP{i}-> | hin].
133+
+ case: (hi (line_comb hd l a M) h2 hl2) => _ hr.
137134
by rewrite hr // line_comb_row_eq.
138-
case: (hi (line_comb hd l a M) h2 hl2) => hl _.
139-
rewrite hl // !line_comb_row_neq //.
140-
+ by rewrite eq_sym.
141-
apply/negP => /eqP heq.
142-
by move: h1; rewrite heq hin.
143-
rewrite in_cons negb_or => /andP [hl hr].
135+
case: (hi (line_comb hd l a M) h2 hl2) => -> // _.
136+
rewrite !line_comb_row_neq // eq_sym // eq_sym.
137+
by apply: contraNneq h1 => ->.
138+
rewrite negb_or => /andP [hl hr].
144139
case: (hi (line_comb hd l a M) h2 hl2) => _ hR.
145140
by rewrite hR // !line_comb_row_neq // eq_sym.
146141
Qed.
@@ -157,17 +152,14 @@ Proof.
157152
move => s.
158153
elim : s M => [ | hd tl hi] //= M /andP [h1 h2].
159154
rewrite in_cons negb_or => /andP [hl1 hl2].
160-
split => i.
161-
- rewrite in_cons => /orP [] hin.
162-
+ rewrite (eqP hin) {i hin}.
163-
case: (hi (line_comb hd l (a hd) M) h2 hl2) => _ hr.
155+
split => i; rewrite in_cons.
156+
- move/orP => [/eqP{i}-> | hin].
157+
+ case: (hi (line_comb hd l (a hd) M) h2 hl2) => _ hr.
164158
by rewrite hr // line_comb_row_eq.
165-
case: (hi (line_comb hd l (a hd) M) h2 hl2) => hl _.
166-
rewrite hl // !line_comb_row_neq //.
167-
+ by rewrite eq_sym.
168-
apply/negP => /eqP heq.
169-
by move: h1; rewrite heq hin.
170-
rewrite in_cons negb_or => /andP [hl hr].
159+
case: (hi (line_comb hd l (a hd) M) h2 hl2) => -> // _.
160+
rewrite !line_comb_row_neq // eq_sym // eq_sym.
161+
by apply: contraNneq h1 => ->.
162+
rewrite negb_or => /andP [hl hr].
171163
case: (hi (line_comb hd l (a hd) M) h2 hl2) => _ hR.
172164
by rewrite hR // !line_comb_row_neq // eq_sym.
173165
Qed.
@@ -179,24 +171,13 @@ Proof.
179171
move => n k l a M hkl.
180172
have h : row k (line_comb k l a M) = 1 *: row k M +
181173
a *: row k (\matrix_(i < n) if i == k then row l M else row i M).
182-
- rewrite /line_comb scale1r.
183-
by apply/rowP => i; rewrite !mxE eqxx !mxE.
174+
by rewrite scale1r; apply/rowP => i; rewrite !mxE eqxx !mxE.
184175
rewrite (determinant_multilinear h).
185176
- rewrite mul1r [X in a * X](determinant_alternate hkl).
186177
+ by rewrite mulr0 addr0.
187-
move => x; rewrite !mxE eqxx eq_sym.
188-
move: hkl.
189-
by case hlk : (k == l).
190-
- rewrite /line_comb; apply/matrixP => i j; rewrite !mxE.
191-
case heq: (lift k i == k).
192-
+ move/negP : (neq_lift k i).
193-
by rewrite (eqP heq) eqxx.
194-
by rewrite !mxE.
195-
- rewrite /line_comb; apply/matrixP => i j; rewrite !mxE.
196-
case heq: (lift k i == k).
197-
+ move/negP : (neq_lift k i).
198-
by rewrite (eqP heq) eqxx.
199-
by rewrite !mxE.
178+
by move => x; rewrite !mxE eqxx eq_sym (negbTE hkl).
179+
- by apply/matrixP => i j; rewrite !mxE eq_sym (negbTE (neq_lift k i)) !mxE.
180+
by apply/matrixP => i j; rewrite !mxE eq_sym (negbTE (neq_lift k i)) !mxE.
200181
Qed.
201182

202183
Lemma det_lines_comb m a l (M: 'M[R]_m) s:
@@ -226,17 +207,11 @@ move => n k l a M /eqP ->; clear k.
226207
have h : row l (line_comb l l a M) = 1 *: row l M + a *: row l M.
227208
- rewrite /line_scale.
228209
by apply/rowP => i; rewrite !mxE eqxx !mxE mul1r.
229-
rewrite (determinant_multilinear h) ?mulrDl => //.
210+
rewrite (determinant_multilinear h) ?mulrDl //.
230211
rewrite /line_scale; apply/matrixP => i j; rewrite !mxE.
231-
case heq: (lift l i == l).
232-
+ move/negP : (neq_lift l i).
233-
by rewrite (eqP heq) eqxx.
234-
by rewrite !mxE.
212+
by rewrite eq_sym (negbTE (neq_lift l i)) !mxE.
235213
rewrite /line_scale; apply/matrixP => i j; rewrite !mxE.
236-
case heq: (lift l i == l).
237-
- move/negP : (neq_lift l i).
238-
by rewrite (eqP heq) eqxx.
239-
by rewrite !mxE.
214+
by rewrite eq_sym (negbTE (neq_lift l i)) !mxE.
240215
Qed.
241216

242-
End atomic_operations.
217+
End atomic_operations.

theory/bareiss.v

Lines changed: 31 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -62,25 +62,24 @@ Variable R : comRingType.
6262

6363
Fixpoint bareiss_rec m (a : {poly R}) :
6464
'M[{poly R}]_(1 + m, 1 + m) -> {poly R} :=
65-
match m with
66-
| S p => fun M =>
65+
if m is p.+1 then
66+
fun M =>
6767
let d := M 0 0 in
6868
let l := ursubmx M in
6969
let c := dlsubmx M in
7070
let N := drsubmx M in
71-
let M' := (d *: N - c *m l) in
71+
let M' := d *: N - c *m l in
7272
let M'' := map_mx (fun x => rdivp x a) M' in
73-
bareiss_rec d M''
74-
| _ => fun M => M 0 0
75-
end.
73+
bareiss_rec d M''
74+
else fun M => M 0 0.
7675

77-
Definition bareiss n (M : 'M_(1 + n, 1 + n)) : {poly R} := bareiss_rec 1 M.
76+
Definition bareiss n (M : 'M_(1 + n, 1 + n)) : {poly R} := bareiss_rec 1 M.
7877

7978
Definition bareiss_char_poly n (M : 'M_(1 + n, 1 + n)) : {poly R} :=
8079
bareiss (char_poly_mx M).
8180

8281
(* The actual determinant function based on Bareiss *)
83-
Definition bdet n (M : 'M_(1 + n, 1 + n)) : R :=
82+
Definition bdet n (M : 'M_(1 + n, 1 + n)) : R :=
8483
(bareiss_char_poly (- M))`_0.
8584

8685
End bareiss.
@@ -91,8 +90,8 @@ Variable R : comRingType.
9190

9291
Lemma bareiss_recE : forall m a (M : 'M[{poly R}]_(1 + m)),
9392
a \is monic ->
94-
(forall p (h h' : p < 1 + m), pminor h h' M \is monic) ->
95-
(forall k (f g : 'I_k.+1 -> 'I_m.+1), rdvdp (a ^+ k) (minor f g M)) ->
93+
(forall p (h h' : p < 1 + m), pminor h h' M \is monic) ->
94+
(forall k (f g : 'I_k.+1 -> 'I_m.+1), rdvdp (a ^+ k) (minor f g M)) ->
9695
a ^+ m * (bareiss_rec a M) = \det M.
9796
Proof.
9897
elim=> [a M _ _ _|m ih a M am hpm hdvd] /=.
@@ -101,12 +100,12 @@ have ak_monic k : a ^+ k \is monic by apply/monic_exp.
101100
set d := M 0 0; set M' := (_ - _); set M'' := map_mx _ _; rewrite /= in M' M'' *.
102101
have d_monic : d \is monic.
103102
have -> // : d = pminor (ltn0Sn _) (ltn0Sn _) M.
104-
have h : widen_ord (ltn0Sn m.+1) =1 (fun _ => 0)
103+
have h : widen_ord (ltn0Sn m.+1) =1 (fun=> 0)
105104
by move=> x; apply/ord_inj; rewrite [x]ord1.
106105
by rewrite /pminor (minor_eq h h) minor1.
107106
have dk_monic : forall k, d ^+ k \is monic by move=> k; apply/monic_exp.
108107
have hM' : M' = a *: M''.
109-
pose f := fun m (i : 'I_m) (x : 'I_2) => if x == 0 then 0 else (lift 0 i).
108+
pose f := fun m (i : 'I_m) (x : 'I_2) => if x == 0 then 0 else lift 0 i.
110109
apply/matrixP => i j.
111110
rewrite !mxE big_ord1 !rshift1 [a * _]mulrC rdivpK ?(eqP am,expr1n,mulr1) //.
112111
move: (hdvd 1%nat (f _ i) (f _ j)).
@@ -122,7 +121,7 @@ case/rdvdpP: (hdvd _ (lift_pred f) (lift_pred g)) => // x hx.
122121
apply/rdvdpP => //; exists x.
123122
apply/(@lregX _ _ k.+1 (monic_lreg am))/(monic_lreg d_monic).
124123
rewrite -detZ -submatrix_scale -hM' bareiss_block_key_lemma_sub.
125-
by rewrite mulrA [x * _]mulrC mulrACA -exprS [_ * x]mulrC -hx.
124+
by rewrite mulrA [x * _]mulrC mulrACA -exprS [_ * x]mulrC -hx.
126125
Qed.
127126

128127
Lemma bareissE n (M : 'M[{poly R}]_(1 + n))
@@ -166,9 +165,7 @@ Proof.
166165
rewrite /dvd_step => n a M hj.
167166
rewrite -detZ; f_equal.
168167
apply/matrixP => i j; rewrite !mxE.
169-
case: odivrP.
170-
- move => d /=; by rewrite mulrC.
171-
move => h.
168+
case: odivrP=>[d|h] /=; first by rewrite mulrC.
172169
case/dvdrP: (hj i j) => d hd.
173170
by move: (h d); rewrite hd eqxx.
174171
Qed.
@@ -200,17 +197,15 @@ Proof.
200197
rewrite !mxE.
201198
case: splitP => x //; rewrite [x]ord1 {x} !mxE => _.
202199
case: splitP => x; first by rewrite [x]ord1.
203-
rewrite /= /bump /leq0n => /eqP; rewrite eqSS => /eqP h.
204-
by have -> : i = x by apply/ord_inj.
200+
by rewrite /= /bump /leq0n => /eqP; rewrite eqSS => /eqP/ord_inj->.
205201
Qed.
206202

207203
Lemma blockEi0 m n d (l: 'rV[R]_n) (c: 'cV[R]_m) (M: 'M[R]_(m,n)) i:
208204
(block_mx d%:M l c M) (lift 0 i) 0 = (c i 0).
209205
Proof.
210206
rewrite !mxE.
211207
case: splitP => x; first by rewrite [x]ord1.
212-
rewrite /= /bump /leq0n => /eqP; rewrite eqSS => /eqP h.
213-
have -> : i = x by apply/ord_inj.
208+
rewrite /= /bump /leq0n => /eqP; rewrite eqSS => /eqP/ord_inj->.
214209
rewrite !mxE.
215210
by case: splitP => y //; rewrite [y]ord1 {y} => _.
216211
Qed.
@@ -220,12 +215,10 @@ Lemma blockEij m n d (l: 'rV[R]_n) (c: 'cV[R]_m) (M: 'M[R]_(m,n)) i j:
220215
Proof.
221216
rewrite !mxE.
222217
case: splitP => x; first by rewrite [x]ord1.
223-
rewrite /= /bump /leq0n => /eqP; rewrite eqSS => /eqP h.
224-
have -> : i = x by apply/ord_inj.
218+
rewrite /= /bump /leq0n => /eqP; rewrite eqSS => /eqP/ord_inj->.
225219
rewrite !mxE.
226220
case: splitP => y; first by rewrite [y]ord1.
227-
rewrite /= /bump /leq0n => /eqP; rewrite eqSS => /eqP h'.
228-
by have -> : j = y by apply/ord_inj.
221+
by rewrite /= /bump /leq0n => /eqP; rewrite eqSS => /eqP/ord_inj->.
229222
Qed.
230223

231224
(*
@@ -275,10 +268,8 @@ have h4 : forall i j, a %| M' i j.
275268
*)
276269
have h6 : forall i j, M' i j = a * M'' i j.
277270
- move => i j; rewrite [(dvd_step _ _) i j]mxE.
278-
case: odivrP.
279-
+ move => dv /=; by rewrite mulrC.
280-
move => h.
281-
case/dvdrP: (h4 i j ) => dv hdv.
271+
case: odivrP => [dv|h] /=; first by rewrite mulrC.
272+
case/dvdrP: (h4 i j) => dv hdv.
282273
by move: (h dv); rewrite hdv eqxx.
283274
have h6' : M' = a *: M'' by apply/matrixP => i j; rewrite h6 !mxE.
284275
(*
@@ -308,10 +299,7 @@ have h10 : forall k (f1: 'I_k.+1 -> 'I_m) (f2: 'I_k.+1 -> 'I_n),
308299
have hMk : d^+ k.+1 != 0 by apply/lregP/lregX.
309300
rewrite -(@dvdr_mul2l _ d) // mulrA h8 //.
310301
by rewrite mulrAC -exprS dvdr_mul2l //.
311-
split.
312-
- exact : h2.
313-
- exact : h10.
314-
- exact : h6'.
302+
split=> //.
315303
rewrite -/M'' => p h h'.
316304
apply/(@lregMl _ (a ^+ p.+1)).
317305
rewrite -h7.
@@ -330,17 +318,16 @@ Qed.
330318
formal definition of bareiss algorithm
331319
*)
332320
Fixpoint bareiss_rec m a : 'M[R]_(1 + m) -> R :=
333-
match m return 'M[R]_(1 + m) -> R with
334-
| S p => fun (M: 'M[R]_(1 + _)) =>
335-
let d := M 0 0 in
336-
let l := ursubmx M in
337-
let c := dlsubmx M in
338-
let N := drsubmx M in
339-
let: M' := d *: N - c *m l in
340-
let: M'' := dvd_step a M' in
341-
bareiss_rec d M''
342-
| _ => fun M => M 0 0
343-
end.
321+
if m is p.+1 return 'M[R]_(1 + m) -> R then
322+
fun (M: 'M[R]_(1 + _)) =>
323+
let d := M 0 0 in
324+
let l := ursubmx M in
325+
let c := dlsubmx M in
326+
let N := drsubmx M in
327+
let M' := d *: N - c *m l in
328+
let M'' := dvd_step a M' in
329+
bareiss_rec d M''
330+
else fun M => M 0 0.
344331

345332
(*
346333
from sketch, we can express the properties of bareiss
@@ -444,9 +431,7 @@ Lemma char_poly_altE : forall n (M: 'M[R]_(1 + n)),
444431
char_poly_alt M = char_poly M.
445432
Proof.
446433
rewrite /char_poly_alt /char_poly => n M.
447-
rewrite bareissE //.
448-
move => p h h'; apply/monic_lreg.
449-
apply pminor_char_poly_mx_monic.
434+
by rewrite bareissE // => p h h'; exact/monic_lreg/pminor_char_poly_mx_monic.
450435
Qed.
451436

452437
(* The actual determinant function based on bareiss *)

0 commit comments

Comments
 (0)