Defining a function recursively

I want to construct polynomials (or just function) $$f_n$$ from $$f_{n-1}$$ using mathematica. For some kind of reason, I didn't manage to do this. So basically something like a recurrence table, but for function.

I do have it for sagemath, so perhaps if possible, I'd like somehow to convert that to mathematical. I have something like this

def T(x,y,n):
return x if n == 0 else x^2*T(y^2*x,2y+4,n-1)


In fact my function is actually more complicated, but just to give an example. Is there a way to write the analogue of this in mathematica?

Thank you

• Just to get you started T[x_, y_, n_] := 0 /; n == 0 Dec 1 '21 at 23:33

Try this:

T[x_, y_, n_?IntegerQ] /; n >= 0 := If[n === 0, x, Expand[x^2*T[y^2*x, 2 y + 4, n - 1]]]


Test:

T[x, y, 0]
(*x*)
T[x, y, 1]
(*x^3 y^2*)
T[x, y, 2]
(*16 x^5 y^6 + 16 x^5 y^7 + 4 x^5 y^8*)
T[x, y, 3]
(*589824 x^7 y^10 + 2162688 x^7 y^11 + 3457024 x^7 y^12 + 3145728 x^7 y^13 + 1781760 x^7 y^14 + 643072 x^7 y^15 + 144384 x^7 y^16 + 18432 x^7 y^17 + 1024 x^7 y^18*)

• Oh wauw, had no idea this was possible. Thanks! Dec 2 '21 at 1:07

To include memorization

Clear[T]

T[x_, y_, 0] = x;
T[x_, y_, n_Integer?Positive] :=
T[x, y, n] = x^2*T[y^2*x, 2 y + 4, n - 1] // Simplify;


The first several:

T[x, y, #] & /@ Range[0, 5]//Column