NIntegrate fails when integrating over a list from an external (MathLink) function. For simplicity, consider an external function f[x] that returns the list {x,2x}. In Mathematica, the function would be defined as f[x_]={x,2x}
. In reality I have a much more complicated function that cannot be implemented inside Mathematica. NIntegrate works fine if the external function f[x] returns a single number, not a list.
Install["f"]
(* LinkObject[...] *)
f[.5]
(* {0.5, 1.} *)
NIntegrate[f[x], {x, 0, 1}]
(* Integrand f[x] is not numerical at {x} = {0.00795732}. >>*)
(* Integrand f[x] is not numerical at {x} = {0.00795732}. >>*)
This is the file f.tm that defines the external function f[x]:
#include "mathlink.h"
:Begin:
:Function: f
:Pattern: f[x_?NumericQ]
:Arguments: {x}
:ArgumentTypes: {Real}
:ReturnType: Manual
:End:
void f(double x)
{
double y[2];
y[0] = x;
y[1] = 2 * x;
MLPutRealList(stdlink, y, 2);
}
int main(int argc, char* argv[])
{
return MLMain(argc, argv);
}