def test_one():
from math import sin, cos, tan, asin, acos, atan
from math import sinh, cosh, tanh, asinh, acosh, atanh
from math import exp, expm1, log, log10, log1p, sqrt, lgamma
from math import fabs, ceil, floor, trunc, erf, erfc
try:
from math import log2
except ImportError:
def log2(x):
return log(x) / log(2)
def wrapper(f, v):
try:
return f(v)
except ValueError:
if f == sqrt:
return float('nan')
if v >= 0:
return float('inf')
else:
return -float('inf')
def compare(a, b):
if isfinite(a) and isfinite(b):
return assert_almost_equals(a, b)
return str(a) == str(b)
for f in [sin, cos, tan, asin, acos, atan,
sinh, cosh, tanh, asinh, acosh, atanh,
exp, expm1, log, log2, log10, log1p, sqrt,
lgamma,
fabs, ceil, floor, trunc,
erf, erfc]:
for p in [0.5, 1]:
a = random_lst(p=p)
b = SparseArray.fromlist(a)
c = getattr(b, f.__name__)()
res = [wrapper(f, x) for x in a]
index = [k for k, v in enumerate(res) if v != 0]
res = [x for x in res if x != 0]
print(f, p, c.non_zero, len(res))
assert c.non_zero == len(res)
[assert_almost_equals(v, w) for v, w in zip(index,
c.index)]
[compare(v, w) for v, w in zip(res,
c.data)]
评论列表
文章目录