|
| 1 | +(* ::Package:: *) |
| 2 | + |
| 3 | +BeginPackage["ToPython`"] |
| 4 | + ToPython::usage = "ToPython[expression,numpystring] converts Mathematica expression to a Numpy compatible expression. |
| 5 | + because Numpy can be imported in several ways, numpystring is a string that will be added to appended to function names, e.g., Cos->numpy.cos" |
| 6 | +Begin["Private`"] |
| 7 | +ToPython[x_,numpyprefix_:"numpy"]:=Module[{expression=x,greekrule,PythonForm,numpypre=numpyprefix,lp,rp,a,b}, |
| 8 | +(*FUNCTION TO CONVERT MATHEMATICA EXPRESSION TO NUMPY; |
| 9 | +----------------------------------------------------; |
| 10 | +INPUT ARGUMENTS; |
| 11 | +x: your mathematica expression, it can be numbers, literals, complexes or lists; |
| 12 | +numpy\[LetterSpace]prefix: string defining your Numpy import prefix, e.g.: |
| 13 | +if your used "import numpy as np", your prefix should be the string "np" |
| 14 | +if your used "from numpy import *", your prefix should be the empty string "" |
| 15 | +; |
| 16 | +OUTPUT; |
| 17 | +the Numpy python-ready expression (to be copied as a string); |
| 18 | +!The formatted expression will be copied ot your clipboard, ready to paste on Python!; |
| 19 | +------------------------------------------------------; |
| 20 | +Not tested for every possible combination; use at your risk, by Gustavo Wiederhecker*) |
| 21 | +If[numpyprefix=="",sep="",sep="."];(*if no prefix is included, the "." separator is not used*) |
| 22 | +lp="( "; |
| 23 | +rp=" )"; |
| 24 | +PythonForm[Rational[a_,b_]]:=PythonForm[a]<>"/"<>PythonForm[b]; |
| 25 | +PythonForm[Complex[a_,b_]]:="complex"<>lp<>PythonForm[a]<>","<>PythonForm[b]<>rp; |
| 26 | +PythonForm[Times[a_,b_]]:=PythonForm[a]<>" * "<>PythonForm[b]; |
| 27 | +PythonForm[Plus[a_,b_]]:=lp<>PythonForm[a]<>" + "<>PythonForm[b]<>rp; |
| 28 | +PythonForm[h_[args__]]:=numpypre<>sep<>ToLowerCase[PythonForm[h]]<>lp<>PythonForm[args]<>rp; |
| 29 | +PythonForm[Power[a_,b_]]:=lp<>PythonForm[a]<>rp<>"**"<>lp<>PythonForm[b]<>rp; |
| 30 | +PythonForm[a_ListQ]:=numpypre<>sep<>"array"<>StringReplace[ToString[a],{"{"-> "[","}"-> "]"}]; |
| 31 | +PythonForm[Arg]=numpypre<>sep<>"angle"; |
| 32 | +(*Some functions that are note defined in numpy*) |
| 33 | +PythonForm[Csc]:="1/"<>numpypre<>sep<>"sin"; |
| 34 | +PythonForm[Sec]:="1/"<>numpypre<>sep<>"cos"; |
| 35 | +PythonForm[Cot]:="1/"<>numpypre<>sep<>"tan"; |
| 36 | +PythonForm[Csch]:="1/"<>numpypre<>sep<>"sinh"; |
| 37 | +PythonForm[Sech]:="1/"<>numpypre<>sep<>"cosh"; |
| 38 | +PythonForm[Coth]:="1/"<>numpypre<>sep<>"tanh"; |
| 39 | +(*Handling arrays*) |
| 40 | +PythonForm[List[args__]]:=numpypre<>sep<>"array"<>lp<>"["<>Table[PythonForm[{args}[[ii]]]<>",",{ii,1,Length@{args}}]<>"]"<>rp; |
| 41 | +(*Pi and E*) |
| 42 | +PythonForm[\[Pi]]=numpypre<>sep<>"pi"; |
| 43 | +PythonForm[E]=numpypre<>sep<>"e"; |
| 44 | +(*real numbers, engineering notation*) |
| 45 | +PythonForm[r_Real]:=Block[{a=MantissaExponent[r]},If[r>=0,ToString[N[a[[1]],6]]<>"e"<>ToString[a[[2]]],"("<>ToString[N[a[[1]],6]]<>"e"<>ToString[a[[2]]]<>")"]]; |
| 46 | +(*Greek characters*) |
| 47 | +greekrule={"\[Alpha]"->"alpha","\[Beta]"->"beta","\[Gamma]"->"gamma","\[Delta]"->"delta","\[CurlyEpsilon]"->"curlyepsilon","\[Zeta]"->"zeta","\[Eta]"->"eta","\[Theta]"->"theta","\[Iota]"->"iota","\[Kappa]"->"kappa","\[Lambda]"->"lambda","\[Mu]"->"mu","\[Nu]"->"nu","\[Xi]"->"xi","\[Omicron]"->"omicron","\[Pi]"->"pi","\[Rho]"->"rho","\[FinalSigma]"->"finalsigma","\[Sigma]"->"sigma","\[Tau]"->"tau","\[Upsilon]"->"upsilon","\[CurlyPhi]"->"curlyphi","\[Chi]"->"chi","\[Psi]"->"psi","\[Omega]"->"omega","\[CapitalAlpha]"->"Alpha","\[CapitalBeta]"->"Beta","\[CapitalGamma]"->"Gamma","\[CapitalDelta]"->"Delta","\[CapitalEpsilon]"->"CurlyEpsilon","\[CapitalZeta]"->"Zeta","\[CapitalEta]"->"Eta","\[CapitalTheta]"->"Theta","\[CapitalIota]"->"Iota","\[CapitalKappa]"->"Kappa","\[CapitalLambda]"->"Lambda","\[CapitalMu]"->"Mu","\[CapitalNu]"->"Nu","\[CapitalXi]"->"Xi","\[CapitalOmicron]"->"Omicron","\[CapitalPi]"->"Pi","\[CapitalRho]"->"Rho","\[CapitalSigma]"->"Sigma","\[CapitalTau]"->"Tau","\[CapitalUpsilon]"->"Upsilon","\[CapitalPhi]"->"CurlyPhi","\[CapitalChi]"->"Chi","\[CapitalPsi]"->"Psi","\[CapitalOmega]"->"Omega"}; |
| 48 | +(*Everything else*) |
| 49 | +PythonForm[allOther_]:=StringReplace[ToString[allOther,FortranForm],greekrule]; |
| 50 | +(*Copy results to clipboard*) |
| 51 | +CopyToClipboard[PythonForm[expression]]; |
| 52 | +PythonForm[expression]] |
| 53 | +End[] |
| 54 | +EndPackage[] |
0 commit comments