-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathRandom.hs
More file actions
61 lines (53 loc) · 1.41 KB
/
Random.hs
File metadata and controls
61 lines (53 loc) · 1.41 KB
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
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE KindSignatures #-}
module ArrayFire.Random where
import Control.Exception
import Control.Monad
import Foreign.C.String
import Foreign.C.Types
import Foreign.Marshal hiding (void)
import Foreign.Marshal.Array
import Foreign.ForeignPtr
import Foreign.Ptr
import Foreign.Storable
import Data.Proxy
import ArrayFire.Internal.Array
import ArrayFire.Exception
import ArrayFire.Types
import ArrayFire.Internal.Defines
import ArrayFire.Internal.Random
rand
:: forall dims a
. (Dims dims, AFType a)
=> (Ptr AFArray -> CUInt -> Ptr DimT -> AFDtype -> IO AFErr)
-> IO (Array a)
rand f = do
ptr <- alloca $ \ptrPtr -> mask_ $ do
dimArray <- newArray dimt
throwAFError =<< f ptrPtr n dimArray typ
peek ptrPtr
Array <$>
newForeignPtr
af_release_array_finalizer
ptr
where
n = fromIntegral (length dimt)
dimt :: [DimT] = toDims (Proxy @ dims)
typ = afType (Proxy @ a)
randn
:: forall dims a
. (Dims dims, AFType a)
=> IO (Array a)
randn = rand @dims @a af_randn
randu
:: forall dims a
. (Dims dims, AFType a)
=> IO (Array a)
randu = rand @dims @a af_randu