0
$\begingroup$

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]])];
gg[3]
$\endgroup$

1 Answer 1

3
$\begingroup$

Maybe this way?

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

And then

a = RandomReal[{0, 1}];
w = RandomReal[{0, 1}, 10000];
gg = getgg[a, w];
$\endgroup$
11
  • $\begingroup$ ...Thank you for your prompt reply. But this code is not working. $\endgroup$
    – a b
    Commented 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$ Commented 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
    Commented 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$ Commented Mar 31, 2019 at 18:31
  • $\begingroup$ I added the picture with errors $\endgroup$
    – a b
    Commented Mar 31, 2019 at 18:44

Your Answer

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

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