Skip to content

Commit 07b54ad

Browse files
committed
Add hypot
1 parent d02cc8d commit 07b54ad

6 files changed

Lines changed: 26 additions & 5 deletions

File tree

numexpr/expressions.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ def multiply(x, y):
348348

349349
'fmod': func(numpy.fmod, 'float'),
350350
'arctan2': func(numpy.arctan2, 'float'),
351+
'hypot': func(numpy.hypot, 'float'),
351352

352353
'log': func(numpy.log, 'float'),
353354
'log1p': func(numpy.log1p, 'float'),

numexpr/functions.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ FUNC_FF(FUNC_FF_LAST, NULL, NULL, NULL, NULL)
4949
#endif
5050
FUNC_FFF(FUNC_FMOD_FFF, "fmod_fff", fmodf, fmodf2, vsfmod)
5151
FUNC_FFF(FUNC_ARCTAN2_FFF, "arctan2_fff", atan2f, atan2f2, vsAtan2)
52+
FUNC_FFF(FUNC_HYPOT_FFF, "hypot_fff", hypotf, hypotf2, vsHypot)
5253
FUNC_FFF(FUNC_FFF_LAST, NULL, NULL, NULL, NULL)
5354
#ifdef ELIDE_FUNC_FFF
5455
#undef ELIDE_FUNC_FFF
@@ -122,6 +123,7 @@ FUNC_BF(FUNC_BF_LAST, NULL, NULL, NULL, NULL)
122123
#endif
123124
FUNC_DDD(FUNC_FMOD_DDD, "fmod_ddd", fmod, vdfmod)
124125
FUNC_DDD(FUNC_ARCTAN2_DDD, "arctan2_ddd", atan2, vdAtan2)
126+
FUNC_DDD(FUNC_HYPOT_DDD, "hypot_ddd", hypot, vdHypot)
125127
FUNC_DDD(FUNC_DDD_LAST, NULL, NULL, NULL)
126128
#ifdef ELIDE_FUNC_DDD
127129
#undef ELIDE_FUNC_DDD

numexpr/interpreter.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,16 @@ static void vzLog1p(MKL_INT n, const MKL_Complex16* x1, MKL_Complex16* dest)
413413
vzLn(n, dest, dest);
414414
};
415415

416+
static void vzLog2(MKL_INT n, const MKL_Complex16* x1, MKL_Complex16* dest)
417+
{
418+
MKL_INT j;
419+
vzLn(n, x1, dest);
420+
for (j=0; j<n; j++) {
421+
dest[j].real = dest[j].real * M_LOG2_E;
422+
dest[j].imag = dest[j].imag * M_LOG2_E;
423+
};
424+
};
425+
416426
/* Use this instead of native vzAbs in VML as it seems to work badly */
417427
static void vzAbs_(MKL_INT n, const MKL_Complex16* x1, MKL_Complex16* dest)
418428
{

numexpr/msvc_function_stubs.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#define fmodf(x, y) ((float)fmod((double)(x), (double)(y)))
4343
#define atan2f(x, y) ((float)atan2((double)(x), (double)(y)))
4444
#define ceilf(x) ((float)ceil((double)(x)))
45+
#define hypotf(x) ((float)hypot((double)(x)))
4546

4647
/* The next are directly called from interp_body.cpp */
4748
#define powf(x, y) ((float)pow((double)(x), (double)(y)))
@@ -148,6 +149,11 @@ inline float atan2f2(float x, float y) {
148149
return atan2f(x, y);
149150
}
150151

152+
inline float hypotf2(float x, float y) {
153+
return hypotf(x, y);
154+
}
155+
156+
151157
// Boolean output functions
152158
inline bool isnanf2(float x) {
153159
return isnanf_(x);

numexpr/necompiler.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
"isnan",
7575
"isfinite",
7676
"isinf",
77+
"hypot"
7778
]
7879

7980

numexpr/tests/test_numexpr.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
from numpy import (allclose, arange, arccos, arccosh, arcsin, arcsinh, arctan,
2626
arctan2, arctanh, array, array_equal, cdouble, ceil, conj,
2727
copy, cos, cosh, empty, exp, expm1, float64, floor, fmod,
28-
int32, int64, isinf, isnan, linspace, log, log1p, log2,
29-
log10, ones_like, prod, ravel, rec, shape, sin, sinh, sqrt,
30-
sum, tan, tanh, uint16, where, zeros)
28+
hypot, int32, int64, isinf, isnan, linspace, log, log1p,
29+
log2, log10, ones_like, prod, ravel, rec, shape, sin, sinh,
30+
sqrt, sum, tan, tanh, uint16, where, zeros)
3131
from numpy.testing import (assert_allclose, assert_array_almost_equal,
3232
assert_array_equal, assert_equal)
3333

@@ -714,7 +714,7 @@ def test_ex_uses_vml(self):
714714
vml_funcs = [ "sin", "cos", "tan", "arcsin", "arccos", "arctan",
715715
"sinh", "cosh", "tanh", "arcsinh", "arccosh", "arctanh",
716716
"log", "log1p","log10", "log2", "exp", "expm1", "abs", "conj",
717-
"arctan2", "fmod"]
717+
"arctan2", "fmod", "hypot"]
718718
for func in vml_funcs:
719719
strexpr = func+'(a)'
720720
_, ex_uses_vml = numexpr.necompiler.getExprNames(strexpr, {})
@@ -813,7 +813,7 @@ def test_changing_nthreads_01_dec(self):
813813
tests.append(('1_ARG_FUNCS', func1tests))
814814

815815
func2tests = []
816-
for func in ['arctan2', 'fmod']:
816+
for func in ['arctan2', 'fmod', 'hypot']:
817817
func2tests.append("a + %s(b+c, d+1)" % func)
818818
func2tests.append("a + %s(b+c, 1)" % func)
819819
func2tests.append("a + %s(1, d+1)" % func)
@@ -877,6 +877,7 @@ class Skip(Exception): pass
877877
or "%" in expr
878878
or "arctan2" in expr
879879
or "fmod" in expr
880+
or "hypot" in expr
880881
or "floor" in expr
881882
or "ceil" in expr
882883
)

0 commit comments

Comments
 (0)