@@ -1,10 +1,11 @@
1+{-# LANGUAGE ConstraintKinds #-}
2+{-# LANGUAGE TypeFamilies #-}
1{-# LANGUAGE AllowAmbiguousTypes #-}
3{-# LANGUAGE AllowAmbiguousTypes #-}
2{-# LANGUAGE DataKinds #-}
4{-# LANGUAGE DataKinds #-}
3{-# LANGUAGE DerivingVia #-}
5{-# LANGUAGE DerivingVia #-}
4{-# LANGUAGE FlexibleContexts #-}
6{-# LANGUAGE FlexibleContexts #-}
5{-# LANGUAGE FlexibleInstances #-}
7{-# LANGUAGE FlexibleInstances #-}
6{-# LANGUAGE GADTs #-}
8{-# LANGUAGE GADTs #-}
7−{-# LANGUAGE KindSignatures #-}
8{-# LANGUAGE MultiParamTypeClasses #-}
9{-# LANGUAGE MultiParamTypeClasses #-}
9{-# LANGUAGE NoStarIsType #-}
10{-# LANGUAGE NoStarIsType #-}
10{-# LANGUAGE RankNTypes #-}
11{-# LANGUAGE RankNTypes #-}
@@ -15,7 +16,7 @@
16-----------------------------------------------------------------------------
17-----------------------------------------------------------------------------
18−-- Module : Matrix.Type
19+-- Module : LAoP.Matrix.Type
19-- Copyright : (c) Armando Santos 2019-2020
20-- Copyright : (c) Armando Santos 2019-2020
20-- Maintainer : armandoifsantos@gmail.com
21-- Maintainer : armandoifsantos@gmail.com
21-- Stability : experimental
22-- Stability : experimental
@@ -36,7 +37,7 @@
37-----------------------------------------------------------------------------
38-----------------------------------------------------------------------------
39−module Matrix.Type
40+module LAoP.Matrix.Type
40 ( -- | LAoP (Linear Algebra of Programming) Inductive Matrix definition.
41 ( -- | LAoP (Linear Algebra of Programming) Inductive Matrix definition.
42 -- LAoP generalises relations and functions treating them as
43 -- LAoP generalises relations and functions treating them as
@@ -55,15 +56,24 @@ module Matrix.Type
55 -- * Type safe matrix representation
56 -- * Type safe matrix representation
56 Matrix (..),
57 Matrix (..),
59+ -- * Constraint type aliases
58 -- * Type aliases
69 -- * Type aliases
62 -- * Primitives
73 -- * Primitives
68 -- * Auxiliary type families
78 -- * Auxiliary type families
69 I.FromNat,
79 I.FromNat,
@@ -75,24 +85,27 @@ module Matrix.Type
75 fromLists,
85 fromLists,
78 matrixBuilder,
89 matrixBuilder,
86 -- * Functor instance equivalent function
98 -- * Functor instance equivalent function
89 -- * Applicative/Monoidal instance equivalent functions
102 -- * Applicative/Monoidal instance equivalent functions
93 -- * Selective equivalent instance function
106 -- * Selective equivalent instance function
94− selectM,
107+ selectM,
96 -- * Monad equivalent instance function
109 -- * Monad equivalent instance function
@@ -100,25 +113,34 @@ module Matrix.Type
100 -- * Misc
113 -- * Misc
101 -- ** Get dimensions
114 -- ** Get dimensions
105 -- ** Matrix Transposition
120 -- ** Matrix Transposition
123+ -- ** Scalar multiplication/division of matrices
108 -- ** McCarthy's Conditional
127 -- ** McCarthy's Conditional
111 -- ** Matrix "abiding"
130 -- ** Matrix "abiding"
112− abideJS,
131+ abideJF,
113− abideSJ,
132+ abideFJ,
115 -- * Biproduct approach
137 -- * Biproduct approach
116− -- ** Split
138+ -- ** Fork
118 -- *** Projections
140 -- *** Projections
121− -- ** Junc
143+ -- ** Join
123 -- *** Injections
145 -- *** Injections
@@ -132,11 +154,11 @@ module Matrix.Type
132 -- | Note that given the restrictions imposed it is not possible to
154 -- | Note that given the restrictions imposed it is not possible to
133 -- implement the standard type classes present in standard Haskell.
155 -- implement the standard type classes present in standard Haskell.
134 -- *** Matrix pairing projections
156 -- *** Matrix pairing projections
138 -- *** Matrix pairing
160 -- *** Matrix pairing
141 -- * Matrix composition and lifting
163 -- * Matrix composition and lifting
@@ -144,10 +166,13 @@ module Matrix.Type
145 -- | Note that given the restrictions imposed it is not possible to
167 -- | Note that given the restrictions imposed it is not possible to
146 -- implement the standard type classes present in standard Haskell.
168 -- implement the standard type classes present in standard Haskell.
152 -- * Matrix printing
177 -- * Matrix printing
@@ -158,22 +183,44 @@ where
158import Data.Void
183import Data.Void
159import Data.Proxy
184import Data.Proxy
160import Data.Kind
185import Data.Kind
161−import GHC.TypeLits
186+import GHC.TypeLits
162import Control.DeepSeq
187import Control.DeepSeq
163−import qualified Control.Category as C
188+import LAoP.Utils
164−import qualified Matrix.Internal as I
189+import qualified LAoP.Matrix.Internal as I
190+import Prelude hiding (id, (.))
166newtype Matrix e (cols :: Type) (rows :: Type) = M (I.Matrix e (I.Normalize cols) (I.Normalize rows))
192newtype Matrix e (cols :: Type) (rows :: Type) = M (I.Matrix e (I.Normalize cols) (I.Normalize rows))
167 deriving (Show, Num, Eq, Ord, NFData) via (I.Matrix e (I.Normalize cols) (I.Normalize rows))
193 deriving (Show, Num, Eq, Ord, NFData) via (I.Matrix e (I.Normalize cols) (I.Normalize rows))
195+-- | Constraint type synonyms to keep the type signatures less convoluted
196+type Countable a = KnownNat (I.Count a)
197+type CountableDims a b = (Countable a, Countable b)
198+type CountableN a = KnownNat (I.Count (I.Normalize a))
199+type CountableDimsN a b = (CountableN a, CountableN b)
200+type FLN a b = I.FromLists (I.Normalize a) (I.Normalize b)
201+type Liftable e a b = (Bounded a, Bounded b, Enum a, Enum b, Eq b, Num e, Ord e)
202+type Trivial a = I.Normalize (I.Normalize a) ~ I.Normalize (I.Normalize (I.Normalize a))
203+type Trivial2 a = I.Normalize a ~ I.Normalize (I.Normalize a)
204+type Trivial3 a = I.FromNat (I.Count (I.Normalize (I.Normalize a))) ~ I.Normalize (I.Normalize a)
205+type TrivialP a b = I.Normalize (a, b) ~ I.Normalize (I.Normalize a, I.Normalize b)
169−-- | It isn't possible to implement the 'id' function so it's
207+-- | It is possible to implement a constrained version of the category type
170−-- implementation is 'undefined'. However 'comp' can be and this partial
208+-- class.
171−-- class implementation exists just to make the code more readable.
173−-- Please use 'identity' instead.
174−instance (Num e) => C.Category (Matrix e) where
209+instance (Num e) => Category (Matrix e) where
175− id = undefined
210+ type Object (Matrix e) a = (FLN a a, CountableN a)
176− (.) = comp
212+ (.) = comp
214+-- | Bifunctor equivalent function
222+ ) => (a -> b) -> (c -> d) -> Matrix e a c -> Matrix e b d
223+bimapM f g m = fromF g . m . tr (fromF f)
178-- | Zero type alias
225-- | Zero type alias
179type Zero = Void
226type Zero = Void
@@ -183,92 +230,75 @@ type One = ()
184-- Primitives
231-- Primitives
186−-- | Empty matrix constructor
187−empty :: Matrix e Zero Zero
190-- | Unit matrix constructor
233-- | Unit matrix constructor
191one :: e -> Matrix e One One
234one :: e -> Matrix e One One
192one = M . I.One
235one = M . I.One
194−-- | Matrix 'Junc' constructor
237+-- | Matrix 'Join' constructor
196− (I.Normalize (Either a b) ~ Either (I.Normalize a) (I.Normalize b)) =>
197 Matrix e a rows ->
239 Matrix e a rows ->
198 Matrix e b rows ->
240 Matrix e b rows ->
199 Matrix e (Either a b) rows
241 Matrix e (Either a b) rows
200−junc (M a) (M b) = M (I.Junc a b)
242+join (M a) (M b) = M (I.Join a b)
202infixl 3 |||
244infixl 3 |||
203−-- | Matrix 'Junc' constructor
245+-- | Matrix 'Join' constructor
205− (I.Normalize (Either a b) ~ Either (I.Normalize a) (I.Normalize b)) =>
206 Matrix e a rows ->
247 Matrix e a rows ->
207 Matrix e b rows ->
248 Matrix e b rows ->
208 Matrix e (Either a b) rows
249 Matrix e (Either a b) rows
209−(|||) = junc
250+(|||) = join
211−-- | Matrix 'Split' constructor
252+-- | Matrix 'Fork' constructor
213− (I.Normalize (Either a b) ~ Either (I.Normalize a) (I.Normalize b)) =>
214 Matrix e cols a ->
254 Matrix e cols a ->
215 Matrix e cols b ->
255 Matrix e cols b ->
216 Matrix e cols (Either a b)
256 Matrix e cols (Either a b)
217−split (M a) (M b) = M (I.Split a b)
257+fork (M a) (M b) = M (I.Fork a b)
219infixl 2 ===
259infixl 2 ===
220−-- | Matrix 'Split' constructor
260+-- | Matrix 'Fork' constructor
222− (I.Normalize (Either a b) ~ Either (I.Normalize a) (I.Normalize b)) =>
223 Matrix e cols a ->
262 Matrix e cols a ->
224 Matrix e cols b ->
263 Matrix e cols b ->
225 Matrix e cols (Either a b)
264 Matrix e cols (Either a b)
226−(===) = split
265+(===) = fork
228-- Functor hierarchy
267-- Functor hierarchy
230-- | Functor instance equivalent function
269-- | Functor instance equivalent function
231−fmapM ::
270+fmapM ::
232− ( Bounded a,
271+ ( Liftable e a b,
233− Bounded b,
272+ CountableDimsN a b,
239− KnownNat (I.Count (I.Normalize a)),
240− KnownNat (I.Count (I.Normalize b)),
241− I.FromLists e (I.Normalize b) (I.Normalize a)
244 (a -> b) -> Matrix e c a -> Matrix e c b
276 (a -> b) -> Matrix e c a -> Matrix e c b
245−fmapM f m = fromF' f `comp` m
277+fmapM f m = fromF f . m
247-- | Applicative instance equivalent 'unit' function,
279-- | Applicative instance equivalent 'unit' function,
248unitM :: (Num e) => Matrix e () ()
280unitM :: (Num e) => Matrix e () ()
249unitM = one 1
281unitM = one 1
251-- | Applicative instance equivalent 'unit' function,
283-- | Applicative instance equivalent 'unit' function,
252−multM ::
284+multM ::
253− ( KnownNat (I.Count (I.Normalize a)),
285+ ( CountableDimsN a b,
254− KnownNat (I.Count (I.Normalize b)),
286+ CountableN (a, b),
255− KnownNat (I.Count (I.Normalize (a, b))),
257− I.FromLists e (I.Normalize (a, b)) (I.Normalize a),
288+ FLN (a, b) a,
258− I.FromLists e (I.Normalize (a, b)) (I.Normalize b),
289+ FLN (a, b) b,
259− I.Normalize (a, b) ~ I.Normalize (I.Normalize a, I.Normalize b)
290+ TrivialP a b
260 ) => Matrix e c a -> Matrix e c b -> Matrix e c (a, b)
291 ) => Matrix e c a -> Matrix e c b -> Matrix e c (a, b)
261−multM a b = khatri a b
292+multM = kr
264-- | Monad instance equivalent 'return' function,
294-- | Monad instance equivalent 'return' function,
265−returnM ::
295+returnM ::
266− forall e a .
296+ forall e a .
267− ( Num e,
297+ ( Num e,
268− Enum e,
298+ Enum e,
269− Enum a,
299+ Enum a,
270− I.FromLists e () (I.Normalize a),
300+ FLN () a,
271− KnownNat (I.Count a)
301+ Countable a
272 ) => a -> Matrix e One a
302 ) => a -> Matrix e One a
273returnM a = col l
303returnM a = col l
@@ -284,62 +314,66 @@ bindM = flip comp
285-- | Build a matrix out of a list of list of elements. Throws a runtime
315-- | Build a matrix out of a list of list of elements. Throws a runtime
286-- error if the dimensions do not match.
316-- error if the dimensions do not match.
287−fromLists :: (I.FromLists e (I.Normalize cols) (I.Normalize rows)) => [[e]] -> Matrix e cols rows
317+fromLists :: (FLN cols rows) => [[e]] -> Matrix e cols rows
288fromLists = M . I.fromLists
318fromLists = M . I.fromLists
290-- | Matrix builder function. Constructs a matrix provided with
320-- | Matrix builder function. Constructs a matrix provided with
291−-- a construction function.
321+-- a construction function that operates with indices.
323+ (FLN cols rows, CountableDimsN cols rows )
324+ => ((Int, Int) -> e) -> Matrix e cols rows
325+matrixBuilder' = M . I.matrixBuilder'
327+-- | Matrix builder function. Constructs a matrix provided with
328+-- a construction function that operates with arbitrary types.
292matrixBuilder ::
329matrixBuilder ::
293− (I.FromLists e (I.Normalize cols) (I.Normalize rows), KnownNat (I.Count (I.Normalize cols)), KnownNat (I.Count (I.Normalize rows))) =>
330+ ( FLN a b,
294− ((Int, Int) -> e) ->
331+ Enum a,
295− Matrix e cols rows
337+ ) => ((a, b) -> e) -> Matrix e a b
296−matrixBuilder = M . I.matrixBuilder
338+matrixBuilder f = M (I.matrixBuilder f)
298-- | Constructs a column vector matrix
340-- | Constructs a column vector matrix
299−col :: (I.FromLists e () (I.Normalize rows)) => [e] -> Matrix e One rows
341+col :: (FLN () rows) => [e] -> Matrix e One rows
300col = M . I.col
342col = M . I.col
302-- | Constructs a row vector matrix
344-- | Constructs a row vector matrix
303−row :: (I.FromLists e (I.Normalize cols) ()) => [e] -> Matrix e cols One
345+row :: (FLN cols ()) => [e] -> Matrix e cols One
304row = M . I.row
346row = M . I.row
306-- | Lifts functions to matrices with arbitrary dimensions.
348-- | Lifts functions to matrices with arbitrary dimensions.
308-- NOTE: Be careful to not ask for a matrix bigger than the cardinality of
350-- NOTE: Be careful to not ask for a matrix bigger than the cardinality of
309-- types @a@ or @b@ allows.
351-- types @a@ or @b@ allows.
310−fromF ::
352+fromF' ::
311− ( Bounded a,
353+ ( Liftable e a b,
312− Bounded b,
354+ CountableDimsN cols rows,
313− Enum a,
355+ FLN rows cols
318− KnownNat (I.Count (I.Normalize cols)),
319− KnownNat (I.Count (I.Normalize rows)),
320− I.FromLists e (I.Normalize rows) (I.Normalize cols)
323− Matrix e cols rows
357+ (a -> b) -> Matrix e cols rows
324−fromF = M . I.fromF
358+fromF' = M . I.fromF'
326-- | Lifts functions to matrices with dimensions matching @a@ and @b@
360-- | Lifts functions to matrices with dimensions matching @a@ and @b@
327-- cardinality's.
361-- cardinality's.
328−fromF' ::
362+fromF ::
329− ( Bounded a,
363+ ( Liftable e a b,
330− Bounded b,
364+ CountableDimsN a b,
336− KnownNat (I.Count (I.Normalize a)),
337− KnownNat (I.Count (I.Normalize b)),
338− I.FromLists e (I.Normalize b) (I.Normalize a)
341− Matrix e a b
367+ (a -> b) -> Matrix e a b
342−fromF' = M . I.fromF'
368+fromF = M . I.fromF
370+-- | Lifts relation functions to Boolean Matrix
372+ ( Liftable (Natural 0 1) a b,
375+ ) => (a -> b -> Bool) -> Matrix (Natural 0 1) a b
344-- Conversion
378-- Conversion
@@ -355,8 +389,8 @@ toList (M m) = I.toList m
356-- | The zero matrix. A matrix wholly filled with zeros.
390-- | The zero matrix. A matrix wholly filled with zeros.
358− (Num e, I.FromLists e (I.Normalize cols) (I.Normalize rows), KnownNat (I.Count (I.Normalize cols)), KnownNat (I.Count (I.Normalize rows))) =>
392+ (Num e, FLN cols rows, CountableDimsN cols rows)
359− Matrix e cols rows
393+ => Matrix e cols rows
360zeros = M I.zeros
394zeros = M I.zeros
362-- Ones Matrix
396-- Ones Matrix
@@ -365,8 +399,8 @@ zeros = M I.zeros
366-- Also known as T (Top) matrix.
400-- Also known as T (Top) matrix.
368− (Num e, I.FromLists e (I.Normalize cols) (I.Normalize rows), KnownNat (I.Count (I.Normalize cols)), KnownNat (I.Count (I.Normalize rows))) =>
402+ (Num e, FLN cols rows, CountableDimsN cols rows)
369− Matrix e cols rows
403+ => Matrix e cols rows
370ones = M I.ones
404ones = M I.ones
372-- Const Matrix
406-- Const Matrix
@@ -374,9 +408,8 @@ ones = M I.ones
374-- | The constant matrix constructor. A matrix wholly filled with a given
408-- | The constant matrix constructor. A matrix wholly filled with a given
376constant ::
410constant ::
377− (Num e, I.FromLists e (I.Normalize cols) (I.Normalize rows), KnownNat (I.Count (I.Normalize cols)), KnownNat (I.Count (I.Normalize rows))) =>
411+ (Num e, FLN cols rows, CountableDimsN cols rows)
379− Matrix e cols rows
412+ => e -> Matrix e cols rows
380constant = M . I.constant
413constant = M . I.constant
382-- Bang Matrix
415-- Bang Matrix
@@ -384,17 +417,30 @@ constant = M . I.constant
384-- | The T (Top) row vector matrix.
417-- | The T (Top) row vector matrix.
386 forall e cols.
419 forall e cols.
387− (Num e, Enum e, I.FromLists e (I.Normalize cols) (), KnownNat (I.Count (I.Normalize cols))) =>
420+ (Num e, Enum e, FLN cols (), CountableN cols) =>
388 Matrix e cols One
421 Matrix e cols One
389bang = M I.bang
422bang = M I.bang
424+-- | Point constant relation
433+ ) => a -> Matrix e One a
434+point = fromF . const
391−-- Identity Matrix
436+-- iden Matrix
393−-- | Identity matrix
438+-- | iden matrix
394−identity ::
439+iden ::
395− (Num e, I.FromLists e (I.Normalize a) (I.Normalize a), KnownNat (I.Count (I.Normalize a))) =>
440+ (Num e, FLN a a, CountableN a) =>
396 Matrix e a a
441 Matrix e a a
397−identity = M I.identity
442+iden = M I.iden
443+{-# NOINLINE iden #-}
399-- Matrix composition (MMM)
445-- Matrix composition (MMM)
@@ -404,15 +450,32 @@ identity = M I.identity
404-- from LAoP.
450-- from LAoP.
405comp :: (Num e) => Matrix e cr rows -> Matrix e cols cr -> Matrix e cols rows
451comp :: (Num e) => Matrix e cr rows -> Matrix e cols cr -> Matrix e cols rows
406comp (M a) (M b) = M (I.comp a b)
452comp (M a) (M b) = M (I.comp a b)
453+{-# NOINLINE comp #-}
455+ "comp/iden1" forall m. comp m iden = m ;
456+ "comp/iden2" forall m. comp iden m = m
459+-- Scalar multiplication of matrices
462+-- | Scalar multiplication of matrices.
463+(.|) :: Num e => e -> Matrix e cols rows -> Matrix e cols rows
464+(.|) e (M m) = M (e I..| m)
466+-- Scalar division of matrices
469+-- | Scalar multiplication of matrices.
470+(./) :: Fractional e => Matrix e cols rows -> e -> Matrix e cols rows
471+(./) (M m) e = M (m I../ e)
408-- | Biproduct first component projection
473-- | Biproduct first component projection
411− KnownNat (I.Count (I.Normalize n)),
476+ CountableDimsN n m,
412− KnownNat (I.Count (I.Normalize m)),
477+ FLN n m,
413− I.FromLists e (I.Normalize n) (I.Normalize m),
478+ FLN m m
414− I.FromLists e (I.Normalize m) (I.Normalize m),
415− I.Normalize (Either m n) ~ Either (I.Normalize m) (I.Normalize n)
417 Matrix e (Either m n) m
480 Matrix e (Either m n) m
418p1 = M I.p1
481p1 = M I.p1
@@ -420,11 +483,9 @@ p1 = M I.p1
420-- | Biproduct second component projection
483-- | Biproduct second component projection
423− KnownNat (I.Count (I.Normalize n)),
486+ CountableDimsN n m,
424− KnownNat (I.Count (I.Normalize m)),
487+ FLN m n,
425− I.FromLists e (I.Normalize m) (I.Normalize n),
488+ FLN n n
426− I.FromLists e (I.Normalize n) (I.Normalize n),
427− I.Normalize (Either m n) ~ Either (I.Normalize m) (I.Normalize n)
429 Matrix e (Either m n) n
490 Matrix e (Either m n) n
430p2 = M I.p2
491p2 = M I.p2
@@ -434,11 +495,9 @@ p2 = M I.p2
434-- | Biproduct first component injection
495-- | Biproduct first component injection
437− KnownNat (I.Count (I.Normalize n)),
498+ CountableDimsN n m,
438− KnownNat (I.Count (I.Normalize m)),
499+ FLN n m,
439− I.FromLists e (I.Normalize n) (I.Normalize m),
500+ FLN m m
440− I.FromLists e (I.Normalize m) (I.Normalize m),
441− I.Normalize (Either m n) ~ Either (I.Normalize m) (I.Normalize n)
443 Matrix e m (Either m n)
502 Matrix e m (Either m n)
444i1 = tr p1
503i1 = tr p1
@@ -446,11 +505,9 @@ i1 = tr p1
446-- | Biproduct second component injection
505-- | Biproduct second component injection
449− KnownNat (I.Count (I.Normalize n)),
508+ CountableDimsN n m,
450− KnownNat (I.Count (I.Normalize m)),
509+ FLN m n,
451− I.FromLists e (I.Normalize m) (I.Normalize n),
510+ FLN n n
452− I.FromLists e (I.Normalize n) (I.Normalize n),
453− I.Normalize (Either m n) ~ Either (I.Normalize m) (I.Normalize n)
455 Matrix e n (Either m n)
512 Matrix e n (Either m n)
456i2 = tr p2
513i2 = tr p2
@@ -459,24 +516,34 @@ i2 = tr p2
460-- | Obtain the number of rows.
517-- | Obtain the number of rows.
462−-- NOTE: The 'KnownNat' constaint is needed in order to obtain the
519+-- NOTE: The 'KnownNat' constraint is needed in order to obtain the
463−-- dimensions in constant time.
520+-- dimensions in constant time. For a version that doesn't require the
464−--
521+-- constraint see 'rows''.
465−-- TODO: A 'rows' function that does not need the 'KnownNat' constraint in
466−-- exchange for performance.
467−rows :: (KnownNat (I.Count (I.Normalize rows))) => Matrix e cols rows -> Int
522+rows :: (CountableN rows) => Matrix e cols rows -> Int
468rows (M m) = I.rows m
523rows (M m) = I.rows m
525+-- | Obtain the number of rows in an inefficient manner, but without any
528+-- For a more efficient version see 'rows'.
529+rows' :: Matrix e cols rows -> Int
530+rows' (M m) = I.rows' m
470-- | Obtain the number of columns.
532-- | Obtain the number of columns.
472−-- NOTE: The 'KnownNat' constaint is needed in order to obtain the
473−-- dimensions in constant time.
475−-- TODO: A 'columns' function that does not need the 'KnownNat' constraint in
534+-- NOTE: The 'KnownNat' constraint is needed in order to obtain the
476−-- exchange for performance.
535+-- dimensions in constant time. For a version that doesn't require the
536+-- constraint see 'columns''.
477−columns :: (KnownNat (I.Count (I.Normalize cols))) => Matrix e cols rows -> Int
537+columns :: (CountableN cols) => Matrix e cols rows -> Int
478columns (M m) = I.columns m
538columns (M m) = I.columns m
540+-- | Obtain the number of columns in an inefficient manner, but without any
543+-- For a more efficient version see 'columns'.
544+columns' :: Matrix e cols rows -> Int
545+columns' (M m) = I.columns' m
480-- Coproduct Bifunctor
547-- Coproduct Bifunctor
482infixl 5 -|-
549infixl 5 -|-
@@ -484,14 +551,11 @@ infixl 5 -|-
484-- | Matrix coproduct functor also known as matrix direct sum.
551-- | Matrix coproduct functor also known as matrix direct sum.
487− KnownNat (I.Count (I.Normalize j)),
554+ CountableDimsN j k,
488− KnownNat (I.Count (I.Normalize k)),
555+ FLN k k,
489− I.FromLists e (I.Normalize k) (I.Normalize k),
556+ FLN j k,
490− I.FromLists e (I.Normalize j) (I.Normalize k),
557+ FLN k j,
491− I.FromLists e (I.Normalize k) (I.Normalize j),
558+ FLN j j
492− I.FromLists e (I.Normalize j) (I.Normalize j),
493− I.Normalize (Either n m) ~ Either (I.Normalize n) (I.Normalize m),
494− I.Normalize (Either k j) ~ Either (I.Normalize k) (I.Normalize j)
496 Matrix e n k ->
560 Matrix e n k ->
497 Matrix e m j ->
561 Matrix e m j ->
@@ -501,104 +565,98 @@ infixl 5 -|-
501-- Khatri Rao Product and projections
565-- Khatri Rao Product and projections
503-- | Khatri Rao product first component projection matrix.
567-- | Khatri Rao product first component projection matrix.
505 forall e m k .
569 forall e m k .
507− KnownNat (I.Count (I.Normalize m)),
571+ CountableDimsN m k,
508− KnownNat (I.Count (I.Normalize k)),
572+ CountableN (m, k),
509− KnownNat (I.Count (I.Normalize (m, k))),
573+ FLN (m, k) m,
510− I.FromLists e (I.Normalize (m, k)) (I.Normalize m),
574+ TrivialP m k
511− I.Normalize (m, k) ~ I.Normalize (I.Normalize m, I.Normalize k)
512 ) => Matrix e (m, k) m
575 ) => Matrix e (m, k) m
513−kp1 = M (I.kp1 @e @(I.Normalize m) @(I.Normalize k))
576+fstM = M (I.fstM @e @(I.Normalize m) @(I.Normalize k))
515-- | Khatri Rao product second component projection matrix.
578-- | Khatri Rao product second component projection matrix.
517 forall e m k.
580 forall e m k.
519− KnownNat (I.Count (I.Normalize k)),
582+ CountableDimsN k m,
520− KnownNat (I.Count (I.Normalize m)),
583+ CountableN (m, k),
521− KnownNat (I.Count (I.Normalize (m, k))),
584+ FLN (m, k) k,
522− I.FromLists e (I.Normalize (m, k)) (I.Normalize k),
585+ TrivialP m k
523− I.Normalize (m, k) ~ I.Normalize (I.Normalize m, I.Normalize k)
524 ) => Matrix e (m, k) k
586 ) => Matrix e (m, k) k
525−kp2 = M (I.kp2 @e @(I.Normalize m) @(I.Normalize k))
587+sndM = M (I.sndM @e @(I.Normalize m) @(I.Normalize k))
527-- | Khatri Rao Matrix product also known as matrix pairing.
589-- | Khatri Rao Matrix product also known as matrix pairing.
529-- NOTE: That this is not a true categorical product, see for instance:
591-- NOTE: That this is not a true categorical product, see for instance:
532−-- | kp1 `comp` khatri a b == a
594+-- | fstM . kr a b == a
533−-- khatri a b ==> |
595+-- kr a b ==> |
534−-- | kp2 `comp` khatri a b == b
596+-- | sndM . kr a b == b
537-- __Emphasis__ on the implication symbol.
599-- __Emphasis__ on the implication symbol.
539 forall e cols a b.
601 forall e cols a b.
541− KnownNat (I.Count (I.Normalize a)),
603+ CountableDimsN a b,
542− KnownNat (I.Count (I.Normalize b)),
604+ CountableN (a, b),
543− KnownNat (I.Count (I.Normalize (a, b))),
605+ FLN (a, b) a,
544− I.FromLists e (I.Normalize (a, b)) (I.Normalize a),
606+ FLN (a, b) b,
545− I.FromLists e (I.Normalize (a, b)) (I.Normalize b),
607+ TrivialP a b
546− I.Normalize (a, b) ~ I.Normalize (I.Normalize a, I.Normalize b)
547 ) => Matrix e cols a -> Matrix e cols b -> Matrix e cols (a, b)
608 ) => Matrix e cols a -> Matrix e cols b -> Matrix e cols (a, b)
548−khatri a b =
609+kr a b =
549− let kp1' = kp1 @e @a @b
610+ let fstM' = fstM @e @a @b
550− kp2' = kp2 @e @a @b
611+ sndM' = sndM @e @a @b
551− in comp (tr kp1') a * comp (tr kp2') b
612+ in comp (tr fstM') a * comp (tr sndM') b
553-- Product Bifunctor (Kronecker)
614-- Product Bifunctor (Kronecker)
555infixl 4 ><
616infixl 4 ><
557−-- | Matrix product functor also known as kronecker product
618+-- | Matrix product functor also known as Kronecker product
559 forall e m p n q.
620 forall e m p n q.
561− KnownNat (I.Count (I.Normalize m)),
622+ CountableDimsN m n,
562− KnownNat (I.Count (I.Normalize n)),
623+ CountableDimsN p q,
563− KnownNat (I.Count (I.Normalize p)),
624+ CountableDimsN (m, n) (p, q),
564− KnownNat (I.Count (I.Normalize q)),
625+ FLN (m, n) m,
565− KnownNat (I.Count (I.Normalize (m, n))),
626+ FLN (m, n) n,
566− KnownNat (I.Count (I.Normalize (p, q))),
627+ FLN (p, q) p,
567− I.FromLists e (I.Normalize (m, n)) (I.Normalize m),
628+ FLN (p, q) q,
568− I.FromLists e (I.Normalize (m, n)) (I.Normalize n),
629+ TrivialP m n,
569− I.FromLists e (I.Normalize (p, q)) (I.Normalize p),
630+ TrivialP p q
570− I.FromLists e (I.Normalize (p, q)) (I.Normalize q),
571− I.Normalize (m, n) ~ I.Normalize (I.Normalize m, I.Normalize n),
572− I.Normalize (p, q) ~ I.Normalize (I.Normalize p, I.Normalize q)
573 ) => Matrix e m p -> Matrix e n q -> Matrix e (m, n) (p, q)
631 ) => Matrix e m p -> Matrix e n q -> Matrix e (m, n) (p, q)
574(><) a b =
632(><) a b =
575− let kp1' = kp1 @e @m @n
633+ let fstM' = fstM @e @m @n
576− kp2' = kp2 @e @m @n
634+ sndM' = sndM @e @m @n
577− in khatri (comp a kp1') (comp b kp2')
635+ in kr (comp a fstM') (comp b sndM')
579−-- Matrix abide Junc Split
637+-- Matrix abide Join Fork
581−-- | Matrix "abiding" followin the 'Junc'-'Split' abide law.
639+-- | Matrix "abiding" followin the 'Join'-'Fork' abide law.
586−-- 'Junc' ('Split' a c) ('Split' b d) == 'Split' ('Junc' a b) ('Junc' c d)
644+-- 'Join' ('Fork' a c) ('Fork' b d) == 'Fork' ('Join' a b) ('Join' c d)
588−abideJS :: Matrix e cols rows -> Matrix e cols rows
646+abideJF :: Matrix e cols rows -> Matrix e cols rows
589−abideJS (M m) = M (I.abideJS m)
647+abideJF (M m) = M (I.abideJF m)
591−-- Matrix abide Split Junc
649+-- Matrix abide Fork Join
593−-- | Matrix "abiding" followin the 'Split'-'Junc' abide law.
651+-- | Matrix "abiding" followin the 'Fork'-'Join' abide law.
598−-- 'Split' ('Junc' a b) ('Junc' c d) == 'Junc' ('Split' a c) ('Split' b d)
656+-- 'Fork' ('Join' a b) ('Join' c d) == 'Join' ('Fork' a c) ('Fork' b d)
600−abideSJ :: Matrix e cols rows -> Matrix e cols rows
658+abideFJ :: Matrix e cols rows -> Matrix e cols rows
601−abideSJ (M m) = M (I.abideSJ m)
659+abideFJ (M m) = M (I.abideFJ m)
603-- Matrix transposition
661-- Matrix transposition
@@ -610,11 +668,10 @@ tr (M m) = M (I.tr m)
611-- | Selective functors 'select' operator equivalent inspired by the
669-- | Selective functors 'select' operator equivalent inspired by the
612-- ArrowMonad solution presented in the paper.
670-- ArrowMonad solution presented in the paper.
613−selectM ::
671+selectM ::
615− I.FromLists e (I.Normalize b) (I.Normalize b),
673+ FLN b b,
616− KnownNat (I.Count (I.Normalize b)),
674+ CountableN b
617− I.Normalize (Either a b) ~ Either (I.Normalize a) (I.Normalize b)
618 ) => Matrix e cols (Either a b) -> Matrix e a b -> Matrix e cols b
675 ) => Matrix e cols (Either a b) -> Matrix e a b -> Matrix e cols b
619selectM (M m) (M y) = M (I.select m y)
676selectM (M m) (M y) = M (I.select m y)
@@ -622,15 +679,14 @@ selectM (M m) (M y) = M (I.select m y)
623-- | McCarthy's Conditional expresses probabilistic choice.
680-- | McCarthy's Conditional expresses probabilistic choice.
625− ( I.Normalize (I.Normalize a) ~ I.Normalize a,
682+ ( Trivial a,
626− KnownNat (I.Count (I.Normalize a)),
683+ Trivial2 a,
627− I.FromLists e () (I.Normalize a),
684+ Trivial3 a,
628− I.FromLists e (I.Normalize a) (),
685+ CountableN a,
629− I.FromLists e (I.Normalize a) (I.Normalize a),
686+ FLN () a,
630− Bounded a,
687+ FLN a (),
631− Enum a,
688+ FLN a a,
632− Num e,
689+ Liftable e a Bool
636 (a -> Bool) -> Matrix e a b -> Matrix e a b -> Matrix e a b
692 (a -> Bool) -> Matrix e a b -> Matrix e a b -> Matrix e a b
@@ -639,9 +695,13 @@ cond p (M a) (M b) = M (I.cond p a b)
639-- Pretty print
695-- Pretty print
641-- | Matrix pretty printer
697-- | Matrix pretty printer
642−pretty :: (KnownNat (I.Count (I.Normalize cols)), Show e) => Matrix e cols rows -> String
698+pretty :: (CountableDimsN cols rows, Show e) => Matrix e cols rows -> String
643pretty (M m) = I.pretty m
699pretty (M m) = I.pretty m
645-- | Matrix pretty printer
701-- | Matrix pretty printer
646−prettyPrint :: (KnownNat (I.Count (I.Normalize cols)), Show e) => Matrix e cols rows -> IO ()
702+prettyPrint :: (CountableDimsN cols rows, Show e) => Matrix e cols rows -> IO ()
647prettyPrint (M m) = I.prettyPrint m
703prettyPrint (M m) = I.prettyPrint m
705+-- | Zip two matrices with a given binary function
706+zipWithM :: (e -> f -> g) -> Matrix e a b -> Matrix f a b -> Matrix g a b
707+zipWithM f (M a) (M b) = M (I.zipWithM f a b)