-
Notifications
You must be signed in to change notification settings - Fork 0
/
solve.py
executable file
·43 lines (38 loc) · 1.22 KB
/
solve.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
#!/usr/bin/env python3
import sys, sympy
def solve(*eqs):
exprs = []
vars = []
for eq in eqs:
if isinstance(eq, str) and '==' in eq:
exprs.append(sympy.Eq(*map(sympy.sympify, eq.split('=='))))
else:
eq = sympy.sympify(eq)
if isinstance(eq, sympy.Symbol):
vars.append(eq)
else:
exprs.append(eq)
if len(eqs) == 1:
return sympy.solve(exprs[0], *vars)
return sympy.solve(exprs, *vars)
if __name__ == '__main__':
res = solve(*sys.argv[1:])
if isinstance(res, list):
for expr in res:
if isinstance(expr, dict):
items = sorted(expr.items(), key=lambda i: i[0].name)
for var, expr in items:
sympy.pprint(sympy.Eq(var, expr))
print()
elif isinstance(expr, tuple):
for expr in expr:
sympy.pprint(expr)
print()
else:
sympy.pprint(expr)
elif isinstance(res, dict):
items = sorted(res.items(), key=lambda i: i[0].name)
for var, expr in items:
sympy.pprint(sympy.Eq(var, expr))
else:
sympy.pprint(res)