Message Boards Message Boards

Avoid problem with Minimize of a WSTP function?



I have created a WSTP function ReturnEvalErrorP that takes a list of doubles as input and returns a double. I can call this function succesfully:


and it returns a double. I want to minimize this function, so I defined a function:

Clear[f]; f[p1_] := ReturnEvalErrorP[{p1}]

and f[0.1] returns a double. Calling Minimize with this function however always fails with {$Failed, {p1 -> 0}}

    Minimize[f[p1], {p1}, Reals]

Any ideas? Gijsbert

3 months ago

The tm definition of the function is as follows:

double return_eval_error_p P((double *p, long dp));

:Function:       return_eval_error_p
:Pattern:        ReturnEvalErrorP[p_List]
:Arguments:      {p}
:ArgumentTypes:  {RealList}
:ReturnType:     Real

:Evaluate: ReturnEvalErrorP::usage = "ReturnEvalErrorP[{}]"
3 months ago

FYI, You have to specify that the argument to f is a real number:

Clear[f]; f[p1_Real] := ReturnEvalErrorP[{p1}]

ReturnEvalErrorP runs parallel using OpenMPI. Feel free to contact me if you want to know how to launch and use OpenMPI from your WSTP function.

Regards, Gijsbert

3 months ago

Minimize is meant for symbolic computation. Use FindMinimum or NMinimize for numerical calculations.

You will also need to ensure that your function only evaluates with numerical arguments:

You can do this either with a wrapper function (as in the linked article) or by modifying the :Pattern: in the MathLink template.

POSTED BY: Szabolcs Horvát
3 months ago

Group Abstract Group Abstract