Implement NumPy math functions #339

Merged
sb10q merged 1 commits from issue-149 into master 2024-08-17 17:37:20 +08:00
1 changed files with 54 additions and 48 deletions

View File

@ -14,136 +14,142 @@ def dbl_nan() -> float:
def dbl_inf() -> float: def dbl_inf() -> float:
... ...
def dbl_pi() -> float:
return 3.1415926535897932384626433
Outdated
Review

Please use the same indentation as the other files.

Please use the same indentation as the other files.
def dbl_e() -> float:
return 2.71828182845904523536028747135266249775724709369995
def test_isnan(): def test_isnan():
for x in [dbl_nan(), 0.0, dbl_inf()]: for x in [dbl_nan(), 0.0, dbl_inf()]:
output_bool(isnan(x)) output_bool(isnan(x))
def test_isinf(): def test_isinf():
for x in [dbl_inf(), 0.0, dbl_nan()]: for x in [dbl_inf(), -dbl_inf(), 0.0, dbl_nan()]:
output_bool(isinf(x)) output_bool(isinf(x))
def test_sin(): def test_sin():
pi = 3.1415926535897932384626433 pi = dbl_pi()
for x in [-pi, -pi / 2.0, -pi / 4.0, 0.0, pi / 4.0, pi / 2.0, pi]: for x in [-pi, -pi / 2.0, -pi / 4.0, 0.0, pi / 4.0, pi / 2.0, pi, dbl_inf(), -dbl_inf(), dbl_nan()]:
output_float64(sin(x)) output_float64(sin(x))
def test_cos(): def test_cos():
pi = 3.1415926535897932384626433 pi = dbl_pi()
for x in [-pi, -pi / 2.0, -pi / 4.0, 0.0, pi / 4.0, pi / 2.0, pi]: for x in [-pi, -pi / 2.0, -pi / 4.0, 0.0, pi / 4.0, pi / 2.0, pi, dbl_inf(), -dbl_inf(), dbl_nan()]:
output_float64(cos(x)) output_float64(cos(x))
def test_exp(): def test_exp():
for x in [0.0, 1.0]: for x in [0.0, 1.0, dbl_inf(), -dbl_inf(), dbl_nan()]:
output_float64(exp(x)) output_float64(exp(x))
def test_exp2(): def test_exp2():
for x in [0.0, 1.0]: for x in [0.0, 1.0, dbl_inf(), -dbl_inf(), dbl_nan()]:
output_float64(exp2(x)) output_float64(exp2(x))
def test_log(): def test_log():
e = 2.71828182845904523536028747135266249775724709369995 e = dbl_e()
for x in [1.0, e]: for x in [1.0, e, dbl_inf(), -dbl_inf(), dbl_nan()]:
output_float64(log(x)) output_float64(log(x))
def test_log10(): def test_log10():
for x in [1.0, 10.0]: for x in [1.0, 10.0, dbl_inf(), -dbl_inf(), dbl_nan()]:
output_float64(log10(x)) output_float64(log10(x))
def test_log2(): def test_log2():
for x in [1.0, 2.0]: for x in [1.0, 2.0, dbl_inf(), -dbl_inf(), dbl_nan()]:
output_float64(log2(x)) output_float64(log2(x))
def test_fabs(): def test_fabs():
for x in [-1.0, 0.0, 1.0]: for x in [-1.0, 0.0, 1.0, dbl_inf(), -dbl_inf(), dbl_nan()]:
output_float64(fabs(x)) output_float64(fabs(x))
def test_floor(): def test_floor():
for x in [-1.5, -0.5, 0.5, 1.5]: for x in [-1.5, -0.5, 0.5, 1.5, dbl_inf(), -dbl_inf(), dbl_nan()]:
output_float64(floor(x)) output_float64(floor(x))
def test_ceil(): def test_ceil():
for x in [-1.5, -0.5, 0.5, 1.5]: for x in [-1.5, -0.5, 0.5, 1.5, dbl_inf(), -dbl_inf(), dbl_nan()]:
output_float64(ceil(x)) output_float64(ceil(x))
def test_trunc(): def test_trunc():
for x in [-1.5, -0.5, 0.5, 1.5]: for x in [-1.5, -0.5, 0.5, 1.5, dbl_inf(), -dbl_inf(), dbl_nan()]:
output_float64(trunc(x)) output_float64(trunc(x))
def test_sqrt(): def test_sqrt():
for x in [1.0, 2.0, 4.0]: for x in [1.0, 2.0, 4.0, dbl_inf(), -dbl_inf(), dbl_nan()]:
output_float64(sqrt(x)) output_float64(sqrt(x))
def test_rint(): def test_rint():
for x in [-1.5, -0.5, 0.5, 1.5]: for x in [-1.5, -0.5, 0.5, 1.5, dbl_inf(), -dbl_inf(), dbl_nan()]:
output_float64(rint(x)) output_float64(rint(x))
def test_tan(): def test_tan():
pi = 3.1415926535897932384626433 pi = dbl_pi()
for x in [-pi, -pi / 2.0, -pi / 4.0, 0.0, pi / 4.0, pi / 2.0, pi]: for x in [-pi, -pi / 2.0, -pi / 4.0, 0.0, pi / 4.0, pi / 2.0, pi, dbl_inf(), -dbl_inf(), dbl_nan()]:
output_float64(tan(x)) output_float64(tan(x))
def test_arcsin(): def test_arcsin():
for x in [-1.0, -0.5, 0.0, 0.5, 1.0]: for x in [-1.0, -0.5, 0.0, 0.5, 1.0, dbl_inf(), -dbl_inf(), dbl_nan()]:
output_float64(arcsin(x)) output_float64(arcsin(x))
def test_arccos(): def test_arccos():
for x in [-1.0, -0.5, 0.0, 0.5, 1.0]: for x in [-1.0, -0.5, 0.0, 0.5, 1.0, dbl_inf(), -dbl_inf(), dbl_nan()]:
output_float64(arccos(x)) output_float64(arccos(x))
def test_arctan(): def test_arctan():
for x in [-1.0, -0.5, 0.0, 0.5, 1.0]: for x in [-1.0, -0.5, 0.0, 0.5, 1.0, dbl_inf(), -dbl_inf(), dbl_nan()]:
output_float64(arctan(x)) output_float64(arctan(x))
def test_sinh(): def test_sinh():
for x in [-1.0, -0.5, 0.0, 0.5, 1.0]: for x in [-1.0, -0.5, 0.0, 0.5, 1.0, dbl_inf(), -dbl_inf(), dbl_nan()]:
output_float64(sinh(x)) output_float64(sinh(x))
def test_cosh(): def test_cosh():
for x in [-1.0, -0.5, 0.0, 0.5, 1.0]: for x in [-1.0, -0.5, 0.0, 0.5, 1.0, dbl_inf(), -dbl_inf(), dbl_nan()]:
output_float64(cosh(x)) output_float64(cosh(x))
def test_tanh(): def test_tanh():
for x in [-1.0, -0.5, 0.0, 0.5, 1.0]: for x in [-1.0, -0.5, 0.0, 0.5, 1.0, dbl_inf(), -dbl_inf(), dbl_nan()]:
output_float64(tanh(x)) output_float64(tanh(x))
def test_arcsinh(): def test_arcsinh():
for x in [-1.0, -0.5, 0.0, 0.5, 1.0]: for x in [-1.0, -0.5, 0.0, 0.5, 1.0, dbl_inf(), -dbl_inf(), dbl_nan()]:
output_float64(arcsinh(x)) output_float64(arcsinh(x))
def test_arccosh(): def test_arccosh():
for x in [-1.0, -0.5, 0.0, 0.5, 1.0]: for x in [-1.0, -0.5, 0.0, 0.5, 1.0, dbl_inf(), -dbl_inf(), dbl_nan()]:
output_float64(arccosh(x)) output_float64(arccosh(x))
def test_arctanh(): def test_arctanh():
for x in [-1.0, -0.5, 0.0, 0.5, 1.0]: for x in [-1.0, -0.5, 0.0, 0.5, 1.0, dbl_inf(), -dbl_inf(), dbl_nan()]:
output_float64(arctanh(x)) output_float64(arctanh(x))
def test_expm1(): def test_expm1():
for x in [0.0, 1.0]: for x in [0.0, 1.0, dbl_inf(), -dbl_inf(), dbl_nan()]:
output_float64(expm1(x)) output_float64(expm1(x))
def test_cbrt(): def test_cbrt():
for x in [1.0, 8.0, 27.0]: for x in [1.0, 8.0, 27.0, dbl_inf(), -dbl_inf(), dbl_nan()]:
output_float64(expm1(x)) output_float64(expm1(x))
def test_erf(): def test_erf():
for x in [-3.0, -2.0, -1.0, 0.0, 1.0, 2.0, 3.0]: for x in [-3.0, -2.0, -1.0, 0.0, 1.0, 2.0, 3.0, dbl_inf(), -dbl_inf(), dbl_nan()]:
output_float64(erf(x)) output_float64(erf(x))
def test_erfc(): def test_erfc():
for x in [-3.0, -2.0, -1.0, 0.0, 1.0, 2.0, 3.0]: for x in [-3.0, -2.0, -1.0, 0.0, 1.0, 2.0, 3.0, dbl_inf(), -dbl_inf(), dbl_nan()]:
output_float64(erfc(x)) output_float64(erfc(x))
def test_gamma(): def test_gamma():
for x in [-2.0, -1.5, -1.0, -0.5, 0.0, 0.5, 1.0, 1.5, 2.0]: for x in [-2.0, -1.5, -1.0, -0.5, 0.0, 0.5, 1.0, 1.5, 2.0, dbl_inf(), -dbl_inf(), dbl_nan()]:
output_float64(gamma(x)) output_float64(gamma(x))
def test_gammaln(): def test_gammaln():
for x in [-2.0, -1.5, -1.0, -0.5, 0.0, 0.5, 1.0, 1.5, 2.0]: for x in [-2.0, -1.5, -1.0, -0.5, 0.0, 0.5, 1.0, 1.5, 2.0, dbl_inf(), -dbl_inf(), dbl_nan()]:
output_float64(gammaln(x)) output_float64(gammaln(x))
def test_j0(): def test_j0():
for x in [-2.0, -1.5, -1.0, -0.5, 0.0, 0.5, 1.0, 1.5, 2.0]: for x in [-2.0, -1.5, -1.0, -0.5, 0.0, 0.5, 1.0, 1.5, 2.0, dbl_inf(), -dbl_inf(), dbl_nan()]:
output_float64(j0(x)) output_float64(j0(x))
def test_j1(): def test_j1():
@ -151,38 +157,38 @@ def test_j1():
output_float64(j1(x)) output_float64(j1(x))
def test_arctan2(): def test_arctan2():
for x1 in [-1.0, -0.5, 0.0, 0.5, 1.0]: for x1 in [-1.0, -0.5, 0.0, 0.5, 1.0, dbl_inf(), -dbl_inf(), dbl_nan()]:
for x2 in [-1.0, -0.5, 0.0, 0.5, 1.0]: for x2 in [-1.0, -0.5, 0.0, 0.5, 1.0, dbl_inf(), -dbl_inf(), dbl_nan()]:
output_float64(arctan2(x1, x2)) output_float64(arctan2(x1, x2))
def test_copysign(): def test_copysign():
for x1 in [-1.0, -0.5, 0.0, 0.5, 1.0]: for x1 in [-1.0, -0.5, 0.0, 0.5, 1.0, dbl_inf(), -dbl_inf(), dbl_nan()]:
for x2 in [-1.0, -0.5, 0.0, 0.5, 1.0]: for x2 in [-1.0, -0.5, 0.0, 0.5, 1.0, dbl_inf(), -dbl_inf(), dbl_nan()]:
output_float64(copysign(x1, x2)) output_float64(copysign(x1, x2))
def test_fmax(): def test_fmax():
for x1 in [-1.0, -0.5, 0.0, 0.5, 1.0]: for x1 in [-1.0, -0.5, 0.0, 0.5, 1.0, dbl_inf(), -dbl_inf(), dbl_nan()]:
for x2 in [-1.0, -0.5, 0.0, 0.5, 1.0]: for x2 in [-1.0, -0.5, 0.0, 0.5, 1.0, dbl_inf(), -dbl_inf(), dbl_nan()]:
output_float64(fmax(x1, x2)) output_float64(fmax(x1, x2))
def test_fmin(): def test_fmin():
for x1 in [-1.0, -0.5, 0.0, 0.5, 1.0]: for x1 in [-1.0, -0.5, 0.0, 0.5, 1.0, dbl_inf(), -dbl_inf(), dbl_nan()]:
for x2 in [-1.0, -0.5, 0.0, 0.5, 1.0]: for x2 in [-1.0, -0.5, 0.0, 0.5, 1.0, dbl_inf(), -dbl_inf(), dbl_nan()]:
output_float64(fmin(x1, x2)) output_float64(fmin(x1, x2))
def test_ldexp(): def test_ldexp():
for x1 in [-2.0, -1.5, -1.0, -0.5, 0.0, 0.5, 1.0, 1.5, 2.0]: for x1 in [-2.0, -1.5, -1.0, -0.5, 0.0, 0.5, 1.0, 1.5, 2.0, dbl_inf(), -dbl_inf(), dbl_nan()]:
for x2 in [-2, -1, 0, 1, 2]: for x2 in [-2, -1, 0, 1, 2]:
output_float64(ldexp(x1, x2)) output_float64(ldexp(x1, x2))
def test_hypot(): def test_hypot():
for x1 in [-2.0, -1.5, -1.0, -0.5, 0.0, 0.5, 1.0, 1.5, 2.0]: for x1 in [-2.0, -1.5, -1.0, -0.5, 0.0, 0.5, 1.0, 1.5, 2.0, dbl_inf(), -dbl_inf(), dbl_nan()]:
for x2 in [-2.0, -1.5, -1.0, -0.5, 0.0, 0.5, 1.0, 1.5, 2.0]: for x2 in [-2.0, -1.5, -1.0, -0.5, 0.0, 0.5, 1.0, 1.5, 2.0, dbl_inf(), -dbl_inf(), dbl_nan()]:
output_float64(hypot(x1, x2)) output_float64(hypot(x1, x2))
def test_nextafter(): def test_nextafter():
for x1 in [-2.0, -1.5, -1.0, -0.5, 0.0, 0.5, 1.0, 1.5, 2.0]: for x1 in [-2.0, -1.5, -1.0, -0.5, 0.0, 0.5, 1.0, 1.5, 2.0, dbl_inf(), -dbl_inf(), dbl_nan()]:
for x2 in [-2.0, -1.5, -1.0, -0.5, 0.0, 0.5, 1.0, 1.5, 2.0]: for x2 in [-2.0, -1.5, -1.0, -0.5, 0.0, 0.5, 1.0, 1.5, 2.0, dbl_inf(), -dbl_inf(), dbl_nan()]:
output_float64(nextafter(x1, x2)) output_float64(nextafter(x1, x2))
def run() -> int32: def run() -> int32: