aboutsummaryrefslogtreecommitdiff
path: root/Data/Matrix/Category.agda
blob: 5a33440ea70d581460cc2c39057f909512a9758c (plain)
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
{-# OPTIONS --without-K --safe #-}

open import Algebra using (Semiring)
open import Level using (Level; 0ℓ; _⊔_)

module Data.Matrix.Category {c  : Level} (R : Semiring c ) where

module R = Semiring R

import Data.Vec.Relation.Binary.Pointwise.Inductive as PW
import Relation.Binary.Reasoning.Setoid as ≈-Reasoning

open import Categories.Category using (Category)
open import Categories.Category.Helper using (categoryHelper)
open import Data.Matrix.Core R.setoid using (Matrix; Matrixₛ; _≋_; ≋-isEquiv; _ᵀ; -ᵀ-cong; _∷ₕ_; mapRows; _ᵀᵀ; module ≋; _∥_; _≑_)
open import Data.Matrix.Monoid R.+-monoid using (𝟎; _[+]_)
open import Data.Matrix.Transform R using ([_]_; _[_]; -[-]-cong; [-]--cong; -[-]ᵀ; []-∙; [-]--∥; [++]-≑; I; Iᵀ; I[-]; map--[-]-I; [-]-𝟎; [⟨0⟩]-)
open import Data.Nat using ()
open import Data.Vec using (Vec; map)
open import Data.Vec.Properties using (map-id; map-∘)
open import Data.Vector.Bisemimodule R using (_∙_; ∙-cong)
open import Data.Vector.Core R.setoid using (Vector; Vectorₛ; module ≊; _≊_)
open import Data.Vector.Monoid R.+-monoid using () renaming (⟨ε⟩ to ⟨0⟩)
open import Function using (id)
open import Relation.Binary.PropositionalEquality as  using (_≡_; module ≡-Reasoning)

open R
open Vec
open private
  variable
    n m p :     A B C D : -- matrix multiplication
_·_ : Matrix m p  Matrix n m  Matrix n p
_·_ A B = mapRows ([_] B) A

-- alternative form
_·′_ : Matrix m p  Matrix n m  Matrix n p
_·′_ A B = mapRows (A [_]) (B ) infixr 9 _·_ _·′_

·-·′ : (A : Matrix m p) (B : Matrix n m)  A · B  A ·′ B
·-·′ A B = begin
    mapRows ([_] B) A       ≡⟨ mapRows ([_] B) A ᵀᵀ     mapRows ([_] B) A     ≡⟨ ≡.cong (_ᵀ) (-[-]ᵀ A B)     mapRows (A [_]) (B )    where
    open ≡-Reasoning

opaque

  unfolding _[_]

  ·-[] : {A B C : } (M : Matrix A B) (N : Matrix B C) (V : Vector A)  (N · M) [ V ]  N [ M [ V ] ]
  ·-[] {A} {B} {zero} M [] V = PW.[]
  ·-[] {A} {B} {suc C} M (N₀  N) V = []-∙ N₀ M V PW.∷ ·-[] M N V

opaque

  unfolding [_]_

  []-· : {A B C : } (V : Vector C) (M : Matrix A B) (N : Matrix B C)  [ V ] (N · M)  [ [ V ] N ] M
  []-· {A} {B} {C} V M N = begin
      [ V ] (map ([_] M) N)           ≡⟨ ≡.cong (map (V ∙_)) (-[-]ᵀ N M)       map (V ∙_) (map (N [_]) (M ))  ≡⟨ map-∘ (V ∙_) (N [_]) (M )       map (λ h  V  N [ h ]) (M )   ≈⟨ PW.map⁺ (λ {W} ≋W  trans ([]-∙ V N W) (∙-cong ≊.refl (-[-]-cong N ≋W))) {xs = M } ≋.refl       map ([ V ] N ∙_) (M )              where
      open ≈-Reasoning (Vectorₛ A)

opaque

  unfolding _∥_

  ·-∥
      : (M : Matrix C D)
        (N : Matrix A C)
        (P : Matrix B C)
       M · (N  P)  M · N  M · P
  ·-∥ {C} {D} {A} {B} [] N P = ≡.refl
  ·-∥ {C} {D} {A} {B} (M₀  M) N P = ≡.cong₂ _∷_ ([-]--∥ M₀ N P) (·-∥ M N P)

opaque

  unfolding _≑_

  ≑-· : (M : Matrix B C)
        (N : Matrix B D)
        (P : Matrix A B)
       (M  N) · P  (M · P)  (N · P)
  ≑-· [] N P = ≡.refl
  ≑-· (M₀  M) N P = ≡.cong ([ M₀ ] P ∷_) (≑-· M N P)

opaque

  unfolding _[+]_

  ∥-·-≑
      : (W : Matrix A C)
        (X : Matrix B C)
        (Y : Matrix D A)
        (Z : Matrix D B)
       (W  X) · (Y  Z)  (W · Y) [+] (X · Z)
  ∥-·-≑ [] [] Y Z = PW.[]
  ∥-·-≑ {A} {C} {B} {D} (W₀  W) (X₀  X) Y Z = [++]-≑ W₀ X₀ Y Z PW.∷ ∥-·-≑ W X Y Z
    where
      open ≈-Reasoning (Matrixₛ A B)

opaque

  unfolding Matrix

  ·-resp-≋ : {X X′ : Matrix n p} {Y Y′ : Matrix m n}  X  X′  Y  Y′  X · Y  X′ · Y′
  ·-resp-≋ ≋X ≋Y = PW.map⁺ (λ {_} {y} ≋V  [-]--cong ≋V ≋Y) ≋X

  ·-assoc : {A B C D : } {f : Matrix A B} {g : Matrix B C} {h : Matrix C D}  (h · g) · f  h · g · f
  ·-assoc {A} {B} {C} {D} {f} {g} {h} = begin
      map ([_] f) (map ([_] g) h) ≡⟨ map-∘ ([_] f) ([_] g) h       map (λ v  [ [ v ] g ] f) h ≈⟨ PW.map⁺ (λ {x} x≊y  ≊.trans ([]-· x f g) ([-]--cong ([-]--cong x≊y ≋.refl) ≋.refl)) {xs = h} ≋.refl       map (λ v  [ v ] (g · f)) h     where
      open ≈-Reasoning (Matrixₛ A D)

  ·-Iˡ : {f : Matrix n m}  I · f  f
  ·-Iˡ {A} {B} {f} = begin
      I · f               ≡⟨ ·-·′ I f       map (I [_]) (f )  ≈⟨ -ᵀ-cong (PW.map⁺ (λ {x} ≊V  ≊.trans (I[-] x) ≊V) {xs = f } ≋.refl)       map id (f )       ≡⟨ ≡.cong (_ᵀ) (map-id (f ))       f                 ≡⟨ f ᵀᵀ       f                       where
      open ≈-Reasoning (Matrixₛ A B)

  ·-Iʳ : {f : Matrix n m}  f · I  f
  ·-Iʳ {A} {B} {f} = begin
      f · I               ≡⟨ ·-·′ f I       map (f [_]) (I )  ≈⟨ -ᵀ-cong (≋.reflexive (≡.cong (map (f [_])) Iᵀ))       map (f [_]) I      ≈⟨ -ᵀ-cong (map--[-]-I f)       f                 ≡⟨ f ᵀᵀ       f     where
      open ≈-Reasoning (Matrixₛ A B)

opaque

  unfolding 𝟎

  ·-𝟎ʳ : (M : Matrix A B)   M · 𝟎 {C}  𝟎
  ·-𝟎ʳ [] = ≋.refl
  ·-𝟎ʳ (M₀  M) = begin
      [ M₀ ] 𝟎  M · 𝟎  ≈⟨ [-]-𝟎 M₀ PW.∷ ·-𝟎ʳ M       ⟨0⟩  𝟎               where
      open ≈-Reasoning (Matrixₛ _ _)

  ·-𝟎ˡ : (M : Matrix A B)   𝟎 {B} {C} · M  𝟎
  ·-𝟎ˡ {A} {B} {zero} M = PW.[]
  ·-𝟎ˡ {A} {B} {suc C} M = [⟨0⟩]- M PW.∷ ·-𝟎ˡ M

-- The category of matrices over a rig
Mat : Category 0 c (c  )
Mat = categoryHelper record
    { Obj =     ; _⇒_ = Matrix
    ; _≈_ = _≋_
    ; id = I
    ; _∘_ = _·_
    ; assoc = λ {A B C D f g h}  ·-assoc {f = f} {g} {h}
    ; identityˡ = ·-Iˡ
    ; identityʳ = ·-Iʳ
    ; equiv = ≋-isEquiv
    ; ∘-resp-≈ = ·-resp-≋
    }