I have two lists $a$ and $b$ of length $n$ and $n-1$ respectively (typically I have $n \approx 1000$). I have to compute a list $\theta$ of length $n$ which is defined recursively by the following relation: $$ \theta_i = a_i\theta_{i-1} - b_{i-1}^2\theta_{i-2}, \;\;\;\;\; i = 2,...,n $$ where $\theta_0 = 1$ and $\theta_1 = a_1$. I can easily code a simple loop that performs this operation:
theta[[1]] = a[[1]];
theta[[2]] = a[[2]]*theta[[1]] - b[[1]]^2;
Do[
theta[[i]] =
a[[i]]*theta[[i - 1]] - (b[[i - 1]]^2)*theta[[i - 2]];
, {i, 3, n}];
where I have computed explicitly $\theta_2$ for simplicity.
So far so good, but I know that the Do[]
loops are usually not the best way to go in Mathematica, so I wonder if there is some way to speed up this code. Also consider that I absolutely need to insert this code into Compile[]
, where the lists $a$ and $b$ are the input of my function. This is because I want to give the attribute Listable to my function for performance reasons.
I know that sometimes there are clever ways to code recursive functions in general, but I really don't know much about this topic, so any help related to some basic concepts of recursive procedures is also appreciated. Thanks!