{-# LANGUAGE NoImplicitPrelude #-}

{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}


-- |
-- Module      : OAlg.Entity.Matrix.Entries
-- Description : entries of matrices
-- Copyright   : (c) Erich Gut
-- License     : BSD3
-- Maintainer  : zerich.gut@gmail.com
-- 
-- entries of matrices and viewing them as a column of rows respectively as a row
-- of columns. 
module OAlg.Entity.Matrix.Entries
  (
    -- * Entries
    Entries(..), etsxs, etsEmpty, etsAdd, etsMlt, etsJoin
  , etscr, etsrc, crets, rcets
  , etsElimZeros

    -- * Row
  , Row(..), rowxs, rowEmpty, rowIsEmpty, rowHead, rowTail
  , rowFilter, rowMapShift, rowAppend, rowInterlace
  , rowElimZeros, rowSwap, rowAdd, rowMltl, rowShear, rowScale
  
    -- * Col
  , Col(..), colxs, colEmpty, colIsEmpty, colHead, colTail
  , colFilter, colMapShift, colAppend, colInterlace
  , colElimZeros, colSwap, colAdd, colMltr, colShear, colScale

    -- * Col Row
  , crHeadColAt, crHeadRowAt

    -- * Duality
  , coEntries, coEntriesInv
 
  ) where

import Control.Monad

import Data.Foldable
import Data.List (map,sortBy,filter,(++))

import OAlg.Prelude

import OAlg.Structure.Multiplicative
import OAlg.Structure.Additive
import OAlg.Structure.Distributive
import OAlg.Structure.Number
import OAlg.Structure.Operational

import OAlg.Entity.Sequence


--------------------------------------------------------------------------------
-- Row -

-- | viewing a partial sequence as a row.
newtype Row j x = Row (PSequence j x) deriving (Row j x -> Row j x -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall j x. (Eq x, Eq j) => Row j x -> Row j x -> Bool
/= :: Row j x -> Row j x -> Bool
$c/= :: forall j x. (Eq x, Eq j) => Row j x -> Row j x -> Bool
== :: Row j x -> Row j x -> Bool
$c== :: forall j x. (Eq x, Eq j) => Row j x -> Row j x -> Bool
Eq,Row j x -> Statement
forall a. (a -> Statement) -> Validable a
forall j x. (Entity x, Entity j, Ord j) => Row j x -> Statement
valid :: Row j x -> Statement
$cvalid :: forall j x. (Entity x, Entity j, Ord j) => Row j x -> Statement
Validable,forall a. Show a -> Eq a -> Validable a -> Typeable a -> Entity a
forall {j} {x}. (Entity x, Entity j, Ord j) => Eq (Row j x)
forall {j} {x}. (Entity x, Entity j, Ord j) => Show (Row j x)
forall {j} {x}. (Entity x, Entity j, Ord j) => Typeable (Row j x)
forall j x. (Entity x, Entity j, Ord j) => Validable (Row j x)
Entity,forall a b. a -> Row j b -> Row j a
forall a b. (a -> b) -> Row j a -> Row j b
forall j a b. a -> Row j b -> Row j a
forall j a b. (a -> b) -> Row j a -> Row j b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> Row j b -> Row j a
$c<$ :: forall j a b. a -> Row j b -> Row j a
fmap :: forall a b. (a -> b) -> Row j a -> Row j b
$cfmap :: forall j a b. (a -> b) -> Row j a -> Row j b
Functor,Row j x -> N
forall x. (x -> N) -> LengthN x
forall j x. Row j x -> N
lengthN :: Row j x -> N
$clengthN :: forall j x. Row j x -> N
LengthN)

instance (Show x,Show j) => Show (Row j x) where
  show :: Row j x -> String
show (Row (PSequence [(x, j)]
xs)) = String
"Row " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show [(x, j)]
xs

instance Ord j => Sequence (Row j) j x where
  list :: forall (p :: * -> *). p j -> Row j x -> [(x, j)]
list p j
_ = forall j x. Row j x -> [(x, j)]
rowxs
  Row PSequence j x
xs ?? :: Row j x -> j -> Maybe x
?? j
j = PSequence j x
xs forall (s :: * -> *) i x. Sequence s i x => s x -> i -> Maybe x
?? j
j

instance Ord j => Opr (Permutation j) (Row j x) where Row PSequence j x
xs <* :: Row j x -> Permutation j -> Row j x
<* Permutation j
p = forall j x. PSequence j x -> Row j x
Row (PSequence j x
xs forall f x. Opr f x => x -> f -> x
<* Permutation j
p)

instance (Entity x, Entity j, Ord j) => TotalOpr (Permutation j) (Row j x)

instance (Entity x, Entity j, Ord j) => PermutableSequence (Row j) j x where
  permuteBy :: forall (p :: * -> *) w.
p j
-> (w -> w -> Ordering)
-> (x -> w)
-> Row j x
-> (Row j x, Permutation j)
permuteBy p j
f w -> w -> Ordering
c x -> w
w (Row PSequence j x
xs) = (forall j x. PSequence j x -> Row j x
Row PSequence j x
xs',Permutation j
p) where (PSequence j x
xs',Permutation j
p) = forall (s :: * -> *) i x (p :: * -> *) w.
PermutableSequence s i x =>
p i
-> (w -> w -> Ordering) -> (x -> w) -> s x -> (s x, Permutation i)
permuteBy p j
f w -> w -> Ordering
c x -> w
w PSequence j x
xs

--------------------------------------------------------------------------------
-- rowxs -

-- | underlying list of indexed entries.
rowxs :: Row j x -> [(x,j)]
rowxs :: forall j x. Row j x -> [(x, j)]
rowxs (Row PSequence j x
xs) = forall i x. PSequence i x -> [(x, i)]
psqxs PSequence j x
xs

--------------------------------------------------------------------------------
-- rowEmpty -

-- | the empty row.
rowEmpty :: Row j x
rowEmpty :: forall j x. Row j x
rowEmpty = forall j x. PSequence j x -> Row j x
Row forall i x. PSequence i x
psqEmpty

--------------------------------------------------------------------------------
-- rowIsEmpty -

-- | check for being empty.
rowIsEmpty :: Row j x -> Bool
rowIsEmpty :: forall j x. Row j x -> Bool
rowIsEmpty (Row PSequence j x
rw) = forall i x. PSequence i x -> Bool
psqIsEmpty PSequence j x
rw

--------------------------------------------------------------------------------
-- rowHead -

-- | head.
rowHead :: Row j x -> (x,j)
rowHead :: forall j x. Row j x -> (x, j)
rowHead (Row PSequence j x
xs) = forall i x. PSequence i x -> (x, i)
psqHead PSequence j x
xs

--------------------------------------------------------------------------------
-- rowTail -

-- | tail.
rowTail :: Row j x -> Row j x
rowTail :: forall j x. Row j x -> Row j x
rowTail (Row PSequence j x
xs) = forall j x. PSequence j x -> Row j x
Row (forall i x. PSequence i x -> PSequence i x
psqTail PSequence j x
xs)

--------------------------------------------------------------------------------
-- rowFilter -

-- | filtering a row by the given predicate.
rowFilter :: (x -> Bool) -> Row j x -> Row j x
rowFilter :: forall x j. (x -> Bool) -> Row j x -> Row j x
rowFilter x -> Bool
p (Row PSequence j x
xs) = forall j x. PSequence j x -> Row j x
Row forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall x i. (x -> Bool) -> PSequence i x -> PSequence i x
psqFilter x -> Bool
p PSequence j x
xs

--------------------------------------------------------------------------------
-- rowMapShift -

-- | mapping and shifting of a row.
rowMapShift :: Number j => j -> ((x,j) -> y) -> Row j x -> Row j y
rowMapShift :: forall j x y. Number j => j -> ((x, j) -> y) -> Row j x -> Row j y
rowMapShift j
o (x, j) -> y
f (Row PSequence j x
xs) = forall j x. PSequence j x -> Row j x
Row forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall i x y.
Number i =>
i -> ((x, i) -> y) -> PSequence i x -> PSequence i y
psqMapShift j
o (x, j) -> y
f PSequence j x
xs

--------------------------------------------------------------------------------
-- rowAppend -

-- | appending a row.
--
--  __Property__ Let @zs = 'rowAppend' xs ys@ where @..(x,l) = xs@ and
-- @(y,f).. = ys@ then holds:
--
-- [If] @l '<' f@
--
-- [Then] @zs@ is 'valid'.
rowAppend :: Row j x -> Row j x -> Row j x
rowAppend :: forall j x. Row j x -> Row j x -> Row j x
rowAppend (Row PSequence j x
xs) (Row PSequence j x
ys) = forall j x. PSequence j x -> Row j x
Row forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall i x. PSequence i x -> PSequence i x -> PSequence i x
psqAppend PSequence j x
xs PSequence j x
ys

--------------------------------------------------------------------------------
-- rowInterlace -

-- | interlacing two rows.
rowInterlace :: Ord j
  => (x -> y -> z) -> (x -> z) -> (y -> z) -> Row j x -> Row j y -> Row j z
rowInterlace :: forall j x y z.
Ord j =>
(x -> y -> z)
-> (x -> z) -> (y -> z) -> Row j x -> Row j y -> Row j z
rowInterlace x -> y -> z
(+) x -> z
xz y -> z
yz (Row PSequence j x
xs) (Row PSequence j y
ys) = forall j x. PSequence j x -> Row j x
Row forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall i x y z.
Ord i =>
(x -> y -> z)
-> (x -> z)
-> (y -> z)
-> PSequence i x
-> PSequence i y
-> PSequence i z
psqInterlace x -> y -> z
(+) x -> z
xz y -> z
yz PSequence j x
xs PSequence j y
ys

--------------------------------------------------------------------------------
-- rowElimZeros -

-- | elimination of 'zero's.
rowElimZeros :: Additive a => Row i a -> Row i a
rowElimZeros :: forall a i. Additive a => Row i a -> Row i a
rowElimZeros (Row PSequence i a
rw) = forall j x. PSequence j x -> Row j x
Row forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall x i. (x -> Bool) -> PSequence i x -> PSequence i x
psqFilter (forall b. Boolean b => b -> b
notforall (c :: * -> * -> *) y z x.
Category c =>
c y z -> c x y -> c x z
.forall a. Additive a => a -> Bool
isZero) PSequence i a
rw

--------------------------------------------------------------------------------
-- rowAdd - 

-- | adding two rows.
rowAdd :: (Additive a, Ord j) => Row j a -> Row j a -> Row j a
rowAdd :: forall a j. (Additive a, Ord j) => Row j a -> Row j a -> Row j a
rowAdd (Row PSequence j a
a) (Row PSequence j a
b) = forall a i. Additive a => Row i a -> Row i a
rowElimZeros forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall j x. PSequence j x -> Row j x
Row forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall i x y z.
Ord i =>
(x -> y -> z)
-> (x -> z)
-> (y -> z)
-> PSequence i x
-> PSequence i y
-> PSequence i z
psqInterlace forall a. Additive a => a -> a -> a
(+) forall x. x -> x
id forall x. x -> x
id PSequence j a
a PSequence j a
b

--------------------------------------------------------------------------------
-- rowSwap -

-- | swapping two entries of a row.
--
-- __Pre__ @k < l@.
rowSwap :: Ord j => j -> j -> Row j x -> Row j x
rowSwap :: forall j x. Ord j => j -> j -> Row j x -> Row j x
rowSwap j
k j
l (Row PSequence j x
rw) = forall j x. PSequence j x -> Row j x
Row forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall i a. Ord i => i -> i -> PSequence i a -> PSequence i a
psqSwap j
k j
l PSequence j x
rw

--------------------------------------------------------------------------------
-- rowMltl -

-- | multiplies each element of the row by the given factor from the left.
rowMltl :: Distributive a => a -> Row j a -> Row j a
rowMltl :: forall a j. Distributive a => a -> Row j a -> Row j a
rowMltl a
a = forall a i. Additive a => Row i a -> Row i a
rowElimZeros forall (c :: * -> * -> *) y z x.
Category c =>
c y z -> c x y -> c x z
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (a
aforall c. Multiplicative c => c -> c -> c
*)

--------------------------------------------------------------------------------
-- rowShear -

-- | shears two entries of a row.
--
--  __Property__ Let @r' = 'rowShear' (<*) (+) k l s t u v r@, then holds:
--
--  [Pre] @k '<' l@.
--
--  __Note__ 'rowShear' is like /multiplying/ the given row from the right with
--  the matrix given by @k l s t u v@.
rowShear :: Ord j
  => (Maybe x -> s -> Maybe x) -> (Maybe x -> Maybe x -> Maybe x)
  -> j -> j -> s -> s -> s -> s
  -> Row j x -> Row j x
rowShear :: forall j x s.
Ord j =>
(Maybe x -> s -> Maybe x)
-> (Maybe x -> Maybe x -> Maybe x)
-> j
-> j
-> s
-> s
-> s
-> s
-> Row j x
-> Row j x
rowShear Maybe x -> s -> Maybe x
(<*) Maybe x -> Maybe x -> Maybe x
(+) j
k j
l s
s s
t s
u s
v (Row PSequence j x
xs) = forall j x. PSequence j x -> Row j x
Row (forall i a.
Ord i =>
(Maybe a -> Maybe a -> Maybe a, i)
-> (Maybe a -> Maybe a -> Maybe a, i)
-> PSequence i a
-> PSequence i a
psqShear (Maybe x -> Maybe x -> Maybe x
sk,j
k) (Maybe x -> Maybe x -> Maybe x
sl,j
l) PSequence j x
xs) where
  sk :: Maybe x -> Maybe x -> Maybe x
sk Maybe x
xk Maybe x
xl = (Maybe x
xkMaybe x -> s -> Maybe x
<*s
s) Maybe x -> Maybe x -> Maybe x
+ (Maybe x
xlMaybe x -> s -> Maybe x
<*s
u)
  sl :: Maybe x -> Maybe x -> Maybe x
sl Maybe x
xk Maybe x
xl = (Maybe x
xkMaybe x -> s -> Maybe x
<*s
t) Maybe x -> Maybe x -> Maybe x
+ (Maybe x
xlMaybe x -> s -> Maybe x
<*s
v)

--------------------------------------------------------------------------------
-- rowScale -

-- | scales the entry at the given position by the given factor.
rowScale :: Ord j
  => (x -> s -> Maybe x)
  -> j -> s
  -> Row j x -> Row j x
rowScale :: forall j x s.
Ord j =>
(x -> s -> Maybe x) -> j -> s -> Row j x -> Row j x
rowScale x -> s -> Maybe x
(<*) j
j s
s Row j x
rw
  = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. HasCallStack => Maybe a -> a
fromJust
  forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall x j. (x -> Bool) -> Row j x -> Row j x
rowFilter forall a. Maybe a -> Bool
isJust
  forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall j x y z.
Ord j =>
(x -> y -> z)
-> (x -> z) -> (y -> z) -> Row j x -> Row j y -> Row j z
rowInterlace x -> s -> Maybe x
(<*) forall a. a -> Maybe a
Just (forall b a. b -> a -> b
const forall a. Maybe a
Nothing) Row j x
rw (forall j x. PSequence j x -> Row j x
Row (forall i x. [(x, i)] -> PSequence i x
PSequence [(s
s,j
j)]))


--------------------------------------------------------------------------------
-- Col - 

-- | viewing a partial sequence as a column.
newtype Col i x = Col (PSequence i x) deriving (Col i x -> Col i x -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall i x. (Eq x, Eq i) => Col i x -> Col i x -> Bool
/= :: Col i x -> Col i x -> Bool
$c/= :: forall i x. (Eq x, Eq i) => Col i x -> Col i x -> Bool
== :: Col i x -> Col i x -> Bool
$c== :: forall i x. (Eq x, Eq i) => Col i x -> Col i x -> Bool
Eq,Col i x -> Statement
forall a. (a -> Statement) -> Validable a
forall i x. (Entity x, Entity i, Ord i) => Col i x -> Statement
valid :: Col i x -> Statement
$cvalid :: forall i x. (Entity x, Entity i, Ord i) => Col i x -> Statement
Validable,forall a. Show a -> Eq a -> Validable a -> Typeable a -> Entity a
forall {i} {x}. (Entity x, Entity i, Ord i) => Eq (Col i x)
forall {i} {x}. (Entity x, Entity i, Ord i) => Show (Col i x)
forall {i} {x}. (Entity x, Entity i, Ord i) => Typeable (Col i x)
forall i x. (Entity x, Entity i, Ord i) => Validable (Col i x)
Entity,forall a b. a -> Col i b -> Col i a
forall a b. (a -> b) -> Col i a -> Col i b
forall i a b. a -> Col i b -> Col i a
forall i a b. (a -> b) -> Col i a -> Col i b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> Col i b -> Col i a
$c<$ :: forall i a b. a -> Col i b -> Col i a
fmap :: forall a b. (a -> b) -> Col i a -> Col i b
$cfmap :: forall i a b. (a -> b) -> Col i a -> Col i b
Functor,Col i x -> N
forall x. (x -> N) -> LengthN x
forall i x. Col i x -> N
lengthN :: Col i x -> N
$clengthN :: forall i x. Col i x -> N
LengthN)

instance (Show x,Show i) => Show (Col i x) where
  show :: Col i x -> String
show (Col (PSequence [(x, i)]
xs)) = String
"Col " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show [(x, i)]
xs

instance Ord i => Sequence (Col i) i x where
  list :: forall (p :: * -> *). p i -> Col i x -> [(x, i)]
list p i
_ = forall i x. Col i x -> [(x, i)]
colxs
  Col PSequence i x
xs ?? :: Col i x -> i -> Maybe x
?? i
i = PSequence i x
xs forall (s :: * -> *) i x. Sequence s i x => s x -> i -> Maybe x
?? i
i

instance Ord i => Opr (Permutation i) (Col i x) where Col PSequence i x
xs <* :: Col i x -> Permutation i -> Col i x
<* Permutation i
p = forall i x. PSequence i x -> Col i x
Col (PSequence i x
xs forall f x. Opr f x => x -> f -> x
<* Permutation i
p)

instance (Entity x, Entity i, Ord i) => TotalOpr (Permutation i) (Col i x)

instance (Entity x, Entity i, Ord i) => PermutableSequence (Col i) i x where
  permuteBy :: forall (p :: * -> *) w.
p i
-> (w -> w -> Ordering)
-> (x -> w)
-> Col i x
-> (Col i x, Permutation i)
permuteBy p i
f w -> w -> Ordering
c x -> w
w (Col PSequence i x
xs) = (forall i x. PSequence i x -> Col i x
Col PSequence i x
xs',Permutation i
p) where (PSequence i x
xs',Permutation i
p) = forall (s :: * -> *) i x (p :: * -> *) w.
PermutableSequence s i x =>
p i
-> (w -> w -> Ordering) -> (x -> w) -> s x -> (s x, Permutation i)
permuteBy p i
f w -> w -> Ordering
c x -> w
w PSequence i x
xs

--------------------------------------------------------------------------------
-- colxs -

-- | underlying list of indexed entries.
colxs :: Col i x -> [(x,i)]
colxs :: forall i x. Col i x -> [(x, i)]
colxs (Col PSequence i x
xs) = forall i x. PSequence i x -> [(x, i)]
psqxs PSequence i x
xs

--------------------------------------------------------------------------------
-- colEmpty -

-- | the empty column.
colEmpty :: Col i x
colEmpty :: forall i x. Col i x
colEmpty = forall i x. PSequence i x -> Col i x
Col forall i x. PSequence i x
psqEmpty

--------------------------------------------------------------------------------
-- colIsEmpty -

-- | check for being empty.
colIsEmpty :: Col i x -> Bool
colIsEmpty :: forall i x. Col i x -> Bool
colIsEmpty (Col PSequence i x
cl) = forall i x. PSequence i x -> Bool
psqIsEmpty PSequence i x
cl

--------------------------------------------------------------------------------
-- colHead -

-- | head.
colHead :: Col i x -> (x,i)
colHead :: forall i x. Col i x -> (x, i)
colHead (Col PSequence i x
xs) = forall i x. PSequence i x -> (x, i)
psqHead PSequence i x
xs

--------------------------------------------------------------------------------
-- colTail -

-- | tail.
colTail :: Col i x -> Col i x
colTail :: forall i x. Col i x -> Col i x
colTail (Col PSequence i x
xs) = forall i x. PSequence i x -> Col i x
Col (forall i x. PSequence i x -> PSequence i x
psqTail PSequence i x
xs)

--------------------------------------------------------------------------------
-- colFilter -

-- | filtering a column by the given predicate.
colFilter :: (x -> Bool) -> Col i x -> Col i x
colFilter :: forall x i. (x -> Bool) -> Col i x -> Col i x
colFilter x -> Bool
p (Col PSequence i x
xs) = forall i x. PSequence i x -> Col i x
Col forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall x i. (x -> Bool) -> PSequence i x -> PSequence i x
psqFilter x -> Bool
p PSequence i x
xs

--------------------------------------------------------------------------------
-- colMapShift -

-- | mapping and shifting of a column.
colMapShift :: Number i => i -> ((x,i) -> y) -> Col i x -> Col i y
colMapShift :: forall i x y. Number i => i -> ((x, i) -> y) -> Col i x -> Col i y
colMapShift i
o (x, i) -> y
f (Col PSequence i x
xs) = forall i x. PSequence i x -> Col i x
Col forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall i x y.
Number i =>
i -> ((x, i) -> y) -> PSequence i x -> PSequence i y
psqMapShift i
o (x, i) -> y
f PSequence i x
xs

--------------------------------------------------------------------------------
-- colAppend -

-- | appending a column..
--
--  __Property__ Let @zs = 'colAppend' xs ys@ where @..(x,l) = xs@ and
-- @(y,f).. = ys@ then holds:
--
-- [If] @l '<' f@
--
-- [Then] @zs@ is 'valid'.
colAppend :: Col i x -> Col i x -> Col i x
colAppend :: forall i x. Col i x -> Col i x -> Col i x
colAppend (Col PSequence i x
xs) (Col PSequence i x
ys) = forall i x. PSequence i x -> Col i x
Col (forall i x. PSequence i x -> PSequence i x -> PSequence i x
psqAppend PSequence i x
xs PSequence i x
ys)

--------------------------------------------------------------------------------
-- colInterlace -

-- | interlacing two columns.
colInterlace :: Ord i
  => (x -> y -> z) -> (x -> z) -> (y -> z) -> Col i x -> Col i y -> Col i z
colInterlace :: forall i x y z.
Ord i =>
(x -> y -> z)
-> (x -> z) -> (y -> z) -> Col i x -> Col i y -> Col i z
colInterlace x -> y -> z
(+) x -> z
xz y -> z
yz (Col PSequence i x
xs) (Col PSequence i y
ys) = forall i x. PSequence i x -> Col i x
Col forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall i x y z.
Ord i =>
(x -> y -> z)
-> (x -> z)
-> (y -> z)
-> PSequence i x
-> PSequence i y
-> PSequence i z
psqInterlace x -> y -> z
(+) x -> z
xz y -> z
yz PSequence i x
xs PSequence i y
ys

--------------------------------------------------------------------------------
-- colElimZeros -

-- | elimination of 'zero's.
colElimZeros :: Additive a => Col i a -> Col i a
colElimZeros :: forall a i. Additive a => Col i a -> Col i a
colElimZeros (Col PSequence i a
rw) = forall i x. PSequence i x -> Col i x
Col forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall x i. (x -> Bool) -> PSequence i x -> PSequence i x
psqFilter (forall b. Boolean b => b -> b
notforall (c :: * -> * -> *) y z x.
Category c =>
c y z -> c x y -> c x z
.forall a. Additive a => a -> Bool
isZero) PSequence i a
rw

--------------------------------------------------------------------------------
-- colSwap -

-- | swapping two entries of a column.
--
-- __Pre__ @k < l@.
colSwap :: Ord i => i -> i -> Col i x -> Col i x
colSwap :: forall i x. Ord i => i -> i -> Col i x -> Col i x
colSwap i
k i
l (Col PSequence i x
rws) = forall i x. PSequence i x -> Col i x
Col (forall i a. Ord i => i -> i -> PSequence i a -> PSequence i a
psqSwap i
k i
l PSequence i x
rws)

--------------------------------------------------------------------------------
-- colAdd -

-- | adding two columns.
colAdd :: (Additive a, Ord i) => Col i a -> Col i a -> Col i a
colAdd :: forall a i. (Additive a, Ord i) => Col i a -> Col i a -> Col i a
colAdd (Col PSequence i a
a) (Col PSequence i a
b) = forall a i. Additive a => Col i a -> Col i a
colElimZeros forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall i x. PSequence i x -> Col i x
Col forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall i x y z.
Ord i =>
(x -> y -> z)
-> (x -> z)
-> (y -> z)
-> PSequence i x
-> PSequence i y
-> PSequence i z
psqInterlace forall a. Additive a => a -> a -> a
(+) forall x. x -> x
id forall x. x -> x
id PSequence i a
a PSequence i a
b

--------------------------------------------------------------------------------
-- colShear -

-- | shears two entries of a column.
--
--  __Property__ Let @c' = 'colShear' (<*) (+) k l s t u v c@, then holds:
--
--  [Pre] @k '<' l@.
--
--  __Note__ 'colShear' is like /multiplying/ the given column from the left with
--  the matrix given by @k l s t u v@.
colShear :: Ord i
  => (s -> Maybe x -> Maybe x) -> (Maybe x -> Maybe x -> Maybe x)
  -> i -> i -> s -> s -> s -> s
  -> Col i x -> Col i x
colShear :: forall i s x.
Ord i =>
(s -> Maybe x -> Maybe x)
-> (Maybe x -> Maybe x -> Maybe x)
-> i
-> i
-> s
-> s
-> s
-> s
-> Col i x
-> Col i x
colShear s -> Maybe x -> Maybe x
(*>) Maybe x -> Maybe x -> Maybe x
(+) i
k i
l s
s s
t s
u s
v (Col PSequence i x
xs) = forall i x. PSequence i x -> Col i x
Col (forall i a.
Ord i =>
(Maybe a -> Maybe a -> Maybe a, i)
-> (Maybe a -> Maybe a -> Maybe a, i)
-> PSequence i a
-> PSequence i a
psqShear (Maybe x -> Maybe x -> Maybe x
sk,i
k) (Maybe x -> Maybe x -> Maybe x
sl,i
l) PSequence i x
xs) where
  sk :: Maybe x -> Maybe x -> Maybe x
sk Maybe x
xk Maybe x
xl = (s
ss -> Maybe x -> Maybe x
*>Maybe x
xk) Maybe x -> Maybe x -> Maybe x
+ (s
ts -> Maybe x -> Maybe x
*>Maybe x
xl)
  sl :: Maybe x -> Maybe x -> Maybe x
sl Maybe x
xk Maybe x
xl = (s
us -> Maybe x -> Maybe x
*>Maybe x
xk) Maybe x -> Maybe x -> Maybe x
+ (s
vs -> Maybe x -> Maybe x
*>Maybe x
xl)

--------------------------------------------------------------------------------
-- colScale -

-- | scales the entry at the given position by the given factor.
colScale :: Ord i
  => (s -> x -> Maybe x)
  -> i -> s
  -> Col i x -> Col i x
colScale :: forall i s x.
Ord i =>
(s -> x -> Maybe x) -> i -> s -> Col i x -> Col i x
colScale s -> x -> Maybe x
(*>) i
i s
s Col i x
cl
  = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. HasCallStack => Maybe a -> a
fromJust
  forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall x i. (x -> Bool) -> Col i x -> Col i x
colFilter forall a. Maybe a -> Bool
isJust
  forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall i x y z.
Ord i =>
(x -> y -> z)
-> (x -> z) -> (y -> z) -> Col i x -> Col i y -> Col i z
colInterlace s -> x -> Maybe x
(*>) (forall b a. b -> a -> b
const forall a. Maybe a
Nothing) forall a. a -> Maybe a
Just (forall i x. PSequence i x -> Col i x
Col (forall i x. [(x, i)] -> PSequence i x
PSequence [(s
s,i
i)])) Col i x
cl
  
--------------------------------------------------------------------------------
-- colMltr -

-- | multiplies each element of the column by the given factor from the right.
colMltr :: Distributive a => Col i a -> a -> Col i a
colMltr :: forall a i. Distributive a => Col i a -> a -> Col i a
colMltr Col i a
cl a
a = forall a i. Additive a => Col i a -> Col i a
colElimZeros forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall c. Multiplicative c => c -> c -> c
*a
a) Col i a
cl

--------------------------------------------------------------------------------
-- Col - Duality -

-- | to the dual of a column, with inverse 'coColInv'.
coCol :: Col i x -> Row i (Op x)
coCol :: forall i x. Col i x -> Row i (Op x)
coCol (Col PSequence i x
cl) = forall j x. PSequence j x -> Row j x
Row forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall x. x -> Op x
Op PSequence i x
cl

-- | from the dual of a column, with inverse 'coCol'.
coColInv :: Row i (Op x) -> Col i x
coColInv :: forall i x. Row i (Op x) -> Col i x
coColInv (Row PSequence i (Op x)
rw) = forall i x. PSequence i x -> Col i x
Col forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall x. Op x -> x
fromOp PSequence i (Op x)
rw

--------------------------------------------------------------------------------
-- Row - Col - Duality -

type instance Dual (Row j (Col i x)) = Col j (Row i (Op x))

-- | to the dual of a row of columns, with inverse 'coRowColInv'.
coRowCol :: Row j (Col i x) -> Dual (Row j (Col i x))
coRowCol :: forall j i x. Row j (Col i x) -> Dual (Row j (Col i x))
coRowCol (Row PSequence j (Col i x)
cls) = forall i x. PSequence i x -> Col i x
Col forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall i x. Col i x -> Row i (Op x)
coCol PSequence j (Col i x)
cls

-- | from the dual of a row of columns, with inverse 'coRowCol'.
coRowColInv :: Dual (Row j (Col i x)) -> Row j (Col i x)
            -- Col j (Row i (Op x))   -> Row j (Col i x)
coRowColInv :: forall j i x. Dual (Row j (Col i x)) -> Row j (Col i x)
coRowColInv (Col PSequence j (Row i (Op x))
rws) = forall j x. PSequence j x -> Row j x
Row forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall i x. Row i (Op x) -> Col i x
coColInv PSequence j (Row i (Op x))
rws

{-
coColRow :: Col i (Row j a) -> Row j (Col i (Op a))
coColRow = error "nyi"

coColRowInv :: Row j (Col i (Op a)) -> Col i (Row j a)
coColRowInv = error "nyi"
-}

--------------------------------------------------------------------------------
-- Entries -

-- | two dimensional partial sequence.
newtype Entries i j x = Entries (PSequence (i,j) x)
  deriving (Entries i j x -> Entries i j x -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall i j x.
(Eq x, Eq i, Eq j) =>
Entries i j x -> Entries i j x -> Bool
/= :: Entries i j x -> Entries i j x -> Bool
$c/= :: forall i j x.
(Eq x, Eq i, Eq j) =>
Entries i j x -> Entries i j x -> Bool
== :: Entries i j x -> Entries i j x -> Bool
$c== :: forall i j x.
(Eq x, Eq i, Eq j) =>
Entries i j x -> Entries i j x -> Bool
Eq,Entries i j x -> Entries i j x -> Bool
Entries i j x -> Entries i j x -> Ordering
Entries i j x -> Entries i j x -> Entries i j x
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall {i} {j} {x}. (Ord x, Ord i, Ord j) => Eq (Entries i j x)
forall i j x.
(Ord x, Ord i, Ord j) =>
Entries i j x -> Entries i j x -> Bool
forall i j x.
(Ord x, Ord i, Ord j) =>
Entries i j x -> Entries i j x -> Ordering
forall i j x.
(Ord x, Ord i, Ord j) =>
Entries i j x -> Entries i j x -> Entries i j x
min :: Entries i j x -> Entries i j x -> Entries i j x
$cmin :: forall i j x.
(Ord x, Ord i, Ord j) =>
Entries i j x -> Entries i j x -> Entries i j x
max :: Entries i j x -> Entries i j x -> Entries i j x
$cmax :: forall i j x.
(Ord x, Ord i, Ord j) =>
Entries i j x -> Entries i j x -> Entries i j x
>= :: Entries i j x -> Entries i j x -> Bool
$c>= :: forall i j x.
(Ord x, Ord i, Ord j) =>
Entries i j x -> Entries i j x -> Bool
> :: Entries i j x -> Entries i j x -> Bool
$c> :: forall i j x.
(Ord x, Ord i, Ord j) =>
Entries i j x -> Entries i j x -> Bool
<= :: Entries i j x -> Entries i j x -> Bool
$c<= :: forall i j x.
(Ord x, Ord i, Ord j) =>
Entries i j x -> Entries i j x -> Bool
< :: Entries i j x -> Entries i j x -> Bool
$c< :: forall i j x.
(Ord x, Ord i, Ord j) =>
Entries i j x -> Entries i j x -> Bool
compare :: Entries i j x -> Entries i j x -> Ordering
$ccompare :: forall i j x.
(Ord x, Ord i, Ord j) =>
Entries i j x -> Entries i j x -> Ordering
Ord,Entries i j x -> Statement
forall a. (a -> Statement) -> Validable a
forall i j x.
(Entity x, Entity i, Entity j, Ord i, Ord j) =>
Entries i j x -> Statement
valid :: Entries i j x -> Statement
$cvalid :: forall i j x.
(Entity x, Entity i, Entity j, Ord i, Ord j) =>
Entries i j x -> Statement
Validable,forall a. Show a -> Eq a -> Validable a -> Typeable a -> Entity a
forall {i} {j} {x}.
(Entity x, Entity i, Entity j, Ord i, Ord j) =>
Eq (Entries i j x)
forall {i} {j} {x}.
(Entity x, Entity i, Entity j, Ord i, Ord j) =>
Show (Entries i j x)
forall {i} {j} {x}.
(Entity x, Entity i, Entity j, Ord i, Ord j) =>
Typeable (Entries i j x)
forall i j x.
(Entity x, Entity i, Entity j, Ord i, Ord j) =>
Validable (Entries i j x)
Entity,forall a b. a -> Entries i j b -> Entries i j a
forall a b. (a -> b) -> Entries i j a -> Entries i j b
forall i j a b. a -> Entries i j b -> Entries i j a
forall i j a b. (a -> b) -> Entries i j a -> Entries i j b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> Entries i j b -> Entries i j a
$c<$ :: forall i j a b. a -> Entries i j b -> Entries i j a
fmap :: forall a b. (a -> b) -> Entries i j a -> Entries i j b
$cfmap :: forall i j a b. (a -> b) -> Entries i j a -> Entries i j b
Functor,Entries i j x -> N
forall x. (x -> N) -> LengthN x
forall i j x. Entries i j x -> N
lengthN :: Entries i j x -> N
$clengthN :: forall i j x. Entries i j x -> N
LengthN)

instance (Show x, Show i, Show j) => Show (Entries i j x) where
  show :: Entries i j x -> String
show Entries i j x
ets = String
"Entries" forall a. [a] -> [a] -> [a]
++ (forall a. Show a => a -> String
show forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall i j x. Entries i j x -> [(x, i, j)]
etsxs Entries i j x
ets)  

--------------------------------------------------------------------------------
-- etsxs -

-- | underlying list of indexed entries.
etsxs :: Entries i j x -> [(x,i,j)]
etsxs :: forall i j x. Entries i j x -> [(x, i, j)]
etsxs (Entries PSequence (i, j) x
xs) = forall a b. (a -> b) -> [a] -> [b]
map (\(x
x,(i
i,j
j)) -> (x
x,i
i,j
j)) forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall i x. PSequence i x -> [(x, i)]
psqxs PSequence (i, j) x
xs
                         
--------------------------------------------------------------------------------
-- etsEmpty -

-- | the empty sequence of entries.
etsEmpty :: Entries i j x
etsEmpty :: forall i j x. Entries i j x
etsEmpty = forall i j x. PSequence (i, j) x -> Entries i j x
Entries forall i x. PSequence i x
psqEmpty

--------------------------------------------------------------------------------
-- etsElimZeros -

-- | elimination of 'zero's.
etsElimZeros :: Additive x => Entries i j x -> Entries i j x
etsElimZeros :: forall x i j. Additive x => Entries i j x -> Entries i j x
etsElimZeros (Entries PSequence (i, j) x
xs) = forall i j x. PSequence (i, j) x -> Entries i j x
Entries (forall x i. (x -> Bool) -> PSequence i x -> PSequence i x
psqFilter (forall b. Boolean b => b -> b
not forall (c :: * -> * -> *) y z x.
Category c =>
c y z -> c x y -> c x z
. forall a. Additive a => a -> Bool
isZero) PSequence (i, j) x
xs)

--------------------------------------------------------------------------------
-- etsAdd -

-- | adding two entries.
--
--   __Property__ Let @zs = 'etsAdd' xs ys@, then holds:
--
--   [Pre] For all @(i,j)@ in @(__i__,__j__)@ where there exists an @(x,i,j)@ in @xs@ and
--   a @(y,i,j)@ in @ys@ holds:
--   @'OAlg.Structure.Fibred.Definition.root' x '==' 'OAlg.Structure.Fibred.Definition.root' y@.
--
--   [Post]
--
--      (1) @zs@ is 'valid'.
--
--      (2) For all @(i,j)@ in @(__i__,__j__)@ holds:
--
--          (1) If exists a @(x,i,j)@ in @xs@ but not exists a @(y,i,j)@ in @ys@ then
--          there exists a @(z,i,j)@ in @zs@ with @z '==' x@.
--
--          (2) If exists a @(y,i,j)@ in @ys@ but not exists a @(x,i,j)@ in @xs@ then
--          there exists a @(z,i,j)@ in @zs@ with @z '==' y@.
--
--          (3) If exists a @(x,i,j)@ in @xs@ and @(y,i,j)@ in @ys@ then there
--          exists a @(z,i,j)@ in @zs@ with @z '==' x '+' y@.
etsAdd :: (Additive x, Ord i, Ord j) => Entries i j x -> Entries i j x -> Entries i j x
etsAdd :: forall x i j.
(Additive x, Ord i, Ord j) =>
Entries i j x -> Entries i j x -> Entries i j x
etsAdd (Entries PSequence (i, j) x
xs) (Entries PSequence (i, j) x
ys)
  = forall i j x. PSequence (i, j) x -> Entries i j x
Entries forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall x i. (x -> Bool) -> PSequence i x -> PSequence i x
psqFilter (forall b. Boolean b => b -> b
not forall (c :: * -> * -> *) y z x.
Category c =>
c y z -> c x y -> c x z
. forall a. Additive a => a -> Bool
isZero) forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall i x y z.
Ord i =>
(x -> y -> z)
-> (x -> z)
-> (y -> z)
-> PSequence i x
-> PSequence i y
-> PSequence i z
psqInterlace forall a. Additive a => a -> a -> a
(+) forall x. x -> x
id forall x. x -> x
id PSequence (i, j) x
xs PSequence (i, j) x
ys

--------------------------------------------------------------------------------
-- Transposable -

instance (Transposable x, Ord n) => Transposable (Entries n n x) where
  transpose :: Entries n n x -> Entries n n x
transpose (Entries PSequence (n, n) x
xs) = forall i j x. PSequence (i, j) x -> Entries i j x
Entries
                         forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall i x. [(x, i)] -> PSequence i x
PSequence
                         forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy (forall i a. Ord i => (a -> i) -> a -> a -> Ordering
fcompare forall a b. (a, b) -> b
snd)
                         forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (\(x
x,(n
i,n
j)) -> (forall x. Transposable x => x -> x
transpose x
x,(n
j,n
i)))
                         forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall i x. PSequence i x -> [(x, i)]
psqxs forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ PSequence (n, n) x
xs
                         
--------------------------------------------------------------------------------
-- Entrie - Duality -

type instance Dual (Entries i j x) = Entries j i (Op x)

-- | to the dual of 'Entries', with inverse 'coEntriesInv'.
coEntries :: (Ord i, Ord j) => Entries i j x -> Dual (Entries i j x)
coEntries :: forall i j x.
(Ord i, Ord j) =>
Entries i j x -> Dual (Entries i j x)
coEntries (Entries PSequence (i, j) x
xs) = forall i j x. PSequence (i, j) x -> Entries i j x
Entries
                       forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall i x. [(x, i)] -> PSequence i x
PSequence
                       forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy (forall i a. Ord i => (a -> i) -> a -> a -> Ordering
fcompare forall a b. (a, b) -> b
snd)
                       forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (\(x
x,(i
i,j
j)) -> (forall x. x -> Op x
Op x
x,(j
j,i
i)))
                       forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall i x. PSequence i x -> [(x, i)]
psqxs forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ PSequence (i, j) x
xs


-- | from the dual of 'Entries', with inverse 'coEntries'.
coEntriesInv :: (Ord i, Ord j) => Dual (Entries i j x) -> Entries i j x
coEntriesInv :: forall i j x.
(Ord i, Ord j) =>
Dual (Entries i j x) -> Entries i j x
coEntriesInv (Entries PSequence (j, i) (Op x)
xs') = forall i j x. PSequence (i, j) x -> Entries i j x
Entries
                           forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall i x. [(x, i)] -> PSequence i x
PSequence
                           forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy (forall i a. Ord i => (a -> i) -> a -> a -> Ordering
fcompare forall a b. (a, b) -> b
snd)
                           forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (\(Op x
x,(j
j,i
i)) -> (x
x,(i
i,j
j)))
                           forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ (\(PSequence [(Op x, (j, i))]
xs) -> [(Op x, (j, i))]
xs) forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ PSequence (j, i) (Op x)
xs'

--------------------------------------------------------------------------------
-- etscr -

-- | the underlying column of rows.
etscr :: Eq i => Entries i j x -> Col i (Row j x)
etscr :: forall i j x. Eq i => Entries i j x -> Col i (Row j x)
etscr (Entries PSequence (i, j) x
xs) = forall i x. PSequence i x -> Col i x
Col forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall i x. [(x, i)] -> PSequence i x
PSequence forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall {y} {b} {x}. Eq y => PSequence (y, b) x -> [(Row b x, y)]
cr PSequence (i, j) x
xs where
  cr :: PSequence (y, b) x -> [(Row b x, y)]
cr (PSequence []) = []
  cr PSequence (y, b) x
xs             = (Row b x
l,y
i) forall a. a -> [a] -> [a]
: PSequence (y, b) x -> [(Row b x, y)]
cr PSequence (y, b) x
r where
    (PSequence [(x, (y, b))]
ls,PSequence (y, b) x
r) = forall x i.
((x, i) -> Bool) -> PSequence i x -> (PSequence i x, PSequence i x)
psqSplitWhile ((forall a. Eq a => a -> a -> Bool
==y
i) forall (c :: * -> * -> *) y z x.
Category c =>
c y z -> c x y -> c x z
. forall {a} {a} {b}. (a, (a, b)) -> a
ix) PSequence (y, b) x
xs
    ix :: (a, (a, b)) -> a
ix (a
_,(a
i,b
_)) = a
i
    i :: y
i = forall {a} {a} {b}. (a, (a, b)) -> a
ix forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall i x. PSequence i x -> (x, i)
psqHead PSequence (y, b) x
xs
    l :: Row b x
l = forall j x. PSequence j x -> Row j x
Row forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall i x. [(x, i)] -> PSequence i x
PSequence forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (\(x
x,(y
_,b
j)) -> (x
x,b
j)) forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ [(x, (y, b))]
ls 

--------------------------------------------------------------------------------
-- etsrc -

-- | the underlying row of columns.
etsrc :: (Ord i, Ord j) => Entries i j x -> Row j (Col i x)
etsrc :: forall i j x. (Ord i, Ord j) => Entries i j x -> Row j (Col i x)
etsrc = forall j i x. Dual (Row j (Col i x)) -> Row j (Col i x)
coRowColInv forall (c :: * -> * -> *) y z x.
Category c =>
c y z -> c x y -> c x z
. forall i j x. Eq i => Entries i j x -> Col i (Row j x)
etscr forall (c :: * -> * -> *) y z x.
Category c =>
c y z -> c x y -> c x z
. forall i j x.
(Ord i, Ord j) =>
Entries i j x -> Dual (Entries i j x)
coEntries

--------------------------------------------------------------------------------
-- etsMlt -

-- | multiplication.
etsMlt :: (Distributive x, Ord k) => Col i (Row k x) -> Row j (Col k x) -> Col i (Row j x)
etsMlt :: forall x k i j.
(Distributive x, Ord k) =>
Col i (Row k x) -> Row j (Col k x) -> Col i (Row j x)
etsMlt (Col PSequence i (Row k x)
rws) (Row PSequence j (Col k x)
cls) = forall i x. PSequence i x -> Col i x
Col PSequence i (Row j x)
rws' where
  rws' :: PSequence i (Row j x)
rws' = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall {y} {i} {j}.
(Additive y, Ord i, Multiplicative y) =>
Row i y -> PSequence j (Col i y) -> Row j y
<*PSequence j (Col k x)
cls) PSequence i (Row k x)
rws
  
  Row i y
rw <* :: Row i y -> PSequence j (Col i y) -> Row j y
<* PSequence j (Col i y)
cls
    = forall j x. PSequence j x -> Row j x
Row
    forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. HasCallStack => Maybe a -> a
fromJust
    forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall x i. (x -> Bool) -> PSequence i x -> PSequence i x
psqFilter forall a. Maybe a -> Bool
isJust
    forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Row i y
rwforall {y} {i}.
(Additive y, Ord i, Multiplicative y) =>
Row i y -> Col i y -> Maybe y
<*>) PSequence j (Col i y)
cls
    
  Row PSequence i y
rw <*> :: Row i y -> Col i y -> Maybe y
<*> Col PSequence i y
cl
    = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl forall {a}. Additive a => Maybe a -> Maybe a -> Maybe a
add forall a. Maybe a
Nothing
    forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall i x y z.
Ord i =>
(x -> y -> z)
-> (x -> z)
-> (y -> z)
-> PSequence i x
-> PSequence i y
-> PSequence i z
psqInterlace forall {a}. (Multiplicative a, Additive a) => a -> a -> Maybe a
mlt (forall b a. b -> a -> b
const forall a. Maybe a
Nothing) (forall b a. b -> a -> b
const forall a. Maybe a
Nothing) PSequence i y
rw PSequence i y
cl

  mlt :: a -> a -> Maybe a
mlt a
x a
y = if forall a. Additive a => a -> Bool
isZero a
xy then forall a. Maybe a
Nothing else (forall a. a -> Maybe a
Just a
xy) where xy :: a
xy = a
xforall c. Multiplicative c => c -> c -> c
*a
y
  
  add :: Maybe a -> Maybe a -> Maybe a
add Maybe a
Nothing Maybe a
y         = Maybe a
y
  add Maybe a
x Maybe a
Nothing         = Maybe a
x
  add (Just a
x) (Just a
y) = if forall a. Additive a => a -> Bool
isZero a
xy then forall a. Maybe a
Nothing else (forall a. a -> Maybe a
Just a
xy) where xy :: a
xy = a
xforall a. Additive a => a -> a -> a
+a
y
  
--------------------------------------------------------------------------------
-- crets -

-- | the entries given by a column of rows.
crets :: Col i (Row j x) -> Entries i j x
crets :: forall i j x. Col i (Row j x) -> Entries i j x
crets (Col PSequence i (Row j x)
rws) = forall i j x. PSequence (i, j) x -> Entries i j x
Entries forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall i x. [(x, i)] -> PSequence i x
PSequence [(x, (i, j))]
ets where
  ets :: [(x, (i, j))]
ets = forall (m :: * -> *) a. Monad m => m (m a) -> m a
join forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (\(Row j x
rw,i
i) -> (forall {a} {i} {x}. a -> Row i x -> [(x, (a, i))]
xijs i
i Row j x
rw)) forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall i x. PSequence i x -> [(x, i)]
psqxs PSequence i (Row j x)
rws
  xijs :: a -> Row i x -> [(x, (a, i))]
xijs a
i (Row PSequence i x
xj) = forall a b. (a -> b) -> [a] -> [b]
map (\(x
x,i
j) -> (x
x,(a
i,i
j))) forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall i x. PSequence i x -> [(x, i)]
psqxs PSequence i x
xj

--------------------------------------------------------------------------------
-- rcets -

-- | the entries given by a row of columns.
rcets :: (Ord i, Ord j) => Row j (Col i x) -> Entries i j x
rcets :: forall i j x. (Ord i, Ord j) => Row j (Col i x) -> Entries i j x
rcets = forall i j x. PSequence (i, j) x -> Entries i j x
Entries forall (c :: * -> * -> *) y z x.
Category c =>
c y z -> c x y -> c x z
. forall i x. Ord i => (x -> x -> x) -> [(x, i)] -> PSequence i x
psequence (forall a b c. ((a, b) -> c) -> a -> b -> c
curry forall a b. (a, b) -> a
fst) forall (c :: * -> * -> *) y z x.
Category c =>
c y z -> c x y -> c x z
. forall i x j. [(Col i x, j)] -> [(x, (i, j))]
ets forall (c :: * -> * -> *) y z x.
Category c =>
c y z -> c x y -> c x z
. forall j x. Row j x -> [(x, j)]
rowxs where
  ets :: [(Col i x,j)] -> [(x,(i,j))]
  ets :: forall i x j. [(Col i x, j)] -> [(x, (i, j))]
ets = forall (m :: * -> *) a. Monad m => m (m a) -> m a
join forall (c :: * -> * -> *) y z x.
Category c =>
c y z -> c x y -> c x z
. forall a b. (a -> b) -> [a] -> [b]
map (\(Col i x
cl,j
j) -> forall a b. (a -> b) -> [a] -> [b]
map (\(x
x,i
i) -> (x
x,(i
i,j
j))) forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall i x. Col i x -> [(x, i)]
colxs Col i x
cl)

--------------------------------------------------------------------------------
-- etsJoin -

-- | joining entries of entries.
--
--  __Property__ Let @xs' = 'etsJoin' r c xs@
--
--  [Pre] For all @(xij,i,j)@ in @xs@ holds:
--
--        (1) @i '<' 'lengthN' r@ and @j '<' 'lengthN' c@
--
--        (2) For all @(_,i',j')@ in @xij@ holds: @i' '<' ri@ and @j' '<' cj@ where
--        @..ri.. = r@, @..cj.. = c@.
--
--  [Post] @xs'@ is 'valid'.
etsJoin :: (i ~ N, j ~ N)
  => ProductSymbol i -> ProductSymbol j -> Entries i j (Entries i j x) -> Entries i j x
etsJoin :: forall i j x.
(i ~ N, j ~ N) =>
ProductSymbol i
-> ProductSymbol j -> Entries i j (Entries i j x) -> Entries i j x
etsJoin ProductSymbol i
r ProductSymbol j
c Entries i j (Entries i j x)
es = forall i j x. Col i (Row j x) -> Entries i j x
crets Col N (Row N x)
es' where
  es' :: Col N (Row N x)
es' = forall i j o x.
(i ~ N, j ~ N, o ~ N) =>
[(o, i)]
-> [(o, j)] -> [(Row j (Entries i j x), i)] -> Col i (Row j x)
join (forall {t} {b}. Additive t => t -> [(t, b)] -> [(t, b)]
accum i
0 (forall x. ProductSymbol x -> [(x, N)]
psyxs ProductSymbol i
r))
             (forall {t} {b}. Additive t => t -> [(t, b)] -> [(t, b)]
accum i
0 (forall x. ProductSymbol x -> [(x, N)]
psyxs ProductSymbol j
c))
             (forall i x. Col i x -> [(x, i)]
colxs forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall i j x. Eq i => Entries i j x -> Col i (Row j x)
etscr Entries i j (Entries i j x)
es)

  accum :: t -> [(t, b)] -> [(t, b)]
accum t
_ []          = []
  accum t
o ((t
d,b
i):[(t, b)]
dis) = (t
o,b
i)forall a. a -> [a] -> [a]
:t -> [(t, b)] -> [(t, b)]
accum (t
oforall a. Additive a => a -> a -> a
+t
d) [(t, b)]
dis

  join :: (i ~ N, j ~ N, o ~ N)
    => [(o,i)] -> [(o,j)]
    -> [(Row j (Entries i j x),i)] -> Col i (Row j x)
  join :: forall i j o x.
(i ~ N, j ~ N, o ~ N) =>
[(o, i)]
-> [(o, j)] -> [(Row j (Entries i j x), i)] -> Col i (Row j x)
join [(o, i)]
_ [(o, j)]
_ []                                  = forall i x. Col i x
colEmpty
  join ((o
_,i
i):[(o, i)]
ois) [(o, j)]
ojs rws :: [(Row j (Entries i j x), i)]
rws@((Row j (Entries i j x)
_,i
i'):[(Row j (Entries i j x), i)]
_) | i
i forall a. Ord a => a -> a -> Bool
< i
i' = forall i j o x.
(i ~ N, j ~ N, o ~ N) =>
[(o, i)]
-> [(o, j)] -> [(Row j (Entries i j x), i)] -> Col i (Row j x)
join [(o, i)]
ois [(o, j)]
ojs [(Row j (Entries i j x), i)]
rws
  join ((o
o,i
_):[(o, i)]
ois) [(o, j)]
ojs ((Row j (Entries i j x)
rw,i
_):[(Row j (Entries i j x), i)]
rws)            =  forall i j o x.
(i ~ N, j ~ N, o ~ N) =>
o -> [(o, j)] -> Row j (Entries i j x) -> Col i (Row j x)
joinRow o
o [(o, j)]
ojs Row j (Entries i j x)
rw
                                               forall i x. Col i x -> Col i x -> Col i x
`colAppend` forall i j o x.
(i ~ N, j ~ N, o ~ N) =>
[(o, i)]
-> [(o, j)] -> [(Row j (Entries i j x), i)] -> Col i (Row j x)
join [(o, i)]
ois [(o, j)]
ojs [(Row j (Entries i j x), i)]
rws

  joinRow :: (i ~ N, j ~ N, o ~ N)
    => o -> [(o,j)] -> Row j (Entries i j x) -> Col i (Row j x)
  joinRow :: forall i j o x.
(i ~ N, j ~ N, o ~ N) =>
o -> [(o, j)] -> Row j (Entries i j x) -> Col i (Row j x)
joinRow o
oi [(o, j)]
ojs Row j (Entries i j x)
rw = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl forall i j x.
Ord i =>
Col i (Row j x) -> Col i (Row j x) -> Col i (Row j x)
(||) forall i x. Col i x
colEmpty forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall i j o x.
(i ~ N, j ~ N, o ~ N) =>
o -> [(o, j)] -> Row j (Entries i j x) -> [Col i (Row j x)]
rwShift o
oi [(o, j)]
ojs Row j (Entries i j x)
rw

  rwShift :: (i ~ N, j ~ N, o ~ N)
    => o -> [(o,j)] -> Row j (Entries i j x) -> [Col i (Row j x)]
  rwShift :: forall i j o x.
(i ~ N, j ~ N, o ~ N) =>
o -> [(o, j)] -> Row j (Entries i j x) -> [Col i (Row j x)]
rwShift o
oi [(o, j)]
ojs Row j (Entries i j x)
rw = [(j, j)] -> [(Entries o j x, j)] -> [Col o (Row j x)]
shRow [(o, j)]
ojs (forall j x. Row j x -> [(x, j)]
rowxs Row j (Entries i j x)
rw) where
    shRow :: [(j, j)] -> [(Entries o j x, j)] -> [Col o (Row j x)]
shRow [(j, j)]
_ []                               = []
    shRow ((j
_,j
j):[(j, j)]
ojs) rw :: [(Entries o j x, j)]
rw@((Entries o j x
_,j
j'):[(Entries o j x, j)]
_) | j
j forall a. Ord a => a -> a -> Bool
< j
j' = [(j, j)] -> [(Entries o j x, j)] -> [Col o (Row j x)]
shRow [(j, j)]
ojs [(Entries o j x, j)]
rw
    shRow ((j
oj,j
_):[(j, j)]
ojs) ((Entries o j x
xs,j
_):[(Entries o j x, j)]
rw)           = forall {i} {j} {y}.
(Number i, Number j) =>
i -> j -> Col i (Row j y) -> Col i (Row j y)
shColRow o
oi j
oj (forall i j x. Eq i => Entries i j x -> Col i (Row j x)
etscr Entries o j x
xs) forall a. a -> [a] -> [a]
: [(j, j)] -> [(Entries o j x, j)] -> [Col o (Row j x)]
shRow [(j, j)]
ojs [(Entries o j x, j)]
rw

    shColRow :: i -> j -> Col i (Row j y) -> Col i (Row j y)
shColRow i
oi j
oj = forall i x y. Number i => i -> ((x, i) -> y) -> Col i x -> Col i y
colMapShift i
oi (forall j x y. Number j => j -> ((x, j) -> y) -> Row j x -> Row j y
rowMapShift j
oj forall a b. (a, b) -> a
fst forall (c :: * -> * -> *) y z x.
Category c =>
c y z -> c x y -> c x z
. forall a b. (a, b) -> a
fst)

  (||) :: Ord i => Col i (Row j x) -> Col i (Row j x) -> Col i (Row j x)
  || :: forall i j x.
Ord i =>
Col i (Row j x) -> Col i (Row j x) -> Col i (Row j x)
(||) = forall i x y z.
Ord i =>
(x -> y -> z)
-> (x -> z) -> (y -> z) -> Col i x -> Col i y -> Col i z
colInterlace forall j x. Row j x -> Row j x -> Row j x
rowAppend forall x. x -> x
id forall x. x -> x
id

--------------------------------------------------------------------------------
-- crHeadColAt -

-- | get the head column at @j@.
--
--   __Pre__ for all @j'@ in @rws@ holds: @j '<=' j'@.
crHeadColAt :: Eq j => j -> Col i (Row j a) -> Col i a
crHeadColAt :: forall j i a. Eq j => j -> Col i (Row j a) -> Col i a
crHeadColAt j
j Col i (Row j a)
rws
  = forall i x. PSequence i x -> Col i x
Col forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall i x. [(x, i)] -> PSequence i x
PSequence
  forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (\((a
a,j
_),i
i) -> (a
a,i
i))
  forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall a. (a -> Bool) -> [a] -> [a]
filter ((j
jforall a. Eq a => a -> a -> Bool
==)forall (c :: * -> * -> *) y z x.
Category c =>
c y z -> c x y -> c x z
.forall a b. (a, b) -> b
sndforall (c :: * -> * -> *) y z x.
Category c =>
c y z -> c x y -> c x z
.forall a b. (a, b) -> a
fst)
  forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall i x. Col i x -> [(x, i)]
colxs
  forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall j x. Row j x -> (x, j)
rowHead
  forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall x i. (x -> Bool) -> Col i x -> Col i x
colFilter (forall b. Boolean b => b -> b
not forall (c :: * -> * -> *) y z x.
Category c =>
c y z -> c x y -> c x z
. forall j x. Row j x -> Bool
rowIsEmpty)  Col i (Row j a)
rws

--------------------------------------------------------------------------------
-- crHeadRowAt -

-- | get the head row at @i@.
--
--   __Pre__ for all @i'@ in @rws@ holdst: @i '<=' i'@.
crHeadRowAt :: Eq i => i -> Col i (Row j a) -> Row j a
crHeadRowAt :: forall i j a. Eq i => i -> Col i (Row j a) -> Row j a
crHeadRowAt i
i Col i (Row j a)
rws = if i
i forall a. Eq a => a -> a -> Bool
== i
i' then Row j a
rw else forall j x. Row j x
rowEmpty where
  (Row j a
rw,i
i') = forall i x. Col i x -> (x, i)
colHead Col i (Row j a)
rws