I am trying to make the code for the following recurrence. Here wjoin[[i]] is used for calling values from a data set and a is a parameter.

  • How can I perform the following recurrence using Compile?
  • How can I store the previous values of gg[i] to calculate gg[i + 1]?

I wrote the following code. But it is not working.

gg[1] = a - wjoin[[1]]
gg[2] = (a)*gg[1] - wjoin[[2]])
gg = Compile[{{r1, _Integer}}, 1/(a)*((1 + a )*gg[r1] - wjoin[[r1]])];

1 Answer 1


Maybe this way?

getgg = Compile[{{a, _Real}, {w, _Real, 1}},
    g = Table[0., {Length[w]}];
    g[[1]] = a - w[[1]];
    g[[2]] = a g[[1]] - w[[2]];
     g[[i]] = ((1. + a) g[[i - 1]] - w[[i]])/a,
     {i, 3, Length[w]}
   CompilationTarget -> "C"

And then

a = RandomReal[{0, 1}];
w = RandomReal[{0, 1}, 10000];
gg = getgg[a, w];
  • $\begingroup$ ...Thank you for your prompt reply. But this code is not working. $\endgroup$
    – a b
    Mar 31, 2019 at 16:07
  • 3
    $\begingroup$ Oh, it does work. The only problem is that quite large numbers appear during the calculations. Numbers that cannot be represented by machine precision numbers. But whether such large numbers appear or not does depend on the values of a and w. Since you did not provide example values, I had to guess. $\endgroup$ Mar 31, 2019 at 16:10
  • $\begingroup$ It seems that I do have c compiler driver. Can you please help me to inform step by step in this case. I search for this. There are a few posts about this. I am really confused now. $\endgroup$
    – a b
    Mar 31, 2019 at 18:27
  • $\begingroup$ I am also confused. What exactly is the problem? What error messages do you get? You may also try CompilationTarget -> "WVM" instead if you don't have a working C compiler. $\endgroup$ Mar 31, 2019 at 18:31
  • $\begingroup$ I added the picture with errors $\endgroup$
    – a b
    Mar 31, 2019 at 18:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.