Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reduce the number of operations on a simple expression

Lets say I take a computation that involves only addition and multiplication:

(a+b)*(c+d)

which can be done in many other ways, eg.

a*(c+d) + b*(c+d)
a*c + a*d + b*c + b*d

In terms of additions and multiplications the number of operations required for each of the three examples shown are (2,1) (3,2) (3,4) respectively. Clearly, if the goal is to reduce the total number of operations the first is superior. Is there a way, given an arbitrary expression to find the computation order that requires the least number of operations?

Note: This question is being re-asked from SE.math for the insight and perspective of the CS crowd.

like image 991
Hooked Avatar asked Dec 07 '10 21:12

Hooked


People also ask

How do you simplify order of operations?

Explain to students that they have developed a set of steps called the order of operations. When simplifying, do all expressions inside parentheses first, then all exponents, then all multiplication and division operations from left to right, and finally all addition and subtraction operations from left to right.

What is a simple expression?

A simple expression specifies a column, pseudocolumn, constant, sequence number, or null.

How do you reduce a number?

You can take any number, regardless of size, and reduce it down to a single digit from 1 to 9. Let us take the year 1968 as an example. To reduce it to it's single digit we must add together each of its 4 numerals. So by adding together 1 + 9 + 6 + 8 we get 24.

What are operations in an expression?

An operational expression consists of one or more single operations. A single operation is either a prefix operation (an operator preceding a single operand) or an infix operation (an operator between two operands).


1 Answers

What you want is to effectively generate all possible equivalent algebraic expressions, and choose the one that takes the least costly number of steps (adding X three times is cheaper on most machines than multiplying X by 3).

It is impractical to do this, as the number of "equivalent" formulas is infinite.

However, Pelegrí-Llopart figured out a scheme to generate optimal code given a fixed number of algebraic rewrite rules, called "BURS" (bottom-up rewrite system). This has been implemented in some code generators.

In essence, he constructs offline a big automata whose states track the set of possible applied rewrites. Each state knows what to generate when it occurs, so the online time for code generation is cheap.

like image 83
Ira Baxter Avatar answered Oct 20 '22 09:10

Ira Baxter