Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"nbsphinx": "hidden"
},
"outputs": [],
"source": [
"from pystencils.session import *"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Demo: Assignment collections and simplification\n",
"\n",
"\n",
"## Assignment collections\n",
"\n",
"The assignment collection class helps to formulate and simplify assignments for numerical kernels. \n",
"\n",
"An ``AssignmentCollection`` is an ordered collection of assignments, together with an optional ordered collection of subexpressions, that are required to evaluate the main assignments. There are various simplification rules available that operate on ``AssignmentCollection``s."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We start by defining some stencil update rule. Here we also use the *pystencils* ``Field``, note however that the assignment collection module works purely on the *sympy* level."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>Main Assignments:</div><table style=\"border:none; width: 100%; \"><tr style=\"border:none\"> <td style=\"border:none\">$${{f}_{C}^{1}} \\leftarrow {{f}_{E}^{0}} \\left(a^{2} - c\\right) + {{f}_{N}^{0}} \\left(a^{2} + b\\right) + {{f}_{S}^{0}} a^{2} + {{f}_{W}^{0}} \\left(a^{2} - 2 c\\right)$$</td> </tr> <tr style=\"border:none\"> <td style=\"border:none\">$${{f}_{C}^{0}} \\leftarrow {{f}_{E}^{0}} \\left(c^{2} - c\\right) + {{f}_{N}^{0}} \\left(b + c^{2}\\right) + {{f}_{S}^{0}} \\left(- a^{2} + c^{2}\\right) + {{f}_{W}^{0}} \\left(c^{2} - 2 c\\right)$$</td> </tr> </table>"
],
"text/plain": [
"Equation Collection for f_C^1,f_C^0"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a,b,c = sp.symbols(\"a b c\")\n",
"f = ps.fields(\"f(2) : [2D]\")\n",
"\n",
"a1 = ps.Assignment(f[0,0](1), (a**2 +b) * f[0,1] + \\\n",
" (a**2 - c) * f[1,0] + \\\n",
" (a**2 - 2*c) * f[-1,0] + \\\n",
" (a**2) * f[0, -1])\n",
"\n",
"a2 = ps.Assignment(f[0,0](0), (c**2 +b) * f[0,1] + \\\n",
" (c**2 - c) * f[1,0] + \\\n",
" (c**2 - 2*c) * f[-1,0] + \\\n",
" (c**2 - a**2) * f[0, -1])\n",
"\n",
"\n",
"ac = ps.AssignmentCollection([a1, a2], subexpressions=[])\n",
"ac"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*sympy* operations can be applied on an assignment collection: In this example we first expand the collection, then look for common subexpressions."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"expand_all = ps.simp.apply_to_all_assignments(sp.expand)\n",
"expandedEc = expand_all(ac)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>Subexpressions:</div><table style=\"border:none; width: 100%; \"><tr style=\"border:none\"> <td style=\"border:none\">$$\\xi_{0} \\leftarrow - {{f}_{E}^{0}} c + {{f}_{N}^{0}} b - 2 {{f}_{W}^{0}} c$$</td> </tr> <tr style=\"border:none\"> <td style=\"border:none\">$$\\xi_{1} \\leftarrow a^{2}$$</td> </tr> <tr style=\"border:none\"> <td style=\"border:none\">$$\\xi_{2} \\leftarrow {{f}_{S}^{0}} \\xi_{1}$$</td> </tr> <tr style=\"border:none\"> <td style=\"border:none\">$$\\xi_{3} \\leftarrow c^{2}$$</td> </tr> </table><div>Main Assignments:</div><table style=\"border:none; width: 100%; \"><tr style=\"border:none\"> <td style=\"border:none\">$${{f}_{C}^{1}} \\leftarrow {{f}_{E}^{0}} \\xi_{1} + {{f}_{N}^{0}} \\xi_{1} + {{f}_{W}^{0}} \\xi_{1} + \\xi_{0} + \\xi_{2}$$</td> </tr> <tr style=\"border:none\"> <td style=\"border:none\">$${{f}_{C}^{0}} \\leftarrow {{f}_{E}^{0}} \\xi_{3} + {{f}_{N}^{0}} \\xi_{3} + {{f}_{S}^{0}} \\xi_{3} + {{f}_{W}^{0}} \\xi_{3} + \\xi_{0} - \\xi_{2}$$</td> </tr> </table>"
],
"text/plain": [
"Equation Collection for f_C^1,f_C^0"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ac_cse = ps.simp.sympy_cse(expandedEc)\n",
"ac_cse"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Symbols occuring in assignment collections are classified into 3 categories:\n",
"- ``free_symbols``: symbols that occur in right-hand-sides but never on left-hand-sides\n",
"- ``bound_symbols``: symbols that occur on left-hand-sides\n",
"- ``defined_symbols``: symbols that occur on left-hand-sides of a main assignment"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAM0AAAAcBAMAAAAw8CbQAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAInbvRDKJ3asQu82ZVGZbSvgjAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAD7ElEQVRIDbVVS4gcVRQ9M91dPdXd9dmZhTDNjGY1EM1ixEW0RVBcTW8UXE0TcUSJ0GggH0SbCbjQLAoVlRDSDSp+shlciLpwKhOJEX8tKAQXTpNFcGUmOlkMMeq5t36vqpBZBC/T7517zrl1q+q9VwMYsXevkQgsEdMHezlLyZBT/yNxe84gJ5UIXMEl01E2mGoBT83FRL2DnZxWInAYXmBYygZDTGDtywjNd2Jm2MWBRNS5RNjXURkblpLB0DL49meCq9cTZtnHiwnWuUTUrqE+USkaSgZDM+Dngq2thFkO8FyCdS4RtR3U24alZDA0Az4h2LpLGXvjHO9uf6aeXvDzBB18nko7sZQqEgH2O/0UCzD7zDx7k297O9Nf3hznCTq4Pl4nsZQqEgFoDjJMZPYZPtLJ7R57+w3kCNCR22/FCuPSnrlZ8n02genQOD8Oly1HgA4sGOenWGH0WQqMJN/nKSqHDmW61S8QEId14ev0CsWKVIBajdR4b7/dc9IQAPfE0TBHoOgo5sDUhY17o5pP576Liw/OdYF9ktQHSv2hYzZ47QxHqOgo5njQb26p1b4RDH1Ft42ba8Aye9mvKDGVntboopillouio5jDvg+tkZY4O/B6ihYxM+bLOQw8HF1Plt1ZP3lx3Vcduuq//I3q81EBVTrw2Mqv3djAfPrP0L0/tNdDpVrpIW6tYdgTjpTGqRDVTxTpss8DjSBSdCnd94E74xziqIbaP634CPgG+CmyVEbggZNohlgaCyAlUfuYw+s9gY0BB75CSxKJD/izZjsIJZEQRz1AQ5M4Z5MjQDui+Aiy9RneBD8EAqKnwqw0bfWF0c/JIh53JZHYz5/VGGV9xOH5cESUkHwe7j64vuYYjnHWUex1uFYSbAj20P0Wfd+GHW6JbYw43qBBMPBa9Zrd5l2tCSOO5iIHI7+KM5vBdEw12+5RS8WZSSNU0BrgdDd3TuUEO399NWLNEUJUZQV/xgtOh2/rGDHEYZ//J5cvBe1lv01RLPbqk3vuiNDG93HVyu+iGuf0GaZWX+4e+uzWiKiHq29ywo8yiAPY0+eQ5rOPjocPTIRXSkCGMirt4wy+oKMZ2v6ZaCtcak5IhKgscGI/qMPuwt4ycgwfgndFHGKJI0UpyJ6n/qHcpnxln+aqEB5/iwOLrUVO8pTqcMaojYwc3nuovAuGvggBGcqouA/v0F3waeG/NScEJoSXz3GojmHf5Cx7UB087qdoTHNUBqiPqCols4HSnRuvDz8SUVgvfXtxvRsnMp3n50L6pdG6fSX7XAs748vfrnFWHa8GuxpvzWDdrfXV1Vu7zG7VtdX/+0GMO/gXjcIRAhBP5DAAAAAASUVORK5CYII=\n",
"text/latex": [
"$$\\left\\{{{f}_{E}^{0}}, {{f}_{N}^{0}}, {{f}_{S}^{0}}, {{f}_{W}^{0}}, a, b, c\\right\\}$$"
],
"text/plain": [
"set([f_E__0, f_N__0, f_S__0, f_W__0, a, b, c])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ac_cse.free_symbols"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},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Y45364gaCpDD6mKrJ81nGDA26nv/6r7H1/BRaZ2UutcMANdkGH16qMdF8c+feA/gXm2fQ29JvAmkJArsBt+47GJ8ciPTlaK1w2ylZ1pDfxX3W66j3gmEQ+X//FbLXXYpbM5V4G8i6eZ4/YkT+BHXruLdILSWqVG/3rSkNHclXzQTGzh+lEPvhh0uyXlJ9nP/4VzTJ7xO0v0Ye4Bn/pkoP0QpJa5fatr5uRaBTqudqBIvNGvs3jx18H+CcM99O12K9epAla+JL5AkN45bPiHkUtSSWcomdeakiSTeXzcmC0+VM1y5jEgaWXY2pDrmdehukPfUPjHzNogkvnGUwu9ceUPyU9sqmlP9HDHgJUEuvUFCUPAgAAAABJRU5ErkJggg==\n",
"text/latex": [
"$$\\left\\{{{f}_{C}^{0}}, {{f}_{C}^{1}}, \\xi_{0}, \\xi_{1}, \\xi_{2}, \\xi_{3}\\right\\}$$"
],
"text/plain": [
"set([f_C__0, f_C__1, ξ₀, ξ₁, ξ₂, ξ₃])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ac_cse.bound_symbols"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAE0AAAAcBAMAAADW9S/RAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAInbvRDKJ3asQu82ZVGZbSvgjAAAACXBIWXMAAA7EAAAOxAGVKw4bAAACAUlEQVQ4EYVSPWsUURQ9G3dmHONMtg+4o0JAEBJtrITpglWCf2A3WvhBAouICSlkiWChWwwxJBqEXbCJpAkpBFPIRiSKVUq7LFYhVZAEIhrIuffN7L5RwQfvnnPuPfPm3jcDWGtoyBJCnbU/EirDalDP5Z0vNUsXLqTCi/HTypMG4nM+meTF2CCa47ie0hTUh5fvRRYPs1qlhMcZN2h82BDl7me1SoIHGTeY+m6Lckc053/c5HlXTV3i68sl0x9g+05PHbO/g57v6c72v3zNG3FuXv9gIZ03f94O0Ne27i+QtgPTk/3eO0xPTibd97o1oPD5KJKE5du91uhahIRPZtrdxLAwr676h8ZeGIh6vDIO+M9UF7q3nZbLrGUrfAiMGq1thytb86W0yLmAvca984LLbRTfCYG2vQ7/jSoGmWswwvII0VlneFFlwJk6UI55gghZKxyXtx62yMvbDGdrDDgVAY+IN0XI4hf0qjwqItd5zfdtxuiTn+8V/F8E+DxrOCFy2/c3lsBriQGYllKRD82qZLDu+T4w0Db5oER0W8Bvoy1fUP9g2il0aGH5Wz+J9HuOu3ee97bGgVrGM8/K7BLDV+4qd+rb5/T8J8Gf3p8gdLi/bzJ4HSxK3vgCGVOX27iVZFzx7tYlxVWNz/PFnFOFe0WhOPd3yc44c/87yHKfAFzmbSKAO3ifAAAAAElFTkSuQmCC\n",
"text/latex": [
"$$\\left\\{{{f}_{C}^{0}}, {{f}_{C}^{1}}\\right\\}$$"
],
"text/plain": [
"set([f_C__0, f_C__1])"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ac_cse.defined_symbols"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Assignment collections can be splitted up, and merged together. For splitting, a list of symbols that occur on the left-hand-side in the main assignments has to be passed. The returned assignment collection only contains these main assignments together with all necessary subexpressions."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>Subexpressions:</div><table style=\"border:none; width: 100%; \"><tr style=\"border:none\"> <td style=\"border:none\">$$\\xi_{0} \\leftarrow - {{f}_{E}^{0}} c + {{f}_{N}^{0}} b - 2 {{f}_{W}^{0}} c$$</td> </tr> <tr style=\"border:none\"> <td style=\"border:none\">$$\\xi_{1} \\leftarrow a^{2}$$</td> </tr> <tr style=\"border:none\"> <td style=\"border:none\">$$\\xi_{2} \\leftarrow {{f}_{S}^{0}} \\xi_{1}$$</td> </tr> </table><div>Main Assignments:</div><table style=\"border:none; width: 100%; \"><tr style=\"border:none\"> <td style=\"border:none\">$${{f}_{C}^{1}} \\leftarrow {{f}_{E}^{0}} \\xi_{1} + {{f}_{N}^{0}} \\xi_{1} + {{f}_{W}^{0}} \\xi_{1} + \\xi_{0} + \\xi_{2}$$</td> </tr> </table>"
],
"text/plain": [
"Equation Collection for f_C^1"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ac_f0 = ac_cse.new_filtered([f(0)])\n",
"ac_f1 = ac_cse.new_filtered([f(1)])\n",
"ac_f1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note here that $\\xi_4$ is no longer part of the subexpressions, since it is not used in the main assignment of $f_C^1$.\n",
"\n",
"If we merge both collections together, we end up with the original collection."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>Subexpressions:</div><table style=\"border:none; width: 100%; \"><tr style=\"border:none\"> <td style=\"border:none\">$$\\xi_{0} \\leftarrow - {{f}_{E}^{0}} c + {{f}_{N}^{0}} b - 2 {{f}_{W}^{0}} c$$</td> </tr> <tr style=\"border:none\"> <td style=\"border:none\">$$\\xi_{1} \\leftarrow a^{2}$$</td> </tr> <tr style=\"border:none\"> <td style=\"border:none\">$$\\xi_{2} \\leftarrow {{f}_{S}^{0}} \\xi_{1}$$</td> </tr> <tr style=\"border:none\"> <td style=\"border:none\">$$\\xi_{3} \\leftarrow c^{2}$$</td> </tr> </table><div>Main Assignments:</div><table style=\"border:none; width: 100%; \"><tr style=\"border:none\"> <td style=\"border:none\">$${{f}_{C}^{0}} \\leftarrow {{f}_{E}^{0}} \\xi_{3} + {{f}_{N}^{0}} \\xi_{3} + {{f}_{S}^{0}} \\xi_{3} + {{f}_{W}^{0}} \\xi_{3} + \\xi_{0} - \\xi_{2}$$</td> </tr> <tr style=\"border:none\"> <td style=\"border:none\">$${{f}_{C}^{1}} \\leftarrow {{f}_{E}^{0}} \\xi_{1} + {{f}_{N}^{0}} \\xi_{1} + {{f}_{W}^{0}} \\xi_{1} + \\xi_{0} + \\xi_{2}$$</td> </tr> </table>"
],
"text/plain": [
"Equation Collection for f_C^0,f_C^1"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ac_f0.new_merged(ac_f1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There is also a method that inserts all subexpressions into the main assignments. This is the inverse operation of common subexpression elimination."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>Main Assignments:</div><table style=\"border:none; width: 100%; \"><tr style=\"border:none\"> <td style=\"border:none\">$${{f}_{C}^{0}} \\leftarrow {{f}_{E}^{0}} c^{2} - {{f}_{E}^{0}} c + {{f}_{N}^{0}} b + {{f}_{N}^{0}} c^{2} - {{f}_{S}^{0}} a^{2} + {{f}_{S}^{0}} c^{2} + {{f}_{W}^{0}} c^{2} - 2 {{f}_{W}^{0}} c$$</td> </tr> </table>"
],
"text/plain": [
"Equation Collection for f_C^0"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"assert sp.simplify(ac_f0.new_without_subexpressions().main_assignments[0].rhs - a2.rhs) == 0\n",
"ac_f0.new_without_subexpressions()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To evaluate an assignment collection, use the ``lambdify`` method. It is very similar to *sympy*s ``lambdify`` function."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAL4AAAAcBAMAAAAtjhhLAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAInbvRDKJ3asQu82ZVGZbSvgjAAAACXBIWXMAAA7EAAAOxAGVKw4bAAADEUlEQVRIDa1VT0gUURj/re7OOrr/7kludYqE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ad {{f}_{C}^{1}} : -17\\right \\}$$"
],
"text/plain": [
"{f_C__0: 75, f_C__1: -17}"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"evalFct = ac_cse.lambdify([f[0,1], f[1,0]], # new parameters of returned function\n",
" fixed_symbols={a:1, b:2, c:3, f[0,-1]: 4, f[-1,0]: 5}) # fix values of other symbols\n",
"evalFct(2,1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"lambdify is rather slow for evaluation. The intended way to evaluate an assignment collection is *pystencils* i.e. create a fast kernel, that applies the update at every site of a structured grid. The collection can be directly passed to the `create_kernel` function."
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"func = ps.create_kernel(ac_cse).compile()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Simplification Strategies\n",
"\n",
"In above examples, we already applied simplification rules to assignment collections. Simplification rules are functions that take, as a single argument, an assignment collection and return an modified/simplified copy of it. The ``SimplificationStrategy`` class holds a list of simplification rules and can apply all of them in the specified order. Additionally it provides useful printing and reporting functions. \n",
"\n",
"We start by creating a simplification strategy, consisting of the expand and CSE simplifications we have already applied above:"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"strategy = ps.simp.SimplificationStrategy()\n",
"strategy.add(ps.simp.apply_to_all_assignments(sp.expand))\n",
"strategy.add(ps.simp.sympy_cse)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This strategy can be applied to any assignment collection:"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>Subexpressions:</div><table style=\"border:none; width: 100%; \"><tr style=\"border:none\"> <td style=\"border:none\">$$\\xi_{0} \\leftarrow - {{f}_{E}^{0}} c + {{f}_{N}^{0}} b - 2 {{f}_{W}^{0}} c$$</td> </tr> <tr style=\"border:none\"> <td style=\"border:none\">$$\\xi_{1} \\leftarrow a^{2}$$</td> </tr> <tr style=\"border:none\"> <td style=\"border:none\">$$\\xi_{2} \\leftarrow {{f}_{S}^{0}} \\xi_{1}$$</td> </tr> <tr style=\"border:none\"> <td style=\"border:none\">$$\\xi_{3} \\leftarrow c^{2}$$</td> </tr> </table><div>Main Assignments:</div><table style=\"border:none; width: 100%; \"><tr style=\"border:none\"> <td style=\"border:none\">$${{f}_{C}^{1}} \\leftarrow {{f}_{E}^{0}} \\xi_{1} + {{f}_{N}^{0}} \\xi_{1} + {{f}_{W}^{0}} \\xi_{1} + \\xi_{0} + \\xi_{2}$$</td> </tr> <tr style=\"border:none\"> <td style=\"border:none\">$${{f}_{C}^{0}} \\leftarrow {{f}_{E}^{0}} \\xi_{3} + {{f}_{N}^{0}} \\xi_{3} + {{f}_{S}^{0}} \\xi_{3} + {{f}_{W}^{0}} \\xi_{3} + \\xi_{0} - \\xi_{2}$$</td> </tr> </table>"
],
"text/plain": [
"Equation Collection for f_C^1,f_C^0"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"strategy(ac)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The strategy can also print the simplification results at each stage. \n",
"The report contains information about the number of operations after each simplification as well as the runtime of each simplification routine."
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<table style=\"border:none\"><tr><th>Name</th><th>Runtime</th><th>Adds</th><th>Muls</th><th>Divs</th><th>Total</th></tr><tr><td>OriginalTerm</td><td>-</td> <td>13</td> <td>19</td> <td>0</td> <td>32</td> </tr><tr><td>expand</td><td>0.11 ms</td> <td>13</td> <td>26</td> <td>0</td> <td>39</td> </tr><tr><td>sympy_cse</td><td>3.25 ms</td> <td>11</td> <td>14</td> <td>0</td> <td>25</td> </tr></table>"
],
"text/plain": [
"<pystencils.simp.simplificationstrategy.SimplificationStrategy.create_simplification_report.<locals>.Report at 0x7f9be404fda0>"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"strategy.create_simplification_report(ac)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The strategy can also print the full collection after each simplification..."
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<h5 style=\"padding-bottom:10px\">Initial Version</h5> <div style=\"padding-left:20px;\"><div>Main Assignments:</div><table style=\"border:none; width: 100%; \"><tr style=\"border:none\"> <td style=\"border:none\">$${{f}_{C}^{1}} \\leftarrow {{f}_{E}^{0}} \\left(a^{2} - c\\right) + {{f}_{N}^{0}} \\left(a^{2} + b\\right) + {{f}_{S}^{0}} a^{2} + {{f}_{W}^{0}} \\left(a^{2} - 2 c\\right)$$</td> </tr> <tr style=\"border:none\"> <td style=\"border:none\">$${{f}_{C}^{0}} \\leftarrow {{f}_{E}^{0}} \\left(c^{2} - c\\right) + {{f}_{N}^{0}} \\left(b + c^{2}\\right) + {{f}_{S}^{0}} \\left(- a^{2} + c^{2}\\right) + {{f}_{W}^{0}} \\left(c^{2} - 2 c\\right)$$</td> </tr> </table></div><h5 style=\"padding-bottom:10px\">expand</h5> <div style=\"padding-left:20px;\"><div>Main Assignments:</div><table style=\"border:none; width: 100%; \"><tr style=\"border:none\"> <td style=\"border:none\">$${{f}_{C}^{1}} \\leftarrow {{f}_{E}^{0}} a^{2} - {{f}_{E}^{0}} c + {{f}_{N}^{0}} a^{2} + {{f}_{N}^{0}} b + {{f}_{S}^{0}} a^{2} + {{f}_{W}^{0}} a^{2} - 2 {{f}_{W}^{0}} c$$</td> </tr> <tr style=\"border:none\"> <td style=\"border:none\">$${{f}_{C}^{0}} \\leftarrow {{f}_{E}^{0}} c^{2} - {{f}_{E}^{0}} c + {{f}_{N}^{0}} b + {{f}_{N}^{0}} c^{2} - {{f}_{S}^{0}} a^{2} + {{f}_{S}^{0}} c^{2} + {{f}_{W}^{0}} c^{2} - 2 {{f}_{W}^{0}} c$$</td> </tr> </table></div><h5 style=\"padding-bottom:10px\">sympy_cse</h5> <div style=\"padding-left:20px;\"><div>Subexpressions:</div><table style=\"border:none; width: 100%; \"><tr style=\"border:none\"> <td style=\"border:none\">$$\\xi_{0} \\leftarrow - {{f}_{E}^{0}} c + {{f}_{N}^{0}} b - 2 {{f}_{W}^{0}} c$$</td> </tr> <tr style=\"border:none\"> <td style=\"border:none\">$$\\xi_{1} \\leftarrow a^{2}$$</td> </tr> <tr style=\"border:none\"> <td style=\"border:none\">$$\\xi_{2} \\leftarrow {{f}_{S}^{0}} \\xi_{1}$$</td> </tr> <tr style=\"border:none\"> <td style=\"border:none\">$$\\xi_{3} \\leftarrow c^{2}$$</td> </tr> </table><div>Main Assignments:</div><table style=\"border:none; width: 100%; \"><tr style=\"border:none\"> <td style=\"border:none\">$${{f}_{C}^{1}} \\leftarrow {{f}_{E}^{0}} \\xi_{1} + {{f}_{N}^{0}} \\xi_{1} + {{f}_{W}^{0}} \\xi_{1} + \\xi_{0} + \\xi_{2}$$</td> </tr> <tr style=\"border:none\"> <td style=\"border:none\">$${{f}_{C}^{0}} \\leftarrow {{f}_{E}^{0}} \\xi_{3} + {{f}_{N}^{0}} \\xi_{3} + {{f}_{S}^{0}} \\xi_{3} + {{f}_{W}^{0}} \\xi_{3} + \\xi_{0} - \\xi_{2}$$</td> </tr> </table></div>"
],
"text/plain": [
"<pystencils.simp.simplificationstrategy.SimplificationStrategy.show_intermediate_results.<locals>.IntermediateResults at 0x7f9bad688dd8>"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"strategy.show_intermediate_results(ac)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"... or only specific assignments for better readability"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<h5 style=\"padding-bottom:10px\">Initial Version</h5> <div style=\"padding-left:20px;\">$${{f}_{C}^{1}} \\leftarrow {{f}_{E}^{0}} \\left(a^{2} - c\\right) + {{f}_{N}^{0}} \\left(a^{2} + b\\right) + {{f}_{S}^{0}} a^{2} + {{f}_{W}^{0}} \\left(a^{2} - 2 c\\right)$$</div><h5 style=\"padding-bottom:10px\">expand</h5> <div style=\"padding-left:20px;\">$${{f}_{C}^{1}} \\leftarrow {{f}_{E}^{0}} a^{2} - {{f}_{E}^{0}} c + {{f}_{N}^{0}} a^{2} + {{f}_{N}^{0}} b + {{f}_{S}^{0}} a^{2} + {{f}_{W}^{0}} a^{2} - 2 {{f}_{W}^{0}} c$$</div><h5 style=\"padding-bottom:10px\">sympy_cse</h5> <div style=\"padding-left:20px;\">$${{f}_{C}^{1}} \\leftarrow {{f}_{E}^{0}} \\xi_{1} + {{f}_{N}^{0}} \\xi_{1} + {{f}_{W}^{0}} \\xi_{1} + \\xi_{0} + \\xi_{2}$$</div>"
],
"text/plain": [
"<pystencils.simp.simplificationstrategy.SimplificationStrategy.show_intermediate_results.<locals>.IntermediateResults at 0x7f9bad688b00>"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"strategy.show_intermediate_results(ac, symbols=[f(1)])"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 1
}