序列#

序列是一个有限的或无限的延迟计算的列表。

sympy.series.sequences.sequence(seq, limits=None)[源代码]#

返回适当的序列对象。

解释

If seq is a SymPy sequence, returns SeqPer object otherwise returns SeqFormula object.

实例

>>> from sympy import sequence
>>> from sympy.abc import n
>>> sequence(n**2, (n, 0, 5))
SeqFormula(n**2, (n, 0, 5))
>>> sequence((1, 2, 3), (n, 0, 5))
SeqPer((1, 2, 3), (n, 0, 5))

序列基#

class sympy.series.sequences.SeqBase(*args)[源代码]#

序列的基类

coeff(pt)[源代码]#

返回点pt处的系数

coeff_mul(other)[源代码]#

应在以下情况下使用 other 不是一个序列。应定义以定义自定义行为。

实例

>>> from sympy import SeqFormula
>>> from sympy.abc import n
>>> SeqFormula(n**2).coeff_mul(2)
SeqFormula(2*n**2, (n, 0, oo))

笔记

“*”只定义序列与序列的乘法。

find_linear_recurrence(n, d=None, gfvar=None)[源代码]#

Finds the shortest linear recurrence that satisfies the first n terms of sequence of order \(\leq\) n/2 if possible. If d is specified, find shortest linear recurrence of order \(\leq\) min(d, n/2) if possible. Returns list of coefficients [b(1), b(2), ...] corresponding to the recurrence relation x(n) = b(1)*x(n-1) + b(2)*x(n-2) + ... Returns [] if no recurrence is found. If gfvar is specified, also returns ordinary generating function as a function of gfvar.

实例

>>> from sympy import sequence, sqrt, oo, lucas
>>> from sympy.abc import n, x, y
>>> sequence(n**2).find_linear_recurrence(10, 2)
[]
>>> sequence(n**2).find_linear_recurrence(10)
[3, -3, 1]
>>> sequence(2**n).find_linear_recurrence(10)
[2]
>>> sequence(23*n**4+91*n**2).find_linear_recurrence(10)
[5, -10, 10, -5, 1]
>>> sequence(sqrt(5)*(((1 + sqrt(5))/2)**n - (-(1 + sqrt(5))/2)**(-n))/5).find_linear_recurrence(10)
[1, 1]
>>> sequence(x+y*(-2)**(-n), (n, 0, oo)).find_linear_recurrence(30)
[1/2, 1/2]
>>> sequence(3*5**n + 12).find_linear_recurrence(20,gfvar=x)
([6, -5], 3*(5 - 21*x)/((x - 1)*(5*x - 1)))
>>> sequence(lucas(n)).find_linear_recurrence(15,gfvar=x)
([1, 1], (x - 2)/(x**2 + x - 1))
property free_symbols#

此方法返回对象中的符号,不包括具有特定值的符号(即伪符号)。

实例

>>> from sympy import SeqFormula
>>> from sympy.abc import n, m
>>> SeqFormula(m*n**2, (n, 0, 5)).free_symbols
{m}
property gen#

返回序列的生成器

property interval#

定义序列的间隔

property length#

序列的长度

property start#

序列的起点。这一点也包括在内

property stop#

序列的结束点。这一点也包括在内

property variables#

返回有界变量的元组

初等序列#

class sympy.series.sequences.SeqFormula(formula, limits=None)[源代码]#

表示基于公式的序列。

元素是使用公式生成的。

实例

>>> from sympy import SeqFormula, oo, Symbol
>>> n = Symbol('n')
>>> s = SeqFormula(n**2, (n, 0, 5))
>>> s.formula
n**2

在某一点上的价值

>>> s.coeff(3)
9

支持切片

>>> s[:]
[0, 1, 4, 9, 16, 25]

可迭代的

>>> list(s)
[0, 1, 4, 9, 16, 25]

序列从负无穷大开始

>>> SeqFormula(n**2, (-oo, 0))[0:6]
[0, 1, 4, 9, 16, 25]
coeff_mul(coeff)[源代码]#

参见文档字符串基础系数

class sympy.series.sequences.SeqPer(periodical, limits=None)[源代码]#

表示周期性序列。

元素在给定的时间段后重复。

实例

>>> from sympy import SeqPer, oo
>>> from sympy.abc import k
>>> s = SeqPer((1, 2, 3), (0, 5))
>>> s.periodical
(1, 2, 3)
>>> s.period
3

在某一点上的价值

>>> s.coeff(3)
1

支持切片

>>> s[:]
[1, 2, 3, 1, 2, 3]

可迭代的

>>> list(s)
[1, 2, 3, 1, 2, 3]

序列从负无穷大开始

>>> SeqPer((1, 2, 3), (-oo, 0))[0:6]
[1, 2, 3, 1, 2, 3]

周期公式

>>> SeqPer((k, k**2, k**3), (k, 0, oo))[0:6]
[0, 1, 8, 3, 16, 125]
coeff_mul(coeff)[源代码]#

参见文档字符串基础系数

单子序列#

class sympy.series.sequences.EmptySequence[源代码]#

表示空序列。

空序列也可以作为单例作为 S.EmptySequence .

实例

>>> from sympy import EmptySequence, SeqPer
>>> from sympy.abc import x
>>> EmptySequence
EmptySequence
>>> SeqPer((1, 2), (x, 0, 10)) + EmptySequence
SeqPer((1, 2), (x, 0, 10))
>>> SeqPer((1, 2)) * EmptySequence
EmptySequence
>>> EmptySequence.coeff_mul(-1)
EmptySequence
coeff_mul(coeff)[源代码]#

参见文档字符串基础系数

复合序列#

class sympy.series.sequences.SeqAdd(*args, **kwargs)[源代码]#

表示序列的逐项相加。

规则:
  • 定义序列的区间是各序列区间的交集。

  • 什么都行+ EmptySequence 保持不变。

  • 其他规则在中定义 _add 序列类的方法。

实例

>>> from sympy import EmptySequence, oo, SeqAdd, SeqPer, SeqFormula
>>> from sympy.abc import n
>>> SeqAdd(SeqPer((1, 2), (n, 0, oo)), EmptySequence)
SeqPer((1, 2), (n, 0, oo))
>>> SeqAdd(SeqPer((1, 2), (n, 0, 5)), SeqPer((1, 2), (n, 6, 10)))
EmptySequence
>>> SeqAdd(SeqPer((1, 2), (n, 0, oo)), SeqFormula(n**2, (n, 0, oo)))
SeqAdd(SeqFormula(n**2, (n, 0, oo)), SeqPer((1, 2), (n, 0, oo)))
>>> SeqAdd(SeqFormula(n**3), SeqFormula(n**2))
SeqFormula(n**3 + n**2, (n, 0, oo))
static reduce(args)[源代码]#

简化 SeqAdd 使用已知规则。

他们问自己是否可以用任何成分对来简化所有的成分序列。

笔记

改编自 Union.reduce

class sympy.series.sequences.SeqMul(*args, **kwargs)[源代码]#

表示序列的逐项乘法。

解释

只处理序列的乘法。有关其他对象的乘法,请参见 SeqBase.coeff_mul() .

规则:
  • 定义序列的区间是各序列区间的交集。

  • 什么都行 * EmptySequence 收益率 EmptySequence .

  • 其他规则在中定义 _mul 序列类的方法。

实例

>>> from sympy import EmptySequence, oo, SeqMul, SeqPer, SeqFormula
>>> from sympy.abc import n
>>> SeqMul(SeqPer((1, 2), (n, 0, oo)), EmptySequence)
EmptySequence
>>> SeqMul(SeqPer((1, 2), (n, 0, 5)), SeqPer((1, 2), (n, 6, 10)))
EmptySequence
>>> SeqMul(SeqPer((1, 2), (n, 0, oo)), SeqFormula(n**2))
SeqMul(SeqFormula(n**2, (n, 0, oo)), SeqPer((1, 2), (n, 0, oo)))
>>> SeqMul(SeqFormula(n**3), SeqFormula(n**2))
SeqFormula(n**5, (n, 0, oo))
static reduce(args)[源代码]#

简化a SeqMul 使用已知规则。

解释

他们问自己是否可以用任何成分对来简化所有的成分序列。

笔记

改编自 Union.reduce

Recursive Sequences#

class sympy.series.sequences.RecursiveSeq(recurrence, yn, n, initial=None, start=0)[源代码]#

A finite degree recursive sequence.

参数:

recurrence : SymPy expression defining recurrence

This is not an equality, only the expression that the nth term is equal to. For example, if a(n) = f(a(n - 1), ..., a(n - d)), then the expression should be f(a(n - 1), ..., a(n - d)).

yn : applied undefined function

Represents the nth term of the sequence as e.g. y(n) where y is an undefined function and \(n\) is the sequence index.

n : symbolic argument

The name of the variable that the recurrence is in, e.g., n if the recurrence function is y(n).

initial : iterable with length equal to the degree of the recurrence

The initial values of the recurrence.

start : start value of sequence (inclusive)

解释

That is, a sequence a(n) that depends on a fixed, finite number of its previous values. The general form is

a(n) = f(a(n - 1), a(n - 2), ..., a(n - d))

for some fixed, positive integer d, where f is some function defined by a SymPy expression.

实例

>>> from sympy import Function, symbols
>>> from sympy.series.sequences import RecursiveSeq
>>> y = Function("y")
>>> n = symbols("n")
>>> fib = RecursiveSeq(y(n - 1) + y(n - 2), y(n), n, [0, 1])
>>> fib.coeff(3) # Value at a particular point
2
>>> fib[:6] # supports slicing
[0, 1, 1, 2, 3, 5]
>>> fib.recurrence # inspect recurrence
Eq(y(n), y(n - 2) + y(n - 1))
>>> fib.degree # automatically determine degree
2
>>> for x in zip(range(10), fib): # supports iteration
...     print(x)
(0, 0)
(1, 1)
(2, 1)
(3, 2)
(4, 3)
(5, 5)
(6, 8)
(7, 13)
(8, 21)
(9, 34)
property initial#

The initial values of the sequence

property interval#

Interval on which sequence is defined.

property n#

Sequence index symbol

property recurrence#

Equation defining recurrence.

property start#

序列的起点。这一点也包括在内

property stop#

The ending point of the sequence. (oo)

property y#

Undefined function for the nth term of the sequence

property yn#

Applied function representing the nth term