I have a function that takes lots of optional arguments (basically some design parameters, about 10 of them) and performs some basic algebraic calculations involving complex numbers with the options. The function generally evaluates to a complex number. For example, I have something like:

Options[myfunc] = {param1 -> 1.0, param2 -> 2.0, param3 -> 3.0, etc.};
myfunc[x, opts: OptionsPattern[]] := 
      {var1,var2,  etc.},
      var1 = OptionValue[param1]; etc.
      I*x*var1 + var2 + 1/(var3 + I var4)*... etc.

The function is listable in x and I want to evaluate it as fast as possible for different x's.

I need to use this function in lots of plotting, solving and optimization procedures as part of a Manipulate GUI. The problem is that such procedures takes significant amount of time (~ 10s) which renders the Manipulate GUI unusable.

My question is if I can compile myfunc somehow to speed up the rest of the processing? If not, is there an alternative way to define a function with Options (or optional arguments) to make it compilable? Can I compile the entire Manipulate GUI? If compilation is not possible, any hints about how to define such function in an efficient way?

  • $\begingroup$ Compiled functions do not take options. You can, however, construct a wrapper that takes options and then passes values to a compiled function. $\endgroup$ May 20, 2015 at 7:20
  • $\begingroup$ Thanks, sounds like a good solution... $\endgroup$
    – Bichoy
    May 20, 2015 at 7:21
  • $\begingroup$ I'd suggest introducing the parameters as a vector with an argument for the count of the arguments. Then you can special case inside the compiled code. Or, compile separate functions for different numbers of arguments and have a wrapper that specializes to them from the general case. No time to elaborate now; sorry. $\endgroup$ May 20, 2015 at 8:23
  • $\begingroup$ I'm not sure if this is wholly relevant to the case at hand, but one sample manipulate I have direct from Wolfram takes a slightly different approach. Basically define a single parameter for your function as an association. myFunc[commsAssoc_] within the assoc you have different keys referring to both the data and the parameters, and you control the function processing by writing to the association before calling it, and potentially write the output back before quitting the function. Its not a particularly clean way of doing it but seems to work reasonably well on our large-ish datasets. $\endgroup$ May 20, 2015 at 12:28

1 Answer 1


Without a more complete example of your function all I can offer are bare guidelines to (hopefully) point you in the right direction.

  • The first level is merely syntactical; you would use OptionsPattern, OptionValue etc., in a high level function simply for convenience, but pass all arguments as machine types to an inner compiled function.

  • A second level is to use the high-level function as a selector for multiple compiled functions. Most simply this might be illustrated with a "Method" option which while having a human-readable value name would be used only to choose the appropriate compiled function to which the other arguments are passed.

  • A different level is compilation at the time the high-level function is called based on the option values and other parameters. Possibly memoization would be used to prevent re-compiling the same thing multiple times. One may also want to modularize the code and use CompilationOptions -> {"InlineCompiledFunctions" -> True} as part of this process.

  • $\begingroup$ Thanks a lot, all my optional arguments are just real numbers (they are optional because some designs might have them, some might not ...). I like all your ideas and I will try to see how I can taylor them to fit function. Thanks again. $\endgroup$
    – Bichoy
    May 20, 2015 at 7:24
  • $\begingroup$ @Bichoy You're welcome. If you have trouble applying any of this please edit your question with a minimum-working-example and I shall try to help. $\endgroup$
    – Mr.Wizard
    May 20, 2015 at 7:25

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.