forked from arrayfire-community/arrayfire_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom.py
More file actions
49 lines (40 loc) · 1.27 KB
/
random.py
File metadata and controls
49 lines (40 loc) · 1.27 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
from . import array
from .array import *
from . import helper
from .helper import *
def rand(d0,d1=None,d2=None,d3=None,dtype='float32'):
dims = getdims((d0,d1,d2,d3))
ty = getty(dtype)
if (dims == 1):
res = ndarray((d0,))
res.ptr = libaf.randu(d0, 1, 1, 1, ty)
elif (dims == 2):
res = ndarray((d0, d1))
res.ptr = libaf.randu(d1, d0, 1, 1, ty)
elif (dims == 3):
res = ndarray((d0, d1, d2))
res.ptr = libaf.randu(d2, d1, d0, 1, ty)
else:
res = ndarray((d0, d2, d2, d3))
res.ptr = libaf.randu(d3, d2, d1, d0, ty)
if (res.ptr == 0):
raise Exception("Failure in rand")
return res
def randn(d0,d1=None,d2=None,d3=None,dtype='float32'):
dims = getdims((d0,d1,d2,d3))
ty = getty(dtype)
if (dims == 1):
res = ndarray((d0,))
res.ptr = libaf.randn(d0, 1, 1, 1, ty)
elif (dims == 2):
res = ndarray((d0, d1))
res.ptr = libaf.randn(d1, d0, 1, 1, ty)
elif (dims == 3):
res = ndarray((d0, d1, d2))
res.ptr = libaf.randn(d2, d1, d0, 1, ty)
else:
res = ndarray((d0, d2, d2, d3))
res.ptr = libaf.randn(d3, d2, d1, d0, ty)
if (res.ptr == 0):
raise Exception("Failure in randn")
return res