@@ -87,10 +87,9 @@ module LAoP.Matrix.Type
87 fromLists,
87 fromLists,
91 matrixBuilder,
90 matrixBuilder,
@@ -125,7 +124,7 @@ module LAoP.Matrix.Type
127 -- ** McCarthy's Conditional
126 -- ** McCarthy's Conditional
130 -- ** Matrix "abiding"
129 -- ** Matrix "abiding"
@@ -168,7 +167,6 @@ module LAoP.Matrix.Type
168 -- implement the standard type classes present in standard Haskell.
167 -- implement the standard type classes present in standard Haskell.
174 -- * Relation
172 -- * Relation
@@ -181,12 +179,11 @@ module LAoP.Matrix.Type
183import Data.Void
181import Data.Void
185import Data.Kind
182import Data.Kind
186import GHC.TypeLits
183import GHC.TypeLits
187import Control.DeepSeq
184import Control.DeepSeq
188import LAoP.Utils
185import LAoP.Utils
189−import qualified LAoP.Matrix.Internal as I
186+import qualified LAoP.Matrix.Alternative as I
190import Prelude hiding (id, (.))
187import Prelude hiding (id, (.))
192newtype Matrix e (cols :: Type) (rows :: Type) = M (I.Matrix e (I.Normalize cols) (I.Normalize rows))
189newtype Matrix e (cols :: Type) (rows :: Type) = M (I.Matrix e (I.Normalize cols) (I.Normalize rows))
@@ -198,27 +195,30 @@ type CountableDimensions a b = (Countable a, Countable b)
198type CountableN a = KnownNat (I.Count (I.Normalize a))
195type CountableN a = KnownNat (I.Count (I.Normalize a))
199type CountableDimensionsN a b = (CountableN a, CountableN b)
196type CountableDimensionsN a b = (CountableN a, CountableN b)
200type FromListsN e a b = I.FromLists e (I.Normalize a) (I.Normalize b)
197type FromListsN e a b = I.FromLists e (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)
198+type Liftable a b = (Eq b, Enumerable a, Enum a, EnumN a, Enum b, EnumN b, ConstructableN a b)
202type Trivial a = I.Normalize (I.Normalize a) ~ I.Normalize (I.Normalize (I.Normalize a))
199type Trivial a = I.Normalize (I.Normalize a) ~ I.Normalize (I.Normalize (I.Normalize a))
203type Trivial2 a = I.Normalize a ~ I.Normalize (I.Normalize a)
200type 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)
205type TrivialP a b = I.Normalize (a, b) ~ I.Normalize (I.Normalize a, I.Normalize b)
201type TrivialP a b = I.Normalize (a, b) ~ I.Normalize (I.Normalize a, I.Normalize b)
202+type EqN a = Eq (I.Normalize a)
203+type EnumN a = Enum (I.Normalize a)
204+type Enumerable a = (Enum a, Bounded a)
205+type EnumerableN a = (Enum (I.Normalize a), Bounded (I.Normalize a))
206+type ConstructNorm a = (Enum a, Enum (I.Normalize a))
207+type ConstructN a = I.Construct (I.Normalize a)
208+type ConstructableN a b = (I.Construct (I.Normalize a), I.Construct (I.Normalize b))
207-- | It is possible to implement a constrained version of the category type
210-- | It is possible to implement a constrained version of the category type
209instance (Num e) => Category (Matrix e) where
212instance (Num e) => Category (Matrix e) where
210− type Object (Matrix e) a = (FromListsN e a a, CountableN a)
213+ type Object (Matrix e) a = (Num e, EqN a, ConstructN a, EnumerableN a)
211 id = iden
214 id = iden
212 (.) = comp
215 (.) = comp
214-- | Bifunctor equivalent function
217-- | Bifunctor equivalent function
216− ( Liftable e a b,
219+ ( Num e,
217− Liftable e c d,
220+ Liftable a b,
218− CountableDimensionsN a c,
221+ Liftable c d
219− CountableDimensionsN b d,
222 ) => (a -> b) -> (c -> d) -> Matrix e a c -> Matrix e b d
222 ) => (a -> b) -> (c -> d) -> Matrix e a c -> Matrix e b d
223bimapM f g m = fromF g . m . tr (fromF f)
223bimapM f g m = fromF g . m . tr (fromF f)
@@ -272,9 +272,8 @@ infixl 2 ===
273-- | Functor instance equivalent function
273-- | Functor instance equivalent function
275− ( Liftable e a b,
276+ Liftable a b
276− CountableDimensionsN a b,
280 (a -> b) -> Matrix e c a -> Matrix e c b
279 (a -> b) -> Matrix e c a -> Matrix e c b
@@ -286,29 +285,18 @@ unitM = one 1
287-- | Applicative instance equivalent 'unit' function,
286-- | Applicative instance equivalent 'unit' function,
289− ( CountableDimensionsN a b,
292− FromListsN e (a, b) a,
289+ FstM a b,
293− FromListsN e (a, b) b,
290+ SndM a b
295 ) => 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)
296multM = kr
292multM = kr
298-- | Monad instance equivalent 'return' function,
294-- | Monad instance equivalent 'return' function,
299−returnM ::
295+returnM ::
302− Enum e,
297+ Liftable () a
306 ) => a -> Matrix e One a
298 ) => a -> Matrix e One a
307−returnM a = col l
299+returnM = point
309− i = fromInteger $ natVal (Proxy :: Proxy (I.Count a))
311− l = take x [0,0..] ++ [1] ++ take (i - x - 1) [0,0..]
313-- | Monad instance equivalent '(>>=)' function,
301-- | Monad instance equivalent '(>>=)' function,
314bindM :: (Num e) => Matrix e a b -> Matrix e b c -> Matrix e a c
302bindM :: (Num e) => Matrix e a b -> Matrix e b c -> Matrix e a c
@@ -321,63 +309,39 @@ bindM = flip comp
321fromLists :: (FromListsN e cols rows) => [[e]] -> Matrix e cols rows
309fromLists :: (FromListsN e cols rows) => [[e]] -> Matrix e cols rows
322fromLists = M . I.fromLists
310fromLists = M . I.fromLists
324−-- | Matrix builder function. Constructs a matrix provided with
325−-- a construction function that operates with indices.
327− (FromListsN e cols rows, CountableDimensionsN cols rows )
328− => ((Int, Int) -> e) -> Matrix e cols rows
329−matrixBuilder' = M . I.matrixBuilder'
331-- | Matrix builder function. Constructs a matrix provided with
312-- | Matrix builder function. Constructs a matrix provided with
332-- a construction function that operates with arbitrary types.
313-- a construction function that operates with arbitrary types.
333matrixBuilder ::
314matrixBuilder ::
334− ( FromListsN e a b,
315+ ( Num e,
335− Enum a,
316+ Enumerable a,
337− Bounded a,
318+ EnumN a,
338− Bounded b,
319+ EnumN b,
339− Eq a,
320+ ConstructableN a b
340− CountableDimensionsN a b
321+ )
341− ) => ((a, b) -> e) -> Matrix e a b
322+ => ((a, b) -> e) -> Matrix e a b
342−matrixBuilder f = M (I.matrixBuilder f)
323+matrixBuilder = M . I.matrixBuilderN
344-- | Constructs a column vector matrix
325-- | Constructs a column vector matrix
345−col :: (FromListsN e () rows) => [e] -> Matrix e One rows
326+colL :: (FromListsN e () rows) => [e] -> Matrix e One rows
346−col = M . I.col
327+colL = M . I.colL
348-- | Constructs a row vector matrix
329-- | Constructs a row vector matrix
349−row :: (FromListsN e cols ()) => [e] -> Matrix e cols One
330+rowL :: (FromListsN e cols ()) => [e] -> Matrix e cols One
350−row = M . I.row
331+rowL = M . I.rowL
352−-- | Lifts functions to matrices with arbitrary dimensions.
354−-- NOTE: Be careful to not ask for a matrix bigger than the cardinality of
355−-- types @a@ or @b@ allows.
358− CountableDimensionsN cols rows,
359− FromListsN e rows cols
361− (a -> b) -> Matrix e cols rows
362−fromF' = M . I.fromF'
364-- | Lifts functions to matrices with dimensions matching @a@ and @b@
333-- | Lifts functions to matrices with dimensions matching @a@ and @b@
365-- cardinality's.
334-- cardinality's.
367− ( Liftable e a b,
337+ Liftable a b
368− CountableDimensionsN a b,
338+ )
371− (a -> b) -> Matrix e a b
339+ => (a -> b) -> Matrix e a b
372−fromF = M . I.fromF
340+fromF = M . I.fromFN
374-- | Lifts relation functions to Boolean Matrix
342-- | Lifts relation functions to Boolean Matrix
376− ( Liftable (Natural 0 1) a b,
377− CountableDimensionsN a b,
378− FromListsN (Natural 0 1) b a
379− ) => (a -> b -> Bool) -> Matrix (Natural 0 1) a b
343+toRel :: Liftable a b => (a -> b -> Bool) -> Matrix (Natural 0 1) a b
380−toRel = M . I.toRel
344+toRel = M . I.toRelN
382-- Conversion
346-- Conversion
@@ -393,8 +357,11 @@ toList (M m) = I.toList m
394-- | The zero matrix. A matrix wholly filled with zeros.
358-- | The zero matrix. A matrix wholly filled with zeros.
396− (Num e, FromListsN e cols rows, CountableDimensionsN cols rows)
360+ ( Num e,
361+ ConstructableN cols rows,
397− => Matrix e cols rows
364+ => Matrix e cols rows
398zeros = M I.zeros
365zeros = M I.zeros
400-- Ones Matrix
367-- Ones Matrix
@@ -403,8 +370,11 @@ zeros = M I.zeros
404-- Also known as T (Top) matrix.
371-- Also known as T (Top) matrix.
406− (Num e, FromListsN e cols rows, CountableDimensionsN cols rows)
373+ ( Num e,
374+ ConstructableN cols rows,
407− => Matrix e cols rows
377+ => Matrix e cols rows
408ones = M I.ones
378ones = M I.ones
410-- Const Matrix
380-- Const Matrix
@@ -412,28 +382,28 @@ ones = M I.ones
412-- | The constant matrix constructor. A matrix wholly filled with a given
382-- | The constant matrix constructor. A matrix wholly filled with a given
414constant ::
384constant ::
415− (Num e, FromListsN e cols rows, CountableDimensionsN cols rows)
385+ ( Num e,
386+ ConstructableN cols rows,
416− => e -> Matrix e cols rows
389+ => e -> Matrix e cols rows
417constant = M . I.constant
390constant = M . I.constant
419-- Bang Matrix
392-- Bang Matrix
421-- | The T (Top) row vector matrix.
394-- | The T (Top) row vector matrix.
423− forall e cols.
396+ ( Num e,
424− (Num e, Enum e, FromListsN e cols (), CountableN cols) =>
397+ ConstructN cols,
425− Matrix e cols One
400+ => Matrix e cols One
426−bang = M I.bang
401+bang = ones
428-- | Point constant matrix
403-- | Point constant matrix
429−point ::
404+point ::
434− Ord e,
406+ Liftable () a
437 ) => a -> Matrix e One a
407 ) => a -> Matrix e One a
438point = fromF . const
408point = fromF . const
@@ -441,8 +411,12 @@ point = fromF . const
442-- | iden matrix
412-- | iden matrix
444− (Num e, FromListsN e a a, CountableN a) =>
414+ ( Num e,
445− Matrix e a a
419+ => Matrix e a a
446iden = M I.iden
420iden = M I.iden
447{-# NOINLINE iden #-}
421{-# NOINLINE iden #-}
@@ -476,44 +450,48 @@ infixl 7 ./
477-- | Biproduct first component projection
451-- | Biproduct first component projection
479− ( Num e,
453+ ( Num e,
480− CountableDimensionsN n m,
454+ EqN m,
481− FromListsN e n m,
455+ ConstructableN m n,
482− FromListsN e m m
456+ EnumerableN m,
484− Matrix e (Either m n) m
459+ => Matrix e (Either m n) m
485p1 = M I.p1
460p1 = M I.p1
487-- | Biproduct second component projection
462-- | Biproduct second component projection
489− ( Num e,
464+ ( Num e,
490− CountableDimensionsN n m,
465+ EqN n,
491− FromListsN e m n,
466+ ConstructableN m n,
492− FromListsN e n n
467+ EnumerableN m,
494− Matrix e (Either m n) n
470+ => Matrix e (Either m n) n
495p2 = M I.p2
471p2 = M I.p2
497-- Injections
473-- Injections
499-- | Biproduct first component injection
475-- | Biproduct first component injection
501− ( Num e,
477+ ( Num e,
502− CountableDimensionsN n m,
478+ EqN m,
503− FromListsN e n m,
479+ ConstructableN m n,
504− FromListsN e m m
480+ EnumerableN m,
506− Matrix e m (Either m n)
483+ => Matrix e m (Either m n)
507i1 = tr p1
484i1 = tr p1
509-- | Biproduct second component injection
486-- | Biproduct second component injection
511− ( Num e,
488+ ( Num e,
512− CountableDimensionsN n m,
489+ EqN n,
513− FromListsN e m n,
490+ ConstructableN m n,
514− FromListsN e n n
491+ EnumerableN m,
516− Matrix e n (Either m n)
494+ => Matrix e n (Either m n)
517i2 = tr p2
495i2 = tr p2
519-- Dimensions
497-- Dimensions
@@ -525,7 +503,7 @@ i2 = tr p2
526-- TODO: A 'rows' function that does not need the 'KnownNat' constraint in
504-- TODO: A 'rows' function that does not need the 'KnownNat' constraint in
527-- exchange for performance.
505-- exchange for performance.
528−rows :: (CountableN rows) => Matrix e cols rows -> Int
506+rows :: CountableN rows => Matrix e cols rows -> Int
529rows (M m) = I.rows m
507rows (M m) = I.rows m
531-- | Obtain the number of columns.
509-- | Obtain the number of columns.
@@ -535,7 +513,7 @@ rows (M m) = I.rows m
536-- TODO: A 'columns' function that does not need the 'KnownNat' constraint in
514-- TODO: A 'columns' function that does not need the 'KnownNat' constraint in
537-- exchange for performance.
515-- exchange for performance.
538−columns :: (CountableN cols) => Matrix e cols rows -> Int
516+columns :: CountableN cols => Matrix e cols rows -> Int
539columns (M m) = I.columns m
517columns (M m) = I.columns m
541-- Coproduct Bifunctor
519-- Coproduct Bifunctor
@@ -544,41 +522,53 @@ infixl 5 -|-
545-- | Matrix coproduct functor also known as matrix direct sum.
523-- | Matrix coproduct functor also known as matrix direct sum.
547− ( Num e,
525+ ( Num e,
548− CountableDimensionsN j k,
526+ EqN k,
549− FromListsN e k k,
527+ EqN j,
550− FromListsN e j k,
528+ ConstructableN k j,
551− FromListsN e k j,
529+ EnumerableN k,
552− FromListsN e j j
530+ EnumerableN j
556− Matrix e (Either n m) (Either k j)
531+ ) => Matrix e n k -> Matrix e m j -> Matrix e (Either n m) (Either k j)
557(-|-) (M a) (M b) = M ((I.-|-) a b)
532(-|-) (M a) (M b) = M ((I.-|-) a b)
559-- Khatri Rao Product and projections
534-- Khatri Rao Product and projections
561−-- | Khatri Rao product first component projection matrix.
536+type FstM a b =
562−fstM ::
537+ ( EqN a,
563− forall e m k .
538+ ConstructableN (I.Normalize a) (I.Normalize (a, b)),
564− ( Num e,
539+ ConstructN (I.Normalize a, I.Normalize b),
565− CountableDimensionsN m k,
540+ EnumerableN a,
566− CountableN (m, k),
541+ EnumerableN b,
567− FromListsN e (m, k) m,
542+ EnumN (I.Normalize a, I.Normalize b),
568− TrivialP m k
543+ Trivial2 a,
569− ) => Matrix e (m, k) m
544+ TrivialP a b
570−fstM = M (I.fstM @e @(I.Normalize m) @(I.Normalize k))
545+ )
547+-- | Khatri Rao product first component projection matrix.
552+ ) => Matrix e (a, b) a
553+fstM = M (I.fstM @e @(I.Normalize a) @(I.Normalize b))
557+ ConstructableN (I.Normalize b) (I.Normalize (a, b)),
558+ ConstructN (I.Normalize a, I.Normalize b),
561+ EnumN (I.Normalize a, I.Normalize b),
572-- | Khatri Rao product second component projection matrix.
565-- | Khatri Rao product second component projection matrix.
574− forall e m k.
567+ forall e a b.
575− ( Num e,
568+ ( Num e,
576− CountableDimensionsN k m,
569+ SndM a b
578− FromListsN e (m, k) k,
580− ) => Matrix e (m, k) k
570+ ) => Matrix e (a, b) b
581−sndM = M (I.sndM @e @(I.Normalize m) @(I.Normalize k))
571+sndM = M (I.sndM @e @(I.Normalize a) @(I.Normalize b))
583-- | Khatri Rao Matrix product also known as matrix pairing.
573-- | Khatri Rao Matrix product also known as matrix pairing.
@@ -592,14 +582,12 @@ sndM = M (I.sndM @e @(I.Normalize m) @(I.Normalize k))
593-- __Emphasis__ on the implication symbol.
583-- __Emphasis__ on the implication symbol.
595− forall e cols a b.
585+ forall e cols a b.
596− ( Num e,
586+ ( Num e,
597− CountableDimensionsN a b,
587+ FstM a b,
598− CountableN (a, b),
588+ SndM a b
599− FromListsN e (a, b) a,
589+ )
600− FromListsN e (a, b) b,
602− ) => Matrix e cols a -> Matrix e cols b -> Matrix e cols (a, b)
590+ => Matrix e cols a -> Matrix e cols b -> Matrix e cols (a, b)
604 let fstM' = fstM @e @a @b
592 let fstM' = fstM @e @a @b
605 sndM' = sndM @e @a @b
593 sndM' = sndM @e @a @b
@@ -608,21 +596,38 @@ kr a b =
608-- Product Bifunctor (Kronecker)
596-- Product Bifunctor (Kronecker)
610infixl 4 ><
598infixl 4 ><
599+type Kronecker m n p q =
600+ ( EqN (I.Normalize m),
601+ EqN (I.Normalize n),
602+ EqN (I.Normalize p),
603+ EqN (I.Normalize q),
604+ ConstructableN (I.Normalize m) (I.Normalize n),
605+ ConstructableN (I.Normalize p) (I.Normalize q),
606+ ConstructableN (I.Normalize m, I.Normalize n) (I.Normalize p, I.Normalize q),
607+ ConstructableN (I.Normalize (I.Normalize m, I.Normalize n)) (I.Normalize (I.Normalize p, I.Normalize q)),
610+ EnumerableN (I.Normalize m),
611+ EnumerableN (I.Normalize n),
612+ EnumerableN (I.Normalize p),
613+ EnumerableN (I.Normalize q),
614+ EnumerableN (I.Normalize m, I.Normalize n),
615+ EnumerableN (I.Normalize p, I.Normalize q),
612-- | Matrix product functor also known as Kronecker product
624-- | Matrix product functor also known as Kronecker product
614− forall e m p n q.
626+ forall e m p n q.
615− ( Num e,
627+ ( Num e,
616− CountableDimensionsN m n,
628+ Kronecker m n p q
617− CountableDimensionsN p q,
629+ )
618− CountableDimensionsN (m, n) (p, q),
619− FromListsN e (m, n) m,
620− FromListsN e (m, n) n,
621− FromListsN e (p, q) p,
622− FromListsN e (p, q) q,
625− ) => Matrix e m p -> Matrix e n q -> Matrix e (m, n) (p, q)
630+ => Matrix e m p -> Matrix e n q -> Matrix e (m, n) (p, q)
626(><) a b =
631(><) a b =
627 let fstM' = fstM @e @m @n
632 let fstM' = fstM @e @m @n
628 sndM' = sndM @e @m @n
633 sndM' = sndM @e @m @n
@@ -662,34 +667,23 @@ tr (M m) = M (I.tr m)
663-- | Selective functors 'select' operator equivalent inspired by the
668-- | Selective functors 'select' operator equivalent inspired by the
664-- ArrowMonad solution presented in the paper.
669-- ArrowMonad solution presented in the paper.
669− ) => Matrix e cols (Either a b) -> Matrix e a b -> Matrix e cols b
670+selectM :: (Num e, EqN b, ConstructN b, EnumerableN b) => Matrix e cols (Either a b) -> Matrix e a b -> Matrix e cols b
670selectM (M m) (M y) = M (I.select m y)
671selectM (M m) (M y) = M (I.select m y)
672-- McCarthy's Conditional
673-- McCarthy's Conditional
674-- | McCarthy's Conditional expresses probabilistic choice.
675-- | McCarthy's Conditional expresses probabilistic choice.
675−cond ::
676+-- cond ::
676− ( Trivial a,
677+-- (
686− (a -> Bool) -> Matrix e a b -> Matrix e a b -> Matrix e a b
680+-- (a -> Bool) -> Matrix e a b -> Matrix e a b -> Matrix e a b
687−cond p (M a) (M b) = M (I.cond p a b)
681+-- cond p (M a) (M b) =
689-- Pretty print
683-- Pretty print
691-- | Matrix pretty printer
685-- | Matrix pretty printer
692−pretty :: (CountableDimensionsN cols rows, Show e) => Matrix e cols rows -> String
686+pretty :: (Show e, CountableDimensionsN cols rows) => Matrix e cols rows -> String
693pretty (M m) = I.pretty m
687pretty (M m) = I.pretty m
695-- | Matrix pretty printer
689-- | Matrix pretty printer