# Is there any PythonForm ? to automate Sin[] to math.sin()? [duplicate]

This question already has an answer here:

Sin[] does not exist in Python and while I can do FortranForm and similar things, I do not know of any PythonForm, is there anything built-in to convert an expression to Python?

Of course I can make rules like Sin[a_]->math.sin(a) but is going to be tedious and maybe somebody (at Wolfram) did this already?

I think the question is sufficiently different from this one 85445/convert-mathematica-math-expression-form-to-python-math-expression

Answer that I take from @masterxilo and I will keep updating as more known functions appear under my radar

PythonForm~SetAttributes~HoldAll
PythonForm[Sin[x_]] := StringTemplate["math.sin()"]@PythonForm@x
PythonForm[Cos[x_]] := StringTemplate["math.cos()"]@PythonForm@x
PythonForm[Log[x_]] := StringTemplate["math.log()"]@PythonForm@x
PythonForm[Rational[a_, b_]] :=
StringTemplate["(/)"][PythonForm@a, PythonForm@b]
PythonForm[Pi] := ToString["math.pi"]
PythonForm[Times[a_, b_]] :=
StringTemplate["( * )"][PythonForm@a, PythonForm@b]
PythonForm[Power[a_, b_]] :=
StringTemplate["(**)"][PythonForm@a, PythonForm@b]
PythonForm[Plus[a_, b_]] :=
StringTemplate["(+)"][PythonForm@a, PythonForm@b]
PythonForm[x_] := ToString@x;
PythonForm[f_Symbol[args___]] :=
StringTemplate["()"][ToString@f, PythonForm@args]
PythonForm[args___] := (PythonForm /@ {args})~StringRiffle~", ";


## marked as duplicate by bobthechemist, J. M. will be back soon♦May 17 '17 at 12:18

• you actually cant do ->math.sin() because mathematica will take it as a dot product. You can do something like ->mathdotsin() and fix it later with a string replace. Quite a hack though. – george2079 Aug 10 '16 at 16:44
• yes, Dot[] might stand in your way. For now I am doing an InputForm of everything and then replacing in the python editor . ->.,^ ->**, [ -> ( and ]->) .. awful but works, I was hoping in something more classy ... – Rho Phi Aug 10 '16 at 16:57
• Also related: mathematica.stackexchange.com/questions/85817/… – Michael E2 Aug 19 '16 at 13:46

If I where to do this manually, I would start with

PythonForm~SetAttributes~HoldAll
(*known symbols*)
PythonForm[Sin[x_]] := StringTemplate["math.sin()"]@PythonForm@x
PythonForm[Times[a_, b_]] :=
StringTemplate["( * )"][PythonForm@a, PythonForm@b]
(*fallback*)
PythonForm[x_] := ToString@x;
PythonForm[f_Symbol[args___]] :=
StringTemplate["()"][ToString@f, PythonForm@args]
PythonForm[args___] := (PythonForm /@ {args})~StringRiffle~", ";

PythonForm[Sin[2 * Sin[x] * 4 * f[x, y]]]


"math.sin((2 * (math.sin(x) * (4 * f(x, y)))))"

• You can probably leverage ToString@FortranForm to do some of the work. – masterxilo Aug 19 '16 at 13:42
• I am editing my question to add the list of (known symbols) I have added so far. It should be noticed that this function cannot evaluate things like % because of the HoldAll I presume. – Rho Phi Aug 31 '16 at 15:01

Have a look at my solution to this problem, at: https://mathematica.stackexchange.com/a/144200/48312. I also prepared a Mathematica package for handling this at https://github.com/gwiederhecker/ToPython.

• Are those questions duplicates? (don't have time to read everything now and it seems you did it so I'm asking :)) p.s. we try to avoid link-only answers. Could you show an input with OP's case and the output generated by your function? – Kuba May 17 '17 at 12:11
• @kuba I would say so. – bobthechemist May 17 '17 at 12:17
• @bobthechemist thanks. – Kuba May 17 '17 at 12:19
{"ft_lang_label":"__label__en","ft_lang_prob":0.6921927,"math_prob":0.65464735,"size":1839,"snap":"2019-35-2019-39","text_gpt3_token_len":517,"char_repetition_ratio":0.20871934,"word_repetition_ratio":0.0,"special_character_ratio":0.29526916,"punctuation_ratio":0.1577287,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.990203,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-09-23T14:36:42Z\",\"WARC-Record-ID\":\"<urn:uuid:b1d6b194-f12a-406b-9222-f4dfdfa05e05>\",\"Content-Length\":\"139135\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:11cae91b-e093-43db-916b-f2e48e94e4ca>\",\"WARC-Concurrent-To\":\"<urn:uuid:c2ec9e4c-39aa-47b4-82e9-acf202cb4cd2>\",\"WARC-IP-Address\":\"151.101.193.69\",\"WARC-Target-URI\":\"https://mathematica.stackexchange.com/questions/123623/is-there-any-pythonform-to-automate-sin-to-math-sin\",\"WARC-Payload-Digest\":\"sha1:TXD52JB7HLVJA6JY5EYCE3QTTBNEZJZY\",\"WARC-Block-Digest\":\"sha1:OI53JEEKH6YETNFH36AJDEEQG65OG7B5\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-39/CC-MAIN-2019-39_segments_1568514576965.71_warc_CC-MAIN-20190923125729-20190923151729-00028.warc.gz\"}"}