Skip to content

Commit 6461214

Browse files
committed
Poly in s(n) and x_0
1 parent 0a23e4f commit 6461214

File tree

2 files changed

+126
-10
lines changed

2 files changed

+126
-10
lines changed

sumpy/recurrence.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,13 +401,19 @@ def get_shifted_recurrence_exp_from_pde(pde: LinearPDESystemOperator) -> sp.Expr
401401

402402
idx_l, terms = _extract_idx_terms_from_recurrence(r)
403403

404-
r_ret = r
404+
# How much do we need to shift the recurrence relation
405+
shift_idx = max(idx_l)
405406

406407
n = sp.symbols("n")
408+
r = r.subs(n, n-shift_idx)
409+
410+
idx_l, terms = _extract_idx_terms_from_recurrence(r)
411+
412+
r_ret = r
407413
for i in range(len(idx_l)):
408414
r_ret = r_ret.subs(terms[i], (-1)**(n+idx_l[i])*terms[i])
409415

410-
return r_ret
416+
return r_ret, (max(idx_l)+1-min(idx_l))
411417

412418

413419
def shift_recurrence(r: sp.Expr) -> sp.Expr:

test/taylor_recurrence.ipynb

Lines changed: 118 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
"cell_type": "markdown",
55
"metadata": {},
66
"source": [
7-
"# Part 1: Generalizing the Recurrence:"
7+
"# Generalizing a Taylor Recurrence"
88
]
99
},
1010
{
1111
"cell_type": "code",
12-
"execution_count": 1,
12+
"execution_count": 28,
1313
"metadata": {},
1414
"outputs": [],
1515
"source": [
@@ -35,7 +35,7 @@
3535
},
3636
{
3737
"cell_type": "code",
38-
"execution_count": 2,
38+
"execution_count": 29,
3939
"metadata": {},
4040
"outputs": [],
4141
"source": [
@@ -46,7 +46,7 @@
4646
},
4747
{
4848
"cell_type": "code",
49-
"execution_count": 3,
49+
"execution_count": 30,
5050
"metadata": {},
5151
"outputs": [],
5252
"source": [
@@ -59,7 +59,7 @@
5959
},
6060
{
6161
"cell_type": "code",
62-
"execution_count": 5,
62+
"execution_count": 31,
6363
"metadata": {},
6464
"outputs": [],
6565
"source": [
@@ -75,7 +75,7 @@
7575
},
7676
{
7777
"cell_type": "code",
78-
"execution_count": 6,
78+
"execution_count": 32,
7979
"metadata": {},
8080
"outputs": [],
8181
"source": [
@@ -93,11 +93,121 @@
9393
},
9494
{
9595
"cell_type": "code",
96-
"execution_count": 12,
96+
"execution_count": 33,
97+
"metadata": {},
98+
"outputs": [
99+
{
100+
"data": {
101+
"text/plain": [
102+
"4"
103+
]
104+
},
105+
"execution_count": 33,
106+
"metadata": {},
107+
"output_type": "execute_result"
108+
}
109+
],
110+
"source": [
111+
"recur, order = get_shifted_recurrence_exp_from_pde(laplace2d)\n",
112+
"order"
113+
]
114+
},
115+
{
116+
"cell_type": "code",
117+
"execution_count": 34,
118+
"metadata": {},
119+
"outputs": [
120+
{
121+
"data": {
122+
"text/latex": [
123+
"$\\displaystyle 1.55431223447522 \\cdot 10^{-15}$"
124+
],
125+
"text/plain": [
126+
"1.55431223447522e-15"
127+
]
128+
},
129+
"execution_count": 34,
130+
"metadata": {},
131+
"output_type": "execute_result"
132+
}
133+
],
134+
"source": [
135+
"#Sanity check that recurrence is correct\n",
136+
"derivs_lap = compute_derivatives(5)\n",
137+
"exp = recur.subs(n, 4)\n",
138+
"exp.subs(s(4), derivs_lap[4]).subs(s(3), derivs_lap[3]).subs(s(2), derivs_lap[2]).subs(s(1), derivs_lap[1]).subs(var[0],np.random.rand()).subs(var[1],np.random.rand())"
139+
]
140+
},
141+
{
142+
"cell_type": "markdown",
143+
"metadata": {},
144+
"source": [
145+
"## Step 2: We need to arrange the terms in the recurrence as a polynomial in $x_0$, $s(n)$\n",
146+
"$$\n",
147+
"table[i, j]\n",
148+
"$$\n",
149+
"Where $i = 0$ represents the coefficient attached to $s(n)$ and $i = 1$ represents $s(n-1)$, etc. and the $j$ is for the polynomial in $x_0$."
150+
]
151+
},
152+
{
153+
"cell_type": "code",
154+
"execution_count": 59,
155+
"metadata": {},
156+
"outputs": [
157+
{
158+
"data": {
159+
"text/latex": [
160+
"$\\displaystyle \\operatorname{Poly}{\\left( \\left(\\left(-1\\right)^{n} x_{0}^{3} + \\left(-1\\right)^{n} x_{0} x_{1}^{2}\\right) s{\\left(n \\right)} + \\left(- 3 \\left(-1\\right)^{n} n x_{0}^{2} - \\left(-1\\right)^{n} n x_{1}^{2} + 5 \\left(-1\\right)^{n} x_{0}^{2} + 3 \\left(-1\\right)^{n} x_{1}^{2}\\right) s{\\left(n - 1 \\right)} + \\left(3 \\left(-1\\right)^{n} n^{2} x_{0} - 13 \\left(-1\\right)^{n} n x_{0} + 14 \\left(-1\\right)^{n} x_{0}\\right) s{\\left(n - 2 \\right)} + \\left(- \\left(-1\\right)^{n} n^{3} + 8 \\left(-1\\right)^{n} n^{2} - 21 \\left(-1\\right)^{n} n + 18 \\left(-1\\right)^{n}\\right) s{\\left(n - 3 \\right)}, s{\\left(n \\right)}, s{\\left(n - 1 \\right)}, s{\\left(n - 2 \\right)}, s{\\left(n - 3 \\right)}, domain=\\mathbb{Z}\\left[n, x_{0}, \\left(-1\\right)^{n}, x_{1}\\right] \\right)}$"
161+
],
162+
"text/plain": [
163+
"Poly(((-1)**n*x0**3 + (-1)**n*x0*x1**2)*(s(n)) + (-3*(-1)**n*n*x0**2 - (-1)**n*n*x1**2 + 5*(-1)**n*x0**2 + 3*(-1)**n*x1**2)*(s(n - 1)) + (3*(-1)**n*n**2*x0 - 13*(-1)**n*n*x0 + 14*(-1)**n*x0)*(s(n - 2)) + (-(-1)**n*n**3 + 8*(-1)**n*n**2 - 21*(-1)**n*n + 18*(-1)**n)*(s(n - 3)), s(n), s(n - 1), s(n - 2), s(n - 3), domain='ZZ[n,x0,(-1)**n,x1]')"
164+
]
165+
},
166+
"execution_count": 59,
167+
"metadata": {},
168+
"output_type": "execute_result"
169+
}
170+
],
171+
"source": [
172+
"recur\n",
173+
"poly_in_s_n = sp.poly(recur, [s(n-i) for i in range(order)])\n",
174+
"poly_in_s_n"
175+
]
176+
},
177+
{
178+
"cell_type": "code",
179+
"execution_count": null,
97180
"metadata": {},
98181
"outputs": [],
99182
"source": [
100-
"recur = get_shifted_recurrence_exp_from_pde(laplace2d)"
183+
"coeff_s_n = [poly_in_s_n.coeff_monomial(poly_in_s_n.gens[i]) for i in range(order)]\n",
184+
"\n",
185+
"table = []\n",
186+
"for i in range(len(coeff_s_n)):\n",
187+
" table.append(sp.poly(coeff_s_n[i], var[0]).all_coeffs()[::-1])"
188+
]
189+
},
190+
{
191+
"cell_type": "code",
192+
"execution_count": 58,
193+
"metadata": {},
194+
"outputs": [
195+
{
196+
"data": {
197+
"text/plain": [
198+
"[[0, (-1)**n*x1**2, 0, (-1)**n],\n",
199+
" [-(-1)**n*n*x1**2 + 3*(-1)**n*x1**2, 0, -3*(-1)**n*n + 5*(-1)**n],\n",
200+
" [0, 3*(-1)**n*n**2 - 13*(-1)**n*n + 14*(-1)**n],\n",
201+
" [-(-1)**n*n**3 + 8*(-1)**n*n**2 - 21*(-1)**n*n + 18*(-1)**n]]"
202+
]
203+
},
204+
"execution_count": 58,
205+
"metadata": {},
206+
"output_type": "execute_result"
207+
}
208+
],
209+
"source": [
210+
"table"
101211
]
102212
},
103213
{

0 commit comments

Comments
 (0)