1
$\begingroup$

I try to compile this code, but always failed, could anybody guide me?

a = 1; n = 10; list = Table[1.1, {i, n}];
f = Compile[{{list, _Complex, 1}}, p = Table[0, {i, n}];
  Do[p[[i + 1]] = 2 p[[i]] - list[[i]] a^2, {i, 1, n - 1}];
  p - p[[n/2]]]
f[list]
$\endgroup$

1 Answer 1

3
$\begingroup$

You are against the 2nd and 4th rule mentioned in this post. After modifying according to the rules, f compiles successfully:

a = 1; n = 10; list = Table[1.1, {i, n}];
f = With[{a = a, n = n}, 
  Compile[{{list, _Complex, 1}}, Module[{p = Table[0 + 0 I, {i, n}]},
    Do[p[[i + 1]] = 2 p[[i]] - list[[i]] a^2, {i, 1, n - 1}];
    p - p[[n/2 // Round]]]]]
f[list]
$\endgroup$
1
  • $\begingroup$ Thank you very much! $\endgroup$
    – dnvin
    Commented Oct 31, 2018 at 7:16

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.