on master help sign in
Added matrixBuilder that constructs matrices with a function that operates with arbitrary data types
commit77aae984a8525cc3cccf6c3e4de40187716c8546 authorArmando Santos <armandoifsantos@gmail.com> dateMon 24 Feb 2020, 19:23 (7 years ago) parente4dbb8f
4 files changed+68−21
src/LAoP/Matrix/Internal.hs+26−9 src/LAoP/Matrix/Nat.hs+5−3 src/LAoP/Matrix/Type.hs+17−3 src/LAoP/Relation/Internal.hs+20−6
src/LAoP/Matrix/Internal.hs+26−9
@@ -66,6 +66,7 @@ module LAoP.Matrix.Internal
6666 toLists,
6767 toList,
6868 matrixBuilder,
69+ matrixBuilder',
6970 row,
7071 col,
7172 zeros,
@@ -354,18 +355,34 @@ instance {-# OVERLAPPABLE #-} (FromLists e (Either a b) c, FromLists e (Either a
354355 else error "Not all rows have the same length"
355356
356357-- | Matrix builder function. Constructs a matrix provided with
357−-- a construction function.
358−matrixBuilder ::
358+-- a construction function that operates with indices.
359+matrixBuilder' ::
359360 forall e cols rows.
360361 ( FromLists e cols rows,
361362 CountableDimensions cols rows
362363 ) => ((Int, Int) -> e) -> Matrix e cols rows
363−matrixBuilder f =
364+matrixBuilder' f =
364365 let c = fromInteger $ natVal (Proxy :: Proxy (Count cols))
365366 r = fromInteger $ natVal (Proxy :: Proxy (Count rows))
366367 positions = [(a, b) | a <- [0 .. (r - 1)], b <- [0 .. (c - 1)]]
367368 in fromLists . map (map f) . groupBy (\(x, _) (w, _) -> x == w) $ positions
368369
370+-- | Matrix builder function. Constructs a matrix provided with
371+-- a construction function that operates with arbitrary types.
372+matrixBuilder ::
373+ forall e a b.
374+ ( FromListsN e a b,
375+ Enum a,
376+ Enum b,
377+ Bounded a,
378+ Bounded b,
379+ Eq a,
380+ CountableDimensionsN a b
381+ ) => ((a, b) -> e) -> Matrix e (Normalize a) (Normalize b)
382+matrixBuilder f =
383+ let positions = [(a, b) | a <- [minBound .. maxBound], b <- [minBound .. maxBound]]
384+ in fromLists . map (map f) . groupBy (\(x, _) (w, _) -> x == w) $ positions
385+
369386-- | Constructs a column vector matrix
370387col :: (FromLists e () rows) => [e] -> Matrix e () rows
371388col = fromLists . map (: [])
@@ -451,7 +468,7 @@ toList = concat . toLists
451468
452469-- | The zero matrix. A matrix wholly filled with zeros.
453470zeros :: (Num e, FromLists e cols rows, CountableDimensions cols rows) => Matrix e cols rows
454−zeros = matrixBuilder (const 0)
471+zeros = matrixBuilder' (const 0)
455472
456473-- Ones Matrix
457474
@@ -459,14 +476,14 @@ zeros = matrixBuilder (const 0)
459476--
460477-- Also known as T (Top) matrix.
461478ones :: (Num e, FromLists e cols rows, CountableDimensions cols rows) => Matrix e cols rows
462−ones = matrixBuilder (const 1)
479+ones = matrixBuilder' (const 1)
463480
464481-- Const Matrix
465482
466483-- | The constant matrix constructor. A matrix wholly filled with a given
467484-- value.
468485constant :: (Num e, FromLists e cols rows, CountableDimensions cols rows) => e -> Matrix e cols rows
469−constant e = matrixBuilder (const e)
486+constant e = matrixBuilder' (const e)
470487
471488-- Bang Matrix
472489
@@ -480,7 +497,7 @@ bang =
480497
481498-- | iden matrix.
482499iden :: (Num e, FromLists e cols cols, Countable cols) => Matrix e cols cols
483−iden = matrixBuilder (bool 0 1 . uncurry (==))
500+iden = matrixBuilder' (bool 0 1 . uncurry (==))
484501{-# NOINLINE iden #-}
485502
486503-- Matrix composition (MMM)
@@ -592,7 +609,7 @@ fstM ::
592609 FromLists e (Normalize (m, k)) m,
593610 CountableN (m, k)
594611 ) => Matrix e (Normalize (m, k)) m
595−fstM = matrixBuilder f
612+fstM = matrixBuilder' f
596613 where
597614 offset = fromInteger (natVal (Proxy :: Proxy (Count k)))
598615 f (x, y)
@@ -607,7 +624,7 @@ sndM ::
607624 FromLists e (Normalize (m, k)) k,
608625 CountableN (m, k)
609626 ) => Matrix e (Normalize (m, k)) k
610−sndM = matrixBuilder f
627+sndM = matrixBuilder' f
611628 where
612629 offset = fromInteger (natVal (Proxy :: Proxy (Count k)))
613630 f (x, y)
src/LAoP/Matrix/Nat.hs+5−3
@@ -82,7 +82,7 @@ module LAoP.Matrix.Nat
8282 fromLists,
8383 toLists,
8484 toList,
85− matrixBuilder,
85+ matrixBuilder',
8686 row,
8787 col,
8888 zeros,
@@ -231,10 +231,12 @@ infixl 2 ===
231231fromLists :: (FromListsN e cols rows) => [[e]] -> Matrix e cols rows
232232fromLists = M . I.fromLists
233233
234−matrixBuilder ::
234+-- | Matrix builder function. Constructs a matrix provided with
235+-- a construction function that operates with indices.
236+matrixBuilder' ::
235237 (FromListsN e cols rows, CountableN cols, CountableN rows)
236238 => ((Int, Int) -> e) -> Matrix e cols rows
237−matrixBuilder = M . I.matrixBuilder
239+matrixBuilder' = M . I.matrixBuilder'
238240
239241col :: (I.FromLists e () (I.FromNat rows)) => [e] -> Matrix e 1 rows
240242col = M . I.col
src/LAoP/Matrix/Type.hs+17−3
@@ -86,6 +86,7 @@ module LAoP.Matrix.Type
8686 fromLists,
8787 toLists,
8888 toList,
89+ matrixBuilder',
8990 matrixBuilder,
9091 row,
9192 col,
@@ -322,11 +323,24 @@ fromLists :: (FromListsN e cols rows) => [[e]] -> Matrix e cols rows
322323fromLists = M . I.fromLists
323324
324325-- | Matrix builder function. Constructs a matrix provided with
325−-- a construction function.
326−matrixBuilder ::
326+-- a construction function that operates with indices.
327+matrixBuilder' ::
327328 (FromListsN e cols rows, CountableDimensionsN cols rows )
328329 => ((Int, Int) -> e) -> Matrix e cols rows
329−matrixBuilder = M . I.matrixBuilder
330+matrixBuilder' = M . I.matrixBuilder'
331+
332+-- | Matrix builder function. Constructs a matrix provided with
333+-- a construction function that operates with arbitrary types.
334+matrixBuilder ::
335+ ( FromListsN e a b,
336+ Enum a,
337+ Enum b,
338+ Bounded a,
339+ Bounded b,
340+ Eq a,
341+ CountableDimensionsN a b
342+ ) => ((a, b) -> e) -> Matrix e a b
343+matrixBuilder f = M (I.matrixBuilder f)
330344
331345-- | Constructs a column vector matrix
332346col :: (FromListsN e () rows) => [e] -> Matrix e One rows
src/LAoP/Relation/Internal.hs+20−6
@@ -74,6 +74,7 @@ module LAoP.Relation.Internal
7474 toBool,
7575 pt,
7676 belongs,
77+ relationBuilder',
7778 relationBuilder,
7879 zeros,
7980 ones,
@@ -272,11 +273,24 @@ infixl 2 ===
272273fromLists :: (FromListsN a b) => [[Boolean]] -> Relation a b
273274fromLists = R . I.fromLists
274275
275−-- | Matrix builder function. Constructs a matrix provided with
276−-- a construction function.
277−relationBuilder ::
276+-- | Relation builder function. Constructs a relation provided with
277+-- a construction function that operates with indices.
278+relationBuilder' ::
278279 (FromListsN a b, CountableDimensionsN a b) =>
279280 ((Int, Int) -> Boolean) -> Relation a b
281+relationBuilder' = R . I.matrixBuilder'
282+
283+-- | Relation builder function. Constructs a relation provided with
284+-- a construction function that operates with arbitrary types.
285+relationBuilder ::
286+ ( FromListsN a b,
287+ Enum a,
288+ Enum b,
289+ Bounded a,
290+ Bounded b,
291+ Eq a,
292+ CountableDimensionsN a b
293+ ) => ((a, b) -> Boolean) -> Relation a b
280294relationBuilder = R . I.matrixBuilder
281295
282296-- | Lifts functions to matrices with arbitrary dimensions.
@@ -379,7 +393,7 @@ belongs = toRel elemR
379393zeros ::
380394 (FromListsN a b, CountableDimensionsN a b) =>
381395 Relation a b
382−zeros = relationBuilder (const (nat 0))
396+zeros = relationBuilder' (const (nat 0))
383397
384398-- Ones Matrix
385399
@@ -394,7 +408,7 @@ zeros = relationBuilder (const (nat 0))
394408ones ::
395409 (FromListsN a b, CountableDimensionsN a b) =>
396410 Relation a b
397−ones = relationBuilder (const (nat 1))
411+ones = relationBuilder' (const (nat 1))
398412
399413-- Bang Matrix
400414
@@ -423,7 +437,7 @@ point = fromF . const
423437-- @
424438iden ::
425439 (FromListsN a a, CountableN a) => Relation a a
426−iden = relationBuilder (bool (nat 0) (nat 1) . uncurry (==))
440+iden = relationBuilder' (bool (nat 0) (nat 1) . uncurry (==))
427441
428442-- | Relational composition
429443--