@@ -66,6 +66,7 @@ module LAoP.Matrix.Internal
68 matrixBuilder,
68 matrixBuilder,
@@ -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"
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
370+-- | Matrix builder function. Constructs a matrix provided with
371+-- a construction function that operates with arbitrary types.
380+ CountableDimensionsN a b
381+ ) => ((a, b) -> e) -> Matrix e (Normalize a) (Normalize b)
383+ let positions = [(a, b) | a <- [minBound .. maxBound], b <- [minBound .. maxBound]]
384+ in fromLists . map (map f) . groupBy (\(x, _) (w, _) -> x == w) $ positions
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
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)
456-- Ones Matrix
473-- Ones Matrix
@@ -459,14 +476,14 @@ zeros = matrixBuilder (const 0)
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)
464-- Const Matrix
481-- Const Matrix
466-- | The constant matrix constructor. A matrix wholly filled with a given
483-- | The constant matrix constructor. A matrix wholly filled with a given
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)
471-- Bang Matrix
488-- Bang Matrix
@@ -480,7 +497,7 @@ bang =
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 #-}
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
597 offset = fromInteger (natVal (Proxy :: Proxy (Count k)))
614 offset = fromInteger (natVal (Proxy :: Proxy (Count k)))
@@ -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
612 offset = fromInteger (natVal (Proxy :: Proxy (Count k)))
629 offset = fromInteger (natVal (Proxy :: Proxy (Count k)))