<commit_before>{-|
Module      : Numeric.LinearAlgebra.Easy.ST
Description : Easy linear algebra: build vectors and matrices in ST monad
Copyright   : (C) <NAME>, 2016
Licence     : MIT
Maintainer  : <EMAIL>
Stability   : experimental
Portability : portable

Typical usage is to import the top-level module unqualified using @import Numeric.LinearAlgebra.Easy@
-}

{-# LANGUAGE Rank2Types #-}

module Numeric.LinearAlgebra.Easy.ST
    ( STMatrix
    , modifyMatrix
    , newMatrix
    , readMatrix
    , runSTMatrix
    , withSTMatrix
    , writeMatrix
    ) where

import           Control.Monad.ST
import qualified Numeric.LinearAlgebra.Devel as LAD
import           Numeric.LinearAlgebra.Easy.Types

type STMatrix s = LAD.STMatrix s R

-- |Creates a new mutable matrix of doubles
newMatrix :: R -> Int -> Int -> ST s (STMatrix s)
newMatrix = LAD.newMatrix

-- |Safely freezes converts a mutable matrix of doubles
runSTMatrix :: (forall s . ST s (STMatrix s)) -> Matrix
runSTMatrix = LAD.runSTMatrix

-- |Wraps operations on a mutable matrix of doubles
--
-- >>> :{
-- withSTMatrix 9 3 3 $ \m' -> do
--     writeMatrix m' 0 0 10.0
--     writeMatrix m' 1 1 20.0
--     writeMatrix m' 2 2 30.0
-- :}
-- (3><3)
--  [ 10.0,  9.0,  9.0
--  ,  9.0, 20.0,  9.0
--  ,  9.0,  9.0, 30.0 ]
--
withSTMatrix :: R -> Int -> Int -> (forall s . STMatrix s -> ST s ()) -> Matrix
withSTMatrix value m n f = runSTMatrix $ do
    x <- newMatrix value m n
    f x
    return x

-- |Applies a function to a value in a mutable matrix of doubles
modifyMatrix :: STMatrix s -> Int -> Int -> (R -> R) -> ST s ()
modifyMatrix = LAD.modifyMatrix

-- |Reads a value from a mutable matrix of doubles
readMatrix :: STMatrix s -> Int -> Int -> ST s R
readMatrix = LAD.readMatrix

-- |Writes a value to a mutable matrix of doubles
writeMatrix :: STMatrix s -> Int -> Int -> R -> ST s ()
writeMatrix = LAD.writeMatrix<commit_msg>Finish basic documentation of ST module<commit_after>{-|
Module      : Numeric.LinearAlgebra.Easy.ST
Description : Easy linear algebra: build vectors and matrices in ST monad
Copyright   : (C) <NAME>, 2016
Licence     : MIT
Maintainer  : <EMAIL>
Stability   : experimental
Portability : portable

Mutable matrices of @Double@ in the @ST@ monads

Typical usage is to import the top-level module unqualified using @import Numeric.LinearAlgebra.Easy@
-}

{-# LANGUAGE Rank2Types #-}

module Numeric.LinearAlgebra.Easy.ST
    ( STMatrix
    , modifyMatrix
    , newMatrix
    , readMatrix
    , runSTMatrix
    , withSTMatrix
    , writeMatrix
    ) where

import           Control.Monad.ST
import qualified Numeric.LinearAlgebra.Devel as LAD
import           Numeric.LinearAlgebra.Easy.Types

-- |A mutable matrix of @Double@ values
type STMatrix s = LAD.STMatrix s R

-- |Creates a new mutable matrix of @Double@ values
newMatrix :: R -> Int -> Int -> ST s (STMatrix s)
newMatrix = LAD.newMatrix

-- |Safely freezes and converts a mutable matrix of @Double@ values
runSTMatrix :: (forall s . ST s (STMatrix s)) -> Matrix
runSTMatrix = LAD.runSTMatrix

-- |Wraps operations on a mutable matrix of @Double@ values
--
-- >>> :{
-- withSTMatrix 9 3 3 $ \m' -> do
--     writeMatrix m' 0 0 10.0
--     writeMatrix m' 1 1 20.0
--     writeMatrix m' 2 2 30.0
-- :}
-- (3><3)
--  [ 10.0,  9.0,  9.0
--  ,  9.0, 20.0,  9.0
--  ,  9.0,  9.0, 30.0 ]
--
withSTMatrix :: R -> Int -> Int -> (forall s . STMatrix s -> ST s ()) -> Matrix
withSTMatrix value m n f = runSTMatrix $ do
    x <- newMatrix value m n
    f x
    return x

-- |Applies a function to a value in a mutable matrix of @Double@ values
modifyMatrix :: STMatrix s -> Int -> Int -> (R -> R) -> ST s ()
modifyMatrix = LAD.modifyMatrix

-- |Reads a value from a mutable matrix of @Double@ values
readMatrix :: STMatrix s -> Int -> Int -> ST s R
readMatrix = LAD.readMatrix

-- |Writes a value to a mutable matrix of @Double@ values
writeMatrix :: STMatrix s -> Int -> Int -> R -> ST s ()
writeMatrix = LAD.writeMatrix
{'fraction_non_alphanumeric': 0.11935402472874085, 'fraction_numerical': 0.0232147363108756, 'mean_word_length': 2.6874418604651162, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7476406', 'n_tokens_mistral': 1394, 'n_tokens_neox': 1321, 'n_words': 479}