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.
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.
A simple expression specifies a column, pseudocolumn, constant, sequence number, or null.
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.
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With