{-# LANGUAGE StrictData #-}
{-# LANGUAGE TypeFamilyDependencies #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE NoStarIsType #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
module LAoP.Matrix.Internal
(
Matrix (..),
Countable,
CountableDimensions,
CountableN,
CountableDimensionsN,
FromListsN,
Liftable,
Trivial,
empty,
one,
join,
fork,
FromNat,
Count,
Normalize,
FromLists,
fromLists,
toLists,
toList,
matrixBuilder,
matrixBuilder',
row,
col,
zeros,
ones,
bang,
constant,
columns,
rows,
tr,
(.|),
(./),
select,
branch,
cond,
abideJF,
abideFJ,
zipWithM,
(===),
p1,
p2,
(|||),
i1,
i2,
(-|-),
(><),
fstM,
sndM,
kr,
iden,
comp,
fromF',
fromF,
pretty,
prettyPrint,
toBool,
fromBool,
compRel,
divR,
divL,
divS,
fromFRel,
fromFRel',
toRel,
negateM,
orM,
andM,
subM
)
where
import LAoP.Utils.Internal
import Data.Bool
import Data.Kind
import Data.List
import Data.Maybe
import Data.Proxy
import Data.Void
import GHC.TypeLits
import Data.Type.Equality
import GHC.Generics
import Control.DeepSeq
import Prelude hiding (id, (.))
import qualified Prelude (id, (.))
data Matrix e cols rows where
Empty :: Matrix e Void Void
One :: e -> Matrix e () ()
Join :: Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
Fork :: Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
deriving instance (Show e) => Show (Matrix e cols rows)
type family Count (d :: Type) :: Nat where
Count (Natural n m) = (m - n) + 1
Count (List a) = (^) 2 (Count a)
Count (Either a b) = (+) (Count a) (Count b)
Count (a, b) = (*) (Count a) (Count b)
Count (a -> b) = (^) (Count b) (Count a)
Count (M1 _ _ f p) = Count (f p)
Count (K1 _ _ _) = 1
Count (V1 _) = 0
Count (U1 _) = 1
Count ((:*:) a b p) = Count (a p) * Count (b p)
Count ((:+:) a b p) = Count (a p) + Count (b p)
Count d = Count (Rep d R)
type family FromNat (n :: Nat) :: Type where
FromNat 0 = Void
FromNat 1 = ()
FromNat n = FromNat' (Mod n 2 == 0) (FromNat (Div n 2))
type family FromNat' (b :: Bool) (m :: Type) :: Type where
FromNat' 'True m = Either m m
FromNat' 'False m = Either () (Either m m)
type family Normalize (d :: Type) :: Type where
Normalize (Either a b) = Either (Normalize a) (Normalize b)
Normalize d = FromNat (Count d)
type Countable a = KnownNat (Count a)
type CountableN a = KnownNat (Count (Normalize a))
type CountableDimensions a b = (Countable a, Countable b)
type CountableDimensionsN a b = (CountableN a, CountableN b)
type FromListsN e a b = FromLists e (Normalize a) (Normalize b)
type Liftable e a b = (Bounded a, Bounded b, Enum a, Enum b, Eq b, Num e, Ord e)
type Trivial a = FromNat (Count a) ~ a
instance (Num e) => Category (Matrix e) where
type Object (Matrix e) a = (FromLists e a a, Countable a)
id :: Matrix e a a
id = Matrix e a a
forall e cols.
(Num e, FromLists e cols cols, Countable cols) =>
Matrix e cols cols
iden
. :: Matrix e b c -> Matrix e a b -> Matrix e a c
(.) = Matrix e b c -> Matrix e a b -> Matrix e a c
forall e cr rows cols.
Num e =>
Matrix e cr rows -> Matrix e cols cr -> Matrix e cols rows
comp
instance NFData e => NFData (Matrix e cols rows) where
rnf :: Matrix e cols rows -> ()
rnf Empty = ()
rnf (One e :: e
e) = e -> ()
forall a. NFData a => a -> ()
rnf e
e
rnf (Join a :: Matrix e a rows
a b :: Matrix e b rows
b) = Matrix e a rows -> ()
forall a. NFData a => a -> ()
rnf Matrix e a rows
a () -> () -> ()
forall a b. a -> b -> b
`seq` Matrix e b rows -> ()
forall a. NFData a => a -> ()
rnf Matrix e b rows
b
rnf (Fork a :: Matrix e cols a
a b :: Matrix e cols b
b) = Matrix e cols a -> ()
forall a. NFData a => a -> ()
rnf Matrix e cols a
a () -> () -> ()
forall a b. a -> b -> b
`seq` Matrix e cols b -> ()
forall a. NFData a => a -> ()
rnf Matrix e cols b
b
instance Eq e => Eq (Matrix e cols rows) where
Empty == :: Matrix e cols rows -> Matrix e cols rows -> Bool
== Empty = Bool
True
(One a :: e
a) == (One b :: e
b) = e
a e -> e -> Bool
forall a. Eq a => a -> a -> Bool
== e
b
(Join a :: Matrix e a rows
a b :: Matrix e b rows
b) == (Join c :: Matrix e a rows
c d :: Matrix e b rows
d) = Matrix e a rows
a Matrix e a rows -> Matrix e a rows -> Bool
forall a. Eq a => a -> a -> Bool
== Matrix e a rows
Matrix e a rows
c Bool -> Bool -> Bool
&& Matrix e b rows
b Matrix e b rows -> Matrix e b rows -> Bool
forall a. Eq a => a -> a -> Bool
== Matrix e b rows
Matrix e b rows
d
(Fork a :: Matrix e cols a
a b :: Matrix e cols b
b) == (Fork c :: Matrix e cols a
c d :: Matrix e cols b
d) = Matrix e cols a
a Matrix e cols a -> Matrix e cols a -> Bool
forall a. Eq a => a -> a -> Bool
== Matrix e cols a
Matrix e cols a
c Bool -> Bool -> Bool
&& Matrix e cols b
b Matrix e cols b -> Matrix e cols b -> Bool
forall a. Eq a => a -> a -> Bool
== Matrix e cols b
Matrix e cols b
d
x :: Matrix e cols rows
x@(Fork _ _) == y :: Matrix e cols rows
y@(Join _ _) = Matrix e cols rows
x Matrix e cols rows -> Matrix e cols rows -> Bool
forall a. Eq a => a -> a -> Bool
== Matrix e cols rows -> Matrix e cols rows
forall e cols rows. Matrix e cols rows -> Matrix e cols rows
abideJF Matrix e cols rows
y
x :: Matrix e cols rows
x@(Join _ _) == y :: Matrix e cols rows
y@(Fork _ _) = Matrix e cols rows -> Matrix e cols rows
forall e cols rows. Matrix e cols rows -> Matrix e cols rows
abideJF Matrix e cols rows
x Matrix e cols rows -> Matrix e cols rows -> Bool
forall a. Eq a => a -> a -> Bool
== Matrix e cols rows
y
instance Num e => Num (Matrix e cols rows) where
a :: Matrix e cols rows
a + :: Matrix e cols rows -> Matrix e cols rows -> Matrix e cols rows
+ b :: Matrix e cols rows
b = (e -> e -> e)
-> Matrix e cols rows -> Matrix e cols rows -> Matrix e cols rows
forall e f g cols rows.
(e -> f -> g)
-> Matrix e cols rows -> Matrix f cols rows -> Matrix g cols rows
zipWithM e -> e -> e
forall a. Num a => a -> a -> a
(+) Matrix e cols rows
a Matrix e cols rows
b
a :: Matrix e cols rows
a - :: Matrix e cols rows -> Matrix e cols rows -> Matrix e cols rows
- b :: Matrix e cols rows
b = (e -> e -> e)
-> Matrix e cols rows -> Matrix e cols rows -> Matrix e cols rows
forall e f g cols rows.
(e -> f -> g)
-> Matrix e cols rows -> Matrix f cols rows -> Matrix g cols rows
zipWithM (-) Matrix e cols rows
a Matrix e cols rows
b
a :: Matrix e cols rows
a * :: Matrix e cols rows -> Matrix e cols rows -> Matrix e cols rows
* b :: Matrix e cols rows
b = (e -> e -> e)
-> Matrix e cols rows -> Matrix e cols rows -> Matrix e cols rows
forall e f g cols rows.
(e -> f -> g)
-> Matrix e cols rows -> Matrix f cols rows -> Matrix g cols rows
zipWithM e -> e -> e
forall a. Num a => a -> a -> a
(*) Matrix e cols rows
a Matrix e cols rows
b
abs :: Matrix e cols rows -> Matrix e cols rows
abs Empty = Matrix e cols rows
forall e. Matrix e Void Void
Empty
abs (One a :: e
a) = e -> Matrix e () ()
forall e. e -> Matrix e () ()
One (e -> e
forall a. Num a => a -> a
abs e
a)
abs (Join a :: Matrix e a rows
a b :: Matrix e b rows
b) = Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
forall e a rows b.
Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
Join (Matrix e a rows -> Matrix e a rows
forall a. Num a => a -> a
abs Matrix e a rows
a) (Matrix e b rows -> Matrix e b rows
forall a. Num a => a -> a
abs Matrix e b rows
b)
abs (Fork a :: Matrix e cols a
a b :: Matrix e cols b
b) = Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
forall e cols a b.
Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
Fork (Matrix e cols a -> Matrix e cols a
forall a. Num a => a -> a
abs Matrix e cols a
a) (Matrix e cols b -> Matrix e cols b
forall a. Num a => a -> a
abs Matrix e cols b
b)
signum :: Matrix e cols rows -> Matrix e cols rows
signum Empty = Matrix e cols rows
forall e. Matrix e Void Void
Empty
signum (One a :: e
a) = e -> Matrix e () ()
forall e. e -> Matrix e () ()
One (e -> e
forall a. Num a => a -> a
signum e
a)
signum (Join a :: Matrix e a rows
a b :: Matrix e b rows
b) = Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
forall e a rows b.
Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
Join (Matrix e a rows -> Matrix e a rows
forall a. Num a => a -> a
signum Matrix e a rows
a) (Matrix e b rows -> Matrix e b rows
forall a. Num a => a -> a
signum Matrix e b rows
b)
signum (Fork a :: Matrix e cols a
a b :: Matrix e cols b
b) = Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
forall e cols a b.
Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
Fork (Matrix e cols a -> Matrix e cols a
forall a. Num a => a -> a
signum Matrix e cols a
a) (Matrix e cols b -> Matrix e cols b
forall a. Num a => a -> a
signum Matrix e cols b
b)
instance Ord e => Ord (Matrix e cols rows) where
Empty <= :: Matrix e cols rows -> Matrix e cols rows -> Bool
<= Empty = Bool
True
(One a :: e
a) <= (One b :: e
b) = e
a e -> e -> Bool
forall a. Ord a => a -> a -> Bool
<= e
b
(Join a :: Matrix e a rows
a b :: Matrix e b rows
b) <= (Join c :: Matrix e a rows
c d :: Matrix e b rows
d) = (Matrix e a rows
a Matrix e a rows -> Matrix e a rows -> Bool
forall a. Ord a => a -> a -> Bool
<= Matrix e a rows
Matrix e a rows
c) Bool -> Bool -> Bool
&& (Matrix e b rows
b Matrix e b rows -> Matrix e b rows -> Bool
forall a. Ord a => a -> a -> Bool
<= Matrix e b rows
Matrix e b rows
d)
(Fork a :: Matrix e cols a
a b :: Matrix e cols b
b) <= (Fork c :: Matrix e cols a
c d :: Matrix e cols b
d) = (Matrix e cols a
a Matrix e cols a -> Matrix e cols a -> Bool
forall a. Ord a => a -> a -> Bool
<= Matrix e cols a
Matrix e cols a
c) Bool -> Bool -> Bool
&& (Matrix e cols b
b Matrix e cols b -> Matrix e cols b -> Bool
forall a. Ord a => a -> a -> Bool
<= Matrix e cols b
Matrix e cols b
d)
x :: Matrix e cols rows
x@(Fork _ _) <= y :: Matrix e cols rows
y@(Join _ _) = Matrix e cols rows
x Matrix e cols rows -> Matrix e cols rows -> Bool
forall a. Ord a => a -> a -> Bool
<= Matrix e cols rows -> Matrix e cols rows
forall e cols rows. Matrix e cols rows -> Matrix e cols rows
abideJF Matrix e cols rows
y
x :: Matrix e cols rows
x@(Join _ _) <= y :: Matrix e cols rows
y@(Fork _ _) = Matrix e cols rows -> Matrix e cols rows
forall e cols rows. Matrix e cols rows -> Matrix e cols rows
abideJF Matrix e cols rows
x Matrix e cols rows -> Matrix e cols rows -> Bool
forall a. Ord a => a -> a -> Bool
<= Matrix e cols rows
y
empty :: Matrix e Void Void
empty :: Matrix e Void Void
empty = Matrix e Void Void
forall e. Matrix e Void Void
Empty
one :: e -> Matrix e () ()
one :: e -> Matrix e () ()
one = e -> Matrix e () ()
forall e. e -> Matrix e () ()
One
join :: Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
join :: Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
join = Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
forall e a rows b.
Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
Join
infixl 3 |||
(|||) :: Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
||| :: Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
(|||) = Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
forall e a rows b.
Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
Join
fork :: Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
fork :: Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
fork = Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
forall e cols a b.
Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
Fork
infixl 2 ===
(===) :: Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
=== :: Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
(===) = Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
forall e cols a b.
Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
Fork
class FromLists e cols rows where
fromLists :: [[e]] -> Matrix e cols rows
instance FromLists e Void Void where
fromLists :: [[e]] -> Matrix e Void Void
fromLists [] = Matrix e Void Void
forall e. Matrix e Void Void
Empty
fromLists _ = String -> Matrix e Void Void
forall a. HasCallStack => String -> a
error "Wrong dimensions"
instance {-# OVERLAPPING #-} FromLists e () () where
fromLists :: [[e]] -> Matrix e () ()
fromLists [[e :: e
e]] = e -> Matrix e () ()
forall e. e -> Matrix e () ()
One e
e
fromLists _ = String -> Matrix e () ()
forall a. HasCallStack => String -> a
error "Wrong dimensions"
instance {-# OVERLAPPING #-} (FromLists e cols ()) => FromLists e (Either () cols) () where
fromLists :: [[e]] -> Matrix e (Either () cols) ()
fromLists [h :: e
h : t :: [e]
t] = Matrix e () () -> Matrix e cols () -> Matrix e (Either () cols) ()
forall e a rows b.
Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
Join (e -> Matrix e () ()
forall e. e -> Matrix e () ()
One e
h) ([[e]] -> Matrix e cols ()
forall e cols rows.
FromLists e cols rows =>
[[e]] -> Matrix e cols rows
fromLists [[e]
t])
fromLists _ = String -> Matrix e (Either () cols) ()
forall a. HasCallStack => String -> a
error "Wrong dimensions"
instance {-# OVERLAPPABLE #-} (FromLists e a (), FromLists e b (), Countable a) => FromLists e (Either a b) () where
fromLists :: [[e]] -> Matrix e (Either a b) ()
fromLists [l :: [e]
l] =
let rowsA :: Int
rowsA = Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Proxy (Count a) -> Integer
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Integer
natVal (Proxy (Count a)
forall k (t :: k). Proxy t
Proxy :: Proxy (Count a)))
in Matrix e a () -> Matrix e b () -> Matrix e (Either a b) ()
forall e a rows b.
Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
Join ([[e]] -> Matrix e a ()
forall e cols rows.
FromLists e cols rows =>
[[e]] -> Matrix e cols rows
fromLists [Int -> [e] -> [e]
forall a. Int -> [a] -> [a]
take Int
rowsA [e]
l]) ([[e]] -> Matrix e b ()
forall e cols rows.
FromLists e cols rows =>
[[e]] -> Matrix e cols rows
fromLists [Int -> [e] -> [e]
forall a. Int -> [a] -> [a]
drop Int
rowsA [e]
l])
fromLists _ = String -> Matrix e (Either a b) ()
forall a. HasCallStack => String -> a
error "Wrong dimensions"
instance {-# OVERLAPPING #-} (FromLists e () rows) => FromLists e () (Either () rows) where
fromLists :: [[e]] -> Matrix e () (Either () rows)
fromLists ([h :: e
h] : t :: [[e]]
t) = Matrix e () () -> Matrix e () rows -> Matrix e () (Either () rows)
forall e cols a b.
Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
Fork (e -> Matrix e () ()
forall e. e -> Matrix e () ()
One e
h) ([[e]] -> Matrix e () rows
forall e cols rows.
FromLists e cols rows =>
[[e]] -> Matrix e cols rows
fromLists [[e]]
t)
fromLists _ = String -> Matrix e () (Either () rows)
forall a. HasCallStack => String -> a
error "Wrong dimensions"
instance {-# OVERLAPPABLE #-} (FromLists e () a, FromLists e () b, Countable a) => FromLists e () (Either a b) where
fromLists :: [[e]] -> Matrix e () (Either a b)
fromLists l :: [[e]]
l@([_] : _) =
let rowsA :: Int
rowsA = Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Proxy (Count a) -> Integer
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Integer
natVal (Proxy (Count a)
forall k (t :: k). Proxy t
Proxy :: Proxy (Count a)))
in Matrix e () a -> Matrix e () b -> Matrix e () (Either a b)
forall e cols a b.
Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
Fork ([[e]] -> Matrix e () a
forall e cols rows.
FromLists e cols rows =>
[[e]] -> Matrix e cols rows
fromLists (Int -> [[e]] -> [[e]]
forall a. Int -> [a] -> [a]
take Int
rowsA [[e]]
l)) ([[e]] -> Matrix e () b
forall e cols rows.
FromLists e cols rows =>
[[e]] -> Matrix e cols rows
fromLists (Int -> [[e]] -> [[e]]
forall a. Int -> [a] -> [a]
drop Int
rowsA [[e]]
l))
fromLists _ = String -> Matrix e () (Either a b)
forall a. HasCallStack => String -> a
error "Wrong dimensions"
instance {-# OVERLAPPABLE #-} (FromLists e (Either a b) c, FromLists e (Either a b) d, Countable c) => FromLists e (Either a b) (Either c d) where
fromLists :: [[e]] -> Matrix e (Either a b) (Either c d)
fromLists l :: [[e]]
l@(h :: [e]
h : t :: [[e]]
t) =
let lh :: Int
lh = [e] -> Int
forall (t :: Type -> Type) a. Foldable t => t a -> Int
length [e]
h
rowsC :: Int
rowsC = Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Proxy (Count c) -> Integer
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Integer
natVal (Proxy (Count c)
forall k (t :: k). Proxy t
Proxy :: Proxy (Count c)))
condition :: Bool
condition = ([e] -> Bool) -> [[e]] -> Bool
forall (t :: Type -> Type) a.
Foldable t =>
(a -> Bool) -> t a -> Bool
all ((Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
lh) (Int -> Bool) -> ([e] -> Int) -> [e] -> Bool
forall (k :: Type -> Type -> Type) b c a.
Category k =>
k b c -> k a b -> k a c
. [e] -> Int
forall (t :: Type -> Type) a. Foldable t => t a -> Int
length) [[e]]
t
in if Int
lh Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> 0 Bool -> Bool -> Bool
&& Bool
condition
then Matrix e (Either a b) c
-> Matrix e (Either a b) d -> Matrix e (Either a b) (Either c d)
forall e cols a b.
Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
Fork ([[e]] -> Matrix e (Either a b) c
forall e cols rows.
FromLists e cols rows =>
[[e]] -> Matrix e cols rows
fromLists (Int -> [[e]] -> [[e]]
forall a. Int -> [a] -> [a]
take Int
rowsC [[e]]
l)) ([[e]] -> Matrix e (Either a b) d
forall e cols rows.
FromLists e cols rows =>
[[e]] -> Matrix e cols rows
fromLists (Int -> [[e]] -> [[e]]
forall a. Int -> [a] -> [a]
drop Int
rowsC [[e]]
l))
else String -> Matrix e (Either a b) (Either c d)
forall a. HasCallStack => String -> a
error "Not all rows have the same length"
matrixBuilder' ::
forall e cols rows.
( FromLists e cols rows,
CountableDimensions cols rows
) => ((Int, Int) -> e) -> Matrix e cols rows
matrixBuilder' :: ((Int, Int) -> e) -> Matrix e cols rows
matrixBuilder' f :: (Int, Int) -> e
f =
let c :: Int
c = Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Integer -> Int) -> Integer -> Int
forall a b. (a -> b) -> a -> b
$ Proxy (Count cols) -> Integer
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Integer
natVal (Proxy (Count cols)
forall k (t :: k). Proxy t
Proxy :: Proxy (Count cols))
r :: Int
r = Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Integer -> Int) -> Integer -> Int
forall a b. (a -> b) -> a -> b
$ Proxy (Count rows) -> Integer
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Integer
natVal (Proxy (Count rows)
forall k (t :: k). Proxy t
Proxy :: Proxy (Count rows))
positions :: [(Int, Int)]
positions = [(Int
a, Int
b) | Int
a <- [0 .. (Int
r Int -> Int -> Int
forall a. Num a => a -> a -> a
- 1)], Int
b <- [0 .. (Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- 1)]]
in [[e]] -> Matrix e cols rows
forall e cols rows.
FromLists e cols rows =>
[[e]] -> Matrix e cols rows
fromLists ([[e]] -> Matrix e cols rows)
-> ([[(Int, Int)]] -> [[e]])
-> [[(Int, Int)]]
-> Matrix e cols rows
forall (k :: Type -> Type -> Type) b c a.
Category k =>
k b c -> k a b -> k a c
. ([(Int, Int)] -> [e]) -> [[(Int, Int)]] -> [[e]]
forall a b. (a -> b) -> [a] -> [b]
map (((Int, Int) -> e) -> [(Int, Int)] -> [e]
forall a b. (a -> b) -> [a] -> [b]
map (Int, Int) -> e
f) ([[(Int, Int)]] -> Matrix e cols rows)
-> ([(Int, Int)] -> [[(Int, Int)]])
-> [(Int, Int)]
-> Matrix e cols rows
forall (k :: Type -> Type -> Type) b c a.
Category k =>
k b c -> k a b -> k a c
. ((Int, Int) -> (Int, Int) -> Bool)
-> [(Int, Int)] -> [[(Int, Int)]]
forall a. (a -> a -> Bool) -> [a] -> [[a]]
groupBy (\(x :: Int
x, _) (w :: Int
w, _) -> Int
x Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
w) ([(Int, Int)] -> Matrix e cols rows)
-> [(Int, Int)] -> Matrix e cols rows
forall a b. (a -> b) -> a -> b
$ [(Int, Int)]
positions
matrixBuilder ::
forall e a b.
( FromListsN e a b,
CountableN b,
Enum a,
Enum b,
Bounded a,
Bounded b,
Eq a,
CountableDimensionsN a b
) => ((a, b) -> e) -> Matrix e (Normalize a) (Normalize b)
matrixBuilder :: ((a, b) -> e) -> Matrix e (Normalize a) (Normalize b)
matrixBuilder f :: (a, b) -> e
f =
let r :: Int
r = Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Integer -> Int) -> Integer -> Int
forall a b. (a -> b) -> a -> b
$ Proxy (Count (Normalize b)) -> Integer
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Integer
natVal (Proxy (Count (Normalize b))
forall k (t :: k). Proxy t
Proxy :: Proxy (Count (Normalize b)))
positions :: [(a, b)]
positions = [(a
a, b
b) | a
a <- [a
forall a. Bounded a => a
minBound .. a
forall a. Bounded a => a
maxBound], b
b <- [b
forall a. Bounded a => a
minBound .. b
forall a. Bounded a => a
maxBound]]
in [[e]] -> Matrix e (Normalize a) (Normalize b)
forall e cols rows.
FromLists e cols rows =>
[[e]] -> Matrix e cols rows
fromLists ([[e]] -> Matrix e (Normalize a) (Normalize b))
-> ([[(a, b)]] -> [[e]])
-> [[(a, b)]]
-> Matrix e (Normalize a) (Normalize b)
forall (k :: Type -> Type -> Type) b c a.
Category k =>
k b c -> k a b -> k a c
. ([(a, b)] -> [e]) -> [[(a, b)]] -> [[e]]
forall a b. (a -> b) -> [a] -> [b]
map (((a, b) -> e) -> [(a, b)] -> [e]
forall a b. (a -> b) -> [a] -> [b]
map (a, b) -> e
f) ([[(a, b)]] -> Matrix e (Normalize a) (Normalize b))
-> ([[(a, b)]] -> [[(a, b)]])
-> [[(a, b)]]
-> Matrix e (Normalize a) (Normalize b)
forall (k :: Type -> Type -> Type) b c a.
Category k =>
k b c -> k a b -> k a c
. [[(a, b)]] -> [[(a, b)]]
forall a. [[a]] -> [[a]]
transpose ([[(a, b)]] -> Matrix e (Normalize a) (Normalize b))
-> ([(a, b)] -> [[(a, b)]])
-> [(a, b)]
-> Matrix e (Normalize a) (Normalize b)
forall (k :: Type -> Type -> Type) b c a.
Category k =>
k b c -> k a b -> k a c
. Int -> [(a, b)] -> [[(a, b)]]
forall a. Int -> [a] -> [[a]]
buildList Int
r ([(a, b)] -> Matrix e (Normalize a) (Normalize b))
-> [(a, b)] -> Matrix e (Normalize a) (Normalize b)
forall a b. (a -> b) -> a -> b
$ [(a, b)]
positions
where
buildList :: Int -> [a] -> [[a]]
buildList _ [] = []
buildList r :: Int
r l :: [a]
l = Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
take Int
r [a]
l [a] -> [[a]] -> [[a]]
forall a. a -> [a] -> [a]
: Int -> [a] -> [[a]]
buildList Int
r (Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
drop Int
r [a]
l)
col :: (FromLists e () rows) => [e] -> Matrix e () rows
col :: [e] -> Matrix e () rows
col = [[e]] -> Matrix e () rows
forall e cols rows.
FromLists e cols rows =>
[[e]] -> Matrix e cols rows
fromLists ([[e]] -> Matrix e () rows)
-> ([e] -> [[e]]) -> [e] -> Matrix e () rows
forall (k :: Type -> Type -> Type) b c a.
Category k =>
k b c -> k a b -> k a c
. (e -> [e]) -> [e] -> [[e]]
forall a b. (a -> b) -> [a] -> [b]
map (e -> [e] -> [e]
forall a. a -> [a] -> [a]
: [])
row :: (FromLists e cols ()) => [e] -> Matrix e cols ()
row :: [e] -> Matrix e cols ()
row = [[e]] -> Matrix e cols ()
forall e cols rows.
FromLists e cols rows =>
[[e]] -> Matrix e cols rows
fromLists ([[e]] -> Matrix e cols ())
-> ([e] -> [[e]]) -> [e] -> Matrix e cols ()
forall (k :: Type -> Type -> Type) b c a.
Category k =>
k b c -> k a b -> k a c
. ([e] -> [[e]] -> [[e]]
forall a. a -> [a] -> [a]
: [])
fromF' ::
forall a b cols rows e.
( Liftable e a b,
CountableDimensions cols rows,
FromLists e rows cols
) =>
(a -> b) ->
Matrix e cols rows
fromF' :: (a -> b) -> Matrix e cols rows
fromF' f :: a -> b
f =
let minA :: a
minA = Bounded a => a
forall a. Bounded a => a
minBound @a
maxA :: a
maxA = Bounded a => a
forall a. Bounded a => a
maxBound @a
minB :: b
minB = Bounded b => b
forall a. Bounded a => a
minBound @b
maxB :: b
maxB = Bounded b => b
forall a. Bounded a => a
maxBound @b
ccols :: Int
ccols = Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Integer -> Int) -> Integer -> Int
forall a b. (a -> b) -> a -> b
$ Proxy (Count cols) -> Integer
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Integer
natVal (Proxy (Count cols)
forall k (t :: k). Proxy t
Proxy :: Proxy (Count cols))
rrows :: Int
rrows = Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Integer -> Int) -> Integer -> Int
forall a b. (a -> b) -> a -> b
$ Proxy (Count rows) -> Integer
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Integer
natVal (Proxy (Count rows)
forall k (t :: k). Proxy t
Proxy :: Proxy (Count rows))
elementsA :: [a]
elementsA = Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
take Int
ccols [a
minA .. a
maxA]
elementsB :: [b]
elementsB = Int -> [b] -> [b]
forall a. Int -> [a] -> [a]
take Int
rrows [b
minB .. b
maxB]
combinations :: [(a, b)]
combinations = (,) (a -> b -> (a, b)) -> [a] -> [b -> (a, b)]
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> [a]
elementsA [b -> (a, b)] -> [b] -> [(a, b)]
forall (f :: Type -> Type) a b.
Applicative f =>
f (a -> b) -> f a -> f b
<*> [b]
elementsB
combAp :: [e]
combAp = (((Int, Int), e) -> e) -> [((Int, Int), e)] -> [e]
forall a b. (a -> b) -> [a] -> [b]
map ((Int, Int), e) -> e
forall a b. (a, b) -> b
snd ([((Int, Int), e)] -> [e])
-> ([((Int, Int), e)] -> [((Int, Int), e)])
-> [((Int, Int), e)]
-> [e]
forall (k :: Type -> Type -> Type) b c a.
Category k =>
k b c -> k a b -> k a c
. [((Int, Int), e)] -> [((Int, Int), e)]
forall a. Ord a => [a] -> [a]
sort ([((Int, Int), e)] -> [e])
-> ([(a, b)] -> [((Int, Int), e)]) -> [(a, b)] -> [e]
forall (k :: Type -> Type -> Type) b c a.
Category k =>
k b c -> k a b -> k a c
. ((a, b) -> ((Int, Int), e)) -> [(a, b)] -> [((Int, Int), e)]
forall a b. (a -> b) -> [a] -> [b]
map (\(a :: a
a, b :: b
b) -> if a -> b
f a
a b -> b -> Bool
forall a. Eq a => a -> a -> Bool
== b
b
then ((a -> Int
forall a. Enum a => a -> Int
fromEnum a
a, b -> Int
forall a. Enum a => a -> Int
fromEnum b
b), 1)
else ((a -> Int
forall a. Enum a => a -> Int
fromEnum a
a, b -> Int
forall a. Enum a => a -> Int
fromEnum b
b), 0)) ([(a, b)] -> [e]) -> [(a, b)] -> [e]
forall a b. (a -> b) -> a -> b
$ [(a, b)]
combinations
mList :: [[e]]
mList = [e] -> Int -> [[e]]
forall a. [a] -> Int -> [[a]]
buildList [e]
combAp Int
rrows
in Matrix e rows cols -> Matrix e cols rows
forall e cols rows. Matrix e cols rows -> Matrix e rows cols
tr (Matrix e rows cols -> Matrix e cols rows)
-> Matrix e rows cols -> Matrix e cols rows
forall a b. (a -> b) -> a -> b
$ [[e]] -> Matrix e rows cols
forall e cols rows.
FromLists e cols rows =>
[[e]] -> Matrix e cols rows
fromLists [[e]]
mList
where
buildList :: [a] -> Int -> [[a]]
buildList [] _ = []
buildList l :: [a]
l r :: Int
r = Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
take Int
r [a]
l [a] -> [[a]] -> [[a]]
forall a. a -> [a] -> [a]
: [a] -> Int -> [[a]]
buildList (Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
drop Int
r [a]
l) Int
r
fromF ::
forall a b e.
( Liftable e a b,
CountableDimensionsN a b,
FromListsN e b a
) =>
(a -> b) ->
Matrix e (Normalize a) (Normalize b)
fromF :: (a -> b) -> Matrix e (Normalize a) (Normalize b)
fromF f :: a -> b
f =
let minA :: a
minA = Bounded a => a
forall a. Bounded a => a
minBound @a
maxA :: a
maxA = Bounded a => a
forall a. Bounded a => a
maxBound @a
minB :: b
minB = Bounded b => b
forall a. Bounded a => a
minBound @b
maxB :: b
maxB = Bounded b => b
forall a. Bounded a => a
maxBound @b
ccols :: Int
ccols = Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Integer -> Int) -> Integer -> Int
forall a b. (a -> b) -> a -> b
$ Proxy (Count (Normalize a)) -> Integer
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Integer
natVal (Proxy (Count (Normalize a))
forall k (t :: k). Proxy t
Proxy :: Proxy (Count (Normalize a)))
rrows :: Int
rrows = Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Integer -> Int) -> Integer -> Int
forall a b. (a -> b) -> a -> b
$ Proxy (Count (Normalize b)) -> Integer
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Integer
natVal (Proxy (Count (Normalize b))
forall k (t :: k). Proxy t
Proxy :: Proxy (Count (Normalize b)))
elementsA :: [a]
elementsA = Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
take Int
ccols [a
minA .. a
maxA]
elementsB :: [b]
elementsB = Int -> [b] -> [b]
forall a. Int -> [a] -> [a]
take Int
rrows [b
minB .. b
maxB]
combinations :: [(a, b)]
combinations = (,) (a -> b -> (a, b)) -> [a] -> [b -> (a, b)]
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> [a]
elementsA [b -> (a, b)] -> [b] -> [(a, b)]
forall (f :: Type -> Type) a b.
Applicative f =>
f (a -> b) -> f a -> f b
<*> [b]
elementsB
combAp :: [e]
combAp = (((Int, Int), e) -> e) -> [((Int, Int), e)] -> [e]
forall a b. (a -> b) -> [a] -> [b]
map ((Int, Int), e) -> e
forall a b. (a, b) -> b
snd ([((Int, Int), e)] -> [e])
-> ([((Int, Int), e)] -> [((Int, Int), e)])
-> [((Int, Int), e)]
-> [e]
forall (k :: Type -> Type -> Type) b c a.
Category k =>
k b c -> k a b -> k a c
. [((Int, Int), e)] -> [((Int, Int), e)]
forall a. Ord a => [a] -> [a]
sort ([((Int, Int), e)] -> [e])
-> ([(a, b)] -> [((Int, Int), e)]) -> [(a, b)] -> [e]
forall (k :: Type -> Type -> Type) b c a.
Category k =>
k b c -> k a b -> k a c
. ((a, b) -> ((Int, Int), e)) -> [(a, b)] -> [((Int, Int), e)]
forall a b. (a -> b) -> [a] -> [b]
map (\(a :: a
a, b :: b
b) -> if a -> b
f a
a b -> b -> Bool
forall a. Eq a => a -> a -> Bool
== b
b
then ((a -> Int
forall a. Enum a => a -> Int
fromEnum a
a, b -> Int
forall a. Enum a => a -> Int
fromEnum b
b), 1)
else ((a -> Int
forall a. Enum a => a -> Int
fromEnum a
a, b -> Int
forall a. Enum a => a -> Int
fromEnum b
b), 0)) ([(a, b)] -> [e]) -> [(a, b)] -> [e]
forall a b. (a -> b) -> a -> b
$ [(a, b)]
combinations
mList :: [[e]]
mList = [e] -> Int -> [[e]]
forall a. [a] -> Int -> [[a]]
buildList [e]
combAp Int
rrows
in Matrix e (Normalize b) (Normalize a)
-> Matrix e (Normalize a) (Normalize b)
forall e cols rows. Matrix e cols rows -> Matrix e rows cols
tr (Matrix e (Normalize b) (Normalize a)
-> Matrix e (Normalize a) (Normalize b))
-> Matrix e (Normalize b) (Normalize a)
-> Matrix e (Normalize a) (Normalize b)
forall a b. (a -> b) -> a -> b
$ [[e]] -> Matrix e (Normalize b) (Normalize a)
forall e cols rows.
FromLists e cols rows =>
[[e]] -> Matrix e cols rows
fromLists [[e]]
mList
where
buildList :: [a] -> Int -> [[a]]
buildList [] _ = []
buildList l :: [a]
l r :: Int
r = Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
take Int
r [a]
l [a] -> [[a]] -> [[a]]
forall a. a -> [a] -> [a]
: [a] -> Int -> [[a]]
buildList (Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
drop Int
r [a]
l) Int
r
toLists :: Matrix e cols rows -> [[e]]
toLists :: Matrix e cols rows -> [[e]]
toLists Empty = []
toLists (One e :: e
e) = [[e
e]]
toLists (Fork l :: Matrix e cols a
l r :: Matrix e cols b
r) = Matrix e cols a -> [[e]]
forall e cols rows. Matrix e cols rows -> [[e]]
toLists Matrix e cols a
l [[e]] -> [[e]] -> [[e]]
forall a. [a] -> [a] -> [a]
++ Matrix e cols b -> [[e]]
forall e cols rows. Matrix e cols rows -> [[e]]
toLists Matrix e cols b
r
toLists (Join l :: Matrix e a rows
l r :: Matrix e b rows
r) = ([e] -> [e] -> [e]) -> [[e]] -> [[e]] -> [[e]]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith [e] -> [e] -> [e]
forall a. [a] -> [a] -> [a]
(++) (Matrix e a rows -> [[e]]
forall e cols rows. Matrix e cols rows -> [[e]]
toLists Matrix e a rows
l) (Matrix e b rows -> [[e]]
forall e cols rows. Matrix e cols rows -> [[e]]
toLists Matrix e b rows
r)
toList :: Matrix e cols rows -> [e]
toList :: Matrix e cols rows -> [e]
toList = [[e]] -> [e]
forall (t :: Type -> Type) a. Foldable t => t [a] -> [a]
concat ([[e]] -> [e])
-> (Matrix e cols rows -> [[e]]) -> Matrix e cols rows -> [e]
forall (k :: Type -> Type -> Type) b c a.
Category k =>
k b c -> k a b -> k a c
. Matrix e cols rows -> [[e]]
forall e cols rows. Matrix e cols rows -> [[e]]
toLists
zeros :: (Num e, FromLists e cols rows, CountableDimensions cols rows) => Matrix e cols rows
zeros :: Matrix e cols rows
zeros = ((Int, Int) -> e) -> Matrix e cols rows
forall e cols rows.
(FromLists e cols rows, CountableDimensions cols rows) =>
((Int, Int) -> e) -> Matrix e cols rows
matrixBuilder' (e -> (Int, Int) -> e
forall a b. a -> b -> a
const 0)
ones :: (Num e, FromLists e cols rows, CountableDimensions cols rows) => Matrix e cols rows
ones :: Matrix e cols rows
ones = ((Int, Int) -> e) -> Matrix e cols rows
forall e cols rows.
(FromLists e cols rows, CountableDimensions cols rows) =>
((Int, Int) -> e) -> Matrix e cols rows
matrixBuilder' (e -> (Int, Int) -> e
forall a b. a -> b -> a
const 1)
constant :: (Num e, FromLists e cols rows, CountableDimensions cols rows) => e -> Matrix e cols rows
constant :: e -> Matrix e cols rows
constant e :: e
e = ((Int, Int) -> e) -> Matrix e cols rows
forall e cols rows.
(FromLists e cols rows, CountableDimensions cols rows) =>
((Int, Int) -> e) -> Matrix e cols rows
matrixBuilder' (e -> (Int, Int) -> e
forall a b. a -> b -> a
const e
e)
bang :: forall e cols. (Num e, Enum e, FromLists e cols (), Countable cols) => Matrix e cols ()
bang :: Matrix e cols ()
bang =
let c :: Int
c = Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Integer -> Int) -> Integer -> Int
forall a b. (a -> b) -> a -> b
$ Proxy (Count cols) -> Integer
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Integer
natVal (Proxy (Count cols)
forall k (t :: k). Proxy t
Proxy :: Proxy (Count cols))
in [[e]] -> Matrix e cols ()
forall e cols rows.
FromLists e cols rows =>
[[e]] -> Matrix e cols rows
fromLists [Int -> [e] -> [e]
forall a. Int -> [a] -> [a]
take Int
c [1, 1 ..]]
iden :: forall e cols . (Num e, FromLists e cols cols, Countable cols) => Matrix e cols cols
iden :: Matrix e cols cols
iden = ((Int, Int) -> e) -> Matrix e cols cols
forall e cols rows.
(FromLists e cols rows, CountableDimensions cols rows) =>
((Int, Int) -> e) -> Matrix e cols rows
matrixBuilder' (e -> e -> Bool -> e
forall a. a -> a -> Bool -> a
bool 0 1 (Bool -> e) -> ((Int, Int) -> Bool) -> (Int, Int) -> e
forall (k :: Type -> Type -> Type) b c a.
Category k =>
k b c -> k a b -> k a c
. (Int -> Int -> Bool) -> (Int, Int) -> Bool
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
(==))
{-# NOINLINE iden #-}
comp :: (Num e) => Matrix e cr rows -> Matrix e cols cr -> Matrix e cols rows
comp :: Matrix e cr rows -> Matrix e cols cr -> Matrix e cols rows
comp Empty Empty = Matrix e cols rows
forall e. Matrix e Void Void
Empty
comp (One a :: e
a) (One b :: e
b) = e -> Matrix e () ()
forall e. e -> Matrix e () ()
One (e
a e -> e -> e
forall a. Num a => a -> a -> a
* e
b)
comp (Join a :: Matrix e a rows
a b :: Matrix e b rows
b) (Fork c :: Matrix e cols a
c d :: Matrix e cols b
d) = Matrix e a rows -> Matrix e cols a -> Matrix e cols rows
forall e cr rows cols.
Num e =>
Matrix e cr rows -> Matrix e cols cr -> Matrix e cols rows
comp Matrix e a rows
a Matrix e cols a
Matrix e cols a
c Matrix e cols rows -> Matrix e cols rows -> Matrix e cols rows
forall a. Num a => a -> a -> a
+ Matrix e b rows -> Matrix e cols b -> Matrix e cols rows
forall e cr rows cols.
Num e =>
Matrix e cr rows -> Matrix e cols cr -> Matrix e cols rows
comp Matrix e b rows
b Matrix e cols b
Matrix e cols b
d
comp (Fork a :: Matrix e cr a
a b :: Matrix e cr b
b) c :: Matrix e cols cr
c = Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
forall e cols a b.
Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
Fork (Matrix e cr a -> Matrix e cols cr -> Matrix e cols a
forall e cr rows cols.
Num e =>
Matrix e cr rows -> Matrix e cols cr -> Matrix e cols rows
comp Matrix e cr a
a Matrix e cols cr
c) (Matrix e cr b -> Matrix e cols cr -> Matrix e cols b
forall e cr rows cols.
Num e =>
Matrix e cr rows -> Matrix e cols cr -> Matrix e cols rows
comp Matrix e cr b
b Matrix e cols cr
c)
comp c :: Matrix e cr rows
c (Join a :: Matrix e a cr
a b :: Matrix e b cr
b) = Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
forall e a rows b.
Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
Join (Matrix e cr rows -> Matrix e a cr -> Matrix e a rows
forall e cr rows cols.
Num e =>
Matrix e cr rows -> Matrix e cols cr -> Matrix e cols rows
comp Matrix e cr rows
c Matrix e a cr
a) (Matrix e cr rows -> Matrix e b cr -> Matrix e b rows
forall e cr rows cols.
Num e =>
Matrix e cr rows -> Matrix e cols cr -> Matrix e cols rows
comp Matrix e cr rows
c Matrix e b cr
b)
{-# NOINLINE comp #-}
{-# RULES
"comp/iden1" forall m. comp m iden = m ;
"comp/iden2" forall m. comp iden m = m
#-}
infixl 7 .|
(.|) :: Num e => e -> Matrix e cols rows -> Matrix e cols rows
.| :: e -> Matrix e cols rows -> Matrix e cols rows
(.|) _ Empty = Matrix e cols rows
forall e. Matrix e Void Void
Empty
(.|) e :: e
e (One a :: e
a) = e -> Matrix e () ()
forall e. e -> Matrix e () ()
One (e
e e -> e -> e
forall a. Num a => a -> a -> a
* e
a)
(.|) e :: e
e (Join a :: Matrix e a rows
a b :: Matrix e b rows
b) = Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
forall e a rows b.
Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
Join (e
e e -> Matrix e a rows -> Matrix e a rows
forall e cols rows.
Num e =>
e -> Matrix e cols rows -> Matrix e cols rows
.| Matrix e a rows
a) (e
e e -> Matrix e b rows -> Matrix e b rows
forall e cols rows.
Num e =>
e -> Matrix e cols rows -> Matrix e cols rows
.| Matrix e b rows
b)
(.|) e :: e
e (Fork a :: Matrix e cols a
a b :: Matrix e cols b
b) = Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
forall e cols a b.
Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
Fork (e
e e -> Matrix e cols a -> Matrix e cols a
forall e cols rows.
Num e =>
e -> Matrix e cols rows -> Matrix e cols rows
.| Matrix e cols a
a) (e
e e -> Matrix e cols b -> Matrix e cols b
forall e cols rows.
Num e =>
e -> Matrix e cols rows -> Matrix e cols rows
.| Matrix e cols b
b)
infixl 7 ./
(./) :: Fractional e => Matrix e cols rows -> e -> Matrix e cols rows
./ :: Matrix e cols rows -> e -> Matrix e cols rows
(./) Empty _ = Matrix e cols rows
forall e. Matrix e Void Void
Empty
(./) (One a :: e
a) e :: e
e = e -> Matrix e () ()
forall e. e -> Matrix e () ()
One (e
a e -> e -> e
forall a. Fractional a => a -> a -> a
/ e
e)
(./) (Join a :: Matrix e a rows
a b :: Matrix e b rows
b) e :: e
e = Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
forall e a rows b.
Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
Join (Matrix e a rows
a Matrix e a rows -> e -> Matrix e a rows
forall e cols rows.
Fractional e =>
Matrix e cols rows -> e -> Matrix e cols rows
./ e
e) (Matrix e b rows
b Matrix e b rows -> e -> Matrix e b rows
forall e cols rows.
Fractional e =>
Matrix e cols rows -> e -> Matrix e cols rows
./ e
e)
(./) (Fork a :: Matrix e cols a
a b :: Matrix e cols b
b) e :: e
e = Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
forall e cols a b.
Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
Fork (Matrix e cols a
a Matrix e cols a -> e -> Matrix e cols a
forall e cols rows.
Fractional e =>
Matrix e cols rows -> e -> Matrix e cols rows
./ e
e) (Matrix e cols b
b Matrix e cols b -> e -> Matrix e cols b
forall e cols rows.
Fractional e =>
Matrix e cols rows -> e -> Matrix e cols rows
./ e
e)
p1 :: (Num e, CountableDimensions n m, FromLists e n m, FromLists e m m) => Matrix e (Either m n) m
p1 :: Matrix e (Either m n) m
p1 = Matrix e m m -> Matrix e n m -> Matrix e (Either m n) m
forall e a rows b.
Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
join Matrix e m m
forall (k :: Type -> Type -> Type) a.
(Category k, Object k a) =>
k a a
id Matrix e n m
forall e cols rows.
(Num e, FromLists e cols rows, CountableDimensions cols rows) =>
Matrix e cols rows
zeros
p2 :: (Num e, CountableDimensions n m, FromLists e m n, FromLists e n n) => Matrix e (Either m n) n
p2 :: Matrix e (Either m n) n
p2 = Matrix e m n -> Matrix e n n -> Matrix e (Either m n) n
forall e a rows b.
Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
join Matrix e m n
forall e cols rows.
(Num e, FromLists e cols rows, CountableDimensions cols rows) =>
Matrix e cols rows
zeros Matrix e n n
forall (k :: Type -> Type -> Type) a.
(Category k, Object k a) =>
k a a
id
i1 :: (Num e, CountableDimensions n m, FromLists e n m, FromLists e m m) => Matrix e m (Either m n)
i1 :: Matrix e m (Either m n)
i1 = Matrix e (Either m n) m -> Matrix e m (Either m n)
forall e cols rows. Matrix e cols rows -> Matrix e rows cols
tr Matrix e (Either m n) m
forall e n m.
(Num e, CountableDimensions n m, FromLists e n m,
FromLists e m m) =>
Matrix e (Either m n) m
p1
i2 :: (Num e, CountableDimensions n m, FromLists e m n, FromLists e n n) => Matrix e n (Either m n)
i2 :: Matrix e n (Either m n)
i2 = Matrix e (Either m n) n -> Matrix e n (Either m n)
forall e cols rows. Matrix e cols rows -> Matrix e rows cols
tr Matrix e (Either m n) n
forall e n m.
(Num e, CountableDimensions n m, FromLists e m n,
FromLists e n n) =>
Matrix e (Either m n) n
p2
rows :: forall e cols rows. (Countable rows) => Matrix e cols rows -> Int
rows :: Matrix e cols rows -> Int
rows _ = Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Integer -> Int) -> Integer -> Int
forall a b. (a -> b) -> a -> b
$ Proxy (Count rows) -> Integer
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Integer
natVal (Proxy (Count rows)
forall k (t :: k). Proxy t
Proxy :: Proxy (Count rows))
columns :: forall e cols rows. (Countable cols) => Matrix e cols rows -> Int
columns :: Matrix e cols rows -> Int
columns _ = Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Integer -> Int) -> Integer -> Int
forall a b. (a -> b) -> a -> b
$ Proxy (Count cols) -> Integer
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Integer
natVal (Proxy (Count cols)
forall k (t :: k). Proxy t
Proxy :: Proxy (Count cols))
infixl 5 -|-
(-|-) ::
forall e n k m j.
( Num e,
CountableDimensions j k,
FromLists e k k,
FromLists e j k,
FromLists e k j,
FromLists e j j
) =>
Matrix e n k ->
Matrix e m j ->
Matrix e (Either n m) (Either k j)
-|- :: Matrix e n k -> Matrix e m j -> Matrix e (Either n m) (Either k j)
(-|-) a :: Matrix e n k
a b :: Matrix e m j
b = Matrix e n (Either k j)
-> Matrix e m (Either k j) -> Matrix e (Either n m) (Either k j)
forall e a rows b.
Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
Join (Matrix e k (Either k j)
forall e n m.
(Num e, CountableDimensions n m, FromLists e n m,
FromLists e m m) =>
Matrix e m (Either m n)
i1 Matrix e k (Either k j) -> Matrix e n k -> Matrix e n (Either k j)
forall (k :: Type -> Type -> Type) b c a.
Category k =>
k b c -> k a b -> k a c
. Matrix e n k
a) (Matrix e j (Either k j)
forall e n m.
(Num e, CountableDimensions n m, FromLists e m n,
FromLists e n n) =>
Matrix e n (Either m n)
i2 Matrix e j (Either k j) -> Matrix e m j -> Matrix e m (Either k j)
forall (k :: Type -> Type -> Type) b c a.
Category k =>
k b c -> k a b -> k a c
. Matrix e m j
b)
fstM ::
forall e m k .
( Num e,
CountableDimensions k m,
FromLists e (Normalize (m, k)) m,
CountableN (m, k)
) => Matrix e (Normalize (m, k)) m
fstM :: Matrix e (Normalize (m, k)) m
fstM = ((Int, Int) -> e) -> Matrix e (FromNat (Count m * Count k)) m
forall e cols rows.
(FromLists e cols rows, CountableDimensions cols rows) =>
((Int, Int) -> e) -> Matrix e cols rows
matrixBuilder' (Int, Int) -> e
f
where
offset :: Int
offset = Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Proxy (Count k) -> Integer
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Integer
natVal (Proxy (Count k)
forall k (t :: k). Proxy t
Proxy :: Proxy (Count k)))
f :: (Int, Int) -> e
f (x :: Int
x, y :: Int
y)
| Int
y Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= (Int
x Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
offset) Bool -> Bool -> Bool
&& Int
y Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= (Int
x Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
offset Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
offset Int -> Int -> Int
forall a. Num a => a -> a -> a
- 1) = 1
| Bool
otherwise = 0
sndM ::
forall e m k .
( Num e,
CountableDimensions k m,
FromLists e (Normalize (m, k)) k,
CountableN (m, k)
) => Matrix e (Normalize (m, k)) k
sndM :: Matrix e (Normalize (m, k)) k
sndM = ((Int, Int) -> e) -> Matrix e (FromNat (Count m * Count k)) k
forall e cols rows.
(FromLists e cols rows, CountableDimensions cols rows) =>
((Int, Int) -> e) -> Matrix e cols rows
matrixBuilder' (Int, Int) -> e
f
where
offset :: Int
offset = Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Proxy (Count k) -> Integer
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Integer
natVal (Proxy (Count k)
forall k (t :: k). Proxy t
Proxy :: Proxy (Count k)))
f :: (Int, Int) -> e
f (x :: Int
x, y :: Int
y)
| Int
x Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
y Bool -> Bool -> Bool
|| Int -> Int -> Int
forall a. Integral a => a -> a -> a
mod (Int
y Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
x) Int
offset Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== 0 = 1
| Bool
otherwise = 0
kr ::
forall e cols a b.
( Num e,
CountableDimensions a b,
CountableN (a, b),
FromLists e (Normalize (a, b)) a,
FromLists e (Normalize (a, b)) b
) => Matrix e cols a -> Matrix e cols b -> Matrix e cols (Normalize (a, b))
kr :: Matrix e cols a
-> Matrix e cols b -> Matrix e cols (Normalize (a, b))
kr a :: Matrix e cols a
a b :: Matrix e cols b
b =
let fstM' :: Matrix e (Normalize (a, b)) a
fstM' = (Num e, CountableDimensions b a, FromLists e (Normalize (a, b)) a,
CountableN (a, b)) =>
Matrix e (Normalize (a, b)) a
forall e m k.
(Num e, CountableDimensions k m, FromLists e (Normalize (m, k)) m,
CountableN (m, k)) =>
Matrix e (Normalize (m, k)) m
fstM @e @a @b
sndM' :: Matrix e (Normalize (a, b)) b
sndM' = (Num e, CountableDimensions b a, FromLists e (Normalize (a, b)) b,
CountableN (a, b)) =>
Matrix e (Normalize (a, b)) b
forall e m k.
(Num e, CountableDimensions k m, FromLists e (Normalize (m, k)) k,
CountableN (m, k)) =>
Matrix e (Normalize (m, k)) k
sndM @e @a @b
in (Matrix e (FromNat (Count a * Count b)) a
-> Matrix e a (FromNat (Count a * Count b))
forall e cols rows. Matrix e cols rows -> Matrix e rows cols
tr Matrix e (Normalize (a, b)) a
Matrix e (FromNat (Count a * Count b)) a
fstM' Matrix e a (FromNat (Count a * Count b))
-> Matrix e cols a -> Matrix e cols (FromNat (Count a * Count b))
forall (k :: Type -> Type -> Type) b c a.
Category k =>
k b c -> k a b -> k a c
. Matrix e cols a
a) Matrix e cols (FromNat (Count a * Count b))
-> Matrix e cols (FromNat (Count a * Count b))
-> Matrix e cols (FromNat (Count a * Count b))
forall a. Num a => a -> a -> a
* (Matrix e (FromNat (Count a * Count b)) b
-> Matrix e b (FromNat (Count a * Count b))
forall e cols rows. Matrix e cols rows -> Matrix e rows cols
tr Matrix e (Normalize (a, b)) b
Matrix e (FromNat (Count a * Count b)) b
sndM' Matrix e b (FromNat (Count a * Count b))
-> Matrix e cols b -> Matrix e cols (FromNat (Count a * Count b))
forall (k :: Type -> Type -> Type) b c a.
Category k =>
k b c -> k a b -> k a c
. Matrix e cols b
b)
infixl 4 ><
(><) ::
forall e m p n q.
( Num e,
CountableDimensions m n,
CountableDimensions p q,
CountableDimensionsN (m, n) (p, q),
FromLists e (Normalize (m, n)) m,
FromLists e (Normalize (m, n)) n,
FromLists e (Normalize (p, q)) p,
FromLists e (Normalize (p, q)) q
)
=> Matrix e m p -> Matrix e n q -> Matrix e (Normalize (m, n)) (Normalize (p, q))
>< :: Matrix e m p
-> Matrix e n q -> Matrix e (Normalize (m, n)) (Normalize (p, q))
(><) a :: Matrix e m p
a b :: Matrix e n q
b =
let fstM' :: Matrix e (Normalize (m, n)) m
fstM' = (Num e, CountableDimensions n m, FromLists e (Normalize (m, n)) m,
CountableN (m, n)) =>
Matrix e (Normalize (m, n)) m
forall e m k.
(Num e, CountableDimensions k m, FromLists e (Normalize (m, k)) m,
CountableN (m, k)) =>
Matrix e (Normalize (m, k)) m
fstM @e @m @n
sndM' :: Matrix e (Normalize (m, n)) n
sndM' = (Num e, CountableDimensions n m, FromLists e (Normalize (m, n)) n,
CountableN (m, n)) =>
Matrix e (Normalize (m, n)) n
forall e m k.
(Num e, CountableDimensions k m, FromLists e (Normalize (m, k)) k,
CountableN (m, k)) =>
Matrix e (Normalize (m, k)) k
sndM @e @m @n
in Matrix e (FromNat (Count m * Count n)) p
-> Matrix e (FromNat (Count m * Count n)) q
-> Matrix e (FromNat (Count m * Count n)) (Normalize (p, q))
forall e cols a b.
(Num e, CountableDimensions a b, CountableN (a, b),
FromLists e (Normalize (a, b)) a,
FromLists e (Normalize (a, b)) b) =>
Matrix e cols a
-> Matrix e cols b -> Matrix e cols (Normalize (a, b))
kr (Matrix e m p
a Matrix e m p
-> Matrix e (FromNat (Count m * Count n)) m
-> Matrix e (FromNat (Count m * Count n)) p
forall (k :: Type -> Type -> Type) b c a.
Category k =>
k b c -> k a b -> k a c
. Matrix e (Normalize (m, n)) m
Matrix e (FromNat (Count m * Count n)) m
fstM') (Matrix e n q
b Matrix e n q
-> Matrix e (FromNat (Count m * Count n)) n
-> Matrix e (FromNat (Count m * Count n)) q
forall (k :: Type -> Type -> Type) b c a.
Category k =>
k b c -> k a b -> k a c
. Matrix e (Normalize (m, n)) n
Matrix e (FromNat (Count m * Count n)) n
sndM')
abideJF :: Matrix e cols rows -> Matrix e cols rows
abideJF :: Matrix e cols rows -> Matrix e cols rows
abideJF (Join (Fork a :: Matrix e a a
a c :: Matrix e a b
c) (Fork b :: Matrix e b a
b d :: Matrix e b b
d)) = Matrix e (Either a b) a
-> Matrix e (Either a b) b -> Matrix e (Either a b) (Either a b)
forall e cols a b.
Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
Fork (Matrix e a a -> Matrix e b a -> Matrix e (Either a b) a
forall e a rows b.
Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
Join (Matrix e a a -> Matrix e a a
forall e cols rows. Matrix e cols rows -> Matrix e cols rows
abideJF Matrix e a a
a) (Matrix e b a -> Matrix e b a
forall e cols rows. Matrix e cols rows -> Matrix e cols rows
abideJF Matrix e b a
b)) (Matrix e a b -> Matrix e b b -> Matrix e (Either a b) b
forall e a rows b.
Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
Join (Matrix e a b -> Matrix e a b
forall e cols rows. Matrix e cols rows -> Matrix e cols rows
abideJF Matrix e a b
c) (Matrix e b b -> Matrix e b b
forall e cols rows. Matrix e cols rows -> Matrix e cols rows
abideJF Matrix e b b
d))
abideJF Empty = Matrix e cols rows
forall e. Matrix e Void Void
Empty
abideJF (One e :: e
e) = e -> Matrix e () ()
forall e. e -> Matrix e () ()
One e
e
abideJF (Join a :: Matrix e a rows
a b :: Matrix e b rows
b) = Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
forall e a rows b.
Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
Join (Matrix e a rows -> Matrix e a rows
forall e cols rows. Matrix e cols rows -> Matrix e cols rows
abideJF Matrix e a rows
a) (Matrix e b rows -> Matrix e b rows
forall e cols rows. Matrix e cols rows -> Matrix e cols rows
abideJF Matrix e b rows
b)
abideJF (Fork a :: Matrix e cols a
a b :: Matrix e cols b
b) = Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
forall e cols a b.
Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
Fork (Matrix e cols a -> Matrix e cols a
forall e cols rows. Matrix e cols rows -> Matrix e cols rows
abideJF Matrix e cols a
a) (Matrix e cols b -> Matrix e cols b
forall e cols rows. Matrix e cols rows -> Matrix e cols rows
abideJF Matrix e cols b
b)
abideFJ :: Matrix e cols rows -> Matrix e cols rows
abideFJ :: Matrix e cols rows -> Matrix e cols rows
abideFJ (Fork (Join a :: Matrix e a a
a b :: Matrix e b a
b) (Join c :: Matrix e a b
c d :: Matrix e b b
d)) = Matrix e a (Either a b)
-> Matrix e b (Either a b) -> Matrix e (Either a b) (Either a b)
forall e a rows b.
Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
Join (Matrix e a a -> Matrix e a b -> Matrix e a (Either a b)
forall e cols a b.
Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
Fork (Matrix e a a -> Matrix e a a
forall e cols rows. Matrix e cols rows -> Matrix e cols rows
abideFJ Matrix e a a
a) (Matrix e a b -> Matrix e a b
forall e cols rows. Matrix e cols rows -> Matrix e cols rows
abideFJ Matrix e a b
c)) (Matrix e b a -> Matrix e b b -> Matrix e b (Either a b)
forall e cols a b.
Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
Fork (Matrix e b a -> Matrix e b a
forall e cols rows. Matrix e cols rows -> Matrix e cols rows
abideFJ Matrix e b a
b) (Matrix e b b -> Matrix e b b
forall e cols rows. Matrix e cols rows -> Matrix e cols rows
abideFJ Matrix e b b
d))
abideFJ Empty = Matrix e cols rows
forall e. Matrix e Void Void
Empty
abideFJ (One e :: e
e) = e -> Matrix e () ()
forall e. e -> Matrix e () ()
One e
e
abideFJ (Join a :: Matrix e a rows
a b :: Matrix e b rows
b) = Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
forall e a rows b.
Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
Join (Matrix e a rows -> Matrix e a rows
forall e cols rows. Matrix e cols rows -> Matrix e cols rows
abideFJ Matrix e a rows
a) (Matrix e b rows -> Matrix e b rows
forall e cols rows. Matrix e cols rows -> Matrix e cols rows
abideFJ Matrix e b rows
b)
abideFJ (Fork a :: Matrix e cols a
a b :: Matrix e cols b
b) = Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
forall e cols a b.
Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
Fork (Matrix e cols a -> Matrix e cols a
forall e cols rows. Matrix e cols rows -> Matrix e cols rows
abideFJ Matrix e cols a
a) (Matrix e cols b -> Matrix e cols b
forall e cols rows. Matrix e cols rows -> Matrix e cols rows
abideFJ Matrix e cols b
b)
tr :: Matrix e cols rows -> Matrix e rows cols
tr :: Matrix e cols rows -> Matrix e rows cols
tr Empty = Matrix e rows cols
forall e. Matrix e Void Void
Empty
tr (One e :: e
e) = e -> Matrix e () ()
forall e. e -> Matrix e () ()
One e
e
tr (Join a :: Matrix e a rows
a b :: Matrix e b rows
b) = Matrix e rows a -> Matrix e rows b -> Matrix e rows (Either a b)
forall e cols a b.
Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
Fork (Matrix e a rows -> Matrix e rows a
forall e cols rows. Matrix e cols rows -> Matrix e rows cols
tr Matrix e a rows
a) (Matrix e b rows -> Matrix e rows b
forall e cols rows. Matrix e cols rows -> Matrix e rows cols
tr Matrix e b rows
b)
tr (Fork a :: Matrix e cols a
a b :: Matrix e cols b
b) = Matrix e a cols -> Matrix e b cols -> Matrix e (Either a b) cols
forall e a rows b.
Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
Join (Matrix e cols a -> Matrix e a cols
forall e cols rows. Matrix e cols rows -> Matrix e rows cols
tr Matrix e cols a
a) (Matrix e cols b -> Matrix e b cols
forall e cols rows. Matrix e cols rows -> Matrix e rows cols
tr Matrix e cols b
b)
select :: (Num e, FromLists e b b, Countable b) => Matrix e cols (Either a b) -> Matrix e a b -> Matrix e cols b
select :: Matrix e cols (Either a b) -> Matrix e a b -> Matrix e cols b
select (Fork a :: Matrix e cols a
a b :: Matrix e cols b
b) y :: Matrix e a b
y = Matrix e a b
y Matrix e a b -> Matrix e cols a -> Matrix e cols b
forall (k :: Type -> Type -> Type) b c a.
Category k =>
k b c -> k a b -> k a c
. Matrix e cols a
Matrix e cols a
a Matrix e cols b -> Matrix e cols b -> Matrix e cols b
forall a. Num a => a -> a -> a
+ Matrix e cols b
Matrix e cols b
b
select (Join (Fork a :: Matrix e a a
a c :: Matrix e a b
c) (Fork b :: Matrix e b a
b d :: Matrix e b b
d)) y :: Matrix e a b
y = Matrix e a b -> Matrix e b b -> Matrix e (Either a b) b
forall e a rows b.
Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
join (Matrix e a b
y Matrix e a b -> Matrix e a a -> Matrix e a b
forall (k :: Type -> Type -> Type) b c a.
Category k =>
k b c -> k a b -> k a c
. Matrix e a a
Matrix e a a
a Matrix e a b -> Matrix e a b -> Matrix e a b
forall a. Num a => a -> a -> a
+ Matrix e a b
Matrix e a b
c) (Matrix e a b
y Matrix e a b -> Matrix e b a -> Matrix e b b
forall (k :: Type -> Type -> Type) b c a.
Category k =>
k b c -> k a b -> k a c
. Matrix e b a
Matrix e b a
b Matrix e b b -> Matrix e b b -> Matrix e b b
forall a. Num a => a -> a -> a
+ Matrix e b b
Matrix e b b
d)
select m :: Matrix e cols (Either a b)
m y :: Matrix e a b
y = Matrix e a b -> Matrix e b b -> Matrix e (Either a b) b
forall e a rows b.
Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
join Matrix e a b
y Matrix e b b
forall (k :: Type -> Type -> Type) a.
(Category k, Object k a) =>
k a a
id Matrix e (Either a b) b
-> Matrix e cols (Either a b) -> Matrix e cols b
forall (k :: Type -> Type -> Type) b c a.
Category k =>
k b c -> k a b -> k a c
. Matrix e cols (Either a b)
m
branch ::
( Num e,
CountableDimensions a b,
CountableDimensions c (Either b c),
FromLists e c b,
FromLists e a b,
FromLists e a a,
FromLists e b b,
FromLists e c c,
FromLists e b a,
FromLists e b c,
FromLists e (Either b c) b,
FromLists e (Either b c) c
)
=> Matrix e cols (Either a b) -> Matrix e a c -> Matrix e b c -> Matrix e cols c
branch :: Matrix e cols (Either a b)
-> Matrix e a c -> Matrix e b c -> Matrix e cols c
branch x :: Matrix e cols (Either a b)
x l :: Matrix e a c
l r :: Matrix e b c
r = Matrix e cols (Either a b) -> Matrix e cols (Either a (Either b c))
forall e a b c cols.
(Num e, Countable a, CountableDimensions b c, FromLists e a b,
FromLists e c b, FromLists e b b, FromLists e b a,
FromLists e a a) =>
Matrix e cols (Either a b) -> Matrix e cols (Either a (Either b c))
f Matrix e cols (Either a b)
x Matrix e cols (Either a (Either b c))
-> Matrix e a (Either b c) -> Matrix e cols (Either b c)
forall e b cols a.
(Num e, FromLists e b b, Countable b) =>
Matrix e cols (Either a b) -> Matrix e a b -> Matrix e cols b
`select` Matrix e a c -> Matrix e a (Either b c)
forall e b c a.
(Num e, CountableDimensions b c, FromLists e b c,
FromLists e c c) =>
Matrix e a c -> Matrix e a (Either b c)
g Matrix e a c
l Matrix e cols (Either b c) -> Matrix e b c -> Matrix e cols c
forall e b cols a.
(Num e, FromLists e b b, Countable b) =>
Matrix e cols (Either a b) -> Matrix e a b -> Matrix e cols b
`select` Matrix e b c
r
where
f :: (Num e, Countable a, CountableDimensions b c, FromLists e a b, FromLists e c b, FromLists e b b, FromLists e b a, FromLists e a a)
=> Matrix e cols (Either a b) -> Matrix e cols (Either a (Either b c))
f :: Matrix e cols (Either a b) -> Matrix e cols (Either a (Either b c))
f m :: Matrix e cols (Either a b)
m = Matrix e (Either a b) a
-> Matrix e (Either a b) (Either b c)
-> Matrix e (Either a b) (Either a (Either b c))
forall e cols a b.
Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
fork (Matrix e a (Either a b) -> Matrix e (Either a b) a
forall e cols rows. Matrix e cols rows -> Matrix e rows cols
tr Matrix e a (Either a b)
forall e n m.
(Num e, CountableDimensions n m, FromLists e n m,
FromLists e m m) =>
Matrix e m (Either m n)
i1) (Matrix e b (Either b c)
forall e n m.
(Num e, CountableDimensions n m, FromLists e n m,
FromLists e m m) =>
Matrix e m (Either m n)
i1 Matrix e b (Either b c)
-> Matrix e (Either a b) b -> Matrix e (Either a b) (Either b c)
forall (k :: Type -> Type -> Type) b c a.
Category k =>
k b c -> k a b -> k a c
. Matrix e b (Either a b) -> Matrix e (Either a b) b
forall e cols rows. Matrix e cols rows -> Matrix e rows cols
tr Matrix e b (Either a b)
forall e n m.
(Num e, CountableDimensions n m, FromLists e m n,
FromLists e n n) =>
Matrix e n (Either m n)
i2) Matrix e (Either a b) (Either a (Either b c))
-> Matrix e cols (Either a b)
-> Matrix e cols (Either a (Either b c))
forall (k :: Type -> Type -> Type) b c a.
Category k =>
k b c -> k a b -> k a c
. Matrix e cols (Either a b)
m
g :: (Num e, CountableDimensions b c, FromLists e b c, FromLists e c c) => Matrix e a c -> Matrix e a (Either b c)
g :: Matrix e a c -> Matrix e a (Either b c)
g m :: Matrix e a c
m = Matrix e c (Either b c)
forall e n m.
(Num e, CountableDimensions n m, FromLists e m n,
FromLists e n n) =>
Matrix e n (Either m n)
i2 Matrix e c (Either b c) -> Matrix e a c -> Matrix e a (Either b c)
forall (k :: Type -> Type -> Type) b c a.
Category k =>
k b c -> k a b -> k a c
. Matrix e a c
m
cond ::
( Trivial cols,
Countable cols,
FromLists e () cols,
FromLists e cols (),
FromLists e cols cols,
Bounded a,
Enum a,
Num e,
Ord e
)
=>
(a -> Bool) -> Matrix e cols rows -> Matrix e cols rows -> Matrix e cols rows
cond :: (a -> Bool)
-> Matrix e cols rows -> Matrix e cols rows -> Matrix e cols rows
cond p :: a -> Bool
p f :: Matrix e cols rows
f g :: Matrix e cols rows
g = Matrix e cols rows
-> Matrix e cols rows -> Matrix e (Either cols cols) rows
forall e a rows b.
Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
join Matrix e cols rows
f Matrix e cols rows
g Matrix e (Either cols cols) rows
-> Matrix e cols (Either cols cols) -> Matrix e cols rows
forall (k :: Type -> Type -> Type) b c a.
Category k =>
k b c -> k a b -> k a c
. (a -> Bool) -> Matrix e cols (Either cols cols)
forall q e a.
(Trivial q, Countable q, FromLists e () q, FromLists e q (),
FromLists e q q, Bounded a, Enum a, Num e, Ord e) =>
(a -> Bool) -> Matrix e q (Either q q)
grd a -> Bool
p
grd ::
( Trivial q,
Countable q,
FromLists e () q,
FromLists e q (),
FromLists e q q,
Bounded a,
Enum a,
Num e,
Ord e
)
=>
(a -> Bool) -> Matrix e q (Either q q)
grd :: (a -> Bool) -> Matrix e q (Either q q)
grd f :: a -> Bool
f = Matrix e q q -> Matrix e q q -> Matrix e q (Either q q)
forall e cols a b.
Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
fork ((a -> Bool) -> Matrix e q q
forall e a q.
(Trivial q, Countable q, FromLists e () q, FromLists e q (),
FromLists e q q, Liftable e a Bool) =>
(a -> Bool) -> Matrix e q q
corr a -> Bool
f) ((a -> Bool) -> Matrix e q q
forall e a q.
(Trivial q, Countable q, FromLists e () q, FromLists e q (),
FromLists e q q, Liftable e a Bool) =>
(a -> Bool) -> Matrix e q q
corr (Bool -> Bool
not (Bool -> Bool) -> (a -> Bool) -> a -> Bool
forall (k :: Type -> Type -> Type) b c a.
Category k =>
k b c -> k a b -> k a c
. a -> Bool
f))
corr ::
forall e a q .
( Trivial q,
Countable q,
FromLists e () q,
FromLists e q (),
FromLists e q q,
Liftable e a Bool
)
=> (a -> Bool) -> Matrix e q q
corr :: (a -> Bool) -> Matrix e q q
corr p :: a -> Bool
p = let f :: Matrix e q ()
f = (a -> Bool) -> Matrix e q ()
forall a b cols rows e.
(Liftable e a b, CountableDimensions cols rows,
FromLists e rows cols) =>
(a -> b) -> Matrix e cols rows
fromF' a -> Bool
p :: Matrix e q ()
in Matrix e q () -> Matrix e q q -> Matrix e q (Normalize ((), q))
forall e cols a b.
(Num e, CountableDimensions a b, CountableN (a, b),
FromLists e (Normalize (a, b)) a,
FromLists e (Normalize (a, b)) b) =>
Matrix e cols a
-> Matrix e cols b -> Matrix e cols (Normalize (a, b))
kr Matrix e q ()
f (Matrix e q q
forall (k :: Type -> Type -> Type) a.
(Category k, Object k a) =>
k a a
id :: Matrix e q q)
prettyAux :: Show e => [[e]] -> [[e]] -> String
prettyAux :: [[e]] -> [[e]] -> String
prettyAux [] _ = ""
prettyAux [[e :: e
e]] m :: [[e]]
m = "│ " String -> ShowS
forall a. [a] -> [a] -> [a]
++ ShowS
fill (e -> String
forall a. Show a => a -> String
show e
e) String -> ShowS
forall a. [a] -> [a] -> [a]
++ " │\n"
where
v :: [String]
v = ([e] -> String) -> [[e]] -> [String]
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap [e] -> String
forall a. Show a => a -> String
show [[e]]
m
widest :: Int
widest = [Int] -> Int
forall (t :: Type -> Type) a. (Foldable t, Ord a) => t a -> a
maximum ([Int] -> Int) -> [Int] -> Int
forall a b. (a -> b) -> a -> b
$ (String -> Int) -> [String] -> [Int]
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap String -> Int
forall (t :: Type -> Type) a. Foldable t => t a -> Int
length [String]
v
fill :: ShowS
fill str :: String
str = Int -> Char -> String
forall a. Int -> a -> [a]
replicate (Int
widest Int -> Int -> Int
forall a. Num a => a -> a -> a
- String -> Int
forall (t :: Type -> Type) a. Foldable t => t a -> Int
length String
str Int -> Int -> Int
forall a. Num a => a -> a -> a
- 2) ' ' String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
str
prettyAux [h :: [e]
h] m :: [[e]]
m = "│ " String -> ShowS
forall a. [a] -> [a] -> [a]
++ ShowS
fill ([String] -> String
unwords ([String] -> String) -> [String] -> String
forall a b. (a -> b) -> a -> b
$ (e -> String) -> [e] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map e -> String
forall a. Show a => a -> String
show [e]
h) String -> ShowS
forall a. [a] -> [a] -> [a]
++ " │\n"
where
v :: [String]
v = ([e] -> String) -> [[e]] -> [String]
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap [e] -> String
forall a. Show a => a -> String
show [[e]]
m
widest :: Int
widest = [Int] -> Int
forall (t :: Type -> Type) a. (Foldable t, Ord a) => t a -> a
maximum ([Int] -> Int) -> [Int] -> Int
forall a b. (a -> b) -> a -> b
$ (String -> Int) -> [String] -> [Int]
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap String -> Int
forall (t :: Type -> Type) a. Foldable t => t a -> Int
length [String]
v
fill :: ShowS
fill str :: String
str = Int -> Char -> String
forall a. Int -> a -> [a]
replicate (Int
widest Int -> Int -> Int
forall a. Num a => a -> a -> a
- String -> Int
forall (t :: Type -> Type) a. Foldable t => t a -> Int
length String
str Int -> Int -> Int
forall a. Num a => a -> a -> a
- 2) ' ' String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
str
prettyAux (h :: [e]
h : t :: [[e]]
t) l :: [[e]]
l = "│ " String -> ShowS
forall a. [a] -> [a] -> [a]
++ ShowS
fill ([String] -> String
unwords ([String] -> String) -> [String] -> String
forall a b. (a -> b) -> a -> b
$ (e -> String) -> [e] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map e -> String
forall a. Show a => a -> String
show [e]
h) String -> ShowS
forall a. [a] -> [a] -> [a]
++ " │\n" String -> ShowS
forall a. [a] -> [a] -> [a]
++
[[e]] -> [[e]] -> String
forall e. Show e => [[e]] -> [[e]] -> String
prettyAux [[e]]
t [[e]]
l
where
v :: [String]
v = ([e] -> String) -> [[e]] -> [String]
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap [e] -> String
forall a. Show a => a -> String
show [[e]]
l
widest :: Int
widest = [Int] -> Int
forall (t :: Type -> Type) a. (Foldable t, Ord a) => t a -> a
maximum ([Int] -> Int) -> [Int] -> Int
forall a b. (a -> b) -> a -> b
$ (String -> Int) -> [String] -> [Int]
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap String -> Int
forall (t :: Type -> Type) a. Foldable t => t a -> Int
length [String]
v
fill :: ShowS
fill str :: String
str = Int -> Char -> String
forall a. Int -> a -> [a]
replicate (Int
widest Int -> Int -> Int
forall a. Num a => a -> a -> a
- String -> Int
forall (t :: Type -> Type) a. Foldable t => t a -> Int
length String
str Int -> Int -> Int
forall a. Num a => a -> a -> a
- 2) ' ' String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
str
pretty :: (CountableDimensions cols rows, Show e) => Matrix e cols rows -> String
pretty :: Matrix e cols rows -> String
pretty m :: Matrix e cols rows
m = [String] -> String
forall (t :: Type -> Type) a. Foldable t => t [a] -> [a]
concat
[ "┌ ", [String] -> String
unwords (Int -> String -> [String]
forall a. Int -> a -> [a]
replicate (Matrix e cols rows -> Int
forall e cols rows. Countable cols => Matrix e cols rows -> Int
columns Matrix e cols rows
m) String
blank), " ┐\n"
, [String] -> String
unlines
[ "│ " String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
unwords ((Int -> String) -> [Int] -> [String]
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap (\j :: Int
j -> ShowS
fill ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$ e -> String
forall a. Show a => a -> String
show (e -> String) -> e -> String
forall a b. (a -> b) -> a -> b
$ Int -> Int -> Matrix e cols rows -> e
forall rows cols a.
(KnownNat (Count rows), KnownNat (Count cols)) =>
Int -> Int -> Matrix a cols rows -> a
getElem Int
i Int
j Matrix e cols rows
m) [1..Matrix e cols rows -> Int
forall e cols rows. Countable cols => Matrix e cols rows -> Int
columns Matrix e cols rows
m]) String -> ShowS
forall a. [a] -> [a] -> [a]
++ " │" | Int
i <- [1..Matrix e cols rows -> Int
forall e cols rows. Countable rows => Matrix e cols rows -> Int
rows Matrix e cols rows
m] ]
, "└ ", [String] -> String
unwords (Int -> String -> [String]
forall a. Int -> a -> [a]
replicate (Matrix e cols rows -> Int
forall e cols rows. Countable cols => Matrix e cols rows -> Int
columns Matrix e cols rows
m) String
blank), " ┘"
]
where
strings :: [String]
strings = (e -> String) -> [e] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map e -> String
forall a. Show a => a -> String
show (Matrix e cols rows -> [e]
forall e cols rows. Matrix e cols rows -> [e]
toList Matrix e cols rows
m)
widest :: Int
widest = [Int] -> Int
forall (t :: Type -> Type) a. (Foldable t, Ord a) => t a -> a
maximum ([Int] -> Int) -> [Int] -> Int
forall a b. (a -> b) -> a -> b
$ (String -> Int) -> [String] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map String -> Int
forall (t :: Type -> Type) a. Foldable t => t a -> Int
length [String]
strings
fill :: ShowS
fill str :: String
str = Int -> Char -> String
forall a. Int -> a -> [a]
replicate (Int
widest Int -> Int -> Int
forall a. Num a => a -> a -> a
- String -> Int
forall (t :: Type -> Type) a. Foldable t => t a -> Int
length String
str) ' ' String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
str
blank :: String
blank = ShowS
fill ""
safeGet :: Int -> Int -> Matrix a cols rows -> Maybe a
safeGet i :: Int
i j :: Int
j m :: Matrix a cols rows
m
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Matrix a cols rows -> Int
forall e cols rows. Countable rows => Matrix e cols rows -> Int
rows Matrix a cols rows
m Bool -> Bool -> Bool
|| Int
j Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Matrix a cols rows -> Int
forall e cols rows. Countable cols => Matrix e cols rows -> Int
columns Matrix a cols rows
m Bool -> Bool -> Bool
|| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< 1 Bool -> Bool -> Bool
|| Int
j Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< 1 = Maybe a
forall a. Maybe a
Nothing
| Bool
otherwise = a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> a -> Maybe a
forall a b. (a -> b) -> a -> b
$ Int -> Int -> Matrix a cols rows -> [a] -> a
forall cols e rows a.
KnownNat (Count cols) =>
Int -> Int -> Matrix e cols rows -> [a] -> a
unsafeGet Int
i Int
j Matrix a cols rows
m (Matrix a cols rows -> [a]
forall e cols rows. Matrix e cols rows -> [e]
toList Matrix a cols rows
m)
unsafeGet :: Int -> Int -> Matrix e cols rows -> [a] -> a
unsafeGet i :: Int
i j :: Int
j m :: Matrix e cols rows
m l :: [a]
l = [a]
l [a] -> Int -> a
forall a. [a] -> Int -> a
!! Int -> (Int, Int) -> Int
forall a. Num a => a -> (a, a) -> a
encode (Matrix e cols rows -> Int
forall e cols rows. Countable cols => Matrix e cols rows -> Int
columns Matrix e cols rows
m) (Int
i,Int
j)
encode :: a -> (a, a) -> a
encode m :: a
m (i :: a
i,j :: a
j) = (a
ia -> a -> a
forall a. Num a => a -> a -> a
-1)a -> a -> a
forall a. Num a => a -> a -> a
*a
m a -> a -> a
forall a. Num a => a -> a -> a
+ a
j a -> a -> a
forall a. Num a => a -> a -> a
- 1
getElem :: Int -> Int -> Matrix a cols rows -> a
getElem i :: Int
i j :: Int
j m :: Matrix a cols rows
m =
a -> Maybe a -> a
forall a. a -> Maybe a -> a
fromMaybe
(String -> a
forall a. HasCallStack => String -> a
error (String -> a) -> String -> a
forall a b. (a -> b) -> a -> b
$
"getElem: Trying to get the "
String -> ShowS
forall a. [a] -> [a] -> [a]
++ (Int, Int) -> String
forall a. Show a => a -> String
show (Int
i, Int
j)
String -> ShowS
forall a. [a] -> [a] -> [a]
++ " element from a "
String -> ShowS
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show (Matrix a cols rows -> Int
forall e cols rows. Countable rows => Matrix e cols rows -> Int
rows Matrix a cols rows
m) String -> ShowS
forall a. [a] -> [a] -> [a]
++ "x" String -> ShowS
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show (Matrix a cols rows -> Int
forall e cols rows. Countable cols => Matrix e cols rows -> Int
columns Matrix a cols rows
m)
String -> ShowS
forall a. [a] -> [a] -> [a]
++ " matrix."
)
(Int -> Int -> Matrix a cols rows -> Maybe a
forall rows cols a.
(KnownNat (Count rows), KnownNat (Count cols)) =>
Int -> Int -> Matrix a cols rows -> Maybe a
safeGet Int
i Int
j Matrix a cols rows
m)
prettyPrint :: (CountableDimensions cols rows, Show e) => Matrix e cols rows -> IO ()
prettyPrint :: Matrix e cols rows -> IO ()
prettyPrint = String -> IO ()
putStrLn (String -> IO ())
-> (Matrix e cols rows -> String) -> Matrix e cols rows -> IO ()
forall (k :: Type -> Type -> Type) b c a.
Category k =>
k b c -> k a b -> k a c
. Matrix e cols rows -> String
forall cols rows e.
(CountableDimensions cols rows, Show e) =>
Matrix e cols rows -> String
pretty
zipWithM :: (e -> f -> g) -> Matrix e cols rows -> Matrix f cols rows -> Matrix g cols rows
zipWithM :: (e -> f -> g)
-> Matrix e cols rows -> Matrix f cols rows -> Matrix g cols rows
zipWithM _ Empty Empty = Matrix g cols rows
forall e. Matrix e Void Void
Empty
zipWithM f :: e -> f -> g
f (One a :: e
a) (One b :: f
b) = g -> Matrix g () ()
forall e. e -> Matrix e () ()
One (e -> f -> g
f e
a f
b)
zipWithM f :: e -> f -> g
f (Join a :: Matrix e a rows
a b :: Matrix e b rows
b) (Join c :: Matrix f a rows
c d :: Matrix f b rows
d) = Matrix g a rows -> Matrix g b rows -> Matrix g (Either a b) rows
forall e a rows b.
Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
Join ((e -> f -> g)
-> Matrix e a rows -> Matrix f a rows -> Matrix g a rows
forall e f g cols rows.
(e -> f -> g)
-> Matrix e cols rows -> Matrix f cols rows -> Matrix g cols rows
zipWithM e -> f -> g
f Matrix e a rows
a Matrix f a rows
Matrix f a rows
c) ((e -> f -> g)
-> Matrix e b rows -> Matrix f b rows -> Matrix g b rows
forall e f g cols rows.
(e -> f -> g)
-> Matrix e cols rows -> Matrix f cols rows -> Matrix g cols rows
zipWithM e -> f -> g
f Matrix e b rows
b Matrix f b rows
Matrix f b rows
d)
zipWithM f :: e -> f -> g
f (Fork a :: Matrix e cols a
a b :: Matrix e cols b
b) (Fork c :: Matrix f cols a
c d :: Matrix f cols b
d) = Matrix g cols a -> Matrix g cols b -> Matrix g cols (Either a b)
forall e cols a b.
Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
Fork ((e -> f -> g)
-> Matrix e cols a -> Matrix f cols a -> Matrix g cols a
forall e f g cols rows.
(e -> f -> g)
-> Matrix e cols rows -> Matrix f cols rows -> Matrix g cols rows
zipWithM e -> f -> g
f Matrix e cols a
a Matrix f cols a
Matrix f cols a
c) ((e -> f -> g)
-> Matrix e cols b -> Matrix f cols b -> Matrix g cols b
forall e f g cols rows.
(e -> f -> g)
-> Matrix e cols rows -> Matrix f cols rows -> Matrix g cols rows
zipWithM e -> f -> g
f Matrix e cols b
b Matrix f cols b
Matrix f cols b
d)
zipWithM f :: e -> f -> g
f x :: Matrix e cols rows
x@(Fork _ _) y :: Matrix f cols rows
y@(Join _ _) = (e -> f -> g)
-> Matrix e cols rows -> Matrix f cols rows -> Matrix g cols rows
forall e f g cols rows.
(e -> f -> g)
-> Matrix e cols rows -> Matrix f cols rows -> Matrix g cols rows
zipWithM e -> f -> g
f Matrix e cols rows
x (Matrix f cols rows -> Matrix f cols rows
forall e cols rows. Matrix e cols rows -> Matrix e cols rows
abideJF Matrix f cols rows
y)
zipWithM f :: e -> f -> g
f x :: Matrix e cols rows
x@(Join _ _) y :: Matrix f cols rows
y@(Fork _ _) = (e -> f -> g)
-> Matrix e cols rows -> Matrix f cols rows -> Matrix g cols rows
forall e f g cols rows.
(e -> f -> g)
-> Matrix e cols rows -> Matrix f cols rows -> Matrix g cols rows
zipWithM e -> f -> g
f (Matrix e cols rows -> Matrix e cols rows
forall e cols rows. Matrix e cols rows -> Matrix e cols rows
abideJF Matrix e cols rows
x) Matrix f cols rows
y
type Boolean = Natural 0 1
type Relation a b = Matrix Boolean a b
toBool :: (Num e, Eq e) => e -> Bool
toBool :: e -> Bool
toBool n :: e
n
| e
n e -> e -> Bool
forall a. Eq a => a -> a -> Bool
== 0 = Bool
False
| e
n e -> e -> Bool
forall a. Eq a => a -> a -> Bool
== 1 = Bool
True
fromBool :: Bool -> Natural 0 1
fromBool :: Bool -> Natural 0 1
fromBool True = Int -> Natural 0 1
forall (n :: Nat) (m :: Nat).
(KnownNat n, KnownNat m) =>
Int -> Natural n m
nat 1
fromBool False = Int -> Natural 0 1
forall (n :: Nat) (m :: Nat).
(KnownNat n, KnownNat m) =>
Int -> Natural n m
nat 0
negateM :: Relation cols rows -> Relation cols rows
negateM :: Relation cols rows -> Relation cols rows
negateM Empty = Relation cols rows
forall e. Matrix e Void Void
Empty
negateM (One (Nat p :: Int
p)) = Natural 0 1 -> Matrix (Natural 0 1) () ()
forall e. e -> Matrix e () ()
One (Int -> Natural 0 1
forall (start :: Nat) (end :: Nat). Int -> Natural start end
Nat (Int -> Int
forall a. Num a => a -> a
negate Int
p))
negateM (Join a :: Matrix (Natural 0 1) a rows
a b :: Matrix (Natural 0 1) b rows
b) = Matrix (Natural 0 1) a rows
-> Matrix (Natural 0 1) b rows
-> Matrix (Natural 0 1) (Either a b) rows
forall e a rows b.
Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
Join (Matrix (Natural 0 1) a rows -> Matrix (Natural 0 1) a rows
forall cols rows. Relation cols rows -> Relation cols rows
negateM Matrix (Natural 0 1) a rows
a) (Matrix (Natural 0 1) b rows -> Matrix (Natural 0 1) b rows
forall cols rows. Relation cols rows -> Relation cols rows
negateM Matrix (Natural 0 1) b rows
b)
negateM (Fork a :: Matrix (Natural 0 1) cols a
a b :: Matrix (Natural 0 1) cols b
b) = Matrix (Natural 0 1) cols a
-> Matrix (Natural 0 1) cols b
-> Matrix (Natural 0 1) cols (Either a b)
forall e cols a b.
Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
Fork (Matrix (Natural 0 1) cols a -> Matrix (Natural 0 1) cols a
forall cols rows. Relation cols rows -> Relation cols rows
negateM Matrix (Natural 0 1) cols a
a) (Matrix (Natural 0 1) cols b -> Matrix (Natural 0 1) cols b
forall cols rows. Relation cols rows -> Relation cols rows
negateM Matrix (Natural 0 1) cols b
b)
orM :: Relation cols rows -> Relation cols rows -> Relation cols rows
orM :: Relation cols rows -> Relation cols rows -> Relation cols rows
orM Empty Empty = Relation cols rows
forall e. Matrix e Void Void
Empty
orM (One a :: Natural 0 1
a) (One b :: Natural 0 1
b) = Natural 0 1 -> Matrix (Natural 0 1) () ()
forall e. e -> Matrix e () ()
One (Bool -> Natural 0 1
fromBool (Natural 0 1 -> Bool
forall e. (Num e, Eq e) => e -> Bool
toBool Natural 0 1
a Bool -> Bool -> Bool
|| Natural 0 1 -> Bool
forall e. (Num e, Eq e) => e -> Bool
toBool Natural 0 1
b))
orM (Join a :: Matrix (Natural 0 1) a rows
a b :: Matrix (Natural 0 1) b rows
b) (Join c :: Matrix (Natural 0 1) a rows
c d :: Matrix (Natural 0 1) b rows
d) = Matrix (Natural 0 1) a rows
-> Matrix (Natural 0 1) b rows
-> Matrix (Natural 0 1) (Either a b) rows
forall e a rows b.
Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
Join (Matrix (Natural 0 1) a rows
-> Matrix (Natural 0 1) a rows -> Matrix (Natural 0 1) a rows
forall cols rows.
Relation cols rows -> Relation cols rows -> Relation cols rows
orM Matrix (Natural 0 1) a rows
a Matrix (Natural 0 1) a rows
Matrix (Natural 0 1) a rows
c) (Matrix (Natural 0 1) b rows
-> Matrix (Natural 0 1) b rows -> Matrix (Natural 0 1) b rows
forall cols rows.
Relation cols rows -> Relation cols rows -> Relation cols rows
orM Matrix (Natural 0 1) b rows
b Matrix (Natural 0 1) b rows
Matrix (Natural 0 1) b rows
d)
orM (Fork a :: Matrix (Natural 0 1) cols a
a b :: Matrix (Natural 0 1) cols b
b) (Fork c :: Matrix (Natural 0 1) cols a
c d :: Matrix (Natural 0 1) cols b
d) = Matrix (Natural 0 1) cols a
-> Matrix (Natural 0 1) cols b
-> Matrix (Natural 0 1) cols (Either a b)
forall e cols a b.
Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
Fork (Matrix (Natural 0 1) cols a
-> Matrix (Natural 0 1) cols a -> Matrix (Natural 0 1) cols a
forall cols rows.
Relation cols rows -> Relation cols rows -> Relation cols rows
orM Matrix (Natural 0 1) cols a
a Matrix (Natural 0 1) cols a
Matrix (Natural 0 1) cols a
c) (Matrix (Natural 0 1) cols b
-> Matrix (Natural 0 1) cols b -> Matrix (Natural 0 1) cols b
forall cols rows.
Relation cols rows -> Relation cols rows -> Relation cols rows
orM Matrix (Natural 0 1) cols b
b Matrix (Natural 0 1) cols b
Matrix (Natural 0 1) cols b
d)
orM x :: Relation cols rows
x@(Fork _ _) y :: Relation cols rows
y@(Join _ _) = Relation cols rows -> Relation cols rows -> Relation cols rows
forall cols rows.
Relation cols rows -> Relation cols rows -> Relation cols rows
orM Relation cols rows
x (Relation cols rows -> Relation cols rows
forall e cols rows. Matrix e cols rows -> Matrix e cols rows
abideJF Relation cols rows
y)
orM x :: Relation cols rows
x@(Join _ _) y :: Relation cols rows
y@(Fork _ _) = Relation cols rows -> Relation cols rows -> Relation cols rows
forall cols rows.
Relation cols rows -> Relation cols rows -> Relation cols rows
orM (Relation cols rows -> Relation cols rows
forall e cols rows. Matrix e cols rows -> Matrix e cols rows
abideJF Relation cols rows
x) Relation cols rows
y
andM :: Relation cols rows -> Relation cols rows -> Relation cols rows
andM :: Relation cols rows -> Relation cols rows -> Relation cols rows
andM Empty Empty = Relation cols rows
forall e. Matrix e Void Void
Empty
andM (One a :: Natural 0 1
a) (One b :: Natural 0 1
b) = Natural 0 1 -> Matrix (Natural 0 1) () ()
forall e. e -> Matrix e () ()
One (Bool -> Natural 0 1
fromBool (Natural 0 1 -> Bool
forall e. (Num e, Eq e) => e -> Bool
toBool Natural 0 1
a Bool -> Bool -> Bool
&& Natural 0 1 -> Bool
forall e. (Num e, Eq e) => e -> Bool
toBool Natural 0 1
b))
andM (Join a :: Matrix (Natural 0 1) a rows
a b :: Matrix (Natural 0 1) b rows
b) (Join c :: Matrix (Natural 0 1) a rows
c d :: Matrix (Natural 0 1) b rows
d) = Matrix (Natural 0 1) a rows
-> Matrix (Natural 0 1) b rows
-> Matrix (Natural 0 1) (Either a b) rows
forall e a rows b.
Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
Join (Matrix (Natural 0 1) a rows
-> Matrix (Natural 0 1) a rows -> Matrix (Natural 0 1) a rows
forall cols rows.
Relation cols rows -> Relation cols rows -> Relation cols rows
andM Matrix (Natural 0 1) a rows
a Matrix (Natural 0 1) a rows
Matrix (Natural 0 1) a rows
c) (Matrix (Natural 0 1) b rows
-> Matrix (Natural 0 1) b rows -> Matrix (Natural 0 1) b rows
forall cols rows.
Relation cols rows -> Relation cols rows -> Relation cols rows
andM Matrix (Natural 0 1) b rows
b Matrix (Natural 0 1) b rows
Matrix (Natural 0 1) b rows
d)
andM (Fork a :: Matrix (Natural 0 1) cols a
a b :: Matrix (Natural 0 1) cols b
b) (Fork c :: Matrix (Natural 0 1) cols a
c d :: Matrix (Natural 0 1) cols b
d) = Matrix (Natural 0 1) cols a
-> Matrix (Natural 0 1) cols b
-> Matrix (Natural 0 1) cols (Either a b)
forall e cols a b.
Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
Fork (Matrix (Natural 0 1) cols a
-> Matrix (Natural 0 1) cols a -> Matrix (Natural 0 1) cols a
forall cols rows.
Relation cols rows -> Relation cols rows -> Relation cols rows
andM Matrix (Natural 0 1) cols a
a Matrix (Natural 0 1) cols a
Matrix (Natural 0 1) cols a
c) (Matrix (Natural 0 1) cols b
-> Matrix (Natural 0 1) cols b -> Matrix (Natural 0 1) cols b
forall cols rows.
Relation cols rows -> Relation cols rows -> Relation cols rows
andM Matrix (Natural 0 1) cols b
b Matrix (Natural 0 1) cols b
Matrix (Natural 0 1) cols b
d)
andM x :: Relation cols rows
x@(Fork _ _) y :: Relation cols rows
y@(Join _ _) = Relation cols rows -> Relation cols rows -> Relation cols rows
forall cols rows.
Relation cols rows -> Relation cols rows -> Relation cols rows
andM Relation cols rows
x (Relation cols rows -> Relation cols rows
forall e cols rows. Matrix e cols rows -> Matrix e cols rows
abideJF Relation cols rows
y)
andM x :: Relation cols rows
x@(Join _ _) y :: Relation cols rows
y@(Fork _ _) = Relation cols rows -> Relation cols rows -> Relation cols rows
forall cols rows.
Relation cols rows -> Relation cols rows -> Relation cols rows
andM (Relation cols rows -> Relation cols rows
forall e cols rows. Matrix e cols rows -> Matrix e cols rows
abideJF Relation cols rows
x) Relation cols rows
y
subM :: Relation cols rows -> Relation cols rows -> Relation cols rows
subM :: Relation cols rows -> Relation cols rows -> Relation cols rows
subM Empty Empty = Relation cols rows
forall e. Matrix e Void Void
Empty
subM (One a :: Natural 0 1
a) (One b :: Natural 0 1
b) = if Natural 0 1
a Natural 0 1 -> Natural 0 1 -> Natural 0 1
forall a. Num a => a -> a -> a
- Natural 0 1
b Natural 0 1 -> Natural 0 1 -> Bool
forall a. Ord a => a -> a -> Bool
< Int -> Natural 0 1
forall (n :: Nat) (m :: Nat).
(KnownNat n, KnownNat m) =>
Int -> Natural n m
nat 0 then Natural 0 1 -> Matrix (Natural 0 1) () ()
forall e. e -> Matrix e () ()
One (Int -> Natural 0 1
forall (n :: Nat) (m :: Nat).
(KnownNat n, KnownNat m) =>
Int -> Natural n m
nat 0) else Natural 0 1 -> Matrix (Natural 0 1) () ()
forall e. e -> Matrix e () ()
One (Natural 0 1
a Natural 0 1 -> Natural 0 1 -> Natural 0 1
forall a. Num a => a -> a -> a
- Natural 0 1
b)
subM (Join a :: Matrix (Natural 0 1) a rows
a b :: Matrix (Natural 0 1) b rows
b) (Join c :: Matrix (Natural 0 1) a rows
c d :: Matrix (Natural 0 1) b rows
d) = Matrix (Natural 0 1) a rows
-> Matrix (Natural 0 1) b rows
-> Matrix (Natural 0 1) (Either a b) rows
forall e a rows b.
Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
Join (Matrix (Natural 0 1) a rows
-> Matrix (Natural 0 1) a rows -> Matrix (Natural 0 1) a rows
forall cols rows.
Relation cols rows -> Relation cols rows -> Relation cols rows
subM Matrix (Natural 0 1) a rows
a Matrix (Natural 0 1) a rows
Matrix (Natural 0 1) a rows
c) (Matrix (Natural 0 1) b rows
-> Matrix (Natural 0 1) b rows -> Matrix (Natural 0 1) b rows
forall cols rows.
Relation cols rows -> Relation cols rows -> Relation cols rows
subM Matrix (Natural 0 1) b rows
b Matrix (Natural 0 1) b rows
Matrix (Natural 0 1) b rows
d)
subM (Fork a :: Matrix (Natural 0 1) cols a
a b :: Matrix (Natural 0 1) cols b
b) (Fork c :: Matrix (Natural 0 1) cols a
c d :: Matrix (Natural 0 1) cols b
d) = Matrix (Natural 0 1) cols a
-> Matrix (Natural 0 1) cols b
-> Matrix (Natural 0 1) cols (Either a b)
forall e cols a b.
Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
Fork (Matrix (Natural 0 1) cols a
-> Matrix (Natural 0 1) cols a -> Matrix (Natural 0 1) cols a
forall cols rows.
Relation cols rows -> Relation cols rows -> Relation cols rows
subM Matrix (Natural 0 1) cols a
a Matrix (Natural 0 1) cols a
Matrix (Natural 0 1) cols a
c) (Matrix (Natural 0 1) cols b
-> Matrix (Natural 0 1) cols b -> Matrix (Natural 0 1) cols b
forall cols rows.
Relation cols rows -> Relation cols rows -> Relation cols rows
subM Matrix (Natural 0 1) cols b
b Matrix (Natural 0 1) cols b
Matrix (Natural 0 1) cols b
d)
subM x :: Relation cols rows
x@(Fork _ _) y :: Relation cols rows
y@(Join _ _) = Relation cols rows -> Relation cols rows -> Relation cols rows
forall cols rows.
Relation cols rows -> Relation cols rows -> Relation cols rows
subM Relation cols rows
x (Relation cols rows -> Relation cols rows
forall e cols rows. Matrix e cols rows -> Matrix e cols rows
abideJF Relation cols rows
y)
subM x :: Relation cols rows
x@(Join _ _) y :: Relation cols rows
y@(Fork _ _) = Relation cols rows -> Relation cols rows -> Relation cols rows
forall cols rows.
Relation cols rows -> Relation cols rows -> Relation cols rows
subM (Relation cols rows -> Relation cols rows
forall e cols rows. Matrix e cols rows -> Matrix e cols rows
abideJF Relation cols rows
x) Relation cols rows
y
compRel :: Relation cr rows -> Relation cols cr -> Relation cols rows
compRel :: Relation cr rows -> Relation cols cr -> Relation cols rows
compRel Empty Empty = Relation cols rows
forall e. Matrix e Void Void
Empty
compRel (One a :: Natural 0 1
a) (One b :: Natural 0 1
b) = Natural 0 1 -> Matrix (Natural 0 1) () ()
forall e. e -> Matrix e () ()
One (Bool -> Natural 0 1
fromBool (Natural 0 1 -> Bool
forall e. (Num e, Eq e) => e -> Bool
toBool Natural 0 1
a Bool -> Bool -> Bool
&& Natural 0 1 -> Bool
forall e. (Num e, Eq e) => e -> Bool
toBool Natural 0 1
b))
compRel (Join a :: Matrix (Natural 0 1) a rows
a b :: Matrix (Natural 0 1) b rows
b) (Fork c :: Matrix (Natural 0 1) cols a
c d :: Matrix (Natural 0 1) cols b
d) = Relation cols rows -> Relation cols rows -> Relation cols rows
forall cols rows.
Relation cols rows -> Relation cols rows -> Relation cols rows
orM (Matrix (Natural 0 1) a rows
-> Relation cols a -> Relation cols rows
forall cr rows cols.
Relation cr rows -> Relation cols cr -> Relation cols rows
compRel Matrix (Natural 0 1) a rows
a Relation cols a
Matrix (Natural 0 1) cols a
c) (Matrix (Natural 0 1) b rows
-> Relation cols b -> Relation cols rows
forall cr rows cols.
Relation cr rows -> Relation cols cr -> Relation cols rows
compRel Matrix (Natural 0 1) b rows
b Relation cols b
Matrix (Natural 0 1) cols b
d)
compRel (Fork a :: Matrix (Natural 0 1) cr a
a b :: Matrix (Natural 0 1) cr b
b) c :: Relation cols cr
c = Matrix (Natural 0 1) cols a
-> Matrix (Natural 0 1) cols b
-> Matrix (Natural 0 1) cols (Either a b)
forall e cols a b.
Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
Fork (Matrix (Natural 0 1) cr a
-> Relation cols cr -> Matrix (Natural 0 1) cols a
forall cr rows cols.
Relation cr rows -> Relation cols cr -> Relation cols rows
compRel Matrix (Natural 0 1) cr a
a Relation cols cr
c) (Matrix (Natural 0 1) cr b
-> Relation cols cr -> Matrix (Natural 0 1) cols b
forall cr rows cols.
Relation cr rows -> Relation cols cr -> Relation cols rows
compRel Matrix (Natural 0 1) cr b
b Relation cols cr
c)
compRel c :: Relation cr rows
c (Join a :: Matrix (Natural 0 1) a cr
a b :: Matrix (Natural 0 1) b cr
b) = Matrix (Natural 0 1) a rows
-> Matrix (Natural 0 1) b rows
-> Matrix (Natural 0 1) (Either a b) rows
forall e a rows b.
Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
Join (Relation cr rows
-> Matrix (Natural 0 1) a cr -> Matrix (Natural 0 1) a rows
forall cr rows cols.
Relation cr rows -> Relation cols cr -> Relation cols rows
compRel Relation cr rows
c Matrix (Natural 0 1) a cr
a) (Relation cr rows
-> Matrix (Natural 0 1) b cr -> Matrix (Natural 0 1) b rows
forall cr rows cols.
Relation cr rows -> Relation cols cr -> Relation cols rows
compRel Relation cr rows
c Matrix (Natural 0 1) b cr
b)
divR :: Relation b c -> Relation b a -> Relation a c
divR :: Relation b c -> Relation b a -> Relation a c
divR Empty Empty = Relation a c
forall e. Matrix e Void Void
Empty
divR (One a :: Natural 0 1
a) (One b :: Natural 0 1
b) = Natural 0 1 -> Matrix (Natural 0 1) () ()
forall e. e -> Matrix e () ()
One (Bool -> Natural 0 1
fromBool (Bool -> Bool
not (Natural 0 1 -> Bool
forall e. (Num e, Eq e) => e -> Bool
toBool Natural 0 1
b) Bool -> Bool -> Bool
|| Natural 0 1 -> Bool
forall e. (Num e, Eq e) => e -> Bool
toBool Natural 0 1
a))
divR (Join a :: Matrix (Natural 0 1) a c
a b :: Matrix (Natural 0 1) b c
b) (Join c :: Matrix (Natural 0 1) a a
c d :: Matrix (Natural 0 1) b a
d) = Relation a c -> Relation a c -> Relation a c
forall cols rows.
Relation cols rows -> Relation cols rows -> Relation cols rows
andM (Matrix (Natural 0 1) a c -> Relation a a -> Relation a c
forall b c a. Relation b c -> Relation b a -> Relation a c
divR Matrix (Natural 0 1) a c
a Relation a a
Matrix (Natural 0 1) a a
c) (Matrix (Natural 0 1) b c -> Relation b a -> Relation a c
forall b c a. Relation b c -> Relation b a -> Relation a c
divR Matrix (Natural 0 1) b c
b Relation b a
Matrix (Natural 0 1) b a
d)
divR (Fork a :: Matrix (Natural 0 1) b a
a b :: Matrix (Natural 0 1) b b
b) c :: Relation b a
c = Matrix (Natural 0 1) a a
-> Matrix (Natural 0 1) a b -> Matrix (Natural 0 1) a (Either a b)
forall e cols a b.
Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
Fork (Matrix (Natural 0 1) b a
-> Relation b a -> Matrix (Natural 0 1) a a
forall b c a. Relation b c -> Relation b a -> Relation a c
divR Matrix (Natural 0 1) b a
a Relation b a
c) (Matrix (Natural 0 1) b b
-> Relation b a -> Matrix (Natural 0 1) a b
forall b c a. Relation b c -> Relation b a -> Relation a c
divR Matrix (Natural 0 1) b b
b Relation b a
c)
divR c :: Relation b c
c (Fork a :: Matrix (Natural 0 1) b a
a b :: Matrix (Natural 0 1) b b
b) = Matrix (Natural 0 1) a c
-> Matrix (Natural 0 1) b c -> Matrix (Natural 0 1) (Either a b) c
forall e a rows b.
Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
Join (Relation b c
-> Matrix (Natural 0 1) b a -> Matrix (Natural 0 1) a c
forall b c a. Relation b c -> Relation b a -> Relation a c
divR Relation b c
c Matrix (Natural 0 1) b a
a) (Relation b c
-> Matrix (Natural 0 1) b b -> Matrix (Natural 0 1) b c
forall b c a. Relation b c -> Relation b a -> Relation a c
divR Relation b c
c Matrix (Natural 0 1) b b
b)
divL :: Relation c b -> Relation a b -> Relation a c
divL :: Relation c b -> Relation a b -> Relation a c
divL x :: Relation c b
x y :: Relation a b
y = Matrix (Natural 0 1) c a -> Relation a c
forall e cols rows. Matrix e cols rows -> Matrix e rows cols
tr (Relation b a -> Relation b c -> Matrix (Natural 0 1) c a
forall b c a. Relation b c -> Relation b a -> Relation a c
divR (Relation a b -> Relation b a
forall e cols rows. Matrix e cols rows -> Matrix e rows cols
tr Relation a b
y) (Relation c b -> Relation b c
forall e cols rows. Matrix e cols rows -> Matrix e rows cols
tr Relation c b
x))
divS :: Relation c a -> Relation b a -> Relation c b
divS :: Relation c a -> Relation b a -> Relation c b
divS s :: Relation c a
s r :: Relation b a
r = Relation b a -> Relation c a -> Relation c b
forall c b a. Relation c b -> Relation a b -> Relation a c
divL Relation b a
r Relation c a
s Relation c b -> Relation c b -> Relation c b
forall cols rows.
Relation cols rows -> Relation cols rows -> Relation cols rows
`intersection` Relation a b -> Relation a c -> Relation c b
forall b c a. Relation b c -> Relation b a -> Relation a c
divR (Relation b a -> Relation a b
forall e cols rows. Matrix e cols rows -> Matrix e rows cols
tr Relation b a
r) (Relation c a -> Relation a c
forall e cols rows. Matrix e cols rows -> Matrix e rows cols
tr Relation c a
s)
where
intersection :: Relation cols rows -> Relation cols rows -> Relation cols rows
intersection = Relation cols rows -> Relation cols rows -> Relation cols rows
forall cols rows.
Relation cols rows -> Relation cols rows -> Relation cols rows
andM
fromFRel' ::
forall a b cols rows.
( Liftable Boolean a b,
CountableDimensions cols rows,
FromLists Boolean rows cols
) =>
(a -> b) ->
Relation cols rows
fromFRel' :: (a -> b) -> Relation cols rows
fromFRel' f :: a -> b
f =
let minA :: a
minA = Bounded a => a
forall a. Bounded a => a
minBound @a
maxA :: a
maxA = Bounded a => a
forall a. Bounded a => a
maxBound @a
minB :: b
minB = Bounded b => b
forall a. Bounded a => a
minBound @b
maxB :: b
maxB = Bounded b => b
forall a. Bounded a => a
maxBound @b
ccols :: Int
ccols = Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Integer -> Int) -> Integer -> Int
forall a b. (a -> b) -> a -> b
$ Proxy (Count cols) -> Integer
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Integer
natVal (Proxy (Count cols)
forall k (t :: k). Proxy t
Proxy :: Proxy (Count cols))
rrows :: Int
rrows = Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Integer -> Int) -> Integer -> Int
forall a b. (a -> b) -> a -> b
$ Proxy (Count rows) -> Integer
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Integer
natVal (Proxy (Count rows)
forall k (t :: k). Proxy t
Proxy :: Proxy (Count rows))
elementsA :: [a]
elementsA = Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
take Int
ccols [a
minA .. a
maxA]
elementsB :: [b]
elementsB = Int -> [b] -> [b]
forall a. Int -> [a] -> [a]
take Int
rrows [b
minB .. b
maxB]
combinations :: [(a, b)]
combinations = (,) (a -> b -> (a, b)) -> [a] -> [b -> (a, b)]
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> [a]
elementsA [b -> (a, b)] -> [b] -> [(a, b)]
forall (f :: Type -> Type) a b.
Applicative f =>
f (a -> b) -> f a -> f b
<*> [b]
elementsB
combAp :: [Natural 0 1]
combAp = (((Int, Int), Natural 0 1) -> Natural 0 1)
-> [((Int, Int), Natural 0 1)] -> [Natural 0 1]
forall a b. (a -> b) -> [a] -> [b]
map ((Int, Int), Natural 0 1) -> Natural 0 1
forall a b. (a, b) -> b
snd ([((Int, Int), Natural 0 1)] -> [Natural 0 1])
-> ([((Int, Int), Natural 0 1)] -> [((Int, Int), Natural 0 1)])
-> [((Int, Int), Natural 0 1)]
-> [Natural 0 1]
forall (k :: Type -> Type -> Type) b c a.
Category k =>
k b c -> k a b -> k a c
. [((Int, Int), Natural 0 1)] -> [((Int, Int), Natural 0 1)]
forall a. Ord a => [a] -> [a]
sort ([((Int, Int), Natural 0 1)] -> [Natural 0 1])
-> ([(a, b)] -> [((Int, Int), Natural 0 1)])
-> [(a, b)]
-> [Natural 0 1]
forall (k :: Type -> Type -> Type) b c a.
Category k =>
k b c -> k a b -> k a c
. ((a, b) -> ((Int, Int), Natural 0 1))
-> [(a, b)] -> [((Int, Int), Natural 0 1)]
forall a b. (a -> b) -> [a] -> [b]
map (\(a :: a
a, b :: b
b) -> if a -> b
f a
a b -> b -> Bool
forall a. Eq a => a -> a -> Bool
== b
b
then ((a -> Int
forall a. Enum a => a -> Int
fromEnum a
a, b -> Int
forall a. Enum a => a -> Int
fromEnum b
b), Int -> Natural 0 1
forall (n :: Nat) (m :: Nat).
(KnownNat n, KnownNat m) =>
Int -> Natural n m
nat 1)
else ((a -> Int
forall a. Enum a => a -> Int
fromEnum a
a, b -> Int
forall a. Enum a => a -> Int
fromEnum b
b), Int -> Natural 0 1
forall (n :: Nat) (m :: Nat).
(KnownNat n, KnownNat m) =>
Int -> Natural n m
nat 0)) ([(a, b)] -> [Natural 0 1]) -> [(a, b)] -> [Natural 0 1]
forall a b. (a -> b) -> a -> b
$ [(a, b)]
combinations
mList :: [[Natural 0 1]]
mList = [Natural 0 1] -> Int -> [[Natural 0 1]]
forall a. [a] -> Int -> [[a]]
buildList [Natural 0 1]
combAp Int
rrows
in Matrix (Natural 0 1) rows cols -> Relation cols rows
forall e cols rows. Matrix e cols rows -> Matrix e rows cols
tr (Matrix (Natural 0 1) rows cols -> Relation cols rows)
-> Matrix (Natural 0 1) rows cols -> Relation cols rows
forall a b. (a -> b) -> a -> b
$ [[Natural 0 1]] -> Matrix (Natural 0 1) rows cols
forall e cols rows.
FromLists e cols rows =>
[[e]] -> Matrix e cols rows
fromLists [[Natural 0 1]]
mList
where
buildList :: [a] -> Int -> [[a]]
buildList [] _ = []
buildList l :: [a]
l r :: Int
r = Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
take Int
r [a]
l [a] -> [[a]] -> [[a]]
forall a. a -> [a] -> [a]
: [a] -> Int -> [[a]]
buildList (Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
drop Int
r [a]
l) Int
r
fromFRel ::
forall a b.
( Liftable Boolean a b,
CountableDimensionsN a b,
FromLists Boolean (Normalize b) (Normalize a)
) =>
(a -> b) ->
Relation (Normalize a) (Normalize b)
fromFRel :: (a -> b) -> Relation (Normalize a) (Normalize b)
fromFRel f :: a -> b
f =
let minA :: a
minA = Bounded a => a
forall a. Bounded a => a
minBound @a
maxA :: a
maxA = Bounded a => a
forall a. Bounded a => a
maxBound @a
minB :: b
minB = Bounded b => b
forall a. Bounded a => a
minBound @b
maxB :: b
maxB = Bounded b => b
forall a. Bounded a => a
maxBound @b
ccols :: Int
ccols = Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Integer -> Int) -> Integer -> Int
forall a b. (a -> b) -> a -> b
$ Proxy (Count (Normalize a)) -> Integer
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Integer
natVal (Proxy (Count (Normalize a))
forall k (t :: k). Proxy t
Proxy :: Proxy (Count (Normalize a)))
rrows :: Int
rrows = Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Integer -> Int) -> Integer -> Int
forall a b. (a -> b) -> a -> b
$ Proxy (Count (Normalize b)) -> Integer
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Integer
natVal (Proxy (Count (Normalize b))
forall k (t :: k). Proxy t
Proxy :: Proxy (Count (Normalize b)))
elementsA :: [a]
elementsA = Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
take Int
ccols [a
minA .. a
maxA]
elementsB :: [b]
elementsB = Int -> [b] -> [b]
forall a. Int -> [a] -> [a]
take Int
rrows [b
minB .. b
maxB]
combinations :: [(a, b)]
combinations = (,) (a -> b -> (a, b)) -> [a] -> [b -> (a, b)]
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> [a]
elementsA [b -> (a, b)] -> [b] -> [(a, b)]
forall (f :: Type -> Type) a b.
Applicative f =>
f (a -> b) -> f a -> f b
<*> [b]
elementsB
combAp :: [Natural 0 1]
combAp = (((Int, Int), Natural 0 1) -> Natural 0 1)
-> [((Int, Int), Natural 0 1)] -> [Natural 0 1]
forall a b. (a -> b) -> [a] -> [b]
map ((Int, Int), Natural 0 1) -> Natural 0 1
forall a b. (a, b) -> b
snd ([((Int, Int), Natural 0 1)] -> [Natural 0 1])
-> ([((Int, Int), Natural 0 1)] -> [((Int, Int), Natural 0 1)])
-> [((Int, Int), Natural 0 1)]
-> [Natural 0 1]
forall (k :: Type -> Type -> Type) b c a.
Category k =>
k b c -> k a b -> k a c
. [((Int, Int), Natural 0 1)] -> [((Int, Int), Natural 0 1)]
forall a. Ord a => [a] -> [a]
sort ([((Int, Int), Natural 0 1)] -> [Natural 0 1])
-> ([(a, b)] -> [((Int, Int), Natural 0 1)])
-> [(a, b)]
-> [Natural 0 1]
forall (k :: Type -> Type -> Type) b c a.
Category k =>
k b c -> k a b -> k a c
. ((a, b) -> ((Int, Int), Natural 0 1))
-> [(a, b)] -> [((Int, Int), Natural 0 1)]
forall a b. (a -> b) -> [a] -> [b]
map (\(a :: a
a, b :: b
b) -> if a -> b
f a
a b -> b -> Bool
forall a. Eq a => a -> a -> Bool
== b
b
then ((a -> Int
forall a. Enum a => a -> Int
fromEnum a
a, b -> Int
forall a. Enum a => a -> Int
fromEnum b
b), Int -> Natural 0 1
forall (n :: Nat) (m :: Nat).
(KnownNat n, KnownNat m) =>
Int -> Natural n m
nat 1)
else ((a -> Int
forall a. Enum a => a -> Int
fromEnum a
a, b -> Int
forall a. Enum a => a -> Int
fromEnum b
b), Int -> Natural 0 1
forall (n :: Nat) (m :: Nat).
(KnownNat n, KnownNat m) =>
Int -> Natural n m
nat 0)) ([(a, b)] -> [Natural 0 1]) -> [(a, b)] -> [Natural 0 1]
forall a b. (a -> b) -> a -> b
$ [(a, b)]
combinations
mList :: [[Natural 0 1]]
mList = [Natural 0 1] -> Int -> [[Natural 0 1]]
forall a. [a] -> Int -> [[a]]
buildList [Natural 0 1]
combAp Int
rrows
in Matrix (Natural 0 1) (Normalize b) (Normalize a)
-> Relation (Normalize a) (Normalize b)
forall e cols rows. Matrix e cols rows -> Matrix e rows cols
tr (Matrix (Natural 0 1) (Normalize b) (Normalize a)
-> Relation (Normalize a) (Normalize b))
-> Matrix (Natural 0 1) (Normalize b) (Normalize a)
-> Relation (Normalize a) (Normalize b)
forall a b. (a -> b) -> a -> b
$ [[Natural 0 1]] -> Matrix (Natural 0 1) (Normalize b) (Normalize a)
forall e cols rows.
FromLists e cols rows =>
[[e]] -> Matrix e cols rows
fromLists [[Natural 0 1]]
mList
where
buildList :: [a] -> Int -> [[a]]
buildList [] _ = []
buildList l :: [a]
l r :: Int
r = Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
take Int
r [a]
l [a] -> [[a]] -> [[a]]
forall a. a -> [a] -> [a]
: [a] -> Int -> [[a]]
buildList (Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
drop Int
r [a]
l) Int
r
toRel ::
forall a b.
( Bounded a,
Bounded b,
Enum a,
Enum b,
Eq b,
CountableDimensionsN a b,
FromListsN Boolean b a
)
=> (a -> b -> Bool) -> Relation (Normalize a) (Normalize b)
toRel :: (a -> b -> Bool) -> Relation (Normalize a) (Normalize b)
toRel f :: a -> b -> Bool
f =
let minA :: a
minA = Bounded a => a
forall a. Bounded a => a
minBound @a
maxA :: a
maxA = Bounded a => a
forall a. Bounded a => a
maxBound @a
minB :: b
minB = Bounded b => b
forall a. Bounded a => a
minBound @b
maxB :: b
maxB = Bounded b => b
forall a. Bounded a => a
maxBound @b
ccols :: Int
ccols = Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Integer -> Int) -> Integer -> Int
forall a b. (a -> b) -> a -> b
$ Proxy (Count (Normalize a)) -> Integer
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Integer
natVal (Proxy (Count (Normalize a))
forall k (t :: k). Proxy t
Proxy :: Proxy (Count (Normalize a)))
rrows :: Int
rrows = Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Integer -> Int) -> Integer -> Int
forall a b. (a -> b) -> a -> b
$ Proxy (Count (Normalize b)) -> Integer
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Integer
natVal (Proxy (Count (Normalize b))
forall k (t :: k). Proxy t
Proxy :: Proxy (Count (Normalize b)))
elementsA :: [a]
elementsA = Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
take Int
ccols [a
minA .. a
maxA]
elementsB :: [b]
elementsB = Int -> [b] -> [b]
forall a. Int -> [a] -> [a]
take Int
rrows [b
minB .. b
maxB]
combinations :: [(a, b)]
combinations = (,) (a -> b -> (a, b)) -> [a] -> [b -> (a, b)]
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> [a]
elementsA [b -> (a, b)] -> [b] -> [(a, b)]
forall (f :: Type -> Type) a b.
Applicative f =>
f (a -> b) -> f a -> f b
<*> [b]
elementsB
combAp :: [Natural 0 1]
combAp = (((Int, Int), Natural 0 1) -> Natural 0 1)
-> [((Int, Int), Natural 0 1)] -> [Natural 0 1]
forall a b. (a -> b) -> [a] -> [b]
map ((Int, Int), Natural 0 1) -> Natural 0 1
forall a b. (a, b) -> b
snd ([((Int, Int), Natural 0 1)] -> [Natural 0 1])
-> ([((Int, Int), Natural 0 1)] -> [((Int, Int), Natural 0 1)])
-> [((Int, Int), Natural 0 1)]
-> [Natural 0 1]
forall (k :: Type -> Type -> Type) b c a.
Category k =>
k b c -> k a b -> k a c
. [((Int, Int), Natural 0 1)] -> [((Int, Int), Natural 0 1)]
forall a. Ord a => [a] -> [a]
sort ([((Int, Int), Natural 0 1)] -> [Natural 0 1])
-> ([(a, b)] -> [((Int, Int), Natural 0 1)])
-> [(a, b)]
-> [Natural 0 1]
forall (k :: Type -> Type -> Type) b c a.
Category k =>
k b c -> k a b -> k a c
. ((a, b) -> ((Int, Int), Natural 0 1))
-> [(a, b)] -> [((Int, Int), Natural 0 1)]
forall a b. (a -> b) -> [a] -> [b]
map (\(a :: a
a, b :: b
b) -> if (a -> b -> Bool) -> (a, b) -> Bool
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry a -> b -> Bool
f (a
a, b
b)
then ((a -> Int
forall a. Enum a => a -> Int
fromEnum a
a, b -> Int
forall a. Enum a => a -> Int
fromEnum b
b), Int -> Natural 0 1
forall (n :: Nat) (m :: Nat).
(KnownNat n, KnownNat m) =>
Int -> Natural n m
nat 1)
else ((a -> Int
forall a. Enum a => a -> Int
fromEnum a
a, b -> Int
forall a. Enum a => a -> Int
fromEnum b
b), Int -> Natural 0 1
forall (n :: Nat) (m :: Nat).
(KnownNat n, KnownNat m) =>
Int -> Natural n m
nat 0)) ([(a, b)] -> [Natural 0 1]) -> [(a, b)] -> [Natural 0 1]
forall a b. (a -> b) -> a -> b
$ [(a, b)]
combinations
mList :: [[Natural 0 1]]
mList = [Natural 0 1] -> Int -> [[Natural 0 1]]
forall a. [a] -> Int -> [[a]]
buildList [Natural 0 1]
combAp Int
rrows
in Matrix (Natural 0 1) (Normalize b) (Normalize a)
-> Relation (Normalize a) (Normalize b)
forall e cols rows. Matrix e cols rows -> Matrix e rows cols
tr (Matrix (Natural 0 1) (Normalize b) (Normalize a)
-> Relation (Normalize a) (Normalize b))
-> Matrix (Natural 0 1) (Normalize b) (Normalize a)
-> Relation (Normalize a) (Normalize b)
forall a b. (a -> b) -> a -> b
$ [[Natural 0 1]] -> Matrix (Natural 0 1) (Normalize b) (Normalize a)
forall e cols rows.
FromLists e cols rows =>
[[e]] -> Matrix e cols rows
fromLists [[Natural 0 1]]
mList
where
buildList :: [a] -> Int -> [[a]]
buildList [] _ = []
buildList l :: [a]
l r :: Int
r = Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
take Int
r [a]
l [a] -> [[a]] -> [[a]]
forall a. a -> [a] -> [a]
: [a] -> Int -> [[a]]
buildList (Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
drop Int
r [a]
l) Int
r