Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

This question is intimately connected with previous one: "How to create internally optimized expression for computing with high WorkingPrecision?"

Oleksandr R. correctly states in the comment:

A good answer will hopefully discuss the Experimental`NumericalFunction, which is a structure produced by FindMinimum from the function and its Jacobian that is optimized for fast numerical evaluation. I know almost nothing about these objects or how to create/use them, but I would like to find out.

From other questions on this site it is apparent that other built-in numerical functions also use Experimental`NumericalFunction which seems to be the standard way to optimize numerical evaluation. It is not documented but it is placed in the Experimental context which "contains functions that are being considered for official inclusion in future versions of Mathematica". This undocumented function is of crucial importance because currently it is the only way to optimize arbitrary precision calculations. For MachinePrecision we have Compile but nothing documented for arbitrary precision. How to work with Experimental`NumericalFunction?

share|improve this question
1  
One way to collect such objects for examination is this. It might be useful to have a temporary community wiki answer where people can share findings before a final answer is developed. –  Szabolcs Mar 6 at 20:27
1  
Example: f = Experimental`CreateNumericalFunction[{x, y}, {Sin[x + y], x^2 y}, {2}, WorkingPrecision -> 20]. Usage: f[{2, 3}]. See also f["Properties"] and Options[ExperimentalCreateNumericalFunction]. I don't really know how to take advantage of them. On small examples like f` they do not appear to be more efficient. –  Michael E2 Mar 11 at 3:04

1 Answer 1

up vote 13 down vote accepted

To create Experimental`NumericalFunction, one needs to evaluate Experimental`CreateNumericalFunction[vars, expr, dims] where vars is a list of arguments, expr - the expression from which the numerical function will be created, dims - the dimensions of the output matrix produced by this expression. If the output is scalar, then dims should be set to {}. It also accepts fourth argument but I have not figured out yet how to use it.

The Experimental`CreateNumericalFunction has several options, including WorkingPrecision, EvaluationMonitor, StepMonitor, Compiled, Hessian, Gradient and Jacobian which seem to work in the same way as in FindMinimum. Hessian, Gradient and Jacobian can be Automatic, Symbolic or FiniteDifference, meaning of those is documented. I tried to use the "Sparse" suboption of Hessian and found that it is ignored. It also has interesting options "ErrorReturn", "SampleArgument" and Message the purpose of which is unclear to me.

The created Experimental`NumericalFunction has useful properties. First of all, you can directly compute Gradient/Jacobian or Hessian for expr for any numerical values of parameters:

f = Experimental`CreateNumericalFunction[{x, y}, {Sin[x + y], x^2 y}, {2}];

f["Hessian"[{1, 2}]]
f["Gradient"[{1, 2}]]
f["Jacobian"[{1, 2}]]
{{{-0.14112,-0.14112},{-0.14112,-0.14112}},{{4.,2.},{2.,0.}}}
{{-0.989992,-0.989992},{4.,1.}}
{{-0.989992,-0.989992},{4.,1.}}

When called for the first time, symbolic Hessian or Jacobian will be created (if the corresponding parameter is set to Automatic or Symbolic), further evaluation will be executed MUCH faster. For Experimental`NumericalFunction Gradient and Jacobian seems to be synonyms.

There also is NDSolve`ValidNumericalFunctionQ which seemingly tests whether the created Experimental`NumericalFunction is valid.

There also are some useful Messages for Experimental`CreateNumericalFunction defined in the file "Messages.m".

share|improve this answer
    
I have tested it a little in the way described in the linked question and found almost no speedup for MachinePrecision calculations in the simplest case I work with and more than 8x speedup in more involved cases (although the results are not identical to an ordinary function). –  Alexey Popkov Mar 28 at 6:03
    
Maybe you can help OP here –  Kuba Apr 24 at 17:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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