-
Notifications
You must be signed in to change notification settings - Fork 1
/
module2_formula_rearrangement.py
47 lines (41 loc) · 2.05 KB
/
module2_formula_rearrangement.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
import requests
import sympy
import random
def get_random_formula_rearrangements(defining_formula,formula_identifiers):
"""Generate random formula rearrangements using Sympy."""
formula_rearrangements = []
try:
# Transform LaTeX to Sympy
url = "https://vmext-demo.formulasearchengine.com/math/translation"
params = {'cas': 'SymPy', 'genericExperimentalFeatures': 'true', 'latex': defining_formula}
response = requests.post(url, params=params)
formula_string = response.json()['result']
# translate formula lhs and rhs
formula_lhs_sympy = sympy.sympify(formula_string.split("==")[0])
formula_rhs_sympy = sympy.sympify(formula_string.split("==")[1])
# translate identifiers
identifiers_sympy = sympy.symbols(' '.join([identifier[1] for identifier in formula_identifiers]))
# Solve equation for different identifiers
for identifier in identifiers_sympy:
eq = sympy.Eq(lhs=formula_lhs_sympy,rhs=formula_rhs_sympy)
formula_rearrangements.append((identifier,sympy.solve(eq,identifier)))
# select random rearrangement
selected = random.choice(formula_rearrangements)
# update defining formula
defining_formula_rearranged = str(selected[0]) + " = " + str(selected[1][0])
# rearrange identifier
lhs_identifier = []
rhs_identifier = []
for identifier in formula_identifiers:
if identifier[1] == str(selected[0]):
lhs_identifier.append(identifier)
formula_unit_dimension = identifier[2]
else:
rhs_identifier.append(identifier)
formula_identifiers_rearranged = lhs_identifier + rhs_identifier
except:
# if formula rearrangement was not successful
defining_formula_rearranged = defining_formula
formula_identifiers_rearranged = formula_identifiers
formula_unit_dimension = formula_identifiers[0][2]
return defining_formula_rearranged,formula_identifiers_rearranged,formula_unit_dimension