|
Some types of expressions can be reorganized in such a way so as to improve, or otherwise optimize, the way in which they evaluate.
The package Optimize.m implements a command called Optimize to automate a basic type of syntactic optimization of expressions, and is primarily intended for use with writing Compile functions.
Here are two examples: f[x_Real] := Optimize[Sin[x] + Cos[Sin[x]]]; and g = Compile[x, Optimize[Sin[x] + Cos[Sin[x]]]];.
In each case Optimize evaluates its argument and optimizes the expression Sin[x] + Cos[Sin[x]] by replacing it with a Block which, for this expression, avoids evaluating Sin[x] more than once.
In 19 lines, the definition of the Optimize[expr_] function manages to use no less that 28 diverse Mathematica functions including: Append, Apply, Count, DeleteCases, Depth, Drop, First, Flatten, Fold, FoldList, Last, Length, Level, List, Literal, Map, MapIndexed, Partition, Position, ReplaceAll, Reverse, Rule, Select, Table, Take, Transpose, and Union.
And it all does something very useful.
|
|