Skip to content

Commit 2f488ac

Browse files
added example
1 parent 2e71b07 commit 2f488ac

File tree

1 file changed

+315
-0
lines changed

1 file changed

+315
-0
lines changed

ToPython_example.nb

Lines changed: 315 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,315 @@
1+
(* Content-type: application/vnd.wolfram.mathematica *)
2+
3+
(*** Wolfram Notebook File ***)
4+
(* http://www.wolfram.com/nb *)
5+
6+
(* CreatedBy='Mathematica 11.0' *)
7+
8+
(*CacheID: 234*)
9+
(* Internal cache information:
10+
NotebookFileLineBreakTest
11+
NotebookFileLineBreakTest
12+
NotebookDataPosition[ 158, 7]
13+
NotebookDataLength[ 10294, 305]
14+
NotebookOptionsPosition[ 8703, 252]
15+
NotebookOutlinePosition[ 9213, 274]
16+
CellTagsIndexPosition[ 9127, 269]
17+
WindowFrame->Normal*)
18+
19+
(* Beginning of Notebook Content *)
20+
Notebook[{
21+
Cell[BoxData[
22+
RowBox[{
23+
RowBox[{"(*",
24+
RowBox[{
25+
RowBox[{"loads", " ", "the", " ", "package"}], " ", "-", " ",
26+
RowBox[{"after", " ", "installation"}]}], "*)"}], "\[IndentingNewLine]",
27+
RowBox[{"Needs", "[", "\"\<ToPython`\>\"", "]"}]}]], "Input",
28+
CellChangeTimes->{{3.70181767476367*^9, 3.701817694205203*^9}, {
29+
3.7018181976937943`*^9, 3.701818211998588*^9}}],
30+
31+
Cell[CellGroupData[{
32+
33+
Cell[BoxData[
34+
RowBox[{"?", "ToPython"}]], "Input",
35+
CellChangeTimes->{{3.70181815383568*^9, 3.7018181578773003`*^9},
36+
3.701818214459565*^9}],
37+
38+
Cell[BoxData[
39+
StyleBox["\<\"ToPython[expression,numpystring] converts Mathematica \
40+
expression to a Numpy compatible expression.\\n because Numpy can be imported \
41+
in several ways, numpystring is a string that will be added to appended to \
42+
function names, e.g., Cos->numpy.cos\"\>", "MSG"]], "Print", "PrintUsage",
43+
CellChangeTimes->{3.7018181586956463`*^9},
44+
CellTags->"Info273701807358-2755471"]
45+
}, Open ]],
46+
47+
Cell[CellGroupData[{
48+
49+
Cell[BoxData[
50+
RowBox[{
51+
RowBox[{"(*",
52+
RowBox[{"Expression", " ", "examples"}], "*)"}], "\[IndentingNewLine]",
53+
RowBox[{
54+
RowBox[{"ToPython", "[",
55+
RowBox[{"a", "+", "b"}], "]"}], "\[IndentingNewLine]",
56+
RowBox[{"ToPython", "[",
57+
RowBox[{"a", "*", "b"}], "]"}], "\[IndentingNewLine]",
58+
RowBox[{"ToPython", "[",
59+
RowBox[{"a", "/", "b"}], "]"}], "\[IndentingNewLine]",
60+
RowBox[{"ToPython", "[",
61+
RowBox[{
62+
RowBox[{"(",
63+
RowBox[{"a", "+", "b"}], ")"}], "/",
64+
RowBox[{"(",
65+
RowBox[{"d", "+", "e", "+", "g"}], ")"}]}], "]"}],
66+
"\[IndentingNewLine]",
67+
RowBox[{"ToPython", "[",
68+
RowBox[{
69+
RowBox[{"(",
70+
RowBox[{"a", "+", "b"}], ")"}], "^",
71+
RowBox[{"(",
72+
RowBox[{"d", "+", "e", "+", "g"}], ")"}]}], "]"}],
73+
"\[IndentingNewLine]",
74+
RowBox[{"ToPython", "[",
75+
RowBox[{"Exp", "[",
76+
RowBox[{"a", "+", "b"}], "]"}], "]"}], "\[IndentingNewLine]",
77+
RowBox[{"ToPython", "[",
78+
RowBox[{
79+
RowBox[{"Sin", "[",
80+
RowBox[{"(",
81+
RowBox[{"a", "+", "b"}], ")"}], "]"}], "/",
82+
RowBox[{"Cos", "[",
83+
RowBox[{"d", "+", "e"}], "]"}]}], "]"}], "\[IndentingNewLine]",
84+
RowBox[{"ToPython", "[",
85+
RowBox[{
86+
RowBox[{"Sin", "[",
87+
RowBox[{"(",
88+
RowBox[{"a", "+", "b"}], ")"}], "]"}], "/",
89+
RowBox[{"Tanh", "[",
90+
RowBox[{"d", "+", "e"}], "]"}]}], "]"}], "\[IndentingNewLine]",
91+
RowBox[{"ToPython", "[",
92+
RowBox[{"\[Pi]", " ",
93+
RowBox[{"Cosh", "[", "a", "]"}]}], "]"}], "\[IndentingNewLine]",
94+
RowBox[{"ToPython", "[",
95+
RowBox[{"Log10", "[", "x", "]"}], "]"}], "\[IndentingNewLine]",
96+
RowBox[{"(*",
97+
RowBox[{"Expression", " ", "with", " ", "greek", " ", "letters"}], "*)"}],
98+
"\[IndentingNewLine]",
99+
RowBox[{"ToPython", "[",
100+
RowBox[{"Sin", "[",
101+
RowBox[{"\[Alpha]", "+", "\[Beta]"}], "]"}], "]"}],
102+
"\[IndentingNewLine]",
103+
RowBox[{"(*",
104+
RowBox[{"Numeric", " ", "examples"}], "*)"}], "\[IndentingNewLine]",
105+
RowBox[{"ToPython", "[", "2", "]"}], "\[IndentingNewLine]",
106+
RowBox[{"ToPython", "[",
107+
RowBox[{"1", "/", "3"}], "]"}], "\[IndentingNewLine]",
108+
RowBox[{"ToPython", "[",
109+
RowBox[{"1.0", "/", "3"}], "]"}], "\[IndentingNewLine]",
110+
RowBox[{"ToPython", "[", "2.31", "]"}], "\[IndentingNewLine]",
111+
RowBox[{"ToPython", "[",
112+
RowBox[{"2.31", "+",
113+
RowBox[{"5.3", "I"}]}], "]"}], "\[IndentingNewLine]",
114+
RowBox[{"(*",
115+
RowBox[{"Array", " ", "handling"}], "*)"}], "\[IndentingNewLine]",
116+
RowBox[{"ToPython", "[",
117+
RowBox[{"{",
118+
RowBox[{"1", ",", "2", ",", "3"}], "}"}], "]"}], "\[IndentingNewLine]",
119+
RowBox[{"ToPython", "[",
120+
RowBox[{"{",
121+
RowBox[{"{",
122+
RowBox[{"1", ",", "2", ",", "3"}], "}"}], "}"}], "]"}],
123+
"\[IndentingNewLine]",
124+
RowBox[{"ToPython", "[",
125+
RowBox[{"Cos", "[",
126+
RowBox[{"{",
127+
RowBox[{"1", ",", "2", ",", "3"}], "}"}], "]"}], "]"}],
128+
"\[IndentingNewLine]",
129+
RowBox[{"(*",
130+
RowBox[{"Example", " ", "with", " ", "numpy", " ", "as", " ", "np"}],
131+
"*)"}], "\[IndentingNewLine]",
132+
RowBox[{"ToPython", "[",
133+
RowBox[{
134+
RowBox[{"\[Pi]", " ",
135+
RowBox[{
136+
RowBox[{"Cosh", "[", "a", "]"}], "/",
137+
RowBox[{"Sin", "[", "b", "]"}]}]}], ",", "\"\<np\>\""}], "]"}],
138+
"\[IndentingNewLine]",
139+
RowBox[{"ToPython", "[",
140+
RowBox[{
141+
RowBox[{"Exp", "[",
142+
RowBox[{"a", "+", "b"}], "]"}], ",", "\"\<np\>\""}], "]"}],
143+
"\[IndentingNewLine]",
144+
RowBox[{"ToPython", "[",
145+
RowBox[{
146+
RowBox[{"Cos", "[",
147+
RowBox[{"{",
148+
RowBox[{"1", ",", "2", ",", "3"}], "}"}], "]"}], ",", "\"\<np\>\""}],
149+
"]"}], "\[IndentingNewLine]",
150+
RowBox[{"(*",
151+
RowBox[{
152+
"Example", " ", "with", " ", "numpy", " ", "as", " ",
153+
"\"\<from numpy import *\>\""}], "*)"}], "\[IndentingNewLine]",
154+
RowBox[{"ToPython", "[",
155+
RowBox[{
156+
RowBox[{"\[Pi]", " ",
157+
RowBox[{
158+
RowBox[{"Cosh", "[", "a", "]"}], "/",
159+
RowBox[{"Sin", "[", "b", "]"}]}]}], ",", "\"\<\>\""}], "]"}],
160+
"\[IndentingNewLine]",
161+
RowBox[{"ToPython", "[",
162+
RowBox[{
163+
RowBox[{"Exp", "[",
164+
RowBox[{"a", "+", "b"}], "]"}], ",", "\"\<\>\""}], "]"}],
165+
"\[IndentingNewLine]",
166+
RowBox[{"ToPython", "[",
167+
RowBox[{
168+
RowBox[{"Cos", "[",
169+
RowBox[{"{",
170+
RowBox[{"1", ",", "2", ",", "3"}], "}"}], "]"}], ",", "\"\<\>\""}],
171+
"]"}]}]}]], "Input"],
172+
173+
Cell[BoxData["\<\"( a + b )\"\>"], "Output",
174+
CellChangeTimes->{3.701817713973694*^9}],
175+
176+
Cell[BoxData["\<\"a * b\"\>"], "Output",
177+
CellChangeTimes->{3.7018177140188704`*^9}],
178+
179+
Cell[BoxData["\<\"a * ( b )**( -1 )\"\>"], "Output",
180+
CellChangeTimes->{3.701817714041884*^9}],
181+
182+
Cell[BoxData["\<\"( a + b ) * ( ( d + ( e + g ) ) )**( -1 )\"\>"], "Output",
183+
CellChangeTimes->{3.701817714075296*^9}],
184+
185+
Cell[BoxData["\<\"( ( a + b ) )**( ( d + ( e + g ) ) )\"\>"], "Output",
186+
CellChangeTimes->{3.7018177141115913`*^9}],
187+
188+
Cell[BoxData["\<\"( numpy.e )**( ( a + b ) )\"\>"], "Output",
189+
CellChangeTimes->{3.701817714138361*^9}],
190+
191+
Cell[BoxData["\<\"numpy.1/numpy.cos( ( d + e ) ) * numpy.sin( ( a + b ) \
192+
)\"\>"], "Output",
193+
CellChangeTimes->{3.7018177141633997`*^9}],
194+
195+
Cell[BoxData["\<\"numpy.1/numpy.tanh( ( d + e ) ) * numpy.sin( ( a + b ) \
196+
)\"\>"], "Output",
197+
CellChangeTimes->{3.701817714189672*^9}],
198+
199+
Cell[BoxData["\<\"numpy.pi * numpy.cosh( a )\"\>"], "Output",
200+
CellChangeTimes->{3.701817714232546*^9}],
201+
202+
Cell[BoxData["\<\"( numpy.log( 10 ) )**( -1 ) * numpy.log( x )\"\>"], "Output",
203+
CellChangeTimes->{3.701817714264065*^9}],
204+
205+
Cell[BoxData["\<\"numpy.sin( ( alpha + beta ) )\"\>"], "Output",
206+
CellChangeTimes->{3.701817714289968*^9}],
207+
208+
Cell[BoxData["\<\"2\"\>"], "Output",
209+
CellChangeTimes->{3.7018177143315697`*^9}],
210+
211+
Cell[BoxData["\<\"1/3\"\>"], "Output",
212+
CellChangeTimes->{3.701817714353628*^9}],
213+
214+
Cell[BoxData["\<\"0.333333e0\"\>"], "Output",
215+
CellChangeTimes->{3.7018177143904676`*^9}],
216+
217+
Cell[BoxData["\<\"0.231e1\"\>"], "Output",
218+
CellChangeTimes->{3.701817714423192*^9}],
219+
220+
Cell[BoxData["\<\"complex( 0.231e1,0.53e1 )\"\>"], "Output",
221+
CellChangeTimes->{3.701817714456196*^9}],
222+
223+
Cell[BoxData["\<\"numpy.array( [1,2,3,] )\"\>"], "Output",
224+
CellChangeTimes->{3.701817714486806*^9}],
225+
226+
Cell[BoxData["\<\"numpy.array( [numpy.array( [1,2,3,] ),] )\"\>"], "Output",
227+
CellChangeTimes->{3.701817714500472*^9}],
228+
229+
Cell[BoxData["\<\"numpy.array( [numpy.cos( 1 ),numpy.cos( 2 ),numpy.cos( 3 \
230+
),] )\"\>"], "Output",
231+
CellChangeTimes->{3.701817714522142*^9}],
232+
233+
Cell[BoxData["\<\"np.pi * np.cosh( a ) * np.1/np.sin( b )\"\>"], "Output",
234+
CellChangeTimes->{3.7018177145365868`*^9}],
235+
236+
Cell[BoxData["\<\"( np.e )**( ( a + b ) )\"\>"], "Output",
237+
CellChangeTimes->{3.7018177145737057`*^9}],
238+
239+
Cell[BoxData["\<\"np.array( [np.cos( 1 ),np.cos( 2 ),np.cos( 3 ),] )\"\>"], \
240+
"Output",
241+
CellChangeTimes->{3.701817714607751*^9}],
242+
243+
Cell[BoxData["\<\"pi * cosh( a ) * 1/sin( b )\"\>"], "Output",
244+
CellChangeTimes->{3.701817714640382*^9}],
245+
246+
Cell[BoxData["\<\"( e )**( ( a + b ) )\"\>"], "Output",
247+
CellChangeTimes->{3.701817714670789*^9}],
248+
249+
Cell[BoxData["\<\"array( [cos( 1 ),cos( 2 ),cos( 3 ),] )\"\>"], "Output",
250+
CellChangeTimes->{3.701817714689067*^9}]
251+
}, Open ]]
252+
},
253+
WindowSize->{808, 701},
254+
WindowMargins->{{36, Automatic}, {16, Automatic}},
255+
FrontEndVersion->"11.0 for Mac OS X x86 (32-bit, 64-bit Kernel) (July 28, \
256+
2016)",
257+
StyleDefinitions->"Default.nb"
258+
]
259+
(* End of Notebook Content *)
260+
261+
(* Internal cache information *)
262+
(*CellTagsOutline
263+
CellTagsIndex->{
264+
"Info273701807358-2755471"->{
265+
Cell[1107, 37, 399, 6, 79, "Print",
266+
CellTags->"Info273701807358-2755471"]}
267+
}
268+
*)
269+
(*CellTagsIndex
270+
CellTagsIndex->{
271+
{"Info273701807358-2755471", 9023, 263}
272+
}
273+
*)
274+
(*NotebookFileOutline
275+
Notebook[{
276+
Cell[558, 20, 377, 8, 54, "Input"],
277+
Cell[CellGroupData[{
278+
Cell[960, 32, 144, 3, 32, "Input"],
279+
Cell[1107, 37, 399, 6, 79, "Print",
280+
CellTags->"Info273701807358-2755471"]
281+
}, Open ]],
282+
Cell[CellGroupData[{
283+
Cell[1543, 48, 4409, 122, 663, "Input"],
284+
Cell[5955, 172, 86, 1, 32, "Output"],
285+
Cell[6044, 175, 84, 1, 32, "Output"],
286+
Cell[6131, 178, 94, 1, 32, "Output"],
287+
Cell[6228, 181, 118, 1, 32, "Output"],
288+
Cell[6349, 184, 115, 1, 32, "Output"],
289+
Cell[6467, 187, 103, 1, 32, "Output"],
290+
Cell[6573, 190, 136, 2, 32, "Output"],
291+
Cell[6712, 194, 135, 2, 32, "Output"],
292+
Cell[6850, 198, 103, 1, 32, "Output"],
293+
Cell[6956, 201, 121, 1, 32, "Output"],
294+
Cell[7080, 204, 106, 1, 32, "Output"],
295+
Cell[7189, 207, 80, 1, 32, "Output"],
296+
Cell[7272, 210, 80, 1, 32, "Output"],
297+
Cell[7355, 213, 89, 1, 32, "Output"],
298+
Cell[7447, 216, 84, 1, 32, "Output"],
299+
Cell[7534, 219, 102, 1, 32, "Output"],
300+
Cell[7639, 222, 100, 1, 32, "Output"],
301+
Cell[7742, 225, 118, 1, 32, "Output"],
302+
Cell[7863, 228, 141, 2, 32, "Output"],
303+
Cell[8007, 232, 118, 1, 32, "Output"],
304+
Cell[8128, 235, 102, 1, 32, "Output"],
305+
Cell[8233, 238, 129, 2, 32, "Output"],
306+
Cell[8365, 242, 104, 1, 32, "Output"],
307+
Cell[8472, 245, 97, 1, 32, "Output"],
308+
Cell[8572, 248, 115, 1, 32, "Output"]
309+
}, Open ]]
310+
}
311+
]
312+
*)
313+
314+
(* End of internal cache information *)
315+

0 commit comments

Comments
 (0)