Skip to content
This repository was archived by the owner on Jul 24, 2024. It is now read-only.

Commit ea19776

Browse files
committed
refactor(data/finsupp): generalize module construction for finsupp
1 parent b29ab1b commit ea19776

File tree

3 files changed

+47
-39
lines changed

3 files changed

+47
-39
lines changed

algebra/linear_algebra/basic.lean

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -67,30 +67,15 @@ end finsupp
6767
namespace lc
6868
variables [ring α] [module α β]
6969

70-
instance : has_scalar α (lc α β) :=
71-
⟨λa (v : β →₀ α), v.map_range ((*) a) (mul_zero a)⟩
70+
instance : has_scalar α (lc α β) := finsupp.to_has_scalar
7271

73-
@[simp] lemma smul_apply {a : α} {v : lc α β} {b : β} :
74-
(a • v) b = a * v b :=
75-
rfl
76-
77-
instance : module α (lc α β) :=
78-
{ smul := (•),
79-
smul_add := assume a x y, finsupp.ext $ by simp [mul_add],
80-
add_smul := assume a x y, finsupp.ext $ by simp [add_mul],
81-
one_smul := assume x, finsupp.ext $ by simp,
82-
mul_smul := assume r s x, finsupp.ext $ by simp [mul_assoc],
83-
.. finsupp.add_comm_group }
84-
85-
lemma sum_smul_index {γ : Type x} [add_comm_monoid γ] {g : lc α β} {a : α} {h : β → α → γ}
86-
(h0 : ∀i, h i 0 = 0) : (a • g).sum h = g.sum (λi b, h i (a * b)) :=
87-
finsupp.sum_map_range_index h0
72+
instance : module α (lc α β) := finsupp.to_module
8873

8974
lemma is_linear_map_sum [module α γ] [module α δ] {f : β → α → γ} {g : δ → lc α β}
9075
(hf : ∀b, is_linear_map (f b)) (hg : is_linear_map g) : is_linear_map (λd, (g d).sum f) :=
9176
⟨assume d₁ d₂, by simp [hg.add, finsupp.sum_add_index, (hf _).zero, (hf _).add],
9277
assume a d,
93-
by simp [hg.smul, sum_smul_index, (hf _).zero, finsupp.smul_sum, ((hf _).smul _ _).symm]⟩
78+
by simp [hg.smul, finsupp.sum_smul_index, (hf _).zero, finsupp.smul_sum, ((hf _).smul _ _).symm]⟩
9479

9580
end lc
9681

@@ -144,7 +129,7 @@ def span (s : set β) : set β := { x | ∃(v : lc α β), (∀x∉s, v x = 0)
144129
add_ := assume x y ⟨vx, hx, eqx⟩ ⟨vy, hy, eqy⟩, ⟨vx + vy,
145130
by simp [hx, hy, eqx, eqy, finsupp.sum_add_index, add_smul] {contextual := tt}⟩,
146131
smul := assume a b ⟨v, hv, veq⟩, ⟨a • v,
147-
by simp [hv, veq, lc.sum_smul_index, finsupp.smul_sum, smul_smul] {contextual := tt}⟩ }
132+
by simp [hv, veq, finsupp.sum_smul_index, finsupp.smul_sum, smul_smul] {contextual := tt}⟩ }
148133

149134
lemma subset_span : s ⊆ span s :=
150135
assume b (hb : b ∈ s),
@@ -377,7 +362,7 @@ repr_eq hs (is_submodule.add hb hb')
377362
repr_eq hs (is_submodule.smul _ hb)
378363
(by simp [repr_eq_zero] {contextual := tt})
379364
(calc (a • hs.repr b).sum (λb a, a • b) = (hs.repr b).sum (λb a', a • (a' • b)) :
380-
by simp [lc.sum_smul_index, add_smul, smul_smul]
365+
by simp [finsupp.sum_smul_index, add_smul, smul_smul]
381366
... = a • (hs.repr b).sum (λb a', a' • b) : finsupp.smul_sum.symm
382367
... = a • b : by rw [repr_sum_eq hs hb])
383368

@@ -652,7 +637,7 @@ iff.intro
652637
have l.sum (λb a', (a * a') • b) = a • l.sum (λb a, a • b),
653638
by simp [finsupp.smul_sum, smul_smul],
654639
by simp [-sub_eq_add_neg, add_smul, finsupp.sum_add_index, finsupp.sum_single_index,
655-
lc.sum_smul_index, this, eq]⟩)
640+
finsupp.sum_smul_index, this, eq]⟩)
656641

657642
lemma linear_independent_singleton {b : β} (hb : b ≠ 0) : linear_independent ({b} : set β) :=
658643
linear_independent_iff_not_mem_span.mpr $ by simp [hb] {contextual := tt}

algebra/linear_algebra/multivariate_polynomial.lean

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Authors: Johannes Hölzl
55
66
Multivariate Polynomial
77
-/
8-
import data.finsupp
8+
import data.finsupp algebra.linear_algebra.basic
99
noncomputable theory
1010

1111
open classical set function finsupp lattice
@@ -241,4 +241,12 @@ end total_degree
241241

242242
end comm_semiring
243243

244+
section comm_ring
245+
variable [comm_ring α]
246+
instance : ring (mv_polynomial σ α) := finsupp.to_ring
247+
instance : has_scalar α (mv_polynomial σ α) := finsupp.to_has_scalar
248+
instance : module α (mv_polynomial σ α) := finsupp.to_module
249+
250+
end comm_ring
251+
244252
end mv_polynomial

data/finsupp.lean

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ The best is to define a copy and select the instances best suited.
2121
2222
-/
2323

24-
import data.set.finite data.finset algebra.big_operators
24+
import data.set.finite data.finset algebra.big_operators algebra.module
2525
noncomputable theory
2626

2727
open classical set function
@@ -38,7 +38,8 @@ def finsupp (α : Type u) (β : Type v) [has_zero β] := {f : α → β // finit
3838
infix →₀ := finsupp
3939

4040
namespace finsupp
41-
variables {α : Type u} {β : Type v} {α₁ : Type u₁} {α₂ : Type u₂} {β₁ : Type v₁} {β₂ : Type v₂}
41+
variables {α : Type u} {β : Type v} {γ : Type w}
42+
{α₁ : Type u₁} {α₂ : Type u₂} {β₁ : Type v₁} {β₂ : Type v₂}
4243

4344
section
4445
variable [has_zero β]
@@ -156,27 +157,27 @@ finset.ext.mpr $ assume a, by by_cases p a; simp *
156157
end filter
157158

158159
-- [to_additive finsupp.sum] for finsupp.prod doesn't work, the equation lemmas are not generated
159-
def sum {γ : Type w} [has_zero β] [add_comm_monoid γ] (f : α →₀ β) (g : α → β → γ) : γ :=
160+
def sum [has_zero β] [add_comm_monoid γ] (f : α →₀ β) (g : α → β → γ) : γ :=
160161
f.support.sum (λa, g a (f a))
161162

162163
@[to_additive finsupp.sum]
163-
def prod {γ : Type w} [has_zero β] [comm_monoid γ] (f : α →₀ β) (g : α → β → γ) : γ :=
164+
def prod [has_zero β] [comm_monoid γ] (f : α →₀ β) (g : α → β → γ) : γ :=
164165
f.support.prod (λa, g a (f a))
165166
attribute [to_additive finsupp.sum.equations._eqn_1] finsupp.prod.equations._eqn_1
166167

167168
@[to_additive finsupp.sum_map_range_index]
168-
lemma prod_map_range_index {γ : Type w} [has_zero β₁] [has_zero β₂] [comm_monoid γ]
169+
lemma prod_map_range_index [has_zero β₁] [has_zero β₂] [comm_monoid γ]
169170
{f : β₁ → β₂} {hf : f 0 = 0} {g : α →₀ β₁} {h : α → β₂ → γ} (h0 : ∀a, h a 0 = 1) :
170171
(map_range f hf g).prod h = g.prod (λa b, h a (f b)) :=
171172
finset.prod_subset support_map_range $ by simp [h0] {contextual := tt}
172173

173174
@[to_additive finsupp.sum_zero_index]
174-
lemma prod_zero_index {γ : Type w} [add_comm_monoid β] [comm_monoid γ] {h : α → β → γ} :
175+
lemma prod_zero_index [add_comm_monoid β] [comm_monoid γ] {h : α → β → γ} :
175176
(0 : α →₀ β).prod h = 1 :=
176177
by simp [finsupp.prod]
177178

178179
@[to_additive finsupp.sum_single_index]
179-
lemma prod_single_index {γ : Type w} [add_comm_monoid β] [comm_monoid γ] {a : α} {b : β}
180+
lemma prod_single_index [add_comm_monoid β] [comm_monoid γ] {a : α} {b : β}
180181
{h : α → β → γ} (h_zero : h a 0 = 1) : (single a b).prod h = h a b :=
181182
begin
182183
by_cases b = 0 with h,
@@ -226,18 +227,13 @@ instance [add_group β] : add_group (α →₀ β) :=
226227
.. finsupp.add_monoid }
227228

228229
@[to_additive finsupp.sum_neg_index]
229-
lemma prod_neg_index {γ : Type w} [add_group β] [comm_monoid γ]
230-
{g : α →₀ β} {h : α → β → γ} (h0 : ∀a, h a 0 = 1) :
231-
(-g).prod h = g.prod (λa b, h a (- b)) :=
230+
lemma prod_neg_index [add_group β] [comm_monoid γ] {g : α →₀ β} {h : α → β → γ}
231+
(h0 : ∀a, h a 0 = 1) : (-g).prod h = g.prod (λa b, h a (- b)) :=
232232
prod_map_range_index h0
233233

234-
@[simp] lemma neg_apply [add_group β] {g : α →₀ β} {a : α} :
235-
(- g) a = - g a :=
236-
rfl
234+
@[simp] lemma neg_apply [add_group β] {g : α →₀ β} {a : α} : (- g) a = - g a := rfl
237235

238-
@[simp] lemma sub_apply [add_group β] {g₁ g₂ : α →₀ β} {a : α} :
239-
(g₁ - g₂) a = g₁ a - g₂ a :=
240-
rfl
236+
@[simp] lemma sub_apply [add_group β] {g₁ g₂ : α →₀ β} {a : α} : (g₁ - g₂) a = g₁ a - g₂ a := rfl
241237

242238
@[simp] lemma support_neg [add_group β] {f : α →₀ β} : support (-f) = support f :=
243239
finset.subset.antisymm
@@ -414,7 +410,7 @@ rfl
414410

415411
section comap_domain
416412

417-
variables {α' : Type u₁} {γ : Type x} {δ : Type y} [has_zero δ]
413+
variables {α' : Type u₁} {δ : Type y} [has_zero δ]
418414
{f : α → α'} {hf : injective f} {p : α → Prop}
419415

420416
section zero
@@ -565,4 +561,23 @@ lemma prod_single {ι : Type x} [add_comm_monoid α] [comm_semiring β] {s : fin
565561
s.prod (λi, single (a i) (b i)) = single (s.sum a) (s.prod b) :=
566562
finset.induction_on s (by simp [one_def]) (by simp [single_mul_single] {contextual := tt})
567563

564+
def to_has_scalar [semiring β] : has_scalar β (α →₀ β) := ⟨λa v, v.map_range ((*) a) (mul_zero a)⟩
565+
local attribute [instance] to_has_scalar
566+
567+
@[simp] lemma smul_apply [semiring β] {a : α} {b : β} {v : α →₀ β} : (b • v) a = b * v a :=
568+
rfl
569+
570+
/- should this be stronger? [module γ β] → module γ (α →₀ β) -/
571+
def to_module [ring β] : module β (α →₀ β) :=
572+
{ smul := (•),
573+
smul_add := assume a x y, finsupp.ext $ by simp [mul_add],
574+
add_smul := assume a x y, finsupp.ext $ by simp [add_mul],
575+
one_smul := assume x, finsupp.ext $ by simp,
576+
mul_smul := assume r s x, finsupp.ext $ by simp [mul_assoc],
577+
.. finsupp.add_comm_group }
578+
579+
lemma sum_smul_index [ring β] [add_comm_monoid γ] {g : α →₀ β} {b : β} {h : α → β → γ}
580+
(h0 : ∀i, h i 0 = 0) : (b • g).sum h = g.sum (λi a, h i (b * a)) :=
581+
finsupp.sum_map_range_index h0
582+
568583
end finsupp

0 commit comments

Comments
 (0)