/
Matrix.purs
72 lines (53 loc) · 2.54 KB
/
Matrix.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
-----------------------------------------------------------------------------
--
-- Module : ST.Matrix
-- Copyright : Michael Karg
-- License : Apache-2.0
--
-- Maintainer : jnf@arcor.de
-- Stability :
-- Portability :
--
-- | Binding to mjs library
--
-----------------------------------------------------------------------------
module Data.ST.Matrix where
import Prelude
import Control.Monad.Eff (Eff)
import Control.Monad.ST (ST())
import Data.TypeNat (class Sized)
import Data.Array.ST (STArray)
import Data.Matrix as M
newtype STMat s h a = STMat (STArray h a)
-- try array cloning with .slice() instead of the for-loop
-- implementation in Data.Array.ST. Needs benchmarking.
foreign import copyImpl :: forall a b h r. a -> Eff (st :: ST h | r) b
-- | Create an immutable copy of a mutable array.
freeze :: forall a h r. STArray h a -> Eff (st :: ST h | r) (Array a)
freeze = copyImpl
-- | Create a mutable copy of an immutable array.
thaw :: forall a h r. Array a -> Eff (st :: ST h | r) (STArray h a)
thaw = copyImpl
-- | Freeze an ST array. Do not mutate the STArray afterwards!
foreign import unsafeFreeze :: forall a h. STArray h a -> Array a
foreign import unsafeThaw :: forall a h. Array a -> STArray h a
cloneSTMat :: forall s h a r. (STMat s h a) -> Eff (st :: ST h | r) (STMat s h a)
cloneSTMat (STMat arr) = STMat <<< unsafeThaw <$> freeze arr
fromSTMat :: forall s h a r. (Sized s) => (STMat s h a) -> Eff (st :: ST h | r) (M.Mat s a)
fromSTMat (STMat arr) = do
x <- freeze arr
pure (M.fromArray x)
toSTMat :: forall s h a r. (M.Mat s a) -> Eff (st :: ST h | r) (STMat s h a)
toSTMat m = STMat <$> thaw (M.toArray m)
-- copyToSTMat :: forall s h a r. (M.Matrix (M.Mat s) a) => (M.Mat s a) -> (STMat s h a) -> Eff (st :: ST h | r) Unit
foreign import copyToSTMat :: forall s h a r. (M.Mat s a) -> (STMat s h a) -> Eff (st :: ST h | r) Unit
identityST' :: forall s h r. (Sized s) => Eff (st :: ST h | r) (STMat s h Number)
identityST' =
let m = M.identity' :: M.Mat s Number
in STMat <$> thaw (M.toArray m)
foreign import scaleSTMatrixInt :: forall a h r. (EuclideanRing a) => a -> STArray h a -> Eff (st :: ST h | r) Unit
scaleSTMatrix :: forall s a h r. (EuclideanRing a) => a -> (STMat s h a) -> Eff (st :: ST h | r) (STMat s h a)
scaleSTMatrix x v@(STMat arr) = scaleSTMatrixInt x arr *> pure v
fromMatrix :: forall s h r a. M.Mat s a -> Eff (st :: ST h | r) (STMat s h a)
fromMatrix (M.Mat m) = STMat <$> thaw m
foreign import runSTMatrix :: forall s a r. (forall h. Eff (st :: ST h | r) (STMat s h a)) -> Eff r (M.Mat s a)