{-# LANGUAGE DefaultSignatures, NoMonomorphismRestriction, RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Algebra.Matrix.Generic
( Matrix(..), Column, Row, Size, Index, WrapImmutable, Mutable
, rowCount, columnCount, generate, freeze, thaw
, fromRows, fromColumns, create, (!), wrapImmutable
) where
import Algebra.Matrix.Generic.Base
import Algebra.Matrix.Generic.Mutable (Index, MMatrix, Size)
import qualified Algebra.Matrix.Generic.Mutable as GM
import Algebra.Prelude.Core hiding (Vector, generate)
import Control.Monad.Primitive (PrimMonad, PrimState)
import Control.Monad.ST
import Data.Foldable (foldrM)
import Data.Functor.Identity
import Data.Primitive.MutVar
import Data.Vector.Generic (Mutable, Vector)
import qualified Data.Vector.Generic as GV
class ( Vector (Column mat) a, Vector (Row mat) a
, MMatrix (Mutable mat) a
, Row mat ~ Row (Mutable mat)
, Column mat ~ Column (Mutable mat)
)
=> Matrix mat a where
basicRowCount :: mat a -> Size
basicColumnCount :: mat a -> Size
unsafeFreeze :: PrimMonad m => Mutable mat (PrimState m) a -> m (mat a)
default unsafeFreeze :: (PrimMonad m, Mutable mat ~ WrapImmutable mat)
=> Mutable mat (PrimState m) a -> m (mat a)
unsafeFreeze (WrapImmutable m _ _) = MutVar (PrimState m) (mat a) -> m (mat a)
forall (m :: * -> *) a.
PrimMonad m =>
MutVar (PrimState m) a -> m a
readMutVar MutVar (PrimState m) (mat a)
m
unsafeThaw :: PrimMonad m => mat a -> m (Mutable mat (PrimState m) a)
default unsafeThaw :: (Mutable mat ~ WrapImmutable mat, PrimMonad m)
=> mat a -> m (Mutable mat (PrimState m) a)
unsafeThaw = mat a -> m (Mutable mat (PrimState m) a)
forall (m :: * -> *) (mat :: * -> *) a.
(PrimMonad m, Matrix mat a) =>
mat a -> m (WrapImmutable mat (PrimState m) a)
wrapImmutable
basicUnsafeIndexM :: Monad m => mat a -> Index -> Index -> m a
basicUnsafeGetRowM :: Monad m => mat a -> Index -> m (Row mat a)
basicUnsafeGetRowM mat a
m Index
i = Row (Mutable mat) a -> m (Row (Mutable mat) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Row (Mutable mat) a -> m (Row (Mutable mat) a))
-> Row (Mutable mat) a -> m (Row (Mutable mat) a)
forall a b. (a -> b) -> a -> b
$ (forall s. ST s (Row (Mutable mat) a)) -> Row (Mutable mat) a
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (Row (Mutable mat) a)) -> Row (Mutable mat) a)
-> (forall s. ST s (Row (Mutable mat) a)) -> Row (Mutable mat) a
forall a b. (a -> b) -> a -> b
$
Index
-> Mutable mat (PrimState (ST s)) a -> ST s (Row (Mutable mat) a)
forall (mat :: * -> * -> *) a (m :: * -> *).
(MMatrix mat a, PrimMonad m) =>
Index -> mat (PrimState m) a -> m (Row mat a)
GM.unsafeGetRow Index
i (Mutable mat s a -> ST s (Row (Mutable mat) a))
-> ST s (Mutable mat s a) -> ST s (Row (Mutable mat) a)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< mat a -> ST s (Mutable mat (PrimState (ST s)) a)
forall (mat :: * -> *) a (m :: * -> *).
(Matrix mat a, PrimMonad m) =>
mat a -> m (Mutable mat (PrimState m) a)
unsafeThaw mat a
m
basicUnsafeGetColumnM :: Monad m => mat a -> Index -> m (Column mat a)
basicUnsafeGetColumnM mat a
m Index
i = Column (Mutable mat) a -> m (Column (Mutable mat) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Column (Mutable mat) a -> m (Column (Mutable mat) a))
-> Column (Mutable mat) a -> m (Column (Mutable mat) a)
forall a b. (a -> b) -> a -> b
$ (forall s. ST s (Column (Mutable mat) a)) -> Column (Mutable mat) a
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (Column (Mutable mat) a))
-> Column (Mutable mat) a)
-> (forall s. ST s (Column (Mutable mat) a))
-> Column (Mutable mat) a
forall a b. (a -> b) -> a -> b
$
Index
-> Mutable mat (PrimState (ST s)) a
-> ST s (Column (Mutable mat) a)
forall (mat :: * -> * -> *) a (m :: * -> *).
(MMatrix mat a, PrimMonad m) =>
Index -> mat (PrimState m) a -> m (Column mat a)
GM.unsafeGetColumn Index
i (Mutable mat s a -> ST s (Column (Mutable mat) a))
-> ST s (Mutable mat s a) -> ST s (Column (Mutable mat) a)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< mat a -> ST s (Mutable mat (PrimState (ST s)) a)
forall (mat :: * -> *) a (m :: * -> *).
(Matrix mat a, PrimMonad m) =>
mat a -> m (Mutable mat (PrimState m) a)
unsafeThaw mat a
m
basicUnsafeCopy :: PrimMonad m => Mutable mat (PrimState m) a -> mat a -> m ()
basicUnsafeCopy Mutable mat (PrimState m) a
mmat = Mutable mat (PrimState m) a -> Mutable mat (PrimState m) a -> m ()
forall (mat :: * -> * -> *) a (m :: * -> *).
(MMatrix mat a, PrimMonad m) =>
mat (PrimState m) a -> mat (PrimState m) a -> m ()
GM.unsafeCopy Mutable mat (PrimState m) a
mmat (Mutable mat (PrimState m) a -> m ())
-> (mat a -> m (Mutable mat (PrimState m) a)) -> mat a -> m ()
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< mat a -> m (Mutable mat (PrimState m) a)
forall (mat :: * -> *) a (m :: * -> *).
(Matrix mat a, PrimMonad m) =>
mat a -> m (Mutable mat (PrimState m) a)
unsafeThaw
unsafeGenerate :: Size -> Size -> (Index -> Index -> a) -> mat a
unsafeGenerate Index
w Index
h Index -> Index -> a
f = (forall s. ST s (mat a)) -> mat a
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (mat a)) -> mat a)
-> (forall s. ST s (mat a)) -> mat a
forall a b. (a -> b) -> a -> b
$ Mutable mat s a -> ST s (mat a)
forall (mat :: * -> *) a (m :: * -> *).
(Matrix mat a, PrimMonad m) =>
Mutable mat (PrimState m) a -> m (mat a)
unsafeFreeze (Mutable mat s a -> ST s (mat a))
-> ST s (Mutable mat s a) -> ST s (mat a)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Index
-> Index
-> (Index -> Index -> a)
-> ST s (Mutable mat (PrimState (ST s)) a)
forall (mat :: * -> * -> *) a (m :: * -> *).
(MMatrix mat a, PrimMonad m) =>
Index -> Index -> (Index -> Index -> a) -> m (mat (PrimState m) a)
GM.unsafeGenerate Index
w Index
h Index -> Index -> a
f
unsafeWrite :: mat a -> Index -> Index -> a -> mat a
unsafeWrite mat a
mat Index
i Index
j a
x = mat a -> (forall s. Mutable mat s a -> ST s ()) -> mat a
forall (mat :: * -> *) a.
Matrix mat a =>
mat a -> (forall s. Mutable mat s a -> ST s ()) -> mat a
withMutable mat a
mat ((forall s. Mutable mat s a -> ST s ()) -> mat a)
-> (forall s. Mutable mat s a -> ST s ()) -> mat a
forall a b. (a -> b) -> a -> b
$ \Mutable mat s a
m -> Mutable mat (PrimState (ST s)) a -> Index -> Index -> a -> ST s ()
forall (mat :: * -> * -> *) a (m :: * -> *).
(MMatrix mat a, PrimMonad m) =>
mat (PrimState m) a -> Index -> Index -> a -> m ()
GM.unsafeWrite Mutable mat s a
Mutable mat (PrimState (ST s)) a
m Index
i Index
j a
x
unsafeFromRows :: [Row mat a] -> mat a
unsafeFromRows [Row mat a]
rs = (forall s. ST s (mat a)) -> mat a
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (mat a)) -> mat a)
-> (forall s. ST s (mat a)) -> mat a
forall a b. (a -> b) -> a -> b
$ Mutable mat s a -> ST s (mat a)
forall (mat :: * -> *) a (m :: * -> *).
(Matrix mat a, PrimMonad m) =>
Mutable mat (PrimState m) a -> m (mat a)
unsafeFreeze (Mutable mat s a -> ST s (mat a))
-> ST s (Mutable mat s a) -> ST s (mat a)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< [Row (Mutable mat) a] -> ST s (Mutable mat (PrimState (ST s)) a)
forall (mat :: * -> * -> *) a (m :: * -> *).
(MMatrix mat a, PrimMonad m) =>
[Row mat a] -> m (mat (PrimState m) a)
GM.unsafeFromRows [Row mat a]
[Row (Mutable mat) a]
rs
unsafeFromColumns :: [Column mat a] -> mat a
unsafeFromColumns [Column mat a]
rs = (forall s. ST s (mat a)) -> mat a
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (mat a)) -> mat a)
-> (forall s. ST s (mat a)) -> mat a
forall a b. (a -> b) -> a -> b
$ Mutable mat s a -> ST s (mat a)
forall (mat :: * -> *) a (m :: * -> *).
(Matrix mat a, PrimMonad m) =>
Mutable mat (PrimState m) a -> m (mat a)
unsafeFreeze (Mutable mat s a -> ST s (mat a))
-> ST s (Mutable mat s a) -> ST s (mat a)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< [Column (Mutable mat) a] -> ST s (Mutable mat (PrimState (ST s)) a)
forall (mat :: * -> * -> *) a (m :: * -> *).
(MMatrix mat a, PrimMonad m) =>
[Column mat a] -> m (mat (PrimState m) a)
GM.unsafeFromColumns [Column mat a]
[Column (Mutable mat) a]
rs
toRows :: mat a -> [Row mat a]
toRows mat a
mat = (forall s. ST s [Row (Mutable mat) a]) -> [Row (Mutable mat) a]
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s [Row (Mutable mat) a]) -> [Row (Mutable mat) a])
-> (forall s. ST s [Row (Mutable mat) a]) -> [Row (Mutable mat) a]
forall a b. (a -> b) -> a -> b
$ Mutable mat s a -> ST s [Row (Mutable mat) a]
forall (mat :: * -> * -> *) a (m :: * -> *).
(MMatrix mat a, PrimMonad m) =>
mat (PrimState m) a -> m [Row mat a]
GM.toRows (Mutable mat s a -> ST s [Row (Mutable mat) a])
-> ST s (Mutable mat s a) -> ST s [Row (Mutable mat) a]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< mat a -> ST s (Mutable mat (PrimState (ST s)) a)
forall (mat :: * -> *) a (m :: * -> *).
(Matrix mat a, PrimMonad m) =>
mat a -> m (Mutable mat (PrimState m) a)
unsafeThaw mat a
mat
toColumns ::mat a -> [Column mat a]
toColumns mat a
mat = (forall s. ST s [Column (Mutable mat) a])
-> [Column (Mutable mat) a]
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s [Column (Mutable mat) a])
-> [Column (Mutable mat) a])
-> (forall s. ST s [Column (Mutable mat) a])
-> [Column (Mutable mat) a]
forall a b. (a -> b) -> a -> b
$ Mutable mat s a -> ST s [Column (Mutable mat) a]
forall (mat :: * -> * -> *) a (m :: * -> *).
(MMatrix mat a, PrimMonad m) =>
mat (PrimState m) a -> m [Column mat a]
GM.toColumns (Mutable mat s a -> ST s [Column (Mutable mat) a])
-> ST s (Mutable mat s a) -> ST s [Column (Mutable mat) a]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< mat a -> ST s (Mutable mat (PrimState (ST s)) a)
forall (mat :: * -> *) a (m :: * -> *).
(Matrix mat a, PrimMonad m) =>
mat a -> m (Mutable mat (PrimState m) a)
unsafeThaw mat a
mat
swapRows :: mat a -> Index -> Index -> mat a
swapRows mat a
mat Index
i Index
j = mat a -> (forall s. Mutable mat s a -> ST s ()) -> mat a
forall (mat :: * -> *) a.
Matrix mat a =>
mat a -> (forall s. Mutable mat s a -> ST s ()) -> mat a
withMutable mat a
mat ((forall s. Mutable mat s a -> ST s ()) -> mat a)
-> (forall s. Mutable mat s a -> ST s ()) -> mat a
forall a b. (a -> b) -> a -> b
$ Index -> Index -> Mutable mat (PrimState (ST s)) a -> ST s ()
forall (m :: * -> *) (mat :: * -> * -> *) a.
(PrimMonad m, MMatrix mat a) =>
Index -> Index -> mat (PrimState m) a -> m ()
GM.swapRows Index
i Index
j
scaleRow :: (Commutative a) => mat a -> Index -> a -> mat a
scaleRow mat a
mat Index
i a
c = mat a -> (forall s. Mutable mat s a -> ST s ()) -> mat a
forall (mat :: * -> *) a.
Matrix mat a =>
mat a -> (forall s. Mutable mat s a -> ST s ()) -> mat a
withMutable mat a
mat ((forall s. Mutable mat s a -> ST s ()) -> mat a)
-> (forall s. Mutable mat s a -> ST s ()) -> mat a
forall a b. (a -> b) -> a -> b
$ Index -> a -> Mutable mat (PrimState (ST s)) a -> ST s ()
forall a (mat :: * -> * -> *) (m :: * -> *).
(Multiplicative a, Commutative a, MMatrix mat a, PrimMonad m) =>
Index -> a -> mat (PrimState m) a -> m ()
GM.scaleRow Index
i a
c
unsafeIMapRowM :: Monad m => mat a -> Index -> (Index -> a -> m a) -> m (mat a)
unsafeIMapRowM mat a
mat Index
i Index -> a -> m a
f =
let act :: Index -> mat a -> m (mat a)
act Index
j mat a
m = mat a -> Index -> Index -> a -> mat a
forall (mat :: * -> *) a.
Matrix mat a =>
mat a -> Index -> Index -> a -> mat a
unsafeWrite mat a
m Index
i Index
j (a -> mat a) -> m a -> m (mat a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Index -> a -> m a
f Index
i (a -> m a) -> m a -> m a
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< mat a -> Index -> Index -> m a
forall (mat :: * -> *) a (m :: * -> *).
(Matrix mat a, Monad m) =>
mat a -> Index -> Index -> m a
basicUnsafeIndexM mat a
m Index
i Index
j)
in (Index -> mat a -> m (mat a)) -> mat a -> [Index] -> m (mat a)
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> b -> m b) -> b -> t a -> m b
foldrM Index -> mat a -> m (mat a)
act mat a
mat [Index
0.. mat a -> Index
forall (mat :: * -> *) a. Matrix mat a => mat a -> Index
columnCount mat a
mat Index -> Index -> Index
forall r. Group r => r -> r -> r
- Index
1]
unsafeIMapRowM_ :: Monad m => mat a -> Index -> (Index -> a -> m b) -> m ()
unsafeIMapRowM_ mat a
mat Index
i Index -> a -> m b
f = m (mat a) -> m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (m (mat a) -> m ()) -> m (mat a) -> m ()
forall a b. (a -> b) -> a -> b
$ mat a -> Index -> (Index -> a -> m a) -> m (mat a)
forall (mat :: * -> *) a (m :: * -> *).
(Matrix mat a, Monad m) =>
mat a -> Index -> (Index -> a -> m a) -> m (mat a)
unsafeIMapRowM mat a
mat Index
i (\Index
j a
x -> Index -> a -> m b
f Index
j a
x m b -> m a -> m a
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
x)
unsafeIMapRow :: mat a -> Index -> (Index -> a -> a) -> mat a
unsafeIMapRow mat a
mat Index
i Index -> a -> a
f = mat a -> (forall s. Mutable mat s a -> ST s ()) -> mat a
forall (mat :: * -> *) a.
Matrix mat a =>
mat a -> (forall s. Mutable mat s a -> ST s ()) -> mat a
withMutable mat a
mat ((forall s. Mutable mat s a -> ST s ()) -> mat a)
-> (forall s. Mutable mat s a -> ST s ()) -> mat a
forall a b. (a -> b) -> a -> b
$ \Mutable mat s a
n -> Mutable mat (PrimState (ST s)) a
-> Index -> (Index -> a -> a) -> ST s ()
forall (mat :: * -> * -> *) a (m :: * -> *).
(MMatrix mat a, PrimMonad m) =>
mat (PrimState m) a -> Index -> (Index -> a -> a) -> m ()
GM.basicUnsafeIMapRow Mutable mat s a
Mutable mat (PrimState (ST s)) a
n Index
i Index -> a -> a
f
combineRows :: (Commutative a, Semiring a) => Index -> a -> Index -> mat a -> mat a
combineRows Index
i a
c Index
j mat a
mat = mat a -> (forall s. Mutable mat s a -> ST s ()) -> mat a
forall (mat :: * -> *) a.
Matrix mat a =>
mat a -> (forall s. Mutable mat s a -> ST s ()) -> mat a
withMutable mat a
mat ((forall s. Mutable mat s a -> ST s ()) -> mat a)
-> (forall s. Mutable mat s a -> ST s ()) -> mat a
forall a b. (a -> b) -> a -> b
$ Index -> a -> Index -> Mutable mat (PrimState (ST s)) a -> ST s ()
forall (mat :: * -> * -> *) a (m :: * -> *).
(MMatrix mat a, Semiring a, Commutative a, PrimMonad m) =>
Index -> a -> Index -> mat (PrimState m) a -> m ()
GM.combineRows Index
i a
c Index
j
gaussReduction :: (Eq a, Normed a, Field a) => mat a -> (mat a, mat a, a)
gaussReduction mat a
mat = (forall s. ST s (mat a, mat a, a)) -> (mat a, mat a, a)
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (mat a, mat a, a)) -> (mat a, mat a, a))
-> (forall s. ST s (mat a, mat a, a)) -> (mat a, mat a, a)
forall a b. (a -> b) -> a -> b
$ do
(Mutable mat s a
m', Mutable mat s a
p, a
d) <- Mutable mat s a -> ST s (Mutable mat s a, Mutable mat s a, a)
forall a (m :: * -> *) (mat :: * -> * -> *).
(Eq a, PrimMonad m, Field a, Normed a, MMatrix mat a) =>
mat (PrimState m) a
-> m (mat (PrimState m) a, mat (PrimState m) a, a)
GM.gaussReduction (Mutable mat s a -> ST s (Mutable mat s a, Mutable mat s a, a))
-> ST s (Mutable mat s a)
-> ST s (Mutable mat s a, Mutable mat s a, a)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< mat a -> ST s (Mutable mat (PrimState (ST s)) a)
forall (mat :: * -> *) a (m :: * -> *).
(Matrix mat a, PrimMonad m) =>
mat a -> m (Mutable mat (PrimState m) a)
unsafeThaw mat a
mat
mat a
tmat <- Mutable mat (PrimState (ST s)) a -> ST s (mat a)
forall (mat :: * -> *) a (m :: * -> *).
(Matrix mat a, PrimMonad m) =>
Mutable mat (PrimState m) a -> m (mat a)
unsafeFreeze Mutable mat s a
Mutable mat (PrimState (ST s)) a
m'
mat a
piv <- Mutable mat (PrimState (ST s)) a -> ST s (mat a)
forall (mat :: * -> *) a (m :: * -> *).
(Matrix mat a, PrimMonad m) =>
Mutable mat (PrimState m) a -> m (mat a)
unsafeFreeze Mutable mat s a
Mutable mat (PrimState (ST s)) a
p
(mat a, mat a, a) -> ST s (mat a, mat a, a)
forall (m :: * -> *) a. Monad m => a -> m a
return (mat a
tmat, mat a
piv, a
d)
data WrapImmutable mat s a = WrapImmutable { WrapImmutable mat s a -> MutVar s (mat a)
_getMutVar :: MutVar s (mat a)
, WrapImmutable mat s a -> Index
_wiRowCount :: Size
, WrapImmutable mat s a -> Index
_wiColCount :: Size
}
type instance Column (WrapImmutable mat) = Column mat
type instance Row (WrapImmutable mat) = Row mat
wrapImmutable :: (PrimMonad m, Matrix mat a) => mat a -> m (WrapImmutable mat (PrimState m) a)
wrapImmutable :: mat a -> m (WrapImmutable mat (PrimState m) a)
wrapImmutable mat a
mat = do
MutVar (PrimState m) (mat a)
mvar <- mat a -> m (MutVar (PrimState m) (mat a))
forall (m :: * -> *) a.
PrimMonad m =>
a -> m (MutVar (PrimState m) a)
newMutVar mat a
mat
WrapImmutable mat (PrimState m) a
-> m (WrapImmutable mat (PrimState m) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (WrapImmutable mat (PrimState m) a
-> m (WrapImmutable mat (PrimState m) a))
-> WrapImmutable mat (PrimState m) a
-> m (WrapImmutable mat (PrimState m) a)
forall a b. (a -> b) -> a -> b
$ MutVar (PrimState m) (mat a)
-> Index -> Index -> WrapImmutable mat (PrimState m) a
forall k (mat :: k -> *) s (a :: k).
MutVar s (mat a) -> Index -> Index -> WrapImmutable mat s a
WrapImmutable MutVar (PrimState m) (mat a)
mvar (mat a -> Index
forall (mat :: * -> *) a. Matrix mat a => mat a -> Index
rowCount mat a
mat) (mat a -> Index
forall (mat :: * -> *) a. Matrix mat a => mat a -> Index
columnCount mat a
mat)
instance (Monoidal a, Matrix mat a) => MMatrix (WrapImmutable mat) a where
basicUnsafeNew :: Index -> Index -> m (WrapImmutable mat (PrimState m) a)
basicUnsafeNew Index
n Index
m = mat a -> m (WrapImmutable mat (PrimState m) a)
forall (m :: * -> *) (mat :: * -> *) a.
(PrimMonad m, Matrix mat a) =>
mat a -> m (WrapImmutable mat (PrimState m) a)
wrapImmutable (mat a -> m (WrapImmutable mat (PrimState m) a))
-> mat a -> m (WrapImmutable mat (PrimState m) a)
forall a b. (a -> b) -> a -> b
$ Index -> Index -> (Index -> Index -> a) -> mat a
forall (mat :: * -> *) a.
Matrix mat a =>
Index -> Index -> (Index -> Index -> a) -> mat a
generate Index
n Index
m ((Index -> Index -> a) -> mat a) -> (Index -> Index -> a) -> mat a
forall a b. (a -> b) -> a -> b
$ (Index -> a) -> Index -> Index -> a
forall a b. a -> b -> a
const Index -> a
forall m. Monoidal m => m
zero
basicInitialise :: WrapImmutable mat (PrimState m) a -> m ()
basicInitialise WrapImmutable mat (PrimState m) a
_ = () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
basicRowCount :: WrapImmutable mat s a -> Index
basicRowCount (WrapImmutable MutVar s (mat a)
_ Index
r Index
_) = Index
r
basicColumnCount :: WrapImmutable mat s a -> Index
basicColumnCount (WrapImmutable MutVar s (mat a)
_ Index
_ Index
c) = Index
c
unsafeGetRow :: Index
-> WrapImmutable mat (PrimState m) a
-> m (Row (WrapImmutable mat) a)
unsafeGetRow Index
i (WrapImmutable MutVar (PrimState m) (mat a)
m Index
_ Index
_) = (mat a -> Index -> m (Row (Mutable mat) a))
-> Index -> mat a -> m (Row (Mutable mat) a)
forall a b c. (a -> b -> c) -> b -> a -> c
flip mat a -> Index -> m (Row (Mutable mat) a)
forall (mat :: * -> *) a (m :: * -> *).
(Matrix mat a, Monad m) =>
mat a -> Index -> m (Row mat a)
basicUnsafeGetRowM Index
i (mat a -> m (Row (Mutable mat) a))
-> m (mat a) -> m (Row (Mutable mat) a)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< MutVar (PrimState m) (mat a) -> m (mat a)
forall (m :: * -> *) a.
PrimMonad m =>
MutVar (PrimState m) a -> m a
readMutVar MutVar (PrimState m) (mat a)
m
unsafeGetColumn :: Index
-> WrapImmutable mat (PrimState m) a
-> m (Column (WrapImmutable mat) a)
unsafeGetColumn Index
i (WrapImmutable MutVar (PrimState m) (mat a)
m Index
_ Index
_) = (mat a -> Index -> m (Column (Mutable mat) a))
-> Index -> mat a -> m (Column (Mutable mat) a)
forall a b c. (a -> b -> c) -> b -> a -> c
flip mat a -> Index -> m (Column (Mutable mat) a)
forall (mat :: * -> *) a (m :: * -> *).
(Matrix mat a, Monad m) =>
mat a -> Index -> m (Column mat a)
basicUnsafeGetColumnM Index
i (mat a -> m (Column (Mutable mat) a))
-> m (mat a) -> m (Column (Mutable mat) a)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< MutVar (PrimState m) (mat a) -> m (mat a)
forall (m :: * -> *) a.
PrimMonad m =>
MutVar (PrimState m) a -> m a
readMutVar MutVar (PrimState m) (mat a)
m
unsafeFromRows :: [Row (WrapImmutable mat) a]
-> m (WrapImmutable mat (PrimState m) a)
unsafeFromRows = mat a -> m (WrapImmutable mat (PrimState m) a)
forall (m :: * -> *) (mat :: * -> *) a.
(PrimMonad m, Matrix mat a) =>
mat a -> m (WrapImmutable mat (PrimState m) a)
wrapImmutable (mat a -> m (WrapImmutable mat (PrimState m) a))
-> ([Row mat a] -> mat a)
-> [Row mat a]
-> m (WrapImmutable mat (PrimState m) a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Row mat a] -> mat a
forall (mat :: * -> *) a. Matrix mat a => [Row mat a] -> mat a
unsafeFromRows
unsafeFromColumns :: [Column (WrapImmutable mat) a]
-> m (WrapImmutable mat (PrimState m) a)
unsafeFromColumns = mat a -> m (WrapImmutable mat (PrimState m) a)
forall (m :: * -> *) (mat :: * -> *) a.
(PrimMonad m, Matrix mat a) =>
mat a -> m (WrapImmutable mat (PrimState m) a)
wrapImmutable (mat a -> m (WrapImmutable mat (PrimState m) a))
-> ([Column mat a] -> mat a)
-> [Column mat a]
-> m (WrapImmutable mat (PrimState m) a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Column mat a] -> mat a
forall (mat :: * -> *) a. Matrix mat a => [Column mat a] -> mat a
unsafeFromColumns
unsafeCopy :: WrapImmutable mat (PrimState m) a
-> WrapImmutable mat (PrimState m) a -> m ()
unsafeCopy (WrapImmutable MutVar (PrimState m) (mat a)
dest Index
_ Index
_) (WrapImmutable MutVar (PrimState m) (mat a)
src Index
_ Index
_) =
MutVar (PrimState m) (mat a) -> mat a -> m ()
forall (m :: * -> *) a.
PrimMonad m =>
MutVar (PrimState m) a -> a -> m ()
writeMutVar MutVar (PrimState m) (mat a)
dest (mat a -> m ()) -> m (mat a) -> m ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< MutVar (PrimState m) (mat a) -> m (mat a)
forall (m :: * -> *) a.
PrimMonad m =>
MutVar (PrimState m) a -> m a
readMutVar MutVar (PrimState m) (mat a)
src
unsafeRead :: WrapImmutable mat (PrimState m) a -> Index -> Index -> m a
unsafeRead (WrapImmutable MutVar (PrimState m) (mat a)
m Index
_ Index
_) Index
i Index
j =
MutVar (PrimState m) (mat a) -> m (mat a)
forall (m :: * -> *) a.
PrimMonad m =>
MutVar (PrimState m) a -> m a
readMutVar MutVar (PrimState m) (mat a)
m m (mat a) -> (mat a -> m a) -> m a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \mat a
n -> mat a -> Index -> Index -> m a
forall (mat :: * -> *) a (m :: * -> *).
(Matrix mat a, Monad m) =>
mat a -> Index -> Index -> m a
basicUnsafeIndexM mat a
n Index
i Index
j
unsafeWrite :: WrapImmutable mat (PrimState m) a -> Index -> Index -> a -> m ()
unsafeWrite WrapImmutable mat (PrimState m) a
im Index
i Index
j a
x = WrapImmutable mat (PrimState m) a -> (mat a -> mat a) -> m ()
forall k (m :: * -> *) (mat :: k -> *) (a :: k).
PrimMonad m =>
WrapImmutable mat (PrimState m) a -> (mat a -> mat a) -> m ()
withImmutable WrapImmutable mat (PrimState m) a
im ((mat a -> mat a) -> m ()) -> (mat a -> mat a) -> m ()
forall a b. (a -> b) -> a -> b
$ \mat a
n -> mat a -> Index -> Index -> a -> mat a
forall (mat :: * -> *) a.
Matrix mat a =>
mat a -> Index -> Index -> a -> mat a
unsafeWrite mat a
n Index
i Index
j a
x
unsafeGenerate :: Index
-> Index
-> (Index -> Index -> a)
-> m (WrapImmutable mat (PrimState m) a)
unsafeGenerate Index
w Index
h Index -> Index -> a
f = do
MutVar (PrimState m) (mat a)
m <- mat a -> m (MutVar (PrimState m) (mat a))
forall (m :: * -> *) a.
PrimMonad m =>
a -> m (MutVar (PrimState m) a)
newMutVar (mat a -> m (MutVar (PrimState m) (mat a)))
-> mat a -> m (MutVar (PrimState m) (mat a))
forall a b. (a -> b) -> a -> b
$ Index -> Index -> (Index -> Index -> a) -> mat a
forall (mat :: * -> *) a.
Matrix mat a =>
Index -> Index -> (Index -> Index -> a) -> mat a
unsafeGenerate Index
w Index
h Index -> Index -> a
f
WrapImmutable mat (PrimState m) a
-> m (WrapImmutable mat (PrimState m) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (WrapImmutable mat (PrimState m) a
-> m (WrapImmutable mat (PrimState m) a))
-> WrapImmutable mat (PrimState m) a
-> m (WrapImmutable mat (PrimState m) a)
forall a b. (a -> b) -> a -> b
$ MutVar (PrimState m) (mat a)
-> Index -> Index -> WrapImmutable mat (PrimState m) a
forall k (mat :: k -> *) s (a :: k).
MutVar s (mat a) -> Index -> Index -> WrapImmutable mat s a
WrapImmutable MutVar (PrimState m) (mat a)
m Index
w Index
h
basicSet :: WrapImmutable mat (PrimState m) a -> a -> m ()
basicSet WrapImmutable mat (PrimState m) a
im a
x =
WrapImmutable mat (PrimState m) a -> (mat a -> mat a) -> m ()
forall k (m :: * -> *) (mat :: k -> *) (a :: k).
PrimMonad m =>
WrapImmutable mat (PrimState m) a -> (mat a -> mat a) -> m ()
withImmutable WrapImmutable mat (PrimState m) a
im ((mat a -> mat a) -> m ()) -> (mat a -> mat a) -> m ()
forall a b. (a -> b) -> a -> b
$ mat a -> mat a -> mat a
forall a b. a -> b -> a
const (mat a -> mat a -> mat a) -> mat a -> mat a -> mat a
forall a b. (a -> b) -> a -> b
$ Index -> Index -> (Index -> Index -> a) -> mat a
forall (mat :: * -> *) a.
Matrix mat a =>
Index -> Index -> (Index -> Index -> a) -> mat a
generate (WrapImmutable mat (PrimState m) a -> Index
forall k (mat :: k -> *) s (a :: k). WrapImmutable mat s a -> Index
_wiRowCount WrapImmutable mat (PrimState m) a
im) (WrapImmutable mat (PrimState m) a -> Index
forall k (mat :: k -> *) s (a :: k). WrapImmutable mat s a -> Index
_wiColCount WrapImmutable mat (PrimState m) a
im) ((Index -> Index -> a) -> mat a) -> (Index -> Index -> a) -> mat a
forall a b. (a -> b) -> a -> b
$ (Index -> a) -> Index -> Index -> a
forall a b. a -> b -> a
const ((Index -> a) -> Index -> Index -> a)
-> (Index -> a) -> Index -> Index -> a
forall a b. (a -> b) -> a -> b
$ a -> Index -> a
forall a b. a -> b -> a
const a
x
basicUnsafeIMapRowM :: WrapImmutable mat (PrimState m) a
-> Index -> (Index -> a -> m a) -> m ()
basicUnsafeIMapRowM (WrapImmutable MutVar (PrimState m) (mat a)
m Index
_ Index
c) Index
i Index -> a -> m a
f =
[Index] -> (Index -> m ()) -> m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Index
0..Index
cIndex -> Index -> Index
forall r. Group r => r -> r -> r
-Index
1] ((Index -> m ()) -> m ()) -> (Index -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Index
j -> do
mat a
n <- MutVar (PrimState m) (mat a) -> m (mat a)
forall (m :: * -> *) a.
PrimMonad m =>
MutVar (PrimState m) a -> m a
readMutVar MutVar (PrimState m) (mat a)
m
MutVar (PrimState m) (mat a) -> mat a -> m ()
forall (m :: * -> *) a.
PrimMonad m =>
MutVar (PrimState m) a -> a -> m ()
writeMutVar MutVar (PrimState m) (mat a)
m (mat a -> m ()) -> (a -> mat a) -> a -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. mat a -> Index -> Index -> a -> mat a
forall (mat :: * -> *) a.
Matrix mat a =>
mat a -> Index -> Index -> a -> mat a
unsafeWrite mat a
n Index
i Index
j (a -> m ()) -> m a -> m ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Index -> a -> m a
f Index
j (mat a
n mat a -> (Index, Index) -> a
forall (mat :: * -> *) a.
Matrix mat a =>
mat a -> (Index, Index) -> a
! (Index
i, Index
j))
basicUnsafeIMapRow :: WrapImmutable mat (PrimState m) a
-> Index -> (Index -> a -> a) -> m ()
basicUnsafeIMapRow WrapImmutable mat (PrimState m) a
im Index
i Index -> a -> a
f = WrapImmutable mat (PrimState m) a -> (mat a -> mat a) -> m ()
forall k (m :: * -> *) (mat :: k -> *) (a :: k).
PrimMonad m =>
WrapImmutable mat (PrimState m) a -> (mat a -> mat a) -> m ()
withImmutable WrapImmutable mat (PrimState m) a
im ((mat a -> mat a) -> m ()) -> (mat a -> mat a) -> m ()
forall a b. (a -> b) -> a -> b
$ \mat a
n -> mat a -> Index -> (Index -> a -> a) -> mat a
forall (mat :: * -> *) a.
Matrix mat a =>
mat a -> Index -> (Index -> a -> a) -> mat a
unsafeIMapRow mat a
n Index
i Index -> a -> a
f
basicUnsafeSwapRows :: WrapImmutable mat (PrimState m) a -> Index -> Index -> m ()
basicUnsafeSwapRows WrapImmutable mat (PrimState m) a
im Index
i Index
j = WrapImmutable mat (PrimState m) a -> (mat a -> mat a) -> m ()
forall k (m :: * -> *) (mat :: k -> *) (a :: k).
PrimMonad m =>
WrapImmutable mat (PrimState m) a -> (mat a -> mat a) -> m ()
withImmutable WrapImmutable mat (PrimState m) a
im ((mat a -> mat a) -> m ()) -> (mat a -> mat a) -> m ()
forall a b. (a -> b) -> a -> b
$ \mat a
n -> mat a -> Index -> Index -> mat a
forall (mat :: * -> *) a.
Matrix mat a =>
mat a -> Index -> Index -> mat a
swapRows mat a
n Index
i Index
j
unsafeScaleRow :: WrapImmutable mat (PrimState m) a -> Index -> a -> m ()
unsafeScaleRow WrapImmutable mat (PrimState m) a
im Index
i a
c = WrapImmutable mat (PrimState m) a -> (mat a -> mat a) -> m ()
forall k (m :: * -> *) (mat :: k -> *) (a :: k).
PrimMonad m =>
WrapImmutable mat (PrimState m) a -> (mat a -> mat a) -> m ()
withImmutable WrapImmutable mat (PrimState m) a
im ((mat a -> mat a) -> m ()) -> (mat a -> mat a) -> m ()
forall a b. (a -> b) -> a -> b
$ \mat a
n -> mat a -> Index -> a -> mat a
forall (mat :: * -> *) a.
(Matrix mat a, Commutative a) =>
mat a -> Index -> a -> mat a
scaleRow mat a
n Index
i a
c
combineRows :: Index -> a -> Index -> WrapImmutable mat (PrimState m) a -> m ()
combineRows Index
i a
c Index
j WrapImmutable mat (PrimState m) a
m = WrapImmutable mat (PrimState m) a -> (mat a -> mat a) -> m ()
forall k (m :: * -> *) (mat :: k -> *) (a :: k).
PrimMonad m =>
WrapImmutable mat (PrimState m) a -> (mat a -> mat a) -> m ()
withImmutable WrapImmutable mat (PrimState m) a
m ((mat a -> mat a) -> m ()) -> (mat a -> mat a) -> m ()
forall a b. (a -> b) -> a -> b
$ \mat a
n -> Index -> a -> Index -> mat a -> mat a
forall (mat :: * -> *) a.
(Matrix mat a, Commutative a, Semiring a) =>
Index -> a -> Index -> mat a -> mat a
combineRows Index
i a
c Index
j mat a
n
withImmutable :: PrimMonad m => WrapImmutable mat (PrimState m) a -> (mat a -> mat a) -> m ()
withImmutable :: WrapImmutable mat (PrimState m) a -> (mat a -> mat a) -> m ()
withImmutable (WrapImmutable MutVar (PrimState m) (mat a)
m Index
_ Index
_) mat a -> mat a
mut =
MutVar (PrimState m) (mat a) -> m (mat a)
forall (m :: * -> *) a.
PrimMonad m =>
MutVar (PrimState m) a -> m a
readMutVar MutVar (PrimState m) (mat a)
m m (mat a) -> (mat a -> m ()) -> m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= MutVar (PrimState m) (mat a) -> mat a -> m ()
forall (m :: * -> *) a.
PrimMonad m =>
MutVar (PrimState m) a -> a -> m ()
writeMutVar MutVar (PrimState m) (mat a)
m (mat a -> m ()) -> (mat a -> mat a) -> mat a -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. mat a -> mat a
mut
withMutable :: Matrix mat a => mat a -> (forall s. Mutable mat s a -> ST s ()) -> mat a
withMutable :: mat a -> (forall s. Mutable mat s a -> ST s ()) -> mat a
withMutable mat a
m forall s. Mutable mat s a -> ST s ()
act = (forall s. ST s (mat a)) -> mat a
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (mat a)) -> mat a)
-> (forall s. ST s (mat a)) -> mat a
forall a b. (a -> b) -> a -> b
$ do
Mutable mat s a
mat' <- mat a -> ST s (Mutable mat (PrimState (ST s)) a)
forall (mat :: * -> *) a (m :: * -> *).
(Matrix mat a, PrimMonad m) =>
mat a -> m (Mutable mat (PrimState m) a)
thaw mat a
m
Mutable mat s a -> ST s ()
forall s. Mutable mat s a -> ST s ()
act Mutable mat s a
mat'
Mutable mat (PrimState (ST s)) a -> ST s (mat a)
forall (mat :: * -> *) a (m :: * -> *).
(Matrix mat a, PrimMonad m) =>
Mutable mat (PrimState m) a -> m (mat a)
unsafeFreeze Mutable mat s a
Mutable mat (PrimState (ST s)) a
mat'
{-# INLINE withMutable #-}
rowCount :: Matrix mat a => mat a -> Size
rowCount :: mat a -> Index
rowCount = mat a -> Index
forall (mat :: * -> *) a. Matrix mat a => mat a -> Index
basicRowCount
columnCount :: Matrix mat a => mat a -> Size
columnCount :: mat a -> Index
columnCount = mat a -> Index
forall (mat :: * -> *) a. Matrix mat a => mat a -> Index
basicColumnCount
generate :: Matrix mat a => Size -> Size -> (Index -> Index -> a) -> mat a
generate :: Index -> Index -> (Index -> Index -> a) -> mat a
generate Index
w Index
h Index -> Index -> a
f = (forall s. ST s (mat a)) -> mat a
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (mat a)) -> mat a)
-> (forall s. ST s (mat a)) -> mat a
forall a b. (a -> b) -> a -> b
$ Mutable mat s a -> ST s (mat a)
forall (mat :: * -> *) a (m :: * -> *).
(Matrix mat a, PrimMonad m) =>
Mutable mat (PrimState m) a -> m (mat a)
unsafeFreeze (Mutable mat s a -> ST s (mat a))
-> ST s (Mutable mat s a) -> ST s (mat a)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Index
-> Index
-> (Index -> Index -> a)
-> ST s (Mutable mat (PrimState (ST s)) a)
forall (m :: * -> *) (mat :: * -> * -> *) a.
(PrimMonad m, MMatrix mat a) =>
Index -> Index -> (Index -> Index -> a) -> m (mat (PrimState m) a)
GM.generate Index
w Index
h Index -> Index -> a
f
freeze :: (Matrix mat a, PrimMonad m) => Mutable mat (PrimState m) a -> m (mat a)
freeze :: Mutable mat (PrimState m) a -> m (mat a)
freeze = Mutable mat (PrimState m) a -> m (mat a)
forall (mat :: * -> *) a (m :: * -> *).
(Matrix mat a, PrimMonad m) =>
Mutable mat (PrimState m) a -> m (mat a)
unsafeFreeze (Mutable mat (PrimState m) a -> m (mat a))
-> (Mutable mat (PrimState m) a -> m (Mutable mat (PrimState m) a))
-> Mutable mat (PrimState m) a
-> m (mat a)
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< Mutable mat (PrimState m) a -> m (Mutable mat (PrimState m) a)
forall (m :: * -> *) (mat :: * -> * -> *) a.
(PrimMonad m, MMatrix mat a) =>
mat (PrimState m) a -> m (mat (PrimState m) a)
GM.clone
thaw :: (Matrix mat a, PrimMonad m) => mat a -> m (Mutable mat (PrimState m) a)
thaw :: mat a -> m (Mutable mat (PrimState m) a)
thaw = Mutable mat (PrimState m) a -> m (Mutable mat (PrimState m) a)
forall (m :: * -> *) (mat :: * -> * -> *) a.
(PrimMonad m, MMatrix mat a) =>
mat (PrimState m) a -> m (mat (PrimState m) a)
GM.clone (Mutable mat (PrimState m) a -> m (Mutable mat (PrimState m) a))
-> (mat a -> m (Mutable mat (PrimState m) a))
-> mat a
-> m (Mutable mat (PrimState m) a)
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< mat a -> m (Mutable mat (PrimState m) a)
forall (mat :: * -> *) a (m :: * -> *).
(Matrix mat a, PrimMonad m) =>
mat a -> m (Mutable mat (PrimState m) a)
unsafeThaw
fromRows :: Matrix mat a => [Row mat a] -> mat a
fromRows :: [Row mat a] -> mat a
fromRows [Row mat a]
rs
| let lens :: [Index]
lens = (Row (Mutable mat) a -> Index) -> [Row (Mutable mat) a] -> [Index]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
map Row (Mutable mat) a -> Index
forall (v :: * -> *) a. Vector v a => v a -> Index
GV.length [Row mat a]
[Row (Mutable mat) a]
rs
, [Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
and ([Bool] -> Bool) -> [Bool] -> Bool
forall a b. (a -> b) -> a -> b
$ (Index -> Index -> Bool) -> [Index] -> [Index] -> [Bool]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith Index -> Index -> Bool
forall a. Eq a => a -> a -> Bool
(==) [Index]
lens ([Index] -> [Index]
forall a. [a] -> [a]
tail [Index]
lens) = [Row mat a] -> mat a
forall (mat :: * -> *) a. Matrix mat a => [Row mat a] -> mat a
unsafeFromRows [Row mat a]
rs
| Bool
otherwise = [Char] -> mat a
forall a. HasCallStack => [Char] -> a
error [Char]
"fromRows: rows should be of the same length"
fromColumns :: Matrix mat a => [Column mat a] -> mat a
fromColumns :: [Column mat a] -> mat a
fromColumns [Column mat a]
cs
| let lens :: [Index]
lens = (Column (Mutable mat) a -> Index)
-> [Column (Mutable mat) a] -> [Index]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
map Column (Mutable mat) a -> Index
forall (v :: * -> *) a. Vector v a => v a -> Index
GV.length [Column mat a]
[Column (Mutable mat) a]
cs
, [Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
and ([Bool] -> Bool) -> [Bool] -> Bool
forall a b. (a -> b) -> a -> b
$ (Index -> Index -> Bool) -> [Index] -> [Index] -> [Bool]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith Index -> Index -> Bool
forall a. Eq a => a -> a -> Bool
(==) [Index]
lens ([Index] -> [Index]
forall a. [a] -> [a]
tail [Index]
lens) = [Column mat a] -> mat a
forall (mat :: * -> *) a. Matrix mat a => [Column mat a] -> mat a
unsafeFromColumns [Column mat a]
cs
| Bool
otherwise = [Char] -> mat a
forall a. HasCallStack => [Char] -> a
error [Char]
"fromColumns: columns should be of the same length"
create :: Matrix mat a => (forall s. ST s (Mutable mat s a)) -> mat a
create :: (forall s. ST s (Mutable mat s a)) -> mat a
create forall s. ST s (Mutable mat s a)
act = (forall s. ST s (mat a)) -> mat a
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (mat a)) -> mat a)
-> (forall s. ST s (mat a)) -> mat a
forall a b. (a -> b) -> a -> b
$ Mutable mat s a -> ST s (mat a)
forall (mat :: * -> *) a (m :: * -> *).
(Matrix mat a, PrimMonad m) =>
Mutable mat (PrimState m) a -> m (mat a)
unsafeFreeze (Mutable mat s a -> ST s (mat a))
-> ST s (Mutable mat s a) -> ST s (mat a)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< ST s (Mutable mat s a)
forall s. ST s (Mutable mat s a)
act
(!) :: Matrix mat a => mat a -> (Index, Index) -> a
(!) mat a
mat (Index
i, Index
j) = Identity a -> a
forall a. Identity a -> a
runIdentity (Identity a -> a) -> Identity a -> a
forall a b. (a -> b) -> a -> b
$ mat a -> Index -> Index -> Identity a
forall (mat :: * -> *) a (m :: * -> *).
(Matrix mat a, Monad m) =>
mat a -> Index -> Index -> m a
basicUnsafeIndexM mat a
mat Index
i Index
j