Skip to content

Compatibility with random 1.3 #149

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions massiv/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# 1.0.5

* Adjust `uniformArray` and `uniformRangeArray` to use `SplitGen` instead of deprecated `split` whenever newer version of `random >= 1.3` is in use.
* Add `Functor` instance for `Border`
* Improve performance and reduce allocations during computation of higher dimension `DW` arrays [#142](https://github.com/lehins/massiv/issues/142)

Expand Down
1 change: 1 addition & 0 deletions massiv/src/Data/Massiv/Array/Manifest/Primitive.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UnboxedTuples #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -Wno-deprecations #-}

-- |
-- Module : Data.Massiv.Array.Manifest.Primitive
Expand Down
37 changes: 34 additions & 3 deletions massiv/src/Data/Massiv/Array/Ops/Construct.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MultiParamTypeClasses #-}
Expand Down Expand Up @@ -293,7 +294,7 @@ iunfoldlS_ sz f acc0 = DLArray{dlComp = Seq, dlSize = sz, dlLoad = load}
-- >>> import Data.Massiv.Array
-- >>> import System.Random as Random
-- >>> gen = Random.mkStdGen 217
-- >>> randomArray gen Random.split Random.random (ParN 2) (Sz2 2 3) :: Array DL Ix2 Double
-- >>> randomArray gen Random.splitGen Random.random (ParN 2) (Sz2 2 3) :: Array DL Ix2 Double
-- Array DL (ParN 2) (Sz (2 :. 3))
-- [ [ 0.2616843941380331, 0.600959468331641, 0.4382415961606372 ]
-- , [ 0.27812817813217605, 0.2993277194932741, 0.2774105268603957 ]
Expand Down Expand Up @@ -385,6 +386,19 @@ makeSplitSeedArray it seed splitSeed comp sz genFunc =
-- | Generate a random array where all elements are sampled from a uniform distribution.
--
-- @since 1.0.0
#if MIN_VERSION_random(1,3,0)
uniformArray
:: forall ix e g
. (Index ix, SplitGen g, Uniform e)
=> g
-- ^ Initial random value generator.
-> Comp
-- ^ Computation strategy.
-> Sz ix
-- ^ Resulting size of the array.
-> Array DL ix e
uniformArray gen = randomArray gen splitGen uniform
#else
uniformArray
:: forall ix e g
. (Index ix, RandomGen g, Uniform e)
Expand All @@ -396,11 +410,27 @@ uniformArray
-- ^ Resulting size of the array.
-> Array DL ix e
uniformArray gen = randomArray gen split uniform
#endif
{-# INLINE uniformArray #-}

-- | Same as `uniformArray`, but will generate values in a supplied range.
--
-- @since 1.0.0
#if MIN_VERSION_random(1,3,0)
uniformRangeArray
:: forall ix e g
. (Index ix, SplitGen g, UniformRange e)
=> g
-- ^ Initial random value generator.
-> (e, e)
-- ^ Inclusive range in which values will be generated in.
-> Comp
-- ^ Computation strategy.
-> Sz ix
-- ^ Resulting size of the array.
-> Array DL ix e
uniformRangeArray gen r = randomArray gen splitGen (uniformR r)
#else
uniformRangeArray
:: forall ix e g
. (Index ix, RandomGen g, UniformRange e)
Expand All @@ -414,6 +444,7 @@ uniformRangeArray
-- ^ Resulting size of the array.
-> Array DL ix e
uniformRangeArray gen r = randomArray gen split (uniformR r)
#endif
{-# INLINE uniformRangeArray #-}

-- | Similar to `randomArray` but performs generation sequentially, which means it doesn't
Expand Down Expand Up @@ -489,8 +520,8 @@ randomArrayS gen sz nextRandom =
-- >>> gens <- initWorkerStates Par (MWC.initialize . A.toPrimitiveVector . A.singleton @P @Ix1 . fromIntegral . getWorkerId)
-- >>> randomArrayWS gens (Sz2 2 3) (uniformRM (0, 9)) :: IO (Matrix P Double)
-- Array P Par (Sz (2 :. 3))
-- [ [ 8.999240522095299, 6.832223390653755, 3.065728078741671 ]
-- , [ 7.242581103346686, 2.4565807301968623, 0.4514262066689775 ]
-- [ [ 8.999240522095299, 6.832223390653754, 1.434271921258329 ]
-- , [ 7.242581103346687, 2.0434192698031377, 4.048573793331022 ]
-- ]
-- >>> randomArrayWS gens (Sz1 6) (uniformRM (0, 9)) :: IO (Vector P Int)
-- Array P Par (Sz1 6)
Expand Down
Loading