demo_derivatives.ipynb 50.2 KB
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
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pystencils.session import *"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Demo: Working with derivatives\n",
    "\n",
    "\n",
    "## Overview\n",
    "This notebook demonstrates how to formulate continuous differential operators in *pystencils* and automatically derive finite difference stencils from them.\n",
    "\n",
    "Instead of using the built-in derivatives in *sympy*, *pystencils* comes with its own derivative objects. They represent spatial derivatives of pystencils fields."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
Markus Holzer's avatar
Markus Holzer committed
32
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAD4AAAAYCAYAAACiNE5vAAAACXBIWXMAAA7EAAAOxAGVKw4bAAADhklEQVRYCd2Y600bQRCAjZUCrKSCUEKADqADp4RAB0H8gn8IOoBUwKMD6ICEDqADEB2Q71vfnvbWd4f3MLKSkcazj3nuzO4crL2+vo5yODo6mrC2Da6Dt8zvc55/fT7OAyBIAz4ADfYc3GTtCvpfwVqa8SrTB9D9NErmP5lvQb+n60PG6DhD7qWSPWYex0PUDZbJM26md3HGUk/BzE9Z/5Yulo6RP0HmM3gB7oJW10ogD/wBLy7zLFRzM/NeRw3WoA3+N3gLrgQ+pVYJ0Mx2wTMbW12bb62j20OzknwsPcSVBa2vjcBdwCmzsgHqnHDH2jXUF/7RhYGwg9xjFfRAFcsTyx+3m8q5vWgCR82UQfsonTOv9yJPH4XfgzRo9Vg1dovnUj3ILBXqwHHEoEdQnWwAa39Y8GHbY9x3HRpy6QQ5Pxj2oafp+qrG4XHDmfjCNtpY4pR3U7ickbJf9FsxQue9hseuEflm3Ev8RbctuYZ4xw34hU3LsA106Jr9cO+htiXhCfzCvOvAAhM/oQ126Wfdg/f+128I41Iboz4Z9k7BMzBc1TEDs2lgrdlg37speMdHzHXyCRrL9oZxuCbud4DdoA4q5UFW+zvQ2j7jYhsLylzBFw50jFF7qnA3I3O/ZvM+cUzB1EnH2+x7eF1gxruqSX3hUBPhITbelKli0NeJGTcToYQTw2HI3pSBGQ+fqswNzgzl2VM+lDO0DTZZ7DpYHUkPsthGoV8hUePKS7PaaFMoM5Bf4AbjGKhBt4FtKlZOYz9xai7j1V5+6MU2MFgiYwJ2wuOGA/Zne6t/hRlEhK+s5Y7FvZQadJfxUAnoqbOaCJrd1F6yNTfsszHHXC20yRjPenzVRzjm15nYB12HYNCxKnJ5vwu69CqX68znUV+fjRKZcNCx1KPyXsrhxPfATOVQlzJ89mSrR/B+X4TR/I/6DKiGRW3UAgwKZfT9sSjwytgxNLY4jTq2x6cZ94WdsBaCgrZmnHUPq+0Qe22oF9RGCr0yCaP2HooDx6CfnH60nIB+Dfni/wBT0Cl7uzR0hHQzG/vh0gh+ARvy78Jn1wmwgExknV09/wOzSjw8PNwGT4b4gNy0RA7+CXijTHHG47Eti5IpX3tLt5H1t/TD7xVr6xR9olZgaNsrD1wvCUJn/Muv8dC51wPr8He95nNi8PoZ7Cfr7C0qKZWP5rUUP8pGrvsv7qHl6SwSHmoAAAAASUVORK5CYII=\n",
33
      "text/latex": [
Markus Holzer's avatar
Markus Holzer committed
34
       "$\\displaystyle {\\partial_{0} {f}_{(0,0)}}$"
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
      ],
      "text/plain": [
       "D(f[0,0])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f = ps.fields(\"f: [2D]\")\n",
    "first_derivative_x = ps.fd.diff(f, 0)\n",
    "first_derivative_x"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This object is the derivative of the field $f$ with respect to the first spatial coordinate $x$. To get a finite difference approximation a discretization strategy is required:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
Markus Holzer's avatar
Markus Holzer committed
65
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAIIAAAAsCAYAAACpFWBjAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAFqUlEQVR4Ae2c7VHcMBCG7zIpgJAOoIMkdAAdQFIB0EGY/IJ/DOmAdMBAB5AKQugAOoChA/I+iuWzJdln6c4cp/POCH2tZOnd1erzGL+8vIza6OTk5Fz5zwXPqeI23FZsZfJyweddm8TUyTPlr8tdyB3IbcsNVCCQEz6tiqD+InyUAGW4lbuRG2iCQDb4jJumBmk7o/9a7oPCw3QwEb4J5YZPm0XYUY8fBiVwNGASzQofzyJI8Jg7OolFeJK7w1f6ofyVp1zx8RTBSlodZjtxJP+nTRv8CQK54ROcGtTJjaLLjYtD8exW+CYIzTGk+r/Psbq5VVXp90Lxie1QG55Bi6ACu/rIpfxx6GNKZ/pg/VACofCa0r7KHSr8OVTOTRMf21PoUe6j4kcmVvmjtHO5ztOSeGnHbzn8rrSnckyBnUi80fh0qthhKvoyV0xVZxDP9863bXRLgQcbqfpF43bk79l0hT8pbM8YOglAZVCmR/lm6pG/LXctx/qkSijkmZynJFUmGxbfs8KdFNGWSfCj8Emof6R+9IVpEM/g1KCG04imEcIo5rSxJDX6Tg6BBpWnZKwHqKdqUQijDHZaMtyK2/ROClb/RG+xKHxSWtEXpk14NinCFzX+T0MHEFYpwAae1mSVR9gI1lUcRjMgu2SUwU1cYLxXfFL6FYmph6enCJUKPYtQ5CGsWalpdD+p4vVA5SilO2UE2PpPeiV8UjoSg6mHp6cIaoEZkepwaNQzkhFWX4QShDqE8vHtt0CLxCel/yFMPTxDi0VG3lXDFxESlcxKTXVQvztd8K0+lS+2L9H4aFChPHaH1PY9dlyh/reVsXkxmHp4GkXQx9kOfZPPToD579TW7vg0EmHNRHRWjoYzyu+cytw42fClAuRUHx+dFR+Vp0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"text/latex": [
Markus Holzer's avatar
Markus Holzer committed
67
       "$\\displaystyle \\frac{{f}_{(1,0)} - {f}_{(-1,0)}}{2 h}$"
68
69
      ],
      "text/plain": [
Markus Holzer's avatar
Markus Holzer committed
70
71
72
       "f_E - f_W\n",
       "─────────\n",
       "   2⋅h   "
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
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "discretize_2nd_order = ps.fd.Discretization2ndOrder(dx=sp.symbols(\"h\"))\n",
    "discretize_2nd_order(first_derivative_x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Strictly speaking, derivative objects act on *field accesses*, not *fields*, that why there is a $(0,0)$ index at the field:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
Markus Holzer's avatar
Markus Holzer committed
99
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAD4AAAAYCAYAAACiNE5vAAAACXBIWXMAAA7EAAAOxAGVKw4bAAADhklEQVRYCd2Y600bQRCAjZUCrKSCUEKADqADp4RAB0H8gn8IOoBUwKMD6ICEDqADEB2Q71vfnvbWd4f3MLKSkcazj3nuzO4crL2+vo5yODo6mrC2Da6Dt8zvc55/fT7OAyBIAz4ADfYc3GTtCvpfwVqa8SrTB9D9NErmP5lvQb+n60PG6DhD7qWSPWYex0PUDZbJM26md3HGUk/BzE9Z/5Yulo6RP0HmM3gB7oJW10ogD/wBLy7zLFRzM/NeRw3WoA3+N3gLrgQ+pVYJ0Mx2wTMbW12bb62j20OzknwsPcSVBa2vjcBdwCmzsgHqnHDH2jXUF/7RhYGwg9xjFfRAFcsTyx+3m8q5vWgCR82UQfsonTOv9yJPH4XfgzRo9Vg1dovnUj3ILBXqwHHEoEdQnWwAa39Y8GHbY9x3HRpy6QQ5Pxj2oafp+qrG4XHDmfjCNtpY4pR3U7ickbJf9FsxQue9hseuEflm3Ev8RbctuYZ4xw34hU3LsA106Jr9cO+htiXhCfzCvOvAAhM/oQ126Wfdg/f+128I41Iboz4Z9k7BMzBc1TEDs2lgrdlg37speMdHzHXyCRrL9oZxuCbud4DdoA4q5UFW+zvQ2j7jYhsLylzBFw50jFF7qnA3I3O/ZvM+cUzB1EnH2+x7eF1gxruqSX3hUBPhITbelKli0NeJGTcToYQTw2HI3pSBGQ+fqswNzgzl2VM+lDO0DTZZ7DpYHUkPsthGoV8hUePKS7PaaFMoM5Bf4AbjGKhBt4FtKlZOYz9xai7j1V5+6MU2MFgiYwJ2wuOGA/Zne6t/hRlEhK+s5Y7FvZQadJfxUAnoqbOaCJrd1F6yNTfsszHHXC20yRjPenzVRzjm15nYB12HYNCxKnJ5vwu69CqX68znUV+fjRKZcNCx1KPyXsrhxPfATOVQlzJ89mSrR/B+X4TR/I/6DKiGRW3UAgwKZfT9sSjwytgxNLY4jTq2x6cZ94WdsBaCgrZmnHUPq+0Qe22oF9RGCr0yCaP2HooDx6CfnH60nIB+Dfni/wBT0Cl7uzR0hHQzG/vh0gh+ARvy78Jn1wmwgExknV09/wOzSjw8PNwGT4b4gNy0RA7+CXijTHHG47Eti5IpX3tLt5H1t/TD7xVr6xR9olZgaNsrD1wvCUJn/Muv8dC51wPr8He95nNi8PoZ7Cfr7C0qKZWP5rUUP8pGrvsv7qHl6SwSHmoAAAAASUVORK5CYII=\n",
100
      "text/latex": [
Markus Holzer's avatar
Markus Holzer committed
101
       "$\\displaystyle {\\partial_{0} {f}_{(0,0)}}$"
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
      ],
      "text/plain": [
       "D(f[0,0])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "first_derivative_x"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Sometimes it might be useful to specify derivatives at an offset e.g."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
Markus Holzer's avatar
Markus Holzer committed
130
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAO8AAAAyCAYAAAC00IxIAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAMbUlEQVR4Ae2d7ZXUNhSGZ/dQwIZUEOiAJBWwdECSCiAdkMMv+MeBDiAVBNJBkgr46CB0wLIdbN5Hq6vIsuSRPeP5WEvnaG1LV1fSK90PyfLsydXV1WpdeP78+V+i+U3XT+toW35DoCEwHQHJ2JlKv1P8SfeXQ5xO1gmvGMDoL13fDDFqefMgINxfi7MN4ot1AzpPKw6T603FRv26J8SRu++Hxvt0aFhUEAafdW2COwTUTHnC/aVY31b8Q/Gx4rliC0LgJmOjvuHh/qb4cWiwi8IrBkyWO7rCpIX9IMAYILgI8AfFvxVbuEbgRmMjuftT3fxTVwxoNmTdZhW4I2qkHrP9OVuyJc6KgHDHyrLX8I3uzW2etc5jYb4kbNTXfzUuL3Xteb8ly8ukYX3VBHd/M/qBqmbJ0gS3PwZLwgbPF+E9S2HoWV4R4Y5A/E1K3J7nR8Djz+TE8l4osv65UPqvui46LBUb9Rsv+ENvDrDbHMdnz559VXwZp7X7Lka7wENjcKX4pFSX8h4q3inlb5o+VPemvDctr7ZdDbVPebNiM6X9Q+1dx09lzxWvFDvj3XGbJdlPJOGY5xeLVvF77rzGgT0HQnaDSvl4R5e6hmWN7s9IVxzcoXRc/Z+hMsp7pchrqoMKatNobKZ0YAibEr+hMsqbjKfKMg/wwHj7EMKtcHd9g2vGDldbZyXA7PiR93wrjQMD1glKQ7k+0PUny9A99PYaqbc2Mrr4WlnmnehYQlW9cRAddf+jWNUG3x4OI/T6Gbc1uR+FTVK26lHtOQg8k8ZiUBkPlPQleUF4fYPRamFSQNDCXhD4UbUGq5q0AO3bsYgaOyb/J10fJrTFx5oyovlb0W2W6OomTJGhMjzN90M0W8gbhc2U+tSPg8Azabt5YX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LwQ+oIwYFu/hYZAQ2AHCEjeeL2G0Qw/m5yrdlB4KSBGfKTPRwAwbKEh0BCYEQHJGTvvvyve131vnRtX/R+hQaovjWjuKAAAAABJRU5ErkJggg==\n",
131
      "text/latex": [
Markus Holzer's avatar
Markus Holzer committed
132
       "$\\displaystyle \\left( {\\partial_{0} {f}_{(0,1)}}, \\  \\frac{{f}_{(1,1)} - {f}_{(-1,1)}}{2 h}\\right)$"
133
134
      ],
      "text/plain": [
Markus Holzer's avatar
Markus Holzer committed
135
136
137
       "⎛           f_NE - f_NW⎞\n",
       "⎜D(f[0,1]), ───────────⎟\n",
       "⎝               2⋅h    ⎠"
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
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "derivative_offset = ps.fd.diff(f[0, 1], 0)\n",
    "derivative_offset, discretize_2nd_order(derivative_offset)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Another example with second order derivatives:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
Markus Holzer's avatar
Markus Holzer committed
164
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAMIAAAAYCAYAAACoRCJ4AAAACXBIWXMAAA7EAAAOxAGVKw4bAAAFqklEQVR4Ae2b7XEUORCGB5cDcEEE+DI4IAOTgSGDwxkcxS/7H2UyACLgIwNDBNyRwZEBlDPwvY8YzUra0aw09LLaXXdVW99vt7rVkmZmfefm5qZL6eLi4kh1J+Jj8SeVv6Z9asrWeDWy97Gvtb2t8Vr0yUGqlCZNALwQs/jfiB+q7oPSWWSNN0uJPRpkbW9rvFZdcSc8ETRpToIXSp+HCqv8t8qPlD4J61flrfFy8iTntdqu+/aXKvt8bshO1lvb2xpvzOit+C49ETgJnvUGCPXmZDhV/Z9hZUHeGm9JpHS6VOVd8TvxMzEn2r6Stb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KKrJ8/UYnqJcUBAE0XXJGi9Rit03pyu6pHqmZQ+3pLdvGMHwTWNjSgPPY5inhfYulmuNFwhu0nf+ahTomc/KOP55Ymz3W7qm5JF+ttTgqS/fBDixIO6Y71xu+Q86slgHqpHjB1WOwR7RRuBxKlKCFd452gbfVQVC76GXSocHQ02SPN8Y5i6EUjze4BxJjlvkSkdPBNUTkGOBOikHXDEyQpocE3REHr/UnE2SzQ8Hp370OBu7gYHN+y76x5xSgwULhg9qf4ifq272blaCpz58ByAIkMd7+2zgqY2fG/CxKOqjsl/oS3qrjeeOz+K/lB+CbGqM+jpSn1F5vr2lVLoStGdiNjDmzKvU4buQ8lVUgqc+zfuu4z/Udo3Pz89PxJdz5qVxpzXj1P9IfFUz5rZvfs3Jlhvx3ZyrUdWOsYnO2oF4I8FVZ+yKlFVJ/dklx95mZMeogVOGHfaWDCywKd/tZCDgDxmUxcn1yD1TFPqInzgUX/HUlyOfn1hEV7BCWbfdMhaQPX+/73b9mObqsq45rhN7XTpvE+467Zti/w9+uITdWpTEXgAAAABJRU5ErkJggg==\n",
165
      "text/latex": [
Markus Holzer's avatar
Markus Holzer committed
166
       "$\\displaystyle {\\partial_{0} {\\partial_{0} {f}_{(0,0)}}} + {\\partial_{1} {\\partial_{1} {f}_{(0,0)}}}$"
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
      ],
      "text/plain": [
       "D(D(f[0,0])) + D(D(f[0,0]))"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "laplacian = ps.fd.diff(f, 0, 0) + ps.fd.diff(f, 1, 1)\n",
    "laplacian"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
Markus Holzer's avatar
Markus Holzer committed
189
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAeYAAAAsCAYAAACwlxupAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAK40lEQVR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VEQA57D0QEX9NbWqusc8ldwB8+FkPMbhaITp+BpEY2ARfuI+/BMm0CGOM2W+IkXDbPLQQL+ClxMBEaiUAM5n6YFK+y622jLMscSWkTf3l/mhEHvWy5O163hFzTiGO4d8zex5F6ENERCB2ghID9TWYxPrK8M8EdyBs/G+0iWMq/1kNWfEn7t1gcyd7r62RUAEVkNAemA1XTncED38NcynlFReKfeexoYB5pI1/cdSKql6iIAIZCUgPZAVbzmFyzCX0xfOmsAAc1maBpgPfXUdl6b54RF7Ft2V0bYIiMAKCEgPrKATI5ogwxwBayHRpzwuTszejBlR5/DNx0SQxs9w0njLiYAIrJOA9MA6+9XZKhlmJ5aiInlfyTbKrCBn0mYZ+x4Ms/1QGGXkREAE1kFAemAd/RjUCj38FYRpUSEaYL4WZTu+/vQMBvkUXq9C2XS0LwLrIiA9sK7+HGzN/xb7P/aQN5W+AAAAAElFTkSuQmCC\n",
190
      "text/latex": [
Markus Holzer's avatar
Markus Holzer committed
191
       "$\\displaystyle \\frac{- 2 {f}_{(0,0)} + {f}_{(1,0)} + {f}_{(-1,0)}}{h^{2}} + \\frac{- 2 {f}_{(0,0)} + {f}_{(0,1)} + {f}_{(0,-1)}}{h^{2}}$"
192
193
      ],
      "text/plain": [
Markus Holzer's avatar
Markus Holzer committed
194
195
196
197
       "-2⋅f_C + f_E + f_W   -2⋅f_C + f_N + f_S\n",
       "────────────────── + ──────────────────\n",
       "         2                    2        \n",
       "        h                    h         "
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
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "discretize_2nd_order(laplacian)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Working with derivative terms\n",
    "\n",
    "No automatic simplifications are done on derivative terms i.e. linearity relations or product rule are not applied automatically."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
Markus Holzer's avatar
Markus Holzer committed
225
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAQUAAAAXCAYAAAAC/1q1AAAACXBIWXMAAA7EAAAOxAGVKw4bAAAIOElEQVR4Ae2c/1XcOBDHHR4FkFDBcR2QpIJbOsjRQaADePkL/uORDrirgCQdJKkgCR0cHYRsB9z3o/Uotiyv7cXyYrzznlbSaPRjvh6NxjLJs/v7+yyk8/PzHfFmSntKX1S/CWW61jXGntJt135Dy6fQfWgdxjLfBuv1P6nYvtwKlyUhnME7JRzBP0qvxPuofGVS/xN13l95gIE6ptB9oKWPbpoN1ukfmTCu7DnxdpQ47I04rNmfnp4VIwU6qOWd8lMvoULe6bXyv4v8NmX1eSM5+pbGbNN3SBmtr3fdw/Vrjivx5jn/QnUrh6JPuj4E1k8awJbKCedfEsWuLdKnDL0s2p7KOIW5coKALIwUiBCO1GidkYEQfiN+xfO41pqffJyKk6kRXze7V91DZYTFpXgvlK6VjpSIyKZKSbGeKqgRve/E4+Bh37KnPymVHILqmWzzvbLjfL9m2zAL9J/KH9TIQJ6o5zwM2byOb19SYCNwOiYlrY11EQY5T7fiZH3rHi4DR/BWCcfwXemL0uhoJFiPDtfYgnvA+kZjtI3u2afs1+OSU2jYVHid17HFL+EdaszjJe19NeEFSStTAt39WjQ2Tov1cWmLwx2lQ8gVetRY52t8KtmDsW4LBPav9EvptOQUGEBMTrSXShgv9E08wg4uJ1p/PVAf7hJay0t27dSX7hFFDsS71fiGaURkWqyEWE8LyH61Zb8elpyCHtRnMTFef7qrPMsfINN32eRshMYTUWMTsvxkcBGb5rt4XV5RXMeH/mjOPnV3y8lxAwcihTvV+YpD7vF1ghP7SYS1u9AWlGZLHGTY7kNeKUf/ZKQ/hzwRx64SBzsX3HX7i/16sKUfR/mDypSXDFZ1BI33Ixdvk72SEO/pUdK43AHQTiTynqQyEQpOYlDS3DiETLnp6eZXfVXdrT8hGe90PJQryuEcCIrHJS4PLAlp7NInpySTtBxUa+kda42J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h8sCWioF0ldOQLQxEX8CvWVa3U4UHIPgWswQBczDFwSIBNuKfF8kQU8fvPnNWZm1p/W+vFCgXJGNh45ptCE8VSXbLcQ2RK+5QDWficGo+CtJZedS8ohcHWYYrhYYCetI7W+Fqnjn14bnUGb0MmzfvGWuPhcL3TVZ2/T/D1XJmpYo3epTeAHI9KJtxctAWeW5XWZsaFRPAojjQIZT4JxYyNSKDVohajrfzLKRCeBCsPtqRjo+7CgT+OsQiJ+wQipgpJBkfpHkTQuHQO9ePrAbgWaWmfgiDzWWRWYHcqPgqshQGHDZ9ZvWOFJ01Idjo6xcTfYN38iDmknV2V/j+F5n4LCYFsRskfL3GZcSpedFOKz4UPrwsxp7EYcES/TbqrnU2Hrry3fVW97pNQprYoNuLX4qs2jP6r0luVfRSyrI9kHUkmOp+1P7Z8mU5qw/FyILlwWHWcA7hcq1z5NC1eVHfxJ4+1MOCw4A7CXUJvq9KZ1LlL6M/m4AGu5da7s3INHVrojpFhoOR1Fzo2CzJEUiU8l82hNk6958r5MuRpWR+E1O5OVOWjcc4NOlk0cCQ57hLQj8MpfHVAfWiD9QKH2C9YYocL4n9eSp3Ozs72lE5SzzPG8YXLFfh0WbvkZ0o7Hft0nqfL+GOQ3WBd3evC5ESpZH/JHcIYjGXda9RDuVRqvckle9Rlzcgrzbr0eaqywmGDdUMgsHEKDQANtTlkrK2dQtc1pRy761oeg3xKPFKOPRR2/wMdsuuwbt1IoQAAAABJRU5ErkJggg==\n",
226
      "text/latex": [
Markus Holzer's avatar
Markus Holzer committed
227
       "$\\displaystyle {\\partial_{0} (c + {\\partial_{0} {f}_{(0,0)}} + {\\partial_{0} {g}_{(0,0)}} + 5) }$"
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
      ],
      "text/plain": [
       "D(c + Diff(f_C, 0, -1) + Diff(g_C, 0, -1) + 5)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f, g = ps.fields(\"f, g :[2D]\")\n",
    "c = sp.symbols(\"c\")\n",
    "δ = ps.fd.diff\n",
    "\n",
    "expr = δ( δ(f, 0) +  δ(g, 0) + c + 5 , 0)\n",
    "expr"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This nested term can not be discretized automatically. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Only derivatives with field or field accesses as arguments can be discretized\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    discretize_2nd_order(expr)\n",
    "except ValueError as e:\n",
    "    print(e)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Linearity\n",
    "The following function expands all derivatives exploiting linearity:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
Markus Holzer's avatar
Markus Holzer committed
289
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPkAAAAYCAYAAAA4V1u3AAAACXBIWXMAAA7EAAAOxAGVKw4bAAAHIklEQVR4Ae2c61UeNxCG1xwKIHEFIR34UkFwByQdxHQQH37BPw7pAKcCjDvAqcCOO4g7sI87IO+z1shasVcY7X4bvjlHSBpJrzQzGl1Wn/3o5uamyun09HRPvAOFfYV3yn/M66w17y2bN95a9TrHuEvougTmHLqY0sdOXllC49zHCjj2a4Vn4l0pXj15y+aNt3oFFxSghK5LYBZUwZ2hH6U7uYRmBz9W/CpFVP4P5Z8r/jXlryntLZs3Xpsu1ceF+F9D2Znylm6r/r/lldB1CcxNNUC+k7ODvwwKSMfMjn4o/pOUubK0t2zeeA11StfnYvyocKnwUoET1kOlErougbmR9smd/F+N8o0mWGPHCHl4i0w09X+gwES/D3nL5o2Xy4a8ODiO/kHhncIqycF+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1o2jlgaK03LXeJoIwODb6IuYtkpSzpNBPFLCgITDSkbdOclbNm+8qAvpx5w23xXQHWX5wjnWHrEPJaa0YbFJr2EpzhzpErr2xqw/Rgf7mE7wGyh+qF7SfruMRAPg6Sa+y8IL9JP4+cSysjTGwePkUZsjBe5zODXPaBDls/8jC43BWzZXvFoz3//YaSju1t+LKpx87ALZsEeC0Zdsa4Pt6XcR8rYdQnhjCo97Nwsh851F8bECdsp/zLSY/Won14AqDZCvgI0vgfAz6nJ4HLhxhBceK+ZGUAHZXHWVKInJ0mUDdJzrP88b1C17WEELhhW1tRm7qBiGe+xtOwbojSk8FuW4MCvP+3jM06cI/eb2yvN1xVC34U9W0IJhRZ3227EaY2IN3u6kbav7rSPmGMyRdVBGl0JGQvRX85ZtLJ7q8WZ6FUbHffyyY6ToHkNGGttHbKDExDbYuWuypbBD6aL2myjT0Fjr8jGYqvNEgVeIaBd4AiDkH0gXs98kJw/aOVMcP8ZIKNK8EXpMhtBFMxI2z3Fz/DTQW7YxeFxp9iRfPVEUt+7k4rOIti2uvX2Aq0AfKfW2SSrSn123Eva0pPqfw35jZZoy+CHMY4GlvoANuY+/CvaKfYX8IvZr/KcRcUQDiWTS8GOYnxUQitV69eQt2xCeynnnZnKgR+51nYulyvhay/eORh3lzYlv2UNl7Cp/K/yudFxA+tqobk2q09qflW9aPEamqWPuw1QZDn6kYHdx7MjOnh/V627Fb9XnQB/3tl/F/wyzDevQwcnJyYHC+V3spXaHU9qp/p7C9ZQ227r980j6XMR+dzmu16vS9s/8Ggg7BMfvtmNf54BUnx2ndXfpbPTthzfsUlty0sBS9ts6uZMB54LRRMHxOLJzNBxL/CR49HVKdblCcOxsXAvGdrat160B6XR++22PWP1HrE3VD8fpUmMriV1qzGvDLanjHPs/3HftqL6VlNMAAAAASUVORK5CYII=\n",
290
      "text/latex": [
Markus Holzer's avatar
Markus Holzer committed
291
       "$\\displaystyle {\\partial_{0} c} + {\\partial_{0} {\\partial_{0} {f}_{(0,0)}}} + {\\partial_{0} {\\partial_{0} {g}_{(0,0)}}}$"
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
      ],
      "text/plain": [
       "D(c) + D(D(f[0,0])) + D(D(g[0,0]))"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ps.fd.expand_diff_linear(expr)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The symbol $c$ that was included is interpreted as a function by default. \n",
    "We can control the simplification behaviour by specifying all functions or all constants:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
Markus Holzer's avatar
Markus Holzer committed
321
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAMIAAAAYCAYAAACoRCJ4AAAACXBIWXMAAA7EAAAOxAGVKw4bAAAGLUlEQVR4Ae2b7VEcORCG1xQBUOcIjsvAHxEczoALwSaDc/EL/rlwBrYjwDgD7Ah85wzOGdhFBtz76NQ6jRjtaIbenYVFVUJfrbdb3eqWRsCj6+vrRZlOT0/31HegvK/8We1vJc2YtjfeGN7bSOutb2+8TbTJTimUFo0DHCuz+d8rP1PfhcpJyRtvkhBbNMlb3954m2qKR/mJoEVzEhyrfJ0LrPafaj9X+UfeP1T3xqvxE593GruK42/Utnptyr3s99a3N94mK708ETgJXkUF5HJzMhyq/0ne2VD3xrvBUjKdqfMX5XPlV8qcaNuavPXtjbexdikd4R9J+lGbqxNRY5u+sZvMG69PkWx+nABn+Ev5s/KdS9LxgTJruU3y1rc33m3WttK5uzm6DEHkr6WfGnheG+zr98YrebB51LenzAc9jnonnSCui3WQJydvfXvjTV7YGiZ2HAF+WjxR6akyG4v0VX2fVO4rf6djTPLGK3i/UPu7eJisxfD2Nb317Y23qRbpOIIWfRk31pEJrL78yB7lCN54mUw4K07AifBTfHjVokxyq711yVvf3ngYRJiHKrhZ/KCtRJBljy27jQTCVf7YMfC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TOLl80JjNdqMcIWrmjcr0YahFUed9eOpGaMXjNWHPFKqy90RQPw7Z56hL+YCrDI88LZ2TEcKPv9ScnMSbPxxcx58ZtK6pdS1DeMcCyvcLDsH3wWuttxM8Y3sW23X+Mad15RLYNgy/UPstLmpyNGvBEw2/B0CJ8OOuWXU8jfEScVTSqF2VW2NEqC/KL1VPTrZsjmhDEk0vPxvftLJlTWNkXoanMZzgSNm+DbAhJ0R5LQos1d+rywEet7bdgv9Qu2/55OTkQPlsyro073DMPNHvKV+OmfNAW99z0uUstptyNQqeu8k/YrThqtN3zFZFFz3RqzdSVSfpdNIYEe8hOWhgLtvdS0fAHlIom5PrEUdxa+JPHJqveKLlusYxX72mtTJ+oPtfA9Ln+m13349pri6rWuMqsVcl813CXaV+S+x/AQ3mxxrhRin1AAAAAElFTkSuQmCC\n",
322
      "text/latex": [
Markus Holzer's avatar
Markus Holzer committed
323
       "$\\displaystyle {\\partial_{0} {\\partial_{0} {f}_{(0,0)}}} + {\\partial_{0} {\\partial_{0} {g}_{(0,0)}}}$"
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
      ],
      "text/plain": [
       "D(D(f[0,0])) + D(D(g[0,0]))"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ps.fd.expand_diff_linear(expr, functions=(f[0,0], g[0, 0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
Markus Holzer's avatar
Markus Holzer committed
345
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAMIAAAAYCAYAAACoRCJ4AAAACXBIWXMAAA7EAAAOxAGVKw4bAAAGLUlEQVR4Ae2b7VEcORCG1xQBUOcIjsvAHxEczoALwSaDc/EL/rlwBrYjwDgD7Ah85wzOGdhFBtz76NQ6jRjtaIbenYVFVUJfrbdb3eqWRsCj6+vrRZlOT0/31HegvK/8We1vJc2YtjfeGN7bSOutb2+8T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dulRnH6Gz8vtgFttxIpCI+p0jW4KysbnXPc0WgPB9iV+2WSRh3Buvw1PycPogSxlhFnEMx8xTq9xT5+CQ4WqYA6yx7q1vb7zwyBJtY2phb5HSA8yctttFEgnAc2R6j6cvpl/VX24qG8tLnCBtNm+8nFGs2+mTon5Gg5PgmC2pI3fLBNH0zUFHdjVshPEj89b3CvD4DiBQ8C1F0HisjI3KX4bOZrvgCBJoIQH5cu98vdNfpJpT4ASd65I3XiEHSq3JiiylnGXb4G7IbQM9GDbUN6fV8QzDvfTW9wrwCFopcAmf3x+kdlQIui1tVbYjaQi8nT1nAz0YNlS13Y5RtJQS3r4n+qLfjWvKEOYYPNHyrnwRMfk+OK/gIyMLTmkMH5s0cg76qBnFIIdKDF4z+tDcpvGRaxrEbMETzRNlXtaSTegTOLl80JjNdqMcIWrmjcr0YahFUed9eOpGaMXjNWHPFKqy90RQPw7Z56hL+YCrDI88LZ2TEcKPv9ScnMSbPxxcx58ZtK6pdS1DeMcCyvcLDsH3wWuttxM8Y3sW23X+Mad15RLYNgy/UPstLmpyNGvBEw2/B0CJ8OOuWXU8jfEScVTSqF2VW2NEqC/KL1VPTrZsjmhDEk0vPxvftLJlTWNkXoanMZzgSNm+DbAhJ0R5LQos1d+rywEet7bdgv9Qu2/55OTkQPlsyro073DMPNHvKV+OmfNAW99z0uUstptyNQqeu8k/YrThqtN3zFZFFz3RqzdSVSfpdNIYEe8hOWhgLtvdS0fAHlIom5PrEUdxa+JPHJqveKLlusYxX72mtTJ+oPtfA9Ln+m13349pri6rWuMqsVcl813CXaV+S+x/AQ3mxxrhRin1AAAAAElFTkSuQmCC\n",
346
      "text/latex": [
Markus Holzer's avatar
Markus Holzer committed
347
       "$\\displaystyle {\\partial_{0} {\\partial_{0} {f}_{(0,0)}}} + {\\partial_{0} {\\partial_{0} {g}_{(0,0)}}}$"
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
      ],
      "text/plain": [
       "D(D(f[0,0])) + D(D(g[0,0]))"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ps.fd.expand_diff_linear(expr, constants=[c])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The expanded term can then be discretized:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
Markus Holzer's avatar
Markus Holzer committed
376
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAeUAAAAsCAYAAABboKCqAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAL5ElEQVR4Ae2d7ZWWOBiG1WMB49jB2IEfFezYgR8VOHagx1/6z+N2oFuBqx2MW8GqHWgHjtOBe99sQAgJkBDeBLhzTl4g37mSPA8Jgffqr1+/rqQwr169OkE6z01ad3G84DXcvxq3wQPCvUWASxPoNa7r88F4e/EUH39Li42fzaF90Baz5MChy6v8RKA0AtdSFMgMxLc4PjX2DtKlMv6C69OxPBDmDcIcw76HPYMdjYMwuzHi429qsfGzObQP2oIKOVoOHLq8yk8ESiSQRCmjYlSqT9sVxADlrJmz3Q9td885FTEVMhXzZ9hPsKs0qPcpLOuT0myCj9ik7BJFpjVXDhRZqZhCLdTXY4pSVBxxGW+O6+NBJoXgzPYbgN+AbS87U7k+gNsJ7HdXSnBn3CPYTybuahWyqR/rQpvEbIyP2CTpFcUmEi0Hiq1RfMGS9vX4YhQXU1xGmiTVTJmK9LtRqq4sh5TU/ZG4rvT25CY+/tYWGz+bHD5z5ECO8ipPESiOQJKZMpTxQ0/NbtMd/r3NXnDjkiyFKu+uL3DNZW4eO8vgcNulER9/s4uNn01OH7RLjBx4gDLfg/1hyv4RRz4Cemeud3sAA7HxtP6W2SRRyi5ugEaFfAJb78juBDOD7h2O3P7N3dZ/dgLs/EJ8/B1AbPxsSvNBW3nlAPz4DJoKmBtDr+BIefEFlvtKdq2UxQY9wGO2zuaap94pnDnz/QiAXmVrBiHz8j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YWREQAS2QUByYBvtOFoLbfQaRZQ9AJUvX32yDV9xegxlfAqr151sOroWgW0RkBzYVnt6a/MfM2R8O7RQA3cAAAAASUVORK5CYII=\n",
377
      "text/latex": [
Markus Holzer's avatar
Markus Holzer committed
378
       "$\\displaystyle \\frac{- 2 {f}_{(0,0)} + {f}_{(1,0)} + {f}_{(-1,0)}}{h^{2}} + \\frac{- 2 {g}_{(0,0)} + {g}_{(1,0)} + {g}_{(-1,0)}}{h^{2}}$"
379
380
      ],
      "text/plain": [
Markus Holzer's avatar
Markus Holzer committed
381
382
383
384
       "-2⋅f_C + f_E + f_W   -2⋅g_C + g_E + g_W\n",
       "────────────────── + ──────────────────\n",
       "         2                    2        \n",
       "        h                    h         "
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
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "discretize_2nd_order(ps.fd.expand_diff_linear(expr, constants=[c]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Product rule\n",
    "\n",
    "The next cells show how to apply product rule and its reverse:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
Markus Holzer's avatar
Markus Holzer committed
412
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAH0AAAAXCAYAAAAm70AZAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAFwElEQVRoBe2a63EUORRGBxcBDEsETAg8MrAzYMlgxxmY8i/7H2UyMEQAJgPIANYZLBngdQbeczQtoVa/24zNVs2t0uh179Wn+1Cr235wc3OzKOn09HTJ2D5lRflC/7LkmdpHx4ryfarcjv92Fmiz+16pEiadfUzR0e8ozxm7oJ5NyB8h/HS2gp3gbSxgsmn/RA/yTGfSDD+mfp04aFRCL6j/zMfHtJF5CZ+yNZ1dsvCdM3ddzb+hH9tdIrvxAQtgQ51+TW0SL8pMN8PXTOr8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M2sx5T5C0/KomlXLPswxJI3tJ21VRDRxxvk2nFHAWq2hPpMDjdATbhzTPdPh0rCZ64OSP9spiv9eH1HrCgPLVd8OZdjdi1rsZwg4nQNRpDFJoh4/5aHYEugzMFKH3fz1O/WvM+cHdiFhM4w+kk/r0K5JTqDcxGTCCU2PYVps1I3mBrjw2F4PWDRTwFfJ6Xt2HZ5DNYAtgw8POnFwNyS4pr59QrkzPSds3ahyX0Gby+GqUgdAw+S+3tgvH7wN3ADK6cfIQHm0x2Ohvy3dwPMh5xfqTwkvcXpUHM+0nSP+KXjnNxHRMMSN2V6eo0s2vy9IcwyL+Gz4tooBEykdW67fQ5ZjwPdrH7PPd7RdtJdte42zADL2W5fgifiB+G0Yk/CBs1Y8mgMKsFFSk4nY51fDOIc2Utj6dFbc0+DJUT/GtdcrpK+2Scl+CJgVieXDGbDSaf5fLp8PJoV410Z7h7MG+QbE6inyeu/zmz7XJycrKiHM1dB9lzdUyRh3+fspwiI++ctbrWmKNrDu6+dZg7otRst3WHdxlk6jjAzyijnQjvesYaa+T2p8r18W8bt/ucivl/43QNy+ZGO73PEV1z29K/Lb1zbfIfzLr+GVLUm+IAAAAASUVORK5CYII=\n",
413
      "text/latex": [
Markus Holzer's avatar
Markus Holzer committed
414
       "$\\displaystyle {\\partial_{0} ({f}_{(0,0)} {g}_{(0,0)}) }$"
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
      ],
      "text/plain": [
       "D(f_C*g_C)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "expr = δ(f[0, 0] * g[0, 0], 0 )\n",
    "expr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
Markus Holzer's avatar
Markus Holzer committed
437
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPQAAAAYCAYAAADNqdAHAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAGe0lEQV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zjB/LJ1HSvrvrIS5tft29Uvp+yusYe259Q1p+yh81yHP6f+OWWvM9dUn9x6xvL/B4h2hVR2GmXBAAAAAElFTkSuQmCC\n",
438
      "text/latex": [
Markus Holzer's avatar
Markus Holzer committed
439
       "$\\displaystyle {f}_{(0,0)} {\\partial_{0} {g}_{(0,0)}} + {g}_{(0,0)} {\\partial_{0} {f}_{(0,0)}}$"
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
      ],
      "text/plain": [
       "f_C⋅D(g[0,0]) + g_C⋅D(f[0,0])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "expanded_expr = ps.fd.expand_diff_products(expr)\n",
    "expanded_expr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
Markus Holzer's avatar
Markus Holzer committed
462
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAH0AAAAXCAYAAAAm70AZAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAFwElEQVRoBe2a63EUORRGBxcBDEsETAg8MrAzYMlgxxmY8i/7H2UyMEQAJgPIANYZLBngdQbeczQtoVa/24zNVs2t0uh179Wn+1Cr235wc3OzKOn09HTJ2D5lRflC/7LkmdpHx4ryfarcjv92Fmiz+16pEiadfUzR0e8ozxm7oJ5NyB8h/HS2gp3gbSxgsmn/RA/yTGfSDD+mfp04aFRCL6j/zMfHtJF5CZ+yNZ1dsvCdM3ddzb+hH9tdIrvxAQtgQ51+TW0SL8pMN8PXTOr8nGR+yfikbK30NIIoV5y34T+j/wflA2VN8dTZ0S0tgF3fouKw8kfD6f8w+ZHJWnZVfcemOkEnmrljSUfrcB3/jfKFsqNfYwH9oD8WteO9TzeONyAuqUcf8fD+S3nUpzfOwWdAfaY8ol0Lusizq29nAf2BhicPSzVMmG3PKNHwXxn7RN+b/OjbNzI+y0fzw3sgP3JxXbo7+sUW0B+vak7H4Gaahj+Mi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QrkzPSds3ahyX0Gby+GqUgdAw+S+3tgvH7wN3ADK6cfIQHm0x2Ohvy3dwPMh5xfqTwkvcXpUHM+0nSP+KXjnNxHRMMSN2V6eo0s2vy9IcwyL+Gz4tooBEykdW67fQ5ZjwPdrH7PPd7RdtJdte42zADL2W5fgifiB+G0Yk/CBs1Y8mgMKsFFSk4nY51fDOIc2Utj6dFbc0+DJUT/GtdcrpK+2Scl+CJgVieXDGbDSaf5fLp8PJoV410Z7h7MG+QbE6inyeu/zmz7XJycrKiHM1dB9lzdUyRh3+fspwiI++ctbrWmKNrDu6+dZg7otRst3WHdxlk6jjAzyijnQjvesYaa+T2p8r18W8bt/ucivl/43QNy+ZGO73PEV1z29K/Lb1zbfIfzLr+GVLUm+IAAAAASUVORK5CYII=\n",
463
      "text/latex": [
Markus Holzer's avatar
Markus Holzer committed
464
       "$\\displaystyle {\\partial_{0} ({f}_{(0,0)} {g}_{(0,0)}) }$"
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
      ],
      "text/plain": [
       "D(f_C*g_C)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "recombined_expr = ps.fd.combine_diff_products(expanded_expr)\n",
    "recombined_expr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "assert recombined_expr == expr"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Evaluate derivatives\n",
    "\n",
    "Arguments of derivative need not be to be fields, only when trying to discretize them. \n",
    "The next cells show how to transform them to *sympy* derivatives and evaluate them."
   ]
  },
  {
   "cell_type": "code",
Markus Holzer's avatar
Markus Holzer committed
501
   "execution_count": 18,
502
503
504
505
   "metadata": {},
   "outputs": [
    {
     "data": {
Markus Holzer's avatar
Markus Holzer committed
506
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAGYAAAA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rWXucp/gT45gl4d+gKkIN88hJfZF8YpCdRtwX5UgnLvkvW+clXipXEjw/LfRTPljAbl3i4Mjexk+BWpLLAfB4tnr9nyPHDZN+J1zNifXOSrEjrBi9xvg77ACHGjevh1GX7BumVTfesIH8vfIH0Mnzoog8+UM6aA3pFxw+VzcKt6s8ko0gE6UEfWjwAz44BWgikp7grAJ4LBn+41nqXAD7SaxwrtXBbQVec+oc+Zwlhna6MF/dQzSnezAwON/ENmNbCncL0ewiXqAp0QDo59+SYlY6HovdyFuEe+3E3v8JHb+t2o2ZtNh34G0MrPTKxpL20hTD2jdrzeTSzu7hgzI6d2I/xP1iwzdYY0ZLmTWe00y9JhxmgPTH2zsA6IkLPfRp/PDJ0NNjPcChLlPRQ8Yi1sOPWMlvbL5BiY+cXvD4kKDI8r7FfWSuOVY/cDdEoYNdb+YiC/LhD/IjaUQTMw/K3I52LeC5wZ9X5W6BzvlxoMxoCpZ7RCnS78dXlMOzs7+047KGmY39KW5Zpj1q5oLdwa5yHm6HRMe/UQstfJRKcl7UqcnXDviN5o1qsJiHDHPqXrjHHfF5yX+9zAzLfNDawa6Rux9X/MUC1xQiIG3w9M/0fJP5/UPMA19S1RXY6r0f+3c+xjefSTIT290x0z1hoQ69mh4IvOu6bvrT+Uz7bj+ZDI1WVSxoy1UBMN/tEWL5axLLYaH7v4edC6Dv4FMIw+Qo3ltiAAAAAASUVORK5CYII=\n",
507
508
509
510
511
512
513
      "text/latex": [
       "$\\displaystyle {\\partial_{0} (k^{3} + 2 k) }$"
      ],
      "text/plain": [
       "D(k**3 + 2*k)"
      ]
     },
Markus Holzer's avatar
Markus Holzer committed
514
     "execution_count": 18,
515
516
517
518
519
520
521
522
523
524
525
526
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "k = sp.symbols(\"k\")\n",
    "expr = δ(k**3 + 2 * k, 0 )\n",
    "expr"
   ]
  },
  {
   "cell_type": "code",
Markus Holzer's avatar
Markus Holzer committed
527
   "execution_count": 19,
528
529
530
531
   "metadata": {},
   "outputs": [
    {
     "data": {
Markus Holzer's avatar
Markus Holzer committed
532
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAEMAAAAVCAYAAAAdHVOZAAAACXBIWXMAAA7EAAAOxAGVKw4bAAADHklEQVRYCdWX21FbQQxADUMBhnQAHfDoADqAdBDoIJn88cdAB9BBJnQQlwDugJRA6MA5R1ntrG98CWPwcKMZWVppH5JW0l2vzWaz0arg/Px8zN5fy/7bhX5C/rSqM1+z78ZrFr9g7SWOn+U8+Gv4e3AnZUOi6ys25pQAHDZnXMJvI9ttZINhVx0Ms+JuMN7+w5C1VfaM7tlkhJlxDB1kmdSegYE2uKxvG59ja34CrcBY3XdwH3x8qWPMszSOwT3wXQAb9OlLOTzsd4x8qiwyg4EOdpudhuv0Cfpb6BwgsxHeQTOAc/p2wByNsHm617t8SdIG6FHaBm+mfgaP4CfrRXEKtdkZgITMiPw0pjypN/0jB32UPeM2oB745LjI+pb0yll3CGrrMqDjcxfHXmaJl+OljzIYponCemtMrLwTW9CoMs6AterKM89AmBHX8LsivAY8gsvAmEXiMqDND9jQXa8PY22NngGjYLM9AVlmic50wVTzlnsDVhZYSh4urcC6uRuqitUy+uiF9Nk8rg20tYMFRjHSCv6m1RVefc0K5hi4A1DHbUhxIHQuwOjeDbDlpOfwePOgn84FA4EKHdUxS+evNwJzdNh5kTGM2wwyA8RFAUQ8LCj+Rk/TsuwZYSXKKXgFGsVv4D18Ohtz+DFYwgRd8FC/Ngbpp3LwfwEb5y32X2nws48uJv1ijk5uwmfqmxEfQRuhn9b4RsO/GbCnZ2TQ2323ymBRA/Yi+0qh3SP4coZftvqp3VCDoNZNZ5VlolFivjXkzQAfT3tuCr5pQNhvYYNFbpbqQNwk/FLAej/PW9AaCDfKMolaR2kW9ELRW2MX8BpsmllKEczehQNSYKsB3YHWLIKPt08GwxKwdqIUGtv3C599wPIQcpwN1gCNWO9rbrCAfV7aATSf5GmrAXqMMoHpKnXMCWbKGXwGybSyNnPMMMCyET78IcP7xWYvzEy28XffTr5sryIYMDeggnaSi+PN3rhmcC5yzHwfXgbSgD20upwzIOpfB31a9JyPnvfs12RAjoQpBNxsfXUD7fNrvU8xULnl2S3RNzP1N/jzMT/6sRbgAAAAAElFTkSuQmCC\n",
533
534
535
536
537
538
539
540
      "text/latex": [
       "$\\displaystyle 3 k^{2} + 2$"
      ],
      "text/plain": [
       "   2    \n",
       "3⋅k  + 2"
      ]
     },
Markus Holzer's avatar
Markus Holzer committed
541
     "execution_count": 19,
542
543
544
545
546
547
548
549
550
551
552
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ps.fd.evaluate_diffs(expr, var=k)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
Markus Holzer's avatar
Markus Holzer committed
553
   "display_name": "Python 3 (ipykernel)",
554
555
556
557
558
559
560
561
562
563
564
565
566
   "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",
Markus Holzer's avatar
Markus Holzer committed
567
   "version": "3.9.7"
568
569
570
571
572
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}