1. Rearranging expressions so that they require the fewest number of operations is generally far less important than to evaluate them so that the calculation is numerically accurate. 2. There are programs that attempt to optimize (rearrange) the evaluation of expressions. In Maxima, there is a command "optimize". Also, most FORTRAN compilers try to do this. 3. There is no uniform procedure that will work in all cases. 4. You could learn a lot about this problem by using Google. 5. Some people consider it impolite to post hundreds of lines of a pretty much random program.