组合#
该模块实现各种组合功能。
- class sympy.functions.combinatorial.numbers.bell(n, k_sym=None, symbols=None)[源代码]#
贝尔数/贝尔多项式
铃数满足 \(B_0 = 1\) 和
\[B{n=\sum{k=0}^{n-1}\binom{n-1}{k}B}k。\]它们也由以下公式给出:
\[B{n=\frac{1}{e}\sum{k=0}^{\infty}\frac{k^n}{k!}.\]贝尔多项式由 \(B_0(x) = 1\) 和
\[B{n(x)=x\sum{k=1}^{n-1}\binom{n-1}{k-1}B{k-1}(x)。\]第二类贝尔多项式(有时称为“部分”贝尔多项式或不完全贝尔多项式)定义为
\[B{n,k}(x_1,x\u2,\dotsc x{n-k+1})=\]bell(n)
给予 \(n^{{th}}\) 铃号, \(B_n\) .bell(n, x)
给予 \(n^{{th}}\) 贝尔多项式, \(B_n(x)\) .bell(n, k, (x1, x2, ...))
给出第二类贝尔多项式, \(B_{{n,k}}(x_1, x_2, \dotsc, x_{{n-k+1}})\) .
笔记
不要与伯努利数和伯努利多项式混淆,它们使用相同的符号。
实例
>>> from sympy import bell, Symbol, symbols
>>> [bell(n) for n in range(11)] [1, 1, 2, 5, 15, 52, 203, 877, 4140, 21147, 115975] >>> bell(30) 846749014511809332450147 >>> bell(4, Symbol('t')) t**4 + 6*t**3 + 7*t**2 + t >>> bell(6, 2, symbols('x:6')[1:]) 6*x1*x5 + 15*x2*x4 + 10*x3**2
工具书类
- class sympy.functions.combinatorial.numbers.bernoulli(n, x=None)[源代码]#
Bernoulli numbers / Bernoulli polynomials / Bernoulli function
伯努利数是由 \(B_0 = 1\) 以及递归关系 (\(n > 0\) ):
\[n+1 = \sum_{k=0}^n \binom{n+1}{k} B_k\]They are also commonly defined by their exponential generating function, which is \(\frac{x}{1 - e^{-x}}\). For odd indices > 1, the Bernoulli numbers are zero.
伯努利多项式满足类似公式:
\[B_n(x) = \sum_{k=0}^n (-1)^k \binom{n}{k} B_k x^{n-k}\]Bernoulli numbers and Bernoulli polynomials are related as \(B_n(1) = B_n\).
The generalized Bernoulli function \(\operatorname{B}(s, a)\) is defined for any complex \(s\) and \(a\), except where \(a\) is a nonpositive integer and \(s\) is not a nonnegative integer. It is an entire function of \(s\) for fixed \(a\), related to the Hurwitz zeta function by
\[\begin{split}\operatorname{B}(s, a) = \begin{cases} -s \zeta(1-s, a) & s \ne 0 \\ 1 & s = 0 \end{cases}\end{split}\]When \(s\) is a nonnegative integer this function reduces to the Bernoulli polynomials: \(\operatorname{B}(n, x) = B_n(x)\). When \(a\) is omitted it is assumed to be 1, yielding the (ordinary) Bernoulli function which interpolates the Bernoulli numbers and is related to the Riemann zeta function.
我们使用Ramanujan公式计算伯努利数:
\[B{n=\frac{A(n)-S(n)}{\binom{n+3}{n}}\]在哪里?
\[A(n)=\begin{cases}\frac{n+3}{3}&\]还有:
\[S(n) = \sum_{k=1}^{[n/6]} \binom{n+3}{n-6k} B_{n-6k}\]This formula is similar to the sum given in the definition, but cuts \(\frac{2}{3}\) of the terms. For Bernoulli polynomials, we use Appell sequences.
For \(n\) a nonnegative integer and \(s\), \(a\), \(x\) arbitrary complex numbers,
bernoulli(n)
gives the nth Bernoulli number, \(B_n\)bernoulli(s)
gives the Bernoulli function \(\operatorname{B}(s)\)bernoulli(n, x)
gives the nth Bernoulli polynomial in \(x\), \(B_n(x)\)bernoulli(s, a)
gives the generalized Bernoulli function \(\operatorname{B}(s, a)\)
在 1.12 版本发生变更:
bernoulli(1)
gives \(+\frac{1}{2}\) instead of \(-\frac{1}{2}\). This choice of value confers several theoretical advantages [R214], including the extension to complex parameters described above which this function now implements. The previous behavior, defined only for nonnegative integers \(n\), can be obtained with(-1)**n*bernoulli(n)
.实例
>>> from sympy import bernoulli >>> from sympy.abc import x >>> [bernoulli(n) for n in range(11)] [1, 1/2, 1/6, 0, -1/30, 0, 1/42, 0, -1/30, 0, 5/66] >>> bernoulli(1000001) 0 >>> bernoulli(3, x) x**3 - 3*x**2/2 + x/2
参见
andre
,bell
,catalan
,euler
,fibonacci
,harmonic
,lucas
,genocchi
,partition
,tribonacci
,sympy.polys.appellseqs.bernoulli_poly
工具书类
[R214] (1,2)Peter Luschny, "The Bernoulli Manifesto", https://luschny.de/math/zeta/The-Bernoulli-Manifesto.html
[R215]Peter Luschny, "An introduction to the Bernoulli function", https://arxiv.org/abs/2009.06743
- class sympy.functions.combinatorial.factorials.binomial(n, k)[源代码]#
二项式系数的实现。根据需要的解释,可以用两种方式定义:
\[\binom{n}{k} = \frac{n!}{k!(n-k)!}\ \text{or}\ \binom{n}{k} = \frac{(n)_k}{k!}\]首先,在严格的组合意义上,它定义了我们可以选择的方式的数量 \(k\) 一组元素 \(n\) 元素。在这种情况下,两个参数都是非负整数,并且使用基于素数分解的有效算法计算二项式。
另一个定义是对任意 \(n\) 然而 \(k\) 也必须是非负的。这个例子在求和时非常有用。
For the sake of convenience, for negative integer \(k\) this function will return zero no matter the other argument.
展开二项式 \(n\) 是一个符号,请使用
expand_func()
或expand(func=True)
. 前者将保持多项式的因子形式,而后者则扩展多项式本身。有关详细信息,请参见示例。实例
>>> from sympy import Symbol, Rational, binomial, expand_func >>> n = Symbol('n', integer=True, positive=True)
>>> binomial(15, 8) 6435
>>> binomial(n, -1) 0
帕斯卡三角形的行可以用二项式函数生成:
>>> for N in range(8): ... print([binomial(N, i) for i in range(N + 1)]) ... [1] [1, 1] [1, 2, 1] [1, 3, 3, 1] [1, 4, 6, 4, 1] [1, 5, 10, 10, 5, 1] [1, 6, 15, 20, 15, 6, 1] [1, 7, 21, 35, 35, 21, 7, 1]
一条给定的对角线,例如第四条对角线:
>>> N = -4 >>> [binomial(N, i) for i in range(1 - N)] [1, -4, 10, -20, 35]
>>> binomial(Rational(5, 4), 3) -5/128 >>> binomial(Rational(-5, 4), 3) -195/128
>>> binomial(n, 3) binomial(n, 3)
>>> binomial(n, 3).expand(func=True) n**3/6 - n**2/2 + n/3
>>> expand_func(binomial(n, 3)) n*(n - 2)*(n - 1)/6
In many cases, we can also compute binomial coefficients modulo a prime p quickly using Lucas' Theorem [R217], though we need to include \(evaluate=False\) to postpone evaluation:
>>> from sympy import Mod >>> Mod(binomial(156675, 4433, evaluate=False), 10**5 + 3) 28625
Using a generalisation of Lucas's Theorem given by Granville [R218], we can extend this to arbitrary n:
>>> Mod(binomial(10**18, 10**12, evaluate=False), (10**5 + 3)**2) 3744312326
工具书类
[R218] (1,2)Binomial coefficients modulo prime powers, Andrew Granville, Available: https://web.archive.org/web/20170202003812/http://www.dms.umontreal.ca/~andrew/PDF/BinCoeff.pdf
- class sympy.functions.combinatorial.numbers.catalan(n)[源代码]#
加泰罗尼亚数字
这个 \(n^{{th}}\) 加泰罗尼亚数字由以下公式给出:
\[C{n=\frac{1}{n+1}\binom{2n}{n}\]catalan(n)
gives the \(n^{th}\) Catalan number, \(C_n\)
实例
>>> from sympy import (Symbol, binomial, gamma, hyper, ... catalan, diff, combsimp, Rational, I)
>>> [catalan(i) for i in range(1,10)] [1, 2, 5, 14, 42, 132, 429, 1430, 4862]
>>> n = Symbol("n", integer=True)
>>> catalan(n) catalan(n)
加泰罗尼亚数字可以转换成其他几个涉及其他数学函数的相同表达式
>>> catalan(n).rewrite(binomial) binomial(2*n, n)/(n + 1)
>>> catalan(n).rewrite(gamma) 4**n*gamma(n + 1/2)/(sqrt(pi)*gamma(n + 2))
>>> catalan(n).rewrite(hyper) hyper((-n, 1 - n), (2,), 1)
对于n的一些非整数值,我们可以通过改写gamma函数得到闭式表达式:
>>> catalan(Rational(1, 2)).rewrite(gamma) 8/(3*pi)
我们可以区分被解释为n中连续实数函数的加泰罗尼亚数C(n):
>>> diff(catalan(n), n) (polygamma(0, n + 1/2) - polygamma(0, n + 2) + log(4))*catalan(n)
作为一个更高级的示例,请考虑以下连续数字之间的比率:
>>> combsimp((catalan(n + 1)/catalan(n)).rewrite(binomial)) 2*(2*n + 1)/(n + 2)
加泰罗尼亚数可以推广为复数:
>>> catalan(I).rewrite(gamma) 4**I*gamma(1/2 + I)/(sqrt(pi)*gamma(2 + I))
并以任意精度进行评估:
>>> catalan(I).evalf(20) 0.39764993382373624267 - 0.020884341620842555705*I
参见
andre
,bell
,bernoulli
,euler
,fibonacci
,harmonic
,lucas
,genocchi
,partition
,tribonacci
,sympy.functions.combinatorial.factorials.binomial
工具书类
- class sympy.functions.combinatorial.numbers.euler(n, x=None)[源代码]#
Euler numbers / Euler polynomials / Euler function
欧拉数由以下公式给出:
\[E{2n}=I\sum{k=1}^{2n+1}\sum{j=0}^k\binom{k}{j}\]\[E{2n+1}=0\]欧拉数和欧拉多项式的关系是
\[E\u n=2^n E\u n\左(\frac{1}{2}\右)。\]We compute symbolic Euler polynomials using Appell sequences, but numerical evaluation of the Euler polynomial is computed more efficiently (and more accurately) using the mpmath library.
The Euler polynomials are special cases of the generalized Euler function, related to the Genocchi function as
\[\operatorname{E}(s, a) = -\frac{\operatorname{G}(s+1, a)}{s+1}\]with the limit of \(\psi\left(\frac{a+1}{2}\right) - \psi\left(\frac{a}{2}\right)\) being taken when \(s = -1\). The (ordinary) Euler function interpolating the Euler numbers is then obtained as \(\operatorname{E}(s) = 2^s \operatorname{E}\left(s, \frac{1}{2}\right)\).
euler(n)
gives the nth Euler number \(E_n\).euler(s)
gives the Euler function \(\operatorname{E}(s)\).euler(n, x)
gives the nth Euler polynomial \(E_n(x)\).euler(s, a)
gives the generalized Euler function \(\operatorname{E}(s, a)\).
实例
>>> from sympy import euler, Symbol, S >>> [euler(n) for n in range(10)] [1, 0, -1, 0, 5, 0, -61, 0, 1385, 0] >>> [2**n*euler(n,1) for n in range(10)] [1, 1, 0, -2, 0, 16, 0, -272, 0, 7936] >>> n = Symbol("n") >>> euler(n + 2*n) euler(3*n)
>>> x = Symbol("x") >>> euler(n, x) euler(n, x)
>>> euler(0, x) 1 >>> euler(1, x) x - 1/2 >>> euler(2, x) x**2 - x >>> euler(3, x) x**3 - 3*x**2/2 + 1/4 >>> euler(4, x) x**4 - 2*x**3 + x
>>> euler(12, S.Half) 2702765/4096 >>> euler(12) 2702765
参见
andre
,bell
,bernoulli
,catalan
,fibonacci
,harmonic
,lucas
,genocchi
,partition
,tribonacci
,sympy.polys.appellseqs.euler_poly
工具书类
- class sympy.functions.combinatorial.factorials.factorial(n)[源代码]#
非负整数上阶乘函数的实现。按照惯例(与gamma函数和二项式系数一致),负整数的阶乘是复无穷大。
阶乘在组合学中非常重要,它给出了 \(n\) 对象可以被置换。它也出现在微积分、概率论、数论等领域。
阶乘与伽玛函数有着严格的关系。事实上 \(n! = gamma(n+1)\) 对于非负整数。这种重写在组合简化的情况下非常有用。
阶乘的计算是用两种算法完成的。对于小参数,使用预计算的查找表。但是对于较大的输入算法,使用质数摆动。它是已知的最快的算法 \(n!\) 通过对一类特殊数的素数分解,这里称之为“摇摆数”。
实例
>>> from sympy import Symbol, factorial, S >>> n = Symbol('n', integer=True)
>>> factorial(0) 1
>>> factorial(7) 5040
>>> factorial(-2) zoo
>>> factorial(n) factorial(n)
>>> factorial(2*n) factorial(2*n)
>>> factorial(S(1)/2) factorial(1/2)
- class sympy.functions.combinatorial.factorials.subfactorial(arg)[源代码]#
The subfactorial counts the derangements of \(n\) items and is defined for non-negative integers as:
\[\begin{split}!n=\begin{cases}1&n=0\\0&n=1\\\end{split}\]也可以写成
int(round(n!/exp(1)))
但是这个函数实现了带缓存的递归定义。An interesting analytic expression is the following [R228]
\[!x=\伽马(x+1,-1)/e\]which is valid for non-negative integers \(x\). The above formula is not very useful in case of non-integers. \(\Gamma(x + 1, -1)\) is single-valued only for integral arguments \(x\), elsewhere on the positive real axis it has an infinite number of branches none of which are real.
实例
>>> from sympy import subfactorial >>> from sympy.abc import n >>> subfactorial(n + 1) subfactorial(n + 1) >>> subfactorial(5) 44
工具书类
- class sympy.functions.combinatorial.factorials.factorial2(arg)[源代码]#
双因子 \(n!!\) ,不要与 \((n!)!\)
对于非负整数和奇数负整数,双阶乘定义为:
\[\begin{split}n!!=\begin{cases}1&n=0\\\end{split}\]实例
>>> from sympy import factorial2, var >>> n = var('n') >>> n n >>> factorial2(n + 1) factorial2(n + 1) >>> factorial2(5) 15 >>> factorial2(-1) 1 >>> factorial2(-5) 1/3
工具书类
- class sympy.functions.combinatorial.factorials.FallingFactorial(x, k)[源代码]#
降阶乘(与升阶乘相关)是一个双值函数,出现于具体数学、超几何函数和级数展开式中。它的定义是
\[\texttt{ff(x, k)} = (x)_k = x \cdot (x-1) \cdots (x-k+1)\]where \(x\) can be arbitrary expression and \(k\) is an integer. For more information check "Concrete mathematics" by Graham, pp. 66 or [R230].
When \(x\) is a \(~.Poly\) instance of degree \(\ge 1\) with single variable, \((x)_k = x(y) \cdot x(y-1) \cdots x(y-k+1)\), where \(y\) is the variable of \(x\). This is as described in
>>> from sympy import ff, Poly, Symbol >>> from sympy.abc import x >>> n = Symbol('n', integer=True)
>>> ff(x, 0) 1 >>> ff(5, 5) 120 >>> ff(x, 5) == x*(x - 1)*(x - 2)*(x - 3)*(x - 4) True >>> ff(Poly(x**2, x), 2) Poly(x**4 - 2*x**3 + x**2, x, domain='ZZ') >>> ff(n, n) factorial(n)
重写很复杂,除非知道参数之间的关系,但是下降阶乘可以用gamma、阶乘和二项式以及上升阶乘重写。
>>> from sympy import factorial, rf, gamma, binomial, Symbol >>> n = Symbol('n', integer=True, positive=True) >>> F = ff(n, n - 2) >>> for i in (rf, ff, factorial, binomial, gamma): ... F.rewrite(i) ... RisingFactorial(3, n - 2) FallingFactorial(n, n - 2) factorial(n)/2 binomial(n, n - 2)*factorial(n - 2) gamma(n + 1)/2
工具书类
[R231]Peter Paule, "Greatest Factorial Factorization and Symbolic Summation", Journal of Symbolic Computation, vol. 20, pp. 235-268, 1995.
- class sympy.functions.combinatorial.numbers.fibonacci(n, sym=None)[源代码]#
斐波纳契数/斐波那契多项式
Fibonacci数是由初始项定义的整数序列 \(F_0 = 0\) , \(F_1 = 1\) 二项递推关系 \(F_n = F_{{n-1}} + F_{{n-2}}\) . 这个定义通过公式扩展到任意的实数和复杂参数
\[F\u z=\frac{\phi^z-\cos(\piz)\phi^{-z}}}{\sqrt 5}\]斐波那契多项式由 \(F_1(x) = 1\) , \(F_2(x) = x\) 和 \(F_n(x) = x*F_{{n-1}}(x) + F_{{n-2}}(x)\) 对于 \(n > 2\) . 对于所有正整数 \(n\) , \(F_n(1) = F_n\) .
fibonacci(n)
gives the \(n^{th}\) Fibonacci number, \(F_n\)fibonacci(n, x)
gives the \(n^{th}\) Fibonacci polynomial in \(x\), \(F_n(x)\)
实例
>>> from sympy import fibonacci, Symbol
>>> [fibonacci(x) for x in range(11)] [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55] >>> fibonacci(5, Symbol('t')) t**4 + 3*t**2 + 1
工具书类
- class sympy.functions.combinatorial.numbers.tribonacci(n, sym=None)[源代码]#
tribonaci数/tribonaci多项式
tribonaci数是由初始项定义的整数序列 \(T_0 = 0\) , \(T_1 = 1\) , \(T_2 = 1\) 三项递推关系 \(T_n = T_{{n-1}} + T_{{n-2}} + T_{{n-3}}\) .
tribonaci多项式的定义如下 \(T_0(x) = 0\) , \(T_1(x) = 1\) , \(T_2(x) = x^2\) 和 \(T_n(x) = x^2 T_{{n-1}}(x) + x T_{{n-2}}(x) + T_{{n-3}}(x)\) 对于 \(n > 2\) . 对于所有正整数 \(n\) , \(T_n(1) = T_n\) .
tribonacci(n)
gives the \(n^{th}\) Tribonacci number, \(T_n\)tribonacci(n, x)
gives the \(n^{th}\) Tribonacci polynomial in \(x\), \(T_n(x)\)
实例
>>> from sympy import tribonacci, Symbol
>>> [tribonacci(x) for x in range(11)] [0, 1, 1, 2, 4, 7, 13, 24, 44, 81, 149] >>> tribonacci(5, Symbol('t')) t**8 + 3*t**5 + 3*t**2
工具书类
[R235]https://en.wikipedia.org/wiki/Generalizations_Fibonacci_数#Tribonacci_数
- class sympy.functions.combinatorial.numbers.harmonic(n, m=None)[源代码]#
调和数
第n次谐波数由 \(\operatorname{{H}}_{{n}} = 1 + \frac{{1}}{{2}} + \frac{{1}}{{3}} + \ldots + \frac{{1}}{{n}}\) .
更广泛地说:
\[\运算符名称{H}{n,m}=\sum{k=1}^{n}\frac{1}{k^m}\]AS \(n \rightarrow \infty\) , \(\operatorname{{H}}_{{n,m}} \rightarrow \zeta(m)\) ,Riemann-zeta函数。
harmonic(n)
gives the nth harmonic number, \(\operatorname{H}_n\)harmonic(n, m)
gives the nth generalized harmonic number of order \(m\), \(\operatorname{H}_{n,m}\), whereharmonic(n) == harmonic(n, 1)
This function can be extended to complex \(n\) and \(m\) where \(n\) is not a negative integer or \(m\) is a nonpositive integer as
\[\begin{split}\operatorname{H}_{n,m} = \begin{cases} \zeta(m) - \zeta(m, n+1) & m \ne 1 \\ \psi(n+1) + \gamma & m = 1 \end{cases}\end{split}\]实例
>>> from sympy import harmonic, oo
>>> [harmonic(n) for n in range(6)] [0, 1, 3/2, 11/6, 25/12, 137/60] >>> [harmonic(n, 2) for n in range(6)] [0, 1, 5/4, 49/36, 205/144, 5269/3600] >>> harmonic(oo, 2) pi**2/6
>>> from sympy import Symbol, Sum >>> n = Symbol("n")
>>> harmonic(n).rewrite(Sum) Sum(1/_k, (_k, 1, n))
我们可以计算所有整数和正有理参数的调和数:
>>> from sympy import S, expand_func, simplify >>> harmonic(8) 761/280 >>> harmonic(11) 83711/27720
>>> H = harmonic(1/S(3)) >>> H harmonic(1/3) >>> He = expand_func(H) >>> He -log(6) - sqrt(3)*pi/6 + 2*Sum(log(sin(_k*pi/3))*cos(2*_k*pi/3), (_k, 1, 1)) + 3*Sum(1/(3*_k + 1), (_k, 0, 0)) >>> He.doit() -log(6) - sqrt(3)*pi/6 - log(sqrt(3)/2) + 3 >>> H = harmonic(25/S(7)) >>> He = simplify(expand_func(H).doit()) >>> He log(sin(2*pi/7)**(2*cos(16*pi/7))/(14*sin(pi/7)**(2*cos(pi/7))*cos(pi/14)**(2*sin(pi/14)))) + pi*tan(pi/14)/2 + 30247/9900 >>> He.n(40) 1.983697455232980674869851942390639915940 >>> harmonic(25/S(7)).n(40) 1.983697455232980674869851942390639915940
我们可以用polygamma函数重写谐波数:
>>> from sympy import digamma, polygamma >>> m = Symbol("m", integer=True, positive=True)
>>> harmonic(n).rewrite(digamma) polygamma(0, n + 1) + EulerGamma
>>> harmonic(n).rewrite(polygamma) polygamma(0, n + 1) + EulerGamma
>>> harmonic(n,3).rewrite(polygamma) polygamma(2, n + 1)/2 + zeta(3)
>>> simplify(harmonic(n,m).rewrite(polygamma)) Piecewise((polygamma(0, n + 1) + EulerGamma, Eq(m, 1)), (-(-1)**m*polygamma(m - 1, n + 1)/factorial(m - 1) + zeta(m), True))
可以提取参数中的整数偏移量:
>>> from sympy import expand_func
>>> expand_func(harmonic(n+4)) harmonic(n) + 1/(n + 4) + 1/(n + 3) + 1/(n + 2) + 1/(n + 1)
>>> expand_func(harmonic(n-4)) harmonic(n) - 1/(n - 1) - 1/(n - 2) - 1/(n - 3) - 1/n
也可以计算一些限制:
>>> from sympy import limit, oo
>>> limit(harmonic(n), n, oo) oo
>>> limit(harmonic(n, 2), n, oo) pi**2/6
>>> limit(harmonic(n, 3), n, oo) zeta(3)
For \(m > 1\), \(H_{n,m}\) tends to \(\zeta(m)\) in the limit of infinite \(n\):
>>> m = Symbol("m", positive=True) >>> limit(harmonic(n, m+1), n, oo) zeta(m + 1)
工具书类
- class sympy.functions.combinatorial.numbers.lucas(n)[源代码]#
卢卡斯数字
Lucas数满足与Fibonacci序列相似的递归关系,其中每个项都是前两项的和。它们是通过选择初始值生成的 \(L_0 = 2\) 和 \(L_1 = 1\) .
lucas(n)
给予 \(n^{{th}}\) 卢卡斯数
实例
>>> from sympy import lucas
>>> [lucas(x) for x in range(11)] [2, 1, 3, 4, 7, 11, 18, 29, 47, 76, 123]
工具书类
- class sympy.functions.combinatorial.numbers.genocchi(n, x=None)[源代码]#
Genocchi numbers / Genocchi polynomials / Genocchi function
Genocchi数是一个整数序列 \(G_n\) 满足关系:
\[\frac{-2t}{1 + e^{-t}} = \sum_{n=0}^\infty \frac{G_n t^n}{n!}\]They are related to the Bernoulli numbers by
\[G_n = 2 (1 - 2^n) B_n\]and generalize like the Bernoulli numbers to the Genocchi polynomials and function as
\[\operatorname{G}(s, a) = 2 \left(\operatorname{B}(s, a) - 2^s \operatorname{B}\left(s, \frac{a+1}{2}\right)\right)\]在 1.12 版本发生变更:
genocchi(1)
gives \(-1\) instead of \(1\).实例
>>> from sympy import genocchi, Symbol >>> [genocchi(n) for n in range(9)] [0, -1, -1, 0, 1, 0, -3, 0, 17] >>> n = Symbol('n', integer=True, positive=True) >>> genocchi(2*n + 1) 0 >>> x = Symbol('x') >>> genocchi(4, x) -4*x**3 + 6*x**2 - 1
参见
bell
,bernoulli
,catalan
,euler
,fibonacci
,harmonic
,lucas
,partition
,tribonacci
,sympy.polys.appellseqs.genocchi_poly
工具书类
[R245]Peter Luschny, "An introduction to the Bernoulli function", https://arxiv.org/abs/2009.06743
- class sympy.functions.combinatorial.numbers.andre(n)[源代码]#
Andre numbers / Andre function
The Andre number \(\mathcal{A}_n\) is Luschny's name for half the number of alternating permutations on \(n\) elements, where a permutation is alternating if adjacent elements alternately compare "greater" and "smaller" going from left to right. For example, \(2 < 3 > 1 < 4\) is an alternating permutation.
This sequence is A000111 in the OEIS, which assigns the names up/down numbers and Euler zigzag numbers. It satisfies a recurrence relation similar to that for the Catalan numbers, with \(\mathcal{A}_0 = 1\) and
\[2 \mathcal{A}_{n+1} = \sum_{k=0}^n \binom{n}{k} \mathcal{A}_k \mathcal{A}_{n-k}\]The Bernoulli and Euler numbers are signed transformations of the odd- and even-indexed elements of this sequence respectively:
\[\operatorname{B}_{2k} = \frac{2k \mathcal{A}_{2k-1}}{(-4)^k - (-16)^k}\]\[\operatorname{E}_{2k} = (-1)^k \mathcal{A}_{2k}\]Like the Bernoulli and Euler numbers, the Andre numbers are interpolated by the entire Andre function:
\[\begin{split}\mathcal{A}(s) = (-i)^{s+1} \operatorname{Li}_{-s}(i) + i^{s+1} \operatorname{Li}_{-s}(-i) = \\ \frac{2 \Gamma(s+1)}{(2\pi)^{s+1}} (\zeta(s+1, 1/4) - \zeta(s+1, 3/4) \cos{\pi s})\end{split}\]实例
>>> from sympy import andre, euler, bernoulli >>> [andre(n) for n in range(11)] [1, 1, 1, 2, 5, 16, 61, 272, 1385, 7936, 50521] >>> [(-1)**k * andre(2*k) for k in range(7)] [1, -1, 5, -61, 1385, -50521, 2702765] >>> [euler(2*k) for k in range(7)] [1, -1, 5, -61, 1385, -50521, 2702765] >>> [andre(2*k-1) * (2*k) / ((-4)**k - (-16)**k) for k in range(1, 8)] [1/6, -1/30, 1/42, -1/30, 5/66, -691/2730, 7/6] >>> [bernoulli(2*k) for k in range(1, 8)] [1/6, -1/30, 1/42, -1/30, 5/66, -691/2730, 7/6]
工具书类
[R248]Peter Luschny, "An introduction to the Bernoulli function", https://arxiv.org/abs/2009.06743
- class sympy.functions.combinatorial.numbers.partition(n)[源代码]#
分区编号
分区号是一个整数序列 \(p_n\) 代表了不同的表达方式的数量 \(n\) 作为自然数的总和(顺序无关)。的生成函数 \(p_n\) 计算公式:
\[\和{n=0}^\infty p_n x^n=\prod{k=1}^\infty(1-x^k)^{-1}\]实例
>>> from sympy import partition, Symbol >>> [partition(n) for n in range(9)] [1, 1, 2, 3, 5, 7, 11, 15, 22] >>> n = Symbol('n', integer=True, negative=True) >>> partition(n) 0
工具书类
- class sympy.functions.combinatorial.numbers.divisor_sigma(n, k=1)[源代码]#
计算除数函数 \(\sigma_k(n)\) 对于正整数n
divisor_sigma(n, k)
is equal tosum([x**k for x in divisors(n)])
如果n的素因式分解是:
\[n=\prod{i=1}^\omega p\u i^{m\u i},\]然后
\[\西格玛(n)=\prod{i=1}^\omega(1+p_i^k+p_i^{2k}+\cdots\]实例
>>> from sympy.functions.combinatorial.numbers import divisor_sigma >>> divisor_sigma(18, 0) 6 >>> divisor_sigma(39, 1) 56 >>> divisor_sigma(12, 2) 210 >>> divisor_sigma(37) 38
参见
sympy.ntheory.factor_.divisor_count
,totient
,sympy.ntheory.factor_.divisors
,sympy.ntheory.factor_.factorint
工具书类
- class sympy.functions.combinatorial.numbers.udivisor_sigma(n, k=1)[源代码]#
求酉除数函数 \(\sigma_k^*(n)\) 对于正整数n
udivisor_sigma(n, k)
is equal tosum([x**k for x in udivisors(n)])
如果n的素因式分解是:
\[n=\prod{i=1}^\omega p\u i^{m\u i},\]然后
\[\西格玛(n)=\prod{i=1}^\omega(1+p_i^{m_ik})。\]- 参数:
k :和中除数的幂
对于k=0,1:
udivisor_sigma(n, 0)
等于udivisor_count(n)
udivisor_sigma(n, 1)
等于sum(udivisors(n))
k的默认值为1。
实例
>>> from sympy.functions.combinatorial.numbers import udivisor_sigma >>> udivisor_sigma(18, 0) 4 >>> udivisor_sigma(74, 1) 114 >>> udivisor_sigma(36, 3) 47450 >>> udivisor_sigma(111) 152
参见
sympy.ntheory.factor_.divisor_count
,totient
,sympy.ntheory.factor_.divisors
,sympy.ntheory.factor_.udivisors
,sympy.ntheory.factor_.udivisor_count
,divisor_sigma
,sympy.ntheory.factor_.factorint
工具书类
- class sympy.functions.combinatorial.numbers.legendre_symbol(a, p)[源代码]#
返回Legendre符号 \((a / p)\) .
对于整数
a
还有一个奇怪的质数p
,Legendre符号定义为\[\genfrac(){}{}{a}{p}=\begin{cases}\]实例
>>> from sympy.functions.combinatorial.numbers import legendre_symbol >>> [legendre_symbol(i, 7) for i in range(7)] [0, 1, 1, -1, 1, -1, -1] >>> sorted(set([i**2 % 7 for i in range(7)])) [0, 1, 2, 4]
- class sympy.functions.combinatorial.numbers.jacobi_symbol(m, n)[源代码]#
返回Jacobi符号 \((m / n)\) .
对于任何整数
m
以及任何正的奇数整数n
雅可比符号被定义为勒让德符号对应的素数因子的乘积n
:\[\genfrac(){}{}{m}{n}=\]就像勒让德符号,如果雅各比符号 \(\genfrac(){{}}{{}}{{m}}{{n}} = -1\) 然后
m
是一个二次非剩余模n
.不像雅各布的符号,但是 \(\genfrac(){{}}{{}}{{m}}{{n}} = 1\) 然后
m
可能是也可能不是二次剩余模n
.实例
>>> from sympy.functions.combinatorial.numbers import jacobi_symbol, legendre_symbol >>> from sympy import S >>> jacobi_symbol(45, 77) -1 >>> jacobi_symbol(60, 121) 1
两者之间的关系
jacobi_symbol
和legendre_symbol
具体表现为:>>> L = legendre_symbol >>> S(45).factors() {3: 2, 5: 1} >>> jacobi_symbol(7, 45) == L(7, 3)**2 * L(7, 5)**1 True
- class sympy.functions.combinatorial.numbers.kronecker_symbol(a, n)[源代码]#
Returns the Kronecker symbol \((a / n)\).
实例
>>> from sympy.functions.combinatorial.numbers import kronecker_symbol >>> kronecker_symbol(45, 77) -1 >>> kronecker_symbol(13, -120) 1
工具书类
- class sympy.functions.combinatorial.numbers.mobius(n)[源代码]#
Mobius函数将自然数映射到{-1,0,1}
- 定义如下:
\(1\) 如果 \(n = 1\) .
\(0\) 如果 \(n\) 有一个平方素数。
\((-1)^k\) 如果 \(n\) 是一个无平方的正整数 \(k\) 素数因子。
它是数论和组合学中一个重要的乘法函数。它在数学级数、代数数论和物理学中都有应用(费米子算符用Mobius函数模型有很具体的实现)。
实例
>>> from sympy.functions.combinatorial.numbers import mobius >>> mobius(13*7) 1 >>> mobius(1) 1 >>> mobius(13*7*5) -1 >>> mobius(13**2) 0
Even in the case of a symbol, if it clearly contains a squared prime factor, it will be zero.
>>> from sympy import Symbol >>> n = Symbol("n", integer=True, positive=True) >>> mobius(4*n) 0 >>> mobius(n**2) 0
工具书类
[R255]托马斯科西“初等数论及其应用”
计算正整数n的不同素数因子的个数。
如果n的素因式分解是:
\[n=\prod{i=1}^k p_i^{m_i},\]然后
primenu(n)
或 \(\nu(n)\) 是:\[\nu(n)=k。\]实例
>>> from sympy.functions.combinatorial.numbers import primenu >>> primenu(1) 0 >>> primenu(30) 3
工具书类
- class sympy.functions.combinatorial.numbers.primeomega(n)[源代码]#
计算正整数n的重数的素数。
如果n的素因式分解是:
\[n=\prod{i=1}^k p_i^{m_i},\]然后
primeomega(n)
或 \(\Omega(n)\) 是:\[\ω(n)=\sum{i=1}^k m_i。\]实例
>>> from sympy.functions.combinatorial.numbers import primeomega >>> primeomega(1) 0 >>> primeomega(20) 3
工具书类
- class sympy.functions.combinatorial.numbers.totient(n)[源代码]#
计算欧拉函数φ(n)
totient(n)
或 \(\phi(n)\) 是正整数的数目 \(\leq\) n相对来说是n的素数。实例
>>> from sympy.functions.combinatorial.numbers import totient >>> totient(1) 1 >>> totient(25) 20 >>> totient(45) == totient(5)*totient(9) True
工具书类
- class sympy.functions.combinatorial.numbers.reduced_totient(n)[源代码]#
计算Carmichael约化到客户函数lambda(n)
reduced_totient(n)
或 \(\lambda(n)\) 最小的m>0是这样的吗 \(k^m \equiv 1 \mod n\) 对于所有k,相对质数对n。实例
>>> from sympy.functions.combinatorial.numbers import reduced_totient >>> reduced_totient(1) 1 >>> reduced_totient(8) 2 >>> reduced_totient(30) 4
参见
工具书类
- class sympy.functions.combinatorial.numbers.primepi(n)[源代码]#
表示素数计数函数pi(n)=小于或等于n的素数数。
实例
>>> from sympy.functions.combinatorial.numbers import primepi >>> from sympy import prime, prevprime, isprime >>> primepi(25) 9
So there are 9 primes less than or equal to 25. Is 25 prime?
>>> isprime(25) False
It is not. So the first prime less than 25 must be the 9th prime:
>>> prevprime(25) == prime(9) True
参见
sympy.ntheory.primetest.isprime
如果n是素数测试
sympy.ntheory.generate.primerange
生成给定范围内的所有素数
sympy.ntheory.generate.prime
返回第n个素数
工具书类
- class sympy.functions.combinatorial.factorials.RisingFactorial(x, k)[源代码]#
Rising factorial (also called Pochhammer symbol [R268]) is a double valued function arising in concrete mathematics, hypergeometric functions and series expansions. It is defined by:
\[\texttt{rf(y, k)} = (x)^k = x \cdot (x+1) \cdots (x+k-1)\]where \(x\) can be arbitrary expression and \(k\) is an integer. For more information check "Concrete mathematics" by Graham, pp. 66 or visit https://mathworld.wolfram.com/RisingFactorial.html page.
When \(x\) is a \(~.Poly\) instance of degree \(\ge 1\) with a single variable, \((x)^k = x(y) \cdot x(y+1) \cdots x(y+k-1)\), where \(y\) is the variable of \(x\). This is as described in [R269].
实例
>>> from sympy import rf, Poly >>> from sympy.abc import x >>> rf(x, 0) 1 >>> rf(1, 5) 120 >>> rf(x, 5) == x*(1 + x)*(2 + x)*(3 + x)*(4 + x) True >>> rf(Poly(x**3, x), 2) Poly(x**6 + 3*x**5 + 3*x**4 + x**3, x, domain='ZZ')
Rewriting is complicated unless the relationship between the arguments is known, but rising factorial can be rewritten in terms of gamma, factorial, binomial, and falling factorial.
>>> from sympy import Symbol, factorial, ff, binomial, gamma >>> n = Symbol('n', integer=True, positive=True) >>> R = rf(n, n + 2) >>> for i in (rf, ff, factorial, binomial, gamma): ... R.rewrite(i) ... RisingFactorial(n, n + 2) FallingFactorial(2*n + 1, n + 2) factorial(2*n + 1)/factorial(n - 1) binomial(2*n + 1, n + 2)*factorial(n + 2) gamma(2*n + 2)/gamma(n)
工具书类
- sympy.functions.combinatorial.numbers.stirling(n, k, d=None, kind=2, signed=False)[源代码]#
返回第一个或第二个(默认)类型的Stirling数字\(S(n,k)\)。
\(k=1\)到\(n\)的第二类斯特林数之和为
bell(n)
. 这些数字的重复关系是:\[{0\大括号0}=1;{n\大括号0}={0\大括号k}=0;\]\[{{n+1}\大括号k}=j{n\大括号k}+{n\大括号{k-1}}\]- 其中\(j\)是:
第一类斯特林数为\(n\),第一类有符号斯特林数为\(-n\),第二类斯特林数为k$。
The first kind of Stirling number counts the number of permutations of
n
distinct items that havek
cycles; the second kind counts the ways in whichn
distinct items can be partitioned intok
parts. Ifd
is given, the "reduced Stirling number of the second kind" is returned: \(S^{d}(n, k) = S(n - d + 1, k - d + 1)\) with \(n \ge k \ge d\). (This counts the ways to partition \(n\) consecutive integers into \(k\) groups with no pairwise difference less than \(d\). See example below.)要获得第一类有符号的斯特林数,请使用关键字
signed=True
. 使用此关键字自动设置kind
到1。实例
>>> from sympy.functions.combinatorial.numbers import stirling, bell >>> from sympy.combinatorics import Permutation >>> from sympy.utilities.iterables import multiset_partitions, permutations
第一种(默认为无符号):
>>> [stirling(6, i, kind=1) for i in range(7)] [0, 120, 274, 225, 85, 15, 1] >>> perms = list(permutations(range(4))) >>> [sum(Permutation(p).cycles == i for p in perms) for i in range(5)] [0, 6, 11, 6, 1] >>> [stirling(4, i, kind=1) for i in range(5)] [0, 6, 11, 6, 1]
第一类(签字):
>>> [stirling(4, i, signed=True) for i in range(5)] [0, -6, 11, -6, 1]
第二种:
>>> [stirling(10, i) for i in range(12)] [0, 1, 511, 9330, 34105, 42525, 22827, 5880, 750, 45, 1, 0] >>> sum(_) == bell(10) True >>> len(list(multiset_partitions(range(4), 2))) == stirling(4, 2) True
减少的第二类:
>>> from sympy import subsets, oo >>> def delta(p): ... if len(p) == 1: ... return oo ... return min(abs(i[0] - i[1]) for i in subsets(p, 2)) >>> parts = multiset_partitions(range(5), 3) >>> d = 2 >>> sum(1 for p in parts if all(delta(i) >= d for i in p)) 7 >>> stirling(5, 3, 2) 7
工具书类
枚举#
有三种功能。它们中的每一个都试图有效地计算给定集合或多集的给定组合量,这些集合或多集可以作为整数、序列或多集(以元素为键、重数为值的字典)。这个 k
参数指示要拾取的元素数(或要创建的分区数)。什么时候? k
为“无”,是所有枚举的总和 k
(从0到 n
)返回。A replacement
参数被识别为组合和排列;这表示任何项都可能以与原始集合中的项数相同的重数出现。
>>> from sympy.functions.combinatorial.numbers import nC, nP, nT
>>> items = 'baby'
- sympy.functions.combinatorial.numbers.nC(n, k=None, replacement=False)[源代码]#
返回
n
采取的项目k
一次。的可能值
n
:整数-长度集
n
序列-内部转换为多集
多集{element:multiplicity}
如果
k
是None则长度为0到中表示的项数的所有组合的总和n
将被退回。如果
replacement
为True,则给定项可以在k
项目。(例如,对于“ab”,2的集合将包括“aa”、“ab”和“bb”。)中元素的多重性n
当replacement
为True,但考虑元素总数,因为没有元素的出现次数超过n
.实例
>>> from sympy.functions.combinatorial.numbers import nC >>> from sympy.utilities.iterables import multiset_combinations >>> nC(3, 2) 3 >>> nC('abc', 2) 3 >>> nC('aab', 2) 2
什么时候?
replacement
为True,则每个项的重数可以等于n
:>>> nC('aabc', replacement=True) 35 >>> [len(list(multiset_combinations('aaaabbbbcccc', i))) for i in range(5)] [1, 3, 6, 10, 15] >>> sum(_) 35
如果有
k
具有多重性的项目m_1, m_2, ..., m_k
则长度为0到0的所有组合的总和k
是产品,(m_1 + 1)*(m_2 + 1)*...*(m_k + 1)
. 当每个项目的多重性为1(即k个唯一项目)时,则有2个**k个组合。例如,如果有4个唯一项,则组合总数为16:>>> sum(nC(4, i) for i in range(5)) 16
工具书类
- sympy.functions.combinatorial.numbers.nP(n, k=None, replacement=False)[源代码]#
返回
n
采取的项目k
一次。的可能值
n
:整数-长度集
n
序列-内部转换为多集
多集{element:multiplicity}
如果
k
是None则是长度为0到表示的项数的所有排列的总和n
将被退回。如果
replacement
为True,则给定项可以在k
项目。(例如,对于“ab”,2的置换将包括“aa”、“ab”、“ba”和“bb”。)中元素的多重性n
当replacement
为True,但考虑元素总数,因为没有元素的出现次数超过n
.实例
>>> from sympy.functions.combinatorial.numbers import nP >>> from sympy.utilities.iterables import multiset_permutations, multiset >>> nP(3, 2) 6 >>> nP('abc', 2) == nP(multiset('abc'), 2) == 6 True >>> nP('aab', 2) 3 >>> nP([1, 2, 2], 2) 3 >>> [nP(3, i) for i in range(4)] [1, 3, 6, 6] >>> nP(3) == sum(_) True
什么时候?
replacement
为True,则每个项的重数可以等于n
:>>> nP('aabc', replacement=True) 121 >>> [len(list(multiset_permutations('aaaabbbbcccc', i))) for i in range(5)] [1, 3, 9, 27, 81] >>> sum(_) 121
工具书类
- sympy.functions.combinatorial.numbers.nT(n, k=None)[源代码]#
返回
k
-分区大小n
项目。的可能值
n
:整数-
n
相同的项目序列-内部转换为多集
多集{element:multiplicity}
Note: the convention for
nT
is different than that ofnC
andnP
in that here an integer indicatesn
identical items instead of a set of lengthn
; this is in keeping with thepartitions
function which treats its integer-n
input like a list ofn
1s. One can userange(n)
forn
to indicaten
distinct items.如果
k
是None,则是对中表示的元素进行分区的方法总数n
将被退回。实例
>>> from sympy.functions.combinatorial.numbers import nT
给定多集的分区:
>>> [nT('aabbc', i) for i in range(1, 7)] [1, 8, 11, 5, 1, 0] >>> nT('aabbc') == sum(_) True
>>> [nT("mississippi", i) for i in range(1, 12)] [1, 74, 609, 1521, 1768, 1224, 579, 197, 50, 9, 1]
所有项目相同时的分区:
>>> [nT(5, i) for i in range(1, 6)] [1, 2, 2, 1, 1] >>> nT('1'*5) == sum(_) True
当所有项目都不同时:
>>> [nT(range(5), i) for i in range(1, 6)] [1, 15, 25, 10, 1] >>> nT(range(5)) == sum(_) True
用正整数和表示的整数的划分:
>>> from sympy import partition >>> partition(4) 5 >>> nT(4, 1) + nT(4, 2) + nT(4, 3) + nT(4, 4) 5 >>> nT('1'*4) 5
参见
sympy.utilities.iterables.partitions
,sympy.utilities.iterables.multiset_partitions
,sympy.functions.combinatorial.numbers.partition
工具书类