Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add tests for gufunc attributes
  • Loading branch information
guilhermeleobas committed Sep 28, 2021
commit ed2ea862bec2e7a1b491fa766718288a09ee8627
51 changes: 51 additions & 0 deletions numba/tests/npyufunc/test_gufunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,59 @@ def gufunc(x, res):
contains = hasattr(gufunc, attr)
self.assertTrue(contains, 'dynamic gufunc not exporting "%s"' % (attr,))

a = np.array([1, 2, 3, 4])
res = np.array([0, 0, 0, 0])
gufunc(a, res) # trigger compilation
self.assertPreciseEqual(res, np.array([1, 3, 6, 10]))

# other attributes are not callable from a gufunc with signaturek
# see: https://github.com/numba/numba/issues/2794
# note: this is a limitation in NumPy source code!
self.assertEqual(gufunc.signature, "(n)->(n)")

with self.assertRaises(RuntimeError) as raises:
gufunc.accumulate(a)
self.assertEqual(str(raises.exception), "Reduction not defined on ufunc with signature")

with self.assertRaises(RuntimeError) as raises:
gufunc.reduce(a)
self.assertEqual(str(raises.exception), "Reduction not defined on ufunc with signature")

with self.assertRaises(RuntimeError) as raises:
gufunc.reduceat(a, [0, 2])
self.assertEqual(str(raises.exception), "Reduction not defined on ufunc with signature")

with self.assertRaises(TypeError) as raises:
gufunc.outer(a, a)
self.assertEqual(str(raises.exception), "method outer is not allowed in ufunc with non-trivial signature")

def test_gufunc_attributes2(self):
@guvectorize('(),()->()')
def add(x, y, res):
res[0] = x + y

# add signature "(),() -> ()" is evaluated to None
self.assertIsNone(add.signature)

# test other attributes using "add" gufunc
a = np.array([1, 2, 3, 4])
b = np.array([4, 3, 2, 1])
res = np.array([0, 0, 0, 0])
add(a, b, res) # trigger compilation
self.assertPreciseEqual(res, np.array([5, 5, 5, 5]))

# now test other attributes
self.assertIsNone(add.signature)
self.assertEqual(add.reduce(a), 10)
self.assertPreciseEqual(add.accumulate(a), np.array([1, 3, 6, 10]))
self.assertPreciseEqual(add.outer([0, 1], [1, 2]), np.array([[1, 2], [2, 3]]))
self.assertPreciseEqual(add.reduceat(a, [0, 2]), np.array([3, 7]))

x = np.array([1, 2, 3, 4])
y = np.array([1, 2])
add.at(x, [0, 1], y)
self.assertPreciseEqual(x, np.array([2, 4, 3, 4]))


class TestGUVectorizeScalar(TestCase):
"""
Expand Down