|
| 1 | +from .library import * |
| 2 | +from .array import * |
| 3 | + |
| 4 | +def arith_binary_func(lhs, rhs, c_func): |
| 5 | + out = array() |
| 6 | + |
| 7 | + is_left_array = isinstance(lhs, array) |
| 8 | + is_right_array = isinstance(rhs, array) |
| 9 | + |
| 10 | + if not (is_left_array or is_right_array): |
| 11 | + TypeError("Atleast one input needs to be of type arrayfire.array") |
| 12 | + |
| 13 | + elif (is_left_array and is_right_array): |
| 14 | + safe_call(c_func(pointer(out.arr), lhs.arr, rhs.arr, False)) |
| 15 | + |
| 16 | + elif (is_valid_scalar(rhs)): |
| 17 | + ldims = dim4_tuple(lhs.dims()) |
| 18 | + lty = lhs.type() |
| 19 | + other = array() |
| 20 | + other.arr = constant_array(rhs, ldims[0], ldims[1], ldims[2], ldims[3], lty) |
| 21 | + safe_call(c_func(pointer(out.arr), lhs.arr, other.arr, False)) |
| 22 | + |
| 23 | + else: |
| 24 | + rdims = dim4_tuple(rhs.dims()) |
| 25 | + rty = rhs.type() |
| 26 | + other = array() |
| 27 | + other.arr = constant_array(lhs, rdims[0], rdims[1], rdims[2], rdims[3], rty) |
| 28 | + safe_call(c_func(pointer(out.arr), lhs.arr, other.arr, False)) |
| 29 | + |
| 30 | + return out |
| 31 | + |
| 32 | +def arith_unary_func(a, c_func): |
| 33 | + out = array() |
| 34 | + safe_call(c_func(pointer(out.arr), a.arr)) |
| 35 | + return out |
| 36 | + |
| 37 | +def cast(a, dtype=f32): |
| 38 | + out=array() |
| 39 | + safe_call(clib.af_cast(pointer(out.arr), a.arr, dtype)) |
| 40 | + return out |
| 41 | + |
| 42 | +def minof(lhs, rhs): |
| 43 | + return arith_binary_func(lhs, rhs, clib.af_minof) |
| 44 | + |
| 45 | +def maxof(lhs, rhs): |
| 46 | + return arith_binary_func(lhs, rhs, clib.af_maxof) |
| 47 | + |
| 48 | +def rem(lhs, rhs): |
| 49 | + return arith_binary_func(lhs, rhs, clib.af_rem) |
| 50 | + |
| 51 | +def abs(a): |
| 52 | + return arith_unary_func(a, clib.af_abs) |
| 53 | + |
| 54 | +def arg(a): |
| 55 | + return arith_unary_func(a, clib.af_arg) |
| 56 | + |
| 57 | +def sign(a): |
| 58 | + return arith_unary_func(a, clib.af_sign) |
| 59 | + |
| 60 | +def round(a): |
| 61 | + return arith_unary_func(a, clib.af_round) |
| 62 | + |
| 63 | +def trunc(a): |
| 64 | + return arith_unary_func(a, clib.af_trunc) |
| 65 | + |
| 66 | +def floor(a): |
| 67 | + return arith_unary_func(a, clib.af_floor) |
| 68 | + |
| 69 | +def ceil(a): |
| 70 | + return arith_unary_func(a, clib.af_ceil) |
| 71 | + |
| 72 | +def hypot(lhs, rhs): |
| 73 | + return arith_binary_func(lhs, rhs, clib.af_hypot) |
| 74 | + |
| 75 | +def sin(a): |
| 76 | + return arith_unary_func(a, clib.af_sin) |
| 77 | + |
| 78 | +def cos(a): |
| 79 | + return arith_unary_func(a, clib.af_cos) |
| 80 | + |
| 81 | +def tan(a): |
| 82 | + return arith_unary_func(a, clib.af_tan) |
| 83 | + |
| 84 | +def asin(a): |
| 85 | + return arith_unary_func(a, clib.af_asin) |
| 86 | + |
| 87 | +def acos(a): |
| 88 | + return arith_unary_func(a, clib.af_acos) |
| 89 | + |
| 90 | +def atan(a): |
| 91 | + return arith_unary_func(a, clib.af_atan) |
| 92 | + |
| 93 | +def atan2(lhs, rhs): |
| 94 | + return arith_binary_func(lhs, rhs, clib.af_atan2) |
| 95 | + |
| 96 | +def cplx(lhs, rhs=None): |
| 97 | + if rhs is None: |
| 98 | + return arith_unary_func(lhs, clib.af_cplx) |
| 99 | + else: |
| 100 | + return arith_binary_func(lhs, rhs, clib.af_cplx2) |
| 101 | + |
| 102 | +def real(lhs): |
| 103 | + return arith_unary_func(lhs, clib.af_real) |
| 104 | + |
| 105 | +def imag(lhs): |
| 106 | + return arith_unary_func(lhs, clib.af_imag) |
| 107 | + |
| 108 | +def conjg(lhs): |
| 109 | + return arith_unary_func(lhs, clib.af_conjg) |
| 110 | + |
| 111 | +def sinh(a): |
| 112 | + return arith_unary_func(a, clib.af_sinh) |
| 113 | + |
| 114 | +def cosh(a): |
| 115 | + return arith_unary_func(a, clib.af_cosh) |
| 116 | + |
| 117 | +def tanh(a): |
| 118 | + return arith_unary_func(a, clib.af_tanh) |
| 119 | + |
| 120 | +def asinh(a): |
| 121 | + return arith_unary_func(a, clib.af_asinh) |
| 122 | + |
| 123 | +def acosh(a): |
| 124 | + return arith_unary_func(a, clib.af_acosh) |
| 125 | + |
| 126 | +def atanh(a): |
| 127 | + return arith_unary_func(a, clib.af_atanh) |
| 128 | + |
| 129 | +def root(lhs, rhs): |
| 130 | + return arith_binary_func(lhs, rhs, clib.af_root) |
| 131 | + |
| 132 | +def pow(lhs, rhs): |
| 133 | + return arith_binary_func(lhs, rhs, clib.af_pow) |
| 134 | + |
| 135 | +def pow2(a): |
| 136 | + return arith_unary_func(a, clib.af_pow2) |
| 137 | + |
| 138 | +def exp(a): |
| 139 | + return arith_unary_func(a, clib.af_exp) |
| 140 | + |
| 141 | +def expm1(a): |
| 142 | + return arith_unary_func(a, clib.af_expm1) |
| 143 | + |
| 144 | +def erf(a): |
| 145 | + return arith_unary_func(a, clib.af_erf) |
| 146 | + |
| 147 | +def erfc(a): |
| 148 | + return arith_unary_func(a, clib.af_erfc) |
| 149 | + |
| 150 | +def log(a): |
| 151 | + return arith_unary_func(a, clib.af_log) |
| 152 | + |
| 153 | +def log1p(a): |
| 154 | + return arith_unary_func(a, clib.af_log1p) |
| 155 | + |
| 156 | +def log10(a): |
| 157 | + return arith_unary_func(a, clib.af_log10) |
| 158 | + |
| 159 | +def log2(a): |
| 160 | + return arith_unary_func(a, clib.af_log2) |
| 161 | + |
| 162 | +def sqrt(a): |
| 163 | + return arith_unary_func(a, clib.af_sqrt) |
| 164 | + |
| 165 | +def cbrt(a): |
| 166 | + return arith_unary_func(a, clib.af_cbrt) |
| 167 | + |
| 168 | +def factorial(a): |
| 169 | + return arith_unary_func(a, clib.af_factorial) |
| 170 | + |
| 171 | +def tgamma(a): |
| 172 | + return arith_unary_func(a, clib.af_tgamma) |
| 173 | + |
| 174 | +def lgamma(a): |
| 175 | + return arith_unary_func(a, clib.af_lgamma) |
| 176 | + |
| 177 | +def iszero(a): |
| 178 | + return arith_unary_func(a, clib.af_iszero) |
| 179 | + |
| 180 | +def isinf(a): |
| 181 | + return arith_unary_func(a, clib.af_isinf) |
| 182 | + |
| 183 | +def isnan(a): |
| 184 | + return arith_unary_func(a, clib.af_isnan) |
0 commit comments