When working with a Compile function I can specify if a given argument is a matrix, a vector or a scalar. For instance, something like:

fC=Compile[{{x,_Real,2},{y,_Real,2},{a,_Real}},a x^2+y^2];

Is there a way to do something similar with Experimental`CreateNumericalFunction? I attempted something like

fE=Experimental`CreateNumericalFunction[{x, y,a},a x^2+y^2, {}, {{_Real,2},{_Real,2},{_Real}}, Compiled -> True];

but it does not work.

  • $\begingroup$ I doubt it can be done, but it seems there is not much known about CreateNumericalFunction outside WRI. The third argument should be the output dimensions, I think. {} indicates a scalar output, which does not correspond to the formula. I don't know if the inputs can be anything other than a scalar either. $\endgroup$
    – Michael E2
    Aug 22 '20 at 2:01

({Compiled -> Automatic, "ErrorReturn" -> Automatic, Evaluated -> Automatic, EvaluationMonitor -> None, Gradient -> Automatic, Hessian -> Automatic, Jacobian -> Automatic, Message -> Automatic, "SampleArgument" -> False, StepMonitor -> None, WorkingPrecision -> MachinePrecision})

One of the StandardNamespaces is Experimental`.

The blue color always indicated that the StandardNamespace is not loaded.


There is evidence that this is analog to ValueFunction with the change that not a value is altered but a numerical output is created.

f = Experimental`CreateNumericalFunction[{a,x,y},a x^2+y^2, {}, {_Real,_Real,_Real}, 
  WorkingPrecision -> 30]


{ff[2, 1, 1], ff[1, 2, 1], ff[1, 1, 2], ff[2, 2, 2]}

{3.00000000000000000000000000000, 5.00000000000000000000000000000, \
5.00000000000000000000000000000, 12.0000000000000000000000000000}


It is no problem to do this with matrices. Just replace the ^2 postfix operator with the . operator. I do not see a matrix in the given input for a. Therefore I prefered to give an example with scalars from Reals.

f = Experimental`CreateNumericalFunction[{a, x1, x2, y1, y2}, 
  a {x1, x2}.{x1, x2} + {y1, y2}.{y1, 
     y2}, {}, {_Real, _Real, _Real, _Real, _Real}, 
  WorkingPrecision -> 30]

NumericalFunction[{a, x1, x2, y1, y2}, a (x1^2 + x2^2) + y1^2 + y2^2, "-NumericalFunctionData-"]) f[1,1,1,1,1]


Experimental`CreateNumericalFunction works internal, in the function corpus with vector operators, but in the functions head it does not accept others then a list of scalars typed in the elementary Mathematica type like Integers, Reals, Complexes. 

This contains an alternative formulation how to work with Experimental`Numericalfunction. So the functions forth argument, second last defines the type of the input parameters strict.

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

ff["Hessian"[{1, 2}]]
ff["Gradient"[{1, 2}]]
ff["Jacobian"[{1, 2}]]
ff[{1, 0}]

(*{{{-0.14112, -0.14112}, {-0.14112, -0.14112}}, {{4., 2.}, {2., 0.}}} {{-0.989992, -0.989992}, {4., 1.}} {{-0.989992, -0.989992}, {4., 1.}} {0.841471, 0.} *)

If has several options to the symbol making the input precalculated.

This is a very old function and still experimental. Maybe it somehow caused the Standardnamespace Experimental'. It is somewhat of a superfunction. It is used in NMinimize, FindMinimum and alike to speed up.

I recommend NDSolveStateData for the work with NumericalFunction in general as a result, output from Mathematica input.

I am not able to get more information about this 4th parameter of Experimental_CreateNumericalFunction. Because of downward compatibility, the {n} input is valid, more modern appears the list of scalars types corresponding to the number of different input parameters that have to be scalars or flatten if list or array. This is similar to the concepts used in InterpolatingFunction.

To get the form

f = Experimental`CreateNumericalFunction[{a, x, y}, 
  a x.x + y.y, {}, {5}, WorkingPrecision -> 30]

like in

enter image description here

Either the matrix has to be hard coded in the function body or there has to be used a Mathematica internal object in the argument list like NDSolve'$1234. This difference is got already on the output to the creation input. Leave out the forth parameter, the {n}, has only success on this first output either with or without braces around the parameter row.

A nice solution is:

Experimental`CreateNumericalFunction[{#, {}} & /@ vars, funcsExpr, Dimensions[funcsExpr], _Real & /@ vars];


vars = {x};
  • 3
    $\begingroup$ You need to make it accept matrix arguments. $\endgroup$
    – Michael E2
    Aug 22 '20 at 20:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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