Skip to content

Implement the reciprocal ufunc #124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions quaddtype/numpy_quaddtype/src/ops.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ quad_square(const Sleef_quad *op)
return Sleef_mulq1_u05(*op, *op);
}

static inline Sleef_quad
quad_reciprocal(const Sleef_quad *op)
{
return Sleef_divq1_u05(QUAD_ONE, *op);
}

static inline Sleef_quad
quad_log(const Sleef_quad *op)
{
Expand Down Expand Up @@ -211,6 +217,12 @@ ld_square(const long double *op)
return (*op) * (*op);
}

static inline long double
ld_reciprocal(const long double *op)
{
return 1.0L / (*op);
}

static inline long double
ld_log(const long double *op)
{
Expand Down
3 changes: 3 additions & 0 deletions quaddtype/numpy_quaddtype/src/umath/unary_ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ init_quad_unary_ops(PyObject *numpy)
if (create_quad_unary_ufunc<quad_square, ld_square>(numpy, "square") < 0) {
return -1;
}
if (create_quad_unary_ufunc<quad_reciprocal, ld_reciprocal>(numpy, "reciprocal") < 0) {
return -1;
}
if (create_quad_unary_ufunc<quad_log, ld_log>(numpy, "log") < 0) {
return -1;
}
Expand Down
24 changes: 12 additions & 12 deletions quaddtype/release_tracker.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
| add | ✅ | ✅ |
| subtract | ✅ | ✅ |
| multiply | ✅ | ✅ |
| matmul | ✅ | ✅ |
| matmul | ✅ | ✅ |
| divide | ✅ | ✅ |
| logaddexp | | |
| logaddexp2 | | |
Expand All @@ -24,7 +24,7 @@
| absolute | ✅ | ✅ |
| fabs | | |
| rint | ✅ | ❌ _Need: basic tests + edge cases (NaN/inf/±0.0/halfway cases)_ |
| sign | ✅ | ✅ |
| sign | ✅ | ✅ |
| heaviside | | |
| conj | | |
| conjugate | | |
Expand All @@ -35,10 +35,10 @@
| log10 | ✅ | ❌ _Need: basic tests + edge cases (NaN/inf/0/-values/1)_ |
| expm1 | | |
| log1p | ✅ | ❌ _Need: basic tests + edge cases (NaN/inf/-1/small values)_ |
| sqrt | ✅ | ❌ _Need: basic tests + edge cases (NaN/inf/0/-values)_ |
| square | ✅ | ❌ _Need: basic tests + edge cases (NaN/inf/0/large values)_ |
| sqrt | ✅ | |
| square | ✅ | |
| cbrt | | |
| reciprocal | | |
| reciprocal | | ✅ |
| gcd | | |
| lcm | | |
| sin | ✅ | ❌ _Need: basic tests + edge cases (NaN/inf/0/π multiples/2π range)_ |
Expand Down Expand Up @@ -77,14 +77,14 @@
| logical_not | | |
| maximum | ✅ | ✅ |
| minimum | ✅ | ✅ |
| fmax | ✅ | ✅ |
| fmin | ✅ | ✅ |
| isfinite | ✅ | ✅ |
| isinf | ✅ | ✅ |
| isnan | ✅ | ✅ |
| fmax | ✅ | ✅ |
| fmin | ✅ | ✅ |
| isfinite | ✅ | ✅ |
| isinf | ✅ | ✅ |
| isnan | ✅ | ✅ |
| isnat | | |
| signbit | ✅ | ✅ |
| copysign | ✅ | ✅ |
| signbit | ✅ | ✅ |
| copysign | ✅ | ✅ |
| nextafter | | |
| spacing | | |
| modf | | |
Expand Down
8 changes: 5 additions & 3 deletions quaddtype/tests/test_quaddtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ def test_array_aminmax(op, a, b):
np.testing.assert_array_equal(np.array(quad_res).astype(float), float_res)


@pytest.mark.parametrize("op", ["negative", "positive", "absolute", "sign", "signbit", "isfinite", "isinf", "isnan"])
@pytest.mark.parametrize("val", ["3.0", "-3.0", "12.5", "100.0", "0.0", "-0.0", "inf", "-inf", "nan", "-nan"])
@pytest.mark.parametrize("op", ["negative", "positive", "absolute", "sign", "signbit", "isfinite", "isinf", "isnan", "sqrt", "square", "reciprocal"])
@pytest.mark.parametrize("val", ["3.0", "-3.0", "12.5", "100.0", "1e100", "0.0", "-0.0", "inf", "-inf", "nan", "-nan"])
def test_unary_ops(op, val):
op_func = dict(negative=operator.neg, positive=operator.pos, absolute=operator.abs).get(op, None)
nop_func = getattr(np, op)
Expand All @@ -120,7 +120,9 @@ def test_unary_ops(op, val):
float_result = of(float_val)

np.testing.assert_array_equal(np.array(quad_result).astype(float), float_result)
assert np.signbit(float_result) == np.signbit(quad_result)

if op in ["negative", "positive", "absolute", "sign"]:
assert np.signbit(float_result) == np.signbit(quad_result)


def test_inf():
Expand Down
Loading