I'm computing rather complicated expressions using NIntegrate, and for that, my integrand is created as some compiled function (to speed up evaluation).

Foo = Compile[{{arg1, _Real, 1}, {arg2, _Real, 2}}, ...]

The arg1, and arg2 to be put in are results of the operation of Eigensystem; so what I did was,


Where H is a matrix that depends explicitly on k1,k2,k3. What happens afterwards, you can imagine, I get an error of the form

CompiledFunction::cfta: Argument {Root[14936.76864003200+5932.79232000000 Cos[k1]+476.799680000000 Cos[2 k1]+<<72>>+576.000000000000 Cos[k1+Times[<<2>>]+kz]+<<17>>&,1],Root[<<63>>+<<75>>+<<17>>&,2],Root[<<1>>&,3],Root[<<63>>+<<75>>+<<17>>&,4]} at position 2 should be a rank 1 tensor of machine-size complex numbers.

Which appears to mean that the current point in NIntegrate is somehow not passed on to Eigensystem. How do I fix this? Thank you!

  • $\begingroup$ Please provide a minimal example of code that evaluates and produces the error you observe. $\endgroup$ – Anton Antonov Jul 7 '19 at 12:07

This might help: wrap the compiled function in one that accepts numeric arguments only.

Heres is an example (f wraps fc):

Clear[fc, f]
fc = Compile[{{arg1, _Real, 0}, {arg2, _Real, 0}}, arg1 + arg2];
f[x_?NumericQ, y_?NumericQ] := fc[x, y];

NIntegrate[fc[x, y], {x, 0, 10}, {y, 0, 1}]

(* During evaluation of In[191]:= CompiledFunction::cfsa: Argument x at position 1 should be a machine-size real number. *)

(* 55. *)

NIntegrate[f[x, y], {x, 0, 10}, {y, 0, 1}]

(* 55. *)
  • $\begingroup$ Thank you! Worked like a charm :) $\endgroup$ – Daniel Kaplan Jul 8 '19 at 8:52
  • $\begingroup$ Ok, good luck ! $\endgroup$ – Anton Antonov Jul 8 '19 at 19:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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