Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add safe freeze and thaw for matrices

  • Loading branch information...
commit 4e7f4085d98fa8651a4737fc8c5979e10812ed0d 1 parent 0becc85
@Shimuuar authored
Showing with 17 additions and 1 deletion.
  1. +17 −1 Data/Matrix/Generic.hs
View
18 Data/Matrix/Generic.hs
@@ -21,6 +21,8 @@ module Data.Matrix.Generic (
, (@!)
, unsafeIndex
-- * Converions to/from mutable
+ , freeze
+ , thaw
, unsafeFreeze
, unsafeThaw
-- * Conversion to string
@@ -79,7 +81,7 @@ cols = basicCols
-- | Indexing operator without range checking.
-unsafeIndex :: IsMatrix mat a
+unsafeIndex :: IsMatrix mat a
=> mat a -- ^ Matrix
-> (Int,Int) -- ^ (row,column)
-> a
@@ -99,6 +101,20 @@ m @! a@(i,j)
| otherwise = unsafeIndex m a
+-- | Convert mutable matrix to immutable.
+freeze :: (PrimMonad m, IsMatrix mat a) => Mutable mat (PrimState m) a -> m (mat a)
+{-# INLINE freeze #-}
+freeze m = do
+ unsafeFreeze =<< M.clone m
+
+
+-- | Convert immutable matrix to mutable.
+thaw :: (PrimMonad m, IsMatrix mat a) => mat a -> m (Mutable mat (PrimState m) a)
+{-# INLINE thaw #-}
+thaw m = do
+ M.clone =<< unsafeThaw m
+
+
-- | Convert mutable matrix to immutable. Mutable matrix may not be
-- modified after operation.
unsafeFreeze :: (PrimMonad m, IsMatrix mat a) => Mutable mat (PrimState m) a -> m (mat a)
Please sign in to comment.
Something went wrong with that request. Please try again.