Re: problem with using external functions
- To: mathgroup at smc.vnet.net
- Subject: [mg91587] Re: problem with using external functions
- From: David Bailey <dave at Remove_Thisdbailey.co.uk>
- Date: Thu, 28 Aug 2008 03:18:16 -0400 (EDT)
- References: <g93av9$k3e$1@smc.vnet.net>
Valery Yundin wrote:
> Dear MathGroup,
>
> I've a problem with using external functions defined in MathLink app.
> I guess the problem is in the evaluation order. I'm giving more
> details below.
>
> This is the function template:
> :Begin:
> :Function: my_xfxM
> :Pattern: xfxM[nset_Integer, x_Real, Q_]
> :Arguments: {nset, x, Q}
> :ArgumentTypes: {Integer, Real64, Real64}
> :ReturnType: Manual
> :End:
>
> The function body (the return value is list of doubles):
> void my_xfxM(const int nset, const double x, const double Q) {
> vector<double> result = LHAPDF::xfxM(nset, x, Q);
> double* c_result = &(*result.begin());
> MLPutReal64List(stdlink, c_result, result.size());
> }
>
> I'm defining another function in Mathematica:
> (it should multiply list of values returned by fucntion call by reversed
> list of another function call and drop first 7 elements.
> eg. if xfxM(1, x, 100.) returns {x1, ..., x14} the result should be:
> Plus@@{x8*y14,x9*y13,...,x14*y8})
> myintegrand =
> Function[{x, y},
> (Plus @@ (Drop[xfxM[1, x, 100.] Reverse[xfxM[1, y, 100.]], 7]))]
>
> Direct evaluation works well
> myintegrand[0.5, 0.3]
>
> But smth like this does not work:
> myintegrand[x, y] /. {x->0.5, y->0.3}
>
> And also when I try to integrate this function:
> NIntegrate[myintegrand[x, y], {x, 0.001, 1}, {y, 0.001, 1},
> Method -> {"LocalAdaptive"}]
>
> The NIntegrate gives error message:
> Drop::drop: Cannot drop positions 1 through 7 in xfxM[1,x,100.] \
> xfxM[100.,y,1]. >>
> NIntegrate::inum: "Integrand (7+xfxM[1,x,100.]\xfxM[100.,y,1])/(14000\
> \x\y) is not numerical at {x,y} = {0.50048828125`,0.50048828125`}"
>
> How should I define my function to make this work? I think the problem is
> that Mathematica tries to evaluate function body with formal parameters
> (of "Symbol" type) not with just numerical values.
>
> --
> With best regards, Valery Yundin.
>
Rather than defining myintegrand using a pure function, define it with
pattern arguments:
myintegrand[x_Real,y_Real]:=expression;
or, if you think think the arguments might be integers, rationals, etc:
myintegrand[x_?NumericQ,y_?NumericQ]:=expression
This will prevent any evaluation unless the arguments are numbers.
David Bailey
http://www.dbaileyconsultancy.co.uk