-
Notifications
You must be signed in to change notification settings - Fork 0
Use_cases
The following examples are going to help you become familiar with PySequences.
The aim of this page is to introduce most of the sequences implementations in PySequences.
This section aims to show how to model sequences, in order to calculate any of their terms.
In this example, we consider the following sequence:
u0 = 6
un+1 = (un + 3)2
First, we import the pySequences module:
from pySequences import *Now, we need to create a new sequence object with the Sequence() constructor.
For this, we have to use the following syntax:
Sequence(sequenceType, formula, sequenceName = "u")Here are the parameters values in this case:
sequenceType =
"recurrenceRelation"formula: The syntax for this parameter is quite particular, because it changes depending on the sequenceType value.
Here, sequenceType is equal to
"recurrenceRelation".So, the syntax for this parameter is as follows:
(n0, u_n0, i, u_nPlusI)In this example, we have:
- n0 =
0, because the first n-value of the sequence is 0 (u0).- u_n0 =
6, because u0 = 6.- i =
1, because the increment is equal to 1 (un+1).- u_nPlusI =
lambda u_n: (u_n + 3)**2, because un+1 = (un + 3)2.Thus, the formula parameter is equal to:
(0, 6, 1, lambda u_n: (u_n + 3)**2).sequenceName =
"u"
Note: Here, you can keep this parameter empty because the default sequenceName value is already "u".
Here is the full solution:
from pySequences import *
u = Sequence("recurrenceRelation", (0, 6, 1, lambda u_n: (u_n + 3)**2), "u")If you want to display the y-value for n = 5, you just have to add the following line:
print(u.calc(5))Before drawing the points on a graph, you need to specify that you want to store them in the pointsList attribute.
To do so, you have to calculate the point you want to store, with the store parameter of the calc() method:
# If you want to add the point for n = 8:
u.calc(8, True)Then, the new calculated point is added to the pointsList attribute.
You can now draw the stored points:
u.draw()In this example, we consider the sequence defined by the following function:
f(x) = 3x2 - 6
vn = f(n)
First, we import the pySequences module:
from pySequences import *Now, we need to create a new sequence object with the Sequence() constructor.
For this, we have to use the following syntax:
Sequence(sequenceType, formula, sequenceName = "u")Here are the parameters values in this case:
sequenceType =
"function"formula: The syntax for this parameter is quite particular, because it changes depending on the sequenceType value.
Here, sequenceType is equal to
"function".So, the syntax for this parameter is as follows:
function(a lambda function representing the function formula used to calculate the sequence values)In this example, function is equal to:
lambda n: 3 * n**2 - 6
because:
vn = f(n) = 3n2 - 6sequenceName =
"v"
Here is the full solution:
from pySequences import *
v = Sequence("function", lambda n: 3 * n**2 - 6, "v")If you want to display the y-value for n = 5, you just have to add the following line:
print(v.calc(5))Before drawing the points on a graph, you need to specify that you want to store them in the pointsList attribute.
To do so, you have to calculate the point you want to store, with the store parameter of the calc() method:
# If you want to add the point for n = 8:
v.calc(8, True)Then, the new calculated point is added to the pointsList attribute.
You can now draw the stored points:
v.draw()In this example, we consider the sequence defined by the following set:
wn = {(0; 5), (1; 10), (2; 20)}
First, we import the pySequences module:
from pySequences import *Now, we need to create a new sequence object with the Sequence() constructor.
For this, we have to use the following syntax:
Sequence(sequenceType, formula, sequenceName = "u")Here are the parameters values in this case:
sequenceType =
"pointsList"formula: The syntax for this parameter is quite particular, because it changes depending on the sequenceType value.
Here, sequenceType is equal to
"pointsList".So, the syntax for this parameter is as follows:
{n0: u_n0, n1: u_n1, ...}(the n-values are stored as dict keys and the y-values as dict values)In this example, function is equal to:
{0: 5, 1: 10, 2: 20}sequenceName =
"w"
Here is the full solution:
from pySequences import *
w = Sequence("pointsList", {0: 5, 1: 10, 2: 20}, "w")If you want to display the y-value for n = 5, you just have to add the following line:
print(w.calc(5))You can draw the points of the sequence with the followig line:
w.draw()Multi-sequences drawing enables to draw several sequences on the same graph.
To do so, we need to use the Trace objects.
from pySequences import * myGraph = Trace(markerSize=10) u = Sequence("recurrenceRelation", (0, 6, 1, lambda u_n: u_n + 3, "u")) v = Sequence("function", lambda n: 1.5 * n, "v")First, we have to calculate some points and store them in the pointsList attribute.
# Loop for calculating the sequences values between 0 and 10 for n_temp in range(0, 10): u.calc(n_temp, True) v.calc(n_temp, True)Now, we have to add the sequences points to the Trace object.
To differentiate the two sequences, we will assign them different colors, for example red for u and blue for v.myGraph.addPoints(u.pointsList, "red") myGraph.addPoints(v.pointsList, "blue") myGraph.draw()
Here is the full solution:
from pySequences import *
myGraph = Trace(markerSize=20)
u = Sequence("recurrenceRelation", (0, 6, 1, lambda u_n: u_n + 3, "u"))
v = Sequence("function", lambda n: 1.5 * n, "v")
# Loop for calculating the sequences values between 0 and 10
for n_temp in range(0, 10):
u.calc(n_temp, True)
v.calc(n_temp, True)
myGraph.addPoints(u.pointsList, "red")
myGraph.addPoints(v.pointsList, "blue")
myGraph.draw()