on master help sign in
Added matrixBuilder that constructs matrices with a function that operates with arbitrary data types
77aae98Armando Santos
src/LAoP/Matrix/Internal.hs+26−9
@@ -66,6 +66,7 @@ module LAoP.Matrix.Internal
66 toLists,
66 toLists,
67 toList,
67 toList,
68 matrixBuilder,
68 matrixBuilder,
69+ matrixBuilder',
69 row,
70 row,
70 col,
71 col,
71 zeros,
72 zeros,
@@ -354,18 +355,34 @@ instance {-# OVERLAPPABLE #-} (FromLists e (Either a b) c, FromLists e (Either a
354 else error "Not all rows have the same length"
355 else error "Not all rows have the same length"
355
356
356-- | Matrix builder function. Constructs a matrix provided with
357-- | Matrix builder function. Constructs a matrix provided with
357−-- a construction function.
358+-- a construction function that operates with indices.
358−matrixBuilder ::
359+matrixBuilder' ::
359 forall e cols rows.
360 forall e cols rows.
360 ( FromLists e cols rows,
361 ( FromLists e cols rows,
361 CountableDimensions cols rows
362 CountableDimensions cols rows
362 ) => ((Int, Int) -> e) -> Matrix e cols rows
363 ) => ((Int, Int) -> e) -> Matrix e cols rows
363−matrixBuilder f =
364+matrixBuilder' f =
364 let c = fromInteger $ natVal (Proxy :: Proxy (Count cols))
365 let c = fromInteger $ natVal (Proxy :: Proxy (Count cols))
365 r = fromInteger $ natVal (Proxy :: Proxy (Count rows))
366 r = fromInteger $ natVal (Proxy :: Proxy (Count rows))
366 positions = [(a, b) | a <- [0 .. (r - 1)], b <- [0 .. (c - 1)]]
367 positions = [(a, b) | a <- [0 .. (r - 1)], b <- [0 .. (c - 1)]]
367 in fromLists . map (map f) . groupBy (\(x, _) (w, _) -> x == w) $ positions
368 in fromLists . map (map f) . groupBy (\(x, _) (w, _) -> x == w) $ positions
368
369
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+
369-- | Constructs a column vector matrix
386-- | Constructs a column vector matrix
370col :: (FromLists e () rows) => [e] -> Matrix e () rows
387col :: (FromLists e () rows) => [e] -> Matrix e () rows
371col = fromLists . map (: [])
388col = fromLists . map (: [])
@@ -451,7 +468,7 @@ toList = concat . toLists
451
468
452-- | The zero matrix. A matrix wholly filled with zeros.
469-- | The zero matrix. A matrix wholly filled with zeros.
453zeros :: (Num e, FromLists e cols rows, CountableDimensions cols rows) => Matrix e cols rows
470zeros :: (Num e, FromLists e cols rows, CountableDimensions cols rows) => Matrix e cols rows
454−zeros = matrixBuilder (const 0)
471+zeros = matrixBuilder' (const 0)
455
472
456-- Ones Matrix
473-- Ones Matrix
457
474
@@ -459,14 +476,14 @@ zeros = matrixBuilder (const 0)
459--
476--
460-- Also known as T (Top) matrix.
477-- Also known as T (Top) matrix.
461ones :: (Num e, FromLists e cols rows, CountableDimensions cols rows) => Matrix e cols rows
478ones :: (Num e, FromLists e cols rows, CountableDimensions cols rows) => Matrix e cols rows
462−ones = matrixBuilder (const 1)
479+ones = matrixBuilder' (const 1)
463
480
464-- Const Matrix
481-- Const Matrix
465
482
466-- | The constant matrix constructor. A matrix wholly filled with a given
483-- | The constant matrix constructor. A matrix wholly filled with a given
467-- value.
484-- value.
468constant :: (Num e, FromLists e cols rows, CountableDimensions cols rows) => e -> Matrix e cols rows
485constant :: (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)
470
487
471-- Bang Matrix
488-- Bang Matrix
472
489
@@ -480,7 +497,7 @@ bang =
480
497
481-- | iden matrix.
498-- | iden matrix.
482iden :: (Num e, FromLists e cols cols, Countable cols) => Matrix e cols cols
499iden :: (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 (==))
484{-# NOINLINE iden #-}
501{-# NOINLINE iden #-}
485
502
486-- Matrix composition (MMM)
503-- Matrix composition (MMM)
@@ -592,7 +609,7 @@ fstM ::
592 FromLists e (Normalize (m, k)) m,
609 FromLists e (Normalize (m, k)) m,
593 CountableN (m, k)
610 CountableN (m, k)
594 ) => Matrix e (Normalize (m, k)) m
611 ) => Matrix e (Normalize (m, k)) m
595−fstM = matrixBuilder f
612+fstM = matrixBuilder' f
596 where
613 where
597 offset = fromInteger (natVal (Proxy :: Proxy (Count k)))
614 offset = fromInteger (natVal (Proxy :: Proxy (Count k)))
598 f (x, y)
615 f (x, y)
@@ -607,7 +624,7 @@ sndM ::
607 FromLists e (Normalize (m, k)) k,
624 FromLists e (Normalize (m, k)) k,
608 CountableN (m, k)
625 CountableN (m, k)
609 ) => Matrix e (Normalize (m, k)) k
626 ) => Matrix e (Normalize (m, k)) k
610−sndM = matrixBuilder f
627+sndM = matrixBuilder' f
611 where
628 where
612 offset = fromInteger (natVal (Proxy :: Proxy (Count k)))
629 offset = fromInteger (natVal (Proxy :: Proxy (Count k)))
613 f (x, y)
630 f (x, y)
src/LAoP/Matrix/Nat.hs+5−3
@@ -82,7 +82,7 @@ module LAoP.Matrix.Nat
82 fromLists,
82 fromLists,
83 toLists,
83 toLists,
84 toList,
84 toList,
85− matrixBuilder,
85+ matrixBuilder',
86 row,
86 row,
87 col,
87 col,
88 zeros,
88 zeros,
@@ -231,10 +231,12 @@ infixl 2 ===
231fromLists :: (FromListsN e cols rows) => [[e]] -> Matrix e cols rows
231fromLists :: (FromListsN e cols rows) => [[e]] -> Matrix e cols rows
232fromLists = M . I.fromLists
232fromLists = M . I.fromLists
233
233
234+-- | Matrix builder function. Constructs a matrix provided with
235+-- a construction function that operates with indices.
234−matrixBuilder ::
236+matrixBuilder' ::
235 (FromListsN e cols rows, CountableN cols, CountableN rows)
237 (FromListsN e cols rows, CountableN cols, CountableN rows)
236 => ((Int, Int) -> e) -> Matrix e cols rows
238 => ((Int, Int) -> e) -> Matrix e cols rows
237−matrixBuilder = M . I.matrixBuilder
239+matrixBuilder' = M . I.matrixBuilder'
238
240
239col :: (I.FromLists e () (I.FromNat rows)) => [e] -> Matrix e 1 rows
241col :: (I.FromLists e () (I.FromNat rows)) => [e] -> Matrix e 1 rows
240col = M . I.col
242col = M . I.col
src/LAoP/Matrix/Type.hs+17−3
@@ -86,6 +86,7 @@ module LAoP.Matrix.Type
86 fromLists,
86 fromLists,
87 toLists,
87 toLists,
88 toList,
88 toList,
89+ matrixBuilder',
89 matrixBuilder,
90 matrixBuilder,
90 row,
91 row,
91 col,
92 col,
@@ -322,11 +323,24 @@ fromLists :: (FromListsN e cols rows) => [[e]] -> Matrix e cols rows
322fromLists = M . I.fromLists
323fromLists = M . I.fromLists
323
324
324-- | Matrix builder function. Constructs a matrix provided with
325-- | Matrix builder function. Constructs a matrix provided with
325−-- a construction function.
326+-- a construction function that operates with indices.
326−matrixBuilder ::
327+matrixBuilder' ::
327 (FromListsN e cols rows, CountableDimensionsN cols rows )
328 (FromListsN e cols rows, CountableDimensionsN cols rows )
328 => ((Int, Int) -> e) -> Matrix e cols rows
329 => ((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)
330
344
331-- | Constructs a column vector matrix
345-- | Constructs a column vector matrix
332col :: (FromListsN e () rows) => [e] -> Matrix e One rows
346col :: (FromListsN e () rows) => [e] -> Matrix e One rows
src/LAoP/Relation/Internal.hs+20−6
@@ -74,6 +74,7 @@ module LAoP.Relation.Internal
74 toBool,
74 toBool,
75 pt,
75 pt,
76 belongs,
76 belongs,
77+ relationBuilder',
77 relationBuilder,
78 relationBuilder,
78 zeros,
79 zeros,
79 ones,
80 ones,
@@ -272,11 +273,24 @@ infixl 2 ===
272fromLists :: (FromListsN a b) => [[Boolean]] -> Relation a b
273fromLists :: (FromListsN a b) => [[Boolean]] -> Relation a b
273fromLists = R . I.fromLists
274fromLists = R . I.fromLists
274
275
275−-- | Matrix builder function. Constructs a matrix provided with
276+-- | Relation builder function. Constructs a relation provided with
276−-- a construction function.
277+-- a construction function that operates with indices.
277−relationBuilder ::
278+relationBuilder' ::
278 (FromListsN a b, CountableDimensionsN a b) =>
279 (FromListsN a b, CountableDimensionsN a b) =>
279 ((Int, Int) -> Boolean) -> Relation a b
280 ((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
280relationBuilder = R . I.matrixBuilder
294relationBuilder = R . I.matrixBuilder
281
295
282-- | Lifts functions to matrices with arbitrary dimensions.
296-- | Lifts functions to matrices with arbitrary dimensions.
@@ -379,7 +393,7 @@ belongs = toRel elemR
379zeros ::
393zeros ::
380 (FromListsN a b, CountableDimensionsN a b) =>
394 (FromListsN a b, CountableDimensionsN a b) =>
381 Relation a b
395 Relation a b
382−zeros = relationBuilder (const (nat 0))
396+zeros = relationBuilder' (const (nat 0))
383
397
384-- Ones Matrix
398-- Ones Matrix
385
399
@@ -394,7 +408,7 @@ zeros = relationBuilder (const (nat 0))
394ones ::
408ones ::
395 (FromListsN a b, CountableDimensionsN a b) =>
409 (FromListsN a b, CountableDimensionsN a b) =>
396 Relation a b
410 Relation a b
397−ones = relationBuilder (const (nat 1))
411+ones = relationBuilder' (const (nat 1))
398
412
399-- Bang Matrix
413-- Bang Matrix
400
414
@@ -423,7 +437,7 @@ point = fromF . const
423-- @
437-- @
424iden ::
438iden ::
425 (FromListsN a a, CountableN a) => Relation a a
439 (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 (==))
427
441
428-- | Relational composition
442-- | Relational composition
429--
443--