I am trying to use a compiled function in order to save time...

LegrendeTransform = Compile[{y},  MaxValue[Sin[x] - x *y  , x]]

Now I want to to evaluate and plot:

Plot[{LegrendeTransform[y]}, {y, 0, 1}]

But I get this error:

CompiledFunction::cfse: Compiled expression 0.9995462353179709` should be a machine-size integer. >>
CompiledFunction::cfex: Could not complete external evaluation at instruction 1; proceeding with uncompiled evaluation. >>

Could you explain the reason for this result? The number 0.9995462353179709 is so similar to 1 that I would have no problem if Mathematica decides to approximate it with an integer....


  • $\begingroup$ How about Compile[{{y, _Real}}, __]. $\endgroup$
    – Greg Hurst
    Commented Nov 5, 2013 at 20:53
  • $\begingroup$ This question can not be answered completely without additional information. One thing is certain: MaxValue cannot be compiled down, so the whole exercise is likely to be doomed to fail. $\endgroup$
    – halirutan
    Commented Nov 6, 2013 at 0:23
  • $\begingroup$ @RiemannZeta Compile[{{y, _Real}}, __] I get the same error $\endgroup$
    – altroware
    Commented Nov 6, 2013 at 10:28
  • $\begingroup$ @halirutan . I saw that the problem occurs with a generic function (and edited the question accordingly). The point is that I can not compile any MaxValue function. Could you explain me why, please? Is there another method to make the function faster? Thanks. $\endgroup$
    – altroware
    Commented Nov 6, 2013 at 10:30
  • $\begingroup$ @altroware I voted to reopen your question since it is now self-contained. I give you an answer after it is open again. $\endgroup$
    – halirutan
    Commented Nov 6, 2013 at 10:41

1 Answer 1


You get this error message because the compiler cannot deduce the type of the MaxValue expression. Therefore, it assumes that it is an integer and it gives a message, when a real number is returned. The solution is simple: Tell the compiler that the expression involving MaxValue is of type _Real

LegrendeTransform = Compile[{y},
  MaxValue[Sin[x] - x*y, x], {{MaxValue[_, _], _Real}}];


(* 0.342427 *)

This doesn't solve the underlying problem that MaxValue cannot be compiled. Please see this post for a list of compilable functions. In general, algorithms which involve algebraic (opposed to numeric) calculations are very unlikely to be compilable.


I understood that this function is not "compilable" as it is algebraic, but it seems that with your modification MaxValue is compiled. What does this mean?

It does mean that although you can create a compiled function, your code or not all of your code runs in compiled form. You can use CompilePrint to check this. In the following you see, that in the compiled function nothing happens except that your parameter R0 is assigned. The whole work is done by a MainEvaluated call. This means the compiled function leaves its (fast) environment and asks the kernel (just as you would do in the notebook) for the answer. It gets the answer back and gives it then to you. Therefore, this is without doubt a way to make your computation slower

<< CompiledFunctionTools`

(* "
        1 argument
        2 Real registers
        Underflow checking off
        Overflow checking off
        Integer overflow checking on
        RuntimeAttributes -> {}

        R0 = A1
        Result = R1

1   R1 = MainEvaluate[ Function[{y}, MaxValue[Sin[x] - x y, x]][ R0]]
2   Return
" *)
  • $\begingroup$ Thank you for the answer. Actually I get also this warning: LegrendeTransform[.7] During evaluation of In[389]:= NMaximize::cvdiv: Failed to converge to a solution. The function may be unbounded. >> Out[389]= 16062.5 Perhaps this is related to a different problem. I understood that this function is not "compilable" as it is algebraic, but it seems that with your modification MaxValue is compiled. What does this mean? $\endgroup$
    – altroware
    Commented Nov 7, 2013 at 21:16
  • $\begingroup$ @altroware See my update in the answer. Your other error is related to MaxValue itself and you should get it even if you call the uncompiled function in the notebook. This error comes from the form of your target-function you want to maximize. $\endgroup$
    – halirutan
    Commented Nov 8, 2013 at 7:46
  • $\begingroup$ Isn't MaxValue[Sin[x] - x *y , x] unbounded for any finite real y? $\endgroup$
    – Ymareth
    Commented Nov 8, 2013 at 9:27
  • $\begingroup$ @Ymareth Yes, for all y!=0. The Sin was a badly chosen toy-example, since the OP's real function was probably too complex to post. $\endgroup$
    – halirutan
    Commented Nov 8, 2013 at 9:35
  • $\begingroup$ @Ymareth Ops sorry... this in fact detected by Mathematica (see my first comment). $\endgroup$
    – altroware
    Commented Nov 8, 2013 at 15:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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