Ask Your Question
2

sequence of functions

asked 2016-09-05 01:47:21 +0100

math4tots gravatar image

How do I define a sequence of functions in Sage?

I'd like to do something like

f(0)(a, b) = a * b
f(n)(a, b) = a + b * f(n-1)(a+b, a-b)

Such that when I use it, I could e.g. solve for x in

f(3)(x, x+1) = 1
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2016-09-05 11:44:48 +0100

tmonteil gravatar image

updated 2016-09-05 11:51:55 +0100

instead of symbolic functions, you could just use a (recursive) Python function:

def f(n):
    if n == 0:
        return lambda a,b : a*b
    else:
        return lambda a,b : a + b * f(n-1)(a+b, a-b)

Then you can do:

sage: f(0)(2,3)
6
sage: f(3)(x, x+1)
(8*(2*x + 1)*(x + 1) + 1)*(x + 1) + x

sage: solve(f(3)(x, x+1)==1, x)
[x == -1/2*(1/288*sqrt(89)*sqrt(3) + 49/864)^(1/3)*(I*sqrt(3) + 1) - 1/144*(I*sqrt(3) - 1)/(1/288*sqrt(89)*sqrt(3) + 49/864)^(1/3) - 5/6, x == -1/2*(1/288*sqrt(89)*sqrt(3) + 49/864)^(1/3)*(-I*sqrt(3) + 1) + 1/144*(I*sqrt(3) + 1)/(1/288*sqrt(89)*sqrt(3) + 49/864)^(1/3) - 5/6, x == (1/288*sqrt(89)*sqrt(3) + 49/864)^(1/3) - 1/72/(1/288*sqrt(89)*sqrt(3) + 49/864)^(1/3) - 5/6]

For the latter, if you want algrbraic numbers, you can define x not to be just a symbol (leading to f(3)(x, x+1) being a symbolic expression) but a polynomial indeterminate:

sage: R.<x> = ZZ[]
sage: (f(3)(x, x+1)-1).roots() # no integrer root
[]
sage: (f(3)(x, x+1)-1).roots(QQbar) # 3 algebraic roots, each with multiplicity 1
[(-0.3779244420252417?, 1),
 (-1.061037778987380? - 0.4440885163776650?*I, 1),
 (-1.061037778987380? + 0.4440885163776650?*I, 1)]
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2016-09-05 01:47:21 +0100

Seen: 521 times

Last updated: Sep 05 '16