on master help sign in
Added bimap. Fixed instances
commit7f7796e5c4d8d054b33474f2a6f92cc01318121a authorArmando Santos <armandoifsantos@gmail.com> dateFri 14 Feb 2020, 21:51 (7 years ago) parent74e48a5
4 files changed+27−28
app/Spreadsheets.hs+3−6 package.yaml+2−2 src/LAoP/Matrix/Internal.hs+10−20 src/LAoP/Matrix/Type.hs+12
app/Spreadsheets.hs+3−6
@@ -2,9 +2,8 @@
22{-# LANGUAGE GADTs #-}
33{-# LANGUAGE FlexibleContexts #-}
44{-# LANGUAGE DeriveGeneric #-}
5+
56module Spreadsheets
6− (
7− )
87 where
98
109import LAoP.Matrix.Type
@@ -17,7 +16,7 @@ data Student = Student1 | Student2 | Student3 | Student4
1716data Question = Question1 | Question2 | Question3 | Question4
1817 deriving (Eq, Show, Enum, Bounded, Generic)
1918
20−data Results = Test | Exam | Final
19+data Results = Exam | Test | Final
2120 deriving (Eq, Show, Enum, Bounded, Generic)
2221
2322test :: Matrix Float One Results
@@ -44,9 +43,7 @@ xls w m t = junc (split w m) (split zeros r)
4443 rExam = m `comp` tr w
4544 rTest = tr t
4645 rFinal = rTest `maxPP` rExam
47− rAux = junc rTest (junc rExam rFinal)
48− r = tr (converter `comp` tr rAux)
49− converter = junc test (junc exam final)
46+ r = (rExam `comp` tr exam) + (rTest `comp` tr test) + (rFinal `comp` tr final)
5047
5148-- | Overloaded, point-wise 'max' function
5249maxPP_ :: Ord e => I.Matrix e a b -> I.Matrix e a b -> I.Matrix e a b
package.yaml+2−2
@@ -1,5 +1,5 @@
11name: laop
2−version: 0.1.0.3
2+version: 0.1.0.5
33github: "bolt12/laop"
44license: BSD3
55author: "Armando Santos"
@@ -38,7 +38,7 @@ executables:
3838 - -fexcess-precision # Good for doubles
3939 - -optc-O3 # Good for doubles
4040 - -optc-ffast-math # Good for doubles
41− - -O # Optimize
41+ - -O2
4242 dependencies:
4343 - laop
4444 - criterion
src/LAoP/Matrix/Internal.hs+10−20
@@ -239,10 +239,8 @@ instance Eq e => Eq (Matrix e cols rows) where
239239 (One a) == (One b) = a == b
240240 (Junc a b) == (Junc c d) = a == c && b == d
241241 (Split a b) == (Split c d) = a == c && b == d
242− (Split (Junc a b) (Junc c d)) == (Junc (Split a' c') (Split b' d')) =
243− (a == a') && (b == b') && (c == c') && (d == d')
244− (Junc (Split a' c') (Split b' d')) == (Split (Junc a b) (Junc c d)) =
245− (a == a') && (b == b') && (c == c') && (d == d')
242+ x@(Split _ _) == y@(Junc _ _) = x == abideJS y
243+ x@(Junc _ _) == y@(Split _ _) = abideJS x == y
246244
247245instance Num e => Num (Matrix e cols rows) where
248246
@@ -250,28 +248,22 @@ instance Num e => Num (Matrix e cols rows) where
250248 (One a) + (One b) = One (a + b)
251249 (Junc a b) + (Junc c d) = Junc (a + c) (b + d)
252250 (Split a b) + (Split c d) = Split (a + c) (b + d)
253− (Split (Junc a b) (Junc c d)) + (Junc (Split a' c') (Split b' d')) =
254− Split (Junc (a + a') (b + b')) (Junc (c + c') (d + d'))
255− (Junc (Split a' c') (Split b' d')) + (Split (Junc a b) (Junc c d)) =
256− Split (Junc (a + a') (b + b')) (Junc (c + c') (d + d'))
251+ x@(Split _ _) + y@(Junc _ _) = x + abideJS y
252+ x@(Junc _ _) + y@(Split _ _) = abideJS x + y
257253
258254 Empty - Empty = Empty
259255 (One a) - (One b) = One (a - b)
260256 (Junc a b) - (Junc c d) = Junc (a - c) (b - d)
261257 (Split a b) - (Split c d) = Split (a - c) (b - d)
262− (Split (Junc a b) (Junc c d)) - (Junc (Split a' c') (Split b' d')) =
263− Split (Junc (a - a') (b - b')) (Junc (c - c') (d - d'))
264− (Junc (Split a' c') (Split b' d')) - (Split (Junc a b) (Junc c d)) =
265− Split (Junc (a - a') (b - b')) (Junc (c - c') (d - d'))
258+ x@(Split _ _) - y@(Junc _ _) = x - abideJS y
259+ x@(Junc _ _) - y@(Split _ _) = abideJS x - y
266260
267261 Empty * Empty = Empty
268262 (One a) * (One b) = One (a * b)
269263 (Junc a b) * (Junc c d) = Junc (a * c) (b * d)
270264 (Split a b) * (Split c d) = Split (a * c) (b * d)
271− (Split (Junc a b) (Junc c d)) * (Junc (Split a' c') (Split b' d')) =
272− Split (Junc (a * a') (b * b')) (Junc (c * c') (d * d'))
273− (Junc (Split a' c') (Split b' d')) * (Split (Junc a b) (Junc c d)) =
274− Split (Junc (a * a') (b * b')) (Junc (c * c') (d * d'))
265+ x@(Split _ _) * y@(Junc _ _) = x * abideJS y
266+ x@(Junc _ _) * y@(Split _ _) = abideJS x * y
275267
276268 abs Empty = Empty
277269 abs (One a) = One (abs a)
@@ -288,10 +280,8 @@ instance Ord e => Ord (Matrix e cols rows) where
288280 (One a) <= (One b) = a <= b
289281 (Junc a b) <= (Junc c d) = (a <= c) && (b <= d)
290282 (Split a b) <= (Split c d) = (a <= c) && (b <= d)
291− (Split (Junc a b) (Junc c d)) <= (Junc (Split a' c') (Split b' d')) =
292− (a <= a') && (b <= b') && (c <= c') && (d <= d')
293− (Junc (Split a' c') (Split b' d')) <= (Split (Junc a b) (Junc c d)) =
294− (a <= a') && (b <= b') && (c <= c') && (d <= d')
283+ x@(Split _ _) <= y@(Junc _ _) = x <= abideJS y
284+ x@(Junc _ _) <= y@(Split _ _) = abideJS x <= y
295285
296286-- Primitives
297287
src/LAoP/Matrix/Type.hs+12
@@ -97,6 +97,7 @@ module LAoP.Matrix.Type
9797
9898 -- * Functor instance equivalent function
9999 fmapM,
100+ bimapM,
100101
101102 -- * Applicative/Monoidal instance equivalent functions
102103 unitM,
@@ -203,6 +204,17 @@ instance (Num e) => C.Category (Matrix e) where
203204 id = undefined
204205 (.) = comp
205206
207+-- | Bifunctor equivalent function
208+bimapM ::
209+ ( Liftable e a b,
210+ Liftable e c d,
211+ CountableDimensionsN a c,
212+ CountableDimensionsN b d,
213+ FromListsN e d c,
214+ FromListsN e b a
215+ ) => (a -> b) -> (c -> d) -> Matrix e a c -> Matrix e b d
216+bimapM f g m = fromF' g `comp` m `comp` tr (fromF' f)
217+
206218-- | Zero type alias
207219type Zero = Void
208220