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
7273x = 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 )))
0 commit comments