I am trying to convert some set of equations to a form that will be easy for Python to evaluate numerically, hence I am using Mathematica's FortranForm
My problem is that
FortranForm[np.diag]
gives
Dot(np,diag)
whereas I want
np.diag
I do not want
FortranForm["np.diag"]
which results in
"np.diag"
What I want is to be able to cast the DiagonalMatrix[equations]
part of my expressions to the equivalent in Python - np.diag(equations)
, where I have imported numpy
as np
Solution, but not the desired one
I found a way round my problem, though it is not what I want, but I will post it for people that my just care about the end result.
Use FortranForm["np.diag"]
and then export the result to a text file. Then in Python one can read the file and rewrite it line by line, while stripping all the quotes using this piece of code
def fromMathematica(func):
"""" string -> string
Imports a file, created by Mathematica and reads it, so that it can be evaluated for specific values
"""
file = open('path' + func + .txt' , 'r+')
lines = file.readlines()
file.seek(0)
file.truncate()
for line in lines:
line = line.replace('"', '').strip()
file.write(line)
file.seek(0)
m = file.read()
return m
StringReplace
. $\endgroup$FortranForm
will return it surrounded by quotes, which is what I want to avoid $\endgroup$npdotdiag
in mathematica and do a stringreplace on the result. (You should not need to do anything by hand! ) $\endgroup$