Spaces:
Running
Running
File size: 8,305 Bytes
6a86ad5 |
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 |
r"""
Efficient functions for generating Appell sequences.
An Appell sequence is a zero-indexed sequence of polynomials `p_i(x)`
satisfying `p_{i+1}'(x)=(i+1)p_i(x)` for all `i`. This definition leads
to the following iterative algorithm:
.. math :: p_0(x) = c_0,\ p_i(x) = i \int_0^x p_{i-1}(t)\,dt + c_i
The constant coefficients `c_i` are usually determined from the
just-evaluated integral and `i`.
Appell sequences satisfy the following identity from umbral calculus:
.. math :: p_n(x+y) = \sum_{k=0}^n \binom{n}{k} p_k(x) y^{n-k}
References
==========
.. [1] https://en.wikipedia.org/wiki/Appell_sequence
.. [2] Peter Luschny, "An introduction to the Bernoulli function",
https://arxiv.org/abs/2009.06743
"""
from sympy.polys.densearith import dup_mul_ground, dup_sub_ground, dup_quo_ground
from sympy.polys.densetools import dup_eval, dup_integrate
from sympy.polys.domains import ZZ, QQ
from sympy.polys.polytools import named_poly
from sympy.utilities import public
def dup_bernoulli(n, K):
"""Low-level implementation of Bernoulli polynomials."""
if n < 1:
return [K.one]
p = [K.one, K(-1,2)]
for i in range(2, n+1):
p = dup_integrate(dup_mul_ground(p, K(i), K), 1, K)
if i % 2 == 0:
p = dup_sub_ground(p, dup_eval(p, K(1,2), K) * K(1<<(i-1), (1<<i)-1), K)
return p
@public
def bernoulli_poly(n, x=None, polys=False):
r"""Generates the Bernoulli polynomial `\operatorname{B}_n(x)`.
`\operatorname{B}_n(x)` is the unique polynomial satisfying
.. math :: \int_{x}^{x+1} \operatorname{B}_n(t) \,dt = x^n.
Based on this, we have for nonnegative integer `s` and integer
`a` and `b`
.. math :: \sum_{k=a}^{b} k^s = \frac{\operatorname{B}_{s+1}(b+1) -
\operatorname{B}_{s+1}(a)}{s+1}
which is related to Jakob Bernoulli's original motivation for introducing
the Bernoulli numbers, the values of these polynomials at `x = 1`.
Examples
========
>>> from sympy import summation
>>> from sympy.abc import x
>>> from sympy.polys import bernoulli_poly
>>> bernoulli_poly(5, x)
x**5 - 5*x**4/2 + 5*x**3/3 - x/6
>>> def psum(p, a, b):
... return (bernoulli_poly(p+1,b+1) - bernoulli_poly(p+1,a)) / (p+1)
>>> psum(4, -6, 27)
3144337
>>> summation(x**4, (x, -6, 27))
3144337
>>> psum(1, 1, x).factor()
x*(x + 1)/2
>>> psum(2, 1, x).factor()
x*(x + 1)*(2*x + 1)/6
>>> psum(3, 1, x).factor()
x**2*(x + 1)**2/4
Parameters
==========
n : int
Degree of the polynomial.
x : optional
polys : bool, optional
If True, return a Poly, otherwise (default) return an expression.
See Also
========
sympy.functions.combinatorial.numbers.bernoulli
References
==========
.. [1] https://en.wikipedia.org/wiki/Bernoulli_polynomials
"""
return named_poly(n, dup_bernoulli, QQ, "Bernoulli polynomial", (x,), polys)
def dup_bernoulli_c(n, K):
"""Low-level implementation of central Bernoulli polynomials."""
p = [K.one]
for i in range(1, n+1):
p = dup_integrate(dup_mul_ground(p, K(i), K), 1, K)
if i % 2 == 0:
p = dup_sub_ground(p, dup_eval(p, K.one, K) * K((1<<(i-1))-1, (1<<i)-1), K)
return p
@public
def bernoulli_c_poly(n, x=None, polys=False):
r"""Generates the central Bernoulli polynomial `\operatorname{B}_n^c(x)`.
These are scaled and shifted versions of the plain Bernoulli polynomials,
done in such a way that `\operatorname{B}_n^c(x)` is an even or odd function
for even or odd `n` respectively:
.. math :: \operatorname{B}_n^c(x) = 2^n \operatorname{B}_n
\left(\frac{x+1}{2}\right)
Parameters
==========
n : int
Degree of the polynomial.
x : optional
polys : bool, optional
If True, return a Poly, otherwise (default) return an expression.
"""
return named_poly(n, dup_bernoulli_c, QQ, "central Bernoulli polynomial", (x,), polys)
def dup_genocchi(n, K):
"""Low-level implementation of Genocchi polynomials."""
if n < 1:
return [K.zero]
p = [-K.one]
for i in range(2, n+1):
p = dup_integrate(dup_mul_ground(p, K(i), K), 1, K)
if i % 2 == 0:
p = dup_sub_ground(p, dup_eval(p, K.one, K) // K(2), K)
return p
@public
def genocchi_poly(n, x=None, polys=False):
r"""Generates the Genocchi polynomial `\operatorname{G}_n(x)`.
`\operatorname{G}_n(x)` is twice the difference between the plain and
central Bernoulli polynomials, so has degree `n-1`:
.. math :: \operatorname{G}_n(x) = 2 (\operatorname{B}_n(x) -
\operatorname{B}_n^c(x))
The factor of 2 in the definition endows `\operatorname{G}_n(x)` with
integer coefficients.
Parameters
==========
n : int
Degree of the polynomial plus one.
x : optional
polys : bool, optional
If True, return a Poly, otherwise (default) return an expression.
See Also
========
sympy.functions.combinatorial.numbers.genocchi
"""
return named_poly(n, dup_genocchi, ZZ, "Genocchi polynomial", (x,), polys)
def dup_euler(n, K):
"""Low-level implementation of Euler polynomials."""
return dup_quo_ground(dup_genocchi(n+1, ZZ), K(-n-1), K)
@public
def euler_poly(n, x=None, polys=False):
r"""Generates the Euler polynomial `\operatorname{E}_n(x)`.
These are scaled and reindexed versions of the Genocchi polynomials:
.. math :: \operatorname{E}_n(x) = -\frac{\operatorname{G}_{n+1}(x)}{n+1}
Parameters
==========
n : int
Degree of the polynomial.
x : optional
polys : bool, optional
If True, return a Poly, otherwise (default) return an expression.
See Also
========
sympy.functions.combinatorial.numbers.euler
"""
return named_poly(n, dup_euler, QQ, "Euler polynomial", (x,), polys)
def dup_andre(n, K):
"""Low-level implementation of Andre polynomials."""
p = [K.one]
for i in range(1, n+1):
p = dup_integrate(dup_mul_ground(p, K(i), K), 1, K)
if i % 2 == 0:
p = dup_sub_ground(p, dup_eval(p, K.one, K), K)
return p
@public
def andre_poly(n, x=None, polys=False):
r"""Generates the Andre polynomial `\mathcal{A}_n(x)`.
This is the Appell sequence where the constant coefficients form the sequence
of Euler numbers ``euler(n)``. As such they have integer coefficients
and parities matching the parity of `n`.
Luschny calls these the *Swiss-knife polynomials* because their values
at 0 and 1 can be simply transformed into both the Bernoulli and Euler
numbers. Here they are called the Andre polynomials because
`|\mathcal{A}_n(n\bmod 2)|` for `n \ge 0` generates what Luschny calls
the *Andre numbers*, A000111 in the OEIS.
Examples
========
>>> from sympy import bernoulli, euler, genocchi
>>> from sympy.abc import x
>>> from sympy.polys import andre_poly
>>> andre_poly(9, x)
x**9 - 36*x**7 + 630*x**5 - 5124*x**3 + 12465*x
>>> [andre_poly(n, 0) for n in range(11)]
[1, 0, -1, 0, 5, 0, -61, 0, 1385, 0, -50521]
>>> [euler(n) for n in range(11)]
[1, 0, -1, 0, 5, 0, -61, 0, 1385, 0, -50521]
>>> [andre_poly(n-1, 1) * n / (4**n - 2**n) for n in range(1, 11)]
[1/2, 1/6, 0, -1/30, 0, 1/42, 0, -1/30, 0, 5/66]
>>> [bernoulli(n) for n in range(1, 11)]
[1/2, 1/6, 0, -1/30, 0, 1/42, 0, -1/30, 0, 5/66]
>>> [-andre_poly(n-1, -1) * n / (-2)**(n-1) for n in range(1, 11)]
[-1, -1, 0, 1, 0, -3, 0, 17, 0, -155]
>>> [genocchi(n) for n in range(1, 11)]
[-1, -1, 0, 1, 0, -3, 0, 17, 0, -155]
>>> [abs(andre_poly(n, n%2)) for n in range(11)]
[1, 1, 1, 2, 5, 16, 61, 272, 1385, 7936, 50521]
Parameters
==========
n : int
Degree of the polynomial.
x : optional
polys : bool, optional
If True, return a Poly, otherwise (default) return an expression.
See Also
========
sympy.functions.combinatorial.numbers.andre
References
==========
.. [1] Peter Luschny, "An introduction to the Bernoulli function",
https://arxiv.org/abs/2009.06743
"""
return named_poly(n, dup_andre, ZZ, "Andre polynomial", (x,), polys)
|