forked from arrayfire/arrayfire-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharith.py
More file actions
194 lines (137 loc) · 4.51 KB
/
Copy patharith.py
File metadata and controls
194 lines (137 loc) · 4.51 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#######################################################
# Copyright (c) 2015, ArrayFire
# All rights reserved.
#
# This file is distributed under 3-clause BSD license.
# The complete license agreement can be obtained at:
# http://arrayfire.com/licenses/BSD-3-Clause
########################################################
from .library import *
from .array import *
from .broadcast import *
def arith_binary_func(lhs, rhs, c_func):
out = array()
is_left_array = isinstance(lhs, array)
is_right_array = isinstance(rhs, array)
if not (is_left_array or is_right_array):
TypeError("Atleast one input needs to be of type arrayfire.array")
elif (is_left_array and is_right_array):
safe_call(c_func(ct.pointer(out.arr), lhs.arr, rhs.arr, bcast.get()))
elif (is_number(rhs)):
ldims = dim4_tuple(lhs.dims())
lty = lhs.type()
other = array()
other.arr = constant_array(rhs, ldims[0], ldims[1], ldims[2], ldims[3], lty)
safe_call(c_func(ct.pointer(out.arr), lhs.arr, other.arr, bcast.get()))
else:
rdims = dim4_tuple(rhs.dims())
rty = rhs.type()
other = array()
other.arr = constant_array(lhs, rdims[0], rdims[1], rdims[2], rdims[3], rty)
safe_call(c_func(ct.pointer(out.arr), lhs.arr, other.arr, bcast.get()))
return out
def arith_unary_func(a, c_func):
out = array()
safe_call(c_func(ct.pointer(out.arr), a.arr))
return out
def cast(a, dtype=f32):
out=array()
safe_call(clib.af_cast(ct.pointer(out.arr), a.arr, dtype))
return out
def minof(lhs, rhs):
return arith_binary_func(lhs, rhs, clib.af_minof)
def maxof(lhs, rhs):
return arith_binary_func(lhs, rhs, clib.af_maxof)
def rem(lhs, rhs):
return arith_binary_func(lhs, rhs, clib.af_rem)
def abs(a):
return arith_unary_func(a, clib.af_abs)
def arg(a):
return arith_unary_func(a, clib.af_arg)
def sign(a):
return arith_unary_func(a, clib.af_sign)
def round(a):
return arith_unary_func(a, clib.af_round)
def trunc(a):
return arith_unary_func(a, clib.af_trunc)
def floor(a):
return arith_unary_func(a, clib.af_floor)
def ceil(a):
return arith_unary_func(a, clib.af_ceil)
def hypot(lhs, rhs):
return arith_binary_func(lhs, rhs, clib.af_hypot)
def sin(a):
return arith_unary_func(a, clib.af_sin)
def cos(a):
return arith_unary_func(a, clib.af_cos)
def tan(a):
return arith_unary_func(a, clib.af_tan)
def asin(a):
return arith_unary_func(a, clib.af_asin)
def acos(a):
return arith_unary_func(a, clib.af_acos)
def atan(a):
return arith_unary_func(a, clib.af_atan)
def atan2(lhs, rhs):
return arith_binary_func(lhs, rhs, clib.af_atan2)
def cplx(lhs, rhs=None):
if rhs is None:
return arith_unary_func(lhs, clib.af_cplx)
else:
return arith_binary_func(lhs, rhs, clib.af_cplx2)
def real(lhs):
return arith_unary_func(lhs, clib.af_real)
def imag(lhs):
return arith_unary_func(lhs, clib.af_imag)
def conjg(lhs):
return arith_unary_func(lhs, clib.af_conjg)
def sinh(a):
return arith_unary_func(a, clib.af_sinh)
def cosh(a):
return arith_unary_func(a, clib.af_cosh)
def tanh(a):
return arith_unary_func(a, clib.af_tanh)
def asinh(a):
return arith_unary_func(a, clib.af_asinh)
def acosh(a):
return arith_unary_func(a, clib.af_acosh)
def atanh(a):
return arith_unary_func(a, clib.af_atanh)
def root(lhs, rhs):
return arith_binary_func(lhs, rhs, clib.af_root)
def pow(lhs, rhs):
return arith_binary_func(lhs, rhs, clib.af_pow)
def pow2(a):
return arith_unary_func(a, clib.af_pow2)
def exp(a):
return arith_unary_func(a, clib.af_exp)
def expm1(a):
return arith_unary_func(a, clib.af_expm1)
def erf(a):
return arith_unary_func(a, clib.af_erf)
def erfc(a):
return arith_unary_func(a, clib.af_erfc)
def log(a):
return arith_unary_func(a, clib.af_log)
def log1p(a):
return arith_unary_func(a, clib.af_log1p)
def log10(a):
return arith_unary_func(a, clib.af_log10)
def log2(a):
return arith_unary_func(a, clib.af_log2)
def sqrt(a):
return arith_unary_func(a, clib.af_sqrt)
def cbrt(a):
return arith_unary_func(a, clib.af_cbrt)
def factorial(a):
return arith_unary_func(a, clib.af_factorial)
def tgamma(a):
return arith_unary_func(a, clib.af_tgamma)
def lgamma(a):
return arith_unary_func(a, clib.af_lgamma)
def iszero(a):
return arith_unary_func(a, clib.af_iszero)
def isinf(a):
return arith_unary_func(a, clib.af_isinf)
def isnan(a):
return arith_unary_func(a, clib.af_isnan)