on master help sign in
Merge branch 'master' of https://github.com/bolt12/laop
10f021aArmando Santos
src/LAoP/Matrix/Internal.hs+34−18
@@ -30,7 +30,7 @@
30-- motivation behind the library, the underlying theory, and implementation details.
30-- motivation behind the library, the underlying theory, and implementation details.
31--
31--
32-- This module offers many of the combinators mentioned in the work of
32-- This module offers many of the combinators mentioned in the work of
33−-- Macedo (2012) and Oliveira (2012).
33+-- Macedo (2012) and Oliveira (2012).
34--
34--
35-- This is an Internal module and it is no supposed to be imported.
35-- This is an Internal module and it is no supposed to be imported.
36--
36--
@@ -43,8 +43,8 @@ module LAoP.Matrix.Internal
43 --
43 --
44 -- There exists two type families that make it easier to write
44 -- There exists two type families that make it easier to write
45 -- matrix dimensions: 'FromNat' and 'Count'. This approach
45 -- matrix dimensions: 'FromNat' and 'Count'. This approach
46− -- leads to a very straightforward implementation
46+ -- leads to a very straightforward implementation
47− -- of LAoP combinators.
47+ -- of LAoP combinators.
48
48
49 -- * Type safe matrix representation
49 -- * Type safe matrix representation
50 Matrix (..),
50 Matrix (..),
@@ -86,7 +86,9 @@ module LAoP.Matrix.Internal
86 -- * Misc
86 -- * Misc
87 -- ** Get dimensions
87 -- ** Get dimensions
88 columns,
88 columns,
89+ columns',
89 rows,
90 rows,
91+ rows',
90
92
91 -- ** Matrix Transposition
93 -- ** Matrix Transposition
92 tr,
94 tr,
@@ -335,7 +337,7 @@ instance (FromLists () rows) => FromLists () (Either () rows) where
335 fromLists _ = error "Wrong dimensions"
337 fromLists _ = error "Wrong dimensions"
336
338
337instance {-# OVERLAPPABLE #-} (FromLists () a, FromLists () b, Countable a) => FromLists () (Either a b) where
339instance {-# OVERLAPPABLE #-} (FromLists () a, FromLists () b, Countable a) => FromLists () (Either a b) where
338− fromLists l@([_] : _) =
340+ fromLists l@([_] : _) =
339 let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
341 let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
340 in Fork (fromLists (take rowsA l)) (fromLists (drop rowsA l))
342 in Fork (fromLists (take rowsA l)) (fromLists (drop rowsA l))
341 fromLists _ = error "Wrong dimensions"
343 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
511comp (Fork a b) c = Fork (comp a c) (comp b c) -- Fork fusion law
513comp (Fork a b) c = Fork (comp a c) (comp b c) -- Fork fusion law
512comp c (Join a b) = Join (comp c a) (comp c b) -- Join fusion law
514comp c (Join a b) = Join (comp c a) (comp c b) -- Join fusion law
513{-# NOINLINE comp #-}
515{-# NOINLINE comp #-}
514−{-# RULES
516+{-# RULES
515 "comp/iden1" forall m. comp m iden = m ;
517 "comp/iden1" forall m. comp m iden = m ;
516 "comp/iden2" forall m. comp iden m = m
518 "comp/iden2" forall m. comp iden m = m
517#-}
519#-}
@@ -558,24 +560,38 @@ i2 = tr p2
558
560
559-- | Obtain the number of rows.
561-- | Obtain the number of rows.
560--
562--
561−-- NOTE: The 'KnownNat' constaint is needed in order to obtain the
563+-- NOTE: The 'KnownNat' constraint is needed in order to obtain the
562−-- dimensions in constant time.
564+-- dimensions in constant time. For a version that doesn't require the
563−--
565+-- constraint see 'rows''.
564−-- TODO: A 'rows' function that does not need the 'KnownNat' constraint in
565−-- exchange for performance.
566rows :: forall e cols rows. (Countable rows) => Matrix e cols rows -> Int
566rows :: forall e cols rows. (Countable rows) => Matrix e cols rows -> Int
567rows _ = fromInteger $ natVal (Proxy :: Proxy (Count rows))
567rows _ = fromInteger $ natVal (Proxy :: Proxy (Count rows))
568
568
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+
569-- | Obtain the number of columns.
578-- | Obtain the number of columns.
570−--
571−-- NOTE: The 'KnownNat' constaint is needed in order to obtain the
572−-- dimensions in constant time.
573--
579--
574−-- TODO: A 'columns' function that does not need the 'KnownNat' constraint in
580+-- NOTE: The 'KnownNat' constraint is needed in order to obtain the
575−-- exchange for performance.
581+-- dimensions in constant time. For a version that doesn't require the
582+-- constraint see 'columns''.
576columns :: forall e cols rows. (Countable cols) => Matrix e cols rows -> Int
583columns :: forall e cols rows. (Countable cols) => Matrix e cols rows -> Int
577columns _ = fromInteger $ natVal (Proxy :: Proxy (Count cols))
584columns _ = fromInteger $ natVal (Proxy :: Proxy (Count cols))
578
585
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+
579-- Coproduct Bifunctor
595-- Coproduct Bifunctor
580
596
581infixl 5 -|-
597infixl 5 -|-
@@ -630,7 +646,7 @@ sndM = matrixBuilder' f
630-- | Khatri Rao Matrix product also known as matrix pairing.
646-- | Khatri Rao Matrix product also known as matrix pairing.
631--
647--
632-- NOTE: That this is not a true categorical product, see for instance:
648-- NOTE: That this is not a true categorical product, see for instance:
633−--
649+--
634-- @
650-- @
635-- | fstM . kr a b == a
651-- | fstM . kr a b == a
636-- kr a b ==> |
652-- kr a b ==> |
@@ -666,7 +682,7 @@ infixl 4 ><
666 FL (Normalize (m, n)) n,
682 FL (Normalize (m, n)) n,
667 FL (Normalize (p, q)) p,
683 FL (Normalize (p, q)) p,
668 FL (Normalize (p, q)) q
684 FL (Normalize (p, q)) q
669− )
685+ )
670 => Matrix e m p -> Matrix e n q -> Matrix e (Normalize (m, n)) (Normalize (p, q))
686 => Matrix e m p -> Matrix e n q -> Matrix e (Normalize (m, n)) (Normalize (p, q))
671(><) a b =
687(><) a b =
672 let fstM' = fstM @e @m @n
688 let fstM' = fstM @e @m @n
@@ -989,7 +1005,7 @@ toRel ::
989 Eq b,
1005 Eq b,
990 CountableDimsN a b,
1006 CountableDimsN a b,
991 FLN b a
1007 FLN b a
992− )
1008+ )
993 => (a -> b -> Bool) -> Relation (Normalize a) (Normalize b)
1009 => (a -> b -> Bool) -> Relation (Normalize a) (Normalize b)
994toRel f =
1010toRel f =
995 let minA = minBound @a
1011 let minA = minBound @a
src/LAoP/Matrix/Type.hs+40−28
@@ -104,8 +104,8 @@ module LAoP.Matrix.Type
104 multM,
104 multM,
105
105
106 -- * Selective equivalent instance function
106 -- * Selective equivalent instance function
107− selectM,
107+ selectM,
108−
108+
109 -- * Monad equivalent instance function
109 -- * Monad equivalent instance function
110 returnM,
110 returnM,
111 bindM,
111 bindM,
@@ -113,7 +113,9 @@ module LAoP.Matrix.Type
113 -- * Misc
113 -- * Misc
114 -- ** Get dimensions
114 -- ** Get dimensions
115 columns,
115 columns,
116+ columns',
116 rows,
117 rows,
118+ rows',
117
119
118 -- ** Matrix Transposition
120 -- ** Matrix Transposition
119 tr,
121 tr,
@@ -265,7 +267,7 @@ infixl 2 ===
265-- Functor hierarchy
267-- Functor hierarchy
266
268
267-- | Functor instance equivalent function
269-- | Functor instance equivalent function
268−fmapM ::
270+fmapM ::
269 ( Liftable e a b,
271 ( Liftable e a b,
270 CountableDimsN a b,
272 CountableDimsN a b,
271 FLN b a
273 FLN b a
@@ -279,7 +281,7 @@ unitM :: (Num e) => Matrix e () ()
279unitM = one 1
281unitM = one 1
280
282
281-- | Applicative instance equivalent 'unit' function,
283-- | Applicative instance equivalent 'unit' function,
282−multM ::
284+multM ::
283 ( CountableDimsN a b,
285 ( CountableDimsN a b,
284 CountableN (a, b),
286 CountableN (a, b),
285 Num e,
287 Num e,
@@ -290,8 +292,8 @@ multM ::
290multM = kr
292multM = kr
291
293
292-- | Monad instance equivalent 'return' function,
294-- | Monad instance equivalent 'return' function,
293−returnM ::
295+returnM ::
294− forall e a .
296+ forall e a .
295 ( Num e,
297 ( Num e,
296 Enum e,
298 Enum e,
297 Enum a,
299 Enum a,
@@ -387,7 +389,7 @@ toList (M m) = I.toList m
387
389
388-- | The zero matrix. A matrix wholly filled with zeros.
390-- | The zero matrix. A matrix wholly filled with zeros.
389zeros ::
391zeros ::
390− (Num e, FLN cols rows, CountableDimsN cols rows)
392+ (Num e, FLN cols rows, CountableDimsN cols rows)
391 => Matrix e cols rows
393 => Matrix e cols rows
392zeros = M I.zeros
394zeros = M I.zeros
393
395
@@ -397,7 +399,7 @@ zeros = M I.zeros
397--
399--
398-- Also known as T (Top) matrix.
400-- Also known as T (Top) matrix.
399ones ::
401ones ::
400− (Num e, FLN cols rows, CountableDimsN cols rows)
402+ (Num e, FLN cols rows, CountableDimsN cols rows)
401 => Matrix e cols rows
403 => Matrix e cols rows
402ones = M I.ones
404ones = M I.ones
403
405
@@ -406,7 +408,7 @@ ones = M I.ones
406-- | The constant matrix constructor. A matrix wholly filled with a given
408-- | The constant matrix constructor. A matrix wholly filled with a given
407-- value.
409-- value.
408constant ::
410constant ::
409− (Num e, FLN cols rows, CountableDimsN cols rows)
411+ (Num e, FLN cols rows, CountableDimsN cols rows)
410 => e -> Matrix e cols rows
412 => e -> Matrix e cols rows
411constant = M . I.constant
413constant = M . I.constant
412
414
@@ -420,7 +422,7 @@ bang ::
420bang = M I.bang
422bang = M I.bang
421
423
422-- | Point constant relation
424-- | Point constant relation
423−point ::
425+point ::
424 ( Bounded a,
426 ( Bounded a,
425 Enum a,
427 Enum a,
426 Eq a,
428 Eq a,
@@ -449,7 +451,7 @@ iden = M I.iden
449comp :: (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
450comp (M a) (M b) = M (I.comp a b)
452comp (M a) (M b) = M (I.comp a b)
451{-# NOINLINE comp #-}
453{-# NOINLINE comp #-}
452−{-# RULES
454+{-# RULES
453 "comp/iden1" forall m. comp m iden = m ;
455 "comp/iden1" forall m. comp m iden = m ;
454 "comp/iden2" forall m. comp iden m = m
456 "comp/iden2" forall m. comp iden m = m
455#-}
457#-}
@@ -514,24 +516,34 @@ i2 = tr p2
514
516
515-- | Obtain the number of rows.
517-- | Obtain the number of rows.
516--
518--
517−-- NOTE: The 'KnownNat' constaint is needed in order to obtain the
519+-- NOTE: The 'KnownNat' constraint is needed in order to obtain the
518−-- dimensions in constant time.
520+-- dimensions in constant time. For a version that doesn't require the
519−--
521+-- constraint see 'rows''.
520−-- TODO: A 'rows' function that does not need the 'KnownNat' constraint in
521−-- exchange for performance.
522rows :: (CountableN rows) => Matrix e cols rows -> Int
522rows :: (CountableN rows) => Matrix e cols rows -> Int
523rows (M m) = I.rows m
523rows (M m) = I.rows m
524
524
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+
525-- | Obtain the number of columns.
532-- | Obtain the number of columns.
526−--
527−-- NOTE: The 'KnownNat' constaint is needed in order to obtain the
528−-- dimensions in constant time.
529--
533--
530−-- TODO: A 'columns' function that does not need the 'KnownNat' constraint in
534+-- NOTE: The 'KnownNat' constraint is needed in order to obtain the
531−-- exchange for performance.
535+-- dimensions in constant time. For a version that doesn't require the
536+-- constraint see 'columns''.
532columns :: (CountableN cols) => Matrix e cols rows -> Int
537columns :: (CountableN cols) => Matrix e cols rows -> Int
533columns (M m) = I.columns m
538columns (M m) = I.columns m
534
539
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+
535-- Coproduct Bifunctor
547-- Coproduct Bifunctor
536
548
537infixl 5 -|-
549infixl 5 -|-
@@ -553,7 +565,7 @@ infixl 5 -|-
553-- Khatri Rao Product and projections
565-- Khatri Rao Product and projections
554
566
555-- | Khatri Rao product first component projection matrix.
567-- | Khatri Rao product first component projection matrix.
556−fstM ::
568+fstM ::
557 forall e m k .
569 forall e m k .
558 ( Num e,
570 ( Num e,
559 CountableDimsN m k,
571 CountableDimsN m k,
@@ -564,7 +576,7 @@ fstM ::
564fstM = M (I.fstM @e @(I.Normalize m) @(I.Normalize k))
576fstM = M (I.fstM @e @(I.Normalize m) @(I.Normalize k))
565
577
566-- | Khatri Rao product second component projection matrix.
578-- | Khatri Rao product second component projection matrix.
567−sndM ::
579+sndM ::
568 forall e m k.
580 forall e m k.
569 ( Num e,
581 ( Num e,
570 CountableDimsN k m,
582 CountableDimsN k m,
@@ -577,9 +589,9 @@ sndM = M (I.sndM @e @(I.Normalize m) @(I.Normalize k))
577-- | Khatri Rao Matrix product also known as matrix pairing.
589-- | Khatri Rao Matrix product also known as matrix pairing.
578--
590--
579-- 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:
580−--
592+--
581-- @
593-- @
582−-- | fstM . kr a b == a
594+-- | fstM . kr a b == a
583-- kr a b ==> |
595-- kr a b ==> |
584-- | sndM . kr a b == b
596-- | sndM . kr a b == b
585-- @
597-- @
@@ -625,7 +637,7 @@ infixl 4 ><
625-- Matrix abide Join Fork
637-- Matrix abide Join Fork
626
638
627-- | Matrix "abiding" followin the 'Join'-'Fork' abide law.
639-- | Matrix "abiding" followin the 'Join'-'Fork' abide law.
628−--
640+--
629-- Law:
641-- Law:
630--
642--
631-- @
643-- @
@@ -637,7 +649,7 @@ abideJF (M m) = M (I.abideJF m)
637-- Matrix abide Fork Join
649-- Matrix abide Fork Join
638
650
639-- | Matrix "abiding" followin the 'Fork'-'Join' abide law.
651-- | Matrix "abiding" followin the 'Fork'-'Join' abide law.
640−--
652+--
641-- Law:
653-- Law:
642--
654--
643-- @
655-- @
@@ -656,7 +668,7 @@ tr (M m) = M (I.tr m)
656
668
657-- | Selective functors 'select' operator equivalent inspired by the
669-- | Selective functors 'select' operator equivalent inspired by the
658-- ArrowMonad solution presented in the paper.
670-- ArrowMonad solution presented in the paper.
659−selectM ::
671+selectM ::
660 ( Num e,
672 ( Num e,
661 FLN b b,
673 FLN b b,
662 CountableN b
674 CountableN b