I have a Python file that has a variable defined using sympy. For instance, let us take the following (crazy) example that I could have eventually since I am automatizing a routine:
from sympy import *
e=symbols('e')
a=e*10**(-100)-sin(e**2-1)+cos(e*10**(-50)+1)
file=open('file.txt','w')
file.write(str(a))
file.close()
After this, the file is saved in the following way:
1.0e-100*e - sin(e**2 - 1) + cos(1.0e-50*e + 1)
I would like to open this file in Mathematica and save the output as a variable in Mathematica format.
The problem here is that to replace e
by *^
is not an option since e
is a parameter of the variable that is being saved so that would change even the variable e
. An interesting fact about this is that when e
is followed immediately by an integer, then the meaning of the e
is *^
. However, I don't know how to check that. Besides, I haven't been able to find a way to change the trigonometric functions into Mathematica input since the arguments of those functions could be anything.
Up until now, I have solved the problem with sqrt and general exponents but I don't know how to deal with the scientific notation and trigonometric functions.
EDIT: Up until now it seems that this solves the problem with e
in Mathematica:
file=Import["file.txt"]
file = StringReplace[file, "**" -> "^"]
file = StringReplace[file, "e" -> "*^"]
file = StringReplace[file, "*^ " -> "e "]
file = StringReplace[file, "*^^" -> "e^"]
file = StringReplace[file, "*^*" -> "e*"]
Yet I am not sure if there is another method to solve it or if that would solve all the problems with the e
.
FullForm
of Mathematica in sympy? If so, the conversion will be much easier. $\endgroup$srepr
. Now I believe you know what to do. $\endgroup$a=x**2
, thensrepr(a)
will be equal toPow(Symbol('x'),Integer(2))
. That only expands the operations but not in a "usual" mathematical way. I don't know if that can be read by Mathematica then... $\endgroup$