5

I would like to pass a CompiledFunction to a FindRoot algorithm and therefore I use the option RuntimeOptions -> "EvaluateSymbolically" -> False. For example:

ff = Compile[x, x, RuntimeOptions -> "EvaluateSymbolically" -> False]

As expected and as demanded, the function does not evaluate when passing a non-numerical argument:

ff[a]

CompiledFunction[..][a]

However, when passing "1-a" the following warning appears:

ff[1 - a]

CompiledFunction::cfsa: Argument 1-a at position 1 should be a machine-size real number. >>

My question: Does MMA now use an uncompiled evaluation when returning:

ff[1-a]/.a->2

Unfortunately, I cannot relabel variables in my setup, due to the large number of occurrences of 1-a

Michael E2
  • 235,386
  • 17
  • 334
  • 747
Breugem
  • 785
  • 3
  • 12
  • 2
    I'm inclined to say that the warning CompiledFunction::cfsa is unintended, i.e. it's a bug. Since ff[1-a] still returns a CompiledFunction object, f[1-a]/.a->2 should evaluate using compiled code. – QuantumDot Oct 01 '16 at 12:25

1 Answers1

6

This is just a warning message, which is issued because 1 - a is not a variable (it is a normal expression with head Plus) and can be suppressed e.g. by setting "WarningMessages" -> False.

Uncompiled evaluation does not take place in

ff[1 - a] /. a -> 2

which can be checked by setting the "RuntimeErrorHandler" option.

ilian
  • 25,474
  • 4
  • 117
  • 186
  • +1, but it's not particularly obvious what counts as a "variable": ff[Sin[x]], ff[BesselJ[0, x]], ff[Sin[-x]], ff[BesselJ[1/2, x]] -- the latter two giving warnings, presumably because they have the head Times (after evaluating). It seems to be whatever expr is accepted by D in D[1, expr]. (Silly example: D[1, Plot[x, {x, 0, 1}]].) – Michael E2 Oct 01 '16 at 17:29
  • 1
    @MichaelE2 Yes, D (and several other functions) use the same test to determine what might be a variable. It is a heuristic that explicitly forbids several common heads (including List, Plus, Times, Equal, And, ConditionalExpression etc.) but anything else is let through, and no, it hasn't been taught to care about Sin, BesselJ or Plot. – ilian Oct 02 '16 at 16:14