Does mathematics call Neimization symbols, not numbers?

I noticed the following behavior when using NMinimize in Mathematica. The first call to the objective function is the names of variables, not points from space, as you would expect.

So, for example, if my target function is a module, this module is called only once, evaluated symbolically, and then in further iterations, this symbolic expression is evaluated with points from the variable space.

This behavior can significantly slow down the calculation for a large expression. Is there any way around this? Has anyone else experienced this? Is there a way to speed up NMinimize then?

Example:

 dummy[x_] := Module[ {}, Print["x=", x ]; 4 x^4 - 4 x^2 + 1 ] In: NMinimize[dummy[x], x] Out:x=x {0., {x -> 0.707107}} 
+6
source share
2 answers

Did you try to define your function only for calculating numerical input?

 dummy[x_?NumericQ] := ... 
+3
source

For some dummy functions, an โ€œexact numericalโ€ call can also be very slow. The example of finding FixedPoint[Sqrt,2.] FixedPoint[Sqrt,2] fast, but FixedPoint[Sqrt,2] will work until something breaks!

By "exact number" I mean things like Integers , Rationals and numeric characters like Sqrt[2] , Cos[5] , Pi , EulerGamma , etc.

that is, things that return a numerical value under the action of N[] .

In this case, it is probably better to use

 dummy[_?InexactNumberQ] := .... 

or even

 dummy[_?MachineNumberQ] := .... 
+2
source

Source: https://habr.com/ru/post/886833/


All Articles