3

I can get NMinimize to work with compiled functions in simple cases.

This works with a constraint:

poly1 = Compile[{{x, _Real}, {y, _Real}}, x^2 - y x + y^4, 
  RuntimeOptions -> {"EvaluateSymbolically" -> False}]

NMinimize[{poly1[x, y], Norm[{x, y}] < 1}, {x, y}] NMinimize[{poly1 @@ {x, y}, Norm[{x, y}] < 1}, {x, y}]

Or, a domain specification:

NMinimize[{poly1 @@ {x, y}}, {x, y} ∈ Disk[{0, 0}, 1]]

This appears to work, but the CompiledFunction warnings makes me think that the compiled function is not being used:

poly2 = Compile[{{x, _Real, 1}}, x[[1]]^2 - x[[2]] x[[1]] + x[[2]]^4, 
  RuntimeOptions -> {"EvaluateSymbolically" -> False}]
NMinimize[poly2[p], p ∈ Vectors[2, Reals]]

Another attempt didn't work at all:

poly3 = FunctionCompile[
  Function[{Typed[p, "NumericArray"::["Real64", 1]]}, 
   p[[1]]^2 - p[[1]] p[[2]] + p[[2]]^2]]

NMinimize[poly3[p], p ∈ Vectors[2, Reals]]

Can anyone see a way to get compiled functions to work with the domain specifications such as Element[p,Disk[]]?

xzczd
  • 65,995
  • 9
  • 163
  • 468
Craig Carter
  • 4,416
  • 16
  • 28
  • NMinimize[poly2[p], p \[Element] Disk[]] seems to work, however it spits out several error messages. I would report this to Wolfram. – Daniel Huber Jul 16 '22 at 11:05
  • 1
    @DanielHuber Issue of poly2 is a duplicate of this post: https://mathematica.stackexchange.com/q/127693/1871 – xzczd Jul 16 '22 at 11:39
  • @xzczd , So in light of Szbolcs's comment at the bottom of mathematica.stackexchange.com/q/127693/1871 , should this considered a bug? I'll report in any case. However, I can't see why it is the same issue as in the linked post. – Craig Carter Jul 16 '22 at 13:55
  • The warning is slightly different (cfsa v.s. cfta), but the behavior is the same: a warning pops up, but it's merely a warning, the CompiledFunction[…][…] is still unevaluated. (Just observe the output of poly2[{a, b}], "EvaluateSymbolically" -> False does its work. ) I'm not sure if this should be considered a bug, but I agree this is worth reporting. – xzczd Jul 16 '22 at 15:27
  • @xzczd I reported it. Thanks, – Craig Carter Jul 16 '22 at 16:24

0 Answers0