@@ -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)