Skip to content

Commit 2c71f7e

Browse files
committed
clean up expected values of test scripts
1 parent 5c864a2 commit 2c71f7e

File tree

7 files changed

+237
-164
lines changed

7 files changed

+237
-164
lines changed

tests/1d/numpy/universal_functions.py

Lines changed: 79 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -7,142 +7,94 @@
77
import numpy as np
88
import scipy as spy
99

10-
result = (np.sin(np.pi/2))
11-
ref_result = 1.0
12-
print(math.isclose(result, ref_result, rel_tol=1E-6, abs_tol=1E-6))
10+
def test_function(func, reference, expression):
11+
result = expression
12+
print(f'testing {func}')
13+
print(math.isclose(result, reference, rel_tol=1E-6, abs_tol=1E-6))
14+
print()
1315

14-
result = (np.cos(np.pi/2))
15-
ref_result = 0.0
16-
print(math.isclose(result, ref_result, rel_tol=1E-6, abs_tol=1E-6))
16+
def test_function_array(func, reference, expression):
17+
print(f'testing {func}')
18+
results = []
19+
for i in range(len(references)):
20+
results.append(math.isclose(references[i], expression[i], rel_tol=1E-6, abs_tol=1E-6))
21+
print(results)
22+
print()
1723

18-
result = (np.tan(np.pi/2))
19-
ref_result = 1.633123935319537e+16
20-
print(math.isclose(result, ref_result, rel_tol=1E-6, abs_tol=1E-6))
2124

22-
result = (np.sinh(np.pi/2))
23-
ref_result = 2.3012989023072947
24-
print(math.isclose(result, ref_result, rel_tol=1E-6, abs_tol=1E-6))
25+
test_function('sin', 1.0, np.sin(np.pi/2))
2526

26-
result = (np.cosh(np.pi/2))
27-
ref_result = 2.5091784786580567
28-
print(math.isclose(result, ref_result, rel_tol=1E-6, abs_tol=1E-6))
27+
test_function('cos', 0.0, (np.cos(np.pi/2)))
2928

30-
result = (np.tanh(np.pi/2))
31-
ref_result = 0.9171523356672744
32-
print(math.isclose(result, ref_result, rel_tol=1E-6, abs_tol=1E-6))
29+
test_function('tan', 1.633123935319537e+16, np.tan(np.pi/2))
3330

34-
ref_result = np.pi/2
35-
result = (np.asin(np.sin(np.pi/2)))
36-
print(math.isclose(result, ref_result, rel_tol=1E-6, abs_tol=1E-6))
31+
test_function('sinh', 2.3012989023072947, np.sinh(np.pi/2))
3732

38-
result = (np.acos(np.cos(np.pi/2)))
39-
print(math.isclose(result, ref_result, rel_tol=1E-6, abs_tol=1E-6))
33+
test_function('cosh:', 2.5091784786580567, np.cosh(np.pi/2))
4034

41-
result = (np.atan(np.tan(np.pi/2)))
42-
print(math.isclose(result, ref_result, rel_tol=1E-6, abs_tol=1E-6))
35+
test_function('tanh', 0.9171523356672744, np.tanh(np.pi/2))
4336

44-
result = (np.cosh(np.acosh(np.pi/2)))
45-
print(math.isclose(result, ref_result, rel_tol=1E-6, abs_tol=1E-6))
37+
test_function('asin', np.pi/2, np.asin(np.sin(np.pi/2)))
4638

47-
result = (np.sinh(np.asinh(np.pi/2)))
48-
print(math.isclose(result, ref_result, rel_tol=1E-6, abs_tol=1E-6))
49-
50-
print(np.degrees(np.pi))
51-
print(np.radians(np.degrees(np.pi)))
52-
print(np.floor(np.pi))
53-
print(np.ceil(np.pi))
54-
print(np.sqrt(np.pi))
55-
print(np.exp(1))
56-
print(np.log(np.exp(1)))
57-
58-
print(np.log2(2**1))
59-
60-
print(np.log10(10**1))
61-
print(math.isclose(np.exp(1) - np.expm1(1), 1))
62-
63-
x = np.array([-1, +1, +1, -1])
64-
y = np.array([-1, -1, +1, +1])
65-
result = (np.arctan2(y, x) * 180 / np.pi)
66-
ref_result = np.array([-135.0, -45.0, 45.0, 135.0], dtype=np.float)
67-
cmp_result = []
68-
for i in range(len(x)):
69-
cmp_result.append(math.isclose(result[i], ref_result[i], rel_tol=1E-9, abs_tol=1E-9))
70-
print(cmp_result)
39+
test_function('acos', np.pi/2, np.acos(np.cos(np.pi/2)))
40+
41+
test_function('atan', np.pi/2, np.atan(np.tan(np.pi/2)))
42+
43+
test_function('acosh', np.pi/2, np.cosh(np.acosh(np.pi/2)))
44+
45+
test_function('asinh', np.pi/2, np.sinh(np.asinh(np.pi/2)))
46+
47+
test_function('degrees', 180.0, np.degrees(np.pi))
48+
49+
test_function('radians', np.pi, np.radians(180.0))
50+
51+
test_function('floor', 3.0, np.floor(np.pi))
52+
53+
test_function('ceil', 4.0, np.ceil(np.pi))
54+
55+
test_function('sqrt', 1.7724538509055159, np.sqrt(np.pi))
56+
57+
test_function('exp', 2.718281828459045, np.exp(1))
58+
59+
test_function('log', 1.0, np.log(2.718281828459045))
60+
61+
test_function('log2', 1.0, np.log2(2))
62+
63+
test_function('log10', 1.0, np.log10(10.0))
64+
65+
test_function('expm1', 1.0, np.exp(1) - np.expm1(1))
66+
67+
x = [-1, +1, +1, -1]
68+
y = [-1, -1, +1, +1]
69+
reference = [-135.0, -45.0, 45.0, 135.0]
70+
for i in range(4):
71+
test_function('arctan2', reference[i], np.arctan2(y[i], x[i]) * 180 / np.pi)
7172

7273
x = np.linspace(-2*np.pi, 2*np.pi, 5)
73-
result = np.sin(x)
74-
ref_result = np.array([2.4492936e-16, -1.2246468e-16, 0.0000000e+00, 1.2246468e-16, -2.4492936e-16], dtype=np.float)
75-
cmp_result = []
76-
for i in range(len(x)):
77-
cmp_result.append(math.isclose(result[i], ref_result[i], rel_tol=1E-9, abs_tol=1E-9))
78-
print(cmp_result)
79-
80-
result = np.cos(x)
81-
ref_result = np.array([1., -1., 1., -1., 1.], dtype=np.float)
82-
cmp_result = []
83-
for i in range(len(x)):
84-
cmp_result.append(math.isclose(result[i], ref_result[i], rel_tol=1E-9, abs_tol=1E-9))
85-
print(cmp_result)
86-
87-
result = np.tan(x)
88-
ref_result = np.array([2.4492936e-16, 1.2246468e-16, 0.0000000e+00, -1.2246468e-16, -2.4492936e-16], dtype=np.float)
89-
cmp_result = []
90-
for i in range(len(x)):
91-
cmp_result.append(math.isclose(result[i], ref_result[i], rel_tol=1E-9, abs_tol=1E-9))
92-
print(cmp_result)
93-
94-
result = np.sinh(x)
95-
ref_result = np.array([-267.74489404, -11.54873936, 0., 11.54873936, 267.74489404], dtype=np.float)
96-
cmp_result = []
97-
for i in range(len(x)):
98-
cmp_result.append(math.isclose(result[i], ref_result[i], rel_tol=1E-9, abs_tol=1E-9))
99-
print(cmp_result)
100-
101-
result = np.cosh(x)
102-
ref_result = np.array([267.74676148, 11.59195328, 1.0, 11.59195328, 267.74676148], dtype=np.float)
103-
cmp_result = []
104-
for i in range(len(x)):
105-
cmp_result.append(math.isclose(result[i], ref_result[i], rel_tol=1E-9, abs_tol=1E-9))
106-
print(cmp_result)
107-
108-
result = np.tanh(x)
109-
ref_result = np.array([-0.9999930253396107, -0.99627207622075, 0.0, 0.99627207622075, 0.9999930253396107], dtype=np.float)
110-
cmp_result = []
111-
for i in range(len(x)):
112-
cmp_result.append(math.isclose(result[i], ref_result[i], rel_tol=1E-9, abs_tol=1E-9))
113-
print(cmp_result)
114-
115-
result = np.sinc(x)
116-
ref_result = np.array([0.03935584386392389, -0.04359862862918773, 1.0, -0.04359862862918773, 0.03935584386392389])
117-
cmp_result = []
118-
for i in range(len(x)):
119-
cmp_result.append(math.isclose(result[i], ref_result[i], rel_tol=1E-9, abs_tol=1E-9))
120-
print(cmp_result)
121-
122-
result = (spy.special.erf(np.linspace(-3, 3, num=5)))
123-
ref_result = np.array([-0.9999779095030014, -0.9661051464753108, 0.0, 0.9661051464753108, 0.9999779095030014], dtype=np.float)
124-
cmp_result = []
125-
for i in range(len(ref_result)):
126-
cmp_result.append(math.isclose(result[i], ref_result[i], rel_tol=1E-9, abs_tol=1E-9))
127-
print(cmp_result)
128-
129-
result = (spy.special.erfc(np.linspace(-3, 3, num=5)))
130-
ref_result = np.array([1.99997791e+00, 1.96610515e+00, 1.00000000e+00, 3.38948535e-02, 2.20904970e-05], dtype=np.float)
131-
cmp_result = []
132-
for i in range(len(ref_result)):
133-
cmp_result.append(math.isclose(result[i], ref_result[i], rel_tol=1E-6, abs_tol=1E-6))
134-
print(cmp_result)
135-
136-
result = (spy.special.gamma(np.array([0, 0.5, 1, 5])))
137-
ref_result = np.array([1.77245385, 1.0, 24.0])
138-
cmp_result = []
139-
cmp_result.append(math.isinf(result[0]))
140-
for i in range(len(ref_result)):
141-
cmp_result.append(math.isclose(result[i+1], ref_result[i], rel_tol=1E-9, abs_tol=1E-9))
142-
print(cmp_result)
143-
144-
result = (spy.special.gammaln([0, -1, -2, -3, -4]))
145-
cmp_result = []
146-
for i in range(len(ref_result)):
147-
cmp_result.append(math.isinf(result[i]))
148-
print(cmp_result)
74+
75+
references = [2.4492936e-16, -1.2246468e-16, 0.0000000e+00, 1.2246468e-16, -2.4492936e-16]
76+
test_function_array('sin', references, np.sin(x))
77+
78+
references = [1., -1., 1., -1., 1.]
79+
test_function_array('cos', references, np.cos(x))
80+
81+
references = [0.0, 0.0, 0.0, 0.0, 0.0]
82+
test_function_array('tan', references, np.tan(x))
83+
84+
references = [-267.74489404, -11.54873936, 0., 11.54873936, 267.74489404]
85+
test_function_array('sinh', references, np.sinh(x))
86+
87+
references = [267.74676148, 11.59195328, 1.0, 11.59195328, 267.74676148]
88+
test_function_array('cosh', references, np.cosh(x))
89+
90+
references = [-0.9999930253396107, -0.99627207622075, 0.0, 0.99627207622075, 0.9999930253396107]
91+
test_function_array('tanh', references, np.tanh(x))
92+
93+
references = [0.03935584386392389, -0.04359862862918773, 1.0, -0.04359862862918773, 0.03935584386392389]
94+
test_function_array('sinc', references, np.sinc(x))
95+
96+
references = [-0.9999779095030014, -0.9661051464753108, 0.0, 0.9661051464753108, 0.9999779095030014]
97+
test_function_array('erf', references, spy.special.erf(np.linspace(-3, 3, num=5)))
98+
99+
references = [1.99997791e+00, 1.96610515e+00, 1.00000000e+00, 3.38948535e-02, 2.20904970e-05]
100+
test_function_array('erfc', references, spy.special.erfc(np.linspace(-3, 3, num=5)))
Lines changed: 81 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,102 @@
1+
testing sin
12
True
3+
4+
testing cos
25
True
6+
7+
testing tan
38
True
9+
10+
testing sinh
411
True
12+
13+
testing cosh:
514
True
15+
16+
testing tanh
617
True
18+
19+
testing asin
720
True
21+
22+
testing acos
823
True
24+
25+
testing atan
926
True
27+
28+
testing acosh
1029
True
30+
31+
testing asinh
1132
True
12-
180.0
13-
3.141592653589793
14-
3.0
15-
4.0
16-
1.772453850905516
17-
2.718281828459045
18-
1.0
19-
1.0
20-
1.0
33+
34+
testing degrees
2135
True
22-
[True, True, True, True]
36+
37+
testing radians
38+
True
39+
40+
testing floor
41+
True
42+
43+
testing ceil
44+
True
45+
46+
testing sqrt
47+
True
48+
49+
testing exp
50+
True
51+
52+
testing log
53+
True
54+
55+
testing log2
56+
True
57+
58+
testing log10
59+
True
60+
61+
testing expm1
62+
True
63+
64+
testing arctan2
65+
True
66+
67+
testing arctan2
68+
True
69+
70+
testing arctan2
71+
True
72+
73+
testing arctan2
74+
True
75+
76+
testing sin
2377
[True, True, True, True, True]
78+
79+
testing cos
2480
[True, True, True, True, True]
81+
82+
testing tan
2583
[True, True, True, True, True]
84+
85+
testing sinh
2686
[True, True, True, True, True]
87+
88+
testing cosh
2789
[True, True, True, True, True]
90+
91+
testing tanh
2892
[True, True, True, True, True]
93+
94+
testing sinc
2995
[True, True, True, True, True]
96+
97+
testing erf
3098
[True, True, True, True, True]
99+
100+
testing erfc
31101
[True, True, True, True, True]
32-
[True, True, True, True]
33-
[True, True, True]
102+

tests/2d/numpy/cholesky.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
11
from ulab import numpy as np
22

3+
import math
4+
35
a = np.array([[1, 2], [2, 5]])
4-
print(np.linalg.cholesky(a))
6+
reference = np.array([[1.0, 0.0], [2.0, 1.0]])
7+
result = np.linalg.cholesky(a)
8+
for i in range(2):
9+
for j in range(2):
10+
print(f'({i}, {j}): {math.isclose(result[i][j], reference[i][j], rel_tol=1E-6, abs_tol=1E-6)}')
511

6-
b = a = np.array([[25, 15, -5], [15, 18, 0], [-5, 0, 11]])
7-
print(np.linalg.cholesky(b))
12+
b = np.array([[25, 15, -5], [15, 18, 0], [-5, 0, 11]])
13+
reference = np.array([[5.0, 0.0, 0.0], [3.0, 3.0, 0.0], [-1.0, 1.0, 3.0]])
14+
result = np.linalg.cholesky(b)
15+
for i in range(3):
16+
for j in range(3):
17+
print(f'({i}, {j}): {math.isclose(result[i][j], reference[i][j], rel_tol=1E-6, abs_tol=1E-6)}')
818

919
c = np.array([[18, 22, 54, 42], [22, 70, 86, 62], [54, 86, 174, 134], [42, 62, 134, 106]])
10-
print(np.linalg.cholesky(c))
11-
12-
13-
14-
20+
reference = np.array([[4.242640687119285, 0.0, 0.0, 0.0],
21+
[5.185449728701349, 6.565905201197403, 0.0, 0.0],
22+
[12.727922061357857, 3.0460384954008553, 1.6497422479090682, 0.0],
23+
[9.899494936611665, 1.6245538642137891, 1.8497110052313865, 1.3926212476455826]])
24+
result = np.linalg.cholesky(c)
25+
for i in range(4):
26+
for j in range(4):
27+
print(f'({i}, {j}): {math.isclose(result[i][j], reference[i][j], rel_tol=1E-6, abs_tol=1E-6)}')

0 commit comments

Comments
 (0)