on master help sign in
Optimize Select
commiteb22ee608fc347624c950945d6ecddfa8a627828 authorArmando Santos <armandoifsantos@gmail.com> dateFri 7 Feb 2020, 11:09 (7 years ago) parentab2a4da
3 files changed+39−5
src/LAoP/Dist.hs+1 src/LAoP/Dist/Internal.hs+9−2 src/LAoP/Matrix/Internal.hs+29−3
src/LAoP/Dist.hs+1
@@ -54,6 +54,7 @@ module LAoP.Dist
5454
5555 -- * Selective equivalent functions
5656 selectD,
57+ selectD2,
5758
5859 -- * Monad equivalent functions
5960 returnD,
src/LAoP/Dist/Internal.hs+9−2
@@ -25,6 +25,7 @@ module LAoP.Dist.Internal
2525 unitD,
2626 multD,
2727 selectD,
28+ selectD2,
2829 returnD,
2930 bindD,
3031 (??),
@@ -49,7 +50,7 @@ import Data.Proxy
4950import Data.List (sortBy)
5051
5152-- | Type synonym for probability value
52−type Prob = Double
53+type Prob = Float
5354
5455-- | Type synonym for column vector matrices. This represents a probability
5556-- distribution.
@@ -94,8 +95,14 @@ selectD ::
9495 FromListsN b b,
9596 CountableN b
9697 ) => Dist (Either a b) -> Matrix Prob a b -> Dist b
97−selectD (D d) m = D (junc m identity `comp` d)
98+selectD (D d) m = D (selectM d m)
9899
100+selectD2 ::
101+ ( TrivialE a b,
102+ FromListsN b b,
103+ CountableN b
104+ ) => Dist (Either a b) -> Matrix Prob a b -> Dist b
105+selectD2 (D d) m = D (junc m identity `comp` d)
99106
100107-- | Monad instance 'return' function
101108returnD :: forall a . (Enum a, FromListsN () a, Countable a) => a -> Dist a
src/LAoP/Matrix/Internal.hs+29−3
@@ -81,7 +81,8 @@ module LAoP.Matrix.Internal
8181 tr,
8282
8383 -- ** Selective operator
84− select,
84+ select,
85+ branch,
8586
8687 -- ** McCarthy's Conditional
8788 cond,
@@ -313,7 +314,7 @@ infixl 2 ===
313314-- | Type class for defining the 'fromList' conversion function.
314315--
315316-- Given that it is not possible to branch on types at the term level type
316−-- classes are needed bery much like an inductive definition but on types.
317+-- classes are needed very much like an inductive definition but on types.
317318class FromLists e cols rows where
318319 -- | Build a matrix out of a list of list of elements. Throws a runtime
319320 -- error if the dimensions do not match.
@@ -689,7 +690,32 @@ tr (Split a b) = Junc (tr a) (tr b)
689690-- | Selective functors 'select' operator equivalent inspired by the
690691-- ArrowMonad solution presented in the paper.
691692select :: (Num e, FromLists e b b, Countable b) => Matrix e cols (Either a b) -> Matrix e a b -> Matrix e cols b
692−select m y = junc y identity . m
693+select (Split a b) y = y . a + b -- Divide-and-conquer law
694+select (Junc (Split a c) (Split b d)) y = junc (y . a + c) (y . b + d) -- Pattern matching + DnC law
695+select m y = junc y identity . m
696+
697+branch ::
698+ ( Num e,
699+ CountableDimensions a b,
700+ CountableDimensions c (Either b c),
701+ FromLists e c b,
702+ FromLists e a b,
703+ FromLists e a a,
704+ FromLists e b b,
705+ FromLists e c c,
706+ FromLists e b a,
707+ FromLists e b c,
708+ FromLists e (Either b c) b,
709+ FromLists e (Either b c) c
710+ )
711+ => Matrix e cols (Either a b) -> Matrix e a c -> Matrix e b c -> Matrix e cols c
712+branch x l r = f x `select` g l `select` r
713+ where
714+ f :: (Num e, Countable a, CountableDimensions b c, FromLists e a b, FromLists e c b, FromLists e b b, FromLists e b a, FromLists e a a)
715+ => Matrix e cols (Either a b) -> Matrix e cols (Either a (Either b c))
716+ f m = split (tr i1) (i1 . tr i2) . m
717+ g :: (Num e, CountableDimensions b c, FromLists e b c, FromLists e c c) => Matrix e a c -> Matrix e a (Either b c)
718+ g m = i2 . m
693719
694720-- McCarthy's Conditional
695721