Mathematica 9 is now available

Wolfram Library Archive

All Collections Articles Books Conference Proceedings
Courseware Demos MathSource Technical Notes
Title Downloads

Optimizing Expressions

Terry Robb
Organization: RMIT University
Department: Department of Mathematics
Old MathSource #

Revision date


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.

*Mathematica Technology > Programming
Downloads Download Wolfram CDF Player

Optimize.txt (4.3 KB) - Plain-text documentation
Optimize.m (7.5 KB) - Mathematica package
Optimize.nb (42.6 KB) - Example notebook

Files specific to Mathematica 2.2 version:
Download (23 KB) - Example notebook