Ask Your Question

Revision history [back]

How can you get the n'th function in a sequence defined by a recurrence relation?

Let a_0(x) and b_0(x) be given functions. Then define a_n and b_n by the following relations (or any)

a_n(x) = a_{n-1}(x+1) - a_{n-1}(x) + a_0(x+1)a_{n-1}(x) + b_{n-1}(x) b_n(x) = b_{n-1}(x+1) - b_{n-1}(x) + a_{n-1}(x)b_0(x+1)

How can I write a program to print the n'th pair of functions in the sequence?

I'm new to sagemath and I've been trying to work through this problem on my own, but the bugs I've been running across make me think the way I'm writing my methods is wrong. Here's what I came up with using a different relation

var('x,a,b')

#the first functions in the recurrence relation. Making these more complicated will give you wrong outputs.
def lam_0(x):
    return a*x
def s_0(x):
    return b

#The lam_n based on the starting function lam_0. 
#Example: lam_2 would be lam_forward(lam_0, s_0, z, 2)
def lam_forward(lam_func, s_func, z, n = 1):
    if n <= 0:
        return lam_0(z)
    else:
        return lam_forward(lam_func, s_func, z + 1, n - 1) - lam_forward(lam_func, s_func, z, n - 1) + lam_forward(lam_func, s_func, z + 1, n - 1)*lam_0(z) + s_forward(lam_func, s_func, z+1, n-1)

#The s_n based on the starting function s_0.
def s_forward(lam_func, s_func, z, n = 1):
    if n <= 0:
        return s_0(z)
    else:
        return s_forward(lam_func, s_func, z+1, n-1) - s_forward(lam_func, s_func, z+1, n-1) + lam_forward(lam_func, s_func, z + 1, n - 1)*s_0(z)

#Print the n'th pair of functions
def printNthFunctions(n=0):
    print('Lam_', n, ' =', lam_forward(lam_0(x), s_0(x), x, n).full_simplify())
    print('S_', n, ' = ', s_forward(lam_0(x), s_0(x), x, n).full_simplify())

#BUG After n=1, this does not output the correct function, even though the individual functions are correct
def d_n(z, n):
    return  s_forward(lam_0(z), s_0(z), z, n)*lam_forward(lam_0(z), s_0(z), z, n-1) - s_forward(lam_0(z), s_0(z), z, n-1)*lam_forward(lam_0(z), s_0(z), z, n)