Sequences¶
A sequence is a finite or infinite lazily evaluated list.
- sympy.series.sequences.sequence(seq, limits=None)[source]¶
Returns appropriate sequence object.
Explanation
If
seq
is a SymPy sequence, returnsSeqPer
object otherwise returnsSeqFormula
object.Examples
>>> 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))
Sequences Base¶
- class sympy.series.sequences.SeqBase(*args)[source]¶
Base class for sequences
- coeff_mul(other)[source]¶
Should be used when
other
is not a sequence. Should be defined to define custom behaviour.Examples
>>> from sympy import SeqFormula >>> from sympy.abc import n >>> SeqFormula(n**2).coeff_mul(2) SeqFormula(2*n**2, (n, 0, oo))
Notes
‘*’ defines multiplication of sequences with sequences only.
- find_linear_recurrence(n, d=None, gfvar=None)[source]¶
Finds the shortest linear recurrence that satisfies the first n terms of sequence of order \(\leq\)
n/2
if possible. Ifd
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 relationx(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.Examples
>>> 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¶
This method returns the symbols in the object, excluding those that take on a specific value (i.e. the dummy symbols).
Examples
>>> from sympy import SeqFormula >>> from sympy.abc import n, m >>> SeqFormula(m*n**2, (n, 0, 5)).free_symbols {m}
- property gen¶
Returns the generator for the sequence
- property interval¶
The interval on which the sequence is defined
- property length¶
Length of the sequence
- property start¶
The starting point of the sequence. This point is included
- property stop¶
The ending point of the sequence. This point is included
- property variables¶
Returns a tuple of variables that are bounded
Elementary Sequences¶
- class sympy.series.sequences.SeqFormula(formula, limits=None)[source]¶
Represents sequence based on a formula.
Elements are generated using a formula.
Examples
>>> from sympy import SeqFormula, oo, Symbol >>> n = Symbol('n') >>> s = SeqFormula(n**2, (n, 0, 5)) >>> s.formula n**2
For value at a particular point
>>> s.coeff(3) 9
supports slicing
>>> s[:] [0, 1, 4, 9, 16, 25]
iterable
>>> list(s) [0, 1, 4, 9, 16, 25]
sequence starts from negative infinity
>>> SeqFormula(n**2, (-oo, 0))[0:6] [0, 1, 4, 9, 16, 25]
See also
- class sympy.series.sequences.SeqPer(periodical, limits=None)[source]¶
Represents a periodic sequence.
The elements are repeated after a given period.
Examples
>>> 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
For value at a particular point
>>> s.coeff(3) 1
supports slicing
>>> s[:] [1, 2, 3, 1, 2, 3]
iterable
>>> list(s) [1, 2, 3, 1, 2, 3]
sequence starts from negative infinity
>>> SeqPer((1, 2, 3), (-oo, 0))[0:6] [1, 2, 3, 1, 2, 3]
Periodic formulas
>>> SeqPer((k, k**2, k**3), (k, 0, oo))[0:6] [0, 1, 8, 3, 16, 125]
See also
Singleton Sequences¶
- class sympy.series.sequences.EmptySequence[source]¶
Represents an empty sequence.
The empty sequence is also available as a singleton as
S.EmptySequence
.Examples
>>> 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
Compound Sequences¶
- class sympy.series.sequences.SeqAdd(*args, **kwargs)[source]¶
Represents term-wise addition of sequences.
- Rules:
The interval on which sequence is defined is the intersection of respective intervals of sequences.
Anything +
EmptySequence
remains unchanged.Other rules are defined in
_add
methods of sequence classes.
Examples
>>> 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))
See also
- class sympy.series.sequences.SeqMul(*args, **kwargs)[source]¶
Represents term-wise multiplication of sequences.
Explanation
Handles multiplication of sequences only. For multiplication with other objects see
SeqBase.coeff_mul()
.- Rules:
The interval on which sequence is defined is the intersection of respective intervals of sequences.
Anything *
EmptySequence
returnsEmptySequence
.Other rules are defined in
_mul
methods of sequence classes.
Examples
>>> 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))
See also
Recursive Sequences¶
- class sympy.series.sequences.RecursiveSeq(recurrence, yn, n, initial=None, start=0)[source]¶
A finite degree recursive sequence.
- Parameters
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 bef(a(n - 1), ..., a(n - d))
.yn : applied undefined function
Represents the nth term of the sequence as e.g.
y(n)
wherey
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 isy(n)
.initial : iterable with length equal to the degree of the recurrence
The initial values of the recurrence.
start : start value of sequence (inclusive)
Explanation
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.
Examples
>>> 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)
See also
- 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¶
The starting point of the sequence. This point is included
- 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