sympy: function to convert string into actual sympy object

67 views Asked by At

assume I have a csv-file in the following format:

csc($0);\csc@@{$0};1;;;;;https://docs.sympy.org/latest/modules/functions/elementary.html#csc
cot($0);\cot@@{$0};1;;;;;https://docs.sympy.org/latest/modules/functions/elementary.html#cot
> ;;;;;;;
sinh($0);\sinh@@{$0};1;;;;;https://docs.sympy.org/latest/modules/functions/elementary.html#sinh


I want to read it into a Python script using:

default_semantic_latex_table = {}
with open("CAS_SymPy.csv") as file:
        reader = csv.reader(file, delimiter=";")
        for line in reader:
            if line[0] != '':
                tup = (line[0], line[2])
                default_semantic_latex_table[tup] = str(line[1])

I'd like to get a dict of the following form: default_semantic_latex_table = { (sympy.functions.elementary.trigonometric.sinh, 1): FormatTemplate(r"\sinh@{$0}")}

The first element of the tuple should not be a string but an actual sympy-object.

Does anyone know about a function that can convert a string into a sympy-object such as sympy.functions.elementary.trigonometric.sin, sympy.functions.elementary.exponential.exp or sympy.concrete.products.Product? I'd greatly appreciate any help!

1

There are 1 answers

4
smichr On

sympify converst strings to SymPy objects:

>>> from sympy import sympify, pi
>>> sympify('cos')
cos
>>> _(pi)
-1