# How to work with ExperimentalNumericalFunction?

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 ExperimentalNumericalFunction, 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 ExperimentalNumericalFunction 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 ExperimentalNumericalFunction?

-
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 '14 at 20:27
Example: f = ExperimentalCreateNumericalFunction[{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 '14 at 3:04
Have you seen NDSolveStateData, §NDSolveStateData Properties? It has ways of getting a NumericalFunction from diff. eqs. –  Michael E2 Sep 16 '14 at 12:54

To create ExperimentalNumericalFunction, one needs to evaluate ExperimentalCreateNumericalFunction[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 ExperimentalCreateNumericalFunction 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 ExperimentalNumericalFunction has useful properties. First of all, you can directly compute Gradient/Jacobian or Hessian for expr for any numerical values of parameters:

f = ExperimentalCreateNumericalFunction[{x, y}, {Sin[x + y], x^2 y}, {2}];

f["Hessian"[{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 ExperimentalNumericalFunction Gradient and Jacobian seems to be synonyms.

There also is NDSolveValidNumericalFunctionQ which seemingly tests whether the created ExperimentalNumericalFunction is valid.

There also are some useful Messages for ExperimentalCreateNumericalFunction defined in the file "Messages.m".

-
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 '14 at 6:03
Maybe you can help OP here –  Kuba Apr 24 '14 at 17:34