{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveFunctor, PartialTypeSignatures, PatternSynonyms #-}
{-# LANGUAGE StandaloneDeriving, TypeOperators, ViewPatterns       #-}
{-# OPTIONS_GHC -rtsopts -threaded -fno-liberate-case     #-}
{-# OPTIONS_GHC  -fmax-simplifier-iterations=20 #-}
{-# OPTIONS_GHC -funfolding-keeness-factor1000 -optlo-O3 #-}
#ifndef mingw32_HOST_OS
{-# OPTIONS_GHC -fllvm #-}
#endif
{-# OPTIONS_GHC -fsimplifier-phases=3  #-}
{-# OPTIONS_GHC -funfolding-use-threshold1000                   #-}
module Algebra.Matrix.RepaIntMap
  ( RIMMatrix', RIMMatrix, URIMMatrix, DRIMMatrix, fromRows
  , delayMatrix, forceToVPar, forceToVSeq
  , gaussReductionD, gaussReductionP, gaussReductionS
  ) where
import           Algebra.Matrix.Generic      (Column, Matrix, Mutable, Row,
                                              WrapImmutable)
import qualified Algebra.Matrix.Generic      as GM
import           Algebra.Prelude.Core        hiding (Max, Min, traverse)
import           Control.DeepSeq             (NFData (..))
import           Control.Lens                (ifoldMap, imap, (%=), _1, _2)
import           Control.Monad.State
import           Data.Array.Repa             as Repa
import           Data.Array.Repa.Eval        as Repa hiding (zero)
import           Data.Array.Repa.Repr.Vector as Repa
import           Data.Functor.Identity       (runIdentity)
import           Data.IntMap                 (IntMap)
import qualified Data.IntMap                 as IM
import qualified Data.IntSet                 as IS
import           Data.Ord                    (Down (..))
import           Data.Semigroup              (Min (..), Option (..))
import qualified Data.Vector                 as V

type RIMMatrix  = RIMMatrix' V
type URIMMatrix = RIMMatrix' U
type DRIMMatrix = RIMMatrix' D

data RIMMatrix' repr a = RIM { RIMMatrix' repr a -> Int
_rimColCount :: Int
                             , RIMMatrix' repr a -> Array repr (Z :. Int) (IntMap a)
_rimRows :: Array repr (Z :. Int) (IntMap a)
                             }

instance (NFData a) => NFData (RIMMatrix' V a) where
  rnf :: RIMMatrix' V a -> ()
rnf (RIM Int
i Array V (Z :. Int) (IntMap a)
arr) = Int -> ()
forall a. NFData a => a -> ()
rnf Int
i () -> () -> ()
`seq` Vector (IntMap a) -> ()
forall a. NFData a => a -> ()
rnf (Array V (Z :. Int) (IntMap a) -> Vector (IntMap a)
forall sh e. Array V sh e -> Vector e
Repa.toVector Array V (Z :. Int) (IntMap a)
arr)

instance (NFData a) => NFData (RIMMatrix' U a) where
  rnf :: RIMMatrix' U a -> ()
rnf (RIM Int
i Array U (Z :. Int) (IntMap a)
arr) = Int -> ()
forall a. NFData a => a -> ()
rnf Int
i () -> () -> ()
`seq` Vector (IntMap a) -> ()
forall a. NFData a => a -> ()
rnf (Array U (Z :. Int) (IntMap a) -> Vector (IntMap a)
forall sh e. Array U sh e -> Vector e
Repa.toUnboxed Array U (Z :. Int) (IntMap a)
arr)

instance (NFData a) => NFData (RIMMatrix' D a) where
  rnf :: RIMMatrix' D a -> ()
rnf (RIM Int
i Array D (Z :. Int) (IntMap a)
arr) =
    let (Z
Z :. Int
j, (Z :. Int) -> IntMap a
fun) = Array D (Z :. Int) (IntMap a) -> (Z :. Int, (Z :. Int) -> IntMap a)
forall sh r1 a.
(Shape sh, Source r1 a) =>
Array r1 sh a -> (sh, sh -> a)
toFunction Array D (Z :. Int) (IntMap a)
arr
    in Int -> ()
forall a. NFData a => a -> ()
rnf Int
i () -> () -> ()
`seq` Int -> ()
forall a. NFData a => a -> ()
rnf Int
j () -> () -> ()
`seq` ((Z :. Int) -> IntMap a) -> ()
forall a. NFData a => a -> ()
rnf (Z :. Int) -> IntMap a
fun

deriving instance (Eq a, Source repr (IntMap a)) => Eq (RIMMatrix' repr a)

instance (Monoidal a, Show a, Source repr (IntMap a)) => Show (RIMMatrix' repr a) where
  showsPrec :: Int -> RIMMatrix' repr a -> ShowS
showsPrec Int
d = Int -> [Vector a] -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec Int
d ([Vector a] -> ShowS)
-> (RIMMatrix' repr a -> [Vector a]) -> RIMMatrix' repr a -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RIMMatrix' repr a -> [Vector a]
forall a r.
(Monoidal a, Source r (IntMap a)) =>
RIMMatrix' r a -> [Vector a]
toRows

forceToVPar :: DRIMMatrix a -> RIMMatrix a
forceToVPar :: DRIMMatrix a -> RIMMatrix a
forceToVPar = (Array D (Z :. Int) (IntMap a) -> Array V (Z :. Int) (IntMap a))
-> DRIMMatrix a -> RIMMatrix a
forall rep a rep'.
(Array rep (Z :. Int) (IntMap a)
 -> Array rep' (Z :. Int) (IntMap a))
-> RIMMatrix' rep a -> RIMMatrix' rep' a
withArray (Identity (Array V (Z :. Int) (IntMap a))
-> Array V (Z :. Int) (IntMap a)
forall a. Identity a -> a
runIdentity (Identity (Array V (Z :. Int) (IntMap a))
 -> Array V (Z :. Int) (IntMap a))
-> (Array D (Z :. Int) (IntMap a)
    -> Identity (Array V (Z :. Int) (IntMap a)))
-> Array D (Z :. Int) (IntMap a)
-> Array V (Z :. Int) (IntMap a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Array D (Z :. Int) (IntMap a)
-> Identity (Array V (Z :. Int) (IntMap a))
forall r1 sh e r2 (m :: * -> *).
(Load r1 sh e, Target r2 e, Source r2 e, Monad m) =>
Array r1 sh e -> m (Array r2 sh e)
computeP)

forceToVSeq :: DRIMMatrix a -> RIMMatrix a
forceToVSeq :: DRIMMatrix a -> RIMMatrix a
forceToVSeq = (Array D (Z :. Int) (IntMap a) -> Array V (Z :. Int) (IntMap a))
-> DRIMMatrix a -> RIMMatrix a
forall rep a rep'.
(Array rep (Z :. Int) (IntMap a)
 -> Array rep' (Z :. Int) (IntMap a))
-> RIMMatrix' rep a -> RIMMatrix' rep' a
withArray Array D (Z :. Int) (IntMap a) -> Array V (Z :. Int) (IntMap a)
forall r1 sh e r2.
(Load r1 sh e, Target r2 e) =>
Array r1 sh e -> Array r2 sh e
computeS

toRows :: (Monoidal a, Source r (IntMap a)) => RIMMatrix' r a -> [Vector a]
toRows :: RIMMatrix' r a -> [Vector a]
toRows (RIM Int
c Array r (Z :. Int) (IntMap a)
rs) = Array D (Z :. Int) (Vector a) -> [Vector a]
forall sh r e. (Shape sh, Source r e) => Array r sh e -> [e]
toList (Array D (Z :. Int) (Vector a) -> [Vector a])
-> Array D (Z :. Int) (Vector a) -> [Vector a]
forall a b. (a -> b) -> a -> b
$ (IntMap a -> Vector a)
-> Array r (Z :. Int) (IntMap a) -> Array D (Z :. Int) (Vector a)
forall sh r a b.
(Shape sh, Source r a) =>
(a -> b) -> Array r sh a -> Array D sh b
Repa.map (Int -> IntMap a -> Vector a
forall a. Monoidal a => Int -> IntMap a -> Vector a
dicToRow Int
c) Array r (Z :. Int) (IntMap a)
rs

dicToRow :: Monoidal a => Int -> IntMap a -> Vector a
dicToRow :: Int -> IntMap a -> Vector a
dicToRow Int
c IntMap a
d = Int -> (Int -> a) -> Vector a
forall a. Int -> (Int -> a) -> Vector a
V.generate Int
c ((Int -> a) -> Vector a) -> (Int -> a) -> Vector a
forall a b. (a -> b) -> a -> b
$ \Int
i -> a -> Maybe a -> a
forall a. a -> Maybe a -> a
fromMaybe a
forall m. Monoidal m => m
zero (Maybe a -> a) -> Maybe a -> a
forall a b. (a -> b) -> a -> b
$ Int -> IntMap a -> Maybe a
forall a. Int -> IntMap a -> Maybe a
IM.lookup Int
i IntMap a
d

fromRows :: DecidableZero a => [Vector a] -> RIMMatrix a
fromRows :: [Vector a] -> RIMMatrix a
fromRows [Vector a]
vs =
  let cols :: Int
cols = Vector a -> Int
forall a. Vector a -> Int
V.length (Vector a -> Int) -> Vector a -> Int
forall a b. (a -> b) -> a -> b
$ [Vector a] -> Vector a
forall a. [a] -> a
head [Vector a]
vs
      rows :: Int
rows = [Vector a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Vector a]
vs
  in Int -> Array V (Z :. Int) (IntMap a) -> RIMMatrix a
forall repr a.
Int -> Array repr (Z :. Int) (IntMap a) -> RIMMatrix' repr a
RIM Int
cols (Array V (Z :. Int) (IntMap a) -> RIMMatrix a)
-> Array V (Z :. Int) (IntMap a) -> RIMMatrix a
forall a b. (a -> b) -> a -> b
$
     (Z :. Int) -> [IntMap a] -> Array V (Z :. Int) (IntMap a)
forall sh a. Shape sh => sh -> [a] -> Array V sh a
fromListVector (Z
Z Z -> Int -> Z :. Int
forall tail head. tail -> head -> tail :. head
:. Int
rows) ([IntMap a] -> Array V (Z :. Int) (IntMap a))
-> [IntMap a] -> Array V (Z :. Int) (IntMap a)
forall a b. (a -> b) -> a -> b
$ (Vector a -> IntMap a) -> [Vector a] -> [IntMap a]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Vector a -> IntMap a
forall a. DecidableZero a => Vector a -> IntMap a
vecToIM [Vector a]
vs

vecToIM :: DecidableZero a => Vector a -> IntMap a
vecToIM :: Vector a -> IntMap a
vecToIM = [(Int, a)] -> IntMap a
forall a. [(Int, a)] -> IntMap a
IM.fromList ([(Int, a)] -> IntMap a)
-> (Vector a -> [(Int, a)]) -> Vector a -> IntMap a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Maybe (Int, a)] -> [(Int, a)]
forall a. [Maybe a] -> [a]
catMaybes ([Maybe (Int, a)] -> [(Int, a)])
-> (Vector a -> [Maybe (Int, a)]) -> Vector a -> [(Int, a)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int -> a -> Maybe (Int, a)) -> [a] -> [Maybe (Int, a)]
forall i (f :: * -> *) a b.
FunctorWithIndex i f =>
(i -> a -> b) -> f a -> f b
imap (\Int
i a
v -> (,) Int
i (a -> (Int, a)) -> Maybe a -> Maybe (Int, a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> Maybe a
forall a. DecidableZero a => a -> Maybe a
guardZero a
v) ([a] -> [Maybe (Int, a)])
-> (Vector a -> [a]) -> Vector a -> [Maybe (Int, a)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector a -> [a]
forall a. Vector a -> [a]
V.toList

indexed :: (Source r b)
        => Array r DIM1 b -> Array D DIM1 (Int, b)
indexed :: Array r (Z :. Int) b -> Array D (Z :. Int) (Int, b)
indexed Array r (Z :. Int) b
xs = Array r (Z :. Int) b
-> ((Z :. Int) -> Z :. Int)
-> (((Z :. Int) -> b) -> (Z :. Int) -> (Int, b))
-> Array D (Z :. Int) (Int, b)
forall r sh sh' a b.
(Source r a, Shape sh) =>
Array r sh a
-> (sh -> sh') -> ((sh -> a) -> sh' -> b) -> Array D sh' b
traverse Array r (Z :. Int) b
xs (Z :. Int) -> Z :. Int
forall a. a -> a
id (\(Z :. Int) -> b
f (Z
Z :. Int
i) -> (Int
i, (Z :. Int) -> b
f (Z
Z Z -> Int -> Z :. Int
forall tail head. tail -> head -> tail :. head
:.Int
i)))

data Weighted w a = Weighted { Weighted w a -> w
getWeight :: w
                             , Weighted w a -> a
unWeight :: a
                             }
                    deriving (ReadPrec [Weighted w a]
ReadPrec (Weighted w a)
Int -> ReadS (Weighted w a)
ReadS [Weighted w a]
(Int -> ReadS (Weighted w a))
-> ReadS [Weighted w a]
-> ReadPrec (Weighted w a)
-> ReadPrec [Weighted w a]
-> Read (Weighted w a)
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
forall w a. (Read w, Read a) => ReadPrec [Weighted w a]
forall w a. (Read w, Read a) => ReadPrec (Weighted w a)
forall w a. (Read w, Read a) => Int -> ReadS (Weighted w a)
forall w a. (Read w, Read a) => ReadS [Weighted w a]
readListPrec :: ReadPrec [Weighted w a]
$creadListPrec :: forall w a. (Read w, Read a) => ReadPrec [Weighted w a]
readPrec :: ReadPrec (Weighted w a)
$creadPrec :: forall w a. (Read w, Read a) => ReadPrec (Weighted w a)
readList :: ReadS [Weighted w a]
$creadList :: forall w a. (Read w, Read a) => ReadS [Weighted w a]
readsPrec :: Int -> ReadS (Weighted w a)
$creadsPrec :: forall w a. (Read w, Read a) => Int -> ReadS (Weighted w a)
Read, Int -> Weighted w a -> ShowS
[Weighted w a] -> ShowS
Weighted w a -> String
(Int -> Weighted w a -> ShowS)
-> (Weighted w a -> String)
-> ([Weighted w a] -> ShowS)
-> Show (Weighted w a)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall w a. (Show w, Show a) => Int -> Weighted w a -> ShowS
forall w a. (Show w, Show a) => [Weighted w a] -> ShowS
forall w a. (Show w, Show a) => Weighted w a -> String
showList :: [Weighted w a] -> ShowS
$cshowList :: forall w a. (Show w, Show a) => [Weighted w a] -> ShowS
show :: Weighted w a -> String
$cshow :: forall w a. (Show w, Show a) => Weighted w a -> String
showsPrec :: Int -> Weighted w a -> ShowS
$cshowsPrec :: forall w a. (Show w, Show a) => Int -> Weighted w a -> ShowS
Show, a -> Weighted w b -> Weighted w a
(a -> b) -> Weighted w a -> Weighted w b
(forall a b. (a -> b) -> Weighted w a -> Weighted w b)
-> (forall a b. a -> Weighted w b -> Weighted w a)
-> Functor (Weighted w)
forall a b. a -> Weighted w b -> Weighted w a
forall a b. (a -> b) -> Weighted w a -> Weighted w b
forall w a b. a -> Weighted w b -> Weighted w a
forall w a b. (a -> b) -> Weighted w a -> Weighted w b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> Weighted w b -> Weighted w a
$c<$ :: forall w a b. a -> Weighted w b -> Weighted w a
fmap :: (a -> b) -> Weighted w a -> Weighted w b
$cfmap :: forall w a b. (a -> b) -> Weighted w a -> Weighted w b
Functor)

instance Eq a => Eq (Weighted a b) where
  == :: Weighted a b -> Weighted a b -> Bool
(==) = a -> a -> Bool
forall a. Eq a => a -> a -> Bool
(==) (a -> a -> Bool)
-> (Weighted a b -> a) -> Weighted a b -> Weighted a b -> Bool
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` Weighted a b -> a
forall w a. Weighted w a -> w
getWeight

instance Ord a => Ord (Weighted a b) where
  compare :: Weighted a b -> Weighted a b -> Ordering
compare = (Weighted a b -> a) -> Weighted a b -> Weighted a b -> Ordering
forall a b. Ord a => (b -> a) -> b -> b -> Ordering
comparing Weighted a b -> a
forall w a. Weighted w a -> w
getWeight

type Trivial a = Weighted () a

guardZero :: DecidableZero a => a -> Maybe a
guardZero :: a -> Maybe a
guardZero a
c | a -> Bool
forall r. DecidableZero r => r -> Bool
isZero a
c = Maybe a
forall a. Maybe a
Nothing
            | Bool
otherwise = a -> Maybe a
forall a. a -> Maybe a
Just a
c

{-# INLINE withArray #-}
withArray :: (Array rep DIM1 (IntMap a) -> Array rep' DIM1 (IntMap a))
          -> RIMMatrix' rep a -> RIMMatrix' rep' a
withArray :: (Array rep (Z :. Int) (IntMap a)
 -> Array rep' (Z :. Int) (IntMap a))
-> RIMMatrix' rep a -> RIMMatrix' rep' a
withArray Array rep (Z :. Int) (IntMap a) -> Array rep' (Z :. Int) (IntMap a)
f (RIM Int
c Array rep (Z :. Int) (IntMap a)
rs) = Int -> Array rep' (Z :. Int) (IntMap a) -> RIMMatrix' rep' a
forall repr a.
Int -> Array repr (Z :. Int) (IntMap a) -> RIMMatrix' repr a
RIM Int
c (Array rep (Z :. Int) (IntMap a) -> Array rep' (Z :. Int) (IntMap a)
f Array rep (Z :. Int) (IntMap a)
rs)

delayMatrix :: Source rep (IntMap a) => RIMMatrix' rep a -> RIMMatrix' D a
delayMatrix :: RIMMatrix' rep a -> RIMMatrix' D a
delayMatrix = (Array rep (Z :. Int) (IntMap a) -> Array D (Z :. Int) (IntMap a))
-> RIMMatrix' rep a -> RIMMatrix' D a
forall rep a rep'.
(Array rep (Z :. Int) (IntMap a)
 -> Array rep' (Z :. Int) (IntMap a))
-> RIMMatrix' rep a -> RIMMatrix' rep' a
withArray Array rep (Z :. Int) (IntMap a) -> Array D (Z :. Int) (IntMap a)
forall sh r e.
(Shape sh, Source r e) =>
Array r sh e -> Array D sh e
delay

-- | Perofms row echelon reduction, sequentially
gaussReductionS :: (Field a, DecidableZero a, Normed a)
                => DRIMMatrix a -> RIMMatrix a
gaussReductionS :: DRIMMatrix a -> RIMMatrix a
gaussReductionS = (Array D (Z :. Int) (IntMap a) -> Array V (Z :. Int) (IntMap a))
-> DRIMMatrix a -> RIMMatrix a
forall rep a rep'.
(Array rep (Z :. Int) (IntMap a)
 -> Array rep' (Z :. Int) (IntMap a))
-> RIMMatrix' rep a -> RIMMatrix' rep' a
withArray Array D (Z :. Int) (IntMap a) -> Array V (Z :. Int) (IntMap a)
forall r1 sh e r2.
(Load r1 sh e, Target r2 e) =>
Array r1 sh e -> Array r2 sh e
computeS (DRIMMatrix a -> RIMMatrix a)
-> (DRIMMatrix a -> DRIMMatrix a) -> DRIMMatrix a -> RIMMatrix a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DRIMMatrix a -> DRIMMatrix a
forall a repr.
(DecidableZero a, Normed a, Field a, Source repr (IntMap a)) =>
RIMMatrix' repr a -> RIMMatrix' D a
gaussReductionD

-- | Perofms row echelon reduction, parallelly
gaussReductionP :: (Field a, DecidableZero a, Normed a)
                => DRIMMatrix a -> RIMMatrix a
gaussReductionP :: DRIMMatrix a -> RIMMatrix a
gaussReductionP = (Array D (Z :. Int) (IntMap a) -> Array V (Z :. Int) (IntMap a))
-> DRIMMatrix a -> RIMMatrix a
forall rep a rep'.
(Array rep (Z :. Int) (IntMap a)
 -> Array rep' (Z :. Int) (IntMap a))
-> RIMMatrix' rep a -> RIMMatrix' rep' a
withArray (Identity (Array V (Z :. Int) (IntMap a))
-> Array V (Z :. Int) (IntMap a)
forall a. Identity a -> a
runIdentity (Identity (Array V (Z :. Int) (IntMap a))
 -> Array V (Z :. Int) (IntMap a))
-> (Array D (Z :. Int) (IntMap a)
    -> Identity (Array V (Z :. Int) (IntMap a)))
-> Array D (Z :. Int) (IntMap a)
-> Array V (Z :. Int) (IntMap a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Array D (Z :. Int) (IntMap a)
-> Identity (Array V (Z :. Int) (IntMap a))
forall r1 sh e r2 (m :: * -> *).
(Load r1 sh e, Target r2 e, Source r2 e, Monad m) =>
Array r1 sh e -> m (Array r2 sh e)
computeP) (DRIMMatrix a -> RIMMatrix a)
-> (DRIMMatrix a -> DRIMMatrix a) -> DRIMMatrix a -> RIMMatrix a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DRIMMatrix a -> DRIMMatrix a
forall a repr.
(DecidableZero a, Normed a, Field a, Source repr (IntMap a)) =>
RIMMatrix' repr a -> RIMMatrix' D a
gaussReductionD

rankG :: Normed a
      => IntSet ->  Int -> IntMap a
      -> Option (Min (Int, Weighted (Down (Norm a)) a, Int, Trivial (IntMap a)))
rankG :: IntSet
-> Int
-> IntMap a
-> Option
     (Min (Int, Weighted (Down (Norm a)) a, Int, Trivial (IntMap a)))
rankG IntSet
remain Int
i IntMap a
dic
  | Int
i Int -> IntSet -> Bool
`IS.notMember` IntSet
remain = Maybe
  (Min (Int, Weighted (Down (Norm a)) a, Int, Trivial (IntMap a)))
-> Option
     (Min (Int, Weighted (Down (Norm a)) a, Int, Trivial (IntMap a)))
forall a. Maybe a -> Option a
Option Maybe
  (Min (Int, Weighted (Down (Norm a)) a, Int, Trivial (IntMap a)))
forall a. Maybe a
Nothing
  | Bool
otherwise = Maybe
  (Min (Int, Weighted (Down (Norm a)) a, Int, Trivial (IntMap a)))
-> Option
     (Min (Int, Weighted (Down (Norm a)) a, Int, Trivial (IntMap a)))
forall a. Maybe a -> Option a
Option (Maybe
   (Min (Int, Weighted (Down (Norm a)) a, Int, Trivial (IntMap a)))
 -> Option
      (Min (Int, Weighted (Down (Norm a)) a, Int, Trivial (IntMap a))))
-> Maybe
     (Min (Int, Weighted (Down (Norm a)) a, Int, Trivial (IntMap a)))
-> Option
     (Min (Int, Weighted (Down (Norm a)) a, Int, Trivial (IntMap a)))
forall a b. (a -> b) -> a -> b
$ do
  ((Int
k, a
v), IntMap a
_) <- IntMap a -> Maybe ((Int, a), IntMap a)
forall a. IntMap a -> Maybe ((Int, a), IntMap a)
IM.minViewWithKey IntMap a
dic
  Min (Int, Weighted (Down (Norm a)) a, Int, Trivial (IntMap a))
-> Maybe
     (Min (Int, Weighted (Down (Norm a)) a, Int, Trivial (IntMap a)))
forall (m :: * -> *) a. Monad m => a -> m a
return ((Int, Weighted (Down (Norm a)) a, Int, Trivial (IntMap a))
-> Min (Int, Weighted (Down (Norm a)) a, Int, Trivial (IntMap a))
forall a. a -> Min a
Min (Int
k, Down (Norm a) -> a -> Weighted (Down (Norm a)) a
forall w a. w -> a -> Weighted w a
Weighted (Norm a -> Down (Norm a)
forall a. a -> Down a
Down (Norm a -> Down (Norm a)) -> Norm a -> Down (Norm a)
forall a b. (a -> b) -> a -> b
$ a -> Norm a
forall a. Normed a => a -> Norm a
norm a
v) a
v, Int
i, () -> IntMap a -> Trivial (IntMap a)
forall w a. w -> a -> Weighted w a
Weighted () IntMap a
dic))

rowCount :: Source r (IntMap a) => RIMMatrix' r a -> Int
rowCount :: RIMMatrix' r a -> Int
rowCount RIMMatrix' r a
m = let (Z
Z :. Int
n) = Array r (Z :. Int) (IntMap a) -> Z :. Int
forall r e sh. (Source r e, Shape sh) => Array r sh e -> sh
extent (Array r (Z :. Int) (IntMap a) -> Z :. Int)
-> Array r (Z :. Int) (IntMap a) -> Z :. Int
forall a b. (a -> b) -> a -> b
$ RIMMatrix' r a -> Array r (Z :. Int) (IntMap a)
forall repr a.
RIMMatrix' repr a -> Array repr (Z :. Int) (IntMap a)
_rimRows RIMMatrix' r a
m in Int
n

gaussReductionD :: (DecidableZero a, Normed a , Field a, Source repr (IntMap a))
                => RIMMatrix' repr a -> RIMMatrix' D a
gaussReductionD :: RIMMatrix' repr a -> RIMMatrix' D a
gaussReductionD matr :: RIMMatrix' repr a
matr@(RIM Int
nCol Array repr (Z :. Int) (IntMap a)
r0) = Int -> Array D (Z :. Int) (IntMap a) -> RIMMatrix' D a
forall repr a.
Int -> Array repr (Z :. Int) (IntMap a) -> RIMMatrix' repr a
RIM Int
nCol (Array D (Z :. Int) (IntMap a) -> RIMMatrix' D a)
-> Array D (Z :. Int) (IntMap a) -> RIMMatrix' D a
forall a b. (a -> b) -> a -> b
$ (Array D (Z :. Int) (IntMap a), IntSet)
-> Array D (Z :. Int) (IntMap a)
forall a b. (a, b) -> a
fst ((Array D (Z :. Int) (IntMap a), IntSet)
 -> Array D (Z :. Int) (IntMap a))
-> (Array D (Z :. Int) (IntMap a), IntSet)
-> Array D (Z :. Int) (IntMap a)
forall a b. (a -> b) -> a -> b
$ State (Array D (Z :. Int) (IntMap a), IntSet) ()
-> (Array D (Z :. Int) (IntMap a), IntSet)
-> (Array D (Z :. Int) (IntMap a), IntSet)
forall s a. State s a -> s -> s
execState State (Array D (Z :. Int) (IntMap a), IntSet) ()
loop (Array repr (Z :. Int) (IntMap a) -> Array D (Z :. Int) (IntMap a)
forall sh r e.
(Shape sh, Source r e) =>
Array r sh e -> Array D sh e
delay Array repr (Z :. Int) (IntMap a)
r0, [Int] -> IntSet
IS.fromList [Int
0..Int
nRow Int -> Int -> Int
forall r. Group r => r -> r -> r
- Int
1])
  where
    nRow :: Int
nRow = RIMMatrix' repr a -> Int
forall r a. Source r (IntMap a) => RIMMatrix' r a -> Int
rowCount RIMMatrix' repr a
matr
    loop :: State (Array D (Z :. Int) (IntMap a), IntSet) ()
loop = do
      (Array D (Z :. Int) (IntMap a)
rs, IntSet
remain) <- StateT
  (Array D (Z :. Int) (IntMap a), IntSet)
  Identity
  (Array D (Z :. Int) (IntMap a), IntSet)
forall s (m :: * -> *). MonadState s m => m s
get
      Bool
-> State (Array D (Z :. Int) (IntMap a), IntSet) ()
-> State (Array D (Z :. Int) (IntMap a), IntSet) ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (IntSet -> Bool
IS.null IntSet
remain) (State (Array D (Z :. Int) (IntMap a), IntSet) ()
 -> State (Array D (Z :. Int) (IntMap a), IntSet) ())
-> State (Array D (Z :. Int) (IntMap a), IntSet) ()
-> State (Array D (Z :. Int) (IntMap a), IntSet) ()
forall a b. (a -> b) -> a -> b
$
        case Option
  (Min
     (Int, Weighted (Down (Norm a)) a, Int, Weighted () (IntMap a)))
-> Maybe
     (Min
        (Int, Weighted (Down (Norm a)) a, Int, Weighted () (IntMap a)))
forall a. Option a -> Maybe a
getOption (Option
   (Min
      (Int, Weighted (Down (Norm a)) a, Int, Weighted () (IntMap a)))
 -> Maybe
      (Min
         (Int, Weighted (Down (Norm a)) a, Int, Weighted () (IntMap a))))
-> Option
     (Min
        (Int, Weighted (Down (Norm a)) a, Int, Weighted () (IntMap a)))
-> Maybe
     (Min
        (Int, Weighted (Down (Norm a)) a, Int, Weighted () (IntMap a)))
forall a b. (a -> b) -> a -> b
$ (Int
 -> IntMap a
 -> Option
      (Min
         (Int, Weighted (Down (Norm a)) a, Int, Weighted () (IntMap a))))
-> [IntMap a]
-> Option
     (Min
        (Int, Weighted (Down (Norm a)) a, Int, Weighted () (IntMap a)))
forall i (f :: * -> *) m a.
(FoldableWithIndex i f, Monoid m) =>
(i -> a -> m) -> f a -> m
ifoldMap (IntSet
-> Int
-> IntMap a
-> Option
     (Min
        (Int, Weighted (Down (Norm a)) a, Int, Weighted () (IntMap a)))
forall a.
Normed a =>
IntSet
-> Int
-> IntMap a
-> Option
     (Min (Int, Weighted (Down (Norm a)) a, Int, Trivial (IntMap a)))
rankG IntSet
remain) ([IntMap a]
 -> Option
      (Min
         (Int, Weighted (Down (Norm a)) a, Int, Weighted () (IntMap a))))
-> [IntMap a]
-> Option
     (Min
        (Int, Weighted (Down (Norm a)) a, Int, Weighted () (IntMap a)))
forall a b. (a -> b) -> a -> b
$ Array D (Z :. Int) (IntMap a) -> [IntMap a]
forall sh r e. (Shape sh, Source r e) => Array r sh e -> [e]
toList Array D (Z :. Int) (IntMap a)
rs of
          Maybe
  (Min
     (Int, Weighted (Down (Norm a)) a, Int, Weighted () (IntMap a)))
Nothing -> () -> State (Array D (Z :. Int) (IntMap a), IntSet) ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
          Just (Min (Int
col, Weighted Down (Norm a)
_ a
newC, Int
k, Weighted () (IntMap a) -> IntMap a
forall w a. Weighted w a -> a
unWeight -> IntMap a
dic)) -> do
            (IntSet -> Identity IntSet)
-> (Array D (Z :. Int) (IntMap a), IntSet)
-> Identity (Array D (Z :. Int) (IntMap a), IntSet)
forall s t a b. Field2 s t a b => Lens s t a b
_2 ((IntSet -> Identity IntSet)
 -> (Array D (Z :. Int) (IntMap a), IntSet)
 -> Identity (Array D (Z :. Int) (IntMap a), IntSet))
-> (IntSet -> IntSet)
-> State (Array D (Z :. Int) (IntMap a), IntSet) ()
forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a b -> (a -> b) -> m ()
%= Int -> IntSet -> IntSet
IS.delete Int
k
            let cases :: Int -> IntMap a -> IntMap a
cases Int
i IntMap a
d
                  | Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
k = (a -> a) -> IntMap a -> IntMap a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (a -> a -> a
forall r. Division r => r -> r -> r
/a
newC) IntMap a
d
                  | Bool
otherwise =
                    case Int -> IntMap a -> Maybe a
forall a. Int -> IntMap a -> Maybe a
IM.lookup Int
col IntMap a
d of
                      Maybe a
Nothing -> IntMap a
d
                      Just a
c ->
                        let scale :: a
scale = a
c a -> a -> a
forall r. Multiplicative r => r -> r -> r
* a -> a
forall r. Division r => r -> r
recip a
newC
                        in (a -> Bool) -> IntMap a -> IntMap a
forall a. (a -> Bool) -> IntMap a -> IntMap a
IM.filter (Bool -> Bool
not (Bool -> Bool) -> (a -> Bool) -> a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bool
forall r. DecidableZero r => r -> Bool
isZero) (IntMap a -> IntMap a) -> IntMap a -> IntMap a
forall a b. (a -> b) -> a -> b
$
                           (a -> a -> a) -> IntMap a -> IntMap a -> IntMap a
forall a. (a -> a -> a) -> IntMap a -> IntMap a -> IntMap a
IM.unionWith (-) IntMap a
d (IntMap a -> IntMap a) -> IntMap a -> IntMap a
forall a b. (a -> b) -> a -> b
$
                           (a -> a) -> IntMap a -> IntMap a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (a
scale a -> a -> a
forall r. Multiplicative r => r -> r -> r
*) IntMap a
dic
            (Array D (Z :. Int) (IntMap a)
 -> Identity (Array D (Z :. Int) (IntMap a)))
-> (Array D (Z :. Int) (IntMap a), IntSet)
-> Identity (Array D (Z :. Int) (IntMap a), IntSet)
forall s t a b. Field1 s t a b => Lens s t a b
_1 ((Array D (Z :. Int) (IntMap a)
  -> Identity (Array D (Z :. Int) (IntMap a)))
 -> (Array D (Z :. Int) (IntMap a), IntSet)
 -> Identity (Array D (Z :. Int) (IntMap a), IntSet))
-> (Array D (Z :. Int) (IntMap a) -> Array D (Z :. Int) (IntMap a))
-> State (Array D (Z :. Int) (IntMap a), IntSet) ()
forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a b -> (a -> b) -> m ()
%= ((Int, IntMap a) -> IntMap a)
-> Array D (Z :. Int) (Int, IntMap a)
-> Array D (Z :. Int) (IntMap a)
forall sh r a b.
(Shape sh, Source r a) =>
(a -> b) -> Array r sh a -> Array D sh b
Repa.map ((Int -> IntMap a -> IntMap a) -> (Int, IntMap a) -> IntMap a
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Int -> IntMap a -> IntMap a
cases) (Array D (Z :. Int) (Int, IntMap a)
 -> Array D (Z :. Int) (IntMap a))
-> (Array D (Z :. Int) (IntMap a)
    -> Array D (Z :. Int) (Int, IntMap a))
-> Array D (Z :. Int) (IntMap a)
-> Array D (Z :. Int) (IntMap a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Array D (Z :. Int) (IntMap a) -> Array D (Z :. Int) (Int, IntMap a)
forall r b.
Source r b =>
Array r (Z :. Int) b -> Array D (Z :. Int) (Int, b)
indexed
            State (Array D (Z :. Int) (IntMap a), IntSet) ()
loop

type instance Mutable (RIMMatrix' r) = WrapImmutable (RIMMatrix' r)
type instance Row (RIMMatrix' r) = Vector
type instance Column (RIMMatrix' r) = Vector

lookCoe :: Monoidal c => Int -> IntMap c -> c
lookCoe :: Int -> IntMap c -> c
lookCoe Int
i = c -> Maybe c -> c
forall a. a -> Maybe a -> a
fromMaybe c
forall m. Monoidal m => m
zero (Maybe c -> c) -> (IntMap c -> Maybe c) -> IntMap c -> c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> IntMap c -> Maybe c
forall a. Int -> IntMap a -> Maybe a
IM.lookup Int
i

updateAtRowIndex :: Source r1 (IntMap a) => RIMMatrix' r1 a -> Int -> (IntMap a -> IntMap a) -> RIMMatrix' D a
updateAtRowIndex :: RIMMatrix' r1 a -> Int -> (IntMap a -> IntMap a) -> RIMMatrix' D a
updateAtRowIndex (RIM Int
c Array r1 (Z :. Int) (IntMap a)
m) Int
i IntMap a -> IntMap a
f = Int -> Array D (Z :. Int) (IntMap a) -> RIMMatrix' D a
forall repr a.
Int -> Array repr (Z :. Int) (IntMap a) -> RIMMatrix' repr a
RIM Int
c (Array D (Z :. Int) (IntMap a) -> RIMMatrix' D a)
-> Array D (Z :. Int) (IntMap a) -> RIMMatrix' D a
forall a b. (a -> b) -> a -> b
$
  let (Z :. Int
sh, (Z :. Int) -> IntMap a
fun) = Array r1 (Z :. Int) (IntMap a)
-> (Z :. Int, (Z :. Int) -> IntMap a)
forall sh r1 a.
(Shape sh, Source r1 a) =>
Array r1 sh a -> (sh, sh -> a)
toFunction Array r1 (Z :. Int) (IntMap a)
m
  in (Z :. Int)
-> ((Z :. Int) -> IntMap a) -> Array D (Z :. Int) (IntMap a)
forall sh a. sh -> (sh -> a) -> Array D sh a
fromFunction Z :. Int
sh (((Z :. Int) -> IntMap a) -> Array D (Z :. Int) (IntMap a))
-> ((Z :. Int) -> IntMap a) -> Array D (Z :. Int) (IntMap a)
forall a b. (a -> b) -> a -> b
$ \(Z
Z :. Int
k) ->
    if Int
k Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
i
    then IntMap a -> IntMap a
f (IntMap a -> IntMap a) -> IntMap a -> IntMap a
forall a b. (a -> b) -> a -> b
$ (Z :. Int) -> IntMap a
fun ((Z :. Int) -> IntMap a) -> (Z :. Int) -> IntMap a
forall a b. (a -> b) -> a -> b
$ Z
Z Z -> Int -> Z :. Int
forall tail head. tail -> head -> tail :. head
:. Int
k
    else (Z :. Int) -> IntMap a
fun ((Z :. Int) -> IntMap a) -> (Z :. Int) -> IntMap a
forall a b. (a -> b) -> a -> b
$ Z
Z Z -> Int -> Z :. Int
forall tail head. tail -> head -> tail :. head
:. Int
k
{-# INLINE updateAtRowIndex #-}

instance (DecidableZero a) => Matrix (RIMMatrix' D) a where
  {-# SPECIALISE instance DecidableZero a => Matrix (RIMMatrix' D) a #-}
  basicRowCount :: RIMMatrix' D a -> Int
basicRowCount = RIMMatrix' D a -> Int
forall r a. Source r (IntMap a) => RIMMatrix' r a -> Int
rowCount
  basicColumnCount :: RIMMatrix' D a -> Int
basicColumnCount = RIMMatrix' D a -> Int
forall repr a. RIMMatrix' repr a -> Int
_rimColCount
  basicUnsafeIndexM :: RIMMatrix' D a -> Int -> Int -> m a
basicUnsafeIndexM (RIM Int
_ Array D (Z :. Int) (IntMap a)
m) Int
i Int
j =
    a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> m a) -> a -> m a
forall a b. (a -> b) -> a -> b
$ Int -> IntMap a -> a
forall c. Monoidal c => Int -> IntMap c -> c
lookCoe Int
j (IntMap a -> a) -> IntMap a -> a
forall a b. (a -> b) -> a -> b
$ Array D (Z :. Int) (IntMap a)
m Array D (Z :. Int) (IntMap a) -> (Z :. Int) -> IntMap a
forall sh r e. (Shape sh, Source r e) => Array r sh e -> sh -> e
Repa.! (Z
Z Z -> Int -> Z :. Int
forall tail head. tail -> head -> tail :. head
:. Int
i)
  basicUnsafeGetRowM :: RIMMatrix' D a -> Int -> m (Row (RIMMatrix' D) a)
basicUnsafeGetRowM (RIM Int
c Array D (Z :. Int) (IntMap a)
m) Int
i = Vector a -> m (Vector a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Vector a -> m (Vector a)) -> Vector a -> m (Vector a)
forall a b. (a -> b) -> a -> b
$ Int -> IntMap a -> Vector a
forall a. Monoidal a => Int -> IntMap a -> Vector a
dicToRow Int
c (IntMap a -> Vector a) -> IntMap a -> Vector a
forall a b. (a -> b) -> a -> b
$ Array D (Z :. Int) (IntMap a)
m Array D (Z :. Int) (IntMap a) -> (Z :. Int) -> IntMap a
forall sh r e. (Shape sh, Source r e) => Array r sh e -> sh -> e
Repa.! (Z
Z Z -> Int -> Z :. Int
forall tail head. tail -> head -> tail :. head
:. Int
i)
  basicUnsafeGetColumnM :: RIMMatrix' D a -> Int -> m (Column (RIMMatrix' D) a)
basicUnsafeGetColumnM (RIM Int
_ Array D (Z :. Int) (IntMap a)
m) Int
i = Array V (Z :. Int) a -> Vector a
forall sh e. Array V sh e -> Vector e
toVector (Array V (Z :. Int) a -> Vector a)
-> m (Array V (Z :. Int) a) -> m (Vector a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Array D (Z :. Int) a -> m (Array V (Z :. Int) a)
forall r1 sh e r2 (m :: * -> *).
(Load r1 sh e, Target r2 e, Source r2 e, Monad m) =>
Array r1 sh e -> m (Array r2 sh e)
computeP ((IntMap a -> a)
-> Array D (Z :. Int) (IntMap a) -> Array D (Z :. Int) a
forall sh r a b.
(Shape sh, Source r a) =>
(a -> b) -> Array r sh a -> Array D sh b
Repa.map (Int -> IntMap a -> a
forall c. Monoidal c => Int -> IntMap c -> c
lookCoe Int
i) Array D (Z :. Int) (IntMap a)
m)
  unsafeGenerate :: Int -> Int -> (Int -> Int -> a) -> RIMMatrix' D a
unsafeGenerate Int
rs Int
cs Int -> Int -> a
f = Int -> Array D (Z :. Int) (IntMap a) -> RIMMatrix' D a
forall repr a.
Int -> Array repr (Z :. Int) (IntMap a) -> RIMMatrix' repr a
RIM Int
cs (Array D (Z :. Int) (IntMap a) -> RIMMatrix' D a)
-> Array D (Z :. Int) (IntMap a) -> RIMMatrix' D a
forall a b. (a -> b) -> a -> b
$ (Z :. Int)
-> ((Z :. Int) -> IntMap a) -> Array D (Z :. Int) (IntMap a)
forall sh a. sh -> (sh -> a) -> Array D sh a
fromFunction (Z
Z Z -> Int -> Z :. Int
forall tail head. tail -> head -> tail :. head
:. Int
rs) (((Z :. Int) -> IntMap a) -> Array D (Z :. Int) (IntMap a))
-> ((Z :. Int) -> IntMap a) -> Array D (Z :. Int) (IntMap a)
forall a b. (a -> b) -> a -> b
$ \(Z
Z :. Int
i) ->
    Vector a -> IntMap a
forall a. DecidableZero a => Vector a -> IntMap a
vecToIM (Vector a -> IntMap a) -> Vector a -> IntMap a
forall a b. (a -> b) -> a -> b
$ Int -> (Int -> a) -> Vector a
forall a. Int -> (Int -> a) -> Vector a
V.generate Int
cs ((Int -> a) -> Vector a) -> (Int -> a) -> Vector a
forall a b. (a -> b) -> a -> b
$ \Int
j -> Int -> Int -> a
f Int
i Int
j
  unsafeWrite :: RIMMatrix' D a -> Int -> Int -> a -> RIMMatrix' D a
unsafeWrite RIMMatrix' D a
m Int
i Int
j a
x = ((Array D (Z :. Int) (IntMap a) -> Array D (Z :. Int) (IntMap a))
 -> RIMMatrix' D a -> RIMMatrix' D a)
-> RIMMatrix' D a
-> (Array D (Z :. Int) (IntMap a) -> Array D (Z :. Int) (IntMap a))
-> RIMMatrix' D a
forall a b c. (a -> b -> c) -> b -> a -> c
flip (Array D (Z :. Int) (IntMap a) -> Array D (Z :. Int) (IntMap a))
-> RIMMatrix' D a -> RIMMatrix' D a
forall rep a rep'.
(Array rep (Z :. Int) (IntMap a)
 -> Array rep' (Z :. Int) (IntMap a))
-> RIMMatrix' rep a -> RIMMatrix' rep' a
withArray RIMMatrix' D a
m ((Array D (Z :. Int) (IntMap a) -> Array D (Z :. Int) (IntMap a))
 -> RIMMatrix' D a)
-> (Array D (Z :. Int) (IntMap a) -> Array D (Z :. Int) (IntMap a))
-> RIMMatrix' D a
forall a b. (a -> b) -> a -> b
$ \Array D (Z :. Int) (IntMap a)
n ->
    let (Z :. Int
sh, (Z :. Int) -> IntMap a
fun) = Array D (Z :. Int) (IntMap a) -> (Z :. Int, (Z :. Int) -> IntMap a)
forall sh r1 a.
(Shape sh, Source r1 a) =>
Array r1 sh a -> (sh, sh -> a)
toFunction Array D (Z :. Int) (IntMap a)
n
    in (Z :. Int)
-> ((Z :. Int) -> IntMap a) -> Array D (Z :. Int) (IntMap a)
forall sh a. sh -> (sh -> a) -> Array D sh a
fromFunction Z :. Int
sh (((Z :. Int) -> IntMap a) -> Array D (Z :. Int) (IntMap a))
-> ((Z :. Int) -> IntMap a) -> Array D (Z :. Int) (IntMap a)
forall a b. (a -> b) -> a -> b
$ \(Z
Z :. Int
k) ->
      if Int
k Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
i
      then if a -> Bool
forall r. DecidableZero r => r -> Bool
isZero a
x
      then Int -> IntMap a -> IntMap a
forall a. Int -> IntMap a -> IntMap a
IM.delete Int
j (IntMap a -> IntMap a) -> IntMap a -> IntMap a
forall a b. (a -> b) -> a -> b
$ (Z :. Int) -> IntMap a
fun ((Z :. Int) -> IntMap a) -> (Z :. Int) -> IntMap a
forall a b. (a -> b) -> a -> b
$ Z
ZZ -> Int -> Z :. Int
forall tail head. tail -> head -> tail :. head
:. Int
k
      else Int -> a -> IntMap a -> IntMap a
forall a. Int -> a -> IntMap a -> IntMap a
IM.insert Int
j a
x (IntMap a -> IntMap a) -> IntMap a -> IntMap a
forall a b. (a -> b) -> a -> b
$ (Z :. Int) -> IntMap a
fun ((Z :. Int) -> IntMap a) -> (Z :. Int) -> IntMap a
forall a b. (a -> b) -> a -> b
$ Z
ZZ -> Int -> Z :. Int
forall tail head. tail -> head -> tail :. head
:. Int
k
      else (Z :. Int) -> IntMap a
fun ((Z :. Int) -> IntMap a) -> (Z :. Int) -> IntMap a
forall a b. (a -> b) -> a -> b
$ Z
Z Z -> Int -> Z :. Int
forall tail head. tail -> head -> tail :. head
:. Int
k
  unsafeFromRows :: [Row (RIMMatrix' D) a] -> RIMMatrix' D a
unsafeFromRows = (Array V (Z :. Int) (IntMap a) -> Array D (Z :. Int) (IntMap a))
-> RIMMatrix' V a -> RIMMatrix' D a
forall rep a rep'.
(Array rep (Z :. Int) (IntMap a)
 -> Array rep' (Z :. Int) (IntMap a))
-> RIMMatrix' rep a -> RIMMatrix' rep' a
withArray Array V (Z :. Int) (IntMap a) -> Array D (Z :. Int) (IntMap a)
forall sh r e.
(Shape sh, Source r e) =>
Array r sh e -> Array D sh e
delay (RIMMatrix' V a -> RIMMatrix' D a)
-> ([Vector a] -> RIMMatrix' V a) -> [Vector a] -> RIMMatrix' D a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Vector a] -> RIMMatrix' V a
forall a. DecidableZero a => [Vector a] -> RIMMatrix a
fromRows
  unsafeFromColumns :: [Column (RIMMatrix' D) a] -> RIMMatrix' D a
unsafeFromColumns [Column (RIMMatrix' D) a]
cs0 =
    let cs :: Vector (Vector a)
cs = [Vector a] -> Vector (Vector a)
forall a. [a] -> Vector a
V.fromList [Vector a]
[Column (RIMMatrix' D) a]
cs0
        ncol :: Int
ncol = Vector (Vector a) -> Int
forall a. Vector a -> Int
V.length Vector (Vector a)
cs
    in Int -> Array D (Z :. Int) (IntMap a) -> RIMMatrix' D a
forall repr a.
Int -> Array repr (Z :. Int) (IntMap a) -> RIMMatrix' repr a
RIM Int
ncol (Array D (Z :. Int) (IntMap a) -> RIMMatrix' D a)
-> Array D (Z :. Int) (IntMap a) -> RIMMatrix' D a
forall a b. (a -> b) -> a -> b
$ (Z :. Int)
-> ((Z :. Int) -> IntMap a) -> Array D (Z :. Int) (IntMap a)
forall sh a. sh -> (sh -> a) -> Array D sh a
fromFunction (Z
Z Z -> Int -> Z :. Int
forall tail head. tail -> head -> tail :. head
:. Vector a -> Int
forall a. Vector a -> Int
V.length ([Vector a] -> Vector a
forall a. [a] -> a
head [Vector a]
[Column (RIMMatrix' D) a]
cs0)) (((Z :. Int) -> IntMap a) -> Array D (Z :. Int) (IntMap a))
-> ((Z :. Int) -> IntMap a) -> Array D (Z :. Int) (IntMap a)
forall a b. (a -> b) -> a -> b
$ \(Z
Z :. Int
i) ->
      [(Int, a)] -> IntMap a
forall a. [(Int, a)] -> IntMap a
IM.fromList [ (Int
i, a
c)
                  | Int
j <- [Int
0.. Int
ncol Int -> Int -> Int
forall r. Group r => r -> r -> r
- Int
1]
                  , let c :: a
c = Vector (Vector a)
cs Vector (Vector a) -> Int -> Vector a
forall a. Vector a -> Int -> a
V.! Int
j Vector a -> Int -> a
forall a. Vector a -> Int -> a
V.! Int
i
                  , Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ a -> Bool
forall r. DecidableZero r => r -> Bool
isZero a
c
                  ]
  toRows :: RIMMatrix' D a -> [Row (RIMMatrix' D) a]
toRows = RIMMatrix' D a -> [Row (RIMMatrix' D) a]
forall a r.
(Monoidal a, Source r (IntMap a)) =>
RIMMatrix' r a -> [Vector a]
toRows
  toColumns :: RIMMatrix' D a -> [Column (RIMMatrix' D) a]
toColumns (RIM Int
c Array D (Z :. Int) (IntMap a)
m) =
    (Int -> Vector a) -> [Int] -> [Vector a]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\Int
j -> (IntMap a -> a) -> Vector (IntMap a) -> Vector a
forall a b. (a -> b) -> Vector a -> Vector b
V.map (Int -> IntMap a -> a
forall c. Monoidal c => Int -> IntMap c -> c
lookCoe Int
j) (Vector (IntMap a) -> Vector a) -> Vector (IntMap a) -> Vector a
forall a b. (a -> b) -> a -> b
$ Array V (Z :. Int) (IntMap a) -> Vector (IntMap a)
forall sh e. Array V sh e -> Vector e
toVector (Array V (Z :. Int) (IntMap a) -> Vector (IntMap a))
-> Array V (Z :. Int) (IntMap a) -> Vector (IntMap a)
forall a b. (a -> b) -> a -> b
$ Identity (Array V (Z :. Int) (IntMap a))
-> Array V (Z :. Int) (IntMap a)
forall a. Identity a -> a
runIdentity (Identity (Array V (Z :. Int) (IntMap a))
 -> Array V (Z :. Int) (IntMap a))
-> Identity (Array V (Z :. Int) (IntMap a))
-> Array V (Z :. Int) (IntMap a)
forall a b. (a -> b) -> a -> b
$ Array D (Z :. Int) (IntMap a)
-> Identity (Array V (Z :. Int) (IntMap a))
forall r1 sh e r2 (m :: * -> *).
(Load r1 sh e, Target r2 e, Source r2 e, Monad m) =>
Array r1 sh e -> m (Array r2 sh e)
computeP Array D (Z :. Int) (IntMap a)
m) [Int
0.. Int
c Int -> Int -> Int
forall r. Group r => r -> r -> r
- Int
1]

  swapRows :: RIMMatrix' D a -> Int -> Int -> RIMMatrix' D a
swapRows RIMMatrix' D a
im Int
i Int
j = ((Array D (Z :. Int) (IntMap a) -> Array D (Z :. Int) (IntMap a))
 -> RIMMatrix' D a -> RIMMatrix' D a)
-> RIMMatrix' D a
-> (Array D (Z :. Int) (IntMap a) -> Array D (Z :. Int) (IntMap a))
-> RIMMatrix' D a
forall a b c. (a -> b -> c) -> b -> a -> c
flip (Array D (Z :. Int) (IntMap a) -> Array D (Z :. Int) (IntMap a))
-> RIMMatrix' D a -> RIMMatrix' D a
forall rep a rep'.
(Array rep (Z :. Int) (IntMap a)
 -> Array rep' (Z :. Int) (IntMap a))
-> RIMMatrix' rep a -> RIMMatrix' rep' a
withArray RIMMatrix' D a
im ((Array D (Z :. Int) (IntMap a) -> Array D (Z :. Int) (IntMap a))
 -> RIMMatrix' D a)
-> (Array D (Z :. Int) (IntMap a) -> Array D (Z :. Int) (IntMap a))
-> RIMMatrix' D a
forall a b. (a -> b) -> a -> b
$ \Array D (Z :. Int) (IntMap a)
m ->
    let (Z :. Int
sh, (Z :. Int) -> IntMap a
fun) = Array D (Z :. Int) (IntMap a) -> (Z :. Int, (Z :. Int) -> IntMap a)
forall sh r1 a.
(Shape sh, Source r1 a) =>
Array r1 sh a -> (sh, sh -> a)
toFunction Array D (Z :. Int) (IntMap a)
m
    in (Z :. Int)
-> ((Z :. Int) -> IntMap a) -> Array D (Z :. Int) (IntMap a)
forall sh a. sh -> (sh -> a) -> Array D sh a
fromFunction Z :. Int
sh (((Z :. Int) -> IntMap a) -> Array D (Z :. Int) (IntMap a))
-> ((Z :. Int) -> IntMap a) -> Array D (Z :. Int) (IntMap a)
forall a b. (a -> b) -> a -> b
$ \(Z
Z :. Int
k) ->
      if Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
k
      then (Z :. Int) -> IntMap a
fun (Z
Z Z -> Int -> Z :. Int
forall tail head. tail -> head -> tail :. head
:. Int
j)
      else if Int
j Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
k
      then (Z :. Int) -> IntMap a
fun (Z
Z Z -> Int -> Z :. Int
forall tail head. tail -> head -> tail :. head
:. Int
i)
      else (Z :. Int) -> IntMap a
fun (Z
Z Z -> Int -> Z :. Int
forall tail head. tail -> head -> tail :. head
:. Int
k)
  scaleRow :: RIMMatrix' D a -> Int -> a -> RIMMatrix' D a
scaleRow RIMMatrix' D a
im Int
i a
c = RIMMatrix' D a -> Int -> (IntMap a -> IntMap a) -> RIMMatrix' D a
forall r1 a.
Source r1 (IntMap a) =>
RIMMatrix' r1 a -> Int -> (IntMap a -> IntMap a) -> RIMMatrix' D a
updateAtRowIndex RIMMatrix' D a
im Int
i ((IntMap a -> IntMap a) -> RIMMatrix' D a)
-> (IntMap a -> IntMap a) -> RIMMatrix' D a
forall a b. (a -> b) -> a -> b
$ \IntMap a
d ->
      if a -> Bool
forall r. DecidableZero r => r -> Bool
isZero a
c
      then IntMap a
forall a. IntMap a
IM.empty
      else (a -> a) -> IntMap a -> IntMap a
forall a b. (a -> b) -> IntMap a -> IntMap b
IM.map (a
c a -> a -> a
forall r. Multiplicative r => r -> r -> r
*) IntMap a
d
  unsafeIMapRow :: RIMMatrix' D a -> Int -> (Int -> a -> a) -> RIMMatrix' D a
unsafeIMapRow RIMMatrix' D a
m Int
i Int -> a -> a
f =
    RIMMatrix' D a -> Int -> (IntMap a -> IntMap a) -> RIMMatrix' D a
forall r1 a.
Source r1 (IntMap a) =>
RIMMatrix' r1 a -> Int -> (IntMap a -> IntMap a) -> RIMMatrix' D a
updateAtRowIndex RIMMatrix' D a
m Int
i ((IntMap a -> IntMap a) -> RIMMatrix' D a)
-> (IntMap a -> IntMap a) -> RIMMatrix' D a
forall a b. (a -> b) -> a -> b
$
    (Int -> a -> Maybe a) -> IntMap a -> IntMap a
forall a b. (Int -> a -> Maybe b) -> IntMap a -> IntMap b
IM.mapMaybeWithKey (\ Int
j -> a -> Maybe a
forall a. DecidableZero a => a -> Maybe a
guardZero (a -> Maybe a) -> (a -> a) -> a -> Maybe a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> a -> a
f Int
j)