on master help sign in
Minor changes
commita0105941f2f968133953d3b916e6779e13d1ea5c authorArmando Santos <armandoifsantos@gmail.com> dateSat 28 Dec 2019, 02:14 (7 years ago) parent50d12b6
2 files changed+104−4
src/Matrix/Nat.hs+46−2 src/Matrix/Type.hs+58−2
src/Matrix/Nat.hs+46−2
@@ -33,6 +33,12 @@ module Matrix.Nat
3333 -- * Type safe matrix representation
3434 Matrix (..),
3535
36+ -- * Primitives
37+ empty,
38+ one,
39+ junc,
40+ split,
41+
3642 -- * Auxiliary type families
3743 I.FromNat,
3844 I.Count,
@@ -59,18 +65,24 @@ module Matrix.Nat
5965 -- ** Matrix Transposition
6066 tr,
6167
68+ -- ** Selective operator
69+ select,
70+
71+ -- ** McCarthy's Conditional
72+ cond,
73+
6274 -- ** Matrix "abiding"
6375 abideJS,
6476 abideSJ,
6577
6678 -- * Biproduct approach
6779 -- ** Split
68− split,
80+ (===),
6981 -- *** Projections
7082 p1,
7183 p2,
7284 -- ** Junc
73− junc,
85+ (|||),
7486 -- *** Injections
7587 i1,
7688 i2,
@@ -129,6 +141,14 @@ junc ::
129141 Matrix e cols3 rows
130142junc (M a) (M b) = M (I.Junc a b)
131143
144+infixl 3 |||
145+(|||) ::
146+ (I.FromNat cols3 ~ Either (I.FromNat cols1) (I.FromNat cols2)) =>
147+ Matrix e cols1 rows ->
148+ Matrix e cols2 rows ->
149+ Matrix e cols3 rows
150+(|||) = junc
151+
132152split ::
133153 (I.FromNat rows3 ~ Either (I.FromNat rows1) (I.FromNat rows2)) =>
134154 Matrix e cols rows1 ->
@@ -136,6 +156,14 @@ split ::
136156 Matrix e cols rows3
137157split (M a) (M b) = M (I.Split a b)
138158
159+infixl 2 ===
160+(===) ::
161+ (I.FromNat rows3 ~ Either (I.FromNat rows1) (I.FromNat rows2)) =>
162+ Matrix e cols rows1 ->
163+ Matrix e cols rows2 ->
164+ Matrix e cols rows3
165+(===) = split
166+
139167-- Construction
140168
141169fromLists :: (I.FromLists e (I.FromNat cols) (I.FromNat rows)) => [[e]] -> Matrix e cols rows
@@ -383,6 +411,22 @@ abideSJ (M m) = M (I.abideSJ m)
383411tr :: Matrix e cols rows -> Matrix e rows cols
384412tr (M m) = M (I.tr m)
385413
414+-- Selective 'select' operator
415+
416+select
417+ :: (Num e, Bounded a1, Bounded b, Enum a1, Enum b, Ord e,
418+ KnownNat (I.Count a2), KnownNat (I.Count (I.FromNat rows1)),
419+ I.FromLists e (I.FromNat rows1) a2,
420+ I.FromLists e (I.FromNat rows1) (I.FromNat rows1), Eq b,
421+ I.FromNat cols1 ~ I.FromNat cols2,
422+ I.FromNat rows2 ~ Either a2 (I.FromNat rows1)) =>
423+ Matrix e cols1 rows2 -> (a1 -> b) -> Matrix e cols2 rows1
424+select (M m) y = M (I.select m y)
425+
426+-- McCarthy's Conditional
427+
428+cond p (M a) (M b) = M (I.cond p a b)
429+
386430-- Pretty print
387431
388432pretty :: Show e => Matrix e cols rows -> String
src/Matrix/Type.hs+58−2
@@ -33,6 +33,12 @@ module Matrix.Type
3333 -- * Type safe matrix representation
3434 Matrix (..),
3535
36+ -- * Primitives
37+ empty,
38+ one,
39+ junc,
40+ split,
41+
3642 -- * Auxiliary type families
3743 FromNat,
3844 Count,
@@ -59,18 +65,24 @@ module Matrix.Type
5965 -- ** Matrix Transposition
6066 tr,
6167
68+ -- ** Selective operator
69+ select,
70+
71+ -- ** McCarthy's Conditional
72+ cond,
73+
6274 -- ** Matrix "abiding"
6375 abideJS,
6476 abideSJ,
6577
6678 -- * Biproduct approach
6779 -- ** Split
68− split,
80+ (===),
6981 -- *** Projections
7082 p1,
7183 p2,
7284 -- ** Junc
73− junc,
85+ (|||),
7486 -- *** Injections
7587 i1,
7688 i2,
@@ -186,9 +198,17 @@ one = One
186198junc :: Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
187199junc = Junc
188200
201+infixl 3 |||
202+(|||) :: Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
203+(|||) = Junc
204+
189205split :: Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
190206split = Split
191207
208+infixl 2 ===
209+(===) :: Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
210+(===) = Split
211+
192212-- Construction
193213
194214class FromLists e cols rows where
@@ -456,6 +476,42 @@ tr (One e) = One e
456476tr (Junc a b) = Split (tr a) (tr b)
457477tr (Split a b) = Junc (tr a) (tr b)
458478
479+-- Selective 'select' operator
480+
481+select ::
482+ ( Num e,
483+ Bounded a1,
484+ Bounded b,
485+ Enum a1,
486+ Enum b,
487+ Ord e,
488+ KnownNat (Count a2),
489+ KnownNat (Count rows),
490+ FromLists e rows a2,
491+ FromLists e rows rows,
492+ Eq b
493+ ) => Matrix e cols (Either a2 rows) -> (a1 -> b) -> Matrix e cols rows
494+select m y = (junc (fromF y) identity) `comp` m
495+
496+-- McCarthy's Conditional
497+
498+cond p f g = junc f g `comp` split (corr p) (corr (not . p))
499+
500+corr ::
501+ forall e a q .
502+ ( KhatriP1 e q q,
503+ KhatriP2 e q q,
504+ FromLists e q q,
505+ KnownNat (Count q),
506+ Bounded a,
507+ Enum a,
508+ Num e,
509+ Ord e
510+ )
511+ => (a -> Bool) -> Matrix e (Normalize (q, q)) (Normalize (q, q))
512+corr p = let f = fromF p :: Matrix e q q
513+ in f >< (identity :: Matrix e q q)
514+
459515-- Pretty print
460516
461517prettyAux :: Show e => [[e]] -> String