-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmathematica.py
37 lines (30 loc) · 1.03 KB
/
mathematica.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
from sympy import Matrix
from sympy.parsing.mathematica import parse_mathematica
from sympy import lambdify
from sympy import sympify
from sympy import Symbol
from numpy import pi, e
def convert_to_mathematica_matrix(matrix: Matrix) -> str:
""" Converts sympy matrix into a string that can be
pasted into Mathematica input. """
matrix_str = str(matrix.tolist())
return matrix_str \
.replace('[', '{') \
.replace(']', '}') \
.replace('cos', 'Cos') \
.replace('sin', 'Sin') \
.replace('(', '[') \
.replace(')', ']')
def get_func_from_mathematica(file_name: str):
with open(file_name, 'r') as file:
content = file.read()
expr_str = parse_mathematica(content)
# Convert the string to a sympy expression
expr = sympify(expr_str)
# Define the variables
k = Symbol('k')
# Use lambdify to create a function
# that can be evaluated numerically
subbed_expr = expr.subs({'pi': pi, 'e': e})
func = lambdify(k, subbed_expr, 'numpy')
return func