# Replacement inside NIntegrate

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,

NIntegrate[Foo[#[[1]],#[[2]],...]&[Eigensystem[H]],{k1,-1,1},{k2,-1,1},...]


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!

• Please provide a minimal example of code that evaluates and produces the error you observe. – Anton Antonov Jul 7 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. *)

• Thank you! Worked like a charm :) – Daniel Kaplan Jul 8 at 8:52
• Ok, good luck ! – Anton Antonov Jul 8 at 19:11