Skip to content

Speedup the reduce method of the maximum minimum family of ufuncs. #18

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

Closed
wants to merge 4 commits into from
Closed
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
62 changes: 49 additions & 13 deletions numpy/core/src/umath/loops.c.src
Original file line number Diff line number Diff line change
Expand Up @@ -683,10 +683,19 @@ NPY_NO_EXPORT void
NPY_NO_EXPORT void
@S@@TYPE@_@kind@(char **args, intp *dimensions, intp *steps, void *NPY_UNUSED(func))
{
BINARY_LOOP {
const @s@@type@ in1 = *(@s@@type@ *)ip1;
const @s@@type@ in2 = *(@s@@type@ *)ip2;
*((@s@@type@ *)op1) = (in1 @OP@ in2) ? in1 : in2;
if (IS_BINARY_REDUCE) {
BINARY_REDUCE_LOOP(@s@@type@) {
const @s@@type@ in2 = *(@type@ *)ip2;
io1 = (io1 @OP@ in2) ? io1 : in2;
}
*((@s@@type@ *)iop1) = io1;
}
else {
BINARY_LOOP {
const @s@@type@ in1 = *(@s@@type@ *)ip1;
const @s@@type@ in2 = *(@s@@type@ *)ip2;
*((@s@@type@ *)op1) = (in1 @OP@ in2) ? in1 : in2;
}
}
}
/**end repeat2**/
Expand Down Expand Up @@ -921,11 +930,20 @@ NPY_NO_EXPORT void
NPY_NO_EXPORT void
@TYPE@_@kind@(char **args, intp *dimensions, intp *steps, void *NPY_UNUSED(func))
{
if (IS_BINARY_REDUCE) {
BINARY_REDUCE_LOOP(@type@) {
const @type@ in2 = *(@type@ *)ip2;
io1 = (io1 @OP@ in2) ? io1 : in2;
}
*((@type@ *)iop1) = io1;
}
else {
BINARY_LOOP {
const @type@ in1 = *(@type@ *)ip1;
const @type@ in2 = *(@type@ *)ip2;
*((@type@ *)op1) = (in1 @OP@ in2) ? in1 : in2;
}
}
}
/**end repeat1**/

Expand All @@ -949,7 +967,7 @@ NPY_NO_EXPORT void

/**end repeat**/

/* FIXME: implement the following correctly using the metadata: data is the
/* FIXME: implement the following correctly using the metadata: data is the
sequence of ndarrays in the same order as args.
*/
NPY_NO_EXPORT void
Expand Down Expand Up @@ -1141,10 +1159,19 @@ NPY_NO_EXPORT void
@TYPE@_@kind@(char **args, intp *dimensions, intp *steps, void *NPY_UNUSED(func))
{
/* */
BINARY_LOOP {
const @type@ in1 = *(@type@ *)ip1;
const @type@ in2 = *(@type@ *)ip2;
*((@type@ *)op1) = (in1 @OP@ in2 || npy_isnan(in1)) ? in1 : in2;
if (IS_BINARY_REDUCE) {
BINARY_REDUCE_LOOP(@type@) {
const @type@ in2 = *(@type@ *)ip2;
io1 = (io1 @OP@ in2 || npy_isnan(io1)) ? io1 : in2;
}
*((@type@ *)iop1) = io1;
}
else {
BINARY_LOOP {
const @type@ in1 = *(@type@ *)ip1;
const @type@ in2 = *(@type@ *)ip2;
*((@type@ *)op1) = (in1 @OP@ in2 || npy_isnan(in1)) ? in1 : in2;
}
}
}
/**end repeat1**/
Expand All @@ -1157,10 +1184,19 @@ NPY_NO_EXPORT void
@TYPE@_@kind@(char **args, intp *dimensions, intp *steps, void *NPY_UNUSED(func))
{
/* */
BINARY_LOOP {
const @type@ in1 = *(@type@ *)ip1;
const @type@ in2 = *(@type@ *)ip2;
*((@type@ *)op1) = (in1 @OP@ in2 || npy_isnan(in2)) ? in1 : in2;
if (IS_BINARY_REDUCE) {
BINARY_REDUCE_LOOP(@type@) {
const @type@ in2 = *(@type@ *)ip2;
io1 = (io1 @OP@ in2 || npy_isnan(in2)) ? io1 : in2;
}
*((@type@ *)iop1) = io1;
}
else {
BINARY_LOOP {
const @type@ in1 = *(@type@ *)ip1;
const @type@ in2 = *(@type@ *)ip2;
*((@type@ *)op1) = (in1 @OP@ in2 || npy_isnan(in2)) ? in1 : in2;
}
}
}
/**end repeat1**/
Expand Down
84 changes: 84 additions & 0 deletions numpy/core/tests/test_umath.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,27 @@ def test_ldexp_overflow(self):


class TestMaximum(TestCase):
def test_reduce(self):
dflt = np.typecodes['AllFloat']
dint = np.typecodes['AllInteger']
seq1 = np.arange(11)
seq2 = seq1[::-1]
func = np.maximum.reduce
for dt in dint:
tmp1 = seq1.astype(dt)
tmp2 = seq2.astype(dt)
assert_equal(func(tmp1), 10)
assert_equal(func(tmp2), 10)
for dt in dflt:
tmp1 = seq1.astype(dt)
tmp2 = seq2.astype(dt)
assert_equal(func(tmp1), 10)
assert_equal(func(tmp2), 10)
tmp1[::2] = np.nan
tmp2[::2] = np.nan
assert_equal(func(tmp1), np.nan)
assert_equal(func(tmp2), np.nan)

def test_reduce_complex(self):
assert_equal(np.maximum.reduce([1,2j]),1)
assert_equal(np.maximum.reduce([1+3j,2j]),1+3j)
Expand All @@ -434,6 +455,27 @@ def test_complex_nans(self):


class TestMinimum(TestCase):
def test_reduce(self):
dflt = np.typecodes['AllFloat']
dint = np.typecodes['AllInteger']
seq1 = np.arange(11)
seq2 = seq1[::-1]
func = np.minimum.reduce
for dt in dint:
tmp1 = seq1.astype(dt)
tmp2 = seq2.astype(dt)
assert_equal(func(tmp1), 0)
assert_equal(func(tmp2), 0)
for dt in dflt:
tmp1 = seq1.astype(dt)
tmp2 = seq2.astype(dt)
assert_equal(func(tmp1), 0)
assert_equal(func(tmp2), 0)
tmp1[::2] = np.nan
tmp2[::2] = np.nan
assert_equal(func(tmp1), np.nan)
assert_equal(func(tmp2), np.nan)

def test_reduce_complex(self):
assert_equal(np.minimum.reduce([1,2j]),2j)
assert_equal(np.minimum.reduce([1+3j,2j]),2j)
Expand All @@ -455,6 +497,27 @@ def test_complex_nans(self):


class TestFmax(TestCase):
def test_reduce(self):
dflt = np.typecodes['AllFloat']
dint = np.typecodes['AllInteger']
seq1 = np.arange(11)
seq2 = seq1[::-1]
func = np.fmax.reduce
for dt in dint:
tmp1 = seq1.astype(dt)
tmp2 = seq2.astype(dt)
assert_equal(func(tmp1), 10)
assert_equal(func(tmp2), 10)
for dt in dflt:
tmp1 = seq1.astype(dt)
tmp2 = seq2.astype(dt)
assert_equal(func(tmp1), 10)
assert_equal(func(tmp2), 10)
tmp1[::2] = np.nan
tmp2[::2] = np.nan
assert_equal(func(tmp1), 9)
assert_equal(func(tmp2), 9)

def test_reduce_complex(self):
assert_equal(np.fmax.reduce([1,2j]),1)
assert_equal(np.fmax.reduce([1+3j,2j]),1+3j)
Expand All @@ -476,6 +539,27 @@ def test_complex_nans(self):


class TestFmin(TestCase):
def test_reduce(self):
dflt = np.typecodes['AllFloat']
dint = np.typecodes['AllInteger']
seq1 = np.arange(11)
seq2 = seq1[::-1]
func = np.fmin.reduce
for dt in dint:
tmp1 = seq1.astype(dt)
tmp2 = seq2.astype(dt)
assert_equal(func(tmp1), 0)
assert_equal(func(tmp2), 0)
for dt in dflt:
tmp1 = seq1.astype(dt)
tmp2 = seq2.astype(dt)
assert_equal(func(tmp1), 0)
assert_equal(func(tmp2), 0)
tmp1[::2] = np.nan
tmp2[::2] = np.nan
assert_equal(func(tmp1), 1)
assert_equal(func(tmp2), 1)

def test_reduce_complex(self):
assert_equal(np.fmin.reduce([1,2j]),2j)
assert_equal(np.fmin.reduce([1+3j,2j]),2j)
Expand Down
20 changes: 14 additions & 6 deletions numpy/lib/function_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1447,8 +1447,7 @@ def nanmin(a, axis=None):
Positive infinity is treated as a very large number and negative infinity
is treated as a very small (i.e. negative) number.

If the input has a integer type, an integer type is returned unless
the input contains NaNs and infinity.
If the input has a integer type the function is equivalent to np.min.


Examples
Expand All @@ -1469,7 +1468,12 @@ def nanmin(a, axis=None):
-inf

"""
return _nanop(np.min, np.inf, a, axis)
#return _nanop(np.min, np.inf, a, axis)
a = np.asanyarray(a)
if axis is not None:
return np.fmin.reduce(a, axis)
else:
return np.fmin.reduce(a.flat)

def nanargmin(a, axis=None):
"""
Expand Down Expand Up @@ -1541,8 +1545,7 @@ def nanmax(a, axis=None):
Positive infinity is treated as a very large number and negative infinity
is treated as a very small (i.e. negative) number.

If the input has a integer type, an integer type is returned unless
the input contains NaNs and infinity.
If the input has a integer type the function is equivalent to np.max.

Examples
--------
Expand All @@ -1562,7 +1565,12 @@ def nanmax(a, axis=None):
inf

"""
return _nanop(np.max, -np.inf, a, axis)
#return _nanop(np.max, -np.inf, a, axis)
a = np.asanyarray(a)
if axis is not None:
return np.fmax.reduce(a, axis)
else:
return np.fmax.reduce(a.flat)

def nanargmax(a, axis=None):
"""
Expand Down
Loading