-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
363 lines (259 loc) · 9.42 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# test.py
##############################################################################
# Imports #
##############################################################################
import pytest
from calculus import *
from linear_algebra import *
from statistics_and_probability import *
from number_theory import *
import numpy as np
from sympy import Function, sympify, Eq, dsolve
import trigo_and_log as trig
import math
##############################################################################
# CONSTANTS #
##############################################################################
x = symbols('x')
F = Function('F')
"""
calculus tests
"""
def test_calculate_limit():
result = calculate_limit('x', 2)
print(f"calculate_limit('x', 2) = {result}")
assert result == 2
result = calculate_limit('x^2', 3)
print(f"calculate_limit('x^2', 3) = {result}")
assert result == 9
def test_calculate_derivative():
result = calculate_derivative('x^2')
print(f"calculate_derivative('x^2') = {result}")
assert str(result) == '2*x'
result = calculate_derivative('sin(x)')
print(f"calculate_derivative('sin(x)') = {result}")
assert str(result) == 'cos(x)'
def test_calculate_integral():
result = calculate_integral('2*x')
print(f"calculate_integral('2*x') = {result}")
assert str(result) == 'x**2'
result = calculate_integral('cos(x)')
print(f"calculate_integral('cos(x)') = {result}")
assert str(result) == 'sin(x)'
def test_calculate_definite_integral():
result = calculate_definite_integral('x', 0, 2)
print(f"calculate_definite_integral('x', 0, 2) = {result}")
assert result == '2'
result = calculate_definite_integral('x^2', 0, 3)
print(f"calculate_definite_integral('x^2', 0, 3) = {result}")
assert result == '9'
def test_calculate_improper_integral():
result = calculate_improper_integral('exp(-x)', 0)
print(f"calculate_improper_integral('exp(-x)', 0) = {result}")
assert result == 1
def test_calculate_double_derivative():
result = calculate_double_derivative('x^3')
print(f"calculate_double_derivative('x^3') = {result}")
assert str(result) == '6*x'
result = calculate_double_derivative('sin(x)')
print(f"calculate_double_derivative('sin(x)') = {result}")
assert str(result) == '-sin(x)'
"""
linear algebra tests
"""
def test_solve_linear():
A = np.array([[3, 1], [1, 2]])
b = np.array([9, 8])
result = solve_linear(A, b)
print(f"solve_linear(A, b) = {result}")
assert np.array_equal(result, np.array([2, 3]))
def test_solve_quadratic():
result = solve_quadratic(1, -3, 2)
print(f"solve_quadratic(1, -3, 2) = {result}")
assert np.array_equal(result, (2.0, 1.0))
def test_add_matrices():
m1 = np.array([[1, 2], [3, 4]])
m2 = np.array([[5, 6], [7, 8]])
result = add_matrices(m1, m2)
print(f"add_matrices(m1, m2) = {result}")
assert np.array_equal(result, np.array([[6, 8], [10, 12]]))
def test_subtract_matrices():
m1 = np.array([[1, 2], [3, 4]])
m2 = np.array([[5, 6], [7, 8]])
result = subtract_matrices(m1, m2)
print(f"subtract_matrices(m1, m2) = {result}")
assert np.array_equal(result, np.array([[-4, -4], [-4, -4]]))
def test_scalar_multiply():
matrix = np.array([[1, 2], [3, 4]])
scalar = 2
result = scalar_multiply(scalar, matrix)
print(f"scalar_multiply(scalar, matrix) = {result}")
assert np.array_equal(result, np.array([[2, 4], [6, 8]]))
def test_multiply_matrices():
m1 = np.array([[1, 2], [3, 4]])
m2 = np.array([[5, 6], [7, 8]])
result = multiply_matrices(m1, m2)
print(f"multiply_matrices(m1, m2) = {result}")
assert np.array_equal(result, np.array([[19, 22], [43, 50]]))
def test_determinant():
matrix = np.array([[1, 2], [3, 4]])
result = determinant(matrix)
print(f"determinant(matrix) = {result}")
assert result > -2.0004 or result < -2
def test_is_symmetric():
matrix = np.array([[1, 2], [2, 1]])
result = is_symmetric(matrix)
print(f"is_symmetric(matrix) = {result}")
assert result == True
def test_inverse():
matrix = np.array([[1, 2], [3, 4]])
result = inverse(matrix)
print(f"inverse(matrix) = {result}")
assert np.allclose(result, np.array([[-2.0, 1.0], [1.5, -0.5]]))
def test_eigen():
matrix = np.array([[1, 2], [2, 1]])
result = eigen(matrix)
print(f"eigen(matrix) = {result}")
assert np.allclose(result[0], np.array([3., -1.])) # Checking the eigenvalues
def test_transpose():
matrix = np.array([[1, 2], [3, 4]])
result = transpose(matrix)
print(f"transpose(matrix) = {result}")
assert np.array_equal(result, np.array([[1, 3], [2, 4]]))
def test_trace():
matrix = np.array([[1, 2], [3, 4]])
result = trace(matrix)
print(f"trace(matrix) = {result}")
assert result == 5
def test_rank():
matrix = np.array([[1, 2], [3, 4]])
result = rank(matrix)
print(f"rank(matrix) = {result}")
assert result == 2
"""
Number theory tests
"""
def test_check_prime():
assert check_prime(17)
assert not check_prime(15)
def test_prime_factorization():
assert prime_factorization(18) == [2, 3]
assert prime_factorization(19) == [19]
def test_power():
assert power(2, 3) == 8
assert power(5, 0) == 1
def test_sqrt():
assert sqrt(16) == 4
assert sqrt(0) == 0
assert sqrt(-1) is None
def test_find_gcd():
assert find_gcd(48, 18) == 6
assert find_gcd(7, 1) == 1
def test_find_lcm():
assert find_lcm(15, 20) == 60
assert find_lcm(5, 7) == 35
def test_fibonacci():
assert fibonacci(1) == 1
assert fibonacci(7) == 13
def test_factorial():
assert factorial(5) == 120
assert factorial(0) == 1
assert factorial(-1) is None
"""
Statistics and probability tests
"""
@pytest.mark.parametrize("numbers, expected_mean", [
([1, 2, 3, 4, 5], 3),
([1, 2, 3], 2),
([], None),
])
def test_calculate_mean(numbers, expected_mean):
calculated_mean = calculate_mean(numbers)
print(f"Calculated mean: {calculated_mean}")
assert calculated_mean == expected_mean
@pytest.mark.parametrize("numbers, expected_median", [
([1, 2, 3, 4, 5], 3),
([1, 2, 3], 2),
([], None),
])
def test_calculate_median(numbers, expected_median):
calculated_median = calculate_median(numbers)
print(f"Calculated median: {calculated_median}")
assert calculated_median == expected_median
@pytest.mark.parametrize("numbers, expected_stdev", [
([1, 2, 3, 4, 5], pytest.approx(1.5811, rel=1e-4)),
([1, 2, 3], 1.0),
([], None),
])
def test_calculate_stdev(numbers, expected_stdev):
calculated_stdev = calculate_stdev(numbers)
print(f"Calculated standard deviation: {calculated_stdev}")
assert calculated_stdev == expected_stdev
@pytest.mark.parametrize("numbers, expected_variance", [
([1, 2, 3, 4, 5], pytest.approx(2.5, rel=1e-4)),
([1, 2, 3], 1),
([], None),
])
def test_calculate_variance(numbers, expected_variance):
calculated_variance = calculate_variance(numbers)
print(f"Calculated variance: {calculated_variance}")
assert calculated_variance == expected_variance
@pytest.mark.parametrize("numbers, expected_mode", [
([1, 2, 3, 4, 4], 4),
([1, 2, 3], 1),
([], None),
])
def test_calculate_mode(numbers, expected_mode):
calculated_mode = calculate_mode(numbers)
print(f"Calculated mode: {calculated_mode}")
assert calculated_mode == expected_mode
@pytest.mark.parametrize("event_outcomes, sample_space, expected_probability", [
(2, 5, 0.4),
(0, 5, 0.0),
(5, 0, None),
])
def test_probability(event_outcomes, sample_space, expected_probability):
calculated_probability = probability(event_outcomes, sample_space)
print(f"Calculated probability: {calculated_probability}")
assert calculated_probability == expected_probability
@pytest.mark.parametrize("n, r, expected_combinations", [
(5, 2, 10),
(0, 2, None),
(5, 6, None),
])
def test_combinations(n, r, expected_combinations):
calculated_combinations = combinations(n, r)
print(f"Calculated combinations: {calculated_combinations}")
assert calculated_combinations == expected_combinations
@pytest.mark.parametrize("n, r, expected_permutations", [
(5, 2, 20),
(0, 2, None),
(5, 6, None),
])
def test_permutations(n, r, expected_permutations):
calculated_permutations = permutations(n, r)
print(f"Calculated permutations: {calculated_permutations}")
assert calculated_permutations == expected_permutations
"""
trigo and log tests
"""
def test_sin():
assert math.isclose(trig.sin(math.pi / 2), 1, rel_tol=1e-9)
assert math.isclose(trig.sin(math.pi), 0, rel_tol=1e-9, abs_tol=1e-9)
def test_cos():
assert math.isclose(trig.cos(0), 1, rel_tol=1e-9)
assert math.isclose(trig.cos(math.pi), -1, rel_tol=1e-9)
def test_tan():
assert math.isclose(trig.tan(0), 0, rel_tol=1e-9)
def test_arcsin():
assert math.isclose(trig.arcsin(1), math.pi / 2, rel_tol=1e-9)
def test_arccos():
assert math.isclose(trig.arccos(1), 0, rel_tol=1e-9)
def test_arctan():
assert math.isclose(trig.arctan(1), math.pi / 4, rel_tol=1e-9)
def test_ln():
assert math.isclose(trig.ln(math.e), 1, rel_tol=1e-9)
def test_log2():
assert math.isclose(trig.log2(4), 2, rel_tol=1e-9)
def test_exp():
assert math.isclose(trig.exp(1), math.e, rel_tol=1e-9)