25
$\begingroup$

Are there functions, programs or packages that deal with optimizing mathematica code i.e. re-writing ineffecive or convoluted code. -This question has haunted me since I joined SE.

The background for my question being the following: I have noticed that a considerable amount of answers and comments deal with trying to improve a given code sample. It is fascinating to see in how many ways a particular problem can be solved and often there is an unspoken and cheerful competition as who can write the fastest and or shortest code that does the job. - This creates an extremely creative and positive atmosphere here. Given the above, the answer to my question could be no.

However: Since Mathematica must be considered extremely powerful with, patterns, rules and an extensive set of built-in functions and advanced algorithms, it seems quite reasonable to expect that someone has tried to write a code optimizing program, at least on a very basic level. So the answer could be yes.

So to recap: Does there exist any program/function/package that give the user feedback in better or alternative code?

$\endgroup$
  • $\begingroup$ My guess is probably not. A significant obstacle is Mathematica's symbolic/interpreted design, meaning you often cannot tell what code is doing until you evaluate it, and the same block of code might evaluate completely differently depending on the arguments you pass. I suppose you could look for certain types of optimizations, but you would have to make some strong assumptions and the scope would necessarily be quite limited. $\endgroup$ – mfvonh Aug 23 '14 at 16:51
  • $\begingroup$ I'll be thrilled to see a code that can effectively do that. I don't think it is impossible, but provided the different ways a problem can be solved and the potential set of design parameters that can make a solution better than other, I think that it would be better to create a list of community selected core problems and associated solutions with evaluation comments on pertinent design parameters. $\endgroup$ – Ariel Sepulveda Aug 23 '14 at 16:57
  • 4
    $\begingroup$ FWIW, there's Experimental`OptimizeExpression. $\endgroup$ – Michael E2 Aug 23 '14 at 19:53
  • $\begingroup$ @mfvonh It requires a certain amount of ignorance to be able to ask a question like this. Nothing frees you from the shackles of a complex reality as a limited insight in the matter. However the question has always intrigued me and I'm hoping something good will come out of it. $\endgroup$ – MathLind Aug 24 '14 at 6:18
  • 1
    $\begingroup$ There may be some cases where given some Mathematica built-ins F1, F2, F3, ... , FN possibly each with its own arguments, a rule to reduce the composition FN@* ... @*F4@*F3@*F2@*F1 to a single builtin G could be constructed- even in some appropriately general case if you're lucky. I'm pretty comfortable that I'll never need an auto-optimizer, but what would definitely be useful is a code-readability optimizer/beautifier for all those awful huge differential equations we get on here. $\endgroup$ – Histograms May 22 '15 at 18:18
9
$\begingroup$

At the Wolfram Technology Conference 2016 there were two presentations that imply that with the next-generation Wolfram Compiler there will be some limited ability to have code optimize code.

Presentation 1: Replacing your code with a more efficient version

The compiler runs an analysis on your code looking for patterns that can be optimized. For example, the following code squares the elements in a list and then adds 3:

foo=#+3&;
bar=#^2&;
Map[foo, Map[bar, Range@10]]

Because foo and bar have no side effects, the compiler symbolically rewrites the code to use "loop fusion" and expresses line 3 as:

Map[foo@*bar, Range@10]

This new expression is shorter and traverses over the list only once. In the demonstration at the conference they showed that there is intermediate step which contains the loop-fused expression. Thus it would be possible replace your original code with the new optimized expression, which is more effective.

Presentation 2: Deconvoluting Code

There has been significant effort invested in trying to understand if two code segments are equivalent. The presentation talked about an online programming challenge system that evaluated not just test cases, but true code equivalence.

The system the presenter demonstrated used systems of transformation rules to change code into a canonical form that could be compared to a "correct" solution. For example, his system would take either of the following expressions:

Range@10
Table[i,{i,1,10}]

And through rule transformations convert them into the "canonical form"

Table[i,{i,1,10,1}]

Which is equivalent to the previous expressions. Since this technology exists, it would also be possible to reverse the rules, and take the rather convoluted expression

Table[i,{i,1,10,1}]

And apply:

Table[a___,{i_, b_, c_, 1}]->Table[a,{i,b,c}]
Table[i,{i_, 1,  n_,}]->Range[n]

To get the clearer Range[10] function back out.

Conclusion

There are indeed programs that attempt to optimize code and there exists efforts underway that would allow for more advanced code deconvolution. Many of these methods however relied on freedom from 'side effects' common in state-based procedural programming. Furthermore these projects have not yet developed direct/user-friendly interfaces to give the feedback to the user.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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