-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpression_utilities.py
More file actions
811 lines (695 loc) · 30.9 KB
/
expression_utilities.py
File metadata and controls
811 lines (695 loc) · 30.9 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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
# Default parameters for expression handling
# Any contexts that use this collection of utility functions
# must define values for theses parameters
DEFAULT_SIGNIFICANT_FIGURES = 2
default_parameters = {
"complexNumbers": False,
"convention": "equal_precedence",
"elementary_functions": False,
"strict_syntax": True,
"multiple_answers_criteria": "all",
}
# -------- String Manipulation imports
from .slr_parsing_utilities import (
SLR_expression_parser,
infix,
group,
compose
)
from .syntactical_comparison_utilities import is_number_regex
# (Sympy) Expression Parsing imports
from sympy.parsing.sympy_parser import parse_expr, split_symbols_custom, _token_splittable
from sympy.parsing.sympy_parser import T as parser_transformations
from sympy.printing.latex import LatexPrinter
from sympy import Basic, Symbol, Equality, Function
import re
from typing import Dict, List, TypedDict
from ..feedback.symbolic import feedback_generators as feedback_string_generators
class ModifiedLatexPrinter(LatexPrinter):
"""Modified LatexPrinter class that prints logarithms other than the natural logarithm correctly.
"""
def _print_log(self, expr, exp=None):
if self._settings["ln_notation"] and len(expr.args) < 2:
log_not = r"\ln"
else:
log_not = r"\log"
if len(expr.args) > 1:
base = self._print(expr.args[1])
log_not = r"\log_{%s}" % base
tex = r"%s{\left(%s \right)}" % (log_not, self._print(expr.args[0]))
if exp is not None:
return r"%s^{%s}" % (tex, exp)
else:
return tex
elementary_functions_names = [
('sin', []), ('sinc', []), ('csc', ['cosec']), ('cos', []), ('sec', []), ('tan', []), ('cot', ['cotan']),
('asin', ['arcsin']), ('acsc', ['arccsc', 'arccosec', 'acosec']), ('acos', ['arccos']), ('asec', ['arcsec']),
('atan', ['arctan']), ('acot', ['arccot', 'arccotan', 'acotan']), ('atan2', ['arctan2']),
('sinh', []), ('cosh', []), ('tanh', []), ('csch', ['cosech']), ('sech', []),
('asinh', ['arcsinh']), ('acosh', ['arccosh']), ('atanh', ['arctanh']),
('acsch', ['arccsch', 'arccosech']), ('asech', ['arcsech']),
('exp', ['Exp']), ('E', ['e']), ('log', ['ln']),
('sqrt', []), ('sign', []), ('Abs', ['abs']), ('Max', ['max']), ('Min', ['min']), ('arg', []), ('ceiling', ['ceil']), ('floor', []),
('oo',['Infinity', 'inf', 'infinity', '∞']),
# Special symbols to make sure plus_minus and minus_plus are not destroyed during preprocessing
('plus_minus', []), ('minus_plus', []),
# Below this line should probably not be collected with elementary functions. Some like 'common operations' would be a better name
('summation', ['sum', 'Sum']), ('Integral', ['int']), ('Derivative', ['diff']), ('re', ['real']), ('im', ['imag']), ('conjugate', ['conj'])
]
for data in elementary_functions_names:
upper_case_alternatives = [data[0].upper()]
for alternative in data[1]:
if alternative.upper() not in upper_case_alternatives:
upper_case_alternatives.append(alternative.upper())
data[1].extend(upper_case_alternatives)
special_symbols_names = [
("Alpha", ["Α", "𝚨", "𝛢", "𝜜", "𝝖", "𝞐"]),
("alpha", ["α", "𝛂", "𝛼", "𝜶", "𝝰", "𝞪"]),
("Beta", ["Β", "𝚩", "𝛣", "𝜝", "𝝗", "𝞑"]),
("beta", ["β", "ϐ", "𝛃", "𝛽", "𝜷", "𝝱", "𝞫"]),
("Gamma", ["Γ", "𝚪", "𝛤", "𝜞", "𝝘", "𝞒"]),
("gamma", ["γ", "𝛄", "𝛾", "𝜸", "𝝲", "𝞬"]),
("Delta", ["Δ", "𝚫", "𝛥", "𝜟", "𝝙", "𝞓"]),
("delta", ["δ", "𝛅", "𝛿", "𝜹", "𝝳", "𝞭"]),
("Epsilon", ["Ε", "𝚬", "𝛦", "𝜠", "𝝚", "𝞔"]),
("epsilon", ["ε", "ϵ", "𝛆", "𝜀", "𝜺", "𝝴", "𝞊", "𝞮"]),
("Zeta", ["Ζ", "𝚭", "𝛧", "𝜡", "𝝛", "𝞕"]),
("zeta", ["ζ", "𝛇", "𝜁", "𝜻", "𝝵", "𝞯"]),
("Eta", ["Η", "𝚮", "𝛨", "𝜢", "𝝜", "𝞖"]),
("eta", ["η", "𝛈", "𝜂", "𝜼", "𝝶", "𝞰"]),
("Theta", ["Θ", "ϴ", "𝚯", "𝛩", "𝜣", "𝝝", "𝞗"]),
("theta", ["θ", "ϑ", "𝛉", "𝜃", "𝜗", "𝜽", "𝝑", "𝝷", "𝞋", "𝞱"]),
("Iota", ["Ι", "𝚰", "𝛪", "𝜤", "𝝞", "𝞘"]),
("iota", ["ι", "𝛊", "𝜄", "𝜾", "𝝸", "𝞲"]),
("Kappa", ["Κ", "𝚱", "𝛫", "𝜥", "𝝟", "𝞙"]),
("kappa", ["κ", "ϰ", "𝛋", "𝜅", "𝜘", "𝜿", "𝝒", "𝝹", "𝞌", "𝞳"]),
("Lambda", ["Λ", "𝚲", "𝛬", "𝜦", "𝝠", "𝞚"]),
("lamda", ["λ", "𝛌", "𝜆", "𝝀", "𝝺", "𝞴"]), # lambda mispelt here to minimise conflict with Python in-built
("Mu", ["Μ", "𝚳", "𝛭", "𝜧", "𝝡", "𝞛"]),
("mu", ["μ", "µ", "𝛍", "𝜇", "𝝁", "𝝻", "𝞵"]),
("Nu", ["Ν", "𝚴", "𝛮", "𝜨", "𝝢", "𝞜"]),
("nu", ["ν", "𝛎", "𝜈", "𝝂", "𝝼", "𝞶"]),
("Xi", ["Ξ", "𝚵", "𝛯", "𝜩", "𝝣", "𝞝"]),
("xi", ["ξ", "𝛏", "𝜉", "𝝃", "𝝽", "𝞷"]),
("Omicron", ["Ο", "𝚶", "𝛰", "𝜪", "𝝤", "𝞞"]),
("omicron", ["ο", "𝛐", "𝜊", "𝝄", "𝝾", "𝞸"]),
("Pi", ["Π", "𝚷", "𝛱", "𝜫", "𝝥", "𝞟"]),
("pi", ["π", "ϖ", "𝛑", "𝜋", "𝝅", "𝝿", "𝞹"]),
("Rho", ["Ρ", "𝚸", "𝛲", "𝜬", "𝝦", "𝞠"]),
("rho", ["ρ", "ϱ", "𝛒", "𝜌", "𝝆", "𝞀", "𝞺"]),
("Sigma", ["Σ", "𝚺", "𝛴", "𝜮", "𝝨", "𝞢"]),
("sigma", ["σ", "ς", "𝛔", "𝜎", "𝝈", "𝞂", "𝞼"]),
("Tau", ["Τ", "𝚻", "𝛵", "𝜯", "𝝩", "𝞣"]),
("tau", ["τ", "𝛕", "𝜏", "𝝉", "𝞃", "𝞽"]),
("Upsilon", ["Υ", "𝚼", "𝛶", "𝜰", "𝝪", "𝞤"]),
("upsilon", ["υ", "𝛖", "𝜐", "𝝊", "𝞄", "𝞾"]),
("Phi", ["Φ", "𝚽", "𝛷", "𝜱", "𝝫", "𝞥"]),
("phi", ["φ", "ϕ", "𝛗", "𝜑", "𝜙", "𝝋", "𝝓", "𝞅", "𝞍", "𝞿", "𝟇"]),
("Chi", ["Χ", "𝚾", "𝛸", "𝜲", "𝝬", "𝞦"]),
("chi", ["χ", "𝛘", "𝜒", "𝝌", "𝞆", "𝟀"]),
("Psi", ["Ψ", "𝚿", "𝛹", "𝜳", "𝝭", "𝞧"]),
("psi", ["ψ", "𝛙", "𝜓", "𝝍", "𝞇", "𝟁"]),
("Omega", ["Ω", "𝛀", "𝛺", "𝜴", "𝝮", "𝞨"]),
("omega", ["ω", "𝛚", "𝜔", "𝝎", "𝞈", "𝟂"])
]
# -------- String Manipulation Utilities
def create_expression_set(exprs, params):
if isinstance(exprs, str):
if exprs.startswith('{') and exprs.endswith('}'):
exprs = [expr.strip() for expr in exprs[1:-1].split(',')]
else:
exprs = [exprs]
expr_set = set()
for expr in exprs:
expr = substitute_input_symbols(expr, params)[0]
if "plus_minus" in params.keys():
expr = expr.replace(params["plus_minus"], "plus_minus")
if "minus_plus" in params.keys():
expr = expr.replace(params["minus_plus"], "minus_plus")
if ("plus_minus" in expr) or ("minus_plus" in expr):
for pm_mp_ops in [("+", "-"), ("-", "+")]:
expr_string = expr.replace("plus_minus", pm_mp_ops[0]).replace("minus_plus", pm_mp_ops[1]).strip()
while expr_string[0] == "+":
expr_string = expr_string[1:]
expr_set.add(expr_string.strip())
else:
expr_set.add(expr)
return list(expr_set)
def convert_absolute_notation(expr, name):
"""
Accept || as another form of writing modulus of an expression.
Function makes the input parseable by SymPy, SymPy only accepts Abs()
REMARK: this function cannot handle nested ||. It will attempt to pair
each | with the closest | to the right and return a string with a warning
if it detects an ambiguity.
Parameters
----------
expr : string
Expression to convert, might have ||
Returns
-------
expr : string
converted response input
"""
# positions of the || values
n_expr = expr.count('|')
if n_expr == 2:
expr = list(expr)
expr[expr.index("|")] = "Abs("
expr[expr.index("|")] = ")"
expr = "".join(expr)
elif n_expr > 0:
expr_start_abs_pos = []
expr_end_abs_pos = []
expr_ambiguous_abs_pos = []
if expr[0] == "|":
expr_start_abs_pos.append(0)
for i in range(1, len(expr)-1):
if expr[i] == "|":
if (expr[i-1].isalnum() or expr[i-1] in "()[]{}") and not (expr[i+1].isalnum() or expr[i+1] in "()[]{}"):
expr_end_abs_pos.append(i)
elif (expr[i+1].isalnum() or expr[i+1] in "()[]{}") and not (expr[i-1].isalnum() or expr[i-1] in "()[]{}"):
expr_start_abs_pos.append(i)
else:
expr_ambiguous_abs_pos.append(i)
if expr[-1] == "|":
expr_end_abs_pos.append(len(expr)-1)
expr = list(expr)
for i in expr_start_abs_pos:
expr[i] = "Abs("
for i in expr_end_abs_pos:
expr[i] = ")"
k = 0
prev_ambiguous = -1
for i in expr_ambiguous_abs_pos:
prev_start = -1
for j in expr_start_abs_pos:
if j < i:
prev_start = j
else:
break
prev_end = -1
for j in expr_end_abs_pos:
if j < i:
prev_end = j
else:
break
if max(prev_start, prev_end, prev_ambiguous) == prev_end:
if expr[i-1].isalnum():
expr[i] = "*Abs("
else:
expr[i] = "Abs("
elif max(prev_start, prev_end, prev_ambiguous) == prev_ambiguous:
if k % 2 == 0:
if expr[i-1].isalnum():
expr[i] = "*Abs("
else:
expr[i] = "Abs("
else:
expr[i] = ")"
k += 1
else:
expr[i] = ")"
prev_ambiguous = i
expr = "".join(expr)
ambiguity_tag = "ABSOLUTE_VALUE_NOTATION_AMBIGUITY"
remark = ""
if n_expr > 2 and len(expr_ambiguous_abs_pos) > 0:
remark = feedback_string_generators["INTERNAL"](ambiguity_tag)({'name': name})
feedback = None
if len(remark) > 0:
feedback = (ambiguity_tag, remark)
return expr, feedback
def SLR_implicit_multiplication_convention_parser(convention):
delimiters = [
(("(", ")"), group(1))
]
costum_tokens = [
(" *(\*|\+|-| ) *", "SPLIT"), (" */ *", "SOLIDUS")
]
infix_operators = []
costum_productions = [("E", "*E", group(2, empty=True)), ("E", "EE", group(2, empty=True))]
if convention == "equal_precedence":
costum_productions += [("E", "E/E", infix)]
elif convention == "implicit_higher_precedence":
costum_productions += [("E", "E/E", compose(infix, group(1, empty=True, delimiters=["(", ")"])))]
else:
raise Exception(f"Unknown convention {convention}")
undefined = ("O", "OTHER")
expression_node = ("E", "EXPRESSION_NODE")
return SLR_expression_parser(delimiters=delimiters, infix_operators=infix_operators, undefined=undefined, expression_node=expression_node, costum_tokens=costum_tokens, costum_productions=costum_productions)
def preprocess_according_to_chosen_convention(expression, parameters):
convention = parameters.get("convention", None)
if convention is not None:
parser = SLR_implicit_multiplication_convention_parser(convention)
expression = parser.parse(parser.scan(expression))[0].content_string()
return expression
def transform_unicode_greek_symbols(expr):
alias_substitutions = []
for (name, alias_list) in special_symbols_names:
if name in expr:
alias_substitutions += [(name, " "+name+" ")]
for alias in alias_list:
if alias in expr:
alias_substitutions += [(alias, " "+name+" ")]
return alias_substitutions
def convert_unicode_dashes(expr):
unicode_dashes = [
"‐", # HYPHEN
"‑", # NON-BREAKING HYPHEN
"‒", # FIGURE DASH
"–", # EN DASH
"—", # EM DASH
"−", # MINUS SIGN
"﹣", # SMALL HYPHEN-MINUS
"-", # FULLWIDTH HYPHEN-MINUS
]
return [(dash, "-") for dash in unicode_dashes if dash in expr]
def protect_elementary_functions_substitutions(expr):
alias_substitutions = []
for (name, alias_list) in elementary_functions_names:
if name in expr:
alias_substitutions += [(name, " "+name+" ")]
for alias in alias_list:
if alias in expr:
alias_substitutions += [(alias, " "+name+" ")]
return alias_substitutions
def substitute_input_symbols(exprs, params):
'''
Input:
exprs : a string or a list of strings
params : Evaluation function parameter dictionary
Output:
List of strings where alternatives for input symbols have been replaced with
their corresponsing input symbol code.
Remark:
Alternatives are sorted before substitution so that longer alternatives takes precedence.
'''
if isinstance(exprs, str):
exprs = [exprs]
substitutions = [(expr, expr) for expr in params.get("reserved_keywords", [])]
substitutions += [(expr, expr) for expr in params.get("unsplittable_symbols", [])]
if "plus_minus" in params.keys():
substitutions += [(params["plus_minus"], "plus_minus")]
if "minus_plus" in params.keys():
substitutions += [(params["minus_plus"], "minus_plus")]
input_symbols = params.get("symbols",dict())
input_symbols_alternatives = []
for (code, definition) in input_symbols.items():
input_symbols_alternatives += definition["aliases"]
if params.get("elementary_functions", False) is True:
alias_substitutions = []
for expr in exprs:
for (name, alias_list) in elementary_functions_names+special_symbols_names:
if name in input_symbols_alternatives:
continue
else:
if (name in expr) and not (name in input_symbols_alternatives):
alias_substitutions += [(name, " "+name)]
for alias in alias_list:
if (alias in expr) and not (alias in input_symbols_alternatives):
alias_substitutions += [(alias, " "+name)]
substitutions += alias_substitutions
if "symbols" in params.keys():
# Removing invalid input symbols
input_symbols_to_remove = []
aliases_to_remove = []
for (code, symbol_data) in input_symbols.items():
if len(code) == 0:
input_symbols_to_remove += [code]
else:
if len(code.strip()) == 0:
input_symbols_to_remove += [code]
else:
aliases = symbol_data["aliases"]
for i in range(0, len(aliases)):
if len(aliases[i]) > 0:
aliases[i].strip()
if len(aliases[i]) == 0:
aliases_to_remove += [(code, i)]
for (code, i) in aliases_to_remove:
del input_symbols[code]["aliases"][i]
for code in input_symbols_to_remove:
del input_symbols[code]
# Since 'lambda' is a reserved keyword in python
# it needs to be replaced with 'lamda' for expression
# parsing to work properly
lambda_value = input_symbols.pop("lambda", {"latex": r"\lambda", "aliases": ["lambda"]})
if lambda_value is not None:
lambda_value["aliases"].append("lambda")
input_symbols.update({"lamda": lambda_value})
params.update({"symbols": input_symbols})
for (code, symbol_data) in input_symbols.items():
substitutions.append((code, code))
for alias in symbol_data["aliases"]:
if len(alias) > 0:
substitutions.append((alias, code))
# REMARK: This is to ensure capability with response areas that use the old formatting
# for input_symbols. Should be removed when all response areas are updated.
if "input_symbols" in params.keys():
input_symbols = params["input_symbols"]
input_symbols_to_remove = []
alternatives_to_remove = []
for k in range(0, len(input_symbols)):
if len(input_symbols[k]) > 0:
input_symbols[k][0].strip()
if len(input_symbols[k][0]) == 0:
input_symbols_to_remove += [k]
else:
for i in range(0, len(input_symbols[k][1])):
if len(input_symbols[k][1][i]) > 0:
input_symbols[k][1][i].strip()
if len(input_symbols[k][1][i]) == 0:
alternatives_to_remove += [(k, i)]
for (k, i) in alternatives_to_remove:
del input_symbols[k][1][i]
for k in input_symbols_to_remove:
del input_symbols[k]
for input_symbol in params["input_symbols"]:
substitutions.append((input_symbol[0], input_symbol[0]))
for alternative in input_symbol[1]:
if len(alternative) > 0:
substitutions.append((alternative, input_symbol[0]))
# Since 'lambda' is a reserved keyword in python
# we need to make sure it is not substituted back in
substitutions = [(original, subs.replace("lambda", "lamda")) for (original, subs) in substitutions]
# Since 'as' is a reserved keyword in python, we add a subsitution of 'as' to 'a*s' if 'as' is not a defined symbol
if 'as' not in input_symbols:
substitutions += [('as', 'a*s')]
substitutions = list(set(substitutions))
if len(substitutions) > 0:
substitutions.sort(key=substitutions_sort_key)
for k in range(0, len(exprs)):
exprs[k] = substitute(exprs[k], substitutions)
exprs[k] = " ".join(exprs[k].split())
return exprs
def find_matching_parenthesis(string, index, delimiters=None):
depth = 0
if delimiters is None:
delimiters = ('(', ')')
for k in range(index, len(string)):
if string[k] == delimiters[0]:
depth += 1
continue
if string[k] == delimiters[1]:
depth += -1
if depth == 0:
return k
return -1
def substitute(string, substitutions):
'''
Input:
string (required) : a string or a list of strings
substitutions (required) : a list with elements of the form (string,string)
or ((string,list of strings),string)
Output:
A string that is the input string where any occurence of the left element
of each pair in substitutions have been replaced with the corresponding right element.
If the first element in the substitution is of the form (string,list of strings) then
the substitution will only happen if the first element followed by one of the strings
in the list in the second element.
Remarks:
Substitutions are made in the input order but if a substitutions left element is a
substring of a preceding substitutions right element there will be no substitution.
In most cases it is good practice to sort the substitutions by the length of the left
element in descending order.
Examples:
substitute("abc bc c", [("abc","p"), ("bc","q"), ("c","r")])
returns: "p q r"
substitute("abc bc c", [("c","r"), ("bc","q"), ("abc","p")])
returns: "abr br r"
substitute("p bc c", [("p","abc"), ("bc","q"), ("c","r")])
returns: "abc q c"
substitute("p bc c", [("c","r"), ("bc","q"), ("p","abc")])
returns: "abc br r"
'''
if isinstance(string, str):
string = [string]
# Perform substitutions
new_string = []
for part in string:
if not isinstance(part, str):
new_string.append(part)
else:
index = 0
string_buffer = ""
while index < len(part):
matched_start = False
for k, pair in enumerate(substitutions):
if isinstance(pair[0], tuple):
match = False
for look_ahead in pair[0][1]:
if part.startswith(pair[0][0]+look_ahead, index):
match = True
break
substitution_length = len(pair[0][0])
else:
match = part.startswith(pair[0], index)
substitution_length = len(pair[0])
if match:
matched_start = True
if len(string_buffer) > 0:
new_string.append(string_buffer)
string_buffer = ""
new_string.append(k)
index += substitution_length
break
if not matched_start:
string_buffer += part[index]
index += 1
if len(string_buffer) > 0:
new_string.append(string_buffer)
for k, elem in enumerate(new_string):
if isinstance(elem, int):
new_string[k] = substitutions[elem][1]
return "".join(new_string)
def compute_relative_tolerance_from_significant_decimals(string):
rtol = None
string = string.strip()
if re.fullmatch(is_number_regex, string) is None:
rtol = 0
else:
if "e" in string.casefold():
string = "".join(string.split())
separators = "e*^ "
separator_indices = []
for separator in separators:
if separator in string:
separator_indices.append(string.index(separator))
else:
separator_indices.append(len(string))
index = min(separator_indices)
significant_characters = string[0:index].replace(".", "")
significant_characters = significant_characters.lstrip("-0")
rtol = 5*10**(-max(len(significant_characters), DEFAULT_SIGNIFICANT_FIGURES))
return rtol
# -------- (Sympy) Expression Parsing Utilities
class SymbolData(TypedDict):
latex: str
aliases: List[str]
SymbolDict = Dict[str, SymbolData]
symbol_latex_re = re.compile(
r"(?P<start>\\\(|\$\$|\$)(?P<latex>.*?)(?P<end>\\\)|\$\$|\$)"
)
def sympy_symbols(symbols):
"""Create a mapping of local variables for parsing sympy expressions.
Args:
symbols (SymbolDict): A dictionary of sympy symbol strings to LaTeX
symbol strings.
Note:
Only the sympy string is used in this function.
Returns:
Dict[str, Symbol]: A dictionary of sympy symbol strings to sympy
Symbol objects.
"""
return {k: Symbol(k) for k in symbols}
def extract_latex(symbol):
"""Returns the latex portion of a symbol string.
Note:
Only the first matched expression is returned.
Args:
symbol (str): The string to extract latex from.
Returns:
str: The latex string.
"""
if (match := symbol_latex_re.search(symbol)) is None:
return symbol
return match.group("latex")
def latex_symbols(symbols):
"""Create a mapping between custom Symbol objects and LaTeX symbol strings.
Used when parsing a sympy Expression to a LaTeX string.
Args:
symbols (SymbolDict): A dictionary of sympy symbol strings to LaTeX
symbol strings.
Returns:
Dict[Symbol, str]: A dictionary of sympy Symbol objects to LaTeX
strings.
"""
symbol_dict = {
Symbol(k): extract_latex(v["latex"])
for (k, v) in symbols.items()
}
return symbol_dict
def sympy_to_latex(equation, symbols, settings=None):
default_settings = {
"symbol_names": latex_symbols(symbols),
"ln_notation": True,
}
if settings is None:
settings = default_settings
else:
for key in default_settings.keys():
if key not in settings.keys():
settings[key] = default_settings[key]
latex_out = ModifiedLatexPrinter(settings).doprint(equation)
return latex_out
def create_sympy_parsing_params(params, unsplittable_symbols=tuple(), symbol_assumptions=tuple()):
'''
Input:
params : evaluation function parameter dictionary
unsplittable_symbols : list of strings that will not be split when parsing
even if implicit multiplication is used.
Output:
parsing_params: A dictionary that contains necessary info for the
parse_expression function.
'''
unsplittable_symbols = list(unsplittable_symbols)+params.get("reserved_keywords", [])
if "symbols" in params.keys():
for symbol in params["symbols"].keys():
if len(symbol) > 1:
unsplittable_symbols.append(symbol)
if params.get("specialFunctions", False) is True:
from sympy import beta, gamma, zeta, Lambda, Chi
else:
beta = Symbol("beta")
gamma = Symbol("gamma")
zeta = Symbol("zeta")
Lambda = Symbol("Lambda")
Chi = Symbol("Chi")
if params["complexNumbers"] is True:
from sympy import I
else:
I = Symbol("I")
if params["elementary_functions"] is True:
from sympy import E
else:
E = Symbol("E")
N = Symbol("N")
O = Symbol("O")
Q = Symbol("Q")
S = Symbol("S")
symbol_dict = {
"beta": beta,
"gamma": gamma,
"zeta": zeta,
"Lambda": Lambda,
"Chi": Chi,
"I": I,
"N": N,
"O": O,
"Q": Q,
"S": S,
"E": E
}
symbol_dict.update(sympy_symbols(unsplittable_symbols))
strict_syntax = params.get("strict_syntax", True)
parsing_params = {
"unsplittable_symbols": unsplittable_symbols,
"strict_syntax": strict_syntax,
"symbol_dict": symbol_dict,
"extra_transformations": tuple(),
"elementary_functions": params["elementary_functions"],
"convention": params["convention"],
"simplify": params.get("simplify", False),
"rationalise": params.get("rationalise", True),
"constants": set(),
"complexNumbers": params["complexNumbers"],
"reserved_keywords": params.get("reserved_keywords",[]),
}
symbol_assumptions = list(symbol_assumptions)
if "symbol_assumptions" in params.keys():
symbol_assumptions_strings = params["symbol_assumptions"]
index = symbol_assumptions_strings.find("(")
while index > -1:
index_match = find_matching_parenthesis(symbol_assumptions_strings, index)
try:
symbol_assumption = eval(symbol_assumptions_strings[index+1:index_match])
symbol_assumptions.append(symbol_assumption)
except (SyntaxError, TypeError) as e:
raise Exception("List of symbol assumptions not written correctly.") from e
index = symbol_assumptions_strings.find('(', index_match+1)
for symbol, assumption in symbol_assumptions:
try:
if assumption.lower() == "constant":
parsing_params["constants"] = parsing_params["constants"].union({symbol})
if assumption.lower() == "function":
parsing_params["symbol_dict"].update({symbol: eval("Function('"+symbol+"')")})
else:
parsing_params["symbol_dict"].update({symbol: eval("Symbol('"+symbol+"',"+assumption+"=True)")})
except Exception as e:
raise Exception(f"Assumption {assumption} for symbol {symbol} caused a problem.") from e
return parsing_params
def substitutions_sort_key(x):
return -len(x[0])-len(x[1])/(10**(1+len(str(len(x[1])))))
def preprocess_expression(name, expr, parameters):
expr = substitute_input_symbols(expr.strip(), parameters)
expr = expr[0]
expr, abs_feedback = convert_absolute_notation(expr, name)
success = True
if abs_feedback is not None:
success = False
return success, expr, abs_feedback
def parse_expression(expr_string, parsing_params):
'''
Input:
expr_string : string to be parsed into a sympy expression
parsing_params : dictionary that contains parsing parameters
Output:
sympy expression created by parsing expr configured according
to the parameters in parsing_params
'''
expr_set = create_expression_set(expr_string, parsing_params)
strict_syntax = parsing_params.get("strict_syntax", False)
extra_transformations = parsing_params.get("extra_transformations", ())
unsplittable_symbols = parsing_params.get("unsplittable_symbols", ())
symbol_dict = parsing_params.get("symbol_dict", {})
separate_unsplittable_symbols = [(x, " " + x + " ") for x in unsplittable_symbols]
substitutions = separate_unsplittable_symbols
parsed_expr_set = set()
for expr in expr_set:
expr = preprocess_according_to_chosen_convention(expr, parsing_params)
substitutions = list(set(substitutions))
substitutions += transform_unicode_greek_symbols(expr)
substitutions.sort(key=substitutions_sort_key)
if parsing_params["elementary_functions"] is True:
substitutions += protect_elementary_functions_substitutions(expr)
substitutions = list(set(substitutions))
substitutions.sort(key=substitutions_sort_key)
expr = substitute(expr, substitutions)
expr = " ".join(expr.split())
can_split = lambda x: False if x in unsplittable_symbols else _token_splittable(x)
if strict_syntax is True:
transformations = parser_transformations[0:4] + extra_transformations
else:
transformations = (parser_transformations[0:5, 6] + extra_transformations +
(split_symbols_custom(can_split),) + parser_transformations[8, 9])
if parsing_params.get("rationalise", False):
transformations += parser_transformations[11]
if "=" in expr:
expr_parts = expr.split("=")
lhs = parse_expr(expr_parts[0], transformations=transformations, local_dict=symbol_dict)
rhs = parse_expr(expr_parts[1], transformations=transformations, local_dict=symbol_dict)
parsed_expr = Equality(lhs, rhs, evaluate=False)
elif parsing_params.get("simplify", False):
parsed_expr = parse_expr(expr, transformations=transformations, local_dict=symbol_dict)
if not isinstance(parsed_expr, Equality):
parsed_expr = parsed_expr.simplify()
else:
parsed_expr = parse_expr(expr, transformations=transformations, local_dict=symbol_dict, evaluate=False)
if not isinstance(parsed_expr, Basic):
raise ValueError(f"Failed to parse Sympy expression `{expr}`")
parsed_expr_set.add(parsed_expr)
if len(expr_set) == 1:
return parsed_expr_set.pop()
else:
return parsed_expr_set