具体

超几何项

在递归求解和求和的过程中,超几何项占据了中心舞台。形式上,这些是由一阶线性递归算子消灭的序列。简单来说,如果我们给定项 \(a(n)\),那么如果其连续项比率是 \(n\) 中的有理函数,它就是超几何的。

要检查一个序列是否属于这种类型,你可以使用 is_hypergeometric 方法,该方法在 Basic 类中可用。以下是一个涉及多项式的简单示例:

>>> from sympy import *
>>> n, k = symbols('n,k')
>>> (n**2 + 1).is_hypergeometric(n)
True

当然,多项式是超几何的,但是否有更复杂的此类序列呢?这里有一些简单的例子:

>>> factorial(n).is_hypergeometric(n)
True
>>> binomial(n, k).is_hypergeometric(n)
True
>>> rf(n, k).is_hypergeometric(n)
True
>>> ff(n, k).is_hypergeometric(n)
True
>>> gamma(n).is_hypergeometric(n)
True
>>> (2**n).is_hypergeometric(n)
True

我们看到,在具体数学的求和和其他部分中使用的所有物种都是超几何的。还要注意,二项式系数以及上升和下降阶乘在其两个参数中都是超几何的:

>>> binomial(n, k).is_hypergeometric(k)
True
>>> rf(n, k).is_hypergeometric(k)
True
>>> ff(n, k).is_hypergeometric(k)
True

进一步说,所有之前展示的例子对于整数线性参数都是有效的:

>>> factorial(2*n).is_hypergeometric(n)
True
>>> binomial(3*n+1, k).is_hypergeometric(n)
True
>>> rf(n+1, k-1).is_hypergeometric(n)
True
>>> ff(n-1, k+1).is_hypergeometric(n)
True
>>> gamma(5*n).is_hypergeometric(n)
True
>>> (2**(n-7)).is_hypergeometric(n)
True

然而,非线性参数使得这些序列无法成为超几何序列:

>>> factorial(n**2).is_hypergeometric(n)
False
>>> (2**(n**3 + 1)).is_hypergeometric(n)
False

如果不仅需要知道是否为超几何项,可以使用 hypersimp() 函数。它将尝试简化组合表达式,如果给定的项是超几何的,它将返回一个最小次数的多项式商。否则,它将返回 \(None\) 以表示该序列不是超几何的:

>>> hypersimp(factorial(2*n), n)
2*(n + 1)*(2*n + 1)
>>> hypersimp(factorial(n**2), n)

具体类参考

class sympy.concrete.summations.Sum(function, *symbols, **assumptions)[源代码][源代码]

表示未求和的求和符号。

属性:
args

返回 ‘self’ 的参数元组。

assumptions0

返回对象 \(type\) 假设。

bound_symbols

只返回虚拟变量。

canonical_variables

返回一个字典,将 self.bound_symbols 中定义的任何变量映射到与表达式中任何自由符号不冲突的符号。

expr_free_symbols

类似于 free_symbols,但仅在自由符号包含在表达式节点中时返回它们。

free_symbols

此方法返回对象中的符号,排除那些具有特定值的符号(即

func

表达式中的顶级函数。

function

返回函数在极限范围内的应用。

has_empty_sequence

如果对空序列计算和或积,则返回 True。

has_finite_limits

如果通过显式边界、边界假设或变量假设得知边界是有限的,则返回 True。

has_reversed_limits

如果界限已知是反向顺序,无论是通过明确的界限、对界限的假设,还是对变量的假设,则返回 True。

is_algebraic
is_antihermitian
is_commutative
is_comparable

如果 self 可以计算为一个具有精度的实数(或已经是一个实数),则返回 True,否则返回 False。

is_complex
is_composite
is_even
is_extended_negative
is_extended_nonnegative
is_extended_nonpositive
is_extended_nonzero
is_extended_positive
is_extended_real
is_finite
is_hermitian
is_imaginary
is_infinite
is_integer
is_irrational
is_negative
is_noninteger
is_nonnegative
is_nonpositive
is_nonzero
is_number

如果 Sum 没有自由符号,则返回 True,否则返回 False。

is_odd
is_polar
is_positive
is_prime
is_rational
is_real
is_transcendental
is_zero
种类
limits

返回表达式的限制。

variables

返回一个限制变量的列表。

方法

apart([x])

请参阅 sympy.polys 中的 apart 函数。

args_cnc([cset, warn, split_1])

返回 [交换因子, 非交换因子] 的自身。

as_coeff_Add([rational])

高效地提取求和的系数。

as_coeff_Mul([rational])

高效地提取乘积的系数。

as_coeff_add(*deps)

返回元组 (c, args),其中 self 被写成一个 Add,a

as_coeff_exponent(x)

c*x**e -> c,e 其中 x 可以是任何符号表达式。

as_coeff_mul(*deps, **kwargs)

返回元组 (c, args),其中 self 被写成一个 Mul,m

as_coefficient(expr)

提取给定表达式中的符号系数。

as_coefficients_dict(*syms)

返回一个将术语映射到其有理数系数的字典。

as_content_primitive([radical, clear])

此方法应递归地从所有参数中移除一个 Rational,并返回该内容和新的自身(原始类型)。

as_dummy()

返回表达式,其中任何具有结构绑定符号的对象都被替换为在其出现的对象中唯一的规范符号,并且仅对交换性具有默认假设为True。

as_expr(*gens)

将多项式转换为 SymPy 表达式。

as_independent(*deps, **hint)

将 Mul 或 Add 的大部分天真分离为不依赖于 deps 的参数。

as_leading_term(*symbols[, logx, cdir])

返回自身级数展开的领先(非零)项。

as_numer_denom()

返回一个表达式的分子和分母。

as_ordered_factors([order])

返回有序因子列表(如果是 Mul),否则返回 [self]。

as_ordered_terms([order, data])

将表达式转换为有序的项列表。

as_poly(*gens, **args)

self 转换为多项式,或返回 None

as_powers_dict()

将自身作为一个因子的字典返回,每个因子都被视为一个幂。

as_real_imag([deep])

对 'self' 执行复杂的扩展,并返回一个包含收集到的实部和虚部的元组。

as_terms()

将一个表达式转换为项的列表。

aseries([x, n, bound, hir])

自变量的渐近级数展开

atoms(*types)

返回构成当前对象的原子。

cancel(*gens, **args)

参见 sympy.polys 中的取消函数

change_index(var, trafo[, newvar])

更改求和或乘积的索引。

class_key()

类的好顺序。

coeff(x[, n, right, _first])

返回包含 x**n 的项中的系数。

collect(syms[, func, evaluate, exact, ...])

请参阅 sympy.simplify 中的 collect 函数。

combsimp()

请参阅 sympy.simplify 中的 combsimp 函数。

compare(other)

如果对象在规范意义上小于、等于或大于其他对象,则返回 -1、0、1。

compute_leading_term(x[, logx])

已弃用的函数,用于计算级数的首项。

conjugate()

返回 'self' 的复数共轭。

could_extract_minus_sign()

如果 self 以 -1 作为前导因子,或在求和中有比正号更多的负号,则返回 True,否则返回 False。

count(query)

计算匹配的子表达式的数量。

count_ops([visual])

doit(**hints)

dummy_eq(other[, symbol])

比较两个表达式并处理哑符号。

equals(other[, failing_expression])

如果 self == other 则返回 True,如果不相等则返回 False,或者返回 None。

euler_maclaurin([m, n, eps, eval_integral])

返回一个欧拉-麦克劳林近似值,其中 m 是直接求和的前导项数,n 是尾部的项数。

eval_zeta_function(f, limits)

检查函数是否与zeta函数匹配。

evalf([n, subs, maxn, chop, strict, quad, ...])

将给定的公式计算到 n 位精度。

expand([deep, modulus, power_base, ...])

使用提示扩展表达式。

extract_additively(c)

如果可以从自身减去 c 并且使所有匹配的系数趋向于零,则返回 self - c,否则返回 None。

extract_branch_factor([allow_half])

尝试以 exp_polar(2*pi*I*n)*z 的方式优雅地表达自身。

extract_multiplicatively(c)

如果无法以一种良好的方式将 self 表示为 c * 某个东西,即保留 self 参数的属性,则返回 None。

factor(*gens, **args)

参见 sympy.polys.polytools 中的 factor() 函数

find(query[, group])

查找所有匹配查询的子表达式。

fourier_series([limits])

计算自身的傅里叶正弦/余弦级数

fps([x, x0, dir, hyper, order, rational, full])

计算自身的形式幂级数。

fromiter(args, **assumptions)

从可迭代对象创建一个新对象。

gammasimp()

参见 sympy.simplify 中的 gammasimp 函数

getO()

如果有加法 O(..) 符号,则返回该符号,否则返回 None。

getn()

返回表达式的顺序。

has(*patterns)

测试是否有任何子表达式匹配任何模式。

has_free(*patterns)

如果 self 包含对象 x 作为自由表达式,则返回 True,否则返回 False。

has_xfree(s)

如果 self 有 s 中的任何一个模式作为自由参数,则返回 True,否则返回 False。

index(x)

返回虚拟变量在限制列表中的索引。

integrate(*args, **kwargs)

请参阅 sympy.integrals 中的 integrate 函数。

invert(g, *gens, **args)

返回 selfg 的乘法逆元,其中 self``(和 ``g)可以是符号表达式。

is_absolutely_convergent()

检查无穷级数的绝对收敛性。

is_algebraic_expr(*syms)

此测试给定的表达式是否在给定的符号 syms 中是代数的。

is_constant(*wrt, **flags)

如果 self 是常量则返回 True,如果不是则返回 False,如果无法明确确定常量性则返回 None。

is_convergent()

检查一个和的收敛性。

is_meromorphic(x, a)

此测试表达式是否作为给定符号 x 的函数在点 a 处是亚纯的。

is_polynomial(*syms)

如果 self 是 syms 中的多项式,则返回 True,否则返回 False。

is_rational_function(*syms)

测试函数是否为给定符号 syms 中的两个多项式的比率。

is_same(b[, approx])

如果 a 和 b 结构相同则返回 True,否则返回 False。

leadterm(x[, logx, cdir])

返回前导项 a*x**b 作为元组 (a, b)。

limit(x, xlim[, dir])

计算极限 x->xlim。

lseries([x, x0, dir, logx, cdir])

用于生成序列项的迭代器的包装器。

match(pattern[, old])

模式匹配。

matches(expr[, repl_dict, old])

用于 match() 的辅助方法,用于在 self 中的通配符符号与 expr 中的表达式之间寻找匹配。

n([n, subs, maxn, chop, strict, quad, verbose])

将给定的公式计算到 n 位精度。

normal()

将表达式作为分数返回。

nseries([x, x0, n, dir, logx, cdir])

如果假设允许,则包装到 _eval_nseries,否则包装到 series。

nsimplify([constants, tolerance, full])

参见 sympy.simplify 中的 nsimplify 函数

powsimp(*args, **kwargs)

请参阅 sympy.simplify 中的 powsimp 函数

primitive()

返回可以从自身每个项中非递归提取的正有理数(即,将自身视为一个加法)。

radsimp(**kwargs)

参见 sympy.simplify 中的 radsimp 函数

ratsimp()

参见 sympy.simplify 中的 ratsimp 函数

rcall(*args)

通过表达式树递归应用于参数。

refine([assumption])

请参阅 sympy.assumptions 中的 refine 函数。

removeO()

如果存在,移除加性的 O(..) 符号

reorder(*arg)

在包含求和或乘积的表达式中重新排序限制。

reorder_limit(x, y)

交换一个 Sum 或 Product 表达式的两个限制元组。

replace(query, value[, map, simultaneous, exact])

self 中匹配的子表达式替换为 value

reverse_order(*indices)

反转求和中的极限顺序。

rewrite(*args[, deep])

使用定义的规则重写 self

round([n])

返回 x 四舍五入到给定的十进制位数。

separate([deep, force])

参见 sympy.simplify 中的单独函数

series([x, x0, n, dir, logx, cdir])

x = x0 附近对 "self" 进行级数展开,当 n=None 时逐项给出级数项(即惰性级数),否则当 n != None 时一次性给出所有项。

simplify(**kwargs)

请参阅 sympy.simplify 中的 simplify 函数。

sort_key([order])

subs(*args, **kwargs)

在简化参数后,在表达式中用新内容替换旧内容。

taylor_term(n, x, *previous_terms)

泰勒项的一般方法。

together(*args, **kwargs)

请参阅 sympy.polys 中的 together 函数。

trigsimp(**args)

参见 sympy.simplify 中的 trigsimp 函数

xreplace(rule)

替换表达式中对象的出现。

伴随

as_base_exp

复制

差异

目录

is_hypergeometric

转置

参考文献

[1]

Michael Karr, “有限项的求和”,《ACM期刊》,第28卷第2期,1981年4月,第305-350页 https://dl.acm.org/doi/10.1145/322248.322255

示例

>>> from sympy.abc import i, k, m, n, x
>>> from sympy import Sum, factorial, oo, IndexedBase, Function
>>> Sum(k, (k, 1, m))
Sum(k, (k, 1, m))
>>> Sum(k, (k, 1, m)).doit()
m**2/2 + m/2
>>> Sum(k**2, (k, 1, m))
Sum(k**2, (k, 1, m))
>>> Sum(k**2, (k, 1, m)).doit()
m**3/3 + m**2/2 + m/6
>>> Sum(x**k, (k, 0, oo))
Sum(x**k, (k, 0, oo))
>>> Sum(x**k, (k, 0, oo)).doit()
Piecewise((1/(1 - x), Abs(x) < 1), (Sum(x**k, (k, 0, oo)), True))
>>> Sum(x**k/factorial(k), (k, 0, oo)).doit()
exp(x)

以下是使用符号索引进行求和的示例。你可以使用 IndexedBase 类的 Function 或 IndexedBase 类:

>>> f = Function('f')
>>> Sum(f(n), (n, 0, 3)).doit()
f(0) + f(1) + f(2) + f(3)
>>> Sum(f(n), (n, 0, oo)).doit()
Sum(f(n), (n, 0, oo))
>>> f = IndexedBase('f')
>>> Sum(f[n]**2, (n, 0, 3)).doit()
f[0]**2 + f[1]**2 + f[2]**2 + f[3]**2

一个例子展示了即使对于看似无意义的极限值,求和的符号结果仍然有效。然后,Karr 约定允许我们根据上述规则交换极限,从而对这些和给出完全有效的解释:

>>> S = Sum(i, (i, 1, n)).doit()
>>> S
n**2/2 + n/2
>>> S.subs(n, -4)
6
>>> Sum(i, (i, 1, -4)).doit()
6
>>> Sum(-i, (i, -3, 0)).doit()
6

Karr 求和约定的显式示例:

>>> S1 = Sum(i**2, (i, m, m+n-1)).doit()
>>> S1
m**2*n + m*n**2 - m*n + n**3/3 - n**2/2 + n/6
>>> S2 = Sum(i**2, (i, m+n, m-1)).doit()
>>> S2
-m**2*n - m*n**2 + m*n - n**3/3 + n**2/2 - n/6
>>> S1 + S2
0
>>> S3 = Sum(i, (i, m, m-1)).doit()
>>> S3
0
euler_maclaurin(
m=0,
n=0,
eps=0,
eval_integral=True,
)[源代码][源代码]

返回一个欧拉-麦克劳林近似值,其中 m 是直接求和的前导项数,n 是尾部的项数。

当 m = n = 0 时,这仅仅是相应的积分加上一阶端点修正。

返回 (s, e),其中 s 是欧拉-麦克劳林近似值,e 是估计的误差(取为尾部中第一个省略项的幅度):

>>> from sympy.abc import k, a, b
>>> from sympy import Sum
>>> Sum(1/k, (k, 2, 5)).doit().evalf()
1.28333333333333
>>> s, e = Sum(1/k, (k, 2, 5)).euler_maclaurin()
>>> s
-log(2) + 7/20 + log(5)
>>> from sympy import sstr
>>> print(sstr((s.evalf(), e.evalf()), full_prec=True))
(1.26629073187415, 0.0175000000000000)

端点可以是符号性的:

>>> s, e = Sum(1/k, (k, a, b)).euler_maclaurin()
>>> s
-log(a) + log(b) + 1/(2*b) + 1/(2*a)
>>> e
Abs(1/(12*b**2) - 1/(12*a**2))

如果函数是一个最高次数为 2n+1 的多项式,欧拉-麦克劳林公式将变得精确(并且返回 e = 0):

>>> Sum(k, (k, 2, b)).euler_maclaurin()
(b**2/2 + b/2 - 1, 0)
>>> Sum(k, (k, 2, b)).doit()
b**2/2 + b/2 - 1

当指定了一个非零的 eps 时,求和会在余项小于 epsilon 时立即结束。

eval_zeta_function(f, limits)[源代码][源代码]

检查函数是否与zeta函数匹配。

如果匹配,则返回一个 \(Piecewise\) 表达式,因为 zeta 函数在 \(s > 1\)\(q > 0\) 时才收敛。

is_absolutely_convergent()[源代码][源代码]

检查无穷级数的绝对收敛性。

与检查无穷级数的 sequence_term 的绝对值的收敛性相同。

参考文献

示例

>>> from sympy import Sum, Symbol, oo
>>> n = Symbol('n', integer=True)
>>> Sum((-1)**n, (n, 1, oo)).is_absolutely_convergent()
False
>>> Sum((-1)**n/n**2, (n, 1, oo)).is_absolutely_convergent()
True
is_convergent()[源代码][源代码]

检查一个和的收敛性。

参考文献

[1]

https://en.wikipedia.org/wiki/收敛性测试

示例

>>> from sympy import factorial, S, Sum, Symbol, oo
>>> n = Symbol('n', integer=True)
>>> Sum(n/(n - 1), (n, 4, 7)).is_convergent()
True
>>> Sum(n/(2*n + 1), (n, 1, oo)).is_convergent()
False
>>> Sum(factorial(n)/5**n, (n, 1, oo)).is_convergent()
False
>>> Sum(1/n**(S(6)/5), (n, 1, oo)).is_convergent()
True
reverse_order(*indices)[源代码][源代码]

反转求和中的极限顺序。

参考文献

[1]

Michael Karr, “有限项的求和”,《ACM期刊》,第28卷第2期,1981年4月,第305-350页 https://dl.acm.org/doi/10.1145/322248.322255

示例

>>> from sympy import Sum
>>> from sympy.abc import x, y, a, b, c, d
>>> Sum(x, (x, 0, 3)).reverse_order(x)
Sum(-x, (x, 4, -1))
>>> Sum(x*y, (x, 1, 5), (y, 0, 6)).reverse_order(x, y)
Sum(x*y, (x, 6, 0), (y, 7, -1))
>>> Sum(x, (x, a, b)).reverse_order(x)
Sum(-x, (x, b + 1, a - 1))
>>> Sum(x, (x, a, b)).reverse_order(0)
Sum(-x, (x, b + 1, a - 1))

虽然指定要反转的限制时应优先使用变量名,但当存在多个同名符号时,索引计数表示法会非常方便。

>>> S = Sum(x**2, (x, a, b), (x, c, d))
>>> S
Sum(x**2, (x, a, b), (x, c, d))
>>> S0 = S.reverse_order(0)
>>> S0
Sum(-x**2, (x, b + 1, a - 1), (x, c, d))
>>> S1 = S0.reverse_order(1)
>>> S1
Sum(x**2, (x, b + 1, a - 1), (x, d + 1, c - 1))

当然,我们可以混合使用这两种表示法:

>>> Sum(x*y, (x, a, b), (y, 2, 5)).reverse_order(x, 1)
Sum(x*y, (x, b + 1, a - 1), (y, 6, 1))
>>> Sum(x*y, (x, a, b), (y, 2, 5)).reverse_order(y, x)
Sum(x*y, (x, b + 1, a - 1), (y, 6, 1))
class sympy.concrete.products.Product(function, *symbols, **assumptions)[源代码][源代码]

表示未计算的产品。

属性:
args

返回 ‘self’ 的参数元组。

assumptions0

返回对象 \(type\) 假设。

bound_symbols

只返回虚拟变量。

canonical_variables

返回一个字典,将 self.bound_symbols 中定义的任何变量映射到与表达式中任何自由符号不冲突的符号。

expr_free_symbols

类似于 free_symbols,但仅在自由符号包含在表达式节点中时返回它们。

free_symbols

此方法返回对象中的符号,排除那些具有特定值的符号(即

func

表达式中的顶级函数。

函数
has_empty_sequence

如果对空序列计算和或积,则返回 True。

has_finite_limits

如果通过显式边界、边界假设或变量假设得知边界是有限的,则返回 True。

has_reversed_limits

如果界限已知是反向顺序,无论是通过明确的界限、对界限的假设,还是对变量的假设,则返回 True。

is_algebraic
is_antihermitian
is_commutative
is_comparable

如果 self 可以计算为一个具有精度的实数(或已经是一个实数),则返回 True,否则返回 False。

is_complex
is_composite
is_even
is_extended_negative
is_extended_nonnegative
is_extended_nonpositive
is_extended_nonzero
is_extended_positive
is_extended_real
is_finite
is_hermitian
is_imaginary
is_infinite
is_integer
is_irrational
is_negative
is_noninteger
is_nonnegative
is_nonpositive
is_nonzero
is_number

如果 Sum 没有自由符号,则返回 True,否则返回 False。

is_odd
is_polar
is_positive
is_prime
is_rational
is_real
is_transcendental
is_zero
种类
limits

返回表达式的限制。

术语
variables

返回一个限制变量的列表。

方法

apart([x])

请参阅 sympy.polys 中的 apart 函数。

args_cnc([cset, warn, split_1])

返回 [交换因子, 非交换因子] 的自身。

as_coeff_Add([rational])

高效地提取求和的系数。

as_coeff_Mul([rational])

高效地提取乘积的系数。

as_coeff_add(*deps)

返回元组 (c, args),其中 self 被写成一个 Add,a

as_coeff_exponent(x)

c*x**e -> c,e 其中 x 可以是任何符号表达式。

as_coeff_mul(*deps, **kwargs)

返回元组 (c, args),其中 self 被写成一个 Mul,m

as_coefficient(expr)

提取给定表达式中的符号系数。

as_coefficients_dict(*syms)

返回一个将术语映射到其有理数系数的字典。

as_content_primitive([radical, clear])

此方法应递归地从所有参数中移除一个 Rational,并返回该内容和新的自身(原始类型)。

as_dummy()

返回表达式,其中任何具有结构绑定符号的对象都被替换为在其出现的对象中唯一的规范符号,并且仅对交换性具有默认假设为True。

as_expr(*gens)

将多项式转换为 SymPy 表达式。

as_independent(*deps, **hint)

将 Mul 或 Add 的大部分天真分离为不依赖于 deps 的参数。

as_leading_term(*symbols[, logx, cdir])

返回自身级数展开的领先(非零)项。

as_numer_denom()

返回一个表达式的分子和分母。

as_ordered_factors([order])

返回有序因子列表(如果是 Mul),否则返回 [self]。

as_ordered_terms([order, data])

将表达式转换为有序的项列表。

as_poly(*gens, **args)

self 转换为多项式,或返回 None

as_powers_dict()

将自身作为一个因子的字典返回,每个因子都被视为一个幂。

as_real_imag([deep])

对 'self' 执行复杂的扩展,并返回一个包含收集到的实部和虚部的元组。

as_terms()

将一个表达式转换为项的列表。

aseries([x, n, bound, hir])

自变量的渐近级数展开

atoms(*types)

返回构成当前对象的原子。

cancel(*gens, **args)

参见 sympy.polys 中的取消函数

change_index(var, trafo[, newvar])

更改求和或乘积的索引。

class_key()

类的好顺序。

coeff(x[, n, right, _first])

返回包含 x**n 的项中的系数。

collect(syms[, func, evaluate, exact, ...])

请参阅 sympy.simplify 中的 collect 函数。

combsimp()

请参阅 sympy.simplify 中的 combsimp 函数。

compare(other)

如果对象在规范意义上小于、等于或大于其他对象,则返回 -1、0、1。

compute_leading_term(x[, logx])

已弃用的函数,用于计算级数的首项。

conjugate()

返回 'self' 的复数共轭。

could_extract_minus_sign()

如果 self 以 -1 作为前导因子,或在求和中有比正号更多的负号,则返回 True,否则返回 False。

count(query)

计算匹配的子表达式的数量。

count_ops([visual])

doit(**hints)

dummy_eq(other[, symbol])

比较两个表达式并处理哑符号。

equals(other[, failing_expression])

如果 self == other 则返回 True,如果不相等则返回 False,或者返回 None。

evalf([n, subs, maxn, chop, strict, quad, ...])

将给定的公式计算到 n 位精度。

expand([deep, modulus, power_base, ...])

使用提示扩展表达式。

extract_additively(c)

如果可以从自身减去 c 并且使所有匹配的系数趋向于零,则返回 self - c,否则返回 None。

extract_branch_factor([allow_half])

尝试以 exp_polar(2*pi*I*n)*z 的方式优雅地表达自身。

extract_multiplicatively(c)

如果无法以一种良好的方式将 self 表示为 c * 某个东西,即保留 self 参数的属性,则返回 None。

factor(*gens, **args)

参见 sympy.polys.polytools 中的 factor() 函数

find(query[, group])

查找所有匹配查询的子表达式。

fourier_series([limits])

计算自身的傅里叶正弦/余弦级数

fps([x, x0, dir, hyper, order, rational, full])

计算自身的形式幂级数。

fromiter(args, **assumptions)

从可迭代对象创建一个新对象。

gammasimp()

参见 sympy.simplify 中的 gammasimp 函数

getO()

如果有加法 O(..) 符号,则返回该符号,否则返回 None。

getn()

返回表达式的顺序。

has(*patterns)

测试是否有任何子表达式匹配任何模式。

has_free(*patterns)

如果 self 包含对象 x 作为自由表达式,则返回 True,否则返回 False。

has_xfree(s)

如果 self 有 s 中的任何一个模式作为自由参数,则返回 True,否则返回 False。

index(x)

返回虚拟变量在限制列表中的索引。

integrate(*args, **kwargs)

请参阅 sympy.integrals 中的 integrate 函数。

invert(g, *gens, **args)

返回 selfg 的乘法逆元,其中 self``(和 ``g)可以是符号表达式。

is_algebraic_expr(*syms)

此测试给定的表达式是否在给定的符号 syms 中是代数的。

is_constant(*wrt, **flags)

如果 self 是常量则返回 True,如果不是则返回 False,如果无法明确确定常量性则返回 None。

is_convergent()

See docs of Sum.is_convergent() for explanation of convergence in SymPy.

is_meromorphic(x, a)

此测试表达式是否作为给定符号 x 的函数在点 a 处是亚纯的。

is_polynomial(*syms)

如果 self 是 syms 中的多项式,则返回 True,否则返回 False。

is_rational_function(*syms)

测试函数是否为给定符号 syms 中的两个多项式的比率。

is_same(b[, approx])

如果 a 和 b 结构相同则返回 True,否则返回 False。

leadterm(x[, logx, cdir])

返回前导项 a*x**b 作为元组 (a, b)。

limit(x, xlim[, dir])

计算极限 x->xlim。

lseries([x, x0, dir, logx, cdir])

用于生成序列项的迭代器的包装器。

match(pattern[, old])

模式匹配。

matches(expr[, repl_dict, old])

用于 match() 的辅助方法,用于在 self 中的通配符符号与 expr 中的表达式之间寻找匹配。

n([n, subs, maxn, chop, strict, quad, verbose])

将给定的公式计算到 n 位精度。

normal()

将表达式作为分数返回。

nseries([x, x0, n, dir, logx, cdir])

如果假设允许,则包装到 _eval_nseries,否则包装到 series。

nsimplify([constants, tolerance, full])

参见 sympy.simplify 中的 nsimplify 函数

powsimp(*args, **kwargs)

请参阅 sympy.simplify 中的 powsimp 函数

primitive()

返回可以从自身每个项中非递归提取的正有理数(即,将自身视为一个加法)。

radsimp(**kwargs)

参见 sympy.simplify 中的 radsimp 函数

ratsimp()

参见 sympy.simplify 中的 ratsimp 函数

rcall(*args)

通过表达式树递归应用于参数。

refine([assumption])

请参阅 sympy.assumptions 中的 refine 函数。

removeO()

如果存在,移除加性的 O(..) 符号

reorder(*arg)

在包含求和或乘积的表达式中重新排序限制。

reorder_limit(x, y)

交换一个 Sum 或 Product 表达式的两个限制元组。

replace(query, value[, map, simultaneous, exact])

self 中匹配的子表达式替换为 value

reverse_order(*indices)

反转产品中限制的顺序。

rewrite(*args[, deep])

使用定义的规则重写 self

round([n])

返回 x 四舍五入到给定的十进制位数。

separate([deep, force])

参见 sympy.simplify 中的单独函数

series([x, x0, n, dir, logx, cdir])

x = x0 附近对 "self" 进行级数展开,当 n=None 时逐项给出级数项(即惰性级数),否则当 n != None 时一次性给出所有项。

simplify(**kwargs)

请参阅 sympy.simplify 中的 simplify 函数。

sort_key([order])

subs(*args, **kwargs)

在简化参数后,在表达式中用新内容替换旧内容。

taylor_term(n, x, *previous_terms)

泰勒项的一般方法。

together(*args, **kwargs)

请参阅 sympy.polys 中的 together 函数。

trigsimp(**args)

参见 sympy.simplify 中的 trigsimp 函数

xreplace(rule)

替换表达式中对象的出现。

伴随

as_base_exp

复制

差异

目录

is_hypergeometric

转置

参见

Sum, summation
product

参考文献

[1]

Michael Karr, “有限项的求和”,《ACM期刊》,第28卷第2期,1981年4月,第305-350页 https://dl.acm.org/doi/10.1145/322248.322255

示例

>>> from sympy.abc import a, b, i, k, m, n, x
>>> from sympy import Product, oo
>>> Product(k, (k, 1, m))
Product(k, (k, 1, m))
>>> Product(k, (k, 1, m)).doit()
factorial(m)
>>> Product(k**2,(k, 1, m))
Product(k**2, (k, 1, m))
>>> Product(k**2,(k, 1, m)).doit()
factorial(m)**2

Wallis 的 pi 乘积公式:

>>> W = Product(2*i/(2*i-1) * 2*i/(2*i+1), (i, 1, oo))
>>> W
Product(4*i**2/((2*i - 1)*(2*i + 1)), (i, 1, oo))

直接计算目前失败:

>>> W.doit()
Product(4*i**2/((2*i - 1)*(2*i + 1)), (i, 1, oo))

但我们可以通过有限乘积的极限来接近无穷乘积:

>>> from sympy import limit
>>> W2 = Product(2*i/(2*i-1)*2*i/(2*i+1), (i, 1, n))
>>> W2
Product(4*i**2/((2*i - 1)*(2*i + 1)), (i, 1, n))
>>> W2e = W2.doit()
>>> W2e
4**n*factorial(n)**2/(2**(2*n)*RisingFactorial(1/2, n)*RisingFactorial(3/2, n))
>>> limit(W2e, n, oo)
pi/2

通过相同的公式,我们可以计算 sin(pi/2):

>>> from sympy import combsimp, pi, gamma, simplify
>>> P = pi * x * Product(1 - x**2/k**2, (k, 1, n))
>>> P = P.subs(x, pi/2)
>>> P
pi**2*Product(1 - pi**2/(4*k**2), (k, 1, n))/2
>>> Pe = P.doit()
>>> Pe
pi**2*RisingFactorial(1 - pi/2, n)*RisingFactorial(1 + pi/2, n)/(2*factorial(n)**2)
>>> limit(Pe, n, oo).gammasimp()
sin(pi**2/2)
>>> Pe.rewrite(gamma)
(-1)**n*pi**2*gamma(pi/2)*gamma(n + 1 + pi/2)/(2*gamma(1 + pi/2)*gamma(-n + pi/2)*gamma(n + 1)**2)

下限大于上限的产品:

>>> Product(1/i, (i, 6, 1)).doit()
120
>>> Product(i, (i, 2, 5)).doit()
120

空积:

>>> Product(i, (i, n, n-1)).doit()
1

一个例子展示了乘积的符号结果对于看似无意义的极限值仍然是有效的。然后,Karr 约定允许我们根据上述规则交换极限,从而对这些乘积给出完全有效的解释:

>>> P = Product(2, (i, 10, n)).doit()
>>> P
2**(n - 9)
>>> P.subs(n, 5)
1/16
>>> Product(2, (i, 10, 5)).doit()
1/16
>>> 1/Product(2, (i, 6, 9)).doit()
1/16

Karr 求和约定应用于乘积的显式示例:

>>> P1 = Product(x, (i, a, b)).doit()
>>> P1
x**(-a + b + 1)
>>> P2 = Product(x, (i, b+1, a-1)).doit()
>>> P2
x**(a - b - 1)
>>> simplify(P1 * P2)
1

另一个:

>>> P1 = Product(i, (i, b, a)).doit()
>>> P1
RisingFactorial(b, a - b + 1)
>>> P2 = Product(i, (i, a+1, b-1)).doit()
>>> P2
RisingFactorial(a + 1, -a + b - 1)
>>> P1 * P2
RisingFactorial(b, a - b + 1)*RisingFactorial(a + 1, -a + b - 1)
>>> combsimp(P1 * P2)
1
is_convergent()[源代码][源代码]

See docs of Sum.is_convergent() for explanation of convergence in SymPy.

参考文献

示例

>>> from sympy import Product, Symbol, cos, pi, exp, oo
>>> n = Symbol('n', integer=True)
>>> Product(n/(n + 1), (n, 1, oo)).is_convergent()
False
>>> Product(1/n**2, (n, 1, oo)).is_convergent()
False
>>> Product(cos(pi/n), (n, 1, oo)).is_convergent()
True
>>> Product(exp(-n**2), (n, 1, oo)).is_convergent()
False
reverse_order(*indices)[源代码][源代码]

反转产品中限制的顺序。

参考文献

[1]

Michael Karr, “有限项的求和”,《ACM期刊》,第28卷第2期,1981年4月,第305-350页 https://dl.acm.org/doi/10.1145/322248.322255

示例

>>> from sympy import gamma, Product, simplify, Sum
>>> from sympy.abc import x, y, a, b, c, d
>>> P = Product(x, (x, a, b))
>>> Pr = P.reverse_order(x)
>>> Pr
Product(1/x, (x, b + 1, a - 1))
>>> Pr = Pr.doit()
>>> Pr
1/RisingFactorial(b + 1, a - b - 1)
>>> simplify(Pr.rewrite(gamma))
Piecewise((gamma(b + 1)/gamma(a), b > -1), ((-1)**(-a + b + 1)*gamma(1 - a)/gamma(-b), True))
>>> P = P.doit()
>>> P
RisingFactorial(a, -a + b + 1)
>>> simplify(P.rewrite(gamma))
Piecewise((gamma(b + 1)/gamma(a), a > 0), ((-1)**(-a + b + 1)*gamma(1 - a)/gamma(-b), True))

虽然指定要反转的限制时应优先使用变量名,但当存在多个同名符号时,索引计数表示法会非常方便。

>>> S = Sum(x*y, (x, a, b), (y, c, d))
>>> S
Sum(x*y, (x, a, b), (y, c, d))
>>> S0 = S.reverse_order(0)
>>> S0
Sum(-x*y, (x, b + 1, a - 1), (y, c, d))
>>> S1 = S0.reverse_order(1)
>>> S1
Sum(x*y, (x, b + 1, a - 1), (y, d + 1, c - 1))

当然,我们可以混合使用这两种表示法:

>>> Sum(x*y, (x, a, b), (y, 2, 5)).reverse_order(x, 1)
Sum(x*y, (x, b + 1, a - 1), (y, 6, 1))
>>> Sum(x*y, (x, a, b), (y, 2, 5)).reverse_order(y, x)
Sum(x*y, (x, b + 1, a - 1), (y, 6, 1))
class sympy.concrete.expr_with_intlimits.ExprWithIntLimits(
function,
*symbols,
**assumptions,
)[源代码][源代码]

产品与和的超级类。

属性:
args

返回 ‘self’ 的参数元组。

assumptions0

返回对象 \(type\) 假设。

bound_symbols

只返回虚拟变量。

canonical_variables

返回一个字典,将 self.bound_symbols 中定义的任何变量映射到与表达式中任何自由符号不冲突的符号。

expr_free_symbols

类似于 free_symbols,但仅在自由符号包含在表达式节点中时返回它们。

free_symbols

此方法返回对象中的符号,排除那些具有特定值的符号(即

func

表达式中的顶级函数。

function

返回函数在极限范围内的应用。

has_empty_sequence

如果对空序列计算和或积,则返回 True。

has_finite_limits

如果通过显式边界、边界假设或变量假设得知边界是有限的,则返回 True。

has_reversed_limits

如果界限已知是反向顺序,无论是通过明确的界限、对界限的假设,还是对变量的假设,则返回 True。

is_algebraic
is_antihermitian
is_commutative
is_comparable

如果 self 可以计算为一个具有精度的实数(或已经是一个实数),则返回 True,否则返回 False。

is_complex
is_composite
is_even
is_extended_negative
is_extended_nonnegative
is_extended_nonpositive
is_extended_nonzero
is_extended_positive
is_extended_real
is_finite
is_hermitian
is_imaginary
is_infinite
is_integer
is_irrational
is_negative
is_noninteger
is_nonnegative
is_nonpositive
is_nonzero
is_number

如果 Sum 没有自由符号,则返回 True,否则返回 False。

is_odd
is_polar
is_positive
is_prime
is_rational
is_real
is_transcendental
is_zero
种类
limits

返回表达式的限制。

variables

返回一个限制变量的列表。

方法

apart([x])

请参阅 sympy.polys 中的 apart 函数。

args_cnc([cset, warn, split_1])

返回 [交换因子, 非交换因子] 的自身。

as_coeff_Add([rational])

高效地提取求和的系数。

as_coeff_Mul([rational])

高效地提取乘积的系数。

as_coeff_add(*deps)

返回元组 (c, args),其中 self 被写成一个 Add,a

as_coeff_exponent(x)

c*x**e -> c,e 其中 x 可以是任何符号表达式。

as_coeff_mul(*deps, **kwargs)

返回元组 (c, args),其中 self 被写成一个 Mul,m

as_coefficient(expr)

提取给定表达式中的符号系数。

as_coefficients_dict(*syms)

返回一个将术语映射到其有理数系数的字典。

as_content_primitive([radical, clear])

此方法应递归地从所有参数中移除一个 Rational,并返回该内容和新的自身(原始类型)。

as_dummy()

返回表达式,其中任何具有结构绑定符号的对象都被替换为在其出现的对象中唯一的规范符号,并且仅对交换性具有默认假设为True。

as_expr(*gens)

将多项式转换为 SymPy 表达式。

as_independent(*deps, **hint)

将 Mul 或 Add 的大部分天真分离为不依赖于 deps 的参数。

as_leading_term(*symbols[, logx, cdir])

返回自身级数展开的领先(非零)项。

as_numer_denom()

返回一个表达式的分子和分母。

as_ordered_factors([order])

返回有序因子列表(如果是 Mul),否则返回 [self]。

as_ordered_terms([order, data])

将表达式转换为有序的项列表。

as_poly(*gens, **args)

self 转换为多项式,或返回 None

as_powers_dict()

将自身作为一个因子的字典返回,每个因子都被视为一个幂。

as_real_imag([deep])

对 'self' 执行复杂的扩展,并返回一个包含收集到的实部和虚部的元组。

as_terms()

将一个表达式转换为项的列表。

aseries([x, n, bound, hir])

自变量的渐近级数展开

atoms(*types)

返回构成当前对象的原子。

cancel(*gens, **args)

参见 sympy.polys 中的取消函数

change_index(var, trafo[, newvar])

更改求和或乘积的索引。

class_key()

类的好顺序。

coeff(x[, n, right, _first])

返回包含 x**n 的项中的系数。

collect(syms[, func, evaluate, exact, ...])

请参阅 sympy.simplify 中的 collect 函数。

combsimp()

请参阅 sympy.simplify 中的 combsimp 函数。

compare(other)

如果对象在规范意义上小于、等于或大于其他对象,则返回 -1、0、1。

compute_leading_term(x[, logx])

已弃用的函数,用于计算级数的首项。

conjugate()

返回 'self' 的复数共轭。

could_extract_minus_sign()

如果 self 以 -1 作为前导因子,或在求和中有比正号更多的负号,则返回 True,否则返回 False。

count(query)

计算匹配的子表达式的数量。

count_ops([visual])

doit(**hints)

评估默认情况下不评估的对象,如极限、积分、求和和乘积。

dummy_eq(other[, symbol])

比较两个表达式并处理哑符号。

equals(other[, failing_expression])

如果 self == other 则返回 True,如果不相等则返回 False,或者返回 None。

evalf([n, subs, maxn, chop, strict, quad, ...])

将给定的公式计算到 n 位精度。

expand([deep, modulus, power_base, ...])

使用提示扩展表达式。

extract_additively(c)

如果可以从自身减去 c 并且使所有匹配的系数趋向于零,则返回 self - c,否则返回 None。

extract_branch_factor([allow_half])

尝试以 exp_polar(2*pi*I*n)*z 的方式优雅地表达自身。

extract_multiplicatively(c)

如果无法以一种良好的方式将 self 表示为 c * 某个东西,即保留 self 参数的属性,则返回 None。

factor(*gens, **args)

参见 sympy.polys.polytools 中的 factor() 函数

find(query[, group])

查找所有匹配查询的子表达式。

fourier_series([limits])

计算自身的傅里叶正弦/余弦级数

fps([x, x0, dir, hyper, order, rational, full])

计算自身的形式幂级数。

fromiter(args, **assumptions)

从可迭代对象创建一个新对象。

gammasimp()

参见 sympy.simplify 中的 gammasimp 函数

getO()

如果有加法 O(..) 符号,则返回该符号,否则返回 None。

getn()

返回表达式的顺序。

has(*patterns)

测试是否有任何子表达式匹配任何模式。

has_free(*patterns)

如果 self 包含对象 x 作为自由表达式,则返回 True,否则返回 False。

has_xfree(s)

如果 self 有 s 中的任何一个模式作为自由参数,则返回 True,否则返回 False。

index(x)

返回虚拟变量在限制列表中的索引。

integrate(*args, **kwargs)

请参阅 sympy.integrals 中的 integrate 函数。

invert(g, *gens, **args)

返回 selfg 的乘法逆元,其中 self``(和 ``g)可以是符号表达式。

is_algebraic_expr(*syms)

此测试给定的表达式是否在给定的符号 syms 中是代数的。

is_constant(*wrt, **flags)

如果 self 是常量则返回 True,如果不是则返回 False,如果无法明确确定常量性则返回 None。

is_meromorphic(x, a)

此测试表达式是否作为给定符号 x 的函数在点 a 处是亚纯的。

is_polynomial(*syms)

如果 self 是 syms 中的多项式,则返回 True,否则返回 False。

is_rational_function(*syms)

测试函数是否为给定符号 syms 中的两个多项式的比率。

is_same(b[, approx])

如果 a 和 b 结构相同则返回 True,否则返回 False。

leadterm(x[, logx, cdir])

返回前导项 a*x**b 作为元组 (a, b)。

limit(x, xlim[, dir])

计算极限 x->xlim。

lseries([x, x0, dir, logx, cdir])

用于生成序列项的迭代器的包装器。

match(pattern[, old])

模式匹配。

matches(expr[, repl_dict, old])

用于 match() 的辅助方法,用于在 self 中的通配符符号与 expr 中的表达式之间寻找匹配。

n([n, subs, maxn, chop, strict, quad, verbose])

将给定的公式计算到 n 位精度。

normal()

将表达式作为分数返回。

nseries([x, x0, n, dir, logx, cdir])

如果假设允许,则包装到 _eval_nseries,否则包装到 series。

nsimplify([constants, tolerance, full])

参见 sympy.simplify 中的 nsimplify 函数

powsimp(*args, **kwargs)

请参阅 sympy.simplify 中的 powsimp 函数

primitive()

返回可以从自身每个项中非递归提取的正有理数(即,将自身视为一个加法)。

radsimp(**kwargs)

参见 sympy.simplify 中的 radsimp 函数

ratsimp()

参见 sympy.simplify 中的 ratsimp 函数

rcall(*args)

通过表达式树递归应用于参数。

refine([assumption])

请参阅 sympy.assumptions 中的 refine 函数。

removeO()

如果存在,移除加性的 O(..) 符号

reorder(*arg)

在包含求和或乘积的表达式中重新排序限制。

reorder_limit(x, y)

交换一个 Sum 或 Product 表达式的两个限制元组。

replace(query, value[, map, simultaneous, exact])

self 中匹配的子表达式替换为 value

rewrite(*args[, deep])

使用定义的规则重写 self

round([n])

返回 x 四舍五入到给定的十进制位数。

separate([deep, force])

参见 sympy.simplify 中的单独函数

series([x, x0, n, dir, logx, cdir])

x = x0 附近对 "self" 进行级数展开,当 n=None 时逐项给出级数项(即惰性级数),否则当 n != None 时一次性给出所有项。

simplify(**kwargs)

请参阅 sympy.simplify 中的 simplify 函数。

sort_key([order])

subs(*args, **kwargs)

在简化参数后,在表达式中用新内容替换旧内容。

taylor_term(n, x, *previous_terms)

泰勒项的一般方法。

together(*args, **kwargs)

请参阅 sympy.polys 中的 together 函数。

trigsimp(**args)

参见 sympy.simplify 中的 trigsimp 函数

xreplace(rule)

替换表达式中对象的出现。

伴随

as_base_exp

复制

差异

目录

is_hypergeometric

转置

change_index(
var,
trafo,
newvar=None,
)[源代码][源代码]

更改求和或乘积的索引。

对索引变量 \(x\) 执行线性变换 \(x \mapsto a x + b\)。对于 \(a\),唯一允许的值是 \(\pm 1\)。也可以指定在索引变更后使用的新变量。

示例

>>> from sympy import Sum, Product, simplify
>>> from sympy.abc import x, y, a, b, c, d, u, v, i, j, k, l
>>> S = Sum(x, (x, a, b))
>>> S.doit()
-a**2/2 + a/2 + b**2/2 + b/2
>>> Sn = S.change_index(x, x + 1, y)
>>> Sn
Sum(y - 1, (y, a + 1, b + 1))
>>> Sn.doit()
-a**2/2 + a/2 + b**2/2 + b/2
>>> Sn = S.change_index(x, -x, y)
>>> Sn
Sum(-y, (y, -b, -a))
>>> Sn.doit()
-a**2/2 + a/2 + b**2/2 + b/2
>>> Sn = S.change_index(x, x+u)
>>> Sn
Sum(-u + x, (x, a + u, b + u))
>>> Sn.doit()
-a**2/2 - a*u + a/2 + b**2/2 + b*u + b/2 - u*(-a + b + 1) + u
>>> simplify(Sn.doit())
-a**2/2 + a/2 + b**2/2 + b/2
>>> Sn = S.change_index(x, -x - u, y)
>>> Sn
Sum(-u - y, (y, -b - u, -a - u))
>>> Sn.doit()
-a**2/2 - a*u + a/2 + b**2/2 + b*u + b/2 - u*(-a + b + 1) + u
>>> simplify(Sn.doit())
-a**2/2 + a/2 + b**2/2 + b/2
>>> P = Product(i*j**2, (i, a, b), (j, c, d))
>>> P
Product(i*j**2, (i, a, b), (j, c, d))
>>> P2 = P.change_index(i, i+3, k)
>>> P2
Product(j**2*(k - 3), (k, a + 3, b + 3), (j, c, d))
>>> P3 = P2.change_index(j, -j, l)
>>> P3
Product(l**2*(k - 3), (k, a + 3, b + 3), (l, -d, -c))

当只处理符号时,我们可以进行一般的线性变换:

>>> Sn = S.change_index(x, u*x+v, y)
>>> Sn
Sum((-v + y)/u, (y, b*u + v, a*u + v))
>>> Sn.doit()
-v*(a*u - b*u + 1)/u + (a**2*u**2/2 + a*u*v + a*u/2 - b**2*u**2/2 - b*u*v + b*u/2 + v)/u
>>> simplify(Sn.doit())
a**2*u/2 + a/2 - b**2*u/2 + b/2

然而,最后一个结果可能与通常的求和结果不一致,其中索引增量总是1。这很明显,因为我们只有在 u 等于 +1 或 -1 时才能得到原始值。

property has_empty_sequence

如果对空序列计算和或积,则返回 True。

参见

has_reversed_limits
has_finite_limits

示例

>>> from sympy import Sum, Product, Symbol
>>> m = Symbol('m')
>>> Sum(m, (m, 1, 0)).has_empty_sequence
True
>>> Sum(m, (m, 1, 1)).has_empty_sequence
False
>>> M = Symbol('M', integer=True, positive=True)
>>> Product(m, (m, 1, M)).has_empty_sequence
False
>>> Product(m, (m, 2, M)).has_empty_sequence
>>> Product(m, (m, M + 1, M)).has_empty_sequence
True
>>> N = Symbol('N', integer=True, positive=True)
>>> Sum(m, (m, N, M)).has_empty_sequence
>>> N = Symbol('N', integer=True, negative=True)
>>> Sum(m, (m, N, M)).has_empty_sequence
False
index(x)[源代码][源代码]

返回虚拟变量在限制列表中的索引。

示例

>>> from sympy.abc import x, y, a, b, c, d
>>> from sympy import Sum, Product
>>> Sum(x*y, (x, a, b), (y, c, d)).index(x)
0
>>> Sum(x*y, (x, a, b), (y, c, d)).index(y)
1
>>> Product(x*y, (x, a, b), (y, c, d)).index(x)
0
>>> Product(x*y, (x, a, b), (y, c, d)).index(y)
1
reorder(*arg)[源代码][源代码]

在包含求和或乘积的表达式中重新排序限制。

示例

>>> from sympy import Sum, Product
>>> from sympy.abc import x, y, z, a, b, c, d, e, f
>>> Sum(x*y, (x, a, b), (y, c, d)).reorder((x, y))
Sum(x*y, (y, c, d), (x, a, b))
>>> Sum(x*y*z, (x, a, b), (y, c, d), (z, e, f)).reorder((x, y), (x, z), (y, z))
Sum(x*y*z, (z, e, f), (y, c, d), (x, a, b))
>>> P = Product(x*y*z, (x, a, b), (y, c, d), (z, e, f))
>>> P.reorder((x, y), (x, z), (y, z))
Product(x*y*z, (z, e, f), (y, c, d), (x, a, b))

我们也可以通过计数来选择索引变量,从最内层开始:

>>> Sum(x**2, (x, a, b), (x, c, d)).reorder((0, 1))
Sum(x**2, (x, c, d), (x, a, b))

当然,我们可以混合使用这两种方案:

>>> Sum(x*y, (x, a, b), (y, c, d)).reorder((y, x))
Sum(x*y, (y, c, d), (x, a, b))
>>> Sum(x*y, (x, a, b), (y, c, d)).reorder((y, 0))
Sum(x*y, (y, c, d), (x, a, b))
reorder_limit(x, y)[源代码][源代码]

交换一个 Sum 或 Product 表达式的两个限制元组。

示例

>>> from sympy.abc import x, y, z, a, b, c, d, e, f
>>> from sympy import Sum, Product
>>> Sum(x*y*z, (x, a, b), (y, c, d), (z, e, f)).reorder_limit(0, 2)
Sum(x*y*z, (z, e, f), (y, c, d), (x, a, b))
>>> Sum(x**2, (x, a, b), (x, c, d)).reorder_limit(1, 0)
Sum(x**2, (x, c, d), (x, a, b))
>>> Product(x*y*z, (x, a, b), (y, c, d), (z, e, f)).reorder_limit(0, 2)
Product(x*y*z, (z, e, f), (y, c, d), (x, a, b))

具体函数参考

sympy.concrete.summations.summation(f, *symbols, **kwargs)[源代码][源代码]

计算 f 关于符号的总和。

示例

>>> from sympy import summation, oo, symbols, log
>>> i, n, m = symbols('i n m', integer=True)
>>> summation(2*i - 1, (i, 1, n))
n**2
>>> summation(1/2**i, (i, 0, oo))
2
>>> summation(1/log(n)**n, (n, 2, oo))
Sum(log(n)**(-n), (n, 2, oo))
>>> summation(i, (i, 0, n), (n, 0, m))
m**3/6 + m**2/2 + m/3
>>> from sympy.abc import x
>>> from sympy import factorial
>>> summation(x**n/factorial(n), (n, 0, oo))
exp(x)
sympy.concrete.products.product(*args, **kwargs)[源代码][源代码]

计算乘积。

示例

>>> from sympy import product, symbols
>>> i, n, m, k = symbols('i n m k', integer=True)
>>> product(i, (i, 1, k))
factorial(k)
>>> product(m, (i, 1, k))
m**k
>>> product(i, (i, 1, k), (k, 1, n))
Product(factorial(k), (k, 1, n))
sympy.concrete.gosper.gosper_normal(f, g, n, polys=True)[源代码][源代码]

计算 fg 的 Gosper 正规形式。

示例

>>> from sympy.concrete.gosper import gosper_normal
>>> from sympy.abc import n
>>> gosper_normal(4*n+5, 2*(4*n+1)*(2*n+3), n, polys=False)
(1/4, n + 3/2, n + 1/4)
sympy.concrete.gosper.gosper_term(f, n)[源代码][源代码]

计算 f 的高斯超几何项。

示例

>>> from sympy.concrete.gosper import gosper_term
>>> from sympy import factorial
>>> from sympy.abc import n
>>> gosper_term((4*n + 1)*factorial(n)/factorial(2*n + 1), n)
(-n - 1/2)/(n + 1/4)
sympy.concrete.gosper.gosper_sum(f, k)[源代码][源代码]

Gosper 的超几何求和算法。

参考文献

[1]

Marko Petkovsek, Herbert S. Wilf, Doron Zeilberger, A = B, AK Peters, Ltd., Wellesley, MA, USA, 1997, pp. 73–100

示例

>>> from sympy.concrete.gosper import gosper_sum
>>> from sympy import factorial
>>> from sympy.abc import n, k
>>> f = (4*k + 1)*factorial(k)/factorial(2*k + 1)
>>> gosper_sum(f, (k, 0, n))
(-factorial(n) + 2*factorial(2*n + 1))/factorial(2*n + 1)
>>> _.subs(n, 2) == sum(f.subs(k, i) for i in [0, 1, 2])
True
>>> gosper_sum(f, (k, 3, n))
(-60*factorial(n) + factorial(2*n + 1))/(60*factorial(2*n + 1))
>>> _.subs(n, 5) == sum(f.subs(k, i) for i in [3, 4, 5])
True