on master help sign in
Added inefficient row and column implementations
commit47340db5d2ce89631021e897be5c9d0f460b3e16 authoriko <ilyakooo0@gmail.com> dateSun 10 May 2020, 00:06 (6 years ago) parent87fd633
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,
@@ -334,7 +336,7 @@ instance {-# OVERLAPPING #-} (FromLists () rows) => FromLists () (Either () rows
334336 fromLists _ = error "Wrong dimensions"
335337
336338instance {-# OVERLAPPABLE #-} (FromLists () a, FromLists () b, Countable a) => FromLists () (Either a b) where
337− fromLists l@([_] : _) =
339+ fromLists l@([_] : _) =
338340 let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
339341 in Fork (fromLists (take rowsA l)) (fromLists (drop rowsA l))
340342 fromLists _ = error "Wrong dimensions"
@@ -510,7 +512,7 @@ comp (Join a b) (Fork c d) = comp a c + comp b d -- Divide-and-conquer l
510512comp (Fork a b) c = Fork (comp a c) (comp b c) -- Fork fusion law
511513comp c (Join a b) = Join (comp c a) (comp c b) -- Join fusion law
512514{-# NOINLINE comp #-}
513−{-# RULES
515+{-# RULES
514516 "comp/iden1" forall m. comp m iden = m ;
515517 "comp/iden2" forall m. comp iden m = m
516518#-}
@@ -557,24 +559,38 @@ i2 = tr p2
557559
558560-- | Obtain the number of rows.
559561--
560−-- NOTE: The 'KnownNat' constaint is needed in order to obtain the
561−-- dimensions in constant time.
562−--
563−-- TODO: A 'rows' function that does not need the 'KnownNat' constraint in
564−-- exchange for performance.
562+-- NOTE: The 'KnownNat' constraint is needed in order to obtain the
563+-- dimensions in constant time. For a version that doesn't require the
564+-- constraint see 'rows''.
565565rows :: forall e cols rows. (Countable rows) => Matrix e cols rows -> Int
566566rows _ = fromInteger $ natVal (Proxy :: Proxy (Count rows))
567567
568+-- | Obtain the number of rows in an inefficient manner, but without any
569+-- constraints.
570+--
571+-- For a more efficient version see 'rows'.
572+rows' :: Matrix e cols rows -> Int
573+rows' (One _) = 1
574+rows' (Join lhs _) = rows' lhs
575+rows' (Fork top bottom) = rows' top + rows' bottom
576+
568577-- | Obtain the number of columns.
569−--
570−-- NOTE: The 'KnownNat' constaint is needed in order to obtain the
571−-- dimensions in constant time.
572578--
573−-- TODO: A 'columns' function that does not need the 'KnownNat' constraint in
574−-- exchange for performance.
579+-- NOTE: The 'KnownNat' constraint is needed in order to obtain the
580+-- dimensions in constant time. For a version that doesn't require the
581+-- constraint see 'columns''.
575582columns :: forall e cols rows. (Countable cols) => Matrix e cols rows -> Int
576583columns _ = fromInteger $ natVal (Proxy :: Proxy (Count cols))
577584
585+-- | Obtain the number of columns in an inefficient manner, but without any
586+-- constraints.
587+--
588+-- For a more efficient version see 'columns'.
589+columns' :: Matrix e cols rows -> Int
590+columns' (One _) = 1
591+columns' (Join lhs rhs) = columns' lhs + columns' rhs
592+columns' (Fork top _) = columns' top
593+
578594-- Coproduct Bifunctor
579595
580596infixl 5 -|-
@@ -629,7 +645,7 @@ sndM = matrixBuilder' f
629645-- | Khatri Rao Matrix product also known as matrix pairing.
630646--
631647-- NOTE: That this is not a true categorical product, see for instance:
632−--
648+--
633649-- @
634650-- | fstM . kr a b == a
635651-- kr a b ==> |
@@ -665,7 +681,7 @@ infixl 4 ><
665681 FL (Normalize (m, n)) n,
666682 FL (Normalize (p, q)) p,
667683 FL (Normalize (p, q)) q
668− )
684+ )
669685 => Matrix e m p -> Matrix e n q -> Matrix e (Normalize (m, n)) (Normalize (p, q))
670686(><) a b =
671687 let fstM' = fstM @e @m @n
@@ -988,7 +1004,7 @@ toRel ::
9881004 Eq b,
9891005 CountableDimsN a b,
9901006 FLN b a
991− )
1007+ )
9921008 => (a -> b -> Bool) -> Relation (Normalize a) (Normalize b)
9931009toRel f =
9941010 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