on master help sign in
Merge branch 'master' of https://github.com/bolt12/laop
commit10f021a76c338be77de31c7368289bb94c484584 authorArmando Santos <armandoifsantos@gmail.com> dateSun 10 May 2020, 15:40 (6 years ago) parentceee0a2cbe09f5
2 files changed+74−46
src/LAoP/Matrix/Internal.hs+34−18 src/LAoP/Matrix/Type.hs+40−28
src/LAoP/Matrix/Internal.hs+34−18
@@ -30,7 +30,7 @@
3030-- motivation behind the library, the underlying theory, and implementation details.
3131--
3232-- This module offers many of the combinators mentioned in the work of
33−-- Macedo (2012) and Oliveira (2012).
33+-- Macedo (2012) and Oliveira (2012).
3434--
3535-- This is an Internal module and it is no supposed to be imported.
3636--
@@ -43,8 +43,8 @@ module LAoP.Matrix.Internal
4343 --
4444 -- There exists two type families that make it easier to write
4545 -- matrix dimensions: 'FromNat' and 'Count'. This approach
46− -- leads to a very straightforward implementation
47− -- of LAoP combinators.
46+ -- leads to a very straightforward implementation
47+ -- of LAoP combinators.
4848
4949 -- * Type safe matrix representation
5050 Matrix (..),
@@ -86,7 +86,9 @@ module LAoP.Matrix.Internal
8686 -- * Misc
8787 -- ** Get dimensions
8888 columns,
89+ columns',
8990 rows,
91+ rows',
9092
9193 -- ** Matrix Transposition
9294 tr,
@@ -335,7 +337,7 @@ instance (FromLists () rows) => FromLists () (Either () rows) where
335337 fromLists _ = error "Wrong dimensions"
336338
337339instance {-# OVERLAPPABLE #-} (FromLists () a, FromLists () b, Countable a) => FromLists () (Either a b) where
338− fromLists l@([_] : _) =
340+ fromLists l@([_] : _) =
339341 let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
340342 in Fork (fromLists (take rowsA l)) (fromLists (drop rowsA l))
341343 fromLists _ = error "Wrong dimensions"
@@ -511,7 +513,7 @@ comp (Join a b) (Fork c d) = comp a c + comp b d -- Divide-and-conquer l
511513comp (Fork a b) c = Fork (comp a c) (comp b c) -- Fork fusion law
512514comp c (Join a b) = Join (comp c a) (comp c b) -- Join fusion law
513515{-# NOINLINE comp #-}
514−{-# RULES
516+{-# RULES
515517 "comp/iden1" forall m. comp m iden = m ;
516518 "comp/iden2" forall m. comp iden m = m
517519#-}
@@ -558,24 +560,38 @@ i2 = tr p2
558560
559561-- | Obtain the number of rows.
560562--
561−-- NOTE: The 'KnownNat' constaint is needed in order to obtain the
562−-- dimensions in constant time.
563−--
564−-- TODO: A 'rows' function that does not need the 'KnownNat' constraint in
565−-- exchange for performance.
563+-- NOTE: The 'KnownNat' constraint is needed in order to obtain the
564+-- dimensions in constant time. For a version that doesn't require the
565+-- constraint see 'rows''.
566566rows :: forall e cols rows. (Countable rows) => Matrix e cols rows -> Int
567567rows _ = fromInteger $ natVal (Proxy :: Proxy (Count rows))
568568
569+-- | Obtain the number of rows in an inefficient manner, but without any
570+-- constraints.
571+--
572+-- For a more efficient version see 'rows'.
573+rows' :: Matrix e cols rows -> Int
574+rows' (One _) = 1
575+rows' (Join lhs _) = rows' lhs
576+rows' (Fork top bottom) = rows' top + rows' bottom
577+
569578-- | Obtain the number of columns.
570−--
571−-- NOTE: The 'KnownNat' constaint is needed in order to obtain the
572−-- dimensions in constant time.
573579--
574−-- TODO: A 'columns' function that does not need the 'KnownNat' constraint in
575−-- exchange for performance.
580+-- NOTE: The 'KnownNat' constraint is needed in order to obtain the
581+-- dimensions in constant time. For a version that doesn't require the
582+-- constraint see 'columns''.
576583columns :: forall e cols rows. (Countable cols) => Matrix e cols rows -> Int
577584columns _ = fromInteger $ natVal (Proxy :: Proxy (Count cols))
578585
586+-- | Obtain the number of columns in an inefficient manner, but without any
587+-- constraints.
588+--
589+-- For a more efficient version see 'columns'.
590+columns' :: Matrix e cols rows -> Int
591+columns' (One _) = 1
592+columns' (Join lhs rhs) = columns' lhs + columns' rhs
593+columns' (Fork top _) = columns' top
594+
579595-- Coproduct Bifunctor
580596
581597infixl 5 -|-
@@ -630,7 +646,7 @@ sndM = matrixBuilder' f
630646-- | Khatri Rao Matrix product also known as matrix pairing.
631647--
632648-- NOTE: That this is not a true categorical product, see for instance:
633−--
649+--
634650-- @
635651-- | fstM . kr a b == a
636652-- kr a b ==> |
@@ -666,7 +682,7 @@ infixl 4 ><
666682 FL (Normalize (m, n)) n,
667683 FL (Normalize (p, q)) p,
668684 FL (Normalize (p, q)) q
669− )
685+ )
670686 => Matrix e m p -> Matrix e n q -> Matrix e (Normalize (m, n)) (Normalize (p, q))
671687(><) a b =
672688 let fstM' = fstM @e @m @n
@@ -989,7 +1005,7 @@ toRel ::
9891005 Eq b,
9901006 CountableDimsN a b,
9911007 FLN b a
992− )
1008+ )
9931009 => (a -> b -> Bool) -> Relation (Normalize a) (Normalize b)
9941010toRel f =
9951011 let minA = minBound @a
src/LAoP/Matrix/Type.hs+40−28
@@ -104,8 +104,8 @@ module LAoP.Matrix.Type
104104 multM,
105105
106106 -- * Selective equivalent instance function
107− selectM,
108−
107+ selectM,
108+
109109 -- * Monad equivalent instance function
110110 returnM,
111111 bindM,
@@ -113,7 +113,9 @@ module LAoP.Matrix.Type
113113 -- * Misc
114114 -- ** Get dimensions
115115 columns,
116+ columns',
116117 rows,
118+ rows',
117119
118120 -- ** Matrix Transposition
119121 tr,
@@ -265,7 +267,7 @@ infixl 2 ===
265267-- Functor hierarchy
266268
267269-- | Functor instance equivalent function
268−fmapM ::
270+fmapM ::
269271 ( Liftable e a b,
270272 CountableDimsN a b,
271273 FLN b a
@@ -279,7 +281,7 @@ unitM :: (Num e) => Matrix e () ()
279281unitM = one 1
280282
281283-- | Applicative instance equivalent 'unit' function,
282−multM ::
284+multM ::
283285 ( CountableDimsN a b,
284286 CountableN (a, b),
285287 Num e,
@@ -290,8 +292,8 @@ multM ::
290292multM = kr
291293
292294-- | Monad instance equivalent 'return' function,
293−returnM ::
294− forall e a .
295+returnM ::
296+ forall e a .
295297 ( Num e,
296298 Enum e,
297299 Enum a,
@@ -387,7 +389,7 @@ toList (M m) = I.toList m
387389
388390-- | The zero matrix. A matrix wholly filled with zeros.
389391zeros ::
390− (Num e, FLN cols rows, CountableDimsN cols rows)
392+ (Num e, FLN cols rows, CountableDimsN cols rows)
391393 => Matrix e cols rows
392394zeros = M I.zeros
393395
@@ -397,7 +399,7 @@ zeros = M I.zeros
397399--
398400-- Also known as T (Top) matrix.
399401ones ::
400− (Num e, FLN cols rows, CountableDimsN cols rows)
402+ (Num e, FLN cols rows, CountableDimsN cols rows)
401403 => Matrix e cols rows
402404ones = M I.ones
403405
@@ -406,7 +408,7 @@ ones = M I.ones
406408-- | The constant matrix constructor. A matrix wholly filled with a given
407409-- value.
408410constant ::
409− (Num e, FLN cols rows, CountableDimsN cols rows)
411+ (Num e, FLN cols rows, CountableDimsN cols rows)
410412 => e -> Matrix e cols rows
411413constant = M . I.constant
412414
@@ -420,7 +422,7 @@ bang ::
420422bang = M I.bang
421423
422424-- | Point constant relation
423−point ::
425+point ::
424426 ( Bounded a,
425427 Enum a,
426428 Eq a,
@@ -449,7 +451,7 @@ iden = M I.iden
449451comp :: (Num e) => Matrix e cr rows -> Matrix e cols cr -> Matrix e cols rows
450452comp (M a) (M b) = M (I.comp a b)
451453{-# NOINLINE comp #-}
452−{-# RULES
454+{-# RULES
453455 "comp/iden1" forall m. comp m iden = m ;
454456 "comp/iden2" forall m. comp iden m = m
455457#-}
@@ -514,24 +516,34 @@ i2 = tr p2
514516
515517-- | Obtain the number of rows.
516518--
517−-- NOTE: The 'KnownNat' constaint is needed in order to obtain the
518−-- dimensions in constant time.
519−--
520−-- TODO: A 'rows' function that does not need the 'KnownNat' constraint in
521−-- exchange for performance.
519+-- NOTE: The 'KnownNat' constraint is needed in order to obtain the
520+-- dimensions in constant time. For a version that doesn't require the
521+-- constraint see 'rows''.
522522rows :: (CountableN rows) => Matrix e cols rows -> Int
523523rows (M m) = I.rows m
524524
525+-- | Obtain the number of rows in an inefficient manner, but without any
526+-- constraints.
527+--
528+-- For a more efficient version see 'rows'.
529+rows' :: Matrix e cols rows -> Int
530+rows' (M m) = I.rows' m
531+
525532-- | Obtain the number of columns.
526−--
527−-- NOTE: The 'KnownNat' constaint is needed in order to obtain the
528−-- dimensions in constant time.
529533--
530−-- TODO: A 'columns' function that does not need the 'KnownNat' constraint in
531−-- exchange for performance.
534+-- NOTE: The 'KnownNat' constraint is needed in order to obtain the
535+-- dimensions in constant time. For a version that doesn't require the
536+-- constraint see 'columns''.
532537columns :: (CountableN cols) => Matrix e cols rows -> Int
533538columns (M m) = I.columns m
534539
540+-- | Obtain the number of columns in an inefficient manner, but without any
541+-- constraints.
542+--
543+-- For a more efficient version see 'columns'.
544+columns' :: Matrix e cols rows -> Int
545+columns' (M m) = I.columns' m
546+
535547-- Coproduct Bifunctor
536548
537549infixl 5 -|-
@@ -553,7 +565,7 @@ infixl 5 -|-
553565-- Khatri Rao Product and projections
554566
555567-- | Khatri Rao product first component projection matrix.
556−fstM ::
568+fstM ::
557569 forall e m k .
558570 ( Num e,
559571 CountableDimsN m k,
@@ -564,7 +576,7 @@ fstM ::
564576fstM = M (I.fstM @e @(I.Normalize m) @(I.Normalize k))
565577
566578-- | Khatri Rao product second component projection matrix.
567−sndM ::
579+sndM ::
568580 forall e m k.
569581 ( Num e,
570582 CountableDimsN k m,
@@ -577,9 +589,9 @@ sndM = M (I.sndM @e @(I.Normalize m) @(I.Normalize k))
577589-- | Khatri Rao Matrix product also known as matrix pairing.
578590--
579591-- NOTE: That this is not a true categorical product, see for instance:
580−--
592+--
581593-- @
582−-- | fstM . kr a b == a
594+-- | fstM . kr a b == a
583595-- kr a b ==> |
584596-- | sndM . kr a b == b
585597-- @
@@ -625,7 +637,7 @@ infixl 4 ><
625637-- Matrix abide Join Fork
626638
627639-- | Matrix "abiding" followin the 'Join'-'Fork' abide law.
628−--
640+--
629641-- Law:
630642--
631643-- @
@@ -637,7 +649,7 @@ abideJF (M m) = M (I.abideJF m)
637649-- Matrix abide Fork Join
638650
639651-- | Matrix "abiding" followin the 'Fork'-'Join' abide law.
640−--
652+--
641653-- Law:
642654--
643655-- @
@@ -656,7 +668,7 @@ tr (M m) = M (I.tr m)
656668
657669-- | Selective functors 'select' operator equivalent inspired by the
658670-- ArrowMonad solution presented in the paper.
659−selectM ::
671+selectM ::
660672 ( Num e,
661673 FLN b b,
662674 CountableN b