-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_expression.py
More file actions
443 lines (370 loc) · 16.8 KB
/
test_expression.py
File metadata and controls
443 lines (370 loc) · 16.8 KB
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
import unittest
from expression import *
class PaserTests(unittest.TestCase):
def test_init(self):
self.assertIsNotNone(Expression("1+2"))
def setUp(self):
Expression.reset_global_variables()
super().setUp()
def test_tokenize_variables(self):
possible = ['a','b','A','Z','abcd','ABC','a1']
for some_token in possible:
token, work_string = Expression.next_token(work_string=some_token)
self.assertIsNotNone(token, f"Fail: {some_token}")
self.assertTrue(token.type == TokenType.VAR)
self.assertTrue(token.text == some_token)
def test_tokenize_add(self):
possible = ['+']
for some_token in possible:
token, work_string = Expression.next_token(work_string=some_token)
self.assertIsNotNone(token)
self.assertTrue(token.type == TokenType.ADD)
self.assertTrue(token.text == some_token)
def test_tokenize_sub(self):
possible = ['-']
for some_token in possible:
token, work_string = Expression.next_token(work_string=some_token)
self.assertIsNotNone(token)
self.assertTrue(token.type == TokenType.SUB)
self.assertTrue(token.text == some_token)
def test_tokenize_mul(self):
possible = ['*']
for some_token in possible:
token, work_string = Expression.next_token(work_string=some_token)
self.assertIsNotNone(token)
self.assertTrue(token.type == TokenType.MUL)
self.assertTrue(token.text == some_token)
def test_tokenize_div(self):
possible = ['/','÷']
for some_token in possible:
token, work_string = Expression.next_token(work_string=some_token)
self.assertIsNotNone(token)
self.assertTrue(token.type == TokenType.DIV)
self.assertTrue(token.text == some_token)
def test_tokenize_plusminus(self):
possible = ['±']
for some_token in possible:
token, work_string = Expression.next_token(work_string=some_token)
self.assertIsNotNone(token)
self.assertTrue(token.type == TokenType.PLUSMINUS)
self.assertTrue(token.text == some_token)
def test_tokenize_parenthesis(self):
possible = ['(']
for some_token in possible:
token, work_string = Expression.next_token(work_string=some_token)
self.assertIsNotNone(token)
self.assertTrue(token.type == TokenType.LEFT_PAR)
self.assertTrue(token.text == some_token)
possible = [')']
for some_token in possible:
token, work_string = Expression.next_token(work_string=some_token)
self.assertIsNotNone(token)
self.assertTrue(token.type == TokenType.RIGHT_PAR)
self.assertTrue(token.text == some_token)
def test_tokenize_number(self):
possible = ['1.0','12','123','123.123','123e-3']
for some_token in possible:
token, work_string = Expression.next_token(work_string=some_token)
self.assertIsNotNone(token)
self.assertTrue(token.type == TokenType.NUMBER)
self.assertTrue(token.text == some_token)
def test_tokenize_assign(self):
possible = ['=']
for some_token in possible:
token, work_string = Expression.next_token(work_string=some_token)
self.assertIsNotNone(token)
self.assertTrue(token.type == TokenType.ASSIGN)
self.assertTrue(token.text == some_token)
def test_tokenize(self):
possible = ['a+b','a-b','a*b','a/b','a+12','a+b-c*d/f']
for expression in possible:
Expression(expression)
def test_rpn_stack(self):
# possible = ['1+2-3','1+2*3','1*2+3']
possible = ['1+2*3']
for expression in possible:
p = Expression(expression)
def test_evaluate(self):
possible = [('1+2-3',0),('1+2*3',7),('1*2+3',5),('1/3',1/3)]
for expression, result in possible:
p = Expression(expression)
self.assertEqual(p.evaluate({}), result)
def test_evaluate_plusminus(self):
m = Expression("5±1")
mean, std = m.evaluate_uncertainty()
self.assertAlmostEqual(mean, 5, 1)
self.assertAlmostEqual(std, 1, 1)
def test_evaluate_variables_values(self):
m = Expression("5±a")
mean, std = m.evaluate_uncertainty(variables={"a":2})
self.assertAlmostEqual(mean, 5, 1)
self.assertAlmostEqual(std, 2, 1)
m = Expression("5±a%")
mean, std = m.evaluate_uncertainty(variables={"a":2})
self.assertAlmostEqual(mean, 5, 1)
self.assertAlmostEqual(std, 0.02*5, 1)
def test_evaluate_assignment(self):
m = Expression("a=5")
mean, std = m.evaluate_uncertainty()
self.assertAlmostEqual(mean, 5, 1)
self.assertAlmostEqual(std, 0, 1)
self.assertEqual(m.evaluate(), 5)
m = Expression("b=5+a")
mean, std = m.evaluate_uncertainty()
self.assertAlmostEqual(mean, 10, 1)
self.assertAlmostEqual(std, 0, 1)
m = Expression("b±(a/5)")
mean, std = m.evaluate_uncertainty()
self.assertAlmostEqual(mean, 10, 1)
self.assertAlmostEqual(std, 1, 1)
m = Expression("(b±a)/5)")
mean, std = m.evaluate_uncertainty()
self.assertAlmostEqual(mean, 2, 1)
self.assertAlmostEqual(std, 1, 1)
def test_check_assignment(self):
m = Expression("a=5")
m2 = Expression("b=a")
self.assertEqual(m.evaluate(), m2.evaluate())
def test_split(self):
self.assertTrue("a=1".split('=') == ['a','1'])
def test_check_assignment_with_uncertainty(self):
m = Expression("a=5±1")
m2 = Expression("a+a")
# self.assertEqual(m.evaluate(), m2.evaluate())
def test_regex_neg(self):
m = re.match(r'(±)(?!%)',"±%1")
self.assertIsNone(m)
m = re.match(r'(±)(?!%)',"±%")
self.assertIsNone(m)
m = re.match(r'(±%)',"±%")
self.assertIsNotNone(m)
m = re.match(r'(±)(?!%)',"±1")
self.assertIsNotNone(m)
m = re.match(r'(±)(?!%)',"±1")
self.assertIsNotNone(m)
def test_uncertainty_in_percent(self):
m = Expression("1±1%")
mean, std = m.evaluate_uncertainty()
self.assertAlmostEqual(mean, 1, 1)
self.assertAlmostEqual(std, 0.01, 1)
m = Expression("1±%1")
mean, std = m.evaluate_uncertainty()
self.assertAlmostEqual(mean, 1, 1)
self.assertAlmostEqual(std, 0.01, 1)
m = Expression("1±(0.5+0.5)%")
mean, std = m.evaluate_uncertainty()
self.assertAlmostEqual(mean, 1, 1)
self.assertAlmostEqual(std, 0.01, 1)
def test_uncertainty_in_absolute(self):
m = Expression("1±0.5")
mean, std = m.evaluate_uncertainty()
self.assertAlmostEqual(mean, 1, 1)
self.assertAlmostEqual(std, 0.5, 1)
m = Expression("1±(0.25+0.25)")
mean, std = m.evaluate_uncertainty()
self.assertAlmostEqual(mean, 1, 1)
self.assertAlmostEqual(std, 0.5, 1)
def test_uncertainty_with_calculation(self):
m = Expression("(1±0.5)+1")
mean, std = m.evaluate_uncertainty()
self.assertAlmostEqual(mean, 2, 1)
self.assertAlmostEqual(std, 0.5, 1)
m = Expression("(1±(0.25+0.25))+1")
mean, std = m.evaluate_uncertainty()
self.assertAlmostEqual(mean, 2, 1)
self.assertAlmostEqual(std, 0.5, 1)
def test_comma(self):
m = Expression("1,1")
self.assertEqual(m.evaluate(), 1)
def test_function(self):
m = Expression("sin(1)")
self.assertAlmostEqual(m.evaluate(), np.sin(1))
m = Expression("cos(1)")
self.assertAlmostEqual(m.evaluate(), np.cos(1))
m = Expression("sqrt(2)")
self.assertAlmostEqual(m.evaluate(), np.sqrt(2))
def test_function_with_uncertainty(self):
m = Expression("sin(1±0.1)")
def test_constants(self):
self.assertAlmostEqual(Expression("pi").evaluate(), np.pi)
self.assertAlmostEqual(Expression("π").evaluate(), np.pi)
self.assertAlmostEqual(Expression("e").evaluate(), np.exp(1))
def test_function_decfinition(self):
fct = Expression("f(x) := x*2")
m = Expression("f(2)")
self.assertEqual(m.evaluate(), 4)
def test_error_no_value(self):
with self.assertRaises(Expression.VariableUndefined):
m = Expression("a").evaluate()
def test_error_invalid_lvalue(self):
with self.assertRaises(Expression.InvalidLeftValue):
m = Expression("1=2").evaluate()
def test_error_unrecognized_token(self):
invalid_strings = [':','.']
for work_string in invalid_strings:
with self.assertRaises(Expression.UnrecognizedToken):
print(Expression.next_token(work_string))
def test_history(self):
Expression("a=10")
Expression("b=20")
Expression("f(x):=x*x")
self.assertEqual(Expression("a+b").evaluate(), 30)
self.assertEqual(Expression("f(a+b)").evaluate(), 30*30)
def test_priority(self):
"""
I am writing a mathematical parser that evaluates mathematical
expressions, including assignment of variables and functions. I need
to unittest many different expressions to confirm its validity. Give
me a list of strings of mathematical expression that cover all cases
for priority of operations (+ - * /)
"""
operations = [
"3 + 4 * 2", # multiplication before addition
"(3 + 4) * 2", # parentheses change priority
"6 / 3 - 2", # division before subtraction
"6 / (3 - 2)", # parentheses change priority
"2 + 3 * 4 / 2", # mixed multiplication and division
"2 * 3 + 4 / 2", # multiplication and division before addition
"2 * (3 + 4) / 2", # parentheses with multiplication and division
"2 * ((3 + 4) / 2)", # nested parentheses
"3 + 4 * 2 - 1", # multiple operations, multiplication first
"3 - 4 * 2 + 1", # subtraction and addition, multiplication first
"(3 - 4) * 2 + 1", # parentheses change order
"5 + 6 * (3 / 2)", # parentheses, division inside
"5 * 6 / 2 + 3", # multiplication and division before addition
"4 + 3 - 2 * 2 / 1", # subtraction and addition after multiplication/division
"2 / 2 + 3 * 3 - 1", # division and multiplication before addition/subtraction
"(2 + 3) * (5 - 2)", # multiple parentheses
"10 - 4 / 2 * 3", # division and multiplication before subtraction
"(10 - 4) / (2 * 3)", # parentheses with division and multiplication
"8 / 2 * (2 + 2)", # division and multiplication with parentheses
"7 + 3 * 4 - (10 / 5)", # parentheses affect division first
"(7 + 3) * (5 / 2)", # combination of operations with parentheses
"2 + 3 * (5 - 2) / 2", # expression with mixed operations inside parentheses
"100 / (5 * 2) + 6", # division before addition with parentheses
"6 + (4 * 3) / 2 - 7" # addition and subtraction with parentheses
]
for expression in operations:
m = Expression(expression)
self.assertEqual(m.evaluate(), eval(expression), f"{expression} {m.rpn_list}")
def test_distribution(self):
m=Expression("10±1")
print(m.evaluate())
def test_define_vs_assign(self):
# := stores Expression (independent on each reference)
Expression("a := 10±2")
self.assertIsInstance(Expression.global_variables['a'], Expression)
m = Expression("a + a")
mean, std = m.evaluate_uncertainty(N=500_000)
self.assertAlmostEqual(mean, 20, 0)
self.assertAlmostEqual(std, 2*np.sqrt(2), 0) # independent: sqrt(4+4)
def test_assign_stores_result(self):
# = evaluates immediately, stores ndarray (correlated on reuse)
Expression("a = 10±2")
self.assertIsInstance(Expression.global_variables['a'], np.ndarray)
m = Expression("a + a")
mean, std = m.evaluate_uncertainty(N=500_000)
self.assertAlmostEqual(mean, 20, 0)
self.assertAlmostEqual(std, 4, 0) # correlated: 2*2 = 4
def test_assign_two_variables_independent(self):
Expression("a = 10±2")
Expression("b = 10±2")
m = Expression("a + b")
mean, std = m.evaluate_uncertainty(N=500_000)
self.assertAlmostEqual(mean, 20, 0)
self.assertAlmostEqual(std, 2*np.sqrt(2), 0)
def test_has_distributions_true(self):
m = Expression("10±1")
self.assertTrue(m.has_distributions)
def test_has_distributions_false(self):
m = Expression("10+1")
self.assertFalse(m.has_distributions)
def test_has_distributions_with_variable(self):
Expression("a := 10±1")
m = Expression("a + 1")
self.assertTrue(m.has_distributions)
def test_evaluate_scalar_with_assign(self):
# = stores ndarray, scalar mode should return mean
Expression("a = 10±2")
m = Expression("a + 5")
scalar = m.evaluate_scalar()
self.assertAlmostEqual(scalar, 15, 0)
self.assertIsInstance(scalar, (int, float, np.floating))
def test_evaluate_scalar_with_define(self):
Expression("a := 10±2")
m = Expression("a * 2")
scalar = m.evaluate_scalar()
self.assertAlmostEqual(scalar, 20, 0)
def test_define_function(self):
Expression("f(x) := x*x")
self.assertIsInstance(Expression.functions['f'], Expression)
m = Expression("f(3)")
self.assertEqual(m.evaluate(), 9)
def test_define_is_definition(self):
m = Expression("a := 5")
self.assertTrue(m.is_definition)
self.assertTrue(m.is_assignment)
def test_assign_is_not_definition(self):
m = Expression("a = 5")
self.assertFalse(m.is_definition)
self.assertTrue(m.is_assignment)
def test_no_assignment(self):
m = Expression("1+2")
self.assertFalse(m.is_assignment)
self.assertFalse(m.is_definition)
self.assertIsNone(m.lvalue)
def test_define_function_with_uncertainty(self):
Expression("f(x) := x*2")
m = Expression("f(5±1)")
mean, std = m.evaluate_uncertainty(N=500_000)
self.assertAlmostEqual(mean, 10, 0)
self.assertAlmostEqual(std, 2, 0)
def test_random_function(self):
m = Expression("random(10, 2)")
mean, std = m.evaluate_uncertainty(N=500_000)
self.assertAlmostEqual(mean, 10, 0)
self.assertAlmostEqual(std, 2, 0)
def test_random_scalar(self):
m = Expression("random(10, 2)")
scalar = m.evaluate_scalar()
self.assertAlmostEqual(scalar, 10, 5)
def test_random_independence(self):
m = Expression("random(10, 2) + random(10, 2)")
mean, std = m.evaluate_uncertainty(N=500_000)
self.assertAlmostEqual(mean, 20, 0)
self.assertAlmostEqual(std, 2*np.sqrt(2), 0)
def test_numbers_token(self):
token, rest = Expression.next_token("[10, 11, 12.3]")
self.assertEqual(token.type, TokenType.NUMBERS)
self.assertEqual(len(token.value), 3)
self.assertAlmostEqual(token.value[0], 10)
self.assertAlmostEqual(token.value[2], 12.3)
def test_numbers_evaluate(self):
m = Expression("[10, 11, 12]")
result = m.evaluate()
self.assertIsInstance(result, np.ndarray)
self.assertEqual(len(result), 3)
def test_numbers_scalar(self):
m = Expression("[10, 20, 30]")
scalar = m.evaluate_scalar()
self.assertAlmostEqual(scalar, 20, 5)
def test_numbers_assign(self):
Expression("a = [8, 10, 12]")
m = Expression("a")
mean, std = m.evaluate_uncertainty()
self.assertAlmostEqual(mean, 10, 0)
self.assertAlmostEqual(std, np.std([8, 10, 12], ddof=1), 1)
def test_numbers_operations(self):
Expression("a = [10, 20, 30]")
m = Expression("a * 2")
scalar = m.evaluate_scalar()
self.assertAlmostEqual(scalar, 40, 0)
def test_comma_in_function(self):
"""Test that comma works correctly in multi-arg context."""
m = Expression("random(5, 1)")
mean, std = m.evaluate_uncertainty(N=500_000)
self.assertAlmostEqual(mean, 5, 0)
self.assertAlmostEqual(std, 1, 0)
if __name__ == "__main__":
# unittest.main(defaultTest=["PaserTests.test_evaluate_variables_values"])
unittest.main()