Skip to content

Commit 02115b2

Browse files
committed
Add test function for fastmath
1 parent 1a48f3d commit 02115b2

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

stumpy/fastmath.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,36 @@
11
import importlib
22

3+
from numba import njit
4+
35
from stumpy import config
46

57

8+
@njit(fastmath=True)
9+
def _add_assoc(x, y): # pragma: no cover
10+
"""
11+
A dummy function to test the fastmath module
12+
13+
Parameters
14+
----------
15+
x : float
16+
A float value
17+
18+
y : floatf
19+
A float value
20+
21+
Returns
22+
-------
23+
out : float
24+
The ouput valus
25+
26+
Notes
27+
-----
28+
This is taken from the following link:
29+
https://numba.pydata.org/numba-doc/dev/user/performance-tips.html#fastmath
30+
"""
31+
return (x - y) + y
32+
33+
634
def _set(module_name, func_name, flag):
735
"""
836
Set fastmath flag for a given function

tests/test_fastmath.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import numpy as np
2+
3+
from stumpy import fastmath
4+
5+
6+
def test_fastmath():
7+
# Test the _set and _reset function in fastmath.py
8+
# The test is done by changing the value of fastmath flag for
9+
# the fastmath._add_assoc function
10+
# See: https://numba.pydata.org/numba-doc/dev/user/performance-tips.html#fastmath
11+
12+
x, y = 0.0, np.inf
13+
14+
# fastmath=False
15+
fastmath._set("fastmath", "_add_assoc", False)
16+
out = fastmath._add_assoc(x, y)
17+
assert np.isnan(out)
18+
19+
# fastmath={'reassoc', 'nsz'}
20+
fastmath._set("fastmath", "_add_assoc", {"reassoc", "nsz"})
21+
out = fastmath._add_assoc(x, y)
22+
assert out == 0.0
23+
24+
# fastmath={'reassoc'}
25+
fastmath._set("fastmath", "_add_assoc", {"reassoc"})
26+
out = fastmath._add_assoc(x, y)
27+
assert np.isnan(out)
28+
29+
# fastmath={'nsz'}
30+
fastmath._set("fastmath", "_add_assoc", {"nsz"})
31+
out = fastmath._add_assoc(x, y)
32+
assert np.isnan(out)
33+
34+
# reset value of fastmath (default is True)
35+
fastmath._reset("fastmath", "_add_assoc")
36+
out = fastmath._add_assoc(x, y)
37+
assert out == 0.0

0 commit comments

Comments
 (0)