Skip to content

Commit 0635b84

Browse files
authored
Merge pull request #124 from juntyr/reciprocal
Implement the reciprocal ufunc
2 parents 29ca55e + bea5d1c commit 0635b84

File tree

4 files changed

+32
-15
lines changed

4 files changed

+32
-15
lines changed

quaddtype/numpy_quaddtype/src/ops.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ quad_square(const Sleef_quad *op)
7272
return Sleef_mulq1_u05(*op, *op);
7373
}
7474

75+
static inline Sleef_quad
76+
quad_reciprocal(const Sleef_quad *op)
77+
{
78+
return Sleef_divq1_u05(QUAD_ONE, *op);
79+
}
80+
7581
static inline Sleef_quad
7682
quad_log(const Sleef_quad *op)
7783
{
@@ -211,6 +217,12 @@ ld_square(const long double *op)
211217
return (*op) * (*op);
212218
}
213219

220+
static inline long double
221+
ld_reciprocal(const long double *op)
222+
{
223+
return 1.0L / (*op);
224+
}
225+
214226
static inline long double
215227
ld_log(const long double *op)
216228
{

quaddtype/numpy_quaddtype/src/umath/unary_ops.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,9 @@ init_quad_unary_ops(PyObject *numpy)
177177
if (create_quad_unary_ufunc<quad_square, ld_square>(numpy, "square") < 0) {
178178
return -1;
179179
}
180+
if (create_quad_unary_ufunc<quad_reciprocal, ld_reciprocal>(numpy, "reciprocal") < 0) {
181+
return -1;
182+
}
180183
if (create_quad_unary_ufunc<quad_log, ld_log>(numpy, "log") < 0) {
181184
return -1;
182185
}

quaddtype/release_tracker.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
| add |||
88
| subtract |||
99
| multiply |||
10-
| matmul |||
10+
| matmul | ||
1111
| divide |||
1212
| logaddexp | | |
1313
| logaddexp2 | | |
@@ -24,7 +24,7 @@
2424
| absolute |||
2525
| fabs | | |
2626
| rint ||_Need: basic tests + edge cases (NaN/inf/±0.0/halfway cases)_ |
27-
| sign |||
27+
| sign | ||
2828
| heaviside | | |
2929
| conj | | |
3030
| conjugate | | |
@@ -35,10 +35,10 @@
3535
| log10 ||_Need: basic tests + edge cases (NaN/inf/0/-values/1)_ |
3636
| expm1 | | |
3737
| log1p ||_Need: basic tests + edge cases (NaN/inf/-1/small values)_ |
38-
| sqrt || _Need: basic tests + edge cases (NaN/inf/0/-values)_ |
39-
| square || _Need: basic tests + edge cases (NaN/inf/0/large values)_ |
38+
| sqrt || |
39+
| square || |
4040
| cbrt | | |
41-
| reciprocal | | |
41+
| reciprocal | | |
4242
| gcd | | |
4343
| lcm | | |
4444
| sin ||_Need: basic tests + edge cases (NaN/inf/0/π multiples/2π range)_ |
@@ -77,14 +77,14 @@
7777
| logical_not | | |
7878
| maximum |||
7979
| minimum |||
80-
| fmax |||
81-
| fmin |||
82-
| isfinite |||
83-
| isinf |||
84-
| isnan |||
80+
| fmax | ||
81+
| fmin | ||
82+
| isfinite | ||
83+
| isinf | ||
84+
| isnan | ||
8585
| isnat | | |
86-
| signbit |||
87-
| copysign |||
86+
| signbit | ||
87+
| copysign | ||
8888
| nextafter | | |
8989
| spacing | | |
9090
| modf | | |

quaddtype/tests/test_quaddtype.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ def test_array_aminmax(op, a, b):
103103
np.testing.assert_array_equal(np.array(quad_res).astype(float), float_res)
104104

105105

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

122122
np.testing.assert_array_equal(np.array(quad_result).astype(float), float_result)
123-
assert np.signbit(float_result) == np.signbit(quad_result)
123+
124+
if op in ["negative", "positive", "absolute", "sign"]:
125+
assert np.signbit(float_result) == np.signbit(quad_result)
124126

125127

126128
def test_inf():

0 commit comments

Comments
 (0)