Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Smith Normal Form #710

Merged
merged 30 commits into from
Apr 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
7493aef
integers form bezout domain
kangrongji Jan 10, 2022
7411a0e
create base file
kangrongji Jan 10, 2022
42dd776
universe level
kangrongji Jan 14, 2022
7d2731b
finish divisibility
kangrongji Jan 24, 2022
bb048d2
clean
kangrongji Jan 24, 2022
42f72eb
revise
kangrongji Jan 27, 2022
58e2f2f
existence
kangrongji Jan 28, 2022
2a4e87c
Merge branch 'master' into smithexistence
kangrongji Jan 28, 2022
ae3bca5
modify the definition of similarity
kangrongji Jan 29, 2022
543e39e
Merge branch 'smithexistence' of https://github.com/kangrongji/cubica…
kangrongji Jan 29, 2022
e2d61f3
add diagonalization
kangrongji Jan 31, 2022
3a571d4
modify
kangrongji Jan 31, 2022
4fa36c5
renaming
kangrongji Feb 1, 2022
e40bebe
modify
kangrongji Feb 1, 2022
16c55c8
modify
kangrongji Feb 1, 2022
7fd8e00
resolve conflict
kangrongji Feb 10, 2022
4a72484
Merge pull request #2 from agda/master
kangrongji Feb 10, 2022
e8a42a3
Merge branch 'master' into smithexistence
kangrongji Mar 26, 2022
903d864
Merge branch 'master' of https://github.com/agda/cubical into agda-ma…
kangrongji Apr 4, 2022
f54476f
Merge branch 'agda-master' into smithexistence
kangrongji Apr 4, 2022
b32de41
clean
kangrongji Apr 4, 2022
9fa47af
resolve conflicts
kangrongji Apr 4, 2022
3f76fe7
move everything about integers to a new fold
kangrongji Apr 4, 2022
fa90db1
add a base file
kangrongji Apr 4, 2022
2bc4586
safe
kangrongji Apr 4, 2022
c3c59e2
modify
kangrongji Apr 5, 2022
b340a10
indetation
kangrongji Apr 5, 2022
dcbb27a
some benchmarks
kangrongji Apr 5, 2022
a6a6013
typos
kangrongji Apr 5, 2022
a84ba1a
delete unused import
kangrongji Apr 6, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 117 additions & 0 deletions Cubical/Algebra/IntegerMatrix/Base.agda
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
{-

Matrix with coefficients in integers

-}
{-# OPTIONS --safe #-}
module Cubical.Algebra.IntegerMatrix.Base where

open import Cubical.Foundations.Prelude
open import Cubical.Foundations.Function hiding (const)
open import Cubical.Foundations.Powerset

open import Cubical.Data.Nat
hiding (_·_)
renaming (_+_ to _+ℕ_ ; +-assoc to +Assocℕ)
open import Cubical.Data.Nat.Order
open import Cubical.Data.Nat.Divisibility
using (m∣n→m≤n)
renaming (∣-trans to ∣ℕ-trans)
open import Cubical.Data.Int
hiding (_+_ ; _·_ ; _-_ ; -_ ; addEq)
open import Cubical.Data.Int.Divisibility
open import Cubical.Data.FinData

open import Cubical.Data.Empty as Empty
open import Cubical.Data.Unit as Unit
open import Cubical.Data.Sum
open import Cubical.Data.Sigma

open import Cubical.Algebra.Matrix
open import Cubical.Algebra.Matrix.CommRingCoefficient
open import Cubical.Algebra.Ring.BigOps
open import Cubical.Algebra.CommRing
open import Cubical.Algebra.CommRing.Instances.Int
renaming (ℤ to ℤRing)

open import Cubical.Relation.Nullary
open import Cubical.Induction.WellFounded

private
variable
m n k : ℕ

open CommRingStr (ℤRing .snd)
open Coefficient ℤRing
open Sum (CommRing→Ring ℤRing)

-- Using well-foundedness to do induction

Norm : ℤ → Type
Norm n = Acc _<_ (abs n)


-- Divisibility of matrix elements

∣-sum :
(a : ℤ)(V : FinVec ℤ n)
→ ((i : Fin n) → a ∣ V i)
→ a ∣ ∑ V
∣-sum {n = 0} _ _ _ = ∣-zeroʳ
∣-sum {n = suc n} _ _ p = ∣-+ (p zero) (∣-sum _ _ (p ∘ suc))

∣-left⋆ :
(a : ℤ)(M : Mat m n)(N : Mat n k)
→ ((i : Fin m)(j : Fin n) → a ∣ M i j)
→ (i : Fin m)(j : Fin k) → a ∣ (M ⋆ N) i j
∣-left⋆ a M N div i j =
∣-sum a (λ l → M i l · N l j) (λ l → ∣-left· (div i l))

∣-right⋆ :
(a : ℤ)(M : Mat m n)(N : Mat n k)
→ ((i : Fin n)(j : Fin k) → a ∣ N i j)
→ (i : Fin m)(j : Fin k) → a ∣ (M ⋆ N) i j
∣-right⋆ a M N div i j =
∣-sum a (λ l → M i l · N l j) (λ l → ∣-right· {n = M i l} (div l j))

open SimRel
open Sim

sim∣ :
(a : ℤ)(M : Mat m n)
→ (sim : Sim M)
→ ((i : Fin m)(j : Fin n) → a ∣ M i j)
→ (i : Fin m)(j : Fin n) → a ∣ sim .result i j
sim∣ a M sim div i j =
subst (a ∣_) (λ t → sim .simrel .transEq (~ t) i j)
(∣-left⋆ _ _ (sim .simrel .transMatR)
(∣-right⋆ _ (sim .simrel .transMatL) _ div) i j)


-- Find a pivot to begin reduction

data PivotOrNot (a : ℤ)(M : Mat (suc m) (suc n)) : Type where
pivot : (i : Fin (suc m))(j : Fin (suc n))(p : ¬ a ∣ M i j) → PivotOrNot a M
noPivot : ((i : Fin (suc m))(j : Fin (suc n)) → a ∣ M i j) → PivotOrNot a M

findPivot : (a : ℤ)(M : Mat (suc m) (suc n)) → PivotOrNot a M
findPivot a M =
let pivot? = ∀Dec2 (λ i j → a ∣ M i j) (λ _ _ → dec∣ _ _) in
case pivot?
return (λ _ → PivotOrNot a M) of λ
{ (inl p) → noPivot p
; (inr p) → pivot (p .fst) (p .snd .fst) (p . snd .snd) }

-- Find an non-zero element

data NonZeroOrNot (M : Mat (suc m) (suc n)) : Type where
hereIs : (i : Fin (suc m))(j : Fin (suc n))(p : ¬ M i j ≡ 0) → NonZeroOrNot M
allZero : M ≡ 𝟘 → NonZeroOrNot M

findNonZero : (M : Mat (suc m) (suc n)) → NonZeroOrNot M
findNonZero M =
let nonZero? = ∀Dec2 (λ i j → M i j ≡ 0) (λ _ _ → discreteℤ _ 0) in
case nonZero?
return (λ _ → NonZeroOrNot M) of λ
{ (inl p) → allZero (λ t i j → p i j t)
; (inr p) → hereIs (p .fst) (p .snd .fst) (p . snd .snd) }
Loading