Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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....


share|improve this question
How about Compile[{{y, _Real}}, __]. – Chip Hurst Nov 5 '13 at 20:53
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. – halirutan Nov 6 '13 at 0:23
@RiemannZeta Compile[{{y, _Real}}, __] I get the same error – altroware Nov 6 '13 at 10:28
@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. – altroware Nov 6 '13 at 10:30
@altroware I voted to reopen your question since it is now self-contained. I give you an answer after it is open again. – halirutan Nov 6 '13 at 10:41
up vote 8 down vote accepted

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
" *)
share|improve this answer
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? – altroware Nov 7 '13 at 21:16
@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. – halirutan Nov 8 '13 at 7:46
Isn't MaxValue[Sin[x] - x *y , x] unbounded for any finite real y? – Ymareth Nov 8 '13 at 9:27
@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. – halirutan Nov 8 '13 at 9:35
@Ymareth Ops sorry... this in fact detected by Mathematica (see my first comment). – altroware Nov 8 '13 at 15:17

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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