# recursively solve and substitute [closed]

I'm working on a pretty complicated problem (theoretically, not necessarily computationally). I'm not sure how to distill my question down to a simple example. But basically I have a system of equations over the ring of polynomials (or power series) over the symbolic ring. I want to be able to recursively solve for one symbolic function in terms of another, substitute the solution, and repeat. The ultimate goal is to extract relevant data all as a function of some positive integer k. It seems like I should be able to build a loop to do this, but I'm new to sage and programming in general. I haven't been able to find anything quite like what I'm trying to do online. So I'm asking for suggestions.

Here are some more details:

I have several polynomial expressions

```
j,k,z = var('j,k,z')
a,f=function('a,f')
p= lambda k : z^(-2*k) + sum(a(j)*z^(-j) for j in range(2*k-1))
F= lambda k : sum(f(j)*z^j, j, 2, 2*k)
```

which I plug into a formal matrix valued function and perform a series of algebraic manipulations. In the end I get a matrix whose entries are polynomials in the a(j)'s and f(j)'s. I want to recursively solve for the f(j)'s in terms of the a(j)'s so as to diagonalize this matrix expression. I can do this "by hand" but I want a function that will just spit out the end result. For example I know that f(2) = 1/4*a(2*k-2) for any k.

I've tried, for example:

```
def A1(k) :
for j in range(2,2*k) :
A0(k)=A0(k).substitute(f0(k,j)[0])
return A0(k)
```

Here A0(k) is a previously defined matrix expression and f0(k,j) is a function that solves for f(j). This code actually runs but then trying to call A1(2), etc., leads to errors.

Another attempt was to define

```
A1= lambda k,j : coef(A0(k),-k-2+j).substitute(f0(k,j)[0])
```

and then try to define a function of k in terms of the A1(k,j)'s using the sum() command. (In the code above coef(A,j) is a function that returns the coefficient of the z^j term in a polynomial over a ring of matrices).

The problem with this last attempt is that A1(k,j) for j bigger than 2 returns terms that include f(2), f(3), etc. I need something that solves for f(2), substitutes this value for f(2) into all future computations; then solves for f(3), substitutes this values for f(3) into all future computations; and so on until f(2*k).

I apologize if this question is too broad somehow. In looking over some of the literature on loops, solve(), substitute(), etc., I haven't found anything quite like this. Any suggestions? And thank you.