How to define a mathematical sequence (in Python, using SymPy)
Task
In mathematics, a sequence is an infinite list of values, typically
real numbers, often written
(Let’s assume that sequences are indexed starting with index 0, at
How can we express sequences in mathematical software?
Related tasks:
Solution
This answer assumes you have imported SymPy as follows.
1
2
from sympy import * # load all math functions
init_printing( use_latex='mathjax' ) # use pretty math output
Sequences are typically written in terms of an independent variable
We define a term of an example sequence as (n,0,oo)
means that oo
being the SymPy
notation for
1
2
3
4
var( 'n' ) # use n as a symbol
a_n = 1 / ( n + 1 ) # formula for a term
seq = sequence( a_n, (n,0,oo) ) # build the sequence
seq
You can ask for specific terms in the sequence, or many terms in a row, as follows.
1
seq[20]
1
seq[:10]
You can compute the limit of a sequence,
1
limit( a_n, n, oo )
Content last modified on 24 July 2023.
See a problem? Tell us or edit the source.
Contributed by Nathan Carter (ncarter@bentley.edu)